Skip to content

Commit

Permalink
mapsize: try to shrink mapsize until init works
Browse files Browse the repository at this point in the history
On some systems like windows and iOS, the mapsize can be an issue.
This continually decreases the mapsize until we find one that works.

Will-Fix: damus-io/notedeck#491
  • Loading branch information
jb55 committed Nov 29, 2024
1 parent 6025e1c commit 71154e4
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::bindings;

#[derive(Copy, Clone)]
pub struct Config {
pub config: bindings::ndb_config,
}
Expand Down
63 changes: 62 additions & 1 deletion src/ndb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::os::raw::c_int;
use std::path::Path;
use std::sync::Arc;
use tokio::task; // Make sure to import the task module
use tracing::debug;

#[derive(Debug)]
struct NdbRef {
Expand Down Expand Up @@ -51,7 +52,28 @@ impl Ndb {
let _ = fs::create_dir_all(path);
}

let result = unsafe { bindings::ndb_init(&mut ndb, db_dir_cstr.as_ptr(), config.as_ptr()) };
let min_mapsize = 1024 * 1024 * 512;
let mut mapsize = config.config.mapsize;
let mut config = *config;

let result = loop {
let result =
unsafe { bindings::ndb_init(&mut ndb, db_dir_cstr.as_ptr(), config.as_ptr()) };

if result == 0 {
mapsize /= 2;
config = config.set_mapsize(mapsize);
debug!("ndb init failed, reducing mapsize to {}", mapsize);

if mapsize > min_mapsize {
continue;
} else {
break 0;
}
} else {
break result;
}
};

if result == 0 {
return Err(Error::DbOpenFailed);
Expand Down Expand Up @@ -448,4 +470,43 @@ mod tests {
assert_eq!(note.kind(), 1);
}
}

#[test]
#[cfg(target_os = "windows")]
fn test_windows_large_mapsize() {
use std::{fs, path::Path};

let db = "target/testdbs/windows_large_mapsize";
test_util::cleanup_db(&db);

{
// 32 TiB should be way too big for CI
let config =
Config::new().set_mapsize(1024usize * 1024usize * 1024usize * 1024usize * 32usize);

// in this case, nostrdb should try to keep resizing to
// smaller mapsizes until success

let ndb = Ndb::new(db, &config);

assert!(ndb.is_ok());
}

let file_len = fs::metadata(Path::new(db).join("data.mdb"))
.expect("metadata")
.len();

assert!(file_len > 0);

if cfg!(target_os = "windows") {
// on windows the default mapsize will be 1MB when we fail
// to open it
assert_ne!(file_len, 1048576);
} else {
assert!(file_len < 1024u64 * 1024u64);
}

// we should definitely clean this up... especially on windows
test_util::cleanup_db(&db);
}
}

0 comments on commit 71154e4

Please sign in to comment.