Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
duckdb::WriteAheadLogReplayer Struct Reference
Collaboration diagram for duckdb::WriteAheadLogReplayer:

Public Member Functions

 WriteAheadLogReplayer (QueryContext context, StorageManager &storage_manager, const string &wal_path)
unique_ptr< WriteAheadLog > Replay ()

Private Member Functions

unique_ptr< WriteAheadLog > ReplayLog (unique_ptr< FileHandle > handle, WALReplayState replay_state=WALReplayState::MAIN_WAL)
void CopyOverWAL (BufferedFileReader &reader, FileHandle &target, data_ptr_t buffer, idx_t buffer_size, idx_t copy_end)
void MergeIntoRecoveryWAL (Connection &con, const ReplayState &checkpoint_state, BufferedFileReader &main_wal_reader, const string &recovery_path, unique_ptr< FileHandle > checkpoint_handle)

Private Attributes

QueryContext context
StorageManager & storage_manager
AttachedDatabase & database
const string & main_wal_path
FileSystem & fs

Constructor & Destructor Documentation

◆ WriteAheadLogReplayer()

duckdb::WriteAheadLogReplayer::WriteAheadLogReplayer ( QueryContext context,
StorageManager & storage_manager,
const string & wal_path )
444098 : context(context), storage_manager(storage_manager), database(storage_manager.GetAttached()),
444099 main_wal_path(main_wal_path), fs(FileSystem::Get(storage_manager.GetAttached())) {
444100}

Member Function Documentation

◆ Replay()

unique_ptr< WriteAheadLog > duckdb::WriteAheadLogReplayer::Replay ( )
444102 {
444103 auto handle = fs.OpenFile(main_wal_path, FileFlags::FILE_FLAGS_READ | FileFlags::FILE_FLAGS_NULL_IF_NOT_EXISTS);
444104 if (!handle) {
444105 // WAL does not exist - instantiate an empty WAL
444106 return make_uniq<WriteAheadLog>(storage_manager, main_wal_path);
444107 }
444108
444109 // context is passed for metric collection purposes only!!
444110 auto wal_handle = ReplayLog(std::move(handle));
444111 if (wal_handle) {
444112 return wal_handle;
444113 }
444114 // replay returning NULL indicates we can nuke the WAL entirely - but only if this is not a read-only connection
444115 if (!storage_manager.GetAttached().IsReadOnly()) {
444116 fs.TryRemoveFile(main_wal_path);
444117 }
444118 return make_uniq<WriteAheadLog>(storage_manager, main_wal_path);
444119}
static constexpr FileOpenFlags FILE_FLAGS_NULL_IF_NOT_EXISTS
Return NULL if the file does not exist instead of throwing an error.
Definition duckdb.hpp:7865
static constexpr FileOpenFlags FILE_FLAGS_READ
Open file with read access.
Definition duckdb.hpp:7850

◆ ReplayLog()

unique_ptr< WriteAheadLog > duckdb::WriteAheadLogReplayer::ReplayLog ( unique_ptr< FileHandle > handle,
WALReplayState replay_state = WALReplayState::MAIN_WAL )
private
444192 {
444193 auto &database = storage_manager.GetAttached();
444194 Connection con(database.GetDatabase());
444195 auto wal_path = handle->GetPath();
444196 BufferedFileReader reader(FileSystem::Get(database), std::move(handle));
444197 if (reader.Finished()) {
444198 // WAL file exists, but it is empty - we can delete the file
444199 return nullptr;
444200 }
444201
444202 con.BeginTransaction();
444203 MetaTransaction::Get(*con.context).ModifyDatabase(database, DatabaseModificationType());
444204
444205 auto &config = DBConfig::GetConfig(database.GetDatabase());
444206 // first deserialize the WAL to look for a checkpoint flag
444207 // if there is a checkpoint flag, we might have already flushed the contents of the WAL to disk
444208 ReplayState checkpoint_state(database, *con.context, replay_state);
444209 try {
444210 idx_t replay_entry_count = 0;
444211 while (true) {
444212 replay_entry_count++;
444213 // read the current entry (deserialize only)
444214 checkpoint_state.current_position = reader.CurrentOffset();
444215 auto deserializer = WriteAheadLogDeserializer::GetEntryDeserializer(checkpoint_state, reader, true);
444216 if (deserializer.ReplayEntry()) {
444217 // check if the file is exhausted
444218 if (reader.Finished()) {
444219 // we finished reading the file: break
444220 break;
444221 }
444222 }
444223 }
444224 auto client_context = context.GetClientContext();
444225 if (client_context) {
444226 auto &profiler = *client_context->client_data->profiler;
444227 profiler.AddToCounter(MetricType::WAL_REPLAY_ENTRY_COUNT, replay_entry_count);
444228 }
444229 } catch (std::exception &ex) { // LCOV_EXCL_START
444230 ErrorData error(ex);
444231 // ignore serialization exceptions - they signal a torn WAL
444232 if (error.Type() != ExceptionType::SERIALIZATION) {
444233 error.Throw("Failure while replaying WAL file \"" + wal_path + "\": ");
444234 }
444235 } // LCOV_EXCL_STOP
444236 unique_ptr<FileHandle> checkpoint_handle;
444237 if (checkpoint_state.checkpoint_id.IsValid()) {
444238 if (replay_state == WALReplayState::CHECKPOINT_WAL) {
444239 throw InvalidInputException(
444240 "Failure while replaying checkpoint WAL file \"%s\": checkpoint WAL cannot contain a checkpoint marker",
444241 wal_path);
444242 }
444243 // there is a checkpoint flag
444244 // this means a checkpoint was on-going when we crashed
444245 // we need to reconcile this with what is in the data file
444246 // first check if there is a checkpoint WAL
444247 auto &manager = database.GetStorageManager();
444248 auto checkpoint_wal = manager.GetCheckpointWALPath();
444249 checkpoint_handle =
444251 bool checkpoint_was_successful = manager.IsCheckpointClean(checkpoint_state.checkpoint_id);
444252 if (!checkpoint_handle) {
444253 // no checkpoint WAL - either we just need to replay this WAL, or we are done
444254 if (checkpoint_was_successful) {
444255 // the contents of the WAL have already been checkpointed and there is no checkpoint WAL - we are done
444256 return nullptr;
444257 }
444258 } else {
444259 // we have a checkpoint WAL
444260 if (checkpoint_was_successful) {
444261 // the checkpoint was successful
444262 // the main WAL is no longer needed, we only need to replay the checkpoint WAL
444263 // if this is a read-only connection then replay the checkpoint WAL directly
444264 if (storage_manager.GetAttached().IsReadOnly()) {
444265 return ReplayLog(std::move(checkpoint_handle), WALReplayState::CHECKPOINT_WAL);
444266 }
444267 // if this is not a read-only connection we need to finish the checkpoint
444268 // overwrite the current WAL with the checkpoint WAL
444269 checkpoint_handle.reset();
444270
444271 fs.MoveFile(checkpoint_wal, wal_path);
444272
444273 // now open the handle again and replay the checkpoint WAL
444274 checkpoint_handle =
444276 return ReplayLog(std::move(checkpoint_handle), WALReplayState::CHECKPOINT_WAL);
444277 }
444278 // the checkpoint was unsuccessful
444279 // this means we need to replay both this WAL and the checkpoint WAL
444280 // if this is a read-only connection - replay both WAL files
444281 if (!storage_manager.GetAttached().IsReadOnly()) {
444282 // if this is not a read-only connection, then merge the two WALs and replay the merged WAL
444283 // we merge into the recovery WAL path
444284 auto recovery_path = manager.GetRecoveryWALPath();
444285 MergeIntoRecoveryWAL(con, checkpoint_state, reader, recovery_path, std::move(checkpoint_handle));
444286
444287 // replay the (combined) recovery WAL
444288 auto main_handle = fs.OpenFile(wal_path, FileFlags::FILE_FLAGS_READ);
444289 return ReplayLog(std::move(main_handle), WALReplayState::CHECKPOINT_WAL);
444290 }
444291 }
444292 }
444293 if (checkpoint_state.expected_checkpoint_id.IsValid()) {
444294 // we expected a checkpoint id - but no checkpoint has happened - abort!
444295 auto expected_id = checkpoint_state.expected_checkpoint_id.GetIndex();
444296 WriteAheadLogDeserializer::ThrowVersionError(expected_id - 1, expected_id);
444297 }
444298
444299 // we need to recover from the WAL: actually set up the replay state
444300 ReplayState state(database, *con.context, replay_state);
444301
444302 // reset the reader - we are going to read the WAL from the beginning again
444303 reader.Reset();
444304
444305 // replay the WAL
444306 // note that everything is wrapped inside a try/catch block here
444307 // there can be errors in WAL replay because of a corrupt WAL file
444308 idx_t successful_offset = 0;
444309 bool all_succeeded = false;
444310 try {
444311 while (true) {
444312 // read the current entry
444313 auto deserializer = WriteAheadLogDeserializer::GetEntryDeserializer(state, reader);
444314 if (deserializer.ReplayEntry()) {
444315 con.Commit();
444316
444317 // Commit any outstanding indexes.
444318 for (auto &info : state.replay_index_infos) {
444319 info.index_list.get().AddIndex(std::move(info.index));
444320 }
444321 state.replay_index_infos.clear();
444322
444323 successful_offset = reader.CurrentOffset();
444324 // check if the file is exhausted
444325 if (reader.Finished()) {
444326 // we finished reading the file: break
444327 all_succeeded = true;
444328 break;
444329 }
444330 con.BeginTransaction();
444331 MetaTransaction::Get(*con.context).ModifyDatabase(database, DatabaseModificationType());
444332 }
444333 }
444334 } catch (std::exception &ex) { // LCOV_EXCL_START
444335 // exception thrown in WAL replay: rollback
444336 con.Query("ROLLBACK");
444337 ErrorData error(ex);
444338 // serialization failure means a truncated WAL
444339 // these failures are ignored unless abort_on_wal_failure is true
444340 // other failures always result in an error
444341 if (config.options.abort_on_wal_failure || error.Type() != ExceptionType::SERIALIZATION) {
444342 error.Throw("Failure while replaying WAL file \"" + wal_path + "\": ");
444343 }
444344 } catch (...) {
444345 // exception thrown in WAL replay: rollback
444346 con.Query("ROLLBACK");
444347 throw;
444348 } // LCOV_EXCL_STOP
444349 if (all_succeeded && checkpoint_handle) {
444350 // we have successfully replayed the main WAL - but there is still a checkpoint WAL remaining
444351 // this can only happen in read-only mode
444352 // replay the checkpoint WAL and return
444353 return ReplayLog(std::move(checkpoint_handle), WALReplayState::CHECKPOINT_WAL);
444354 }
444355 auto init_state = all_succeeded ? WALInitState::UNINITIALIZED : WALInitState::UNINITIALIZED_REQUIRES_TRUNCATE;
444356 return make_uniq<WriteAheadLog>(storage_manager, wal_path, successful_offset, init_state);
444357}
static WriteAheadLogDeserializer GetEntryDeserializer(ReplayState &state_p, BufferedFileReader &stream, bool deserialize_only=false)
Definition duckdb.cpp:443877
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

◆ CopyOverWAL()

void duckdb::WriteAheadLogReplayer::CopyOverWAL ( BufferedFileReader & reader,
FileHandle & target,
data_ptr_t buffer,
idx_t buffer_size,
idx_t copy_end )
private
444122 {
444123 while (!reader.Finished()) {
444124 idx_t read_count = MinValue<idx_t>(buffer_size, copy_end - reader.CurrentOffset());
444125 if (read_count == 0) {
444126 break;
444127 }
444128 reader.ReadData(context, buffer, read_count);
444129
444130 target.Write(buffer, read_count);
444131 }
444132}

◆ MergeIntoRecoveryWAL()

void duckdb::WriteAheadLogReplayer::MergeIntoRecoveryWAL ( Connection & con,
const ReplayState & checkpoint_state,
BufferedFileReader & main_wal_reader,
const string & recovery_path,
unique_ptr< FileHandle > checkpoint_handle )
private
444136 {
444137 auto recovery_handle =
444139
444140 static constexpr idx_t BATCH_SIZE = Storage::DEFAULT_BLOCK_SIZE;
444141 auto buffer = make_uniq_array<data_t>(BATCH_SIZE);
444142
444143 // first copy over the main WAL contents
444144 auto copy_end = checkpoint_state.checkpoint_position.GetIndex();
444145 main_wal_reader.Reset();
444146 CopyOverWAL(main_wal_reader, *recovery_handle, buffer.get(), BATCH_SIZE, copy_end);
444147
444148 auto checkpoint_wal_path = checkpoint_handle->GetPath();
444149 // now copy over the checkpoint WAL
444150 {
444151 BufferedFileReader checkpoint_reader(fs, std::move(checkpoint_handle));
444152
444153 if (checkpoint_reader.FileSize() != 0) {
444154 // skip over the version entry
444155 ReplayState checkpoint_replay_state(database, *con.context, WALReplayState::CHECKPOINT_WAL);
444156
444157 auto deserializer =
444158 WriteAheadLogDeserializer::GetEntryDeserializer(checkpoint_replay_state, checkpoint_reader, true);
444159 deserializer.ReplayEntry();
444160
444161 if (checkpoint_replay_state.wal_version != checkpoint_state.wal_version) {
444162 throw InvalidInputException("Failure while replaying checkpoint WAL file \"%s\": checkpoint "
444163 "WAL version is different from main WAL version",
444164 checkpoint_wal_path);
444165 }
444166
444167 CopyOverWAL(checkpoint_reader, *recovery_handle, buffer.get(), BATCH_SIZE, checkpoint_reader.FileSize());
444168 }
444169 }
444170
444171 auto debug_checkpoint_abort = Settings::Get<DebugCheckpointAbortSetting>(storage_manager.GetDatabase());
444172
444173 // move over the recovery WAL over the main WAL
444174 recovery_handle->Sync();
444175 recovery_handle.reset();
444176
444177 if (debug_checkpoint_abort == CheckpointAbort::DEBUG_ABORT_BEFORE_MOVING_RECOVERY) {
444178 throw FatalException("Checkpoint aborted before moving recovery file because of PRAGMA checkpoint_abort flag");
444179 }
444180
444181 fs.MoveFile(recovery_path, main_wal_path);
444182
444183 if (debug_checkpoint_abort == CheckpointAbort::DEBUG_ABORT_BEFORE_DELETING_CHECKPOINT_WAL) {
444184 throw FatalException(
444185 "Checkpoint aborted before deleting checkpoint file because of PRAGMA checkpoint_abort flag");
444186 }
444187
444188 // delete the checkpoint WAL
444189 fs.RemoveFile(checkpoint_wal_path);
444190}
static constexpr FileOpenFlags FILE_FLAGS_FILE_CREATE_NEW
Always create a new file. If a file exists, the file is truncated. Cannot be used together with CREAT...
Definition duckdb.hpp:7858
static constexpr FileOpenFlags FILE_FLAGS_WRITE
Open file with write access.
Definition duckdb.hpp:7852
static constexpr idx_t DEFAULT_BLOCK_SIZE
The default block size.
Definition duckdb.hpp:9720

The documentation for this struct was generated from the following file: