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::SingleFileCheckpointWriter Class Referencefinal
Inheritance diagram for duckdb::SingleFileCheckpointWriter:
Collaboration diagram for duckdb::SingleFileCheckpointWriter:

Public Member Functions

 SingleFileCheckpointWriter (QueryContext context, AttachedDatabase &db, BlockManager &block_manager, CheckpointOptions options)
void CreateCheckpoint () override
MetadataWriter & GetMetadataWriter () override
MetadataManager & GetMetadataManager () override
unique_ptr< TableDataWriter > GetTableDataWriter (TableCatalogEntry &table) override
BlockManager & GetBlockManager ()
CheckpointOptions GetCheckpointOptions () const
optional_ptr< ClientContext > GetClientContext () const
void WriteTable (TableCatalogEntry &table, Serializer &serializer) override
Public Member Functions inherited from duckdb::CheckpointWriter
 CheckpointWriter (AttachedDatabase &db)

Private Attributes

optional_ptr< ClientContext > context
unique_ptr< MetadataWriter > metadata_writer
 The metadata writer is responsible for writing schema information.
unique_ptr< MetadataWriter > table_metadata_writer
 The table data writer is responsible for writing the DataPointers used by the table chunks.
PartialBlockManager partial_block_manager
CheckpointOptions options
 Checkpoint type.
unordered_map< block_id_t, idx_t > verify_block_usage_count
 Block usage count for verification purposes.

Friends

class SingleFileRowGroupWriter
class SingleFileTableDataWriter

Additional Inherited Members

Public Attributes inherited from duckdb::CheckpointWriter
AttachedDatabase & db
 The database.
Protected Member Functions inherited from duckdb::CheckpointWriter
virtual void WriteEntry (CatalogEntry &entry, Serializer &serializer)
virtual void WriteSchema (SchemaCatalogEntry &schema, Serializer &serializer)
virtual void WriteView (ViewCatalogEntry &table, Serializer &serializer)
virtual void WriteSequence (SequenceCatalogEntry &table, Serializer &serializer)
virtual void WriteMacro (ScalarMacroCatalogEntry &table, Serializer &serializer)
virtual void WriteTableMacro (TableMacroCatalogEntry &table, Serializer &serializer)
virtual void WriteIndex (IndexCatalogEntry &index_catalog_entry, Serializer &serializer)
virtual void WriteType (TypeCatalogEntry &type, Serializer &serializer)

Constructor & Destructor Documentation

◆ SingleFileCheckpointWriter()

duckdb::SingleFileCheckpointWriter::SingleFileCheckpointWriter ( QueryContext context,
AttachedDatabase & db,
BlockManager & block_manager,
CheckpointOptions options )
389736 : CheckpointWriter(db), context(context.GetClientContext()),
389737 partial_block_manager(context, block_manager, PartialBlockType::FULL_CHECKPOINT), options(options_p) {
389738}
AttachedDatabase & db
The database.
Definition duckdb.cpp:388889
PartialBlockManager partial_block_manager
Definition duckdb.cpp:388984
CheckpointOptions options
Checkpoint type.
Definition duckdb.cpp:388986

Member Function Documentation

◆ CreateCheckpoint()

void duckdb::SingleFileCheckpointWriter::CreateCheckpoint ( )
overridevirtual

Set up the writers for the checkpoints

Implements duckdb::CheckpointWriter.

389825 {
389826 auto &storage_manager = db.GetStorageManager().Cast<SingleFileStorageManager>();
389827 if (storage_manager.InMemory()) {
389828 return;
389829 }
389830 if (ValidChecker::IsInvalidated(db.GetDatabase())) {
389831 // don't checkpoint invalidated databases
389832 return;
389833 }
389834 // assert that the checkpoint manager hasn't been used before
389835 D_ASSERT(!metadata_writer);
389836
389837 auto &block_manager = GetBlockManager();
389838 auto &metadata_manager = GetMetadataManager();
389839
389841 metadata_writer = make_uniq<MetadataWriter>(metadata_manager);
389842 table_metadata_writer = make_uniq<MetadataWriter>(metadata_manager);
389843
389844 // get the id of the first meta block
389845 auto meta_block = metadata_writer->GetMetaBlockPointer();
389846
389847 // write a checkpoint flag to the WAL
389848 // in case a crash happens during the checkpoint, we know a checkpoint was instantiated
389849 // we write the root meta block of the planned checkpoint to the WAL
389850 // during recovery we use this:
389851 // * if the root meta block matches the checkpoint entry, we know the checkpoint was completed
389852 // * if the root meta block does not match the checkpoint entry, we know the checkpoint was not completed
389853 // if the checkpoint was completed we don't need to replay the WAL - otherwise we need to replay the WAL
389854 // we also know if a checkpoint was running that we need to check for the checkpoint WAL (`.checkpoint.wal`)
389855 // to replay any concurrent commits that have succeeded and ensure these are not lost
389856 auto &transaction_manager = db.GetTransactionManager().Cast<DuckTransactionManager>();
389857 ActiveCheckpointWrapper active_checkpoint(transaction_manager);
389858 auto has_wal = storage_manager.WALStartCheckpoint(meta_block, options);
389859
389860 catalog_entry_vector_t catalog_entries;
389861 try {
389862 auto checkpoint_sleep_ms = Settings::Get<DebugCheckpointSleepMsSetting>(db.GetDatabase());
389863 if (checkpoint_sleep_ms > 0) {
389864 ThreadUtil::SleepMs(checkpoint_sleep_ms);
389865 }
389866
389867 vector<reference<SchemaCatalogEntry>> schemas;
389868 // we scan the set of committed schemas
389869 auto &catalog = Catalog::GetCatalog(db).Cast<DuckCatalog>();
389870 catalog.ScanSchemas([&](SchemaCatalogEntry &entry) { schemas.push_back(entry); });
389871
389872 D_ASSERT(catalog.IsDuckCatalog());
389873
389874 auto &dependency_manager = *catalog.GetDependencyManager();
389875 catalog_entries = GetCatalogEntries(schemas);
389876 dependency_manager.ReorderEntries(catalog_entries);
389877
389878 // write the actual data into the database
389879
389880 // Create a serializer to write the checkpoint data
389881 // The serialized format is roughly:
389882 /*
389883 {
389884 schemas: [
389885 {
389886 schema: <schema_info>,
389887 custom_types: [ { type: <type_info> }, ... ],
389888 sequences: [ { sequence: <sequence_info> }, ... ],
389889 tables: [ { table: <table_info> }, ... ],
389890 views: [ { view: <view_info> }, ... ],
389891 macros: [ { macro: <macro_info> }, ... ],
389892 table_macros: [ { table_macro: <table_macro_info> }, ... ],
389893 indexes: [ { index: <index_info>, root_offset <block_ptr> }, ... ]
389894 }
389895 ]
389896 }
389897 */
389898 BinarySerializer serializer(*metadata_writer, SerializationOptions(db));
389899 serializer.Begin();
389900 serializer.WriteList(100, "catalog_entries", catalog_entries.size(), [&](Serializer::List &list, idx_t i) {
389901 auto &entry = catalog_entries[i];
389902 list.WriteObject([&](Serializer &obj) { WriteEntry(entry.get(), obj); });
389903 });
389904 serializer.End();
389905
389906 metadata_writer->Flush();
389907 table_metadata_writer->Flush();
389908
389909 auto debug_checkpoint_abort = Settings::Get<DebugCheckpointAbortSetting>(db.GetDatabase());
389910 if (debug_checkpoint_abort == CheckpointAbort::DEBUG_ABORT_BEFORE_HEADER) {
389911 throw FatalException("Checkpoint aborted before header write because of PRAGMA checkpoint_abort flag");
389912 }
389913
389914 // finally write the updated header
389915 DatabaseHeader header;
389916 header.meta_block = meta_block.block_pointer;
389917 header.block_alloc_size = block_manager.GetBlockAllocSize();
389918 header.vector_size = STANDARD_VECTOR_SIZE;
389919 block_manager.WriteHeader(context, header);
389920
389921 auto debug_verify_blocks = Settings::Get<DebugVerifyBlocksSetting>(db.GetDatabase());
389922 if (debug_verify_blocks) {
389923 // extend verify_block_usage_count
389924 auto metadata_info = storage_manager.GetMetadataInfo();
389925 for (auto &info : metadata_info) {
389926 verify_block_usage_count[info.block_id]++;
389927 }
389928 for (auto &entry_ref : catalog_entries) {
389929 auto &entry = entry_ref.get();
389930 if (entry.type != CatalogType::TABLE_ENTRY) {
389931 continue;
389932 }
389933 auto &table = entry.Cast<DuckTableEntry>();
389934 auto &storage = table.GetStorage();
389935 auto segment_info = storage.GetColumnSegmentInfo(context);
389936 for (auto &segment : segment_info) {
389937 verify_block_usage_count[segment.block_id]++;
389938 if (StringUtil::Contains(segment.segment_info, "Overflow String Block Ids: ")) {
389939 auto overflow_blocks =
389940 StringUtil::Replace(segment.segment_info, "Overflow String Block Ids: ", "");
389941 auto splits = StringUtil::Split(overflow_blocks, ", ");
389942 for (auto &split : splits) {
389943 auto overflow_block_id = std::stoll(split);
389944 verify_block_usage_count[overflow_block_id]++;
389945 }
389946 }
389947 }
389948 }
389949 block_manager.VerifyBlocks(verify_block_usage_count);
389950 }
389951
389952 if (debug_checkpoint_abort == CheckpointAbort::DEBUG_ABORT_BEFORE_TRUNCATE) {
389953 throw FatalException("Checkpoint aborted before truncate because of PRAGMA checkpoint_abort flag");
389954 }
389955
389956 // truncate the file
389957 block_manager.Truncate();
389958
389959 if (debug_checkpoint_abort == CheckpointAbort::DEBUG_ABORT_BEFORE_WAL_FINISH) {
389960 throw FatalException("Checkpoint aborted before truncate because of PRAGMA checkpoint_abort flag");
389961 }
389962
389963 // truncate the WAL
389964 if (has_wal) {
389965 unique_ptr<lock_guard<mutex>> owned_wal_lock;
389966 optional_ptr<lock_guard<mutex>> wal_lock;
389967 if (!options.wal_lock) {
389968 // not holding the WAL lock yet - grab it
389969 owned_wal_lock = storage_manager.GetWALLock();
389970 wal_lock = *owned_wal_lock;
389971 } else {
389972 // we already have the WAL lock - just refer to it
389973 wal_lock = options.wal_lock;
389974 }
389975 storage_manager.WALFinishCheckpoint(*wal_lock);
389976 }
389977 } catch (std::exception &ex) {
389978 // any exceptions thrown here are fatal
389979 ErrorData error(ex);
389980 if (error.Type() == ExceptionType::FATAL) {
389981 ValidChecker::Invalidate(db.GetDatabase(), error.Message());
389982 throw;
389983 }
389984 throw FatalException("Failed to create checkpoint: %s", error.Message());
389985 }
389986
389987 // for any indexes that were appended to while checkpointing, merge the delta back into the main index
389988 // FIXME: we only clean up appends made to tables that are part of this checkpoint
389989 // Currently, that is correct, since we don't allow creating tables DURING a checkpoint
389990 // In the future, we will allow this
389991 // When that happens, we should ensure the delta indexes are NOT used for tables created DURING a checkpoint
389992 // this is also not necessary - if we are not checkpointing a table, we are not checkpointing its indexes
389993 // ergo we don't need the delta indexes
389994 for (auto &entry_ref : catalog_entries) {
389995 auto &entry = entry_ref.get();
389996 if (entry.type != CatalogType::TABLE_ENTRY) {
389997 continue;
389998 }
389999 auto &table = entry.Cast<DuckTableEntry>();
390000 auto &storage = table.GetStorage();
390001 auto &table_info = storage.GetDataTableInfo();
390002 auto &index_list = table_info->GetIndexes();
390003 index_list.MergeCheckpointDeltas(options.transaction_id);
390004 }
390005 active_checkpoint.Clear();
390006}
static DUCKDB_API Catalog & GetCatalog(ClientContext &context, const string &catalog_name)
Get the specified Catalog from the ClientContext.
Definition duckdb.cpp:7211
unordered_map< block_id_t, idx_t > verify_block_usage_count
Block usage count for verification purposes.
Definition duckdb.cpp:388988
unique_ptr< MetadataWriter > metadata_writer
The metadata writer is responsible for writing schema information.
Definition duckdb.cpp:388979
unique_ptr< MetadataWriter > table_metadata_writer
The table data writer is responsible for writing the DataPointers used by the table chunks.
Definition duckdb.cpp:388981
static DUCKDB_API bool Contains(const string &haystack, const string &needle)
Returns true if the needle string exists in the haystack.
Definition duckdb.cpp:109323
static DUCKDB_API vector< string > Split(const string &str, char delimiter)
Split the input string based on newline char.
Definition duckdb.cpp:109734
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237
Here is the call graph for this function:

◆ GetMetadataWriter()

MetadataWriter & duckdb::SingleFileCheckpointWriter::GetMetadataWriter ( )
overridevirtual

Implements duckdb::CheckpointWriter.

389745 {
389746 return *metadata_writer;
389747}

◆ GetMetadataManager()

MetadataManager & duckdb::SingleFileCheckpointWriter::GetMetadataManager ( )
overridevirtual

Implements duckdb::CheckpointWriter.

389749 {
389750 return GetBlockManager().GetMetadataManager();
389751}

◆ GetTableDataWriter()

unique_ptr< TableDataWriter > duckdb::SingleFileCheckpointWriter::GetTableDataWriter ( TableCatalogEntry & table)
overridevirtual

Implements duckdb::CheckpointWriter.

389753 {
389754 return make_uniq<SingleFileTableDataWriter>(*this, table, *table_metadata_writer);
389755}

◆ GetBlockManager()

BlockManager & duckdb::SingleFileCheckpointWriter::GetBlockManager ( )
389740 {
389741 auto &storage_manager = db.GetStorageManager().Cast<SingleFileStorageManager>();
389742 return *storage_manager.block_manager;
389743}

◆ GetCheckpointOptions()

CheckpointOptions duckdb::SingleFileCheckpointWriter::GetCheckpointOptions ( ) const
inline
388966 {
388967 return options;
388968 }

◆ GetClientContext()

optional_ptr< ClientContext > duckdb::SingleFileCheckpointWriter::GetClientContext ( ) const
inline
388969 {
388970 return context;
388971 }

◆ WriteTable()

void duckdb::SingleFileCheckpointWriter::WriteTable ( TableCatalogEntry & table,
Serializer & serializer )
overridevirtual

Implements duckdb::CheckpointWriter.

390280 {
390281 // Write the table metadata
390282 serializer.WriteProperty(100, "table", &table);
390283
390284 // If there is a context available, bind indexes before serialization.
390285 // This is necessary so that buffered index operations are replayed before we checkpoint, otherwise
390286 // we would lose them if there was a restart after this.
390287 if (context && context->transaction.HasActiveTransaction()) {
390288 auto &info = table.GetStorage().GetDataTableInfo();
390289 info->BindIndexes(*context);
390290 }
390291 // FIXME: If we do not have a context, however, the unbound indexes have to be serialized to disk.
390292
390293 // Write the table data
390294 auto table_lock = table.GetStorage().GetCheckpointLock();
390295 auto writer = GetTableDataWriter(table);
390296 if (writer) {
390297 writer->WriteTableData(serializer);
390298 }
390299}

Member Data Documentation

◆ partial_block_manager

PartialBlockManager duckdb::SingleFileCheckpointWriter::partial_block_manager
private

Because this is single-file storage, we can share partial blocks across an entire checkpoint.


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