/root/doris/be/src/olap/memtable_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 <cstdint> |
26 | | #include <memory> |
27 | | #include <mutex> |
28 | | #include <shared_mutex> |
29 | | #include <unordered_set> |
30 | | #include <vector> |
31 | | |
32 | | #include "common/status.h" |
33 | | #include "olap/delta_writer_context.h" |
34 | | #include "olap/memtable.h" |
35 | | #include "olap/olap_common.h" |
36 | | #include "olap/partial_update_info.h" |
37 | | #include "olap/rowset/rowset.h" |
38 | | #include "olap/tablet.h" |
39 | | #include "olap/tablet_meta.h" |
40 | | #include "olap/tablet_schema.h" |
41 | | #include "util/spinlock.h" |
42 | | #include "util/uid_util.h" |
43 | | |
44 | | namespace doris { |
45 | | |
46 | | class FlushToken; |
47 | | class MemTable; |
48 | | class MemTracker; |
49 | | class StorageEngine; |
50 | | class TupleDescriptor; |
51 | | class SlotDescriptor; |
52 | | class OlapTableSchemaParam; |
53 | | class RowsetWriter; |
54 | | struct FlushStatistic; |
55 | | class WorkloadGroup; |
56 | | |
57 | | namespace vectorized { |
58 | | class Block; |
59 | | } // namespace vectorized |
60 | | |
61 | | // Writer for a particular (load, index, tablet). |
62 | | // This class is NOT thread-safe, external synchronization is required. |
63 | | class MemTableWriter { |
64 | | public: |
65 | | MemTableWriter(const WriteRequest& req); |
66 | | |
67 | | ~MemTableWriter(); |
68 | | |
69 | | Status init(std::shared_ptr<RowsetWriter> rowset_writer, TabletSchemaSPtr tablet_schema, |
70 | | std::shared_ptr<PartialUpdateInfo> partial_update_info, |
71 | | std::shared_ptr<WorkloadGroup> wg_sptr, bool unique_key_mow = false); |
72 | | |
73 | | Status write(const vectorized::Block* block, const std::vector<uint32_t>& row_idxs); |
74 | | |
75 | | // flush the last memtable to flush queue, must call it before close_wait() |
76 | | Status close(); |
77 | | // wait for all memtables to be flushed, update profiles if provided. |
78 | | // mem_consumption() should be 0 after this function returns. |
79 | 12 | Status close_wait(RuntimeProfile* profile = nullptr) { |
80 | 12 | RETURN_IF_ERROR(_do_close_wait()); |
81 | 12 | if (profile != nullptr) { |
82 | 12 | _update_profile(profile); |
83 | 12 | } |
84 | 12 | return Status::OK(); |
85 | 12 | } |
86 | | |
87 | | // abandon current memtable and wait for all pending-flushing memtables to be destructed. |
88 | | // mem_consumption() should be 0 after this function returns. |
89 | | Status cancel(); |
90 | | Status cancel_with_status(const Status& st); |
91 | | |
92 | | int64_t mem_consumption(MemType mem); |
93 | | int64_t active_memtable_mem_consumption(); |
94 | | |
95 | | // Submit current memtable to flush queue, and return without waiting. |
96 | | // This is currently for reducing mem consumption of this memtable writer. |
97 | | Status flush_async(); |
98 | | |
99 | | // Wait all memtable in flush queue to be flushed |
100 | | Status wait_flush(); |
101 | | |
102 | 0 | int64_t tablet_id() const { return _req.tablet_id; } |
103 | | |
104 | 0 | int64_t total_received_rows() const { return _total_received_rows; } |
105 | | |
106 | | const FlushStatistic& get_flush_token_stats(); |
107 | | |
108 | | uint64_t flush_running_count() const; |
109 | | |
110 | | private: |
111 | | // push a full memtable to flush executor |
112 | | Status _flush_memtable_async(); |
113 | | |
114 | | void _reset_mem_table(); |
115 | | |
116 | | Status _do_close_wait(); |
117 | | void _update_profile(RuntimeProfile* profile); |
118 | | |
119 | | std::atomic<bool> _is_init = false; |
120 | | bool _is_cancelled = false; |
121 | | bool _is_closed = false; |
122 | | Status _cancel_status; |
123 | | WriteRequest _req; |
124 | | std::shared_ptr<RowsetWriter> _rowset_writer; |
125 | | std::shared_ptr<MemTable> _mem_table; |
126 | | TabletSchemaSPtr _tablet_schema; |
127 | | bool _unique_key_mow = false; |
128 | | |
129 | | // This variable is accessed from writer thread and token flush thread |
130 | | // use a shared ptr to avoid use after free problem. |
131 | | std::shared_ptr<FlushToken> _flush_token; |
132 | | // Save the not active memtable that is in flush queue or under flushing. |
133 | | std::vector<std::weak_ptr<MemTable>> _freezed_mem_tables; |
134 | | // The lock to protect _memtable and _freezed_mem_tables structure to avoid concurrency modification or read |
135 | | SpinLock _mem_table_ptr_lock; |
136 | | QueryThreadContext _query_thread_context; |
137 | | |
138 | | std::mutex _lock; |
139 | | |
140 | | // total rows num written by MemTableWriter |
141 | | std::atomic<int64_t> _total_received_rows = 0; |
142 | | int64_t _wait_flush_time_ns = 0; |
143 | | int64_t _close_wait_time_ns = 0; |
144 | | int64_t _segment_num = 0; |
145 | | |
146 | | MonotonicStopWatch _lock_watch; |
147 | | |
148 | | std::shared_ptr<PartialUpdateInfo> _partial_update_info; |
149 | | }; |
150 | | |
151 | | } // namespace doris |