2021-12-09 14:39:06 +08:00
|
|
|
// Licensed to the LF AI & Data foundation under one
|
|
|
|
// or more contributor license agreements. See the NOTICE file
|
|
|
|
// distributed with this work for additional information
|
|
|
|
// regarding copyright ownership. The ASF licenses this file
|
|
|
|
// to you under the Apache License, Version 2.0 (the
|
|
|
|
// "License"); you may not use this file except in compliance
|
2021-06-23 20:26:10 +08:00
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
2021-12-09 14:39:06 +08:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2021-06-23 20:26:10 +08:00
|
|
|
//
|
2021-12-09 14:39:06 +08:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2021-06-23 20:26:10 +08:00
|
|
|
|
|
|
|
package querynode
|
|
|
|
|
|
|
|
import "C"
|
|
|
|
import (
|
|
|
|
"context"
|
2021-09-24 13:57:54 +08:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-08-18 16:30:11 +08:00
|
|
|
"strconv"
|
2021-09-24 13:57:54 +08:00
|
|
|
"sync"
|
2021-06-23 20:26:10 +08:00
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"github.com/milvus-io/milvus/internal/log"
|
2022-03-03 21:57:56 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/mq/msgstream"
|
2021-07-24 09:25:22 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/storage"
|
2021-06-23 20:26:10 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type queryService struct {
|
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
|
|
|
|
|
|
|
historical *historical
|
|
|
|
streaming *streaming
|
|
|
|
|
2021-09-24 13:57:54 +08:00
|
|
|
queryCollectionMu sync.Mutex // guards queryCollections
|
|
|
|
queryCollections map[UniqueID]*queryCollection
|
2021-06-23 20:26:10 +08:00
|
|
|
|
|
|
|
factory msgstream.Factory
|
2021-07-24 09:25:22 +08:00
|
|
|
|
2022-01-17 14:41:35 +08:00
|
|
|
sessionManager *SessionManager
|
|
|
|
|
2021-08-27 17:51:56 +08:00
|
|
|
localChunkManager storage.ChunkManager
|
|
|
|
remoteChunkManager storage.ChunkManager
|
|
|
|
localCacheEnabled bool
|
2021-06-23 20:26:10 +08:00
|
|
|
}
|
|
|
|
|
2022-01-17 14:41:35 +08:00
|
|
|
type qsOpt func(*queryService)
|
|
|
|
|
|
|
|
func qsOptWithSessionManager(s *SessionManager) qsOpt {
|
|
|
|
return func(qs *queryService) {
|
|
|
|
qs.sessionManager = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-23 20:26:10 +08:00
|
|
|
func newQueryService(ctx context.Context,
|
|
|
|
historical *historical,
|
|
|
|
streaming *streaming,
|
2022-01-17 14:41:35 +08:00
|
|
|
factory msgstream.Factory,
|
|
|
|
opts ...qsOpt,
|
|
|
|
) *queryService {
|
2021-06-23 20:26:10 +08:00
|
|
|
|
|
|
|
queryServiceCtx, queryServiceCancel := context.WithCancel(ctx)
|
2021-07-24 09:25:22 +08:00
|
|
|
|
2021-08-18 16:30:11 +08:00
|
|
|
//TODO godchen: change this to configuration
|
2022-02-07 10:09:45 +08:00
|
|
|
path, err := Params.Load("localStorage.Path")
|
2021-07-24 09:25:22 +08:00
|
|
|
if err != nil {
|
2021-08-18 16:30:11 +08:00
|
|
|
path = "/tmp/milvus/data"
|
2021-07-24 09:25:22 +08:00
|
|
|
}
|
2022-02-07 10:09:45 +08:00
|
|
|
enabled, _ := Params.Load("localStorage.enabled")
|
2021-08-18 16:30:11 +08:00
|
|
|
localCacheEnabled, _ := strconv.ParseBool(enabled)
|
|
|
|
|
2022-02-24 23:53:53 +08:00
|
|
|
localChunkManager := storage.NewLocalChunkManager(storage.RootPath(path))
|
2021-07-24 09:25:22 +08:00
|
|
|
|
2022-02-24 23:53:53 +08:00
|
|
|
remoteChunkManager, err := storage.NewMinioChunkManager(
|
|
|
|
ctx,
|
|
|
|
storage.Address(Params.MinioCfg.Address),
|
|
|
|
storage.AccessKeyID(Params.MinioCfg.AccessKeyID),
|
|
|
|
storage.SecretAccessKeyID(Params.MinioCfg.SecretAccessKey),
|
|
|
|
storage.UseSSL(Params.MinioCfg.UseSSL),
|
|
|
|
storage.BucketName(Params.MinioCfg.BucketName),
|
|
|
|
storage.CreateBucket(true))
|
2021-07-24 09:25:22 +08:00
|
|
|
|
2022-01-17 14:41:35 +08:00
|
|
|
qs := &queryService{
|
2021-06-23 20:26:10 +08:00
|
|
|
ctx: queryServiceCtx,
|
|
|
|
cancel: queryServiceCancel,
|
|
|
|
|
|
|
|
historical: historical,
|
|
|
|
streaming: streaming,
|
|
|
|
|
|
|
|
queryCollections: make(map[UniqueID]*queryCollection),
|
|
|
|
|
|
|
|
factory: factory,
|
2021-07-24 09:25:22 +08:00
|
|
|
|
2021-08-27 17:51:56 +08:00
|
|
|
localChunkManager: localChunkManager,
|
|
|
|
remoteChunkManager: remoteChunkManager,
|
|
|
|
localCacheEnabled: localCacheEnabled,
|
2021-06-23 20:26:10 +08:00
|
|
|
}
|
2022-01-17 14:41:35 +08:00
|
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(qs)
|
|
|
|
}
|
|
|
|
|
|
|
|
return qs
|
2021-06-23 20:26:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (q *queryService) close() {
|
|
|
|
log.Debug("search service closed")
|
2021-09-24 13:57:54 +08:00
|
|
|
q.queryCollectionMu.Lock()
|
2021-10-08 17:41:49 +08:00
|
|
|
for collectionID, sc := range q.queryCollections {
|
|
|
|
sc.close()
|
|
|
|
sc.cancel()
|
|
|
|
delete(q.queryCollections, collectionID)
|
|
|
|
}
|
2021-06-23 20:26:10 +08:00
|
|
|
q.queryCollections = make(map[UniqueID]*queryCollection)
|
2021-09-24 13:57:54 +08:00
|
|
|
q.queryCollectionMu.Unlock()
|
2021-06-23 20:26:10 +08:00
|
|
|
q.cancel()
|
|
|
|
}
|
|
|
|
|
2021-09-24 13:57:54 +08:00
|
|
|
func (q *queryService) addQueryCollection(collectionID UniqueID) error {
|
|
|
|
q.queryCollectionMu.Lock()
|
|
|
|
defer q.queryCollectionMu.Unlock()
|
2021-06-23 20:26:10 +08:00
|
|
|
if _, ok := q.queryCollections[collectionID]; ok {
|
|
|
|
log.Warn("query collection already exists", zap.Any("collectionID", collectionID))
|
2021-09-24 13:57:54 +08:00
|
|
|
err := errors.New(fmt.Sprintln("query collection already exists, collectionID = ", collectionID))
|
|
|
|
return err
|
2021-06-23 20:26:10 +08:00
|
|
|
}
|
|
|
|
ctx1, cancel := context.WithCancel(q.ctx)
|
2021-09-24 13:57:54 +08:00
|
|
|
qc, err := newQueryCollection(ctx1,
|
2021-06-23 20:26:10 +08:00
|
|
|
cancel,
|
|
|
|
collectionID,
|
|
|
|
q.historical,
|
|
|
|
q.streaming,
|
2021-07-24 09:25:22 +08:00
|
|
|
q.factory,
|
2021-08-27 17:51:56 +08:00
|
|
|
q.localChunkManager,
|
|
|
|
q.remoteChunkManager,
|
|
|
|
q.localCacheEnabled,
|
2022-01-17 14:41:35 +08:00
|
|
|
qcOptWithSessionManager(q.sessionManager),
|
2021-08-18 16:30:11 +08:00
|
|
|
)
|
2021-09-24 13:57:54 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-23 20:26:10 +08:00
|
|
|
q.queryCollections[collectionID] = qc
|
2021-09-24 13:57:54 +08:00
|
|
|
return nil
|
2021-06-23 20:26:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (q *queryService) hasQueryCollection(collectionID UniqueID) bool {
|
2021-09-24 13:57:54 +08:00
|
|
|
q.queryCollectionMu.Lock()
|
|
|
|
defer q.queryCollectionMu.Unlock()
|
2021-06-23 20:26:10 +08:00
|
|
|
_, ok := q.queryCollections[collectionID]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2021-09-24 13:57:54 +08:00
|
|
|
func (q *queryService) getQueryCollection(collectionID UniqueID) (*queryCollection, error) {
|
|
|
|
q.queryCollectionMu.Lock()
|
|
|
|
defer q.queryCollectionMu.Unlock()
|
|
|
|
_, ok := q.queryCollections[collectionID]
|
|
|
|
if ok {
|
|
|
|
return q.queryCollections[collectionID], nil
|
|
|
|
}
|
|
|
|
return nil, errors.New(fmt.Sprintln("queryCollection not exists, collectionID = ", collectionID))
|
|
|
|
}
|
|
|
|
|
2021-06-23 20:26:10 +08:00
|
|
|
func (q *queryService) stopQueryCollection(collectionID UniqueID) {
|
2021-09-24 13:57:54 +08:00
|
|
|
q.queryCollectionMu.Lock()
|
|
|
|
defer q.queryCollectionMu.Unlock()
|
2021-06-23 20:26:10 +08:00
|
|
|
sc, ok := q.queryCollections[collectionID]
|
|
|
|
if !ok {
|
2021-07-31 10:47:22 +08:00
|
|
|
log.Warn("stopQueryCollection failed, collection doesn't exist", zap.Int64("collectionID", collectionID))
|
2021-06-23 20:26:10 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
sc.close()
|
|
|
|
sc.cancel()
|
2021-12-29 21:06:03 +08:00
|
|
|
// for not blocking waitNewTsafe, which will block doUnsolvedMsg quit.
|
|
|
|
sc.watcherCond.Broadcast()
|
2021-06-23 20:26:10 +08:00
|
|
|
delete(q.queryCollections, collectionID)
|
|
|
|
}
|