-
Notifications
You must be signed in to change notification settings - Fork 287
feat(transaction): Support snapshot validation #1353
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
Open
CTTY
wants to merge
7
commits into
apache:main
Choose a base branch
from
CTTY:ctty/ss-validation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1e82327
add snapshot validation logic
CTTY 26b434a
fmt
CTTY 6a08af8
minor
CTTY fa109bc
minor
CTTY 66863a9
Merge branch 'main' into ctty/ss-validation
CTTY 29a50fc
remove validate usage
CTTY e329b10
remove Option for to_snapshot
CTTY 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
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
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,229 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you 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::collections::HashSet; | ||
use std::sync::Arc; | ||
|
||
use crate::spec::{ManifestContentType, ManifestFile, Operation, SnapshotRef, TableMetadata}; | ||
use crate::table::Table; | ||
|
||
pub(crate) trait SnapshotValidator { | ||
fn validate(&self, _table: &Table, _snapshot: Option<&SnapshotRef>) {} | ||
|
||
#[allow(dead_code)] | ||
async fn validation_history( | ||
&self, | ||
base: &Table, | ||
to_snapshot: Option<&SnapshotRef>, | ||
from_snapshot: Option<&SnapshotRef>, | ||
matching_operations: HashSet<Operation>, | ||
manifest_content_type: ManifestContentType, | ||
) -> (Vec<ManifestFile>, HashSet<i64>) { | ||
let mut manifests = vec![]; | ||
let mut new_snapshots = HashSet::new(); | ||
let mut last_snapshot: Option<&SnapshotRef> = None; | ||
|
||
let snapshots = Self::ancestors_between(to_snapshot, from_snapshot, base.metadata()); | ||
for current_snapshot in &snapshots { | ||
last_snapshot = Some(current_snapshot); | ||
|
||
if matching_operations.contains(¤t_snapshot.summary().operation) { | ||
new_snapshots.insert(current_snapshot.snapshot_id()); | ||
current_snapshot | ||
.load_manifest_list(base.file_io(), base.metadata()) | ||
.await | ||
.expect("Failed to load manifest list!") | ||
.entries() | ||
.iter() | ||
.for_each(|manifest| { | ||
if manifest.content == manifest_content_type | ||
&& manifest.added_snapshot_id == current_snapshot.snapshot_id() | ||
{ | ||
manifests.push(manifest.clone()); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
if last_snapshot.is_some() | ||
&& last_snapshot.unwrap().parent_snapshot_id() | ||
!= from_snapshot.map(|snapshot| snapshot.snapshot_id()) | ||
{ | ||
panic!( | ||
"Cannot determine history between starting snapshot {} and the last known ancestor {}", | ||
from_snapshot.map_or_else( | ||
|| "None".to_string(), | ||
|snapshot| snapshot.snapshot_id().to_string() | ||
), | ||
last_snapshot.map_or_else( | ||
|| "None".to_string(), | ||
|snapshot| snapshot.parent_snapshot_id().unwrap().to_string() | ||
) | ||
); | ||
} | ||
|
||
(manifests, new_snapshots) | ||
} | ||
|
||
fn ancestors_between( | ||
to_snapshot: Option<&SnapshotRef>, | ||
from_snapshot: Option<&SnapshotRef>, | ||
table_metadata: &TableMetadata, | ||
) -> Vec<SnapshotRef> { | ||
let mut snapshots = Vec::new(); | ||
let mut current_snapshot = to_snapshot; | ||
while let Some(snapshot) = current_snapshot { | ||
snapshots.push(Arc::clone(snapshot)); | ||
match snapshot.parent_snapshot_id() { | ||
Some(parent_snapshot_id) | ||
if from_snapshot.is_some() | ||
&& parent_snapshot_id == from_snapshot.unwrap().snapshot_id() => | ||
{ | ||
break; | ||
} | ||
Some(parent_snapshot_id) => { | ||
current_snapshot = table_metadata.snapshot_by_id(parent_snapshot_id) | ||
} | ||
None => break, | ||
} | ||
} | ||
|
||
snapshots | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::collections::HashSet; | ||
|
||
use crate::TableUpdate; | ||
use crate::spec::{ | ||
DataContentType, DataFileBuilder, DataFileFormat, Literal, ManifestContentType, Operation, | ||
SnapshotRef, Struct, | ||
}; | ||
use crate::transaction::tests::{make_v2_minimal_table, make_v2_table}; | ||
use crate::transaction::validate::SnapshotValidator; | ||
use crate::transaction::{Table, Transaction}; | ||
|
||
struct TestValidator {} | ||
|
||
impl SnapshotValidator for TestValidator {} | ||
|
||
async fn make_v2_table_with_updates() -> (Table, Vec<TableUpdate>) { | ||
let table = make_v2_minimal_table(); | ||
let tx = Transaction::new(&table); | ||
let mut action = tx.fast_append(None, vec![]).unwrap(); | ||
|
||
let data_file_1 = DataFileBuilder::default() | ||
.content(DataContentType::Data) | ||
.file_path("test/1.parquet".to_string()) | ||
.file_format(DataFileFormat::Parquet) | ||
.file_size_in_bytes(100) | ||
.record_count(1) | ||
.partition_spec_id(table.metadata().default_partition_spec_id()) | ||
.partition(Struct::from_iter([Some(Literal::long(300))])) | ||
.build() | ||
.unwrap(); | ||
|
||
let data_file_2 = DataFileBuilder::default() | ||
.content(DataContentType::Data) | ||
.file_path("test/2.parquet".to_string()) | ||
.file_format(DataFileFormat::Parquet) | ||
.file_size_in_bytes(100) | ||
.record_count(1) | ||
.partition_spec_id(table.metadata().default_partition_spec_id()) | ||
.partition(Struct::from_iter([Some(Literal::long(300))])) | ||
.build() | ||
.unwrap(); | ||
|
||
action.add_data_files(vec![data_file_1.clone()]).unwrap(); | ||
let tx = action.apply().await.unwrap(); | ||
let mut action = tx.fast_append(None, vec![]).unwrap(); | ||
action.add_data_files(vec![data_file_2.clone()]).unwrap(); | ||
let tx = action.apply().await.unwrap(); | ||
|
||
(table.clone(), tx.updates) | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_validation_history() { | ||
let (table, updates) = make_v2_table_with_updates().await; | ||
let parent_snapshot = if let TableUpdate::AddSnapshot { snapshot } = &updates[0] { | ||
SnapshotRef::new(snapshot.clone()) | ||
} else { | ||
unreachable!() | ||
}; | ||
let current_snapshot = if let TableUpdate::AddSnapshot { snapshot } = &updates[2] { | ||
SnapshotRef::new(snapshot.clone()) | ||
} else { | ||
unreachable!() | ||
}; | ||
|
||
let test_validator = TestValidator {}; | ||
|
||
// specifying from_snapshot, validating up to the from_snapshot | ||
let (manifests, snapshots) = test_validator | ||
.validation_history( | ||
&table, | ||
Some(¤t_snapshot), | ||
Some(&parent_snapshot), | ||
HashSet::from([Operation::Append]), | ||
ManifestContentType::Data, | ||
) | ||
.await; | ||
|
||
manifests | ||
.iter() | ||
.for_each(|manifest| assert_eq!(manifest.content, ManifestContentType::Data)); | ||
assert_eq!(snapshots.into_iter().collect::<Vec<_>>(), vec![ | ||
current_snapshot.snapshot_id() | ||
]); | ||
} | ||
|
||
#[test] | ||
fn test_ancestor_between() { | ||
let table = make_v2_table(); | ||
let current_snapshot = table.metadata().current_snapshot(); | ||
let parent_snapshot_id = current_snapshot.unwrap().parent_snapshot_id().unwrap(); | ||
let parent_snapshot = table.metadata().snapshot_by_id(parent_snapshot_id); | ||
|
||
// not specifying from_snapshot, listing all ancestors | ||
let all_ancestors = | ||
TestValidator::ancestors_between(current_snapshot, None, table.metadata()); | ||
assert_eq!( | ||
vec![ | ||
current_snapshot.unwrap().snapshot_id(), | ||
current_snapshot.unwrap().parent_snapshot_id().unwrap() | ||
], | ||
all_ancestors | ||
.iter() | ||
.map(|snapshot| snapshot.snapshot_id()) | ||
.collect::<Vec<_>>() | ||
); | ||
|
||
// specifying from_snapshot, listing only 1 snapshot | ||
let ancestors = | ||
TestValidator::ancestors_between(current_snapshot, parent_snapshot, table.metadata()); | ||
assert_eq!( | ||
vec![current_snapshot.unwrap().snapshot_id()], | ||
ancestors | ||
.iter() | ||
.map(|snapshot| snapshot.snapshot_id()) | ||
.collect::<Vec<_>>() | ||
); | ||
} | ||
} |
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.