Skip to content

Commit

Permalink
c/consensus_utils: adjust recovery offset ranges
Browse files Browse the repository at this point in the history
For cloud based recovery, once segments are downloaded, the state for
raft, kvstore and local storage needs to be updated to reflect the
recovered log. Previously, the offsets were slightly off and this commit
adjusts them to always refer to the first/last offsets in the log.
  • Loading branch information
Vlad Lazar committed Jun 1, 2023
1 parent 206ceef commit b25557b
Showing 1 changed file with 5 additions and 14 deletions.
19 changes: 5 additions & 14 deletions src/v/raft/consensus_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -484,15 +484,6 @@ ss::future<> move_persistent_state(
});
}

// Return previous offset. This is different from
// model::prev_offset because it returns -1 for offset 0.
// The model::offset{} is a special case since the result
// of the decrement in this case is undefined.
static model::offset get_prev_offset(model::offset o) {
vassert(o != model::offset{}, "Can't return previous offset");
return o - model::offset{1};
}

ss::future<> create_offset_translator_state_for_pre_existing_partition(
storage::api& api,
const storage::ntp_config& ntp_cfg,
Expand All @@ -514,11 +505,11 @@ ss::future<> create_offset_translator_state_for_pre_existing_partition(
raftlog.debug,
"{} Set highest_known_offset in kv-store to {}",
ntp_cfg.ntp(),
get_prev_offset(max_rp_offset));
max_rp_offset);
co_await api.kvs().put(
storage::kvstore::key_space::offset_translator,
raft::offset_translator::kvstore_highest_known_offset_key(group),
reflection::to_iobuf(get_prev_offset(max_rp_offset)));
reflection::to_iobuf(max_rp_offset));
}

ss::future<> create_raft_state_for_pre_existing_partition(
Expand All @@ -534,19 +525,19 @@ ss::future<> create_raft_state_for_pre_existing_partition(
raftlog.debug,
"{} Prepare raft state, set latest_known_offset {} to the kv-store",
ntp_cfg.ntp(),
get_prev_offset(max_rp_offset));
max_rp_offset);
auto key = raft::details::serialize_group_key(
group, raft::metadata_key::config_latest_known_offset);
co_await api.kvs().put(
storage::kvstore::key_space::consensus,
key,
reflection::to_iobuf(get_prev_offset(max_rp_offset)));
reflection::to_iobuf(max_rp_offset));

// Prepare Raft snapshot
raft::group_configuration group_config(
initial_nodes, ntp_cfg.get_revision());
raft::snapshot_metadata meta = {
.last_included_index = get_prev_offset(min_rp_offset),
.last_included_index = min_rp_offset,
.last_included_term = last_included_term,
.version = raft::snapshot_metadata::current_version,
.latest_configuration = std::move(group_config),
Expand Down

0 comments on commit b25557b

Please sign in to comment.