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

[BUG]: correctly handle replays on local HNSW writer #3936

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
16 changes: 16 additions & 0 deletions chromadb/test/api/test_invalid_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import numpy as np
from chromadb.api import ClientAPI
from chromadb.api.types import IncludeEnum


def test_invalid_update(client: ClientAPI) -> None:
collection = client.create_collection("test")

# Update is invalid because ID does not exist
collection.update(ids=["foo"], embeddings=[[0.0, 0.0, 0.0]])

collection.add(ids=["foo"], embeddings=[[1.0, 1.0, 1.0]])
result = collection.get(ids=["foo"], include=[IncludeEnum.embeddings])
# Embeddings should be the same as what was provided to .add()
assert result["embeddings"] is not None
assert np.allclose(result["embeddings"][0], np.array([1.0, 1.0, 1.0]))
46 changes: 46 additions & 0 deletions rust/segment/src/local_hnsw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,23 @@ impl LocalHnswSegmentReader {
chroma_index::IndexUuid(segment.id.0),
)
.map_err(|_| LocalHnswSegmentReaderError::HnswIndexLoadError)?;

let current_seq_id = {
let (query, values) = Query::select()
.column(MaxSeqId::SeqId)
.from(MaxSeqId::Table)
.and_where(
Expr::col(MaxSeqId::SegmentId).eq(segment.id.to_string()),
)
.build_sqlx(SqliteQueryBuilder);
let row = sqlx::query_with(&query, values)
.fetch_optional(sql_db.get_conn())
.await?;
row.map(|row| row.try_get::<u64, _>(0))
.transpose()?
.unwrap_or_default()
};
Comment on lines +113 to +127
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: wrap this into a method for reuse


// TODO(Sanket): Set allow reset appropriately.
return Ok(Self {
index: LocalHnswIndex {
Expand All @@ -118,6 +135,7 @@ impl LocalHnswSegmentReader {
index_init: true,
allow_reset: false,
num_elements_since_last_persist: 0,
last_seen_seq_id: current_seq_id,
sync_threshold: hnsw_configuration.sync_threshold,
persist_path: Some(index_folder_str.to_string()),
sqlite: sql_db,
Expand Down Expand Up @@ -158,6 +176,7 @@ impl LocalHnswSegmentReader {
index_init: true,
allow_reset: false,
num_elements_since_last_persist: 0,
last_seen_seq_id: 0,
sync_threshold: hnsw_configuration.sync_threshold,
persist_path: None,
sqlite: sql_db,
Expand Down Expand Up @@ -279,6 +298,7 @@ pub struct Inner {
index_init: bool,
allow_reset: bool,
num_elements_since_last_persist: u64,
last_seen_seq_id: u64,
sync_threshold: usize,
persist_path: Option<String>,
sqlite: SqliteDb,
Expand Down Expand Up @@ -425,6 +445,23 @@ impl LocalHnswSegmentWriter {
chroma_index::IndexUuid(segment.id.0),
)
.map_err(|_| LocalHnswSegmentWriterError::HnswIndexLoadError)?;

let current_seq_id = {
let (query, values) = Query::select()
.column(MaxSeqId::SeqId)
.from(MaxSeqId::Table)
.and_where(
Expr::col(MaxSeqId::SegmentId).eq(segment.id.to_string()),
)
.build_sqlx(SqliteQueryBuilder);
let row = sqlx::query_with(&query, values)
.fetch_optional(sql_db.get_conn())
.await?;
row.map(|row| row.try_get::<u64, _>(0))
.transpose()?
.unwrap_or_default()
};

// TODO(Sanket): Set allow reset appropriately.
return Ok(Self {
index: LocalHnswIndex {
Expand All @@ -434,6 +471,7 @@ impl LocalHnswSegmentWriter {
index_init: true,
allow_reset: false,
num_elements_since_last_persist: 0,
last_seen_seq_id: current_seq_id,
sync_threshold: hnsw_configuration.sync_threshold,
persist_path: Some(index_folder_str.to_string()),
sqlite: sql_db,
Expand Down Expand Up @@ -468,6 +506,7 @@ impl LocalHnswSegmentWriter {
index_init: true,
allow_reset: false,
num_elements_since_last_persist: 0,
last_seen_seq_id: 0,
sync_threshold: hnsw_configuration.sync_threshold,
persist_path: Some(index_folder_str.to_string()),
sqlite: sql_db,
Expand Down Expand Up @@ -499,6 +538,7 @@ impl LocalHnswSegmentWriter {
index_init: true,
allow_reset: false,
num_elements_since_last_persist: 0,
last_seen_seq_id: 0,
sync_threshold: hnsw_configuration.sync_threshold,
persist_path: None,
sqlite: sql_db,
Expand All @@ -525,6 +565,10 @@ impl LocalHnswSegmentWriter {
let mut hnsw_batch: HashMap<u32, Vec<(u32, &OperationRecord)>> =
HashMap::with_capacity(log_chunk.len());
for (log, _) in log_chunk.iter() {
if log.log_offset <= guard.last_seen_seq_id as i64 {
continue;
}

guard.num_elements_since_last_persist += 1;
max_seq_id = max_seq_id.max(log.log_offset as u64);
match log.record.operation {
Expand Down Expand Up @@ -688,6 +732,8 @@ impl LocalHnswSegmentWriter {
guard.num_elements_since_last_persist = 0;
}

guard.last_seen_seq_id = max_seq_id;

Ok(next_label)
}
}
Expand Down
Loading