Skip to content

Commit

Permalink
refactor: Move adjust-db tool to database tool (#9264)
Browse files Browse the repository at this point in the history
Refactoring of `adjust-db` tool, moving it to the `database` toolset where there's of database tools should live.
  • Loading branch information
Jure Bajic authored and nikurt committed Jul 2, 2023
1 parent 90f9c9c commit 6638dfd
Show file tree
Hide file tree
Showing 13 changed files with 59 additions and 99 deletions.
14 changes: 1 addition & 13 deletions Cargo.lock

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

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ members = [
"test-utils/runtime-tester/fuzz",
"test-utils/store-validator",
"test-utils/testlib",
"tools/adjust-db",
"tools/database",
"tools/chainsync-loadtest",
"tools/delay-detector",
"tools/indexer/example",
Expand Down Expand Up @@ -169,7 +169,6 @@ lru = "0.7.2"
memmap2 = "0.5"
memoffset = "0.8"
more-asserts = "0.2"
near-adjust-db-tool = { path = "tools/adjust-db", package = "adjust-db-tool" }
near-account-id = { path = "core/account-id", features = ["internal_unstable"] }
near-actix-test-utils = { path = "test-utils/actix-test-utils" }
near-amend-genesis = { path = "tools/amend-genesis" }
Expand Down
3 changes: 2 additions & 1 deletion neard/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ tokio.workspace = true
tracing.workspace = true

nearcore.workspace = true
near-adjust-db-tool.workspace = true
near-amend-genesis.workspace = true
near-chain-configs.workspace = true
near-client.workspace = true
Expand Down Expand Up @@ -78,6 +77,7 @@ nightly = [
"serialize_all_state_changes",
"near-chain-configs/nightly",
"near-client/nightly",
"near-database-tool/nightly",
"near-dyn-configs/nightly",
"near-jsonrpc-primitives/nightly",
"near-mirror/nightly",
Expand All @@ -93,6 +93,7 @@ nightly = [
nightly_protocol = [
"near-chain-configs/nightly_protocol",
"near-client/nightly_protocol",
"near-database-tool/nightly_protocol",
"near-dyn-configs/nightly_protocol",
"near-jsonrpc-primitives/nightly_protocol",
"near-mirror/nightly_protocol",
Expand Down
7 changes: 0 additions & 7 deletions neard/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#[cfg(unix)]
use anyhow::Context;
use near_adjust_db_tool::AdjustDbCommand;
use near_amend_genesis::AmendGenesisCommand;
use near_chain_configs::GenesisValidationMode;
use near_client::ConfigUpdater;
Expand Down Expand Up @@ -129,9 +128,6 @@ impl NeardCmd {
NeardSubCommand::UndoBlock(cmd) => {
cmd.run(&home_dir, genesis_validation)?;
}
NeardSubCommand::AdjustDb(cmd) => {
cmd.run(&home_dir)?;
}
NeardSubCommand::Database(cmd) => {
cmd.run(&home_dir)?;
}
Expand Down Expand Up @@ -255,9 +251,6 @@ pub(super) enum NeardSubCommand {
/// reset the head of the chain locally to the prev block of current head
UndoBlock(UndoBlockCommand),

/// Adjust DB for testing purposes.
AdjustDb(AdjustDbCommand),

/// Set of commands to run on database
Database(DatabaseCommand),
}
Expand Down
18 changes: 0 additions & 18 deletions tools/adjust-db/Cargo.toml

This file was deleted.

23 changes: 0 additions & 23 deletions tools/adjust-db/README.md

This file was deleted.

2 changes: 0 additions & 2 deletions tools/adjust-db/src/lib.rs

This file was deleted.

16 changes: 15 additions & 1 deletion tools/database/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ clap.workspace = true
rayon.workspace = true
strum.workspace = true

near-store.workspace = true
nearcore.workspace = true
near-store.workspace = true
near-chain-configs.workspace = true


[features]
nightly = [
"nightly_protocol",
"near-chain-configs/nightly",
"near-store/nightly",
"nearcore/nightly",
]
nightly_protocol = [
"near-chain-configs/nightly_protocol",
"near-store/nightly_protocol",
"nearcore/nightly_protocol",
]
22 changes: 22 additions & 0 deletions tools/database/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,25 @@ entry (adjust parameters to suit your needs):
* hard nofile 100000
```

## Adjust-db tool
This is a tool that should only be used for testing purposes.
It is intended as a collection of commands that perform small db modifications.


### change-db-kind
Changes DbKind of a DB described in config (cold or hot).
Example usage:
`neard database change-db-kind --new-kind RPC change-cold`
In this example we change DbKind of the cold db to RPC (for some reason).
Notice, that you cannot perform this exact command twice in a row,
because you will not be able to open cold db in the first place.
If you want to change DbKind of the cold db back, you would have to adjust your config:
- .store.path = `<relative cold path>`
- .cold_store = null
- .archive = false

This way neard would try to open db at cold path as RPC db.
Then you can call
`neard database change-db-kind --new-kind Cold change-hot`.
Notice that even though in your mind this db is cold, in your config this db hot, so you have to pass `change-hot`.

Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,6 @@ use near_store::NodeStorage;
use nearcore::NearConfig;
use std::path::Path;

#[derive(clap::Parser)]
pub struct AdjustDbCommand {
#[clap(subcommand)]
subcmd: SubCommand,
}

#[derive(clap::Parser)]
#[clap(subcommand_required = true, arg_required_else_help = true)]
enum SubCommand {
/// Change DbKind of hot or cold db.
ChangeDbKind(ChangeDbKindCmd),
}

impl AdjustDbCommand {
pub fn run(self, home_dir: &Path) -> anyhow::Result<()> {
let near_config = nearcore::config::load_config(
&home_dir,
near_chain_configs::GenesisValidationMode::UnsafeFast,
)
.unwrap_or_else(|e| panic!("Error loading config: {:#}", e));

match self.subcmd {
SubCommand::ChangeDbKind(cmd) => cmd.run(&home_dir, &near_config),
}
}
}

/// This can potentially support db specified not in config, but in command line.
/// `ChangeRelative { path: Path, archive: bool }`
/// But it is a pain to implement, because of all the current storage possibilities.
Expand All @@ -42,7 +15,7 @@ enum DbSelector {
}

#[derive(clap::Args)]
struct ChangeDbKindCmd {
pub(crate) struct ChangeDbKindCommand {
/// Desired DbKind.
#[clap(long)]
new_kind: DbKind,
Expand All @@ -51,8 +24,8 @@ struct ChangeDbKindCmd {
db_selector: DbSelector,
}

impl ChangeDbKindCmd {
fn run(&self, home_dir: &Path, near_config: &NearConfig) -> anyhow::Result<()> {
impl ChangeDbKindCommand {
pub(crate) fn run(&self, home_dir: &Path, near_config: &NearConfig) -> anyhow::Result<()> {
let opener = NodeStorage::opener(
home_dir,
near_config.config.archive,
Expand Down
4 changes: 2 additions & 2 deletions tools/database/src/analyse_data_size_distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{panic, println};
use strum::IntoEnumIterator;

#[derive(Parser)]
pub struct AnalyseDataSizeDistributionCommand {
pub(crate) struct AnalyseDataSizeDistributionCommand {
#[arg(short, long)]
/// If specified only this column will be analysed
column: Option<String>,
Expand Down Expand Up @@ -194,7 +194,7 @@ fn get_column_families(input_col: &Option<String>) -> Vec<DBCol> {
}

impl AnalyseDataSizeDistributionCommand {
pub fn run(&self, home: &PathBuf) -> anyhow::Result<()> {
pub(crate) fn run(&self, home: &PathBuf) -> anyhow::Result<()> {
// Set db options for maximum read performance
let store_config = StoreConfig::default();
let db = RocksDB::open(
Expand Down
12 changes: 12 additions & 0 deletions tools/database/src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::adjust_database::ChangeDbKindCommand;
use crate::analyse_data_size_distribution::AnalyseDataSizeDistributionCommand;
use clap::Parser;
use std::path::PathBuf;
Expand All @@ -13,12 +14,23 @@ pub struct DatabaseCommand {
enum SubCommand {
/// Analyse data size distribution in RocksDB
AnalyseDataSizeDistribution(AnalyseDataSizeDistributionCommand),

/// Change DbKind of hot or cold db.
ChangeDbKind(ChangeDbKindCommand),
}

impl DatabaseCommand {
pub fn run(&self, home: &PathBuf) -> anyhow::Result<()> {
match &self.subcmd {
SubCommand::AnalyseDataSizeDistribution(cmd) => cmd.run(home),
SubCommand::ChangeDbKind(cmd) => {
let near_config = nearcore::config::load_config(
&home,
near_chain_configs::GenesisValidationMode::UnsafeFast,
)
.unwrap_or_else(|e| panic!("Error loading config: {:#}", e));
cmd.run(home, &near_config)
}
}
}
}
1 change: 1 addition & 0 deletions tools/database/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
mod adjust_database;
mod analyse_data_size_distribution;
pub mod commands;

0 comments on commit 6638dfd

Please sign in to comment.