Skip to content

Commit

Permalink
MB-63334: Fix race condition in NormalizeVector (#2094)
Browse files Browse the repository at this point in the history
- Normalizing the query vector in place would create a race condition,
causing different
  indexes within an alias to receive inconsistent query vectors. 
- This inconsistency could lead to incorrect results when merging hits
from all indexes in the
  alias.
  • Loading branch information
CascadingRadium authored Nov 4, 2024
1 parent bed244c commit cb1810f
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions mapping/mapping_vectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import (
"fmt"
"reflect"

faiss "github.com/blevesearch/go-faiss"
"github.com/blevesearch/bleve/v2/document"
"github.com/blevesearch/bleve/v2/util"
index "github.com/blevesearch/bleve_index_api"
faiss "github.com/blevesearch/go-faiss"
)

// Min and Max allowed dimensions for a vector field;
Expand Down Expand Up @@ -263,5 +263,10 @@ func validateVectorFieldAlias(field *FieldMapping, parentName string,
}

func NormalizeVector(vec []float32) []float32 {
return faiss.NormalizeVector(vec)
// make a copy of the vector to avoid modifying the original
// vector in-place
vecCopy := make([]float32, len(vec))
copy(vecCopy, vec)
// normalize the vector copy using in-place normalization provided by faiss
return faiss.NormalizeVector(vecCopy)
}

0 comments on commit cb1810f

Please sign in to comment.