Skip to content

Commit

Permalink
[fix](binlog) Avoid clear binlog dir (#45581)
Browse files Browse the repository at this point in the history
Related PR: #45445

The binlog dir is shared with all rowsets, so the existing binlog dir
should not be cleared directly.

Since the publish_txn might fail even if the add_to_binlog success, so
we need to check whether a file already exists before linking.
  • Loading branch information
w41ter authored Dec 18, 2024
1 parent ad814d2 commit 3d1f0c6
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions be/src/olap/rowset/beta_rowset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "olap/rowset/beta_rowset.h"

#include <ctype.h>
#include <errno.h>
#include <fmt/format.h>

#include <algorithm>
Expand Down Expand Up @@ -578,6 +579,10 @@ Status BetaRowset::add_to_binlog() {
}
}};

// The publish_txn might fail even if the add_to_binlog success, so we need to check
// whether a file already exists before linking.
auto errno_is_file_exists = []() { return Errno::no() == EEXIST; };

// all segments are in the same directory, so cache binlog_dir without multi times check
std::string binlog_dir;
for (int i = 0; i < segments_num; ++i) {
Expand All @@ -586,20 +591,18 @@ Status BetaRowset::add_to_binlog() {
if (binlog_dir.empty()) {
binlog_dir = std::filesystem::path(seg_file).parent_path().append("_binlog").string();

// Delete all existing files in binlog dir, to keep binlog dir clean.
bool exists = true;
RETURN_IF_ERROR(fs->exists(binlog_dir, &exists));
if (exists) {
RETURN_IF_ERROR(fs->delete_directory(binlog_dir));
if (!exists) {
RETURN_IF_ERROR(fs->create_directory(binlog_dir));
}
RETURN_IF_ERROR(fs->create_directory(binlog_dir));
}

auto binlog_file =
(std::filesystem::path(binlog_dir) / std::filesystem::path(seg_file).filename())
.string();
VLOG_DEBUG << "link " << seg_file << " to " << binlog_file;
if (!fs->link_file(seg_file, binlog_file).ok()) {
if (!fs->link_file(seg_file, binlog_file).ok() && !errno_is_file_exists()) {
status = Status::Error<OS_ERROR>("fail to create hard link. from={}, to={}, errno={}",
seg_file, binlog_file, Errno::no());
return status;
Expand All @@ -616,7 +619,7 @@ Status BetaRowset::add_to_binlog() {
std::filesystem::path(index_file).filename())
.string();
VLOG_DEBUG << "link " << index_file << " to " << binlog_index_file;
if (!fs->link_file(index_file, binlog_index_file).ok()) {
if (!fs->link_file(index_file, binlog_index_file).ok() && !errno_is_file_exists()) {
status = Status::Error<OS_ERROR>(
"fail to create hard link. from={}, to={}, errno={}", index_file,
binlog_index_file, Errno::no());
Expand All @@ -632,7 +635,7 @@ Status BetaRowset::add_to_binlog() {
std::filesystem::path(index_file).filename())
.string();
VLOG_DEBUG << "link " << index_file << " to " << binlog_index_file;
if (!fs->link_file(index_file, binlog_index_file).ok()) {
if (!fs->link_file(index_file, binlog_index_file).ok() && !errno_is_file_exists()) {
status = Status::Error<OS_ERROR>(
"fail to create hard link. from={}, to={}, errno={}", index_file,
binlog_index_file, Errno::no());
Expand Down

0 comments on commit 3d1f0c6

Please sign in to comment.