Skip to content

Commit 87b83dc

Browse files
authored
Merge pull request #1847 from Altinity/export-partition-commit-lock
Export partition - Introduce commit lock
2 parents 76ee3a1 + ef0ea11 commit 87b83dc

4 files changed

Lines changed: 19 additions & 4 deletions

File tree

src/Storages/MergeTree/ExportPartitionManifestUpdatingTask.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ void ExportPartitionManifestUpdatingTask::poll()
530530
/// A replica exported the last part but the commit never landed. Try to fix it.
531531
try
532532
{
533-
ExportPartitionUtils::commit(work.metadata, work.destination_storage, zk, log_ptr, work.entry_path, work.context, storage);
533+
ExportPartitionUtils::commit(work.metadata, work.destination_storage, zk, log_ptr, work.entry_path, work.context, storage, storage.getReplicaName());
534534
}
535535
catch (const Exception & e)
536536
{

src/Storages/MergeTree/ExportPartitionTaskScheduler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ void ExportPartitionTaskScheduler::handlePartExportSuccess(
297297
try
298298
{
299299
auto context = ExportPartitionUtils::getContextCopyWithTaskSettings(storage.getContext(), manifest);
300-
ExportPartitionUtils::commit(manifest, destination_storage, zk, storage.log.load(), export_path, context, storage);
300+
ExportPartitionUtils::commit(manifest, destination_storage, zk, storage.log.load(), export_path, context, storage, storage.replica_name);
301301
}
302302
catch (const Exception & e)
303303
{

src/Storages/MergeTree/ExportPartitionUtils.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ namespace ExportPartitionUtils
158158
const LoggerPtr & log,
159159
const std::string & entry_path,
160160
const ContextPtr & context_in,
161-
MergeTreeData & source_storage)
161+
MergeTreeData & source_storage,
162+
const String & replica_name)
162163
{
163164
auto context = Context::createCopy(context_in);
164165
context->setSetting("write_full_path_in_iceberg_metadata", manifest.write_full_path_in_iceberg_metadata);
@@ -171,6 +172,19 @@ namespace ExportPartitionUtils
171172
"Failpoint: export_partition_commit_always_throw");
172173
});
173174

175+
/// Per-task ephemeral lock that serializes the commit phase across replicas.
176+
/// Without it, `handlePartExportSuccess` (post-last-part path) and `tryCleanup`
177+
/// (poll/recovery path) can drive `commitExportPartitionTransaction` concurrently
178+
/// for the same task.
179+
const auto commit_lock_path = fs::path(entry_path) / "commit_lock";
180+
auto commit_lock = zkutil::EphemeralNodeHolder::tryCreate(commit_lock_path, *zk, replica_name);
181+
if (!commit_lock)
182+
{
183+
LOG_INFO(log, "ExportPartition: commit_lock for {} is held by another replica, skipping commit on this replica", entry_path);
184+
return;
185+
}
186+
LOG_INFO(log, "ExportPartition: commit_lock for {} acquired by replica {}", entry_path, replica_name);
187+
174188
const auto exported_paths = ExportPartitionUtils::getExportedPaths(log, zk, entry_path);
175189

176190
if (exported_paths.empty())

src/Storages/MergeTree/ExportPartitionUtils.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ namespace ExportPartitionUtils
4343
const LoggerPtr & log,
4444
const std::string & entry_path,
4545
const ContextPtr & context,
46-
MergeTreeData & source_storage
46+
MergeTreeData & source_storage,
47+
const String & replica_name
4748
);
4849

4950
/// Handles a commit-phase failure for a replicated partition export:

0 commit comments

Comments
 (0)