~ubuntu-branches/ubuntu/raring/cinder/raring-updates

« back to all changes in this revision

Viewing changes to cinder/scheduler/weights/capacity.py

Tags: upstream-2013.1~g2
ImportĀ upstreamĀ versionĀ 2013.1~g2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2012 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
Capacity Weigher.  Weigh hosts by their available capacity.
 
17
 
 
18
The default is to spread volumes across all hosts evenly.  If you prefer
 
19
stacking, you can set the 'capacity_weight_multiplier' option to a negative
 
20
number and the weighing has the opposite effect of the default.
 
21
"""
 
22
 
 
23
import math
 
24
 
 
25
from cinder import flags
 
26
from cinder.openstack.common import cfg
 
27
from cinder.openstack.common.scheduler import weights
 
28
 
 
29
 
 
30
capacity_weight_opts = [
 
31
        cfg.FloatOpt('capacity_weight_multiplier',
 
32
                     default=1.0,
 
33
                     help='Multiplier used for weighing volume capacity. '
 
34
                          'Negative numbers mean to stack vs spread.'),
 
35
]
 
36
 
 
37
FLAGS = flags.FLAGS
 
38
FLAGS.register_opts(capacity_weight_opts)
 
39
 
 
40
 
 
41
class CapacityWeigher(weights.BaseHostWeigher):
 
42
    def _weight_multiplier(self):
 
43
        """Override the weight multiplier."""
 
44
        return FLAGS.capacity_weight_multiplier
 
45
 
 
46
    def _weigh_object(self, host_state, weight_properties):
 
47
        """Higher weights win.  We want spreading to be the default."""
 
48
        reserved = float(host_state.reserved_percentage) / 100
 
49
        free = math.floor(host_state.free_capacity_gb * (1 - reserved))
 
50
        return free