add server-group affinity constraints.

Change-Id: Ibae2d483a6b58929ad0a4c31c15e0320365c7a9d
This commit is contained in:
xyhuang 2014-12-13 07:24:36 +08:00
parent 037e86fc49
commit 00cc1b86d3
2 changed files with 135 additions and 0 deletions

View File

@ -0,0 +1,70 @@
# Copyright (c) 2014 Cisco Systems, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from nova.openstack.common import log as logging
from nova.scheduler.solvers import linearconstraints
LOG = logging.getLogger(__name__)
class ServerGroupAffinityConstraint(linearconstraints.AffinityConstraint):
"""Force to select hosts which host given server group."""
# The linear constraint should be formed as:
# coeff_matrix * var_matrix' (operator) constant_vector
# where (operator) is ==, >, >=, <, <=, !=, etc.
# For convenience, the constant_vector is merged into left-hand-side,
# thus the right-hand-side is always 0.
def __init__(self, *args, **kwargs):
super(ServerGroupAffinityConstraint, self).__init__(*args, **kwargs)
self.policy_name = 'affinity'
def get_coefficient_vectors(self, variables, hosts, instance_uuids,
request_spec, filter_properties):
coefficient_vectors = []
policies = filter_properties.get('group_policies', [])
if self.policy_name not in policies:
coefficient_vectors = [[0 for j in range(self.num_instances)]
for i in range(self.num_hosts)]
return coefficient_vectors
group_hosts = filter_properties.get('group_hosts')
if not group_hosts:
# when the group is empty, we need to place all the instances in
# a same host.
coefficient_vectors = [[1 - self.num_instances] +
[1 for j in range(self.num_instances - 1)] for
i in range(self.num_hosts)]
for host in hosts:
if host.host in group_hosts:
coefficient_vectors.append([0 for j
in range(self.num_instances)])
else:
coefficient_vectors.append([1 for j
in range(self.num_instances)])
return coefficient_vectors
def get_variable_vectors(self, variables, hosts, instance_uuids,
request_spec, filter_properties):
variable_vectors = []
variable_vectors = [[variables[i][j] for j in range(
self.num_instances)] for i in range(self.num_hosts)]
return variable_vectors
def get_operations(self, variables, hosts, instance_uuids,
request_spec, filter_properties):
operations = [(lambda x: x == 0) for i in range(self.num_hosts)]
return operations

View File

@ -0,0 +1,65 @@
# Copyright (c) 2014 Cisco Systems, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from nova.openstack.common import log as logging
from nova.scheduler.solvers import linearconstraints
LOG = logging.getLogger(__name__)
class ServerGroupAntiAffinityConstraint(linearconstraints.AffinityConstraint):
"""Force to select hosts which host given server group."""
# The linear constraint should be formed as:
# coeff_matrix * var_matrix' (operator) constant_vector
# where (operator) is ==, >, >=, <, <=, !=, etc.
# For convenience, the constant_vector is merged into left-hand-side,
# thus the right-hand-side is always 0.
def __init__(self, *args, **kwargs):
super(ServerGroupAntiAffinityConstraint, self).__init__(
*args, **kwargs)
self.policy_name = 'anti-affinity'
def get_coefficient_vectors(self, variables, hosts, instance_uuids,
request_spec, filter_properties):
coefficient_vectors = []
policies = filter_properties.get('group_policies', [])
if self.policy_name not in policies:
coefficient_vectors = [[0 for j in range(self.num_instances)] + [0]
for i in range(self.num_hosts)]
return coefficient_vectors
group_hosts = filter_properties.get('group_hosts')
for host in hosts:
if host.host in group_hosts:
coefficient_vectors.append([1 for j
in range(self.num_instances)] + [1])
else:
coefficient_vectors.append([1 for j
in range(self.num_instances)] + [0])
return coefficient_vectors
def get_variable_vectors(self, variables, hosts, instance_uuids,
request_spec, filter_properties):
variable_vectors = []
variable_vectors = [[variables[i][j] for j in range(
self.num_instances)] + [1] for i in range(self.num_hosts)]
return variable_vectors
def get_operations(self, variables, hosts, instance_uuids,
request_spec, filter_properties):
operations = [(lambda x: x <= 1) for i in range(self.num_hosts)]
return operations