forked from OpenAtomFoundation/pika
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pika_repl_client_conn.cc
244 lines (217 loc) · 10.1 KB
/
pika_repl_client_conn.cc
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Copyright (c) 2015-present, Qihoo, Inc. All rights reserved.
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
#include "include/pika_repl_client_conn.h"
#include <sys/time.h>
#include "include/pika_server.h"
#include "include/pika_rm.h"
#include "slash/include/slash_string.h"
#include "include/pika_rm.h"
#include "include/pika_server.h"
extern PikaConf* g_pika_conf;
extern PikaServer* g_pika_server;
extern PikaReplicaManager* g_pika_rm;
PikaReplClientConn::PikaReplClientConn(int fd,
const std::string& ip_port,
pink::Thread* thread,
void* worker_specific_data,
pink::PinkEpoll* epoll)
: pink::PbConn(fd, ip_port, thread, epoll) {
}
bool PikaReplClientConn::IsTableStructConsistent(
const std::vector<TableStruct>& current_tables,
const std::vector<TableStruct>& expect_tables) {
if (current_tables.size() != expect_tables.size()) {
return false;
}
for (const auto& table_struct : current_tables) {
if (find(expect_tables.begin(), expect_tables.end(),
table_struct) == expect_tables.end()) {
return false;
}
}
return true;
}
int PikaReplClientConn::DealMessage() {
std::shared_ptr<InnerMessage::InnerResponse> response = std::make_shared<InnerMessage::InnerResponse>();
response->ParseFromArray(rbuf_ + cur_pos_ - header_len_, header_len_);
switch (response->type()) {
case InnerMessage::kMetaSync:
{
ReplClientTaskArg* task_arg = new ReplClientTaskArg(response, std::dynamic_pointer_cast<PikaReplClientConn>(shared_from_this()));
g_pika_rm->ScheduleReplClientBGTask(&PikaReplClientConn::HandleMetaSyncResponse, static_cast<void*>(task_arg));
break;
}
case InnerMessage::kDBSync:
{
ReplClientTaskArg* task_arg = new ReplClientTaskArg(response, std::dynamic_pointer_cast<PikaReplClientConn>(shared_from_this()));
g_pika_rm->ScheduleReplClientBGTask(&PikaReplClientConn::HandleDBSyncResponse, static_cast<void*>(task_arg));
break;
}
case InnerMessage::kTrySync:
{
ReplClientTaskArg* task_arg = new ReplClientTaskArg(response, std::dynamic_pointer_cast<PikaReplClientConn>(shared_from_this()));
g_pika_rm->ScheduleReplClientBGTask(&PikaReplClientConn::HandleTrySyncResponse, static_cast<void*>(task_arg));
break;
}
case InnerMessage::kBinlogSync:
{
DispatchBinlogRes(response);
break;
}
default:
break;
}
return 0;
}
void PikaReplClientConn::HandleMetaSyncResponse(void* arg) {
ReplClientTaskArg* task_arg = static_cast<ReplClientTaskArg*>(arg);
std::shared_ptr<pink::PbConn> conn = task_arg->conn;
std::shared_ptr<InnerMessage::InnerResponse> response = task_arg->res;
g_pika_server->ReceiveMetaSyncResponse();
if (response->code() != InnerMessage::kOk) {
std::string reply = response->has_reply() ? response->reply() : "";
LOG(WARNING) << "Meta Sync Failed: " << reply;
g_pika_server->SyncError();
conn->NotifyClose();
delete task_arg;
return;
}
const InnerMessage::InnerResponse_MetaSync meta_sync = response->meta_sync();
if (g_pika_conf->classic_mode() != meta_sync.classic_mode()) {
LOG(WARNING) << "Self in " << (g_pika_conf->classic_mode() ? "classic" : "sharding")
<< " mode, but master in " << (meta_sync.classic_mode() ? "classic" : "sharding")
<< " mode, failed to establish master-slave relationship";
g_pika_server->SyncError();
conn->NotifyClose();
delete task_arg;
return;
}
std::vector<TableStruct> master_table_structs;
for (int idx = 0; idx < meta_sync.tables_info_size(); ++idx) {
InnerMessage::InnerResponse_MetaSync_TableInfo table_info = meta_sync.tables_info(idx);
master_table_structs.emplace_back(table_info.table_name(), table_info.partition_num());
}
bool force_full_sync = g_pika_server->force_full_sync();
std::vector<TableStruct> self_table_structs = g_pika_conf->table_structs();
if (!force_full_sync
&& !PikaReplClientConn::IsTableStructConsistent(self_table_structs, master_table_structs)) {
LOG(WARNING) << "Self table structs inconsistent with master"
<< ", failed to establish master-slave relationship";
g_pika_server->SyncError();
conn->NotifyClose();
delete task_arg;
return;
}
if (force_full_sync) {
LOG(INFO) << "Force full sync, need to rebuild table struct first";
// Purge and rebuild Table Struct consistent with master
if (!g_pika_server->RebuildTableStruct(master_table_structs)) {
LOG(WARNING) << "Need force full sync but rebuild table struct error"
<< ", failed to establish master-slave relationship";
g_pika_server->SyncError();
conn->NotifyClose();
delete task_arg;
return;
}
g_pika_server->PurgeDir(g_pika_conf->trash_path());
}
g_pika_server->MetaSyncDone();
g_pika_conf->SetWriteBinlog("yes");
LOG(INFO) << "Finish to handle meta sync response";
delete task_arg;
}
void PikaReplClientConn::HandleDBSyncResponse(void* arg) {
ReplClientTaskArg* task_arg = static_cast<ReplClientTaskArg*>(arg);
std::shared_ptr<pink::PbConn> conn = task_arg->conn;
std::shared_ptr<InnerMessage::InnerResponse> response = task_arg->res;
const InnerMessage::InnerResponse_DBSync db_sync_response = response->db_sync();
int32_t session_id = db_sync_response.session_id();
const InnerMessage::Partition partition_response = db_sync_response.partition();
std::string table_name = partition_response.table_name();
uint32_t partition_id = partition_response.partition_id();
std::shared_ptr<Partition> partition = g_pika_server->GetTablePartitionById(table_name, partition_id);
if (!partition) {
LOG(WARNING) << "Partition: " << table_name << ":" << partition_id << " Not Found";
delete task_arg;
return;
}
if (response->code() != InnerMessage::kOk) {
partition->SetReplState(ReplState::kError);
std::string reply = response->has_reply() ? response->reply() : "";
LOG(WARNING) << "DBSync Failed: " << reply;
delete task_arg;
return;
}
g_pika_rm->AddSyncSlavePartition(RmNode(g_pika_server->master_ip(),
g_pika_server->master_port(), table_name, partition_id, session_id));
std::string partition_name = partition->GetPartitionName();
partition->SetReplState(ReplState::kWaitDBSync);
LOG(INFO) << "Partition: " << partition_name << " Need Wait To Sync";
delete task_arg;
}
void PikaReplClientConn::HandleTrySyncResponse(void* arg) {
ReplClientTaskArg* task_arg = static_cast<ReplClientTaskArg*>(arg);
std::shared_ptr<pink::PbConn> conn = task_arg->conn;
std::shared_ptr<InnerMessage::InnerResponse> response = task_arg->res;
if (response->code() != InnerMessage::kOk) {
std::string reply = response->has_reply() ? response->reply() : "";
LOG(WARNING) << "TrySync Failed: " << reply;
delete task_arg;
return;
}
const InnerMessage::InnerResponse_TrySync& try_sync_response = response->try_sync();
const InnerMessage::Partition& partition_response = try_sync_response.partition();
std::string table_name = partition_response.table_name();
uint32_t partition_id = partition_response.partition_id();
std::shared_ptr<Partition> partition = g_pika_server->GetTablePartitionById(table_name, partition_id);
if (!partition) {
LOG(WARNING) << "Partition: " << table_name << ":" << partition_id << " Not Found";
delete task_arg;
return;
}
std::string partition_name = partition->GetPartitionName();
if (try_sync_response.reply_code() == InnerMessage::InnerResponse::TrySync::kOk) {
BinlogOffset boffset;
int32_t session_id = try_sync_response.session_id();
partition->SetReplState(ReplState::kConnected);
partition->logger()->GetProducerStatus(&boffset.filenum, &boffset.offset);
g_pika_rm->AddSyncSlavePartition(RmNode(g_pika_server->master_ip(), g_pika_server->master_port(), table_name, partition_id, session_id));
g_pika_server->SendPartitionBinlogSyncAckRequest(table_name, partition_id, boffset, boffset, true);
LOG(INFO) << "Partition: " << partition_name << " TrySync Ok";
} else if (try_sync_response.reply_code() == InnerMessage::InnerResponse::TrySync::kSyncPointBePurged) {
partition->SetReplState(ReplState::kTryDBSync);
LOG(INFO) << "Partition: " << partition_name << " Need To Try DBSync";
} else if (try_sync_response.reply_code() == InnerMessage::InnerResponse::TrySync::kSyncPointLarger) {
partition->SetReplState(ReplState::kError);
LOG(WARNING) << "Partition: " << partition_name << " TrySync Error, Because the invalid filenum and offset";
} else if (try_sync_response.reply_code() == InnerMessage::InnerResponse::TrySync::kError) {
partition->SetReplState(ReplState::kError);
LOG(WARNING) << "Partition: " << partition_name << " TrySync Error";
}
delete task_arg;
}
void PikaReplClientConn::DispatchBinlogRes(const std::shared_ptr<InnerMessage::InnerResponse> res) {
// partition to a bunch of binlog chips
std::unordered_map<PartitionInfo, std::vector<int>*, hash_partition_info> par_binlog;
for (int i = 0; i < res->binlog_sync_size(); ++i) {
const InnerMessage::InnerResponse::BinlogSync& binlog_res = res->binlog_sync(i);
// hash key: table + partition_id
PartitionInfo p_info(binlog_res.partition().table_name(),
binlog_res.partition().partition_id());
if (par_binlog.find(p_info) == par_binlog.end()) {
par_binlog[p_info] = new std::vector<int>();
}
par_binlog[p_info]->push_back(i);
}
for (auto& binlog_nums : par_binlog) {
RmNode node(binlog_nums.first.table_name_, binlog_nums.first.partition_id_);
g_pika_rm->SetSlaveLastRecvTime(node, slash::NowMicros());
g_pika_rm->GetPikaReplClient()->ScheduleWriteBinlogTask(
binlog_nums.first.table_name_ + std::to_string(binlog_nums.first.partition_id_),
res,
std::dynamic_pointer_cast<PikaReplClientConn>(shared_from_this()),
reinterpret_cast<void*>(binlog_nums.second));
}
}