Modify Affinity filter for RequestSpec
Change-Id: Iaefc5114114f4590849418f6869f0458aa0cdc0e Partially-Implements: blueprint request-spec-object-mitaka
This commit is contained in:
@@ -30,11 +30,8 @@ class DifferentHostFilter(filters.BaseHostFilter):
|
||||
# The hosts the instances are running on doesn't change within a request
|
||||
run_filter_once_per_request = True
|
||||
|
||||
@filters.compat_legacy_props
|
||||
def host_passes(self, host_state, filter_properties):
|
||||
scheduler_hints = filter_properties.get('scheduler_hints') or {}
|
||||
|
||||
affinity_uuids = scheduler_hints.get('different_host', [])
|
||||
def host_passes(self, host_state, spec_obj):
|
||||
affinity_uuids = spec_obj.get_scheduler_hint('different_host')
|
||||
if affinity_uuids:
|
||||
overlap = utils.instance_uuids_overlap(host_state, affinity_uuids)
|
||||
return not overlap
|
||||
@@ -50,11 +47,8 @@ class SameHostFilter(filters.BaseHostFilter):
|
||||
# The hosts the instances are running on doesn't change within a request
|
||||
run_filter_once_per_request = True
|
||||
|
||||
@filters.compat_legacy_props
|
||||
def host_passes(self, host_state, filter_properties):
|
||||
scheduler_hints = filter_properties.get('scheduler_hints') or {}
|
||||
|
||||
affinity_uuids = scheduler_hints.get('same_host', [])
|
||||
def host_passes(self, host_state, spec_obj):
|
||||
affinity_uuids = spec_obj.get_scheduler_hint('same_host')
|
||||
if affinity_uuids and host_state.instances:
|
||||
overlap = utils.instance_uuids_overlap(host_state, affinity_uuids)
|
||||
return overlap
|
||||
@@ -69,12 +63,9 @@ class SimpleCIDRAffinityFilter(filters.BaseHostFilter):
|
||||
# The address of a host doesn't change within a request
|
||||
run_filter_once_per_request = True
|
||||
|
||||
@filters.compat_legacy_props
|
||||
def host_passes(self, host_state, filter_properties):
|
||||
scheduler_hints = filter_properties.get('scheduler_hints') or {}
|
||||
|
||||
affinity_cidr = scheduler_hints.get('cidr', '/24')
|
||||
affinity_host_addr = scheduler_hints.get('build_near_host_ip')
|
||||
def host_passes(self, host_state, spec_obj):
|
||||
affinity_cidr = spec_obj.get_scheduler_hint('cidr', '/24')
|
||||
affinity_host_addr = spec_obj.get_scheduler_hint('build_near_host_ip')
|
||||
host_ip = host_state.host_ip
|
||||
if affinity_host_addr:
|
||||
affinity_net = netaddr.IPNetwork(str.join('', (affinity_host_addr,
|
||||
@@ -90,14 +81,15 @@ class _GroupAntiAffinityFilter(filters.BaseHostFilter):
|
||||
"""Schedule the instance on a different host from a set of group
|
||||
hosts.
|
||||
"""
|
||||
@filters.compat_legacy_props
|
||||
def host_passes(self, host_state, filter_properties):
|
||||
def host_passes(self, host_state, spec_obj):
|
||||
# Only invoke the filter is 'anti-affinity' is configured
|
||||
policies = filter_properties.get('group_policies', [])
|
||||
policies = (spec_obj.instance_group.policies
|
||||
if spec_obj.instance_group else [])
|
||||
if self.policy_name not in policies:
|
||||
return True
|
||||
|
||||
group_hosts = filter_properties.get('group_hosts') or []
|
||||
group_hosts = (spec_obj.instance_group.hosts
|
||||
if spec_obj.instance_group else [])
|
||||
LOG.debug("Group anti affinity: check if %(host)s not "
|
||||
"in %(configured)s", {'host': host_state.host,
|
||||
'configured': group_hosts})
|
||||
@@ -117,14 +109,15 @@ class ServerGroupAntiAffinityFilter(_GroupAntiAffinityFilter):
|
||||
class _GroupAffinityFilter(filters.BaseHostFilter):
|
||||
"""Schedule the instance on to host from a set of group hosts.
|
||||
"""
|
||||
@filters.compat_legacy_props
|
||||
def host_passes(self, host_state, filter_properties):
|
||||
def host_passes(self, host_state, spec_obj):
|
||||
# Only invoke the filter is 'affinity' is configured
|
||||
policies = filter_properties.get('group_policies', [])
|
||||
policies = (spec_obj.instance_group.policies
|
||||
if spec_obj.instance_group else [])
|
||||
if self.policy_name not in policies:
|
||||
return True
|
||||
|
||||
group_hosts = filter_properties.get('group_hosts', [])
|
||||
group_hosts = (spec_obj.instance_group.hosts
|
||||
if spec_obj.instance_group else [])
|
||||
LOG.debug("Group affinity: check if %(host)s in "
|
||||
"%(configured)s", {'host': host_state.host,
|
||||
'configured': group_hosts})
|
||||
|
||||
@@ -33,35 +33,28 @@ class TestDifferentHostFilter(test.NoDBTestCase):
|
||||
host = fakes.FakeHostState('host1', 'node1', {})
|
||||
inst1 = objects.Instance(uuid='different')
|
||||
host.instances = {inst1.uuid: inst1}
|
||||
filter_properties = {'context': mock.sentinel.ctx,
|
||||
'scheduler_hints': {
|
||||
'different_host': ['same'], }}
|
||||
self.assertTrue(self.filt_cls.host_passes(host, filter_properties))
|
||||
|
||||
def test_affinity_different_filter_no_list_passes(self):
|
||||
host = fakes.FakeHostState('host1', 'node1', {})
|
||||
host.instances = {}
|
||||
filter_properties = {'context': mock.sentinel.ctx,
|
||||
'scheduler_hints': {
|
||||
'different_host': 'same'}}
|
||||
self.assertTrue(self.filt_cls.host_passes(host, filter_properties))
|
||||
spec_obj = objects.RequestSpec(
|
||||
context=mock.sentinel.ctx,
|
||||
scheduler_hints=dict(different_host=['same']))
|
||||
self.assertTrue(self.filt_cls.host_passes(host, spec_obj))
|
||||
|
||||
def test_affinity_different_filter_fails(self):
|
||||
inst1 = objects.Instance(uuid='same')
|
||||
host = fakes.FakeHostState('host1', 'node1', {})
|
||||
host.instances = {inst1.uuid: inst1}
|
||||
filter_properties = {'context': mock.sentinel.ctx,
|
||||
'scheduler_hints': {
|
||||
'different_host': ['same'], }}
|
||||
self.assertFalse(self.filt_cls.host_passes(host, filter_properties))
|
||||
spec_obj = objects.RequestSpec(
|
||||
context=mock.sentinel.ctx,
|
||||
scheduler_hints=dict(different_host=['same']))
|
||||
self.assertFalse(self.filt_cls.host_passes(host, spec_obj))
|
||||
|
||||
def test_affinity_different_filter_handles_none(self):
|
||||
inst1 = objects.Instance(uuid='same')
|
||||
host = fakes.FakeHostState('host1', 'node1', {})
|
||||
host.instances = {inst1.uuid: inst1}
|
||||
filter_properties = {'context': mock.sentinel.ctx,
|
||||
'scheduler_hints': None}
|
||||
self.assertTrue(self.filt_cls.host_passes(host, filter_properties))
|
||||
spec_obj = objects.RequestSpec(
|
||||
context=mock.sentinel.ctx,
|
||||
scheduler_hints=None)
|
||||
self.assertTrue(self.filt_cls.host_passes(host, spec_obj))
|
||||
|
||||
|
||||
class TestSameHostFilter(test.NoDBTestCase):
|
||||
@@ -74,35 +67,28 @@ class TestSameHostFilter(test.NoDBTestCase):
|
||||
inst1 = objects.Instance(uuid='same')
|
||||
host = fakes.FakeHostState('host1', 'node1', {})
|
||||
host.instances = {inst1.uuid: inst1}
|
||||
filter_properties = {'context': mock.sentinel.ctx,
|
||||
'scheduler_hints': {
|
||||
'same_host': ['same'], }}
|
||||
self.assertTrue(self.filt_cls.host_passes(host, filter_properties))
|
||||
|
||||
def test_affinity_same_filter_no_list_passes(self):
|
||||
host = fakes.FakeHostState('host1', 'node1', {})
|
||||
host.instances = {}
|
||||
filter_properties = {'context': mock.sentinel.ctx,
|
||||
'scheduler_hints': {
|
||||
'same_host': 'same'}}
|
||||
self.assertTrue(self.filt_cls.host_passes(host, filter_properties))
|
||||
spec_obj = objects.RequestSpec(
|
||||
context=mock.sentinel.ctx,
|
||||
scheduler_hints=dict(same_host=['same']))
|
||||
self.assertTrue(self.filt_cls.host_passes(host, spec_obj))
|
||||
|
||||
def test_affinity_same_filter_fails(self):
|
||||
inst1 = objects.Instance(uuid='different')
|
||||
host = fakes.FakeHostState('host1', 'node1', {})
|
||||
host.instances = {inst1.uuid: inst1}
|
||||
filter_properties = {'context': mock.sentinel.ctx,
|
||||
'scheduler_hints': {
|
||||
'same_host': ['same'], }}
|
||||
self.assertFalse(self.filt_cls.host_passes(host, filter_properties))
|
||||
spec_obj = objects.RequestSpec(
|
||||
context=mock.sentinel.ctx,
|
||||
scheduler_hints=dict(same_host=['same']))
|
||||
self.assertFalse(self.filt_cls.host_passes(host, spec_obj))
|
||||
|
||||
def test_affinity_same_filter_handles_none(self):
|
||||
inst1 = objects.Instance(uuid='different')
|
||||
host = fakes.FakeHostState('host1', 'node1', {})
|
||||
host.instances = {inst1.uuid: inst1}
|
||||
filter_properties = {'context': mock.sentinel.ctx,
|
||||
'scheduler_hints': None}
|
||||
self.assertTrue(self.filt_cls.host_passes(host, filter_properties))
|
||||
spec_obj = objects.RequestSpec(
|
||||
context=mock.sentinel.ctx,
|
||||
scheduler_hints=None)
|
||||
self.assertTrue(self.filt_cls.host_passes(host, spec_obj))
|
||||
|
||||
|
||||
class TestSimpleCIDRAffinityFilter(test.NoDBTestCase):
|
||||
@@ -117,12 +103,13 @@ class TestSimpleCIDRAffinityFilter(test.NoDBTestCase):
|
||||
|
||||
affinity_ip = "10.8.1.100"
|
||||
|
||||
filter_properties = {'context': mock.sentinel.ctx,
|
||||
'scheduler_hints': {
|
||||
'cidr': '/24',
|
||||
'build_near_host_ip': affinity_ip}}
|
||||
spec_obj = objects.RequestSpec(
|
||||
context=mock.sentinel.ctx,
|
||||
scheduler_hints=dict(
|
||||
cidr=['/24'],
|
||||
build_near_host_ip=[affinity_ip]))
|
||||
|
||||
self.assertTrue(self.filt_cls.host_passes(host, filter_properties))
|
||||
self.assertTrue(self.filt_cls.host_passes(host, spec_obj))
|
||||
|
||||
def test_affinity_simple_cidr_filter_fails(self):
|
||||
host = fakes.FakeHostState('host1', 'node1', {})
|
||||
@@ -130,12 +117,13 @@ class TestSimpleCIDRAffinityFilter(test.NoDBTestCase):
|
||||
|
||||
affinity_ip = "10.8.1.100"
|
||||
|
||||
filter_properties = {'context': mock.sentinel.ctx,
|
||||
'scheduler_hints': {
|
||||
'cidr': '/32',
|
||||
'build_near_host_ip': affinity_ip}}
|
||||
spec_obj = objects.RequestSpec(
|
||||
context=mock.sentinel.ctx,
|
||||
scheduler_hints=dict(
|
||||
cidr=['/32'],
|
||||
build_near_host_ip=[affinity_ip]))
|
||||
|
||||
self.assertFalse(self.filt_cls.host_passes(host, filter_properties))
|
||||
self.assertFalse(self.filt_cls.host_passes(host, spec_obj))
|
||||
|
||||
def test_affinity_simple_cidr_filter_handles_none(self):
|
||||
host = fakes.FakeHostState('host1', 'node1', {})
|
||||
@@ -144,25 +132,28 @@ class TestSimpleCIDRAffinityFilter(test.NoDBTestCase):
|
||||
affinity_ip.append('100')
|
||||
affinity_ip = str.join('.', affinity_ip)
|
||||
|
||||
filter_properties = {'context': mock.sentinel.ctx,
|
||||
'scheduler_hints': None}
|
||||
spec_obj = objects.RequestSpec(
|
||||
context=mock.sentinel.ctx,
|
||||
scheduler_hints=None)
|
||||
|
||||
self.assertTrue(self.filt_cls.host_passes(host, filter_properties))
|
||||
self.assertTrue(self.filt_cls.host_passes(host, spec_obj))
|
||||
|
||||
|
||||
class TestGroupAffinityFilter(test.NoDBTestCase):
|
||||
|
||||
def _test_group_anti_affinity_filter_passes(self, filt_cls, policy):
|
||||
host = fakes.FakeHostState('host1', 'node1', {})
|
||||
filter_properties = {}
|
||||
self.assertTrue(filt_cls.host_passes(host, filter_properties))
|
||||
filter_properties = {'group_policies': ['affinity']}
|
||||
self.assertTrue(filt_cls.host_passes(host, filter_properties))
|
||||
filter_properties = {'group_policies': [policy]}
|
||||
filter_properties['group_hosts'] = []
|
||||
self.assertTrue(filt_cls.host_passes(host, filter_properties))
|
||||
filter_properties['group_hosts'] = ['host2']
|
||||
self.assertTrue(filt_cls.host_passes(host, filter_properties))
|
||||
spec_obj = objects.RequestSpec(instance_group=None)
|
||||
self.assertTrue(filt_cls.host_passes(host, spec_obj))
|
||||
spec_obj = objects.RequestSpec(instance_group=objects.InstanceGroup(
|
||||
policies=['affinity']))
|
||||
self.assertTrue(filt_cls.host_passes(host, spec_obj))
|
||||
spec_obj = objects.RequestSpec(instance_group=objects.InstanceGroup(
|
||||
policies=[policy]))
|
||||
spec_obj.instance_group.hosts = []
|
||||
self.assertTrue(filt_cls.host_passes(host, spec_obj))
|
||||
spec_obj.instance_group.hosts = ['host2']
|
||||
self.assertTrue(filt_cls.host_passes(host, spec_obj))
|
||||
|
||||
def test_group_anti_affinity_filter_passes(self):
|
||||
self._test_group_anti_affinity_filter_passes(
|
||||
@@ -171,9 +162,10 @@ class TestGroupAffinityFilter(test.NoDBTestCase):
|
||||
|
||||
def _test_group_anti_affinity_filter_fails(self, filt_cls, policy):
|
||||
host = fakes.FakeHostState('host1', 'node1', {})
|
||||
filter_properties = {'group_policies': [policy],
|
||||
'group_hosts': ['host1']}
|
||||
self.assertFalse(filt_cls.host_passes(host, filter_properties))
|
||||
spec_obj = objects.RequestSpec(instance_group=objects.InstanceGroup(
|
||||
policies=[policy],
|
||||
hosts=['host1']))
|
||||
self.assertFalse(filt_cls.host_passes(host, spec_obj))
|
||||
|
||||
def test_group_anti_affinity_filter_fails(self):
|
||||
self._test_group_anti_affinity_filter_fails(
|
||||
@@ -182,13 +174,15 @@ class TestGroupAffinityFilter(test.NoDBTestCase):
|
||||
|
||||
def _test_group_affinity_filter_passes(self, filt_cls, policy):
|
||||
host = fakes.FakeHostState('host1', 'node1', {})
|
||||
filter_properties = {}
|
||||
self.assertTrue(filt_cls.host_passes(host, filter_properties))
|
||||
filter_properties = {'group_policies': ['anti-affinity']}
|
||||
self.assertTrue(filt_cls.host_passes(host, filter_properties))
|
||||
filter_properties = {'group_policies': ['affinity'],
|
||||
'group_hosts': ['host1']}
|
||||
self.assertTrue(filt_cls.host_passes(host, filter_properties))
|
||||
spec_obj = objects.RequestSpec(instance_group=None)
|
||||
self.assertTrue(filt_cls.host_passes(host, spec_obj))
|
||||
spec_obj = objects.RequestSpec(instance_group=objects.InstanceGroup(
|
||||
policies=['anti-affinity']))
|
||||
self.assertTrue(filt_cls.host_passes(host, spec_obj))
|
||||
spec_obj = objects.RequestSpec(instance_group=objects.InstanceGroup(
|
||||
policies=['affinity'],
|
||||
hosts=['host1']))
|
||||
self.assertTrue(filt_cls.host_passes(host, spec_obj))
|
||||
|
||||
def test_group_affinity_filter_passes(self):
|
||||
self._test_group_affinity_filter_passes(
|
||||
@@ -196,9 +190,10 @@ class TestGroupAffinityFilter(test.NoDBTestCase):
|
||||
|
||||
def _test_group_affinity_filter_fails(self, filt_cls, policy):
|
||||
host = fakes.FakeHostState('host1', 'node1', {})
|
||||
filter_properties = {'group_policies': [policy],
|
||||
'group_hosts': ['host2']}
|
||||
self.assertFalse(filt_cls.host_passes(host, filter_properties))
|
||||
spec_obj = objects.RequestSpec(instance_group=objects.InstanceGroup(
|
||||
policies=[policy],
|
||||
hosts=['host2']))
|
||||
self.assertFalse(filt_cls.host_passes(host, spec_obj))
|
||||
|
||||
def test_group_affinity_filter_fails(self):
|
||||
self._test_group_affinity_filter_fails(
|
||||
|
||||
Reference in New Issue
Block a user