From e79577e4cd108219a4631bac5fc105dae623a38a Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Mon, 30 Sep 2024 10:52:03 +0200 Subject: [PATCH 1/2] Fix checking distance of last snapshot --- src/handle_commit.cxx | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/src/handle_commit.cxx b/src/handle_commit.cxx index 0714086c..477e62bb 100644 --- a/src/handle_commit.cxx +++ b/src/handle_commit.cxx @@ -534,12 +534,28 @@ bool raft_server::snapshot_and_compact(ulong committed_idx, bool forced_creation return false; } - if ( ( forced_creation || - snp_creation_scheduled_ || - !local_snp || - committed_idx >= snapshot_distance + local_snp->get_last_log_idx() ) && - snp_in_progress_.compare_exchange_strong(f, true) ) - { + auto can_create_snapshot = [&](const ptr& local_snapshot) { + if (forced_creation || snp_creation_scheduled_) + return true; + + return !local_snapshot || committed_idx >= snapshot_distance + local_snapshot->get_last_log_idx(); + }; + + if ( can_create_snapshot(local_snp) && + snp_in_progress_.compare_exchange_strong(f, true) ) { + local_snp = get_last_snapshot(); + + // NOTE: + // Because we fetch local_snp before checking the flag snp_in_progress_ + // we can have a newer snapshot that was created between + // fetching of local_snp and setting the snp_in_progress_ to false + // to avoid creating snapshots too soon, we recheck the distance when we are + // sure this is the only thread creating snapshot + if (!can_create_snapshot(local_snp)) { + snp_in_progress_ = false; + return false; + } + snapshot_in_action = true; p_in("creating a snapshot for index %" PRIu64 "", committed_idx); @@ -550,7 +566,6 @@ bool raft_server::snapshot_and_compact(ulong committed_idx, bool forced_creation // // To avoid such a case, while `snp_in_progress_` is true, // we re-check the latest snapshot index here. - local_snp = get_last_snapshot(); if (local_snp && local_snp->get_last_log_idx() >= committed_idx) { p_wn("snapshot index inversion detected, " "skip snapshot creation for index %" PRIu64 ", " From 51fe0fae049589cfa1660aa4214355cf6e004096 Mon Sep 17 00:00:00 2001 From: Jung-Sang Ahn Date: Fri, 11 Oct 2024 15:03:15 -0700 Subject: [PATCH 2/2] [Update PR] Formatting --- src/handle_commit.cxx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/handle_commit.cxx b/src/handle_commit.cxx index 477e62bb..1c05ec08 100644 --- a/src/handle_commit.cxx +++ b/src/handle_commit.cxx @@ -538,7 +538,8 @@ bool raft_server::snapshot_and_compact(ulong committed_idx, bool forced_creation if (forced_creation || snp_creation_scheduled_) return true; - return !local_snapshot || committed_idx >= snapshot_distance + local_snapshot->get_last_log_idx(); + return !local_snapshot || + committed_idx >= snapshot_distance + local_snapshot->get_last_log_idx(); }; if ( can_create_snapshot(local_snp) &&