-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdynamo_scanner.cpp
More file actions
427 lines (384 loc) · 12.5 KB
/
dynamo_scanner.cpp
File metadata and controls
427 lines (384 loc) · 12.5 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
/**
* 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 "dynamo_scanner.h"
#include <utility>
#include "constants.h"
#include "dynamo_handler.h"
#include "dynamo_handler_typed.h"
#include "partition.h"
namespace EloqDS
{
using Aws::DynamoDB::Model::AttributeValue;
using Aws::DynamoDB::Model::QueryOutcome;
using Aws::DynamoDB::Model::QueryOutcomeCallable;
using Aws::DynamoDB::Model::QueryRequest;
const std::string dynamo_partition_key_attribute_name = "pk1_";
const std::string dynamo_sort_key_attribute_name = "___mono_key___";
// Max number of items returned on a dynamo query request.
const int dynamo_scan_page_limit = 1000;
bool DynamoScanner::IsScanWithPushdownCondition()
{
return !pushdown_condition_.empty();
}
bool DynamoScanner::IsScanWithStartKey()
{
return start_key_ != nullptr &&
start_key_->Type() != txservice::KeyType::PositiveInf &&
start_key_->Type() != txservice::KeyType::NegativeInf;
}
#ifdef ON_KEY_OBJECT
void DynamoScanner::EncodeDynamoRowObj(
const Aws::Map<Aws::String, AttributeValue> &row,
ScanHeapTuple<EloqKV::EloqKey, EloqKV::RedisEloqObject> &heap_tuple,
const txservice::RecordSchema *rec_sch)
{
heap_tuple.version_ts_ = std::stoull(row.at("___version___").GetN());
heap_tuple.deleted_ = row.at("___deleted___").GetBool();
Aws::Utils::ByteBuffer packed_key =
row.at(dynamo_sort_key_attribute_name).GetB();
heap_tuple.key_ = std::make_unique<EloqKV::EloqKey>(
(const char *) packed_key.GetUnderlyingData(), packed_key.GetLength());
}
#endif
/**
* @brief Build up scan_req_
*
*/
void DynamoScanner::BuildScanPartitionRequest()
{
initialized_ = true;
// Setup the scan req;
scan_req_.SetConsistentRead(true);
const DynamoCatalogInfo *dynamo_info =
static_cast<const DynamoCatalogInfo *>(kv_info_);
if (table_name_.Type() == txservice::TableType::Secondary)
{
scan_req_.SetTableName(keyspace_ + '.' +
dynamo_info->kv_index_names_.at(table_name_));
}
else
{
scan_req_.SetTableName(keyspace_ + '.' + dynamo_info->kv_table_name_);
}
scan_req_.AddExpressionAttributeNames("#pk",
dynamo_partition_key_attribute_name);
scan_req_.AddExpressionAttributeNames("#mk",
dynamo_sort_key_attribute_name);
scan_req_.AddExpressionAttributeNames("#up", "___unpack_info___");
scan_req_.AddExpressionAttributeNames("#vs", "___version___");
scan_req_.AddExpressionAttributeNames("#dl", "___deleted___");
std::string proj_expr = "#mk, #up, #vs, #dl";
#ifdef ON_KEY_OBJECT
scan_req_.AddExpressionAttributeNames("#pl", "___payload___");
proj_expr += ", #pl";
#endif
scan_req_.SetProjectionExpression(std::move(proj_expr));
std::string key_cond = "#pk = :pk";
if (IsScanWithStartKey())
{
key_cond += " AND #mk";
if (inclusive_)
{
if (scan_forward_)
{
key_cond.append(" >= :sk");
}
else
{
key_cond.append(" <= :sk");
}
}
else
{
if (scan_forward_)
{
key_cond.append(" > :sk");
}
else
{
key_cond.append(" < :sk");
}
}
}
scan_req_.SetKeyConditionExpression(std::move(key_cond));
if (table_name_.Type() != txservice::TableType::Secondary &&
IsScanWithPushdownCondition())
{
SetPushedCond();
}
if (!scan_forward_)
{
scan_req_.SetScanIndexForward(false);
}
}
void DynamoScanner::SetPushedCond()
{
std::string pushed_str;
for (size_t i = 0; i < pushdown_condition_.size(); ++i)
{
auto &cond = pushdown_condition_[i];
if (pushed_str.size())
{
pushed_str.append(" AND ");
}
std::string attr = ":c" + std::to_string(i);
pushed_str.append(cond.field_name_ + " " + cond.op_ + " " + attr);
AttributeValue att_val;
switch (cond.data_type_)
{
case txservice::store::DataStoreDataType::Numeric:
att_val.SetN(cond.val_str_);
break;
case txservice::store::DataStoreDataType::String:
att_val.SetS(cond.val_str_);
break;
case txservice::store::DataStoreDataType::Blob:
{
Aws::Utils::ByteBuffer val_buffer(
reinterpret_cast<const unsigned char *>(cond.val_str_.data()),
cond.val_str_.length());
att_val.SetB(std::move(val_buffer));
break;
}
case txservice::store::DataStoreDataType::Bool:
{
if (cond.val_str_ == "true" || cond.val_str_ == "TRUE")
{
att_val.SetBool(true);
}
else
{
att_val.SetBool(false);
}
break;
}
default:
// Type is certain for pushdown conditions, should not be here.
assert(false);
}
scan_req_.AddExpressionAttributeValues(std::move(attr),
std::move(att_val));
}
if (pushdown_condition_.size() >= 3)
{
// If scan has object type filter, add attribute name ___payload___
scan_req_.AddExpressionAttributeNames("#pl", "___payload___");
}
scan_req_.SetFilterExpression(std::move(pushed_str));
}
/**
* @brief Scan a partition and return a query request future. Will use start
* key as exclusive start key, otherwise use start_key_ passed in from mysql.
*
* @tparam Direction
* @param partition
* @param page_limit max number of items returned per scan, or 1MB of data if
* <= 0
* @param start_key
* @return QueryOutcomeCallable
*/
template <bool Direction>
QueryOutcomeCallable HashPartitionDynamoScanner<Direction>::ScanPartition(
int32_t partition,
int32_t page_limit,
Aws::Map<Aws::String, AttributeValue> *start_key)
{
// AddExpressionAttributeValues will not override existing values, so
// we cannot reuse the same QueryRequest object across different partitions.
// Need to make a new copied object with each partition.
QueryRequest scan_req(scan_req_);
if (start_key != nullptr)
{
scan_req.SetExclusiveStartKey(std::move(*start_key));
}
AttributeValue partition_key;
partition_key.SetN(partition);
scan_req.AddExpressionAttributeValues(":pk", std::move(partition_key));
if (IsScanWithStartKey())
{
// Aws::Utils::ByteBuffer packed_key(
// reinterpret_cast<const unsigned char *>(
// start_key_->PackedValueSlice().data()),
// start_key_->PackedValueSlice().size());
AttributeValue sort_key;
// sort_key.SetB(std::move(packed_key));
DynamoHandlerTyped::BindDynamoReqForKey(sort_key, *start_key_);
scan_req.AddExpressionAttributeValues(":sk", std::move(sort_key));
}
if (page_limit > 0)
{
scan_req.SetLimit(page_limit);
}
return dynamo_client_->QueryCallable(scan_req);
}
/**
* @brief Retrieve result from query request future. Add results to heap_cache_
* add emplace to scan_res_ number of items scanned from this partition.
*
* @tparam Direction
* @param future
* @param partition
* @return true
* @return false
*/
template <bool Direction>
bool HashPartitionDynamoScanner<Direction>::AddPartitionResult(
QueryOutcomeCallable &future, int32_t partition)
{
assert(future.valid());
const QueryOutcome &result = future.get();
if (!result.IsSuccess())
{
LOG(INFO) << "DynamoDB query request failed, "
<< result.GetError().GetExceptionName() << ": "
<< result.GetError().GetMessage();
return false;
}
const Aws::Vector<Aws::Map<Aws::String, AttributeValue>> &rows =
result.GetResult().GetItems();
if (rows.size() == 0 &&
result.GetResult().GetLastEvaluatedKey().size() != 0)
{
// The page_limit we set is the number of items scanned before
// filtering. So if we have passed in pushdown condition to the scan
// request, we might get less items than page_limit, or even 0. But that
// does not necessarilly mean the scan has completed. Continue the scan
// if last evaluated key is not null.
auto end_key = result.GetResult().GetLastEvaluatedKey();
QueryOutcomeCallable future =
ScanPartition(partition, dynamo_scan_page_limit, &end_key);
return AddPartitionResult(future, partition);
}
if (rows.size())
{
for (auto &row : rows)
{
ScanHeapTuple<EloqKV::EloqKey, EloqKV::RedisEloqObject> heap_tuple(
partition);
#ifdef ON_KEY_OBJECT
EncodeDynamoRowObj(row, heap_tuple, rec_sch_);
#else
heap_tuple.rec_ = DynamoHandlerTyped::NewTxRecord(table_name_);
EncodeDynamoRow(row, heap_tuple, rec_sch_);
#endif
heap_cache_.push(std::move(heap_tuple));
}
scan_res_.insert_or_assign(
partition,
std::make_pair(rows.size(),
result.GetResult().GetLastEvaluatedKey()));
}
return true;
}
template <bool Direction>
bool HashPartitionDynamoScanner<Direction>::Init()
{
BuildScanPartitionRequest();
scan_res_.reserve(1024);
#ifdef USE_ONE_DATA_STORE_SHARD
QueryOutcomeCallable scan_future = ScanPartition(0, dynamo_scan_page_limit);
if (!AddPartitionResult(scan_future, 0))
{
return false;
}
#else
std::vector<QueryOutcomeCallable> future_vec;
future_vec.reserve(1024);
for (size_t partition = 0; partition < 1024; ++partition)
{
future_vec.emplace_back(
ScanPartition(partition, dynamo_scan_page_limit));
}
for (size_t partition = 0; partition < 1024; ++partition)
{
if (!AddPartitionResult(future_vec.at(partition), partition))
{
return false;
}
}
#endif
initialized_ = true;
return true;
}
template <bool Direction>
void HashPartitionDynamoScanner<Direction>::Current(
txservice::TxKey &key,
const txservice::TxRecord *&rec,
uint64_t &version_ts,
bool &deleted)
{
if (heap_cache_.size() == 0)
{
key = TxKey();
rec = nullptr;
return;
}
const ScanHeapTuple<EloqKV::EloqKey, EloqKV::RedisEloqObject> &top =
heap_cache_.top();
key = TxKey(top.key_.get());
rec = top.rec_.get();
version_ts = top.version_ts_;
deleted = top.deleted_;
}
template <bool Direction>
bool HashPartitionDynamoScanner<Direction>::MoveNext()
{
if (!initialized_)
{
if (!Init())
{
return false;
}
return true;
}
if (heap_cache_.size() == 0)
{
return true;
}
const ScanHeapTuple<EloqKV::EloqKey, EloqKV::RedisEloqObject> &top =
heap_cache_.top();
// sid is the partition id used in this query.
uint32_t partition = top.sid_;
heap_cache_.pop();
if (--scan_res_.at(partition).first == 0)
{
if (scan_res_.at(partition).second.size())
{
QueryOutcomeCallable scan_future =
ScanPartition(partition,
dynamo_scan_page_limit,
&scan_res_.at(partition).second);
if (!AddPartitionResult(scan_future, partition))
{
return false;
}
}
}
return true;
}
template <bool Direction>
void HashPartitionDynamoScanner<Direction>::End()
{
}
template class HashPartitionDynamoScanner<true>;
template class HashPartitionDynamoScanner<false>;
} // namespace EloqDS