1. 程式人生 > >Nova(project version:2017-5 版)原始碼初步閱讀(1)

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處理。