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
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
444207
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
444214 checkpoint_state.current_position = reader.CurrentOffset();
444216 if (deserializer.ReplayEntry()) {
444217
444218 if (reader.Finished()) {
444219
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) {
444230 ErrorData error(ex);
444231
444232 if (error.Type() != ExceptionType::SERIALIZATION) {
444233 error.Throw("Failure while replaying WAL file \"" + wal_path + "\": ");
444234 }
444235 }
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
444244
444245
444246
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
444254 if (checkpoint_was_successful) {
444255
444256 return nullptr;
444257 }
444258 } else {
444259
444260 if (checkpoint_was_successful) {
444261
444262
444263
444264 if (storage_manager.GetAttached().IsReadOnly()) {
444265 return ReplayLog(std::move(checkpoint_handle), WALReplayState::CHECKPOINT_WAL);
444266 }
444267
444268
444269 checkpoint_handle.reset();
444270
444271 fs.MoveFile(checkpoint_wal, wal_path);
444272
444273
444274 checkpoint_handle =
444276 return ReplayLog(std::move(checkpoint_handle), WALReplayState::CHECKPOINT_WAL);
444277 }
444278
444279
444280
444281 if (!storage_manager.GetAttached().IsReadOnly()) {
444282
444283
444284 auto recovery_path = manager.GetRecoveryWALPath();
444285 MergeIntoRecoveryWAL(con, checkpoint_state, reader, recovery_path, std::move(checkpoint_handle));
444286
444287
444289 return ReplayLog(std::move(main_handle), WALReplayState::CHECKPOINT_WAL);
444290 }
444291 }
444292 }
444293 if (checkpoint_state.expected_checkpoint_id.IsValid()) {
444294
444295 auto expected_id = checkpoint_state.expected_checkpoint_id.GetIndex();
444296 WriteAheadLogDeserializer::ThrowVersionError(expected_id - 1, expected_id);
444297 }
444298
444299
444300 ReplayState state(database, *con.context, replay_state);
444301
444302
444303 reader.Reset();
444304
444305
444306
444307
444308 idx_t successful_offset = 0;
444309 bool all_succeeded = false;
444310 try {
444311 while (true) {
444312
444314 if (deserializer.ReplayEntry()) {
444315 con.Commit();
444316
444317
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
444325 if (reader.Finished()) {
444326
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) {
444335
444336 con.Query("ROLLBACK");
444337 ErrorData error(ex);
444338
444339
444340
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
444346 con.Query("ROLLBACK");
444347 throw;
444348 }
444349 if (all_succeeded && checkpoint_handle) {
444350
444351
444352
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