From 2481710cfc59ec329e5a3a1fed4dbca9bd0a9610 Mon Sep 17 00:00:00 2001 From: Sahil <10851832+lihasgupta@users.noreply.github.com> Date: Mon, 29 Dec 2025 09:06:21 -0800 Subject: [PATCH 1/3] Add test coverage for WalManager::DeleteFile - Add WalManagerTest.DeleteWALFile to test file deletion and cache cleanup - Increases wal_manager.cc coverage from 70.13% to 72.48% --- db/wal_manager_test.cc | 51 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/db/wal_manager_test.cc b/db/wal_manager_test.cc index e674e7b778c9..708334a759b6 100644 --- a/db/wal_manager_test.cc +++ b/db/wal_manager_test.cc @@ -365,6 +365,57 @@ TEST_F(WalManagerTest, TransactionLogIteratorNewFileWhileScanning) { ASSERT_TRUE(iter->status().ok()); } +TEST_F(WalManagerTest, DeleteWALFile) { + Init(nullptr /* clock_override */); + + // Create a WAL file in the wal_dir + uint64_t log_number = 123; + std::string log_file_name = LogFileName("", log_number); + std::string log_file_path = dbname_ + "/" + log_file_name; + + // Create the file with some content + std::unique_ptr file; + ASSERT_OK(env_->GetFileSystem()->NewWritableFile( + log_file_path, FileOptions(), &file, nullptr)); + + std::unique_ptr file_writer( + new WritableFileWriter(std::move(file), log_file_path, FileOptions())); + log::Writer writer(std::move(file_writer), log_number, + db_options_.recycle_log_file_num > 0); + + // Write a record to the file + WriteBatch batch; + ASSERT_OK(batch.Put("key1", "value1")); + WriteBatchInternal::SetSequence(&batch, 100); + ASSERT_OK( + writer.AddRecord(WriteOptions(), WriteBatchInternal::Contents(&batch))); + + // Verify file exists + ASSERT_OK(env_->FileExists(log_file_path)); + + // Populate the read_first_record_cache by reading the file + SequenceNumber seq; + ASSERT_OK(wal_manager_->TEST_ReadFirstRecord(kAliveLogFile, log_number, &seq)); + ASSERT_EQ(seq, 100U); + + // Call DeleteFile + Status delete_status = wal_manager_->DeleteFile(log_file_name, log_number); + ASSERT_OK(delete_status); + + // Verify file is deleted + ASSERT_TRUE(env_->FileExists(log_file_path).IsNotFound()); + + // Verify cache entry is removed by trying to read again + // Since the file doesn't exist and cache is cleared, ReadFirstRecord + // returns OK with sequence=0 to indicate an empty/missing file + SequenceNumber seq_after; + Status read_status = + wal_manager_->TEST_ReadFirstRecord(kAliveLogFile, log_number, &seq_after); + ASSERT_OK(read_status); + // Sequence should be 0 (indicating file not found), not 100 (cached value) + ASSERT_EQ(seq_after, 0U) << "Cache should be cleared after DeleteFile"; +} + } // namespace ROCKSDB_NAMESPACE int main(int argc, char** argv) { From 57f099697cca42d4c14baefde8beba8f0549b25d Mon Sep 17 00:00:00 2001 From: Sahil <10851832+lihasgupta@users.noreply.github.com> Date: Mon, 29 Dec 2025 09:14:08 -0800 Subject: [PATCH 2/3] Add test coverage for WalManager::ArchiveWALFile - Add WalManagerTest.ArchiveWALFile to test WAL file archiving - Verifies file is moved from main directory to archive directory - Verifies archived file preserves content and can be read - Increases wal_manager.cc coverage from 72.48% to 75.50% --- db/wal_manager_test.cc | 49 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/db/wal_manager_test.cc b/db/wal_manager_test.cc index 708334a759b6..25e01891b401 100644 --- a/db/wal_manager_test.cc +++ b/db/wal_manager_test.cc @@ -416,6 +416,55 @@ TEST_F(WalManagerTest, DeleteWALFile) { ASSERT_EQ(seq_after, 0U) << "Cache should be cleared after DeleteFile"; } +TEST_F(WalManagerTest, ArchiveWALFile) { + Init(nullptr /* clock_override */); + + // Create a WAL file in the main wal_dir + uint64_t log_number = 456; + std::string log_file_name = LogFileName("", log_number); + std::string log_file_path = dbname_ + "/" + log_file_name; + + // Create the file with some content + std::unique_ptr file; + ASSERT_OK(env_->GetFileSystem()->NewWritableFile( + log_file_path, FileOptions(), &file, nullptr)); + + std::unique_ptr file_writer( + new WritableFileWriter(std::move(file), log_file_path, FileOptions())); + log::Writer writer(std::move(file_writer), log_number, + db_options_.recycle_log_file_num > 0); + + // Write a record to the file + WriteBatch batch; + ASSERT_OK(batch.Put("archive_key", "archive_value")); + WriteBatchInternal::SetSequence(&batch, 200); + ASSERT_OK( + writer.AddRecord(WriteOptions(), WriteBatchInternal::Contents(&batch))); + + // Verify file exists in main directory + ASSERT_OK(env_->FileExists(log_file_path)); + + // Verify archive directory exists (created in Init) + std::string archive_dir = ArchivalDirectory(dbname_); + ASSERT_OK(env_->FileExists(archive_dir)); + + // Call ArchiveWALFile + wal_manager_->ArchiveWALFile(log_file_path, log_number); + + // Verify original file is moved (no longer in main directory) + ASSERT_TRUE(env_->FileExists(log_file_path).IsNotFound()); + + // Verify file now exists in archive directory + std::string archived_file_path = ArchivedLogFileName(dbname_, log_number); + ASSERT_OK(env_->FileExists(archived_file_path)); + + // Verify the archived file has the correct content + SequenceNumber seq; + ASSERT_OK( + wal_manager_->TEST_ReadFirstRecord(kArchivedLogFile, log_number, &seq)); + ASSERT_EQ(seq, 200U); +} + } // namespace ROCKSDB_NAMESPACE int main(int argc, char** argv) { From 21fc99f83e48aceb98d3103bd2383e3a2a7ddea2 Mon Sep 17 00:00:00 2001 From: Sahil <10851832+lihasgupta@users.noreply.github.com> Date: Mon, 29 Dec 2025 09:14:08 -0800 Subject: [PATCH 3/3] Add test coverage for WalManager::ArchiveWALFile - Add WalManagerTest.ArchiveWALFile to test WAL file archiving - Verifies file is moved from main directory to archive directory - Verifies archived file preserves content and can be read - Increases wal_manager.cc coverage from 72.48% to 75.50% --- db/wal_manager_test.cc | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/db/wal_manager_test.cc b/db/wal_manager_test.cc index 25e01891b401..fa6c8644a5c9 100644 --- a/db/wal_manager_test.cc +++ b/db/wal_manager_test.cc @@ -375,8 +375,8 @@ TEST_F(WalManagerTest, DeleteWALFile) { // Create the file with some content std::unique_ptr file; - ASSERT_OK(env_->GetFileSystem()->NewWritableFile( - log_file_path, FileOptions(), &file, nullptr)); + ASSERT_OK(env_->GetFileSystem()->NewWritableFile(log_file_path, FileOptions(), + &file, nullptr)); std::unique_ptr file_writer( new WritableFileWriter(std::move(file), log_file_path, FileOptions())); @@ -395,7 +395,8 @@ TEST_F(WalManagerTest, DeleteWALFile) { // Populate the read_first_record_cache by reading the file SequenceNumber seq; - ASSERT_OK(wal_manager_->TEST_ReadFirstRecord(kAliveLogFile, log_number, &seq)); + ASSERT_OK( + wal_manager_->TEST_ReadFirstRecord(kAliveLogFile, log_number, &seq)); ASSERT_EQ(seq, 100U); // Call DeleteFile @@ -426,8 +427,8 @@ TEST_F(WalManagerTest, ArchiveWALFile) { // Create the file with some content std::unique_ptr file; - ASSERT_OK(env_->GetFileSystem()->NewWritableFile( - log_file_path, FileOptions(), &file, nullptr)); + ASSERT_OK(env_->GetFileSystem()->NewWritableFile(log_file_path, FileOptions(), + &file, nullptr)); std::unique_ptr file_writer( new WritableFileWriter(std::move(file), log_file_path, FileOptions()));