Nova(project version:2017-5 版)源码初步阅读(1)

一、关于Manager

1.功能用法:

看一下作者怎么说:Managers are responsible for a certain aspect of the system. It is a logical grouping of code relating to a portion of the system. In general other components should be using the manager to make changes to the components that it is responsible for.

Manager用来负责管理系统的某一方法功能。通常来说,在某个组件需要和其他组件的相应的Manager打交道,而不是直接修改某些参数。例如,某一组件可能需要和卷打交道,那么该组件调用VolumeManager中的方法,而不是直接修改数据库中的相应数据。这样做的一个显而易见的好处就是使得和卷相关的代码都在一个相同的地方(这也是面向对象吧,特定的功能交给特定的类)。

2.关于Manager的一点源码阅读工作

(1)nova.Manager文件中的ManagerMeta类: python 元类,用来追踪包装类的public方法,使用时,必须在类中添加属性: trace_args,该属性为一个字典类型,至少包涵一个key为name,value=该类的action。

class ManagerMeta(profiler.get_traced_meta(), type(PeriodicTasks)):
    """Metaclass to trace all children of a specific class.

(2)nova.Manager文件中的Manager类:这应用是只是定义了一些基础的接口,如init_host,cleanup_host,pre_start_hook,post_start_hook,reset方法等。

@six.add_metaclass(ManagerMeta)
class Manager(base.Base, PeriodicTasks):
    __trace_args__ = {"name": "rpc"}

    def __init__(self, host=None, db_driver=None, service_name='undefined'):
        if not host:
            host = CONF.host
        self.host = host
        self.backdoor_port = None
        self.service_name = service_name
        self.notifier = rpc.get_notifier(self.service_name, self.host)
        self.additional_endpoints = []
        super(Manager, self).__init__(db_driver)

    def periodic_tasks(self, context, raise_on_error=False):
        """Tasks to be run at a periodic interval."""
        return self.run_periodic_tasks(context, raise_on_error=raise_on_error)

    def init_host(self):
        """Hook to do additional manager initialization when one requests
        the service be started.  This is called before any service record
        is created.

        Child classes should override this method.
        """
        pass

    def cleanup_host(self):
        """Hook to do cleanup work when the service shuts down.

        Child classes should override this method.
        """
        pass

    def pre_start_hook(self):
        """Hook to provide the manager the ability to do additional
        start-up work before any RPC queues/consumers are created. This is
        called after other initialization has succeeded and a service
        record is created.

        Child classes should override this method.
        """
        pass

    def post_start_hook(self):
        """Hook to provide the manager the ability to do additional
        start-up work immediately after a service creates RPC consumers
        and starts 'running'.

        Child classes should override this method.
        """
        pass

    def reset(self):
        """Hook called on SIGHUP to signal the manager to re-read any
        dynamic configuration or do any reconfiguration tasks.
        """
        pass

(3)Nova.schedule.manager文件:只包含一个类,SchedulerManager(manager.Manager),该类代表Scheduler Service,基类为上面的nova.manager.Manager类.
      该类的主要功能就是选择一个host运行你的示例。但是和schedule相关的工作实际的实现者都是该类绑定的driver,由driver去具体操作,该SchedulerManager主要作用感觉是规范了一下接口。
方法举例:

 @messaging.expected_exceptions(exception.NoValidHost)
    def select_destinations(self, ctxt,
                            request_spec=None, filter_properties=None,
                            spec_obj=_sentinel):
        """Returns destinations(s) best suited for this RequestSpec.

        The result should be a list of dicts with 'host', 'nodename' and
        'limits' as keys.
        """

        # 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)
        dests = self.driver.select_destinations(ctxt, spec_obj)
        return jsonutils.to_primitive(dests)
 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)

3.注意事项

(1)Manager中的方法如果能在本地执行,则直接调用,否则,应该采用RPC方式执行.
(2)Manager应用负责大部分的数据库访问(实现时,不要只实现特定数据相关的访问,而是数据库访问的通用方法)。任何和非通用的工作应该交给Driver处理。

你可能感兴趣的:(openstack基础知识)