-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdata_store_service_scanner.cpp
More file actions
528 lines (457 loc) · 14.9 KB
/
data_store_service_scanner.cpp
File metadata and controls
528 lines (457 loc) · 14.9 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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/**
* Copyright (C) 2025 EloqData Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under either of the following two licenses:
* 1. GNU Affero General Public License, version 3, as published by the Free
* Software Foundation.
* 2. GNU General Public License as published by the Free Software
* Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License or GNU General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License
* and GNU General Public License V2 along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#include "data_store_service_scanner.h"
#include <glog/logging.h>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "data_store_service_client_closure.h"
#include "eloq_data_store_service/object_pool.h"
#include "tx_service/include/tx_key.h"
namespace EloqDS
{
thread_local ObjectPool<SinglePartitionScanner> single_partition_scanner_pool_;
bool SinglePartitionScanner::FetchNextBatch()
{
if (IsRunOutOfData())
{
return false;
}
if (!scanner_)
{
LOG(ERROR) << "Invalid scanner parameters: scanner is null";
return false;
}
DataStoreServiceClient *client = scanner_->GetClient();
if (!client)
{
LOG(ERROR) << "Invalid scanner parameters: client is null";
return false;
}
ResetCache();
scanner_->IncrementInFlightFetchCount();
client->ScanNext(
scanner_->GetTableName(),
partition_id_,
data_shard_id_,
last_key_,
scanner_->GetEndKey(),
session_id_,
true,
// only set inclusive_start for the first batch
first_batch_fetched_ ? false : scanner_->IsInclusiveStart(),
scanner_->IsInclusiveEnd(),
scanner_->IsScanForward(),
scanner_->GetBatchSize(),
// scanner_->SearchConditions(),
nullptr,
this,
ProcessScanNextResult);
first_batch_fetched_ = true;
return true;
}
bool SinglePartitionScanner::ScanClose()
{
if (!scanner_)
{
LOG(ERROR) << "Invalid scanner parameters: scanner is null";
return false;
}
DataStoreServiceClient *client = scanner_->GetClient();
if (!client)
{
LOG(ERROR) << "Invalid scanner parameters: client is null";
return false;
}
scanner_->IncrementInFlightFetchCount();
assert(last_batch_size_ <= scanner_->GetBatchSize());
// If parition scanner is not run out of data,
// we need to close the scanner
if (last_batch_size_ == scanner_->GetBatchSize())
{
client->ScanClose(scanner_->GetTableName(),
partition_id_,
data_shard_id_,
session_id_,
this,
ProcessScanCloseResult);
}
return true;
}
void SinglePartitionScanner::ProcessScanNextResult(
void *data,
::google::protobuf::Closure *closure,
DataStoreServiceClient &client,
const remote::CommonResult &result)
{
SinglePartitionScanner *sp_scanner =
static_cast<SinglePartitionScanner *>(data);
ScanNextClosure *scan_next_closure =
static_cast<ScanNextClosure *>(closure);
if (result.error_code() != remote::DataStoreError::NO_ERROR)
{
LOG(ERROR) << "Failed to fetch next batch: " << result.error_msg();
sp_scanner->scanner_->SetError(
static_cast<remote::DataStoreError>(result.error_code()),
result.error_msg());
sp_scanner->scanner_->DecrementInFlightFetchCount();
return;
}
uint32_t items_size = scan_next_closure->ItemsSize();
sp_scanner->last_batch_size_ = items_size;
sp_scanner->session_id_ = scan_next_closure->GetSessionId();
uint64_t now = txservice::LocalCcShards::ClockTsInMillseconds();
for (uint32_t i = 0; i < items_size; i++)
{
std::string key, value;
uint64_t ts, ttl;
scan_next_closure->GetItem(i, key, value, ts, ttl);
if (i == sp_scanner->last_batch_size_ - 1)
{
sp_scanner->last_key_ = key;
}
if (ttl > 0 && ttl < now)
{
// TTL expired record
DLOG(INFO) << "TTL expired record, key: " << key << ", ttl: " << ttl
<< ", now: " << now;
continue;
}
sp_scanner->scan_cache_.emplace_back(
std::move(key), std::move(value), ts, ttl);
}
sp_scanner->scanner_->DecrementInFlightFetchCount();
}
void SinglePartitionScanner::ProcessScanCloseResult(
void *data,
::google::protobuf::Closure *closure,
DataStoreServiceClient &client,
const remote::CommonResult &result)
{
SinglePartitionScanner *sp_scanner =
static_cast<SinglePartitionScanner *>(data);
if (result.error_code() != remote::DataStoreError::NO_ERROR)
{
LOG(ERROR) << "Failed to fetch next batch: " << result.error_msg();
sp_scanner->scanner_->SetError(
static_cast<remote::DataStoreError>(result.error_code()),
result.error_msg());
}
sp_scanner->scanner_->DecrementInFlightFetchCount();
}
bool SinglePartitionScanner::IsRunOutOfData()
{
return last_batch_size_ < scanner_->GetBatchSize();
}
ScanTuple &SinglePartitionScanner::NextScanTuple()
{
assert(head_ < scan_cache_.size());
return scan_cache_[head_++];
}
void SinglePartitionScanner::ResetCache()
{
head_ = 0;
scan_cache_.clear();
}
template <bool ScanForward>
DataStoreServiceHashPartitionScanner<ScanForward>::
DataStoreServiceHashPartitionScanner(
DataStoreServiceClient *client,
const txservice::CatalogFactory *catalog_factory,
const txservice::KeySchema *key_sch,
const txservice::RecordSchema *rec_sch,
const txservice::TableName &table_name,
const txservice::KVCatalogInfo *kv_info,
const txservice::TxKey &start_key,
bool inclusive,
const std::vector<txservice::DataStoreSearchCond> &pushdown_cond,
size_t batch_size)
: DataStoreServiceScanner(client,
kv_info->GetKvTableName(table_name),
inclusive, // inclusive_start
false, // inclusive_end
"", // end_key
ScanForward,
batch_size),
catalog_factory_(catalog_factory),
key_sch_(key_sch),
rec_sch_(rec_sch),
kv_info_(kv_info),
pushdown_condition_(pushdown_cond),
is_object_key_schema_(table_name.Engine() ==
txservice::TableEngine::EloqKv),
initialized_(false)
{
assert(client_ != nullptr);
if (start_key.Type() == txservice::KeyType::NegativeInf ||
start_key.Type() == txservice::KeyType::PositiveInf)
{
start_key_ = "";
}
else
{
start_key_ = start_key.ToString();
}
end_key_ = "";
// convert pushdown conditions to search conditions
for (const auto &cond : pushdown_cond)
{
remote::SearchCondition search_cond;
search_cond.set_field_name(cond.field_name_);
search_cond.set_op(cond.op_);
search_cond.set_value(cond.val_str_);
search_conditions_.emplace_back(std::move(search_cond));
}
}
template <bool ScanForward>
DataStoreServiceHashPartitionScanner<
ScanForward>::~DataStoreServiceHashPartitionScanner()
{
// wait for all in-flight fetches to complete
WaitUntilInFlightFetchesComplete();
CloseScan();
}
template <bool ScanForward>
void DataStoreServiceHashPartitionScanner<ScanForward>::Current(
txservice::TxKey &key,
const txservice::TxRecord *&rec,
uint64_t &version_ts,
bool &deleted)
{
if (!initialized_)
{
LOG(ERROR) << "Scanner is not initialized";
return;
}
if (error_code_ != remote::DataStoreError::NO_ERROR)
{
LOG(ERROR) << "Scanner error, error code: " << error_code_
<< ", error msg: " << error_msg_;
key = txservice::TxKey();
rec = nullptr;
version_ts = 0;
deleted = false;
return;
}
if (IsRunOutOfData())
{
key = txservice::TxKey();
rec = nullptr;
version_ts = 0;
deleted = false;
return;
}
const ScanHeapTuple<txservice::TxKey, txservice::TxRecord> &top =
result_cache_.top();
key = top.key_->GetShallowCopy();
rec = top.rec_.get();
version_ts = top.version_ts_;
deleted = top.deleted_;
}
template <bool ScanForward>
bool DataStoreServiceHashPartitionScanner<ScanForward>::IsRunOutOfData()
{
if (!initialized_)
{
return true;
}
// when the scanner is initialized, the result cache is empty and
// there is no in-flight fetch
return result_cache_.empty() &&
in_flying_fetch_cnt_.load(std::memory_order_acquire) == 0;
}
template <bool ScanForward>
bool DataStoreServiceHashPartitionScanner<ScanForward>::MoveNext()
{
// Initialize if not already done
if (!initialized_)
{
return Init();
}
// Quick check if scanner is completely empty
if (IsRunOutOfData())
{
return false;
}
if (error_code_ != remote::DataStoreError::NO_ERROR)
{
LOG(ERROR) << "Scanner error, error code: " << error_code_
<< ", error msg: " << error_msg_;
return false;
}
// Extract the next item from the result cache
const ScanHeapTuple<txservice::TxKey, txservice::TxRecord> &top =
result_cache_.top();
uint32_t part_id = top.sid_;
result_cache_.pop();
// Get the corresponding partition scanner
SinglePartitionScanner *part_scanner = partition_scanners_[part_id];
// Handle scanner lifecycle
assert(part_scanner != nullptr);
if (!part_scanner->IsCacheEmpty())
{
AddScanTuple(part_scanner, part_id);
}
else
{
if (part_scanner->IsRunOutOfData())
{
// Free the partition scanner if it's run out of data
PoolableGuard guard(part_scanner);
partition_scanners_[part_id] = nullptr;
}
else
{
// Fetch next batch asynchronously
DLOG(INFO) << "Fetching next batch for partition scanner "
<< part_id;
part_scanner->FetchNextBatch();
// Wait the result of this partition.
WaitUntilInFlightFetchesComplete();
if (part_scanner->IsCacheEmpty())
{
// No more data in this partition.
assert(part_scanner->IsRunOutOfData());
// Free the partition scanner if it's run out of data
PoolableGuard guard(part_scanner);
partition_scanners_[part_id] = nullptr;
return false;
}
// Get the key
AddScanTuple(part_scanner, part_id);
}
}
return true;
}
template <bool ScanForward>
bool DataStoreServiceHashPartitionScanner<ScanForward>::Init()
{
if (initialized_)
{
return true;
}
initialized_ = true;
if (!client_)
{
LOG(ERROR) << "Invalid scanner parameters: client is null";
return false;
}
partition_scanners_.reserve(HASH_PARTITION_COUNT);
for (uint32_t part_cnt = 0; part_cnt < HASH_PARTITION_COUNT; part_cnt++)
{
auto *part_scanner = single_partition_scanner_pool_.NextObject();
uint32_t data_shard_id = client_->GetShardIdByPartitionId(
static_cast<int32_t>(part_cnt), false);
part_scanner->Reset(this, part_cnt, data_shard_id, start_key_);
partition_scanners_.push_back(part_scanner);
// ignore the return value, since the scanner is not used
part_scanner->FetchNextBatch();
}
// Wait the initial HASH_PARTITION_COUNT fetches to finish,
// otherwise the result_cache_ is not overall ordered
WaitUntilInFlightFetchesComplete();
// Get the keys
for (uint32_t part_id = 0; part_id < HASH_PARTITION_COUNT; ++part_id)
{
auto &part_scanner = partition_scanners_[part_id];
if (!part_scanner->IsCacheEmpty())
{
AddScanTuple(part_scanner, part_id);
}
else
{
// There must be no data in this partition. Recycly this scanner.
PoolableGuard guard(part_scanner);
part_scanner = nullptr;
}
}
return true;
}
template <bool ScanForward>
void DataStoreServiceHashPartitionScanner<ScanForward>::AddScanTuple(
SinglePartitionScanner *part_scanner, uint32_t part_id)
{
assert(part_scanner);
auto &part_scan_tuple = part_scanner->NextScanTuple();
ScanHeapTuple<txservice::TxKey, txservice::TxRecord> scan_tuple(
part_id,
std::make_unique<txservice::TxKey>(catalog_factory_->CreateTxKey()),
catalog_factory_->CreateTxRecord());
scan_tuple.key_->SetPackedKey(part_scan_tuple.key_.data(),
part_scan_tuple.key_.size());
size_t offset = 0;
if (is_object_key_schema_)
{
txservice::TxObject *tx_obj =
static_cast<txservice::TxObject *>(scan_tuple.rec_.get());
txservice::TxRecord::Uptr obj_uptr =
tx_obj->DeserializeObject(part_scan_tuple.value_.data(), offset);
scan_tuple.rec_.reset(obj_uptr.release());
}
else
{
scan_tuple.rec_->Deserialize(part_scan_tuple.value_.data(), offset);
}
scan_tuple.version_ts_ = part_scan_tuple.ts_;
scan_tuple.deleted_ = false;
result_cache_.push(std::move(scan_tuple));
}
template <bool ScanForward>
void DataStoreServiceHashPartitionScanner<ScanForward>::End()
{
// do nothing
}
template <bool ScanForward>
bool DataStoreServiceHashPartitionScanner<ScanForward>::CloseScan()
{
DLOG(INFO) << "Closing scanner";
if (!initialized_)
{
return true;
}
// close all partition scanners if they are not run out of data
for (auto *part_scanner : partition_scanners_)
{
if (part_scanner != nullptr && !part_scanner->IsRunOutOfData())
{
part_scanner->ScanClose();
}
}
// wait until all in-flight scan close finish
WaitUntilInFlightFetchesComplete();
// free all partition scanners
for (auto *part_scanner : partition_scanners_)
{
if (part_scanner)
{
DLOG(INFO) << "Free partition scanner "
<< part_scanner->GetPartitionId();
PoolableGuard guard(part_scanner);
}
}
DLOG(INFO) << "Freeing partition scanners";
return true;
}
template class DataStoreServiceHashPartitionScanner<true>;
template class DataStoreServiceHashPartitionScanner<false>;
} // namespace EloqDS