Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / logic / backend_allocator.py @ 47d1d754

History | View | Annotate | Download (4.1 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 import settings
35
from synnefo.db.models import Backend
36
from synnefo.logic.backend import update_resources
37

    
38
log = logging.getLogger(__name__)
39

    
40

    
41
class BackendAllocator():
42
    """Wrapper class for instance allocation.
43

44
    """
45
    def __init__(self):
46
        self.strategy_mod =\
47
            importlib.import_module(settings.BACKEND_ALLOCATOR_MODULE)
48

    
49
    def allocate(self, flavor):
50
        # Get the size of the vm
51
        disk = flavor_disk(flavor)
52
        ram = flavor.ram
53
        cpu = flavor.cpu
54
        vm = {'ram': ram, 'disk': disk, 'cpu': cpu}
55

    
56
        log.debug("Allocating VM: %r", vm)
57

    
58
        # Refresh backends, if needed
59
        refresh_backends_stats()
60

    
61
        # Get available backends
62
        available_backends = get_available_backends()
63

    
64
        if not available_backends:
65
            return None
66

    
67
        # Find the best backend to host the vm, based on the allocation
68
        # strategy
69
        backend = self.strategy_mod.allocate(available_backends, vm)
70

    
71
        log.info("Allocated VM %r, in backend %s", vm, backend)
72

    
73
        # Reduce the free resources of the selected backend by the size of
74
        # the vm
75
        reduce_backend_resources(backend, vm)
76

    
77
        return backend
78

    
79

    
80
def get_available_backends():
81
    """Get available backends from db.
82

83
    """
84
    return Backend.objects.filter(drained=False, offline=False)
85

    
86

    
87
def flavor_disk(flavor):
88
    """ Get flavor's 'real' disk size
89

90
    """
91
    if flavor.disk_template == 'drbd':
92
        return flavor.disk * 1024 * 2
93
    else:
94
        return flavor.disk * 1024
95

    
96

    
97
def reduce_backend_resources(backend, vm):
98
    """ Conservatively update the resources of a backend.
99

100
    Reduce the free resources of the backend by the size of the of the vm that
101
    will host. This is an underestimation of the backend capabilities.
102

103
    """
104

    
105
    new_mfree = backend.mfree - vm['ram']
106
    new_dfree = backend.dfree - vm['disk']
107
    backend.mfree = 0 if new_mfree < 0 else new_mfree
108
    backend.dfree = 0 if new_dfree < 0 else new_dfree
109
    backend.pinst_cnt += 1
110

    
111
    backend.save()
112

    
113

    
114
def refresh_backends_stats():
115
    """ Refresh the statistics of the backends.
116

117
    Set db backend state to the actual state of the backend, if
118
    BACKEND_REFRESH_MIN time has passed.
119

120
    """
121

    
122
    now = datetime.datetime.now()
123
    delta = datetime.timedelta(minutes=settings.BACKEND_REFRESH_MIN)
124
    for b in Backend.objects.filter(drained=False, offline=False):
125
        if now > b.updated + delta:
126
            log.debug("Updating resources of backend %r", b)
127
            update_resources(b)