Repository navigation
Fix point deduplication - #5585
Merged
Merged
Conversation
timvisee
commented
Dec 4, 2024
| } else { | ||
| last_point_id_opt = Some(point_id); | ||
| last_segment_id_opt = Some(segment_id); | ||
| last_point_version_opt = None; |
Member
Author
There was a problem hiding this comment.
The fix: properly clear point version when moving to the next point ID.
Thanks @generall for catching this one!
Comment on lines
+1691
to
+1770
| /// Unit test for a specific bug we caught before. | ||
| /// | ||
| /// See: <https://github.com/qdrant/qdrant/pull/5585> | ||
| #[tokio::test] | ||
| async fn test_points_deduplication_bug() { | ||
| let dir = Builder::new().prefix("segment_dir").tempdir().unwrap(); | ||
|
|
||
| let mut segment1 = empty_segment(dir.path()); | ||
| let mut segment2 = empty_segment(dir.path()); | ||
|
|
||
| segment1 | ||
| .upsert_point( | ||
| 2, | ||
| 10.into(), | ||
| segment::data_types::vectors::only_default_vector(&[0.0; 4]), | ||
| ) | ||
| .unwrap(); | ||
| segment2 | ||
| .upsert_point( | ||
| 3, | ||
| 10.into(), | ||
| segment::data_types::vectors::only_default_vector(&[0.0; 4]), | ||
| ) | ||
| .unwrap(); | ||
|
|
||
| segment1 | ||
| .upsert_point( | ||
| 1, | ||
| 11.into(), | ||
| segment::data_types::vectors::only_default_vector(&[0.0; 4]), | ||
| ) | ||
| .unwrap(); | ||
| segment2 | ||
| .upsert_point( | ||
| 2, | ||
| 11.into(), | ||
| segment::data_types::vectors::only_default_vector(&[0.0; 4]), | ||
| ) | ||
| .unwrap(); | ||
|
|
||
| let mut holder = SegmentHolder::default(); | ||
|
|
||
| let sid1 = holder.add_new(segment1); | ||
| let sid2 = holder.add_new(segment2); | ||
|
|
||
| let duplicate_count = holder | ||
| .find_duplicated_points() | ||
| .values() | ||
| .map(|ids| ids.len()) | ||
| .sum::<usize>(); | ||
| assert_eq!(2, duplicate_count); | ||
|
|
||
| let removed_count = holder.deduplicate_points().await.unwrap(); | ||
| assert_eq!(2, removed_count); | ||
|
|
||
| assert!(!holder.get(sid1).unwrap().get().read().has_point(10.into())); | ||
| assert!(holder.get(sid2).unwrap().get().read().has_point(10.into())); | ||
|
|
||
| assert!(!holder.get(sid1).unwrap().get().read().has_point(11.into())); | ||
| assert!(holder.get(sid2).unwrap().get().read().has_point(11.into())); | ||
|
|
||
| assert_eq!( | ||
| holder | ||
| .get(sid1) | ||
| .unwrap() | ||
| .get() | ||
| .read() | ||
| .available_point_count(), | ||
| 0, | ||
| ); | ||
| assert_eq!( | ||
| holder | ||
| .get(sid2) | ||
| .unwrap() | ||
| .get() | ||
| .read() | ||
| .available_point_count(), | ||
| 2, | ||
| ); | ||
| } |
Member
Author
There was a problem hiding this comment.
Test covering the bug we fixed.
Ordering is important here. The second point must have lower point versions. This made it hard to catch.
Comment on lines
+1772
to
+1851
| #[tokio::test] | ||
| async fn test_points_deduplication_randomized() { | ||
| const POINT_COUNT: usize = 1000; | ||
|
|
||
| let mut rand = rand::thread_rng(); | ||
| let dir = Builder::new().prefix("segment_dir").tempdir().unwrap(); | ||
| let vector = segment::data_types::vectors::only_default_vector(&[0.0; 4]); | ||
|
|
||
| let mut segments = [ | ||
| empty_segment(dir.path()), | ||
| empty_segment(dir.path()), | ||
| empty_segment(dir.path()), | ||
| empty_segment(dir.path()), | ||
| empty_segment(dir.path()), | ||
| ]; | ||
|
|
||
| // Insert points into all segments with random versions | ||
| let mut point_versions = HashMap::new(); | ||
| for id in 0..POINT_COUNT { | ||
| let mut versions = Vec::with_capacity(segments.len()); | ||
| let point_id = PointIdType::from(id as u64); | ||
|
|
||
| for segment in &mut segments { | ||
| let version = rand.gen_range(1..10); | ||
| segment | ||
| .upsert_point(version, point_id, vector.clone()) | ||
| .unwrap(); | ||
| versions.push(version); | ||
| } | ||
|
|
||
| point_versions.insert(id, versions); | ||
| } | ||
|
|
||
| // Put segments into holder | ||
| let mut holder = SegmentHolder::default(); | ||
| let segment_ids = segments | ||
| .into_iter() | ||
| .map(|segment| holder.add_new(segment)) | ||
| .collect::<Vec<_>>(); | ||
|
|
||
| let duplicate_count = holder | ||
| .find_duplicated_points() | ||
| .values() | ||
| .map(|ids| ids.len()) | ||
| .sum::<usize>(); | ||
| assert_eq!(POINT_COUNT * (segment_ids.len() - 1), duplicate_count); | ||
|
|
||
| let removed_count = holder.deduplicate_points().await.unwrap(); | ||
| assert_eq!(POINT_COUNT * (segment_ids.len() - 1), removed_count); | ||
|
|
||
| // Assert points after deduplication | ||
| for id in 0..POINT_COUNT { | ||
| let versions = &point_versions[&id]; | ||
| let max = *versions.iter().max().unwrap(); | ||
| let point_id = PointIdType::from(id as u64); | ||
|
|
||
| let found_versions = segment_ids | ||
| .iter() | ||
| .filter_map(|segment_id| { | ||
| holder | ||
| .get(*segment_id) | ||
| .unwrap() | ||
| .get() | ||
| .read() | ||
| .point_version(point_id) | ||
| }) | ||
| .collect::<Vec<_>>(); | ||
|
|
||
| // We must have exactly one version, and it must be the highest we inserted | ||
| assert_eq!( | ||
| found_versions.len(), | ||
| 1, | ||
| "point version must be maximum known version", | ||
| ); | ||
| assert_eq!( | ||
| found_versions[0], max, | ||
| "point version must be maximum known version", | ||
| ); | ||
| } | ||
| } |
Member
Author
There was a problem hiding this comment.
Test using a thousand random points to assert correct behavior.
timvisee
marked this pull request as ready for review
December 4, 2024 13:37
agourlay
approved these changes
Dec 4, 2024
agourlay
left a comment
Member
There was a problem hiding this comment.
Thank you very much for the tests 🙏
generall
approved these changes
Dec 4, 2024
IvanPleshkov
approved these changes
Dec 4, 2024
This was referenced Dec 5, 2024
timvisee
added a commit
that referenced
this pull request
Dec 9, 2024
* Add unit test for point deduplication bug * Fix point deduplication bug, versions were reused acrosos point IDs * Add unit test with randomized points and versions * Remove obsolete comment * Add link to pull request * Simplify test a bit
7 of 8 tasks
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fix a bug in point deduplication that caused mishandling of point versions.
The 'last point version' was not properly cleared when moving to the next point ID, meaning irrelevant point versions from the previous point leaked into the current one. Because of this it was possible we deleted newer point versions rather than old ones.
I've added two unit tests:
All Submissions:
devbranch. Did you create your branch fromdev?New Feature Submissions:
cargo +nightly fmt --allcommand prior to submission?cargo clippy --all --all-featurescommand?