From a3d2474c5b68a5bbef60667caa8c9e4829a37479 Mon Sep 17 00:00:00 2001 From: DavIvek Date: Thu, 21 Mar 2024 11:50:55 +0100 Subject: [PATCH] Fix timestamps saving on-disk (#1811) --- src/storage/v2/disk/durable_metadata.cpp | 2 +- src/storage/v2/disk/durable_metadata.hpp | 2 +- src/storage/v2/disk/storage.cpp | 7 +++--- src/storage/v2/disk/storage.hpp | 2 ++ tests/unit/clearing_old_disk_data.cpp | 32 ++++++++++++++++++++++++ 5 files changed, 40 insertions(+), 5 deletions(-) diff --git a/src/storage/v2/disk/durable_metadata.cpp b/src/storage/v2/disk/durable_metadata.cpp index 13d515af2..c1f44a587 100644 --- a/src/storage/v2/disk/durable_metadata.cpp +++ b/src/storage/v2/disk/durable_metadata.cpp @@ -42,7 +42,7 @@ DurableMetadata::DurableMetadata(const Config &config) DurableMetadata::DurableMetadata(DurableMetadata &&other) noexcept : durability_kvstore_(std::move(other.durability_kvstore_)), config_(std::move(other.config_)) {} -void DurableMetadata::SaveBeforeClosingDB(uint64_t timestamp, uint64_t vertex_count, uint64_t edge_count) { +void DurableMetadata::UpdateMetaData(uint64_t timestamp, uint64_t vertex_count, uint64_t edge_count) { durability_kvstore_.Put(kLastTransactionStartTimeStamp, std::to_string(timestamp)); durability_kvstore_.Put(kVertexCountDescr, std::to_string(vertex_count)); durability_kvstore_.Put(kEdgeDountDescr, std::to_string(edge_count)); diff --git a/src/storage/v2/disk/durable_metadata.hpp b/src/storage/v2/disk/durable_metadata.hpp index 4aaa8a707..06a26ac15 100644 --- a/src/storage/v2/disk/durable_metadata.hpp +++ b/src/storage/v2/disk/durable_metadata.hpp @@ -41,7 +41,7 @@ class DurableMetadata { std::optional> LoadExistenceConstraintInfoIfExists() const; std::optional> LoadUniqueConstraintInfoIfExists() const; - void SaveBeforeClosingDB(uint64_t timestamp, uint64_t vertex_count, uint64_t edge_count); + void UpdateMetaData(uint64_t timestamp, uint64_t vertex_count, uint64_t edge_count); bool PersistLabelIndexCreation(LabelId label); diff --git a/src/storage/v2/disk/storage.cpp b/src/storage/v2/disk/storage.cpp index 4dbd248f7..9aa6613c7 100644 --- a/src/storage/v2/disk/storage.cpp +++ b/src/storage/v2/disk/storage.cpp @@ -274,8 +274,8 @@ DiskStorage::DiskStorage(Config config) } DiskStorage::~DiskStorage() { - durable_metadata_.SaveBeforeClosingDB(timestamp_, vertex_count_.load(std::memory_order_acquire), - edge_count_.load(std::memory_order_acquire)); + durable_metadata_.UpdateMetaData(timestamp_, vertex_count_.load(std::memory_order_acquire), + edge_count_.load(std::memory_order_acquire)); logging::AssertRocksDBStatus(kvstore_->db_->DestroyColumnFamilyHandle(kvstore_->vertex_chandle)); logging::AssertRocksDBStatus(kvstore_->db_->DestroyColumnFamilyHandle(kvstore_->edge_chandle)); logging::AssertRocksDBStatus(kvstore_->db_->DestroyColumnFamilyHandle(kvstore_->out_edges_chandle)); @@ -1786,7 +1786,8 @@ utils::BasicResult DiskStorage::DiskAccessor::Co if (flags::AreExperimentsEnabled(flags::Experiments::TEXT_SEARCH)) { disk_storage->indices_.text_index_.Commit(); } - + disk_storage->durable_metadata_.UpdateMetaData(disk_storage->timestamp_, disk_storage->vertex_count_, + disk_storage->edge_count_); is_transaction_active_ = false; return {}; diff --git a/src/storage/v2/disk/storage.hpp b/src/storage/v2/disk/storage.hpp index 349a7454a..74f4f4136 100644 --- a/src/storage/v2/disk/storage.hpp +++ b/src/storage/v2/disk/storage.hpp @@ -301,6 +301,8 @@ class DiskStorage final : public Storage { EdgeImportMode GetEdgeImportMode() const; + DurableMetadata *GetDurableMetadata() { return &durable_metadata_; } + private: void LoadPersistingMetadataInfo(); diff --git a/tests/unit/clearing_old_disk_data.cpp b/tests/unit/clearing_old_disk_data.cpp index 395391e12..58682a845 100644 --- a/tests/unit/clearing_old_disk_data.cpp +++ b/tests/unit/clearing_old_disk_data.cpp @@ -179,3 +179,35 @@ TEST_F(ClearingOldDiskDataTest, TestNumOfEntriesWithEdgeValueUpdate) { ASSERT_EQ(disk_test_utils::GetRealNumberOfEntriesInRocksDB(tx_db), 5); } + +TEST_F(ClearingOldDiskDataTest, TestTimestampAfterCommit) { + auto *tx_db = disk_storage->GetRocksDBStorage()->db_; + ASSERT_EQ(disk_test_utils::GetRealNumberOfEntriesInRocksDB(tx_db), 0); + + auto acc1 = disk_storage->Access(ReplicationRole::MAIN); + auto vertex1 = acc1->CreateVertex(); + auto label1 = acc1->NameToLabel("DiskLabel"); + auto property1 = acc1->NameToProperty("DiskProperty"); + ASSERT_TRUE(vertex1.AddLabel(label1).HasValue()); + ASSERT_TRUE(vertex1.SetProperty(property1, memgraph::storage::PropertyValue(10)).HasValue()); + ASSERT_FALSE(acc1->Commit().HasError()); + ASSERT_EQ(disk_test_utils::GetRealNumberOfEntriesInRocksDB(tx_db), 1); + + auto saved_timestamp = disk_storage->GetDurableMetadata()->LoadTimestampIfExists(); + ASSERT_EQ(saved_timestamp.has_value(), true); + ASSERT_EQ(disk_storage->timestamp_, saved_timestamp); + + auto acc2 = disk_storage->Access(ReplicationRole::MAIN); + auto vertex2 = acc2->CreateVertex(); + auto label2 = acc2->NameToLabel("DiskLabel2"); + auto property2 = acc2->NameToProperty("DiskProperty2"); + + ASSERT_TRUE(vertex2.AddLabel(label2).HasValue()); + ASSERT_TRUE(vertex2.SetProperty(property2, memgraph::storage::PropertyValue(10)).HasValue()); + ASSERT_FALSE(acc2->Commit().HasError()); + ASSERT_EQ(disk_test_utils::GetRealNumberOfEntriesInRocksDB(tx_db), 2); + + saved_timestamp = disk_storage->GetDurableMetadata()->LoadTimestampIfExists(); + ASSERT_EQ(saved_timestamp.has_value(), true); + ASSERT_EQ(disk_storage->timestamp_, saved_timestamp); +}