Skip to content

Commit ec00ee4

Browse files
authored
Stop using /dev/shm on Linux (#1338)
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
1 parent af5f3c1 commit ec00ee4

File tree

2 files changed

+14
-30
lines changed

2 files changed

+14
-30
lines changed

zebra-state/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ thiserror = "1.0.22"
2525
tokio = { version = "0.2.22", features = ["sync"] }
2626
displaydoc = "0.1.7"
2727
rocksdb = "0.15.0"
28+
tempdir = "0.3.7"
2829

2930
[dev-dependencies]
3031
zebra-chain = { path = "../zebra-chain", features = ["proptest-impl"] }

zebra-state/src/config.rs

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use serde::{Deserialize, Serialize};
22
use std::path::PathBuf;
3+
use tempdir::TempDir;
34
use zebra_chain::parameters::Network;
45

56
/// Configuration for the state service.
@@ -29,7 +30,9 @@ pub struct Config {
2930

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

45-
fn gen_temp_path() -> PathBuf {
46-
use std::sync::atomic::{AtomicUsize, Ordering};
47-
use std::time::SystemTime;
48-
49-
static SALT_COUNTER: AtomicUsize = AtomicUsize::new(0);
50-
51-
let seed = SALT_COUNTER.fetch_add(1, Ordering::SeqCst) as u128;
52-
53-
let now = SystemTime::now()
54-
.duration_since(SystemTime::UNIX_EPOCH)
55-
.unwrap()
56-
.as_nanos()
57-
<< 48;
58-
59-
#[cfg(not(miri))]
60-
let pid = u128::from(std::process::id());
61-
62-
#[cfg(miri)]
63-
let pid = 0;
64-
65-
let salt = (pid << 16) + now + seed;
66-
67-
if cfg!(target_os = "linux") {
68-
// use shared memory for temporary linux files
69-
format!("/dev/shm/pagecache.tmp.{}", salt).into()
70-
} else {
71-
std::env::temp_dir().join(format!("pagecache.tmp.{}", salt))
72-
}
48+
fn gen_temp_path(prefix: &str) -> PathBuf {
49+
TempDir::new(prefix)
50+
.expect("temporary directory is created successfully")
51+
.into_path()
7352
}
7453

7554
impl Config {
@@ -95,7 +74,11 @@ impl Config {
9574
opts.create_missing_column_families(true);
9675

9776
let path = if self.ephemeral {
98-
gen_temp_path()
77+
gen_temp_path(&format!(
78+
"zebra-state-v{}-{}",
79+
crate::constants::DATABASE_FORMAT_VERSION,
80+
net_dir
81+
))
9982
} else {
10083
self.cache_dir
10184
.join("state")

0 commit comments

Comments
 (0)