-
Notifications
You must be signed in to change notification settings - Fork 774
chore: try to fast read_snapshot #8098
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
21a1d06
Add read_snapshot to spawn worker
BohuTANG b50a706
Change fuse_snapshot from sync to asyncsource
BohuTANG 85df7f4
Fix read_snapshots with last '/' and better the source name
BohuTANG 40e8448
Add more comments for the read_snapshots
BohuTANG d50c87f
Add snapshot chain
BohuTANG a5a9bb5
Make read_snapshots into chunks
BohuTANG cc23d1a
Add TableSnapshotLite for less memory
BohuTANG 37e2e6a
Change the chunk size to 5x requests
BohuTANG 4f317b4
Merge with main
BohuTANG ccdbfd5
Fix the last root snapshot
BohuTANG 20585cd
Refine the unit test
BohuTANG c159d9d
Merge branch 'main' into dev-fast-ss
mergify[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// Copyright 2022 Datafuse Labs. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use std::path::Path; | ||
use std::sync::Arc; | ||
|
||
use common_base::base::tokio::sync::Semaphore; | ||
use common_base::base::Runtime; | ||
use common_catalog::table_context::TableContext; | ||
use common_exception::ErrorCode; | ||
use common_exception::Result; | ||
use common_fuse_meta::meta::TableSnapshot; | ||
use futures_util::future; | ||
use futures_util::TryStreamExt; | ||
use opendal::ObjectMode; | ||
use opendal::Operator; | ||
use tracing::warn; | ||
use tracing::Instrument; | ||
|
||
use crate::io::MetaReaders; | ||
|
||
async fn read_snapshot( | ||
ctx: Arc<dyn TableContext>, | ||
snapshot_location: String, | ||
format_version: u64, | ||
) -> Result<Arc<TableSnapshot>> { | ||
let reader = MetaReaders::table_snapshot_reader(ctx); | ||
reader.read(snapshot_location, None, format_version).await | ||
} | ||
|
||
#[tracing::instrument(level = "debug", skip_all)] | ||
pub async fn read_snapshots_by_root_file( | ||
ctx: Arc<dyn TableContext>, | ||
root_snapshot_file: String, | ||
format_version: u64, | ||
data_accessor: &Operator, | ||
) -> Result<Vec<Result<Arc<TableSnapshot>>>> { | ||
let mut snapshot_files = vec![]; | ||
if let Some(path) = Path::new(&root_snapshot_file).parent() { | ||
let snapshot_prefix = path.to_str().unwrap_or(""); | ||
if snapshot_prefix.contains('/') { | ||
let mut ds = data_accessor.object(snapshot_prefix).list().await?; | ||
// ObjectStreamer implements `futures::Stream` | ||
while let Some(de) = ds.try_next().await? { | ||
match de.mode() { | ||
ObjectMode::FILE => { | ||
snapshot_files.push(de.path().to_string()); | ||
} | ||
_ => { | ||
warn!( | ||
"find not snapshot file in {:}, found: {:?}", | ||
snapshot_prefix, de | ||
); | ||
continue; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
let max_runtime_threads = ctx.get_settings().get_max_threads()? as usize; | ||
let max_io_requests = ctx.get_settings().get_max_storage_io_requests()? as usize; | ||
|
||
// 1.1 combine all the tasks. | ||
let mut iter = snapshot_files.iter(); | ||
let tasks = std::iter::from_fn(move || { | ||
if let Some(location) = iter.next() { | ||
let ctx = ctx.clone(); | ||
let location = location.clone(); | ||
Some( | ||
read_snapshot(ctx, location, format_version) | ||
.instrument(tracing::debug_span!("read_snapshot")), | ||
) | ||
} else { | ||
None | ||
} | ||
}); | ||
|
||
// 1.2 build the runtime. | ||
let semaphore = Arc::new(Semaphore::new(max_io_requests)); | ||
let segments_runtime = Arc::new(Runtime::with_worker_threads( | ||
max_runtime_threads, | ||
Some("fuse-req-snapshots-worker".to_owned()), | ||
)?); | ||
|
||
// 1.3 spawn all the tasks to the runtime. | ||
let join_handlers = segments_runtime | ||
.try_spawn_batch(semaphore.clone(), tasks) | ||
.await?; | ||
|
||
// 1.4 get all the result. | ||
let joint: Vec<Result<Arc<TableSnapshot>>> = future::try_join_all(join_handlers) | ||
.instrument(tracing::debug_span!("read_snapshots_join_all")) | ||
.await | ||
.map_err(|e| ErrorCode::StorageOther(format!("read snapshots failure, {}", e)))?; | ||
Ok(joint) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.