2020-12-12 17:10:42 +08:00
|
|
|
package indexbuilderclient
|
2020-12-11 12:01:20 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/commonpb"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/indexbuilderpb"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/util/typeutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
type UniqueID = typeutil.UniqueID
|
|
|
|
|
|
|
|
type Client struct {
|
2020-12-31 15:44:06 +08:00
|
|
|
client indexbuilderpb.IndexBuildServiceClient
|
|
|
|
address string
|
|
|
|
ctx context.Context
|
2020-12-11 12:01:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type IndexDescription struct {
|
|
|
|
ID UniqueID
|
2020-12-13 06:48:05 +08:00
|
|
|
Status indexbuilderpb.IndexStatus
|
2020-12-11 12:01:20 +08:00
|
|
|
EnqueueTime time.Time
|
|
|
|
ScheduleTime time.Time
|
|
|
|
BuildCompleteTime time.Time
|
|
|
|
}
|
|
|
|
|
2020-12-13 06:48:05 +08:00
|
|
|
func NewBuildIndexClient(ctx context.Context, address string) (*Client, error) {
|
2020-12-11 12:01:20 +08:00
|
|
|
return &Client{
|
2020-12-31 15:44:06 +08:00
|
|
|
address: address,
|
|
|
|
ctx: ctx,
|
2020-12-13 06:48:05 +08:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseTS(t int64) time.Time {
|
|
|
|
return time.Unix(0, t)
|
2020-12-11 12:01:20 +08:00
|
|
|
}
|
|
|
|
|
2020-12-31 15:44:06 +08:00
|
|
|
func (c *Client) tryConnect() error {
|
|
|
|
if c.client != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
conn, err := grpc.DialContext(c.ctx, c.address, grpc.WithInsecure(), grpc.WithBlock())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.client = indexbuilderpb.NewIndexBuildServiceClient(conn)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-11 12:01:20 +08:00
|
|
|
func (c *Client) BuildIndexWithoutID(columnDataPaths []string, typeParams map[string]string, indexParams map[string]string) (UniqueID, error) {
|
2020-12-31 15:44:06 +08:00
|
|
|
if c.tryConnect() != nil {
|
|
|
|
panic("BuildIndexWithoutID: failed to connect index builder")
|
|
|
|
}
|
2020-12-11 12:01:20 +08:00
|
|
|
var typeParamsKV []*commonpb.KeyValuePair
|
|
|
|
for typeParam := range typeParams {
|
|
|
|
typeParamsKV = append(typeParamsKV, &commonpb.KeyValuePair{
|
|
|
|
Key: typeParam,
|
|
|
|
Value: typeParams[typeParam],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
var indexParamsKV []*commonpb.KeyValuePair
|
|
|
|
for indexParam := range indexParams {
|
|
|
|
indexParamsKV = append(indexParamsKV, &commonpb.KeyValuePair{
|
|
|
|
Key: indexParam,
|
|
|
|
Value: indexParams[indexParam],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.TODO()
|
|
|
|
requset := &indexbuilderpb.BuildIndexRequest{
|
|
|
|
DataPaths: columnDataPaths,
|
|
|
|
TypeParams: typeParamsKV,
|
|
|
|
IndexParams: indexParamsKV,
|
|
|
|
}
|
|
|
|
response, err := c.client.BuildIndex(ctx, requset)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
indexID := response.IndexID
|
|
|
|
return indexID, err
|
|
|
|
}
|
|
|
|
|
2020-12-13 06:48:05 +08:00
|
|
|
func (c *Client) DescribeIndex(indexID UniqueID) (*IndexDescription, error) {
|
2020-12-31 15:44:06 +08:00
|
|
|
if c.tryConnect() != nil {
|
|
|
|
panic("DescribeIndex: failed to connect index builder")
|
|
|
|
}
|
2020-12-12 17:10:42 +08:00
|
|
|
ctx := context.TODO()
|
|
|
|
request := &indexbuilderpb.DescribleIndexRequest{
|
|
|
|
IndexID: indexID,
|
|
|
|
}
|
|
|
|
response, err := c.client.DescribeIndex(ctx, request)
|
|
|
|
if err != nil {
|
2020-12-13 06:48:05 +08:00
|
|
|
return &IndexDescription{}, err
|
2020-12-12 17:10:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
indexDescription := IndexDescription{
|
|
|
|
ID: indexID,
|
2020-12-13 06:48:05 +08:00
|
|
|
Status: response.IndexStatus,
|
|
|
|
EnqueueTime: parseTS(response.EnqueTime),
|
|
|
|
ScheduleTime: parseTS(response.ScheduleTime),
|
|
|
|
BuildCompleteTime: parseTS(response.BuildCompleteTime),
|
2020-12-12 17:10:42 +08:00
|
|
|
}
|
2020-12-13 06:48:05 +08:00
|
|
|
return &indexDescription, nil
|
2020-12-11 12:01:20 +08:00
|
|
|
}
|
|
|
|
|
2020-12-12 17:10:42 +08:00
|
|
|
func (c *Client) GetIndexFilePaths(indexID UniqueID) ([]string, error) {
|
2020-12-31 15:44:06 +08:00
|
|
|
if c.tryConnect() != nil {
|
|
|
|
panic("GetIndexFilePaths: failed to connect index builder")
|
|
|
|
}
|
2020-12-12 17:10:42 +08:00
|
|
|
ctx := context.TODO()
|
|
|
|
request := &indexbuilderpb.GetIndexFilePathsRequest{
|
|
|
|
IndexID: indexID,
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := c.client.GetIndexFilePaths(ctx, request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-11 12:01:20 +08:00
|
|
|
|
2020-12-12 17:10:42 +08:00
|
|
|
return response.IndexFilePaths, nil
|
2020-12-11 12:01:20 +08:00
|
|
|
}
|