forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloud_txn_delete_bitmap_cache.cpp
More file actions
362 lines (333 loc) · 15.7 KB
/
Copy pathcloud_txn_delete_bitmap_cache.cpp
File metadata and controls
362 lines (333 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include "cloud/cloud_txn_delete_bitmap_cache.h"
#include <fmt/core.h>
#include <chrono>
#include <memory>
#include <shared_mutex>
#include "cloud/config.h"
#include "common/status.h"
#include "cpp/sync_point.h"
#include "storage/olap_common.h"
#include "storage/rowset/rowset_fwd.h"
#include "storage/tablet/tablet_meta.h"
#include "storage/txn/txn_manager.h"
namespace doris {
CloudTxnDeleteBitmapCache::CloudTxnDeleteBitmapCache(size_t size_in_bytes)
: LRUCachePolicy(CachePolicy::CacheType::CLOUD_TXN_DELETE_BITMAP_CACHE, size_in_bytes,
LRUCacheType::SIZE, /*stale_sweep_time_s*/ 86400, /*num_shards*/ 4,
/*element_count_capacity*/ 0, /*enable_prune*/ true,
/*is_lru_k*/ false),
_stop_latch(1) {}
CloudTxnDeleteBitmapCache::~CloudTxnDeleteBitmapCache() {
_stop_latch.count_down();
_clean_thread->join();
}
Status CloudTxnDeleteBitmapCache::init() {
auto st = Thread::create(
"CloudTxnDeleteBitmapCache", "clean_txn_dbm_thread",
[this]() { this->_clean_thread_callback(); }, &_clean_thread);
if (!st.ok()) {
LOG(WARNING) << "failed to create thread for CloudTxnDeleteBitmapCache, error: " << st;
}
return st;
}
Status CloudTxnDeleteBitmapCache::get_tablet_txn_info(
TTransactionId transaction_id, int64_t tablet_id, RowsetSharedPtr* rowset,
DeleteBitmapPtr* delete_bitmap, RowsetIdUnorderedSet* rowset_ids, int64_t* txn_expiration,
std::shared_ptr<PartialUpdateInfo>* partial_update_info,
std::shared_ptr<PublishStatus>* publish_status, TxnPublishInfo* previous_publish_info) {
{
std::shared_lock<std::shared_mutex> rlock(_rwlock);
TxnKey key(transaction_id, tablet_id);
DBUG_EXECUTE_IF("CloudTxnDeleteBitmapCache.get_tablet_txn_info.not_found", {
return Status::Error<ErrorCode::NOT_FOUND>(
"not found txn info for test, tablet_id={}, transaction_id={}", tablet_id,
transaction_id);
});
auto iter = _txn_map.find(key);
if (iter == _txn_map.end()) {
return Status::Error<ErrorCode::NOT_FOUND, false>(
"not found txn info, tablet_id={}, transaction_id={}", tablet_id,
transaction_id);
}
*rowset = iter->second.rowset;
*txn_expiration = iter->second.txn_expiration;
*partial_update_info = iter->second.partial_update_info;
*publish_status = iter->second.publish_status;
*previous_publish_info = iter->second.publish_info;
}
auto st = get_delete_bitmap(transaction_id, tablet_id, delete_bitmap, rowset_ids, nullptr);
if (st.is<ErrorCode::NOT_FOUND>()) {
// Because of the rowset_ids become empty, all delete bitmap
// will be recalculate in CalcDeleteBitmapTask
if (delete_bitmap != nullptr) {
*delete_bitmap = std::make_shared<DeleteBitmap>(tablet_id);
}
// to avoid to skip calculating
**publish_status = PublishStatus::INIT;
return Status::OK();
}
return st;
}
Result<std::pair<RowsetSharedPtr, DeleteBitmapPtr>>
CloudTxnDeleteBitmapCache::get_rowset_and_delete_bitmap(TTransactionId transaction_id,
int64_t tablet_id) {
RowsetSharedPtr rowset;
{
std::shared_lock<std::shared_mutex> rlock(_rwlock);
TxnKey txn_key(transaction_id, tablet_id);
if (_empty_rowset_markers.contains(txn_key)) {
return std::make_pair(nullptr, nullptr);
}
auto iter = _txn_map.find(txn_key);
if (iter == _txn_map.end()) {
return ResultError(Status::InternalError<false>(""));
}
if (!(iter->second.publish_status &&
*(iter->second.publish_status) == PublishStatus::SUCCEED)) {
return ResultError(Status::InternalError<false>(""));
}
rowset = iter->second.rowset;
}
std::string key_str = fmt::format("{}/{}", transaction_id, tablet_id);
CacheKey key(key_str);
Cache::Handle* handle = lookup(key);
DBUG_EXECUTE_IF("CloudTxnDeleteBitmapCache::get_delete_bitmap.cache_miss", {
handle = nullptr;
LOG(INFO) << "CloudTxnDeleteBitmapCache::get_delete_bitmap.cache_miss, make cache missed "
"when get delete bitmap, txn_id:"
<< transaction_id << ", tablet_id: " << tablet_id;
});
DeleteBitmapCacheValue* val =
handle == nullptr ? nullptr : reinterpret_cast<DeleteBitmapCacheValue*>(value(handle));
if (!val) {
return ResultError(Status::InternalError<false>(""));
}
Defer defer {[this, handle] { release(handle); }};
return std::make_pair(rowset, val->delete_bitmap);
}
Status CloudTxnDeleteBitmapCache::get_delete_bitmap(
TTransactionId transaction_id, int64_t tablet_id, DeleteBitmapPtr* delete_bitmap,
RowsetIdUnorderedSet* rowset_ids, std::shared_ptr<PublishStatus>* publish_status) {
if (publish_status) {
std::shared_lock<std::shared_mutex> rlock(_rwlock);
TxnKey txn_key(transaction_id, tablet_id);
auto iter = _txn_map.find(txn_key);
if (iter == _txn_map.end()) {
return Status::Error<ErrorCode::NOT_FOUND, false>(
"not found txn info, tablet_id={}, transaction_id={}", tablet_id,
transaction_id);
}
*publish_status = iter->second.publish_status;
}
std::string key_str = fmt::format("{}/{}", transaction_id, tablet_id);
CacheKey key(key_str);
Cache::Handle* handle = lookup(key);
DBUG_EXECUTE_IF("CloudTxnDeleteBitmapCache::get_delete_bitmap.cache_miss", {
handle = nullptr;
LOG(INFO) << "CloudTxnDeleteBitmapCache::get_delete_bitmap.cache_miss, make cache missed "
"when get delete bitmap, txn_id:"
<< transaction_id << ", tablet_id: " << tablet_id;
});
DeleteBitmapCacheValue* val =
handle == nullptr ? nullptr : reinterpret_cast<DeleteBitmapCacheValue*>(value(handle));
if (val) {
*delete_bitmap = val->delete_bitmap;
if (rowset_ids) {
*rowset_ids = val->rowset_ids;
}
// must call release handle to reduce the reference count,
// otherwise there will be memory leak
release(handle);
} else {
LOG_INFO("cache missed when get delete bitmap")
.tag("txn_id", transaction_id)
.tag("tablet_id", tablet_id);
return Status::Error<ErrorCode::NOT_FOUND, false>(
"cache missed when get delete bitmap, tablet_id={}, transaction_id={}", tablet_id,
transaction_id);
}
return Status::OK();
}
void CloudTxnDeleteBitmapCache::set_tablet_txn_info(
TTransactionId transaction_id, int64_t tablet_id, DeleteBitmapPtr delete_bitmap,
const RowsetIdUnorderedSet& rowset_ids, RowsetSharedPtr rowset, int64_t txn_expiration,
std::shared_ptr<PartialUpdateInfo> partial_update_info) {
int64_t txn_expiration_min =
duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch())
.count() +
config::tablet_txn_info_min_expired_seconds;
txn_expiration = std::max(txn_expiration_min, txn_expiration);
{
std::unique_lock<std::shared_mutex> wlock(_rwlock);
TxnKey txn_key(transaction_id, tablet_id);
std::shared_ptr<PublishStatus> publish_status =
std::make_shared<PublishStatus>(PublishStatus::INIT);
_txn_map[txn_key] = TxnVal(rowset, txn_expiration, std::move(partial_update_info),
std::move(publish_status));
_expiration_txn.emplace(txn_expiration, txn_key);
}
std::string key_str = fmt::format("{}/{}", transaction_id, tablet_id);
CacheKey key(key_str);
auto val = new DeleteBitmapCacheValue(delete_bitmap, rowset_ids);
size_t charge = sizeof(DeleteBitmapCacheValue);
for (auto& [k, v] : val->delete_bitmap->delete_bitmap) {
charge += v.getSizeInBytes();
}
auto* handle = insert(key, val, charge, charge, CachePriority::NORMAL);
// must call release handle to reduce the reference count,
// otherwise there will be memory leak
release(handle);
LOG_INFO("set txn related delete bitmap")
.tag("txn_id", transaction_id)
.tag("expiration", txn_expiration)
.tag("tablet_id", tablet_id)
.tag("delete_bitmap_size", charge)
.tag("delete_bitmap_count", delete_bitmap->get_delete_bitmap_count())
.tag("delete_bitmap_cardinality", delete_bitmap->cardinality());
}
Status CloudTxnDeleteBitmapCache::update_tablet_txn_info(TTransactionId transaction_id,
int64_t tablet_id,
DeleteBitmapPtr delete_bitmap,
const RowsetIdUnorderedSet& rowset_ids,
PublishStatus publish_status,
TxnPublishInfo publish_info) {
{
std::unique_lock<std::shared_mutex> wlock(_rwlock);
TxnKey txn_key(transaction_id, tablet_id);
if (!_txn_map.contains(txn_key)) {
return Status::Error<ErrorCode::NOT_FOUND, false>(
"not found txn info, tablet_id={}, transaction_id={}, may be expired and be "
"removed",
tablet_id, transaction_id);
}
TxnVal& txn_val = _txn_map[txn_key];
*(txn_val.publish_status) = publish_status;
if (publish_status == PublishStatus::SUCCEED) {
txn_val.publish_info = publish_info;
}
}
std::string key_str = fmt::format("{}/{}", transaction_id, tablet_id);
CacheKey key(key_str);
auto val = new DeleteBitmapCacheValue(delete_bitmap, rowset_ids);
size_t charge = sizeof(DeleteBitmapCacheValue);
for (auto& [k, v] : val->delete_bitmap->delete_bitmap) {
charge += v.getSizeInBytes();
}
auto* handle = insert(key, val, charge, charge, CachePriority::NORMAL);
// must call release handle to reduce the reference count,
// otherwise there will be memory leak
release(handle);
if (config::enable_mow_verbose_log) {
LOG_INFO("update txn related delete bitmap")
.tag("txn_id", transaction_id)
.tag("tablt_id", tablet_id)
.tag("delete_bitmap_size", charge)
.tag("delete_bitmap_count", delete_bitmap->get_delete_bitmap_count())
.tag("delete_bitmap_cardinality", delete_bitmap->cardinality())
.tag("publish_status", static_cast<int>(publish_status));
}
return Status::OK();
}
void CloudTxnDeleteBitmapCache::remove_expired_tablet_txn_info() {
TEST_SYNC_POINT_RETURN_WITH_VOID("CloudTxnDeleteBitmapCache::remove_expired_tablet_txn_info");
std::unique_lock<std::shared_mutex> wlock(_rwlock);
while (!_expiration_txn.empty()) {
auto iter = _expiration_txn.begin();
bool in_txn_map = _txn_map.find(iter->second) != _txn_map.end();
bool in_markers = _empty_rowset_markers.find(iter->second) != _empty_rowset_markers.end();
if (!in_txn_map && !in_markers) {
_expiration_txn.erase(iter);
continue;
}
int64_t current_time = duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now().time_since_epoch())
.count();
if (iter->first > current_time) {
break;
}
// Clean from _txn_map if exists
auto txn_iter = _txn_map.find(iter->second);
if ((txn_iter != _txn_map.end()) && (iter->first == txn_iter->second.txn_expiration)) {
LOG_INFO("clean expired delete bitmap")
.tag("txn_id", txn_iter->first.txn_id)
.tag("expiration", txn_iter->second.txn_expiration)
.tag("tablt_id", txn_iter->first.tablet_id);
std::string key_str = std::to_string(txn_iter->first.txn_id) + "/" +
std::to_string(txn_iter->first.tablet_id); // Cache key container
CacheKey cache_key(key_str);
erase(cache_key);
_txn_map.erase(iter->second);
}
// Clean from _empty_rowset_markers if exists
auto marker_iter = _empty_rowset_markers.find(iter->second);
if (marker_iter != _empty_rowset_markers.end()) {
LOG_INFO("clean expired empty rowset marker")
.tag("txn_id", iter->second.txn_id)
.tag("tablet_id", iter->second.tablet_id);
_empty_rowset_markers.erase(marker_iter);
}
_expiration_txn.erase(iter);
}
}
void CloudTxnDeleteBitmapCache::remove_unused_tablet_txn_info(TTransactionId transaction_id,
int64_t tablet_id) {
std::unique_lock<std::shared_mutex> wlock(_rwlock);
TxnKey txn_key(transaction_id, tablet_id);
auto txn_iter = _txn_map.find(txn_key);
if (txn_iter != _txn_map.end()) {
LOG_INFO("remove unused tablet txn info")
.tag("txn_id", txn_iter->first.txn_id)
.tag("tablt_id", txn_iter->first.tablet_id);
std::string key_str = std::to_string(txn_iter->first.txn_id) + "/" +
std::to_string(txn_iter->first.tablet_id); // Cache key container
CacheKey cache_key(key_str);
erase(cache_key);
_txn_map.erase(txn_key);
}
}
void CloudTxnDeleteBitmapCache::mark_empty_rowset(TTransactionId txn_id, int64_t tablet_id,
int64_t txn_expiration) {
int64_t txn_expiration_min =
duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch())
.count() +
config::tablet_txn_info_min_expired_seconds;
txn_expiration = std::max(txn_expiration_min, txn_expiration);
if (config::enable_mow_verbose_log) {
LOG_INFO("mark empty rowset")
.tag("txn_id", txn_id)
.tag("tablet_id", tablet_id)
.tag("expiration", txn_expiration);
}
std::unique_lock<std::shared_mutex> wlock(_rwlock);
TxnKey txn_key(txn_id, tablet_id);
_empty_rowset_markers.emplace(txn_key);
_expiration_txn.emplace(txn_expiration, txn_key);
}
bool CloudTxnDeleteBitmapCache::is_empty_rowset(TTransactionId txn_id, int64_t tablet_id) {
std::shared_lock<std::shared_mutex> rlock(_rwlock);
TxnKey txn_key(txn_id, tablet_id);
return _empty_rowset_markers.contains(txn_key);
}
void CloudTxnDeleteBitmapCache::_clean_thread_callback() {
do {
remove_expired_tablet_txn_info();
} while (!_stop_latch.wait_for(
std::chrono::seconds(config::remove_expired_tablet_txn_info_interval_seconds)));
}
} // namespace doris