2021-04-19 13:50:12 +08:00
|
|
|
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// 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-01-15 14:38:36 +08:00
|
|
|
package indexservice
|
|
|
|
|
|
|
|
import (
|
2021-01-19 18:32:57 +08:00
|
|
|
"context"
|
2021-03-08 10:09:48 +08:00
|
|
|
"errors"
|
2021-03-08 15:25:55 +08:00
|
|
|
"math/rand"
|
2021-05-27 22:24:29 +08:00
|
|
|
"strconv"
|
2021-06-11 16:52:09 +08:00
|
|
|
"strings"
|
2021-01-19 18:32:57 +08:00
|
|
|
"sync"
|
2021-06-04 16:29:35 +08:00
|
|
|
"sync/atomic"
|
2021-01-19 18:32:57 +08:00
|
|
|
"time"
|
|
|
|
|
2021-05-27 22:24:29 +08:00
|
|
|
"github.com/coreos/etcd/mvcc/mvccpb"
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
"go.etcd.io/etcd/clientv3"
|
2021-03-10 09:56:09 +08:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/allocator"
|
|
|
|
"github.com/milvus-io/milvus/internal/kv"
|
|
|
|
etcdkv "github.com/milvus-io/milvus/internal/kv/etcd"
|
|
|
|
miniokv "github.com/milvus-io/milvus/internal/kv/minio"
|
|
|
|
"github.com/milvus-io/milvus/internal/log"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/commonpb"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/indexpb"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/internalpb"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/milvuspb"
|
|
|
|
"github.com/milvus-io/milvus/internal/tso"
|
|
|
|
"github.com/milvus-io/milvus/internal/util/retry"
|
2021-05-21 19:28:52 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/sessionutil"
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/tsoutil"
|
|
|
|
"github.com/milvus-io/milvus/internal/util/typeutil"
|
2021-01-26 09:38:40 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
reqTimeoutInterval = time.Second * 10
|
2021-04-16 15:37:13 +08:00
|
|
|
durationInterval = time.Second * 10
|
2021-05-27 22:24:29 +08:00
|
|
|
recycleIndexLimit = 20
|
2021-01-15 14:38:36 +08:00
|
|
|
)
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
type IndexService struct {
|
2021-01-26 09:38:40 +08:00
|
|
|
nodeClients *PriorityQueue
|
2021-03-12 14:22:09 +08:00
|
|
|
nodeStates map[UniqueID]*internalpb.ComponentStates
|
2021-06-04 16:29:35 +08:00
|
|
|
stateCode atomic.Value
|
2021-01-26 19:24:09 +08:00
|
|
|
|
|
|
|
ID UniqueID
|
2021-01-15 14:38:36 +08:00
|
|
|
|
2021-01-19 18:32:57 +08:00
|
|
|
loopCtx context.Context
|
|
|
|
loopCancel func()
|
|
|
|
loopWg sync.WaitGroup
|
|
|
|
|
2021-05-21 19:28:52 +08:00
|
|
|
sched *TaskScheduler
|
|
|
|
session *sessionutil.Session
|
2021-01-19 18:32:57 +08:00
|
|
|
|
2021-05-27 22:24:29 +08:00
|
|
|
eventChan <-chan *sessionutil.SessionEvent
|
|
|
|
|
|
|
|
assignChan chan []UniqueID
|
|
|
|
|
2021-02-24 17:12:06 +08:00
|
|
|
idAllocator *allocator.GlobalIDAllocator
|
2021-01-15 14:38:36 +08:00
|
|
|
|
2021-04-12 18:09:28 +08:00
|
|
|
kv kv.BaseKV
|
2021-01-19 18:32:57 +08:00
|
|
|
|
|
|
|
metaTable *metaTable
|
2021-01-26 09:38:40 +08:00
|
|
|
|
2021-05-27 22:24:29 +08:00
|
|
|
nodeTasks *nodeTasks
|
|
|
|
|
2021-01-26 09:38:40 +08:00
|
|
|
nodeLock sync.RWMutex
|
|
|
|
|
2021-01-19 18:32:57 +08:00
|
|
|
// Add callback functions at different stages
|
|
|
|
startCallbacks []func()
|
|
|
|
closeCallbacks []func()
|
2021-01-15 14:38:36 +08:00
|
|
|
}
|
|
|
|
|
2021-01-19 18:32:57 +08:00
|
|
|
type UniqueID = typeutil.UniqueID
|
|
|
|
type Timestamp = typeutil.Timestamp
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
func NewIndexService(ctx context.Context) (*IndexService, error) {
|
2021-03-08 15:25:55 +08:00
|
|
|
rand.Seed(time.Now().UnixNano())
|
2021-01-26 09:38:40 +08:00
|
|
|
ctx1, cancel := context.WithCancel(ctx)
|
2021-03-05 16:52:45 +08:00
|
|
|
i := &IndexService{
|
2021-01-26 09:38:40 +08:00
|
|
|
loopCtx: ctx1,
|
|
|
|
loopCancel: cancel,
|
|
|
|
nodeClients: &PriorityQueue{},
|
2021-05-27 22:24:29 +08:00
|
|
|
nodeTasks: &nodeTasks{},
|
2021-01-26 09:38:40 +08:00
|
|
|
}
|
2021-06-11 09:50:34 +08:00
|
|
|
i.UpdateStateCode(internalpb.StateCode_Abnormal)
|
2021-01-29 17:08:31 +08:00
|
|
|
return i, nil
|
|
|
|
}
|
|
|
|
|
2021-05-25 15:06:05 +08:00
|
|
|
// Register register index service at etcd
|
|
|
|
func (i *IndexService) Register() error {
|
2021-06-11 22:04:41 +08:00
|
|
|
i.session = sessionutil.NewSession(i.loopCtx, Params.MetaRootPath, Params.EtcdEndpoints)
|
2021-05-25 15:06:05 +08:00
|
|
|
i.session.Init(typeutil.IndexServiceRole, Params.Address, true)
|
2021-05-27 22:24:29 +08:00
|
|
|
i.eventChan = i.session.WatchServices(typeutil.IndexNodeRole, 0)
|
2021-05-25 15:06:05 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
func (i *IndexService) Init() error {
|
2021-06-11 22:04:41 +08:00
|
|
|
log.Debug("IndexService", zap.Any("etcd endpoints", Params.EtcdEndpoints))
|
2021-05-21 19:28:52 +08:00
|
|
|
|
2021-05-27 22:24:29 +08:00
|
|
|
i.assignChan = make(chan []UniqueID, 1024)
|
2021-01-26 09:38:40 +08:00
|
|
|
connectEtcdFn := func() error {
|
2021-06-11 22:04:41 +08:00
|
|
|
etcdClient, err := clientv3.New(clientv3.Config{Endpoints: Params.EtcdEndpoints})
|
2021-01-26 09:38:40 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-19 18:36:05 +08:00
|
|
|
etcdKV := etcdkv.NewEtcdKV(etcdClient, Params.MetaRootPath)
|
|
|
|
metakv, err := NewMetaTable(etcdKV)
|
2021-01-26 09:38:40 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
i.metaTable = metakv
|
2021-01-29 17:08:31 +08:00
|
|
|
return err
|
2021-01-26 09:38:40 +08:00
|
|
|
}
|
2021-06-04 16:29:35 +08:00
|
|
|
log.Debug("IndexService try to connect etcd")
|
2021-05-19 18:36:05 +08:00
|
|
|
err := retry.Retry(100000, time.Millisecond*200, connectEtcdFn)
|
2021-05-14 10:05:18 +08:00
|
|
|
if err != nil {
|
2021-06-04 16:29:35 +08:00
|
|
|
log.Debug("IndexService try to connect etcd failed", zap.Error(err))
|
2021-05-14 10:05:18 +08:00
|
|
|
return err
|
|
|
|
}
|
2021-06-04 16:29:35 +08:00
|
|
|
log.Debug("IndexService try to connect etcd success")
|
2021-05-14 10:05:18 +08:00
|
|
|
|
2021-01-26 09:38:40 +08:00
|
|
|
//init idAllocator
|
|
|
|
kvRootPath := Params.KvRootPath
|
2021-06-11 22:04:41 +08:00
|
|
|
i.idAllocator = allocator.NewGlobalIDAllocator("idTimestamp", tsoutil.NewTSOKVBase(Params.EtcdEndpoints, kvRootPath, "index_gid"))
|
2021-01-26 09:38:40 +08:00
|
|
|
if err := i.idAllocator.Initialize(); err != nil {
|
2021-06-04 16:29:35 +08:00
|
|
|
log.Debug("IndexService idAllocator initialize failed", zap.Error(err))
|
2021-01-29 17:08:31 +08:00
|
|
|
return err
|
2021-01-26 09:38:40 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 19:24:09 +08:00
|
|
|
i.ID, err = i.idAllocator.AllocOne()
|
|
|
|
if err != nil {
|
2021-01-29 17:08:31 +08:00
|
|
|
return err
|
2021-01-26 19:24:09 +08:00
|
|
|
}
|
|
|
|
|
2021-02-26 15:17:47 +08:00
|
|
|
option := &miniokv.Option{
|
|
|
|
Address: Params.MinIOAddress,
|
|
|
|
AccessKeyID: Params.MinIOAccessKeyID,
|
|
|
|
SecretAccessKeyID: Params.MinIOSecretAccessKey,
|
|
|
|
UseSSL: Params.MinIOUseSSL,
|
|
|
|
BucketName: Params.MinioBucketName,
|
|
|
|
CreateBucket: true,
|
2021-01-26 09:38:40 +08:00
|
|
|
}
|
2021-02-26 15:17:47 +08:00
|
|
|
|
|
|
|
i.kv, err = miniokv.NewMinIOKV(i.loopCtx, option)
|
2021-01-26 09:38:40 +08:00
|
|
|
if err != nil {
|
2021-06-04 16:29:35 +08:00
|
|
|
log.Debug("IndexService new minio kv failed", zap.Error(err))
|
2021-01-29 17:08:31 +08:00
|
|
|
return err
|
2021-01-26 09:38:40 +08:00
|
|
|
}
|
2021-06-04 16:29:35 +08:00
|
|
|
log.Debug("IndexService new minio kv success")
|
2021-01-26 09:38:40 +08:00
|
|
|
|
|
|
|
i.sched, err = NewTaskScheduler(i.loopCtx, i.idAllocator, i.kv, i.metaTable)
|
|
|
|
if err != nil {
|
2021-06-04 16:29:35 +08:00
|
|
|
log.Debug("IndexService new task scheduler failed", zap.Error(err))
|
2021-01-29 17:08:31 +08:00
|
|
|
return err
|
2021-01-26 09:38:40 +08:00
|
|
|
}
|
2021-06-04 16:29:35 +08:00
|
|
|
log.Debug("IndexService new task scheduler success")
|
2021-03-12 14:22:09 +08:00
|
|
|
i.UpdateStateCode(internalpb.StateCode_Healthy)
|
2021-06-04 16:29:35 +08:00
|
|
|
log.Debug("IndexService", zap.Any("State", i.stateCode.Load()))
|
2021-05-27 22:24:29 +08:00
|
|
|
|
|
|
|
i.nodeTasks = NewNodeTasks()
|
|
|
|
|
|
|
|
err = i.assignTasksServerStart()
|
|
|
|
if err != nil {
|
2021-06-04 16:29:35 +08:00
|
|
|
log.Debug("IndexService assign tasks server start failed", zap.Error(err))
|
2021-05-27 22:24:29 +08:00
|
|
|
return err
|
|
|
|
}
|
2021-06-04 16:29:35 +08:00
|
|
|
log.Debug("IndexService assign tasks server success", zap.Error(err))
|
2021-01-26 09:38:40 +08:00
|
|
|
return nil
|
2021-01-15 14:38:36 +08:00
|
|
|
}
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
func (i *IndexService) Start() error {
|
2021-02-08 14:20:29 +08:00
|
|
|
i.loopWg.Add(1)
|
|
|
|
go i.tsLoop()
|
|
|
|
|
2021-04-16 15:37:13 +08:00
|
|
|
i.loopWg.Add(1)
|
2021-05-27 22:24:29 +08:00
|
|
|
go i.recycleUnusedIndexFiles()
|
|
|
|
|
|
|
|
i.loopWg.Add(1)
|
|
|
|
go i.assignmentTasksLoop()
|
|
|
|
|
|
|
|
i.loopWg.Add(1)
|
|
|
|
go i.watchNodeLoop()
|
|
|
|
|
|
|
|
i.loopWg.Add(1)
|
|
|
|
go i.watchMetaLoop()
|
2021-04-16 15:37:13 +08:00
|
|
|
|
2021-01-26 09:38:40 +08:00
|
|
|
i.sched.Start()
|
|
|
|
// Start callbacks
|
|
|
|
for _, cb := range i.startCallbacks {
|
|
|
|
cb()
|
|
|
|
}
|
2021-03-10 09:56:09 +08:00
|
|
|
log.Debug("IndexService start")
|
2021-01-26 09:38:40 +08:00
|
|
|
|
|
|
|
return nil
|
2021-01-15 14:38:36 +08:00
|
|
|
}
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
func (i *IndexService) Stop() error {
|
2021-01-26 09:38:40 +08:00
|
|
|
i.loopCancel()
|
|
|
|
i.sched.Close()
|
|
|
|
for _, cb := range i.closeCallbacks {
|
|
|
|
cb()
|
|
|
|
}
|
|
|
|
return nil
|
2021-01-15 14:38:36 +08:00
|
|
|
}
|
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
func (i *IndexService) UpdateStateCode(code internalpb.StateCode) {
|
2021-06-04 16:29:35 +08:00
|
|
|
i.stateCode.Store(code)
|
2021-01-29 17:08:31 +08:00
|
|
|
}
|
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
func (i *IndexService) GetComponentStates(ctx context.Context) (*internalpb.ComponentStates, error) {
|
2021-06-04 16:29:35 +08:00
|
|
|
log.Debug("get IndexService component states ...")
|
2021-03-12 14:22:09 +08:00
|
|
|
stateInfo := &internalpb.ComponentInfo{
|
2021-01-26 19:24:09 +08:00
|
|
|
NodeID: i.ID,
|
2021-03-05 16:52:45 +08:00
|
|
|
Role: "IndexService",
|
2021-06-04 16:29:35 +08:00
|
|
|
StateCode: i.stateCode.Load().(internalpb.StateCode),
|
2021-01-26 19:24:09 +08:00
|
|
|
}
|
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
ret := &internalpb.ComponentStates{
|
2021-01-26 19:24:09 +08:00
|
|
|
State: stateInfo,
|
|
|
|
SubcomponentStates: nil, // todo add subcomponents states
|
|
|
|
Status: &commonpb.Status{
|
2021-03-10 22:06:22 +08:00
|
|
|
ErrorCode: commonpb.ErrorCode_Success,
|
2021-01-26 19:24:09 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
return ret, nil
|
2021-01-15 14:38:36 +08:00
|
|
|
}
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
func (i *IndexService) GetTimeTickChannel(ctx context.Context) (*milvuspb.StringResponse, error) {
|
2021-06-04 16:29:35 +08:00
|
|
|
log.Debug("get IndexService time tick channel ...")
|
2021-02-26 17:44:24 +08:00
|
|
|
return &milvuspb.StringResponse{
|
|
|
|
Status: &commonpb.Status{
|
2021-03-10 22:06:22 +08:00
|
|
|
ErrorCode: commonpb.ErrorCode_Success,
|
2021-02-26 17:44:24 +08:00
|
|
|
Reason: "",
|
|
|
|
},
|
|
|
|
Value: "",
|
|
|
|
}, nil
|
2021-01-15 14:38:36 +08:00
|
|
|
}
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
func (i *IndexService) GetStatisticsChannel(ctx context.Context) (*milvuspb.StringResponse, error) {
|
2021-06-04 16:29:35 +08:00
|
|
|
log.Debug("get IndexService statistics channel ...")
|
2021-02-26 17:44:24 +08:00
|
|
|
return &milvuspb.StringResponse{
|
|
|
|
Status: &commonpb.Status{
|
2021-03-10 22:06:22 +08:00
|
|
|
ErrorCode: commonpb.ErrorCode_Success,
|
2021-02-26 17:44:24 +08:00
|
|
|
Reason: "",
|
|
|
|
},
|
|
|
|
Value: "",
|
|
|
|
}, nil
|
2021-01-15 14:38:36 +08:00
|
|
|
}
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
func (i *IndexService) BuildIndex(ctx context.Context, req *indexpb.BuildIndexRequest) (*indexpb.BuildIndexResponse, error) {
|
2021-05-27 22:24:29 +08:00
|
|
|
log.Debug("IndexService building index ...",
|
2021-03-26 10:01:08 +08:00
|
|
|
zap.Int64("IndexBuildID", req.IndexBuildID),
|
|
|
|
zap.String("IndexName = ", req.IndexName),
|
|
|
|
zap.Int64("IndexID = ", req.IndexID),
|
|
|
|
zap.Strings("DataPath = ", req.DataPaths),
|
|
|
|
zap.Any("TypeParams", req.TypeParams),
|
|
|
|
zap.Any("IndexParams", req.IndexParams))
|
2021-05-27 22:24:29 +08:00
|
|
|
hasIndex, indexBuildID := i.metaTable.HasSameReq(req)
|
|
|
|
if hasIndex {
|
|
|
|
log.Debug("IndexService", zap.Any("hasIndex true", indexBuildID))
|
|
|
|
return &indexpb.BuildIndexResponse{
|
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_Success,
|
|
|
|
Reason: "already have same index",
|
|
|
|
},
|
|
|
|
IndexBuildID: indexBuildID,
|
|
|
|
}, nil
|
|
|
|
}
|
2021-01-26 09:38:40 +08:00
|
|
|
ret := &indexpb.BuildIndexResponse{
|
|
|
|
Status: &commonpb.Status{
|
2021-03-10 22:06:22 +08:00
|
|
|
ErrorCode: commonpb.ErrorCode_UnexpectedError,
|
2021-01-19 18:32:57 +08:00
|
|
|
},
|
2021-01-26 09:38:40 +08:00
|
|
|
}
|
2021-02-23 09:58:06 +08:00
|
|
|
t := &IndexAddTask{
|
|
|
|
BaseTask: BaseTask{
|
|
|
|
ctx: ctx,
|
|
|
|
done: make(chan error),
|
|
|
|
table: i.metaTable,
|
|
|
|
},
|
|
|
|
req: req,
|
|
|
|
idAllocator: i.idAllocator,
|
|
|
|
kv: i.kv,
|
|
|
|
}
|
2021-01-26 09:38:40 +08:00
|
|
|
|
|
|
|
if i.nodeClients == nil || i.nodeClients.Len() <= 0 {
|
|
|
|
ret.Status.Reason = "IndexBuilding Service not available"
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
t.nodeClients = i.nodeClients
|
|
|
|
|
|
|
|
var cancel func()
|
|
|
|
t.ctx, cancel = context.WithTimeout(ctx, reqTimeoutInterval)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
fn := func() error {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return errors.New("IndexAddQueue enqueue timeout")
|
|
|
|
default:
|
|
|
|
return i.sched.IndexAddQueue.Enqueue(t)
|
|
|
|
}
|
|
|
|
}
|
2021-01-15 14:38:36 +08:00
|
|
|
|
2021-01-26 09:38:40 +08:00
|
|
|
err := fn()
|
|
|
|
if err != nil {
|
2021-03-10 22:06:22 +08:00
|
|
|
ret.Status.ErrorCode = commonpb.ErrorCode_UnexpectedError
|
2021-01-26 09:38:40 +08:00
|
|
|
ret.Status.Reason = err.Error()
|
|
|
|
return ret, nil
|
|
|
|
}
|
2021-06-15 11:01:36 +08:00
|
|
|
log.Debug("IndexService BuildIndex Enqueue successfully", zap.Any("IndexBuildID", indexBuildID))
|
2021-01-20 18:26:20 +08:00
|
|
|
|
2021-01-26 09:38:40 +08:00
|
|
|
err = t.WaitToFinish()
|
|
|
|
if err != nil {
|
2021-03-10 22:06:22 +08:00
|
|
|
ret.Status.ErrorCode = commonpb.ErrorCode_UnexpectedError
|
2021-01-26 09:38:40 +08:00
|
|
|
ret.Status.Reason = err.Error()
|
|
|
|
return ret, nil
|
|
|
|
}
|
2021-05-27 22:24:29 +08:00
|
|
|
i.assignChan <- []UniqueID{t.indexBuildID}
|
2021-03-10 22:06:22 +08:00
|
|
|
ret.Status.ErrorCode = commonpb.ErrorCode_Success
|
2021-02-02 19:56:04 +08:00
|
|
|
ret.IndexBuildID = t.indexBuildID
|
2021-01-26 09:38:40 +08:00
|
|
|
return ret, nil
|
2021-01-15 14:38:36 +08:00
|
|
|
}
|
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
func (i *IndexService) GetIndexStates(ctx context.Context, req *indexpb.GetIndexStatesRequest) (*indexpb.GetIndexStatesResponse, error) {
|
2021-06-04 16:29:35 +08:00
|
|
|
log.Debug("IndexService get index states ...", zap.Int64s("IndexBuildIDs", req.IndexBuildIDs))
|
2021-01-20 15:02:23 +08:00
|
|
|
var indexStates []*indexpb.IndexInfo
|
2021-02-02 19:56:04 +08:00
|
|
|
for _, indexID := range req.IndexBuildIDs {
|
2021-01-26 09:38:40 +08:00
|
|
|
indexState, err := i.metaTable.GetIndexState(indexID)
|
|
|
|
if err != nil {
|
|
|
|
indexState.Reason = err.Error()
|
2021-01-20 15:02:23 +08:00
|
|
|
}
|
|
|
|
indexStates = append(indexStates, indexState)
|
|
|
|
}
|
2021-03-12 14:22:09 +08:00
|
|
|
ret := &indexpb.GetIndexStatesResponse{
|
2021-01-19 18:32:57 +08:00
|
|
|
Status: &commonpb.Status{
|
2021-03-10 22:06:22 +08:00
|
|
|
ErrorCode: commonpb.ErrorCode_Success,
|
2021-01-19 18:32:57 +08:00
|
|
|
},
|
2021-01-20 15:02:23 +08:00
|
|
|
States: indexStates,
|
2021-01-19 18:32:57 +08:00
|
|
|
}
|
2021-06-06 09:41:35 +08:00
|
|
|
log.Debug("IndexService get index states success",
|
2021-03-26 10:01:08 +08:00
|
|
|
zap.Any("index status", ret.Status),
|
|
|
|
zap.Any("index states", ret.States))
|
|
|
|
|
2021-01-19 18:32:57 +08:00
|
|
|
return ret, nil
|
2021-01-15 14:38:36 +08:00
|
|
|
}
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
func (i *IndexService) DropIndex(ctx context.Context, req *indexpb.DropIndexRequest) (*commonpb.Status, error) {
|
2021-06-06 09:41:35 +08:00
|
|
|
log.Debug("IndexService DropIndex", zap.Any("IndexID", req.IndexID))
|
2021-02-23 11:57:18 +08:00
|
|
|
|
2021-04-16 15:37:13 +08:00
|
|
|
ret := &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_Success,
|
|
|
|
}
|
2021-02-23 11:57:18 +08:00
|
|
|
err := i.metaTable.MarkIndexAsDeleted(req.IndexID)
|
|
|
|
if err != nil {
|
2021-04-16 15:37:13 +08:00
|
|
|
ret.ErrorCode = commonpb.ErrorCode_UnexpectedError
|
|
|
|
ret.Reason = err.Error()
|
|
|
|
return ret, nil
|
2021-02-23 11:57:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
2021-04-16 15:37:13 +08:00
|
|
|
go func() {
|
|
|
|
unissuedIndexBuildIDs := i.sched.IndexAddQueue.tryToRemoveUselessIndexAddTask(req.IndexID)
|
|
|
|
for _, indexBuildID := range unissuedIndexBuildIDs {
|
|
|
|
i.metaTable.DeleteIndex(indexBuildID)
|
|
|
|
}
|
|
|
|
}()
|
2021-02-23 11:57:18 +08:00
|
|
|
}()
|
|
|
|
|
2021-06-06 09:41:35 +08:00
|
|
|
log.Debug("IndexService DropIndex success", zap.Any("IndexID", req.IndexID))
|
2021-04-16 15:37:13 +08:00
|
|
|
return ret, nil
|
2021-02-23 11:57:18 +08:00
|
|
|
}
|
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
func (i *IndexService) GetIndexFilePaths(ctx context.Context, req *indexpb.GetIndexFilePathsRequest) (*indexpb.GetIndexFilePathsResponse, error) {
|
2021-06-06 09:41:35 +08:00
|
|
|
log.Debug("IndexService GetIndexFilePaths", zap.Int64s("IndexBuildIds", req.IndexBuildIDs))
|
2021-02-07 17:02:13 +08:00
|
|
|
var indexPaths []*indexpb.IndexFilePathInfo = nil
|
2021-01-15 14:38:36 +08:00
|
|
|
|
2021-02-02 19:56:04 +08:00
|
|
|
for _, indexID := range req.IndexBuildIDs {
|
2021-02-07 17:02:13 +08:00
|
|
|
indexPathInfo, err := i.metaTable.GetIndexFilePathInfo(indexID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-26 09:38:40 +08:00
|
|
|
indexPaths = append(indexPaths, indexPathInfo)
|
|
|
|
}
|
2021-06-06 09:41:35 +08:00
|
|
|
log.Debug("IndexService GetIndexFilePaths success")
|
2021-01-19 18:32:57 +08:00
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
ret := &indexpb.GetIndexFilePathsResponse{
|
2021-01-26 09:38:40 +08:00
|
|
|
Status: &commonpb.Status{
|
2021-03-10 22:06:22 +08:00
|
|
|
ErrorCode: commonpb.ErrorCode_Success,
|
2021-01-26 09:38:40 +08:00
|
|
|
},
|
|
|
|
FilePaths: indexPaths,
|
2021-01-19 18:32:57 +08:00
|
|
|
}
|
2021-06-06 09:41:35 +08:00
|
|
|
log.Debug("IndexService GetIndexFilePaths ", zap.Any("FilePaths", ret.FilePaths))
|
2021-03-26 10:01:08 +08:00
|
|
|
|
2021-01-26 09:38:40 +08:00
|
|
|
return ret, nil
|
|
|
|
}
|
2021-01-19 18:32:57 +08:00
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
func (i *IndexService) tsLoop() {
|
2021-02-24 17:12:06 +08:00
|
|
|
tsoTicker := time.NewTicker(tso.UpdateTimestampStep)
|
2021-02-08 14:20:29 +08:00
|
|
|
defer tsoTicker.Stop()
|
|
|
|
ctx, cancel := context.WithCancel(i.loopCtx)
|
|
|
|
defer cancel()
|
|
|
|
defer i.loopWg.Done()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-tsoTicker.C:
|
|
|
|
if err := i.idAllocator.UpdateID(); err != nil {
|
2021-06-06 09:41:35 +08:00
|
|
|
log.Debug("IndexService tsLoop UpdateID failed", zap.Error(err))
|
2021-02-08 14:20:29 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
case <-ctx.Done():
|
|
|
|
// Server is closed and it should return nil.
|
2021-06-06 09:41:35 +08:00
|
|
|
log.Debug("IndexService tsLoop is closed")
|
2021-02-08 14:20:29 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-16 15:37:13 +08:00
|
|
|
|
2021-05-27 22:24:29 +08:00
|
|
|
func (i *IndexService) recycleUnusedIndexFiles() {
|
2021-04-16 15:37:13 +08:00
|
|
|
ctx, cancel := context.WithCancel(i.loopCtx)
|
|
|
|
|
|
|
|
defer cancel()
|
|
|
|
defer i.loopWg.Done()
|
|
|
|
|
|
|
|
timeTicker := time.NewTicker(durationInterval)
|
2021-06-06 09:41:35 +08:00
|
|
|
log.Debug("IndexService start recycleUnusedIndexFiles loop")
|
2021-04-16 15:37:13 +08:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case <-timeTicker.C:
|
2021-05-27 22:24:29 +08:00
|
|
|
metas := i.metaTable.GetUnusedIndexFiles(recycleIndexLimit)
|
|
|
|
for _, meta := range metas {
|
|
|
|
if meta.indexMeta.MarkDeleted {
|
|
|
|
unusedIndexFilePathPrefix := strconv.Itoa(int(meta.indexMeta.IndexBuildID))
|
|
|
|
if err := i.kv.RemoveWithPrefix(unusedIndexFilePathPrefix); err != nil {
|
2021-06-06 09:41:35 +08:00
|
|
|
log.Debug("IndexService recycleUnusedIndexFiles Remove index files failed",
|
|
|
|
zap.Any("MarkDeleted", true), zap.Error(err))
|
2021-05-27 22:24:29 +08:00
|
|
|
}
|
|
|
|
i.metaTable.DeleteIndex(meta.indexMeta.IndexBuildID)
|
|
|
|
} else {
|
|
|
|
for j := 1; j < int(meta.indexMeta.Version); j++ {
|
|
|
|
unusedIndexFilePathPrefix := strconv.Itoa(int(meta.indexMeta.IndexBuildID)) + "/" + strconv.Itoa(j)
|
|
|
|
if err := i.kv.RemoveWithPrefix(unusedIndexFilePathPrefix); err != nil {
|
2021-06-06 09:41:35 +08:00
|
|
|
log.Debug("IndexService recycleUnusedIndexFiles Remove index files failed",
|
|
|
|
zap.Any("MarkDeleted", false), zap.Error(err))
|
2021-05-27 22:24:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := i.metaTable.UpdateRecycleState(meta.indexMeta.IndexBuildID); err != nil {
|
2021-06-06 09:41:35 +08:00
|
|
|
log.Debug("IndexService recycleUnusedIndexFiles UpdateRecycleState failed", zap.Error(err))
|
2021-05-27 22:24:29 +08:00
|
|
|
}
|
2021-04-16 15:37:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-27 22:24:29 +08:00
|
|
|
|
|
|
|
func (i *IndexService) assignmentTasksLoop() {
|
|
|
|
ctx, cancel := context.WithCancel(i.loopCtx)
|
|
|
|
|
|
|
|
defer cancel()
|
|
|
|
defer i.loopWg.Done()
|
|
|
|
|
2021-06-06 09:41:35 +08:00
|
|
|
log.Debug("IndexService start assignmentTasksLoop start")
|
2021-05-27 22:24:29 +08:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case indexBuildIDs := <-i.assignChan:
|
|
|
|
for _, indexBuildID := range indexBuildIDs {
|
|
|
|
meta := i.metaTable.GetIndexMeta(indexBuildID)
|
2021-06-06 09:41:35 +08:00
|
|
|
log.Debug("IndexService assignmentTasksLoop ", zap.Any("Meta", meta))
|
2021-05-27 22:24:29 +08:00
|
|
|
if meta.indexMeta.State == commonpb.IndexState_Finished {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := i.metaTable.UpdateVersion(indexBuildID); err != nil {
|
2021-06-06 09:41:35 +08:00
|
|
|
log.Debug("IndexService assignmentTasksLoop metaTable.UpdateVersion failed", zap.Error(err))
|
2021-05-27 22:24:29 +08:00
|
|
|
}
|
2021-05-31 10:32:30 +08:00
|
|
|
nodeID, builderClient := i.nodeClients.PeekClient()
|
2021-05-27 22:24:29 +08:00
|
|
|
if builderClient == nil {
|
2021-06-06 09:41:35 +08:00
|
|
|
log.Debug("IndexService assignmentTasksLoop can not find available IndexNode")
|
2021-05-27 22:24:29 +08:00
|
|
|
i.assignChan <- []UniqueID{indexBuildID}
|
|
|
|
continue
|
|
|
|
}
|
2021-06-11 16:53:42 +08:00
|
|
|
i.nodeTasks.assignTask(nodeID, indexBuildID)
|
2021-05-27 22:24:29 +08:00
|
|
|
req := &indexpb.CreateIndexRequest{
|
|
|
|
IndexBuildID: indexBuildID,
|
|
|
|
IndexName: meta.indexMeta.Req.IndexName,
|
|
|
|
IndexID: meta.indexMeta.Req.IndexID,
|
|
|
|
Version: meta.indexMeta.Version + 1,
|
|
|
|
MetaPath: "/indexes/" + strconv.FormatInt(indexBuildID, 10),
|
|
|
|
DataPaths: meta.indexMeta.Req.DataPaths,
|
|
|
|
TypeParams: meta.indexMeta.Req.TypeParams,
|
|
|
|
IndexParams: meta.indexMeta.Req.IndexParams,
|
|
|
|
}
|
|
|
|
resp, err := builderClient.CreateIndex(ctx, req)
|
|
|
|
if err != nil {
|
2021-06-06 09:41:35 +08:00
|
|
|
log.Debug("IndexService assignmentTasksLoop builderClient.CreateIndex failed", zap.Error(err))
|
2021-06-11 16:53:42 +08:00
|
|
|
continue
|
2021-05-27 22:24:29 +08:00
|
|
|
}
|
|
|
|
if resp.ErrorCode != commonpb.ErrorCode_Success {
|
2021-06-06 09:41:35 +08:00
|
|
|
log.Debug("IndexService assignmentTasksLoop builderClient.CreateIndex failed", zap.String("Reason", resp.Reason))
|
2021-06-11 16:53:42 +08:00
|
|
|
continue
|
2021-06-06 09:41:35 +08:00
|
|
|
}
|
|
|
|
if err = i.metaTable.BuildIndex(indexBuildID, nodeID); err != nil {
|
|
|
|
log.Debug("IndexService assignmentTasksLoop metaTable.BuildIndex failed", zap.Error(err))
|
2021-05-27 22:24:29 +08:00
|
|
|
}
|
|
|
|
i.nodeClients.IncPriority(nodeID, 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IndexService) watchNodeLoop() {
|
|
|
|
ctx, cancel := context.WithCancel(i.loopCtx)
|
|
|
|
|
|
|
|
defer cancel()
|
|
|
|
defer i.loopWg.Done()
|
2021-06-06 09:41:35 +08:00
|
|
|
log.Debug("IndexService watchNodeLoop start")
|
2021-05-27 22:24:29 +08:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case event := <-i.eventChan:
|
|
|
|
switch event.EventType {
|
|
|
|
case sessionutil.SessionAddEvent:
|
|
|
|
serverID := event.Session.ServerID
|
2021-06-06 09:41:35 +08:00
|
|
|
log.Debug("IndexService watchNodeLoop SessionAddEvent", zap.Any("serverID", serverID))
|
2021-05-27 22:24:29 +08:00
|
|
|
case sessionutil.SessionDelEvent:
|
|
|
|
serverID := event.Session.ServerID
|
2021-06-11 16:53:42 +08:00
|
|
|
i.removeNode(serverID)
|
2021-06-06 09:41:35 +08:00
|
|
|
log.Debug("IndexService watchNodeLoop SessionDelEvent ", zap.Any("serverID", serverID))
|
2021-05-31 10:32:30 +08:00
|
|
|
indexBuildIDs := i.nodeTasks.getTasksByNodeID(serverID)
|
2021-06-11 16:53:42 +08:00
|
|
|
log.Debug("IndexNode crashed", zap.Any("IndexNode ID", serverID), zap.Any("task IDs", indexBuildIDs))
|
2021-05-27 22:24:29 +08:00
|
|
|
i.assignChan <- indexBuildIDs
|
|
|
|
i.nodeTasks.delete(serverID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IndexService) watchMetaLoop() {
|
|
|
|
ctx, cancel := context.WithCancel(i.loopCtx)
|
|
|
|
|
|
|
|
defer cancel()
|
|
|
|
defer i.loopWg.Done()
|
2021-06-06 09:41:35 +08:00
|
|
|
log.Debug("IndexService watchMetaLoop start")
|
2021-05-27 22:24:29 +08:00
|
|
|
|
|
|
|
watchChan := i.metaTable.client.WatchWithPrefix("indexes")
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case resp := <-watchChan:
|
2021-06-06 09:41:35 +08:00
|
|
|
log.Debug("IndexService watchMetaLoop find meta updated.")
|
2021-05-27 22:24:29 +08:00
|
|
|
for _, event := range resp.Events {
|
|
|
|
eventRevision := event.Kv.Version
|
|
|
|
indexMeta := &indexpb.IndexMeta{}
|
|
|
|
err := proto.UnmarshalText(string(event.Kv.Value), indexMeta)
|
|
|
|
indexBuildID := indexMeta.IndexBuildID
|
2021-06-06 09:41:35 +08:00
|
|
|
log.Debug("IndexService watchMetaLoop", zap.Any("event.Key", event.Kv.Key),
|
|
|
|
zap.Any("event.V", indexMeta), zap.Any("IndexBuildID", indexBuildID), zap.Error(err))
|
2021-05-27 22:24:29 +08:00
|
|
|
switch event.Type {
|
|
|
|
case mvccpb.PUT:
|
|
|
|
//TODO: get indexBuildID fast
|
|
|
|
reload := i.metaTable.LoadMetaFromETCD(indexBuildID, eventRevision)
|
2021-06-06 09:41:35 +08:00
|
|
|
log.Debug("IndexService watchMetaLoop PUT", zap.Any("IndexBuildID", indexBuildID), zap.Any("reload", reload))
|
2021-05-27 22:24:29 +08:00
|
|
|
if reload {
|
|
|
|
i.nodeTasks.finishTask(indexBuildID)
|
|
|
|
}
|
|
|
|
case mvccpb.DELETE:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IndexService) assignTasksServerStart() error {
|
|
|
|
sessions, _, err := i.session.GetSessions(typeutil.IndexNodeRole)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-11 16:52:09 +08:00
|
|
|
for _, session := range sessions {
|
|
|
|
addrs := strings.Split(session.Address, ":")
|
|
|
|
ip := addrs[0]
|
|
|
|
port, err := strconv.ParseInt(addrs[1], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &indexpb.RegisterNodeRequest{
|
|
|
|
Address: &commonpb.Address{
|
|
|
|
Ip: ip,
|
|
|
|
Port: port,
|
|
|
|
},
|
|
|
|
NodeID: session.ServerID,
|
|
|
|
}
|
|
|
|
if err = i.addNode(session.ServerID, req); err != nil {
|
|
|
|
log.Debug("IndexService", zap.Any("IndexService start find node fatal, err = ", err))
|
|
|
|
}
|
|
|
|
}
|
2021-05-27 22:24:29 +08:00
|
|
|
var serverIDs []int64
|
|
|
|
for _, session := range sessions {
|
|
|
|
serverIDs = append(serverIDs, session.ServerID)
|
|
|
|
}
|
|
|
|
tasks := i.metaTable.GetUnassignedTasks(serverIDs)
|
|
|
|
for _, taskQueue := range tasks {
|
|
|
|
i.assignChan <- taskQueue
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|