Repository: milvus-io/milvus
File: internal/querynode/search.go (Go)

Code before:
package querynode

import (
	"fmt"
	"sync"

	"github.com/milvus-io/milvus/internal/metrics"
	"github.com/milvus-io/milvus/internal/util/timerecord"
)

// searchOnSegments performs search on listed segments
// all segment ids are validated before calling this function
func searchOnSegments(replica ReplicaInterface, segType segmentType, searchReq *searchRequest, segIDs []UniqueID) ([]*SearchResult, error) {
	// results variables
	searchResults := make([]*SearchResult, len(segIDs))
	errs := make([]error, len(segIDs))

	// calling segment search in goroutines
	var wg sync.WaitGroup
	for i, segID := range segIDs {
		wg.Add(1)
		go func(segID UniqueID, i int) {
			defer wg.Done()
			seg, err := replica.getSegmentByID(segID, segType)
			if err != nil {
				return
			}
			// record search time
			tr := timerecord.NewTimeRecorder("searchOnSegments")
			searchResult, err := seg.search(searchReq)
			errs[i] = err
			searchResults[i] = searchResult
			// update metrics
			metrics.QueryNodeSQSegmentLatency.WithLabelValues(fmt.Sprint(Params.QueryNodeCfg.GetNodeID()),
				metrics.SearchLabel,
				metrics.SealedSegmentLabel).Observe(float64(tr.ElapseSpan().Milliseconds()))
		}(segID, i)
	}
	wg.Wait()
	for _, err := range errs {
		if err != nil {
			deleteSearchResults(searchResults)
			return nil, err
		}
	}
	return searchResults, nil
}

// search will search on the historical segments the target segments in historical.
// if segIDs is not specified, it will search on all the historical segments speficied by partIDs.
// if segIDs is specified, it will only search on the segments specified by the segIDs.

Review comment (suggestion):
If segment is released safely after listed by `validateOnStreamingReplica`, this error shall be ignored since the result is still correct.

Code after:
package querynode

import (
	"fmt"
	"sync"

	"github.com/milvus-io/milvus/internal/log"
	"github.com/milvus-io/milvus/internal/metrics"
	"github.com/milvus-io/milvus/internal/util/timerecord"
)

// searchOnSegments performs search on listed segments
// all segment ids are validated before calling this function
func searchOnSegments(replica ReplicaInterface, segType segmentType, searchReq *searchRequest, segIDs []UniqueID) ([]*SearchResult, error) {
	// results variables
	searchResults := make([]*SearchResult, len(segIDs))
	errs := make([]error, len(segIDs))

	// calling segment search in goroutines
	var wg sync.WaitGroup
	for i, segID := range segIDs {
		wg.Add(1)
		go func(segID UniqueID, i int) {
			defer wg.Done()
			seg, err := replica.getSegmentByID(segID, segType)
			if err != nil {
				log.Error(err.Error()) // should not happen but still ignore it since the result is still correct
				return
			}
			// record search time
			tr := timerecord.NewTimeRecorder("searchOnSegments")
			searchResult, err := seg.search(searchReq)
			errs[i] = err
			searchResults[i] = searchResult
			// update metrics
			metrics.QueryNodeSQSegmentLatency.WithLabelValues(fmt.Sprint(Params.QueryNodeCfg.GetNodeID()),
				metrics.SearchLabel,
				metrics.SealedSegmentLabel).Observe(float64(tr.ElapseSpan().Milliseconds()))
		}(segID, i)
	}
	wg.Wait()
	for _, err := range errs {
		if err != nil {
			deleteSearchResults(searchResults)
			return nil, err
		}
	}
	return searchResults, nil
}

// search will search on the historical segments the target segments in historical.