Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write current batch for distributed send atomically (using .tmp + rename) #7600

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 13 additions & 2 deletions dbms/src/Storages/Distributed/DirectoryMonitor.cpp
Expand Up @@ -355,8 +355,19 @@ struct StorageDistributedDirectoryMonitor::Batch
/// we must try to re-send exactly the same batches.
/// So we save contents of the current batch into the current_batch_file_path file
/// and truncate it afterwards if all went well.
WriteBufferFromFile out{parent.current_batch_file_path};
writeText(out);

/// Temporary file is required for atomicity.
String tmp_file{parent.current_batch_file_path + ".tmp"};

if (Poco::File{tmp_file}.exists())
LOG_ERROR(parent.log, "Temporary file `" << tmp_file << "` exists. Unclean shutdown?");

{
WriteBufferFromFile out{tmp_file, O_WRONLY | O_TRUNC | O_CREAT};
writeText(out);
}

Poco::File{tmp_file}.renameTo(parent.current_batch_file_path);
}
auto timeouts = ConnectionTimeouts::getTCPTimeoutsWithFailover(parent.storage.global_context.getSettingsRef());
auto connection = parent.pool->get(timeouts);
Expand Down