2020-04-10 20:01:48 +00:00
|
|
|
use std::sync::Arc;
|
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::*;
|
2023-05-03 10:02:59 +00:00
|
|
|
use garage_util::error::*;
|
2020-04-23 17:05:46 +00:00
|
|
|
|
2021-03-10 15:21:56 +00:00
|
|
|
use garage_table::crdt::*;
|
2021-03-26 18:41:46 +00:00
|
|
|
use garage_table::replication::TableShardedReplication;
|
2020-04-24 10:10:01 +00:00
|
|
|
use garage_table::*;
|
2020-04-09 21:45:07 +00:00
|
|
|
|
2022-05-10 11:16:57 +00:00
|
|
|
use crate::s3::block_ref_table::*;
|
2020-04-18 17:39:08 +00:00
|
|
|
|
2023-01-03 13:44:47 +00:00
|
|
|
mod v05 {
|
|
|
|
use garage_util::crdt;
|
|
|
|
use garage_util::data::{Hash, Uuid};
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
/// A version of an object
|
|
|
|
#[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize)]
|
|
|
|
pub struct Version {
|
|
|
|
/// UUID of the version, used as partition key
|
|
|
|
pub uuid: Uuid,
|
|
|
|
|
|
|
|
// Actual data: the blocks for this version
|
|
|
|
// In the case of a multipart upload, also store the etags
|
|
|
|
// of individual parts and check them when doing CompleteMultipartUpload
|
|
|
|
/// Is this version deleted
|
|
|
|
pub deleted: crdt::Bool,
|
|
|
|
/// list of blocks of data composing the version
|
|
|
|
pub blocks: crdt::Map<VersionBlockKey, VersionBlock>,
|
|
|
|
/// Etag of each part in case of a multipart upload, empty otherwise
|
|
|
|
pub parts_etags: crdt::Map<u64, String>,
|
|
|
|
|
|
|
|
// Back link to bucket+key so that we can figure if
|
|
|
|
// this was deleted later on
|
|
|
|
/// Bucket in which the related object is stored
|
|
|
|
pub bucket: String,
|
|
|
|
/// Key in which the related object is stored
|
|
|
|
pub key: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, Clone, Copy, Debug, Serialize, Deserialize)]
|
|
|
|
pub struct VersionBlockKey {
|
|
|
|
/// Number of the part
|
|
|
|
pub part_number: u64,
|
2024-02-23 15:49:50 +00:00
|
|
|
/// Offset of this sub-segment in its part as sent by the client
|
|
|
|
/// (before any kind of compression or encryption)
|
2023-01-03 13:44:47 +00:00
|
|
|
pub offset: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Informations about a single block
|
|
|
|
#[derive(PartialEq, Eq, Ord, PartialOrd, Clone, Copy, Debug, Serialize, Deserialize)]
|
|
|
|
pub struct VersionBlock {
|
|
|
|
/// Blake2 sum of the block
|
|
|
|
pub hash: Hash,
|
2024-02-23 15:49:50 +00:00
|
|
|
/// Size of the block, before any kind of compression or encryption
|
2023-01-03 13:44:47 +00:00
|
|
|
pub size: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl garage_util::migrate::InitialFormat for Version {}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod v08 {
|
|
|
|
use garage_util::crdt;
|
|
|
|
use garage_util::data::Uuid;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
use super::v05;
|
|
|
|
|
2023-04-27 15:57:54 +00:00
|
|
|
pub use v05::{VersionBlock, VersionBlockKey};
|
|
|
|
|
2023-01-03 13:44:47 +00:00
|
|
|
/// A version of an object
|
|
|
|
#[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize)]
|
|
|
|
pub struct Version {
|
|
|
|
/// UUID of the version, used as partition key
|
|
|
|
pub uuid: Uuid,
|
|
|
|
|
|
|
|
// Actual data: the blocks for this version
|
|
|
|
// In the case of a multipart upload, also store the etags
|
|
|
|
// of individual parts and check them when doing CompleteMultipartUpload
|
|
|
|
/// Is this version deleted
|
|
|
|
pub deleted: crdt::Bool,
|
|
|
|
/// list of blocks of data composing the version
|
|
|
|
pub blocks: crdt::Map<VersionBlockKey, VersionBlock>,
|
|
|
|
/// Etag of each part in case of a multipart upload, empty otherwise
|
|
|
|
pub parts_etags: crdt::Map<u64, String>,
|
|
|
|
|
|
|
|
// Back link to bucket+key so that we can figure if
|
|
|
|
// this was deleted later on
|
|
|
|
/// Bucket in which the related object is stored
|
|
|
|
pub bucket_id: Uuid,
|
|
|
|
/// Key in which the related object is stored
|
|
|
|
pub key: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl garage_util::migrate::Migrate for Version {
|
|
|
|
type Previous = v05::Version;
|
|
|
|
|
|
|
|
fn migrate(old: v05::Version) -> Version {
|
|
|
|
use garage_util::data::blake2sum;
|
|
|
|
|
|
|
|
Version {
|
|
|
|
uuid: old.uuid,
|
|
|
|
deleted: old.deleted,
|
|
|
|
blocks: old.blocks,
|
|
|
|
parts_etags: old.parts_etags,
|
|
|
|
bucket_id: blake2sum(old.bucket.as_bytes()),
|
|
|
|
key: old.key,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-09 21:45:07 +00:00
|
|
|
}
|
|
|
|
|
2023-04-27 15:57:54 +00:00
|
|
|
pub(crate) mod v09 {
|
|
|
|
use garage_util::crdt;
|
|
|
|
use garage_util::data::Uuid;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
use super::v08;
|
|
|
|
|
|
|
|
pub use v08::{VersionBlock, VersionBlockKey};
|
|
|
|
|
|
|
|
/// A version of an object
|
|
|
|
#[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize)]
|
|
|
|
pub struct Version {
|
|
|
|
/// UUID of the version, used as partition key
|
|
|
|
pub uuid: Uuid,
|
|
|
|
|
|
|
|
// Actual data: the blocks for this version
|
|
|
|
// In the case of a multipart upload, also store the etags
|
|
|
|
// of individual parts and check them when doing CompleteMultipartUpload
|
|
|
|
/// Is this version deleted
|
|
|
|
pub deleted: crdt::Bool,
|
|
|
|
/// list of blocks of data composing the version
|
|
|
|
pub blocks: crdt::Map<VersionBlockKey, VersionBlock>,
|
|
|
|
|
2023-05-04 17:44:01 +00:00
|
|
|
// Back link to owner of this version (either an object or a multipart
|
|
|
|
// upload), used to find whether it has been deleted and this version
|
|
|
|
// should in turn be deleted (see versions repair procedure)
|
2023-04-27 15:57:54 +00:00
|
|
|
pub backlink: VersionBacklink,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize)]
|
|
|
|
pub enum VersionBacklink {
|
|
|
|
Object {
|
|
|
|
/// Bucket in which the related object is stored
|
|
|
|
bucket_id: Uuid,
|
|
|
|
/// Key in which the related object is stored
|
|
|
|
key: String,
|
|
|
|
},
|
|
|
|
MultipartUpload {
|
|
|
|
upload_id: Uuid,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
impl garage_util::migrate::Migrate for Version {
|
|
|
|
const VERSION_MARKER: &'static [u8] = b"G09s3v";
|
|
|
|
|
|
|
|
type Previous = v08::Version;
|
|
|
|
|
|
|
|
fn migrate(old: v08::Version) -> Version {
|
|
|
|
Version {
|
|
|
|
uuid: old.uuid,
|
|
|
|
deleted: old.deleted,
|
|
|
|
blocks: old.blocks,
|
|
|
|
backlink: VersionBacklink::Object {
|
|
|
|
bucket_id: old.bucket_id,
|
|
|
|
key: old.key,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub use v09::*;
|
2023-01-03 13:44:47 +00:00
|
|
|
|
2020-04-23 18:16:33 +00:00
|
|
|
impl Version {
|
2023-04-27 15:57:54 +00:00
|
|
|
pub fn new(uuid: Uuid, backlink: VersionBacklink, deleted: bool) -> Self {
|
2021-03-10 15:21:56 +00:00
|
|
|
Self {
|
2020-04-23 18:16:33 +00:00
|
|
|
uuid,
|
2021-03-10 15:21:56 +00:00
|
|
|
deleted: deleted.into(),
|
|
|
|
blocks: crdt::Map::new(),
|
2023-04-27 15:57:54 +00:00
|
|
|
backlink,
|
2020-04-23 18:16:33 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-11 16:31:09 +00:00
|
|
|
|
|
|
|
pub fn has_part_number(&self, part_number: u64) -> bool {
|
2023-04-27 15:57:54 +00:00
|
|
|
self.blocks
|
2022-01-11 16:31:09 +00:00
|
|
|
.items()
|
|
|
|
.binary_search_by(|(k, _)| k.part_number.cmp(&part_number))
|
2023-04-27 15:57:54 +00:00
|
|
|
.is_ok()
|
2022-01-11 16:31:09 +00:00
|
|
|
}
|
2023-05-03 10:02:59 +00:00
|
|
|
|
|
|
|
pub fn n_parts(&self) -> Result<u64, Error> {
|
|
|
|
Ok(self
|
|
|
|
.blocks
|
|
|
|
.items()
|
|
|
|
.last()
|
|
|
|
.ok_or_message("version has no parts")?
|
|
|
|
.0
|
|
|
|
.part_number)
|
|
|
|
}
|
2021-03-10 15:21:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Ord for VersionBlockKey {
|
|
|
|
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
|
|
|
|
self.part_number
|
|
|
|
.cmp(&other.part_number)
|
|
|
|
.then(self.offset.cmp(&other.offset))
|
2020-04-23 18:16:33 +00:00
|
|
|
}
|
2021-03-10 15:21:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialOrd for VersionBlockKey {
|
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
|
|
|
Some(self.cmp(other))
|
2020-04-23 18:16:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-02 21:13:08 +00:00
|
|
|
impl AutoCrdt for VersionBlock {
|
2021-03-10 15:21:56 +00:00
|
|
|
const WARN_IF_DIFFERENT: bool = true;
|
2020-04-09 21:45:07 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 12:55:11 +00:00
|
|
|
impl Entry<Uuid, EmptyKey> for Version {
|
|
|
|
fn partition_key(&self) -> &Uuid {
|
2020-04-10 21:11:52 +00:00
|
|
|
&self.uuid
|
2020-04-09 21:45:07 +00:00
|
|
|
}
|
2020-04-19 15:15:48 +00:00
|
|
|
fn sort_key(&self) -> &EmptyKey {
|
|
|
|
&EmptyKey
|
2020-04-09 21:45:07 +00:00
|
|
|
}
|
2021-03-12 18:57:37 +00:00
|
|
|
fn is_tombstone(&self) -> bool {
|
|
|
|
self.deleted.get()
|
|
|
|
}
|
2021-03-10 15:21:56 +00:00
|
|
|
}
|
2020-04-09 21:45:07 +00:00
|
|
|
|
2021-05-02 21:13:08 +00:00
|
|
|
impl Crdt for Version {
|
2020-04-09 21:45:07 +00:00
|
|
|
fn merge(&mut self, other: &Self) {
|
2021-03-10 15:21:56 +00:00
|
|
|
self.deleted.merge(&other.deleted);
|
|
|
|
|
|
|
|
if self.deleted.get() {
|
2020-04-09 21:45:07 +00:00
|
|
|
self.blocks.clear();
|
2021-03-10 15:21:56 +00:00
|
|
|
} else {
|
|
|
|
self.blocks.merge(&other.blocks);
|
2020-04-09 21:45:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct VersionTable {
|
2020-04-19 11:22:28 +00:00
|
|
|
pub block_ref_table: Arc<Table<BlockRefTable, TableShardedReplication>>,
|
2020-04-09 21:45:07 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 20:24:53 +00:00
|
|
|
impl TableSchema for VersionTable {
|
2021-12-14 11:34:01 +00:00
|
|
|
const TABLE_NAME: &'static str = "version";
|
|
|
|
|
2021-12-14 12:55:11 +00:00
|
|
|
type P = Uuid;
|
2020-04-19 15:15:48 +00:00
|
|
|
type S = EmptyKey;
|
2020-04-09 21:45:07 +00:00
|
|
|
type E = Version;
|
2020-11-20 19:11:04 +00:00
|
|
|
type Filter = DeletedFilter;
|
2020-04-09 21:45:07 +00:00
|
|
|
|
2022-06-08 08:01:44 +00:00
|
|
|
fn updated(
|
|
|
|
&self,
|
2022-12-14 10:58:06 +00:00
|
|
|
tx: &mut db::Transaction,
|
2022-06-08 08:01:44 +00:00
|
|
|
old: Option<&Self::E>,
|
|
|
|
new: Option<&Self::E>,
|
|
|
|
) -> db::TxOpResult<()> {
|
2022-12-14 10:58:06 +00:00
|
|
|
if let (Some(old_v), Some(new_v)) = (old, new) {
|
|
|
|
// Propagate deletion of version blocks
|
|
|
|
if new_v.deleted.get() && !old_v.deleted.get() {
|
|
|
|
let deleted_block_refs = old_v.blocks.items().iter().map(|(_k, vb)| BlockRef {
|
|
|
|
block: vb.hash,
|
|
|
|
version: old_v.uuid,
|
|
|
|
deleted: true.into(),
|
|
|
|
});
|
|
|
|
for block_ref in deleted_block_refs {
|
|
|
|
let res = self.block_ref_table.queue_insert(tx, &block_ref);
|
|
|
|
if let Err(e) = db::unabort(res)? {
|
|
|
|
error!("Unable to enqueue block ref deletion propagation: {}. A repair will be needed.", e);
|
|
|
|
}
|
2021-02-23 19:25:15 +00:00
|
|
|
}
|
2020-04-19 20:52:20 +00:00
|
|
|
}
|
2022-12-14 10:58:06 +00:00
|
|
|
}
|
2022-06-08 08:01:44 +00:00
|
|
|
|
|
|
|
Ok(())
|
2020-04-09 21:45:07 +00:00
|
|
|
}
|
2020-04-17 15:09:57 +00:00
|
|
|
|
2020-11-20 19:11:04 +00:00
|
|
|
fn matches_filter(entry: &Self::E, filter: &Self::Filter) -> bool {
|
2021-03-10 15:21:56 +00:00
|
|
|
filter.apply(entry.deleted.get())
|
2020-04-17 15:09:57 +00:00
|
|
|
}
|
2020-04-09 21:45:07 +00:00
|
|
|
}
|