2020-11-20 17:53:31 +08:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
|
2020-12-02 18:31:56 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/allocator"
|
2020-11-20 17:53:31 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/errors"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/internalpb"
|
2020-12-02 18:31:56 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/masterpb"
|
2020-11-20 17:53:31 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/servicepb"
|
|
|
|
)
|
|
|
|
|
2020-11-30 19:08:32 +08:00
|
|
|
type Cache interface {
|
2020-11-20 17:53:31 +08:00
|
|
|
Hit(collectionName string) bool
|
|
|
|
Get(collectionName string) (*servicepb.CollectionDescription, error)
|
2020-12-02 18:31:56 +08:00
|
|
|
Update(collectionName string) 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-02 18:31:56 +08:00
|
|
|
mu sync.RWMutex
|
|
|
|
proxyID UniqueID
|
|
|
|
metas map[string]*servicepb.CollectionDescription // collection name to schema
|
|
|
|
masterClient masterpb.MasterClient
|
|
|
|
reqIDAllocator *allocator.IDAllocator
|
|
|
|
tsoAllocator *allocator.TimestampAllocator
|
|
|
|
ctx context.Context
|
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
|
|
|
|
}
|
|
|
|
|
2020-11-30 19:08:32 +08:00
|
|
|
func (metaCache *SimpleMetaCache) Get(collectionName string) (*servicepb.CollectionDescription, error) {
|
|
|
|
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-02 18:31:56 +08:00
|
|
|
func (metaCache *SimpleMetaCache) Update(collectionName string) error {
|
|
|
|
reqID, err := metaCache.reqIDAllocator.AllocOne()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ts, err := metaCache.tsoAllocator.AllocOne()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
hasCollectionReq := &internalpb.HasCollectionRequest{
|
|
|
|
MsgType: internalpb.MsgType_kHasCollection,
|
|
|
|
ReqID: reqID,
|
|
|
|
Timestamp: ts,
|
|
|
|
ProxyID: metaCache.proxyID,
|
|
|
|
CollectionName: &servicepb.CollectionName{
|
|
|
|
CollectionName: collectionName,
|
2020-11-30 19:08:32 +08:00
|
|
|
},
|
|
|
|
}
|
2020-12-02 18:31:56 +08:00
|
|
|
has, err := metaCache.masterClient.HasCollection(metaCache.ctx, hasCollectionReq)
|
2020-12-01 17:03:39 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-02 18:31:56 +08:00
|
|
|
if !has.Value {
|
|
|
|
return errors.New("collection " + collectionName + " not exists")
|
|
|
|
}
|
2020-12-01 16:41:25 +08:00
|
|
|
|
2020-12-02 18:31:56 +08:00
|
|
|
reqID, err = metaCache.reqIDAllocator.AllocOne()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ts, err = metaCache.tsoAllocator.AllocOne()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req := &internalpb.DescribeCollectionRequest{
|
|
|
|
MsgType: internalpb.MsgType_kDescribeCollection,
|
|
|
|
ReqID: reqID,
|
|
|
|
Timestamp: ts,
|
|
|
|
ProxyID: metaCache.proxyID,
|
|
|
|
CollectionName: &servicepb.CollectionName{
|
|
|
|
CollectionName: collectionName,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
resp, err := metaCache.masterClient.DescribeCollection(metaCache.ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-01 19:53:53 +08:00
|
|
|
|
2020-11-30 19:08:32 +08:00
|
|
|
metaCache.mu.Lock()
|
|
|
|
defer metaCache.mu.Unlock()
|
2020-12-02 18:31:56 +08:00
|
|
|
metaCache.metas[collectionName] = resp
|
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
|
|
|
|
}
|
|
|
|
|
2020-12-02 18:31:56 +08:00
|
|
|
func newSimpleMetaCache(ctx context.Context,
|
|
|
|
mCli masterpb.MasterClient,
|
|
|
|
idAllocator *allocator.IDAllocator,
|
|
|
|
tsoAllocator *allocator.TimestampAllocator) *SimpleMetaCache {
|
2020-11-20 17:53:31 +08:00
|
|
|
return &SimpleMetaCache{
|
2020-12-02 18:31:56 +08:00
|
|
|
metas: make(map[string]*servicepb.CollectionDescription),
|
|
|
|
masterClient: mCli,
|
|
|
|
reqIDAllocator: idAllocator,
|
|
|
|
tsoAllocator: tsoAllocator,
|
|
|
|
proxyID: Params.ProxyID(),
|
|
|
|
ctx: ctx,
|
2020-11-20 17:53:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-02 18:31:56 +08:00
|
|
|
func initGlobalMetaCache(ctx context.Context,
|
|
|
|
mCli masterpb.MasterClient,
|
|
|
|
idAllocator *allocator.IDAllocator,
|
|
|
|
tsoAllocator *allocator.TimestampAllocator) {
|
|
|
|
globalMetaCache = newSimpleMetaCache(ctx, mCli, idAllocator, tsoAllocator)
|
2020-11-20 17:53:31 +08:00
|
|
|
}
|