16namespace fs = std::filesystem;
25 const fs::path snapshot_dir;
26 const std::optional<fs::path> read_snapshot_dir = std::nullopt;
27 struct PendingSnapshot
30 std::shared_ptr<std::vector<uint8_t>> snapshot;
32 std::map<size_t, PendingSnapshot> pending_snapshots;
36 const std::string& snapshot_dir_,
38 const std::optional<std::string>& read_snapshot_dir_ = std::nullopt) :
39 to_enclave(writer_factory.create_writer_to_inside()),
40 snapshot_dir(snapshot_dir_),
41 read_snapshot_dir(read_snapshot_dir_)
43 if (fs::is_directory(snapshot_dir))
46 "Snapshots will be stored in existing directory: {}", snapshot_dir);
51 "Creating snapshot directory - create_directory({})", snapshot_dir));
52 if (!fs::create_directory(snapshot_dir))
54 throw std::logic_error(fmt::format(
55 "Could not create snapshot directory: {}", snapshot_dir));
60 read_snapshot_dir.has_value() &&
61 !fs::is_directory(read_snapshot_dir.value()))
63 throw std::logic_error(fmt::format(
64 "{} read-only snapshot is not a directory",
65 read_snapshot_dir.value()));
80 size_t requested_size)
82 auto snapshot = std::make_shared<std::vector<uint8_t>>(requested_size);
83 pending_snapshots.emplace(idx, PendingSnapshot{evidence_idx, snapshot});
86 "Added pending snapshot {} [{} bytes]", idx, requested_size);
91#define THROW_ON_ERROR(x, name) \
97 throw std::runtime_error( \
99 "Error ({}) writing snapshot {} in " #x, \
108 const std::filesystem::path
dir;
122 fmt::format(
"Committing snapshot - fsync({})", data->tmp_file_name));
123 fsync(data->snapshot_fd);
128 "Closing snapshot file - close({})", data->tmp_file_name));
129 close(data->snapshot_fd);
133 data->committed_file_name =
134 fmt::format(
"{}{}", data->tmp_file_name, snapshot_committed_suffix);
135 const auto full_committed_path = data->dir / data->committed_file_name;
137 const auto full_tmp_path = data->dir / data->tmp_file_name;
140 "Renaming snapshot to committed - rename({})", data->tmp_file_name));
141 files::rename(full_tmp_path, full_committed_path);
146 uv_work_t* req,
int )
151 "Renamed temporary snapshot {} to {}",
153 data->committed_file_name);
161 const uint8_t* receipt_data,
165 fmt::format(
"Committing snapshot - snapshot_idx={}", snapshot_idx));
169 for (
auto it = pending_snapshots.begin(); it != pending_snapshots.end();
172 if (snapshot_idx == it->first)
175 auto file_name = fmt::format(
177 snapshot_file_prefix,
178 snapshot_idx_delimiter,
180 snapshot_idx_delimiter,
181 it->second.evidence_idx);
182 auto full_snapshot_path = snapshot_dir / file_name;
184 int snapshot_fd = -1;
187 fmt::format(
"Opening snapshot file - open({})", file_name));
189 full_snapshot_path.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0664);
191 if (snapshot_fd == -1)
198 "Cannot write snapshot as file already exists: {}",
204 "Cannot write snapshot: error ({}) opening file {}",
211 const auto& snapshot = it->second.snapshot;
215 "Writing snapshot data ({} bytes) - write({})",
220 write(snapshot_fd, snapshot->data(), snapshot->size()),
225 "Writing snapshot receipt ({} bytes) - write({})",
230 write(snapshot_fd, receipt_data, receipt_size), file_name);
234 "New snapshot file written to {} [{} bytes] (unsynced)",
236 snapshot->size() + receipt_size);
241 auto* work_handle =
new uv_work_t;
247 .tmp_file_name = file_name,
248 .snapshot_fd = snapshot_fd,
249 .committed_file_name = {}};
251 work_handle->data = data;
254#ifdef TEST_MODE_EXECUTE_SYNC_INLINE
266 pending_snapshots.erase(it);
272 LOG_FAIL_FMT(
"Could not find snapshot to commit at {}", snapshot_idx);
274 catch (std::exception& e)
277 "Exception while attempting to commit snapshot at {}: {}",
286 std::vector<fs::path> directories;
287 directories.push_back(snapshot_dir);
288 if (read_snapshot_dir.has_value())
290 directories.push_back(read_snapshot_dir.value());
300 ::consensus::snapshot_allocate,
301 [
this](
const uint8_t* data,
size_t size) {
302 auto idx = serialized::read<::consensus::Index>(data, size);
303 auto evidence_idx = serialized::read<::consensus::Index>(data, size);
304 auto requested_size = serialized::read<size_t>(data, size);
305 auto generation_count = serialized::read<uint32_t>(data, size);
311 ::consensus::snapshot_allocated,
313 std::span<uint8_t>{snapshot->data(), snapshot->size()},
319 ::consensus::snapshot_commit,
320 [
this](
const uint8_t* data,
size_t size) {
321 auto snapshot_idx = serialized::read<::consensus::Index>(data, size);
Definition messaging.h:38
Definition ring_buffer_types.h:157
Definition snapshot_manager.h:21
fs::path get_main_directory() const
Definition snapshot_manager.h:72
SnapshotManager(const std::string &snapshot_dir_, ringbuffer::AbstractWriterFactory &writer_factory, const std::optional< std::string > &read_snapshot_dir_=std::nullopt)
Definition snapshot_manager.h:35
void commit_snapshot(::consensus::Index snapshot_idx, const uint8_t *receipt_data, size_t receipt_size)
Definition snapshot_manager.h:159
SnapshotManager & operator=(const SnapshotManager &)=delete
static void on_snapshot_sync_and_rename_complete(uv_work_t *req, int)
Definition snapshot_manager.h:145
SnapshotManager(const SnapshotManager &)=delete
static void on_snapshot_sync_and_rename(uv_work_t *req)
Definition snapshot_manager.h:116
std::optional< fs::path > find_latest_committed_snapshot()
Definition snapshot_manager.h:284
void register_message_handlers(messaging::Dispatcher< ringbuffer::Message > &disp)
Definition snapshot_manager.h:295
std::shared_ptr< std::vector< uint8_t > > add_pending_snapshot(::consensus::Index idx, ::consensus::Index evidence_idx, size_t requested_size)
Definition snapshot_manager.h:77
#define LOG_INFO_FMT
Definition internal_logger.h:15
#define LOG_DEBUG_FMT
Definition internal_logger.h:14
#define LOG_FAIL_FMT
Definition internal_logger.h:16
#define DISPATCHER_SET_MESSAGE_HANDLER(DISP, MSG,...)
Definition messaging.h:292
uint64_t Index
Definition ledger_enclave_types.h:11
std::shared_ptr< AbstractWriter > WriterPtr
Definition ring_buffer_types.h:154
std::optional< fs::path > find_latest_committed_snapshot_in_directories(const std::vector< fs::path > &directories, std::optional< size_t > minimum_idx=std::nullopt)
Definition filenames.h:224
#define RINGBUFFER_WRITE_MESSAGE(MSG,...)
Definition ring_buffer_types.h:259
#define THROW_ON_ERROR(x, name)
Definition snapshot_manager.h:91
Definition time_bound_logger.h:14
Definition snapshot_manager.h:106
const std::filesystem::path dir
Definition snapshot_manager.h:108
const int snapshot_fd
Definition snapshot_manager.h:110
const std::string tmp_file_name
Definition snapshot_manager.h:109
std::string committed_file_name
Definition snapshot_manager.h:113