2021-01-15 14:38:36 +08:00
|
|
|
package indexnode
|
2020-12-10 17:55:55 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
2021-02-04 19:34:35 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/milvuspb"
|
|
|
|
|
2021-01-26 09:38:40 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/errors"
|
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-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-01-29 17:08:31 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/util/funcutil"
|
2021-01-26 09:38:40 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/util/retry"
|
2020-12-10 17:55:55 +08:00
|
|
|
"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-01-29 17:08:31 +08:00
|
|
|
type NodeImpl struct {
|
|
|
|
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-01-26 09:38:40 +08:00
|
|
|
serviceClient typeutil.IndexServiceInterface // 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-01-29 17:08:31 +08:00
|
|
|
func NewNodeImpl(ctx context.Context) (*NodeImpl, error) {
|
2020-12-10 17:55:55 +08:00
|
|
|
ctx1, cancel := context.WithCancel(ctx)
|
2021-01-29 17:08:31 +08:00
|
|
|
b := &NodeImpl{
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *NodeImpl) Init() error {
|
|
|
|
log.Println("AAAAAAAAAAAAAAAAA", i.serviceClient)
|
2021-02-23 11:40:30 +08:00
|
|
|
err := funcutil.WaitForComponentHealthy(i.serviceClient, "IndexService", 10, time.Second)
|
2021-01-29 17:08:31 +08:00
|
|
|
log.Println("BBBBBBBBB", i.serviceClient)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
request := &indexpb.RegisterNodeRequest{
|
|
|
|
Base: nil,
|
|
|
|
Address: &commonpb.Address{
|
|
|
|
Ip: Params.IP,
|
|
|
|
Port: int64(Params.Port),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err2 := i.serviceClient.RegisterNode(request)
|
|
|
|
if err2 != nil {
|
|
|
|
log.Printf("Index NodeImpl connect to IndexService failed, error= %v", err)
|
|
|
|
return err2
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.Status.ErrorCode != commonpb.ErrorCode_SUCCESS {
|
|
|
|
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-01-12 18:03:24 +08:00
|
|
|
connectMinIOFn := func() error {
|
|
|
|
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
|
|
|
var err error
|
2021-01-29 17:08:31 +08:00
|
|
|
i.kv, err = miniokv.NewMinIOKV(i.loopCtx, option)
|
2021-01-12 18:03:24 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2020-12-30 18:50:56 +08:00
|
|
|
}
|
2021-01-26 09:38:40 +08:00
|
|
|
|
2021-01-29 17:08:31 +08:00
|
|
|
err = retry.Retry(10, time.Millisecond*200, connectMinIOFn)
|
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-01-29 17:08:31 +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-01-29 17:08:31 +08:00
|
|
|
func (i *NodeImpl) 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-01-29 17:08:31 +08:00
|
|
|
func (i *NodeImpl) Stop() error {
|
|
|
|
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-01-29 17:08:31 +08:00
|
|
|
log.Print("NodeImpl closed.")
|
2021-01-26 09:38:40 +08:00
|
|
|
return nil
|
2020-12-10 17:55:55 +08:00
|
|
|
}
|
|
|
|
|
2021-01-29 17:08:31 +08:00
|
|
|
func (i *NodeImpl) UpdateStateCode(code internalpb2.StateCode) {
|
|
|
|
i.stateCode = code
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *NodeImpl) SetIndexServiceClient(serviceClient typeutil.IndexServiceInterface) {
|
2021-01-26 09:38:40 +08:00
|
|
|
i.serviceClient = serviceClient
|
2020-12-10 17:55:55 +08:00
|
|
|
}
|
|
|
|
|
2021-01-29 17:08:31 +08:00
|
|
|
func (i *NodeImpl) BuildIndex(request *indexpb.BuildIndexCmd) (*commonpb.Status, error) {
|
2021-02-23 09:58:06 +08:00
|
|
|
ctx := context.Background()
|
|
|
|
t := &IndexBuildTask{
|
|
|
|
BaseTask: BaseTask{
|
|
|
|
ctx: ctx,
|
|
|
|
done: make(chan error), // intend to do this
|
|
|
|
},
|
|
|
|
cmd: request,
|
|
|
|
kv: i.kv,
|
|
|
|
serviceClient: i.serviceClient,
|
|
|
|
nodeID: Params.NodeID,
|
|
|
|
}
|
2021-01-26 09:38:40 +08:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), reqTimeoutInterval)
|
|
|
|
defer cancel()
|
2020-12-13 06:48:05 +08:00
|
|
|
|
2021-01-26 09:38:40 +08:00
|
|
|
fn := func() error {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return errors.New("Enqueue BuildQueue timeout")
|
|
|
|
default:
|
|
|
|
return i.sched.IndexBuildQueue.Enqueue(t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret := &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_SUCCESS,
|
2020-12-10 17:55:55 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 09:38:40 +08:00
|
|
|
err := fn()
|
|
|
|
if err != nil {
|
|
|
|
ret.ErrorCode = commonpb.ErrorCode_UNEXPECTED_ERROR
|
|
|
|
ret.Reason = err.Error()
|
|
|
|
return ret, nil
|
|
|
|
}
|
2021-02-02 19:56:04 +08:00
|
|
|
log.Println("index scheduler successfully with indexBuildID = ", request.IndexBuildID)
|
2021-01-26 09:38:40 +08:00
|
|
|
err = t.WaitToFinish()
|
|
|
|
log.Println("build index finish ...err = ", err)
|
|
|
|
if err != nil {
|
|
|
|
ret.ErrorCode = commonpb.ErrorCode_UNEXPECTED_ERROR
|
|
|
|
ret.Reason = err.Error()
|
|
|
|
return ret, nil
|
2020-12-10 17:55:55 +08:00
|
|
|
}
|
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-01-29 17:08:31 +08:00
|
|
|
// AddStartCallback adds a callback in the startServer phase.
|
|
|
|
func (i *NodeImpl) AddStartCallback(callbacks ...func()) {
|
|
|
|
i.startCallbacks = append(i.startCallbacks, callbacks...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddCloseCallback adds a callback in the Close phase.
|
|
|
|
func (i *NodeImpl) AddCloseCallback(callbacks ...func()) {
|
|
|
|
i.closeCallbacks = append(i.closeCallbacks, callbacks...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *NodeImpl) GetComponentStates() (*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{
|
|
|
|
ErrorCode: commonpb.ErrorCode_SUCCESS,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2021-02-04 19:34:35 +08:00
|
|
|
func (i *NodeImpl) GetTimeTickChannel() (*milvuspb.StringResponse, error) {
|
|
|
|
return &milvuspb.StringResponse{
|
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_SUCCESS,
|
|
|
|
},
|
|
|
|
}, nil
|
2021-01-26 19:24:09 +08:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:34:35 +08:00
|
|
|
func (i *NodeImpl) GetStatisticsChannel() (*milvuspb.StringResponse, error) {
|
|
|
|
return &milvuspb.StringResponse{
|
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_SUCCESS,
|
|
|
|
},
|
|
|
|
}, nil
|
2021-01-26 19:24:09 +08:00
|
|
|
}
|