Coverage Report

Created: 2026-03-12 17:06

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