diff options
author | Alex Auvolat <alex@adnab.me> | 2021-04-14 23:37:41 +0200 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2021-04-14 23:37:41 +0200 |
commit | 163ee977a0cca3e9995ee24863fbd5dc3d0f4778 (patch) | |
tree | 324cd64a0c8c05c41150f84eae857159a2ad07ef /src | |
parent | 307858bf0a077e6bb7ce77d9f5ca56c4ae2a5e63 (diff) | |
download | garage-163ee977a0cca3e9995ee24863fbd5dc3d0f4778.tar.gz garage-163ee977a0cca3e9995ee24863fbd5dc3d0f4778.zip |
Small improvements to compression code
Diffstat (limited to 'src')
-rw-r--r-- | src/model/block.rs | 28 | ||||
-rw-r--r-- | src/util/config.rs | 3 |
2 files changed, 21 insertions, 10 deletions
diff --git a/src/model/block.rs b/src/model/block.rs index 699ff32d..5e4347f6 100644 --- a/src/model/block.rs +++ b/src/model/block.rs @@ -196,12 +196,12 @@ impl BlockManager { let mut f = fs::File::create(path.clone()).await?; f.write_all(&buffer).await?; + drop(f); if clean_plain { path.set_extension(""); fs::remove_file(path).await?; } - drop(f); Ok(Message::Ok) } @@ -516,17 +516,25 @@ impl BlockManager { /// Send block to nodes that should have it pub async fn rpc_put_block(&self, hash: Hash, data: Vec<u8>) -> Result<(), Error> { let garage = self.garage.load_full().unwrap(); - let compressed = zstd_encode(&data[..], garage.config.compression_level); - let message = if compressed.is_ok() && compressed.as_ref().unwrap().len() < data.len() { - Message::PutBlock { - hash, - data: BlockData::Compressed(compressed.unwrap()), - } + + let compressed = if garage.config.enable_compression { + zstd_encode(&data[..], garage.config.compression_level).ok() + } else { + None + }; + + // If compressed data is not less than 7/8 of the size of the original data, i.e. if we + // don't gain a significant margin by compressing, then we store the plain data instead + // so that we don't lose time decompressing it on reads. + let block_data = if compressed.is_some() && compressed.as_ref().unwrap().len() < (data.len() * 7) / 8 { + BlockData::Compressed(compressed.unwrap()) } else { - Message::PutBlock { + BlockData::Plain(data) + }; + + let message = Message::PutBlock { hash, - data: BlockData::Plain(data), - } + data: block_data, }; let who = self.replication.write_nodes(&hash); self.rpc_client diff --git a/src/util/config.rs b/src/util/config.rs index 29901d46..a893cd66 100644 --- a/src/util/config.rs +++ b/src/util/config.rs @@ -45,6 +45,9 @@ pub struct Config { #[serde(default = "default_replication_factor")] pub data_replication_factor: usize, + /// Enable Zstd compression of block data + pub enable_compression: bool, + /// Zstd compression level used on data blocks #[serde(default)] pub compression_level: i32, |