Skip to content

Commit 9200843

Browse files
committed
support fan out for backfill read and fix more
1 parent 2cc168f commit 9200843

File tree

3 files changed

+41
-13
lines changed

3 files changed

+41
-13
lines changed

src/Storages/Streaming/StorageStream.cpp

+19-6
Original file line numberDiff line numberDiff line change
@@ -486,15 +486,19 @@ void StorageStream::readConcat(
486486
for (auto & stream_shard : shards_to_read)
487487
{
488488
auto create_streaming_source = [this, header, storage_snapshot, stream_shard, seek_to_info = query_info.seek_to_info, context_](
489-
Int64 & max_sn_in_parts) {
489+
Int64 & max_sn_in_parts) -> SourcePtr {
490490
if (max_sn_in_parts < 0)
491491
{
492492
/// Fallback to seek streaming store
493493
auto offsets = stream_shard->getOffsets(seek_to_info);
494494
LOG_INFO(log, "Fused read fallbacks to seek stream for shard={} since there are no historical data", stream_shard->shard);
495495

496-
return std::make_shared<StreamingStoreSource>(
497-
stream_shard, header, storage_snapshot, context_, offsets[stream_shard->shard], log);
496+
if (context_->getSettingsRef().query_resource_group.value == "shared")
497+
return source_multiplexers->createChannel(
498+
stream_shard, header.getNames(), storage_snapshot, context_, offsets[stream_shard->shard]);
499+
else
500+
return std::make_shared<StreamingStoreSource>(
501+
stream_shard, header, storage_snapshot, context_, offsets[stream_shard->shard], log);
498502
}
499503

500504
auto committed = stream_shard->storage->inMemoryCommittedSN();
@@ -526,7 +530,12 @@ void StorageStream::readConcat(
526530
max_sn_in_parts,
527531
committed);
528532

529-
return std::make_shared<StreamingStoreSource>(stream_shard, header, storage_snapshot, context_, max_sn_in_parts + 1, log);
533+
if (context_->getSettingsRef().query_resource_group.value == "shared")
534+
return source_multiplexers->createChannel(
535+
stream_shard, header.getNames(), storage_snapshot, context_, max_sn_in_parts + 1);
536+
else
537+
return std::make_shared<StreamingStoreSource>(
538+
stream_shard, header, storage_snapshot, context_, max_sn_in_parts + 1, log);
530539
}
531540
else
532541
{
@@ -542,8 +551,12 @@ void StorageStream::readConcat(
542551

543552
/// We need reset max_sn_in_parts to tell caller that we are seeking streaming store directly
544553
max_sn_in_parts = -1;
545-
return std::make_shared<StreamingStoreSource>(
546-
stream_shard, header, storage_snapshot, context_, offsets[stream_shard->shard], log);
554+
if (context_->getSettingsRef().query_resource_group.value == "shared")
555+
return source_multiplexers->createChannel(
556+
stream_shard, header.getNames(), storage_snapshot, context_, offsets[stream_shard->shard]);
557+
else
558+
return std::make_shared<StreamingStoreSource>(
559+
stream_shard, header, storage_snapshot, context_, offsets[stream_shard->shard], log);
547560
}
548561
};
549562

src/Storages/Streaming/StreamingStoreSourceMultiplexer.cpp

+18-7
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,8 @@ std::pair<String, Int32> StreamingStoreSourceMultiplexer::getStreamShard() const
280280
return stream_shard->getStreamShard();
281281
}
282282

283+
std::atomic<uint32_t> StreamingStoreSourceMultiplexers::multiplexer_id = 0;
284+
283285
StreamingStoreSourceMultiplexers::StreamingStoreSourceMultiplexers(ContextPtr global_context_, Poco::Logger * log_)
284286
: global_context(std::move(global_context_)), log(log_)
285287
{
@@ -310,7 +312,8 @@ StreamingStoreSourceChannelPtr StreamingStoreSourceMultiplexers::createChannel(
310312
{
311313
multiplexers.emplace(
312314
shard,
313-
StreamingStoreSourceMultiplexerPtrs{std::make_shared<StreamingStoreSourceMultiplexer>(0, stream_shard, global_context, log)});
315+
StreamingStoreSourceMultiplexerPtrs{
316+
std::make_shared<StreamingStoreSourceMultiplexer>(getMultiplexerID(), stream_shard, global_context, log)});
314317
iter = multiplexers.find(shard);
315318
}
316319

@@ -342,7 +345,7 @@ StreamingStoreSourceChannelPtr StreamingStoreSourceMultiplexers::createChannel(
342345
/// If min channels is greater than > 20(default value), create another multiplexer for this shard
343346
if (min_channels > global_context->getSettingsRef().max_channels_per_resource_group.value)
344347
{
345-
best_multiplexer = std::make_shared<StreamingStoreSourceMultiplexer>(iter->second.size(), stream_shard, global_context, log);
348+
best_multiplexer = std::make_shared<StreamingStoreSourceMultiplexer>(getMultiplexerID(), stream_shard, global_context, log);
346349
iter->second.push_back(best_multiplexer);
347350
}
348351

@@ -351,7 +354,7 @@ StreamingStoreSourceChannelPtr StreamingStoreSourceMultiplexers::createChannel(
351354
else
352355
{
353356
/// All multiplexers are shutdown
354-
auto multiplexer{std::make_shared<StreamingStoreSourceMultiplexer>(iter->second.size(), stream_shard, global_context, log)};
357+
auto multiplexer{std::make_shared<StreamingStoreSourceMultiplexer>(getMultiplexerID(), stream_shard, global_context, log)};
355358
iter->second.push_back(multiplexer);
356359
return multiplexer->createChannel(column_names, storage_snapshot, query_context);
357360
}
@@ -366,7 +369,7 @@ StreamingStoreSourceChannelPtr StreamingStoreSourceMultiplexers::createIndepende
366369
/// will startup after `StreamingStoreSourceChannel::recover()` and reset recovered sn
367370
/// The `multiplexer` is cached in created StreamingStoreSourceChannel, we can release this one
368371
auto multiplexer = std::make_shared<StreamingStoreSourceMultiplexer>(
369-
0, std::move(stream_shard), global_context, log, [this](auto multiplexer_) { attachToSharedGroup(multiplexer_); });
372+
getMultiplexerID(), std::move(stream_shard), global_context, log, [this](auto multiplexer_) { attachToSharedGroup(multiplexer_); });
370373
return multiplexer->createChannel(column_names, storage_snapshot, query_context);
371374
}
372375

@@ -379,7 +382,7 @@ StreamingStoreSourceChannelPtr StreamingStoreSourceMultiplexers::createIndepende
379382
{
380383
/// The `multiplexer` is cached in created StreamingStoreSourceChannel, we can release this one
381384
auto multiplexer = std::make_shared<StreamingStoreSourceMultiplexer>(
382-
0, std::move(stream_shard), global_context, log, [this](auto multiplexer_) { attachToSharedGroup(multiplexer_); });
385+
getMultiplexerID(), std::move(stream_shard), global_context, log, [this](auto multiplexer_) { attachToSharedGroup(multiplexer_); });
383386
auto channel = multiplexer->createChannel(column_names, storage_snapshot, query_context);
384387
multiplexer->resetSequenceNumber(start_sn);
385388
multiplexer->startup();
@@ -394,8 +397,17 @@ void StreamingStoreSourceMultiplexers::attachToSharedGroup(StreamingStoreSourceM
394397
detached_multiplexers.clear();
395398

396399
auto & multiplexer_list = multiplexers[multiplexer->stream_shard->getShard()];
397-
for (auto & shared_multiplexer : multiplexer_list)
400+
for (auto it = multiplexer_list.begin(); it != multiplexer_list.end();)
398401
{
402+
if ((*it)->isShutdown())
403+
{
404+
it = multiplexer_list.erase(it);
405+
continue;
406+
}
407+
408+
auto & shared_multiplexer = *it;
409+
++it;
410+
399411
/// Skip multiplexer that already have too many channels
400412
if (shared_multiplexer->totalChannels() > global_context->getSettingsRef().max_channels_per_resource_group.value)
401413
continue;
@@ -410,7 +422,6 @@ void StreamingStoreSourceMultiplexers::attachToSharedGroup(StreamingStoreSourceM
410422
}
411423

412424
/// Not detach channels into any existed shared multiplexer, so we reuse it and join in shared groups
413-
multiplexer->id = multiplexer_list.size(); /// it's thread safe
414425
multiplexer_list.emplace_back(std::move(multiplexer));
415426
}
416427
}

src/Storages/Streaming/StreamingStoreSourceMultiplexer.h

+4
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,14 @@ class StreamingStoreSourceMultiplexers final
122122

123123
void attachToSharedGroup(StreamingStoreSourceMultiplexerPtr multiplexer);
124124

125+
uint32_t getMultiplexerID() { return multiplexer_id.fetch_add(1); }
126+
125127
private:
126128
ContextPtr global_context;
127129
Poco::Logger * log;
128130

131+
static std::atomic<uint32_t> multiplexer_id;
132+
129133
std::mutex multiplexers_mutex;
130134
std::unordered_map<Int32, StreamingStoreSourceMultiplexerPtrs> multiplexers;
131135
StreamingStoreSourceMultiplexerPtrs detached_multiplexers;

0 commit comments

Comments
 (0)