~ubuntu-branches/ubuntu/quantal/nova/quantal-proposed

« back to all changes in this revision

Viewing changes to nova/notifier/capacity_notifier.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-08-16 14:04:11 UTC
  • mto: This revision was merged to the branch mainline in revision 84.
  • Revision ID: package-import@ubuntu.com-20120816140411-0mr4n241wmk30t9l
Tags: upstream-2012.2~f3
ImportĀ upstreamĀ versionĀ 2012.2~f3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2011 OpenStack LLC.
2
 
# All Rights Reserved.
3
 
#
4
 
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
5
 
#    not use this file except in compliance with the License. You may obtain
6
 
#    a copy of the License at
7
 
#
8
 
#         http://www.apache.org/licenses/LICENSE-2.0
9
 
#
10
 
#    Unless required by applicable law or agreed to in writing, software
11
 
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
 
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
 
#    License for the specific language governing permissions and limitations
14
 
#    under the License.
15
 
 
16
 
from nova import context
17
 
from nova import db
18
 
from nova.openstack.common import log as logging
19
 
 
20
 
 
21
 
LOG = logging.getLogger(__name__)
22
 
 
23
 
 
24
 
def notify(_context, message):
25
 
    """Look for specific compute manager events and interprete them
26
 
    so as to keep the Capacity table up to date.
27
 
 
28
 
    NOTE: the True/False return codes are only for testing.
29
 
    """
30
 
 
31
 
    # The event_type must start with 'compute.instance.'
32
 
    event_type = message.get('event_type', None)
33
 
    preamble = 'compute.instance.'
34
 
    if not event_type or not event_type.startswith(preamble):
35
 
        return False
36
 
 
37
 
    # Events we're interested in end with .start and .end
38
 
    event = event_type[len(preamble):]
39
 
    parts = event.split('.')
40
 
    suffix = parts[-1].lower()
41
 
    event = event[:(-len(suffix) - 1)]
42
 
 
43
 
    if suffix not in ['start', 'end']:
44
 
        return False
45
 
    started = suffix == 'start'
46
 
    ended = suffix == 'end'
47
 
 
48
 
    if started and event == 'create':
49
 
        # We've already updated this stuff in the scheduler. Don't redo the
50
 
        # work here.
51
 
        return False
52
 
 
53
 
    work = 1 if started else -1
54
 
 
55
 
    # Extract the host name from the publisher id ...
56
 
    publisher_preamble = 'compute.'
57
 
    publisher = message.get('publisher_id', None)
58
 
    if not publisher or not publisher.startswith(publisher_preamble):
59
 
        return False
60
 
    host = publisher[len(publisher_preamble):]
61
 
 
62
 
    # If we deleted an instance, make sure we reclaim the resources.
63
 
    # We may need to do something explicit for rebuild/migrate.
64
 
    free_ram_mb = 0
65
 
    free_disk_gb = 0
66
 
    vms = 0
67
 
    if ended and event == 'delete':
68
 
        vms = -1
69
 
        payload = message.get('payload', {})
70
 
        free_ram_mb = payload.get('memory_mb', 0)
71
 
        free_disk_gb = payload.get('disk_gb', 0)
72
 
 
73
 
    LOG.debug("EventType=%(event_type)s -> host %(host)s: "
74
 
              "ram %(free_ram_mb)d, disk %(free_disk_gb)d, "
75
 
              "work %(work)d, vms%(vms)d" % locals())
76
 
 
77
 
    db.api.compute_node_utilization_update(context.get_admin_context(), host,
78
 
        free_ram_mb_delta=free_ram_mb, free_disk_gb_delta=free_disk_gb,
79
 
        work_delta=work, vm_delta=vms)
80
 
 
81
 
    return True