diff options
author | Alex Auvolat <alex@adnab.me> | 2023-09-05 14:37:10 +0200 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2023-09-06 16:35:28 +0200 |
commit | a09f86729c1c28c6881b802b49d5574386ef1d0d (patch) | |
tree | bc024c2dede1c2ed03a24f0e0996b074e0d56942 | |
parent | 887b3233f45ade24def08b3faa2d6da5fe85a3a1 (diff) | |
download | garage-a09f86729c1c28c6881b802b49d5574386ef1d0d.tar.gz garage-a09f86729c1c28c6881b802b49d5574386ef1d0d.zip |
block manager: move blocks in write_block if necessary
-rw-r--r-- | src/block/manager.rs | 38 |
1 files changed, 26 insertions, 12 deletions
diff --git a/src/block/manager.rs b/src/block/manager.rs index 73fefa0c..798cedf9 100644 --- a/src/block/manager.rs +++ b/src/block/manager.rs @@ -707,25 +707,39 @@ impl BlockManagerLocked { let compressed = data.is_compressed(); let data = data.inner_buffer(); - let mut path = mgr.data_layout.primary_block_dir(hash); - let directory = path.clone(); - path.push(hex::encode(hash)); + let mut tgt_path = mgr.data_layout.primary_block_dir(hash); + let directory = tgt_path.clone(); + tgt_path.push(hex::encode(hash)); + if compressed { + tgt_path.set_extension("zst"); + } fs::create_dir_all(&directory).await?; let to_delete = match (mgr.find_block(hash).await, compressed) { + // If the block is stored in the wrong directory, + // write it again at the correct path and delete the old path + (Some(DataBlockPath::Plain(p)), false) if p != tgt_path => Some(p), + (Some(DataBlockPath::Compressed(p)), true) if p != tgt_path => Some(p), + + // If the block is already stored not compressed but we have a compressed + // copy, write the compressed copy and delete the uncompressed one + (Some(DataBlockPath::Plain(plain_path)), true) => Some(plain_path), + + // If the block is already stored compressed, + // keep the stored copy, we have nothing to do (Some(DataBlockPath::Compressed(_)), _) => return Ok(()), + + // If the block is already stored not compressed, + // and we don't have a compressed copy either, + // keep the stored copy, we have nothing to do (Some(DataBlockPath::Plain(_)), false) => return Ok(()), - (Some(DataBlockPath::Plain(plain_path)), true) => Some(plain_path), - (None, compressed) => { - if compressed { - path.set_extension("zst"); - } - None - } + + // If the block isn't stored already, just store what is given to us + (None, _) => None, }; - let mut path_tmp = path.clone(); + let mut path_tmp = tgt_path.clone(); let tmp_extension = format!("tmp{}", hex::encode(thread_rng().gen::<[u8; 4]>())); path_tmp.set_extension(tmp_extension); @@ -740,7 +754,7 @@ impl BlockManagerLocked { drop(f); - fs::rename(path_tmp, path).await?; + fs::rename(path_tmp, tgt_path).await?; delete_on_drop.cancel(); |