Skip to content

Commit

Permalink
Stop using /dev/shm on Linux
Browse files Browse the repository at this point in the history
Some systems have a very small /dev/shm, for example, see:
docker-library/postgres#416

So we should just use the temporary directory on all operating systems.

Also:
* use TempDir to generate the temporary path
* delete the code that we copied from sled
* prefix the temporary path with the state version and network
  • Loading branch information
teor2345 committed Nov 20, 2020
1 parent f6dc92a commit 4b140f2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 30 deletions.
1 change: 1 addition & 0 deletions zebra-state/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ thiserror = "1.0.22"
tokio = { version = "0.2.22", features = ["sync"] }
displaydoc = "0.1.7"
rocksdb = "0.15.0"
tempdir = "0.3.7"

[dev-dependencies]
zebra-chain = { path = "../zebra-chain", features = ["proptest-impl"] }
Expand Down
43 changes: 13 additions & 30 deletions zebra-state/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use tempdir::TempDir;
use zebra_chain::parameters::Network;

/// Configuration for the state service.
Expand Down Expand Up @@ -29,7 +30,9 @@ pub struct Config {

/// Whether to use an ephemeral database.
///
/// Ephemeral databases are stored in memory on Linux, and in a temporary directory on other OSes.
/// Ephemeral databases are stored in a temporary directory.
/// They are deleted when Zebra exits successfully.
/// (If Zebra panics or crashes, the ephemeral database won't be deleted.)
///
/// Set to `false` by default. If this is set to `true`, [`cache_dir`] is ignored.
///
Expand All @@ -42,34 +45,10 @@ pub struct Config {
pub debug_stop_at_height: Option<u32>,
}

fn gen_temp_path() -> PathBuf {
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::SystemTime;

static SALT_COUNTER: AtomicUsize = AtomicUsize::new(0);

let seed = SALT_COUNTER.fetch_add(1, Ordering::SeqCst) as u128;

let now = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_nanos()
<< 48;

#[cfg(not(miri))]
let pid = u128::from(std::process::id());

#[cfg(miri)]
let pid = 0;

let salt = (pid << 16) + now + seed;

if cfg!(target_os = "linux") {
// use shared memory for temporary linux files
format!("/dev/shm/pagecache.tmp.{}", salt).into()
} else {
std::env::temp_dir().join(format!("pagecache.tmp.{}", salt))
}
fn gen_temp_path(prefix: &str) -> PathBuf {
TempDir::new(prefix)
.expect("temporary directory is created successfully")
.into_path()
}

impl Config {
Expand All @@ -95,7 +74,11 @@ impl Config {
opts.create_missing_column_families(true);

let path = if self.ephemeral {
gen_temp_path()
gen_temp_path(&format!(
"zebra-state-v{}-{}",
crate::constants::DATABASE_FORMAT_VERSION,
net_dir
))
} else {
self.cache_dir
.join("state")
Expand Down

0 comments on commit 4b140f2

Please sign in to comment.