Coverage Report

Created: 2024-11-21 13:02

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