Coverage Report

Created: 2026-11-02 16:10

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