Skip to content

Commit

Permalink
fix: better error for concurrent create (#2962)
Browse files Browse the repository at this point in the history
Fixes #2403
  • Loading branch information
wjones127 authored Sep 30, 2024
1 parent 8d58258 commit a151f32
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
36 changes: 36 additions & 0 deletions rust/lance/src/dataset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4123,4 +4123,40 @@ mod tests {
dataset.latest_version_id().await.unwrap()
);
}

#[tokio::test]
async fn concurrent_create() {
async fn write(uri: &str) -> Result<()> {
let schema = Arc::new(ArrowSchema::new(vec![ArrowField::new(
"a",
DataType::Int32,
false,
)]));
let empty_reader = RecordBatchIterator::new(vec![], schema.clone());
Dataset::write(empty_reader, uri, None).await?;
Ok(())
}

for _ in 0..5 {
let test_dir = tempdir().unwrap();
let test_uri = test_dir.path().to_str().unwrap();

let (res1, res2) = tokio::join!(write(test_uri), write(test_uri));

assert!(res1.is_ok() || res2.is_ok());
if res1.is_err() {
assert!(
matches!(res1, Err(Error::DatasetAlreadyExists { .. })),
"{:?}",
res1
);
} else {
assert!(
matches!(res2, Err(Error::DatasetAlreadyExists { .. })),
"{:?}",
res2
);
}
}
}
}
17 changes: 13 additions & 4 deletions rust/lance/src/io/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ pub(crate) async fn commit_new_dataset(
let (mut manifest, indices) =
transaction.build_manifest(None, vec![], &transaction_file, write_config)?;

write_manifest_file(
let result = write_manifest_file(
object_store,
commit_handler,
base_path,
Expand All @@ -141,9 +141,18 @@ pub(crate) async fn commit_new_dataset(
write_config,
manifest_naming_scheme,
)
.await?;

Ok(manifest)
.await;

// TODO: Allow Append or Overwrite mode to retry using `commit_transaction`
// if there is a conflict.
match result {
Ok(()) => Ok(manifest),
Err(CommitError::CommitConflict) => Err(crate::Error::DatasetAlreadyExists {
uri: base_path.to_string(),
location: location!(),
}),
Err(CommitError::OtherError(err)) => Err(err),
}
}

/// Internal function to check if a manifest could use some migration.
Expand Down

0 comments on commit a151f32

Please sign in to comment.