MCPcopy Create free account
hub / github.com/Tencent/phxsql / UpdateMasterStatus

Method UpdateMasterStatus

phxsqlproxy/group_status_cache.cpp:69–122  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

67}
68
69int GroupStatusCache::UpdateMasterStatus() {
70 if (std::string(config_->GetSpecifiedMasterIP()) != "") {
71 return 0;
72 }
73
74 std::string master_ip = "";
75 uint32_t expired_time = 0;
76 uint32_t version = 0;
77 std::shared_ptr<PhxBinlogClient> client = PhxBinlogClientPlatformInfo::GetDefault()->GetClientFactory()
78 ->CreateClient();
79 int ret = client->GetMaster(&master_ip, &expired_time, &version);
80 if (ret != 0) {
81 MonitorPluginEntry::GetDefault()->GetMonitorPlugin()->GetMasterInBinLogFail();
82 LogError("%s:%d GetMaster failed ret %d", __func__, __LINE__, ret);
83
84 if (!config_->TryBestIfBinlogsvrDead() || membership_.size() == 0) {
85 return -__LINE__;
86 }
87
88 // try to get master_ip from other binlogsvr
89 ret = client->GetGlobalMaster(membership_, &master_ip, &expired_time, &version, /*require_majority*/false);
90 if (ret != 0) {
91 LogError("%s:%d GetGlobalMaster failed ret %d", __func__, __LINE__, ret);
92 return -__LINE__;
93 }
94
95 if (master_ip == worker_config_->listen_ip_) {
96 // can not believe I'm a master_ip if binlogsvr is dead
97 LogError("%s:%d GetGlobalMaster succ but get %s", __func__, __LINE__, master_ip.c_str());
98 return -__LINE__;
99 }
100 }
101
102 if (!IsMasterValid(master_ip, expired_time)) {
103 LogError("%s:%d GetMaster success but (%s:%u) expired", __func__, __LINE__, master_ip.c_str(), expired_time);
104 return -__LINE__;
105 }
106
107 if (version < master_status_.version_) {
108 LogError("%s:%d GetMaster success but (%s:%u) version smaller than (%s:%u)", __func__, __LINE__,
109 master_ip.c_str(), version, master_status_.master_ip_.c_str(), master_status_.version_);
110 return -__LINE__;
111 }
112
113 // version >= master_status_.version_
114 if (version > master_status_.version_ || expired_time < master_status_.expired_time_) {
115 RWLockManager lock(&master_mutex_, RWLockManager::WRITE);
116 master_status_.master_ip_ = master_ip;
117 master_status_.expired_time_ = expired_time;
118 master_status_.version_ = version;
119 }
120
121 return 0;
122}
123
124int GroupStatusCache::UpdateMembership() {
125 std::shared_ptr<PhxBinlogClient> client = PhxBinlogClientPlatformInfo::GetDefault()->GetClientFactory()

Callers

nothing calls this directly

Calls 9

LogErrorFunction · 0.85
CreateClientMethod · 0.80
GetClientFactoryMethod · 0.80
GetMasterInBinLogFailMethod · 0.80
GetMonitorPluginMethod · 0.80
GetGlobalMasterMethod · 0.80
GetSpecifiedMasterIPMethod · 0.45
GetMasterMethod · 0.45

Tested by

no test coverage detected