From 6ed70dd61bcbfb3fd244a36e2b312f20db99ca3b Mon Sep 17 00:00:00 2001 From: kamilsa Date: Tue, 5 Nov 2024 10:33:20 +0500 Subject: [PATCH] Revert "Chunks are held on rocksdb for 25 hours (#2252)" This reverts commit 8a593bb4ba9f95a12be8de6b4cb1cacdc7f8aa71. --- core/injector/application_injector.cpp | 5 +- .../store/candidate_chunk_key.hpp | 53 ------- .../availability/store/store_impl.cpp | 135 +----------------- .../availability/store/store_impl.hpp | 4 +- core/storage/rocksdb/rocksdb.cpp | 43 +++--- core/storage/rocksdb/rocksdb.hpp | 6 +- core/storage/rocksdb/rocksdb_spaces.cpp | 1 - core/storage/spaces.hpp | 1 - 8 files changed, 24 insertions(+), 224 deletions(-) delete mode 100644 core/parachain/availability/store/candidate_chunk_key.hpp diff --git a/core/injector/application_injector.cpp b/core/injector/application_injector.cpp index 9564a8cfeb..6805c0687d 100644 --- a/core/injector/application_injector.cpp +++ b/core/injector/application_injector.cpp @@ -272,14 +272,11 @@ namespace { // NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions) options.max_open_files = soft_limit.value() / 2; - const std::unordered_map column_ttl = { - {"avaliability_storage", 25 * 60 * 60}}; // 25 hours auto db_res = storage::RocksDb::create(app_config.databasePath(chain_spec->id()), options, app_config.dbCacheSize(), - prevent_destruction, - column_ttl); + prevent_destruction); if (!db_res) { auto log = log::createLogger("Injector", "injector"); log->critical( diff --git a/core/parachain/availability/store/candidate_chunk_key.hpp b/core/parachain/availability/store/candidate_chunk_key.hpp deleted file mode 100644 index 5f9d739194..0000000000 --- a/core/parachain/availability/store/candidate_chunk_key.hpp +++ /dev/null @@ -1,53 +0,0 @@ -/** - * Copyright Quadrivium LLC - * All Rights Reserved - * SPDX-License-Identifier: Apache-2.0 - */ - -#pragma once - -#include - -#include - -#include "parachain/types.hpp" -#include "primitives/common.hpp" - -namespace kagome { - struct CandidateChunkKey { - static constexpr size_t kCandidateHashSize = - sizeof(parachain::CandidateHash); - static constexpr size_t kChunkIndexSize = sizeof(parachain::ChunkIndex); - using Key = common::Blob; - using HashKey = common::Blob; - - static Key encode(const parachain::CandidateHash &candidate_hash, - const parachain::ChunkIndex &chunk_index) { - Key key; - std::copy_n( - encode_hash(candidate_hash).data(), kCandidateHashSize, key.data()); - boost::endian::store_big_u32(key.data() + kCandidateHashSize, - chunk_index); - return key; - } - - static HashKey encode_hash(const parachain::CandidateHash &candidate_hash) { - HashKey key; - std::copy_n(candidate_hash.data(), kCandidateHashSize, key.data()); - return key; - } - - static std::optional< - std::pair> - decode(common::BufferView key) { - if (key.size() != Key::size()) { - return std::nullopt; - } - std::pair candidateChunk; - std::copy_n(key.data(), kCandidateHashSize, candidateChunk.first.data()); - candidateChunk.second = - boost::endian::load_big_u32(key.data() + kCandidateHashSize); - return candidateChunk; - } - }; -} // namespace kagome diff --git a/core/parachain/availability/store/store_impl.cpp b/core/parachain/availability/store/store_impl.cpp index 8d589b9f57..1bf6d61946 100644 --- a/core/parachain/availability/store/store_impl.cpp +++ b/core/parachain/availability/store/store_impl.cpp @@ -5,15 +5,8 @@ */ #include "parachain/availability/store/store_impl.hpp" -#include "candidate_chunk_key.hpp" namespace kagome::parachain { - AvailabilityStoreImpl::AvailabilityStoreImpl( - std::shared_ptr storage) - : storage_{std::move(storage)} { - BOOST_ASSERT(storage_ != nullptr); - } - bool AvailabilityStoreImpl::hasChunk(const CandidateHash &candidate_hash, ValidatorIndex index) const { return state_.sharedAccess([&](const auto &state) { @@ -50,7 +43,7 @@ namespace kagome::parachain { std::optional AvailabilityStoreImpl::getChunk(const CandidateHash &candidate_hash, ValidatorIndex index) const { - auto chunk = state_.sharedAccess( + return state_.sharedAccess( [&](const auto &state) -> std::optional { auto it = state.per_candidate_.find(candidate_hash); @@ -63,30 +56,6 @@ namespace kagome::parachain { } return it2->second; }); - if (chunk) { - return chunk; - } - auto space = storage_->getSpace(storage::Space::kAvaliabilityStorage); - if (not space) { - SL_ERROR(logger, "Failed to get space"); - return std::nullopt; - } - auto chunk_from_db = - space->get(CandidateChunkKey::encode(candidate_hash, index)); - if (not chunk_from_db) { - return std::nullopt; - } - const auto decoded_chunk = - scale::decode(chunk_from_db.value()); - if (not decoded_chunk) { - SL_ERROR(logger, - "Failed to decode chunk candidate {} index {} error {}", - candidate_hash, - index, - decoded_chunk.error()); - return std::nullopt; - } - return decoded_chunk.value(); } std::optional @@ -121,7 +90,7 @@ namespace kagome::parachain { std::vector AvailabilityStoreImpl::getChunks( const CandidateHash &candidate_hash) const { - auto chunks = state_.sharedAccess([&](const auto &state) { + return state_.sharedAccess([&](const auto &state) { std::vector chunks; auto it = state.per_candidate_.find(candidate_hash); if (it != state.per_candidate_.end()) { @@ -131,57 +100,6 @@ namespace kagome::parachain { } return chunks; }); - if (not chunks.empty()) { - return chunks; - } - auto space = storage_->getSpace(storage::Space::kAvaliabilityStorage); - if (not space) { - SL_ERROR(logger, "Failed to get space"); - return chunks; - } - auto cursor = space->cursor(); - if (not cursor) { - SL_ERROR(logger, "Failed to get cursor for AvaliabilityStorage"); - return chunks; - } - const auto seek_key = CandidateChunkKey::encode_hash(candidate_hash); - auto seek_res = cursor->seek(seek_key); - if (not seek_res) { - SL_ERROR(logger, "Failed to seek, error: {}", seek_res.error()); - return chunks; - } - if (not seek_res.value()) { - SL_DEBUG(logger, "Seek not found for candidate {}", candidate_hash); - return chunks; - } - const auto check_key = [&seek_key](const auto &key) { - if (not key) { - return false; - } - const auto &key_value = key.value(); - return key_value.size() >= seek_key.size() - and std::equal(seek_key.begin(), seek_key.end(), key_value.begin()); - }; - while (cursor->isValid() and check_key(cursor->key())) { - const auto cursor_opt_value = cursor->value(); - if (cursor_opt_value) { - auto decoded_res = - scale::decode(cursor_opt_value.value()); - if (decoded_res) { - chunks.emplace_back(std::move(decoded_res.value())); - } else { - SL_ERROR( - logger, "Failed to decode value, error: {}", decoded_res.error()); - } - } else { - SL_ERROR( - logger, "Failed to get value for key {}", cursor->key()->toHex()); - } - if (not cursor->next()) { - break; - } - } - return chunks; } void AvailabilityStoreImpl::printStoragesLoad() { @@ -204,30 +122,7 @@ namespace kagome::parachain { state.candidates_[relay_parent].insert(candidate_hash); auto &candidate_data = state.per_candidate_[candidate_hash]; for (auto &&chunk : std::move(chunks)) { - auto encoded_chunk = scale::encode(chunk); - const auto chunk_index = chunk.index; candidate_data.chunks[chunk.index] = std::move(chunk); - if (not encoded_chunk) { - SL_ERROR(logger, - "Failed to encode chunk, error: {}", - encoded_chunk.error()); - continue; - } - auto space = storage_->getSpace(storage::Space::kAvaliabilityStorage); - if (not space) { - SL_ERROR(logger, "Failed to get space"); - continue; - } - if (auto res = space->put( - CandidateChunkKey::encode(candidate_hash, chunk_index), - std::move(encoded_chunk.value())); - not res) { - SL_ERROR(logger, - "Failed to put chunk candidate {} index {} error {}", - candidate_hash, - chunk_index, - res.error()); - } } candidate_data.pov = pov; candidate_data.data = data; @@ -237,42 +132,18 @@ namespace kagome::parachain { void AvailabilityStoreImpl::putChunk(const network::RelayHash &relay_parent, const CandidateHash &candidate_hash, ErasureChunk &&chunk) { - auto encoded_chunk = scale::encode(chunk); - const auto chunk_index = chunk.index; state_.exclusiveAccess([&](auto &state) { state.candidates_[relay_parent].insert(candidate_hash); state.per_candidate_[candidate_hash].chunks[chunk.index] = std::move(chunk); }); - if (not encoded_chunk) { - SL_ERROR( - logger, "Failed to encode chunk, error: {}", encoded_chunk.error()); - return; - } - - auto space = storage_->getSpace(storage::Space::kAvaliabilityStorage); - if (not space) { - SL_ERROR(logger, "Failed to get AvaliabilityStorage space"); - return; - } - - if (auto res = - space->put(CandidateChunkKey::encode(candidate_hash, chunk_index), - std::move(encoded_chunk.value())); - not res) { - SL_ERROR(logger, - "Failed to put chunk candidate {} index {} error {}", - candidate_hash, - chunk_index, - res.error()); - } } void AvailabilityStoreImpl::remove(const network::RelayHash &relay_parent) { state_.exclusiveAccess([&](auto &state) { if (auto it = state.candidates_.find(relay_parent); it != state.candidates_.end()) { - for (const auto &l : it->second) { + for (auto const &l : it->second) { state.per_candidate_.erase(l); } state.candidates_.erase(it); diff --git a/core/parachain/availability/store/store_impl.hpp b/core/parachain/availability/store/store_impl.hpp index 7be83372b2..861aa2bc88 100644 --- a/core/parachain/availability/store/store_impl.hpp +++ b/core/parachain/availability/store/store_impl.hpp @@ -11,13 +11,12 @@ #include #include #include "log/logger.hpp" -#include "storage/spaced_storage.hpp" #include "utils/safe_object.hpp" + namespace kagome::parachain { class AvailabilityStoreImpl : public AvailabilityStore { public: ~AvailabilityStoreImpl() override = default; - AvailabilityStoreImpl(std::shared_ptr storage); bool hasChunk(const CandidateHash &candidate_hash, ValidatorIndex index) const override; @@ -57,6 +56,5 @@ namespace kagome::parachain { log::Logger logger = log::createLogger("AvailabilityStore", "parachain"); SafeObject state_{}; - std::shared_ptr storage_; }; } // namespace kagome::parachain diff --git a/core/storage/rocksdb/rocksdb.cpp b/core/storage/rocksdb/rocksdb.cpp index 515864eccc..cbb69245cb 100644 --- a/core/storage/rocksdb/rocksdb.cpp +++ b/core/storage/rocksdb/rocksdb.cpp @@ -35,8 +35,7 @@ namespace kagome::storage { const filesystem::path &path, rocksdb::Options options, uint32_t memory_budget_mib, - bool prevent_destruction, - const std::unordered_map &column_ttl) { + bool prevent_destruction) { OUTCOME_TRY(mkdirs(path)); auto log = log::createLogger("RocksDB", "storage"); @@ -62,19 +61,12 @@ namespace kagome::storage { const uint32_t other_spaces_cache_size = (memory_budget - trie_space_cache_size) / (storage::Space::kTotal - 1); std::vector column_family_descriptors; - std::vector ttls; + column_family_descriptors.reserve(Space::kTotal); for (auto i = 0; i < Space::kTotal; ++i) { - const auto space_name = spaceName(static_cast(i)); - auto ttl = 0; - if (const auto it = column_ttl.find(space_name); it != column_ttl.end()) { - ttl = it->second; - } column_family_descriptors.emplace_back( - space_name, + spaceName(static_cast(i)), configureColumn(i != Space::kTrieNode ? other_spaces_cache_size : trie_space_cache_size)); - ttls.push_back(ttl); - SL_INFO(log, "Column family {} configured with TTL {}", space_name, ttl); } std::vector existing_families; @@ -82,12 +74,11 @@ namespace kagome::storage { options, path.native(), &existing_families); if (!res.ok() && !res.IsPathNotFound()) { SL_ERROR(log, - "Can't list column families in {}: {}", + "Can't open database in {}: {}", absolute_path.native(), res.ToString()); return status_as_error(res); } - for (auto &family : existing_families) { if (std::ranges::find_if( column_family_descriptors, @@ -102,21 +93,21 @@ namespace kagome::storage { options.create_missing_column_families = true; auto rocks_db = std::shared_ptr(new RocksDb); - auto status = rocksdb::DBWithTTL::Open(options, - path.native(), - column_family_descriptors, - &rocks_db->column_family_handles_, - &rocks_db->db_, - ttls); - if (not status.ok()) { - SL_ERROR(log, - "Can't open database in {}: {}", - absolute_path.native(), - status.ToString()); - return status_as_error(status); + auto status = rocksdb::DB::Open(options, + path.native(), + column_family_descriptors, + &rocks_db->column_family_handles_, + &rocks_db->db_); + if (status.ok()) { + return rocks_db; } - return rocks_db; + SL_ERROR(log, + "Can't open database in {}: {}", + absolute_path.native(), + status.ToString()); + + return status_as_error(status); } std::shared_ptr RocksDb::getSpace(Space space) { diff --git a/core/storage/rocksdb/rocksdb.hpp b/core/storage/rocksdb/rocksdb.hpp index b0f66fc3e2..ab57020a12 100644 --- a/core/storage/rocksdb/rocksdb.hpp +++ b/core/storage/rocksdb/rocksdb.hpp @@ -10,7 +10,6 @@ #include #include -#include #include #include "filesystem/common.hpp" @@ -49,8 +48,7 @@ namespace kagome::storage { const filesystem::path &path, rocksdb::Options options = rocksdb::Options(), uint32_t memory_budget_mib = kDefaultStateCacheSizeMiB, - bool prevent_destruction = false, - const std::unordered_map& column_ttl = {}); + bool prevent_destruction = false); std::shared_ptr getSpace(Space space) override; @@ -79,7 +77,7 @@ namespace kagome::storage { static rocksdb::ColumnFamilyOptions configureColumn(uint32_t memory_budget); - rocksdb::DBWithTTL *db_{}; + rocksdb::DB *db_{}; std::vector column_family_handles_; boost::container::flat_map> spaces_; rocksdb::ReadOptions ro_; diff --git a/core/storage/rocksdb/rocksdb_spaces.cpp b/core/storage/rocksdb/rocksdb_spaces.cpp index c1f4f920c9..e4eb18b498 100644 --- a/core/storage/rocksdb/rocksdb_spaces.cpp +++ b/core/storage/rocksdb/rocksdb_spaces.cpp @@ -23,7 +23,6 @@ namespace kagome::storage { "trie_value", "dispute_data", "beefy_justification", - "avaliability_storage" }; static_assert(kNames.size() == Space::kTotal - 1); diff --git a/core/storage/spaces.hpp b/core/storage/spaces.hpp index 3f4ef67dd3..286da4bcd1 100644 --- a/core/storage/spaces.hpp +++ b/core/storage/spaces.hpp @@ -23,7 +23,6 @@ namespace kagome::storage { kTrieValue, kDisputeData, kBeefyJustification, - kAvaliabilityStorage, kTotal };