nova conductor这个服务启动的入口函数在nova/cmd/conductor.py 中,我们重点关注heatbeat服务
def main()://调用service的构造函数,其位于nova/service.py中server = service.Service.create(binary='nova-conductor',topic=CONF.conductor.topic,manager=CONF.conductor.manager)workers = CONF.conductor.workers or processutils.get_worker_count()service.serve(server, workers=workers)service.wait()
service的构造函数class Service(service.Service):"""Service object for binaries running on hosts.A service takes a manager and enables rpc by listening to queues basedon topic. It also periodically runs tasks on the manager and reportsits state to the database services table."""def __init__(self, host, binary, topic, manager, report_interval=None,periodic_enable=None, periodic_fuzzy_delay=None,periodic_interval_max=None, db_allowed=True,*args, **kwargs):super(Service, self).__init__()self.host = hostself.binary = binaryself.topic = topicself.manager_class_name = manager
//调用servicegroupself.servicegroup_api = servicegroup.API()servicegroup的函数位于nova/servicegroup/api.py 中,最终调用join函数
class API(object):def join(self, member, group, service=None):"""Add a new member to a service group.:param member: the joined member ID/name:param group: the group ID/name, of the joined member:param service: a `nova.service.Service` object"""return self._driver.join(member, group, service)
在service中发现有两种调用数据库的方法,这里我们采用的是mysql,因此我们用的是db
_driver_name_class_mapping = {'db': 'nova.servicegroup.drivers.db.DbDriver','mc': 'nova.servicegroup.drivers.mc.MemcachedDriver'
}
所以driver的join函数最终是在nova/servicegroup/drivers/db.py 中实现的
class DbDriver(base.Driver):def __init__(self, *args, **kwargs):self.service_down_time = CONF.service_down_timedef join(self, member, group, service=None):"""Add a new member to a service group.:param member: the joined member ID/name:param group: the group ID/name, of the joined member:param service: a `nova.service.Service` object"""LOG.debug('DB_Driver: join new ServiceGroup member %(member)s to ''the %(group)s group, service = %(service)s',{'member': member, 'group': group,'service': service})if service is None:raise RuntimeError(_('service is a mandatory argument for DB based'' ServiceGroup driver'))report_interval = service.report_intervalif report_interval:
核心代码可见是注册了一个time来定时调用_report_stateservice.tg.add_timer(report_interval, self._report_state,api.INITIAL_REPORTING_DELAY, service)
其中_report_state 同样在nova/servicegroup/drivers/db.py 中实现def _report_state(self, service):"""Update the state of this service in the datastore."""try:
可见heatbeat的最终实现就是从db中读到report_count然后自加后在保存回去service.service_ref.report_count += 1service.service_ref.save()# TODO(termie): make this pattern be more elegant.if getattr(service, 'model_disconnected', False):service.model_disconnected = FalseLOG.info(_LI('Recovered from being unable to report status.'))except messaging.MessagingTimeout:# NOTE(johngarbutt) during upgrade we will see messaging timeouts# as nova-conductor is restarted, so only log this error once.if not getattr(service, 'model_disconnected', False):service.model_disconnected = TrueLOG.warning(_LW('Lost connection to nova-conductor ''for reporting service status.'))通过在nova/servicegroup/drivers/db.py 中实现了检查服务是否up的函数def is_up(self, service_ref):"""Moved from nova.utilsCheck whether a service is up based on last heartbeat."""# Keep checking 'updated_at' if 'last_seen_up' isn't set.# Should be able to use only 'last_seen_up' in the M releaselast_heartbeat = (service_ref.get('last_seen_up') orservice_ref['updated_at'] or service_ref['created_at'])if isinstance(last_heartbeat, six.string_types):# NOTE(russellb) If this service_ref came in over rpc via# conductor, then the timestamp will be a string and needs to be# converted back to a datetime.last_heartbeat = timeutils.parse_strtime(last_heartbeat)else:# Objects have proper UTC timezones, but the timeutils comparison# below does not (and will fail)last_heartbeat = last_heartbeat.replace(tzinfo=None)# Timestamps in DB are UTC.elapsed = timeutils.delta_seconds(last_heartbeat, timeutils.utcnow())
可见是否up的判断条件是否两次heatbeat的时间是否小于service_down_time。而这个之是在init函数中通过配置文件读到的self.service_down_time = CONF.service_down_timeis_up = abs(elapsed) <= self.service_down_timeif not is_up:LOG.debug('Seems service %(binary)s on host %(host)s is down. ''Last heartbeat was %(lhb)s. Elapsed time is %(el)s',{'binary': service_ref.get('binary'),'host': service_ref.get('host'),'lhb': str(last_heartbeat), 'el': str(elapsed)})return is_up这样当通过nova service-list 来查询服务是否up是回调到nova/api/openstack/compute/services.pylass ServiceController(wsgi.Controller):def _get_service_detail(self, svc, additional_fields):
//service_is_up 最终会调到前面说的db中的servicegroup/api.py 中的service_is_upalive = self.servicegroup_api.service_is_up(svc)state = (alive and "up") or "down"active = 'enabled'if svc['disabled']:active = 'disabled'service_detail = {'binary': svc['binary'],'host': svc['host'],'id': svc['id'],'zone': svc['availability_zone'],'status': active,'state': state,'updated_at': svc['updated_at'],'disabled_reason': svc['disabled_reason']}for field in additional_fields:service_detail[field] = svc[field]return service_detailservice_is_up 源码如下,可见最终会调用到前面将的db中的is_up来判断service是否updef service_is_up(self, member):"""Check if the given member is up."""# NOTE(johngarbutt) no logging in this method,# so this doesn't slow down the schedulerif member.get('forced_down'):return Falsereturn self._driver.is_up(member)

nova service的heatbeat机制相关推荐

  1. SAP gateway 后台系统的 OData service 服务探测机制实现原理

    Created by Wang, Jerry, last modified on Jan 17, 2015

  2. Android - 广播机制和Service

    花了几天,想实现定位功能,使用的是百度的服务,有时真的很无奈,就是一小小个问题,就能折腾好几天. 首先,我是在主线程(Fragment)中单独进行定位,发现不起作用. 然后我想使用Service和广播 ...

  3. openstack nova 源码分析3-nova目录下的service.py

    nova下的service.py的源码,今天阅读之后 直接就把我理解的以注释的形式添加到了源码中,有些地方不好或者是错了,希望大家帮我指出! import inspect import os impo ...

  4. 探讨8.0版本下后台service存活机制及保活

    前段时间时间对targetsdkversion进行升级,结果发现了一个问题: 在不升级前,app退出后,后台service可以存活很长一段时间:而升级后,8.0以下版本手机还是一样,但是8.0及以上版 ...

  5. ROS基础(二):ros通讯之服务(service)机制

    上一章内容链接: ROS基础(一):ROS通讯之话题(topic)通讯 目录 一.概念 二.实例 1. 小乌龟例程中的service 2. 自定义service 3. 创建服务器节点与客户端节点(c+ ...

  6. Nova如何统计节点硬件资源

    引言 当我们在使用那些建设在OpenStack之上的云平台服务的时候,往往在概览页面都有一个明显的位置用来展示当前集群的一些资源使用情况,如,CPU,内存,硬盘等资源的总量.使用量.剩余量.而且,每当 ...

  7. openstack-ocata版本nova MQ(rpc)接收端(server)浅析

    首先我们看服务端启动过程,在此取compute节点为例: 在nova安装时,会调取pbr模块,根据setup.cfg中相关信息生成启动服务的console_scripts,详情可见pbr官方文档:ht ...

  8. Nova Conductor服务

    Nova Conductor服务的大部分方法都是数据库的查询操作(/nova/conductor/manager.py ConductorManager类).主 要作用是避免Nova Compute服 ...

  9. Nova API的执行过程

    一 用户命令到的HTTP请求 一般的 Openstack 用户和管理员能够通过执行简易的 Openstack Commands 来管理和使用 Openstack . 但需要注意的是,Openstack ...

最新文章

  1. 几种Windows进程通信
  2. this.class.getClassLoader().getResourceAsStream与this.class.getResourceAsStream
  3. 实现双击IE9的Tab键关闭当前页面的功能
  4. Online Shopping网上商城数据库设计
  5. webpack打包的两种方式
  6. DataWhale sklearn学习笔记(一)
  7. Cannot resolve method ‘getTableEnvironment(org.apache.flink.api.java.ExecutionEnvironment)‘
  8. httpClient创建对象、设置超时
  9. node 单个表加条件查询
  10. Java实现大数操作
  11. CAN笔记(4) 协议基本概念
  12. Codeforces 474D. Flowers
  13. 阶段二第一部分:第2章 Nginx进阶与实战
  14. jquery json string conver to object
  15. win10系统Onedrive登录输入邮箱后界面空白的解决方法
  16. 对于html转jsp乱码问题
  17. 【转】DRY原则的误区
  18. 3---条形图(matplotlib)
  19. 教你50招 XP用户不看是你的损失
  20. java装箱_谈谈Java的自动装箱和拆箱

热门文章

  1. 前端开发必备之MDN(API查阅文档)
  2. shopify是什么_Shopify与WooCommerce –哪个是更好的平台? (比较)
  3. Python简单木马编写
  4. ENDNOTE 添加国标参考文献格式
  5. C语言将一字符串输入到数组(长度不超过80)后将其输出, 将其中的英文字母大小写互换后再将整个字符串输出。
  6. Spring Boot 下 zookeeper搭配dubbo 服务注册与发现 搭建
  7. 移动安全事件总结情况说明
  8. 看看你在古代是什么学历?(附真题)
  9. R语言将数据导出到csv时出现科学计数表示
  10. google浏览器Chrome离线安装扩展主题皮肤教程