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

feat(storage): implement its own GC method #2802

Merged
merged 36 commits into from
Jun 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
86a372d
skeleton that rustc accepts
ailisp Jun 6, 2020
b459c16
replace first gc column
ailisp Jun 6, 2020
f6bbbdd
inc_gc
ailisp Jun 6, 2020
9c5ac42
inc_gc
ailisp Jun 6, 2020
4aef4da
missing +1
ailisp Jun 6, 2020
6c4d76f
make rustc happy again
ailisp Jun 6, 2020
1db3d37
add all other columns
ailisp Jun 8, 2020
a48d6ef
refactor to use chain_store_update.gc_col
ailisp Jun 10, 2020
64dbab7
fix tests
ailisp Jun 11, 2020
fd3bb2f
add checking gc col in test
ailisp Jun 11, 2020
61546f1
resolve conflict
ailisp Jun 11, 2020
c0e0f74
add gc column migration
ailisp Jun 11, 2020
afd8f0a
wabt-sys 0.7.2 failed to build with cmake 3.10, rollback to wabt-sys …
ailisp Jun 11, 2020
432f982
bump genesis version
ailisp Jun 12, 2020
547b2aa
Merge branch 'master' into db-col-gc-method
ailisp Jun 12, 2020
abf732e
remove genesis migration
ailisp Jun 12, 2020
c782790
bump storage version
ailisp Jun 12, 2020
b797951
bump storage version
ailisp Jun 12, 2020
86db77e
add info and test with master
ailisp Jun 12, 2020
89a4b7c
Merge branch 'master' into db-col-gc-method
Kouprin Jun 15, 2020
7fe3de6
remove double usage strum_macros::EnumIter
Kouprin Jun 15, 2020
80f444e
clean up comments
ailisp Jun 15, 2020
d2af587
return () from gc_col
ailisp Jun 16, 2020
c29eabe
BlockHeight
ailisp Jun 16, 2020
657ae8b
explicit no-op gc columns
ailisp Jun 16, 2020
bdd94ca
delete coltriechanges
ailisp Jun 16, 2020
e296745
special treat gc_col_state
ailisp Jun 16, 2020
b046369
assert col is in GC_COL
ailisp Jun 16, 2020
6dc423f
fix warning
ailisp Jun 16, 2020
8776ca8
Merge branch 'master' into db-col-gc-method
ailisp Jun 16, 2020
b143ca7
address frol and kouprin's comments
ailisp Jun 17, 2020
39dff99
type alias gccount, use vec instead of hashmap
ailisp Jun 17, 2020
cc80f35
address misha's comment
ailisp Jun 17, 2020
112c583
address misha's comment
ailisp Jun 17, 2020
a593777
address Kouprin's comment
ailisp Jun 18, 2020
a3803e7
Merge branch 'master' into db-col-gc-method
ailisp Jun 18, 2020
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
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions chain/chain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ num-rational = "0.2.4"
tracing = "0.1.13"
thiserror = "1.0"
strum = "0.18"
strum_macros = "0.18"

borsh = "0.6.2"

Expand Down
21 changes: 11 additions & 10 deletions chain/chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -952,13 +952,17 @@ impl Chain {
chain_store_update.commit()?;

// clear all trie data
let mut store_update = StoreUpdate::new_with_tries(self.runtime_adapter.get_tries());
let stored_state = self.store().store().iter_prefix(ColState, &[]);
for (key, _) in stored_state {
let keys: Vec<Vec<u8>> =
self.store().store().iter_prefix(ColState, &[]).map(|kv| kv.0.into()).collect();
let tries = self.runtime_adapter.get_tries();
let mut chain_store_update = self.mut_store().store_update();
let mut store_update = StoreUpdate::new_with_tries(tries);
for key in keys.iter() {
store_update.delete(ColState, key.as_ref());
chain_store_update.inc_gc_col_state();
}
let mut chain_store_update = self.mut_store().store_update();
chain_store_update.merge(store_update);

// The reason to reset tail here is not to allow Tail be greater than Head
chain_store_update.reset_tail();
chain_store_update.commit()?;
Expand Down Expand Up @@ -1793,12 +1797,9 @@ impl Chain {
sync_hash: CryptoHash,
num_parts: u64,
) -> Result<(), Error> {
let mut store_update = self.store.owned_store().store_update();
for part_id in 0..num_parts {
let key = StatePartKey(sync_hash, shard_id, part_id).try_to_vec()?;
store_update.delete(ColStateParts, &key);
}
Ok(store_update.commit()?)
let mut chain_store_update = self.mut_store().store_update();
chain_store_update.gc_col_state_parts(sync_hash, shard_id, num_parts)?;
Ok(chain_store_update.commit()?)
}

/// Apply transactions in chunks for the next epoch in blocks that were blocked on the state sync
Expand Down
398 changes: 287 additions & 111 deletions chain/chain/src/store.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion core/primitives/src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct Version {
pub type DbVersion = u32;

/// Current version of the database.
pub const DB_VERSION: DbVersion = 1;
pub const DB_VERSION: DbVersion = 2;

/// Protocol version type.
pub type ProtocolVersion = u32;
Expand Down
9 changes: 7 additions & 2 deletions core/store/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use std::collections::HashMap;
use std::io;
use std::sync::RwLock;

use borsh::{BorshDeserialize, BorshSerialize};

use rocksdb::{
BlockBasedOptions, ColumnFamily, ColumnFamilyDescriptor, Direction, IteratorMode, Options,
ReadOptions, WriteBatch, DB,
Expand Down Expand Up @@ -34,7 +36,7 @@ impl Into<io::Error> for DBError {
}
}

#[derive(PartialEq, Debug, Copy, Clone, EnumIter)]
#[derive(PartialEq, Debug, Copy, Clone, EnumIter, BorshDeserialize, BorshSerialize)]
pub enum DBCol {
/// Column to indicate which version of database this is.
ColDbVersion = 0,
Expand Down Expand Up @@ -91,10 +93,12 @@ pub enum DBCol {
ColChunkHashesByHeight = 39,
/// Block ordinals.
ColBlockOrdinal = 40,
/// GC Count for each column
ColGCCount = 41,
}

// Do not move this line from enum DBCol
const NUM_COLS: usize = 41;
pub const NUM_COLS: usize = 42;

impl std::fmt::Display for DBCol {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
Expand Down Expand Up @@ -140,6 +144,7 @@ impl std::fmt::Display for DBCol {
Self::ColBlockMerkleTree => "block merkle tree",
Self::ColChunkHashesByHeight => "chunk hashes indexed by height_created",
Self::ColBlockOrdinal => "block ordinal",
Self::ColGCCount => "gc count",
};
write!(formatter, "{}", desc)
}
Expand Down
2 changes: 1 addition & 1 deletion core/store/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use cached::{Cached, SizedCache};
pub use db::DBCol::{self, *};
pub use db::{
CHUNK_TAIL_KEY, HEADER_HEAD_KEY, HEAD_KEY, LARGEST_TARGET_HEIGHT_KEY, LATEST_KNOWN_KEY,
SYNC_HEAD_KEY, TAIL_KEY,
NUM_COLS, SYNC_HEAD_KEY, TAIL_KEY,
};
use near_crypto::PublicKey;
use near_primitives::account::{AccessKey, Account};
Expand Down
19 changes: 17 additions & 2 deletions neard/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::path::Path;
use std::sync::Arc;

use actix::{Actor, Addr};
use log::info;
use log::{error, info};
use tracing::trace;

use near_chain::ChainGenesis;
Expand Down Expand Up @@ -53,8 +53,23 @@ pub fn get_default_home() -> String {

/// Function checks current version of the database and applies migrations to the database.
pub fn apply_store_migrations(path: &String) {
let _db_version = get_store_version(path);
let db_version = get_store_version(path);
if db_version > near_primitives::version::DB_VERSION {
error!(target: "near", "DB version {} is created by a newer version of neard, please update neard or delete data", db_version);
std::process::exit(1);
}
if db_version == near_primitives::version::DB_VERSION {
return;
}
// Add migrations here based on `db_version`.
if db_version == 1 {
// version 1 => 2: add gc column
// Does not need to do anything since open db with option `create_missing_column_families`
// Nevertheless need to bump db version, because db_version 1 binary can't open db_version 2 db
info!(target: "near", "Migrate DB from version 1 to 2");
let store = create_store(&path);
set_store_version(&store);
}
}

pub fn init_and_migrate_store(home_dir: &Path) -> Arc<Store> {
Expand Down