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