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