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