forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.cpp
More file actions
307 lines (282 loc) · 11.3 KB
/
Copy pathutil.cpp
File metadata and controls
307 lines (282 loc) · 11.3 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
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
306
307
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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.
#include "recycler/util.h"
#include <glog/logging.h>
#include <cstdint>
#include "common/util.h"
#include "meta-service/meta_service_schema.h"
#include "meta-store/keys.h"
#include "meta-store/txn_kv.h"
#include "meta-store/txn_kv_error.h"
namespace doris::cloud {
namespace config {
extern int32_t recycle_job_lease_expired_ms;
} // namespace config
int get_all_instances(TxnKv* txn_kv, std::vector<InstanceInfoPB>& res) {
InstanceKeyInfo key0_info {""};
InstanceKeyInfo key1_info {"\xff"}; // instance id are human readable strings
std::string key0;
std::string key1;
instance_key(key0_info, &key0);
instance_key(key1_info, &key1);
std::unique_ptr<Transaction> txn;
TxnErrorCode err = txn_kv->create_txn(&txn);
if (err != TxnErrorCode::TXN_OK) {
LOG(INFO) << "failed to init txn, err=" << err;
return -1;
}
std::unique_ptr<RangeGetIterator> it;
while (it == nullptr /* may be not init */ || it->more()) {
TxnErrorCode err = txn->get(key0, key1, &it);
if (err != TxnErrorCode::TXN_OK) {
LOG(WARNING) << "failed to get instance, err=" << err;
return -1;
}
while (it->has_next()) {
auto [k, v] = it->next();
if (!it->has_next()) key0 = k;
InstanceInfoPB instance_info;
if (!instance_info.ParseFromArray(v.data(), v.size())) {
LOG(WARNING) << "malformed instance info, key=" << hex(k);
return -1;
}
res.push_back(std::move(instance_info));
}
key0.push_back('\x00'); // Update to next smallest key for iteration
}
return 0;
}
int prepare_instance_recycle_job(TxnKv* txn_kv, std::string_view key,
const std::string& instance_id, const std::string& ip_port,
int64_t interval_ms) {
std::string val;
std::unique_ptr<Transaction> txn;
TxnErrorCode err = txn_kv->create_txn(&txn);
if (err != TxnErrorCode::TXN_OK) {
LOG(WARNING) << "failed to create txn";
return -1;
}
err = txn->get(key, &val);
if (err != TxnErrorCode::TXN_OK && err != TxnErrorCode::TXN_KEY_NOT_FOUND) {
LOG(WARNING) << "failed to get kv, err=" << err << " key=" << hex(key);
return -1;
}
using namespace std::chrono;
auto now = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
JobRecyclePB job_info;
auto is_expired = [&]() {
if (!job_info.ParseFromString(val)) {
LOG(WARNING) << "failed to parse JobRecyclePB, key=" << hex(key);
// if failed to parse, just recycle it.
return true;
}
DCHECK(job_info.instance_id() == instance_id);
if (job_info.status() == JobRecyclePB::BUSY) {
if (now < job_info.expiration_time_ms()) {
LOG(INFO) << "job is busy. host=" << job_info.ip_port()
<< " expiration=" << job_info.expiration_time_ms()
<< " instance=" << instance_id;
return false;
}
}
bool finish_expired = now - job_info.last_ctime_ms() > interval_ms;
if (!finish_expired) {
LOG(INFO) << "the time since last finished job is too short. host="
<< job_info.ip_port() << " ctime=" << job_info.last_ctime_ms()
<< " instance=" << instance_id;
}
return finish_expired;
};
if (err == TxnErrorCode::TXN_KEY_NOT_FOUND || is_expired()) {
job_info.set_status(JobRecyclePB::BUSY);
job_info.set_instance_id(instance_id);
job_info.set_ip_port(ip_port);
job_info.set_expiration_time_ms(now + config::recycle_job_lease_expired_ms);
val = job_info.SerializeAsString();
txn->put(key, val);
err = txn->commit();
if (err != TxnErrorCode::TXN_OK) {
LOG(WARNING) << "failed to commit, err=" << err << " key=" << hex(key);
return -1;
}
return 0;
}
return 1;
}
void finish_instance_recycle_job(TxnKv* txn_kv, std::string_view key,
const std::string& instance_id, const std::string& ip_port,
bool success, int64_t ctime_ms) {
std::string val;
int retry_times = 0;
do {
std::unique_ptr<Transaction> txn;
TxnErrorCode err = txn_kv->create_txn(&txn);
if (err != TxnErrorCode::TXN_OK) {
LOG(WARNING) << "failed to create txn";
return;
}
err = txn->get(key, &val);
if (err != TxnErrorCode::TXN_OK) {
LOG(WARNING) << "failed to get kv, err=" << err << " key=" << hex(key);
return;
}
using namespace std::chrono;
auto now = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
JobRecyclePB job_info;
if (!job_info.ParseFromString(val)) {
LOG(WARNING) << "failed to parse JobRecyclePB, key=" << hex(key);
return;
}
DCHECK(job_info.instance_id() == instance_id);
if (job_info.ip_port() != ip_port) {
LOG(WARNING) << "job is doing at other machine: " << job_info.ip_port()
<< " key=" << hex(key);
return;
}
if (job_info.status() != JobRecyclePB::BUSY) {
LOG(WARNING) << "job is not busy, key=" << hex(key);
return;
}
job_info.set_status(JobRecyclePB::IDLE);
job_info.set_instance_id(instance_id);
job_info.set_last_finish_time_ms(now);
job_info.set_last_ctime_ms(ctime_ms);
if (success) {
job_info.set_last_success_time_ms(now);
}
val = job_info.SerializeAsString();
txn->put(key, val);
err = txn->commit();
if (err == TxnErrorCode::TXN_OK) {
LOG(INFO) << "succ to commit to finish recycle job, key=" << hex(key);
return;
}
// maybe conflict with the commit of the leased thread
LOG(WARNING) << "failed to commit to finish recycle job, err=" << err << " key=" << hex(key)
<< " retry_times=" << retry_times;
} while (retry_times++ < 3);
LOG(WARNING) << "finally failed to commit to finish recycle job, key=" << hex(key);
}
int lease_instance_recycle_job(TxnKv* txn_kv, std::string_view key, const std::string& instance_id,
const std::string& ip_port) {
std::string val;
std::unique_ptr<Transaction> txn;
TxnErrorCode err = txn_kv->create_txn(&txn);
if (err != TxnErrorCode::TXN_OK) {
LOG(WARNING) << "failed to create txn";
return -1;
}
err = txn->get(key, &val);
if (err != TxnErrorCode::TXN_OK) {
LOG(WARNING) << "failed to get kv, err=" << err << " key=" << hex(key);
return -1;
}
using namespace std::chrono;
auto now = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
JobRecyclePB job_info;
if (!job_info.ParseFromString(val)) {
LOG(WARNING) << "failed to parse JobRecyclePB, key=" << hex(key);
return 1;
}
DCHECK(job_info.instance_id() == instance_id);
if (job_info.ip_port() != ip_port) {
LOG(WARNING) << "job is doing at other machine: " << job_info.ip_port()
<< " key=" << hex(key);
return 1;
}
if (job_info.status() != JobRecyclePB::BUSY) {
LOG(WARNING) << "job is not busy, key=" << hex(key);
return 1;
}
job_info.set_expiration_time_ms(now + config::recycle_job_lease_expired_ms);
val = job_info.SerializeAsString();
txn->put(key, val);
err = txn->commit();
if (err != TxnErrorCode::TXN_OK) {
LOG(WARNING) << "failed to commit, failed to lease recycle job, err=" << err
<< " key=" << hex(key);
return -1;
}
return 0;
}
// ret: 0: success, 1: tablet not found, -1: failed
int get_tablet_idx(TxnKv* txn_kv, const std::string& instance_id, int64_t tablet_id,
TabletIndexPB& tablet_idx) {
std::unique_ptr<Transaction> txn;
TxnErrorCode err = txn_kv->create_txn(&txn);
if (err != TxnErrorCode::TXN_OK) {
LOG(WARNING) << "failed to create txn";
return -1;
}
std::string key, val;
meta_tablet_idx_key({instance_id, tablet_id}, &key);
err = txn->get(key, &val);
if (err != TxnErrorCode::TXN_OK) {
if (err == TxnErrorCode::TXN_KEY_NOT_FOUND) {
LOG(INFO) << "tablet not found, tablet_id=" << tablet_id << " key=" << hex(key);
return 1;
}
LOG(WARNING) << fmt::format("failed to get tablet_idx, err={} tablet_id={} key={}", err,
tablet_id, hex(key));
return -1;
}
if (!tablet_idx.ParseFromString(val)) [[unlikely]] {
LOG(WARNING) << fmt::format("malformed tablet index value, tablet_id={} key={}", tablet_id,
hex(key));
return -1;
}
if (tablet_id != tablet_idx.tablet_id()) [[unlikely]] {
LOG(WARNING) << "unexpected error given_tablet_id=" << tablet_id
<< " idx_pb_tablet_id=" << tablet_idx.tablet_id() << " key=" << hex(key);
return -1;
}
return 0;
}
int get_tablet_meta(TxnKv* txn_kv, const std::string& instance_id, int64_t tablet_id,
TabletMetaCloudPB& tablet_meta) {
TabletIndexPB tablet_idx;
int ret = get_tablet_idx(txn_kv, instance_id, tablet_id, tablet_idx);
if (ret < 0) {
return ret;
}
std::unique_ptr<Transaction> txn;
TxnErrorCode err = txn_kv->create_txn(&txn);
if (err != TxnErrorCode::TXN_OK) {
LOG(WARNING) << "failed to create txn";
return -1;
}
std::string key, val;
meta_tablet_key({instance_id, tablet_idx.table_id(), tablet_idx.index_id(),
tablet_idx.partition_id(), tablet_id},
&key);
err = txn->get(key, &val);
if (err != TxnErrorCode::TXN_OK) {
LOG(WARNING) << fmt::format(
"failed to get tablet, err={}, table_id={}, index_id={}, partition_id={}, "
"tablet_id={} key={}",
err, tablet_idx.table_id(), tablet_idx.index_id(), tablet_idx.partition_id(),
tablet_id, hex(key));
return -1;
}
if (!tablet_meta.ParseFromString(val)) [[unlikely]] {
LOG(WARNING) << fmt::format("malformed tablet meta, tablet_id={} key={}", tablet_id,
hex(key));
return -1;
}
return 0;
}
} // namespace doris::cloud