congqixia 629da61617
enhance: [2.5][GoSDK] sync milvusclient patches for 2.5.1 (#40410)
Cherry pick from master
pr: #40268 #40284 #40328 #40373 #40381

------------------------------------------
#### fix: [GoSDK] Pass base64 passwd content instead of raw data
(#40268)

Related to #40261

Also add some options for create collection options and refine some
behavior

------------------------------------------
#### fix: [GoSDK] Return role without grants (#40284)

Related to #40274

Previousy DescribeRole returns only roles with grants, this PR add
select role action to check role existence.

Also added database properties related option

-----------------------------------------
#### fix: [GoSDK] Pass only valid data for nullable column (#40328)

Related to #40327

-----------------------------------------
#### enhance: [GoSDK] Add DescribeReplica API & sync rbac v2 (#40373)

Related to #31293 #37031

This PR:
- Add DescribeReplica API
- Add unified RBAC v2 API names(AddPrivilegesToGroup,
RemovePrivilegesFromGroup, GrantPrivilegeV2, RevokePrivilegeV2)
- Mark old ones deprecated

-----------------------------------------
#### enhance: [GoSDK] support update ts caching policy(#40381)

Related to #39093

This PR add update timestamp check and retry policy according to the
design of the related issue

-----------------------------------------

---------

Signed-off-by: Congqi Xia <congqi.xia@zilliz.com>
2025-03-06 17:48:03 +08:00

81 lines
2.3 KiB
Go

package milvusclient
import (
"context"
"math"
"github.com/cockroachdb/errors"
"github.com/milvus-io/milvus/client/v2/entity"
"github.com/milvus-io/milvus/pkg/v2/util/conc"
"github.com/milvus-io/milvus/pkg/v2/util/merr"
"github.com/milvus-io/milvus/pkg/v2/util/retry"
"github.com/milvus-io/milvus/pkg/v2/util/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
})
}