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 <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 "core/custom_allocator.h" |
34 | | #include "exec/sink/vtablet_finder.h" |
35 | | #include "load/channel/adaptive_random_bucket_state.h" |
36 | | #include "runtime/runtime_profile.h" |
37 | | #include "util/bitmap.h" |
38 | | #include "util/uid_util.h" |
39 | | |
40 | | namespace google::protobuf { |
41 | | template <typename Element> |
42 | | class RepeatedField; |
43 | | template <typename Key, typename T> |
44 | | class Map; |
45 | | template <typename T> |
46 | | class RepeatedPtrField; |
47 | | } // namespace google::protobuf |
48 | | |
49 | | namespace doris { |
50 | | class PSlaveTabletNodes; |
51 | | class PSuccessSlaveTabletNodeIds; |
52 | | class PTabletError; |
53 | | class PTabletInfo; |
54 | | class PTabletWriterOpenRequest; |
55 | | class PTabletWriterOpenResult; |
56 | | class PTabletWriterAddBlockRequest; |
57 | | class PTabletWriterAddBlockResult; |
58 | | class PUniqueId; |
59 | | class TupleDescriptor; |
60 | | class OpenPartitionRequest; |
61 | | class StorageEngine; |
62 | | |
63 | | struct TabletsChannelKey { |
64 | | UniqueId id; |
65 | | int64_t index_id; |
66 | | |
67 | 0 | TabletsChannelKey(const PUniqueId& pid, int64_t index_id_) : id(pid), index_id(index_id_) {} |
68 | | |
69 | 0 | ~TabletsChannelKey() noexcept = default; |
70 | | |
71 | 0 | bool operator==(const TabletsChannelKey& rhs) const noexcept { |
72 | 0 | return index_id == rhs.index_id && id == rhs.id; |
73 | 0 | } |
74 | | |
75 | | std::string to_string() const; |
76 | | }; |
77 | | |
78 | | std::ostream& operator<<(std::ostream& os, const TabletsChannelKey& key); |
79 | | |
80 | | class BaseDeltaWriter; |
81 | | class MemTableWriter; |
82 | | class OlapTableSchemaParam; |
83 | | class LoadChannel; |
84 | | struct WriteRequest; |
85 | | |
86 | | // Write channel for a particular (load, index). |
87 | | class BaseTabletsChannel { |
88 | | public: |
89 | | BaseTabletsChannel(const TabletsChannelKey& key, const UniqueId& load_id, bool is_high_priority, |
90 | | RuntimeProfile* profile); |
91 | | |
92 | | virtual ~BaseTabletsChannel(); |
93 | | |
94 | | Status open(const PTabletWriterOpenRequest& request); |
95 | | // open + open writers |
96 | | Status incremental_open(const PTabletWriterOpenRequest& params); |
97 | | |
98 | | virtual std::unique_ptr<BaseDeltaWriter> create_delta_writer(const 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 | 0 | size_t total_received_rows() const { return _total_received_rows; } |
117 | | |
118 | 0 | 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 _init_adaptive_random_bucket_state(const PTabletWriterOpenRequest& request); |
127 | | Status _write_block_data(const PTabletWriterAddBlockRequest& request, int64_t cur_seq, |
128 | | std::unordered_map<int64_t, DorisVector<uint32_t>>& tablet_to_rowidxs, |
129 | | PTabletWriterAddBlockResult* response); |
130 | | Status _write_block_data_for_adaptive_random_bucket( |
131 | | const PTabletWriterAddBlockRequest& request, int64_t cur_seq, |
132 | | std::unordered_map<int64_t, DorisVector<uint32_t>>& partition_to_rowidxs, |
133 | | PTabletWriterAddBlockResult* response); |
134 | | virtual Status _prepare_adaptive_random_bucket_writer(BaseDeltaWriter* writer); |
135 | | Status _build_partition_to_rowidxs_for_adaptive_random_bucket( |
136 | | const PTabletWriterAddBlockRequest& request, |
137 | | std::unordered_map<int64_t, DorisVector<uint32_t>>* partition_to_rowidxs); |
138 | | std::shared_ptr<std::mutex> _get_partition_route_lock(int64_t partition_id); |
139 | | |
140 | | Status _get_current_seq(int64_t& cur_seq, const PTabletWriterAddBlockRequest& request); |
141 | | |
142 | | // open all writer |
143 | | Status _open_all_writers(const PTabletWriterOpenRequest& request); |
144 | | |
145 | | void _add_broken_tablet(int64_t tablet_id); |
146 | | // thread-unsafe, add a shared lock for `_tablet_writers_lock` if needed |
147 | | bool _is_broken_tablet(int64_t tablet_id) const; |
148 | | void _add_error_tablet(google::protobuf::RepeatedPtrField<PTabletError>* tablet_errors, |
149 | | int64_t tablet_id, Status error) const; |
150 | | void _build_tablet_to_rowidxs( |
151 | | const PTabletWriterAddBlockRequest& request, |
152 | | std::unordered_map<int64_t /* tablet_id */, DorisVector<uint32_t> /* row index */>* |
153 | | tablet_to_rowidxs); |
154 | | virtual void _init_profile(RuntimeProfile* profile); |
155 | | |
156 | | // id of this load channel |
157 | | TabletsChannelKey _key; |
158 | | |
159 | | // protect _state change. open and close. when add_batch finished, lock to change _next_seqs also |
160 | | std::mutex _lock; |
161 | | enum State { |
162 | | kInitialized, |
163 | | kOpened, |
164 | | kFinished // closed or cancelled |
165 | | }; |
166 | | State _state; |
167 | | |
168 | | UniqueId _load_id; |
169 | | |
170 | | // initialized in open function |
171 | | int64_t _txn_id = -1; |
172 | | int64_t _index_id = -1; |
173 | | std::shared_ptr<OlapTableSchemaParam> _schema; |
174 | | TupleDescriptor* _tuple_desc = nullptr; |
175 | | bool _open_by_incremental = false; |
176 | | |
177 | | // next sequence we expect |
178 | | std::set<int32_t> _recieved_senders; |
179 | | int _num_remaining_senders = 0; |
180 | | std::vector<int64_t> _next_seqs; |
181 | | Bitmap _closed_senders; |
182 | | // status to return when operate on an already closed/cancelled channel |
183 | | // currently it's OK. |
184 | | Status _close_status; |
185 | | |
186 | | // tablet_id -> TabletChannel. it will only be changed in open() or inc_open() |
187 | | std::unordered_map<int64_t, std::unique_ptr<BaseDeltaWriter>> _tablet_writers; |
188 | | // protect _tablet_writers |
189 | | std::mutex _tablet_writers_lock; |
190 | | // broken tablet ids. |
191 | | // If a tablet write fails, it's id will be added to this set. |
192 | | // So that following batch will not handle this tablet anymore. |
193 | | std::unordered_set<int64_t> _broken_tablets; |
194 | | |
195 | | std::shared_mutex _broken_tablets_lock; |
196 | | |
197 | | std::unordered_set<int64_t> _reducing_tablets; |
198 | | |
199 | | std::unordered_set<int64_t> _partition_ids; |
200 | | std::shared_ptr<AdaptiveRandomBucketState> _adaptive_random_bucket_state; |
201 | | // Protects the route-lock map. Each entry serializes current-tablet selection, write, |
202 | | // and rotation for one partition so all senders on this BE share one current bucket. |
203 | | std::mutex _partition_route_locks_lock; |
204 | | std::unordered_map<int64_t, std::shared_ptr<std::mutex>> _partition_route_locks; |
205 | | |
206 | | static std::atomic<uint64_t> _s_tablet_writer_count; |
207 | | |
208 | | bool _is_high_priority = false; |
209 | | |
210 | | RuntimeProfile* _profile = nullptr; |
211 | | RuntimeProfile::Counter* _add_batch_number_counter = nullptr; |
212 | | RuntimeProfile::HighWaterMarkCounter* _memory_usage_counter = nullptr; |
213 | | RuntimeProfile::HighWaterMarkCounter* _write_memory_usage_counter = nullptr; |
214 | | RuntimeProfile::HighWaterMarkCounter* _flush_memory_usage_counter = nullptr; |
215 | | RuntimeProfile::HighWaterMarkCounter* _max_tablet_memory_usage_counter = nullptr; |
216 | | RuntimeProfile::HighWaterMarkCounter* _max_tablet_write_memory_usage_counter = nullptr; |
217 | | RuntimeProfile::HighWaterMarkCounter* _max_tablet_flush_memory_usage_counter = nullptr; |
218 | | RuntimeProfile::Counter* _add_batch_timer = nullptr; |
219 | | RuntimeProfile::Counter* _write_block_timer = nullptr; |
220 | | RuntimeProfile::Counter* _incremental_open_timer = nullptr; |
221 | | |
222 | | // record rows received and filtered |
223 | | size_t _total_received_rows = 0; |
224 | | size_t _num_rows_filtered = 0; |
225 | | }; |
226 | | |
227 | | class DeltaWriter; |
228 | | |
229 | | // `StorageEngine` mixin for `BaseTabletsChannel` |
230 | | class TabletsChannel final : public BaseTabletsChannel { |
231 | | public: |
232 | | TabletsChannel(StorageEngine& engine, const TabletsChannelKey& key, const UniqueId& load_id, |
233 | | bool is_high_priority, RuntimeProfile* profile); |
234 | | |
235 | | ~TabletsChannel() override; |
236 | | |
237 | | std::unique_ptr<BaseDeltaWriter> create_delta_writer(const WriteRequest& request) override; |
238 | | |
239 | | Status add_batch(const PTabletWriterAddBlockRequest& request, |
240 | | PTabletWriterAddBlockResult* response) override; |
241 | | |
242 | | Status close(LoadChannel* parent, const PTabletWriterAddBlockRequest& req, |
243 | | PTabletWriterAddBlockResult* res, bool* finished) override; |
244 | | |
245 | | Status cancel() override; |
246 | | |
247 | | private: |
248 | | void _init_profile(RuntimeProfile* profile) override; |
249 | | |
250 | | // deal with DeltaWriter commit_txn(), add tablet to list for return. |
251 | | void _commit_txn(DeltaWriter* writer, const PTabletWriterAddBlockRequest& req, |
252 | | PTabletWriterAddBlockResult* res); |
253 | | |
254 | | StorageEngine& _engine; |
255 | | bool _write_single_replica = false; |
256 | | RuntimeProfile::Counter* _slave_replica_timer = nullptr; |
257 | | }; |
258 | | |
259 | | } // namespace doris |