2021-01-15 14:38:36 +08:00
|
|
|
package indexnode
|
2020-12-10 17:55:55 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-03-08 10:09:48 +08:00
|
|
|
"errors"
|
2021-02-25 17:35:36 +08:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2021-03-08 15:25:55 +08:00
|
|
|
"math/rand"
|
2020-12-10 17:55:55 +08:00
|
|
|
"time"
|
|
|
|
|
2021-03-10 09:56:09 +08:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2021-02-25 17:35:36 +08:00
|
|
|
"github.com/opentracing/opentracing-go"
|
|
|
|
"github.com/uber/jaeger-client-go/config"
|
2021-01-19 18:32:57 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/kv"
|
|
|
|
miniokv "github.com/zilliztech/milvus-distributed/internal/kv/minio"
|
2021-03-10 09:56:09 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/log"
|
2021-01-18 19:32:08 +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"
|
2021-01-26 19:24:09 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/internalpb2"
|
2021-03-08 10:09:48 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/milvuspb"
|
2021-03-10 09:56:09 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/types"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/util/funcutil"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/util/typeutil"
|
2021-01-26 09:38:40 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
reqTimeoutInterval = time.Second * 10
|
2020-12-10 17:55:55 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type UniqueID = typeutil.UniqueID
|
2020-12-13 06:48:05 +08:00
|
|
|
type Timestamp = typeutil.Timestamp
|
2020-12-10 17:55:55 +08:00
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
type IndexNode struct {
|
2021-01-29 17:08:31 +08:00
|
|
|
stateCode internalpb2.StateCode
|
2021-01-26 19:24:09 +08:00
|
|
|
|
2021-01-19 18:32:57 +08:00
|
|
|
loopCtx context.Context
|
|
|
|
loopCancel func()
|
|
|
|
|
2021-01-26 09:38:40 +08:00
|
|
|
sched *TaskScheduler
|
2021-01-19 18:32:57 +08:00
|
|
|
|
|
|
|
kv kv.Base
|
|
|
|
|
2021-03-08 10:09:48 +08:00
|
|
|
serviceClient types.IndexService // method factory
|
2021-01-20 18:26:20 +08:00
|
|
|
|
2020-12-10 17:55:55 +08:00
|
|
|
// Add callback functions at different stages
|
|
|
|
startCallbacks []func()
|
|
|
|
closeCallbacks []func()
|
2021-02-25 17:35:36 +08:00
|
|
|
|
|
|
|
closer io.Closer
|
2020-12-10 17:55:55 +08:00
|
|
|
}
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
func NewIndexNode(ctx context.Context) (*IndexNode, error) {
|
2021-03-08 15:25:55 +08:00
|
|
|
rand.Seed(time.Now().UnixNano())
|
2020-12-10 17:55:55 +08:00
|
|
|
ctx1, cancel := context.WithCancel(ctx)
|
2021-03-05 16:52:45 +08:00
|
|
|
b := &IndexNode{
|
2020-12-10 17:55:55 +08:00
|
|
|
loopCtx: ctx1,
|
|
|
|
loopCancel: cancel,
|
|
|
|
}
|
2021-01-29 17:08:31 +08:00
|
|
|
var err error
|
|
|
|
b.sched, err = NewTaskScheduler(b.loopCtx, b.kv)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
func (i *IndexNode) Init() error {
|
2021-02-26 17:44:24 +08:00
|
|
|
ctx := context.Background()
|
2021-03-06 11:52:43 +08:00
|
|
|
err := funcutil.WaitForComponentHealthy(ctx, i.serviceClient, "IndexService", 100, time.Millisecond*200)
|
2021-01-29 17:08:31 +08:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
request := &indexpb.RegisterNodeRequest{
|
|
|
|
Base: nil,
|
|
|
|
Address: &commonpb.Address{
|
|
|
|
Ip: Params.IP,
|
|
|
|
Port: int64(Params.Port),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-02-26 17:44:24 +08:00
|
|
|
resp, err2 := i.serviceClient.RegisterNode(ctx, request)
|
2021-01-29 17:08:31 +08:00
|
|
|
if err2 != nil {
|
2021-03-10 09:56:09 +08:00
|
|
|
log.Debug("indexnode", zap.String("Index NodeImpl connect to IndexService failed", err.Error()))
|
2021-01-29 17:08:31 +08:00
|
|
|
return err2
|
|
|
|
}
|
|
|
|
|
2021-03-10 22:06:22 +08:00
|
|
|
if resp.Status.ErrorCode != commonpb.ErrorCode_Success {
|
2021-01-29 17:08:31 +08:00
|
|
|
return errors.New(resp.Status.Reason)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = Params.LoadConfigFromInitParams(resp.InitParams)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-10 17:55:55 +08:00
|
|
|
|
2021-02-25 17:35:36 +08:00
|
|
|
// TODO
|
|
|
|
cfg := &config.Configuration{
|
|
|
|
ServiceName: fmt.Sprintf("index_node_%d", Params.NodeID),
|
|
|
|
Sampler: &config.SamplerConfig{
|
|
|
|
Type: "const",
|
|
|
|
Param: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
tracer, closer, err := cfg.NewTracer()
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("ERROR: cannot init Jaeger: %v\n", err))
|
|
|
|
}
|
|
|
|
opentracing.SetGlobalTracer(tracer)
|
|
|
|
i.closer = closer
|
|
|
|
|
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,
|
2020-12-30 18:50:56 +08:00
|
|
|
}
|
2021-02-26 15:17:47 +08:00
|
|
|
i.kv, err = miniokv.NewMinIOKV(i.loopCtx, option)
|
2020-12-10 17:55:55 +08:00
|
|
|
if err != nil {
|
2021-01-29 17:08:31 +08:00
|
|
|
return err
|
2020-12-10 17:55:55 +08:00
|
|
|
}
|
|
|
|
|
2021-03-10 15:27:26 +08:00
|
|
|
i.UpdateStateCode(internalpb2.StateCode_Healthy)
|
2020-12-10 17:55:55 +08:00
|
|
|
|
2021-01-26 09:38:40 +08:00
|
|
|
return nil
|
|
|
|
}
|
2020-12-10 17:55:55 +08:00
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
func (i *IndexNode) Start() error {
|
2021-01-26 09:38:40 +08:00
|
|
|
i.sched.Start()
|
2020-12-10 17:55:55 +08:00
|
|
|
|
|
|
|
// Start callbacks
|
2021-01-26 09:38:40 +08:00
|
|
|
for _, cb := range i.startCallbacks {
|
2020-12-10 17:55:55 +08:00
|
|
|
cb()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-26 09:38:40 +08:00
|
|
|
// Close closes the server.
|
2021-03-05 16:52:45 +08:00
|
|
|
func (i *IndexNode) Stop() error {
|
2021-02-25 17:35:36 +08:00
|
|
|
if err := i.closer.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-29 17:08:31 +08:00
|
|
|
i.loopCancel()
|
|
|
|
if i.sched != nil {
|
|
|
|
i.sched.Close()
|
|
|
|
}
|
2021-01-26 09:38:40 +08:00
|
|
|
for _, cb := range i.closeCallbacks {
|
|
|
|
cb()
|
2020-12-10 17:55:55 +08:00
|
|
|
}
|
2021-03-10 09:56:09 +08:00
|
|
|
log.Debug("NodeImpl closed.")
|
2021-01-26 09:38:40 +08:00
|
|
|
return nil
|
2020-12-10 17:55:55 +08:00
|
|
|
}
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
func (i *IndexNode) UpdateStateCode(code internalpb2.StateCode) {
|
2021-01-29 17:08:31 +08:00
|
|
|
i.stateCode = code
|
|
|
|
}
|
|
|
|
|
2021-03-08 10:09:48 +08:00
|
|
|
func (i *IndexNode) SetIndexServiceClient(serviceClient types.IndexService) {
|
2021-01-26 09:38:40 +08:00
|
|
|
i.serviceClient = serviceClient
|
2020-12-10 17:55:55 +08:00
|
|
|
}
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
func (i *IndexNode) BuildIndex(ctx context.Context, request *indexpb.BuildIndexCmd) (*commonpb.Status, error) {
|
2021-02-23 09:58:06 +08:00
|
|
|
t := &IndexBuildTask{
|
|
|
|
BaseTask: BaseTask{
|
|
|
|
ctx: ctx,
|
2021-02-27 10:45:03 +08:00
|
|
|
done: make(chan error),
|
2021-02-23 09:58:06 +08:00
|
|
|
},
|
|
|
|
cmd: request,
|
|
|
|
kv: i.kv,
|
|
|
|
serviceClient: i.serviceClient,
|
|
|
|
nodeID: Params.NodeID,
|
|
|
|
}
|
2020-12-13 06:48:05 +08:00
|
|
|
|
2021-01-26 09:38:40 +08:00
|
|
|
ret := &commonpb.Status{
|
2021-03-10 22:06:22 +08:00
|
|
|
ErrorCode: commonpb.ErrorCode_Success,
|
2020-12-10 17:55:55 +08:00
|
|
|
}
|
|
|
|
|
2021-02-27 10:45:03 +08:00
|
|
|
err := i.sched.IndexBuildQueue.Enqueue(t)
|
2021-01-26 09:38:40 +08:00
|
|
|
if err != nil {
|
2021-03-10 22:06:22 +08:00
|
|
|
ret.ErrorCode = commonpb.ErrorCode_UnexpectedError
|
2021-01-26 09:38:40 +08:00
|
|
|
ret.Reason = err.Error()
|
|
|
|
return ret, nil
|
2020-12-10 17:55:55 +08:00
|
|
|
}
|
2021-03-10 09:56:09 +08:00
|
|
|
log.Debug("indexnode", zap.Int64("indexnode successfully schedule with indexBuildID", request.IndexBuildID))
|
2021-01-26 09:38:40 +08:00
|
|
|
return ret, nil
|
2020-12-10 17:55:55 +08:00
|
|
|
}
|
2021-01-26 19:24:09 +08:00
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
func (i *IndexNode) DropIndex(ctx context.Context, request *indexpb.DropIndexRequest) (*commonpb.Status, error) {
|
2021-02-23 11:57:18 +08:00
|
|
|
i.sched.IndexBuildQueue.tryToRemoveUselessIndexBuildTask(request.IndexID)
|
|
|
|
return &commonpb.Status{
|
2021-03-10 22:06:22 +08:00
|
|
|
ErrorCode: commonpb.ErrorCode_Success,
|
2021-02-23 11:57:18 +08:00
|
|
|
Reason: "",
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-01-29 17:08:31 +08:00
|
|
|
// AddStartCallback adds a callback in the startServer phase.
|
2021-03-05 16:52:45 +08:00
|
|
|
func (i *IndexNode) AddStartCallback(callbacks ...func()) {
|
2021-01-29 17:08:31 +08:00
|
|
|
i.startCallbacks = append(i.startCallbacks, callbacks...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddCloseCallback adds a callback in the Close phase.
|
2021-03-05 16:52:45 +08:00
|
|
|
func (i *IndexNode) AddCloseCallback(callbacks ...func()) {
|
2021-01-29 17:08:31 +08:00
|
|
|
i.closeCallbacks = append(i.closeCallbacks, callbacks...)
|
|
|
|
}
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
func (i *IndexNode) GetComponentStates(ctx context.Context) (*internalpb2.ComponentStates, error) {
|
2021-01-26 19:24:09 +08:00
|
|
|
|
|
|
|
stateInfo := &internalpb2.ComponentInfo{
|
|
|
|
NodeID: Params.NodeID,
|
2021-01-29 17:08:31 +08:00
|
|
|
Role: "NodeImpl",
|
|
|
|
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{
|
2021-03-10 22:06:22 +08:00
|
|
|
ErrorCode: commonpb.ErrorCode_Success,
|
2021-01-26 19:24:09 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
func (i *IndexNode) GetTimeTickChannel(ctx context.Context) (*milvuspb.StringResponse, error) {
|
2021-02-04 19:34:35 +08:00
|
|
|
return &milvuspb.StringResponse{
|
|
|
|
Status: &commonpb.Status{
|
2021-03-10 22:06:22 +08:00
|
|
|
ErrorCode: commonpb.ErrorCode_Success,
|
2021-02-04 19:34:35 +08:00
|
|
|
},
|
|
|
|
}, nil
|
2021-01-26 19:24:09 +08:00
|
|
|
}
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
func (i *IndexNode) GetStatisticsChannel(ctx context.Context) (*milvuspb.StringResponse, error) {
|
2021-02-04 19:34:35 +08:00
|
|
|
return &milvuspb.StringResponse{
|
|
|
|
Status: &commonpb.Status{
|
2021-03-10 22:06:22 +08:00
|
|
|
ErrorCode: commonpb.ErrorCode_Success,
|
2021-02-04 19:34:35 +08:00
|
|
|
},
|
|
|
|
}, nil
|
2021-01-26 19:24:09 +08:00
|
|
|
}
|