Coverage Report

Created: 2026-03-14 20:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/runtime/result_buffer_mgr.h
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
#pragma once
19
20
#include <cctz/time_zone.h>
21
#include <gen_cpp/Types_types.h>
22
#include <gen_cpp/segment_v2.pb.h>
23
24
#include <ctime>
25
#include <map>
26
#include <memory>
27
#include <mutex>
28
#include <shared_mutex>
29
#include <unordered_map>
30
#include <vector>
31
32
#include "common/status.h"
33
#include "exec/sink/writer/varrow_flight_result_writer.h"
34
#include "util/countdown_latch.h"
35
#include "util/hash_util.hpp"
36
37
namespace arrow {
38
class RecordBatch;
39
class Schema;
40
} // namespace arrow
41
42
namespace doris {
43
44
class ResultBlockBufferBase;
45
class PUniqueId;
46
class RuntimeState;
47
class MemTrackerLimiter;
48
class Thread;
49
class GetArrowResultBatchCtx;
50
class GetResultBatchCtx;
51
class Block;
52
53
// manage all result buffer control block in one backend. here we use uniform id `unique_id` to identify buffer slots.
54
// for parallel result sink, it's `instance_id`, for non-parallel sink, it's `query_id`.
55
class ResultBufferMgr {
56
public:
57
    ResultBufferMgr();
58
6
    ~ResultBufferMgr() = default;
59
    // init Result Buffer Mgr, start cancel thread
60
    Status init();
61
62
    void stop();
63
64
    // for non-parallel sink, use query_id. but for parallel sink, use instance id.
65
    Status create_sender(const TUniqueId& unique_id, int buffer_size,
66
                         std::shared_ptr<ResultBlockBufferBase>* sender, RuntimeState* state,
67
                         bool arrow_flight, std::shared_ptr<arrow::Schema> schema = nullptr);
68
69
    template <typename ResultBlockBufferType>
70
    Status find_buffer(const TUniqueId& unique_id, std::shared_ptr<ResultBlockBufferType>& buffer);
71
    // cancel
72
    bool cancel(const TUniqueId& unique_id, const Status& reason);
73
74
    // cancel one query at a future time.
75
    void cancel_at_time(time_t cancel_time, const TUniqueId& unique_id);
76
77
private:
78
    using BufferMap = std::unordered_map<TUniqueId, std::shared_ptr<ResultBlockBufferBase>>;
79
    using TimeoutMap = std::map<time_t, std::vector<TUniqueId>>;
80
81
    template <typename ResultBlockBufferType>
82
    std::shared_ptr<ResultBlockBufferType> _find_control_block(const TUniqueId& unique_id);
83
84
    // used to erase the buffer that fe not clears
85
    // when fe crush, this thread clear the buffer avoid memory leak in this backend
86
    void cancel_thread();
87
88
    // lock for buffer map
89
    std::shared_mutex _buffer_map_lock;
90
    // buffer block map
91
    BufferMap _buffer_map;
92
93
    // lock for timeout map
94
    std::mutex _timeout_lock;
95
96
    // map (cancel_time : query to be cancelled),
97
    // cancel time maybe equal, so use one list
98
    TimeoutMap _timeout_map;
99
100
    CountDownLatch _stop_background_threads_latch;
101
    std::shared_ptr<Thread> _clean_thread;
102
};
103
104
} // namespace doris