Coverage Report

Created: 2026-08-18 06:19

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/io/io_common.h
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#pragma once
19
20
#include <gen_cpp/Types_types.h>
21
22
#include <array>
23
#include <cstddef>
24
#include <cstdint>
25
#include <set>
26
#include <string>
27
28
namespace doris {
29
30
enum class ReaderType : uint8_t {
31
    READER_QUERY = 0,
32
    READER_ALTER_TABLE = 1,
33
    READER_BASE_COMPACTION = 2,
34
    READER_CUMULATIVE_COMPACTION = 3,
35
    READER_CHECKSUM = 4,
36
    READER_COLD_DATA_COMPACTION = 5,
37
    READER_SEGMENT_COMPACTION = 6,
38
    READER_FULL_COMPACTION = 7,
39
    UNKNOWN = 8
40
};
41
42
namespace io {
43
44
class RemoteScanCacheWriteLimiter;
45
46
enum class FileCacheMissPolicy : uint8_t {
47
    READ_THROUGH_AND_WRITE_BACK = 0,
48
    REMOTE_ONLY_ON_MISS = 1,
49
};
50
51
struct FileReaderStats {
52
    size_t read_calls = 0;
53
    size_t read_bytes = 0;
54
    int64_t read_time_ns = 0;
55
    size_t read_rows = 0;
56
};
57
58
struct FileCacheStatistics {
59
    int64_t num_local_io_total = 0;
60
    int64_t num_remote_io_total = 0;
61
    int64_t num_peer_io_total = 0;
62
    int64_t local_io_timer = 0;
63
    int64_t bytes_read_from_local = 0;
64
    int64_t bytes_read_from_remote = 0;
65
    int64_t bytes_read_from_peer = 0;
66
    int64_t remote_io_timer = 0;
67
    int64_t peer_io_timer = 0;
68
    int64_t remote_wait_timer = 0;
69
    int64_t write_cache_io_timer = 0;
70
    int64_t bytes_write_into_cache = 0;
71
    int64_t num_skip_cache_io_total = 0;
72
    int64_t read_cache_file_directly_timer = 0;
73
    int64_t cache_get_or_set_timer = 0;
74
    int64_t lock_wait_timer = 0;
75
    int64_t get_timer = 0;
76
    int64_t set_timer = 0;
77
78
    int64_t inverted_index_num_local_io_total = 0;
79
    int64_t inverted_index_num_remote_io_total = 0;
80
    int64_t inverted_index_num_peer_io_total = 0;
81
    int64_t inverted_index_bytes_read_from_local = 0;
82
    int64_t inverted_index_bytes_read_from_remote = 0;
83
    int64_t inverted_index_bytes_read_from_peer = 0;
84
    int64_t inverted_index_remote_physical_read_bytes = 0;
85
    int64_t inverted_index_bytes_write_into_cache = 0;
86
    int64_t inverted_index_local_io_timer = 0;
87
    int64_t inverted_index_remote_io_timer = 0;
88
    int64_t inverted_index_peer_io_timer = 0;
89
    int64_t inverted_index_io_timer = 0;
90
    int64_t inverted_index_write_cache_io_timer = 0;
91
    int64_t inverted_index_request_bytes = 0;
92
    int64_t inverted_index_read_bytes = 0;
93
    int64_t inverted_index_range_read_count = 0;
94
    int64_t inverted_index_serial_read_rounds = 0;
95
96
    int64_t segment_footer_index_num_local_io_total = 0;
97
    int64_t segment_footer_index_num_remote_io_total = 0;
98
    int64_t segment_footer_index_num_peer_io_total = 0;
99
    int64_t segment_footer_index_bytes_read_from_local = 0;
100
    int64_t segment_footer_index_bytes_read_from_remote = 0;
101
    int64_t segment_footer_index_bytes_read_from_peer = 0;
102
    int64_t segment_footer_index_local_io_timer = 0;
103
    int64_t segment_footer_index_remote_io_timer = 0;
104
    int64_t segment_footer_index_peer_io_timer = 0;
105
    int64_t segment_footer_index_write_cache_io_timer = 0;
106
    int64_t segment_footer_index_bytes_write_into_cache = 0;
107
    int64_t remote_only_on_miss_triggered = 0;
108
    int64_t remote_only_on_miss_threshold_bytes = 0;
109
110
    // Cross-CG / Same-CG peer read statistics
111
    int64_t num_cross_cg_peer_io_total = 0;
112
    int64_t bytes_read_from_cross_cg_peer = 0;
113
    int64_t cross_cg_peer_io_timer = 0; // nanoseconds
114
    int64_t num_same_cg_peer_io_total = 0;
115
    int64_t bytes_read_from_same_cg_peer = 0;
116
    int64_t same_cg_peer_io_timer = 0; // nanoseconds
117
    int64_t num_peer_race_peer_win = 0;
118
    int64_t num_peer_race_s3_win = 0;
119
    int64_t num_peer_lazy_fetch = 0;
120
    int64_t peer_lazy_fetch_timer = 0; // nanoseconds
121
122
    std::set<std::string> peer_hosts;
123
124
2.71M
    void merge_from(const FileCacheStatistics& other) {
125
2.71M
        num_local_io_total += other.num_local_io_total;
126
2.71M
        num_remote_io_total += other.num_remote_io_total;
127
2.71M
        num_peer_io_total += other.num_peer_io_total;
128
2.71M
        local_io_timer += other.local_io_timer;
129
2.71M
        bytes_read_from_local += other.bytes_read_from_local;
130
2.71M
        bytes_read_from_remote += other.bytes_read_from_remote;
131
2.71M
        bytes_read_from_peer += other.bytes_read_from_peer;
132
2.71M
        remote_io_timer += other.remote_io_timer;
133
2.71M
        peer_io_timer += other.peer_io_timer;
134
2.71M
        remote_wait_timer += other.remote_wait_timer;
135
2.71M
        write_cache_io_timer += other.write_cache_io_timer;
136
2.71M
        bytes_write_into_cache += other.bytes_write_into_cache;
137
2.71M
        num_skip_cache_io_total += other.num_skip_cache_io_total;
138
2.71M
        read_cache_file_directly_timer += other.read_cache_file_directly_timer;
139
2.71M
        cache_get_or_set_timer += other.cache_get_or_set_timer;
140
2.71M
        lock_wait_timer += other.lock_wait_timer;
141
2.71M
        get_timer += other.get_timer;
142
2.71M
        set_timer += other.set_timer;
143
144
2.71M
        inverted_index_num_local_io_total += other.inverted_index_num_local_io_total;
145
2.71M
        inverted_index_num_remote_io_total += other.inverted_index_num_remote_io_total;
146
2.71M
        inverted_index_num_peer_io_total += other.inverted_index_num_peer_io_total;
147
2.71M
        inverted_index_bytes_read_from_local += other.inverted_index_bytes_read_from_local;
148
2.71M
        inverted_index_bytes_read_from_remote += other.inverted_index_bytes_read_from_remote;
149
2.71M
        inverted_index_bytes_read_from_peer += other.inverted_index_bytes_read_from_peer;
150
2.71M
        inverted_index_remote_physical_read_bytes +=
151
2.71M
                other.inverted_index_remote_physical_read_bytes;
152
2.71M
        inverted_index_local_io_timer += other.inverted_index_local_io_timer;
153
2.71M
        inverted_index_remote_io_timer += other.inverted_index_remote_io_timer;
154
2.71M
        inverted_index_peer_io_timer += other.inverted_index_peer_io_timer;
155
2.71M
        inverted_index_io_timer += other.inverted_index_io_timer;
156
2.71M
        inverted_index_write_cache_io_timer += other.inverted_index_write_cache_io_timer;
157
2.71M
        inverted_index_bytes_write_into_cache += other.inverted_index_bytes_write_into_cache;
158
2.71M
        inverted_index_request_bytes += other.inverted_index_request_bytes;
159
2.71M
        inverted_index_read_bytes += other.inverted_index_read_bytes;
160
2.71M
        inverted_index_range_read_count += other.inverted_index_range_read_count;
161
2.71M
        inverted_index_serial_read_rounds += other.inverted_index_serial_read_rounds;
162
163
2.71M
        segment_footer_index_num_local_io_total += other.segment_footer_index_num_local_io_total;
164
2.71M
        segment_footer_index_num_remote_io_total += other.segment_footer_index_num_remote_io_total;
165
2.71M
        segment_footer_index_num_peer_io_total += other.segment_footer_index_num_peer_io_total;
166
2.71M
        segment_footer_index_bytes_read_from_local +=
167
2.71M
                other.segment_footer_index_bytes_read_from_local;
168
2.71M
        segment_footer_index_bytes_read_from_remote +=
169
2.71M
                other.segment_footer_index_bytes_read_from_remote;
170
2.71M
        segment_footer_index_bytes_read_from_peer +=
171
2.71M
                other.segment_footer_index_bytes_read_from_peer;
172
2.71M
        segment_footer_index_local_io_timer += other.segment_footer_index_local_io_timer;
173
2.71M
        segment_footer_index_remote_io_timer += other.segment_footer_index_remote_io_timer;
174
2.71M
        segment_footer_index_peer_io_timer += other.segment_footer_index_peer_io_timer;
175
2.71M
        segment_footer_index_write_cache_io_timer +=
176
2.71M
                other.segment_footer_index_write_cache_io_timer;
177
2.71M
        segment_footer_index_bytes_write_into_cache +=
178
2.71M
                other.segment_footer_index_bytes_write_into_cache;
179
2.71M
        remote_only_on_miss_triggered =
180
2.72M
                remote_only_on_miss_triggered || other.remote_only_on_miss_triggered;
181
2.71M
        if (other.remote_only_on_miss_threshold_bytes > remote_only_on_miss_threshold_bytes) {
182
0
            remote_only_on_miss_threshold_bytes = other.remote_only_on_miss_threshold_bytes;
183
0
        }
184
185
2.71M
        num_cross_cg_peer_io_total += other.num_cross_cg_peer_io_total;
186
2.71M
        bytes_read_from_cross_cg_peer += other.bytes_read_from_cross_cg_peer;
187
2.71M
        cross_cg_peer_io_timer += other.cross_cg_peer_io_timer;
188
2.71M
        num_same_cg_peer_io_total += other.num_same_cg_peer_io_total;
189
2.71M
        bytes_read_from_same_cg_peer += other.bytes_read_from_same_cg_peer;
190
2.71M
        same_cg_peer_io_timer += other.same_cg_peer_io_timer;
191
2.71M
        num_peer_race_peer_win += other.num_peer_race_peer_win;
192
2.71M
        num_peer_race_s3_win += other.num_peer_race_s3_win;
193
2.71M
        num_peer_lazy_fetch += other.num_peer_lazy_fetch;
194
2.71M
        peer_lazy_fetch_timer += other.peer_lazy_fetch_timer;
195
196
2.71M
        peer_hosts.insert(other.peer_hosts.begin(), other.peer_hosts.end());
197
2.71M
    }
198
};
199
200
struct IOContext {
201
    ReaderType reader_type = ReaderType::UNKNOWN;
202
    // FIXME(plat1ko): Seems `is_disposable` can be inferred from the `reader_type`?
203
    bool is_disposable = false;
204
    bool is_index_data = false;
205
    bool read_file_cache = true;
206
    // TODO(lightman): use following member variables to control file cache
207
    bool is_persistent = false;
208
    // stop reader when reading, used in some interrupted operations
209
    bool should_stop = false;
210
    int64_t expiration_time = 0;
211
    const TUniqueId* query_id = nullptr;             // Ref
212
    FileCacheStatistics* file_cache_stats = nullptr; // Ref
213
    FileReaderStats* file_reader_stats = nullptr;    // Ref
214
    bool is_inverted_index = false;
215
    // if is_dryrun, read IO will download data to cache but return no data to reader
216
    // useful to skip cache data read from local disk to accelarate warm up
217
    bool is_dryrun = false;
218
    // if `is_warmup` == true, this I/O request is from a warm up task
219
    bool is_warmup {false};
220
    int64_t condition_cache_filtered_rows = 0;
221
    // Rows removed by file-local predicate conjuncts inside FileReader/TableReader. Scanner-level
222
    // output filtering already records its own unselected rows; this counter carries the rows that
223
    // were filtered before the block returned to Scanner.
224
    int64_t predicate_filtered_rows = 0;
225
    // if true, bypass peer read / peer-vs-S3 race and read directly from remote storage
226
    bool bypass_peer_read {false};
227
    FileCacheMissPolicy file_cache_miss_policy = FileCacheMissPolicy::READ_THROUGH_AND_WRITE_BACK;
228
    // From session variable inverted_index_snii_read_no_write_file_cache: SNII index
229
    // reads of this query take REMOTE_ONLY_ON_MISS (hit served, miss reads remote
230
    // and skips the cache write-back). Carried down to the SNII adapter, which is
231
    // the sole place that turns it into a file_cache_miss_policy -- keeping CLucene
232
    // index reads and data reads on the normal write-back path.
233
    bool inverted_index_snii_read_no_write_file_cache = false;
234
    RemoteScanCacheWriteLimiter* remote_scan_cache_write_limiter = nullptr; // Ref
235
};
236
237
} // namespace io
238
} // namespace doris