2021-01-16 10:12:14 +08:00
|
|
|
package querynode
|
2020-12-24 20:55:40 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2021-02-06 11:35:35 +08:00
|
|
|
"sync"
|
2020-12-29 14:43:40 +08:00
|
|
|
"time"
|
2020-12-24 20:55:40 +08:00
|
|
|
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/msgstream"
|
|
|
|
)
|
|
|
|
|
2021-02-20 10:14:03 +08:00
|
|
|
const loadingCheckInterval = 3
|
2021-01-30 16:02:10 +08:00
|
|
|
|
|
|
|
type loadService struct {
|
2020-12-24 20:55:40 +08:00
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
|
|
|
|
2021-02-06 11:35:35 +08:00
|
|
|
segLoader *segmentLoader
|
2021-01-19 11:37:16 +08:00
|
|
|
}
|
|
|
|
|
2021-02-02 19:54:31 +08:00
|
|
|
// -------------------------------------------- load index -------------------------------------------- //
|
|
|
|
func (s *loadService) start() {
|
2021-02-06 11:35:35 +08:00
|
|
|
wg := &sync.WaitGroup{}
|
2021-01-30 16:02:10 +08:00
|
|
|
for {
|
|
|
|
select {
|
2021-02-02 19:54:31 +08:00
|
|
|
case <-s.ctx.Done():
|
2021-01-30 16:02:10 +08:00
|
|
|
return
|
2021-02-20 10:14:03 +08:00
|
|
|
case <-time.After(loadingCheckInterval * time.Second):
|
2021-02-06 11:35:35 +08:00
|
|
|
wg.Add(2)
|
|
|
|
go s.segLoader.indexLoader.doLoadIndex(wg)
|
|
|
|
go s.loadSegmentActively(wg)
|
|
|
|
wg.Wait()
|
2021-01-18 10:38:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-02 19:54:31 +08:00
|
|
|
func (s *loadService) close() {
|
|
|
|
s.cancel()
|
2021-01-12 18:03:24 +08:00
|
|
|
}
|
|
|
|
|
2021-02-06 11:35:35 +08:00
|
|
|
func (s *loadService) loadSegmentActively(wg *sync.WaitGroup) {
|
2021-02-07 21:26:03 +08:00
|
|
|
collectionIDs, partitionIDs, segmentIDs := s.segLoader.replica.getSegmentsBySegmentType(segTypeGrowing)
|
2021-02-06 11:35:35 +08:00
|
|
|
if len(collectionIDs) <= 0 {
|
2021-02-07 10:29:58 +08:00
|
|
|
wg.Done()
|
2021-02-06 11:35:35 +08:00
|
|
|
return
|
2020-12-24 20:55:40 +08:00
|
|
|
}
|
2021-02-06 11:35:35 +08:00
|
|
|
fmt.Println("do load segment for growing segments:", segmentIDs)
|
|
|
|
for i := range collectionIDs {
|
|
|
|
fieldIDs, err := s.segLoader.replica.getFieldIDsByCollectionID(collectionIDs[i])
|
2020-12-24 20:55:40 +08:00
|
|
|
if err != nil {
|
2021-02-06 11:35:35 +08:00
|
|
|
log.Println(err)
|
|
|
|
continue
|
2020-12-24 20:55:40 +08:00
|
|
|
}
|
2021-02-06 11:35:35 +08:00
|
|
|
err = s.loadSegmentInternal(collectionIDs[i], partitionIDs[i], segmentIDs[i], fieldIDs)
|
2020-12-24 20:55:40 +08:00
|
|
|
if err != nil {
|
2021-02-06 11:35:35 +08:00
|
|
|
log.Println(err)
|
2020-12-24 20:55:40 +08:00
|
|
|
}
|
|
|
|
}
|
2021-02-06 11:35:35 +08:00
|
|
|
// sendQueryNodeStats
|
|
|
|
err := s.segLoader.indexLoader.sendQueryNodeStats()
|
2020-12-24 20:55:40 +08:00
|
|
|
if err != nil {
|
2021-02-06 11:35:35 +08:00
|
|
|
log.Println(err)
|
|
|
|
wg.Done()
|
|
|
|
return
|
2020-12-24 20:55:40 +08:00
|
|
|
}
|
|
|
|
|
2021-02-06 11:35:35 +08:00
|
|
|
wg.Done()
|
2020-12-24 20:55:40 +08:00
|
|
|
}
|
2021-01-12 18:03:24 +08:00
|
|
|
|
2021-02-06 11:35:35 +08:00
|
|
|
// load segment passively
|
2021-02-02 19:54:31 +08:00
|
|
|
func (s *loadService) loadSegment(collectionID UniqueID, partitionID UniqueID, segmentIDs []UniqueID, fieldIDs []int64) error {
|
|
|
|
// TODO: interim solution
|
|
|
|
if len(fieldIDs) == 0 {
|
2021-02-06 11:35:35 +08:00
|
|
|
var err error
|
|
|
|
fieldIDs, err = s.segLoader.replica.getFieldIDsByCollectionID(collectionID)
|
2021-02-02 19:54:31 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, segmentID := range segmentIDs {
|
2021-02-06 11:35:35 +08:00
|
|
|
err := s.loadSegmentInternal(collectionID, partitionID, segmentID, fieldIDs)
|
2021-02-02 19:54:31 +08:00
|
|
|
if err != nil {
|
2021-02-06 11:35:35 +08:00
|
|
|
log.Println(err)
|
|
|
|
continue
|
2021-02-02 19:54:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-06 11:35:35 +08:00
|
|
|
func (s *loadService) loadSegmentInternal(collectionID UniqueID, partitionID UniqueID, segmentID UniqueID, fieldIDs []int64) error {
|
2021-02-07 21:26:03 +08:00
|
|
|
// create segment
|
|
|
|
collection, err := s.segLoader.replica.getCollectionByID(collectionID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = s.segLoader.replica.getPartitionByID(partitionID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
segment := newSegment(collection, segmentID, partitionID, collectionID, segTypeSealed)
|
2021-02-06 11:35:35 +08:00
|
|
|
// we don't need index id yet
|
|
|
|
_, buildID, errIndex := s.segLoader.indexLoader.getIndexInfo(collectionID, segmentID)
|
|
|
|
if errIndex == nil {
|
|
|
|
// we don't need load to vector fields
|
2021-02-07 10:29:58 +08:00
|
|
|
vectorFields, err := s.segLoader.replica.getVecFieldIDsByCollectionID(collectionID)
|
2021-02-02 19:54:31 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-06 11:35:35 +08:00
|
|
|
fieldIDs = s.segLoader.filterOutVectorFields(fieldIDs, vectorFields)
|
2021-02-02 19:54:31 +08:00
|
|
|
}
|
2021-02-06 11:35:35 +08:00
|
|
|
paths, srcFieldIDs, err := s.segLoader.getInsertBinlogPaths(segmentID)
|
2021-02-02 19:54:31 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-22 09:58:34 +08:00
|
|
|
targetFields := s.segLoader.getTargetFields(paths, srcFieldIDs, fieldIDs)
|
2021-02-07 21:26:03 +08:00
|
|
|
err = s.segLoader.loadSegmentFieldsData(segment, targetFields)
|
2021-02-02 19:54:31 +08:00
|
|
|
if err != nil {
|
2021-02-06 11:35:35 +08:00
|
|
|
return err
|
2021-02-02 19:54:31 +08:00
|
|
|
}
|
2021-02-07 21:26:03 +08:00
|
|
|
// replace segment
|
|
|
|
err = s.segLoader.replica.replaceGrowingSegmentBySealedSegment(segment)
|
2021-02-06 11:35:35 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-02-02 19:54:31 +08:00
|
|
|
}
|
2021-02-06 11:35:35 +08:00
|
|
|
if errIndex == nil {
|
2021-02-07 17:02:13 +08:00
|
|
|
fmt.Println("loading index...")
|
2021-02-06 11:35:35 +08:00
|
|
|
indexPaths, err := s.segLoader.indexLoader.getIndexPaths(buildID)
|
2021-02-02 19:54:31 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-06 11:35:35 +08:00
|
|
|
err = s.segLoader.indexLoader.loadIndexImmediate(segment, indexPaths)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-02-02 19:54:31 +08:00
|
|
|
}
|
|
|
|
}
|
2021-02-07 21:26:03 +08:00
|
|
|
return nil
|
2021-01-12 18:03:24 +08:00
|
|
|
}
|
2021-01-30 16:02:10 +08:00
|
|
|
|
|
|
|
func newLoadService(ctx context.Context, masterClient MasterServiceInterface, dataClient DataServiceInterface, indexClient IndexServiceInterface, replica collectionReplica, dmStream msgstream.MsgStream) *loadService {
|
|
|
|
ctx1, cancel := context.WithCancel(ctx)
|
|
|
|
|
2021-02-06 11:35:35 +08:00
|
|
|
segLoader := newSegmentLoader(ctx1, masterClient, indexClient, dataClient, replica, dmStream)
|
2021-01-30 16:02:10 +08:00
|
|
|
|
|
|
|
return &loadService{
|
|
|
|
ctx: ctx1,
|
|
|
|
cancel: cancel,
|
|
|
|
|
2021-02-06 11:35:35 +08:00
|
|
|
segLoader: segLoader,
|
2021-01-30 16:02:10 +08:00
|
|
|
}
|
|
|
|
}
|