Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / logic / backend_allocator.py @ 1ad47ca5

History | View | Annotate | Download (6.2 kB)

1 cc7c0f44 Christos Stavrakakis
# Copyright 2011 GRNET S.A. All rights reserved.
2 cc7c0f44 Christos Stavrakakis
#
3 cc7c0f44 Christos Stavrakakis
# Redistribution and use in source and binary forms, with or without
4 cc7c0f44 Christos Stavrakakis
# modification, are permitted provided that the following conditions
5 cc7c0f44 Christos Stavrakakis
# are met:
6 cc7c0f44 Christos Stavrakakis
#
7 cc7c0f44 Christos Stavrakakis
#   1. Redistributions of source code must retain the above copyright
8 cc7c0f44 Christos Stavrakakis
#      notice, this list of conditions and the following disclaimer.
9 cc7c0f44 Christos Stavrakakis
#
10 cc7c0f44 Christos Stavrakakis
#  2. Redistributions in binary form must reproduce the above copyright
11 cc7c0f44 Christos Stavrakakis
#     notice, this list of conditions and the following disclaimer in the
12 cc7c0f44 Christos Stavrakakis
#     documentation and/or other materials provided with the distribution.
13 cc7c0f44 Christos Stavrakakis
#
14 cc7c0f44 Christos Stavrakakis
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15 cc7c0f44 Christos Stavrakakis
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 cc7c0f44 Christos Stavrakakis
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 cc7c0f44 Christos Stavrakakis
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18 cc7c0f44 Christos Stavrakakis
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 cc7c0f44 Christos Stavrakakis
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 cc7c0f44 Christos Stavrakakis
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 cc7c0f44 Christos Stavrakakis
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 cc7c0f44 Christos Stavrakakis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 cc7c0f44 Christos Stavrakakis
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 cc7c0f44 Christos Stavrakakis
# SUCH DAMAGE.
25 cc7c0f44 Christos Stavrakakis
#
26 cc7c0f44 Christos Stavrakakis
# The views and conclusions contained in the software and documentation are
27 cc7c0f44 Christos Stavrakakis
# those of the authors and should not be interpreted as representing official
28 cc7c0f44 Christos Stavrakakis
# policies, either expressed or implied, of GRNET S.A.
29 cc7c0f44 Christos Stavrakakis
30 47d1d754 Christos Stavrakakis
import logging
31 cc7c0f44 Christos Stavrakakis
import datetime
32 cc7c0f44 Christos Stavrakakis
from django.utils import importlib
33 cc7c0f44 Christos Stavrakakis
34 5dd9d123 Christos Stavrakakis
from synnefo.settings import (BACKEND_ALLOCATOR_MODULE, BACKEND_REFRESH_MIN,
35 776d2a3f Christos Stavrakakis
                              BACKEND_PER_USER, ARCHIPELAGO_BACKENDS)
36 cc7c0f44 Christos Stavrakakis
from synnefo.db.models import Backend
37 cc7c0f44 Christos Stavrakakis
from synnefo.logic.backend import update_resources
38 f96f60fd Christos Stavrakakis
from synnefo.api.util import backend_public_networks
39 cc7c0f44 Christos Stavrakakis
40 47d1d754 Christos Stavrakakis
log = logging.getLogger(__name__)
41 47d1d754 Christos Stavrakakis
42 cc7c0f44 Christos Stavrakakis
43 cc7c0f44 Christos Stavrakakis
class BackendAllocator():
44 cc7c0f44 Christos Stavrakakis
    """Wrapper class for instance allocation.
45 cc7c0f44 Christos Stavrakakis

46 cc7c0f44 Christos Stavrakakis
    """
47 cc7c0f44 Christos Stavrakakis
    def __init__(self):
48 cc7c0f44 Christos Stavrakakis
        self.strategy_mod =\
49 5dd9d123 Christos Stavrakakis
            importlib.import_module(BACKEND_ALLOCATOR_MODULE)
50 cc7c0f44 Christos Stavrakakis
51 5dd9d123 Christos Stavrakakis
    def allocate(self, userid, flavor):
52 adc46059 Christos Stavrakakis
        """Allocate a vm of the specified flavor to a backend.
53 adc46059 Christos Stavrakakis

54 adc46059 Christos Stavrakakis
        Warning!!: An explicit commit is required after calling this function,
55 adc46059 Christos Stavrakakis
        in order to release the locks acquired by the get_available_backends
56 adc46059 Christos Stavrakakis
        function.
57 adc46059 Christos Stavrakakis

58 adc46059 Christos Stavrakakis
        """
59 5dd9d123 Christos Stavrakakis
60 5dd9d123 Christos Stavrakakis
        backend = None
61 5dd9d123 Christos Stavrakakis
        backend = get_backend_for_user(userid)
62 5dd9d123 Christos Stavrakakis
        if backend:
63 5dd9d123 Christos Stavrakakis
            return backend
64 5dd9d123 Christos Stavrakakis
65 cc7c0f44 Christos Stavrakakis
        # Get the size of the vm
66 cc7c0f44 Christos Stavrakakis
        disk = flavor_disk(flavor)
67 cc7c0f44 Christos Stavrakakis
        ram = flavor.ram
68 cc7c0f44 Christos Stavrakakis
        cpu = flavor.cpu
69 cc7c0f44 Christos Stavrakakis
        vm = {'ram': ram, 'disk': disk, 'cpu': cpu}
70 cc7c0f44 Christos Stavrakakis
71 47d1d754 Christos Stavrakakis
        log.debug("Allocating VM: %r", vm)
72 47d1d754 Christos Stavrakakis
73 cc7c0f44 Christos Stavrakakis
        # Get available backends
74 cc7c0f44 Christos Stavrakakis
        available_backends = get_available_backends()
75 cc7c0f44 Christos Stavrakakis
76 2e1e6844 Christos Stavrakakis
        # Temporary fix for distinquishing archipelagos capable backends
77 2e1e6844 Christos Stavrakakis
        available_backends = filter_archipelagos_backends(available_backends,
78 2e1e6844 Christos Stavrakakis
                                                          flavor.disk_template)
79 9cf4a3c0 Christos Stavrakakis
        # Refresh backends, if needed
80 9cf4a3c0 Christos Stavrakakis
        refresh_backends_stats(available_backends)
81 9cf4a3c0 Christos Stavrakakis
82 cc7c0f44 Christos Stavrakakis
        if not available_backends:
83 cc7c0f44 Christos Stavrakakis
            return None
84 cc7c0f44 Christos Stavrakakis
85 cc7c0f44 Christos Stavrakakis
        # Find the best backend to host the vm, based on the allocation
86 cc7c0f44 Christos Stavrakakis
        # strategy
87 7fede91e Christos Stavrakakis
        backend = self.strategy_mod.allocate(available_backends, vm)
88 cc7c0f44 Christos Stavrakakis
89 47d1d754 Christos Stavrakakis
        log.info("Allocated VM %r, in backend %s", vm, backend)
90 47d1d754 Christos Stavrakakis
91 cc7c0f44 Christos Stavrakakis
        # Reduce the free resources of the selected backend by the size of
92 cc7c0f44 Christos Stavrakakis
        # the vm
93 cc7c0f44 Christos Stavrakakis
        reduce_backend_resources(backend, vm)
94 cc7c0f44 Christos Stavrakakis
95 cc7c0f44 Christos Stavrakakis
        return backend
96 cc7c0f44 Christos Stavrakakis
97 cc7c0f44 Christos Stavrakakis
98 cc7c0f44 Christos Stavrakakis
def get_available_backends():
99 cc7c0f44 Christos Stavrakakis
    """Get available backends from db.
100 cc7c0f44 Christos Stavrakakis

101 cc7c0f44 Christos Stavrakakis
    """
102 f96f60fd Christos Stavrakakis
    backends = list(Backend.objects.select_for_update().filter(drained=False,
103 f96f60fd Christos Stavrakakis
                                                               offline=False))
104 f96f60fd Christos Stavrakakis
    return filter(lambda x: has_free_ip(x), backends)
105 f96f60fd Christos Stavrakakis
106 f96f60fd Christos Stavrakakis
107 2e1e6844 Christos Stavrakakis
def filter_archipelagos_backends(available_backends, disk_template):
108 2e1e6844 Christos Stavrakakis
    if disk_template.startswith("ext_"):
109 776d2a3f Christos Stavrakakis
        available_backends = filter(lambda x: x.id in ARCHIPELAGO_BACKENDS,
110 2e1e6844 Christos Stavrakakis
                                    available_backends)
111 2e1e6844 Christos Stavrakakis
    else:
112 776d2a3f Christos Stavrakakis
        available_backends = filter(lambda x: x.id not in ARCHIPELAGO_BACKENDS,
113 2e1e6844 Christos Stavrakakis
                                    available_backends)
114 2e1e6844 Christos Stavrakakis
    return available_backends
115 2e1e6844 Christos Stavrakakis
116 2e1e6844 Christos Stavrakakis
117 f96f60fd Christos Stavrakakis
def has_free_ip(backend):
118 f96f60fd Christos Stavrakakis
    """Find if Backend has any free public IP."""
119 f96f60fd Christos Stavrakakis
    for network in backend_public_networks(backend):
120 f96f60fd Christos Stavrakakis
        if not network.get_pool().empty():
121 f96f60fd Christos Stavrakakis
            return True
122 cab31c74 Christos Stavrakakis
    log.warning("No available network in backend %r", backend)
123 f96f60fd Christos Stavrakakis
    return False
124 cc7c0f44 Christos Stavrakakis
125 cc7c0f44 Christos Stavrakakis
126 cc7c0f44 Christos Stavrakakis
def flavor_disk(flavor):
127 cc7c0f44 Christos Stavrakakis
    """ Get flavor's 'real' disk size
128 cc7c0f44 Christos Stavrakakis

129 cc7c0f44 Christos Stavrakakis
    """
130 cc7c0f44 Christos Stavrakakis
    if flavor.disk_template == 'drbd':
131 cc7c0f44 Christos Stavrakakis
        return flavor.disk * 1024 * 2
132 cc7c0f44 Christos Stavrakakis
    else:
133 cc7c0f44 Christos Stavrakakis
        return flavor.disk * 1024
134 cc7c0f44 Christos Stavrakakis
135 cc7c0f44 Christos Stavrakakis
136 cc7c0f44 Christos Stavrakakis
def reduce_backend_resources(backend, vm):
137 cc7c0f44 Christos Stavrakakis
    """ Conservatively update the resources of a backend.
138 cc7c0f44 Christos Stavrakakis

139 cc7c0f44 Christos Stavrakakis
    Reduce the free resources of the backend by the size of the of the vm that
140 cc7c0f44 Christos Stavrakakis
    will host. This is an underestimation of the backend capabilities.
141 cc7c0f44 Christos Stavrakakis

142 cc7c0f44 Christos Stavrakakis
    """
143 cc7c0f44 Christos Stavrakakis
144 cc7c0f44 Christos Stavrakakis
    new_mfree = backend.mfree - vm['ram']
145 cc7c0f44 Christos Stavrakakis
    new_dfree = backend.dfree - vm['disk']
146 cc7c0f44 Christos Stavrakakis
    backend.mfree = 0 if new_mfree < 0 else new_mfree
147 cc7c0f44 Christos Stavrakakis
    backend.dfree = 0 if new_dfree < 0 else new_dfree
148 cc7c0f44 Christos Stavrakakis
    backend.pinst_cnt += 1
149 cc7c0f44 Christos Stavrakakis
150 cc7c0f44 Christos Stavrakakis
    backend.save()
151 cc7c0f44 Christos Stavrakakis
152 cc7c0f44 Christos Stavrakakis
153 9cf4a3c0 Christos Stavrakakis
def refresh_backends_stats(backends):
154 cc7c0f44 Christos Stavrakakis
    """ Refresh the statistics of the backends.
155 cc7c0f44 Christos Stavrakakis

156 cc7c0f44 Christos Stavrakakis
    Set db backend state to the actual state of the backend, if
157 cc7c0f44 Christos Stavrakakis
    BACKEND_REFRESH_MIN time has passed.
158 cc7c0f44 Christos Stavrakakis

159 cc7c0f44 Christos Stavrakakis
    """
160 cc7c0f44 Christos Stavrakakis
161 cc7c0f44 Christos Stavrakakis
    now = datetime.datetime.now()
162 5dd9d123 Christos Stavrakakis
    delta = datetime.timedelta(minutes=BACKEND_REFRESH_MIN)
163 9cf4a3c0 Christos Stavrakakis
    for b in backends:
164 cc7c0f44 Christos Stavrakakis
        if now > b.updated + delta:
165 9cf4a3c0 Christos Stavrakakis
            log.debug("Updating resources of backend %r. Last Updated %r",
166 9cf4a3c0 Christos Stavrakakis
                      b, b.updated)
167 cc7c0f44 Christos Stavrakakis
            update_resources(b)
168 5dd9d123 Christos Stavrakakis
169 5dd9d123 Christos Stavrakakis
170 5dd9d123 Christos Stavrakakis
def get_backend_for_user(userid):
171 5dd9d123 Christos Stavrakakis
    """Find fixed Backend for user based on BACKEND_PER_USER setting."""
172 5dd9d123 Christos Stavrakakis
173 5dd9d123 Christos Stavrakakis
    backend = BACKEND_PER_USER.get(userid)
174 5dd9d123 Christos Stavrakakis
175 5dd9d123 Christos Stavrakakis
    if not backend:
176 5dd9d123 Christos Stavrakakis
        return None
177 5dd9d123 Christos Stavrakakis
178 5dd9d123 Christos Stavrakakis
    try:
179 5dd9d123 Christos Stavrakakis
        try:
180 5dd9d123 Christos Stavrakakis
            backend_id = int(backend)
181 5dd9d123 Christos Stavrakakis
            return Backend.objects.get(id=backend_id)
182 5dd9d123 Christos Stavrakakis
        except ValueError:
183 5dd9d123 Christos Stavrakakis
            pass
184 5dd9d123 Christos Stavrakakis
185 5dd9d123 Christos Stavrakakis
        backend_name = str(backend)
186 5dd9d123 Christos Stavrakakis
        return Backend.objects.get(clustername=backend_name)
187 5dd9d123 Christos Stavrakakis
    except Backend.DoesNotExist:
188 5dd9d123 Christos Stavrakakis
        log.error("Invalid backend %s for user %s", backend, userid)