forked from Tencent/phxsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroup_status_cache.cpp
More file actions
207 lines (171 loc) · 6.95 KB
/
group_status_cache.cpp
File metadata and controls
207 lines (171 loc) · 6.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
Tencent is pleased to support the open source community by making PhxSQL available.
Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the GNU General Public License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
https://opensource.org/licenses/GPL-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
#include "group_status_cache.h"
#include "phxbinlogsvr/client/phxbinlog_client_platform_info.h"
#include "phxcomm/phx_log.h"
#include "phxcomm/lock_manager.h"
#include "phxsqlproxyconfig.h"
#include "monitor_plugin.h"
#include "phxsqlproxyutil.h"
#include <poll.h>
#include <pthread.h>
#include <memory>
#include <vector>
#include <string>
using phxbinlogsvr::PhxBinlogClientPlatformInfo;
using phxbinlogsvr::PhxBinlogClient;
using phxsql::LogError;
using phxsql::LogInfo;
using phxsql::LogVerbose;
using phxsql::RWLockManager;
namespace phxsqlproxy {
GroupStatusCache::GroupStatusCache(PHXSqlProxyConfig * config, WorkerConfig_t * worker_config) :
config_(config), worker_config_(worker_config) {
pthread_rwlock_init(&master_mutex_, NULL);
pthread_rwlock_init(&membership_mutex_, NULL);
}
GroupStatusCache::~GroupStatusCache() {
pthread_rwlock_destroy(&master_mutex_);
pthread_rwlock_destroy(&membership_mutex_);
}
void GroupStatusCache::run() {
while (true) {
int sleep_ms = 300;
int ret = UpdateMasterStatus();
if (ret != 0) {
sleep_ms = 100;
LogError("UpdateMasterStatus ret %d", ret);
}
LogVerbose("UpdateMasterStatus ret %d", ret);
ret = UpdateMembership();
if (ret != 0) {
sleep_ms = 100;
LogError("UpdateMembership ret %d", ret);
}
LogVerbose("UpdateMembership ret %d", ret);
poll(0, 0, sleep_ms);
}
}
int GroupStatusCache::UpdateMasterStatus() {
if (std::string(config_->GetSpecifiedMasterIP()) != "") {
return 0;
}
std::string master_ip = "";
uint32_t expired_time = 0;
uint32_t version = 0;
std::shared_ptr<PhxBinlogClient> client = PhxBinlogClientPlatformInfo::GetDefault()->GetClientFactory()
->CreateClient();
int ret = client->GetMaster(&master_ip, &expired_time, &version);
if (ret != 0) {
MonitorPluginEntry::GetDefault()->GetMonitorPlugin()->GetMasterInBinLogFail();
LogError("%s:%d GetMaster failed ret %d", __func__, __LINE__, ret);
if (!config_->TryBestIfBinlogsvrDead() || membership_.size() == 0) {
return -__LINE__;
}
// try to get master_ip from other binlogsvr
ret = client->GetGlobalMaster(membership_, &master_ip, &expired_time, &version, /*require_majority*/false);
if (ret != 0) {
LogError("%s:%d GetGlobalMaster failed ret %d", __func__, __LINE__, ret);
return -__LINE__;
}
if (master_ip == worker_config_->listen_ip_) {
// can not believe I'm a master_ip if binlogsvr is dead
LogError("%s:%d GetGlobalMaster succ but get %s", __func__, __LINE__, master_ip.c_str());
return -__LINE__;
}
}
if (!IsMasterValid(master_ip, expired_time)) {
LogError("%s:%d GetMaster success but (%s:%u) expired", __func__, __LINE__, master_ip.c_str(), expired_time);
return -__LINE__;
}
if (version < master_status_.version_) {
LogError("%s:%d GetMaster success but (%s:%u) version smaller than (%s:%u)", __func__, __LINE__,
master_ip.c_str(), version, master_status_.master_ip_.c_str(), master_status_.version_);
return -__LINE__;
}
// version >= master_status_.version_
if (version > master_status_.version_ || expired_time < master_status_.expired_time_) {
RWLockManager lock(&master_mutex_, RWLockManager::WRITE);
master_status_.master_ip_ = master_ip;
master_status_.expired_time_ = expired_time;
master_status_.version_ = version;
}
return 0;
}
int GroupStatusCache::UpdateMembership() {
std::shared_ptr<PhxBinlogClient> client = PhxBinlogClientPlatformInfo::GetDefault()->GetClientFactory()
->CreateClient();
std::vector<std::string> membership;
uint32_t port = 0;
int ret = client->GetMemberList(&membership, &port);
if (ret == 0 && membership.size() == 0) {
LogError("%s:%d ret 0 but no member inside", __func__, __LINE__);
ret = -__LINE__;
}
if (ret == 0) {
LogVerbose("%s:%d get membership ret size %zu", __func__, __LINE__, membership_.size());
if (membership != membership_) {
RWLockManager lock(&membership_mutex_, RWLockManager::WRITE);
membership_ = std::move(membership);
}
}
return ret;
}
bool GroupStatusCache::IsMasterValid(const std::string & master_ip, uint32_t expired_time) {
if (master_ip == "") {
return false;
}
if (expired_time < (uint32_t)time(NULL)) {
return false;
}
return true;
}
int GroupStatusCache::GetMaster(std::string & master_ip) {
if (std::string(config_->GetSpecifiedMasterIP()) != "") {
master_ip = config_->GetSpecifiedMasterIP();
LogVerbose("master is specified to [%s]", master_ip.c_str());
return 0;
}
RWLockManager lock(&master_mutex_, RWLockManager::READ);
if (IsMasterValid(master_status_.master_ip_, master_status_.expired_time_)) {
master_ip = master_status_.master_ip_;
return 0;
}
MonitorPluginEntry::GetDefault()->GetMonitorPlugin()->CheckMasterInvalid();
LogError("get master [%s] expiretime [%u] current [%u]", master_status_.master_ip_.c_str(),
master_status_.expired_time_, (uint32_t)time(NULL));
return -__LINE__;
}
bool GroupStatusCache::IsMember(const std::string & ip) {
RWLockManager lock(&membership_mutex_, RWLockManager::READ);
for (auto & itr : membership_) {
if (itr == ip) {
return true;
}
}
return false;
}
int GroupStatusCache::GetSlave(const std::string & master_ip, std::string & slave_ip) {
RWLockManager lock(&membership_mutex_, RWLockManager::READ);
slave_ip = "";
uint64_t t_last_failure = 0;
for (auto & itr : membership_) {
if (itr != master_ip) {
if (slave_ip == "" || last_failure_[itr] < t_last_failure) {
slave_ip = itr;
t_last_failure = last_failure_[itr];
}
}
}
return slave_ip != "" ? 0 : -1;
}
void GroupStatusCache::MarkFailure(const std::string & ip) {
RWLockManager lock(&membership_mutex_, RWLockManager::WRITE);
last_failure_[ip] = GetTimestampMS();
}
}