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

Allow database migration upon adding new database column family to schema #1851

Merged
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Fixed

- [#1856](https://github.com/FuelLabs/fuel-core/pull/1856): Replaced instances of `Union` with `Enum` for GraphQL definitions of `ConsensusParametersVersion` and related types. This is needed because `Union` does not support multiple `Version`s inside discriminants or empty variants.
- [#1851](https://github.com/FuelLabs/fuel-core/pull/1851/): Provided migration capabilities (enabled addition of new column families) to RocksDB instance.

### Changed

Expand Down
80 changes: 46 additions & 34 deletions crates/fuel-core/src/state/rocks_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,6 @@ where
}
block_opts.set_bloom_filter(10.0, true);

let cf_descriptors = columns.clone().into_iter().map(|i| {
ColumnFamilyDescriptor::new(
Self::col_name(i.id()),
Self::cf_opts(i, &block_opts),
)
});

let mut opts = Options::default();
opts.create_if_missing(true);
opts.set_compression_type(DBCompressionType::Lz4);
Expand All @@ -227,34 +220,53 @@ where
opts.set_row_cache(&cache);
}

let db = match DB::open_cf_descriptors(&opts, &path, cf_descriptors) {
Err(_) => {
// setup cfs
match DB::open_cf(&opts, &path, &[] as &[&str]) {
Ok(db) => {
for i in columns {
let opts = Self::cf_opts(i, &block_opts);
db.create_cf(Self::col_name(i.id()), &opts)
.map_err(|e| DatabaseError::Other(e.into()))?;
}
Ok(db)
}
Err(err) => {
tracing::error!("Couldn't open the database with an error: {}. \nTrying to repair the database", err);
DB::repair(&opts, &path)
.map_err(|e| DatabaseError::Other(e.into()))?;

let cf_descriptors = columns.clone().into_iter().map(|i| {
ColumnFamilyDescriptor::new(
Self::col_name(i.id()),
Self::cf_opts(i, &block_opts),
)
});
DB::open_cf_descriptors(&opts, &path, cf_descriptors)
}
}
let existing_column_families = match DB::list_cf(&opts, &path) {
Ok(cfs) => cfs,
Err(err) => {
tracing::error!(
"Couldn't get the list of cfs: {}. Returning an empty list",
err
);
vec![]
}
ok => ok,
};

let mut cf_descriptors_to_open = vec![];
let mut cf_descriptors_to_create = vec![];
for column in columns.clone() {
let column_name = Self::col_name(column.id());
if existing_column_families.contains(&column_name) {
cf_descriptors_to_open.push(ColumnFamilyDescriptor::new(
column_name,
Self::cf_opts(column, &block_opts),
));
} else {
let opts = Self::cf_opts(column, &block_opts);
cf_descriptors_to_create.push((column_name, opts));
}
}
let db = match DB::open_cf_descriptors(&opts, &path, cf_descriptors_to_open) {
Ok(db) => {
// Setup cfs
for (name, opt) in cf_descriptors_to_create {
db.create_cf(name, &opt)
.map_err(|e| DatabaseError::Other(e.into()))?;
}
Ok(db)
Comment on lines +251 to +255
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it makes sense to move this loop after db is opened(below of the match statement). In this case you also will handle the case of the error with repair.

},
Err(err) => {
tracing::error!("Couldn't open the database with an error: {}. \nTrying to repair the database", err);
DB::repair(&opts, &path)
.map_err(|e| DatabaseError::Other(e.into()))?;

let cf_descriptors = columns.clone().into_iter().map(|i| {
ColumnFamilyDescriptor::new(
Self::col_name(i.id()),
Self::cf_opts(i, &block_opts),
)
});
DB::open_cf_descriptors(&opts, &path, cf_descriptors)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
DB::open_cf_descriptors(&opts, &path, cf_descriptors)
DB::open_cf_descriptors(&opts, &path, cf_descriptors_to_open)

},
}
.map_err(|e| DatabaseError::Other(e.into()))?;
let rocks_db = RocksDb {
Expand Down
Loading