root / snf-cyclades-app / synnefo / logic / backend_allocator.py @ d7841399
History | View | Annotate | Download (5.5 kB)
1 |
# Copyright 2011 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or without
|
4 |
# modification, are permitted provided that the following conditions
|
5 |
# are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above copyright
|
8 |
# notice, this list of conditions and the following disclaimer.
|
9 |
#
|
10 |
# 2. Redistributions in binary form must reproduce the above copyright
|
11 |
# notice, this list of conditions and the following disclaimer in the
|
12 |
# documentation and/or other materials provided with the distribution.
|
13 |
#
|
14 |
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
15 |
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
16 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
17 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
18 |
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
19 |
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
20 |
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
21 |
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
22 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
23 |
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
24 |
# SUCH DAMAGE.
|
25 |
#
|
26 |
# The views and conclusions contained in the software and documentation are
|
27 |
# those of the authors and should not be interpreted as representing official
|
28 |
# policies, either expressed or implied, of GRNET S.A.
|
29 |
|
30 |
import logging |
31 |
import datetime |
32 |
from django.utils import importlib |
33 |
|
34 |
from synnefo.settings import (BACKEND_ALLOCATOR_MODULE, BACKEND_REFRESH_MIN, |
35 |
BACKEND_PER_USER) |
36 |
from synnefo.db.models import Backend |
37 |
from synnefo.logic.backend import update_resources |
38 |
from synnefo.api.util import backend_public_networks |
39 |
|
40 |
log = logging.getLogger(__name__) |
41 |
|
42 |
|
43 |
class BackendAllocator(): |
44 |
"""Wrapper class for instance allocation.
|
45 |
|
46 |
"""
|
47 |
def __init__(self): |
48 |
self.strategy_mod =\
|
49 |
importlib.import_module(BACKEND_ALLOCATOR_MODULE) |
50 |
|
51 |
def allocate(self, userid, flavor): |
52 |
"""Allocate a vm of the specified flavor to a backend.
|
53 |
|
54 |
Warning!!: An explicit commit is required after calling this function,
|
55 |
in order to release the locks acquired by the get_available_backends
|
56 |
function.
|
57 |
|
58 |
"""
|
59 |
|
60 |
backend = None
|
61 |
backend = get_backend_for_user(userid) |
62 |
if backend:
|
63 |
return backend
|
64 |
|
65 |
# Get the size of the vm
|
66 |
disk = flavor_disk(flavor) |
67 |
ram = flavor.ram |
68 |
cpu = flavor.cpu |
69 |
vm = {'ram': ram, 'disk': disk, 'cpu': cpu} |
70 |
|
71 |
log.debug("Allocating VM: %r", vm)
|
72 |
|
73 |
# Get available backends
|
74 |
available_backends = get_available_backends() |
75 |
|
76 |
# Refresh backends, if needed
|
77 |
refresh_backends_stats(available_backends) |
78 |
|
79 |
if not available_backends: |
80 |
return None |
81 |
|
82 |
# Find the best backend to host the vm, based on the allocation
|
83 |
# strategy
|
84 |
backend = self.strategy_mod.allocate(available_backends, vm)
|
85 |
|
86 |
log.info("Allocated VM %r, in backend %s", vm, backend)
|
87 |
|
88 |
# Reduce the free resources of the selected backend by the size of
|
89 |
# the vm
|
90 |
reduce_backend_resources(backend, vm) |
91 |
|
92 |
return backend
|
93 |
|
94 |
|
95 |
def get_available_backends(): |
96 |
"""Get available backends from db.
|
97 |
|
98 |
"""
|
99 |
backends = list(Backend.objects.select_for_update().filter(drained=False, |
100 |
offline=False))
|
101 |
return filter(lambda x: has_free_ip(x), backends) |
102 |
|
103 |
|
104 |
def has_free_ip(backend): |
105 |
"""Find if Backend has any free public IP."""
|
106 |
for network in backend_public_networks(backend): |
107 |
if not network.get_pool().empty(): |
108 |
return True |
109 |
return False |
110 |
|
111 |
|
112 |
def flavor_disk(flavor): |
113 |
""" Get flavor's 'real' disk size
|
114 |
|
115 |
"""
|
116 |
if flavor.disk_template == 'drbd': |
117 |
return flavor.disk * 1024 * 2 |
118 |
else:
|
119 |
return flavor.disk * 1024 |
120 |
|
121 |
|
122 |
def reduce_backend_resources(backend, vm): |
123 |
""" Conservatively update the resources of a backend.
|
124 |
|
125 |
Reduce the free resources of the backend by the size of the of the vm that
|
126 |
will host. This is an underestimation of the backend capabilities.
|
127 |
|
128 |
"""
|
129 |
|
130 |
new_mfree = backend.mfree - vm['ram']
|
131 |
new_dfree = backend.dfree - vm['disk']
|
132 |
backend.mfree = 0 if new_mfree < 0 else new_mfree |
133 |
backend.dfree = 0 if new_dfree < 0 else new_dfree |
134 |
backend.pinst_cnt += 1
|
135 |
|
136 |
backend.save() |
137 |
|
138 |
|
139 |
def refresh_backends_stats(backends): |
140 |
""" Refresh the statistics of the backends.
|
141 |
|
142 |
Set db backend state to the actual state of the backend, if
|
143 |
BACKEND_REFRESH_MIN time has passed.
|
144 |
|
145 |
"""
|
146 |
|
147 |
now = datetime.datetime.now() |
148 |
delta = datetime.timedelta(minutes=BACKEND_REFRESH_MIN) |
149 |
for b in backends: |
150 |
if now > b.updated + delta:
|
151 |
log.debug("Updating resources of backend %r. Last Updated %r",
|
152 |
b, b.updated) |
153 |
update_resources(b) |
154 |
|
155 |
|
156 |
def get_backend_for_user(userid): |
157 |
"""Find fixed Backend for user based on BACKEND_PER_USER setting."""
|
158 |
|
159 |
backend = BACKEND_PER_USER.get(userid) |
160 |
|
161 |
if not backend: |
162 |
return None |
163 |
|
164 |
try:
|
165 |
try:
|
166 |
backend_id = int(backend)
|
167 |
return Backend.objects.get(id=backend_id)
|
168 |
except ValueError: |
169 |
pass
|
170 |
|
171 |
backend_name = str(backend)
|
172 |
return Backend.objects.get(clustername=backend_name)
|
173 |
except Backend.DoesNotExist:
|
174 |
log.error("Invalid backend %s for user %s", backend, userid)
|