Coverage Report

Created: 2025-03-12 11:55

/root/doris/be/src/runtime/runtime_state.h
Line
Count
Source (jump to first uncovered line)
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
// This file is copied from
18
// https://github.com/apache/impala/blob/branch-2.9.0/be/src/runtime/runtime-state.h
19
// and modified by Doris
20
21
#pragma once
22
23
#include <gen_cpp/PaloInternalService_types.h>
24
#include <gen_cpp/Types_types.h>
25
#include <gen_cpp/segment_v2.pb.h>
26
#include <stdint.h>
27
28
#include <atomic>
29
#include <cstdint>
30
#include <fstream>
31
#include <functional>
32
#include <memory>
33
#include <mutex>
34
#include <shared_mutex>
35
#include <string>
36
#include <utility>
37
#include <vector>
38
39
#include "agent/be_exec_version_manager.h"
40
#include "cctz/time_zone.h"
41
#include "common/be_mock_util.h"
42
#include "common/compiler_util.h" // IWYU pragma: keep
43
#include "common/config.h"
44
#include "common/factory_creator.h"
45
#include "common/status.h"
46
#include "gutil/integral_types.h"
47
#include "io/fs/file_system.h"
48
#include "io/fs/s3_file_system.h"
49
#include "runtime/task_execution_context.h"
50
#include "runtime/workload_group/workload_group.h"
51
#include "util/debug_util.h"
52
#include "util/runtime_profile.h"
53
#include "vec/columns/columns_number.h"
54
55
namespace doris {
56
class IRuntimeFilter;
57
58
23
inline int32_t get_execution_rpc_timeout_ms(int32_t execution_timeout_sec) {
59
23
    return std::min(config::execution_max_rpc_timeout_sec, execution_timeout_sec) * 1000;
60
23
}
61
62
namespace pipeline {
63
class PipelineXLocalStateBase;
64
class PipelineXSinkLocalStateBase;
65
class PipelineFragmentContext;
66
class PipelineTask;
67
class Dependency;
68
} // namespace pipeline
69
70
class DescriptorTbl;
71
class ObjectPool;
72
class ExecEnv;
73
class RuntimeFilterMgr;
74
class MemTrackerLimiter;
75
class QueryContext;
76
77
// A collection of items that are part of the global state of a
78
// query and shared across all execution nodes of that query.
79
class RuntimeState {
80
    ENABLE_FACTORY_CREATOR(RuntimeState);
81
82
public:
83
    RuntimeState(const TPlanFragmentExecParams& fragment_exec_params,
84
                 const TQueryOptions& query_options, const TQueryGlobals& query_globals,
85
                 ExecEnv* exec_env, QueryContext* ctx,
86
                 const std::shared_ptr<MemTrackerLimiter>& query_mem_tracker = nullptr);
87
88
    RuntimeState(const TUniqueId& instance_id, const TUniqueId& query_id, int32 fragment_id,
89
                 const TQueryOptions& query_options, const TQueryGlobals& query_globals,
90
                 ExecEnv* exec_env, QueryContext* ctx);
91
92
    // Used by pipeline. This runtime state is only used for setup.
93
    RuntimeState(const TUniqueId& query_id, int32 fragment_id, const TQueryOptions& query_options,
94
                 const TQueryGlobals& query_globals, ExecEnv* exec_env, QueryContext* ctx);
95
96
    // RuntimeState for executing expr in fe-support.
97
    RuntimeState(const TQueryGlobals& query_globals);
98
99
    // for job task only
100
    RuntimeState();
101
102
    // Empty d'tor to avoid issues with unique_ptr.
103
    MOCK_DEFINE(virtual) ~RuntimeState();
104
105
    // Set per-query state.
106
    Status init(const TUniqueId& fragment_instance_id, const TQueryOptions& query_options,
107
                const TQueryGlobals& query_globals, ExecEnv* exec_env);
108
109
    // for ut and non-query.
110
0
    void set_exec_env(ExecEnv* exec_env) { _exec_env = exec_env; }
111
112
    // for ut and non-query.
113
    void init_mem_trackers(const std::string& name = "ut", const TUniqueId& id = TUniqueId());
114
115
239
    const TQueryOptions& query_options() const { return _query_options; }
116
8
    int64_t scan_queue_mem_limit() const {
117
8
        return _query_options.__isset.scan_queue_mem_limit ? _query_options.scan_queue_mem_limit
118
8
                                                           : _query_options.mem_limit / 20;
119
8
    }
120
121
6
    int32_t max_column_reader_num() const {
122
6
        return _query_options.__isset.max_column_reader_num ? _query_options.max_column_reader_num
123
6
                                                            : 20000;
124
6
    }
125
126
31
    ObjectPool* obj_pool() const { return _obj_pool.get(); }
127
128
257
    const DescriptorTbl& desc_tbl() const { return *_desc_tbl; }
129
84
    void set_desc_tbl(const DescriptorTbl* desc_tbl) { _desc_tbl = desc_tbl; }
130
73
    MOCK_FUNCTION int batch_size() const { return _query_options.batch_size; }
131
0
    int wait_full_block_schedule_times() const {
132
0
        return _query_options.wait_full_block_schedule_times;
133
0
    }
134
0
    bool abort_on_error() const { return _query_options.abort_on_error; }
135
0
    bool abort_on_default_limit_exceeded() const {
136
0
        return _query_options.abort_on_default_limit_exceeded;
137
0
    }
138
8
    int query_parallel_instance_num() const { return _query_options.parallel_instance; }
139
0
    int max_errors() const { return _query_options.max_errors; }
140
28
    int execution_timeout() const {
141
28
        return _query_options.__isset.execution_timeout ? _query_options.execution_timeout
142
28
                                                        : _query_options.query_timeout;
143
28
    }
144
0
    int max_io_buffers() const { return _query_options.max_io_buffers; }
145
6
    int num_scanner_threads() const {
146
6
        return _query_options.__isset.num_scanner_threads ? _query_options.num_scanner_threads : 0;
147
6
    }
148
14
    int min_scan_concurrency_of_scan_scheduler() const {
149
14
        return _query_options.__isset.min_scan_scheduler_concurrency
150
14
                       ? _query_options.min_scan_scheduler_concurrency
151
14
                       : 0;
152
14
    }
153
154
14
    int min_scan_concurrency_of_scanner() const {
155
14
        return _query_options.__isset.min_scanner_concurrency
156
14
                       ? _query_options.min_scanner_concurrency
157
14
                       : 1;
158
14
    }
159
160
0
    TQueryType::type query_type() const { return _query_options.query_type; }
161
0
    int64_t timestamp_ms() const { return _timestamp_ms; }
162
0
    int32_t nano_seconds() const { return _nano_seconds; }
163
    // if possible, use timezone_obj() rather than timezone()
164
13
    const std::string& timezone() const { return _timezone; }
165
27
    const cctz::time_zone& timezone_obj() const { return _timezone_obj; }
166
0
    const std::string& user() const { return _user; }
167
216
    const TUniqueId& query_id() const { return _query_id; }
168
43
    const TUniqueId& fragment_instance_id() const { return _fragment_instance_id; }
169
    // should only be called in pipeline engine
170
13
    int32_t fragment_id() const { return _fragment_id; }
171
38
    ExecEnv* exec_env() { return _exec_env; }
172
    std::shared_ptr<MemTrackerLimiter> query_mem_tracker() const;
173
174
    // Returns runtime state profile
175
4
    RuntimeProfile* runtime_profile() { return &_profile; }
176
0
    RuntimeProfile* load_channel_profile() { return &_load_channel_profile; }
177
178
0
    bool enable_function_pushdown() const {
179
0
        return _query_options.__isset.enable_function_pushdown &&
180
0
               _query_options.enable_function_pushdown;
181
0
    }
182
183
53
    bool check_overflow_for_decimal() const {
184
53
        return _query_options.__isset.check_overflow_for_decimal &&
185
53
               _query_options.check_overflow_for_decimal;
186
53
    }
187
188
64
    bool enable_decimal256() const {
189
64
        return _query_options.__isset.enable_decimal256 && _query_options.enable_decimal256;
190
64
    }
191
192
0
    bool enable_common_expr_pushdown() const {
193
0
        return _query_options.__isset.enable_common_expr_pushdown &&
194
0
               _query_options.enable_common_expr_pushdown;
195
0
    }
196
197
0
    bool enable_common_expr_pushdown_for_inverted_index() const {
198
0
        return enable_common_expr_pushdown() &&
199
0
               _query_options.__isset.enable_common_expr_pushdown_for_inverted_index &&
200
0
               _query_options.enable_common_expr_pushdown_for_inverted_index;
201
0
    };
202
203
0
    bool mysql_row_binary_format() const {
204
0
        return _query_options.__isset.mysql_row_binary_format &&
205
0
               _query_options.mysql_row_binary_format;
206
0
    }
207
208
0
    bool enable_short_circuit_query_access_column_store() const {
209
0
        return _query_options.__isset.enable_short_circuit_query_access_column_store &&
210
0
               _query_options.enable_short_circuit_query_access_column_store;
211
0
    }
212
213
    // Appends error to the _error_log if there is space
214
    bool log_error(const std::string& error);
215
216
    // Returns true if the error log has not reached _max_errors.
217
0
    bool log_has_space() {
218
0
        std::lock_guard<std::mutex> l(_error_log_lock);
219
0
        return _error_log.size() < _query_options.max_errors;
220
0
    }
221
222
    // Append all _error_log[_unreported_error_idx+] to new_errors and set
223
    // _unreported_error_idx to _errors_log.size()
224
    void get_unreported_errors(std::vector<std::string>* new_errors);
225
226
    [[nodiscard]] MOCK_FUNCTION bool is_cancelled() const;
227
    MOCK_FUNCTION Status cancel_reason() const;
228
0
    void cancel(const Status& reason) {
229
0
        if (_exec_status.update(reason)) {
230
            // Create a error status, so that we could print error stack, and
231
            // we could know which path call cancel.
232
0
            LOG(WARNING) << "Task is cancelled, instance: "
233
0
                         << PrintInstanceStandardInfo(_query_id, _fragment_instance_id)
234
0
                         << ", st = " << reason;
235
0
        } else {
236
0
            LOG(WARNING) << "Task is already cancelled, instance: "
237
0
                         << PrintInstanceStandardInfo(_query_id, _fragment_instance_id)
238
0
                         << ", original cancel msg: " << _exec_status.status()
239
0
                         << ", new cancel msg: " << reason;
240
0
        }
241
0
    }
242
243
0
    void set_backend_id(int64_t backend_id) { _backend_id = backend_id; }
244
0
    int64_t backend_id() const { return _backend_id; }
245
246
17
    void set_be_number(int be_number) { _be_number = be_number; }
247
64
    int be_number(void) const { return _be_number; }
248
249
0
    std::vector<std::string>& output_files() { return _output_files; }
250
251
0
    void set_import_label(const std::string& import_label) { _import_label = import_label; }
252
253
0
    const std::vector<std::string>& export_output_files() const { return _export_output_files; }
254
255
0
    void add_export_output_file(const std::string& file) { _export_output_files.push_back(file); }
256
257
0
    void set_db_name(const std::string& db_name) { _db_name = db_name; }
258
259
0
    const std::string& db_name() { return _db_name; }
260
261
0
    void set_wal_id(int64_t wal_id) { _wal_id = wal_id; }
262
263
3
    int64_t wal_id() const { return _wal_id; }
264
265
0
    void set_content_length(size_t content_length) { _content_length = content_length; }
266
267
0
    size_t content_length() const { return _content_length; }
268
269
0
    const std::string& import_label() { return _import_label; }
270
271
0
    const std::string& load_dir() const { return _load_dir; }
272
273
0
    void set_load_job_id(int64_t job_id) { _load_job_id = job_id; }
274
275
0
    int64_t load_job_id() const { return _load_job_id; }
276
277
    std::string get_error_log_file_path();
278
279
    // append error msg and error line to file when loading data.
280
    // is_summary is true, means we are going to write the summary line
281
    // If we need to stop the processing, set stop_processing to true
282
    Status append_error_msg_to_file(std::function<std::string()> line,
283
                                    std::function<std::string()> error_msg, bool* stop_processing,
284
                                    bool is_summary = false);
285
286
0
    int64_t num_bytes_load_total() { return _num_bytes_load_total.load(); }
287
288
0
    int64_t num_finished_range() { return _num_finished_scan_range.load(); }
289
290
0
    int64_t num_rows_load_total() { return _num_rows_load_total.load(); }
291
292
0
    int64_t num_rows_load_filtered() { return _num_rows_load_filtered.load(); }
293
294
0
    int64_t num_rows_load_unselected() { return _num_rows_load_unselected.load(); }
295
296
0
    int64_t num_rows_filtered_in_strict_mode_partial_update() {
297
0
        return _num_rows_filtered_in_strict_mode_partial_update;
298
0
    }
299
300
0
    int64_t num_rows_load_success() {
301
0
        return num_rows_load_total() - num_rows_load_filtered() - num_rows_load_unselected();
302
0
    }
303
304
0
    void update_num_rows_load_total(int64_t num_rows) { _num_rows_load_total.fetch_add(num_rows); }
305
306
0
    void set_num_rows_load_total(int64_t num_rows) { _num_rows_load_total.store(num_rows); }
307
308
0
    void update_num_bytes_load_total(int64_t bytes_load) {
309
0
        _num_bytes_load_total.fetch_add(bytes_load);
310
0
    }
311
312
2
    void update_num_finished_scan_range(int64_t finished_range) {
313
2
        _num_finished_scan_range.fetch_add(finished_range);
314
2
    }
315
316
0
    void update_num_rows_load_filtered(int64_t num_rows) {
317
0
        _num_rows_load_filtered.fetch_add(num_rows);
318
0
    }
319
320
0
    void update_num_rows_load_unselected(int64_t num_rows) {
321
0
        _num_rows_load_unselected.fetch_add(num_rows);
322
0
    }
323
324
0
    void set_num_rows_filtered_in_strict_mode_partial_update(int64_t num_rows) {
325
0
        _num_rows_filtered_in_strict_mode_partial_update = num_rows;
326
0
    }
327
328
5
    void set_per_fragment_instance_idx(int idx) { _per_fragment_instance_idx = idx; }
329
330
0
    int per_fragment_instance_idx() const { return _per_fragment_instance_idx; }
331
332
5
    void set_num_per_fragment_instances(int num_instances) {
333
5
        _num_per_fragment_instances = num_instances;
334
5
    }
335
336
0
    int num_per_fragment_instances() const { return _num_per_fragment_instances; }
337
338
5
    void set_load_stream_per_node(int load_stream_per_node) {
339
5
        _load_stream_per_node = load_stream_per_node;
340
5
    }
341
342
0
    int load_stream_per_node() const { return _load_stream_per_node; }
343
344
5
    void set_total_load_streams(int total_load_streams) {
345
5
        _total_load_streams = total_load_streams;
346
5
    }
347
348
0
    int total_load_streams() const { return _total_load_streams; }
349
350
5
    void set_num_local_sink(int num_local_sink) { _num_local_sink = num_local_sink; }
351
352
0
    int num_local_sink() const { return _num_local_sink; }
353
354
0
    bool disable_stream_preaggregations() const {
355
0
        return _query_options.disable_stream_preaggregations;
356
0
    }
357
358
0
    int32_t runtime_filter_wait_time_ms() const {
359
0
        return _query_options.runtime_filter_wait_time_ms;
360
0
    }
361
362
0
    bool runtime_filter_wait_infinitely() const {
363
0
        return _query_options.__isset.runtime_filter_wait_infinitely &&
364
0
               _query_options.runtime_filter_wait_infinitely;
365
0
    }
366
367
2
    int32_t runtime_filter_max_in_num() const { return _query_options.runtime_filter_max_in_num; }
368
369
63
    int be_exec_version() const {
370
63
        DCHECK(_query_options.__isset.be_exec_version &&
371
63
               BeExecVersionManager::check_be_exec_version(_query_options.be_exec_version));
372
63
        return _query_options.be_exec_version;
373
63
    }
374
2
    bool enable_local_shuffle() const {
375
2
        return _query_options.__isset.enable_local_shuffle && _query_options.enable_local_shuffle;
376
2
    }
377
378
3
    MOCK_FUNCTION bool enable_local_exchange() const {
379
3
        return _query_options.__isset.enable_local_exchange && _query_options.enable_local_exchange;
380
3
    }
381
382
0
    bool trim_tailing_spaces_for_external_table_query() const {
383
0
        return _query_options.trim_tailing_spaces_for_external_table_query;
384
0
    }
385
386
0
    bool return_object_data_as_binary() const {
387
0
        return _query_options.return_object_data_as_binary;
388
0
    }
389
390
19
    segment_v2::CompressionTypePB fragement_transmission_compression_type() const {
391
19
        if (_query_options.__isset.fragment_transmission_compression_codec) {
392
0
            if (_query_options.fragment_transmission_compression_codec == "lz4") {
393
0
                return segment_v2::CompressionTypePB::LZ4;
394
0
            } else if (_query_options.fragment_transmission_compression_codec == "snappy") {
395
0
                return segment_v2::CompressionTypePB::SNAPPY;
396
0
            } else {
397
0
                return segment_v2::CompressionTypePB::NO_COMPRESSION;
398
0
            }
399
0
        }
400
19
        return segment_v2::CompressionTypePB::NO_COMPRESSION;
401
19
    }
402
403
0
    bool skip_storage_engine_merge() const {
404
0
        return _query_options.__isset.skip_storage_engine_merge &&
405
0
               _query_options.skip_storage_engine_merge;
406
0
    }
407
408
0
    bool skip_delete_predicate() const {
409
0
        return _query_options.__isset.skip_delete_predicate && _query_options.skip_delete_predicate;
410
0
    }
411
412
0
    bool skip_delete_bitmap() const {
413
0
        return _query_options.__isset.skip_delete_bitmap && _query_options.skip_delete_bitmap;
414
0
    }
415
416
0
    bool skip_missing_version() const {
417
0
        return _query_options.__isset.skip_missing_version && _query_options.skip_missing_version;
418
0
    }
419
420
0
    int64_t data_queue_max_blocks() const {
421
0
        return _query_options.__isset.data_queue_max_blocks ? _query_options.data_queue_max_blocks
422
0
                                                            : 1;
423
0
    }
424
425
    bool enable_page_cache() const;
426
427
0
    std::vector<TTabletCommitInfo> tablet_commit_infos() const {
428
0
        std::lock_guard<std::mutex> lock(_tablet_infos_mutex);
429
0
        return _tablet_commit_infos;
430
0
    }
431
432
0
    void add_tablet_commit_infos(std::vector<TTabletCommitInfo>& commit_infos) {
433
0
        std::lock_guard<std::mutex> lock(_tablet_infos_mutex);
434
0
        _tablet_commit_infos.insert(_tablet_commit_infos.end(),
435
0
                                    std::make_move_iterator(commit_infos.begin()),
436
0
                                    std::make_move_iterator(commit_infos.end()));
437
0
    }
438
439
0
    std::vector<TErrorTabletInfo> error_tablet_infos() const {
440
0
        std::lock_guard<std::mutex> lock(_tablet_infos_mutex);
441
0
        return _error_tablet_infos;
442
0
    }
443
444
0
    void add_error_tablet_infos(std::vector<TErrorTabletInfo>& tablet_infos) {
445
0
        std::lock_guard<std::mutex> lock(_tablet_infos_mutex);
446
0
        _error_tablet_infos.insert(_error_tablet_infos.end(),
447
0
                                   std::make_move_iterator(tablet_infos.begin()),
448
0
                                   std::make_move_iterator(tablet_infos.end()));
449
0
    }
450
451
0
    std::vector<THivePartitionUpdate>& hive_partition_updates() { return _hive_partition_updates; }
452
453
0
    std::vector<TIcebergCommitData>& iceberg_commit_datas() { return _iceberg_commit_datas; }
454
455
    // local runtime filter mgr, the runtime filter do not have remote target or
456
    // not need local merge should regist here. the instance exec finish, the local
457
    // runtime filter mgr can release the memory of local runtime filter
458
17
    RuntimeFilterMgr* local_runtime_filter_mgr() { return _runtime_filter_mgr; }
459
460
    RuntimeFilterMgr* global_runtime_filter_mgr();
461
462
17
    void set_runtime_filter_mgr(RuntimeFilterMgr* runtime_filter_mgr) {
463
17
        _runtime_filter_mgr = runtime_filter_mgr;
464
17
    }
465
466
235
    QueryContext* get_query_ctx() { return _query_ctx; }
467
468
    [[nodiscard]] bool low_memory_mode() const;
469
470
    std::weak_ptr<QueryContext> get_query_ctx_weak();
471
    WorkloadGroupPtr workload_group();
472
473
0
    void set_query_mem_tracker(const std::shared_ptr<MemTrackerLimiter>& tracker) {
474
0
        _query_mem_tracker = tracker;
475
0
    }
476
477
13
    void set_query_options(const TQueryOptions& query_options) { _query_options = query_options; }
478
479
0
    bool enable_profile() const {
480
0
        return _query_options.__isset.enable_profile && _query_options.enable_profile;
481
0
    }
482
483
0
    int rpc_verbose_profile_max_instance_count() const {
484
0
        return _query_options.__isset.rpc_verbose_profile_max_instance_count
485
0
                       ? _query_options.rpc_verbose_profile_max_instance_count
486
0
                       : 0;
487
0
    }
488
489
3
    bool enable_share_hash_table_for_broadcast_join() const {
490
3
        return _query_options.__isset.enable_share_hash_table_for_broadcast_join &&
491
3
               _query_options.enable_share_hash_table_for_broadcast_join;
492
3
    }
493
494
0
    bool enable_hash_join_early_start_probe() const {
495
0
        return _query_options.__isset.enable_hash_join_early_start_probe &&
496
0
               _query_options.enable_hash_join_early_start_probe;
497
0
    }
498
499
0
    bool enable_parallel_scan() const {
500
0
        return _query_options.__isset.enable_parallel_scan && _query_options.enable_parallel_scan;
501
0
    }
502
503
0
    bool is_read_csv_empty_line_as_null() const {
504
0
        return _query_options.__isset.read_csv_empty_line_as_null &&
505
0
               _query_options.read_csv_empty_line_as_null;
506
0
    }
507
508
4
    int parallel_scan_max_scanners_count() const {
509
4
        return _query_options.__isset.parallel_scan_max_scanners_count
510
4
                       ? _query_options.parallel_scan_max_scanners_count
511
4
                       : 0;
512
4
    }
513
514
0
    int partition_topn_max_partitions() const {
515
0
        return _query_options.__isset.partition_topn_max_partitions
516
0
                       ? _query_options.partition_topn_max_partitions
517
0
                       : 1024;
518
0
    }
519
520
0
    int partition_topn_per_partition_rows() const {
521
0
        return _query_options.__isset.partition_topn_pre_partition_rows
522
0
                       ? _query_options.partition_topn_pre_partition_rows
523
0
                       : 1000;
524
0
    }
525
526
0
    int64_t parallel_scan_min_rows_per_scanner() const {
527
0
        return _query_options.__isset.parallel_scan_min_rows_per_scanner
528
0
                       ? _query_options.parallel_scan_min_rows_per_scanner
529
0
                       : 0;
530
0
    }
531
532
0
    void set_be_exec_version(int32_t version) noexcept { _query_options.be_exec_version = version; }
533
534
0
    inline bool enable_delete_sub_pred_v2() const {
535
0
        return _query_options.__isset.enable_delete_sub_predicate_v2 &&
536
0
               _query_options.enable_delete_sub_predicate_v2;
537
0
    }
538
539
    using LocalState = doris::pipeline::PipelineXLocalStateBase;
540
    using SinkLocalState = doris::pipeline::PipelineXSinkLocalStateBase;
541
    // get result can return an error message, and we will only call it during the prepare.
542
    void emplace_local_state(int id, std::unique_ptr<LocalState> state);
543
544
    LocalState* get_local_state(int id);
545
    Result<LocalState*> get_local_state_result(int id);
546
547
    void emplace_sink_local_state(int id, std::unique_ptr<SinkLocalState> state);
548
549
    SinkLocalState* get_sink_local_state();
550
551
    Result<SinkLocalState*> get_sink_local_state_result();
552
553
    void resize_op_id_to_local_state(int operator_size);
554
555
    std::vector<std::shared_ptr<RuntimeProfile>> pipeline_id_to_profile();
556
557
    std::vector<std::shared_ptr<RuntimeProfile>> build_pipeline_profile(std::size_t pipeline_size);
558
559
158
    void set_task_execution_context(std::shared_ptr<TaskExecutionContext> context) {
560
158
        _task_execution_context_inited = true;
561
158
        _task_execution_context = context;
562
158
    }
563
564
74
    std::weak_ptr<TaskExecutionContext> get_task_execution_context() {
565
74
        CHECK(_task_execution_context_inited)
566
0
                << "_task_execution_context_inited == false, the ctx is not inited";
567
74
        return _task_execution_context;
568
74
    }
569
570
    Status register_producer_runtime_filter(const doris::TRuntimeFilterDesc& desc,
571
                                            std::shared_ptr<IRuntimeFilter>* producer_filter);
572
573
    Status register_consumer_runtime_filter(const doris::TRuntimeFilterDesc& desc,
574
                                            bool need_local_merge, int node_id,
575
                                            std::shared_ptr<IRuntimeFilter>* producer_filter);
576
    bool is_nereids() const;
577
578
84
    bool enable_spill() const {
579
84
        return (_query_options.__isset.enable_force_spill && _query_options.enable_force_spill) ||
580
84
               (_query_options.__isset.enable_spill && _query_options.enable_spill);
581
84
    }
582
583
0
    bool enable_force_spill() const {
584
0
        return _query_options.__isset.enable_force_spill && _query_options.enable_force_spill;
585
0
    }
586
587
0
    int64_t spill_min_revocable_mem() const {
588
0
        if (_query_options.__isset.min_revocable_mem) {
589
0
            return std::max(_query_options.min_revocable_mem, (int64_t)1);
590
0
        }
591
0
        return 1;
592
0
    }
593
594
0
    int64_t spill_sort_mem_limit() const {
595
0
        if (_query_options.__isset.spill_sort_mem_limit) {
596
0
            return std::max(_query_options.spill_sort_mem_limit, (int64_t)16777216);
597
0
        }
598
0
        return 134217728;
599
0
    }
600
601
0
    int64_t spill_sort_batch_bytes() const {
602
0
        if (_query_options.__isset.spill_sort_batch_bytes) {
603
0
            return std::max(_query_options.spill_sort_batch_bytes, (int64_t)8388608);
604
0
        }
605
0
        return 8388608;
606
0
    }
607
608
0
    int spill_aggregation_partition_count() const {
609
0
        if (_query_options.__isset.spill_aggregation_partition_count) {
610
0
            return std::min(std::max(_query_options.spill_aggregation_partition_count, 16), 8192);
611
0
        }
612
0
        return 32;
613
0
    }
614
615
0
    int spill_hash_join_partition_count() const {
616
0
        if (_query_options.__isset.spill_hash_join_partition_count) {
617
0
            return std::min(std::max(_query_options.spill_hash_join_partition_count, 16), 8192);
618
0
        }
619
0
        return 32;
620
0
    }
621
622
0
    int64_t low_memory_mode_buffer_limit() const {
623
0
        if (_query_options.__isset.low_memory_mode_buffer_limit) {
624
0
            return std::max(_query_options.low_memory_mode_buffer_limit, (int64_t)1);
625
0
        }
626
0
        return 32L * 1024 * 1024;
627
0
    }
628
629
1
    int spill_revocable_memory_high_watermark_percent() const {
630
1
        if (_query_options.__isset.revocable_memory_high_watermark_percent) {
631
1
            return _query_options.revocable_memory_high_watermark_percent;
632
1
        }
633
0
        return -1;
634
1
    }
635
636
0
    MOCK_FUNCTION bool enable_shared_exchange_sink_buffer() const {
637
0
        return _query_options.__isset.enable_shared_exchange_sink_buffer &&
638
0
               _query_options.enable_shared_exchange_sink_buffer;
639
0
    }
640
641
0
    bool fuzzy_disable_runtime_filter_in_be() const {
642
0
        return _query_options.__isset.fuzzy_disable_runtime_filter_in_be &&
643
0
               _query_options.fuzzy_disable_runtime_filter_in_be;
644
0
    }
645
646
44
    size_t minimum_operator_memory_required_bytes() const {
647
44
        if (_query_options.__isset.minimum_operator_memory_required_kb) {
648
44
            return _query_options.minimum_operator_memory_required_kb * 1024;
649
44
        } else {
650
            // refer other database
651
0
            return 100 * 1024;
652
0
        }
653
44
    }
654
655
5
    void set_max_operator_id(int max_operator_id) { _max_operator_id = max_operator_id; }
656
657
0
    int max_operator_id() const { return _max_operator_id; }
658
659
5
    void set_task_id(int id) { _task_id = id; }
660
661
40
    void set_task(pipeline::PipelineTask* task) { _task = task; }
662
663
9
    pipeline::PipelineTask* get_task() const { return _task; }
664
665
58
    int task_id() const { return _task_id; }
666
667
5
    void set_task_num(int task_num) { _task_num = task_num; }
668
669
0
    int task_num() const { return _task_num; }
670
671
0
    int profile_level() const { return _profile_level; }
672
673
private:
674
    Status create_error_log_file();
675
676
    static const int DEFAULT_BATCH_SIZE = 4062;
677
678
    std::shared_ptr<MemTrackerLimiter> _query_mem_tracker = nullptr;
679
680
    // Could not find a better way to record if the weak ptr is inited, use a bool to record
681
    // it. In some unit test cases, the runtime state's task ctx is not inited, then the test
682
    // hang, it is very hard to debug.
683
    bool _task_execution_context_inited = false;
684
    // Hold execution context for other threads
685
    std::weak_ptr<TaskExecutionContext> _task_execution_context;
686
687
    // put runtime state before _obj_pool, so that it will be deconstructed after
688
    // _obj_pool. Because some of object in _obj_pool will use profile when deconstructing.
689
    RuntimeProfile _profile;
690
    RuntimeProfile _load_channel_profile;
691
    // Why 2?
692
    // During cluster upgrade, fe will not pass profile_level to be, so we need to set it to 2
693
    // to make sure user can see all profile counters like before.
694
    int _profile_level = 2;
695
696
    const DescriptorTbl* _desc_tbl = nullptr;
697
    std::shared_ptr<ObjectPool> _obj_pool;
698
699
    // owned by PipelineFragmentContext
700
    RuntimeFilterMgr* _runtime_filter_mgr = nullptr;
701
702
    // Lock protecting _error_log and _unreported_error_idx
703
    std::mutex _error_log_lock;
704
705
    // Logs error messages.
706
    std::vector<std::string> _error_log;
707
708
    // _error_log[_unreported_error_idx+] has been not reported to the coordinator.
709
    int _unreported_error_idx;
710
711
    // Username of user that is executing the query to which this RuntimeState belongs.
712
    std::string _user;
713
714
    //Query-global timestamp_ms
715
    int64_t _timestamp_ms;
716
    int32_t _nano_seconds;
717
    std::string _timezone;
718
    cctz::time_zone _timezone_obj;
719
720
    TUniqueId _query_id;
721
    // fragment id for each TPipelineFragmentParams
722
    int32_t _fragment_id;
723
    TUniqueId _fragment_instance_id;
724
    TQueryOptions _query_options;
725
    ExecEnv* _exec_env = nullptr;
726
727
    AtomicStatus _exec_status;
728
729
    int _per_fragment_instance_idx;
730
    int _num_per_fragment_instances = 0;
731
    int _load_stream_per_node = 0;
732
    int _total_load_streams = 0;
733
    int _num_local_sink = 0;
734
735
    // The backend id on which this fragment instance runs
736
    int64_t _backend_id = -1;
737
738
    // used as send id
739
    int _be_number;
740
741
    // put here to collect files??
742
    std::vector<std::string> _output_files;
743
    std::atomic<int64_t> _num_rows_load_total;      // total rows read from source
744
    std::atomic<int64_t> _num_rows_load_filtered;   // unqualified rows
745
    std::atomic<int64_t> _num_rows_load_unselected; // rows filtered by predicates
746
    std::atomic<int64_t> _num_rows_filtered_in_strict_mode_partial_update;
747
    std::atomic<int64_t> _num_print_error_rows;
748
749
    std::atomic<int64_t> _num_bytes_load_total; // total bytes read from source
750
    std::atomic<int64_t> _num_finished_scan_range;
751
752
    std::vector<std::string> _export_output_files;
753
    std::string _import_label;
754
    std::string _db_name;
755
    std::string _load_dir;
756
    int64_t _load_job_id;
757
    int64_t _wal_id = -1;
758
    size_t _content_length = 0;
759
760
    // mini load
761
    int64_t _error_row_number;
762
    std::string _error_log_file_path;
763
    std::unique_ptr<std::ofstream> _error_log_file; // error file path, absolute path
764
    mutable std::mutex _tablet_infos_mutex;
765
    std::vector<TTabletCommitInfo> _tablet_commit_infos;
766
    std::vector<TErrorTabletInfo> _error_tablet_infos;
767
    int _max_operator_id = 0;
768
    pipeline::PipelineTask* _task = nullptr;
769
    int _task_id = -1;
770
    int _task_num = 0;
771
772
    std::vector<THivePartitionUpdate> _hive_partition_updates;
773
774
    std::vector<TIcebergCommitData> _iceberg_commit_datas;
775
776
    std::vector<std::unique_ptr<doris::pipeline::PipelineXLocalStateBase>> _op_id_to_local_state;
777
778
    std::unique_ptr<doris::pipeline::PipelineXSinkLocalStateBase> _sink_local_state;
779
780
    QueryContext* _query_ctx = nullptr;
781
782
    // true if max_filter_ratio is 0
783
    bool _load_zero_tolerance = false;
784
785
    // only to lock _pipeline_id_to_profile
786
    std::shared_mutex _pipeline_profile_lock;
787
    std::vector<std::shared_ptr<RuntimeProfile>> _pipeline_id_to_profile;
788
789
    // prohibit copies
790
    RuntimeState(const RuntimeState&);
791
792
    // save error log to s3
793
    std::shared_ptr<io::S3FileSystem> _s3_error_fs;
794
    // error file path on s3, ${bucket}/${prefix}/error_log/${label}_${fragment_instance_id}
795
    std::string _s3_error_log_file_path;
796
    std::mutex _s3_error_log_file_lock;
797
};
798
799
#define RETURN_IF_CANCELLED(state)               \
800
24
    do {                                         \
801
24
        if (UNLIKELY((state)->is_cancelled())) { \
802
0
            return (state)->cancel_reason();     \
803
0
        }                                        \
804
24
    } while (false)
805
806
} // namespace doris