Coverage Report

Created: 2026-04-15 12:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exec/sink/load_stream_map_pool.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
#include <brpc/controller.h>
20
#include <bthread/types.h>
21
#include <butil/errno.h>
22
#include <fmt/format.h>
23
#include <gen_cpp/PaloInternalService_types.h>
24
#include <gen_cpp/Types_types.h>
25
#include <gen_cpp/internal_service.pb.h>
26
#include <gen_cpp/types.pb.h>
27
#include <glog/logging.h>
28
#include <google/protobuf/stubs/callback.h>
29
#include <stddef.h>
30
#include <stdint.h>
31
32
#include <atomic>
33
// IWYU pragma: no_include <bits/chrono.h>
34
#include <chrono> // IWYU pragma: keep
35
#include <functional>
36
#include <initializer_list>
37
#include <map>
38
#include <memory>
39
#include <mutex>
40
#include <ostream>
41
#include <queue>
42
#include <set>
43
#include <string>
44
#include <unordered_map>
45
#include <unordered_set>
46
#include <utility>
47
#include <vector>
48
49
#include "common/config.h"
50
#include "common/status.h"
51
#include "core/allocator.h"
52
#include "core/block/block.h"
53
#include "core/column/column.h"
54
#include "core/data_type/data_type.h"
55
#include "exec/sink/load_stream_stub.h"
56
#include "exprs/vexpr_fwd.h"
57
#include "runtime/exec_env.h"
58
#include "runtime/memory/mem_tracker.h"
59
#include "runtime/runtime_profile.h"
60
#include "runtime/thread_context.h"
61
#include "storage/tablet_info.h"
62
#include "util/countdown_latch.h"
63
#include "util/stopwatch.hpp"
64
65
namespace doris {
66
67
class LoadStreamStub;
68
69
class LoadStreamMapPool;
70
71
class LoadStreamMap {
72
public:
73
    LoadStreamMap(UniqueId load_id, int64_t src_id, int num_streams, int num_use,
74
                  LoadStreamMapPool* pool);
75
76
    std::shared_ptr<LoadStreamStubs> get_or_create(int64_t dst_id, bool incremental = false);
77
78
    std::shared_ptr<LoadStreamStubs> at(int64_t dst_id);
79
80
    bool contains(int64_t dst_id);
81
82
    void for_each(std::function<void(int64_t, LoadStreamStubs&)> fn);
83
84
    Status for_each_st(std::function<Status(int64_t, LoadStreamStubs&)> fn);
85
86
    void save_tablets_to_commit(int64_t dst_id, const std::vector<PTabletID>& tablets_to_commit);
87
88
0
    void save_segments_for_tablet(const std::unordered_map<int64_t, int32_t>& segments_for_tablet) {
89
0
        _segments_for_tablet.insert(segments_for_tablet.cbegin(), segments_for_tablet.cend());
90
0
    }
91
92
    // Return true if the last instance is just released.
93
    bool release();
94
95
    // send CLOSE_LOAD to all streams, return ERROR if any.
96
    // only call this method after release() returns true.
97
    void close_load(bool incremental);
98
99
0
    std::unordered_map<int64_t, std::shared_ptr<LoadStreamStubs>> get_streams_for_node() {
100
0
        decltype(_streams_for_node) snapshot;
101
0
        {
102
0
            std::lock_guard<std::mutex> lock(_mutex);
103
0
            snapshot = _streams_for_node;
104
0
        }
105
0
        return snapshot;
106
0
    }
107
108
private:
109
    const UniqueId _load_id;
110
    const int64_t _src_id;
111
    const int _num_streams;
112
    std::atomic<int> _use_cnt;
113
    std::atomic<int> _num_incremental_streams;
114
    std::mutex _mutex;
115
    std::unordered_map<int64_t, std::shared_ptr<LoadStreamStubs>> _streams_for_node;
116
    LoadStreamMapPool* _pool = nullptr;
117
    std::shared_ptr<IndexToTabletSchema> _tablet_schema_for_index;
118
    std::shared_ptr<IndexToEnableMoW> _enable_unique_mow_for_index;
119
120
    std::mutex _tablets_to_commit_mutex;
121
    std::unordered_map<int64_t, std::unordered_map<int64_t, PTabletID>> _tablets_to_commit;
122
    std::unordered_map<int64_t, int32_t> _segments_for_tablet;
123
};
124
125
class LoadStreamMapPool {
126
public:
127
    LoadStreamMapPool();
128
129
    ~LoadStreamMapPool();
130
131
    std::shared_ptr<LoadStreamMap> get_or_create(UniqueId load_id, int64_t src_id, int num_streams,
132
                                                 int num_use);
133
134
    void erase(UniqueId load_id);
135
136
5
    size_t size() {
137
5
        std::lock_guard<std::mutex> lock(_mutex);
138
5
        return _pool.size();
139
5
    }
140
141
private:
142
    std::mutex _mutex;
143
    std::unordered_map<UniqueId, std::shared_ptr<LoadStreamMap>> _pool;
144
};
145
146
} // namespace doris