-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathschedule_plan_executor.cc
More file actions
473 lines (414 loc) · 20.4 KB
/
schedule_plan_executor.cc
File metadata and controls
473 lines (414 loc) · 20.4 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#include "kv_cache_manager/manager/schedule_plan_executor.h"
#include <memory>
#include <unordered_set>
#include "kv_cache_manager/common/logger.h"
#include "kv_cache_manager/common/request_context.h"
#include "kv_cache_manager/common/string_util.h"
#include "kv_cache_manager/data_storage/data_storage_manager.h"
#include "kv_cache_manager/data_storage/data_storage_uri.h"
#include "kv_cache_manager/manager/cache_location.h"
#include "kv_cache_manager/manager/meta_searcher.h"
#include "kv_cache_manager/meta/meta_indexer.h"
#include "kv_cache_manager/meta/meta_indexer_manager.h"
#include "kv_cache_manager/metrics/metrics_registry.h"
namespace kv_cache_manager {
#define DEFINE_METRICS_NAME_FOR_SCHEDULE_PLAN_EXECUTOR(name) \
DEFINE_METRICS_NAME_(SchedulePlanExecutor, schedule_plan_executor, name)
#define REGISTER_METRICS_FOR_SCHEDULE_PLAN_EXECUTOR(name) \
REGISTER_METRICS_COUNTER_(metrics_registry_, schedule_plan_executor, name)
namespace {
template <typename ResultType, typename... Args>
void HandleErrorPromise(const std::shared_ptr<std::promise<ResultType>> &promise,
ErrorCode error_code,
const std::string &err_msg_format,
Args &&...args) {
std::string error_message = StringUtil::FormatString(err_msg_format, std::forward<Args>(args)...);
promise->set_value(ResultType{
.status = error_code,
.error_message = std::move(error_message),
.fail_metas = {},
});
}
} // namespace
DEFINE_METRICS_NAME_FOR_SCHEDULE_PLAN_EXECUTOR(waiting_task_count);
DEFINE_METRICS_NAME_FOR_SCHEDULE_PLAN_EXECUTOR(executing_task_count);
SchedulePlanExecutor::SchedulePlanExecutor(unsigned int thread_count,
const std::shared_ptr<MetaIndexerManager> &meta_manager,
const std::shared_ptr<DataStorageManager> &storage_manager,
const std::shared_ptr<MetricsRegistry> &metrics_registry)
: meta_manager_(meta_manager)
, data_storage_manager_(storage_manager)
, metrics_registry_(metrics_registry)
, stop_(false) {
REGISTER_METRICS_FOR_SCHEDULE_PLAN_EXECUTOR(waiting_task_count);
REGISTER_METRICS_FOR_SCHEDULE_PLAN_EXECUTOR(executing_task_count);
if (thread_count == 0)
thread_count = 1;
for (unsigned int i = 0; i < thread_count; ++i) {
workers_.emplace_back([this]() { WorkerRoutine(); });
}
KVCM_LOG_INFO("SchedulePlanExecutor initialized with %u worker threads", thread_count);
}
void SchedulePlanExecutor::Stop() {
KVCM_LOG_DEBUG("Stopping SchedulePlanExecutor...");
stop_ = true;
condition_.notify_all();
for (auto &worker : workers_) {
if (worker.joinable()) {
worker.join();
}
}
KVCM_LOG_DEBUG("SchedulePlanExecutor stopped");
}
SchedulePlanExecutor::~SchedulePlanExecutor() { Stop(); }
void SchedulePlanExecutor::WorkerRoutine() {
while (!stop_) {
std::function<void()> task;
{
auto wait_until_time = std::chrono::steady_clock::time_point::max();
std::unique_lock<std::mutex> lock(queue_mutex_);
// 检查是否有可执行的任务
if (!tasks_.empty()) {
auto earliest_task = tasks_.begin();
auto now = std::chrono::steady_clock::now();
if (earliest_task->execute_time <= now) {
// 租约时间已到,取出任务执行
task = earliest_task->task;
tasks_.erase(earliest_task);
} else {
// 租约时间未到,记录等待时间点
wait_until_time = earliest_task->execute_time;
}
}
if (!task) {
if (wait_until_time != std::chrono::steady_clock::time_point::max()) {
// 等待到最早任务的时间或新任务到达
condition_.wait_until(lock, wait_until_time);
} else if (tasks_.empty()) {
// 队列为空,等待新任务
condition_.wait(lock, [this] { return stop_ || (!tasks_.empty()); });
}
continue;
}
}
if (task) {
--METRICS_(schedule_plan_executor, waiting_task_count);
++METRICS_(schedule_plan_executor, executing_task_count);
task();
--METRICS_(schedule_plan_executor, executing_task_count);
}
}
}
void SchedulePlanExecutor::DoLocationDelTask(const std::shared_ptr<std::promise<PlanExecuteResult>> &promise,
const CacheLocationDelRequest &task) {
PlanExecuteResult result;
result.status = ErrorCode::EC_OK;
if (task.block_keys.size() != task.location_ids.size()) {
HandleErrorPromise(promise,
ErrorCode::EC_BADARGS,
"block_keys size %d != location_ids size %d",
task.block_keys.size(),
task.location_ids.size());
return;
}
std::shared_ptr<MetaIndexer> indexer = meta_manager_->GetMetaIndexer(task.instance_id);
if (!indexer) {
HandleErrorPromise(promise, ErrorCode::EC_NOENT, "MetaIndexer %s not found", task.instance_id.c_str());
return;
}
MetaSearcher meta_searcher(indexer);
std::vector<CacheLocationMap> location_maps;
BlockMask empty_mask;
auto ctx = std::make_shared<RequestContext>("schedule_plan_executor_call");
ErrorCode get_locations_ec = meta_searcher.BatchGetLocation(ctx.get(), task.block_keys, empty_mask, location_maps);
if (get_locations_ec != ErrorCode::EC_OK) {
HandleErrorPromise(promise, ErrorCode::EC_ERROR, "Failed to get block locations, ec: %d", get_locations_ec);
return;
}
std::vector<int64_t> block_keys_to_delete;
std::vector<std::vector<MetaSearcher::LocationCADTask>> batch_cad_tasks;
size_t total_locations_to_delete = 0;
std::map<std::string, std::vector<DataStorageUri>> delete_uris_by_unique_name;
for (size_t i = 0; i < task.block_keys.size(); i++) {
auto &block_key = task.block_keys[i];
auto &location_map = location_maps[i];
auto &need_delete_location_ids = task.location_ids[i];
std::vector<MetaSearcher::LocationCADTask> cad_tasks;
for (auto &location_id : need_delete_location_ids) {
auto iter = location_map.find(location_id);
if (iter == location_map.end()) {
continue;
}
if (iter->second.status() != CacheLocationStatus::CLS_DELETING) {
continue;
}
for (const auto &loc_spec : iter->second.location_specs()) {
DataStorageUri uri(loc_spec.uri());
if (uri.Valid()) {
std::string storage_unique_name = uri.GetHostName();
delete_uris_by_unique_name[storage_unique_name].emplace_back(uri);
}
}
cad_tasks.push_back({iter->first, CacheLocationStatus::CLS_DELETING});
}
if (!cad_tasks.empty()) {
block_keys_to_delete.push_back(block_key);
total_locations_to_delete += cad_tasks.size();
batch_cad_tasks.emplace_back(std::move(cad_tasks));
}
}
KVCM_LOG_DEBUG("Found %zu meta location(s) to delete", total_locations_to_delete);
// delete storage uris
auto request_context = std::make_shared<RequestContext>("location_del_task_trace");
for (const auto &storage_uris_pair : delete_uris_by_unique_name) {
const std::string &storage_unique_name = storage_uris_pair.first;
const std::vector<DataStorageUri> &storage_uris = storage_uris_pair.second;
KVCM_LOG_DEBUG("Deleting %zu entries from storage: %s", storage_uris.size(), storage_unique_name.c_str());
std::vector<ErrorCode> delete_results =
data_storage_manager_->Delete(request_context.get(), storage_unique_name, storage_uris, nullptr);
for (size_t i = 0; i < delete_results.size(); ++i) {
if (delete_results[i] != ErrorCode::EC_OK) {
// 这里存储删除报错暂且不管,报个warn表示哪个storageUri删失败了
result.status = ErrorCode::EC_PARTIAL_OK;
KVCM_LOG_WARN("Failed to delete kvcache from storage %s, uri: %s",
storage_unique_name.c_str(),
storage_uris[i].ToUriString().c_str());
}
}
}
// delete locations
if (!batch_cad_tasks.empty()) {
std::vector<std::vector<ErrorCode>> delete_meta_results;
ErrorCode delete_meta_ec =
meta_searcher.BatchCADLocationStatus(ctx.get(), block_keys_to_delete, batch_cad_tasks, delete_meta_results);
(void)delete_meta_ec; // 忽略返回值
std::vector<ErrorCode> status_vec;
std::vector<std::string> location_ids;
for (size_t block_key_idx = 0; block_key_idx < delete_meta_results.size(); ++block_key_idx) {
auto &results = delete_meta_results[block_key_idx];
for (size_t location_idx = 0; location_idx < results.size(); location_idx++) {
if (results[location_idx] != ErrorCode::EC_OK) {
status_vec.push_back(results[location_idx]);
location_ids.push_back(batch_cad_tasks[block_key_idx][location_idx].location_id);
result.status = ErrorCode::EC_PARTIAL_OK;
KVCM_LOG_WARN("Failed to CAD meta key %ld, location: %s, error_code: %d",
block_keys_to_delete[block_key_idx],
batch_cad_tasks[block_key_idx][location_idx].location_id.c_str(),
results[location_idx]);
}
}
if (!status_vec.empty()) {
result.fail_metas.push_back(PlanExecuteResultFailMeta{
block_keys_to_delete[block_key_idx], std::move(status_vec), std::move(location_ids)});
status_vec.clear();
location_ids.clear();
}
}
}
KVCM_LOG_DEBUG("DoDelLocationTask completed successfully for instance_id: %s", task.instance_id.c_str());
promise->set_value(result);
}
bool SchedulePlanExecutor::SubmitRaw(const std::function<void()> &task, std::chrono::microseconds delay) {
{
std::lock_guard<std::mutex> lock(queue_mutex_);
if (stop_) {
return false;
}
auto execute_time = std::chrono::steady_clock::now() + delay;
uint64_t sequence_id = sequence_counter_.fetch_add(1, std::memory_order_relaxed);
tasks_.emplace(ScheduledTask{task, execute_time, sequence_id});
}
++METRICS_(schedule_plan_executor, waiting_task_count);
condition_.notify_one();
return true;
}
std::future<PlanExecuteResult> SchedulePlanExecutor::Submit(const CacheMetaDelRequest &task) {
KVCM_LOG_DEBUG("Submitting meta delete task for instance_id: %s, block_keys count: %zu",
task.instance_id.c_str(),
task.block_keys.size());
auto promise = std::make_shared<std::promise<PlanExecuteResult>>();
std::future<PlanExecuteResult> future = promise->get_future();
if (stop_) {
HandleErrorPromise(promise, ErrorCode::EC_ERROR, "SchedulePlanExecutor stopped.");
return future;
}
// 1. sync set block status to deleting
const std::shared_ptr<MetaIndexer> indexer = meta_manager_->GetMetaIndexer(task.instance_id);
if (indexer == nullptr) {
HandleErrorPromise(promise, ErrorCode::EC_NOENT, "MetaIndexer %s not found", task.instance_id.c_str());
return future;
}
MetaSearcher meta_searcher(indexer);
std::vector<CacheLocationMap> location_maps;
BlockMask empty_mask;
auto request_context = std::make_shared<RequestContext>("schedule_plan_executor_call");
ErrorCode get_locations_ec =
meta_searcher.BatchGetLocation(request_context.get(), task.block_keys, empty_mask, location_maps);
if (get_locations_ec != ErrorCode::EC_OK) {
HandleErrorPromise(promise, ErrorCode::EC_ERROR, "Failed to get block locations");
return future;
}
std::vector<int64_t> batch_cas_block_keys;
std::vector<std::vector<MetaSearcher::LocationCASTask>> batch_cas_tasks;
for (size_t block_key_idx = 0; block_key_idx < task.block_keys.size(); ++block_key_idx) {
std::vector<MetaSearcher::LocationCASTask> cas_tasks;
for (const auto &loc_kv : location_maps[block_key_idx]) {
cas_tasks.push_back({loc_kv.first, loc_kv.second.status(), CLS_DELETING});
}
if (cas_tasks.empty()) {
continue;
}
batch_cas_block_keys.emplace_back(task.block_keys[block_key_idx]);
batch_cas_tasks.emplace_back(std::move(cas_tasks));
}
if (batch_cas_block_keys.empty()) {
promise->set_value(PlanExecuteResult{ErrorCode::EC_OK, "", {}});
return future;
}
std::vector<std::vector<ErrorCode>> cas_results;
ErrorCode update_ec =
meta_searcher.BatchCASLocationStatus(request_context.get(), batch_cas_block_keys, batch_cas_tasks, cas_results);
if (update_ec != ErrorCode::EC_OK) {
KVCM_LOG_DEBUG("Location status BatchCASLocationStatus not ok, ec: %d", update_ec);
}
std::string error_message;
CacheLocationDelRequest actual_task{task.instance_id, {}, {}, task.delay};
if (!FillActualTask(batch_cas_block_keys, batch_cas_tasks, cas_results, actual_task, error_message)) {
HandleErrorPromise(promise, ErrorCode::EC_ERROR, "FillActualTask error: %s", error_message.c_str());
return future;
}
if (actual_task.block_keys.empty()) {
promise->set_value(PlanExecuteResult{ErrorCode::EC_OK, "", {}});
return future;
}
KVCM_LOG_DEBUG("Location statuses updated, submitting task to worker pool with delay: %lld microseconds",
static_cast<long long>(task.delay.count()));
bool submit_result =
this->SubmitRaw([this, promise, actual_task]() { DoLocationDelTask(promise, actual_task); }, task.delay);
if (!submit_result) {
HandleErrorPromise(promise, ErrorCode::EC_ERROR, "submit task failed");
return future;
}
return future;
}
bool SchedulePlanExecutor::FillActualTask(
const std::vector<int64_t> &batch_cas_block_keys,
const std::vector<std::vector<MetaSearcher::LocationCASTask>> &batch_cas_tasks,
const std::vector<std::vector<ErrorCode>> &batch_results,
CacheLocationDelRequest &actual_task,
std::string &error_message) {
if (batch_results.size() != batch_cas_block_keys.size() || batch_results.size() != batch_cas_tasks.size()) {
error_message = StringUtil::FormatString(
"Size mismatch between batch_results(%d), batch_cas_block_keys(%d) and batch_cas_tasks(%d).",
batch_results.size(),
batch_cas_block_keys.size(),
batch_cas_tasks.size());
KVCM_LOG_ERROR("%s", error_message.c_str());
return false;
}
for (size_t key_idx = 0; key_idx < batch_results.size(); key_idx++) {
auto &results = batch_results[key_idx];
std::vector<std::string> location_ids;
for (size_t location_idx = 0; location_idx < results.size(); location_idx++) {
if (results[location_idx] != EC_OK) {
KVCM_LOG_INFO("Location status CAS failed, block key: %ld, location_id: %s",
batch_cas_block_keys[key_idx],
batch_cas_tasks[key_idx][location_idx].location_id.c_str());
continue;
}
location_ids.push_back(batch_cas_tasks[key_idx][location_idx].location_id);
}
if (location_ids.empty()) {
continue;
}
actual_task.block_keys.push_back(batch_cas_block_keys[key_idx]);
actual_task.location_ids.emplace_back(std::move(location_ids));
}
return true;
}
std::future<PlanExecuteResult> SchedulePlanExecutor::Submit(const CacheLocationDelRequest &task) {
KVCM_LOG_DEBUG("Submitting location delete task for instance_id: %s, block_keys count: %zu",
task.instance_id.c_str(),
task.block_keys.size());
auto promise = std::make_shared<std::promise<PlanExecuteResult>>();
std::future<PlanExecuteResult> future = promise->get_future();
if (stop_) {
HandleErrorPromise(promise, ErrorCode::EC_ERROR, "SchedulePlanExecutor stopped.");
return future;
}
std::shared_ptr<MetaIndexer> indexer = meta_manager_->GetMetaIndexer(task.instance_id);
if (!indexer) {
HandleErrorPromise(promise, ErrorCode::EC_NOENT, "MetaIndexer %s not found", task.instance_id.c_str());
return future;
}
MetaSearcher meta_searcher(indexer);
std::vector<CacheLocationMap> location_maps;
BlockMask empty_mask;
auto request_context = std::make_shared<RequestContext>("schedule_plan_executor_call");
ErrorCode get_locations_ec =
meta_searcher.BatchGetLocation(request_context.get(), task.block_keys, empty_mask, location_maps);
if (get_locations_ec != ErrorCode::EC_OK) {
HandleErrorPromise(promise, ErrorCode::EC_ERROR, "Failed to get block locations, ec: %d", get_locations_ec);
return future;
}
std::vector<int64_t> batch_cas_block_keys;
std::vector<std::vector<MetaSearcher::LocationCASTask>> batch_cas_tasks;
for (size_t block_key_idx = 0; block_key_idx < task.block_keys.size(); ++block_key_idx) {
std::unordered_set<std::string> target_location_ids(task.location_ids[block_key_idx].begin(),
task.location_ids[block_key_idx].end());
auto block_key = task.block_keys[block_key_idx];
auto &location_map = location_maps[block_key_idx];
std::vector<MetaSearcher::LocationCASTask> location_cas_tasks;
for (const auto &loc_kv : location_map) {
const auto &location = loc_kv.second;
if (location.status() == CacheLocationStatus::CLS_DELETING) {
continue; // ignore already deleting
}
if (target_location_ids.find(location.id()) == target_location_ids.end()) {
continue; // ignore not target location
}
location_cas_tasks.push_back({location.id(), location.status(), CacheLocationStatus::CLS_DELETING});
}
if (location_cas_tasks.empty()) {
continue;
}
batch_cas_block_keys.push_back(block_key);
batch_cas_tasks.emplace_back(std::move(location_cas_tasks));
}
if (batch_cas_block_keys.empty()) {
promise->set_value(PlanExecuteResult{ErrorCode::EC_OK, "", {}});
return future;
}
std::vector<std::vector<ErrorCode>> batch_results;
ErrorCode update_ec = meta_searcher.BatchCASLocationStatus(
request_context.get(), batch_cas_block_keys, batch_cas_tasks, batch_results);
if (update_ec != ErrorCode::EC_OK) {
KVCM_LOG_DEBUG("Location status BatchCASLocationStatus not ok, ec: %d", update_ec);
}
std::string error_message;
CacheLocationDelRequest actual_task{task.instance_id, {}, {}, task.delay};
if (!FillActualTask(batch_cas_block_keys, batch_cas_tasks, batch_results, actual_task, error_message)) {
HandleErrorPromise(promise, ErrorCode::EC_ERROR, "FillActualTask error: %s", error_message.c_str());
return future;
}
if (actual_task.block_keys.empty()) {
promise->set_value(PlanExecuteResult{ErrorCode::EC_OK, "", {}});
return future;
}
KVCM_LOG_DEBUG("Location statuses updated, submitting task to worker pool with delay: %lld microseconds",
static_cast<long long>(task.delay.count()));
bool submit_result =
this->SubmitRaw([this, promise, actual_task]() { DoLocationDelTask(promise, actual_task); }, task.delay);
if (!submit_result) {
HandleErrorPromise(promise, ErrorCode::EC_ERROR, "submit task failed");
return future;
}
return future;
}
bool SchedulePlanExecutor::SubmitNonBlocking(const CacheMetaDelRequest &req) {
return SubmitRaw([this, req]() { Submit(req); }, std::chrono::microseconds{0});
}
bool SchedulePlanExecutor::SubmitNonBlocking(const CacheLocationDelRequest &req) {
return SubmitRaw([this, req]() { Submit(req); }, std::chrono::microseconds{0});
}
} // namespace kv_cache_manager