Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (5.9 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 710b1c43 Christos Stavrakakis
from django.conf import settings
35 cc7c0f44 Christos Stavrakakis
from synnefo.db.models import Backend
36 710b1c43 Christos Stavrakakis
from synnefo.logic import backend as backend_mod
37 710b1c43 Christos Stavrakakis
from synnefo.api import util
38 cc7c0f44 Christos Stavrakakis
39 47d1d754 Christos Stavrakakis
log = logging.getLogger(__name__)
40 47d1d754 Christos Stavrakakis
41 cc7c0f44 Christos Stavrakakis
42 cc7c0f44 Christos Stavrakakis
class BackendAllocator():
43 cc7c0f44 Christos Stavrakakis
    """Wrapper class for instance allocation.
44 cc7c0f44 Christos Stavrakakis

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

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

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

97 f15bf3d9 Christos Stavrakakis
    The list contains the backends that are online and that have enabled
98 f15bf3d9 Christos Stavrakakis
    the disk_template of the new VM.
99 f15bf3d9 Christos Stavrakakis

100 f15bf3d9 Christos Stavrakakis
    Also, if the new VM will be automatically connected to a public network,
101 f15bf3d9 Christos Stavrakakis
    the backends that do not have an available public IPv4 address are
102 f15bf3d9 Christos Stavrakakis
    excluded.
103 cc7c0f44 Christos Stavrakakis

104 cc7c0f44 Christos Stavrakakis
    """
105 5d1f6b84 Christos Stavrakakis
    disk_template = flavor.disk_template
106 5d1f6b84 Christos Stavrakakis
    # Ganeti knows only the 'ext' disk template, but the flavors disk template
107 5d1f6b84 Christos Stavrakakis
    # includes the provider.
108 5d1f6b84 Christos Stavrakakis
    if disk_template.startswith("ext_"):
109 5d1f6b84 Christos Stavrakakis
        disk_template = "ext"
110 5d1f6b84 Christos Stavrakakis
111 f15bf3d9 Christos Stavrakakis
    backends = Backend.objects.select_for_update()
112 f15bf3d9 Christos Stavrakakis
    backends = backends.filter(offline=False, drained=False,
113 5d1f6b84 Christos Stavrakakis
                               disk_templates__contains=disk_template)
114 f15bf3d9 Christos Stavrakakis
    backends = list(backends)
115 710b1c43 Christos Stavrakakis
    if "SNF:ANY_PUBLIC" in settings.DEFAULT_INSTANCE_NETWORKS:
116 710b1c43 Christos Stavrakakis
        backends = filter(lambda x: util.backend_has_free_public_ip(x),
117 710b1c43 Christos Stavrakakis
                          backends)
118 8f2aa316 Christos Stavrakakis
    return backends
119 f96f60fd Christos Stavrakakis
120 f96f60fd Christos Stavrakakis
121 cc7c0f44 Christos Stavrakakis
def flavor_disk(flavor):
122 cc7c0f44 Christos Stavrakakis
    """ Get flavor's 'real' disk size
123 cc7c0f44 Christos Stavrakakis

124 cc7c0f44 Christos Stavrakakis
    """
125 cc7c0f44 Christos Stavrakakis
    if flavor.disk_template == 'drbd':
126 cc7c0f44 Christos Stavrakakis
        return flavor.disk * 1024 * 2
127 cc7c0f44 Christos Stavrakakis
    else:
128 cc7c0f44 Christos Stavrakakis
        return flavor.disk * 1024
129 cc7c0f44 Christos Stavrakakis
130 cc7c0f44 Christos Stavrakakis
131 cc7c0f44 Christos Stavrakakis
def reduce_backend_resources(backend, vm):
132 cc7c0f44 Christos Stavrakakis
    """ Conservatively update the resources of a backend.
133 cc7c0f44 Christos Stavrakakis

134 cc7c0f44 Christos Stavrakakis
    Reduce the free resources of the backend by the size of the of the vm that
135 cc7c0f44 Christos Stavrakakis
    will host. This is an underestimation of the backend capabilities.
136 cc7c0f44 Christos Stavrakakis

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

151 cc7c0f44 Christos Stavrakakis
    Set db backend state to the actual state of the backend, if
152 cc7c0f44 Christos Stavrakakis
    BACKEND_REFRESH_MIN time has passed.
153 cc7c0f44 Christos Stavrakakis

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