Skip to content

Commit bf34f36

Browse files
Storages: add inverted index statistics to ScanContext (#10064)
ref #9843 Part 5 of inverted index, add inverted index statistics to `ScanContext` to improve observability. Other changes: - rename `total_vector_idx_xxx` to `vector_idx_xxx`. Signed-off-by: Lloyd-Pottiger <[email protected]>
1 parent 0d0dc05 commit bf34f36

File tree

11 files changed

+237
-144
lines changed

11 files changed

+237
-144
lines changed

dbms/src/Storages/DeltaMerge/Index/InvertedIndex/Reader/ReaderFromColumnFileTiny.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <Storages/DeltaMerge/Index/InvertedIndex/Reader.h>
2222
#include <Storages/DeltaMerge/Index/InvertedIndex/Reader/ReaderFromColumnFileTiny.h>
2323
#include <Storages/DeltaMerge/Index/LocalIndexCache.h>
24+
#include <Storages/DeltaMerge/ScanContext.h>
2425

2526

2627
namespace DB::DM
@@ -30,11 +31,13 @@ InvertedIndexReaderFromColumnFileTiny::InvertedIndexReaderFromColumnFileTiny(
3031
const ColumnRangePtr & column_range_,
3132
const ColumnFileTiny & tiny_file_,
3233
const IColumnFileDataProviderPtr & data_provider_,
33-
const LocalIndexCachePtr & local_index_cache_)
34+
const LocalIndexCachePtr & local_index_cache_,
35+
const ScanContextPtr & scan_context_)
3436
: tiny_file(tiny_file_)
3537
, data_provider(data_provider_)
3638
, column_range(column_range_)
3739
, local_index_cache(local_index_cache_)
40+
, scan_context(scan_context_)
3841
{
3942
GET_METRIC(tiflash_inverted_index_active_instances, type_memory_reader).Increment();
4043
}
@@ -86,7 +89,6 @@ BitmapFilterPtr InvertedIndexReaderFromColumnFileTiny::load(const SingleColumnRa
8689

8790
{
8891
// Statistics
89-
// TODO: add more statistics to ScanContext
9092
double elapsed = w.elapsedSecondsFromLastTime();
9193
if (is_load_from_storage)
9294
{
@@ -96,11 +98,25 @@ BitmapFilterPtr InvertedIndexReaderFromColumnFileTiny::load(const SingleColumnRa
9698
{
9799
GET_METRIC(tiflash_inverted_index_duration, type_load_cache).Observe(elapsed);
98100
}
101+
102+
if (scan_context)
103+
{
104+
scan_context->inverted_idx_load_from_disk.fetch_add(is_load_from_storage, std::memory_order_relaxed);
105+
scan_context->inverted_idx_load_from_cache.fetch_add(!is_load_from_storage, std::memory_order_relaxed);
106+
scan_context->inverted_idx_load_time_ms.fetch_add(elapsed * 1000, std::memory_order_relaxed);
107+
}
99108
}
100109

101110
RUNTIME_CHECK(index_reader != nullptr);
102111
auto bitmap_filter = column_range->set->search(index_reader, tiny_file.getRows());
103-
GET_METRIC(tiflash_inverted_index_duration, type_search).Observe(w.elapsedSecondsFromLastTime());
112+
auto elapsed = w.elapsedSecondsFromLastTime();
113+
GET_METRIC(tiflash_inverted_index_duration, type_search).Observe(elapsed);
114+
if (scan_context)
115+
{
116+
scan_context->inverted_idx_search_time_ms.fetch_add(elapsed * 1000, std::memory_order_relaxed);
117+
scan_context->inverted_idx_indexed_rows.fetch_add(bitmap_filter->size(), std::memory_order_relaxed);
118+
scan_context->inverted_idx_search_selected_rows.fetch_add(bitmap_filter->count(), std::memory_order_relaxed);
119+
}
104120
return bitmap_filter;
105121
}
106122

dbms/src/Storages/DeltaMerge/Index/InvertedIndex/Reader/ReaderFromColumnFileTiny.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <Storages/DeltaMerge/ColumnFile/ColumnFileTiny.h>
1919
#include <Storages/DeltaMerge/Filter/ColumnRange_fwd.h>
2020
#include <Storages/DeltaMerge/Index/LocalIndexCache_fwd.h>
21+
#include <Storages/DeltaMerge/ScanContext_fwd.h>
2122

2223
namespace DB::DM
2324
{
@@ -30,6 +31,7 @@ class InvertedIndexReaderFromColumnFileTiny
3031
const ColumnRangePtr column_range;
3132
// Global local index cache
3233
const LocalIndexCachePtr local_index_cache;
34+
const ScanContextPtr scan_context;
3335

3436
bool loaded = false;
3537

@@ -38,7 +40,8 @@ class InvertedIndexReaderFromColumnFileTiny
3840
const ColumnRangePtr & column_range_,
3941
const ColumnFileTiny & tiny_file_,
4042
const IColumnFileDataProviderPtr & data_provider_,
41-
const LocalIndexCachePtr & local_index_cache_);
43+
const LocalIndexCachePtr & local_index_cache_,
44+
const ScanContextPtr & scan_context_ = nullptr);
4245

4346
~InvertedIndexReaderFromColumnFileTiny();
4447

dbms/src/Storages/DeltaMerge/Index/InvertedIndex/Reader/ReaderFromDMFile.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <Storages/DeltaMerge/Index/InvertedIndex/Reader.h>
2020
#include <Storages/DeltaMerge/Index/InvertedIndex/Reader/ReaderFromDMFile.h>
2121
#include <Storages/DeltaMerge/Index/LocalIndexCache.h>
22+
#include <Storages/DeltaMerge/ScanContext.h>
2223
#include <Storages/S3/FileCache.h>
2324
#include <Storages/S3/FileCachePerf.h>
2425

@@ -34,10 +35,12 @@ namespace DB::DM
3435
InvertedIndexReaderFromDMFile::InvertedIndexReaderFromDMFile(
3536
const ColumnRangePtr & column_range_,
3637
const DMFilePtr & dmfile_,
37-
const LocalIndexCachePtr & local_index_cache_)
38+
const LocalIndexCachePtr & local_index_cache_,
39+
const ScanContextPtr & scan_context_)
3840
: dmfile(dmfile_)
3941
, column_range(column_range_)
4042
, local_index_cache(local_index_cache_)
43+
, scan_context(scan_context_)
4144
{
4245
GET_METRIC(tiflash_inverted_index_active_instances, type_disk_reader).Increment();
4346
}
@@ -122,7 +125,6 @@ BitmapFilterPtr InvertedIndexReaderFromDMFile::load(const SingleColumnRangePtr &
122125

123126
{
124127
// Statistics
125-
// TODO: add more statistics to ScanContext
126128
double elapsed = w.elapsedSecondsFromLastTime();
127129
if (has_s3_download)
128130
{
@@ -139,11 +141,28 @@ BitmapFilterPtr InvertedIndexReaderFromDMFile::load(const SingleColumnRangePtr &
139141
{
140142
GET_METRIC(tiflash_inverted_index_duration, type_load_cache).Observe(elapsed);
141143
}
144+
145+
if (scan_context)
146+
{
147+
scan_context->inverted_idx_load_from_s3.fetch_add(has_s3_download, std::memory_order_relaxed);
148+
scan_context->inverted_idx_load_from_disk.fetch_add(has_load_from_file, std::memory_order_relaxed);
149+
scan_context->inverted_idx_load_from_cache.fetch_add(
150+
!has_s3_download && !has_load_from_file,
151+
std::memory_order_relaxed);
152+
scan_context->inverted_idx_load_time_ms.fetch_add(elapsed * 1000, std::memory_order_relaxed);
153+
}
142154
}
143155

144156
RUNTIME_CHECK(inverted_index != nullptr);
145157
auto bitmap_filter = column_range->set->search(inverted_index, dmfile->getRows());
146-
GET_METRIC(tiflash_inverted_index_duration, type_search).Observe(w.elapsedSecondsFromLastTime());
158+
auto elapsed = w.elapsedSecondsFromLastTime();
159+
GET_METRIC(tiflash_inverted_index_duration, type_search).Observe(elapsed);
160+
if (scan_context)
161+
{
162+
scan_context->inverted_idx_search_time_ms.fetch_add(elapsed * 1000, std::memory_order_relaxed);
163+
scan_context->inverted_idx_indexed_rows.fetch_add(bitmap_filter->size(), std::memory_order_relaxed);
164+
scan_context->inverted_idx_search_selected_rows.fetch_add(bitmap_filter->count(), std::memory_order_relaxed);
165+
}
147166
return bitmap_filter;
148167
}
149168

dbms/src/Storages/DeltaMerge/Index/InvertedIndex/Reader/ReaderFromDMFile.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <Storages/DeltaMerge/File/DMFile_fwd.h>
1919
#include <Storages/DeltaMerge/Filter/ColumnRange_fwd.h>
2020
#include <Storages/DeltaMerge/Index/LocalIndexCache_fwd.h>
21+
#include <Storages/DeltaMerge/ScanContext_fwd.h>
2122

2223
namespace DB::DM
2324
{
@@ -29,14 +30,16 @@ class InvertedIndexReaderFromDMFile
2930
const ColumnRangePtr column_range;
3031
// Global local index cache
3132
const LocalIndexCachePtr local_index_cache;
33+
const ScanContextPtr scan_context;
3234

3335
bool loaded = false;
3436

3537
public:
3638
InvertedIndexReaderFromDMFile(
3739
const ColumnRangePtr & column_range_,
3840
const DMFilePtr & dmfile_,
39-
const LocalIndexCachePtr & local_index_cache_);
41+
const LocalIndexCachePtr & local_index_cache_,
42+
const ScanContextPtr & scan_context_ = nullptr);
4043

4144
~InvertedIndexReaderFromDMFile();
4245

dbms/src/Storages/DeltaMerge/Index/InvertedIndex/Reader/ReaderFromSegment.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,20 @@ namespace DB::DM
2323
BitmapFilterPtr InvertedIndexReaderFromSegment::loadStable(
2424
const SegmentSnapshotPtr & snapshot,
2525
const ColumnRangePtr & column_range,
26-
const LocalIndexCachePtr & local_index_cache)
26+
const LocalIndexCachePtr & local_index_cache,
27+
const ScanContextPtr & scan_context)
2728
{
28-
InvertedIndexReaderFromSegment reader(snapshot, column_range, local_index_cache);
29+
InvertedIndexReaderFromSegment reader(snapshot, column_range, local_index_cache, scan_context);
2930
return reader.loadStableImpl();
3031
}
3132

3233
BitmapFilterPtr InvertedIndexReaderFromSegment::loadDelta(
3334
const SegmentSnapshotPtr & snapshot,
3435
const ColumnRangePtr & column_range,
35-
const LocalIndexCachePtr & local_index_cache)
36+
const LocalIndexCachePtr & local_index_cache,
37+
const ScanContextPtr & scan_context)
3638
{
37-
InvertedIndexReaderFromSegment reader(snapshot, column_range, local_index_cache);
39+
InvertedIndexReaderFromSegment reader(snapshot, column_range, local_index_cache, scan_context);
3840
return reader.loadDeltaImpl();
3941
}
4042

@@ -43,7 +45,7 @@ BitmapFilterPtr InvertedIndexReaderFromSegment::loadStableImpl()
4345
BitmapFilterPtr bitmap_filter = std::make_shared<BitmapFilter>(0, false);
4446
for (const auto & dmfile : snapshot->stable->getDMFiles())
4547
{
46-
InvertedIndexReaderFromDMFile reader(column_range, dmfile, local_index_cache);
48+
InvertedIndexReaderFromDMFile reader(column_range, dmfile, local_index_cache, scan_context);
4749
if (auto sub_bf = reader.load(); sub_bf)
4850
bitmap_filter->append(*sub_bf);
4951
else
@@ -61,7 +63,8 @@ BitmapFilterPtr InvertedIndexReaderFromSegment::loadDeltaImpl()
6163
{
6264
if (auto * tiny_cf = cf->tryToTinyFile(); tiny_cf)
6365
{
64-
InvertedIndexReaderFromColumnFileTiny reader(column_range, *tiny_cf, data_provider, local_index_cache);
66+
InvertedIndexReaderFromColumnFileTiny
67+
reader(column_range, *tiny_cf, data_provider, local_index_cache, scan_context);
6568
if (auto sub_bf = reader.load(); sub_bf)
6669
bitmap_filter->append(*sub_bf);
6770
else

dbms/src/Storages/DeltaMerge/Index/InvertedIndex/Reader/ReaderFromSegment.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <Storages/DeltaMerge/BitmapFilter/BitmapFilter.h>
1818
#include <Storages/DeltaMerge/Filter/ColumnRange_fwd.h>
1919
#include <Storages/DeltaMerge/Index/LocalIndexCache_fwd.h>
20+
#include <Storages/DeltaMerge/ScanContext_fwd.h>
2021

2122
namespace DB::DM
2223
{
@@ -31,25 +32,30 @@ class InvertedIndexReaderFromSegment
3132
const ColumnRangePtr column_range;
3233
// Global local index cache
3334
const LocalIndexCachePtr local_index_cache;
35+
const ScanContextPtr scan_context;
3436

3537
public:
3638
InvertedIndexReaderFromSegment(
3739
const SegmentSnapshotPtr & snapshot_,
3840
const ColumnRangePtr & column_range_,
39-
const LocalIndexCachePtr & local_index_cache_)
41+
const LocalIndexCachePtr & local_index_cache_,
42+
const ScanContextPtr & scan_context_)
4043
: snapshot(snapshot_)
4144
, column_range(column_range_)
4245
, local_index_cache(local_index_cache_)
46+
, scan_context(scan_context_)
4347
{}
4448

4549
static BitmapFilterPtr loadStable(
4650
const SegmentSnapshotPtr & snapshot,
4751
const ColumnRangePtr & column_range,
48-
const LocalIndexCachePtr & local_index_cache);
52+
const LocalIndexCachePtr & local_index_cache,
53+
const ScanContextPtr & scan_context);
4954
static BitmapFilterPtr loadDelta(
5055
const SegmentSnapshotPtr & snapshot,
5156
const ColumnRangePtr & column_range,
52-
const LocalIndexCachePtr & local_index_cache);
57+
const LocalIndexCachePtr & local_index_cache,
58+
const ScanContextPtr & scan_context);
5359

5460
~InvertedIndexReaderFromSegment() = default;
5561

dbms/src/Storages/DeltaMerge/Index/VectorIndex/Stream/InputStream.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,13 @@ void VectorIndexInputStream::initSearchResults()
114114
if (ctx->dm_context != nullptr && ctx->dm_context->scan_context != nullptr)
115115
{
116116
auto scan_context = ctx->dm_context->scan_context;
117-
scan_context->total_vector_idx_load_from_s3 += ctx->perf->load_from_stable_s3;
118-
scan_context->total_vector_idx_load_from_disk
119-
+= ctx->perf->load_from_stable_disk + ctx->perf->load_from_column_file;
120-
scan_context->total_vector_idx_load_from_cache += ctx->perf->load_from_cache;
121-
scan_context->total_vector_idx_load_time_ms += ctx->perf->total_load_ms;
122-
scan_context->total_vector_idx_search_time_ms += ctx->perf->total_search_ms;
123-
scan_context->total_vector_idx_search_visited_nodes += ctx->perf->visited_nodes;
124-
scan_context->total_vector_idx_search_discarded_nodes += ctx->perf->discarded_nodes;
117+
scan_context->vector_idx_load_from_s3 += ctx->perf->load_from_stable_s3;
118+
scan_context->vector_idx_load_from_disk += ctx->perf->load_from_stable_disk + ctx->perf->load_from_column_file;
119+
scan_context->vector_idx_load_from_cache += ctx->perf->load_from_cache;
120+
scan_context->vector_idx_load_time_ms += ctx->perf->total_load_ms;
121+
scan_context->vector_idx_search_time_ms += ctx->perf->total_search_ms;
122+
scan_context->vector_idx_search_visited_nodes += ctx->perf->visited_nodes;
123+
scan_context->vector_idx_search_discarded_nodes += ctx->perf->discarded_nodes;
125124
}
126125

127126
searchResultsInited = true;
@@ -198,9 +197,8 @@ void VectorIndexInputStream::onReadFinished()
198197
if (ctx->dm_context != nullptr && ctx->dm_context->scan_context != nullptr)
199198
{
200199
auto scan_context = ctx->dm_context->scan_context;
201-
scan_context->total_vector_idx_read_vec_time_ms
202-
+= ctx->perf->total_cf_read_vec_ms + ctx->perf->total_dm_read_vec_ms;
203-
scan_context->total_vector_idx_read_others_time_ms
200+
scan_context->vector_idx_read_vec_time_ms += ctx->perf->total_cf_read_vec_ms + ctx->perf->total_dm_read_vec_ms;
201+
scan_context->vector_idx_read_others_time_ms
204202
+= ctx->perf->total_cf_read_others_ms + ctx->perf->total_dm_read_others_ms;
205203
}
206204
}

0 commit comments

Comments
 (0)