forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathExportPartTask.cpp
More file actions
296 lines (243 loc) · 10.2 KB
/
ExportPartTask.cpp
File metadata and controls
296 lines (243 loc) · 10.2 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
#include <mutex>
#include <Storages/MergeTree/ExportPartTask.h>
#include <Storages/MergeTree/MergeTreeSequentialSource.h>
#include <Storages/MergeTree/MergeTreeData.h>
#include <Interpreters/Context.h>
#include <Interpreters/DatabaseCatalog.h>
#include <Core/Settings.h>
#include <Interpreters/ExpressionActions.h>
#include <Processors/Executors/CompletedPipelineExecutor.h>
#include <Processors/QueryPlan/BuildQueryPipelineSettings.h>
#include <Processors/QueryPlan/Optimizations/QueryPlanOptimizationSettings.h>
#include <Processors/QueryPlan/QueryPlan.h>
#include <QueryPipeline/QueryPipelineBuilder.h>
#include <Common/Exception.h>
#include <Common/ProfileEventsScope.h>
#include <Storages/MergeTree/ExportList.h>
#include <Formats/FormatFactory.h>
namespace ProfileEvents
{
extern const Event PartsExportDuplicated;
extern const Event PartsExportFailures;
extern const Event PartsExports;
extern const Event PartsExportTotalMilliseconds;
}
namespace DB
{
namespace ErrorCodes
{
extern const int UNKNOWN_TABLE;
extern const int FILE_ALREADY_EXISTS;
extern const int LOGICAL_ERROR;
extern const int QUERY_WAS_CANCELLED;
}
namespace Setting
{
extern const SettingsUInt64 min_bytes_to_use_direct_io;
extern const SettingsUInt64 export_merge_tree_part_max_bytes_per_file;
extern const SettingsUInt64 export_merge_tree_part_max_rows_per_file;
}
ExportPartTask::ExportPartTask(MergeTreeData & storage_, const MergeTreePartExportManifest & manifest_)
: storage(storage_),
manifest(manifest_)
{
}
const MergeTreePartExportManifest & ExportPartTask::getManifest() const
{
return manifest;
}
bool ExportPartTask::executeStep()
{
auto local_context = Context::createCopy(storage.getContext());
local_context->makeQueryContextForExportPart();
local_context->setCurrentQueryId(manifest.transaction_id);
local_context->setBackgroundOperationTypeForContext(ClientInfo::BackgroundOperationType::EXPORT_PART);
local_context->setSettings(manifest.settings);
const auto & metadata_snapshot = manifest.metadata_snapshot;
Names columns_to_read = metadata_snapshot->getColumns().getNamesOfPhysical();
MergeTreeSequentialSourceType read_type = MergeTreeSequentialSourceType::Export;
Block block_with_partition_values;
if (metadata_snapshot->hasPartitionKey())
{
/// todo arthur do I need to init minmax_idx?
block_with_partition_values = manifest.data_part->minmax_idx->getBlock(storage);
}
auto destination_storage = DatabaseCatalog::instance().tryGetTable(manifest.destination_storage_id, local_context);
if (!destination_storage)
{
std::lock_guard inner_lock(storage.export_manifests_mutex);
const auto destination_storage_id_name = manifest.destination_storage_id.getNameForLogs();
storage.export_manifests.erase(manifest);
throw Exception(ErrorCodes::UNKNOWN_TABLE, "Failed to reconstruct destination storage: {}", destination_storage_id_name);
}
auto exports_list_entry = storage.getContext()->getExportsList().insert(
getStorageID(),
manifest.destination_storage_id,
manifest.data_part->getBytesOnDisk(),
manifest.data_part->name,
std::vector<std::string>{},
manifest.data_part->rows_count,
manifest.data_part->getBytesOnDisk(),
manifest.data_part->getBytesUncompressedOnDisk(),
manifest.create_time,
local_context);
SinkToStoragePtr sink;
const auto new_file_path_callback = [&exports_list_entry](const std::string & file_path)
{
std::unique_lock lock((*exports_list_entry)->destination_file_paths_mutex);
(*exports_list_entry)->destination_file_paths.push_back(file_path);
};
try
{
sink = destination_storage->import(
manifest.data_part->name + "_" + manifest.data_part->checksums.getTotalChecksumHex(),
block_with_partition_values,
new_file_path_callback,
manifest.file_already_exists_policy == MergeTreePartExportManifest::FileAlreadyExistsPolicy::overwrite,
manifest.settings[Setting::export_merge_tree_part_max_bytes_per_file],
manifest.settings[Setting::export_merge_tree_part_max_rows_per_file],
getFormatSettings(local_context),
local_context);
bool apply_deleted_mask = true;
bool read_with_direct_io = local_context->getSettingsRef()[Setting::min_bytes_to_use_direct_io] > manifest.data_part->getBytesOnDisk();
bool prefetch = false;
MergeTreeData::IMutationsSnapshot::Params mutations_snapshot_params
{
.metadata_version = metadata_snapshot->getMetadataVersion(),
.min_part_metadata_version = manifest.data_part->getMetadataVersion()
};
auto mutations_snapshot = storage.getMutationsSnapshot(mutations_snapshot_params);
auto alter_conversions = MergeTreeData::getAlterConversionsForPart(
manifest.data_part,
mutations_snapshot,
local_context);
QueryPlan plan_for_part;
createReadFromPartStep(
read_type,
plan_for_part,
storage,
storage.getStorageSnapshot(metadata_snapshot, local_context),
RangesInDataPart(manifest.data_part),
alter_conversions,
nullptr,
columns_to_read,
nullptr,
apply_deleted_mask,
std::nullopt,
read_with_direct_io,
prefetch,
local_context,
getLogger("ExportPartition"));
ThreadGroupSwitcher switcher((*exports_list_entry)->thread_group, "");
QueryPlanOptimizationSettings optimization_settings(local_context);
auto pipeline_settings = BuildQueryPipelineSettings(local_context);
auto builder = plan_for_part.buildQueryPipeline(optimization_settings, pipeline_settings);
builder->setProgressCallback([&exports_list_entry](const Progress & progress)
{
(*exports_list_entry)->bytes_read_uncompressed += progress.read_bytes;
(*exports_list_entry)->rows_read += progress.read_rows;
});
pipeline = QueryPipelineBuilder::getPipeline(std::move(*builder));
pipeline.complete(sink);
CompletedPipelineExecutor exec(pipeline);
auto is_cancelled_callback = [this]()
{
return isCancelled();
};
exec.setCancelCallback(is_cancelled_callback, 100);
exec.execute();
if (isCancelled())
{
throw Exception(ErrorCodes::QUERY_WAS_CANCELLED, "Export part was cancelled");
}
std::lock_guard inner_lock(storage.export_manifests_mutex);
storage.writePartLog(
PartLogElement::Type::EXPORT_PART,
{},
(*exports_list_entry)->watch.elapsed(),
manifest.data_part->name,
manifest.data_part,
{manifest.data_part},
nullptr,
nullptr,
exports_list_entry.get());
storage.export_manifests.erase(manifest);
ProfileEvents::increment(ProfileEvents::PartsExports);
ProfileEvents::increment(ProfileEvents::PartsExportTotalMilliseconds, (*exports_list_entry)->watch.elapsedMilliseconds());
if (manifest.completion_callback)
manifest.completion_callback(MergeTreePartExportManifest::CompletionCallbackResult::createSuccess((*exports_list_entry)->destination_file_paths));
}
catch (const Exception & e)
{
if (e.code() == ErrorCodes::FILE_ALREADY_EXISTS)
{
ProfileEvents::increment(ProfileEvents::PartsExportDuplicated);
/// File already exists and the policy is NO_OP, treat it as success.
if (manifest.file_already_exists_policy == MergeTreePartExportManifest::FileAlreadyExistsPolicy::skip)
{
storage.writePartLog(
PartLogElement::Type::EXPORT_PART,
{},
(*exports_list_entry)->watch.elapsed(),
manifest.data_part->name,
manifest.data_part,
{manifest.data_part},
nullptr,
nullptr,
exports_list_entry.get());
std::lock_guard inner_lock(storage.export_manifests_mutex);
storage.export_manifests.erase(manifest);
ProfileEvents::increment(ProfileEvents::PartsExports);
ProfileEvents::increment(ProfileEvents::PartsExportTotalMilliseconds, (*exports_list_entry)->watch.elapsedMilliseconds());
if (manifest.completion_callback)
{
manifest.completion_callback(MergeTreePartExportManifest::CompletionCallbackResult::createSuccess((*exports_list_entry)->destination_file_paths));
}
return false;
}
}
ProfileEvents::increment(ProfileEvents::PartsExportFailures);
storage.writePartLog(
PartLogElement::Type::EXPORT_PART,
ExecutionStatus::fromCurrentException("", true),
(*exports_list_entry)->watch.elapsed(),
manifest.data_part->name,
manifest.data_part,
{manifest.data_part},
nullptr,
nullptr,
exports_list_entry.get());
std::lock_guard inner_lock(storage.export_manifests_mutex);
storage.export_manifests.erase(manifest);
if (manifest.completion_callback)
manifest.completion_callback(MergeTreePartExportManifest::CompletionCallbackResult::createFailure(e));
return false;
}
return false;
}
void ExportPartTask::cancel() noexcept
{
LOG_INFO(getLogger("ExportPartTask"), "Export part {} task cancel() method called", manifest.data_part->name);
cancel_requested.store(true);
pipeline.cancel();
}
bool ExportPartTask::isCancelled() const
{
return cancel_requested.load() || storage.parts_mover.moves_blocker.isCancelled();
}
void ExportPartTask::onCompleted()
{
}
StorageID ExportPartTask::getStorageID() const
{
return storage.getStorageID();
}
Priority ExportPartTask::getPriority() const
{
return Priority{};
}
String ExportPartTask::getQueryId() const
{
return manifest.transaction_id;
}
}