2021-01-16 10:12:14 +08:00
|
|
|
package querynode
|
2020-09-18 15:44:09 +08:00
|
|
|
|
|
|
|
import (
|
2020-11-05 10:52:50 +08:00
|
|
|
"context"
|
2020-09-18 15:44:09 +08:00
|
|
|
"fmt"
|
2020-09-22 15:27:50 +08:00
|
|
|
"log"
|
2020-09-22 15:07:56 +08:00
|
|
|
"path"
|
2020-09-18 15:44:09 +08:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2020-09-21 15:10:54 +08:00
|
|
|
|
2020-11-12 12:04:12 +08:00
|
|
|
"github.com/golang/protobuf/proto"
|
2020-12-07 15:22:20 +08:00
|
|
|
etcdkv "github.com/zilliztech/milvus-distributed/internal/kv/etcd"
|
2021-02-03 16:44:38 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/datapb"
|
2020-11-09 16:27:11 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/etcdpb"
|
2021-02-03 16:44:38 +08:00
|
|
|
"go.etcd.io/etcd/clientv3"
|
2020-09-18 15:44:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-10-24 10:45:57 +08:00
|
|
|
CollectionPrefix = "/collection/"
|
|
|
|
SegmentPrefix = "/segment/"
|
2020-09-18 15:44:09 +08:00
|
|
|
)
|
|
|
|
|
2020-11-05 10:52:50 +08:00
|
|
|
type metaService struct {
|
2020-11-17 20:00:23 +08:00
|
|
|
ctx context.Context
|
2020-12-07 15:22:20 +08:00
|
|
|
kvBase *etcdkv.EtcdKV
|
2020-12-08 14:41:04 +08:00
|
|
|
replica collectionReplica
|
2020-11-05 10:52:50 +08:00
|
|
|
}
|
|
|
|
|
2020-12-08 14:41:04 +08:00
|
|
|
func newMetaService(ctx context.Context, replica collectionReplica) *metaService {
|
2020-12-10 16:31:09 +08:00
|
|
|
ETCDAddr := Params.ETCDAddress
|
|
|
|
MetaRootPath := Params.MetaRootPath
|
2020-11-05 10:52:50 +08:00
|
|
|
|
|
|
|
cli, _ := clientv3.New(clientv3.Config{
|
|
|
|
Endpoints: []string{ETCDAddr},
|
|
|
|
DialTimeout: 5 * time.Second,
|
|
|
|
})
|
|
|
|
|
|
|
|
return &metaService{
|
2020-11-17 20:00:23 +08:00
|
|
|
ctx: ctx,
|
2020-12-07 15:22:20 +08:00
|
|
|
kvBase: etcdkv.NewEtcdKV(cli, MetaRootPath),
|
2020-11-17 20:00:23 +08:00
|
|
|
replica: replica,
|
2020-11-05 10:52:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mService *metaService) start() {
|
|
|
|
// init from meta
|
|
|
|
err := mService.loadCollections()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("metaService loadCollections failed")
|
|
|
|
}
|
|
|
|
err = mService.loadSegments()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("metaService loadSegments failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-04 16:28:14 +08:00
|
|
|
func GetCollectionObjID(key string) string {
|
2020-12-10 16:31:09 +08:00
|
|
|
ETCDRootPath := Params.MetaRootPath
|
2020-11-21 15:06:46 +08:00
|
|
|
|
2020-11-14 11:24:49 +08:00
|
|
|
prefix := path.Join(ETCDRootPath, CollectionPrefix) + "/"
|
2020-09-18 15:44:09 +08:00
|
|
|
return strings.TrimPrefix(key, prefix)
|
|
|
|
}
|
|
|
|
|
2020-11-04 16:28:14 +08:00
|
|
|
func GetSegmentObjID(key string) string {
|
2020-12-10 16:31:09 +08:00
|
|
|
ETCDRootPath := Params.MetaRootPath
|
2020-11-21 15:06:46 +08:00
|
|
|
|
2020-11-14 11:24:49 +08:00
|
|
|
prefix := path.Join(ETCDRootPath, SegmentPrefix) + "/"
|
2020-09-18 15:44:09 +08:00
|
|
|
return strings.TrimPrefix(key, prefix)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isCollectionObj(key string) bool {
|
2020-12-10 16:31:09 +08:00
|
|
|
ETCDRootPath := Params.MetaRootPath
|
2020-11-21 15:06:46 +08:00
|
|
|
|
2020-11-14 11:24:49 +08:00
|
|
|
prefix := path.Join(ETCDRootPath, CollectionPrefix) + "/"
|
2020-09-18 15:44:09 +08:00
|
|
|
prefix = strings.TrimSpace(prefix)
|
|
|
|
index := strings.Index(key, prefix)
|
2020-11-05 10:52:50 +08:00
|
|
|
|
2020-09-18 15:44:09 +08:00
|
|
|
return index == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func isSegmentObj(key string) bool {
|
2020-12-10 16:31:09 +08:00
|
|
|
ETCDRootPath := Params.MetaRootPath
|
2020-11-21 15:06:46 +08:00
|
|
|
|
2020-11-14 11:24:49 +08:00
|
|
|
prefix := path.Join(ETCDRootPath, SegmentPrefix) + "/"
|
2020-09-18 15:44:09 +08:00
|
|
|
prefix = strings.TrimSpace(prefix)
|
|
|
|
index := strings.Index(key, prefix)
|
2020-11-05 10:52:50 +08:00
|
|
|
|
2020-09-18 15:44:09 +08:00
|
|
|
return index == 0
|
|
|
|
}
|
|
|
|
|
2021-02-03 16:44:38 +08:00
|
|
|
func printCollectionStruct(obj *etcdpb.CollectionInfo) {
|
2020-09-18 15:44:09 +08:00
|
|
|
v := reflect.ValueOf(obj)
|
|
|
|
v = reflect.Indirect(v)
|
|
|
|
typeOfS := v.Type()
|
|
|
|
|
2020-09-21 15:10:54 +08:00
|
|
|
for i := 0; i < v.NumField(); i++ {
|
|
|
|
if typeOfS.Field(i).Name == "GrpcMarshalString" {
|
2020-09-18 15:44:09 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
fmt.Printf("Field: %s\tValue: %v\n", typeOfS.Field(i).Name, v.Field(i).Interface())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-03 16:44:38 +08:00
|
|
|
func printSegmentStruct(obj *datapb.SegmentInfo) {
|
2020-09-18 15:44:09 +08:00
|
|
|
v := reflect.ValueOf(obj)
|
|
|
|
v = reflect.Indirect(v)
|
|
|
|
typeOfS := v.Type()
|
|
|
|
|
2020-09-21 15:10:54 +08:00
|
|
|
for i := 0; i < v.NumField(); i++ {
|
2020-09-18 15:44:09 +08:00
|
|
|
fmt.Printf("Field: %s\tValue: %v\n", typeOfS.Field(i).Name, v.Field(i).Interface())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-05 10:52:50 +08:00
|
|
|
func (mService *metaService) processCollectionCreate(id string, value string) {
|
2020-12-03 19:00:11 +08:00
|
|
|
//println(fmt.Sprintf("Create Collection:$%s$", id))
|
2020-11-05 10:52:50 +08:00
|
|
|
|
2020-11-09 16:27:11 +08:00
|
|
|
col := mService.collectionUnmarshal(value)
|
2020-11-05 10:52:50 +08:00
|
|
|
if col != nil {
|
2020-12-10 16:31:09 +08:00
|
|
|
schema := col.Schema
|
2021-01-18 10:38:41 +08:00
|
|
|
err := mService.replica.addCollection(col.ID, schema)
|
2020-11-13 17:20:13 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2021-02-03 16:44:38 +08:00
|
|
|
for _, partitionID := range col.PartitionIDs {
|
|
|
|
err = mService.replica.addPartition(col.ID, partitionID)
|
2020-11-09 16:27:11 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2020-11-05 10:52:50 +08:00
|
|
|
}
|
2020-09-21 15:10:54 +08:00
|
|
|
}
|
2020-09-18 15:44:09 +08:00
|
|
|
}
|
|
|
|
|
2020-11-05 10:52:50 +08:00
|
|
|
func (mService *metaService) processSegmentCreate(id string, value string) {
|
2020-12-03 19:00:11 +08:00
|
|
|
//println("Create Segment: ", id)
|
2020-11-05 10:52:50 +08:00
|
|
|
|
2020-11-09 16:27:11 +08:00
|
|
|
seg := mService.segmentUnmarshal(value)
|
2020-09-22 15:27:50 +08:00
|
|
|
|
2020-11-05 10:52:50 +08:00
|
|
|
// TODO: what if seg == nil? We need to notify master and return rpc request failed
|
|
|
|
if seg != nil {
|
2021-02-03 16:44:38 +08:00
|
|
|
// TODO: get partition id from segment meta
|
|
|
|
err := mService.replica.addSegment(seg.SegmentID, seg.PartitionID, seg.CollectionID, segTypeGrowing)
|
2020-11-09 16:27:11 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
2020-09-21 15:10:54 +08:00
|
|
|
}
|
2020-09-18 15:44:09 +08:00
|
|
|
}
|
|
|
|
|
2020-11-05 10:52:50 +08:00
|
|
|
func (mService *metaService) processCreate(key string, msg string) {
|
2020-12-03 19:00:11 +08:00
|
|
|
//println("process create", key)
|
2020-09-21 15:10:54 +08:00
|
|
|
if isCollectionObj(key) {
|
2020-11-04 16:28:14 +08:00
|
|
|
objID := GetCollectionObjID(key)
|
2020-11-05 10:52:50 +08:00
|
|
|
mService.processCollectionCreate(objID, msg)
|
2020-09-21 15:10:54 +08:00
|
|
|
} else if isSegmentObj(key) {
|
2020-11-04 16:28:14 +08:00
|
|
|
objID := GetSegmentObjID(key)
|
2020-11-05 10:52:50 +08:00
|
|
|
mService.processSegmentCreate(objID, msg)
|
2020-09-21 15:10:54 +08:00
|
|
|
} else {
|
2020-09-18 15:44:09 +08:00
|
|
|
println("can not process create msg:", key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-05 10:52:50 +08:00
|
|
|
func (mService *metaService) loadCollections() error {
|
|
|
|
keys, values, err := mService.kvBase.LoadWithPrefix(CollectionPrefix)
|
2020-11-02 10:26:33 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-05 10:52:50 +08:00
|
|
|
|
2020-09-21 15:10:54 +08:00
|
|
|
for i := range keys {
|
2020-11-04 16:28:14 +08:00
|
|
|
objID := GetCollectionObjID(keys[i])
|
2020-11-05 10:52:50 +08:00
|
|
|
mService.processCollectionCreate(objID, values[i])
|
2020-09-18 15:44:09 +08:00
|
|
|
}
|
2020-11-05 10:52:50 +08:00
|
|
|
|
2020-09-18 15:44:09 +08:00
|
|
|
return nil
|
|
|
|
}
|
2020-11-05 10:52:50 +08:00
|
|
|
|
|
|
|
func (mService *metaService) loadSegments() error {
|
|
|
|
keys, values, err := mService.kvBase.LoadWithPrefix(SegmentPrefix)
|
2020-11-02 10:26:33 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-05 10:52:50 +08:00
|
|
|
|
2020-09-21 15:10:54 +08:00
|
|
|
for i := range keys {
|
2020-11-04 16:28:14 +08:00
|
|
|
objID := GetSegmentObjID(keys[i])
|
2020-11-05 10:52:50 +08:00
|
|
|
mService.processSegmentCreate(objID, values[i])
|
2020-09-18 15:44:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-11-09 16:27:11 +08:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------- Unmarshal and Marshal
|
2021-02-03 16:44:38 +08:00
|
|
|
func (mService *metaService) collectionUnmarshal(value string) *etcdpb.CollectionInfo {
|
|
|
|
col := etcdpb.CollectionInfo{}
|
2020-11-16 17:01:10 +08:00
|
|
|
err := proto.UnmarshalText(value, &col)
|
2020-11-09 16:27:11 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &col
|
|
|
|
}
|
|
|
|
|
2021-02-03 16:44:38 +08:00
|
|
|
func (mService *metaService) collectionMarshal(col *etcdpb.CollectionInfo) string {
|
2020-11-16 17:01:10 +08:00
|
|
|
value := proto.MarshalTextString(col)
|
|
|
|
if value == "" {
|
|
|
|
log.Println("marshal collection failed")
|
2020-11-09 16:27:11 +08:00
|
|
|
return ""
|
|
|
|
}
|
2020-11-16 17:01:10 +08:00
|
|
|
return value
|
2020-11-09 16:27:11 +08:00
|
|
|
}
|
|
|
|
|
2021-02-03 16:44:38 +08:00
|
|
|
func (mService *metaService) segmentUnmarshal(value string) *datapb.SegmentInfo {
|
|
|
|
seg := datapb.SegmentInfo{}
|
2020-11-16 17:01:10 +08:00
|
|
|
err := proto.UnmarshalText(value, &seg)
|
2020-11-09 16:27:11 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &seg
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mService *metaService) segmentMarshal(seg *etcdpb.SegmentMeta) string {
|
2020-11-16 17:01:10 +08:00
|
|
|
value := proto.MarshalTextString(seg)
|
|
|
|
if value == "" {
|
|
|
|
log.Println("marshal segment failed")
|
2020-11-09 16:27:11 +08:00
|
|
|
return ""
|
|
|
|
}
|
2020-11-16 17:01:10 +08:00
|
|
|
return value
|
2020-11-09 16:27:11 +08:00
|
|
|
}
|