Skip to content

Fix point deduplication - #5585

Merged
timvisee merged 6 commits into
devfrom
fix-point-deduplication
Dec 4, 2024
Merged

timvisee merged 6 commits into
devfrom
fix-point-deduplication

Conversation

@timvisee

@timvisee timvisee commented Dec 4, 2024

Copy link
Copy Markdown
Member

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:

  • the first tests this specific problem
  • the second inserts random points into segments and asserts we always keep the highest point version

All Submissions:

  • Contributions should target the dev branch. Did you create your branch from dev?
  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't other open Pull Requests for the same update/change?

New Feature Submissions:

  1. Does your submission pass tests?
  2. Have you formatted your code locally using cargo +nightly fmt --all command prior to submission?
  3. Have you checked your code using cargo clippy --all --all-features command?

} else {
last_point_id_opt = Some(point_id);
last_segment_id_opt = Some(segment_id);
last_point_version_opt = None;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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,
);
}

@timvisee timvisee Dec 4, 2024 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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",
);
}
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Test using a thousand random points to assert correct behavior.

@timvisee
timvisee marked this pull request as ready for review December 4, 2024 13:37

@agourlay agourlay left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you very much for the tests 🙏

@timvisee
timvisee merged commit ba5822d into dev Dec 4, 2024
@timvisee
timvisee deleted the fix-point-deduplication branch December 4, 2024 15:32
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
@timvisee timvisee mentioned this pull request Dec 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants