1,为什么在paste中定义app是都使用工厂方法?


举例:在nova:api-paste.ini文件中


[composite:openstack_compute_api_v2]
 use = call:nova.api.auth:pipeline_factory
 noauth = faultwrap sizelimit noauth ratelimit osapi_compute_app_v2
 keystone = faultwrap sizelimit authtoken keystonecontext ratelimit osapi_compute_app_v2
 keystone_nolimit = faultwrap sizelimit authtoken keystonecontext osapi_compute_app_v2

看一下use内容nova.api.auth:pipline_factory:

def pipeline_factory(loader, global_conf, **local_conf):
    """A paste pipeline replica that keys off of auth_strategy."""
    pipeline = local_conf[CONF.auth_strategy]
    if not CONF.api_rate_limit:
        limit_name = CONF.auth_strategy + '_nolimit'
        pipeline = local_conf.get(limit_name, pipeline)
    pipeline = pipeline.split()
    filters = [loader.get_filter(n) for n in pipeline[:-1]]
    app = loader.get_app(pipeline[-1])
    filters.reverse()
    for filter in filters:
        app = filter(app)
    return app



从这里可以看出,在pipline_factory方法中加载了paste的globalconf和local conf,在这里local conf包括:noauth keyston keyston_nolimit等,此方法返回的是一个app:

filter1(filter2(....(app)))


2 nova中的资源刷新机制


原文地址,,原文总结的非常好!这里相当于是一个学习笔记!

1,服务启动时,加载了CompteManager,并周期执行其中被@periodic_task.periodic_task注解的函数

class Service(service.Service):
    def start(self):
        if self.periodic_enable:
            if self.periodic_fuzzy_delay:
                initial_delay = random.randint(0, self.periodic_fuzzy_delay)
            else:
                initial_delay = None
            self.tg.add_dynamic_timer(self.periodic_tasks,
                                     initial_delay=initial_delay,
                                     periodic_interval_max=
                                        self.periodic_interval_max)
服务加载时,会查询manager中所有带有@periodic_task.periodic_task装饰器的函数,这些函数将会被周期的执行

 

2,被周期执行的函数示例:

class ComputeManager():
	def update_service_capabilities(self, capabilities):
        """Remember these capabilities to send on next periodic update."""
        if not isinstance(capabilities, list):
            capabilities = [capabilities]
        self.last_capabilities = capabilities

    @periodic_task.periodic_task
    def publish_service_capabilities(self, context):
        if self.last_capabilities:
            self.scheduler_rpcapi.update_service_capabilities(context,
                    self.service_name, self.host, self.last_capabilities)

    @periodic_task.periodic_task
    def _report_driver_status(self, context):
        curr_time = time.time()
        if curr_time - self._last_host_check > CONF.host_state_interval:
            self._last_host_check = curr_time
            LOG.info(_("Updating host status"))
            # This will grab info about the host and queue it
            # to be sent to the Schedulers.
            capabilities = self.driver.get_host_stats(refresh=True)
            for capability in (capabilities if isinstance(capabilities, list)
                               else [capabilities]):
                capability['host_ip'] = CONF.my_ip
            self.update_service_capabilities(capabilities)

注:Manager---->SchedulerDependentManager----->ComputeManager,这里直接写出将ComputeMagnager从父类继承的方法写出来了!

 

3,这些周期函数都实现了什么?

_report_driver_status:

def _report_driver_status(self, context):
        curr_time = time.time()
        if curr_time - self._last_host_check > CONF.host_state_interval:
            self._last_host_check = curr_time
            capabilities = self.driver.get_host_stats(refresh=True)
            for capability in (capabilities if isinstance(capabilities, list)
                               else [capabilities]):
                capability['host_ip'] = CONF.my_ip
            self.update_service_capabilities(capabilities)

调用自身update_service_capabilities的函数,即获知当前driver的状态变化,然后修改了last_capabilities变量;

publish_service_capabilities

def publish_service_capabilities(self, context):
        if self.last_capabilities:
            LOG.debug(_('Notifying Schedulers of capabilities ...'))
            self.scheduler_rpcapi.update_service_capabilities(context,
                    self.service_name, self.host, self.last_capabilities)

查询last_capabilities变量,并相应的做出处理,调用了scheduler_rpcapi,交由schedulerManager处理:
class SchedulerManager():
    def update_service_capabilities(self, context, service_name,
                                    host, capabilities):
        """Process a capability update from a service node."""
        if not isinstance(capabilities, list):
            capabilities = [capabilities]
        for capability in capabilities:
            if capability is None:
                capability = {}
            self.driver.update_service_capabilities(service_name, host,
                                                    capability)
即调用了,nova.scheduler.driver.py :scheduler.updata_service_capabilites方法:
class Scheduler(object):
    def update_service_capabilities(self, service_name, host, capabilities):

        self.host_manager.update_service_capabilities(service_name,
                host, capabilities)
class HostManager(object):
    def update_service_capabilities(self, service_name, host, capabilities):
        """Update the per-service capabilities based on this notification."""

        if service_name != 'compute':
            return
        state_key = (host, capabilities.get('hypervisor_hostname'))
        # Copy the capabilities, so we don't modify the original dict
        capab_copy = dict(capabilities)
        capab_copy["timestamp"] = timeutils.utcnow()  # Reported time
        self.service_states[state_key] = capab_copy

 这两个函数可以看成,一个是向自己内部汇报capabilities的变化,自身manager得知此变化后,向scheduler通报自身的capabilities的变化,以便scheduler在选择主机时作出最合适的选择。


3,openstack中的国际化



openstack中的locale目录下是与国际化有关目录,你可以把这个目录看成是一个词典,一个有限词汇的词典;

在这个词典中,一个词语是用msgid来标示的,可以把这个msgid看成是一个词汇的id,然后再不同的语言中,这个msgid被翻译成不同的语言。

好了,这个词典已经放在这里了,那你程序中改怎么用这个词典呢?

 

step 1,在服务开启时加载字典,

例如在cinder-api的bin文件中可以看到:

from cinder.openstack.common import gettextutils
 gettextutils.install('cinder') 
gettextutils.install  funciton:
*********************************
def install(domain):
     
     gettext.install(domain,
                     localedir=os.environ.get(domain.upper() + '_LOCALEDIR'),
                     unicode=True)*********************************
#从本地目录中加载了domain为cinder的字典

 

step 2,在程序中使用这个字典中的翻译工具:_(msg)

#导入翻译工具
from cinder.openstack.common.gettextutils import _
#使用翻译工具_
LOG.debug(_("Reloading cached file %s") % filename)

这里是一个打印调试log的语句,这里首先会从先前加载的cinder字典中,根据你设置的语言,从相应的字典中查找 msgid为"Reloading cached file"的词汇,然后与filename合并成一个字符串打印出来。

 

其中有一点困惑,我是这样理解的:

在cinder-api 文件中加载了这个字典,那我什么时候可以用你这个字典呢?

我理解为,只要cinder-api这个程序在运行过程中,那么其他任何地方都可以使用这个字典。

 

关于国际化可能有更多的细节,暂时能够快速的按照自己的理解,拿来用一用,日后可以细研究。