Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: impl checkpoint for SnapShot #219

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
chore: update fusio to link file
KKould committed Nov 15, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 758daefb51493e5e4a7b1b8d271a5b57d68a224f
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -58,19 +58,19 @@ crc32fast = "1"
crossbeam-skiplist = "0.1"
datafusion = { version = "42", optional = true }
flume = { version = "0.11", features = ["async"] }
fusio = { git = "https://github.com/tonbo-io/fusio.git", rev = "e94f40271498364a12e5dc9f08bec965a63a5b2f", package = "fusio", version = "0.3.3", features = [
fusio = { git = "https://github.com/tonbo-io/fusio.git", rev = "a86969d0564f010f4f1ab136deae4cbe638d8f37", package = "fusio", version = "0.3.3", features = [
"aws",
"dyn",
"fs",
"object_store",
"tokio",
"tokio-http",
] }
fusio-dispatch = { git = "https://github.com/tonbo-io/fusio.git", rev = "e94f40271498364a12e5dc9f08bec965a63a5b2f", package = "fusio-dispatch", version = "0.2.1", features = [
fusio-dispatch = { git = "https://github.com/tonbo-io/fusio.git", rev = "a86969d0564f010f4f1ab136deae4cbe638d8f37", package = "fusio-dispatch", version = "0.2.1", features = [
"aws",
"tokio",
] }
fusio-parquet = { git = "https://github.com/tonbo-io/fusio.git", rev = "e94f40271498364a12e5dc9f08bec965a63a5b2f", package = "fusio-parquet", version = "0.2.1" }
fusio-parquet = { git = "https://github.com/tonbo-io/fusio.git", rev = "a86969d0564f010f4f1ab136deae4cbe638d8f37", package = "fusio-parquet", version = "0.2.1" }
futures-core = "0.3"
futures-io = "0.3"
futures-util = "0.3"
4 changes: 2 additions & 2 deletions bindings/python/Cargo.toml
Original file line number Diff line number Diff line change
@@ -9,8 +9,8 @@ crate-type = ["cdylib"]
[workspace]

[dependencies]
fusio = { git = "https://github.com/tonbo-io/fusio.git", rev = "e94f40271498364a12e5dc9f08bec965a63a5b2f", package = "fusio", version = "0.3.1", features = ["aws", "tokio"] }
fusio-dispatch = { git = "https://github.com/tonbo-io/fusio.git", rev = "e94f40271498364a12e5dc9f08bec965a63a5b2f", package = "fusio-dispatch", version = "0.2.0", features = [
fusio = { git = "https://github.com/tonbo-io/fusio.git", rev = "a86969d0564f010f4f1ab136deae4cbe638d8f37", package = "fusio", version = "0.3.1", features = ["aws", "tokio"] }
fusio-dispatch = { git = "https://github.com/tonbo-io/fusio.git", rev = "a86969d0564f010f4f1ab136deae4cbe638d8f37", package = "fusio-dispatch", version = "0.2.0", features = [
"aws",
"tokio",
] }
19 changes: 18 additions & 1 deletion src/fs/manager.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{collections::HashMap, sync::Arc};

use fusio::{dynamic::DynFs, path::Path, Error};
use fusio::{dynamic, dynamic::DynFs, path::Path, Error};
use fusio_dispatch::FsOptions;

pub struct StoreManager {
@@ -32,4 +32,21 @@ impl StoreManager {
}
}

pub async fn copy(
from_fs: &Arc<dyn DynFs>,
from_path: &Path,
to_fs: &Arc<dyn DynFs>,
to_path: &Path,
) -> Result<(), Error> {
if from_fs.file_system() == to_fs.file_system() {
match from_fs.link(from_path, to_path).await {
Err(Error::Unsupported { .. }) => (),
result => return result,
}
}
dynamic::fs::copy(from_fs, from_path, to_fs, to_path).await?;

Ok(())
}

// TODO: TestCases
35 changes: 20 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -137,7 +137,7 @@ pub use arrow;
use async_lock::RwLock;
use async_stream::stream;
use flume::{bounded, Sender};
use fusio::path::Path;
use fusio::{path::Path, DynFs};
use futures_core::Stream;
use futures_util::StreamExt;
use inmem::{immutable::Immutable, mutable::Mutable};
@@ -160,7 +160,10 @@ pub use crate::option::*;
use crate::{
compaction::{CompactTask, CompactionError, Compactor},
executor::Executor,
fs::{manager::StoreManager, parse_file_id, FileId, FileType},
fs::{
manager::{copy, StoreManager},
parse_file_id, FileId, FileType,
},
serdes::Decode,
snapshot::Snapshot,
stream::{
@@ -481,25 +484,24 @@ where
};

schema.recover_wal_ids =
Some(Self::recover(option, fn_increase_ts, manager, &mut schema).await?);
Some(Self::recover(option, fn_increase_ts, manager.base_fs(), &mut schema).await?);

Ok(schema)
}

async fn recover<F: Fn() -> Timestamp>(
option: Arc<DbOption<R>>,
fn_increase_ts: F,
manager: &StoreManager,
fs: &Arc<dyn DynFs>,
schema: &mut Schema<R>,
) -> Result<Vec<FileId>, DbError<R>> {
let base_fs = manager.base_fs();
let wal_dir_path = option.wal_dir_path();
let mut transaction_map = HashMap::new();
let mut wal_ids = Vec::new();

let wal_metas = {
let mut wal_metas = Vec::new();
let mut wal_stream = base_fs.list(&wal_dir_path).await?;
let mut wal_stream = fs.list(&wal_dir_path).await?;

while let Some(file_meta) = wal_stream.next().await {
wal_metas.push(file_meta?);
@@ -511,7 +513,7 @@ where
for wal_meta in wal_metas {
let wal_path = wal_meta.path;

let file = base_fs
let file = fs
.open_options(&wal_path, FileType::Wal.open_options(false))
.await?;
// SAFETY: wal_stream return only file name
@@ -641,51 +643,54 @@ where

pub(crate) async fn checkpoint<F: Fn(&FileId) -> Path>(
&self,
manager: &StoreManager,
from_fs: &Arc<dyn DynFs>,
to_fs: &Arc<dyn DynFs>,
option: &DbOption<R>,
fn_copy: F,
) -> Result<(), DbError<R>> {
if let Some(wal_id) = self.mutable.wal_id().await {
Self::copy_wal(manager, option, &fn_copy, &wal_id).await?;
Self::copy_wal(from_fs, to_fs, option, &fn_copy, &wal_id).await?;
}
for wal_id in self.immutables.iter().flat_map(|(wal_id, _)| wal_id) {
Self::copy_wal(manager, option, &fn_copy, wal_id).await?;
Self::copy_wal(from_fs, to_fs, option, &fn_copy, wal_id).await?;
}

Ok(())
}

pub(crate) async fn from_checkpoint<F: Fn() -> Timestamp>(
option: Arc<DbOption<R>>,
manager: Arc<StoreManager>,
fs: &Arc<dyn DynFs>,
fn_increase_ts: F,
record_instance: RecordInstance,
) -> Result<Schema<R>, DbError<R>> {
// The Schema restored from checkpoint does not trigger compaction.
let trigger = Arc::new(LengthTrigger::new(usize::MAX));
let mut schema = Schema {
mutable: Mutable::new(&option, trigger.clone(), manager.base_fs()).await?,
mutable: Mutable::new(&option, trigger.clone(), fs).await?,
immutables: Default::default(),
compaction_tx: None,
recover_wal_ids: None,
trigger,
record_instance,
};
let _ = Self::recover(option, fn_increase_ts, &manager, &mut schema).await?;
let _ = Self::recover(option, fn_increase_ts, fs, &mut schema).await?;

Ok(schema)
}

async fn copy_wal<F: Fn(&FileId) -> Path>(
manager: &StoreManager,
from_fs: &Arc<dyn DynFs>,
to_fs: &Arc<dyn DynFs>,
option: &DbOption<R>,
fn_copy: &F,
wal_id: &FileId,
) -> Result<(), DbError<R>> {
let from_path = option.wal_path(wal_id);
let to_path = fn_copy(wal_id);

manager.base_fs().copy(&from_path, &to_path).await?;
copy(from_fs, &from_path, to_fs, &to_path).await?;

Ok(())
}
}
24 changes: 12 additions & 12 deletions src/snapshot.rs
Original file line number Diff line number Diff line change
@@ -71,33 +71,33 @@ impl<'s, R: Record> Snapshot<'s, R> {
}

#[allow(dead_code)]
pub async fn checkpoint(&self, option: &Arc<DbOption<R>>) -> Result<(), DbError<R>> {
pub async fn checkpoint(&self, to_option: &Arc<DbOption<R>>) -> Result<(), DbError<R>> {
let to_manager =
StoreManager::new(to_option.base_fs.clone(), to_option.level_paths.clone())?;
{
self.manager
.base_fs()
.create_dir_all(&option.wal_dir_path())
.create_dir_all(&to_option.wal_dir_path())
.await
.map_err(DbError::Fusio)?;
self.manager
.base_fs()
.create_dir_all(&option.version_log_dir_path())
.create_dir_all(&to_option.version_log_dir_path())
.await
.map_err(DbError::Fusio)?;
}

self.share
.checkpoint(&self.manager, &self.option, |wal_id| {
option.wal_path(wal_id)
})
.await?;
self.version
.checkpoint(
&self.manager,
self.manager.base_fs(),
to_manager.base_fs(),
&self.option,
|table_gen, level| option.table_path(table_gen, level),
|table_log_id| option.version_log_path(table_log_id),
|wal_id| to_option.wal_path(wal_id),
)
.await?;
self.version
.checkpoint(&self.manager, &self.option, &to_manager, to_option)
.await?;

Ok(())
}
@@ -125,7 +125,7 @@ impl<'s, R: Record> Snapshot<'s, R> {
let version_set = VersionSet::from_checkpoint(option.clone(), manager.clone()).await?;
let schema = Schema::from_checkpoint(
option.clone(),
manager.clone(),
manager.base_fs(),
|| version_set.increase_ts(),
RecordInstance::Normal,
)
39 changes: 24 additions & 15 deletions src/version/mod.rs
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ use std::{
};

use flume::{SendError, Sender};
use fusio::{path::Path, DynFs, Write};
use fusio::{dynamic::fs::copy, DynFs, Write};
use parquet::arrow::ProjectionMask;
use thiserror::Error;
use tracing::error;
@@ -304,31 +304,40 @@ where
edits
}

pub(crate) async fn checkpoint<T: Fn(&FileId, usize) -> Path, L: Fn(&FileId) -> Path>(
pub(crate) async fn checkpoint(
&self,
manager: &StoreManager,
option: &DbOption<R>,
fn_copy_table: T,
fn_copy_log: L,
from_manager: &StoreManager,
from_option: &DbOption<R>,
to_manager: &StoreManager,
to_option: &DbOption<R>,
) -> Result<(), DbError<R>> {
for level in 0..MAX_LEVEL {
let level_fs = option
fn level_fs<'a, R: Record>(
store_manager: &'a StoreManager,
option: &DbOption<R>,
level: usize,
) -> &'a Arc<dyn DynFs> {
option
.level_fs_path(level)
.map(|path| manager.get_fs(path))
.unwrap_or(manager.base_fs());
.map(|path| store_manager.get_fs(path))
.unwrap_or(store_manager.base_fs())
}

for level in 0..MAX_LEVEL {
let from_level_fs = level_fs(from_manager, from_option, level);
let to_level_fs = level_fs(to_manager, to_option, level);

for scope in self.level_slice[level].iter() {
let gen = &scope.gen;
let from_path = option.table_path(gen, level);
let to_path = fn_copy_table(gen, level);
let from_path = from_option.table_path(gen, level);
let to_path = to_option.table_path(gen, level);

level_fs.copy(&from_path, &to_path).await?;
copy(from_level_fs, &from_path, to_level_fs, &to_path).await?;
}
}
let mut new_log = manager
let mut new_log = from_manager
.base_fs()
.open_options(
&fn_copy_log(&FileId::new()),
&to_option.version_log_path(&FileId::new()),
FileType::Log.open_options(false),
)
.await?;