Coverage Report

Created: 2024-11-22 00:22

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