2021-01-15 14:38:36 +08:00
|
|
|
package proxynode
|
2020-11-20 17:53:31 +08:00
|
|
|
|
|
|
|
import (
|
2021-02-26 17:44:24 +08:00
|
|
|
"context"
|
2021-03-05 10:15:27 +08:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-11-20 17:53:31 +08:00
|
|
|
"sync"
|
|
|
|
|
2021-01-18 19:32:08 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/commonpb"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/milvuspb"
|
2021-01-31 14:55:36 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/schemapb"
|
2021-03-08 10:09:48 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/types"
|
2021-01-31 14:55:36 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/util/typeutil"
|
2020-11-20 17:53:31 +08:00
|
|
|
)
|
|
|
|
|
2020-11-30 19:08:32 +08:00
|
|
|
type Cache interface {
|
2021-02-26 17:44:24 +08:00
|
|
|
GetCollectionID(ctx context.Context, collectionName string) (typeutil.UniqueID, error)
|
|
|
|
GetPartitionID(ctx context.Context, collectionName string, partitionName string) (typeutil.UniqueID, error)
|
|
|
|
GetCollectionSchema(ctx context.Context, collectionName string) (*schemapb.CollectionSchema, error)
|
|
|
|
RemoveCollection(ctx context.Context, collectionName string)
|
|
|
|
RemovePartition(ctx context.Context, collectionName string, partitionName string)
|
2021-01-31 14:55:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type collectionInfo struct {
|
2021-02-04 18:11:19 +08:00
|
|
|
collID typeutil.UniqueID
|
|
|
|
schema *schemapb.CollectionSchema
|
|
|
|
partInfo map[string]typeutil.UniqueID
|
2021-01-31 14:55:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type MetaCache struct {
|
2021-03-08 10:09:48 +08:00
|
|
|
client types.MasterService
|
2021-01-31 14:55:36 +08:00
|
|
|
|
|
|
|
collInfo map[string]*collectionInfo
|
|
|
|
mu sync.RWMutex
|
2020-11-20 17:53:31 +08:00
|
|
|
}
|
|
|
|
|
2020-11-30 19:08:32 +08:00
|
|
|
var globalMetaCache Cache
|
2020-11-20 17:53:31 +08:00
|
|
|
|
2021-03-08 10:09:48 +08:00
|
|
|
func InitMetaCache(client types.MasterService) error {
|
2021-01-31 14:55:36 +08:00
|
|
|
var err error
|
|
|
|
globalMetaCache, err = NewMetaCache(client)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2020-11-20 17:53:31 +08:00
|
|
|
}
|
|
|
|
|
2021-03-08 10:09:48 +08:00
|
|
|
func NewMetaCache(client types.MasterService) (*MetaCache, error) {
|
2021-01-31 14:55:36 +08:00
|
|
|
return &MetaCache{
|
|
|
|
client: client,
|
|
|
|
collInfo: map[string]*collectionInfo{},
|
|
|
|
}, nil
|
2020-11-20 17:53:31 +08:00
|
|
|
}
|
|
|
|
|
2021-02-26 17:44:24 +08:00
|
|
|
func (m *MetaCache) readCollectionID(ctx context.Context, collectionName string) (typeutil.UniqueID, error) {
|
2021-01-31 14:55:36 +08:00
|
|
|
m.mu.RLock()
|
|
|
|
defer m.mu.RUnlock()
|
|
|
|
|
|
|
|
collInfo, ok := m.collInfo[collectionName]
|
2020-11-20 17:53:31 +08:00
|
|
|
if !ok {
|
2021-03-05 10:15:27 +08:00
|
|
|
return 0, fmt.Errorf("can't find collection name:%s", collectionName)
|
2020-11-20 17:53:31 +08:00
|
|
|
}
|
2021-01-31 14:55:36 +08:00
|
|
|
return collInfo.collID, nil
|
2020-11-20 17:53:31 +08:00
|
|
|
}
|
|
|
|
|
2021-02-26 17:44:24 +08:00
|
|
|
func (m *MetaCache) readCollectionSchema(ctx context.Context, collectionName string) (*schemapb.CollectionSchema, error) {
|
2021-01-31 14:55:36 +08:00
|
|
|
m.mu.RLock()
|
|
|
|
defer m.mu.RUnlock()
|
|
|
|
|
|
|
|
collInfo, ok := m.collInfo[collectionName]
|
|
|
|
if !ok {
|
2021-03-05 10:15:27 +08:00
|
|
|
return nil, fmt.Errorf("can't find collection name:%s", collectionName)
|
2021-01-31 14:55:36 +08:00
|
|
|
}
|
|
|
|
return collInfo.schema, nil
|
|
|
|
}
|
|
|
|
|
2021-02-26 17:44:24 +08:00
|
|
|
func (m *MetaCache) readPartitionID(ctx context.Context, collectionName string, partitionName string) (typeutil.UniqueID, error) {
|
2021-01-31 14:55:36 +08:00
|
|
|
m.mu.RLock()
|
|
|
|
defer m.mu.RUnlock()
|
|
|
|
|
2021-02-04 18:11:19 +08:00
|
|
|
collInfo, ok := m.collInfo[collectionName]
|
|
|
|
if !ok {
|
2021-03-05 10:15:27 +08:00
|
|
|
return 0, fmt.Errorf("can't find collection name:%s", collectionName)
|
2021-02-04 18:11:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
partitionID, ok := collInfo.partInfo[partitionName]
|
2021-01-31 14:55:36 +08:00
|
|
|
if !ok {
|
2021-03-05 10:15:27 +08:00
|
|
|
return 0, fmt.Errorf("can't find partition name:%s", partitionName)
|
2021-01-31 14:55:36 +08:00
|
|
|
}
|
|
|
|
return partitionID, nil
|
|
|
|
}
|
|
|
|
|
2021-02-26 17:44:24 +08:00
|
|
|
func (m *MetaCache) GetCollectionID(ctx context.Context, collectionName string) (typeutil.UniqueID, error) {
|
|
|
|
collID, err := m.readCollectionID(ctx, collectionName)
|
2021-01-31 14:55:36 +08:00
|
|
|
if err == nil {
|
|
|
|
return collID, nil
|
2020-11-30 19:08:32 +08:00
|
|
|
}
|
2021-01-31 14:55:36 +08:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
2020-12-01 16:41:25 +08:00
|
|
|
|
2021-01-31 14:55:36 +08:00
|
|
|
req := &milvuspb.DescribeCollectionRequest{
|
|
|
|
Base: &commonpb.MsgBase{
|
2021-03-10 14:45:35 +08:00
|
|
|
MsgType: commonpb.MsgType_DescribeCollection,
|
2021-01-31 14:55:36 +08:00
|
|
|
},
|
|
|
|
CollectionName: collectionName,
|
|
|
|
}
|
2021-02-26 17:44:24 +08:00
|
|
|
coll, err := m.client.DescribeCollection(ctx, req)
|
2020-12-02 18:31:56 +08:00
|
|
|
if err != nil {
|
2021-01-31 14:55:36 +08:00
|
|
|
return 0, err
|
|
|
|
}
|
2021-03-10 22:06:22 +08:00
|
|
|
if coll.Status.ErrorCode != commonpb.ErrorCode_Success {
|
2021-03-05 10:15:27 +08:00
|
|
|
return 0, errors.New(coll.Status.Reason)
|
2020-12-02 18:31:56 +08:00
|
|
|
}
|
2020-12-01 19:53:53 +08:00
|
|
|
|
2021-01-31 14:55:36 +08:00
|
|
|
_, ok := m.collInfo[collectionName]
|
|
|
|
if !ok {
|
2021-02-04 18:11:19 +08:00
|
|
|
m.collInfo[collectionName] = &collectionInfo{}
|
2021-01-31 14:55:36 +08:00
|
|
|
}
|
2021-02-04 18:11:19 +08:00
|
|
|
m.collInfo[collectionName].schema = coll.Schema
|
|
|
|
m.collInfo[collectionName].collID = coll.CollectionID
|
|
|
|
|
|
|
|
return m.collInfo[collectionName].collID, nil
|
2020-12-03 19:00:11 +08:00
|
|
|
}
|
2021-02-26 17:44:24 +08:00
|
|
|
func (m *MetaCache) GetCollectionSchema(ctx context.Context, collectionName string) (*schemapb.CollectionSchema, error) {
|
|
|
|
collSchema, err := m.readCollectionSchema(ctx, collectionName)
|
2021-01-31 14:55:36 +08:00
|
|
|
if err == nil {
|
|
|
|
return collSchema, nil
|
|
|
|
}
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
2020-12-03 19:00:11 +08:00
|
|
|
|
2021-01-31 14:55:36 +08:00
|
|
|
req := &milvuspb.DescribeCollectionRequest{
|
|
|
|
Base: &commonpb.MsgBase{
|
2021-03-10 14:45:35 +08:00
|
|
|
MsgType: commonpb.MsgType_DescribeCollection,
|
2021-01-31 14:55:36 +08:00
|
|
|
},
|
|
|
|
CollectionName: collectionName,
|
|
|
|
}
|
2021-02-26 17:44:24 +08:00
|
|
|
coll, err := m.client.DescribeCollection(ctx, req)
|
2021-01-31 14:55:36 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-10 22:06:22 +08:00
|
|
|
if coll.Status.ErrorCode != commonpb.ErrorCode_Success {
|
2021-03-05 10:15:27 +08:00
|
|
|
return nil, errors.New(coll.Status.Reason)
|
2021-01-31 14:55:36 +08:00
|
|
|
}
|
2020-11-30 19:08:32 +08:00
|
|
|
|
2021-01-31 14:55:36 +08:00
|
|
|
_, ok := m.collInfo[collectionName]
|
|
|
|
if !ok {
|
2021-02-04 18:11:19 +08:00
|
|
|
m.collInfo[collectionName] = &collectionInfo{}
|
2021-01-31 14:55:36 +08:00
|
|
|
}
|
2021-02-04 18:11:19 +08:00
|
|
|
m.collInfo[collectionName].schema = coll.Schema
|
|
|
|
m.collInfo[collectionName].collID = coll.CollectionID
|
|
|
|
|
|
|
|
return m.collInfo[collectionName].schema, nil
|
2020-11-30 19:08:32 +08:00
|
|
|
}
|
|
|
|
|
2021-02-26 17:44:24 +08:00
|
|
|
func (m *MetaCache) GetPartitionID(ctx context.Context, collectionName string, partitionName string) (typeutil.UniqueID, error) {
|
|
|
|
partitionID, err := m.readPartitionID(ctx, collectionName, partitionName)
|
2021-01-31 14:55:36 +08:00
|
|
|
if err == nil {
|
|
|
|
return partitionID, nil
|
|
|
|
}
|
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
req := &milvuspb.ShowPartitionsRequest{
|
2021-01-31 14:55:36 +08:00
|
|
|
Base: &commonpb.MsgBase{
|
2021-03-10 14:45:35 +08:00
|
|
|
MsgType: commonpb.MsgType_ShowPartitions,
|
2021-01-31 14:55:36 +08:00
|
|
|
},
|
|
|
|
CollectionName: collectionName,
|
|
|
|
}
|
2021-02-26 17:44:24 +08:00
|
|
|
partitions, err := m.client.ShowPartitions(ctx, req)
|
2021-01-31 14:55:36 +08:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2021-03-10 22:06:22 +08:00
|
|
|
if partitions.Status.ErrorCode != commonpb.ErrorCode_Success {
|
2021-03-05 10:15:27 +08:00
|
|
|
return 0, fmt.Errorf("%s", partitions.Status.Reason)
|
2021-01-31 14:55:36 +08:00
|
|
|
}
|
2021-03-19 17:50:26 +08:00
|
|
|
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
|
2021-01-31 14:55:36 +08:00
|
|
|
if len(partitions.PartitionIDs) != len(partitions.PartitionNames) {
|
2021-03-05 10:15:27 +08:00
|
|
|
return 0, fmt.Errorf("partition ids len: %d doesn't equal Partition name len %d",
|
2021-01-31 14:55:36 +08:00
|
|
|
len(partitions.PartitionIDs), len(partitions.PartitionNames))
|
|
|
|
}
|
2021-02-04 18:11:19 +08:00
|
|
|
|
|
|
|
_, ok := m.collInfo[collectionName]
|
|
|
|
if !ok {
|
|
|
|
m.collInfo[collectionName] = &collectionInfo{
|
|
|
|
partInfo: map[string]typeutil.UniqueID{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
partInfo := m.collInfo[collectionName].partInfo
|
2021-02-05 11:49:13 +08:00
|
|
|
if partInfo == nil {
|
|
|
|
partInfo = map[string]typeutil.UniqueID{}
|
|
|
|
}
|
2020-11-30 19:08:32 +08:00
|
|
|
|
2021-01-31 14:55:36 +08:00
|
|
|
for i := 0; i < len(partitions.PartitionIDs); i++ {
|
2021-02-04 18:11:19 +08:00
|
|
|
_, ok := partInfo[partitions.PartitionNames[i]]
|
2021-01-31 14:55:36 +08:00
|
|
|
if !ok {
|
2021-02-04 18:11:19 +08:00
|
|
|
partInfo[partitions.PartitionNames[i]] = partitions.PartitionIDs[i]
|
2021-01-31 14:55:36 +08:00
|
|
|
}
|
|
|
|
}
|
2021-02-04 18:11:19 +08:00
|
|
|
_, ok = partInfo[partitionName]
|
2020-11-30 19:08:32 +08:00
|
|
|
if !ok {
|
2021-03-05 10:15:27 +08:00
|
|
|
return 0, fmt.Errorf("partitionID of partitionName:%s can not be find", partitionName)
|
2020-11-30 19:08:32 +08:00
|
|
|
}
|
2020-11-20 17:53:31 +08:00
|
|
|
|
2021-02-04 18:11:19 +08:00
|
|
|
return partInfo[partitionName], nil
|
2020-11-20 17:53:31 +08:00
|
|
|
}
|
|
|
|
|
2021-02-26 17:44:24 +08:00
|
|
|
func (m *MetaCache) RemoveCollection(ctx context.Context, collectionName string) {
|
2021-01-31 14:55:36 +08:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
delete(m.collInfo, collectionName)
|
2020-11-20 17:53:31 +08:00
|
|
|
}
|
|
|
|
|
2021-02-26 17:44:24 +08:00
|
|
|
func (m *MetaCache) RemovePartition(ctx context.Context, collectionName, partitionName string) {
|
2021-01-31 14:55:36 +08:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
2021-02-04 18:11:19 +08:00
|
|
|
_, ok := m.collInfo[collectionName]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
partInfo := m.collInfo[collectionName].partInfo
|
|
|
|
if partInfo == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
delete(partInfo, partitionName)
|
2020-11-20 17:53:31 +08:00
|
|
|
}
|