2021-01-15 14:38:36 +08:00
|
|
|
package proxynode
|
2020-11-20 17:53:31 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/errors"
|
2021-01-18 19:32:08 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/commonpb"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/milvuspb"
|
2020-11-20 17:53:31 +08:00
|
|
|
)
|
|
|
|
|
2020-11-30 19:08:32 +08:00
|
|
|
type Cache interface {
|
2020-11-20 17:53:31 +08:00
|
|
|
Hit(collectionName string) bool
|
2021-01-22 09:36:18 +08:00
|
|
|
Get(collectionName string) (*milvuspb.DescribeCollectionResponse, error)
|
2020-12-03 19:00:11 +08:00
|
|
|
Sync(collectionName string) error
|
2021-01-22 09:36:18 +08:00
|
|
|
Update(collectionName string, desc *milvuspb.DescribeCollectionResponse) error
|
2020-11-30 19:08:32 +08:00
|
|
|
Remove(collectionName string) error
|
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
|
|
|
|
|
|
|
type SimpleMetaCache struct {
|
2020-12-03 19:00:11 +08:00
|
|
|
mu sync.RWMutex
|
2021-01-22 09:36:18 +08:00
|
|
|
metas map[string]*milvuspb.DescribeCollectionResponse // collection name to schema
|
2020-12-03 19:00:11 +08:00
|
|
|
ctx context.Context
|
2021-01-22 09:36:18 +08:00
|
|
|
proxyInstance *NodeImpl
|
2020-11-20 17:53:31 +08:00
|
|
|
}
|
|
|
|
|
2020-11-30 19:08:32 +08:00
|
|
|
func (metaCache *SimpleMetaCache) Hit(collectionName string) bool {
|
|
|
|
metaCache.mu.RLock()
|
|
|
|
defer metaCache.mu.RUnlock()
|
|
|
|
_, ok := metaCache.metas[collectionName]
|
2020-11-20 17:53:31 +08:00
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2021-01-22 09:36:18 +08:00
|
|
|
func (metaCache *SimpleMetaCache) Get(collectionName string) (*milvuspb.DescribeCollectionResponse, error) {
|
2020-11-30 19:08:32 +08:00
|
|
|
metaCache.mu.RLock()
|
|
|
|
defer metaCache.mu.RUnlock()
|
|
|
|
schema, ok := metaCache.metas[collectionName]
|
2020-11-20 17:53:31 +08:00
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("collection meta miss")
|
|
|
|
}
|
|
|
|
return schema, nil
|
|
|
|
}
|
|
|
|
|
2020-12-03 19:00:11 +08:00
|
|
|
func (metaCache *SimpleMetaCache) Sync(collectionName string) error {
|
|
|
|
dct := &DescribeCollectionTask{
|
|
|
|
Condition: NewTaskCondition(metaCache.ctx),
|
2021-01-22 09:36:18 +08:00
|
|
|
DescribeCollectionRequest: &milvuspb.DescribeCollectionRequest{
|
2021-01-18 19:32:08 +08:00
|
|
|
Base: &commonpb.MsgBase{
|
|
|
|
MsgType: commonpb.MsgType_kDescribeCollection,
|
2020-12-03 19:00:11 +08:00
|
|
|
},
|
2021-01-18 19:32:08 +08:00
|
|
|
CollectionName: collectionName,
|
2020-11-30 19:08:32 +08:00
|
|
|
},
|
2020-12-03 19:00:11 +08:00
|
|
|
masterClient: metaCache.proxyInstance.masterClient,
|
2020-11-30 19:08:32 +08:00
|
|
|
}
|
2020-12-03 19:00:11 +08:00
|
|
|
var cancel func()
|
|
|
|
dct.ctx, cancel = context.WithTimeout(metaCache.ctx, reqTimeoutInterval)
|
|
|
|
defer cancel()
|
2020-12-01 16:41:25 +08:00
|
|
|
|
2020-12-03 19:00:11 +08:00
|
|
|
err := metaCache.proxyInstance.sched.DdQueue.Enqueue(dct)
|
2020-12-02 18:31:56 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-01 19:53:53 +08:00
|
|
|
|
2020-12-03 19:00:11 +08:00
|
|
|
return dct.WaitToFinish()
|
|
|
|
}
|
|
|
|
|
2021-01-22 09:36:18 +08:00
|
|
|
func (metaCache *SimpleMetaCache) Update(collectionName string, desc *milvuspb.DescribeCollectionResponse) error {
|
2020-11-30 19:08:32 +08:00
|
|
|
metaCache.mu.Lock()
|
|
|
|
defer metaCache.mu.Unlock()
|
|
|
|
|
2020-12-03 19:00:11 +08:00
|
|
|
metaCache.metas[collectionName] = desc
|
2020-11-30 19:08:32 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (metaCache *SimpleMetaCache) Remove(collectionName string) error {
|
|
|
|
metaCache.mu.Lock()
|
|
|
|
defer metaCache.mu.Unlock()
|
|
|
|
|
|
|
|
_, ok := metaCache.metas[collectionName]
|
|
|
|
if !ok {
|
|
|
|
return errors.New("cannot find collection: " + collectionName)
|
|
|
|
}
|
|
|
|
delete(metaCache.metas, collectionName)
|
2020-11-20 17:53:31 +08:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-22 09:36:18 +08:00
|
|
|
func newSimpleMetaCache(ctx context.Context, proxyInstance *NodeImpl) *SimpleMetaCache {
|
2020-11-20 17:53:31 +08:00
|
|
|
return &SimpleMetaCache{
|
2021-01-22 09:36:18 +08:00
|
|
|
metas: make(map[string]*milvuspb.DescribeCollectionResponse),
|
2020-12-03 19:00:11 +08:00
|
|
|
proxyInstance: proxyInstance,
|
|
|
|
ctx: ctx,
|
2020-11-20 17:53:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-22 09:36:18 +08:00
|
|
|
func initGlobalMetaCache(ctx context.Context, proxyInstance *NodeImpl) {
|
2020-12-03 19:00:11 +08:00
|
|
|
globalMetaCache = newSimpleMetaCache(ctx, proxyInstance)
|
2020-11-20 17:53:31 +08:00
|
|
|
}
|