2022-07-08 11:30:26 +00:00
|
|
|
use std::path::PathBuf;
|
2020-04-17 13:36:16 +00:00
|
|
|
use std::sync::Arc;
|
2022-02-22 13:52:41 +00:00
|
|
|
use std::time::Duration;
|
2020-04-09 21:45:07 +00:00
|
|
|
|
2021-10-14 09:50:12 +00:00
|
|
|
use async_trait::async_trait;
|
2022-07-18 15:18:47 +00:00
|
|
|
use bytes::Bytes;
|
2022-02-17 22:28:23 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2020-04-09 21:45:07 +00:00
|
|
|
use tokio::fs;
|
2021-03-15 21:36:41 +00:00
|
|
|
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
2022-09-02 14:47:15 +00:00
|
|
|
use tokio::sync::{mpsc, Mutex};
|
2022-02-17 22:28:23 +00:00
|
|
|
|
2022-02-18 19:39:55 +00:00
|
|
|
use opentelemetry::{
|
|
|
|
trace::{FutureExt as OtelFutureExt, TraceContextExt, Tracer},
|
2022-09-05 10:40:17 +00:00
|
|
|
Context,
|
2022-02-18 19:39:55 +00:00
|
|
|
};
|
2020-04-09 21:45:07 +00:00
|
|
|
|
2022-06-08 08:01:44 +00:00
|
|
|
use garage_db as db;
|
|
|
|
|
2020-04-24 10:10:01 +00:00
|
|
|
use garage_util::data::*;
|
2021-10-26 17:13:41 +00:00
|
|
|
use garage_util::error::*;
|
2022-02-22 13:52:41 +00:00
|
|
|
use garage_util::metrics::RecordDuration;
|
2020-04-18 17:39:08 +00:00
|
|
|
|
2021-10-14 09:50:12 +00:00
|
|
|
use garage_rpc::system::System;
|
|
|
|
use garage_rpc::*;
|
2020-04-23 17:05:46 +00:00
|
|
|
|
2021-03-26 18:41:46 +00:00
|
|
|
use garage_table::replication::{TableReplication, TableShardedReplication};
|
2020-04-23 17:05:46 +00:00
|
|
|
|
2022-03-15 11:12:12 +00:00
|
|
|
use crate::block::*;
|
2022-03-15 11:31:23 +00:00
|
|
|
use crate::metrics::*;
|
2022-03-15 11:23:33 +00:00
|
|
|
use crate::rc::*;
|
2022-07-08 11:30:26 +00:00
|
|
|
use crate::repair::*;
|
2022-09-02 14:47:15 +00:00
|
|
|
use crate::resync::*;
|
2020-04-11 21:00:26 +00:00
|
|
|
|
2021-03-26 20:53:28 +00:00
|
|
|
/// Size under which data will be stored inlined in database instead of as files
|
2020-04-18 17:30:05 +00:00
|
|
|
pub const INLINE_THRESHOLD: usize = 3072;
|
|
|
|
|
2021-10-28 12:32:55 +00:00
|
|
|
// Timeout for RPCs that read and write blocks to remote nodes
|
2022-09-02 14:47:15 +00:00
|
|
|
pub(crate) const BLOCK_RW_TIMEOUT: Duration = Duration::from_secs(30);
|
2021-10-28 12:32:55 +00:00
|
|
|
|
|
|
|
// The delay between the moment when the reference counter
|
|
|
|
// drops to zero, and the moment where we allow ourselves
|
|
|
|
// to delete the block locally.
|
2022-03-15 11:23:33 +00:00
|
|
|
pub(crate) const BLOCK_GC_DELAY: Duration = Duration::from_secs(600);
|
2020-04-17 17:16:08 +00:00
|
|
|
|
2021-03-26 20:53:28 +00:00
|
|
|
/// RPC messages used to share blocks of data between nodes
|
2020-04-18 17:21:34 +00:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
2021-10-14 09:50:12 +00:00
|
|
|
pub enum BlockRpc {
|
2020-04-18 17:21:34 +00:00
|
|
|
Ok,
|
2021-03-26 20:53:28 +00:00
|
|
|
/// Message to ask for a block of data, by hash
|
2020-04-18 17:21:34 +00:00
|
|
|
GetBlock(Hash),
|
2021-03-26 20:53:28 +00:00
|
|
|
/// Message to send a block of data, either because requested, of for first delivery of new
|
|
|
|
/// block
|
2021-12-15 10:26:43 +00:00
|
|
|
PutBlock {
|
|
|
|
hash: Hash,
|
|
|
|
data: DataBlock,
|
|
|
|
},
|
2021-03-26 20:53:28 +00:00
|
|
|
/// Ask other node if they should have this block, but don't actually have it
|
2020-04-18 17:21:34 +00:00
|
|
|
NeedBlockQuery(Hash),
|
2021-03-26 20:53:28 +00:00
|
|
|
/// Response : whether the node do require that block
|
2020-04-18 17:21:34 +00:00
|
|
|
NeedBlockReply(bool),
|
|
|
|
}
|
|
|
|
|
2021-10-15 09:05:09 +00:00
|
|
|
impl Rpc for BlockRpc {
|
|
|
|
type Response = Result<BlockRpc, Error>;
|
2021-10-14 09:50:12 +00:00
|
|
|
}
|
2020-04-18 17:21:34 +00:00
|
|
|
|
2021-03-26 20:53:28 +00:00
|
|
|
/// The block manager, handling block exchange between nodes, and block storage on local node
|
2020-04-11 21:00:26 +00:00
|
|
|
pub struct BlockManager {
|
2021-03-26 20:53:28 +00:00
|
|
|
/// Replication strategy, allowing to find on which node blocks should be located
|
2020-04-23 17:05:46 +00:00
|
|
|
pub replication: TableShardedReplication,
|
2021-03-26 20:53:28 +00:00
|
|
|
/// Directory in which block are stored
|
2020-04-11 21:00:26 +00:00
|
|
|
pub data_dir: PathBuf,
|
2022-03-15 11:31:23 +00:00
|
|
|
|
2022-03-15 11:04:12 +00:00
|
|
|
compression_level: Option<i32>,
|
2021-10-22 10:09:03 +00:00
|
|
|
|
2022-07-18 16:40:57 +00:00
|
|
|
mutation_lock: [Mutex<BlockManagerLocked>; 256],
|
2020-04-22 20:32:58 +00:00
|
|
|
|
2022-07-08 11:30:26 +00:00
|
|
|
pub(crate) rc: BlockRc,
|
2022-09-02 14:47:15 +00:00
|
|
|
pub resync: BlockResyncManager,
|
2020-04-22 20:32:58 +00:00
|
|
|
|
2022-07-08 11:30:26 +00:00
|
|
|
pub(crate) system: Arc<System>,
|
2022-09-02 14:47:15 +00:00
|
|
|
pub(crate) endpoint: Arc<Endpoint<BlockRpc, Self>>,
|
2022-02-16 13:23:04 +00:00
|
|
|
|
2022-09-02 14:47:15 +00:00
|
|
|
pub(crate) metrics: BlockManagerMetrics,
|
2022-07-08 11:30:26 +00:00
|
|
|
|
2022-09-02 14:47:15 +00:00
|
|
|
tx_scrub_command: mpsc::Sender<ScrubWorkerCommand>,
|
2020-04-09 21:45:07 +00:00
|
|
|
}
|
|
|
|
|
2021-10-22 10:09:03 +00:00
|
|
|
// This custom struct contains functions that must only be ran
|
|
|
|
// when the lock is held. We ensure that it is the case by storing
|
|
|
|
// it INSIDE a Mutex.
|
|
|
|
struct BlockManagerLocked();
|
|
|
|
|
2020-04-11 21:00:26 +00:00
|
|
|
impl BlockManager {
|
2020-04-18 17:21:34 +00:00
|
|
|
pub fn new(
|
2022-06-08 08:01:44 +00:00
|
|
|
db: &db::Db,
|
2020-04-18 17:21:34 +00:00
|
|
|
data_dir: PathBuf,
|
2022-03-15 11:04:12 +00:00
|
|
|
compression_level: Option<i32>,
|
2020-04-23 17:05:46 +00:00
|
|
|
replication: TableShardedReplication,
|
2020-04-18 17:21:34 +00:00
|
|
|
system: Arc<System>,
|
|
|
|
) -> Arc<Self> {
|
2020-04-11 21:53:32 +00:00
|
|
|
let rc = db
|
|
|
|
.open_tree("block_local_rc")
|
2020-04-11 21:00:26 +00:00
|
|
|
.expect("Unable to open block_local_rc tree");
|
2022-03-15 11:23:33 +00:00
|
|
|
let rc = BlockRc::new(rc);
|
2020-04-17 13:36:16 +00:00
|
|
|
|
2022-09-02 14:47:15 +00:00
|
|
|
let resync = BlockResyncManager::new(db, &system);
|
2022-02-25 19:42:56 +00:00
|
|
|
|
2021-10-19 14:16:10 +00:00
|
|
|
let endpoint = system
|
|
|
|
.netapp
|
2022-05-10 11:16:57 +00:00
|
|
|
.endpoint("garage_block/manager.rs/Rpc".to_string());
|
2020-04-18 17:21:34 +00:00
|
|
|
|
2022-09-02 14:47:15 +00:00
|
|
|
let metrics = BlockManagerMetrics::new(resync.queue.clone(), resync.errors.clone());
|
|
|
|
|
|
|
|
let (scrub_tx, scrub_rx) = mpsc::channel(1);
|
2022-02-16 13:23:04 +00:00
|
|
|
|
2020-04-18 17:21:34 +00:00
|
|
|
let block_manager = Arc::new(Self {
|
2020-04-23 17:05:46 +00:00
|
|
|
replication,
|
2020-04-22 20:32:58 +00:00
|
|
|
data_dir,
|
2022-03-15 11:04:12 +00:00
|
|
|
compression_level,
|
2022-07-18 16:40:57 +00:00
|
|
|
mutation_lock: [(); 256].map(|_| Mutex::new(BlockManagerLocked())),
|
2020-04-11 21:00:26 +00:00
|
|
|
rc,
|
2022-09-02 14:47:15 +00:00
|
|
|
resync,
|
2020-04-17 13:36:16 +00:00
|
|
|
system,
|
2021-10-14 09:50:12 +00:00
|
|
|
endpoint,
|
2022-02-16 13:23:04 +00:00
|
|
|
metrics,
|
2022-09-02 14:47:15 +00:00
|
|
|
tx_scrub_command: scrub_tx,
|
2020-04-18 17:21:34 +00:00
|
|
|
});
|
2021-10-14 09:50:12 +00:00
|
|
|
block_manager.endpoint.set_handler(block_manager.clone());
|
2020-04-23 14:40:59 +00:00
|
|
|
|
2022-09-02 15:18:13 +00:00
|
|
|
// Spawn a bunch of resync workers
|
|
|
|
for index in 0..MAX_RESYNC_WORKERS {
|
|
|
|
let worker = ResyncWorker::new(index, block_manager.clone());
|
|
|
|
block_manager.system.background.spawn_worker(worker);
|
|
|
|
}
|
2022-09-02 14:47:15 +00:00
|
|
|
|
|
|
|
// Spawn scrub worker
|
|
|
|
let scrub_worker = ScrubWorker::new(block_manager.clone(), scrub_rx);
|
|
|
|
block_manager.system.background.spawn_worker(scrub_worker);
|
2022-03-15 11:12:12 +00:00
|
|
|
|
2021-10-14 09:50:12 +00:00
|
|
|
block_manager
|
2020-04-23 14:40:59 +00:00
|
|
|
}
|
|
|
|
|
2021-12-15 10:26:43 +00:00
|
|
|
/// Ask nodes that might have a (possibly compressed) block for it
|
2022-09-02 14:47:15 +00:00
|
|
|
pub(crate) async fn rpc_get_raw_block(&self, hash: &Hash) -> Result<DataBlock, Error> {
|
2021-10-26 08:20:05 +00:00
|
|
|
let who = self.replication.read_nodes(hash);
|
2021-10-22 10:09:03 +00:00
|
|
|
let resps = self
|
|
|
|
.system
|
|
|
|
.rpc
|
|
|
|
.try_call_many(
|
|
|
|
&self.endpoint,
|
|
|
|
&who[..],
|
|
|
|
BlockRpc::GetBlock(*hash),
|
|
|
|
RequestStrategy::with_priority(PRIO_NORMAL)
|
|
|
|
.with_quorum(1)
|
|
|
|
.with_timeout(BLOCK_RW_TIMEOUT)
|
|
|
|
.interrupt_after_quorum(true),
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
for resp in resps {
|
2021-12-15 10:26:43 +00:00
|
|
|
if let BlockRpc::PutBlock { data, .. } = resp {
|
|
|
|
return Ok(data);
|
2021-10-22 10:09:03 +00:00
|
|
|
}
|
2020-04-17 18:58:10 +00:00
|
|
|
}
|
2021-10-22 10:09:03 +00:00
|
|
|
Err(Error::Message(format!(
|
|
|
|
"Unable to read block {:?}: no valid blocks returned",
|
|
|
|
hash
|
|
|
|
)))
|
2020-04-11 21:00:26 +00:00
|
|
|
}
|
|
|
|
|
2021-12-15 10:26:43 +00:00
|
|
|
// ---- Public interface ----
|
|
|
|
|
|
|
|
/// Ask nodes that might have a block for it
|
|
|
|
pub async fn rpc_get_block(&self, hash: &Hash) -> Result<Vec<u8>, Error> {
|
|
|
|
self.rpc_get_raw_block(hash).await?.verify_get(*hash)
|
|
|
|
}
|
|
|
|
|
2021-10-22 10:09:03 +00:00
|
|
|
/// Send block to nodes that should have it
|
2022-07-18 15:18:47 +00:00
|
|
|
pub async fn rpc_put_block(&self, hash: Hash, data: Bytes) -> Result<(), Error> {
|
2021-10-22 10:09:03 +00:00
|
|
|
let who = self.replication.write_nodes(&hash);
|
2022-07-18 15:18:47 +00:00
|
|
|
let data = DataBlock::from_buffer(data, self.compression_level).await;
|
2021-10-22 10:09:03 +00:00
|
|
|
self.system
|
|
|
|
.rpc
|
|
|
|
.try_call_many(
|
|
|
|
&self.endpoint,
|
|
|
|
&who[..],
|
2022-07-18 15:18:47 +00:00
|
|
|
// TODO: remove to_vec() here
|
2021-12-15 10:26:43 +00:00
|
|
|
BlockRpc::PutBlock { hash, data },
|
2021-10-22 10:09:03 +00:00
|
|
|
RequestStrategy::with_priority(PRIO_NORMAL)
|
|
|
|
.with_quorum(self.replication.write_quorum())
|
|
|
|
.with_timeout(BLOCK_RW_TIMEOUT),
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-04-11 21:00:26 +00:00
|
|
|
|
2021-10-22 10:09:03 +00:00
|
|
|
/// Get number of items in the refcount table
|
2022-06-08 08:01:44 +00:00
|
|
|
pub fn rc_len(&self) -> Result<usize, Error> {
|
|
|
|
Ok(self.rc.rc.len()?)
|
2021-10-22 10:09:03 +00:00
|
|
|
}
|
|
|
|
|
2022-07-08 11:30:26 +00:00
|
|
|
/// Send command to start/stop/manager scrub worker
|
|
|
|
pub async fn send_scrub_command(&self, cmd: ScrubWorkerCommand) {
|
2022-09-02 14:47:15 +00:00
|
|
|
let _ = self.tx_scrub_command.send(cmd).await;
|
2022-07-08 11:30:26 +00:00
|
|
|
}
|
|
|
|
|
2021-10-22 10:09:03 +00:00
|
|
|
//// ----- Managing the reference counter ----
|
|
|
|
|
|
|
|
/// Increment the number of time a block is used, putting it to resynchronization if it is
|
|
|
|
/// required, but not known
|
2022-06-08 08:01:44 +00:00
|
|
|
pub fn block_incref(
|
|
|
|
self: &Arc<Self>,
|
|
|
|
tx: &mut db::Transaction,
|
|
|
|
hash: Hash,
|
|
|
|
) -> db::TxOpResult<()> {
|
|
|
|
if self.rc.block_incref(tx, &hash)? {
|
2021-10-28 12:32:55 +00:00
|
|
|
// When the reference counter is incremented, there is
|
|
|
|
// normally a node that is responsible for sending us the
|
|
|
|
// data of the block. However that operation may fail,
|
|
|
|
// so in all cases we add the block here to the todo list
|
|
|
|
// to check later that it arrived correctly, and if not
|
|
|
|
// we will fecth it from someone.
|
2022-06-08 08:01:44 +00:00
|
|
|
let this = self.clone();
|
|
|
|
tokio::spawn(async move {
|
2022-09-02 14:47:15 +00:00
|
|
|
if let Err(e) = this.resync.put_to_resync(&hash, 2 * BLOCK_RW_TIMEOUT) {
|
2022-06-08 08:01:44 +00:00
|
|
|
error!("Block {:?} could not be put in resync queue: {}.", hash, e);
|
|
|
|
}
|
|
|
|
});
|
2020-04-11 21:00:26 +00:00
|
|
|
}
|
2021-10-22 10:09:03 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2020-04-09 21:45:07 +00:00
|
|
|
|
2021-10-22 10:09:03 +00:00
|
|
|
/// Decrement the number of time a block is used
|
2022-06-08 08:01:44 +00:00
|
|
|
pub fn block_decref(
|
|
|
|
self: &Arc<Self>,
|
|
|
|
tx: &mut db::Transaction,
|
|
|
|
hash: Hash,
|
|
|
|
) -> db::TxOpResult<()> {
|
|
|
|
if self.rc.block_decref(tx, &hash)? {
|
2022-03-15 11:23:33 +00:00
|
|
|
// When the RC is decremented, it might drop to zero,
|
|
|
|
// indicating that we don't need the block.
|
|
|
|
// There is a delay before we garbage collect it;
|
|
|
|
// make sure that it is handled in the resync loop
|
|
|
|
// after that delay has passed.
|
2022-06-08 08:01:44 +00:00
|
|
|
let this = self.clone();
|
|
|
|
tokio::spawn(async move {
|
2022-09-02 14:47:15 +00:00
|
|
|
if let Err(e) = this
|
|
|
|
.resync
|
|
|
|
.put_to_resync(&hash, BLOCK_GC_DELAY + Duration::from_secs(10))
|
2022-06-08 08:01:44 +00:00
|
|
|
{
|
|
|
|
error!("Block {:?} could not be put in resync queue: {}.", hash, e);
|
|
|
|
}
|
|
|
|
});
|
2021-10-22 10:09:03 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-04-11 21:00:26 +00:00
|
|
|
|
2021-10-22 10:09:03 +00:00
|
|
|
// ---- Reading and writing blocks locally ----
|
|
|
|
|
|
|
|
/// Write a block to disk
|
2022-09-02 14:47:15 +00:00
|
|
|
pub(crate) async fn write_block(
|
|
|
|
&self,
|
|
|
|
hash: &Hash,
|
|
|
|
data: &DataBlock,
|
|
|
|
) -> Result<BlockRpc, Error> {
|
2022-07-18 16:40:57 +00:00
|
|
|
let tracer = opentelemetry::global::tracer("garage");
|
|
|
|
|
2022-02-16 13:23:04 +00:00
|
|
|
let write_size = data.inner_buffer().len() as u64;
|
|
|
|
|
2022-07-18 16:40:57 +00:00
|
|
|
let res = self.mutation_lock[hash.as_slice()[0] as usize]
|
2021-10-22 10:09:03 +00:00
|
|
|
.lock()
|
2022-07-18 16:40:57 +00:00
|
|
|
.with_context(Context::current_with_span(
|
|
|
|
tracer.start("Acquire mutation_lock"),
|
|
|
|
))
|
2021-10-22 10:09:03 +00:00
|
|
|
.await
|
|
|
|
.write_block(hash, data, self)
|
2022-02-22 12:53:59 +00:00
|
|
|
.bound_record_duration(&self.metrics.block_write_duration)
|
2022-07-18 16:40:57 +00:00
|
|
|
.with_context(Context::current_with_span(
|
|
|
|
tracer.start("BlockManagerLocked::write_block"),
|
|
|
|
))
|
2022-02-16 13:23:04 +00:00
|
|
|
.await?;
|
|
|
|
|
|
|
|
self.metrics.bytes_written.add(write_size);
|
|
|
|
|
|
|
|
Ok(res)
|
2020-04-09 21:45:07 +00:00
|
|
|
}
|
|
|
|
|
2021-03-26 20:53:28 +00:00
|
|
|
/// Read block from disk, verifying it's integrity
|
2022-07-08 11:30:26 +00:00
|
|
|
pub(crate) async fn read_block(&self, hash: &Hash) -> Result<BlockRpc, Error> {
|
2022-02-22 13:52:41 +00:00
|
|
|
let data = self
|
|
|
|
.read_block_internal(hash)
|
2022-02-22 12:53:59 +00:00
|
|
|
.bound_record_duration(&self.metrics.block_read_duration)
|
|
|
|
.await?;
|
|
|
|
|
2022-02-22 13:52:41 +00:00
|
|
|
self.metrics
|
|
|
|
.bytes_read
|
|
|
|
.add(data.inner_buffer().len() as u64);
|
2022-02-22 12:53:59 +00:00
|
|
|
|
|
|
|
Ok(BlockRpc::PutBlock { hash: *hash, data })
|
|
|
|
}
|
2022-02-16 13:23:04 +00:00
|
|
|
|
2022-02-22 12:53:59 +00:00
|
|
|
async fn read_block_internal(&self, hash: &Hash) -> Result<DataBlock, Error> {
|
2021-12-15 10:26:43 +00:00
|
|
|
let mut path = self.block_path(hash);
|
|
|
|
let compressed = match self.is_block_compressed(hash).await {
|
|
|
|
Ok(c) => c,
|
2020-04-17 16:38:11 +00:00
|
|
|
Err(e) => {
|
|
|
|
// Not found but maybe we should have had it ??
|
2022-09-02 14:47:15 +00:00
|
|
|
self.resync.put_to_resync(hash, 2 * BLOCK_RW_TIMEOUT)?;
|
2020-04-17 16:38:11 +00:00
|
|
|
return Err(Into::into(e));
|
|
|
|
}
|
|
|
|
};
|
2021-12-15 10:26:43 +00:00
|
|
|
if compressed {
|
|
|
|
path.set_extension("zst");
|
|
|
|
}
|
|
|
|
let mut f = fs::File::open(&path).await?;
|
|
|
|
|
2020-04-11 21:00:26 +00:00
|
|
|
let mut data = vec![];
|
|
|
|
f.read_to_end(&mut data).await?;
|
2020-04-17 13:36:16 +00:00
|
|
|
drop(f);
|
|
|
|
|
2021-12-15 10:26:43 +00:00
|
|
|
let data = if compressed {
|
|
|
|
DataBlock::Compressed(data)
|
|
|
|
} else {
|
|
|
|
DataBlock::Plain(data)
|
|
|
|
};
|
|
|
|
|
|
|
|
if data.verify(*hash).is_err() {
|
2022-02-16 13:23:04 +00:00
|
|
|
self.metrics.corruption_counter.add(1);
|
|
|
|
|
2022-07-18 16:40:57 +00:00
|
|
|
self.mutation_lock[hash.as_slice()[0] as usize]
|
2021-10-22 10:09:03 +00:00
|
|
|
.lock()
|
|
|
|
.await
|
|
|
|
.move_block_to_corrupted(hash, self)
|
|
|
|
.await?;
|
2022-09-02 14:47:15 +00:00
|
|
|
self.resync.put_to_resync(hash, Duration::from_millis(0))?;
|
2020-04-21 17:08:42 +00:00
|
|
|
return Err(Error::CorruptData(*hash));
|
2020-04-17 13:36:16 +00:00
|
|
|
}
|
2020-04-11 21:00:26 +00:00
|
|
|
|
2022-02-22 12:53:59 +00:00
|
|
|
Ok(data)
|
2020-04-11 21:00:26 +00:00
|
|
|
}
|
2020-04-09 21:45:07 +00:00
|
|
|
|
2022-09-02 14:52:22 +00:00
|
|
|
/// Check if this node has a block and whether it needs it
|
|
|
|
pub(crate) async fn check_block_status(&self, hash: &Hash) -> Result<BlockStatus, Error> {
|
2022-09-05 10:40:17 +00:00
|
|
|
self.mutation_lock[hash.as_slice()[0] as usize]
|
2021-10-22 10:09:03 +00:00
|
|
|
.lock()
|
|
|
|
.await
|
|
|
|
.check_block_status(hash, self)
|
2022-09-02 14:52:22 +00:00
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if this node should have a block, but don't actually have it
|
|
|
|
async fn need_block(&self, hash: &Hash) -> Result<bool, Error> {
|
|
|
|
let BlockStatus { exists, needed } = self.check_block_status(hash).await?;
|
2021-10-28 12:32:55 +00:00
|
|
|
Ok(needed.is_nonzero() && !exists)
|
2020-04-17 17:16:08 +00:00
|
|
|
}
|
|
|
|
|
2022-09-02 14:52:22 +00:00
|
|
|
/// Delete block if it is not needed anymore
|
|
|
|
pub(crate) async fn delete_if_unneeded(&self, hash: &Hash) -> Result<(), Error> {
|
2022-09-05 10:40:17 +00:00
|
|
|
self.mutation_lock[hash.as_slice()[0] as usize]
|
2022-09-02 14:52:22 +00:00
|
|
|
.lock()
|
|
|
|
.await
|
|
|
|
.delete_if_unneeded(hash, self)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
2021-10-22 10:09:03 +00:00
|
|
|
/// Utility: gives the path of the directory in which a block should be found
|
2020-04-11 21:00:26 +00:00
|
|
|
fn block_dir(&self, hash: &Hash) -> PathBuf {
|
|
|
|
let mut path = self.data_dir.clone();
|
|
|
|
path.push(hex::encode(&hash.as_slice()[0..1]));
|
|
|
|
path.push(hex::encode(&hash.as_slice()[1..2]));
|
|
|
|
path
|
|
|
|
}
|
2021-10-22 10:09:03 +00:00
|
|
|
|
2021-12-15 10:26:43 +00:00
|
|
|
/// Utility: give the full path where a block should be found, minus extension if block is
|
|
|
|
/// compressed
|
2020-04-17 17:20:17 +00:00
|
|
|
fn block_path(&self, hash: &Hash) -> PathBuf {
|
|
|
|
let mut path = self.block_dir(hash);
|
|
|
|
path.push(hex::encode(hash.as_ref()));
|
|
|
|
path
|
|
|
|
}
|
2020-04-09 21:45:07 +00:00
|
|
|
|
2021-12-15 10:26:43 +00:00
|
|
|
/// Utility: check if block is stored compressed. Error if block is not stored
|
|
|
|
async fn is_block_compressed(&self, hash: &Hash) -> Result<bool, Error> {
|
|
|
|
let mut path = self.block_path(hash);
|
|
|
|
path.set_extension("zst");
|
|
|
|
if fs::metadata(&path).await.is_ok() {
|
|
|
|
return Ok(true);
|
|
|
|
}
|
|
|
|
path.set_extension("");
|
|
|
|
fs::metadata(&path).await.map(|_| false).map_err(Into::into)
|
|
|
|
}
|
2020-04-17 13:36:16 +00:00
|
|
|
}
|
|
|
|
|
2021-10-14 09:50:12 +00:00
|
|
|
#[async_trait]
|
|
|
|
impl EndpointHandler<BlockRpc> for BlockManager {
|
2021-10-15 09:05:09 +00:00
|
|
|
async fn handle(
|
|
|
|
self: &Arc<Self>,
|
|
|
|
message: &BlockRpc,
|
|
|
|
_from: NodeID,
|
|
|
|
) -> Result<BlockRpc, Error> {
|
|
|
|
match message {
|
2021-12-15 10:26:43 +00:00
|
|
|
BlockRpc::PutBlock { hash, data } => self.write_block(hash, data).await,
|
2021-10-15 09:05:09 +00:00
|
|
|
BlockRpc::GetBlock(h) => self.read_block(h).await,
|
|
|
|
BlockRpc::NeedBlockQuery(h) => self.need_block(h).await.map(BlockRpc::NeedBlockReply),
|
2022-01-03 12:58:05 +00:00
|
|
|
m => Err(Error::unexpected_rpc_message(m)),
|
2021-10-15 09:05:09 +00:00
|
|
|
}
|
2021-10-14 09:50:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-02 14:47:15 +00:00
|
|
|
pub(crate) struct BlockStatus {
|
|
|
|
pub(crate) exists: bool,
|
|
|
|
pub(crate) needed: RcEntry,
|
2021-10-22 10:09:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl BlockManagerLocked {
|
|
|
|
async fn check_block_status(
|
|
|
|
&self,
|
|
|
|
hash: &Hash,
|
|
|
|
mgr: &BlockManager,
|
|
|
|
) -> Result<BlockStatus, Error> {
|
2021-12-15 10:26:43 +00:00
|
|
|
let exists = mgr.is_block_compressed(hash).await.is_ok();
|
2022-03-15 11:23:33 +00:00
|
|
|
let needed = mgr.rc.get_block_rc(hash)?;
|
2021-10-22 10:09:03 +00:00
|
|
|
|
|
|
|
Ok(BlockStatus { exists, needed })
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn write_block(
|
|
|
|
&self,
|
|
|
|
hash: &Hash,
|
2021-12-15 10:26:43 +00:00
|
|
|
data: &DataBlock,
|
2021-10-22 10:09:03 +00:00
|
|
|
mgr: &BlockManager,
|
|
|
|
) -> Result<BlockRpc, Error> {
|
2021-12-15 10:26:43 +00:00
|
|
|
let compressed = data.is_compressed();
|
|
|
|
let data = data.inner_buffer();
|
|
|
|
|
2021-10-22 10:09:03 +00:00
|
|
|
let mut path = mgr.block_dir(hash);
|
2022-03-01 10:52:12 +00:00
|
|
|
let directory = path.clone();
|
2021-10-22 10:09:03 +00:00
|
|
|
path.push(hex::encode(hash));
|
2022-03-01 10:52:12 +00:00
|
|
|
|
|
|
|
fs::create_dir_all(&directory).await?;
|
|
|
|
|
2021-12-15 10:26:43 +00:00
|
|
|
let to_delete = match (mgr.is_block_compressed(hash).await, compressed) {
|
|
|
|
(Ok(true), _) => return Ok(BlockRpc::Ok),
|
|
|
|
(Ok(false), false) => return Ok(BlockRpc::Ok),
|
|
|
|
(Ok(false), true) => {
|
|
|
|
let path_to_delete = path.clone();
|
|
|
|
path.set_extension("zst");
|
|
|
|
Some(path_to_delete)
|
|
|
|
}
|
|
|
|
(Err(_), compressed) => {
|
|
|
|
if compressed {
|
|
|
|
path.set_extension("zst");
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
};
|
2021-10-22 10:09:03 +00:00
|
|
|
|
|
|
|
let mut path2 = path.clone();
|
|
|
|
path2.set_extension("tmp");
|
|
|
|
let mut f = fs::File::create(&path2).await?;
|
|
|
|
f.write_all(data).await?;
|
2022-03-01 10:52:12 +00:00
|
|
|
f.sync_all().await?;
|
2021-10-22 10:09:03 +00:00
|
|
|
drop(f);
|
|
|
|
|
|
|
|
fs::rename(path2, path).await?;
|
2021-12-15 10:26:43 +00:00
|
|
|
if let Some(to_delete) = to_delete {
|
|
|
|
fs::remove_file(to_delete).await?;
|
|
|
|
}
|
2021-10-22 10:09:03 +00:00
|
|
|
|
2022-03-14 10:54:00 +00:00
|
|
|
// We want to ensure that when this function returns, data is properly persisted
|
|
|
|
// to disk. The first step is the sync_all above that does an fsync on the data file.
|
|
|
|
// Now, we do an fsync on the containing directory, to ensure that the rename
|
|
|
|
// is persisted properly. See:
|
|
|
|
// http://thedjbway.b0llix.net/qmail/syncdir.html
|
2022-03-01 10:52:12 +00:00
|
|
|
let dir = fs::OpenOptions::new()
|
|
|
|
.read(true)
|
|
|
|
.mode(0)
|
|
|
|
.open(directory)
|
|
|
|
.await?;
|
|
|
|
dir.sync_all().await?;
|
|
|
|
drop(dir);
|
|
|
|
|
2021-10-22 10:09:03 +00:00
|
|
|
Ok(BlockRpc::Ok)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn move_block_to_corrupted(&self, hash: &Hash, mgr: &BlockManager) -> Result<(), Error> {
|
|
|
|
warn!(
|
|
|
|
"Block {:?} is corrupted. Renaming to .corrupted and resyncing.",
|
|
|
|
hash
|
|
|
|
);
|
2021-12-15 10:26:43 +00:00
|
|
|
let mut path = mgr.block_path(hash);
|
2021-10-22 10:09:03 +00:00
|
|
|
let mut path2 = path.clone();
|
2021-12-15 10:26:43 +00:00
|
|
|
if mgr.is_block_compressed(hash).await? {
|
|
|
|
path.set_extension("zst");
|
|
|
|
path2.set_extension("zst.corrupted");
|
|
|
|
} else {
|
|
|
|
path2.set_extension("corrupted");
|
|
|
|
}
|
2021-10-22 10:09:03 +00:00
|
|
|
fs::rename(path, path2).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn delete_if_unneeded(&self, hash: &Hash, mgr: &BlockManager) -> Result<(), Error> {
|
|
|
|
let BlockStatus { exists, needed } = self.check_block_status(hash, mgr).await?;
|
|
|
|
|
2021-10-28 12:32:55 +00:00
|
|
|
if exists && needed.is_deletable() {
|
2021-12-15 10:26:43 +00:00
|
|
|
let mut path = mgr.block_path(hash);
|
|
|
|
if mgr.is_block_compressed(hash).await? {
|
|
|
|
path.set_extension("zst");
|
|
|
|
}
|
2021-10-22 10:09:03 +00:00
|
|
|
fs::remove_file(path).await?;
|
2022-02-16 13:23:04 +00:00
|
|
|
mgr.metrics.delete_counter.add(1);
|
2021-10-22 10:09:03 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|