/root/doris/be/src/olap/delta_writer.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 <gen_cpp/Types_types.h> |
21 | | #include <gen_cpp/internal_service.pb.h> |
22 | | #include <gen_cpp/types.pb.h> |
23 | | |
24 | | #include <atomic> |
25 | | #include <memory> |
26 | | #include <mutex> |
27 | | #include <shared_mutex> |
28 | | #include <unordered_set> |
29 | | #include <vector> |
30 | | |
31 | | #include "common/status.h" |
32 | | #include "olap/delta_writer_context.h" |
33 | | #include "olap/memtable_writer.h" |
34 | | #include "olap/olap_common.h" |
35 | | #include "olap/rowset/rowset.h" |
36 | | #include "olap/tablet.h" |
37 | | #include "olap/tablet_meta.h" |
38 | | #include "olap/tablet_schema.h" |
39 | | #include "util/spinlock.h" |
40 | | #include "util/uid_util.h" |
41 | | |
42 | | namespace doris { |
43 | | |
44 | | class FlushToken; |
45 | | class MemTable; |
46 | | class StorageEngine; |
47 | | class TupleDescriptor; |
48 | | class SlotDescriptor; |
49 | | class OlapTableSchemaParam; |
50 | | class RowsetWriter; |
51 | | |
52 | | namespace vectorized { |
53 | | class Block; |
54 | | } // namespace vectorized |
55 | | |
56 | | class BaseRowsetBuilder; |
57 | | |
58 | | // Writer for a particular (load, index, tablet). |
59 | | // This class is NOT thread-safe, external synchronization is required. |
60 | | class BaseDeltaWriter { |
61 | | public: |
62 | | BaseDeltaWriter(WriteRequest* req, RuntimeProfile* profile, const UniqueId& load_id); |
63 | | |
64 | | virtual ~BaseDeltaWriter(); |
65 | | |
66 | | Status init(); |
67 | | |
68 | | Status write(const vectorized::Block* block, const std::vector<uint32_t>& row_idxs); |
69 | | |
70 | | // flush the last memtable to flush queue, must call it before build_rowset() |
71 | | Status close(); |
72 | | // wait for all memtables to be flushed. |
73 | | // mem_consumption() should be 0 after this function returns. |
74 | | Status build_rowset(); |
75 | | Status submit_calc_delete_bitmap_task(); |
76 | | Status wait_calc_delete_bitmap(); |
77 | | |
78 | | // abandon current memtable and wait for all pending-flushing memtables to be destructed. |
79 | | // mem_consumption() should be 0 after this function returns. |
80 | | Status cancel(); |
81 | | Status cancel_with_status(const Status& st); |
82 | | |
83 | | int64_t mem_consumption(MemType mem); |
84 | | |
85 | | // Wait all memtable in flush queue to be flushed |
86 | | Status wait_flush(); |
87 | | |
88 | 0 | int64_t partition_id() const { return _req.partition_id; } |
89 | | |
90 | 0 | int64_t tablet_id() const { return _req.tablet_id; } |
91 | | |
92 | 0 | int64_t txn_id() const { return _req.txn_id; } |
93 | | |
94 | 0 | int64_t total_received_rows() const { return _memtable_writer->total_received_rows(); } |
95 | | |
96 | | int64_t num_rows_filtered() const; |
97 | | |
98 | | protected: |
99 | | virtual void _init_profile(RuntimeProfile* profile); |
100 | | |
101 | | bool _is_init = false; |
102 | | bool _is_cancelled = false; |
103 | | WriteRequest _req; |
104 | | std::unique_ptr<BaseRowsetBuilder> _rowset_builder; |
105 | | std::shared_ptr<MemTableWriter> _memtable_writer; |
106 | | |
107 | | std::mutex _lock; |
108 | | |
109 | | // total rows num written by DeltaWriter |
110 | | std::atomic<int64_t> _total_received_rows = 0; |
111 | | |
112 | | RuntimeProfile* _profile = nullptr; |
113 | | RuntimeProfile::Counter* _close_wait_timer = nullptr; |
114 | | RuntimeProfile::Counter* _wait_flush_limit_timer = nullptr; |
115 | | |
116 | | MonotonicStopWatch _lock_watch; |
117 | | }; |
118 | | |
119 | | // `StorageEngine` mixin for `BaseDeltaWriter` |
120 | | class DeltaWriter final : public BaseDeltaWriter { |
121 | | public: |
122 | | DeltaWriter(StorageEngine& engine, WriteRequest* req, RuntimeProfile* profile, |
123 | | const UniqueId& load_id); |
124 | | |
125 | | ~DeltaWriter() override; |
126 | | |
127 | | Status commit_txn(const PSlaveTabletNodes& slave_tablet_nodes); |
128 | | |
129 | | bool check_slave_replicas_done(google::protobuf::Map<int64_t, PSuccessSlaveTabletNodeIds>* |
130 | | success_slave_tablet_node_ids); |
131 | | |
132 | | void add_finished_slave_replicas(google::protobuf::Map<int64_t, PSuccessSlaveTabletNodeIds>* |
133 | | success_slave_tablet_node_ids); |
134 | | |
135 | | void finish_slave_tablet_pull_rowset(int64_t node_id, bool is_succeed); |
136 | | |
137 | | private: |
138 | | void _init_profile(RuntimeProfile* profile) override; |
139 | | |
140 | | void _request_slave_tablet_pull_rowset(PNodeInfo node_info); |
141 | | |
142 | | StorageEngine& _engine; |
143 | | std::unordered_set<int64_t> _unfinished_slave_node; |
144 | | PSuccessSlaveTabletNodeIds _success_slave_node_ids; |
145 | | std::shared_mutex _slave_node_lock; |
146 | | |
147 | | RuntimeProfile::Counter* _commit_txn_timer = nullptr; |
148 | | }; |
149 | | |
150 | | } // namespace doris |