diff --git a/internal/application/repository/retriever/qdrant/repository.go b/internal/application/repository/retriever/qdrant/repository.go index 0754cf4b..5990baa5 100644 --- a/internal/application/repository/retriever/qdrant/repository.go +++ b/internal/application/repository/retriever/qdrant/repository.go @@ -122,8 +122,10 @@ func isMissingCollectionErr(err error) bool { // following write (ensureCollection) will recreate the collection instead of // skipping create and failing the upsert. func (q *qdrantRepository) deletePoints(ctx context.Context, dimension int, collectionName string, points *qdrant.PointsSelector) error { + wait := true _, err := q.client.Delete(ctx, &qdrant.DeletePoints{ CollectionName: collectionName, + Wait: &wait, Points: points, }) if err == nil { @@ -270,8 +272,10 @@ func (q *qdrantRepository) Save(ctx context.Context, Payload: createPayload(embeddingDB), } + wait := true _, err := q.client.Upsert(ctx, &qdrant.UpsertPoints{ CollectionName: collectionName, + Wait: &wait, Points: []*qdrant.PointStruct{point}, }) if err != nil { @@ -337,8 +341,10 @@ func (q *qdrantRepository) BatchSave(ctx context.Context, } batch := points[i:end] + wait := true _, err := q.client.Upsert(ctx, &qdrant.UpsertPoints{ CollectionName: collectionName, + Wait: &wait, Points: batch, }) if err != nil { @@ -504,8 +510,10 @@ func (q *qdrantRepository) BatchUpdateChunkEnabledStatus(ctx context.Context, ch if err := ctx.Err(); err != nil { return errors.Join(updateErr, err) } + wait := true _, err := q.client.SetPayload(ctx, &qdrant.SetPayloadPoints{ CollectionName: collectionName, + Wait: &wait, Payload: newQdrantValueMap(map[string]any{fieldIsEnabled: true}), PointsSelector: qdrant.NewPointsSelectorFilter(&qdrant.Filter{ Must: []*qdrant.Condition{ @@ -524,8 +532,10 @@ func (q *qdrantRepository) BatchUpdateChunkEnabledStatus(ctx context.Context, ch if err := ctx.Err(); err != nil { return errors.Join(updateErr, err) } + wait := true _, err := q.client.SetPayload(ctx, &qdrant.SetPayloadPoints{ CollectionName: collectionName, + Wait: &wait, Payload: newQdrantValueMap(map[string]any{fieldIsEnabled: false}), PointsSelector: qdrant.NewPointsSelectorFilter(&qdrant.Filter{ Must: []*qdrant.Condition{ @@ -589,8 +599,10 @@ func (q *qdrantRepository) BatchUpdateChunkTagID(ctx context.Context, chunkTagMa if err := ctx.Err(); err != nil { return errors.Join(updateErr, err) } + wait := true _, err := q.client.SetPayload(ctx, &qdrant.SetPayloadPoints{ CollectionName: collectionName, + Wait: &wait, Payload: newQdrantValueMap(map[string]any{fieldTagID: tagID}), PointsSelector: qdrant.NewPointsSelectorFilter(&qdrant.Filter{ Must: []*qdrant.Condition{ @@ -1043,8 +1055,10 @@ func (q *qdrantRepository) CopyIndices(ctx context.Context, } if len(targetPoints) > 0 { + wait := true _, err := q.client.Upsert(ctx, &qdrant.UpsertPoints{ CollectionName: collectionName, + Wait: &wait, Points: targetPoints, }) if err != nil { diff --git a/internal/application/repository/retriever/qdrant/repository_test.go b/internal/application/repository/retriever/qdrant/repository_test.go index dcbfa69e..87c5bf54 100644 --- a/internal/application/repository/retriever/qdrant/repository_test.go +++ b/internal/application/repository/retriever/qdrant/repository_test.go @@ -762,3 +762,122 @@ func TestKeywordsRetrieveWarnsWhenNoCollectionMatchesBaseName(t *testing.T) { t.Fatal("a search that never ran must not be logged as a search that found no matches") } } + +func TestPointWritesWaitForApplication(t *testing.T) { + indices := make([]*types.IndexInfo, 101) + embeddings := make(map[string][]float32, len(indices)) + for i := range indices { + id := fmt.Sprintf("chunk-%d", i) + indices[i] = &types.IndexInfo{SourceID: id, ChunkID: id, IsEnabled: true} + embeddings[id] = []float32{0.1, 0.2, 0.3, 0.4} + } + params := map[string]any{fieldEmbedding: embeddings} + chunkMap := make(map[string]string, 65) + for i := range 65 { + chunkMap[fmt.Sprintf("chunk-%d", i)] = fmt.Sprintf("target-%d", i) + } + operations := []struct { + name string + writes int + run func(context.Context, *qdrantRepository) error + }{ + {"save", 1, func(ctx context.Context, repo *qdrantRepository) error { + return repo.Save(ctx, indices[0], params) + }}, + {"batch save", 2, func(ctx context.Context, repo *qdrantRepository) error { + return repo.BatchSave(ctx, indices, params) + }}, + {"copy", 2, func(ctx context.Context, repo *qdrantRepository) error { + return repo.CopyIndices(ctx, "kb-src", map[string]string{"know-1": "know-2"}, + chunkMap, "kb-dst", 4, types.KnowledgeBaseTypeDocument) + }}, + {"enable and disable", 2, func(ctx context.Context, repo *qdrantRepository) error { + return repo.BatchUpdateChunkEnabledStatus(ctx, map[string]bool{"on": true, "off": false}) + }}, + {"tags", 2, func(ctx context.Context, repo *qdrantRepository) error { + return repo.BatchUpdateChunkTagID(ctx, map[string]string{"one": "tag-a", "two": "tag-b"}) + }}, + {"move", 1, func(ctx context.Context, repo *qdrantRepository) error { + return repo.MoveKnowledgeIndices(ctx, "kb-src", "kb-dst", "know-1", nil, 4, "doc") + }}, + } + for _, operation := range deleteOperations() { + operations = append(operations, struct { + name string + writes int + run func(context.Context, *qdrantRepository) error + }{operation.name, 1, operation.run}) + } + for _, operation := range operations { + t.Run(operation.name, func(t *testing.T) { + for _, outcome := range []struct { + name string + err error + }{ + {"completed", nil}, + {"application failure", status.Error(codes.Internal, "failed to apply write")}, + {"timeout", context.DeadlineExceeded}, + } { + t.Run(outcome.name, func(t *testing.T) { + server := ©TestServer{} + for i := range 65 { + server.source = append(server.source, copyTestSourcePoint(i)) + } + applied, acknowledged := 0, 0 + client := newInterceptedQdrantClient(t, func( + _ context.Context, method string, req, reply any, _ *grpc.ClientConn, + _ grpc.UnaryInvoker, _ ...grpc.CallOption, + ) error { + var wait bool + switch request := req.(type) { + case *qdrant.ListCollectionsRequest: + reply.(*qdrant.ListCollectionsResponse).Collections = []*qdrant.CollectionDescription{ + {Name: "vectors_4"}, + } + return nil + case *qdrant.ScrollPoints: + response := reply.(*qdrant.ScrollResponse) + response.Result, response.NextPageOffset = server.page(request) + return nil + case *qdrant.UpsertPoints: + wait = request.GetWait() + case *qdrant.DeletePoints: + wait = request.GetWait() + case *qdrant.SetPayloadPoints: + wait = request.GetWait() + default: + return fmt.Errorf("unexpected RPC %s", method) + } + // An asynchronous write can be acknowledged before it is applied, + // even if application will fail. Only waiting exposes that failure. + response := reply.(*qdrant.PointsOperationResponse) + if !wait { + acknowledged++ + response.Result = &qdrant.UpdateResult{Status: qdrant.UpdateStatus_Acknowledged} + return nil + } + if outcome.err != nil { + return outcome.err + } + applied++ + response.Result = &qdrant.UpdateResult{Status: qdrant.UpdateStatus_Completed} + return nil + }) + repo := &qdrantRepository{client: client, collectionBaseName: "vectors"} + repo.initializedCollections.Store(4, true) + repo.initializedCollections.Store(1024, true) + err := operation.run(context.Background(), repo) + if !errors.Is(err, outcome.err) { + t.Fatalf("got error %v, want %v", err, outcome.err) + } + if acknowledged != 0 { + t.Fatalf("returned with %d writes acknowledged but not applied", acknowledged) + } + if outcome.err == nil && applied != operation.writes { + t.Fatalf("applied %d writes before return, want %d", applied, operation.writes) + } + }) + } + }) + } +}