-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathcloud_delta_writer.cpp
More file actions
150 lines (125 loc) · 5.06 KB
/
cloud_delta_writer.cpp
File metadata and controls
150 lines (125 loc) · 5.06 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
// 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_delta_writer.h"
#include "cloud/cloud_meta_mgr.h"
#include "cloud/cloud_rowset_builder.h"
#include "cloud/cloud_storage_engine.h"
#include "cloud/config.h"
#include "load/delta_writer/delta_writer.h"
#include "runtime/thread_context.h"
namespace doris {
bvar::Adder<int64_t> g_cloud_commit_rowset_count("cloud_commit_rowset_count");
bvar::Adder<int64_t> g_cloud_commit_empty_rowset_count("cloud_commit_empty_rowset_count");
CloudDeltaWriter::CloudDeltaWriter(CloudStorageEngine& engine, const WriteRequest& req,
RuntimeProfile* profile, const UniqueId& load_id)
: BaseDeltaWriter(req, profile, load_id), _engine(engine) {
_rowset_builder = std::make_unique<CloudRowsetBuilder>(engine, req, profile);
_resource_ctx = thread_context()->resource_ctx();
}
CloudDeltaWriter::~CloudDeltaWriter() = default;
Status CloudDeltaWriter::batch_init(std::vector<CloudDeltaWriter*> writers) {
if (writers.empty()) {
return Status::OK();
}
std::vector<std::function<Status()>> tasks;
tasks.reserve(writers.size());
for (auto* writer : writers) {
if (writer->_is_init || writer->_is_cancelled) {
continue;
}
tasks.emplace_back([writer] {
SCOPED_ATTACH_TASK(writer->resource_context());
std::lock_guard<bthread::Mutex> lock(writer->_mtx);
if (writer->_is_init || writer->_is_cancelled) {
return Status::OK();
}
Status st = writer->init(); // included in SCOPED_ATTACH_TASK
return st;
});
}
return cloud::bthread_fork_join(tasks, 10);
}
Status CloudDeltaWriter::write(const Block* block, const DorisVector<uint32_t>& row_idxs) {
if (row_idxs.empty()) [[unlikely]] {
return Status::OK();
}
std::lock_guard lock(_mtx);
CHECK(_is_init || _is_cancelled);
{
SCOPED_TIMER(_wait_flush_limit_timer);
while (_memtable_writer->flush_running_count() >=
config::memtable_flush_running_count_limit) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
return _memtable_writer->write(block, row_idxs);
}
Status CloudDeltaWriter::close() {
std::lock_guard lock(_mtx);
CHECK(_is_init);
return _memtable_writer->close();
}
Status CloudDeltaWriter::cancel_with_status(const Status& st) {
std::lock_guard lock(_mtx);
return BaseDeltaWriter::cancel_with_status(st);
}
Status CloudDeltaWriter::build_rowset() {
std::lock_guard lock(_mtx);
CHECK(_is_init);
return BaseDeltaWriter::build_rowset();
}
CloudRowsetBuilder* CloudDeltaWriter::rowset_builder() {
return static_cast<CloudRowsetBuilder*>(_rowset_builder.get());
}
void CloudDeltaWriter::update_tablet_stats() {
rowset_builder()->update_tablet_stats();
}
const RowsetMetaSharedPtr& CloudDeltaWriter::rowset_meta() {
return rowset_builder()->rowset_meta();
}
Status CloudDeltaWriter::commit_rowset() {
g_cloud_commit_rowset_count << 1;
std::lock_guard<bthread::Mutex> lock(_mtx);
// Handle empty rowset (no data written)
if (!_is_init) {
g_cloud_commit_empty_rowset_count << 1;
return _commit_empty_rowset();
}
// Handle normal rowset with data
return _engine.meta_mgr().commit_rowset(*rowset_meta(), "", nullptr,
rowset_builder()->tablet()->table_id());
}
Status CloudDeltaWriter::_commit_empty_rowset() {
// If skip writing empty rowset metadata is enabled,
// we do not prepare rowset to meta service.
if (config::skip_writing_empty_rowset_metadata) {
rowset_builder()->set_skip_writing_rowset_metadata(true);
}
RETURN_IF_ERROR(_rowset_builder->init());
RETURN_IF_ERROR(_rowset_builder->build_rowset());
// If skip writing empty rowset metadata is enabled, we do not commit rowset to meta service.
if (config::skip_writing_empty_rowset_metadata) {
return Status::OK();
}
// write a empty rowset kv to keep version continuous
return _engine.meta_mgr().commit_rowset(*rowset_meta(), "", nullptr,
rowset_builder()->tablet()->table_id());
}
Status CloudDeltaWriter::set_txn_related_info() {
return rowset_builder()->set_txn_related_info();
}
} // namespace doris