Compare commits
2 commits
07c7895948
...
e9c42bca34
Author | SHA1 | Date | |
---|---|---|---|
e9c42bca34 | |||
cd1069c1d4 |
3 changed files with 106 additions and 92 deletions
|
@ -7,83 +7,110 @@ use zstd::stream::Encoder;
|
||||||
use garage_util::data::*;
|
use garage_util::data::*;
|
||||||
use garage_util::error::*;
|
use garage_util::error::*;
|
||||||
|
|
||||||
|
use garage_net::stream::ByteStream;
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize, Copy, Clone)]
|
#[derive(Debug, Serialize, Deserialize, Copy, Clone)]
|
||||||
pub enum DataBlockHeader {
|
pub enum DataBlockHeader {
|
||||||
Plain,
|
Plain,
|
||||||
Compressed,
|
Compressed,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A possibly compressed block of data
|
#[derive(Debug)]
|
||||||
pub enum DataBlock {
|
pub struct DataBlockElem<T> {
|
||||||
/// Uncompressed data
|
header: DataBlockHeader,
|
||||||
Plain(Bytes),
|
elem: T,
|
||||||
/// Data compressed with zstd
|
|
||||||
Compressed(Bytes),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
/// A possibly compressed block of data
|
||||||
pub enum DataBlockPath {
|
pub type DataBlock = DataBlockElem<Bytes>;
|
||||||
/// Uncompressed data fail
|
|
||||||
Plain(PathBuf),
|
/// A path to a possibly compressed block of data
|
||||||
/// Compressed data fail
|
pub type DataBlockPath = DataBlockElem<PathBuf>;
|
||||||
Compressed(PathBuf),
|
|
||||||
|
/// A stream of possibly compressed block data
|
||||||
|
pub type DataBlockStream = DataBlockElem<ByteStream>;
|
||||||
|
|
||||||
|
impl DataBlockHeader {
|
||||||
|
pub fn is_compressed(&self) -> bool {
|
||||||
|
matches!(self, DataBlockHeader::Compressed)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> DataBlockElem<T> {
|
||||||
|
pub fn from_parts(header: DataBlockHeader, elem: T) -> Self {
|
||||||
|
Self { header, elem }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn plain(elem: T) -> Self {
|
||||||
|
Self {
|
||||||
|
header: DataBlockHeader::Plain,
|
||||||
|
elem,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn compressed(elem: T) -> Self {
|
||||||
|
Self {
|
||||||
|
header: DataBlockHeader::Compressed,
|
||||||
|
elem,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn into_parts(self) -> (DataBlockHeader, T) {
|
||||||
|
(self.header, self.elem)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn as_parts_ref(&self) -> (DataBlockHeader, &T) {
|
||||||
|
(self.header, &self.elem)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Query whether this block is compressed
|
||||||
|
pub fn is_compressed(&self) -> bool {
|
||||||
|
self.header.is_compressed()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DataBlock {
|
impl DataBlock {
|
||||||
/// Query whether this block is compressed
|
|
||||||
pub fn is_compressed(&self) -> bool {
|
|
||||||
matches!(self, DataBlock::Compressed(_))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get the inner, possibly compressed buffer. You should probably use [`DataBlock::verify_get`]
|
/// Get the inner, possibly compressed buffer. You should probably use [`DataBlock::verify_get`]
|
||||||
/// instead
|
/// instead
|
||||||
pub fn inner_buffer(&self) -> &[u8] {
|
pub fn inner_buffer(&self) -> &[u8] {
|
||||||
use DataBlock::*;
|
&self.elem
|
||||||
let (Plain(ref res) | Compressed(ref res)) = self;
|
|
||||||
res
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Verify data integrity. Does not return the buffer content.
|
/// Verify data integrity. Does not return the buffer content.
|
||||||
pub fn verify(&self, hash: Hash) -> Result<(), Error> {
|
pub fn verify(&self, hash: Hash) -> Result<(), Error> {
|
||||||
match self {
|
match self.header {
|
||||||
DataBlock::Plain(data) => {
|
DataBlockHeader::Plain => {
|
||||||
if blake2sum(data) == hash {
|
if blake2sum(&self.elem) == hash {
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
Err(Error::CorruptData(hash))
|
Err(Error::CorruptData(hash))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
DataBlock::Compressed(data) => zstd::stream::copy_decode(&data[..], std::io::sink())
|
DataBlockHeader::Compressed => {
|
||||||
.map_err(|_| Error::CorruptData(hash)),
|
zstd::stream::copy_decode(&self.elem[..], std::io::sink())
|
||||||
|
.map_err(|_| Error::CorruptData(hash))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn from_buffer(data: Bytes, level: Option<i32>) -> DataBlock {
|
pub async fn from_buffer(data: Bytes, level: Option<i32>) -> DataBlock {
|
||||||
tokio::task::spawn_blocking(move || {
|
tokio::task::spawn_blocking(move || {
|
||||||
if let Some(level) = level {
|
if let Some(level) = level {
|
||||||
if let Ok(data) = zstd_encode(&data[..], level) {
|
if let Ok(data_compressed) = zstd_encode(&data[..], level) {
|
||||||
return DataBlock::Compressed(data.into());
|
return DataBlock {
|
||||||
|
header: DataBlockHeader::Compressed,
|
||||||
|
elem: data_compressed.into(),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
DataBlock::Plain(data)
|
DataBlock {
|
||||||
|
header: DataBlockHeader::Plain,
|
||||||
|
elem: data.into(),
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn into_parts(self) -> (DataBlockHeader, Bytes) {
|
|
||||||
match self {
|
|
||||||
DataBlock::Plain(data) => (DataBlockHeader::Plain, data),
|
|
||||||
DataBlock::Compressed(data) => (DataBlockHeader::Compressed, data),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn from_parts(h: DataBlockHeader, bytes: Bytes) -> Self {
|
|
||||||
match h {
|
|
||||||
DataBlockHeader::Plain => DataBlock::Plain(bytes),
|
|
||||||
DataBlockHeader::Compressed => DataBlock::Compressed(bytes),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn zstd_encode<R: std::io::Read>(mut source: R, level: i32) -> std::io::Result<Vec<u8>> {
|
fn zstd_encode<R: std::io::Read>(mut source: R, level: i32) -> std::io::Result<Vec<u8>> {
|
||||||
|
|
|
@ -229,11 +229,9 @@ impl BlockManager {
|
||||||
&self,
|
&self,
|
||||||
hash: &Hash,
|
hash: &Hash,
|
||||||
order_tag: Option<OrderTag>,
|
order_tag: Option<OrderTag>,
|
||||||
) -> Result<(DataBlockHeader, ByteStream), Error> {
|
) -> Result<DataBlockStream, Error> {
|
||||||
self.rpc_get_raw_block_internal(hash, order_tag, |header, stream| async move {
|
self.rpc_get_raw_block_internal(hash, order_tag, |stream| async move { Ok(stream) })
|
||||||
Ok((header, stream))
|
.await
|
||||||
})
|
|
||||||
.await
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Ask nodes that might have a (possibly compressed) block for it
|
/// Ask nodes that might have a (possibly compressed) block for it
|
||||||
|
@ -243,7 +241,8 @@ impl BlockManager {
|
||||||
hash: &Hash,
|
hash: &Hash,
|
||||||
order_tag: Option<OrderTag>,
|
order_tag: Option<OrderTag>,
|
||||||
) -> Result<DataBlock, Error> {
|
) -> Result<DataBlock, Error> {
|
||||||
self.rpc_get_raw_block_internal(hash, order_tag, |header, stream| async move {
|
self.rpc_get_raw_block_internal(hash, order_tag, |block_stream| async move {
|
||||||
|
let (header, stream) = block_stream.into_parts();
|
||||||
read_stream_to_end(stream)
|
read_stream_to_end(stream)
|
||||||
.await
|
.await
|
||||||
.err_context("error in block data stream")
|
.err_context("error in block data stream")
|
||||||
|
@ -259,7 +258,7 @@ impl BlockManager {
|
||||||
f: F,
|
f: F,
|
||||||
) -> Result<T, Error>
|
) -> Result<T, Error>
|
||||||
where
|
where
|
||||||
F: Fn(DataBlockHeader, ByteStream) -> Fut,
|
F: Fn(DataBlockStream) -> Fut,
|
||||||
Fut: futures::Future<Output = Result<T, Error>>,
|
Fut: futures::Future<Output = Result<T, Error>>,
|
||||||
{
|
{
|
||||||
let who = self.replication.read_nodes(hash);
|
let who = self.replication.read_nodes(hash);
|
||||||
|
@ -281,8 +280,8 @@ impl BlockManager {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let (header, stream) = match res.into_parts() {
|
let block_stream = match res.into_parts() {
|
||||||
(Ok(BlockRpc::PutBlock { hash: _, header }), Some(stream)) => (header, stream),
|
(Ok(BlockRpc::PutBlock { hash: _, header }), Some(stream)) => DataBlockStream::from_parts(header, stream),
|
||||||
(Ok(_), _) => {
|
(Ok(_), _) => {
|
||||||
debug!("Get block {:?}: node {:?} returned a malformed response", hash, node);
|
debug!("Get block {:?}: node {:?} returned a malformed response", hash, node);
|
||||||
continue;
|
continue;
|
||||||
|
@ -292,7 +291,7 @@ impl BlockManager {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
match f(header, stream).await {
|
match f(block_stream).await {
|
||||||
Ok(ret) => return Ok(ret),
|
Ok(ret) => return Ok(ret),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
debug!("Get block {:?}: error reading stream from node {:?}: {}", hash, node, e);
|
debug!("Get block {:?}: error reading stream from node {:?}: {}", hash, node, e);
|
||||||
|
@ -316,14 +315,14 @@ impl BlockManager {
|
||||||
|
|
||||||
// ---- Public interface ----
|
// ---- Public interface ----
|
||||||
|
|
||||||
/// Ask nodes that might have a block for it,
|
/// Ask nodes that might have a block for it, return it as a stream
|
||||||
/// return it as a stream
|
|
||||||
pub async fn rpc_get_block_streaming(
|
pub async fn rpc_get_block_streaming(
|
||||||
&self,
|
&self,
|
||||||
hash: &Hash,
|
hash: &Hash,
|
||||||
order_tag: Option<OrderTag>,
|
order_tag: Option<OrderTag>,
|
||||||
) -> Result<ByteStream, Error> {
|
) -> Result<ByteStream, Error> {
|
||||||
let (header, stream) = self.rpc_get_raw_block_streaming(hash, order_tag).await?;
|
let block_stream = self.rpc_get_raw_block_streaming(hash, order_tag).await?;
|
||||||
|
let (header, stream) = block_stream.into_parts();
|
||||||
match header {
|
match header {
|
||||||
DataBlockHeader::Plain => Ok(stream),
|
DataBlockHeader::Plain => Ok(stream),
|
||||||
DataBlockHeader::Compressed => {
|
DataBlockHeader::Compressed => {
|
||||||
|
@ -336,7 +335,7 @@ impl BlockManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Ask nodes that might have a block for it
|
/// Ask nodes that might have a block for it, return it as one big Bytes
|
||||||
pub async fn rpc_get_block(
|
pub async fn rpc_get_block(
|
||||||
&self,
|
&self,
|
||||||
hash: &Hash,
|
hash: &Hash,
|
||||||
|
@ -547,10 +546,7 @@ impl BlockManager {
|
||||||
hash: &Hash,
|
hash: &Hash,
|
||||||
block_path: &DataBlockPath,
|
block_path: &DataBlockPath,
|
||||||
) -> Result<DataBlock, Error> {
|
) -> Result<DataBlock, Error> {
|
||||||
let (path, compressed) = match block_path {
|
let (header, path) = block_path.as_parts_ref();
|
||||||
DataBlockPath::Plain(p) => (p, false),
|
|
||||||
DataBlockPath::Compressed(p) => (p, true),
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut f = fs::File::open(&path).await?;
|
let mut f = fs::File::open(&path).await?;
|
||||||
let mut data = vec![];
|
let mut data = vec![];
|
||||||
|
@ -558,11 +554,7 @@ impl BlockManager {
|
||||||
self.metrics.bytes_read.add(data.len() as u64);
|
self.metrics.bytes_read.add(data.len() as u64);
|
||||||
drop(f);
|
drop(f);
|
||||||
|
|
||||||
let data = if compressed {
|
let data = DataBlock::from_parts(header, data.into());
|
||||||
DataBlock::Compressed(data.into())
|
|
||||||
} else {
|
|
||||||
DataBlock::Plain(data.into())
|
|
||||||
};
|
|
||||||
|
|
||||||
if data.verify(*hash).is_err() {
|
if data.verify(*hash).is_err() {
|
||||||
self.metrics.corruption_counter.add(1);
|
self.metrics.corruption_counter.add(1);
|
||||||
|
@ -615,20 +607,20 @@ impl BlockManager {
|
||||||
// first and then a compressed one (as compression may have been
|
// first and then a compressed one (as compression may have been
|
||||||
// previously enabled).
|
// previously enabled).
|
||||||
if fs::metadata(&path).await.is_ok() {
|
if fs::metadata(&path).await.is_ok() {
|
||||||
return Some(DataBlockPath::Plain(path));
|
return Some(DataBlockPath::plain(path));
|
||||||
}
|
}
|
||||||
path.set_extension("zst");
|
path.set_extension("zst");
|
||||||
if fs::metadata(&path).await.is_ok() {
|
if fs::metadata(&path).await.is_ok() {
|
||||||
return Some(DataBlockPath::Compressed(path));
|
return Some(DataBlockPath::compressed(path));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
path.set_extension("zst");
|
path.set_extension("zst");
|
||||||
if fs::metadata(&path).await.is_ok() {
|
if fs::metadata(&path).await.is_ok() {
|
||||||
return Some(DataBlockPath::Compressed(path));
|
return Some(DataBlockPath::compressed(path));
|
||||||
}
|
}
|
||||||
path.set_extension("");
|
path.set_extension("");
|
||||||
if fs::metadata(&path).await.is_ok() {
|
if fs::metadata(&path).await.is_ok() {
|
||||||
return Some(DataBlockPath::Plain(path));
|
return Some(DataBlockPath::plain(path));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -709,24 +701,25 @@ impl BlockManagerLocked {
|
||||||
tgt_path.set_extension("zst");
|
tgt_path.set_extension("zst");
|
||||||
}
|
}
|
||||||
|
|
||||||
let to_delete = match (existing_path, compressed) {
|
let existing_info = existing_path.map(|x| x.into_parts());
|
||||||
|
let to_delete = match (existing_info, compressed) {
|
||||||
// If the block is stored in the wrong directory,
|
// If the block is stored in the wrong directory,
|
||||||
// write it again at the correct path and delete the old path
|
// write it again at the correct path and delete the old path
|
||||||
(Some(DataBlockPath::Plain(p)), false) if p != tgt_path => Some(p),
|
(Some((DataBlockHeader::Plain, p)), false) if p != tgt_path => Some(p),
|
||||||
(Some(DataBlockPath::Compressed(p)), true) if p != tgt_path => Some(p),
|
(Some((DataBlockHeader::Compressed, p)), true) if p != tgt_path => Some(p),
|
||||||
|
|
||||||
// If the block is already stored not compressed but we have a compressed
|
// If the block is already stored not compressed but we have a compressed
|
||||||
// copy, write the compressed copy and delete the uncompressed one
|
// copy, write the compressed copy and delete the uncompressed one
|
||||||
(Some(DataBlockPath::Plain(plain_path)), true) => Some(plain_path),
|
(Some((DataBlockHeader::Plain, plain_path)), true) => Some(plain_path),
|
||||||
|
|
||||||
// If the block is already stored compressed,
|
// If the block is already stored compressed,
|
||||||
// keep the stored copy, we have nothing to do
|
// keep the stored copy, we have nothing to do
|
||||||
(Some(DataBlockPath::Compressed(_)), _) => return Ok(()),
|
(Some((DataBlockHeader::Compressed, _)), _) => return Ok(()),
|
||||||
|
|
||||||
// If the block is already stored not compressed,
|
// If the block is already stored not compressed,
|
||||||
// and we don't have a compressed copy either,
|
// and we don't have a compressed copy either,
|
||||||
// keep the stored copy, we have nothing to do
|
// keep the stored copy, we have nothing to do
|
||||||
(Some(DataBlockPath::Plain(_)), false) => return Ok(()),
|
(Some((DataBlockHeader::Plain, _)), false) => return Ok(()),
|
||||||
|
|
||||||
// If the block isn't stored already, just store what is given to us
|
// If the block isn't stored already, just store what is given to us
|
||||||
(None, _) => None,
|
(None, _) => None,
|
||||||
|
@ -778,18 +771,14 @@ impl BlockManagerLocked {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn move_block_to_corrupted(&self, block_path: &DataBlockPath) -> Result<(), Error> {
|
async fn move_block_to_corrupted(&self, block_path: &DataBlockPath) -> Result<(), Error> {
|
||||||
let (path, path2) = match block_path {
|
let (header, path) = block_path.as_parts_ref();
|
||||||
DataBlockPath::Plain(p) => {
|
|
||||||
let mut p2 = p.clone();
|
let mut path2 = path.clone();
|
||||||
p2.set_extension("corrupted");
|
if header.is_compressed() {
|
||||||
(p, p2)
|
path2.set_extension("zst.corrupted");
|
||||||
}
|
} else {
|
||||||
DataBlockPath::Compressed(p) => {
|
path2.set_extension("corrupted");
|
||||||
let mut p2 = p.clone();
|
}
|
||||||
p2.set_extension("zst.corrupted");
|
|
||||||
(p, p2)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
fs::rename(path, path2).await?;
|
fs::rename(path, path2).await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -799,9 +788,7 @@ impl BlockManagerLocked {
|
||||||
let rc = mgr.rc.get_block_rc(hash)?;
|
let rc = mgr.rc.get_block_rc(hash)?;
|
||||||
if rc.is_deletable() {
|
if rc.is_deletable() {
|
||||||
while let Some(path) = mgr.find_block(hash).await {
|
while let Some(path) = mgr.find_block(hash).await {
|
||||||
let path = match path {
|
let (_header, path) = path.as_parts_ref();
|
||||||
DataBlockPath::Plain(p) | DataBlockPath::Compressed(p) => p,
|
|
||||||
};
|
|
||||||
fs::remove_file(path).await?;
|
fs::remove_file(path).await?;
|
||||||
mgr.metrics.delete_counter.add(1);
|
mgr.metrics.delete_counter.add(1);
|
||||||
}
|
}
|
||||||
|
|
|
@ -584,8 +584,8 @@ impl Worker for RebalanceWorker {
|
||||||
let prim_loc = self.manager.data_layout.load().primary_block_dir(&hash);
|
let prim_loc = self.manager.data_layout.load().primary_block_dir(&hash);
|
||||||
if path.ancestors().all(|x| x != prim_loc) {
|
if path.ancestors().all(|x| x != prim_loc) {
|
||||||
let block_path = match path.extension() {
|
let block_path = match path.extension() {
|
||||||
None => DataBlockPath::Plain(path.clone()),
|
None => DataBlockPath::plain(path.clone()),
|
||||||
Some(x) if x.to_str() == Some("zst") => DataBlockPath::Compressed(path.clone()),
|
Some(x) if x.to_str() == Some("zst") => DataBlockPath::compressed(path.clone()),
|
||||||
_ => {
|
_ => {
|
||||||
warn!("not rebalancing file: {}", path.to_string_lossy());
|
warn!("not rebalancing file: {}", path.to_string_lossy());
|
||||||
return Ok(WorkerState::Busy);
|
return Ok(WorkerState::Busy);
|
||||||
|
|
Loading…
Add table
Reference in a new issue