OpenStack Compute (Nova)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

495 lines
26KB

  1. # Copyright (c) 2011 OpenStack Foundation
  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. The FilterScheduler is for creating instances locally.
  17. You can customize this scheduler by specifying your own Host Filters and
  18. Weighing Functions.
  19. """
  20. import random
  21. from oslo_log import log as logging
  22. from six.moves import range
  23. from nova.compute import utils as compute_utils
  24. import nova.conf
  25. from nova import exception
  26. from nova.i18n import _
  27. from nova import objects
  28. from nova.objects import fields as fields_obj
  29. from nova import rpc
  30. from nova.scheduler.client import report
  31. from nova.scheduler import driver
  32. from nova.scheduler import utils
  33. CONF = nova.conf.CONF
  34. LOG = logging.getLogger(__name__)
  35. class FilterScheduler(driver.Scheduler):
  36. """Scheduler that can be used for filtering and weighing."""
  37. def __init__(self, *args, **kwargs):
  38. super(FilterScheduler, self).__init__(*args, **kwargs)
  39. self.notifier = rpc.get_notifier('scheduler')
  40. self.placement_client = report.SchedulerReportClient()
  41. def select_destinations(self, context, spec_obj, instance_uuids,
  42. alloc_reqs_by_rp_uuid, provider_summaries,
  43. allocation_request_version=None, return_alternates=False):
  44. """Returns a list of lists of Selection objects, which represent the
  45. hosts and (optionally) alternates for each instance.
  46. :param context: The RequestContext object
  47. :param spec_obj: The RequestSpec object
  48. :param instance_uuids: List of UUIDs, one for each value of the spec
  49. object's num_instances attribute
  50. :param alloc_reqs_by_rp_uuid: Optional dict, keyed by resource provider
  51. UUID, of the allocation_requests that may
  52. be used to claim resources against
  53. matched hosts. If None, indicates either
  54. the placement API wasn't reachable or
  55. that there were no allocation_requests
  56. returned by the placement API. If the
  57. latter, the provider_summaries will be an
  58. empty dict, not None.
  59. :param provider_summaries: Optional dict, keyed by resource provider
  60. UUID, of information that will be used by
  61. the filters/weighers in selecting matching
  62. hosts for a request. If None, indicates that
  63. the scheduler driver should grab all compute
  64. node information locally and that the
  65. Placement API is not used. If an empty dict,
  66. indicates the Placement API returned no
  67. potential matches for the requested
  68. resources.
  69. :param allocation_request_version: The microversion used to request the
  70. allocations.
  71. :param return_alternates: When True, zero or more alternate hosts are
  72. returned with each selected host. The number
  73. of alternates is determined by the
  74. configuration option
  75. `CONF.scheduler.max_attempts`.
  76. """
  77. self.notifier.info(
  78. context, 'scheduler.select_destinations.start',
  79. dict(request_spec=spec_obj.to_legacy_request_spec_dict()))
  80. compute_utils.notify_about_scheduler_action(
  81. context=context, request_spec=spec_obj,
  82. action=fields_obj.NotificationAction.SELECT_DESTINATIONS,
  83. phase=fields_obj.NotificationPhase.START)
  84. host_selections = self._schedule(context, spec_obj, instance_uuids,
  85. alloc_reqs_by_rp_uuid, provider_summaries,
  86. allocation_request_version, return_alternates)
  87. self.notifier.info(
  88. context, 'scheduler.select_destinations.end',
  89. dict(request_spec=spec_obj.to_legacy_request_spec_dict()))
  90. compute_utils.notify_about_scheduler_action(
  91. context=context, request_spec=spec_obj,
  92. action=fields_obj.NotificationAction.SELECT_DESTINATIONS,
  93. phase=fields_obj.NotificationPhase.END)
  94. return host_selections
  95. def _schedule(self, context, spec_obj, instance_uuids,
  96. alloc_reqs_by_rp_uuid, provider_summaries,
  97. allocation_request_version=None, return_alternates=False):
  98. """Returns a list of lists of Selection objects.
  99. :param context: The RequestContext object
  100. :param spec_obj: The RequestSpec object
  101. :param instance_uuids: List of instance UUIDs to place or move.
  102. :param alloc_reqs_by_rp_uuid: Optional dict, keyed by resource provider
  103. UUID, of the allocation_requests that may
  104. be used to claim resources against
  105. matched hosts. If None, indicates either
  106. the placement API wasn't reachable or
  107. that there were no allocation_requests
  108. returned by the placement API. If the
  109. latter, the provider_summaries will be an
  110. empty dict, not None.
  111. :param provider_summaries: Optional dict, keyed by resource provider
  112. UUID, of information that will be used by
  113. the filters/weighers in selecting matching
  114. hosts for a request. If None, indicates that
  115. the scheduler driver should grab all compute
  116. node information locally and that the
  117. Placement API is not used. If an empty dict,
  118. indicates the Placement API returned no
  119. potential matches for the requested
  120. resources.
  121. :param allocation_request_version: The microversion used to request the
  122. allocations.
  123. :param return_alternates: When True, zero or more alternate hosts are
  124. returned with each selected host. The number
  125. of alternates is determined by the
  126. configuration option
  127. `CONF.scheduler.max_attempts`.
  128. """
  129. elevated = context.elevated()
  130. # Find our local list of acceptable hosts by repeatedly
  131. # filtering and weighing our options. Each time we choose a
  132. # host, we virtually consume resources on it so subsequent
  133. # selections can adjust accordingly.
  134. # Note: remember, we are using a generator-iterator here. So only
  135. # traverse this list once. This can bite you if the hosts
  136. # are being scanned in a filter or weighing function.
  137. hosts = self._get_all_host_states(elevated, spec_obj,
  138. provider_summaries)
  139. # NOTE(sbauza): The RequestSpec.num_instances field contains the number
  140. # of instances created when the RequestSpec was used to first boot some
  141. # instances. This is incorrect when doing a move or resize operation,
  142. # so prefer the length of instance_uuids unless it is None.
  143. num_instances = (len(instance_uuids) if instance_uuids
  144. else spec_obj.num_instances)
  145. # For each requested instance, we want to return a host whose resources
  146. # for the instance have been claimed, along with zero or more
  147. # alternates. These alternates will be passed to the cell that the
  148. # selected host is in, so that if for some reason the build fails, the
  149. # cell conductor can retry building the instance on one of these
  150. # alternates instead of having to simply fail. The number of alternates
  151. # is based on CONF.scheduler.max_attempts; note that if there are not
  152. # enough filtered hosts to provide the full number of alternates, the
  153. # list of hosts may be shorter than this amount.
  154. num_alts = (CONF.scheduler.max_attempts - 1
  155. if return_alternates else 0)
  156. if (instance_uuids is None or
  157. not self.USES_ALLOCATION_CANDIDATES or
  158. alloc_reqs_by_rp_uuid is None):
  159. # We still support external scheduler drivers that don't use the
  160. # placement API (and set USES_ALLOCATION_CANDIDATE = False) and
  161. # therefore we skip all the claiming logic for those scheduler
  162. # drivers. Also, if there was a problem communicating with the
  163. # placement API, alloc_reqs_by_rp_uuid will be None, so we skip
  164. # claiming in that case as well. In the case where instance_uuids
  165. # is None, that indicates an older conductor, so we need to return
  166. # the objects without alternates. They will be converted back to
  167. # the older dict format representing HostState objects.
  168. return self._legacy_find_hosts(context, num_instances, spec_obj,
  169. hosts, num_alts,
  170. instance_uuids=instance_uuids)
  171. # A list of the instance UUIDs that were successfully claimed against
  172. # in the placement API. If we are not able to successfully claim for
  173. # all involved instances, we use this list to remove those allocations
  174. # before returning
  175. claimed_instance_uuids = []
  176. # The list of hosts that have been selected (and claimed).
  177. claimed_hosts = []
  178. for num, instance_uuid in enumerate(instance_uuids):
  179. # In a multi-create request, the first request spec from the list
  180. # is passed to the scheduler and that request spec's instance_uuid
  181. # might not be the same as the instance we're processing, so we
  182. # update the instance_uuid in that case before passing the request
  183. # spec to filters since at least one filter
  184. # (ServerGroupAntiAffinityFilter) depends on that information being
  185. # accurate.
  186. spec_obj.instance_uuid = instance_uuid
  187. # Reset the field so it's not persisted accidentally.
  188. spec_obj.obj_reset_changes(['instance_uuid'])
  189. hosts = self._get_sorted_hosts(spec_obj, hosts, num)
  190. if not hosts:
  191. # NOTE(jaypipes): If we get here, that means not all instances
  192. # in instance_uuids were able to be matched to a selected host.
  193. # Any allocations will be cleaned up in the
  194. # _ensure_sufficient_hosts() call.
  195. break
  196. # Attempt to claim the resources against one or more resource
  197. # providers, looping over the sorted list of possible hosts
  198. # looking for an allocation_request that contains that host's
  199. # resource provider UUID
  200. claimed_host = None
  201. for host in hosts:
  202. cn_uuid = host.uuid
  203. if cn_uuid not in alloc_reqs_by_rp_uuid:
  204. msg = ("A host state with uuid = '%s' that did not have a "
  205. "matching allocation_request was encountered while "
  206. "scheduling. This host was skipped.")
  207. LOG.debug(msg, cn_uuid)
  208. continue
  209. alloc_reqs = alloc_reqs_by_rp_uuid[cn_uuid]
  210. # TODO(jaypipes): Loop through all allocation_requests instead
  211. # of just trying the first one. For now, since we'll likely
  212. # want to order the allocation_requests in the future based on
  213. # information in the provider summaries, we'll just try to
  214. # claim resources using the first allocation_request
  215. alloc_req = alloc_reqs[0]
  216. if utils.claim_resources(elevated, self.placement_client,
  217. spec_obj, instance_uuid, alloc_req,
  218. allocation_request_version=allocation_request_version):
  219. claimed_host = host
  220. break
  221. if claimed_host is None:
  222. # We weren't able to claim resources in the placement API
  223. # for any of the sorted hosts identified. So, clean up any
  224. # successfully-claimed resources for prior instances in
  225. # this request and return an empty list which will cause
  226. # select_destinations() to raise NoValidHost
  227. LOG.debug("Unable to successfully claim against any host.")
  228. break
  229. claimed_instance_uuids.append(instance_uuid)
  230. claimed_hosts.append(claimed_host)
  231. # Now consume the resources so the filter/weights will change for
  232. # the next instance.
  233. self._consume_selected_host(claimed_host, spec_obj,
  234. instance_uuid=instance_uuid)
  235. # Check if we were able to fulfill the request. If not, this call will
  236. # raise a NoValidHost exception.
  237. self._ensure_sufficient_hosts(context, claimed_hosts, num_instances,
  238. claimed_instance_uuids)
  239. # We have selected and claimed hosts for each instance. Now we need to
  240. # find alternates for each host.
  241. selections_to_return = self._get_alternate_hosts(
  242. claimed_hosts, spec_obj, hosts, num, num_alts,
  243. alloc_reqs_by_rp_uuid, allocation_request_version)
  244. return selections_to_return
  245. def _ensure_sufficient_hosts(self, context, hosts, required_count,
  246. claimed_uuids=None):
  247. """Checks that we have selected a host for each requested instance. If
  248. not, log this failure, remove allocations for any claimed instances,
  249. and raise a NoValidHost exception.
  250. """
  251. if len(hosts) == required_count:
  252. # We have enough hosts.
  253. return
  254. if claimed_uuids:
  255. self._cleanup_allocations(context, claimed_uuids)
  256. # NOTE(Rui Chen): If multiple creates failed, set the updated time
  257. # of selected HostState to None so that these HostStates are
  258. # refreshed according to database in next schedule, and release
  259. # the resource consumed by instance in the process of selecting
  260. # host.
  261. for host in hosts:
  262. host.updated = None
  263. # Log the details but don't put those into the reason since
  264. # we don't want to give away too much information about our
  265. # actual environment.
  266. LOG.debug('There are %(hosts)d hosts available but '
  267. '%(required_count)d instances requested to build.',
  268. {'hosts': len(hosts),
  269. 'required_count': required_count})
  270. reason = _('There are not enough hosts available.')
  271. raise exception.NoValidHost(reason=reason)
  272. def _cleanup_allocations(self, context, instance_uuids):
  273. """Removes allocations for the supplied instance UUIDs."""
  274. if not instance_uuids:
  275. return
  276. LOG.debug("Cleaning up allocations for %s", instance_uuids)
  277. for uuid in instance_uuids:
  278. self.placement_client.delete_allocation_for_instance(context, uuid)
  279. def _legacy_find_hosts(self, context, num_instances, spec_obj, hosts,
  280. num_alts, instance_uuids=None):
  281. """Some schedulers do not do claiming, or we can sometimes not be able
  282. to if the Placement service is not reachable. Additionally, we may be
  283. working with older conductors that don't pass in instance_uuids.
  284. """
  285. # The list of hosts selected for each instance
  286. selected_hosts = []
  287. for num in range(num_instances):
  288. instance_uuid = instance_uuids[num] if instance_uuids else None
  289. if instance_uuid:
  290. # Update the RequestSpec.instance_uuid before sending it to
  291. # the filters in case we're doing a multi-create request, but
  292. # don't persist the change.
  293. spec_obj.instance_uuid = instance_uuid
  294. spec_obj.obj_reset_changes(['instance_uuid'])
  295. hosts = self._get_sorted_hosts(spec_obj, hosts, num)
  296. if not hosts:
  297. # No hosts left, so break here, and the
  298. # _ensure_sufficient_hosts() call below will handle this.
  299. break
  300. selected_host = hosts[0]
  301. selected_hosts.append(selected_host)
  302. self._consume_selected_host(selected_host, spec_obj,
  303. instance_uuid=instance_uuid)
  304. # Check if we were able to fulfill the request. If not, this call will
  305. # raise a NoValidHost exception.
  306. self._ensure_sufficient_hosts(context, selected_hosts, num_instances)
  307. # This the overall list of values to be returned. There will be one
  308. # item per instance, and each item will be a list of Selection objects
  309. # representing the selected host along with zero or more alternates
  310. # from the same cell.
  311. selections_to_return = self._get_alternate_hosts(selected_hosts,
  312. spec_obj, hosts, num, num_alts)
  313. return selections_to_return
  314. @staticmethod
  315. def _consume_selected_host(selected_host, spec_obj, instance_uuid=None):
  316. LOG.debug("Selected host: %(host)s", {'host': selected_host},
  317. instance_uuid=instance_uuid)
  318. selected_host.consume_from_request(spec_obj)
  319. # If we have a server group, add the selected host to it for the
  320. # (anti-)affinity filters to filter out hosts for subsequent instances
  321. # in a multi-create request.
  322. if spec_obj.instance_group is not None:
  323. spec_obj.instance_group.hosts.append(selected_host.host)
  324. # hosts has to be not part of the updates when saving
  325. spec_obj.instance_group.obj_reset_changes(['hosts'])
  326. # The ServerGroupAntiAffinityFilter also relies on
  327. # HostState.instances being accurate within a multi-create request.
  328. if instance_uuid and instance_uuid not in selected_host.instances:
  329. # Set a stub since ServerGroupAntiAffinityFilter only cares
  330. # about the keys.
  331. selected_host.instances[instance_uuid] = (
  332. objects.Instance(uuid=instance_uuid))
  333. def _get_alternate_hosts(self, selected_hosts, spec_obj, hosts, index,
  334. num_alts, alloc_reqs_by_rp_uuid=None,
  335. allocation_request_version=None):
  336. # We only need to filter/weigh the hosts again if we're dealing with
  337. # more than one instance and are going to be picking alternates.
  338. if index > 0 and num_alts > 0:
  339. # The selected_hosts have all had resources 'claimed' via
  340. # _consume_selected_host, so we need to filter/weigh and sort the
  341. # hosts again to get an accurate count for alternates.
  342. hosts = self._get_sorted_hosts(spec_obj, hosts, index)
  343. # This is the overall list of values to be returned. There will be one
  344. # item per instance, and each item will be a list of Selection objects
  345. # representing the selected host along with alternates from the same
  346. # cell.
  347. selections_to_return = []
  348. for selected_host in selected_hosts:
  349. # This is the list of hosts for one particular instance.
  350. if alloc_reqs_by_rp_uuid:
  351. selected_alloc_req = alloc_reqs_by_rp_uuid.get(
  352. selected_host.uuid)[0]
  353. else:
  354. selected_alloc_req = None
  355. selection = objects.Selection.from_host_state(selected_host,
  356. allocation_request=selected_alloc_req,
  357. allocation_request_version=allocation_request_version)
  358. selected_plus_alts = [selection]
  359. cell_uuid = selected_host.cell_uuid
  360. # This will populate the alternates with many of the same unclaimed
  361. # hosts. This is OK, as it should be rare for a build to fail. And
  362. # if there are not enough hosts to fully populate the alternates,
  363. # it's fine to return fewer than we'd like. Note that we exclude
  364. # any claimed host from consideration as an alternate because it
  365. # will have had its resources reduced and will have a much lower
  366. # chance of being able to fit another instance on it.
  367. for host in hosts:
  368. if len(selected_plus_alts) >= num_alts + 1:
  369. break
  370. if host.cell_uuid == cell_uuid and host not in selected_hosts:
  371. if alloc_reqs_by_rp_uuid is not None:
  372. alt_uuid = host.uuid
  373. if alt_uuid not in alloc_reqs_by_rp_uuid:
  374. msg = ("A host state with uuid = '%s' that did "
  375. "not have a matching allocation_request "
  376. "was encountered while scheduling. This "
  377. "host was skipped.")
  378. LOG.debug(msg, alt_uuid)
  379. continue
  380. # TODO(jaypipes): Loop through all allocation_requests
  381. # instead of just trying the first one. For now, since
  382. # we'll likely want to order the allocation_requests in
  383. # the future based on information in the provider
  384. # summaries, we'll just try to claim resources using
  385. # the first allocation_request
  386. alloc_req = alloc_reqs_by_rp_uuid[alt_uuid][0]
  387. alt_selection = (
  388. objects.Selection.from_host_state(host, alloc_req,
  389. allocation_request_version))
  390. else:
  391. alt_selection = objects.Selection.from_host_state(host)
  392. selected_plus_alts.append(alt_selection)
  393. selections_to_return.append(selected_plus_alts)
  394. return selections_to_return
  395. def _get_sorted_hosts(self, spec_obj, host_states, index):
  396. """Returns a list of HostState objects that match the required
  397. scheduling constraints for the request spec object and have been sorted
  398. according to the weighers.
  399. """
  400. filtered_hosts = self.host_manager.get_filtered_hosts(host_states,
  401. spec_obj, index)
  402. LOG.debug("Filtered %(hosts)s", {'hosts': filtered_hosts})
  403. if not filtered_hosts:
  404. return []
  405. weighed_hosts = self.host_manager.get_weighed_hosts(filtered_hosts,
  406. spec_obj)
  407. if CONF.filter_scheduler.shuffle_best_same_weighed_hosts:
  408. # NOTE(pas-ha) Randomize best hosts, relying on weighed_hosts
  409. # being already sorted by weight in descending order.
  410. # This decreases possible contention and rescheduling attempts
  411. # when there is a large number of hosts having the same best
  412. # weight, especially so when host_subset_size is 1 (default)
  413. best_hosts = [w for w in weighed_hosts
  414. if w.weight == weighed_hosts[0].weight]
  415. random.shuffle(best_hosts)
  416. weighed_hosts = best_hosts + weighed_hosts[len(best_hosts):]
  417. # Log the weighed hosts before stripping off the wrapper class so that
  418. # the weight value gets logged.
  419. LOG.debug("Weighed %(hosts)s", {'hosts': weighed_hosts})
  420. # Strip off the WeighedHost wrapper class...
  421. weighed_hosts = [h.obj for h in weighed_hosts]
  422. # We randomize the first element in the returned list to alleviate
  423. # congestion where the same host is consistently selected among
  424. # numerous potential hosts for similar request specs.
  425. host_subset_size = CONF.filter_scheduler.host_subset_size
  426. if host_subset_size < len(weighed_hosts):
  427. weighed_subset = weighed_hosts[0:host_subset_size]
  428. else:
  429. weighed_subset = weighed_hosts
  430. chosen_host = random.choice(weighed_subset)
  431. weighed_hosts.remove(chosen_host)
  432. return [chosen_host] + weighed_hosts
  433. def _get_all_host_states(self, context, spec_obj, provider_summaries):
  434. """Template method, so a subclass can implement caching."""
  435. # NOTE(jaypipes): provider_summaries being None is treated differently
  436. # from an empty dict. provider_summaries is None when we want to grab
  437. # all compute nodes, for instance when using a scheduler driver that
  438. # sets USES_ALLOCATION_CANDIDATES=False.
  439. # The provider_summaries variable will be an empty dict when the
  440. # Placement API found no providers that match the requested
  441. # constraints, which in turn makes compute_uuids an empty list and
  442. # get_host_states_by_uuids will return an empty generator-iterator
  443. # also, which will eventually result in a NoValidHost error.
  444. compute_uuids = None
  445. if provider_summaries is not None:
  446. compute_uuids = list(provider_summaries.keys())
  447. return self.host_manager.get_host_states_by_uuids(context,
  448. compute_uuids,
  449. spec_obj)