Coverage Report

Created: 2026-04-01 00:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/runtime/runtime_state.cpp
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
// This file is copied from
18
// https://github.com/apache/impala/blob/branch-2.9.0/be/src/runtime/runtime-state.cpp
19
// and modified by Doris
20
21
#include "runtime/runtime_state.h"
22
23
#include <fmt/format.h>
24
#include <gen_cpp/PaloInternalService_types.h>
25
#include <gen_cpp/Types_types.h>
26
#include <glog/logging.h>
27
28
#include <fstream>
29
#include <memory>
30
#include <string>
31
32
#include "cloud/cloud_storage_engine.h"
33
#include "cloud/config.h"
34
#include "common/config.h"
35
#include "common/logging.h"
36
#include "common/object_pool.h"
37
#include "common/status.h"
38
#include "core/value/vdatetime_value.h"
39
#include "exec/operator/operator.h"
40
#include "exec/pipeline/pipeline_fragment_context.h"
41
#include "exec/pipeline/pipeline_task.h"
42
#include "exec/runtime_filter/runtime_filter_consumer.h"
43
#include "exec/runtime_filter/runtime_filter_mgr.h"
44
#include "exec/runtime_filter/runtime_filter_producer.h"
45
#include "exprs/function/cast/cast_to_date_or_datetime_impl.hpp"
46
#include "io/fs/s3_file_system.h"
47
#include "load/load_path_mgr.h"
48
#include "runtime/exec_env.h"
49
#include "runtime/fragment_mgr.h"
50
#include "runtime/memory/mem_tracker_limiter.h"
51
#include "runtime/query_context.h"
52
#include "runtime/thread_context.h"
53
#include "storage/id_manager.h"
54
#include "storage/storage_engine.h"
55
#include "util/timezone_utils.h"
56
#include "util/uid_util.h"
57
58
namespace doris {
59
#include "common/compile_check_begin.h"
60
using namespace ErrorCode;
61
62
RuntimeState::RuntimeState(const TPlanFragmentExecParams& fragment_exec_params,
63
                           const TQueryOptions& query_options, const TQueryGlobals& query_globals,
64
                           ExecEnv* exec_env, QueryContext* ctx,
65
                           const std::shared_ptr<MemTrackerLimiter>& query_mem_tracker)
66
567
        : _profile("Fragment " + print_id(fragment_exec_params.fragment_instance_id)),
67
567
          _load_channel_profile("<unnamed>"),
68
567
          _obj_pool(new ObjectPool()),
69
567
          _unreported_error_idx(0),
70
567
          _query_id(fragment_exec_params.query_id),
71
567
          _per_fragment_instance_idx(0),
72
567
          _num_rows_load_total(0),
73
567
          _num_rows_load_filtered(0),
74
567
          _num_rows_load_unselected(0),
75
567
          _num_print_error_rows(0),
76
567
          _num_bytes_load_total(0),
77
567
          _num_finished_scan_range(0),
78
567
          _query_ctx(ctx) {
79
567
    Status status =
80
567
            init(fragment_exec_params.fragment_instance_id, query_options, query_globals, exec_env);
81
567
    DCHECK(status.ok());
82
567
    _query_mem_tracker = query_mem_tracker;
83
567
    DCHECK(_query_mem_tracker != nullptr);
84
567
}
85
86
RuntimeState::RuntimeState(const TUniqueId& instance_id, const TUniqueId& query_id,
87
                           int32_t fragment_id, const TQueryOptions& query_options,
88
                           const TQueryGlobals& query_globals, ExecEnv* exec_env, QueryContext* ctx)
89
1.96M
        : _profile("Fragment " + print_id(instance_id)),
90
1.96M
          _load_channel_profile("<unnamed>"),
91
1.96M
          _obj_pool(new ObjectPool()),
92
1.96M
          _unreported_error_idx(0),
93
1.96M
          _query_id(query_id),
94
1.96M
          _fragment_id(fragment_id),
95
1.96M
          _per_fragment_instance_idx(0),
96
1.96M
          _num_rows_load_total(0),
97
1.96M
          _num_rows_load_filtered(0),
98
1.96M
          _num_rows_load_unselected(0),
99
1.96M
          _num_rows_filtered_in_strict_mode_partial_update(0),
100
1.96M
          _num_print_error_rows(0),
101
1.96M
          _num_bytes_load_total(0),
102
1.96M
          _num_finished_scan_range(0),
103
1.96M
          _query_ctx(ctx) {
104
1.96M
    [[maybe_unused]] auto status = init(instance_id, query_options, query_globals, exec_env);
105
1.96M
    DCHECK(status.ok());
106
1.96M
    _query_mem_tracker = ctx->query_mem_tracker();
107
1.96M
}
108
109
RuntimeState::RuntimeState(const TUniqueId& query_id, int32_t fragment_id,
110
                           const TQueryOptions& query_options, const TQueryGlobals& query_globals,
111
                           ExecEnv* exec_env, QueryContext* ctx)
112
438k
        : _profile(fmt::format("PipelineX(fragment_id={})", fragment_id)),
113
438k
          _load_channel_profile("<unnamed>"),
114
438k
          _obj_pool(new ObjectPool()),
115
438k
          _unreported_error_idx(0),
116
438k
          _query_id(query_id),
117
438k
          _fragment_id(fragment_id),
118
438k
          _per_fragment_instance_idx(0),
119
438k
          _num_rows_load_total(0),
120
438k
          _num_rows_load_filtered(0),
121
438k
          _num_rows_load_unselected(0),
122
438k
          _num_rows_filtered_in_strict_mode_partial_update(0),
123
438k
          _num_print_error_rows(0),
124
438k
          _num_bytes_load_total(0),
125
438k
          _num_finished_scan_range(0),
126
438k
          _query_ctx(ctx) {
127
    // TODO: do we really need instance id?
128
438k
    Status status = init(TUniqueId(), query_options, query_globals, exec_env);
129
438k
    DCHECK(status.ok());
130
438k
    _query_mem_tracker = ctx->query_mem_tracker();
131
438k
}
132
133
RuntimeState::RuntimeState(const TUniqueId& query_id, int32_t fragment_id,
134
                           const TQueryOptions& query_options, const TQueryGlobals& query_globals,
135
                           ExecEnv* exec_env,
136
                           const std::shared_ptr<MemTrackerLimiter>& query_mem_tracker)
137
1.73k
        : _profile(fmt::format("PipelineX(fragment_id={})", fragment_id)),
138
1.73k
          _load_channel_profile("<unnamed>"),
139
1.73k
          _obj_pool(new ObjectPool()),
140
1.73k
          _unreported_error_idx(0),
141
1.73k
          _query_id(query_id),
142
1.73k
          _fragment_id(fragment_id),
143
1.73k
          _per_fragment_instance_idx(0),
144
1.73k
          _num_rows_load_total(0),
145
1.73k
          _num_rows_load_filtered(0),
146
1.73k
          _num_rows_load_unselected(0),
147
1.73k
          _num_rows_filtered_in_strict_mode_partial_update(0),
148
1.73k
          _num_print_error_rows(0),
149
1.73k
          _num_bytes_load_total(0),
150
1.73k
          _num_finished_scan_range(0) {
151
1.73k
    Status status = init(TUniqueId(), query_options, query_globals, exec_env);
152
1.73k
    DCHECK(status.ok());
153
1.73k
    _query_mem_tracker = query_mem_tracker;
154
1.73k
    DCHECK(_query_mem_tracker != nullptr);
155
1.73k
}
156
157
RuntimeState::RuntimeState(const TQueryOptions& query_options, const TQueryGlobals& query_globals)
158
59.0k
        : _profile("<unnamed>"),
159
59.0k
          _load_channel_profile("<unnamed>"),
160
59.0k
          _obj_pool(new ObjectPool()),
161
59.0k
          _unreported_error_idx(0),
162
59.0k
          _per_fragment_instance_idx(0) {
163
59.0k
    Status status = init(TUniqueId(), query_options, query_globals, nullptr);
164
59.0k
    _exec_env = ExecEnv::GetInstance();
165
59.0k
    init_mem_trackers("<unnamed>");
166
59.0k
    DCHECK(status.ok());
167
59.0k
}
168
169
RuntimeState::RuntimeState()
170
140k
        : _profile("<unnamed>"),
171
140k
          _load_channel_profile("<unnamed>"),
172
140k
          _obj_pool(new ObjectPool()),
173
140k
          _unreported_error_idx(0),
174
140k
          _per_fragment_instance_idx(0) {
175
140k
    _query_options.batch_size = DEFAULT_BATCH_SIZE;
176
140k
    _query_options.be_exec_version = BeExecVersionManager::get_newest_version();
177
140k
    _timezone = TimezoneUtils::default_time_zone;
178
140k
    _timestamp_ms = 0;
179
140k
    _nano_seconds = 0;
180
140k
    TimezoneUtils::find_cctz_time_zone(_timezone, _timezone_obj);
181
140k
    _exec_env = ExecEnv::GetInstance();
182
140k
    init_mem_trackers("<unnamed>");
183
140k
}
184
185
2.60M
RuntimeState::~RuntimeState() {
186
2.60M
    SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(_query_mem_tracker);
187
    // close error log file
188
2.60M
    if (_error_log_file != nullptr && _error_log_file->is_open()) {
189
65
        _error_log_file->close();
190
65
    }
191
2.60M
    _obj_pool->clear();
192
2.60M
}
193
194
11.8k
const std::set<int>& RuntimeState::get_deregister_runtime_filter() const {
195
11.8k
    return _registered_runtime_filter_ids;
196
11.8k
}
197
198
2
void RuntimeState::merge_register_runtime_filter(const std::set<int>& runtime_filter_ids) {
199
2
    _registered_runtime_filter_ids.insert(runtime_filter_ids.begin(), runtime_filter_ids.end());
200
2
}
201
202
Status RuntimeState::init(const TUniqueId& fragment_instance_id, const TQueryOptions& query_options,
203
2.45M
                          const TQueryGlobals& query_globals, ExecEnv* exec_env) {
204
2.45M
    _fragment_instance_id = fragment_instance_id;
205
2.45M
    _query_options = query_options;
206
2.45M
    _lc_time_names = query_globals.lc_time_names;
207
2.46M
    if (query_globals.__isset.time_zone && query_globals.__isset.nano_seconds) {
208
2.46M
        _timezone = query_globals.time_zone;
209
2.46M
        _timestamp_ms = query_globals.timestamp_ms;
210
2.46M
        _nano_seconds = query_globals.nano_seconds;
211
18.4E
    } else if (query_globals.__isset.time_zone) {
212
1.03k
        _timezone = query_globals.time_zone;
213
1.03k
        _timestamp_ms = query_globals.timestamp_ms;
214
1.03k
        _nano_seconds = 0;
215
18.4E
    } else if (!query_globals.now_string.empty()) {
216
0
        _timezone = TimezoneUtils::default_time_zone;
217
0
        VecDateTimeValue dt;
218
0
        CastParameters params;
219
0
        DORIS_CHECK((CastToDateOrDatetime::from_string_strict_mode<DatelikeParseMode::STRICT,
220
0
                                                                   DatelikeTargetType::DATE_TIME>(
221
0
                {query_globals.now_string.c_str(), query_globals.now_string.size()}, dt, nullptr,
222
0
                params)));
223
0
        int64_t timestamp;
224
0
        dt.unix_timestamp(&timestamp, _timezone);
225
0
        _timestamp_ms = timestamp * 1000;
226
0
        _nano_seconds = 0;
227
18.4E
    } else {
228
        //Unit test may set into here
229
18.4E
        _timezone = TimezoneUtils::default_time_zone;
230
18.4E
        _timestamp_ms = 0;
231
18.4E
        _nano_seconds = 0;
232
18.4E
    }
233
2.45M
    TimezoneUtils::find_cctz_time_zone(_timezone, _timezone_obj);
234
235
2.45M
    if (query_globals.__isset.load_zero_tolerance) {
236
2.45M
        _load_zero_tolerance = query_globals.load_zero_tolerance;
237
2.45M
    }
238
239
2.45M
    _exec_env = exec_env;
240
241
2.45M
    if (_query_options.max_errors <= 0) {
242
        // TODO: fix linker error and uncomment this
243
        //_query_options.max_errors = config::max_errors;
244
2.45M
        _query_options.max_errors = 100;
245
2.45M
    }
246
247
2.45M
    if (_query_options.batch_size <= 0) {
248
54.7k
        _query_options.batch_size = DEFAULT_BATCH_SIZE;
249
54.7k
    }
250
251
2.45M
    _db_name = "insert_stmt";
252
2.45M
    _import_label = print_id(fragment_instance_id);
253
254
18.4E
    _profile_level = query_options.__isset.profile_level ? query_options.profile_level : 2;
255
256
2.45M
    return Status::OK();
257
2.45M
}
258
259
2.81k
std::weak_ptr<QueryContext> RuntimeState::get_query_ctx_weak() {
260
2.81k
    return _exec_env->fragment_mgr()->get_query_ctx(_query_ctx->query_id());
261
2.81k
}
262
263
199k
void RuntimeState::init_mem_trackers(const std::string& name, const TUniqueId& id) {
264
199k
    _query_mem_tracker = MemTrackerLimiter::create_shared(
265
199k
            MemTrackerLimiter::Type::OTHER, fmt::format("{}#Id={}", name, print_id(id)));
266
199k
}
267
268
3.06M
std::shared_ptr<MemTrackerLimiter> RuntimeState::query_mem_tracker() const {
269
3.06M
    CHECK(_query_mem_tracker != nullptr);
270
3.06M
    return _query_mem_tracker;
271
3.06M
}
272
273
38.4M
WorkloadGroupPtr RuntimeState::workload_group() {
274
38.4M
    return _query_ctx->workload_group();
275
38.4M
}
276
277
0
bool RuntimeState::log_error(const std::string& error) {
278
0
    std::lock_guard<std::mutex> l(_error_log_lock);
279
280
0
    if (_error_log.size() < _query_options.max_errors) {
281
0
        _error_log.push_back(error);
282
0
        return true;
283
0
    }
284
285
0
    return false;
286
0
}
287
288
47.2k
void RuntimeState::get_unreported_errors(std::vector<std::string>* new_errors) {
289
47.2k
    std::lock_guard<std::mutex> l(_error_log_lock);
290
291
47.2k
    if (_unreported_error_idx < _error_log.size()) {
292
0
        new_errors->assign(_error_log.begin() + _unreported_error_idx, _error_log.end());
293
0
        _unreported_error_idx = (int)_error_log.size();
294
0
    }
295
47.2k
}
296
297
22.9M
bool RuntimeState::is_cancelled() const {
298
    // Maybe we should just return _is_cancelled.load()
299
22.9M
    return !_exec_status.ok() || (_query_ctx && _query_ctx->is_cancelled());
300
22.9M
}
301
302
107
Status RuntimeState::cancel_reason() const {
303
107
    if (!_exec_status.ok()) {
304
0
        return _exec_status.status();
305
0
    }
306
307
107
    if (_query_ctx) {
308
107
        return _query_ctx->exec_status();
309
107
    }
310
311
0
    return Status::Cancelled("Query cancelled");
312
107
}
313
314
const int64_t MAX_ERROR_NUM = 50;
315
316
596
Status RuntimeState::create_error_log_file() {
317
596
    if (config::save_load_error_log_to_s3 && config::is_cloud_mode()) {
318
564
        _s3_error_fs = std::dynamic_pointer_cast<io::S3FileSystem>(
319
564
                ExecEnv::GetInstance()->storage_engine().to_cloud().latest_fs());
320
564
        if (_s3_error_fs) {
321
564
            std::stringstream ss;
322
            // https://dev.mysql.com/doc/dev/mysql-server/latest/page_protocol_basic_err_packet.html
323
            // shorten the path as much as possible to prevent the length of the presigned URL from
324
            // exceeding the MySQL error packet size limit
325
564
            ss << "error_log/" << std::hex << _fragment_instance_id.lo;
326
564
            _s3_error_log_file_path = ss.str();
327
564
        }
328
564
    }
329
330
596
    static_cast<void>(_exec_env->load_path_mgr()->get_load_error_file_name(
331
596
            _db_name, _import_label, _fragment_instance_id, &_error_log_file_path));
332
596
    std::string error_log_absolute_path =
333
596
            _exec_env->load_path_mgr()->get_load_error_absolute_path(_error_log_file_path);
334
596
    _error_log_file = std::make_unique<std::ofstream>(error_log_absolute_path, std::ifstream::out);
335
596
    if (!_error_log_file->is_open()) {
336
0
        std::stringstream error_msg;
337
0
        error_msg << "Fail to open error file: [" << _error_log_file_path << "].";
338
0
        LOG(WARNING) << error_msg.str();
339
0
        return Status::InternalError(error_msg.str());
340
0
    }
341
596
    LOG(INFO) << "create error log file: " << _error_log_file_path
342
596
              << ", query id: " << print_id(_query_id)
343
596
              << ", fragment instance id: " << print_id(_fragment_instance_id);
344
345
596
    return Status::OK();
346
596
}
347
348
Status RuntimeState::append_error_msg_to_file(std::function<std::string()> line,
349
7.86k
                                              std::function<std::string()> error_msg) {
350
7.86k
    if (query_type() != TQueryType::LOAD) {
351
28
        return Status::OK();
352
28
    }
353
    // If file haven't been opened, open it here
354
7.83k
    if (_error_log_file == nullptr) {
355
596
        Status status = create_error_log_file();
356
596
        if (!status.ok()) {
357
0
            LOG(WARNING) << "Create error file log failed. because: " << status;
358
0
            if (_error_log_file != nullptr) {
359
0
                _error_log_file->close();
360
0
            }
361
0
            return status;
362
0
        }
363
        // record the first error message if the file is just created
364
596
        _first_error_msg = error_msg() + ". Src line: " + line();
365
596
        LOG(INFO) << "The first error message: " << _first_error_msg;
366
596
    }
367
    // If num of printed error row exceeds the limit, don't add error messages to error log file any more
368
7.83k
    if (_num_print_error_rows.fetch_add(1, std::memory_order_relaxed) > MAX_ERROR_NUM) {
369
        // if _load_zero_tolerance, return Error to stop the load process immediately.
370
4.90k
        if (_load_zero_tolerance) {
371
10
            return Status::DataQualityError(
372
10
                    "Encountered unqualified data, stop processing. Please check if the source "
373
10
                    "data matches the schema, and consider disabling strict mode or increasing "
374
10
                    "max_filter_ratio.");
375
10
        }
376
4.89k
        return Status::OK();
377
4.90k
    }
378
379
2.93k
    fmt::memory_buffer out;
380
    // Note: export reason first in case src line too long and be truncated.
381
2.93k
    fmt::format_to(out, "Reason: {}. src line [{}]; ", error_msg(), line());
382
383
2.93k
    size_t error_row_size = out.size();
384
2.93k
    if (error_row_size > 0) {
385
2.93k
        if (error_row_size > config::load_error_log_limit_bytes) {
386
0
            fmt::memory_buffer limit_byte_out;
387
0
            limit_byte_out.append(out.data(), out.data() + config::load_error_log_limit_bytes);
388
0
            (*_error_log_file) << fmt::to_string(limit_byte_out) + "error log is too long"
389
0
                               << std::endl;
390
2.93k
        } else {
391
2.93k
            (*_error_log_file) << fmt::to_string(out) << std::endl;
392
2.93k
        }
393
2.93k
    }
394
395
2.93k
    return Status::OK();
396
7.83k
}
397
398
282k
std::string RuntimeState::get_error_log_file_path() {
399
282k
    DBUG_EXECUTE_IF("RuntimeState::get_error_log_file_path.block", {
400
282k
        if (!_error_log_file_path.empty()) {
401
282k
            std::this_thread::sleep_for(std::chrono::seconds(1));
402
282k
        }
403
282k
    });
404
282k
    std::lock_guard<std::mutex> l(_s3_error_log_file_lock);
405
282k
    if (_s3_error_fs && _error_log_file && _error_log_file->is_open()) {
406
        // close error log file
407
531
        _error_log_file->close();
408
531
        std::string error_log_absolute_path =
409
531
                _exec_env->load_path_mgr()->get_load_error_absolute_path(_error_log_file_path);
410
        // upload error log file to s3
411
531
        Status st = _s3_error_fs->upload(error_log_absolute_path, _s3_error_log_file_path);
412
531
        if (!st.ok()) {
413
            // upload failed and return local error log file path
414
0
            LOG(WARNING) << "Fail to upload error file to s3, error_log_file_path="
415
0
                         << _error_log_file_path << ", error=" << st;
416
0
            return _error_log_file_path;
417
0
        }
418
        // expiration must be less than a week (in seconds) for presigned url
419
531
        static const unsigned EXPIRATION_SECONDS = 7 * 24 * 60 * 60 - 1;
420
        // Use public or private endpoint based on configuration
421
531
        _error_log_file_path =
422
531
                _s3_error_fs->generate_presigned_url(_s3_error_log_file_path, EXPIRATION_SECONDS,
423
531
                                                     config::use_public_endpoint_for_error_log);
424
531
    }
425
282k
    return _error_log_file_path;
426
282k
}
427
428
2.03M
void RuntimeState::resize_op_id_to_local_state(int operator_size) {
429
2.03M
    _op_id_to_local_state.resize(-operator_size);
430
2.03M
}
431
432
void RuntimeState::emplace_local_state(int id,
433
2.44M
                                       std::unique_ptr<doris::PipelineXLocalStateBase> state) {
434
2.44M
    id = -id;
435
2.44M
    DCHECK_LT(id, _op_id_to_local_state.size())
436
0
            << state->parent()->get_name() << " node id = " << state->parent()->node_id();
437
2.44M
    DCHECK(!_op_id_to_local_state[id]);
438
2.44M
    _op_id_to_local_state[id] = std::move(state);
439
2.44M
}
440
441
449M
doris::PipelineXLocalStateBase* RuntimeState::get_local_state(int id) {
442
449M
    DCHECK_GT(_op_id_to_local_state.size(), -id);
443
449M
    return _op_id_to_local_state[-id].get();
444
449M
}
445
446
5.91M
Result<RuntimeState::LocalState*> RuntimeState::get_local_state_result(int id) {
447
5.91M
    id = -id;
448
5.91M
    if (id >= _op_id_to_local_state.size()) {
449
0
        return ResultError(Status::InternalError("get_local_state out of range size:{} , id:{}",
450
0
                                                 _op_id_to_local_state.size(), id));
451
0
    }
452
5.91M
    if (!_op_id_to_local_state[id]) {
453
0
        return ResultError(Status::InternalError("get_local_state id:{} is null", id));
454
0
    }
455
5.91M
    return _op_id_to_local_state[id].get();
456
5.91M
};
457
458
void RuntimeState::emplace_sink_local_state(
459
2.02M
        int id, std::unique_ptr<doris::PipelineXSinkLocalStateBase> state) {
460
18.4E
    DCHECK(!_sink_local_state) << " id=" << id << " state: " << state->debug_string(0);
461
2.02M
    _sink_local_state = std::move(state);
462
2.02M
}
463
464
35.9M
doris::PipelineXSinkLocalStateBase* RuntimeState::get_sink_local_state() {
465
35.9M
    return _sink_local_state.get();
466
35.9M
}
467
468
42.4M
Result<RuntimeState::SinkLocalState*> RuntimeState::get_sink_local_state_result() {
469
42.4M
    if (!_sink_local_state) {
470
0
        return ResultError(Status::InternalError("_op_id_to_sink_local_state not exist"));
471
0
    }
472
42.4M
    return _sink_local_state.get();
473
42.4M
}
474
475
1.21M
bool RuntimeState::enable_page_cache() const {
476
1.21M
    return !config::disable_storage_page_cache &&
477
1.21M
           (_query_options.__isset.enable_page_cache && _query_options.enable_page_cache);
478
1.21M
}
479
480
207k
RuntimeFilterMgr* RuntimeState::global_runtime_filter_mgr() {
481
207k
    return _query_ctx->runtime_filter_mgr();
482
207k
}
483
484
Status RuntimeState::register_producer_runtime_filter(
485
78.7k
        const TRuntimeFilterDesc& desc, std::shared_ptr<RuntimeFilterProducer>* producer_filter) {
486
78.7k
    _registered_runtime_filter_ids.insert(desc.filter_id);
487
    // Producers are created by local runtime filter mgr and shared by global runtime filter manager.
488
    // When RF is published, consumers in both global and local RF mgr will be found.
489
78.7k
    RETURN_IF_ERROR(local_runtime_filter_mgr()->register_producer_filter(_query_ctx, desc,
490
78.7k
                                                                         producer_filter));
491
    // Stamp the producer with the current recursive CTE stage so that outgoing merge RPCs
492
    // carry the correct round number and stale messages from old rounds are discarded.
493
    // PFC must still be alive: this runs inside a pipeline task, so the execution context
494
    // cannot have expired yet.
495
    // In unit-test scenarios the task execution context is never set (no PipelineFragmentContext
496
    // exists), so skip the stage stamping — the default stage (0) is correct.
497
79.2k
    if (task_execution_context_inited()) {
498
79.2k
        auto pfc = std::static_pointer_cast<PipelineFragmentContext>(
499
79.2k
                get_task_execution_context().lock());
500
79.2k
        DORIS_CHECK(pfc);
501
79.2k
        (*producer_filter)->set_stage(pfc->rec_cte_stage());
502
79.2k
    }
503
78.7k
    RETURN_IF_ERROR(global_runtime_filter_mgr()->register_local_merger_producer_filter(
504
78.7k
            _query_ctx, desc, *producer_filter));
505
78.7k
    return Status::OK();
506
78.7k
}
507
508
Status RuntimeState::register_consumer_runtime_filter(
509
        const TRuntimeFilterDesc& desc, bool need_local_merge, int node_id,
510
44.8k
        std::shared_ptr<RuntimeFilterConsumer>* consumer_filter) {
511
44.8k
    _registered_runtime_filter_ids.insert(desc.filter_id);
512
44.8k
    bool need_merge = desc.has_remote_targets || need_local_merge;
513
44.8k
    RuntimeFilterMgr* mgr = need_merge ? global_runtime_filter_mgr() : local_runtime_filter_mgr();
514
44.8k
    RETURN_IF_ERROR(mgr->register_consumer_filter(this, desc, node_id, consumer_filter));
515
    // Stamp the consumer with the current recursive CTE stage so that incoming publish RPCs
516
    // from old rounds are detected and discarded.
517
    // PFC must still be alive: this runs inside a pipeline task, so the execution context
518
    // cannot have expired yet.
519
    // In unit-test scenarios the task execution context is never set (no PipelineFragmentContext
520
    // exists), so skip the stage stamping — the default stage (0) is correct.
521
45.2k
    if (task_execution_context_inited()) {
522
45.2k
        auto pfc = std::static_pointer_cast<PipelineFragmentContext>(
523
45.2k
                get_task_execution_context().lock());
524
45.2k
        DORIS_CHECK(pfc);
525
45.2k
        (*consumer_filter)->set_stage(pfc->rec_cte_stage());
526
45.2k
    }
527
44.8k
    return Status::OK();
528
44.8k
}
529
530
11.2M
bool RuntimeState::is_nereids() const {
531
11.2M
    return _query_ctx->is_nereids();
532
11.2M
}
533
534
2.45k
std::vector<std::shared_ptr<RuntimeProfile>> RuntimeState::pipeline_id_to_profile() {
535
2.45k
    std::shared_lock lc(_pipeline_profile_lock);
536
2.45k
    return _pipeline_id_to_profile;
537
2.45k
}
538
539
std::vector<std::shared_ptr<RuntimeProfile>> RuntimeState::build_pipeline_profile(
540
438k
        std::size_t pipeline_size) {
541
438k
    std::unique_lock lc(_pipeline_profile_lock);
542
438k
    if (!_pipeline_id_to_profile.empty()) {
543
0
        return _pipeline_id_to_profile;
544
0
    }
545
438k
    _pipeline_id_to_profile.resize(pipeline_size);
546
438k
    {
547
438k
        size_t pip_idx = 0;
548
691k
        for (auto& pipeline_profile : _pipeline_id_to_profile) {
549
691k
            pipeline_profile =
550
691k
                    std::make_shared<RuntimeProfile>(fmt::format("Pipeline(id={})", pip_idx));
551
691k
            pip_idx++;
552
691k
        }
553
438k
    }
554
438k
    return _pipeline_id_to_profile;
555
438k
}
556
557
83.0M
bool RuntimeState::low_memory_mode() const {
558
#ifdef BE_TEST
559
    if (!_query_ctx) {
560
        return false;
561
    }
562
#endif
563
83.0M
    return _query_ctx->low_memory_mode();
564
83.0M
}
565
566
5.27k
void RuntimeState::set_id_file_map() {
567
5.27k
    _id_file_map = _exec_env->get_id_manager()->add_id_file_map(_query_id, execution_timeout());
568
5.27k
}
569
#include "common/compile_check_end.h"
570
} // end namespace doris