Skip to content

Commit 9d03a81

Browse files
committed
fix: validate scan completion and skip dead manifests
Own, cancel, and join planning workers, propagating failures even when no data task uses the delete index. Skip manifests whose list counts prove there are no live entries in both scan and copy-on-write planning.
1 parent 63d3d52 commit 9d03a81

4 files changed

Lines changed: 517 additions & 65 deletions

File tree

‎crates/iceberg/src/arrow/caching_delete_file_loader.rs‎

Lines changed: 67 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ impl PartnerAccessor<ArrayRef> for EqDelRecordBatchPartnerAccessor {
836836
}
837837

838838
#[cfg(test)]
839-
mod tests {
839+
pub(crate) mod tests {
840840
use std::collections::HashMap;
841841
use std::fs::File;
842842
use std::sync::Arc;
@@ -1970,7 +1970,7 @@ mod tests {
19701970
/// `gated.parquet` until the gate is opened, so one of them is still in flight when
19711971
/// another fails. `LocalFsStorage` never yields, so on the current-thread test
19721972
/// runtime each load would otherwise finish before the next one starts.
1973-
mod gated_storage {
1973+
pub(crate) mod gated_storage {
19741974
use std::sync::Arc;
19751975
use std::sync::atomic::{AtomicBool, Ordering};
19761976

@@ -1988,15 +1988,54 @@ mod tests {
19881988
#[serde(skip)]
19891989
inner: LocalFsStorage,
19901990
#[serde(skip)]
1991-
open: Arc<AtomicBool>,
1991+
gate: Arc<ReadGate>,
1992+
path: String,
1993+
fail: bool,
1994+
}
1995+
1996+
#[derive(Debug, Default)]
1997+
pub(crate) struct ReadGate {
1998+
open: AtomicBool,
1999+
released: tokio::sync::Notify,
2000+
pub(crate) started: tokio::sync::Notify,
2001+
pub(crate) stopped: tokio::sync::Notify,
2002+
}
2003+
2004+
impl ReadGate {
2005+
pub(crate) fn release(&self) {
2006+
self.open.store(true, Ordering::Release);
2007+
self.released.notify_waiters();
2008+
}
19922009
}
19932010

19942011
impl GatedStorage {
1995-
async fn wait_for_gate(&self, path: &str) {
2012+
async fn wait_for_gate(&self, path: &str) -> Result<()> {
19962013
tokio::task::yield_now().await;
1997-
while path.ends_with("gated.parquet") && !self.open.load(Ordering::SeqCst) {
1998-
tokio::task::yield_now().await;
2014+
if !path.ends_with(&self.path) {
2015+
return Ok(());
19992016
}
2017+
struct Stopped<'a>(&'a tokio::sync::Notify);
2018+
impl Drop for Stopped<'_> {
2019+
fn drop(&mut self) {
2020+
self.0.notify_one();
2021+
}
2022+
}
2023+
let _stopped = Stopped(&self.gate.stopped);
2024+
self.gate.started.notify_one();
2025+
loop {
2026+
let released = self.gate.released.notified();
2027+
if self.gate.open.load(Ordering::Acquire) {
2028+
break;
2029+
}
2030+
released.await;
2031+
}
2032+
if self.fail {
2033+
return Err(crate::Error::new(
2034+
crate::ErrorKind::Unexpected,
2035+
"injected manifest read failure",
2036+
));
2037+
}
2038+
Ok(())
20002039
}
20012040
}
20022041

@@ -2020,12 +2059,12 @@ mod tests {
20202059
}
20212060

20222061
async fn read(&self, path: &str) -> Result<Bytes> {
2023-
self.wait_for_gate(path).await;
2062+
self.wait_for_gate(path).await?;
20242063
self.inner.read(path).await
20252064
}
20262065

20272066
async fn reader(&self, path: &str) -> Result<Box<dyn FileRead>> {
2028-
self.wait_for_gate(path).await;
2067+
self.wait_for_gate(path).await?;
20292068
self.inner.reader(path).await
20302069
}
20312070

@@ -2050,21 +2089,35 @@ mod tests {
20502089
}
20512090
}
20522091

2053-
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
2054-
pub(super) struct GatedStorageFactory {
2092+
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
2093+
pub(crate) struct GatedStorageFactory {
20552094
#[serde(skip)]
2056-
pub(super) open: Arc<AtomicBool>,
2095+
pub(crate) gate: Arc<ReadGate>,
2096+
pub(crate) path: String,
2097+
pub(crate) fail: bool,
20572098
}
20582099

20592100
#[typetag::serde(name = "test_gated_storage_factory")]
20602101
impl StorageFactory for GatedStorageFactory {
20612102
fn build(&self, _config: &StorageConfig) -> Result<Arc<dyn Storage>> {
20622103
Ok(Arc::new(GatedStorage {
20632104
inner: LocalFsStorage,
2064-
open: Arc::clone(&self.open),
2105+
gate: Arc::clone(&self.gate),
2106+
path: self.path.clone(),
2107+
fail: self.fail,
20652108
}))
20662109
}
20672110
}
2111+
2112+
impl Default for GatedStorageFactory {
2113+
fn default() -> Self {
2114+
Self {
2115+
gate: Arc::new(ReadGate::default()),
2116+
path: "gated.parquet".into(),
2117+
fail: false,
2118+
}
2119+
}
2120+
}
20682121
}
20692122

20702123
/// Writes a position delete file at `path` for `data.parquet` with `positions`.
@@ -2111,7 +2164,7 @@ mod tests {
21112164
vec![1, 3],
21122165
);
21132166
let factory = Arc::new(gated_storage::GatedStorageFactory::default());
2114-
let gate = Arc::clone(&factory.open);
2167+
let gate = Arc::clone(&factory.gate);
21152168
let file_io = crate::io::FileIOBuilder::new(factory).build();
21162169
let loader = CachingDeleteFileLoader::new(file_io, 10, Runtime::current());
21172170

@@ -2123,7 +2176,7 @@ mod tests {
21232176
.unwrap();
21242177
assert!(err.message().contains("negative position"), "{err}");
21252178

2126-
gate.store(true, std::sync::atomic::Ordering::SeqCst);
2179+
gate.release();
21272180
let filter = tokio::time::timeout(
21282181
std::time::Duration::from_secs(5),
21292182
loader.load_deletes(&[valid], schema),

‎crates/iceberg/src/delete_file_index.rs‎

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ use tokio::sync::Notify;
2525

2626
use crate::error::invalid_data;
2727
use crate::metadata_columns::RESERVED_FIELD_ID_DELETE_FILE_PATH;
28-
use crate::runtime::Runtime;
2928
use crate::scan::{DeleteFileContext, FileScanTaskDeleteFile};
3029
use crate::spec::{DataContentType, DataFile, DataFileFormat, PrimitiveLiteral, Struct};
3130
use crate::{Error, Result};
@@ -102,8 +101,12 @@ fn clone_failed_index_error(err: &Error) -> Error {
102101
}
103102

104103
impl DeleteFileIndex {
105-
/// create a new `DeleteFileIndex` along with the sender that populates it with delete files
106-
pub(crate) fn new(runtime: Runtime) -> (DeleteFileIndex, Sender<DeleteFileContext>) {
104+
/// Creates an index, its input sender, and the future that populates it.
105+
pub(crate) fn new() -> (
106+
DeleteFileIndex,
107+
Sender<DeleteFileContext>,
108+
impl Future<Output = Result<()>> + Send + 'static,
109+
) {
107110
// TODO: what should the channel limit be?
108111
let (tx, rx) = channel(10);
109112
let notify = Arc::new(Notify::new());
@@ -112,7 +115,7 @@ impl DeleteFileIndex {
112115
)));
113116
let delete_file_stream = rx.boxed();
114117

115-
runtime.io().spawn({
118+
let completion = {
116119
let state = state.clone();
117120
async move {
118121
let delete_files: Vec<DeleteFileContext> =
@@ -123,15 +126,20 @@ impl DeleteFileIndex {
123126
Err(err) => DeleteFileIndexState::Failed(Box::new(err)),
124127
};
125128

129+
let result = match &new_state {
130+
DeleteFileIndexState::Failed(err) => Err(clone_failed_index_error(err)),
131+
_ => Ok(()),
132+
};
126133
{
127134
let mut guard = state.write().unwrap();
128135
*guard = new_state;
129136
}
130137
notify.notify_waiters();
138+
result
131139
}
132-
});
140+
};
133141

134-
(DeleteFileIndex { state }, tx)
142+
(DeleteFileIndex { state }, tx, completion)
135143
}
136144

137145
/// Gets all the delete files that apply to the specified data file.
@@ -1290,7 +1298,11 @@ mod tests {
12901298
let dv_1 = build_deletion_vector(data_file.file_path(), &partition, spec_id);
12911299
let dv_2 = build_deletion_vector(data_file.file_path(), &partition, spec_id);
12921300

1293-
let (index, mut tx) = DeleteFileIndex::new(Runtime::current());
1301+
let (index, mut tx, completion) = DeleteFileIndex::new();
1302+
let _completion = crate::runtime::Runtime::current()
1303+
.io()
1304+
.spawn(completion)
1305+
.abort_on_drop();
12941306
tx.try_send(DeleteFileContext {
12951307
manifest_entry: build_added_manifest_entry(5, &dv_1).into(),
12961308
partition_spec_id: spec_id,

‎crates/iceberg/src/scan/context.rs‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
use std::collections::HashMap;
1919
use std::sync::Arc;
2020

21+
use futures::SinkExt;
2122
use futures::channel::mpsc::Sender;
22-
use futures::{SinkExt, TryFutureExt};
2323

2424
use crate::delete_file_index::DeleteFileIndex;
2525
use crate::expr::{Bind, BoundPredicate, Predicate};
@@ -175,10 +175,11 @@ impl ManifestFileContext {
175175
sort_order,
176176
};
177177

178-
sender
179-
.send(manifest_entry_context)
180-
.map_err(|_| Error::new(ErrorKind::Unexpected, "mpsc channel SendError"))
181-
.await?;
178+
// The receiving worker's joined result carries its failure. Channel
179+
// closure only cancels production; it must not mask that failure.
180+
if sender.send(manifest_entry_context).await.is_err() {
181+
return Ok(());
182+
}
182183
}
183184

184185
Ok(())
@@ -325,6 +326,7 @@ impl PlanContext {
325326
// TODO: Ideally we could ditch this intermediate Vec as we return an iterator.
326327
let mut filtered_mfcs = vec![];
327328
for manifest_file in manifest_files {
329+
let mut skipped = false;
328330
let tx = if manifest_file.content == ManifestContentType::Deletes {
329331
delete_file_tx.clone()
330332
} else {
@@ -344,14 +346,18 @@ impl PlanContext {
344346
)
345347
.eval(manifest_file)?
346348
{
347-
continue;
349+
skipped = true;
348350
}
349351

350352
Some(partition_bound_predicate)
351353
} else {
352354
None
353355
};
354356

357+
skipped |= !manifest_file.has_added_files() && !manifest_file.has_existing_files();
358+
if skipped {
359+
continue;
360+
}
355361
let mfc = self.create_manifest_file_context(
356362
manifest_file,
357363
partition_bound_predicate,

0 commit comments

Comments
 (0)