mirror of
https://github.com/milvus-io/milvus.git
synced 2026-07-21 02:05:41 +00:00
Related to #51419 Update the Go client module path, imports, dependencies, tests, examples, and documentation from client/v2 to client/v3. Set the SDK version to 3.0.0 in preparation for the Milvus 3.0 release. Signed-off-by: Congqi Xia <congqi.xia@zilliz.com>
81 lines
2.3 KiB
Go
81 lines
2.3 KiB
Go
package milvusclient
|
|
|
|
import (
|
|
"context"
|
|
"math"
|
|
|
|
"github.com/cockroachdb/errors"
|
|
|
|
"github.com/milvus-io/milvus/client/v3/entity"
|
|
"github.com/milvus-io/milvus/client/v3/internal/conc"
|
|
"github.com/milvus-io/milvus/client/v3/internal/merr"
|
|
"github.com/milvus-io/milvus/client/v3/internal/retry"
|
|
"github.com/milvus-io/milvus/client/v3/internal/typeutil"
|
|
)
|
|
|
|
// CollectionCache stores the cached collection schema information.
|
|
type CollectionCache struct {
|
|
sf conc.Singleflight[*entity.Collection]
|
|
collections *typeutil.ConcurrentMap[string, *entity.Collection]
|
|
fetcher func(context.Context, string) (*entity.Collection, error)
|
|
}
|
|
|
|
func (c *CollectionCache) GetCollection(ctx context.Context, collName string) (*entity.Collection, error) {
|
|
coll, ok := c.collections.Get(collName)
|
|
if ok {
|
|
return coll, nil
|
|
}
|
|
|
|
coll, err, _ := c.sf.Do(collName, func() (*entity.Collection, error) {
|
|
coll, err := c.fetcher(ctx, collName)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
c.collections.Insert(collName, coll)
|
|
return coll, nil
|
|
})
|
|
return coll, err
|
|
}
|
|
|
|
// Evict removes the collection cache related to the provided collection name.
|
|
func (c *CollectionCache) Evict(collName string) {
|
|
c.collections.Remove(collName)
|
|
}
|
|
|
|
// Reset clears all cached info, used when client switching env.
|
|
func (c *CollectionCache) Reset() {
|
|
c.collections = typeutil.NewConcurrentMap[string, *entity.Collection]()
|
|
}
|
|
|
|
func NewCollectionCache(fetcher func(context.Context, string) (*entity.Collection, error)) *CollectionCache {
|
|
return &CollectionCache{
|
|
collections: typeutil.NewConcurrentMap[string, *entity.Collection](),
|
|
fetcher: fetcher,
|
|
}
|
|
}
|
|
|
|
func (c *Client) getCollection(ctx context.Context, collName string) (*entity.Collection, error) {
|
|
return c.collCache.GetCollection(ctx, collName)
|
|
}
|
|
|
|
func (c *Client) retryIfSchemaError(ctx context.Context, collName string, work func(ctx context.Context) (uint64, error)) error {
|
|
var lastTs uint64 = math.MaxUint64
|
|
return retry.Handle(ctx, func() (bool, error) {
|
|
ts, err := work(ctx)
|
|
if err != nil {
|
|
// if schema error
|
|
if errors.Is(err, merr.ErrCollectionSchemaMismatch) {
|
|
sameTs := ts == lastTs
|
|
lastTs = ts
|
|
if !sameTs {
|
|
c.collCache.Evict(collName)
|
|
}
|
|
// retry if not same ts
|
|
return !sameTs, err
|
|
}
|
|
return false, err
|
|
}
|
|
return false, nil
|
|
})
|
|
}
|