Coverage Report

Created: 2025-03-10 22:58

/root/doris/be/src/runtime/tablets_channel.h
Line
Count
Source (jump to first uncovered line)
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 <glog/logging.h>
21
22
#include <atomic>
23
#include <cstdint>
24
#include <mutex>
25
#include <ostream>
26
#include <shared_mutex>
27
#include <string>
28
#include <unordered_map>
29
#include <unordered_set>
30
#include <vector>
31
32
#include "common/status.h"
33
#include "util/bitmap.h"
34
#include "util/runtime_profile.h"
35
#include "util/spinlock.h"
36
#include "util/uid_util.h"
37
#include "vec/common/custom_allocator.h"
38
39
namespace google::protobuf {
40
template <typename Element>
41
class RepeatedField;
42
template <typename Key, typename T>
43
class Map;
44
template <typename T>
45
class RepeatedPtrField;
46
} // namespace google::protobuf
47
48
namespace doris {
49
class PSlaveTabletNodes;
50
class PSuccessSlaveTabletNodeIds;
51
class PTabletError;
52
class PTabletInfo;
53
class PTabletWriterOpenRequest;
54
class PTabletWriterAddBlockRequest;
55
class PTabletWriterAddBlockResult;
56
class PUniqueId;
57
class TupleDescriptor;
58
class OpenPartitionRequest;
59
class StorageEngine;
60
61
struct TabletsChannelKey {
62
    UniqueId id;
63
    int64_t index_id;
64
65
0
    TabletsChannelKey(const PUniqueId& pid, int64_t index_id_) : id(pid), index_id(index_id_) {}
66
67
0
    ~TabletsChannelKey() noexcept = default;
68
69
0
    bool operator==(const TabletsChannelKey& rhs) const noexcept {
70
0
        return index_id == rhs.index_id && id == rhs.id;
71
0
    }
72
73
    std::string to_string() const;
74
};
75
76
std::ostream& operator<<(std::ostream& os, const TabletsChannelKey& key);
77
78
class BaseDeltaWriter;
79
class MemTableWriter;
80
class OlapTableSchemaParam;
81
class LoadChannel;
82
struct WriteRequest;
83
84
// Write channel for a particular (load, index).
85
class BaseTabletsChannel {
86
public:
87
    BaseTabletsChannel(const TabletsChannelKey& key, const UniqueId& load_id, bool is_high_priority,
88
                       RuntimeProfile* profile);
89
90
    virtual ~BaseTabletsChannel();
91
92
    Status open(const PTabletWriterOpenRequest& request);
93
    // open + open writers
94
    Status incremental_open(const PTabletWriterOpenRequest& params);
95
96
    virtual std::unique_ptr<BaseDeltaWriter> create_delta_writer(const WriteRequest& request) = 0;
97
98
    // no-op when this channel has been closed or cancelled
99
    virtual Status add_batch(const PTabletWriterAddBlockRequest& request,
100
                             PTabletWriterAddBlockResult* response) = 0;
101
102
    // Mark sender with 'sender_id' as closed.
103
    // If all senders are closed, close this channel, set '*finished' to true, update 'tablet_vec'
104
    // to include all tablets written in this channel.
105
    // no-op when this channel has been closed or cancelled
106
    virtual Status close(LoadChannel* parent, const PTabletWriterAddBlockRequest& req,
107
                         PTabletWriterAddBlockResult* res, bool* finished) = 0;
108
109
    // no-op when this channel has been closed or cancelled
110
    virtual Status cancel();
111
112
    void refresh_profile();
113
114
0
    size_t total_received_rows() const { return _total_received_rows; }
115
116
0
    size_t num_rows_filtered() const { return _num_rows_filtered; }
117
118
    // means this tablets in this BE is incremental opened partitions.
119
0
    bool is_incremental_channel() const { return _open_by_incremental; }
120
121
0
    bool is_finished() const { return _state == kFinished; }
122
123
protected:
124
    Status _write_block_data(const PTabletWriterAddBlockRequest& request, int64_t cur_seq,
125
                             std::unordered_map<int64_t, DorisVector<uint32_t>>& tablet_to_rowidxs,
126
                             PTabletWriterAddBlockResult* response);
127
128
    Status _get_current_seq(int64_t& cur_seq, const PTabletWriterAddBlockRequest& request);
129
130
    // open all writer
131
    Status _open_all_writers(const PTabletWriterOpenRequest& request);
132
133
    void _add_broken_tablet(int64_t tablet_id);
134
    // thread-unsafe, add a shared lock for `_tablet_writers_lock` if needed
135
    bool _is_broken_tablet(int64_t tablet_id) const;
136
    void _add_error_tablet(google::protobuf::RepeatedPtrField<PTabletError>* tablet_errors,
137
                           int64_t tablet_id, Status error) const;
138
    void _build_tablet_to_rowidxs(
139
            const PTabletWriterAddBlockRequest& request,
140
            std::unordered_map<int64_t /* tablet_id */, DorisVector<uint32_t> /* row index */>*
141
                    tablet_to_rowidxs);
142
    virtual void _init_profile(RuntimeProfile* profile);
143
144
    // id of this load channel
145
    TabletsChannelKey _key;
146
147
    // protect _state change. open and close. when add_batch finished, lock to change _next_seqs also
148
    std::mutex _lock;
149
    enum State {
150
        kInitialized,
151
        kOpened,
152
        kFinished // closed or cancelled
153
    };
154
    State _state;
155
156
    UniqueId _load_id;
157
158
    // initialized in open function
159
    int64_t _txn_id = -1;
160
    int64_t _index_id = -1;
161
    std::shared_ptr<OlapTableSchemaParam> _schema;
162
    TupleDescriptor* _tuple_desc = nullptr;
163
    bool _open_by_incremental = false;
164
165
    // next sequence we expect
166
    std::set<int32_t> _recieved_senders;
167
    int _num_remaining_senders = 0;
168
    std::vector<int64_t> _next_seqs;
169
    Bitmap _closed_senders;
170
    // status to return when operate on an already closed/cancelled channel
171
    // currently it's OK.
172
    Status _close_status;
173
174
    // tablet_id -> TabletChannel. it will only be changed in open() or inc_open()
175
    std::unordered_map<int64_t, std::unique_ptr<BaseDeltaWriter>> _tablet_writers;
176
    // protect _tablet_writers
177
    SpinLock _tablet_writers_lock;
178
    // broken tablet ids.
179
    // If a tablet write fails, it's id will be added to this set.
180
    // So that following batch will not handle this tablet anymore.
181
    std::unordered_set<int64_t> _broken_tablets;
182
183
    std::shared_mutex _broken_tablets_lock;
184
185
    std::unordered_set<int64_t> _reducing_tablets;
186
187
    std::unordered_set<int64_t> _partition_ids;
188
189
    static std::atomic<uint64_t> _s_tablet_writer_count;
190
191
    bool _is_high_priority = false;
192
193
    RuntimeProfile* _profile = nullptr;
194
    RuntimeProfile::Counter* _add_batch_number_counter = nullptr;
195
    RuntimeProfile::HighWaterMarkCounter* _memory_usage_counter = nullptr;
196
    RuntimeProfile::HighWaterMarkCounter* _write_memory_usage_counter = nullptr;
197
    RuntimeProfile::HighWaterMarkCounter* _flush_memory_usage_counter = nullptr;
198
    RuntimeProfile::HighWaterMarkCounter* _max_tablet_memory_usage_counter = nullptr;
199
    RuntimeProfile::HighWaterMarkCounter* _max_tablet_write_memory_usage_counter = nullptr;
200
    RuntimeProfile::HighWaterMarkCounter* _max_tablet_flush_memory_usage_counter = nullptr;
201
    RuntimeProfile::Counter* _add_batch_timer = nullptr;
202
    RuntimeProfile::Counter* _write_block_timer = nullptr;
203
    RuntimeProfile::Counter* _incremental_open_timer = nullptr;
204
205
    // record rows received and filtered
206
    size_t _total_received_rows = 0;
207
    size_t _num_rows_filtered = 0;
208
};
209
210
class DeltaWriter;
211
212
// `StorageEngine` mixin for `BaseTabletsChannel`
213
class TabletsChannel final : public BaseTabletsChannel {
214
public:
215
    TabletsChannel(StorageEngine& engine, const TabletsChannelKey& key, const UniqueId& load_id,
216
                   bool is_high_priority, RuntimeProfile* profile);
217
218
    ~TabletsChannel() override;
219
220
    std::unique_ptr<BaseDeltaWriter> create_delta_writer(const WriteRequest& request) override;
221
222
    Status add_batch(const PTabletWriterAddBlockRequest& request,
223
                     PTabletWriterAddBlockResult* response) override;
224
225
    Status close(LoadChannel* parent, const PTabletWriterAddBlockRequest& req,
226
                 PTabletWriterAddBlockResult* res, bool* finished) override;
227
228
    Status cancel() override;
229
230
private:
231
    void _init_profile(RuntimeProfile* profile) override;
232
233
    // deal with DeltaWriter commit_txn(), add tablet to list for return.
234
    void _commit_txn(DeltaWriter* writer, const PTabletWriterAddBlockRequest& req,
235
                     PTabletWriterAddBlockResult* res);
236
237
    StorageEngine& _engine;
238
    bool _write_single_replica = false;
239
    RuntimeProfile::Counter* _slave_replica_timer = nullptr;
240
};
241
242
} // namespace doris