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

[v24.3.x] raft: Make load_snapshot exception safe #24672

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 32 additions & 15 deletions src/v/raft/persisted_stm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <seastar/core/coroutine.hh>
#include <seastar/core/future.hh>

#include <exception>
#include <filesystem>
namespace raft {
namespace {
Expand Down Expand Up @@ -119,24 +120,40 @@ file_backed_stm_snapshot::load_snapshot() {
}

storage::snapshot_reader& reader = *maybe_reader;
iobuf meta_buf = co_await reader.read_metadata();
iobuf_parser meta_parser(std::move(meta_buf));
auto header = read_snapshot_header(meta_parser, _ntp, name());
if (!header) {
vlog(_log.warn, "Skipping snapshot {} due to old format", store_path());

// can't load old format of the snapshot, since snapshot is missing
// it will be reconstructed by replaying the log
co_await reader.close();
co_return std::nullopt;
}
stm_snapshot snapshot;
snapshot.header = *header;
snapshot.data = co_await read_iobuf_exactly(
reader.input(), snapshot.header.snapshot_size);
std::exception_ptr ex{nullptr};
try {
iobuf meta_buf = co_await reader.read_metadata();
iobuf_parser meta_parser(std::move(meta_buf));
auto header = read_snapshot_header(meta_parser, _ntp, name());
if (!header) {
vlog(
_log.warn,
"Skipping snapshot {} due to old format",
store_path());

_snapshot_size = co_await reader.get_snapshot_size();
// can't load old format of the snapshot, since snapshot is missing
// it will be reconstructed by replaying the log
co_await reader.close();
co_return std::nullopt;
}
snapshot.header = *header;
snapshot.data = co_await read_iobuf_exactly(
reader.input(), snapshot.header.snapshot_size);

_snapshot_size = co_await reader.get_snapshot_size();
} catch (...) {
ex = std::current_exception();
vlog(
_log.warn,
"Exception thrown while reading local snapshot: {}, exception: {}",
store_path(),
ex);
}
co_await reader.close();
if (ex != nullptr) {
std::rethrow_exception(ex);
}
co_await _snapshot_mgr.remove_partial_snapshots();

co_return snapshot;
Expand Down
Loading