Skip to content

Commit

Permalink
MB-60274: Set an upper bound for K value for KNN queries (#1953)
Browse files Browse the repository at this point in the history
# JIRA

https://issues.couchbase.com/browse/MB-60274

# Description

Currently, the value of K in KNN queries is only lower-bounded to 1
without an upper limit. This lack of an upper boundary leads to issues
where users setting K to extremely high values, such as 999999999, cause
a crash due to excessive memory allocation exceeding 8GB.

To address this issue, this commit introduces an upper bound check for
variable K. Setting an upper limit of 10000 for K prevents excessive
memory demands, capping the maximum memory requirement at a reasonable
120KB. This change ensures system stability by preventing memory
overflow issues caused by excessively high K values."
  • Loading branch information
CascadingRadium authored Jan 5, 2024
1 parent 2ac30d6 commit 80aa03f
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions search_knn.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import (

type knnOperator string

const MaxKValue = 10000

type SearchRequest struct {
Query query.Query `json:"query"`
Size int `json:"size"`
Expand Down Expand Up @@ -230,6 +232,9 @@ func validateKNN(req *SearchRequest) error {
if q.K <= 0 || len(q.Vector) == 0 {
return fmt.Errorf("k must be greater than 0 and vector must be non-empty")
}
if q.K > MaxKValue {
return fmt.Errorf("k must be less than %d", MaxKValue)
}
}
switch req.KNNOperator {
case knnOperatorAnd, knnOperatorOr, "":
Expand Down

0 comments on commit 80aa03f

Please sign in to comment.