be/src/storage/changelog.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 <limits> |
21 | | #include <map> |
22 | | #include <memory> |
23 | | #include <mutex> |
24 | | #include <string> |
25 | | #include <string_view> |
26 | | #include <utility> |
27 | | #include <vector> |
28 | | |
29 | | #include "common/logging.h" // DCHECK |
30 | | #include "common/status.h" |
31 | | #include "exec/sink/autoinc_buffer.h" |
32 | | #include "storage/changelog_types.h" |
33 | | #include "storage/olap_common.h" |
34 | | #include "storage/olap_define.h" // DataWriteType |
35 | | #include "storage/rowset/rowset_fwd.h" |
36 | | #include "storage/tablet/tablet_fwd.h" // BaseTabletSPtr |
37 | | #include "storage/tablet/tablet_schema.h" // TabletSchemaSPtr |
38 | | #include "storage/utils.h" // CHANGELOG_*_COL |
39 | | |
40 | | namespace doris { |
41 | | |
42 | | class AutoIncIDBuffer; |
43 | | class PChangelogWriteColumnMappings; |
44 | | class TChangelogWriteColumnMappings; |
45 | | struct PartialUpdateInfo; |
46 | | struct MowContext; |
47 | | |
48 | | namespace changelog { |
49 | | |
50 | | // A layout belongs to exactly these two pinned schemas. Writers share it for |
51 | | // their operation; no CID is stored in rowset metadata or reused after recovery. |
52 | | struct ResolvedChangelogLayout { |
53 | | TabletSchemaSPtr source_schema; |
54 | | TabletSchemaSPtr target_schema; |
55 | | std::vector<uint32_t> current_source_cids; |
56 | | std::vector<uint32_t> current_target_cids; |
57 | | std::vector<bool> current_nullable_widening; |
58 | | std::vector<uint32_t> before_source_cids; |
59 | | std::vector<uint32_t> before_target_cids; |
60 | | uint32_t tso_cid; |
61 | | uint32_t lsn_cid; |
62 | | uint32_t op_cid; |
63 | | bool need_historical_value; |
64 | | }; |
65 | | |
66 | | Status validate_mapping(const PChangelogWriteColumnMappings& mapping); |
67 | | Status mapping_from_thrift(const TChangelogWriteColumnMappings& input, |
68 | | PChangelogWriteColumnMappings* output); |
69 | | Status resolve_layout(const TabletSchemaSPtr& source, const TabletSchemaSPtr& target, |
70 | | const PChangelogWriteColumnMappings& mapping, |
71 | | std::shared_ptr<const ResolvedChangelogLayout>* result); |
72 | | |
73 | | // ATTN(changelog-rename): formerly "__BEFORE__<col>__" |
74 | 293 | inline std::string build_before_column_name(std::string_view name) { |
75 | 293 | std::string before_name = "__DORIS_CHANGELOG_BEFORE_"; |
76 | 293 | before_name.append(name.data(), name.size()); |
77 | 293 | before_name.append("__"); |
78 | 293 | return before_name; |
79 | 293 | } |
80 | | |
81 | | } // namespace changelog |
82 | | |
83 | | constexpr int64_t kChangelogLsnAutoIncId = -1; |
84 | | |
85 | | // Allocate per-row LSNs. |
86 | | // The caller must provide a valid auto-inc buffer (typically from GlobalAutoIncBuffers). |
87 | | inline Status allocate_lsn(const std::shared_ptr<AutoIncIDBuffer>& lsn_buffer, size_t num_rows, |
88 | 7 | std::vector<int64_t>& lsn_ids) { |
89 | 7 | if (lsn_buffer == nullptr) { |
90 | 0 | return Status::InternalError("try to get lsn buffer but null"); |
91 | 0 | } |
92 | 7 | DCHECK(num_rows > 0); |
93 | | |
94 | 7 | std::vector<std::pair<int64_t, size_t>> ranges; |
95 | 7 | RETURN_IF_ERROR(lsn_buffer->sync_request_ids(num_rows, &ranges)); |
96 | | |
97 | 7 | lsn_ids.clear(); |
98 | 7 | lsn_ids.reserve(num_rows); |
99 | 7 | for (const auto& [start, length] : ranges) { |
100 | 18 | for (size_t i = 0; i < length; ++i) { |
101 | 11 | DCHECK_LE(start, std::numeric_limits<int64_t>::max() - static_cast<int64_t>(i)); |
102 | 11 | lsn_ids.push_back(start + static_cast<int64_t>(i)); |
103 | 11 | } |
104 | 7 | } |
105 | 7 | DCHECK_EQ(lsn_ids.size(), num_rows); |
106 | 7 | return Status::OK(); |
107 | 7 | } |
108 | | |
109 | | constexpr int64_t kTsoLogicalBits = 18; |
110 | | |
111 | 29 | inline int64_t extract_tso_physical_time(int64_t tso) { |
112 | 29 | return tso <= 0 ? 0 : tso >> kTsoLogicalBits; |
113 | 29 | } |
114 | | |
115 | 28 | inline int64_t changelog_ttl_cutoff_tso(int64_t reference_tso, int64_t ttl_seconds) { |
116 | 28 | DCHECK_GT(reference_tso, 0); |
117 | 28 | DCHECK_GE(ttl_seconds, 0); |
118 | 28 | const int64_t reference_ms = extract_tso_physical_time(reference_tso); |
119 | 28 | if (ttl_seconds > reference_ms / 1000) { |
120 | 2 | return 0; |
121 | 2 | } |
122 | 26 | constexpr int64_t kMaxLogical = (1L << kTsoLogicalBits) - 1; |
123 | 26 | return ((reference_ms - ttl_seconds * 1000) << kTsoLogicalBits) | kMaxLogical; |
124 | 28 | } |
125 | | |
126 | | namespace segment_v2 { |
127 | | |
128 | | class SegmentAllocatedLsnMap { |
129 | | public: |
130 | 68 | void insert_segment_allocated_lsns(int64_t seg_id, ConstAllocatedLsnVectorSharedPtr lsn_ids) { |
131 | 68 | std::lock_guard<std::mutex> l(_mutex); |
132 | 68 | _seg_id_to_lsn_ids.emplace(seg_id, std::move(lsn_ids)); |
133 | 68 | } |
134 | | |
135 | 54 | void remove_segment(int64_t seg_id) { |
136 | 54 | std::lock_guard<std::mutex> l(_mutex); |
137 | 54 | _seg_id_to_lsn_ids.erase(seg_id); |
138 | 54 | } |
139 | | |
140 | 6 | bool contains_segment(int64_t seg_id) const { |
141 | 6 | std::lock_guard<std::mutex> l(_mutex); |
142 | 6 | return _seg_id_to_lsn_ids.count(seg_id) > 0; |
143 | 6 | } |
144 | | |
145 | 53 | ConstAllocatedLsnVectorSharedPtr get_segment_allocated_lsns(int64_t seg_id) const { |
146 | 53 | std::lock_guard<std::mutex> l(_mutex); |
147 | 53 | auto it = _seg_id_to_lsn_ids.find(seg_id); |
148 | 53 | DCHECK(it != _seg_id_to_lsn_ids.end()) |
149 | 0 | << "SegmentAllocatedLsnMap::get_segment_allocated_lsns missing seg_id=" << seg_id |
150 | 0 | << ", existing_seg_ids=[" << ([&] { |
151 | 0 | std::string s; |
152 | 0 | for (const auto& [id, _] : _seg_id_to_lsn_ids) { |
153 | 0 | if (!s.empty()) { |
154 | 0 | s.push_back(','); |
155 | 0 | } |
156 | 0 | s.append(std::to_string(id)); |
157 | 0 | } |
158 | 0 | return s; |
159 | 0 | }()) |
160 | 0 | << "]"; |
161 | 53 | return it->second; |
162 | 53 | } |
163 | | |
164 | | private: |
165 | | mutable std::mutex _mutex; |
166 | | std::map<int64_t, ConstAllocatedLsnVectorSharedPtr> _seg_id_to_lsn_ids; |
167 | | }; |
168 | | |
169 | | struct SegmentWriteChangelogOptions { |
170 | | public: |
171 | | bool write_before = false; |
172 | | std::shared_ptr<const changelog::ResolvedChangelogLayout> layout; |
173 | | |
174 | | // source context, used for retrieving historical row and building changelog block |
175 | | struct SourceWriteDataOptions { |
176 | | BaseTabletSPtr base_tablet = nullptr; |
177 | | TabletSchemaSPtr tablet_schema = nullptr; |
178 | | std::shared_ptr<PartialUpdateInfo> partial_update_info; |
179 | | std::shared_ptr<MowContext> mow_context; |
180 | | bool is_transient_rowset_writer = false; |
181 | | DataWriteType source_write_type = DataWriteType::TYPE_DEFAULT; |
182 | | // input rowset's changelog rowset, read LSN from it at publish time |
183 | | RowsetSharedPtr changelog_rowset = nullptr; |
184 | | } source; |
185 | | }; |
186 | | } // namespace segment_v2 |
187 | | |
188 | | } // namespace doris |