forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloud_rowset_builder.cpp
More file actions
175 lines (156 loc) · 7.57 KB
/
Copy pathcloud_rowset_builder.cpp
File metadata and controls
175 lines (156 loc) · 7.57 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
// 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_rowset_builder.h"
#include "cloud/cloud_meta_mgr.h"
#include "cloud/cloud_storage_engine.h"
#include "cloud/cloud_tablet.h"
#include "cloud/cloud_tablet_mgr.h"
#include "storage/storage_policy.h"
namespace doris {
using namespace ErrorCode;
CloudRowsetBuilder::CloudRowsetBuilder(CloudStorageEngine& engine, const WriteRequest& req,
RuntimeProfile* profile)
: BaseRowsetBuilder(req, profile), _engine(engine) {}
CloudRowsetBuilder::~CloudRowsetBuilder() {
// Clear file cache immediately when load fails
if (_is_init && _rowset != nullptr && _rowset->rowset_meta()->rowset_state() == PREPARED) {
_rowset->clear_cache();
}
}
Status CloudRowsetBuilder::init() {
_tablet = DORIS_TRY(_engine.get_tablet(_req.tablet_id));
std::shared_ptr<MowContext> mow_context;
if (_tablet->enable_unique_key_merge_on_write()) {
if (config::cloud_mow_sync_rowsets_when_load_txn_begin) {
auto st = std::static_pointer_cast<CloudTablet>(_tablet)->sync_rowsets();
// sync_rowsets will return INVALID_TABLET_STATE when tablet is under alter
if (!st.ok() && !st.is<ErrorCode::INVALID_TABLET_STATE>()) {
return st;
}
}
RETURN_IF_ERROR(init_mow_context(mow_context));
}
RETURN_IF_ERROR(check_tablet_version_count());
using namespace std::chrono;
std::static_pointer_cast<CloudTablet>(_tablet)->last_load_time_ms =
duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
// build tablet schema in request level
RETURN_IF_ERROR(_build_current_tablet_schema(_req.index_id, _req.table_schema_param.get(),
*_tablet->tablet_schema()));
RowsetWriterContext context;
context.txn_id = _req.txn_id;
context.txn_expiration = _req.txn_expiration;
context.load_id = _req.load_id;
context.rowset_state = PREPARED;
context.segments_overlap = OVERLAPPING;
context.tablet_schema = _tablet_schema;
context.newest_write_timestamp = UnixSeconds();
context.tablet_id = _req.tablet_id;
context.index_id = _req.index_id;
context.tablet = _tablet;
context.write_type = DataWriteType::TYPE_DIRECT;
context.mow_context = mow_context;
context.write_file_cache = _req.write_file_cache;
context.partial_update_info = _partial_update_info;
context.file_cache_ttl_sec = _tablet->ttl_seconds();
context.storage_resource = _engine.get_storage_resource(_req.storage_vault_id);
if (!context.storage_resource) {
return Status::InternalError("vault id not found, maybe not sync, vault id {}",
_req.storage_vault_id);
}
_rowset_writer = DORIS_TRY(_tablet->create_rowset_writer(context, false));
_calc_delete_bitmap_token = _engine.calc_delete_bitmap_executor()->create_token();
if (!_skip_writing_rowset_metadata) {
RETURN_IF_ERROR(_engine.meta_mgr().prepare_rowset(*_rowset_writer->rowset_meta(), "",
_tablet->table_id()));
}
_is_init = true;
return Status::OK();
}
Status CloudRowsetBuilder::check_tablet_version_count() {
int64_t version_count = cloud_tablet()->fetch_add_approximate_num_rowsets(0);
DBUG_EXECUTE_IF("RowsetBuilder.check_tablet_version_count.too_many_version",
{ version_count = INT_MAX; });
// TODO(plat1ko): load backoff algorithm
int32_t max_version_config = cloud_tablet()->max_version_config();
if (version_count > max_version_config) {
return Status::Error<TOO_MANY_VERSION>(
"failed to init rowset builder. version count: {}, exceed limit: {}, "
"tablet: {}. Please reduce the frequency of loading data or adjust the "
"max_tablet_version_num or time_series_max_tablet_version_numin be.conf to a "
"larger value.",
version_count, max_version_config, _tablet->tablet_id());
}
return Status::OK();
}
void CloudRowsetBuilder::update_tablet_stats() {
auto* tablet = cloud_tablet();
DCHECK(tablet);
DCHECK(_rowset);
tablet->fetch_add_approximate_num_rowsets(1);
tablet->fetch_add_approximate_num_segments(_rowset->num_segments());
tablet->fetch_add_approximate_num_rows(_rowset->num_rows());
tablet->fetch_add_approximate_data_size(_rowset->total_disk_size());
tablet->fetch_add_approximate_cumu_num_rowsets(1);
tablet->fetch_add_approximate_cumu_num_deltas(_rowset->num_segments());
tablet->write_count.fetch_add(1, std::memory_order_relaxed);
}
CloudTablet* CloudRowsetBuilder::cloud_tablet() {
return static_cast<CloudTablet*>(_tablet.get());
}
const RowsetMetaSharedPtr& CloudRowsetBuilder::rowset_meta() {
return _rowset_writer->rowset_meta();
}
Status CloudRowsetBuilder::set_txn_related_info() {
if (_tablet->enable_unique_key_merge_on_write()) {
// For empty rowsets when skip_writing_empty_rowset_metadata=true,
// store only a lightweight marker instead of full rowset info.
// This allows CalcDeleteBitmapTask to detect and skip gracefully,
// while using minimal memory (~16 bytes per entry).
if (_skip_writing_rowset_metadata) {
_engine.txn_delete_bitmap_cache().mark_empty_rowset(_req.txn_id, _tablet->tablet_id(),
_req.txn_expiration);
return Status::OK();
}
if (config::enable_merge_on_write_correctness_check && _rowset->num_rows() != 0) {
auto st = _tablet->check_delete_bitmap_correctness(
_delete_bitmap, _rowset->end_version() - 1, _req.txn_id, *_rowset_ids);
if (!st.ok()) {
LOG(WARNING) << fmt::format(
"[tablet_id:{}][txn_id:{}][load_id:{}][partition_id:{}] "
"delete bitmap correctness check failed in commit phase!",
_req.tablet_id, _req.txn_id, UniqueId(_req.load_id).to_string(),
_req.partition_id);
return st;
}
}
_engine.txn_delete_bitmap_cache().set_tablet_txn_info(
_req.txn_id, _tablet->tablet_id(), _delete_bitmap, *_rowset_ids, _rowset,
_req.txn_expiration, _partial_update_info);
} else {
if (config::enable_cloud_make_rs_visible_on_be) {
if (_skip_writing_rowset_metadata) {
_engine.committed_rs_mgr().mark_empty_rowset(_req.txn_id, _tablet->tablet_id(),
_req.txn_expiration);
} else {
_engine.meta_mgr().cache_committed_rowset(rowset_meta(), _req.txn_expiration);
}
}
}
return Status::OK();
}
} // namespace doris