Coverage Report

Created: 2026-03-31 11:09

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