Skip to content

Commit

Permalink
support new item in ScanContext (pingcap#6648)
Browse files Browse the repository at this point in the history
ref pingcap#5926

Signed-off-by: ywqzzy <[email protected]>
  • Loading branch information
hongyunyan authored and ywqzzy committed Feb 13, 2023
1 parent ac062c6 commit 2e8feda
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
2 changes: 1 addition & 1 deletion contrib/tipb
15 changes: 9 additions & 6 deletions dbms/src/Flash/Coprocessor/DAGStorageInterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,18 +319,19 @@ void DAGStorageInterpreter::execute(DAGPipeline & pipeline)

void DAGStorageInterpreter::executeImpl(DAGPipeline & pipeline)
{
auto scan_context = std::make_shared<DM::ScanContext>();
dagContext().scan_context_map[table_scan.getTableScanExecutorID()] = scan_context;
mvcc_query_info->scan_context = scan_context;

if (!mvcc_query_info->regions_query_info.empty())
{
auto scan_context = std::make_shared<DM::ScanContext>();
dagContext().scan_context_map[table_scan.getTableScanExecutorID()] = scan_context;
mvcc_query_info->scan_context = scan_context;
buildLocalStreams(pipeline, settings.max_block_size);
}

// Should build `remote_requests` and `null_stream` under protect of `table_structure_lock`.
auto null_stream_if_empty = std::make_shared<NullBlockInputStream>(storage_for_logical_table->getSampleBlockForColumns(required_columns));

auto remote_requests = buildRemoteRequests();
auto remote_requests = buildRemoteRequests(scan_context);

// A failpoint to test pause before alter lock released
FAIL_POINT_PAUSE(FailPoints::pause_with_alter_locks_acquired);
Expand Down Expand Up @@ -736,6 +737,7 @@ void DAGStorageInterpreter::buildLocalStreamsForPhysicalTable(
}
catch (RegionException & e)
{
query_info.mvcc_query_info->scan_context->total_local_region_num -= e.unavailable_region.size();
/// Recover from region exception for batchCop/MPP
if (dag_context.isBatchCop() || dag_context.isMPPTask())
{
Expand Down Expand Up @@ -768,6 +770,7 @@ void DAGStorageInterpreter::buildLocalStreams(DAGPipeline & pipeline, size_t max
size_t total_local_region_num = mvcc_query_info->regions_query_info.size();
if (total_local_region_num == 0)
return;
mvcc_query_info->scan_context->total_local_region_num = total_local_region_num;
const auto table_query_infos = generateSelectQueryInfos();
bool has_multiple_partitions = table_query_infos.size() > 1;
// MultiPartitionStreamPool will be disabled in no partition mode or single-partition case
Expand Down Expand Up @@ -1010,7 +1013,7 @@ std::tuple<Names, NamesAndTypes, std::vector<ExtraCastAfterTSMode>> DAGStorageIn
}

// Build remote requests from `region_retry_from_local_region` and `table_regions_info.remote_regions`
std::vector<RemoteRequest> DAGStorageInterpreter::buildRemoteRequests()
std::vector<RemoteRequest> DAGStorageInterpreter::buildRemoteRequests(const DM::ScanContextPtr & scan_context)
{
std::vector<RemoteRequest> remote_requests;
std::unordered_map<Int64, Int64> region_id_to_table_id_map;
Expand All @@ -1034,7 +1037,7 @@ std::vector<RemoteRequest> DAGStorageInterpreter::buildRemoteRequests()
const auto & retry_regions = retry_regions_map[physical_table_id];
if (retry_regions.empty())
continue;

scan_context->total_remote_region_num += retry_regions.size();
// Append the region into DAGContext to return them to the upper layer.
// The upper layer should refresh its cache about these regions.
for (const auto & r : retry_regions)
Expand Down
2 changes: 1 addition & 1 deletion dbms/src/Flash/Coprocessor/DAGStorageInterpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class DAGStorageInterpreter

std::tuple<Names, NamesAndTypes, std::vector<ExtraCastAfterTSMode>> getColumnsForTableScan(Int64 max_columns_to_read);

std::vector<RemoteRequest> buildRemoteRequests();
std::vector<RemoteRequest> buildRemoteRequests(const DM::ScanContextPtr & scan_context);

TableLockHolders releaseAlterLocks();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ struct MockWriter
summary.scan_context->total_dmfile_rough_set_index_load_time_ns = 10;
summary.scan_context->total_dmfile_read_time_ns = 200;
summary.scan_context->total_create_snapshot_time_ns = 5;
summary.scan_context->total_local_region_num = 10;
summary.scan_context->total_remote_region_num = 5;
return summary;
}

Expand Down
12 changes: 12 additions & 0 deletions dbms/src/Storages/DeltaMerge/ScanContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ class ScanContext
std::atomic<uint64_t> total_dmfile_read_time_ns{0};
std::atomic<uint64_t> total_create_snapshot_time_ns{0};

std::atomic<uint64_t> total_remote_region_num{0};
std::atomic<uint64_t> total_local_region_num{0};


ScanContext() = default;

void deserialize(const tipb::TiFlashScanContext & tiflash_scan_context_pb)
Expand All @@ -56,6 +60,8 @@ class ScanContext
total_dmfile_rough_set_index_load_time_ns = tiflash_scan_context_pb.total_dmfile_rough_set_index_load_time_ms() * 1000000;
total_dmfile_read_time_ns = tiflash_scan_context_pb.total_dmfile_read_time_ms() * 1000000;
total_create_snapshot_time_ns = tiflash_scan_context_pb.total_create_snapshot_time_ms() * 1000000;
total_remote_region_num = tiflash_scan_context_pb.total_remote_region_num();
total_local_region_num = tiflash_scan_context_pb.total_local_region_num();
}

tipb::TiFlashScanContext serialize()
Expand All @@ -68,6 +74,8 @@ class ScanContext
tiflash_scan_context_pb.set_total_dmfile_rough_set_index_load_time_ms(total_dmfile_rough_set_index_load_time_ns / 1000000);
tiflash_scan_context_pb.set_total_dmfile_read_time_ms(total_dmfile_read_time_ns / 1000000);
tiflash_scan_context_pb.set_total_create_snapshot_time_ms(total_create_snapshot_time_ns / 1000000);
tiflash_scan_context_pb.set_total_remote_region_num(total_remote_region_num);
tiflash_scan_context_pb.set_total_local_region_num(total_local_region_num);
return tiflash_scan_context_pb;
}

Expand All @@ -80,6 +88,8 @@ class ScanContext
total_dmfile_rough_set_index_load_time_ns += other.total_dmfile_rough_set_index_load_time_ns;
total_dmfile_read_time_ns += other.total_dmfile_read_time_ns;
total_create_snapshot_time_ns += other.total_create_snapshot_time_ns;
total_local_region_num += other.total_local_region_num;
total_remote_region_num += other.total_remote_region_num;
}

void merge(const tipb::TiFlashScanContext & other)
Expand All @@ -91,6 +101,8 @@ class ScanContext
total_dmfile_rough_set_index_load_time_ns += other.total_dmfile_rough_set_index_load_time_ms() * 1000000;
total_dmfile_read_time_ns += other.total_dmfile_read_time_ms() * 1000000;
total_create_snapshot_time_ns += other.total_create_snapshot_time_ms() * 1000000;
total_local_region_num += other.total_local_region_num();
total_remote_region_num += other.total_remote_region_num();
}
};

Expand Down

0 comments on commit 2e8feda

Please sign in to comment.