Coverage Report

Created: 2026-10-12 09:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/load/channel/load_channel_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 <gen_cpp/internal_service.pb.h>
21
#include <stdint.h>
22
23
#include <condition_variable>
24
#include <functional>
25
#include <memory>
26
#include <mutex>
27
#include <string>
28
#include <unordered_map>
29
#include <utility>
30
31
#include "common/compiler_util.h" // IWYU pragma: keep
32
#include "common/status.h"
33
#include "load/channel/load_channel.h"
34
#include "load/memtable/memtable_memory_limiter.h"
35
#include "runtime/memory/lru_cache_policy.h"
36
#include "runtime/memory/mem_tracker_limiter.h"
37
#include "runtime/thread_context.h"
38
#include "util/countdown_latch.h"
39
#include "util/lru_cache.h"
40
#include "util/uid_util.h"
41
42
namespace doris {
43
44
class PTabletWriterCancelRequest;
45
class EosCompletion;
46
class PTabletWriterOpenRequest;
47
class Thread;
48
49
// LoadChannelMgr -> LoadChannel -> TabletsChannel -> DeltaWriter
50
// All dispatched load data for this backend is routed from this class
51
class LoadChannelMgr {
52
public:
53
    LoadChannelMgr();
54
55
    Status init(int64_t process_mem_limit);
56
57
    // open a new load channel if not exist
58
    Status open(const PTabletWriterOpenRequest& request);
59
60
    // For hang_wait EOS, return a barrier handle to the service, which transfers
61
    // the RPC completion after finishing synchronous response writes.
62
    Status add_batch(const PTabletWriterAddBlockRequest& request,
63
                     PTabletWriterAddBlockResult* response,
64
                     std::shared_ptr<EosCompletion>* eos_completion);
65
66
    // cancel all tablet stream for 'load_id' load
67
    Status cancel(const PTabletWriterCancelRequest& request);
68
69
    void stop();
70
71
2
    std::vector<std::string> get_all_load_channel_ids() {
72
2
        std::vector<std::string> result;
73
2
        std::lock_guard<std::mutex> lock(_lock);
74
75
2
        for (auto& [id, _] : _load_channels) {
76
0
            result.push_back(id.to_string());
77
0
        }
78
2
        return result;
79
2
    }
80
81
private:
82
    Status _get_load_channel(std::shared_ptr<LoadChannel>& channel, bool& is_eof,
83
                             const UniqueId& load_id, const PTabletWriterAddBlockRequest& request);
84
85
    void _finish_load_channel(UniqueId load_id);
86
87
    Status _start_bg_worker();
88
89
    class LoadStateChannelCache : public LRUCachePolicy {
90
    public:
91
        class CacheValue : public LRUCacheValueBase {
92
        public:
93
            std::string _cancel_reason;
94
        };
95
96
        LoadStateChannelCache(size_t capacity)
97
7
                : LRUCachePolicy(CachePolicy::CacheType::LOAD_STATE_CHANNEL_CACHE, capacity,
98
7
                                 LRUCacheType::NUMBER, /*sweep time*/ -1, /*num shards*/ 32,
99
7
                                 /*element capacity*/ 0, /*enable prune */ false,
100
7
                                 /*is lru k*/ false) {}
101
    };
102
103
    using CacheValue = LoadStateChannelCache::CacheValue;
104
105
protected:
106
    // lock protect the load channel map
107
    std::mutex _lock;
108
    // load id -> load channel
109
    std::unordered_map<UniqueId, std::shared_ptr<LoadChannel>> _load_channels;
110
    // load id window, remember the recently initiated load id, regardless of whether they succeed or fail
111
    std::unique_ptr<LoadStateChannelCache> _load_state_channels;
112
113
    MemTableMemoryLimiter* _memtable_memory_limiter = nullptr;
114
115
    CountDownLatch _stop_background_threads_latch;
116
    // thread to clean timeout load channels
117
    std::shared_ptr<Thread> _load_channels_clean_thread;
118
    Status _start_load_channels_clean();
119
};
120
121
} // namespace doris