openstack之stevedore的使用

你所浪费的今天,是昨天死去的人奢望的明天。你所厌恶的现在,是未来的你回不去的曾经。

stevedore库是oslo项目中为OpenStack其他项目提供动态加载功能的公共组件库。stevedore利用python的特性,使得动态加载代码变得更加容易,其也允许你在运行时通过发现和加载扩展插件来配置和扩展你的应用程序。stevedore库基于setuptools的entry points来定义和加载扩展插件,stevedore提供了manager类来实现动态加载扩展插件的通用模式。本文将详细分析stevedore的实现原理以及使用方式。

stevedore的实现

管理基类

本文开头介绍到stevedore通过提供manager类来实现动态加载扩展插件的管理,因此在实现stevedore时,首先为其他父类定义了一个manager基类ExtensionManager类。ExtensionManager类是一个所有其他manager类的基类,其主要的属性和方法如下:

  • namespace:string类型,命名空间,表示entry points的命名空间;
  • invoke_on_load:bool类型,表示是否自动加载扩展插件;
  • invoke_args:tuple类型,表示自动加载extension时传入的参数;
  • invoke_kwds:dict类型,表示自动加载extension时传入的参数;
  • propagate_map_exceptions:bool类型,表示使用map调用时,是否向上传递调用信息;
  • on_load_failure_callback:func类型,表示加载失败时调用的方法;
  • verify_requirements:bool类型,表示是否使用setuptools安装插件所需要的依赖;
  • map(func, args, *kwds):为每一个extension触发func()函数;
  • map_method(method_name, args, *kwds):为每一个extension触发method_name指定的函数;
  • names():获取所有发现的extension名称;
  • entry_points_names():返回所有entry_points的名称列表,每个列表元素是一个有entry points的名称和entry points列表的map对象;
  • list_entry_points():某个命名空间的所有entry points列表。

stevedore中其他所有manager类都需要继承ExtensionManager类,而ExtensionManager类初始化时便会通过namespace等加载所有extension,并对插件进行初始化。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
def __init__(self, namespace,
invoke_on_load=False,
invoke_args=(),
invoke_kwds={},
propagate_map_exceptions=False,
on_load_failure_callback=None,
verify_requirements=False):
self._init_attributes(
namespace,
propagate_map_exceptions=propagate_map_exceptions,
on_load_failure_callback=on_load_failure_callback)
extensions = self._load_plugins(invoke_on_load,
invoke_args,
invoke_kwds,
verify_requirements)
self._init_plugins(extensions)

在ExtensionManager实例化对象时,首先调用_init_attributes()方法初始化namespace等参数,然后会调用_load_plugins()方法加载所有的extension插件;最后会调用_init_plugins()方法设置对象的属性。

在定义ExtensionManager时,还涉及到一个重要的类Extension,该类表示一个extension,该类主要包含如下属性:

  • name:表示一个entry point的名称;
  • entry_point:表示从pkg_resources获得的一个EntryPoint对象;
  • plugin:通过调用entry_point.load()方法返回的plugin类;
  • obj:extension被manager类加载时,会调用plugin(args, *kwds)返回一个plugin对象。

在ExtensionManager的map()方法中,为每一个entry point调用func()函数,而func()函数的第一个参数即为Extension对象。

加载插件的方式

根据entry points配置的不同,stevedore提供了三种加载插件的方式:ExtensionManager、DriverManager、HookManager。下面将分别介绍这三种加载插件的方式:

  • ExtensionManager:一种通用的加载方式。这种方式下,对于给定的命名空间,会加载该命名空间下的所有插件,同时也允许同一个命名空间下的插件拥有相同的名称,其实现即为stevedore.extension.ExtensionManager类;
  • HookManager:在这种加载方式下,对于给定的命名空间,允许同一个命名空间下的插件拥有相同的名称,程序可以根据给定的命名空间和名称加载该名称对应的多个插件,其实现为stevedore.hook.HookManager类;
  • DriverManager:在这种加载方式下,对于给定的命名空间,一个名字只能对应一个entry point,对于同一类资源有多个不同插件的情况,只能选择一个进行注册;这样,在使用时就可以根据命名空间和名称定位到某一个插件,其实现为stevedore.driver.DriverManager类。

在实现这些加载方式的类时,stevedore还定义了多个其他类型的辅助manager类,这些manager类之间的关系如图1所示。

img

由图可知,ExtensionManager类时所有stevedore的manager类的父类,DriverManager类和HookManager类是ExtensionManager子类NamedExtensionManager类的子类。而NamedExtensionManager类中增加了一个属性names,所以DriverManager类和HookManager类在加载对应插件时,只加载names属性所包含的名称的entry point插件。除了这几个类之外,stevedore还定义了其他三个辅助的manager类:

  • EnabledExtensionManager类:该类在ExtensionManager类的基础上添加了一个check_func属性,表示一个验证方法,因此在加载时只加载通过check_func()方法验证的extension插件;
  • DispatchExtensionManager类:该类继承自EnabledExtensionManager类,该类重写了ExtensionManger类中定义的map()和map_method()方法,其为这两个方法添加了filter_func参数,表示只对通过filter_func()方法过滤的extension才会执行func()函数;
  • NameDispatcherExtensionManager类:该类继承自DispathExtensionManager类,该类也定义了一个names属性,在使用时,只有names包含的名称的extension执行map()和map_method()方法时才会执行对应的func()方法。

stevedore的使用

有了stevedore,OpenStack其他项目加载一个扩展插件就要方便的多了。下面通过nova中的加载扩展插件为例详细介绍stevedore的使用方法。在使用stevedore时,nova首先定义了相关的插件,如nova-scheduler服务实现了多种调度方法,这些调度方法便是通过stevedore来进行动态加载的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import abc

import six
from stevedore import driver

import nova.conf
from nova import objects
from nova import servicegroup

CONF = nova.conf.CONF


@six.add_metaclass(abc.ABCMeta)
class Scheduler(object):
"""The base class that all Scheduler classes should inherit from."""

USES_ALLOCATION_CANDIDATES = True
"""Indicates that the scheduler driver calls the Placement API for
allocation candidates and uses those allocation candidates in its
decision-making.
"""

def __init__(self):
self.host_manager = driver.DriverManager(
"nova.scheduler.host_manager",
CONF.scheduler.host_manager,
invoke_on_load=True).driver
self.servicegroup_api = servicegroup.API()

def run_periodic_tasks(self, context):
"""Manager calls this so drivers can perform periodic tasks."""
pass

def hosts_up(self, context, topic):
"""Return the list of hosts that have a running service for topic."""

services = objects.ServiceList.get_by_topic(context, topic)
return [service.host
for service in services
if self.servicegroup_api.service_is_up(service)]

@abc.abstractmethod
def select_destinations(self, context, spec_obj, instance_uuids,
provider_summaries):
"""Returns a list of HostState objects that have been chosen by the
scheduler driver, one for each requested instance
(spec_obj.num_instances)
"""
return []

nova-scheduler首先通过abc定义了一个抽象类Scheduler,用来定义所有调度方法的抽象类,并定义了一个select_destinations()的抽象方法,这个方法即为调度方法,需要具体实现的调度类来实现。接着,nova-scheduler分别实现了FilterScheduler、CachingScheduler、ChanceScheduler、FakeScheduler类来实现具体的调度方法,这里仅以FilterScheduler为例。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
import random

from oslo_log import log as logging
from six.moves import range

import nova.conf
from nova import exception
from nova.i18n import _
from nova import rpc
from nova.scheduler import client
from nova.scheduler import driver

CONF = nova.conf.CONF
LOG = logging.getLogger(__name__)


class FilterScheduler(driver.Scheduler):
"""Scheduler that can be used for filtering and weighing."""
def __init__(self, *args, **kwargs):
super(FilterScheduler, self).__init__(*args, **kwargs)
self.notifier = rpc.get_notifier('scheduler')
scheduler_client = client.SchedulerClient()
self.placement_client = scheduler_client.reportclient

def select_destinations(self, context, spec_obj, instance_uuids,
alloc_reqs_by_rp_uuid, provider_summaries):
"""Returns a list of sorted lists of HostState objects (1 for each
instance) that would satisfy the supplied request_spec. Each of those
lists consist of [chosen_host, alternate1, ..., alternateN], where the
'chosen_host' has already had its resources claimed in Placement,
followed by zero or more alternates. The alternates are hosts that can
satisfy the request, and are included so that if the build for the
chosen host fails, the cell conductor can retry.
:param context: The RequestContext object
:param spec_obj: The RequestSpec object
:param instance_uuids: List of UUIDs, one for each value of the spec
object's num_instances attribute
:param alloc_reqs_by_rp_uuid: Optional dict, keyed by resource provider
UUID, of the allocation_requests that may
be used to claim resources against
matched hosts. If None, indicates either
the placement API wasn't reachable or
that there were no allocation_requests
returned by the placement API. If the
latter, the provider_summaries will be an
empty dict, not None.
:param provider_summaries: Optional dict, keyed by resource provider
UUID, of information that will be used by
the filters/weighers in selecting matching
hosts for a request. If None, indicates that
the scheduler driver should grab all compute
node information locally and that the
Placement API is not used. If an empty dict,
indicates the Placement API returned no
potential matches for the requested
resources.
"""
self.notifier.info(
context, 'scheduler.select_destinations.start',
dict(request_spec=spec_obj.to_legacy_request_spec_dict()))

# NOTE(sbauza): The RequestSpec.num_instances field contains the number
# of instances created when the RequestSpec was used to first boot some
# instances. This is incorrect when doing a move or resize operation,
# so prefer the length of instance_uuids unless it is None.
num_instances = (len(instance_uuids) if instance_uuids
else spec_obj.num_instances)
selected_host_lists = self._schedule(context, spec_obj, instance_uuids,
alloc_reqs_by_rp_uuid, provider_summaries)

# Couldn't fulfill the request_spec
if len(selected_host_lists) < num_instances:
# NOTE(Rui Chen): If multiple creates failed, set the updated time
# of selected HostState to None so that these HostStates are
# refreshed according to database in next schedule, and release
# the resource consumed by instance in the process of selecting
# host.
for host_list in selected_host_lists:
host_list[0].updated = None

# Log the details but don't put those into the reason since
# we don't want to give away too much information about our
# actual environment.
LOG.debug('There are %(hosts)d hosts available but '
'%(num_instances)d instances requested to build.',
{'hosts': len(selected_host_lists),
'num_instances': num_instances})

reason = _('There are not enough hosts available.')
raise exception.NoValidHost(reason=reason)

self.notifier.info(
context, 'scheduler.select_destinations.end',
dict(request_spec=spec_obj.to_legacy_request_spec_dict()))
# NOTE(edleafe) - In this patch we only create the lists of [chosen,
# alt1, alt2, etc.]. In a later patch we will change what we return, so
# for this patch just return the selected hosts.
selected_hosts = [sel_host[0] for sel_host in selected_host_lists]
return selected_hosts

def _schedule(self, context, spec_obj, instance_uuids,
alloc_reqs_by_rp_uuid, provider_summaries):
"""Returns a list of hosts that meet the required specs, ordered by
their fitness.
These hosts will have already had their resources claimed in Placement.
:param context: The RequestContext object
:param spec_obj: The RequestSpec object
:param instance_uuids: List of instance UUIDs to place or move.
:param alloc_reqs_by_rp_uuid: Optional dict, keyed by resource provider
UUID, of the allocation_requests that may
be used to claim resources against
matched hosts. If None, indicates either
the placement API wasn't reachable or
that there were no allocation_requests
returned by the placement API. If the
latter, the provider_summaries will be an
empty dict, not None.
:param provider_summaries: Optional dict, keyed by resource provider
UUID, of information that will be used by
the filters/weighers in selecting matching
hosts for a request. If None, indicates that
the scheduler driver should grab all compute
node information locally and that the
Placement API is not used. If an empty dict,
indicates the Placement API returned no
potential matches for the requested
resources.
"""
elevated = context.elevated()

# Find our local list of acceptable hosts by repeatedly
# filtering and weighing our options. Each time we choose a
# host, we virtually consume resources on it so subsequent
# selections can adjust accordingly.

# Note: remember, we are using an iterator here. So only
# traverse this list once. This can bite you if the hosts
# are being scanned in a filter or weighing function.
hosts = self._get_all_host_states(elevated, spec_obj,
provider_summaries)

# NOTE(sbauza): The RequestSpec.num_instances field contains the number
# of instances created when the RequestSpec was used to first boot some
# instances. This is incorrect when doing a move or resize operation,
# so prefer the length of instance_uuids unless it is None.
num_instances = (len(instance_uuids) if instance_uuids
else spec_obj.num_instances)

# For each requested instance, we want to return a host whose resources
# for the instance have been claimed, along with zero or more
# alternates. These alternates will be passed to the cell that the
# selected host is in, so that if for some reason the build fails, the
# cell conductor can retry building the instance on one of these
# alternates instead of having to simply fail. The number of alternates
# is based on CONF.scheduler.max_attempts; note that if there are not
# enough filtered hosts to provide the full number of alternates, the
# list of hosts may be shorter than this amount.
num_to_return = CONF.scheduler.max_attempts

if (instance_uuids is None or
not self.USES_ALLOCATION_CANDIDATES or
alloc_reqs_by_rp_uuid is None):
# We need to support the caching scheduler, which doesn't use the
# placement API (and has USES_ALLOCATION_CANDIDATE = False) and
# therefore we skip all the claiming logic for that scheduler
# driver. Also, if there was a problem communicating with the
# placement API, alloc_reqs_by_rp_uuid will be None, so we skip
# claiming in that case as well. In the case where instance_uuids
# is None, that indicates an older conductor, so we need to return
# the older-style HostState objects without alternates.
# NOTE(edleafe): moving this logic into a separate method, as this
# method is already way too long. It will also make it easier to
# clean up once we no longer have to worry about older conductors.
include_alternates = (instance_uuids is not None)
return self._legacy_find_hosts(num_instances, spec_obj, hosts,
num_to_return, include_alternates)

# A list of the instance UUIDs that were successfully claimed against
# in the placement API. If we are not able to successfully claim for
# all involved instances, we use this list to remove those allocations
# before returning
claimed_instance_uuids = []

# The list of hosts that have been selected (and claimed).
claimed_hosts = []

for num in range(num_instances):
hosts = self._get_sorted_hosts(spec_obj, hosts, num)
if not hosts:
# NOTE(jaypipes): If we get here, that means not all instances
# in instance_uuids were able to be matched to a selected host.
# So, let's clean up any already-claimed allocations here
# before breaking and returning
self._cleanup_allocations(claimed_instance_uuids)
break

instance_uuid = instance_uuids[num]
# Attempt to claim the resources against one or more resource
# providers, looping over the sorted list of possible hosts
# looking for an allocation_request that contains that host's
# resource provider UUID
claimed_host = None
for host in hosts:
cn_uuid = host.uuid
if cn_uuid not in alloc_reqs_by_rp_uuid:
LOG.debug("Found host state %s that wasn't in "
"allocation_requests. Skipping.", cn_uuid)
continue

alloc_reqs = alloc_reqs_by_rp_uuid[cn_uuid]
if self._claim_resources(elevated, spec_obj, instance_uuid,
alloc_reqs):
claimed_host = host
break

if claimed_host is None:
# We weren't able to claim resources in the placement API
# for any of the sorted hosts identified. So, clean up any
# successfully-claimed resources for prior instances in
# this request and return an empty list which will cause
# select_destinations() to raise NoValidHost
LOG.debug("Unable to successfully claim against any host.")
self._cleanup_allocations(claimed_instance_uuids)
return []

claimed_instance_uuids.append(instance_uuid)
claimed_hosts.append(claimed_host)

# Now consume the resources so the filter/weights will change for
# the next instance.
self._consume_selected_host(claimed_host, spec_obj)

# We have selected and claimed hosts for each instance. Now we need to
# find alternates for each host.
selections_to_return = self._get_alternate_hosts(
claimed_hosts, spec_obj, hosts, num, num_to_return)
return selections_to_return

def _cleanup_allocations(self, instance_uuids):
"""Removes allocations for the supplied instance UUIDs."""
if not instance_uuids:
return
LOG.debug("Cleaning up allocations for %s", instance_uuids)
for uuid in instance_uuids:
self.placement_client.delete_allocation_for_instance(uuid)

def _claim_resources(self, ctx, spec_obj, instance_uuid, alloc_reqs):
"""Given an instance UUID (representing the consumer of resources), the
HostState object for the host that was chosen for the instance, and a
list of allocation_request JSON objects, attempt to claim resources for
the instance in the placement API. Returns True if the claim process
was successful, False otherwise.
:param ctx: The RequestContext object
:param spec_obj: The RequestSpec object
:param instance_uuid: The UUID of the consuming instance
:param cn_uuid: UUID of the host to allocate against
:param alloc_reqs: A list of allocation_request JSON objects that
allocate against (at least) the compute host
selected by the _schedule() method. These
allocation_requests were constructed from a call to
the GET /allocation_candidates placement API call.
Each allocation_request satisfies the original
request for resources and can be supplied as-is
(along with the project and user ID to the placement
API's PUT /allocations/{consumer_uuid} call to claim
resources for the instance
"""
LOG.debug("Attempting to claim resources in the placement API for "
"instance %s", instance_uuid)

project_id = spec_obj.project_id

# NOTE(jaypipes): So, the RequestSpec doesn't store the user_id,
# only the project_id, so we need to grab the user information from
# the context. Perhaps we should consider putting the user ID in
# the spec object?
user_id = ctx.user_id

# TODO(jaypipes): Loop through all allocation_requests instead of just
# trying the first one. For now, since we'll likely want to order the
# allocation_requests in the future based on information in the
# provider summaries, we'll just try to claim resources using the first
# allocation_request
alloc_req = alloc_reqs[0]

return self.placement_client.claim_resources(instance_uuid,
alloc_req, project_id, user_id)

def _legacy_find_hosts(self, num_instances, spec_obj, hosts,
num_to_return, include_alternates):
"""Some schedulers do not do claiming, or we can sometimes not be able
to if the Placement service is not reachable. Additionally, we may be
working with older conductors that don't pass in instance_uuids.
"""
# The list of hosts selected for each instance
selected_hosts = []
# This the overall list of values to be returned. There will be one
# item per instance, and when 'include_alternates' is True, that item
# will be a list of HostState objects representing the selected host
# along with alternates from the same cell. When 'include_alternates'
# is False, the return value will be a list of HostState objects, with
# one per requested instance.
selections_to_return = []

for num in range(num_instances):
hosts = self._get_sorted_hosts(spec_obj, hosts, num)
if not hosts:
return []
selected_host = hosts[0]
selected_hosts.append(selected_host)
self._consume_selected_host(selected_host, spec_obj)

if include_alternates:
selections_to_return = self._get_alternate_hosts(
selected_hosts, spec_obj, hosts, num, num_to_return)
return selections_to_return
return selected_hosts

@staticmethod
def _consume_selected_host(selected_host, spec_obj):
LOG.debug("Selected host: %(host)s", {'host': selected_host})
selected_host.consume_from_request(spec_obj)
if spec_obj.instance_group is not None:
spec_obj.instance_group.hosts.append(selected_host.host)
# hosts has to be not part of the updates when saving
spec_obj.instance_group.obj_reset_changes(['hosts'])

def _get_alternate_hosts(self, selected_hosts, spec_obj, hosts, index,
num_to_return):
# We only need to filter/weigh the hosts again if we're dealing with
# more than one instance since the single selected host will get
# filtered out of the list of alternates below.
if index > 0:
# The selected_hosts have all had resources 'claimed' via
# _consume_selected_host, so we need to filter/weigh and sort the
# hosts again to get an accurate count for alternates.
hosts = self._get_sorted_hosts(spec_obj, hosts, index)
# This is the overall list of values to be returned. There will be one
# item per instance, and that item will be a list of HostState objects
# representing the selected host along with alternates from the same
# cell.
selections_to_return = []
for selected_host in selected_hosts:
# This is the list of hosts for one particular instance.
selected_plus_alts = [selected_host]
cell_uuid = selected_host.cell_uuid
# This will populate the alternates with many of the same unclaimed
# hosts. This is OK, as it should be rare for a build to fail. And
# if there are not enough hosts to fully populate the alternates,
# it's fine to return fewer than we'd like. Note that we exclude
# any claimed host from consideration as an alternate because it
# will have had its resources reduced and will have a much lower
# chance of being able to fit another instance on it.
for host in hosts:
if len(selected_plus_alts) >= num_to_return:
break
if host.cell_uuid == cell_uuid and host not in selected_hosts:
selected_plus_alts.append(host)
selections_to_return.append(selected_plus_alts)
return selections_to_return

def _get_sorted_hosts(self, spec_obj, host_states, index):
"""Returns a list of HostState objects that match the required
scheduling constraints for the request spec object and have been sorted
according to the weighers.
"""
filtered_hosts = self.host_manager.get_filtered_hosts(host_states,
spec_obj, index)

LOG.debug("Filtered %(hosts)s", {'hosts': filtered_hosts})

if not filtered_hosts:
return []

weighed_hosts = self.host_manager.get_weighed_hosts(filtered_hosts,
spec_obj)
# Strip off the WeighedHost wrapper class...
weighed_hosts = [h.obj for h in weighed_hosts]

LOG.debug("Weighed %(hosts)s", {'hosts': weighed_hosts})

# We randomize the first element in the returned list to alleviate
# congestion where the same host is consistently selected among
# numerous potential hosts for similar request specs.
host_subset_size = CONF.filter_scheduler.host_subset_size
if host_subset_size < len(weighed_hosts):
weighed_subset = weighed_hosts[0:host_subset_size]
else:
weighed_subset = weighed_hosts
chosen_host = random.choice(weighed_subset)
weighed_hosts.remove(chosen_host)
return [chosen_host] + weighed_hosts

def _get_all_host_states(self, context, spec_obj, provider_summaries):
"""Template method, so a subclass can implement caching."""
# NOTE(jaypipes): provider_summaries being None is treated differently
# from an empty dict. provider_summaries is None when we want to grab
# all compute nodes, for instance when using the caching scheduler.
# The provider_summaries variable will be an empty dict when the
# Placement API found no providers that match the requested
# constraints, which in turn makes compute_uuids an empty list and
# get_host_states_by_uuids will return an empty tuple also, which will
# eventually result in a NoValidHost error.
compute_uuids = None
if provider_summaries is not None:
compute_uuids = list(provider_summaries.keys())
return self.host_manager.get_host_states_by_uuids(context,
compute_uuids,
spec_obj)

FilterScheduler类首先继承了nova定义的Scheduler抽象类,然后实现了select_destinations()方法来实现具体的调度方法。

定义了各种调度策略之后,接下来就需要将这些不同的调度类作为entry point配置到setup.cfg文件中,nova的调度策略配置信息如下所示:

1
2
3
4
5
6
7
8
[entry_points]
...

nova.scheduler.driver =
filter_scheduler = nova.scheduler.filter_scheduler:FilterScheduler
caching_scheduler = nova.scheduler.caching_scheduler:CachingScheduler
chance_scheduler = nova.scheduler.chance:ChanceScheduler
fake_scheduler = nova.tests.unit.scheduler.fakes:FakeScheduler

通过以上配置信息可以看出,针对nova-scheduler服务定义的调度策略,nova在setup.cfg配置文件中为调度策略定义了nova.scheduler.driver作为entry points的命名空间,并在这个命名空间下配置了四种不同的调度策略。

最后,nova便可以在需要使用调度策略的地方载入不同的调度策略插件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
class SchedulerManager(manager.Manager):
"""Chooses a host to run instances on."""

target = messaging.Target(version='4.4')

_sentinel = object()

def __init__(self, scheduler_driver=None, *args, **kwargs):
client = scheduler_client.SchedulerClient()
self.placement_client = client.reportclient
if not scheduler_driver:
scheduler_driver = CONF.scheduler.driver
self.driver = driver.DriverManager(
"nova.scheduler.driver",
scheduler_driver,
invoke_on_load=True).driver
super(SchedulerManager, self).__init__(service_name='scheduler',
*args, **kwargs)

@periodic_task.periodic_task(
spacing=CONF.scheduler.discover_hosts_in_cells_interval,
run_immediately=True)
def _discover_hosts_in_cells(self, context):
host_mappings = host_mapping_obj.discover_hosts(context)
if host_mappings:
LOG.info(_LI('Discovered %(count)i new hosts: %(hosts)s'),
{'count': len(host_mappings),
'hosts': ','.join(['%s:%s' % (hm.cell_mapping.name,
hm.host)
for hm in host_mappings])})

@periodic_task.periodic_task(spacing=CONF.scheduler.periodic_task_interval,
run_immediately=True)
def _run_periodic_tasks(self, context):
self.driver.run_periodic_tasks(context)

@messaging.expected_exceptions(exception.NoValidHost)
def select_destinations(self, ctxt,
request_spec=None, filter_properties=None,
spec_obj=_sentinel, instance_uuids=None):
"""Returns destinations(s) best suited for this RequestSpec.
The result should be a list of dicts with 'host', 'nodename' and
'limits' as keys.
"""
LOG.debug("Starting to schedule for instances: %s", instance_uuids)

# TODO(sbauza): Change the method signature to only accept a spec_obj
# argument once API v5 is provided.
if spec_obj is self._sentinel:
spec_obj = objects.RequestSpec.from_primitives(ctxt,
request_spec,
filter_properties)
resources = utils.resources_from_request_spec(spec_obj)
alloc_reqs_by_rp_uuid, provider_summaries = None, None
if self.driver.USES_ALLOCATION_CANDIDATES:
res = self.placement_client.get_allocation_candidates(resources)
if res is None:
# We have to handle the case that we failed to connect to the
# Placement service and the safe_connect decorator on
# get_allocation_candidates returns None.
alloc_reqs, provider_summaries = None, None
else:
alloc_reqs, provider_summaries = res
if not alloc_reqs:
LOG.debug("Got no allocation candidates from the Placement "
"API. This may be a temporary occurrence as compute "
"nodes start up and begin reporting inventory to "
"the Placement service.")
raise exception.NoValidHost(reason="")
else:
# Build a dict of lists of allocation requests, keyed by
# provider UUID, so that when we attempt to claim resources for
# a host, we can grab an allocation request easily
alloc_reqs_by_rp_uuid = collections.defaultdict(list)
for ar in alloc_reqs:
for rr in ar['allocations']:
rp_uuid = rr['resource_provider']['uuid']
alloc_reqs_by_rp_uuid[rp_uuid].append(ar)

dests = self.driver.select_destinations(ctxt, spec_obj, instance_uuids,
alloc_reqs_by_rp_uuid, provider_summaries)
dest_dicts = [_host_state_obj_to_dict(d) for d in dests]
return jsonutils.to_primitive(dest_dicts)

def update_aggregates(self, ctxt, aggregates):
"""Updates HostManager internal aggregates information.
:param aggregates: Aggregate(s) to update
:type aggregates: :class:`nova.objects.Aggregate`
or :class:`nova.objects.AggregateList`
"""
# NOTE(sbauza): We're dropping the user context now as we don't need it
self.driver.host_manager.update_aggregates(aggregates)

def delete_aggregate(self, ctxt, aggregate):
"""Deletes HostManager internal information about a specific aggregate.
:param aggregate: Aggregate to delete
:type aggregate: :class:`nova.objects.Aggregate`
"""
# NOTE(sbauza): We're dropping the user context now as we don't need it
self.driver.host_manager.delete_aggregate(aggregate)

def update_instance_info(self, context, host_name, instance_info):
"""Receives information about changes to a host's instances, and
updates the driver's HostManager with that information.
"""
self.driver.host_manager.update_instance_info(context, host_name,
instance_info)

def delete_instance_info(self, context, host_name, instance_uuid):
"""Receives information about the deletion of one of a host's
instances, and updates the driver's HostManager with that information.
"""
self.driver.host_manager.delete_instance_info(context, host_name,
instance_uuid)

def sync_instance_info(self, context, host_name, instance_uuids):
"""Receives a sync request from a host, and passes it on to the
driver's HostManager.
"""
self.driver.host_manager.sync_instance_info(context, host_name,
instance_uuids)

针对nova的调度策略,nova定义了SchedulerManager类专门用于管理调度方法的实现,在这个类中包含了一个driver属性,而在初始化SchedulerManager对象时,这个driver属性会被赋值,由于nova的调度方法的entry points都是名称和调度方法一一对应的,因此driver属性会被赋值为一个DriverManager对象,也就是说每个SchedulerManager对象只能加载一种调度方法进行操作。

-------------本文结束 感谢您的阅读-------------
作者Magiceses
有问题请 留言 或者私信我的 微博
满分是10分的话,这篇文章你给几分