Coverage Report

Created: 2026-05-09 13:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/runtime/query_context.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
18
#include "runtime/query_context.h"
19
20
#include <fmt/core.h>
21
#include <gen_cpp/FrontendService_types.h>
22
#include <gen_cpp/RuntimeProfile_types.h>
23
#include <gen_cpp/Types_types.h>
24
#include <glog/logging.h>
25
26
#include <algorithm>
27
#include <memory>
28
#include <mutex>
29
#include <utility>
30
#include <vector>
31
32
#include "common/logging.h"
33
#include "common/status.h"
34
#include "exec/operator/rec_cte_scan_operator.h"
35
#include "exec/pipeline/dependency.h"
36
#include "exec/pipeline/pipeline_fragment_context.h"
37
#include "exec/runtime_filter/runtime_filter_definitions.h"
38
#include "exec/spill/spill_file_manager.h"
39
#include "runtime/exec_env.h"
40
#include "runtime/fragment_mgr.h"
41
#include "runtime/memory/heap_profiler.h"
42
#include "runtime/runtime_query_statistics_mgr.h"
43
#include "runtime/runtime_state.h"
44
#include "runtime/thread_context.h"
45
#include "runtime/workload_group/workload_group_manager.h"
46
#include "runtime/workload_management/query_task_controller.h"
47
#include "storage/olap_common.h"
48
#include "util/mem_info.h"
49
#include "util/uid_util.h"
50
51
namespace doris {
52
53
class DelayReleaseToken : public Runnable {
54
    ENABLE_FACTORY_CREATOR(DelayReleaseToken);
55
56
public:
57
0
    DelayReleaseToken(std::unique_ptr<ThreadPoolToken>&& token) { token_ = std::move(token); }
58
    ~DelayReleaseToken() override = default;
59
0
    void run() override {}
60
    std::unique_ptr<ThreadPoolToken> token_;
61
};
62
63
0
const std::string toString(QuerySource queryType) {
64
0
    switch (queryType) {
65
0
    case QuerySource::INTERNAL_FRONTEND:
66
0
        return "INTERNAL_FRONTEND";
67
0
    case QuerySource::STREAM_LOAD:
68
0
        return "STREAM_LOAD";
69
0
    case QuerySource::GROUP_COMMIT_LOAD:
70
0
        return "EXTERNAL_QUERY";
71
0
    case QuerySource::ROUTINE_LOAD:
72
0
        return "ROUTINE_LOAD";
73
0
    case QuerySource::EXTERNAL_CONNECTOR:
74
0
        return "EXTERNAL_CONNECTOR";
75
0
    case QuerySource::EXTERNAL_FRONTEND:
76
0
        return "EXTERNAL_FRONTEND";
77
0
    default:
78
0
        return "UNKNOWN";
79
0
    }
80
0
}
81
82
std::shared_ptr<QueryContext> QueryContext::create(TUniqueId query_id, ExecEnv* exec_env,
83
                                                   const TQueryOptions& query_options,
84
                                                   TNetworkAddress coord_addr, bool is_nereids,
85
                                                   TNetworkAddress current_connect_fe,
86
168
                                                   QuerySource query_type) {
87
168
    auto ctx = QueryContext::create_shared(query_id, exec_env, query_options, coord_addr,
88
168
                                           is_nereids, current_connect_fe, query_type);
89
168
    ctx->init_query_task_controller();
90
168
    return ctx;
91
168
}
92
93
QueryContext::QueryContext(TUniqueId query_id, ExecEnv* exec_env,
94
                           const TQueryOptions& query_options, TNetworkAddress coord_addr,
95
                           bool is_nereids, TNetworkAddress current_connect_fe,
96
                           QuerySource query_source)
97
122k
        : _timeout_second(-1),
98
122k
          _query_id(std::move(query_id)),
99
122k
          _exec_env(exec_env),
100
122k
          _is_nereids(is_nereids),
101
122k
          _query_options(query_options),
102
122k
          _query_source(query_source) {
103
122k
    _init_resource_context();
104
122k
    SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(query_mem_tracker());
105
122k
    _query_watcher.start();
106
122k
    _execution_dependency = Dependency::create_unique(-1, -1, "ExecutionDependency", false);
107
122k
    _memory_sufficient_dependency =
108
122k
            Dependency::create_unique(-1, -1, "MemorySufficientDependency", true);
109
110
122k
    _runtime_filter_mgr = std::make_unique<RuntimeFilterMgr>(true);
111
112
122k
    _timeout_second = query_options.execution_timeout;
113
114
122k
    bool initialize_context_holder =
115
122k
            config::enable_file_cache && config::enable_file_cache_query_limit &&
116
122k
            query_options.__isset.enable_file_cache && query_options.enable_file_cache &&
117
122k
            query_options.__isset.file_cache_query_limit_percent &&
118
122k
            query_options.file_cache_query_limit_percent < 100;
119
120
    // Initialize file cache context holders
121
122k
    if (initialize_context_holder) {
122
0
        _query_context_holders = io::FileCacheFactory::instance()->get_query_context_holders(
123
0
                _query_id, query_options.file_cache_query_limit_percent);
124
0
    }
125
126
122k
    bool is_query_type_valid = query_options.query_type == TQueryType::SELECT ||
127
122k
                               query_options.query_type == TQueryType::LOAD ||
128
122k
                               query_options.query_type == TQueryType::EXTERNAL;
129
122k
    DCHECK_EQ(is_query_type_valid, true);
130
131
122k
    this->coord_addr = coord_addr;
132
    // current_connect_fe is used for report query statistics
133
122k
    this->current_connect_fe = current_connect_fe;
134
    // external query has no current_connect_fe
135
122k
    if (query_options.query_type != TQueryType::EXTERNAL) {
136
311
        bool is_report_fe_addr_valid =
137
311
                !this->current_connect_fe.hostname.empty() && this->current_connect_fe.port != 0;
138
311
        DCHECK_EQ(is_report_fe_addr_valid, true);
139
311
    }
140
122k
    clock_gettime(CLOCK_MONOTONIC, &this->_query_arrival_timestamp);
141
122k
    DorisMetrics::instance()->query_ctx_cnt->increment(1);
142
122k
    _mem_arb = MemShareArbitrator::create_shared(
143
122k
            query_id, query_options.mem_limit,
144
122k
            query_options.__isset.max_scan_mem_ratio ? query_options.max_scan_mem_ratio : 1.0);
145
122k
}
146
147
122k
void QueryContext::_init_query_mem_tracker() {
148
122k
    bool has_query_mem_limit = _query_options.__isset.mem_limit && (_query_options.mem_limit > 0);
149
122k
    int64_t bytes_limit = has_query_mem_limit ? _query_options.mem_limit : -1;
150
122k
    if (bytes_limit > MemInfo::mem_limit() || bytes_limit == -1) {
151
0
        VLOG_NOTICE << "Query memory limit " << PrettyPrinter::print(bytes_limit, TUnit::BYTES)
152
0
                    << " exceeds process memory limit of "
153
0
                    << PrettyPrinter::print(MemInfo::mem_limit(), TUnit::BYTES)
154
0
                    << " OR is -1. Using process memory limit instead.";
155
0
        bytes_limit = MemInfo::mem_limit();
156
0
    }
157
    // If the query is a pure load task(streamload, routine load, group commit), then it should not use
158
    // memlimit per query to limit their memory usage.
159
122k
    if (is_pure_load_task()) {
160
122k
        bytes_limit = MemInfo::mem_limit();
161
122k
    }
162
122k
    std::shared_ptr<MemTrackerLimiter> query_mem_tracker;
163
122k
    if (_query_options.query_type == TQueryType::SELECT) {
164
308
        query_mem_tracker = MemTrackerLimiter::create_shared(
165
308
                MemTrackerLimiter::Type::QUERY, fmt::format("Query#Id={}", print_id(_query_id)),
166
308
                bytes_limit);
167
122k
    } else if (_query_options.query_type == TQueryType::LOAD) {
168
3
        query_mem_tracker = MemTrackerLimiter::create_shared(
169
3
                MemTrackerLimiter::Type::LOAD, fmt::format("Load#Id={}", print_id(_query_id)),
170
3
                bytes_limit);
171
122k
    } else if (_query_options.query_type == TQueryType::EXTERNAL) { // spark/flink/etc..
172
122k
        query_mem_tracker = MemTrackerLimiter::create_shared(
173
122k
                MemTrackerLimiter::Type::QUERY, fmt::format("External#Id={}", print_id(_query_id)),
174
122k
                bytes_limit);
175
122k
    } else {
176
0
        LOG(FATAL) << "__builtin_unreachable";
177
0
        __builtin_unreachable();
178
0
    }
179
122k
    if (_query_options.__isset.is_report_success && _query_options.is_report_success) {
180
0
        query_mem_tracker->enable_print_log_usage();
181
0
    }
182
183
    // If enable reserve memory, not enable check limit, because reserve memory will check it.
184
    // If reserve enabled, even if the reserved memory size is smaller than the actual requested memory,
185
    // and the query memory consumption is larger than the limit, we do not expect the query to fail
186
    // after `check_limit` returns an error, but to run as long as possible,
187
    // and will enter the paused state and try to spill when the query reserves next time.
188
    // If the workload group or process runs out of memory, it will be forced to cancel.
189
122k
    query_mem_tracker->set_enable_check_limit(!(_query_options.__isset.enable_reserve_memory &&
190
122k
                                                _query_options.enable_reserve_memory));
191
122k
    _resource_ctx->memory_context()->set_mem_tracker(query_mem_tracker);
192
122k
}
193
194
122k
void QueryContext::_init_resource_context() {
195
122k
    _resource_ctx = ResourceContext::create_shared();
196
122k
    _init_query_mem_tracker();
197
122k
}
198
199
122k
void QueryContext::init_query_task_controller() {
200
122k
    _resource_ctx->set_task_controller(QueryTaskController::create(shared_from_this()));
201
122k
    _resource_ctx->task_controller()->set_task_id(_query_id);
202
122k
    _resource_ctx->task_controller()->set_fe_addr(current_connect_fe);
203
122k
    _resource_ctx->task_controller()->set_query_type(_query_options.query_type);
204
#ifndef BE_TEST
205
    _exec_env->runtime_query_statistics_mgr()->register_resource_context(print_id(_query_id),
206
                                                                         _resource_ctx);
207
#endif
208
122k
}
209
210
122k
QueryContext::~QueryContext() {
211
122k
    SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(query_mem_tracker());
212
    // query mem tracker consumption is equal to 0, it means that after QueryContext is created,
213
    // it is found that query already exists in _query_ctx_map, and query mem tracker is not used.
214
    // query mem tracker consumption is not equal to 0 after use, because there is memory consumed
215
    // on query mem tracker, released on other trackers.
216
122k
    std::string mem_tracker_msg;
217
122k
    if (query_mem_tracker()->peak_consumption() != 0) {
218
35
        mem_tracker_msg = fmt::format(
219
35
                "deregister query/load memory tracker, queryId={}, Limit={}, CurrUsed={}, "
220
35
                "PeakUsed={}",
221
35
                print_id(_query_id), PrettyPrinter::print_bytes(query_mem_tracker()->limit()),
222
35
                PrettyPrinter::print_bytes(query_mem_tracker()->consumption()),
223
35
                PrettyPrinter::print_bytes(query_mem_tracker()->peak_consumption()));
224
35
    }
225
122k
    _resource_ctx->task_controller()->finish();
226
227
122k
    if (enable_profile()) {
228
0
        _report_query_profile();
229
0
    }
230
231
122k
    _runtime_filter_mgr.reset();
232
122k
    _execution_dependency.reset();
233
122k
    _runtime_predicates.clear();
234
122k
    file_scan_range_params_map.clear();
235
122k
    obj_pool.clear();
236
122k
    _merge_controller_handler.reset();
237
238
122k
    DorisMetrics::instance()->query_ctx_cnt->increment(-1);
239
    // fragment_mgr is nullptr in unittest
240
122k
    if (ExecEnv::GetInstance()->fragment_mgr()) {
241
1
        ExecEnv::GetInstance()->fragment_mgr()->remove_query_context(this->_query_id);
242
1
    }
243
    // the only one msg shows query's end. any other msg should append to it if need.
244
122k
    LOG_INFO("Query {} deconstructed, mem_tracker: {}", print_id(this->_query_id), mem_tracker_msg);
245
122k
}
246
247
30
void QueryContext::set_ready_to_execute(Status reason) {
248
30
    set_execution_dependency_ready();
249
30
    _exec_status.update(reason);
250
30
}
251
252
0
void QueryContext::set_ready_to_execute_only() {
253
0
    set_execution_dependency_ready();
254
0
}
255
256
30
void QueryContext::set_execution_dependency_ready() {
257
30
    _execution_dependency->set_ready();
258
30
}
259
260
46
void QueryContext::set_memory_sufficient(bool sufficient) {
261
46
    if (sufficient) {
262
17
        {
263
17
            _memory_sufficient_dependency->set_ready();
264
17
            _resource_ctx->task_controller()->reset_paused_reason();
265
17
        }
266
29
    } else {
267
29
        _memory_sufficient_dependency->block();
268
29
        _resource_ctx->task_controller()->add_paused_count();
269
29
    }
270
46
}
271
272
30
void QueryContext::cancel(Status new_status, int fragment_id) {
273
30
    if (!_exec_status.update(new_status)) {
274
0
        return;
275
0
    }
276
    // Tasks should be always runnable.
277
30
    _execution_dependency->set_always_ready();
278
30
    _memory_sufficient_dependency->set_always_ready();
279
30
    if ((new_status.is<ErrorCode::MEM_LIMIT_EXCEEDED>() ||
280
30
         new_status.is<ErrorCode::MEM_ALLOC_FAILED>()) &&
281
30
        _query_options.__isset.dump_heap_profile_when_mem_limit_exceeded &&
282
30
        _query_options.dump_heap_profile_when_mem_limit_exceeded) {
283
        // if query is cancelled because of query mem limit exceeded, dump heap profile
284
        // at the time of cancellation can get the most accurate memory usage for problem analysis
285
0
        auto wg = workload_group();
286
0
        auto log_str = fmt::format(
287
0
                "Query {} canceled because of memory limit exceeded, dumping memory "
288
0
                "detail profiles. wg: {}. {}",
289
0
                print_id(_query_id), wg ? wg->debug_string() : "null",
290
0
                doris::ProcessProfile::instance()->memory_profile()->process_memory_detail_str());
291
0
        LOG_LONG_STRING(INFO, log_str);
292
0
        std::string dot = HeapProfiler::instance()->dump_heap_profile_to_dot();
293
0
        if (!dot.empty()) {
294
0
            dot += "\n-------------------------------------------------------\n";
295
0
            dot += "Copy the text after `digraph` in the above output to "
296
0
                   "http://www.webgraphviz.com to generate a dot graph.\n"
297
0
                   "after start heap profiler, if there is no operation, will print `No nodes "
298
0
                   "to "
299
0
                   "print`."
300
0
                   "If there are many errors: `addr2line: Dwarf Error`,"
301
0
                   "or other FAQ, reference doc: "
302
0
                   "https://doris.apache.org/community/developer-guide/debug-tool/#4-qa\n";
303
0
            auto nest_log_str =
304
0
                    fmt::format("Query {}, dump heap profile to dot: {}", print_id(_query_id), dot);
305
0
            LOG_LONG_STRING(INFO, nest_log_str);
306
0
        }
307
0
    }
308
309
30
    set_ready_to_execute(new_status);
310
30
    cancel_all_pipeline_context(new_status, fragment_id);
311
30
}
312
313
0
void QueryContext::set_load_error_url(std::string error_url) {
314
0
    std::lock_guard<std::mutex> lock(_error_url_lock);
315
0
    _load_error_url = error_url;
316
0
}
317
318
0
std::string QueryContext::get_load_error_url() {
319
0
    std::lock_guard<std::mutex> lock(_error_url_lock);
320
0
    return _load_error_url;
321
0
}
322
323
0
void QueryContext::set_first_error_msg(std::string error_msg) {
324
0
    std::lock_guard<std::mutex> lock(_error_url_lock);
325
0
    _first_error_msg = error_msg;
326
0
}
327
328
0
std::string QueryContext::get_first_error_msg() {
329
0
    std::lock_guard<std::mutex> lock(_error_url_lock);
330
0
    return _first_error_msg;
331
0
}
332
333
30
void QueryContext::cancel_all_pipeline_context(const Status& reason, int fragment_id) {
334
30
    std::vector<std::weak_ptr<PipelineFragmentContext>> ctx_to_cancel;
335
30
    {
336
30
        std::lock_guard<std::mutex> lock(_pipeline_map_write_lock);
337
30
        for (auto& [f_id, f_context] : _fragment_id_to_pipeline_ctx) {
338
0
            if (fragment_id == f_id) {
339
0
                continue;
340
0
            }
341
0
            ctx_to_cancel.push_back(f_context);
342
0
        }
343
30
    }
344
30
    for (auto& f_context : ctx_to_cancel) {
345
0
        if (auto pipeline_ctx = f_context.lock()) {
346
0
            pipeline_ctx->cancel(reason);
347
0
        }
348
0
    }
349
30
}
350
351
0
std::string QueryContext::print_all_pipeline_context() {
352
0
    std::vector<std::weak_ptr<PipelineFragmentContext>> ctx_to_print;
353
0
    fmt::memory_buffer debug_string_buffer;
354
0
    size_t i = 0;
355
0
    {
356
0
        fmt::format_to(debug_string_buffer, "{} pipeline fragment contexts in query {}. \n",
357
0
                       _fragment_id_to_pipeline_ctx.size(), print_id(_query_id));
358
359
0
        {
360
0
            std::lock_guard<std::mutex> lock(_pipeline_map_write_lock);
361
0
            for (auto& [f_id, f_context] : _fragment_id_to_pipeline_ctx) {
362
0
                ctx_to_print.push_back(f_context);
363
0
            }
364
0
        }
365
0
        for (auto& f_context : ctx_to_print) {
366
0
            if (auto pipeline_ctx = f_context.lock()) {
367
0
                auto elapsed = pipeline_ctx->elapsed_time() / 1000000000.0;
368
0
                fmt::format_to(debug_string_buffer,
369
0
                               "No.{} (elapse_second={}s, fragment_id={}) : {}\n", i, elapsed,
370
0
                               pipeline_ctx->get_fragment_id(), pipeline_ctx->debug_string());
371
0
                i++;
372
0
            }
373
0
        }
374
0
    }
375
0
    return fmt::to_string(debug_string_buffer);
376
0
}
377
378
void QueryContext::set_pipeline_context(const int fragment_id,
379
0
                                        std::shared_ptr<PipelineFragmentContext> pip_ctx) {
380
0
    std::lock_guard<std::mutex> lock(_pipeline_map_write_lock);
381
    // Use insert_or_assign instead of insert to support overwriting old entries
382
    // when recursive CTE recreates PipelineFragmentContext between rounds.
383
0
    _fragment_id_to_pipeline_ctx.insert_or_assign(fragment_id, pip_ctx);
384
0
}
385
386
32
doris::TaskScheduler* QueryContext::get_pipe_exec_scheduler() {
387
32
    if (!_task_scheduler) {
388
0
        throw Exception(Status::InternalError("task_scheduler is null"));
389
0
    }
390
32
    return _task_scheduler;
391
32
}
392
393
43
Status QueryContext::set_workload_group(WorkloadGroupPtr& wg) {
394
43
    _resource_ctx->set_workload_group(wg);
395
    // Should add query first, the workload group will not be deleted,
396
    // then visit workload group's resource
397
    // see task_group_manager::delete_workload_group_by_ids
398
43
    RETURN_IF_ERROR(workload_group()->add_resource_ctx(_query_id, _resource_ctx));
399
400
43
    workload_group()->get_query_scheduler(&_task_scheduler, &_scan_task_scheduler,
401
43
                                          &_remote_scan_task_scheduler);
402
43
    return Status::OK();
403
43
}
404
405
void QueryContext::add_fragment_profile(
406
        int fragment_id, const std::vector<std::shared_ptr<TRuntimeProfileTree>>& pipeline_profiles,
407
0
        std::shared_ptr<TRuntimeProfileTree> load_channel_profile) {
408
0
    if (pipeline_profiles.empty()) {
409
0
        std::string msg = fmt::format("Add pipeline profile failed, query {}, fragment {}",
410
0
                                      print_id(this->_query_id), fragment_id);
411
0
        LOG_ERROR(msg);
412
0
        DCHECK(false) << msg;
413
0
        return;
414
0
    }
415
416
0
#ifndef NDEBUG
417
0
    for (const auto& p : pipeline_profiles) {
418
0
        DCHECK(p != nullptr) << fmt::format("Add pipeline profile failed, query {}, fragment {}",
419
0
                                            print_id(this->_query_id), fragment_id);
420
0
    }
421
0
#endif
422
423
0
    std::lock_guard<std::mutex> l(_profile_mutex);
424
0
    VLOG_ROW << fmt::format(
425
0
            "Query add fragment profile, query {}, fragment {}, pipeline profile count {} ",
426
0
            print_id(this->_query_id), fragment_id, pipeline_profiles.size());
427
428
0
    _profile_map.insert(std::make_pair(fragment_id, pipeline_profiles));
429
430
0
    if (load_channel_profile != nullptr) {
431
0
        _load_channel_profile_map.insert(std::make_pair(fragment_id, load_channel_profile));
432
0
    }
433
0
}
434
435
0
void QueryContext::_report_query_profile() {
436
0
    std::lock_guard<std::mutex> lg(_profile_mutex);
437
438
0
    for (auto& [fragment_id, fragment_profile] : _profile_map) {
439
0
        std::shared_ptr<TRuntimeProfileTree> load_channel_profile = nullptr;
440
441
0
        if (_load_channel_profile_map.contains(fragment_id)) {
442
0
            load_channel_profile = _load_channel_profile_map[fragment_id];
443
0
        }
444
445
0
        ExecEnv::GetInstance()->runtime_query_statistics_mgr()->register_fragment_profile(
446
0
                _query_id, this->coord_addr, fragment_id, fragment_profile, load_channel_profile);
447
0
    }
448
449
0
    ExecEnv::GetInstance()->runtime_query_statistics_mgr()->trigger_profile_reporting();
450
0
}
451
452
std::unordered_map<int, std::vector<std::shared_ptr<TRuntimeProfileTree>>>
453
0
QueryContext::_collect_realtime_query_profile() {
454
0
    std::unordered_map<int, std::vector<std::shared_ptr<TRuntimeProfileTree>>> res;
455
0
    std::lock_guard<std::mutex> lock(_pipeline_map_write_lock);
456
0
    for (const auto& [fragment_id, fragment_ctx_wptr] : _fragment_id_to_pipeline_ctx) {
457
0
        if (auto fragment_ctx = fragment_ctx_wptr.lock()) {
458
0
            if (fragment_ctx == nullptr) {
459
0
                std::string msg =
460
0
                        fmt::format("PipelineFragmentContext is nullptr, query {} fragment_id: {}",
461
0
                                    print_id(_query_id), fragment_id);
462
0
                LOG_ERROR(msg);
463
0
                DCHECK(false) << msg;
464
0
                continue;
465
0
            }
466
467
0
            auto profile = fragment_ctx->collect_realtime_profile();
468
469
0
            if (profile.empty()) {
470
0
                std::string err_msg = fmt::format(
471
0
                        "Get nothing when collecting profile, query {}, fragment_id: {}",
472
0
                        print_id(_query_id), fragment_id);
473
0
                LOG_ERROR(err_msg);
474
0
                DCHECK(false) << err_msg;
475
0
                continue;
476
0
            }
477
478
0
            res.insert(std::make_pair(fragment_id, profile));
479
0
        }
480
0
    }
481
482
0
    return res;
483
0
}
484
485
0
TReportExecStatusParams QueryContext::get_realtime_exec_status() {
486
0
    TReportExecStatusParams exec_status;
487
488
0
    auto realtime_query_profile = _collect_realtime_query_profile();
489
0
    std::vector<std::shared_ptr<TRuntimeProfileTree>> load_channel_profiles;
490
491
0
    for (auto load_channel_profile : _load_channel_profile_map) {
492
0
        if (load_channel_profile.second != nullptr) {
493
0
            load_channel_profiles.push_back(load_channel_profile.second);
494
0
        }
495
0
    }
496
497
0
    exec_status = RuntimeQueryStatisticsMgr::create_report_exec_status_params(
498
0
            this->_query_id, std::move(realtime_query_profile), std::move(load_channel_profiles),
499
0
            /*is_done=*/false);
500
501
0
    return exec_status;
502
0
}
503
504
Status QueryContext::send_block_to_cte_scan(
505
        const TUniqueId& instance_id, int node_id,
506
0
        const google::protobuf::RepeatedPtrField<doris::PBlock>& pblocks, bool eos) {
507
0
    std::unique_lock<std::mutex> l(_cte_scan_lock);
508
0
    auto it = _cte_scan.find(std::make_pair(instance_id, node_id));
509
0
    if (it == _cte_scan.end()) {
510
0
        return Status::InternalError("RecCTEScan not found for instance {}, node {}",
511
0
                                     print_id(instance_id), node_id);
512
0
    }
513
0
    for (const auto& pblock : pblocks) {
514
0
        RETURN_IF_ERROR(it->second->add_block(pblock));
515
0
    }
516
0
    if (eos) {
517
0
        it->second->set_ready();
518
0
    }
519
0
    return Status::OK();
520
0
}
521
522
void QueryContext::registe_cte_scan(const TUniqueId& instance_id, int node_id,
523
0
                                    RecCTEScanLocalState* scan) {
524
0
    std::unique_lock<std::mutex> l(_cte_scan_lock);
525
0
    auto key = std::make_pair(instance_id, node_id);
526
0
    DCHECK(!_cte_scan.contains(key)) << "Duplicate registe cte scan for instance "
527
0
                                     << print_id(instance_id) << ", node " << node_id;
528
0
    _cte_scan.emplace(key, scan);
529
0
}
530
531
0
void QueryContext::deregiste_cte_scan(const TUniqueId& instance_id, int node_id) {
532
0
    std::lock_guard<std::mutex> l(_cte_scan_lock);
533
0
    auto key = std::make_pair(instance_id, node_id);
534
0
    DCHECK(_cte_scan.contains(key)) << "Duplicate deregiste cte scan for instance "
535
0
                                    << print_id(instance_id) << ", node " << node_id;
536
0
    _cte_scan.erase(key);
537
0
}
538
539
0
Status QueryContext::reset_global_rf(const google::protobuf::RepeatedField<int32_t>& filter_ids) {
540
0
    if (_merge_controller_handler) {
541
0
        return _merge_controller_handler->reset_global_rf(this, filter_ids);
542
0
    }
543
0
    return Status::OK();
544
0
}
545
546
} // namespace doris