/nova/tests/scheduler/test_least_cost_scheduler.py

https://github.com/basak/nova
Python | 146 lines | 93 code | 29 blank | 24 comment | 5 complexity | 1c223472ec238429429b0d6dbc1224b9 MD5 | raw file
  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. Tests For Least Cost Scheduler
  17. """
  18. import copy
  19. from nova import test
  20. from nova.scheduler import least_cost
  21. from nova.tests.scheduler import test_abstract_scheduler
  22. MB = 1024 * 1024
  23. class FakeHost(object):
  24. def __init__(self, host_id, free_ram, io):
  25. self.id = host_id
  26. self.free_ram = free_ram
  27. self.io = io
  28. class WeightedSumTestCase(test.TestCase):
  29. def test_empty_domain(self):
  30. domain = []
  31. weighted_fns = []
  32. result = least_cost.weighted_sum(domain, weighted_fns)
  33. expected = []
  34. self.assertEqual(expected, result)
  35. def test_basic_costing(self):
  36. hosts = [
  37. FakeHost(1, 512 * MB, 100),
  38. FakeHost(2, 256 * MB, 400),
  39. FakeHost(3, 512 * MB, 100),
  40. ]
  41. weighted_fns = [
  42. (1, lambda h: h.free_ram), # Fill-first, free_ram is a *cost*
  43. (2, lambda h: h.io), # Avoid high I/O
  44. ]
  45. costs = least_cost.weighted_sum(
  46. domain=hosts, weighted_fns=weighted_fns)
  47. # Each 256 MB unit of free-ram contributes 0.5 points by way of:
  48. # cost = weight * (score/max_score) = 1 * (256/512) = 0.5
  49. # Each 100 iops of IO adds 0.5 points by way of:
  50. # cost = 2 * (100/400) = 2 * 0.25 = 0.5
  51. expected = [1.5, 2.5, 1.5]
  52. self.assertEqual(expected, costs)
  53. class LeastCostSchedulerTestCase(test.TestCase):
  54. def setUp(self):
  55. super(LeastCostSchedulerTestCase, self).setUp()
  56. class FakeZoneManager:
  57. pass
  58. zone_manager = FakeZoneManager()
  59. states = test_abstract_scheduler.fake_zone_manager_service_states(
  60. num_hosts=10)
  61. zone_manager.service_states = states
  62. self.sched = least_cost.LeastCostScheduler()
  63. self.sched.zone_manager = zone_manager
  64. def tearDown(self):
  65. super(LeastCostSchedulerTestCase, self).tearDown()
  66. def assertWeights(self, expected, num, request_spec, hosts):
  67. weighted = self.sched.weigh_hosts("compute", request_spec, hosts)
  68. self.assertDictListMatch(weighted, expected, approx_equal=True)
  69. def test_no_hosts(self):
  70. num = 1
  71. request_spec = {}
  72. hosts = []
  73. expected = []
  74. self.assertWeights(expected, num, request_spec, hosts)
  75. def test_noop_cost_fn(self):
  76. self.flags(least_cost_scheduler_cost_functions=[
  77. 'nova.scheduler.least_cost.noop_cost_fn'],
  78. noop_cost_fn_weight=1)
  79. num = 1
  80. request_spec = {}
  81. hosts = self.sched.filter_hosts(num, request_spec)
  82. expected = [dict(weight=1, hostname=hostname)
  83. for hostname, caps in hosts]
  84. self.assertWeights(expected, num, request_spec, hosts)
  85. def test_cost_fn_weights(self):
  86. self.flags(least_cost_scheduler_cost_functions=[
  87. 'nova.scheduler.least_cost.noop_cost_fn'],
  88. noop_cost_fn_weight=2)
  89. num = 1
  90. request_spec = {}
  91. hosts = self.sched.filter_hosts(num, request_spec)
  92. expected = [dict(weight=2, hostname=hostname)
  93. for hostname, caps in hosts]
  94. self.assertWeights(expected, num, request_spec, hosts)
  95. def test_compute_fill_first_cost_fn(self):
  96. self.flags(least_cost_scheduler_cost_functions=[
  97. 'nova.scheduler.least_cost.compute_fill_first_cost_fn'],
  98. compute_fill_first_cost_fn_weight=1)
  99. num = 1
  100. instance_type = {'memory_mb': 1024}
  101. request_spec = {'instance_type': instance_type}
  102. svc_states = self.sched.zone_manager.service_states.iteritems()
  103. all_hosts = [(host, services["compute"])
  104. for host, services in svc_states
  105. if "compute" in services]
  106. hosts = self.sched.filter_hosts('compute', request_spec, all_hosts)
  107. expected = []
  108. for idx, (hostname, services) in enumerate(hosts):
  109. caps = copy.deepcopy(services)
  110. # Costs are normalized so over 10 hosts, each host with increasing
  111. # free ram will cost 1/N more. Since the lowest cost host has some
  112. # free ram, we add in the 1/N for the base_cost
  113. weight = 0.1 + (0.1 * idx)
  114. wtd_dict = dict(hostname=hostname, weight=weight,
  115. capabilities=caps)
  116. expected.append(wtd_dict)
  117. self.assertWeights(expected, num, request_spec, hosts)