前面的博文分析过servicegroup的实现分为db和mc两种,前面已经说过db了,下来看看mc的实现,这里的mc
全称应该是memcached_client的缩写
class MemcachedDriver(base.Driver):
def __init__(self, *args, **kwargs):
#首先在init函数中为mc赋值
self.mc = cache_utils.get_memcached_client(
expiration_time=CONF.service_down_time)
def join(self, member_id, group_id, service=None):
"""Join the given service with its group."""
LOG.debug('Memcached_Driver: join new ServiceGroup member '
'%(member_id)s to the %(group_id)s group, '
'service = %(service)s',
{'member_id': member_id,
'group_id': group_id,
'service': service})
if service is None:
raise RuntimeError(_('service is a mandatory argument for '
'Memcached based ServiceGroup driver'))
report_interval = service.report_interval
#在join函数中和db的实现一样,都是在要监视的servie中创建一个time,且timer的回调函数为_report_state
#这个join函数是否可以放到
#基类base.Driver 中去实现呢?
if report_interval:
service.tg.add_timer(report_interval, self._report_state,
api.INITIAL_REPORTING_DELAY, service)
def is_up(self, service_ref):
"""Moved from nova.utils
Check whether a service is up based on last heartbeat.
"""
#按存入mc的规则组装key
key = "%(topic)s:%(host)s" % service_ref
#判断这个service是否还活着的方法就是判断mc.ge中是否包含key
is_up = self.mc.get(str(key)) is not None
if not is_up:
LOG.debug('Seems service %s is down', key)
return is_up
def _report_state(self, service):
"""Update the state of this service in the datastore."""
try:
key = "%(topic)s:%(host)s" % service.service_ref
#和is_up函数中相同的规则来组装key,通过mc.set函数保存key和当前的ut时间
self.mc.set(str(key),
timeutils.utcnow())
mc机制和db机制实现基本是一样的,不同在写入的存储介质不一样,db是写入到数据库中.
而mc是写入到mem cache中.
servicegroup的mc实现
最新推荐文章于 2023-01-27 13:42:06 发布