Skip to content

Commit

Permalink
[Storage] Add caches for JMT nodes. (aptos-labs#2969)
Browse files Browse the repository at this point in the history
  • Loading branch information
grao1991 authored Aug 19, 2022
1 parent 31d59c1 commit 0405b79
Show file tree
Hide file tree
Showing 23 changed files with 412 additions and 76 deletions.
23 changes: 23 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion api/test-context/src/test_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use aptos_api_types::{
X_APTOS_LEDGER_TIMESTAMP, X_APTOS_LEDGER_VERSION,
};
use aptos_config::config::{
NodeConfig, RocksdbConfigs, NO_OP_STORAGE_PRUNER_CONFIG, TARGET_SNAPSHOT_SIZE,
NodeConfig, RocksdbConfigs, DEFAULT_MAX_NUM_NODES_PER_LRU_CACHE_SHARD,
NO_OP_STORAGE_PRUNER_CONFIG, TARGET_SNAPSHOT_SIZE,
};
use aptos_crypto::{hash::HashValue, SigningKey};
use aptos_mempool::mocks::MockSharedMempool;
Expand Down Expand Up @@ -113,6 +114,7 @@ pub fn new_test_context(test_name: String, use_db_with_indexer: bool) -> TestCon
RocksdbConfigs::default(),
false, /* indexer */
TARGET_SNAPSHOT_SIZE,
DEFAULT_MAX_NUM_NODES_PER_LRU_CACHE_SHARD,
)
.unwrap(),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

use crate::AptosValidatorInterface;
use anyhow::{anyhow, Result};
use aptos_config::config::{RocksdbConfigs, NO_OP_STORAGE_PRUNER_CONFIG, TARGET_SNAPSHOT_SIZE};
use aptos_config::config::{
RocksdbConfigs, DEFAULT_MAX_NUM_NODES_PER_LRU_CACHE_SHARD, NO_OP_STORAGE_PRUNER_CONFIG,
TARGET_SNAPSHOT_SIZE,
};
use aptos_types::{
account_address::AccountAddress,
account_state::AccountState,
Expand All @@ -27,6 +30,7 @@ impl DBDebuggerInterface {
RocksdbConfigs::default(),
false,
TARGET_SNAPSHOT_SIZE,
DEFAULT_MAX_NUM_NODES_PER_LRU_CACHE_SHARD,
)?)))
}
}
Expand Down
8 changes: 5 additions & 3 deletions aptos-node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ use mempool_notifications::MempoolNotificationSender;
use network::application::storage::PeerMetadataStorage;
use network_builder::builder::NetworkBuilder;
use rand::{rngs::StdRng, SeedableRng};
use state_sync_driver::driver_factory::DriverFactory;
use state_sync_driver::driver_factory::StateSyncRuntimes;
use state_sync_driver::metadata_storage::PersistentMetadataStorage;
use state_sync_driver::{
driver_factory::{DriverFactory, StateSyncRuntimes},
metadata_storage::PersistentMetadataStorage,
};
use std::{
boxed::Box,
collections::{HashMap, HashSet},
Expand Down Expand Up @@ -481,6 +482,7 @@ pub fn setup_environment(node_config: NodeConfig) -> anyhow::Result<AptosHandle>
node_config.storage.rocksdb_configs,
node_config.storage.enable_indexer,
node_config.storage.target_snapshot_size,
node_config.storage.max_num_nodes_per_lru_cache_shard,
)
.map_err(|err| anyhow!("DB failed to open {}", err))?,
);
Expand Down
6 changes: 6 additions & 0 deletions config/src/config/storage_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ use std::{
path::PathBuf,
};

// Lru cache will consume about 2G RAM based on this default value.
pub const DEFAULT_MAX_NUM_NODES_PER_LRU_CACHE_SHARD: usize = 1 << 13;

pub const TARGET_SNAPSHOT_SIZE: usize = 100_000;

/// Port selected RocksDB options for tuning underlying rocksdb instance of AptosDB.
Expand Down Expand Up @@ -76,6 +79,8 @@ pub struct StorageConfig {
data_dir: PathBuf,
/// The threshold that determine whether a snapshot should be committed to state merkle db.
pub target_snapshot_size: usize,
/// The max # of nodes for a lru cache shard.
pub max_num_nodes_per_lru_cache_shard: usize,
/// Rocksdb-specific configurations
pub rocksdb_configs: RocksdbConfigs,
/// Try to enable the internal indexer. The indexer expects to have seen all transactions
Expand Down Expand Up @@ -180,6 +185,7 @@ impl Default for StorageConfig {
rocksdb_configs: RocksdbConfigs::default(),
enable_indexer: false,
target_snapshot_size: TARGET_SNAPSHOT_SIZE,
max_num_nodes_per_lru_cache_shard: DEFAULT_MAX_NUM_NODES_PER_LRU_CACHE_SHARD,
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions crates/aptos-genesis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ pub mod keys;
#[cfg(any(test, feature = "testing"))]
pub mod test_utils;

use crate::builder::GenesisConfiguration;
use crate::config::ValidatorConfiguration;
use aptos_config::config::{RocksdbConfigs, NO_OP_STORAGE_PRUNER_CONFIG, TARGET_SNAPSHOT_SIZE};
use crate::{builder::GenesisConfiguration, config::ValidatorConfiguration};
use aptos_config::config::{
RocksdbConfigs, DEFAULT_MAX_NUM_NODES_PER_LRU_CACHE_SHARD, NO_OP_STORAGE_PRUNER_CONFIG,
TARGET_SNAPSHOT_SIZE,
};
use aptos_crypto::ed25519::Ed25519PublicKey;
use aptos_temppath::TempPath;
use aptos_types::{chain_id::ChainId, transaction::Transaction, waypoint::Waypoint};
Expand Down Expand Up @@ -135,6 +137,7 @@ impl GenesisInfo {
RocksdbConfigs::default(),
false,
TARGET_SNAPSHOT_SIZE,
DEFAULT_MAX_NUM_NODES_PER_LRU_CACHE_SHARD,
)?;
let db_rw = DbReaderWriter::new(aptosdb);
executor::db_bootstrapper::generate_waypoint::<AptosVM>(&db_rw, genesis)
Expand Down
6 changes: 5 additions & 1 deletion execution/db-bootstrapper/src/bin/db-bootstrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
// SPDX-License-Identifier: Apache-2.0

use anyhow::{ensure, format_err, Context, Result};
use aptos_config::config::{RocksdbConfigs, NO_OP_STORAGE_PRUNER_CONFIG, TARGET_SNAPSHOT_SIZE};
use aptos_config::config::{
RocksdbConfigs, DEFAULT_MAX_NUM_NODES_PER_LRU_CACHE_SHARD, NO_OP_STORAGE_PRUNER_CONFIG,
TARGET_SNAPSHOT_SIZE,
};
use aptos_temppath::TempPath;
use aptos_types::{transaction::Transaction, waypoint::Waypoint};
use aptos_vm::AptosVM;
Expand Down Expand Up @@ -55,6 +58,7 @@ fn main() -> Result<()> {
RocksdbConfigs::default(),
false, /* indexer */
TARGET_SNAPSHOT_SIZE,
DEFAULT_MAX_NUM_NODES_PER_LRU_CACHE_SHARD,
)
} else {
// When not committing, we open the DB as secondary so the tool is usable along side a
Expand Down
5 changes: 4 additions & 1 deletion execution/executor-benchmark/src/db_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use aptos_config::{
utils::get_genesis_txn,
};

use aptos_config::config::{PrunerConfig, TARGET_SNAPSHOT_SIZE};
use aptos_config::config::{
PrunerConfig, DEFAULT_MAX_NUM_NODES_PER_LRU_CACHE_SHARD, TARGET_SNAPSHOT_SIZE,
};
use aptos_vm::AptosVM;
use aptosdb::AptosDB;
use executor::db_bootstrapper::{generate_waypoint, maybe_bootstrap};
Expand Down Expand Up @@ -61,6 +63,7 @@ fn bootstrap_with_genesis(db_dir: impl AsRef<Path>) {
rocksdb_configs,
false, /* indexer */
TARGET_SNAPSHOT_SIZE,
DEFAULT_MAX_NUM_NODES_PER_LRU_CACHE_SHARD,
)
.expect("DB should open."),
);
Expand Down
5 changes: 4 additions & 1 deletion execution/executor-benchmark/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ use crate::{
transaction_generator::TransactionGenerator,
};
use aptos_config::config::{
NodeConfig, PrunerConfig, RocksdbConfigs, NO_OP_STORAGE_PRUNER_CONFIG, TARGET_SNAPSHOT_SIZE,
NodeConfig, PrunerConfig, RocksdbConfigs, DEFAULT_MAX_NUM_NODES_PER_LRU_CACHE_SHARD,
NO_OP_STORAGE_PRUNER_CONFIG, TARGET_SNAPSHOT_SIZE,
};
use aptos_jellyfish_merkle::metrics::{
APTOS_JELLYFISH_INTERNAL_ENCODED_BYTES, APTOS_JELLYFISH_LEAF_ENCODED_BYTES,
Expand All @@ -36,6 +37,7 @@ pub fn init_db_and_executor(config: &NodeConfig) -> (DbReaderWriter, BlockExecut
RocksdbConfigs::default(),
false,
config.storage.target_snapshot_size,
config.storage.max_num_nodes_per_lru_cache_shard,
)
.expect("DB should open."),
);
Expand All @@ -59,6 +61,7 @@ fn create_checkpoint(source_dir: impl AsRef<Path>, checkpoint_dir: impl AsRef<Pa
RocksdbConfigs::default(),
false,
TARGET_SNAPSHOT_SIZE,
DEFAULT_MAX_NUM_NODES_PER_LRU_CACHE_SHARD,
)
.expect("db open failure.")
.create_checkpoint(checkpoint_dir.as_ref())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// Copyright (c) Aptos
// SPDX-License-Identifier: Apache-2.0

use crate::driver_factory::DriverFactory;
use crate::metadata_storage::PersistentMetadataStorage;
use aptos_config::config::TARGET_SNAPSHOT_SIZE;
use crate::{driver_factory::DriverFactory, metadata_storage::PersistentMetadataStorage};
use aptos_config::{
config::{RocksdbConfigs, NO_OP_STORAGE_PRUNER_CONFIG},
config::{
RocksdbConfigs, DEFAULT_MAX_NUM_NODES_PER_LRU_CACHE_SHARD, NO_OP_STORAGE_PRUNER_CONFIG,
TARGET_SNAPSHOT_SIZE,
},
utils::get_genesis_txn,
};
use aptos_data_client::aptosnet::AptosNetDataClient;
Expand Down Expand Up @@ -39,6 +40,7 @@ fn test_new_initialized_configs() {
RocksdbConfigs::default(),
false,
TARGET_SNAPSHOT_SIZE,
DEFAULT_MAX_NUM_NODES_PER_LRU_CACHE_SHARD,
)
.unwrap();
let (_, db_rw) = DbReaderWriter::wrap(db);
Expand Down
2 changes: 2 additions & 0 deletions storage/aptosdb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ edition = "2018"
[dependencies]
anyhow = "1.0.57"
arc-swap = "1.5.0"
arr_macro = "0.1.3"
bcs = "0.1.3"
byteorder = "1.4.3"
itertools = "0.10.0"
lru = "0.7.7"
num-derive = "0.3.3"
num-traits = "0.2.15"
once_cell = "1.10.0"
Expand Down
50 changes: 46 additions & 4 deletions storage/aptosdb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ mod db_options;
mod event_store;
mod ledger_counters;
mod ledger_store;
mod lru_node_cache;
mod pruner;
mod state_merkle_db;
mod state_store;
mod system_store;
mod transaction_store;
mod versioned_node_cache;

#[cfg(test)]
mod aptosdb_test;
Expand Down Expand Up @@ -56,9 +58,12 @@ use crate::{
transaction_store::TransactionStore,
};
use anyhow::{bail, ensure, Result};
#[cfg(any(test, feature = "fuzzing"))]
use aptos_config::config::DEFAULT_MAX_NUM_NODES_PER_LRU_CACHE_SHARD;
use aptos_config::config::{
PrunerConfig, RocksdbConfig, RocksdbConfigs, NO_OP_STORAGE_PRUNER_CONFIG, TARGET_SNAPSHOT_SIZE,
};

use aptos_crypto::hash::HashValue;
use aptos_infallible::Mutex;
use aptos_logger::prelude::*;
Expand Down Expand Up @@ -276,6 +281,7 @@ impl AptosDB {
state_merkle_rocksdb: DB,
pruner_config: PrunerConfig,
target_snapshot_size: usize,
max_nodes_per_lru_cache_shard: usize,
hack_for_tests: bool,
) -> Self {
let arc_ledger_rocksdb = Arc::new(ledger_rocksdb);
Expand All @@ -288,6 +294,7 @@ impl AptosDB {
Arc::clone(&arc_ledger_rocksdb),
Arc::clone(&arc_state_merkle_rocksdb),
target_snapshot_size,
max_nodes_per_lru_cache_shard,
hack_for_tests,
));
let ledger_pruner = LedgerPrunerManager::new(
Expand Down Expand Up @@ -322,6 +329,7 @@ impl AptosDB {
rocksdb_configs: RocksdbConfigs,
enable_indexer: bool,
target_snapshot_size: usize,
max_num_nodes_per_lru_cache_shard: usize,
) -> Result<Self> {
ensure!(
pruner_config.eq(&NO_OP_STORAGE_PRUNER_CONFIG) || !readonly,
Expand Down Expand Up @@ -369,6 +377,7 @@ impl AptosDB {
state_merkle_db,
pruner_config,
target_snapshot_size,
max_num_nodes_per_lru_cache_shard,
readonly,
);

Expand Down Expand Up @@ -458,6 +467,7 @@ impl AptosDB {
)?,
NO_OP_STORAGE_PRUNER_CONFIG,
TARGET_SNAPSHOT_SIZE,
0,
true,
))
}
Expand All @@ -467,6 +477,7 @@ impl AptosDB {
db_root_path: P,
readonly: bool,
target_snapshot_size: usize,
max_num_nodes_per_lru_cache_shard: usize,
enable_indexer: bool,
) -> Self {
Self::open(
Expand All @@ -476,20 +487,39 @@ impl AptosDB {
RocksdbConfigs::default(),
enable_indexer,
target_snapshot_size,
max_num_nodes_per_lru_cache_shard,
)
.expect("Unable to open AptosDB")
}

/// This opens db in non-readonly mode, without the pruner.
#[cfg(any(test, feature = "fuzzing"))]
pub fn new_for_test<P: AsRef<Path> + Clone>(db_root_path: P) -> Self {
Self::new_without_pruner(db_root_path, false, TARGET_SNAPSHOT_SIZE, false)
Self::new_without_pruner(
db_root_path,
false,
TARGET_SNAPSHOT_SIZE,
DEFAULT_MAX_NUM_NODES_PER_LRU_CACHE_SHARD,
false,
)
}

/// This opens db in non-readonly mode, without the pruner and cache.
#[cfg(any(test, feature = "fuzzing"))]
pub fn new_for_test_no_cache<P: AsRef<Path> + Clone>(db_root_path: P) -> Self {
Self::new_without_pruner(db_root_path, false, TARGET_SNAPSHOT_SIZE, 0, false)
}

/// This opens db in non-readonly mode, without the pruner, and with the indexer
#[cfg(any(test, feature = "fuzzing"))]
pub fn new_for_test_with_indexer<P: AsRef<Path> + Clone>(db_root_path: P) -> Self {
Self::new_without_pruner(db_root_path, false, TARGET_SNAPSHOT_SIZE, true)
Self::new_without_pruner(
db_root_path,
false,
TARGET_SNAPSHOT_SIZE,
DEFAULT_MAX_NUM_NODES_PER_LRU_CACHE_SHARD,
true,
)
}

/// This opens db in non-readonly mode, without the pruner.
Expand All @@ -498,13 +528,25 @@ impl AptosDB {
db_root_path: P,
target_snapshot_size: usize,
) -> Self {
Self::new_without_pruner(db_root_path, false, target_snapshot_size, false)
Self::new_without_pruner(
db_root_path,
false,
target_snapshot_size,
DEFAULT_MAX_NUM_NODES_PER_LRU_CACHE_SHARD,
false,
)
}

/// This opens db in non-readonly mode, without the pruner.
#[cfg(any(test, feature = "fuzzing"))]
pub fn new_readonly_for_test<P: AsRef<Path> + Clone>(db_root_path: P) -> Self {
Self::new_without_pruner(db_root_path, true, TARGET_SNAPSHOT_SIZE, false)
Self::new_without_pruner(
db_root_path,
true,
TARGET_SNAPSHOT_SIZE,
DEFAULT_MAX_NUM_NODES_PER_LRU_CACHE_SHARD,
false,
)
}

/// This gets the current buffered_state in StateStore.
Expand Down
Loading

0 comments on commit 0405b79

Please sign in to comment.