Coverage Report

Created: 2026-03-12 14:13

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 PTabletWriterOpenRequest;
46
class Thread;
47
48
// LoadChannelMgr -> LoadChannel -> TabletsChannel -> DeltaWriter
49
// All dispatched load data for this backend is routed from this class
50
class LoadChannelMgr {
51
public:
52
    LoadChannelMgr();
53
54
    Status init(int64_t process_mem_limit);
55
56
    // open a new load channel if not exist
57
    Status open(const PTabletWriterOpenRequest& request);
58
59
    Status add_batch(const PTabletWriterAddBlockRequest& request,
60
                     PTabletWriterAddBlockResult* response);
61
62
    // cancel all tablet stream for 'load_id' load
63
    Status cancel(const PTabletWriterCancelRequest& request);
64
65
    void stop();
66
67
0
    std::vector<std::string> get_all_load_channel_ids() {
68
0
        std::vector<std::string> result;
69
0
        std::lock_guard<std::mutex> lock(_lock);
70
71
0
        for (auto& [id, _] : _load_channels) {
72
0
            result.push_back(id.to_string());
73
0
        }
74
0
        return result;
75
0
    }
76
77
private:
78
    Status _get_load_channel(std::shared_ptr<LoadChannel>& channel, bool& is_eof,
79
                             const UniqueId& load_id, const PTabletWriterAddBlockRequest& request);
80
81
    void _finish_load_channel(UniqueId load_id);
82
83
    Status _start_bg_worker();
84
85
    class LoadStateChannelCache : public LRUCachePolicy {
86
    public:
87
        class CacheValue : public LRUCacheValueBase {
88
        public:
89
            std::string _cancel_reason;
90
        };
91
92
        LoadStateChannelCache(size_t capacity)
93
7
                : LRUCachePolicy(CachePolicy::CacheType::LOAD_STATE_CHANNEL_CACHE, capacity,
94
7
                                 LRUCacheType::NUMBER, /*sweep time*/ -1, /*num shards*/ 32,
95
7
                                 /*element capacity*/ 0, /*enable prune */ false,
96
7
                                 /*is lru k*/ false) {}
97
    };
98
99
    using CacheValue = LoadStateChannelCache::CacheValue;
100
101
protected:
102
    // lock protect the load channel map
103
    std::mutex _lock;
104
    // load id -> load channel
105
    std::unordered_map<UniqueId, std::shared_ptr<LoadChannel>> _load_channels;
106
    // load id window, remember the recently initiated load id, regardless of whether they succeed or fail
107
    std::unique_ptr<LoadStateChannelCache> _load_state_channels;
108
109
    MemTableMemoryLimiter* _memtable_memory_limiter = nullptr;
110
111
    CountDownLatch _stop_background_threads_latch;
112
    // thread to clean timeout load channels
113
    std::shared_ptr<Thread> _load_channels_clean_thread;
114
    Status _start_load_channels_clean();
115
};
116
117
} // namespace doris