16namespace fs = std::filesystem;
25 const fs::path snapshot_dir;
26 const std::optional<fs::path> read_snapshot_dir = std::nullopt;
28 struct PendingSnapshot
31 std::shared_ptr<std::vector<uint8_t>> snapshot;
33 std::map<size_t, PendingSnapshot> pending_snapshots;
37 const std::string& snapshot_dir_,
39 const std::optional<std::string>& read_snapshot_dir_ = std::nullopt) :
40 to_enclave(writer_factory.create_writer_to_inside()),
41 snapshot_dir(snapshot_dir_),
42 read_snapshot_dir(read_snapshot_dir_)
44 if (fs::is_directory(snapshot_dir))
47 "Snapshots will be stored in existing directory: {}", snapshot_dir);
49 else if (!fs::create_directory(snapshot_dir))
51 throw std::logic_error(
52 fmt::format(
"Could not create snapshot directory: {}", snapshot_dir));
56 read_snapshot_dir.has_value() &&
57 !fs::is_directory(read_snapshot_dir.value()))
59 throw std::logic_error(fmt::format(
60 "{} read-only snapshot is not a directory",
61 read_snapshot_dir.value()));
73 size_t requested_size)
75 auto snapshot = std::make_shared<std::vector<uint8_t>>(requested_size);
76 pending_snapshots.emplace(idx, PendingSnapshot{evidence_idx, snapshot});
79 "Added pending snapshot {} [{} bytes]", idx, requested_size);
84#define THROW_ON_ERROR(x, name) \
90 throw std::runtime_error(fmt::format( \
91 "Error ({}) writing snapshot {} in " #x, strerror(errno), name)); \
98 const std::filesystem::path
dir;
112 fmt::format(
"Committing snapshot - fsync({})", data->tmp_file_name));
113 fsync(data->snapshot_fd);
116 close(data->snapshot_fd);
119 data->committed_file_name =
120 fmt::format(
"{}{}", data->tmp_file_name, snapshot_committed_suffix);
121 const auto full_committed_path = data->dir / data->committed_file_name;
123 const auto full_tmp_path = data->dir / data->tmp_file_name;
124 files::rename(full_tmp_path, full_committed_path);
132 "Renamed temporary snapshot {} to {}",
134 data->committed_file_name);
142 const uint8_t* receipt_data,
146 fmt::format(
"Committing snapshot - snapshot_idx={}", snapshot_idx));
150 for (
auto it = pending_snapshots.begin(); it != pending_snapshots.end();
153 if (snapshot_idx == it->first)
156 auto file_name = fmt::format(
158 snapshot_file_prefix,
159 snapshot_idx_delimiter,
161 snapshot_idx_delimiter,
162 it->second.evidence_idx);
163 auto full_snapshot_path = snapshot_dir / file_name;
165 int snapshot_fd = open(
166 full_snapshot_path.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0664);
167 if (snapshot_fd == -1)
174 "Cannot write snapshot as file already exists: {}",
180 "Cannot write snapshot: error ({}) opening file {}",
187 const auto& snapshot = it->second.snapshot;
190 write(snapshot_fd, snapshot->data(), snapshot->size()),
193 write(snapshot_fd, receipt_data, receipt_size), file_name);
196 "New snapshot file written to {} [{} bytes] (unsynced)",
198 snapshot->size() + receipt_size);
202 uv_work_t* work_handle =
new uv_work_t;
207 .tmp_file_name = file_name,
208 .snapshot_fd = snapshot_fd};
210 work_handle->data = data;
213#ifdef TEST_MODE_EXECUTE_SYNC_INLINE
225 pending_snapshots.erase(it);
231 LOG_FAIL_FMT(
"Could not find snapshot to commit at {}", snapshot_idx);
233 catch (std::exception& e)
236 "Exception while attempting to commit snapshot at {}: {}",
243 std::optional<std::pair<fs::path, fs::path>>
247 size_t latest_idx = 0;
249 std::optional<fs::path> read_only_latest_committed_snapshot =
251 if (read_snapshot_dir.has_value())
253 read_only_latest_committed_snapshot =
255 read_snapshot_dir.value(), latest_idx);
258 auto main_latest_committed_snapshot =
261 if (main_latest_committed_snapshot.has_value())
263 return std::make_pair(
264 snapshot_dir, main_latest_committed_snapshot.value());
266 else if (read_only_latest_committed_snapshot.has_value())
268 return std::make_pair(
269 read_snapshot_dir.value(),
270 read_only_latest_committed_snapshot.value());
281 ::consensus::snapshot_allocate,
282 [
this](
const uint8_t* data,
size_t size) {
283 auto idx = serialized::read<::consensus::Index>(data, size);
284 auto evidence_idx = serialized::read<::consensus::Index>(data, size);
285 auto requested_size = serialized::read<size_t>(data, size);
286 auto generation_count = serialized::read<uint32_t>(data, size);
292 ::consensus::snapshot_allocated,
294 std::span<uint8_t>{snapshot->data(), snapshot->size()},
300 ::consensus::snapshot_commit,
301 [
this](
const uint8_t* data,
size_t size) {
302 auto snapshot_idx = serialized::read<::consensus::Index>(data, size);
Definition messaging.h:38
Definition ring_buffer_types.h:153
Definition snapshot_manager.h:21
fs::path get_main_directory() const
Definition snapshot_manager.h:65
SnapshotManager(const std::string &snapshot_dir_, ringbuffer::AbstractWriterFactory &writer_factory, const std::optional< std::string > &read_snapshot_dir_=std::nullopt)
Definition snapshot_manager.h:36
static void on_snapshot_sync_and_rename_complete(uv_work_t *req, int status)
Definition snapshot_manager.h:127
void commit_snapshot(::consensus::Index snapshot_idx, const uint8_t *receipt_data, size_t receipt_size)
Definition snapshot_manager.h:140
static void on_snapshot_sync_and_rename(uv_work_t *req)
Definition snapshot_manager.h:106
void register_message_handlers(messaging::Dispatcher< ringbuffer::Message > &disp)
Definition snapshot_manager.h:276
std::optional< std::pair< fs::path, fs::path > > find_latest_committed_snapshot()
Definition snapshot_manager.h:244
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:70
#define LOG_INFO_FMT
Definition logger.h:362
#define LOG_DEBUG_FMT
Definition logger.h:357
#define LOG_FAIL_FMT
Definition logger.h:363
#define DISPATCHER_SET_MESSAGE_HANDLER(DISP, MSG,...)
Definition messaging.h:316
uint64_t Index
Definition ledger_enclave_types.h:11
std::shared_ptr< AbstractWriter > WriterPtr
Definition ring_buffer_types.h:150
std::optional< fs::path > find_latest_committed_snapshot_in_directory(const fs::path &directory, size_t &latest_committed_snapshot_idx)
Definition filenames.h:135
#define RINGBUFFER_WRITE_MESSAGE(MSG,...)
Definition ring_buffer_types.h:255
#define THROW_ON_ERROR(x, name)
Definition snapshot_manager.h:84
Definition time_bound_logger.h:14
Definition snapshot_manager.h:96
const std::filesystem::path dir
Definition snapshot_manager.h:98
const int snapshot_fd
Definition snapshot_manager.h:100
const std::string tmp_file_name
Definition snapshot_manager.h:99
std::string committed_file_name
Definition snapshot_manager.h:103