Coverage Report

Created: 2026-03-16 23:17

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
#include "common/compile_check_begin.h"
67
68
class LoadStreamStub;
69
70
class LoadStreamMapPool;
71
72
class LoadStreamMap {
73
public:
74
    LoadStreamMap(UniqueId load_id, int64_t src_id, int num_streams, int num_use,
75
                  LoadStreamMapPool* pool);
76
77
    std::shared_ptr<LoadStreamStubs> get_or_create(int64_t dst_id, bool incremental = false);
78
79
    std::shared_ptr<LoadStreamStubs> at(int64_t dst_id);
80
81
    bool contains(int64_t dst_id);
82
83
    void for_each(std::function<void(int64_t, LoadStreamStubs&)> fn);
84
85
    Status for_each_st(std::function<Status(int64_t, LoadStreamStubs&)> fn);
86
87
    void save_tablets_to_commit(int64_t dst_id, const std::vector<PTabletID>& tablets_to_commit);
88
89
0
    void save_segments_for_tablet(const std::unordered_map<int64_t, int32_t>& segments_for_tablet) {
90
0
        _segments_for_tablet.insert(segments_for_tablet.cbegin(), segments_for_tablet.cend());
91
0
    }
92
93
    // Return true if the last instance is just released.
94
    bool release();
95
96
    // send CLOSE_LOAD to all streams, return ERROR if any.
97
    // only call this method after release() returns true.
98
    void close_load(bool incremental);
99
100
0
    std::unordered_map<int64_t, std::shared_ptr<LoadStreamStubs>> get_streams_for_node() {
101
0
        decltype(_streams_for_node) snapshot;
102
0
        {
103
0
            std::lock_guard<std::mutex> lock(_mutex);
104
0
            snapshot = _streams_for_node;
105
0
        }
106
0
        return snapshot;
107
0
    }
108
109
private:
110
    const UniqueId _load_id;
111
    const int64_t _src_id;
112
    const int _num_streams;
113
    std::atomic<int> _use_cnt;
114
    std::atomic<int> _num_incremental_streams;
115
    std::mutex _mutex;
116
    std::unordered_map<int64_t, std::shared_ptr<LoadStreamStubs>> _streams_for_node;
117
    LoadStreamMapPool* _pool = nullptr;
118
    std::shared_ptr<IndexToTabletSchema> _tablet_schema_for_index;
119
    std::shared_ptr<IndexToEnableMoW> _enable_unique_mow_for_index;
120
121
    std::mutex _tablets_to_commit_mutex;
122
    std::unordered_map<int64_t, std::unordered_map<int64_t, PTabletID>> _tablets_to_commit;
123
    std::unordered_map<int64_t, int32_t> _segments_for_tablet;
124
};
125
126
class LoadStreamMapPool {
127
public:
128
    LoadStreamMapPool();
129
130
    ~LoadStreamMapPool();
131
132
    std::shared_ptr<LoadStreamMap> get_or_create(UniqueId load_id, int64_t src_id, int num_streams,
133
                                                 int num_use);
134
135
    void erase(UniqueId load_id);
136
137
5
    size_t size() {
138
5
        std::lock_guard<std::mutex> lock(_mutex);
139
5
        return _pool.size();
140
5
    }
141
142
private:
143
    std::mutex _mutex;
144
    std::unordered_map<UniqueId, std::shared_ptr<LoadStreamMap>> _pool;
145
};
146
147
} // namespace doris
148
149
#include "common/compile_check_end.h"