Reproduce multiple RangeComplete, improve cache write

This commit is contained in:
Dominik Werder
2021-05-27 20:08:33 +02:00
parent b1bed52ada
commit 5f1b8d6a36
3 changed files with 101 additions and 64 deletions

View File

@@ -426,9 +426,6 @@ impl ReadableFromFile for MinMaxAvgScalarBinBatch {
Ok(ReadPbv::new(file))
}
fn from_buf(buf: &[u8]) -> Result<Self, Error> {
let mut h = crc32fast::Hasher::new();
h.update(&buf);
info!("try to deserialize from buf len {} crc {}", buf.len(), h.finalize());
let dec: MinMaxAvgScalarBinBatch = serde_cbor::from_slice(&buf)?;
Ok(dec)
}

View File

@@ -602,34 +602,38 @@ where
};
let path = cfd.path(&node_config);
let enc = serde_cbor::to_vec(&values)?;
let mut h = crc32fast::Hasher::new();
h.update(&enc);
info!(
"Writing cache file len {} crc {}\n{:?}\npath: {:?}",
enc.len(),
h.finalize(),
cfd,
path
);
tokio::fs::create_dir_all(path.parent().unwrap()).await?;
let now = Utc::now();
let mut h = crc32fast::Hasher::new();
h.update(&now.timestamp_nanos().to_le_bytes());
let r = h.finalize();
let tmp_path =
path.parent()
.unwrap()
.join(format!("{}.tmp.{:08x}", path.file_name().unwrap().to_str().unwrap(), r));
let res = tokio::task::spawn_blocking({
let path = path.clone();
let tmp_path = tmp_path.clone();
move || {
use fs2::FileExt;
use io::Write;
// TODO write to random tmp file first and then move into place.
info!("try to write tmp at {:?}", tmp_path);
let mut f = std::fs::OpenOptions::new()
.create(true)
.truncate(true)
.create_new(true)
.write(true)
.open(&path)?;
f.lock_exclusive()?;
.open(&tmp_path)?;
if false {
f.lock_exclusive()?;
}
f.write_all(&enc)?;
f.unlock()?;
if false {
f.unlock()?;
}
f.flush()?;
Ok::<_, Error>(enc.len())
}
})
.await??;
tokio::fs::rename(&tmp_path, &path).await?;
let ret = WrittenPbCache { bytes: res as u64 };
Ok(ret)
}