Skip to content

Commit 6757eb2

Browse files
committed
test(arrow): cover waiters that load after an abandoned positional load
Hold the Load guard, poll load_file_for_task for the same file until it waits, then drop the guard: the waiter loads the file itself, or returns the open error when the file is gone. Also load a positional delete file that opens but fails to parse, then rewrite it and load it again on the same loader, which hangs without the guard.
1 parent 6532513 commit 6757eb2

1 file changed

Lines changed: 130 additions & 0 deletions

File tree

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

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1848,4 +1848,134 @@ mod tests {
18481848
assert!(!positions(&fresh).is_empty());
18491849
assert_eq!(positions(&retried), positions(&fresh));
18501850
}
1851+
1852+
/// Takes the load of the first test task's positional delete file, then polls
1853+
/// `load_file_for_task` for the same file until it waits on that load.
1854+
async fn wait_on_pos_del_load(
1855+
tmp_dir: &TempDir,
1856+
) -> (
1857+
PosDelLoadGuard,
1858+
std::pin::Pin<Box<impl Future<Output = Result<DeleteFileContext>>>>,
1859+
String,
1860+
) {
1861+
let task = setup(tmp_dir.path()).remove(0);
1862+
let pos_del = task
1863+
.deletes()
1864+
.iter()
1865+
.find(|delete| delete.file_type() == DataContentType::PositionDeletes)
1866+
.unwrap()
1867+
.clone();
1868+
let loader = CachingDeleteFileLoader::new(FileIO::new_with_fs(), 10, Runtime::current());
1869+
1870+
let PosDelLoadAction::Load(guard) = loader
1871+
.delete_filter
1872+
.try_start_pos_del_load(pos_del.file_path())
1873+
else {
1874+
panic!("expected Load for a new file");
1875+
};
1876+
let path = pos_del.file_path().to_string();
1877+
let mut waiter = Box::pin(async move {
1878+
CachingDeleteFileLoader::load_file_for_task(
1879+
&pos_del,
1880+
loader.basic_delete_file_loader.clone(),
1881+
loader.delete_filter.clone(),
1882+
task.schema_ref(),
1883+
)
1884+
.await
1885+
});
1886+
assert!(futures::poll!(&mut waiter).is_pending());
1887+
(guard, waiter, path)
1888+
}
1889+
1890+
#[tokio::test]
1891+
async fn test_pos_del_waiter_loads_file_after_abandoned_load() {
1892+
let tmp_dir = TempDir::new().unwrap();
1893+
let (guard, waiter, _) = wait_on_pos_del_load(&tmp_dir).await;
1894+
1895+
drop(guard);
1896+
1897+
let ctx = tokio::time::timeout(std::time::Duration::from_secs(5), waiter)
1898+
.await
1899+
.expect("waiter did not wake after the load was abandoned")
1900+
.unwrap();
1901+
assert!(matches!(ctx, DeleteFileContext::PosDels { .. }));
1902+
}
1903+
1904+
#[tokio::test]
1905+
async fn test_pos_del_waiter_returns_open_error_after_abandoned_load() {
1906+
let tmp_dir = TempDir::new().unwrap();
1907+
let (guard, waiter, path) = wait_on_pos_del_load(&tmp_dir).await;
1908+
1909+
std::fs::remove_file(&path).unwrap();
1910+
drop(guard);
1911+
1912+
let result = tokio::time::timeout(std::time::Duration::from_secs(5), waiter)
1913+
.await
1914+
.expect("waiter did not wake after the load was abandoned");
1915+
let err = result.err().unwrap();
1916+
assert!(err.to_string().contains("Failed to open file"), "{err}");
1917+
}
1918+
1919+
#[tokio::test]
1920+
async fn test_pos_del_load_can_be_retried_after_parse_failure() {
1921+
let tmp_dir = TempDir::new().unwrap();
1922+
let path = tmp_dir.path().join("pos-del.parquet");
1923+
let path = path.to_str().unwrap();
1924+
let schema = Arc::new(Schema::builder().build().unwrap());
1925+
let write = |positions: Vec<i64>| {
1926+
let batch = RecordBatch::try_new(
1927+
crate::arrow::delete_filter::tests::create_pos_del_schema(),
1928+
vec![
1929+
Arc::new(StringArray::from_iter_values(vec![
1930+
"data.parquet";
1931+
positions.len()
1932+
])),
1933+
Arc::new(Int64Array::from_iter_values(positions)),
1934+
],
1935+
)
1936+
.unwrap();
1937+
let mut writer =
1938+
ArrowWriter::try_new(File::create(path).unwrap(), batch.schema(), None).unwrap();
1939+
writer.write(&batch).unwrap();
1940+
writer.close().unwrap();
1941+
FileScanTaskDeleteFile::builder()
1942+
.with_file_path(path.to_string())
1943+
.with_file_size_in_bytes(std::fs::metadata(path).unwrap().len())
1944+
.with_file_type(DataContentType::PositionDeletes)
1945+
.with_file_format(DataFileFormat::Parquet)
1946+
.with_partition_spec_id(0)
1947+
.build()
1948+
.unwrap()
1949+
};
1950+
let loader = CachingDeleteFileLoader::new(FileIO::new_with_fs(), 10, Runtime::current());
1951+
1952+
// The file opens but fails to parse, so the load is abandoned after the open.
1953+
let invalid = write(vec![-1]);
1954+
let err = loader
1955+
.load_deletes(&[invalid], Arc::clone(&schema))
1956+
.await
1957+
.unwrap()
1958+
.err()
1959+
.unwrap();
1960+
assert!(err.message().contains("negative position"), "{err}");
1961+
1962+
// Once the file is valid, the same loader loads it instead of waiting.
1963+
let valid = write(vec![1, 3]);
1964+
let filter = tokio::time::timeout(
1965+
std::time::Duration::from_secs(5),
1966+
loader.load_deletes(&[valid], schema),
1967+
)
1968+
.await
1969+
.expect("retry waited on the failed load")
1970+
.unwrap()
1971+
.unwrap();
1972+
let positions: Vec<u64> = filter
1973+
.get_delete_vector_for_path("data.parquet")
1974+
.unwrap()
1975+
.lock()
1976+
.unwrap()
1977+
.iter()
1978+
.collect();
1979+
assert_eq!(positions, vec![1, 3]);
1980+
}
18511981
}

0 commit comments

Comments
 (0)