#![allow(
clippy::unwrap_used,
clippy::expect_used,
clippy::panic,
clippy::missing_panics_doc,
// Test fixtures: every cast here is of a bounded loop counter into a byte, and the
// wrap is what makes the fill vary.
clippy::cast_possible_truncation
)]
use ant_node::storage::migration::{self, LockAttempt, VolumeLock, MIN_RETIRE_DELAY_HOURS};
use ant_node::storage::{ChunkStore, ChunkStoreConfig, LmdbStorage, LmdbStorageConfig};
use std::path::Path;
use std::sync::Arc;
use std::time::Duration;
use tempfile::TempDir;
use tokio_util::sync::CancellationToken;
const CHUNKS: usize = 60;
fn chunk_bytes(node: usize, n: usize) -> Vec<u8> {
let mut content = vec![0u8; 8192];
content[..8].copy_from_slice(&(n as u64).to_le_bytes());
content[8..16].copy_from_slice(&(node as u64).to_le_bytes());
for (i, byte) in content.iter_mut().enumerate().skip(16) {
*byte = ((i.wrapping_mul(29)).wrapping_add(n).wrapping_add(node) % 251) as u8;
}
content
}
async fn seed_legacy(root: &Path, node: usize) -> Vec<[u8; 32]> {
let lmdb = LmdbStorage::new(LmdbStorageConfig {
root_dir: root.to_path_buf(),
verify_on_read: true,
max_map_size: 0,
disk_reserve: 0,
})
.await
.expect("open legacy");
let mut keys = Vec::new();
for n in 0..CHUNKS {
let content = chunk_bytes(node, n);
let address = ant_node::client::compute_address(&content);
lmdb.put(&address, &content).await.expect("seed");
keys.push(address);
}
lmdb.wait_idle().await;
keys
}
#[test]
fn only_one_node_on_a_volume_holds_the_lock() {
let volume = TempDir::new().expect("temp dir");
let node_a = volume.path().join("node-a");
let node_b = volume.path().join("node-b");
let node_c = volume.path().join("node-c");
for root in [&node_a, &node_b, &node_c] {
std::fs::create_dir_all(root).expect("mkdir");
}
let scope = Some(volume.path());
let LockAttempt::Acquired(held) = VolumeLock::try_acquire(&node_a, scope) else {
panic!("the first node must take the lock");
};
assert!(
matches!(VolumeLock::try_acquire(&node_b, scope), LockAttempt::Busy),
"a second node on the same volume must wait"
);
assert!(
matches!(VolumeLock::try_acquire(&node_c, scope), LockAttempt::Busy),
"and so must a third"
);
drop(held);
assert!(
matches!(
VolumeLock::try_acquire(&node_b, scope),
LockAttempt::Acquired(_)
),
"the lock must pass on once the first node lets go"
);
}
fn offline_context() -> migration::MigrationContext {
migration::MigrationContext {
p2p: None,
self_id: None,
self_xor: None,
commitment: None,
replication: None,
sync_state: None,
audit_challenge_coordinator: None,
peer_commitments: None,
close_group_size: 7,
}
}
#[tokio::test]
async fn two_drivers_on_one_volume_do_not_copy_at_the_same_time() {
let volume = TempDir::new().expect("temp dir");
let mut stores = Vec::new();
let mut all_keys = Vec::new();
for node in 0..2 {
let root = volume.path().join(format!("node-{node}"));
std::fs::create_dir_all(&root).expect("mkdir");
let keys = seed_legacy(&root, node).await;
stores.push(driven_node(volume.path(), &root).await);
all_keys.push(keys);
}
let LockAttempt::Acquired(held) =
VolumeLock::try_acquire(&volume.path().join("an-outsider"), Some(volume.path()))
else {
panic!("the outsider must take the lock");
};
let shutdown = CancellationToken::new();
let drivers: Vec<_> = stores
.iter()
.map(|store| {
tokio::spawn(migration::run(
Arc::clone(store),
offline_context(),
shutdown.clone(),
))
})
.collect();
tokio::time::sleep(Duration::from_secs(4)).await;
for (node, store) in stores.iter().enumerate() {
assert_eq!(
store.legacy_only_keys().len(),
CHUNKS,
"node {node} copied while another holder had the volume"
);
}
drop(held);
let mut copier = None;
for _ in 0..300 {
if let Some(node) = stores
.iter()
.position(|s| s.legacy_only_keys().len() < CHUNKS)
{
copier = Some(node);
break;
}
tokio::time::sleep(Duration::from_millis(100)).await;
}
let copier = copier.expect("one driver should have taken the volume and started");
tokio::time::sleep(Duration::from_secs(3)).await;
let waiting = 1 - copier;
assert_eq!(
stores[waiting].legacy_only_keys().len(),
CHUNKS,
"node {waiting} copied while node {copier} held the volume"
);
for _ in 0..600 {
assert_eq!(
stores[waiting].legacy_only_keys().len(),
CHUNKS,
"node {waiting} copied while node {copier} still held the volume"
);
if stores[copier].legacy_only_keys().is_empty() {
break;
}
tokio::time::sleep(Duration::from_millis(100)).await;
}
shutdown.cancel();
for driver in drivers {
let _ = driver.await;
}
assert!(
stores[copier].legacy_only_keys().is_empty(),
"the node holding the volume did not finish copying"
);
for (node, store) in stores.iter().enumerate() {
holds_exactly_its_own(store, node, &all_keys[node]).await;
}
}
async fn driven_node(volume: &Path, root: &Path) -> Arc<ChunkStore> {
let mut config = ChunkStoreConfig {
root_dir: root.to_path_buf(),
disk_reserve: 0,
..ChunkStoreConfig::default()
};
config.migration.tick_secs = 1;
config.migration.copier_throttle_mib_per_sec = 0;
config.migration.copier_slack_mb = 0;
config.migration.batch_chunks = 8;
config.migration.lock_dir = Some(volume.to_path_buf());
Arc::new(ChunkStore::new(config).await.expect("open a node"))
}
async fn holds_exactly_its_own(store: &ChunkStore, node: usize, keys: &[[u8; 32]]) {
assert_eq!(
store.current_chunks().expect("count") as usize,
keys.len(),
"node {node} must hold its own chunks and only its own"
);
for (n, key) in keys.iter().enumerate() {
let served = store
.get(key)
.await
.expect("read")
.expect("every chunk this node seeded must still be here");
assert_eq!(
served,
chunk_bytes(node, n),
"node {node} chunk {n} is wrong"
);
}
}
#[tokio::test]
async fn a_node_waits_for_the_volume_before_it_retires() {
let volume = TempDir::new().expect("temp dir");
let root = volume.path().join("node-0");
std::fs::create_dir_all(&root).expect("mkdir");
let keys = seed_legacy(&root, 0).await;
let store = ready_to_retire(volume.path(), &root, &keys).await;
let shutdown = CancellationToken::new();
let LockAttempt::Acquired(held) =
VolumeLock::try_acquire(&volume.path().join("an-outsider"), Some(volume.path()))
else {
panic!("the outsider must take the lock");
};
let driver = tokio::spawn(migration::run(
Arc::clone(&store),
offline_context(),
shutdown.clone(),
));
for _ in 0..40 {
assert!(
store.legacy_dir_is_on_disk(),
"the node retired while an outsider held the volume"
);
tokio::time::sleep(Duration::from_millis(100)).await;
}
drop(held);
let mut retired = false;
for _ in 0..600 {
if !store.legacy_dir_is_on_disk() && !store.has_legacy() {
retired = true;
break;
}
tokio::time::sleep(Duration::from_millis(100)).await;
}
shutdown.cancel();
let _ = driver.await;
assert!(
retired,
"the node never retired once the volume was free, so it was not waiting for it"
);
for (n, key) in keys.iter().enumerate() {
let served = store
.get(key)
.await
.expect("read")
.expect("every chunk must survive retirement");
assert_eq!(
served,
chunk_bytes(0, n),
"chunk {n} is wrong after retiring"
);
}
}
async fn ready_to_retire(volume: &Path, root: &Path, keys: &[[u8; 32]]) -> Arc<ChunkStore> {
let mut config = ChunkStoreConfig {
root_dir: root.to_path_buf(),
disk_reserve: 0,
..ChunkStoreConfig::default()
};
config.migration.tick_secs = 1;
config.migration.copier_throttle_mib_per_sec = 0;
config.migration.copier_slack_mb = 0;
config.migration.lock_dir = Some(volume.to_path_buf());
let store = Arc::new(ChunkStore::new(config).await.expect("open a node"));
store
.copy_batch(keys, 0, 0, &CancellationToken::new())
.await
.expect("copy every chunk");
store.wait_idle().await;
store.commit_to_files().expect("commit to the file set");
store.note_commitment_rebuilt();
store.note_commitment_rebuilt();
store.force_migration_state(|state| {
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map_or(0, |d| d.as_secs());
state.committed_at_unix = Some(now.saturating_sub(MIN_RETIRE_DELAY_HOURS * 3600 + 60));
});
assert!(
store.legacy_dir_is_on_disk(),
"the environment should still be here before the driver runs"
);
store
}
#[tokio::test]
async fn nodes_sharing_a_volume_do_not_take_each_others_chunks() {
let volume = TempDir::new().expect("temp dir");
let shutdown = CancellationToken::new();
let mut nodes = Vec::new();
for node in 0..2 {
let root = volume.path().join(format!("node-{node}"));
std::fs::create_dir_all(&root).expect("mkdir");
let keys = seed_legacy(&root, node).await;
nodes.push((root, keys));
}
for (node, (root, keys)) in nodes.iter().enumerate() {
let mut config = ChunkStoreConfig {
root_dir: root.clone(),
disk_reserve: 0,
..ChunkStoreConfig::default()
};
config.migration.lock_dir = Some(volume.path().to_path_buf());
let store = ChunkStore::new(config).await.expect("open");
store.copy_batch(keys, 0, 0, &shutdown).await.expect("copy");
store.wait_idle().await;
assert!(
store.legacy_only_keys().is_empty(),
"node {node} should have copied everything"
);
for (n, key) in keys.iter().enumerate() {
let served = store
.get(key)
.await
.expect("read")
.expect("every chunk this node seeded must still be here");
assert_eq!(
served,
chunk_bytes(node, n),
"node {node} chunk {n} came back wrong"
);
}
}
let (root_a, keys_a) = &nodes[0];
let store_a = ChunkStore::new(ChunkStoreConfig {
root_dir: root_a.clone(),
disk_reserve: 0,
..ChunkStoreConfig::default()
})
.await
.expect("reopen node 0");
assert_eq!(
store_a.current_chunks().expect("count") as usize,
keys_a.len(),
"a node must hold its own chunks and only its own"
);
}
#[tokio::test]
async fn a_node_that_cannot_take_the_lock_keeps_serving() {
let volume = TempDir::new().expect("temp dir");
let root = volume.path().join("waiting-node");
std::fs::create_dir_all(&root).expect("mkdir");
let keys = seed_legacy(&root, 0).await;
let LockAttempt::Acquired(_held) =
VolumeLock::try_acquire(&volume.path().join("busy-node"), Some(volume.path()))
else {
panic!("the other node must take the lock");
};
let store = driven_node(volume.path(), &root).await;
let shutdown = CancellationToken::new();
let driver = tokio::spawn(migration::run(
Arc::clone(&store),
offline_context(),
shutdown.clone(),
));
assert!(store.has_legacy());
for _ in 0..30 {
assert_eq!(
store.legacy_only_keys().len(),
CHUNKS,
"the node copied while another held the volume"
);
for (n, key) in keys.iter().enumerate() {
let served = store
.get(key)
.await
.expect("read")
.expect("a node waiting for the volume still serves everything it holds");
assert_eq!(served, chunk_bytes(0, n));
}
tokio::time::sleep(Duration::from_millis(100)).await;
}
shutdown.cancel();
let _ = driver.await;
}