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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
|
#include <unordered_set>
#include <IO/WriteHelpers.h>
#include <Interpreters/MutationsInterpreter.h>
#include <Interpreters/evaluateConstantExpression.h>
#include <Parsers/ASTCreateQuery.h>
#include <Parsers/ASTDropQuery.h>
#include <Parsers/ASTLiteral.h>
#include <Processors/Executors/PullingPipelineExecutor.h>
#include <Processors/Sinks/SinkToStorage.h>
#include <QueryPipeline/Pipe.h>
#include <QueryPipeline/QueryPipelineBuilder.h>
#include <Storages/KVStorageUtils.h>
#include <Storages/KeyDescription.h>
#include <Storages/NamedCollectionsHelpers.h>
#include <Storages/StorageFactory.h>
#include <Storages/StorageInMemoryMetadata.h>
#include <Storages/StorageRedis.h>
#include <Storages/checkAndGetLiteralArgument.h>
#include <Common/Exception.h>
#include <Common/checkStackSize.h>
#include <Common/logger_useful.h>
#include <Common/parseAddress.h>
namespace DB
{
namespace ErrorCodes
{
extern const int BAD_ARGUMENTS;
extern const int LOGICAL_ERROR;
extern const int INTERNAL_REDIS_ERROR;
}
class RedisDataSource : public ISource
{
public:
RedisDataSource(
StorageRedis & storage_,
const Block & header,
FieldVectorPtr keys_,
FieldVector::const_iterator begin_,
FieldVector::const_iterator end_,
const size_t max_block_size_)
: ISource(header)
, storage(storage_)
, primary_key_pos(getPrimaryKeyPos(header, storage.getPrimaryKey()))
, keys(keys_)
, begin(begin_)
, end(end_)
, it(begin)
, max_block_size(max_block_size_)
{
}
RedisDataSource(StorageRedis & storage_, const Block & header, const size_t max_block_size_, const String & pattern_ = "*")
: ISource(header)
, storage(storage_)
, primary_key_pos(getPrimaryKeyPos(header, storage.getPrimaryKey()))
, iterator(-1)
, pattern(pattern_)
, max_block_size(max_block_size_)
{
}
String getName() const override { return storage.getName(); }
Chunk generate() override
{
if (keys)
return generateWithKeys();
return generateFullScan();
}
Chunk generateWithKeys()
{
const auto & sample_block = getPort().getHeader();
if (it >= end)
{
it = {};
return {};
}
const auto & key_column_type = sample_block.getByName(storage.getPrimaryKey().at(0)).type;
auto raw_keys = serializeKeysToRawString(it, end, key_column_type, max_block_size);
return storage.getBySerializedKeys(raw_keys, nullptr);
}
/// TODO scan may get duplicated keys when Redis is rehashing, it is a very rare case.
Chunk generateFullScan()
{
checkStackSize();
/// redis scan ending
if (iterator == 0)
return {};
RedisArray scan_keys;
RedisIterator next_iterator;
std::tie(next_iterator, scan_keys) = storage.scan(iterator == -1 ? 0 : iterator, pattern, max_block_size);
iterator = next_iterator;
/// redis scan can return nothing
if (scan_keys.isNull() || scan_keys.size() == 0)
return generateFullScan();
const auto & sample_block = getPort().getHeader();
MutableColumns columns = sample_block.cloneEmptyColumns();
RedisArray values = storage.multiGet(scan_keys);
for (size_t i = 0; i < scan_keys.size() && !values.get<RedisBulkString>(i).isNull(); i++)
{
fillColumns(scan_keys.get<RedisBulkString>(i).value(),
values.get<RedisBulkString>(i).value(),
primary_key_pos, sample_block, columns
);
}
Block block = sample_block.cloneWithColumns(std::move(columns));
return Chunk(block.getColumns(), block.rows());
}
private:
StorageRedis & storage;
size_t primary_key_pos;
/// For key scan
FieldVectorPtr keys = nullptr;
FieldVector::const_iterator begin;
FieldVector::const_iterator end;
FieldVector::const_iterator it;
/// For full scan
RedisIterator iterator;
String pattern;
const size_t max_block_size;
};
class RedisSink : public SinkToStorage
{
public:
RedisSink(StorageRedis & storage_, const StorageMetadataPtr & metadata_snapshot_);
void consume(Chunk chunk) override;
String getName() const override { return "RedisSink"; }
private:
StorageRedis & storage;
StorageMetadataPtr metadata_snapshot;
size_t primary_key_pos = 0;
};
RedisSink::RedisSink(StorageRedis & storage_, const StorageMetadataPtr & metadata_snapshot_)
: SinkToStorage(metadata_snapshot_->getSampleBlock())
, storage(storage_)
, metadata_snapshot(metadata_snapshot_)
{
for (const auto & column : getHeader())
{
if (column.name == storage.getPrimaryKey()[0])
break;
++primary_key_pos;
}
}
void RedisSink::consume(Chunk chunk)
{
auto rows = chunk.getNumRows();
auto block = getHeader().cloneWithColumns(chunk.detachColumns());
WriteBufferFromOwnString wb_key;
WriteBufferFromOwnString wb_value;
RedisArray data;
for (size_t i = 0; i < rows; ++i)
{
wb_key.restart();
wb_value.restart();
size_t idx = 0;
for (const auto & elem : block)
{
elem.type->getDefaultSerialization()->serializeBinary(*elem.column, i, idx == primary_key_pos ? wb_key : wb_value, {});
++idx;
}
data.add(wb_key.str());
data.add(wb_value.str());
}
storage.multiSet(data);
}
StorageRedis::StorageRedis(
const StorageID & table_id_,
const RedisConfiguration & configuration_,
ContextPtr context_,
const StorageInMemoryMetadata & storage_metadata,
const String & primary_key_)
: IStorage(table_id_)
, WithContext(context_->getGlobalContext())
, table_id(table_id_)
, configuration(configuration_)
, log(&Poco::Logger::get("StorageRedis"))
, primary_key(primary_key_)
{
pool = std::make_shared<RedisPool>(configuration.pool_size);
setInMemoryMetadata(storage_metadata);
}
Pipe StorageRedis::read(
const Names & column_names,
const StorageSnapshotPtr & storage_snapshot,
SelectQueryInfo & query_info,
ContextPtr context_,
QueryProcessingStage::Enum /*processed_stage*/,
size_t max_block_size,
size_t num_streams)
{
storage_snapshot->check(column_names);
FieldVectorPtr keys;
bool all_scan = false;
Block header = storage_snapshot->metadata->getSampleBlock();
auto primary_key_data_type = header.getByName(primary_key).type;
std::tie(keys, all_scan) = getFilterKeys(primary_key, primary_key_data_type, query_info, context_);
if (all_scan)
{
return Pipe(std::make_shared<RedisDataSource>(*this, header, max_block_size));
}
else
{
if (keys->empty())
return {};
Pipes pipes;
::sort(keys->begin(), keys->end());
keys->erase(std::unique(keys->begin(), keys->end()), keys->end());
size_t num_keys = keys->size();
size_t num_threads = std::min<size_t>(num_streams, keys->size());
num_threads = std::min<size_t>(num_threads, configuration.pool_size);
assert(num_keys <= std::numeric_limits<uint32_t>::max());
for (size_t thread_idx = 0; thread_idx < num_threads; ++thread_idx)
{
size_t begin = num_keys * thread_idx / num_threads;
size_t end = num_keys * (thread_idx + 1) / num_threads;
pipes.emplace_back(
std::make_shared<RedisDataSource>(*this, header, keys, keys->begin() + begin, keys->begin() + end, max_block_size));
}
return Pipe::unitePipes(std::move(pipes));
}
}
namespace
{
// host:port, db_index, password, pool_size
RedisConfiguration getRedisConfiguration(ASTs & engine_args, ContextPtr context)
{
RedisConfiguration configuration;
if (engine_args.empty())
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Bad arguments count when creating Redis table engine");
if (auto named_collection = tryGetNamedCollectionWithOverrides(engine_args, context))
{
validateNamedCollection(
*named_collection,
ValidateKeysMultiset<RedisEqualKeysSet>{"host", "port", "hostname", "password", "db_index", "pool_size"},
{});
configuration.host = named_collection->getAny<String>({"host", "hostname"});
configuration.port = static_cast<uint32_t>(named_collection->getOrDefault<UInt64>("port", 6379));
configuration.password = named_collection->getOrDefault<String>("password", DEFAULT_REDIS_PASSWORD);
configuration.db_index = static_cast<uint32_t>(named_collection->getOrDefault<UInt64>("db_index", DEFAULT_REDIS_DB_INDEX));
configuration.pool_size = static_cast<uint32_t>(named_collection->getOrDefault<UInt64>("pool_size", DEFAULT_REDIS_POOL_SIZE));
}
else
{
for (auto & engine_arg : engine_args)
engine_arg = evaluateConstantExpressionOrIdentifierAsLiteral(engine_arg, context);
/// 6379 is the default Redis port.
auto parsed_host_port = parseAddress(checkAndGetLiteralArgument<String>(engine_args[0], "host:port"), 6379);
configuration.host = parsed_host_port.first;
configuration.port = parsed_host_port.second;
if (engine_args.size() > 1)
configuration.db_index = static_cast<uint32_t>(checkAndGetLiteralArgument<UInt64>(engine_args[1], "db_index"));
else
configuration.db_index = DEFAULT_REDIS_DB_INDEX;
if (engine_args.size() > 2)
configuration.password = checkAndGetLiteralArgument<String>(engine_args[2], "password");
else
configuration.password = DEFAULT_REDIS_PASSWORD;
if (engine_args.size() > 3)
configuration.pool_size = static_cast<uint32_t>(checkAndGetLiteralArgument<UInt64>(engine_args[3], "pool_size"));
else
configuration.pool_size = DEFAULT_REDIS_POOL_SIZE;
}
context->getRemoteHostFilter().checkHostAndPort(configuration.host, toString(configuration.port));
return configuration;
}
StoragePtr createStorageRedis(const StorageFactory::Arguments & args)
{
auto configuration = getRedisConfiguration(args.engine_args, args.getLocalContext());
StorageInMemoryMetadata metadata;
metadata.setColumns(args.columns);
metadata.setConstraints(args.constraints);
metadata.setComment(args.comment);
if (!args.storage_def->primary_key)
throw Exception(ErrorCodes::BAD_ARGUMENTS, "StorageRedis must require one column in primary key");
auto primary_key_desc = KeyDescription::getKeyFromAST(args.storage_def->primary_key->ptr(), metadata.columns, args.getContext());
auto primary_key_names = primary_key_desc.expression->getRequiredColumns();
if (primary_key_names.size() != 1)
{
throw Exception(ErrorCodes::BAD_ARGUMENTS, "StorageRedis must require one column in primary key");
}
return std::make_shared<StorageRedis>(args.table_id, configuration, args.getContext(), metadata, primary_key_names[0]);
}
}
Chunk StorageRedis::getBySerializedKeys(const std::vector<std::string> & keys, PaddedPODArray<UInt8> * null_map) const
{
RedisArray redis_keys;
for (const auto & key : keys)
redis_keys.add(key);
return getBySerializedKeys(redis_keys, null_map);
}
Chunk StorageRedis::getBySerializedKeys(const RedisArray & keys, PaddedPODArray<UInt8> * null_map) const
{
Block sample_block = getInMemoryMetadataPtr()->getSampleBlock();
size_t primary_key_pos = getPrimaryKeyPos(sample_block, getPrimaryKey());
MutableColumns columns = sample_block.cloneEmptyColumns();
RedisArray values = multiGet(keys);
if (values.isNull() || values.size() == 0)
return {};
if (null_map)
{
null_map->clear();
null_map->resize_fill(keys.size(), 1);
}
for (size_t i = 0; i < values.size(); ++i)
{
if (!values.get<RedisBulkString>(i).isNull())
{
fillColumns(keys.get<RedisBulkString>(i).value(),
values.get<RedisBulkString>(i).value(),
primary_key_pos, sample_block, columns
);
}
else /// key not found
{
if (null_map)
{
(*null_map)[i] = 0;
for (size_t col_idx = 0; col_idx < sample_block.columns(); ++col_idx)
{
columns[col_idx]->insert(sample_block.getByPosition(col_idx).type->getDefault());
}
}
}
}
size_t num_rows = columns.at(0)->size();
return Chunk(std::move(columns), num_rows);
}
std::pair<RedisIterator, RedisArray> StorageRedis::scan(RedisIterator iterator, const String & pattern, uint64_t max_count)
{
auto connection = getRedisConnection(pool, configuration);
RedisCommand scan("SCAN");
scan << toString(iterator) << "MATCH" << pattern << "COUNT" << toString(max_count);
const auto & result = connection->client->execute<RedisArray>(scan);
RedisIterator next = parse<RedisIterator>(result.get<RedisBulkString>(0).value());
return {next, result.get<RedisArray>(1)};
}
RedisArray StorageRedis::multiGet(const RedisArray & keys) const
{
auto connection = getRedisConnection(pool, configuration);
RedisCommand cmd_mget("MGET");
for (size_t i = 0; i < keys.size(); ++i)
cmd_mget.add(keys.get<RedisBulkString>(i));
return connection->client->execute<RedisArray>(cmd_mget);
}
void StorageRedis::multiSet(const RedisArray & data) const
{
auto connection = getRedisConnection(pool, configuration);
RedisCommand cmd_mget("MSET");
for (size_t i = 0; i < data.size(); ++i)
cmd_mget.add(data.get<RedisBulkString>(i));
auto ret = connection->client->execute<RedisSimpleString>(cmd_mget);
if (ret != "OK")
throw Exception(ErrorCodes::INTERNAL_REDIS_ERROR, "Fail to write to redis table {}, for {}", table_id.getFullNameNotQuoted(), ret);
}
RedisInteger StorageRedis::multiDelete(const RedisArray & keys) const
{
auto connection = getRedisConnection(pool, configuration);
RedisCommand cmd("DEL");
for (size_t i = 0; i < keys.size(); ++i)
cmd.add(keys.get<RedisBulkString>(i));
auto ret = connection->client->execute<RedisInteger>(cmd);
if (ret != static_cast<RedisInteger>(keys.size()))
LOG_DEBUG(
log,
"Try to delete {} rows but actually deleted {} rows from redis table {}.",
keys.size(),
ret,
table_id.getFullNameNotQuoted());
return ret;
}
Chunk StorageRedis::getByKeys(const ColumnsWithTypeAndName & keys, PaddedPODArray<UInt8> & null_map, const Names &) const
{
if (keys.size() != 1)
throw Exception(ErrorCodes::LOGICAL_ERROR, "StorageRedis supports only one key, got: {}", keys.size());
auto raw_keys = serializeKeysToRawString(keys[0]);
if (raw_keys.size() != keys[0].column->size())
throw DB::Exception(ErrorCodes::LOGICAL_ERROR, "Assertion failed: {} != {}", raw_keys.size(), keys[0].column->size());
return getBySerializedKeys(raw_keys, &null_map);
}
Block StorageRedis::getSampleBlock(const Names &) const
{
return getInMemoryMetadataPtr()->getSampleBlock();
}
SinkToStoragePtr StorageRedis::write(
const ASTPtr & /*query*/,
const StorageMetadataPtr & metadata_snapshot,
ContextPtr /*context*/,
bool /*async_insert*/)
{
return std::make_shared<RedisSink>(*this, metadata_snapshot);
}
void StorageRedis::truncate(const ASTPtr & query, const StorageMetadataPtr &, ContextPtr, TableExclusiveLockHolder &)
{
auto connection = getRedisConnection(pool, configuration);
auto * truncate_query = query->as<ASTDropQuery>();
assert(truncate_query != nullptr);
RedisCommand cmd_flush_db("FLUSHDB");
if (!truncate_query->sync)
cmd_flush_db.add("ASYNC");
auto ret = connection->client->execute<RedisSimpleString>(cmd_flush_db);
if (ret != "OK")
throw Exception(ErrorCodes::INTERNAL_REDIS_ERROR, "Fail to truncate redis table {}, for {}", table_id.getFullNameNotQuoted(), ret);
}
void StorageRedis::checkMutationIsPossible(const MutationCommands & commands, const Settings & /* settings */) const
{
if (commands.empty())
return;
if (commands.size() > 1)
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Mutations cannot be combined for StorageRedis");
const auto command_type = commands.front().type;
if (command_type != MutationCommand::Type::UPDATE && command_type != MutationCommand::Type::DELETE)
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Only DELETE and UPDATE mutation supported for StorageRedis");
}
void StorageRedis::mutate(const MutationCommands & commands, ContextPtr context_)
{
if (commands.empty())
return;
assert(commands.size() == 1);
auto metadata_snapshot = getInMemoryMetadataPtr();
auto storage = getStorageID();
auto storage_ptr = DatabaseCatalog::instance().getTable(storage, context_);
if (commands.front().type == MutationCommand::Type::DELETE)
{
MutationsInterpreter::Settings settings(true);
settings.return_all_columns = true;
settings.return_mutated_rows = true;
auto interpreter = std::make_unique<MutationsInterpreter>(storage_ptr, metadata_snapshot, commands, context_, settings);
auto pipeline = QueryPipelineBuilder::getPipeline(interpreter->execute());
PullingPipelineExecutor executor(pipeline);
auto sink = std::make_shared<RedisSink>(*this, metadata_snapshot);
auto header = interpreter->getUpdatedHeader();
auto primary_key_pos = header.getPositionByName(primary_key);
Block block;
while (executor.pull(block))
{
auto & column_type_name = block.getByPosition(primary_key_pos);
auto column = column_type_name.column;
auto size = column->size();
RedisArray keys;
WriteBufferFromOwnString wb_key;
for (size_t i = 0; i < size; ++i)
{
wb_key.restart();
column_type_name.type->getDefaultSerialization()->serializeBinary(*column, i, wb_key, {});
keys.add(wb_key.str());
}
multiDelete(keys);
}
return;
}
assert(commands.front().type == MutationCommand::Type::UPDATE);
if (commands.front().column_to_update_expression.contains(primary_key))
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Primary key cannot be updated (cannot update column {})", primary_key);
MutationsInterpreter::Settings settings(true);
settings.return_all_columns = true;
settings.return_mutated_rows = true;
auto interpreter = std::make_unique<MutationsInterpreter>(storage_ptr, metadata_snapshot, commands, context_, settings);
auto pipeline = QueryPipelineBuilder::getPipeline(interpreter->execute());
PullingPipelineExecutor executor(pipeline);
auto sink = std::make_shared<RedisSink>(*this, metadata_snapshot);
Block block;
while (executor.pull(block))
{
sink->consume(Chunk{block.getColumns(), block.rows()});
}
}
/// TODO support ttl
void registerStorageRedis(StorageFactory & factory)
{
StorageFactory::StorageFeatures features{
.supports_sort_order = true,
.supports_parallel_insert = true,
.source_access_type = AccessType::REDIS,
};
factory.registerStorage("Redis", createStorageRedis, features);
}
}
|