-
Notifications
You must be signed in to change notification settings - Fork 526
/
consistency_check.cpp
305 lines (285 loc) · 9.98 KB
/
consistency_check.cpp
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
* Copyright (c) 2020 NetEase Inc.
*
* Licensed under the Apache 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
*
* http://www.apache.org/licenses/LICENSE-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.
*/
/*
* Project: curve
* File Created: Saturday, 29th June 2019 12:39:40 pm
* Author: tongguangxun
*/
#include <gflags/gflags.h>
#include "src/tools/consistency_check.h"
DEFINE_string(filename, "", "filename to check consistency");
DEFINE_bool(check_hash, true, R"(用户需要先确认copyset的applyindex一致之后
再去查copyset内容是不是一致。通常需要先设置
check_hash = false先检查copyset的applyindex是否一致
如果一致了再设置check_hash = true,
检查copyset内容是不是一致)");
DEFINE_uint32(chunkServerBasePort, 8200, "base port of chunkserver");
DECLARE_string(mdsAddr);
namespace curve {
namespace tool {
std::ostream& operator<<(std::ostream& os, const CopySet& copyset) {
os << "logicPoolId = " << copyset.first
<< ",copysetId = " << copyset.second;
return os;
}
std::ostream& operator<<(std::ostream& os, const CsAddrsType& csAddrs) {
std::vector<std::string> ipVec;
std::vector<uint32_t> seqVec;
for (uint32_t i = 0; i < csAddrs.size(); ++i) {
std::string ip;
uint32_t port;
if (curve::common::NetCommon::SplitAddrToIpPort(csAddrs[i],
&ip, &port)) {
uint32_t csSeq = port - FLAGS_chunkServerBasePort;
ipVec.emplace_back(ip);
seqVec.emplace_back(csSeq);
}
}
os << "hosts:[";
for (uint32_t i = 0; i < ipVec.size(); ++i) {
if (i != 0) {
os << ",";
}
os << ipVec[i];
}
os << "]";
os << ",chunkservers:[";
for (uint32_t i = 0; i < seqVec.size(); ++i) {
if (i != 0) {
os << ",";
}
os << "chunkserver" << seqVec[i];
}
os << "]";
return os;
}
ConsistencyCheck::ConsistencyCheck(
std::shared_ptr<NameSpaceToolCore> nameSpaceToolCore,
std::shared_ptr<ChunkServerClient> csClient) :
nameSpaceToolCore_(nameSpaceToolCore),
csClient_(csClient),
inited_(false) {
}
bool ConsistencyCheck::SupportCommand(const std::string& command) {
return (command == kCheckConsistencyCmd);
}
int ConsistencyCheck::Init() {
if (!inited_) {
int res = nameSpaceToolCore_->Init(FLAGS_mdsAddr);
if (res != 0) {
std::cout << "Init nameSpaceToolCore fail!" << std::endl;
return -1;
}
inited_ = true;
}
return 0;
}
int ConsistencyCheck::RunCommand(const std::string &cmd) {
if (Init() != 0) {
std::cout << "Init ConsistencyCheck failed" << std::endl;
return -1;
}
if (cmd == kCheckConsistencyCmd) {
return CheckFileConsistency(FLAGS_filename, FLAGS_check_hash);
} else {
std::cout << "Command not supported!" << std::endl;
return -1;
}
}
int ConsistencyCheck::CheckFileConsistency(const std::string& fileName,
bool checkHash) {
std::set<CopySet> copysets;
int res = FetchFileCopyset(fileName, ©sets);
if (res != 0) {
std::cout << "FetchFileCopyset of " << fileName << " fail!"
<< std::endl;
return -1;
}
for (auto copyset : copysets) {
res = CheckCopysetConsistency(copyset, checkHash);
if (res != 0) {
std::cout << "CheckCopysetConsistency fail!" << std::endl;
return -1;
}
}
std::cout << "consistency check success!" << std::endl;
return 0;
}
void ConsistencyCheck::PrintHelp(const std::string &cmd) {
if (!SupportCommand(cmd)) {
std::cout << "Command not supported!" << std::endl;
return;
}
std::cout << "Example: " << std::endl;
std::cout << "curve_ops_tool check-consistency -filename=/test [-check_hash=false]" << std::endl; // NOLINT
}
int ConsistencyCheck::FetchFileCopyset(const std::string& fileName,
std::set<CopySet>* copysets) {
std::vector<PageFileSegment> segments;
int res = nameSpaceToolCore_->GetFileSegments(fileName, &segments);
if (res != 0) {
std::cout << "GetFileSegments from mds fail!" << std::endl;
return -1;
}
for (const auto& segment : segments) {
PoolIdType lpid = segment.logicalpoolid();
for (int i = 0; i < segment.chunks_size(); ++i) {
const auto& chunk = segment.chunks(i);
CopySet copyset(lpid, chunk.copysetid());
copysets->emplace(copyset);
chunksInCopyset_[copyset].emplace(chunk.chunkid());
}
}
return 0;
}
int ConsistencyCheck::CheckCopysetConsistency(
const CopySet copyset,
bool checkHash) {
std::vector<ChunkServerLocation> csLocs;
int res = nameSpaceToolCore_->GetChunkServerListInCopySet(
copyset.first,
copyset.second,
&csLocs);
if (res != 0) {
std::cout << "GetServerList info failed, exit consistency check!"
<< std::endl;
return -1;
}
std::vector<std::string> csAddrs;
for (const auto& csLoc : csLocs) {
std::string hostIp = csLoc.hostip();
uint64_t port = csLoc.port();
std::string csAddr = hostIp + ":" + std::to_string(port);
csAddrs.emplace_back(csAddr);
}
// 检查当前copyset的chunkserver内容是否一致
if (checkHash) {
// 先检查apply index是否一致
res = CheckApplyIndex(copyset, csAddrs);
if (res != 0) {
std::cout << "Apply index not match when check hash!" << std::endl;
return -1;
}
return CheckCopysetHash(copyset, csAddrs);
} else {
return CheckApplyIndex(copyset, csAddrs);
}
}
int ConsistencyCheck::GetCopysetStatusResponse(
const std::string& csAddr,
const CopySet copyset,
CopysetStatusResponse* response) {
int res = csClient_->Init(csAddr);
if (res != 0) {
std::cout << "Init chunkserverClient to " << csAddr
<< " fail!" << std::endl;
return -1;
}
CopysetStatusRequest request;
curve::common::Peer *peer = new curve::common::Peer();
peer->set_address(csAddr);
request.set_logicpoolid(copyset.first);
request.set_copysetid(copyset.second);
request.set_allocated_peer(peer);
request.set_queryhash(false);
res = csClient_->GetCopysetStatus(request, response);
if (res != 0) {
std::cout << "GetCopysetStatus from " << csAddr
<< " fail!" << std::endl;
return -1;
}
return 0;
}
int ConsistencyCheck::CheckCopysetHash(const CopySet& copyset,
const CsAddrsType& csAddrs) {
for (const auto& chunkId : chunksInCopyset_[copyset]) {
Chunk chunk(copyset.first, copyset.second, chunkId);
int res = CheckChunkHash(chunk, csAddrs);
if (res != 0) {
std::cout << "{" << chunk
<< "," << csAddrs << "}" << std::endl;
return -1;
}
}
return 0;
}
int ConsistencyCheck::CheckChunkHash(const Chunk& chunk,
const CsAddrsType& csAddrs) {
std::string preHash;
std::string curHash;
bool first = true;
for (const auto& csAddr : csAddrs) {
int res = csClient_->Init(csAddr);
if (res != 0) {
std::cout << "Init chunkserverClient to " << csAddr
<< " fail!" << std::endl;
return -1;
}
res = csClient_->GetChunkHash(chunk, &curHash);
if (res != 0) {
std::cout << "GetChunkHash from " << csAddr << " fail" << std::endl;
return -1;
}
if (first) {
preHash = curHash;
first = false;
continue;
}
if (curHash != preHash) {
std::cout << "Chunk hash not equal!" << std::endl;
std::cout << "previous chunk hash = " << preHash
<< ", current hash = " << curHash << std::endl;
return -1;
}
}
return 0;
}
int ConsistencyCheck::CheckApplyIndex(const CopySet copyset,
const CsAddrsType& csAddrs) {
uint64_t preIndex;
uint64_t curIndex;
bool first = true;
int ret = 0;
for (const auto& csAddr : csAddrs) {
CopysetStatusResponse response;
int res = GetCopysetStatusResponse(csAddr, copyset, &response);
if (res != 0) {
std::cout << "GetCopysetStatusResponse from " << csAddr
<< " fail" << std::endl;
ret = -1;
break;
}
curIndex = response.knownappliedindex();
if (first) {
preIndex = curIndex;
first = false;
continue;
}
if (curIndex != preIndex) {
std::cout << "Apply index not equal!" << std::endl;
std::cout << "previous apply index " << preIndex
<< ", current index = " << curIndex << std::endl;
ret = -1;
break;
}
}
if (ret != 0) {
std::cout << copyset << "," << csAddrs << std::endl;
}
return ret;
}
} // namespace tool
} // namespace curve