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-02-07 17:02:13 +08:00
|
|
|
"fmt"
|
2021-01-19 18:32:57 +08:00
|
|
|
"log"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2021-02-24 17:12:06 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/allocator"
|
2021-01-19 18:32:57 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/kv"
|
|
|
|
etcdkv "github.com/zilliztech/milvus-distributed/internal/kv/etcd"
|
2021-01-26 09:38:40 +08:00
|
|
|
miniokv "github.com/zilliztech/milvus-distributed/internal/kv/minio"
|
2021-03-08 10:09:48 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/tso"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/util/retry"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/util/tsoutil"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/util/typeutil"
|
|
|
|
"go.etcd.io/etcd/clientv3"
|
|
|
|
|
2021-01-19 18:32:57 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/commonpb"
|
2021-01-15 14:38:36 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/indexpb"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/internalpb2"
|
2021-02-26 17:44:24 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/milvuspb"
|
2021-01-26 09:38:40 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
reqTimeoutInterval = time.Second * 10
|
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-01-26 19:24:09 +08:00
|
|
|
nodeStates map[UniqueID]*internalpb2.ComponentStates
|
2021-01-29 17:08:31 +08:00
|
|
|
stateCode internalpb2.StateCode
|
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-01-26 09:38:40 +08:00
|
|
|
sched *TaskScheduler
|
2021-01-19 18:32:57 +08:00
|
|
|
|
2021-02-24 17:12:06 +08:00
|
|
|
idAllocator *allocator.GlobalIDAllocator
|
2021-01-15 14:38:36 +08:00
|
|
|
|
2021-01-19 18:32:57 +08:00
|
|
|
kv kv.Base
|
|
|
|
|
|
|
|
metaTable *metaTable
|
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-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-01-29 17:08:31 +08:00
|
|
|
return i, nil
|
|
|
|
}
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
func (i *IndexService) Init() error {
|
2021-01-26 09:38:40 +08:00
|
|
|
etcdAddress := Params.EtcdAddress
|
|
|
|
log.Println("etcd address = ", etcdAddress)
|
|
|
|
connectEtcdFn := func() error {
|
|
|
|
etcdClient, err := clientv3.New(clientv3.Config{Endpoints: []string{etcdAddress}})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
etcdKV := etcdkv.NewEtcdKV(etcdClient, Params.MetaRootPath)
|
|
|
|
metakv, err := NewMetaTable(etcdKV)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
i.metaTable = metakv
|
|
|
|
return nil
|
|
|
|
}
|
2021-02-26 15:17:47 +08:00
|
|
|
err := retry.Retry(200, time.Millisecond*200, connectEtcdFn)
|
2021-01-26 09:38:40 +08:00
|
|
|
if err != nil {
|
2021-01-29 17:08:31 +08:00
|
|
|
return err
|
2021-01-26 09:38:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//init idAllocator
|
|
|
|
kvRootPath := Params.KvRootPath
|
2021-02-24 17:12:06 +08:00
|
|
|
i.idAllocator = allocator.NewGlobalIDAllocator("idTimestamp", tsoutil.NewTSOKVBase([]string{etcdAddress}, kvRootPath, "index_gid"))
|
2021-01-26 09:38:40 +08:00
|
|
|
if err := i.idAllocator.Initialize(); err != nil {
|
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-01-29 17:08:31 +08:00
|
|
|
return err
|
2021-01-26 09:38:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
i.sched, err = NewTaskScheduler(i.loopCtx, i.idAllocator, i.kv, i.metaTable)
|
|
|
|
if err != nil {
|
2021-01-29 17:08:31 +08:00
|
|
|
return err
|
2021-01-26 09:38:40 +08:00
|
|
|
}
|
2021-01-29 17:08:31 +08:00
|
|
|
i.UpdateStateCode(internalpb2.StateCode_HEALTHY)
|
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-01-26 09:38:40 +08:00
|
|
|
i.sched.Start()
|
|
|
|
// Start callbacks
|
|
|
|
for _, cb := range i.startCallbacks {
|
|
|
|
cb()
|
|
|
|
}
|
2021-03-05 16:52:45 +08:00
|
|
|
log.Print("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-05 16:52:45 +08:00
|
|
|
func (i *IndexService) UpdateStateCode(code internalpb2.StateCode) {
|
2021-01-29 17:08:31 +08:00
|
|
|
i.stateCode = code
|
|
|
|
}
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
func (i *IndexService) GetComponentStates(ctx context.Context) (*internalpb2.ComponentStates, error) {
|
2021-01-26 19:24:09 +08:00
|
|
|
|
|
|
|
stateInfo := &internalpb2.ComponentInfo{
|
|
|
|
NodeID: i.ID,
|
2021-03-05 16:52:45 +08:00
|
|
|
Role: "IndexService",
|
2021-01-29 17:08:31 +08:00
|
|
|
StateCode: i.stateCode,
|
2021-01-26 19:24:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ret := &internalpb2.ComponentStates{
|
|
|
|
State: stateInfo,
|
|
|
|
SubcomponentStates: nil, // todo add subcomponents states
|
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_SUCCESS,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
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-02-26 17:44:24 +08:00
|
|
|
return &milvuspb.StringResponse{
|
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_SUCCESS,
|
|
|
|
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-02-26 17:44:24 +08:00
|
|
|
return &milvuspb.StringResponse{
|
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_SUCCESS,
|
|
|
|
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-02-07 17:02:13 +08:00
|
|
|
fmt.Println("builder building index ..., indexName = ", req.IndexName, "indexID = ", req.IndexID, "dataPath = ", req.DataPaths)
|
2021-01-26 09:38:40 +08:00
|
|
|
ret := &indexpb.BuildIndexResponse{
|
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
|
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 {
|
|
|
|
ret.Status.ErrorCode = commonpb.ErrorCode_UNEXPECTED_ERROR
|
|
|
|
ret.Status.Reason = err.Error()
|
|
|
|
return ret, nil
|
|
|
|
}
|
2021-01-20 18:26:20 +08:00
|
|
|
|
2021-01-26 09:38:40 +08:00
|
|
|
err = t.WaitToFinish()
|
|
|
|
if err != nil {
|
|
|
|
ret.Status.ErrorCode = commonpb.ErrorCode_UNEXPECTED_ERROR
|
|
|
|
ret.Status.Reason = err.Error()
|
|
|
|
return ret, nil
|
|
|
|
}
|
2021-02-08 14:20:29 +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-05 16:52:45 +08:00
|
|
|
func (i *IndexService) GetIndexStates(ctx context.Context, req *indexpb.IndexStatesRequest) (*indexpb.IndexStatesResponse, error) {
|
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-01-19 18:32:57 +08:00
|
|
|
ret := &indexpb.IndexStatesResponse{
|
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_SUCCESS,
|
|
|
|
},
|
2021-01-20 15:02:23 +08:00
|
|
|
States: indexStates,
|
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-02-23 11:57:18 +08:00
|
|
|
i.sched.IndexAddQueue.tryToRemoveUselessIndexAddTask(req.IndexID)
|
|
|
|
|
|
|
|
err := i.metaTable.MarkIndexAsDeleted(req.IndexID)
|
|
|
|
if err != nil {
|
|
|
|
return &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
|
|
|
|
Reason: err.Error(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
go func() {
|
|
|
|
allNodeClients := i.nodeClients.PeekAllClients()
|
|
|
|
for _, client := range allNodeClients {
|
2021-02-26 17:44:24 +08:00
|
|
|
client.DropIndex(ctx, req)
|
2021-02-23 11:57:18 +08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
i.metaTable.removeIndexFile(req.IndexID)
|
|
|
|
i.metaTable.removeMeta(req.IndexID)
|
|
|
|
}()
|
|
|
|
}()
|
|
|
|
|
|
|
|
return &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_SUCCESS,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
func (i *IndexService) GetIndexFilePaths(ctx context.Context, req *indexpb.IndexFilePathsRequest) (*indexpb.IndexFilePathsResponse, error) {
|
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-01-19 18:32:57 +08:00
|
|
|
|
2021-01-26 09:38:40 +08:00
|
|
|
ret := &indexpb.IndexFilePathsResponse{
|
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_SUCCESS,
|
|
|
|
},
|
|
|
|
FilePaths: indexPaths,
|
2021-01-19 18:32:57 +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) NotifyBuildIndex(ctx context.Context, nty *indexpb.BuildIndexNotification) (*commonpb.Status, error) {
|
2021-01-26 09:38:40 +08:00
|
|
|
ret := &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_SUCCESS,
|
2021-01-19 18:32:57 +08:00
|
|
|
}
|
2021-02-27 10:45:03 +08:00
|
|
|
log.Println("[IndexService][NotifyBuildIndex]", nty.String())
|
2021-01-26 09:38:40 +08:00
|
|
|
if err := i.metaTable.NotifyBuildIndex(nty); err != nil {
|
|
|
|
ret.ErrorCode = commonpb.ErrorCode_BUILD_INDEX_ERROR
|
|
|
|
ret.Reason = err.Error()
|
2021-02-27 10:45:03 +08:00
|
|
|
log.Println("[IndexService][NotifyBuildIndex][metaTable][NotifyBuildIndex]", err)
|
|
|
|
|
2021-01-19 18:32:57 +08:00
|
|
|
}
|
2021-01-26 09:38:40 +08:00
|
|
|
i.nodeClients.IncPriority(nty.NodeID, -1)
|
|
|
|
return ret, nil
|
2021-01-15 14:38:36 +08:00
|
|
|
}
|
2021-02-08 14:20:29 +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 {
|
|
|
|
log.Println("failed to update id", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case <-ctx.Done():
|
|
|
|
// Server is closed and it should return nil.
|
|
|
|
log.Println("tsLoop is closed")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|