MonClient/sub_want&renew_subs

MonClient/sub_want&renew_subs

文件

:MonClient/sub_want&renew_subs

方法:

bool sub_want(string what, version_t start, unsigned flags)
void renew_subs()

涉及的方法:

bool _sub_want(string what, version_t start, unsigned flags)
void _renew_subs()

涉及的数据结构:

map sub_new;
map sub_sent;
  • sub_new中保存的是想要订阅的map/version集合,但是还没有想monitor发起订阅请求
  • sub_sent保存的是已经向monitor发起的map/version集合

细节实现:

在sub_want获取monc_lock锁,主要逻辑都下方到_sub_want中。

bool sub_want(string what, version_t start, unsigned flags) {                                                                                                   
  Mutex::Locker l(monc_lock);                                                                                                                                   
  return _sub_want(what, start, flags);                                                                                                                         
}

向sub_new中注入要订阅的map/version;其主要逻辑是判断要订阅的map/version是否已经在sub_new或者sub_sent中,如果存在就返回false,
否则就将要订阅的map/version添加到sub_new中,并返回true

bool _sub_want(string what, version_t start, unsigned flags) {
  if ((sub_new.count(what) == 0 &&
       sub_sent.count(what) &&
       sub_sent[what].start == start &&
       sub_sent[what].flags == flags) ||
      (sub_new.count(what) &&
       sub_new[what].start == start &&
       sub_new[what].flags == flags))
    return false;
  sub_new[what].start = start;
  sub_new[what].flags = flags;
  return true;
}

将sub_new中要订阅的map/version,发送给monitor服务,并将sub_new中的元素合并到sub_sent中,再清空sub_new。

void renew_subs() {
  Mutex::Locker l(monc_lock);
  _renew_subs();
}

void MonClient::_renew_subs()
{
  assert(monc_lock.is_locked());
  if (sub_new.empty()) {
    ldout(cct, 10) << "renew_subs - empty" << dendl;
    return;
  }

  ldout(cct, 10) << "renew_subs" << dendl;
  if (cur_mon.empty())
    _reopen_session();
  else {
    if (sub_renew_sent == utime_t())
      sub_renew_sent = ceph_clock_now(cct);

    MMonSubscribe *m = new MMonSubscribe;
    m->what = sub_new;
    _send_mon_message(m);

    // update sub_sent with sub_new
    sub_new.insert(sub_sent.begin(), sub_sent.end());
    std::swap(sub_new, sub_sent);
    sub_new.clear();
  }
}

你可能感兴趣的:(ceph)