Skip to content

Commit

Permalink
Merge pull request #113 from mempool/junderw/read-only-script-gen
Browse files Browse the repository at this point in the history
Use read only when opening the history DB for popular scripts binary
  • Loading branch information
wiz authored Jan 19, 2025
2 parents c2992a6 + 1ef4742 commit df8e61e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/bin/popular-scripts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ type DB = rocksdb::DBWithThreadMode<rocksdb::MultiThreaded>;
lazy_static! {
static ref HISTORY_DB: DB = {
let config = Config::from_args();
open_raw_db(&config.db_path.join("newindex").join("history"))
open_raw_db(
&config.db_path.join("newindex").join("history"),
electrs::new_index::db::OpenMode::ReadOnly,
)
};
}

Expand Down
24 changes: 21 additions & 3 deletions src/new_index/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ pub enum DBFlush {
impl DB {
pub fn open(path: &Path, config: &Config) -> DB {
let db = DB {
db: open_raw_db(path),
db: open_raw_db(path, OpenMode::ReadWrite),
};
db.verify_compatibility(config);
db
Expand Down Expand Up @@ -290,7 +290,17 @@ impl DB {
}
}

pub fn open_raw_db<T: rocksdb::ThreadMode>(path: &Path) -> rocksdb::DBWithThreadMode<T> {
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[repr(u8)]
pub enum OpenMode {
ReadOnly,
ReadWrite,
}

pub fn open_raw_db<T: rocksdb::ThreadMode>(
path: &Path,
read_mode: OpenMode,
) -> rocksdb::DBWithThreadMode<T> {
debug!("opening DB at {:?}", path);
let mut db_opts = rocksdb::Options::default();
db_opts.create_if_missing(true);
Expand All @@ -308,5 +318,13 @@ pub fn open_raw_db<T: rocksdb::ThreadMode>(path: &Path) -> rocksdb::DBWithThread
// let mut block_opts = rocksdb::BlockBasedOptions::default();
// block_opts.set_block_size(???);

rocksdb::DBWithThreadMode::<T>::open(&db_opts, path).expect("failed to open RocksDB")
match read_mode {
OpenMode::ReadOnly => {
rocksdb::DBWithThreadMode::<T>::open_for_read_only(&db_opts, path, false)
.expect("failed to open RocksDB (READ ONLY)")
}
OpenMode::ReadWrite => {
rocksdb::DBWithThreadMode::<T>::open(&db_opts, path).expect("failed to open RocksDB")
}
}
}
9 changes: 6 additions & 3 deletions start
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ POPULAR_SCRIPTS_FILE="${POPULAR_SCRIPTS_FOLDER}/popular-scripts.txt"
# This function runs the job for generating the popular scripts text file for the precache arg
generate_popular_scripts() {
mkdir -p "${POPULAR_SCRIPTS_FOLDER}"
rm -f "${POPULAR_SCRIPTS_FILE_RAW}" "${POPULAR_SCRIPTS_FILE}"

## Use nproc * 4 threads to generate the txt file (lots of iowait, so 2x~4x core count is ok)
## Only pick up addresses with 101 history events or more
Expand All @@ -107,8 +106,12 @@ generate_popular_scripts() {
--db-dir "${DB_FOLDER}" \
> "${POPULAR_SCRIPTS_FILE_RAW}"

## Sorted and deduplicated just in case
sort "${POPULAR_SCRIPTS_FILE_RAW}" | uniq > "${POPULAR_SCRIPTS_FILE}"
## Only overwrite the existing file if the popular-scripts cargo run succeeded
if [ "$?" = "0" ];then
## Sorted and deduplicated just in case
sort "${POPULAR_SCRIPTS_FILE_RAW}" | uniq > "${POPULAR_SCRIPTS_FILE}"
fi

rm "${POPULAR_SCRIPTS_FILE_RAW}"
}

Expand Down

0 comments on commit df8e61e

Please sign in to comment.