Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

db: cache chain config in local kv::api #2798

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion silkworm/db/chain/local_chain_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,18 @@

namespace silkworm::db::chain {

LocalChainStorage::LocalChainStorage(
db::DataModel data_model,
std::shared_ptr<Cache> cache)
: data_model_{data_model},
cache_{cache} {
std::call_once(cache_->chain_config_once_flag, [this] {
cache_->chain_config = data_model_.read_chain_config();
});
}

Task<ChainConfig> LocalChainStorage::read_chain_config() const {
const auto chain_config{data_model_.read_chain_config()};
const auto chain_config = cache_->chain_config;
if (!chain_config) {
throw std::runtime_error{"empty chain config data in storage"};
}
Expand Down
13 changes: 11 additions & 2 deletions silkworm/db/chain/local_chain_storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#pragma once

#include <mutex>

#include <silkworm/db/access_layer.hpp>

#include "chain_storage.hpp"
Expand All @@ -26,8 +28,14 @@ namespace silkworm::db::chain {
//! in local database (accessed via MDBX API) or local snapshot files (accessed via custom snapshot API)
class LocalChainStorage : public ChainStorage {
public:
explicit LocalChainStorage(db::DataModel data_model)
: data_model_{data_model} {}
struct Cache {
std::optional<ChainConfig> chain_config;
std::once_flag chain_config_once_flag;
};

LocalChainStorage(
db::DataModel data_model,
std::shared_ptr<Cache> cache);
~LocalChainStorage() override = default;

Task<ChainConfig> read_chain_config() const override;
Expand Down Expand Up @@ -74,6 +82,7 @@ class LocalChainStorage : public ChainStorage {

private:
db::DataModel data_model_;
std::shared_ptr<Cache> cache_;
};

} // namespace silkworm::db::chain
4 changes: 2 additions & 2 deletions silkworm/db/kv/api/direct_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace silkworm::db::kv::api {
DirectService::DirectService(ServiceRouter router, DataStoreRef data_store, StateCache* state_cache)
: router_{router},
data_store_{std::move(data_store)},
state_cache_{state_cache} {}
shared_transaction_cache_{std::make_shared<LocalTransaction::Cache>(state_cache)} {}

// rpc Version(google.protobuf.Empty) returns (types.VersionReply);
Task<Version> DirectService::version() {
Expand All @@ -35,7 +35,7 @@ Task<Version> DirectService::version() {

// rpc Tx(stream Cursor) returns (stream Pair);
Task<std::unique_ptr<Transaction>> DirectService::begin_transaction() {
co_return std::make_unique<LocalTransaction>(data_store_, state_cache_);
co_return std::make_unique<LocalTransaction>(data_store_, shared_transaction_cache_);
}

// rpc StateChanges(StateChangeRequest) returns (stream StateChangeBatch);
Expand Down
4 changes: 2 additions & 2 deletions silkworm/db/kv/api/direct_service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include <silkworm/db/data_store.hpp>

#include "local_transaction.hpp"
#include "service.hpp"
#include "service_router.hpp"

Expand Down Expand Up @@ -52,8 +53,7 @@ class DirectService : public Service {
//! The data store
DataStoreRef data_store_;

//! The local state cache built upon incoming state changes
StateCache* state_cache_;
std::shared_ptr<LocalTransaction::Cache> shared_transaction_cache_;
};

} // namespace silkworm::db::kv::api
4 changes: 3 additions & 1 deletion silkworm/db/kv/api/local_transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ Task<std::shared_ptr<CursorDupSort>> LocalTransaction::get_cursor(const std::str

std::shared_ptr<chain::ChainStorage> LocalTransaction::create_storage() {
// The calling thread *must* be the *same* which created this LocalTransaction instance
return std::make_shared<chain::LocalChainStorage>(DataModel{tx_, data_store_.blocks_repository});
return std::make_shared<chain::LocalChainStorage>(
DataModel{tx_, data_store_.blocks_repository},
cache_->chain_storage_cache());
}

Task<TxnId> LocalTransaction::first_txn_num_in_block(BlockNum block_num) {
Expand Down
24 changes: 22 additions & 2 deletions silkworm/db/kv/api/local_transaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include <silkworm/infra/concurrency/task.hpp>

#include <silkworm/db/chain/local_chain_storage.hpp>
#include <silkworm/db/data_store.hpp>

#include "base_transaction.hpp"
Expand All @@ -34,9 +35,27 @@ namespace silkworm::db::kv::api {

class LocalTransaction : public BaseTransaction {
public:
LocalTransaction(DataStoreRef data_store, StateCache* state_cache)
: BaseTransaction(state_cache),
class Cache {
public:
using ChainStorageCache = db::chain::LocalChainStorage::Cache;
explicit Cache(StateCache* state_cache)
: state_cache_{state_cache},
chain_storage_cache_{std::make_shared<ChainStorageCache>()} {}
StateCache* state_cache() const { return state_cache_; }
std::shared_ptr<ChainStorageCache> chain_storage_cache() const { return chain_storage_cache_; }

private:
//! The local state cache built upon incoming state changes
StateCache* state_cache_;
std::shared_ptr<ChainStorageCache> chain_storage_cache_;
};

LocalTransaction(
DataStoreRef data_store,
std::shared_ptr<Cache> cache)
: BaseTransaction{cache->state_cache()},
data_store_{std::move(data_store)},
cache_{std::move(cache)},
tx_{data_store_.chaindata.access_ro().start_ro_tx()} {}

~LocalTransaction() override = default;
Expand Down Expand Up @@ -105,6 +124,7 @@ class LocalTransaction : public BaseTransaction {
std::map<std::string, std::shared_ptr<CursorDupSort>> dup_cursors_;

DataStoreRef data_store_;
std::shared_ptr<Cache> cache_;
uint32_t last_cursor_id_{0};
datastore::kvdb::ROTxnManaged tx_;
uint64_t tx_id_{++next_tx_id_};
Expand Down
Loading