2021-01-16 10:12:14 +08:00
|
|
|
package querynode
|
2020-11-26 16:01:31 +08:00
|
|
|
|
|
|
|
import "C"
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2021-03-22 16:36:10 +08:00
|
|
|
"fmt"
|
|
|
|
"go.uber.org/zap"
|
2021-01-18 19:32:08 +08:00
|
|
|
"strconv"
|
2021-03-05 18:16:50 +08:00
|
|
|
"strings"
|
2020-11-26 16:01:31 +08:00
|
|
|
"sync"
|
|
|
|
|
2021-03-05 09:21:35 +08:00
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/log"
|
2020-11-26 16:01:31 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/msgstream"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/commonpb"
|
2021-03-12 14:22:09 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/internalpb"
|
2021-01-22 09:36:18 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/milvuspb"
|
2020-11-26 16:01:31 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type searchService struct {
|
|
|
|
ctx context.Context
|
|
|
|
wait sync.WaitGroup
|
|
|
|
cancel context.CancelFunc
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
replica ReplicaInterface
|
2021-03-22 16:36:10 +08:00
|
|
|
tSafeMutex *sync.Mutex
|
|
|
|
tSafeWatcher map[UniqueID]*tSafeWatcher
|
2020-11-26 16:01:31 +08:00
|
|
|
|
2021-01-13 10:40:46 +08:00
|
|
|
serviceableTimeMutex sync.Mutex // guards serviceableTime
|
2021-03-22 16:36:10 +08:00
|
|
|
serviceableTime map[UniqueID]Timestamp
|
2020-11-26 16:01:31 +08:00
|
|
|
|
2021-03-22 16:36:10 +08:00
|
|
|
msgBuffer chan *msgstream.SearchMsg
|
|
|
|
unsolvedMsg []*msgstream.SearchMsg
|
2020-12-08 14:41:04 +08:00
|
|
|
searchMsgStream msgstream.MsgStream
|
|
|
|
searchResultMsgStream msgstream.MsgStream
|
|
|
|
queryNodeID UniqueID
|
2020-11-26 16:01:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type ResultEntityIds []UniqueID
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
func newSearchService(ctx context.Context, replica ReplicaInterface, factory msgstream.Factory) *searchService {
|
2020-12-10 16:31:09 +08:00
|
|
|
receiveBufSize := Params.SearchReceiveBufSize
|
2021-02-03 17:30:10 +08:00
|
|
|
|
2021-03-19 20:16:04 +08:00
|
|
|
searchStream, _ := factory.NewQueryMsgStream(ctx)
|
|
|
|
searchResultStream, _ := factory.NewQueryMsgStream(ctx)
|
2021-02-09 17:09:26 +08:00
|
|
|
|
|
|
|
// query node doesn't need to consumer any search or search result channel actively.
|
2021-02-18 16:26:02 +08:00
|
|
|
consumeChannels := Params.SearchChannelNames
|
|
|
|
consumeSubName := Params.MsgChannelSubName
|
|
|
|
searchStream.AsConsumer(consumeChannels, consumeSubName)
|
2021-03-05 18:16:50 +08:00
|
|
|
log.Debug("querynode AsConsumer: " + strings.Join(consumeChannels, ", ") + " : " + consumeSubName)
|
2021-02-18 16:26:02 +08:00
|
|
|
producerChannels := Params.SearchResultChannelNames
|
|
|
|
searchResultStream.AsProducer(producerChannels)
|
2021-03-05 18:16:50 +08:00
|
|
|
log.Debug("querynode AsProducer: " + strings.Join(producerChannels, ", "))
|
2020-11-26 16:01:31 +08:00
|
|
|
|
|
|
|
searchServiceCtx, searchServiceCancel := context.WithCancel(ctx)
|
2021-03-22 16:36:10 +08:00
|
|
|
msgBuffer := make(chan *msgstream.SearchMsg, receiveBufSize)
|
|
|
|
unsolvedMsg := make([]*msgstream.SearchMsg, 0)
|
2020-11-26 16:01:31 +08:00
|
|
|
return &searchService{
|
|
|
|
ctx: searchServiceCtx,
|
|
|
|
cancel: searchServiceCancel,
|
2021-03-22 16:36:10 +08:00
|
|
|
serviceableTime: make(map[UniqueID]Timestamp),
|
2020-11-26 16:01:31 +08:00
|
|
|
msgBuffer: msgBuffer,
|
|
|
|
unsolvedMsg: unsolvedMsg,
|
|
|
|
|
|
|
|
replica: replica,
|
2021-03-22 16:36:10 +08:00
|
|
|
tSafeMutex: &sync.Mutex{},
|
|
|
|
tSafeWatcher: make(map[UniqueID]*tSafeWatcher),
|
2020-11-26 16:01:31 +08:00
|
|
|
|
2021-02-09 17:09:26 +08:00
|
|
|
searchMsgStream: searchStream,
|
|
|
|
searchResultMsgStream: searchResultStream,
|
2020-12-10 16:31:09 +08:00
|
|
|
queryNodeID: Params.QueryNodeID,
|
2020-11-26 16:01:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ss *searchService) start() {
|
2020-12-08 14:41:04 +08:00
|
|
|
ss.searchMsgStream.Start()
|
|
|
|
ss.searchResultMsgStream.Start()
|
2020-11-26 16:01:31 +08:00
|
|
|
ss.wait.Add(2)
|
|
|
|
go ss.receiveSearchMsg()
|
|
|
|
go ss.doUnsolvedMsgSearch()
|
|
|
|
ss.wait.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ss *searchService) close() {
|
2020-12-08 14:41:04 +08:00
|
|
|
if ss.searchMsgStream != nil {
|
|
|
|
ss.searchMsgStream.Close()
|
|
|
|
}
|
|
|
|
if ss.searchResultMsgStream != nil {
|
|
|
|
ss.searchResultMsgStream.Close()
|
|
|
|
}
|
2020-11-26 16:01:31 +08:00
|
|
|
ss.cancel()
|
|
|
|
}
|
|
|
|
|
2021-03-22 16:36:10 +08:00
|
|
|
func (ss *searchService) register(collectionID UniqueID) {
|
|
|
|
tSafe := ss.replica.getTSafe(collectionID)
|
|
|
|
ss.tSafeMutex.Lock()
|
|
|
|
ss.tSafeWatcher[collectionID] = newTSafeWatcher()
|
|
|
|
ss.tSafeMutex.Unlock()
|
|
|
|
tSafe.registerTSafeWatcher(ss.tSafeWatcher[collectionID])
|
2020-11-26 16:01:31 +08:00
|
|
|
}
|
|
|
|
|
2021-03-22 16:36:10 +08:00
|
|
|
func (ss *searchService) waitNewTSafe(collectionID UniqueID) (Timestamp, error) {
|
2020-11-26 16:01:31 +08:00
|
|
|
// block until dataSyncService updating tSafe
|
2021-03-22 16:36:10 +08:00
|
|
|
ss.tSafeWatcher[collectionID].hasUpdate()
|
|
|
|
ts := ss.replica.getTSafe(collectionID)
|
|
|
|
if ts != nil {
|
|
|
|
return ts.get(), nil
|
|
|
|
}
|
|
|
|
return 0, errors.New("tSafe closed, collectionID =" + fmt.Sprintln(collectionID))
|
2020-11-26 16:01:31 +08:00
|
|
|
}
|
|
|
|
|
2021-03-22 16:36:10 +08:00
|
|
|
func (ss *searchService) getServiceableTime(collectionID UniqueID) Timestamp {
|
2020-11-26 16:01:31 +08:00
|
|
|
ss.serviceableTimeMutex.Lock()
|
|
|
|
defer ss.serviceableTimeMutex.Unlock()
|
2021-03-22 16:36:10 +08:00
|
|
|
//t, ok := ss.serviceableTime[collectionID]
|
|
|
|
//if !ok {
|
|
|
|
// return 0, errors.New("cannot found")
|
|
|
|
//}
|
|
|
|
return ss.serviceableTime[collectionID]
|
2020-11-26 16:01:31 +08:00
|
|
|
}
|
|
|
|
|
2021-03-22 16:36:10 +08:00
|
|
|
func (ss *searchService) setServiceableTime(collectionID UniqueID, t Timestamp) {
|
2020-11-26 16:01:31 +08:00
|
|
|
ss.serviceableTimeMutex.Lock()
|
2021-03-13 14:42:53 +08:00
|
|
|
// hard code gracefultime to 1 second
|
|
|
|
// TODO: use config to set gracefultime
|
2021-03-22 16:36:10 +08:00
|
|
|
ss.serviceableTime[collectionID] = t + 1000*1000*1000
|
2020-11-26 16:01:31 +08:00
|
|
|
ss.serviceableTimeMutex.Unlock()
|
|
|
|
}
|
|
|
|
|
2021-03-22 16:36:10 +08:00
|
|
|
func (ss *searchService) collectionCheck(collectionID UniqueID) error {
|
|
|
|
// check if collection exists
|
|
|
|
if _, ok := ss.tSafeWatcher[collectionID]; !ok {
|
|
|
|
err := errors.New("no collection found, collectionID = " + strconv.FormatInt(collectionID, 10))
|
|
|
|
log.Error(err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ss *searchService) emptySearch(searchMsg *msgstream.SearchMsg) {
|
|
|
|
err := ss.search(searchMsg)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err.Error())
|
|
|
|
err2 := ss.publishFailedSearchResult(searchMsg, err.Error())
|
|
|
|
if err2 != nil {
|
|
|
|
log.Error("publish FailedSearchResult failed", zap.Error(err2))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-26 16:01:31 +08:00
|
|
|
func (ss *searchService) receiveSearchMsg() {
|
|
|
|
defer ss.wait.Done()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ss.ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
2021-02-25 17:35:36 +08:00
|
|
|
msgPack, _ := ss.searchMsgStream.Consume()
|
2020-11-26 16:01:31 +08:00
|
|
|
if msgPack == nil || len(msgPack.Msgs) <= 0 {
|
|
|
|
continue
|
|
|
|
}
|
2021-03-22 16:36:10 +08:00
|
|
|
searchNum := 0
|
|
|
|
for _, msg := range msgPack.Msgs {
|
|
|
|
sm, ok := msg.(*msgstream.SearchMsg)
|
|
|
|
if !ok {
|
2020-11-26 16:01:31 +08:00
|
|
|
continue
|
|
|
|
}
|
2021-03-22 16:36:10 +08:00
|
|
|
err := ss.collectionCheck(sm.CollectionID)
|
|
|
|
if err != nil {
|
|
|
|
ss.emptySearch(sm)
|
|
|
|
searchNum++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
serviceTime := ss.getServiceableTime(sm.CollectionID)
|
|
|
|
if msg.BeginTs() > serviceTime {
|
|
|
|
ss.msgBuffer <- sm
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err = ss.search(sm)
|
2020-11-26 16:01:31 +08:00
|
|
|
if err != nil {
|
2021-03-05 09:21:35 +08:00
|
|
|
log.Error(err.Error())
|
2021-03-22 16:36:10 +08:00
|
|
|
err2 := ss.publishFailedSearchResult(sm, err.Error())
|
2020-12-11 11:38:32 +08:00
|
|
|
if err2 != nil {
|
2021-03-05 09:21:35 +08:00
|
|
|
log.Error("publish FailedSearchResult failed", zap.Error(err2))
|
2020-11-30 11:22:59 +08:00
|
|
|
}
|
2020-11-26 16:01:31 +08:00
|
|
|
}
|
2021-03-22 16:36:10 +08:00
|
|
|
searchNum++
|
2020-11-26 16:01:31 +08:00
|
|
|
}
|
2021-03-22 16:36:10 +08:00
|
|
|
log.Debug("ReceiveSearchMsg, do search done", zap.Int("num of searchMsg", searchNum))
|
2020-11-26 16:01:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ss *searchService) doUnsolvedMsgSearch() {
|
|
|
|
defer ss.wait.Done()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ss.ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
2021-03-22 16:36:10 +08:00
|
|
|
searchMsg := make([]*msgstream.SearchMsg, 0)
|
|
|
|
tempMsg := make([]*msgstream.SearchMsg, 0)
|
2020-11-26 16:01:31 +08:00
|
|
|
tempMsg = append(tempMsg, ss.unsolvedMsg...)
|
|
|
|
ss.unsolvedMsg = ss.unsolvedMsg[:0]
|
2021-03-22 16:36:10 +08:00
|
|
|
|
|
|
|
serviceTimeTmpTable := make(map[UniqueID]Timestamp)
|
|
|
|
|
|
|
|
searchNum := 0
|
|
|
|
for _, sm := range tempMsg {
|
|
|
|
err := ss.collectionCheck(sm.CollectionID)
|
|
|
|
if err != nil {
|
|
|
|
ss.emptySearch(sm)
|
|
|
|
searchNum++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
_, ok := serviceTimeTmpTable[sm.CollectionID]
|
|
|
|
if !ok {
|
|
|
|
serviceTime, err := ss.waitNewTSafe(sm.CollectionID)
|
|
|
|
if err != nil {
|
|
|
|
// TODO: emptySearch or continue, note: collection has been released
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ss.setServiceableTime(sm.CollectionID, serviceTime)
|
|
|
|
serviceTimeTmpTable[sm.CollectionID] = serviceTime
|
|
|
|
}
|
|
|
|
if sm.EndTs() <= serviceTimeTmpTable[sm.CollectionID] {
|
|
|
|
searchMsg = append(searchMsg, sm)
|
2020-11-26 16:01:31 +08:00
|
|
|
continue
|
|
|
|
}
|
2021-03-22 16:36:10 +08:00
|
|
|
ss.unsolvedMsg = append(ss.unsolvedMsg, sm)
|
2020-11-26 16:01:31 +08:00
|
|
|
}
|
|
|
|
|
2020-11-26 17:58:08 +08:00
|
|
|
for {
|
2020-11-28 19:06:48 +08:00
|
|
|
msgBufferLength := len(ss.msgBuffer)
|
|
|
|
if msgBufferLength <= 0 {
|
|
|
|
break
|
|
|
|
}
|
2021-03-22 16:36:10 +08:00
|
|
|
sm := <-ss.msgBuffer
|
|
|
|
err := ss.collectionCheck(sm.CollectionID)
|
|
|
|
if err != nil {
|
|
|
|
ss.emptySearch(sm)
|
|
|
|
searchNum++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
_, ok := serviceTimeTmpTable[sm.CollectionID]
|
|
|
|
if !ok {
|
|
|
|
serviceTime, err := ss.waitNewTSafe(sm.CollectionID)
|
|
|
|
if err != nil {
|
|
|
|
// TODO: emptySearch or continue, note: collection has been released
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ss.setServiceableTime(sm.CollectionID, serviceTime)
|
|
|
|
serviceTimeTmpTable[sm.CollectionID] = serviceTime
|
|
|
|
}
|
|
|
|
if sm.EndTs() <= serviceTimeTmpTable[sm.CollectionID] {
|
|
|
|
searchMsg = append(searchMsg, sm)
|
2020-11-26 16:01:31 +08:00
|
|
|
continue
|
|
|
|
}
|
2021-03-22 16:36:10 +08:00
|
|
|
ss.unsolvedMsg = append(ss.unsolvedMsg, sm)
|
2020-11-26 16:01:31 +08:00
|
|
|
}
|
2020-11-26 17:58:08 +08:00
|
|
|
|
2020-11-26 16:01:31 +08:00
|
|
|
if len(searchMsg) <= 0 {
|
|
|
|
continue
|
|
|
|
}
|
2021-03-22 16:36:10 +08:00
|
|
|
for _, sm := range searchMsg {
|
|
|
|
err := ss.search(sm)
|
2020-11-26 16:01:31 +08:00
|
|
|
if err != nil {
|
2021-03-05 09:21:35 +08:00
|
|
|
log.Error(err.Error())
|
2021-03-22 16:36:10 +08:00
|
|
|
err2 := ss.publishFailedSearchResult(sm, err.Error())
|
2020-12-11 11:38:32 +08:00
|
|
|
if err2 != nil {
|
2021-03-05 09:21:35 +08:00
|
|
|
log.Error("publish FailedSearchResult failed", zap.Error(err2))
|
2020-11-30 11:22:59 +08:00
|
|
|
}
|
2020-11-26 16:01:31 +08:00
|
|
|
}
|
2021-03-22 16:36:10 +08:00
|
|
|
searchNum++
|
2020-11-26 16:01:31 +08:00
|
|
|
}
|
2021-03-22 16:36:10 +08:00
|
|
|
log.Debug("doUnsolvedMsgSearch, do search done", zap.Int("num of searchMsg", searchNum))
|
2020-11-26 16:01:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO:: cache map[dsl]plan
|
|
|
|
// TODO: reBatched search requests
|
2021-03-22 16:36:10 +08:00
|
|
|
func (ss *searchService) search(searchMsg *msgstream.SearchMsg) error {
|
2021-01-18 19:32:08 +08:00
|
|
|
searchTimestamp := searchMsg.Base.Timestamp
|
2020-11-26 16:01:31 +08:00
|
|
|
var queryBlob = searchMsg.Query.Value
|
2021-01-22 09:36:18 +08:00
|
|
|
query := milvuspb.SearchRequest{}
|
2020-11-26 16:01:31 +08:00
|
|
|
err := proto.Unmarshal(queryBlob, &query)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("unmarshal query failed")
|
|
|
|
}
|
2021-02-03 11:52:19 +08:00
|
|
|
collectionID := searchMsg.CollectionID
|
|
|
|
collection, err := ss.replica.getCollectionByID(collectionID)
|
2020-11-26 16:01:31 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
dsl := query.Dsl
|
2020-11-30 17:58:23 +08:00
|
|
|
plan, err := createPlan(*collection, dsl)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-26 16:01:31 +08:00
|
|
|
placeHolderGroupBlob := query.PlaceholderGroup
|
2020-11-30 17:58:23 +08:00
|
|
|
placeholderGroup, err := parserPlaceholderGroup(plan, placeHolderGroupBlob)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-26 16:01:31 +08:00
|
|
|
placeholderGroups := make([]*PlaceholderGroup, 0)
|
|
|
|
placeholderGroups = append(placeholderGroups, placeholderGroup)
|
|
|
|
|
|
|
|
searchResults := make([]*SearchResult, 0)
|
2020-12-03 19:00:11 +08:00
|
|
|
matchedSegments := make([]*Segment, 0)
|
2020-11-26 16:01:31 +08:00
|
|
|
|
2021-03-05 09:21:35 +08:00
|
|
|
//log.Debug("search msg's partitionID = ", partitionIDsInQuery)
|
2021-02-05 10:53:11 +08:00
|
|
|
partitionIDsInCol, err := ss.replica.getPartitionIDs(collectionID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-01-12 18:03:24 +08:00
|
|
|
}
|
2021-02-03 18:12:48 +08:00
|
|
|
var searchPartitionIDs []UniqueID
|
|
|
|
partitionIDsInQuery := searchMsg.PartitionIDs
|
|
|
|
if len(partitionIDsInQuery) == 0 {
|
2021-03-15 19:58:52 +08:00
|
|
|
if len(partitionIDsInCol) == 0 {
|
2021-03-22 16:36:10 +08:00
|
|
|
return errors.New("none of this collection's partition has been loaded")
|
2021-03-15 19:58:52 +08:00
|
|
|
}
|
2021-02-03 18:12:48 +08:00
|
|
|
searchPartitionIDs = partitionIDsInCol
|
2021-01-12 18:03:24 +08:00
|
|
|
} else {
|
2021-03-15 19:58:52 +08:00
|
|
|
for _, id := range partitionIDsInQuery {
|
2021-03-22 16:36:10 +08:00
|
|
|
_, err2 := ss.replica.getPartitionByID(id)
|
|
|
|
if err2 != nil {
|
|
|
|
return err2
|
2021-01-12 18:03:24 +08:00
|
|
|
}
|
2020-11-26 16:01:31 +08:00
|
|
|
}
|
2021-03-22 16:36:10 +08:00
|
|
|
searchPartitionIDs = partitionIDsInQuery
|
2021-01-09 09:47:22 +08:00
|
|
|
}
|
|
|
|
|
2021-02-03 18:12:48 +08:00
|
|
|
for _, partitionID := range searchPartitionIDs {
|
2021-02-05 10:53:11 +08:00
|
|
|
segmentIDs, err := ss.replica.getSegmentIDs(partitionID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, segmentID := range segmentIDs {
|
2021-03-05 09:21:35 +08:00
|
|
|
//log.Debug("dsl = ", dsl)
|
2021-02-05 10:53:11 +08:00
|
|
|
segment, err := ss.replica.getSegmentByID(segmentID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-26 16:01:31 +08:00
|
|
|
searchResult, err := segment.segmentSearch(plan, placeholderGroups, []Timestamp{searchTimestamp})
|
2020-11-28 19:06:48 +08:00
|
|
|
|
2020-11-26 16:01:31 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
searchResults = append(searchResults, searchResult)
|
2020-12-03 19:00:11 +08:00
|
|
|
matchedSegments = append(matchedSegments, segment)
|
2020-11-26 16:01:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-28 19:06:48 +08:00
|
|
|
if len(searchResults) <= 0 {
|
2021-01-12 18:03:24 +08:00
|
|
|
for _, group := range placeholderGroups {
|
|
|
|
nq := group.getNumOfQuery()
|
|
|
|
nilHits := make([][]byte, nq)
|
2021-01-22 09:36:18 +08:00
|
|
|
hit := &milvuspb.Hits{}
|
2021-01-12 18:03:24 +08:00
|
|
|
for i := 0; i < int(nq); i++ {
|
|
|
|
bs, err := proto.Marshal(hit)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
nilHits[i] = bs
|
|
|
|
}
|
2021-01-18 19:32:08 +08:00
|
|
|
resultChannelInt, _ := strconv.ParseInt(searchMsg.ResultChannelID, 10, 64)
|
2021-01-12 18:03:24 +08:00
|
|
|
searchResultMsg := &msgstream.SearchResultMsg{
|
2021-01-18 19:32:08 +08:00
|
|
|
BaseMsg: msgstream.BaseMsg{HashValues: []uint32{uint32(resultChannelInt)}},
|
2021-03-12 14:22:09 +08:00
|
|
|
SearchResults: internalpb.SearchResults{
|
2021-01-18 19:32:08 +08:00
|
|
|
Base: &commonpb.MsgBase{
|
2021-03-10 14:45:35 +08:00
|
|
|
MsgType: commonpb.MsgType_SearchResult,
|
2021-01-18 19:32:08 +08:00
|
|
|
MsgID: searchMsg.Base.MsgID,
|
|
|
|
Timestamp: searchTimestamp,
|
|
|
|
SourceID: searchMsg.Base.SourceID,
|
|
|
|
},
|
2021-03-10 22:06:22 +08:00
|
|
|
Status: &commonpb.Status{ErrorCode: commonpb.ErrorCode_Success},
|
2021-01-18 19:32:08 +08:00
|
|
|
ResultChannelID: searchMsg.ResultChannelID,
|
|
|
|
Hits: nilHits,
|
|
|
|
MetricType: plan.getMetricType(),
|
|
|
|
},
|
2021-01-12 18:03:24 +08:00
|
|
|
}
|
|
|
|
err = ss.publishSearchResult(searchResultMsg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2020-12-01 02:16:53 +08:00
|
|
|
}
|
2020-11-28 19:06:48 +08:00
|
|
|
}
|
|
|
|
|
2020-12-03 19:00:11 +08:00
|
|
|
inReduced := make([]bool, len(searchResults))
|
|
|
|
numSegment := int64(len(searchResults))
|
2020-12-11 17:20:14 +08:00
|
|
|
err2 := reduceSearchResults(searchResults, numSegment, inReduced)
|
|
|
|
if err2 != nil {
|
|
|
|
return err2
|
2020-12-03 19:00:11 +08:00
|
|
|
}
|
|
|
|
err = fillTargetEntry(plan, searchResults, matchedSegments, inReduced)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
marshaledHits, err := reorganizeQueryResults(plan, placeholderGroups, searchResults, numSegment, inReduced)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-26 16:01:31 +08:00
|
|
|
hitsBlob, err := marshaledHits.getHitsBlob()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var offset int64 = 0
|
|
|
|
for index := range placeholderGroups {
|
2020-12-03 19:00:11 +08:00
|
|
|
hitBlobSizePeerQuery, err := marshaledHits.hitBlobSizeInGroup(int64(index))
|
2020-11-26 16:01:31 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
hits := make([][]byte, 0)
|
2020-12-03 19:00:11 +08:00
|
|
|
for _, len := range hitBlobSizePeerQuery {
|
2020-11-26 16:01:31 +08:00
|
|
|
hits = append(hits, hitsBlob[offset:offset+len])
|
|
|
|
//test code to checkout marshaled hits
|
|
|
|
//marshaledHit := hitsBlob[offset:offset+len]
|
2021-01-22 09:36:18 +08:00
|
|
|
//unMarshaledHit := milvuspb.Hits{}
|
2020-11-26 16:01:31 +08:00
|
|
|
//err = proto.Unmarshal(marshaledHit, &unMarshaledHit)
|
|
|
|
//if err != nil {
|
|
|
|
// return err
|
|
|
|
//}
|
2021-03-05 09:21:35 +08:00
|
|
|
//log.Debug("hits msg = ", unMarshaledHit)
|
2020-11-26 16:01:31 +08:00
|
|
|
offset += len
|
|
|
|
}
|
2021-01-18 19:32:08 +08:00
|
|
|
resultChannelInt, _ := strconv.ParseInt(searchMsg.ResultChannelID, 10, 64)
|
2020-11-26 16:01:31 +08:00
|
|
|
searchResultMsg := &msgstream.SearchResultMsg{
|
2021-01-18 19:32:08 +08:00
|
|
|
BaseMsg: msgstream.BaseMsg{HashValues: []uint32{uint32(resultChannelInt)}},
|
2021-03-12 14:22:09 +08:00
|
|
|
SearchResults: internalpb.SearchResults{
|
2021-01-18 19:32:08 +08:00
|
|
|
Base: &commonpb.MsgBase{
|
2021-03-10 14:45:35 +08:00
|
|
|
MsgType: commonpb.MsgType_SearchResult,
|
2021-01-18 19:32:08 +08:00
|
|
|
MsgID: searchMsg.Base.MsgID,
|
|
|
|
Timestamp: searchTimestamp,
|
|
|
|
SourceID: searchMsg.Base.SourceID,
|
|
|
|
},
|
2021-03-10 22:06:22 +08:00
|
|
|
Status: &commonpb.Status{ErrorCode: commonpb.ErrorCode_Success},
|
2021-01-18 19:32:08 +08:00
|
|
|
ResultChannelID: searchMsg.ResultChannelID,
|
|
|
|
Hits: hits,
|
|
|
|
MetricType: plan.getMetricType(),
|
|
|
|
},
|
2020-11-26 16:01:31 +08:00
|
|
|
}
|
2021-01-18 19:32:08 +08:00
|
|
|
|
|
|
|
// For debugging, please don't delete.
|
2021-03-22 16:36:10 +08:00
|
|
|
//fmt.Println("==================== search result ======================")
|
2021-01-18 19:32:08 +08:00
|
|
|
//for i := 0; i < len(hits); i++ {
|
|
|
|
// testHits := milvuspb.Hits{}
|
|
|
|
// err := proto.Unmarshal(hits[i], &testHits)
|
|
|
|
// if err != nil {
|
|
|
|
// panic(err)
|
|
|
|
// }
|
2021-03-22 16:36:10 +08:00
|
|
|
// fmt.Println(testHits.IDs)
|
|
|
|
// fmt.Println(testHits.Scores)
|
2021-01-18 19:32:08 +08:00
|
|
|
//}
|
2020-11-26 16:01:31 +08:00
|
|
|
err = ss.publishSearchResult(searchResultMsg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteSearchResults(searchResults)
|
|
|
|
deleteMarshaledHits(marshaledHits)
|
|
|
|
plan.delete()
|
|
|
|
placeholderGroup.delete()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ss *searchService) publishSearchResult(msg msgstream.TsMsg) error {
|
2021-01-12 18:03:24 +08:00
|
|
|
// span, ctx := opentracing.StartSpanFromContext(msg.GetMsgContext(), "publish search result")
|
|
|
|
// defer span.Finish()
|
|
|
|
// msg.SetMsgContext(ctx)
|
2020-11-26 16:01:31 +08:00
|
|
|
msgPack := msgstream.MsgPack{}
|
|
|
|
msgPack.Msgs = append(msgPack.Msgs, msg)
|
2021-02-24 09:48:17 +08:00
|
|
|
err := ss.searchResultMsgStream.Produce(context.TODO(), &msgPack)
|
2020-12-11 12:01:20 +08:00
|
|
|
return err
|
2020-11-26 16:01:31 +08:00
|
|
|
}
|
|
|
|
|
2021-03-22 16:36:10 +08:00
|
|
|
func (ss *searchService) publishFailedSearchResult(searchMsg *msgstream.SearchMsg, errMsg string) error {
|
2021-01-12 18:03:24 +08:00
|
|
|
// span, ctx := opentracing.StartSpanFromContext(msg.GetMsgContext(), "receive search msg")
|
|
|
|
// defer span.Finish()
|
|
|
|
// msg.SetMsgContext(ctx)
|
2021-03-05 09:21:35 +08:00
|
|
|
//log.Debug("Public fail SearchResult!")
|
2020-11-26 16:01:31 +08:00
|
|
|
msgPack := msgstream.MsgPack{}
|
|
|
|
|
2021-01-18 19:32:08 +08:00
|
|
|
resultChannelInt, _ := strconv.ParseInt(searchMsg.ResultChannelID, 10, 64)
|
|
|
|
searchResultMsg := &msgstream.SearchResultMsg{
|
|
|
|
BaseMsg: msgstream.BaseMsg{HashValues: []uint32{uint32(resultChannelInt)}},
|
2021-03-12 14:22:09 +08:00
|
|
|
SearchResults: internalpb.SearchResults{
|
2021-01-18 19:32:08 +08:00
|
|
|
Base: &commonpb.MsgBase{
|
2021-03-10 14:45:35 +08:00
|
|
|
MsgType: commonpb.MsgType_SearchResult,
|
2021-01-18 19:32:08 +08:00
|
|
|
MsgID: searchMsg.Base.MsgID,
|
|
|
|
Timestamp: searchMsg.Base.Timestamp,
|
|
|
|
SourceID: searchMsg.Base.SourceID,
|
|
|
|
},
|
2021-03-10 22:06:22 +08:00
|
|
|
Status: &commonpb.Status{ErrorCode: commonpb.ErrorCode_UnexpectedError, Reason: errMsg},
|
2021-01-18 19:32:08 +08:00
|
|
|
ResultChannelID: searchMsg.ResultChannelID,
|
|
|
|
Hits: [][]byte{},
|
|
|
|
},
|
2020-11-26 16:01:31 +08:00
|
|
|
}
|
2021-01-18 19:32:08 +08:00
|
|
|
|
|
|
|
msgPack.Msgs = append(msgPack.Msgs, searchResultMsg)
|
2021-02-24 09:48:17 +08:00
|
|
|
err := ss.searchResultMsgStream.Produce(context.TODO(), &msgPack)
|
2020-11-26 16:01:31 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|