Skip to content

Commit

Permalink
store: cancel background RocksDB tasks before closing the database
Browse files Browse the repository at this point in the history
Per RocksDB FAQ:

> Q: Is it safe to close RocksDB while another thread is issuing read,
>    write or manual compaction requests?
> A: No.  The users of RocksDB need to make sure all functions have
>    finished before they close RocksDB.  You can speed up the waiting
>    by calling CancelAllBackgroundWork().

Better be safe than sorry so add the call before the rocksdb::DB object
is dropped.

Issue: near#3266
  • Loading branch information
mina86 committed Jun 30, 2021
1 parent 4e79467 commit 9ff45cb
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions core/store/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ use std::marker::PhantomPinned;
use std::sync::RwLock;

use borsh::{BorshDeserialize, BorshSerialize};
#[cfg(feature = "single_thread_rocksdb")]
use rocksdb::Env;
use rocksdb::{
BlockBasedOptions, Cache, ColumnFamily, ColumnFamilyDescriptor, Direction, IteratorMode,
Options, ReadOptions, WriteBatch, DB,
BlockBasedOptions, Cache, ColumnFamily, ColumnFamilyDescriptor, Direction,
Env, IteratorMode, Options, ReadOptions, WriteBatch, DB,
};
use strum::EnumIter;
use tracing::warn;
Expand Down Expand Up @@ -751,13 +749,16 @@ impl PreWriteCheckErr {
}
}

#[cfg(feature = "single_thread_rocksdb")]
impl Drop for RocksDB {
fn drop(&mut self) {
// RocksDB with only one thread stuck on wait some condition var
// Turn on additional threads to proceed
let mut env = Env::default().unwrap();
env.set_background_threads(4);
eprintln!("RocksDB::drop()");
if cfg!(feature = "single_thread_rocksdb") {
// RocksDB with only one thread stuck on wait some condition var
// Turn on additional threads to proceed
let mut env = Env::default().unwrap();
env.set_background_threads(4);
}
self.db.cancel_all_background_work(true);
}
}

Expand Down

0 comments on commit 9ff45cb

Please sign in to comment.