Skip to content
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

Fix empty keys in BlobDataProvider #3551

Merged
merged 2 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions provider/blob/src/blob_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ impl<'data> BlobSchemaV1<'data> {
let mut seen_min = false;
let mut seen_max = false;
for cursor in self.keys.iter0() {
for (_, idx) in cursor.iter1_copied() {
debug_assert!(idx < self.buffers.len());
for (locale, idx) in cursor.iter1_copied() {
debug_assert!(idx < self.buffers.len() || locale == Index32U8::SENTINEL);
Comment on lines +83 to +84
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue: This is problematic because a newer blob won't pass the invariants of an older blob reader

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally ok with breaking debug assertions for new-blob-old-reader.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also instead set the index to 0; it's fine because it'll never be loaded anyway (and if someone does attempt to load it raw, they'll get a deserialize error or potentially GIGO behavior.

Copy link
Member Author

@robertbastian robertbastian Jun 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that for a blob with just an empty key, 0 is also invalid as there will be zero buffers.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm willing to have debug assertions break in that relatively niche case

Copy link
Member

@Manishearth Manishearth Jun 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(but also I'm not against breaking debug assertions with the =len sentinel in the first place. would prefer to avoid it, really do not care about the empty-blob case)

if idx == 0 {
seen_min = true;
}
Expand Down Expand Up @@ -121,3 +121,7 @@ impl<'a> ZeroMapKV<'a> for Index32U8 {
type GetType = Index32U8;
type OwnedType = Box<Index32U8>;
}

impl Index32U8 {
pub(crate) const SENTINEL: &'static Self = unsafe { core::mem::transmute::<&[u8], _>(&[]) };
}
19 changes: 17 additions & 2 deletions provider/blob/src/export/blob_exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub struct BlobExporter<'w> {
/// List of (key hash, locale byte string, blob ID)
#[allow(clippy::type_complexity)]
resources: Mutex<Vec<(DataKeyHash, Vec<u8>, usize)>>,
// All seen keys
all_keys: Mutex<Vec<DataKeyHash>>,
/// Map from blob to blob ID
unique_resources: Mutex<HashMap<Vec<u8>, usize>>,
sink: Box<dyn std::io::Write + Sync + 'w>,
Expand All @@ -34,6 +36,7 @@ impl core::fmt::Debug for BlobExporter<'_> {
f.debug_struct("BlobExporter")
.field("resources", &self.resources)
.field("unique_resources", &self.unique_resources)
.field("all_keys", &self.all_keys)
.field("sink", &"<sink>")
.finish()
}
Expand All @@ -45,6 +48,7 @@ impl<'w> BlobExporter<'w> {
Self {
resources: Mutex::new(Vec::new()),
unique_resources: Mutex::new(HashMap::new()),
all_keys: Mutex::new(Vec::new()),
sink,
}
}
Expand Down Expand Up @@ -79,6 +83,11 @@ impl DataExporter for BlobExporter<'_> {
Ok(())
}

fn flush(&self, key: DataKey) -> Result<(), DataError> {
self.all_keys.lock().expect("poison").push(key.hashed());
Ok(())
}

fn close(&mut self) -> Result<(), DataError> {
// The blob IDs are unstable due to the parallel nature of datagen.
// In order to make a canonical form, we sort them lexicographically now.
Expand All @@ -99,7 +108,7 @@ impl DataExporter for BlobExporter<'_> {
.collect();

// Now build up the ZeroMap2d, changing old ID to new ID
let zm = self
let mut zm = self
.resources
.get_mut()
.expect("poison")
Expand All @@ -112,7 +121,13 @@ impl DataExporter for BlobExporter<'_> {
remap.get(old_id).expect("in-bound index"),
)
})
.collect::<ZeroMap2d<_, _, _>>();
.collect::<ZeroMap2d<DataKeyHash, Index32U8, usize>>();

for key in self.all_keys.lock().expect("poison").iter() {
if zm.get0(key).is_none() {
zm.insert(key, Index32U8::SENTINEL, &sorted.len());
}
}

// Convert the sorted list to a VarZeroVec
let vzv: VarZeroVec<[u8], Index32> = {
Expand Down