2021-11-10 23:56:35 +08:00
|
|
|
// Licensed to the LF AI & Data foundation under one
|
|
|
|
// or more contributor license agreements. See the NOTICE file
|
|
|
|
// distributed with this work for additional information
|
|
|
|
// regarding copyright ownership. The ASF licenses this file
|
|
|
|
// to you under the Apache License, Version 2.0 (the
|
|
|
|
// "License"); you may not use this file except in compliance
|
2021-04-19 10:09:43 +08:00
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
2021-11-10 23:56:35 +08:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2021-04-19 10:09:43 +08:00
|
|
|
//
|
2021-11-10 23:56:35 +08:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2021-04-19 10:09:43 +08:00
|
|
|
|
2021-06-22 14:40:07 +08:00
|
|
|
package proxy
|
2021-01-31 14:55:36 +08:00
|
|
|
|
|
|
|
import (
|
2021-02-26 17:44:24 +08:00
|
|
|
"context"
|
2021-09-12 17:54:01 +08:00
|
|
|
"fmt"
|
2022-12-14 20:45:23 +08:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
2021-01-31 14:55:36 +08:00
|
|
|
"testing"
|
2022-12-14 20:45:23 +08:00
|
|
|
"time"
|
2021-01-31 14:55:36 +08:00
|
|
|
|
2023-02-26 11:31:49 +08:00
|
|
|
"github.com/cockroachdb/errors"
|
2022-05-19 10:13:56 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-02-16 15:38:34 +08:00
|
|
|
"github.com/stretchr/testify/mock"
|
2022-05-19 10:13:56 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-06-25 17:20:43 +08:00
|
|
|
uatomic "go.uber.org/atomic"
|
2023-09-26 09:57:25 +08:00
|
|
|
"google.golang.org/grpc"
|
2022-05-19 10:13:56 +08:00
|
|
|
|
2023-06-09 01:28:37 +08:00
|
|
|
"github.com/milvus-io/milvus-proto/go-api/v2/commonpb"
|
|
|
|
"github.com/milvus-io/milvus-proto/go-api/v2/milvuspb"
|
|
|
|
"github.com/milvus-io/milvus-proto/go-api/v2/schemapb"
|
2023-07-14 10:12:31 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/mocks"
|
2022-08-04 11:04:34 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/internalpb"
|
2022-05-19 10:13:56 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/querypb"
|
2022-04-20 16:15:41 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/rootcoordpb"
|
2021-09-12 17:54:01 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/types"
|
2023-04-06 19:14:32 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/crypto"
|
2023-06-25 17:20:43 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/funcutil"
|
2023-09-04 09:57:09 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/merr"
|
2023-06-25 17:20:43 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/paramtable"
|
2023-04-06 19:14:32 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/typeutil"
|
2021-01-31 14:55:36 +08:00
|
|
|
)
|
|
|
|
|
2023-06-25 17:20:43 +08:00
|
|
|
var dbName = GetCurDBNameFromContextOrDefault(context.Background())
|
|
|
|
|
2021-06-21 17:28:03 +08:00
|
|
|
type MockRootCoordClientInterface struct {
|
2023-09-26 09:57:25 +08:00
|
|
|
types.RootCoordClient
|
2021-09-12 17:54:01 +08:00
|
|
|
Error bool
|
2022-12-14 20:45:23 +08:00
|
|
|
AccessCount int32
|
2022-08-04 11:04:34 +08:00
|
|
|
|
|
|
|
listPolicy func(ctx context.Context, in *internalpb.ListPolicyRequest) (*internalpb.ListPolicyResponse, error)
|
2021-01-31 14:55:36 +08:00
|
|
|
}
|
|
|
|
|
2022-12-14 20:45:23 +08:00
|
|
|
func (m *MockRootCoordClientInterface) IncAccessCount() {
|
|
|
|
atomic.AddInt32(&m.AccessCount, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MockRootCoordClientInterface) GetAccessCount() int {
|
|
|
|
ret := atomic.LoadInt32(&m.AccessCount)
|
|
|
|
return int(ret)
|
|
|
|
}
|
|
|
|
|
2023-09-26 09:57:25 +08:00
|
|
|
func (m *MockRootCoordClientInterface) ShowPartitions(ctx context.Context, in *milvuspb.ShowPartitionsRequest, opts ...grpc.CallOption) (*milvuspb.ShowPartitionsResponse, error) {
|
2021-09-12 17:54:01 +08:00
|
|
|
if m.Error {
|
|
|
|
return nil, errors.New("mocked error")
|
|
|
|
}
|
2021-01-31 14:55:36 +08:00
|
|
|
if in.CollectionName == "collection1" {
|
2021-03-12 14:22:09 +08:00
|
|
|
return &milvuspb.ShowPartitionsResponse{
|
2023-09-20 10:57:23 +08:00
|
|
|
Status: merr.Status(nil),
|
2021-09-12 17:54:01 +08:00
|
|
|
PartitionIDs: []typeutil.UniqueID{1, 2},
|
|
|
|
CreatedTimestamps: []uint64{100, 200},
|
|
|
|
CreatedUtcTimestamps: []uint64{100, 200},
|
|
|
|
PartitionNames: []string{"par1", "par2"},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
if in.CollectionName == "collection2" {
|
|
|
|
return &milvuspb.ShowPartitionsResponse{
|
2023-09-20 10:57:23 +08:00
|
|
|
Status: merr.Status(nil),
|
2021-09-12 17:54:01 +08:00
|
|
|
PartitionIDs: []typeutil.UniqueID{3, 4},
|
|
|
|
CreatedTimestamps: []uint64{201, 202},
|
|
|
|
CreatedUtcTimestamps: []uint64{201, 202},
|
|
|
|
PartitionNames: []string{"par1", "par2"},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
if in.CollectionName == "errorCollection" {
|
|
|
|
return &milvuspb.ShowPartitionsResponse{
|
2023-09-20 10:57:23 +08:00
|
|
|
Status: merr.Status(nil),
|
2021-09-12 17:54:01 +08:00
|
|
|
PartitionIDs: []typeutil.UniqueID{5, 6},
|
|
|
|
CreatedTimestamps: []uint64{201},
|
|
|
|
CreatedUtcTimestamps: []uint64{201},
|
|
|
|
PartitionNames: []string{"par1", "par2"},
|
2021-01-31 14:55:36 +08:00
|
|
|
}, nil
|
|
|
|
}
|
2021-03-12 14:22:09 +08:00
|
|
|
return &milvuspb.ShowPartitionsResponse{
|
2021-01-31 14:55:36 +08:00
|
|
|
Status: &commonpb.Status{
|
2021-09-12 17:54:01 +08:00
|
|
|
ErrorCode: commonpb.ErrorCode_UnexpectedError,
|
2021-01-31 14:55:36 +08:00
|
|
|
},
|
2021-09-12 17:54:01 +08:00
|
|
|
PartitionIDs: []typeutil.UniqueID{},
|
|
|
|
CreatedTimestamps: []uint64{},
|
|
|
|
CreatedUtcTimestamps: []uint64{},
|
|
|
|
PartitionNames: []string{},
|
2021-01-31 14:55:36 +08:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-09-26 09:57:25 +08:00
|
|
|
func (m *MockRootCoordClientInterface) DescribeCollection(ctx context.Context, in *milvuspb.DescribeCollectionRequest, opts ...grpc.CallOption) (*milvuspb.DescribeCollectionResponse, error) {
|
2021-09-12 17:54:01 +08:00
|
|
|
if m.Error {
|
|
|
|
return nil, errors.New("mocked error")
|
|
|
|
}
|
2022-12-14 20:45:23 +08:00
|
|
|
m.IncAccessCount()
|
2023-01-30 10:19:48 +08:00
|
|
|
if in.CollectionName == "collection1" || in.CollectionID == 1 {
|
2021-01-31 14:55:36 +08:00
|
|
|
return &milvuspb.DescribeCollectionResponse{
|
2023-09-20 10:57:23 +08:00
|
|
|
Status: merr.Status(nil),
|
2021-01-31 14:55:36 +08:00
|
|
|
CollectionID: typeutil.UniqueID(1),
|
|
|
|
Schema: &schemapb.CollectionSchema{
|
|
|
|
AutoID: true,
|
2023-01-30 10:19:48 +08:00
|
|
|
Name: "collection1",
|
2021-01-31 14:55:36 +08:00
|
|
|
},
|
2023-06-25 17:20:43 +08:00
|
|
|
DbName: dbName,
|
2021-01-31 14:55:36 +08:00
|
|
|
}, nil
|
|
|
|
}
|
2023-01-30 10:19:48 +08:00
|
|
|
if in.CollectionName == "collection2" || in.CollectionID == 2 {
|
2021-09-12 17:54:01 +08:00
|
|
|
return &milvuspb.DescribeCollectionResponse{
|
2023-09-20 10:57:23 +08:00
|
|
|
Status: merr.Status(nil),
|
2021-09-12 17:54:01 +08:00
|
|
|
CollectionID: typeutil.UniqueID(2),
|
|
|
|
Schema: &schemapb.CollectionSchema{
|
|
|
|
AutoID: true,
|
2023-01-30 10:19:48 +08:00
|
|
|
Name: "collection2",
|
2021-09-12 17:54:01 +08:00
|
|
|
},
|
2023-06-25 17:20:43 +08:00
|
|
|
DbName: dbName,
|
2021-09-12 17:54:01 +08:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
if in.CollectionName == "errorCollection" {
|
|
|
|
return &milvuspb.DescribeCollectionResponse{
|
2023-09-20 10:57:23 +08:00
|
|
|
Status: merr.Status(nil),
|
2021-09-12 17:54:01 +08:00
|
|
|
CollectionID: typeutil.UniqueID(3),
|
|
|
|
Schema: &schemapb.CollectionSchema{
|
|
|
|
AutoID: true,
|
|
|
|
},
|
2023-06-25 17:20:43 +08:00
|
|
|
DbName: dbName,
|
2021-09-12 17:54:01 +08:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-09-04 09:57:09 +08:00
|
|
|
err := merr.WrapErrCollectionNotFound(in.CollectionName)
|
2021-01-31 14:55:36 +08:00
|
|
|
return &milvuspb.DescribeCollectionResponse{
|
2023-09-04 09:57:09 +08:00
|
|
|
Status: merr.Status(err),
|
2021-09-12 17:54:01 +08:00
|
|
|
Schema: nil,
|
2021-01-31 14:55:36 +08:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-09-26 09:57:25 +08:00
|
|
|
func (m *MockRootCoordClientInterface) GetCredential(ctx context.Context, req *rootcoordpb.GetCredentialRequest, opts ...grpc.CallOption) (*rootcoordpb.GetCredentialResponse, error) {
|
2022-04-11 19:49:34 +08:00
|
|
|
if m.Error {
|
|
|
|
return nil, errors.New("mocked error")
|
|
|
|
}
|
2022-12-14 20:45:23 +08:00
|
|
|
m.IncAccessCount()
|
2022-04-11 19:49:34 +08:00
|
|
|
if req.Username == "mockUser" {
|
|
|
|
encryptedPassword, _ := crypto.PasswordEncrypt("mockPass")
|
|
|
|
return &rootcoordpb.GetCredentialResponse{
|
2023-09-20 10:57:23 +08:00
|
|
|
Status: merr.Status(nil),
|
2022-04-11 19:49:34 +08:00
|
|
|
Username: "mockUser",
|
|
|
|
Password: encryptedPassword,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err := fmt.Errorf("can't find credential: " + req.Username)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-09-26 09:57:25 +08:00
|
|
|
func (m *MockRootCoordClientInterface) ListCredUsers(ctx context.Context, req *milvuspb.ListCredUsersRequest, opts ...grpc.CallOption) (*milvuspb.ListCredUsersResponse, error) {
|
2022-04-11 19:49:34 +08:00
|
|
|
if m.Error {
|
|
|
|
return nil, errors.New("mocked error")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &milvuspb.ListCredUsersResponse{
|
2023-09-20 10:57:23 +08:00
|
|
|
Status: merr.Status(nil),
|
2022-04-11 19:49:34 +08:00
|
|
|
Usernames: []string{"mockUser"},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-09-26 09:57:25 +08:00
|
|
|
func (m *MockRootCoordClientInterface) ListPolicy(ctx context.Context, in *internalpb.ListPolicyRequest, opts ...grpc.CallOption) (*internalpb.ListPolicyResponse, error) {
|
2022-08-04 11:04:34 +08:00
|
|
|
if m.listPolicy != nil {
|
|
|
|
return m.listPolicy(ctx, in)
|
|
|
|
}
|
|
|
|
return &internalpb.ListPolicyResponse{
|
2023-09-20 10:57:23 +08:00
|
|
|
Status: merr.Status(nil),
|
2022-08-04 11:04:34 +08:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-01-30 10:19:48 +08:00
|
|
|
// Simulate the cache path and the
|
2021-01-31 14:55:36 +08:00
|
|
|
func TestMetaCache_GetCollection(t *testing.T) {
|
2021-02-26 17:44:24 +08:00
|
|
|
ctx := context.Background()
|
2022-05-19 10:13:56 +08:00
|
|
|
rootCoord := &MockRootCoordClientInterface{}
|
2023-09-26 09:57:25 +08:00
|
|
|
queryCoord := &mocks.MockQueryCoordClient{}
|
2022-06-02 12:16:03 +08:00
|
|
|
mgr := newShardClientMgr()
|
2022-08-04 11:04:34 +08:00
|
|
|
err := InitMetaCache(ctx, rootCoord, queryCoord, mgr)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-01-31 14:55:36 +08:00
|
|
|
|
2023-06-25 17:20:43 +08:00
|
|
|
id, err := globalMetaCache.GetCollectionID(ctx, dbName, "collection1")
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-01-31 14:55:36 +08:00
|
|
|
assert.Equal(t, id, typeutil.UniqueID(1))
|
2022-12-14 20:45:23 +08:00
|
|
|
assert.Equal(t, rootCoord.GetAccessCount(), 1)
|
2021-09-12 17:54:01 +08:00
|
|
|
|
|
|
|
// should'nt be accessed to remote root coord.
|
2023-06-25 17:20:43 +08:00
|
|
|
schema, err := globalMetaCache.GetCollectionSchema(ctx, dbName, "collection1")
|
2022-12-14 20:45:23 +08:00
|
|
|
assert.Equal(t, rootCoord.GetAccessCount(), 1)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-01-31 14:55:36 +08:00
|
|
|
assert.Equal(t, schema, &schemapb.CollectionSchema{
|
|
|
|
AutoID: true,
|
2021-09-12 17:54:01 +08:00
|
|
|
Fields: []*schemapb.FieldSchema{},
|
2023-01-30 10:19:48 +08:00
|
|
|
Name: "collection1",
|
2021-01-31 14:55:36 +08:00
|
|
|
})
|
2023-06-25 17:20:43 +08:00
|
|
|
id, err = globalMetaCache.GetCollectionID(ctx, dbName, "collection2")
|
2022-12-14 20:45:23 +08:00
|
|
|
assert.Equal(t, rootCoord.GetAccessCount(), 2)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-09-12 17:54:01 +08:00
|
|
|
assert.Equal(t, id, typeutil.UniqueID(2))
|
2023-06-25 17:20:43 +08:00
|
|
|
schema, err = globalMetaCache.GetCollectionSchema(ctx, dbName, "collection2")
|
2022-12-14 20:45:23 +08:00
|
|
|
assert.Equal(t, rootCoord.GetAccessCount(), 2)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-09-12 17:54:01 +08:00
|
|
|
assert.Equal(t, schema, &schemapb.CollectionSchema{
|
|
|
|
AutoID: true,
|
|
|
|
Fields: []*schemapb.FieldSchema{},
|
2023-01-30 10:19:48 +08:00
|
|
|
Name: "collection2",
|
2021-09-12 17:54:01 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
// test to get from cache, this should trigger root request
|
2023-06-25 17:20:43 +08:00
|
|
|
id, err = globalMetaCache.GetCollectionID(ctx, dbName, "collection1")
|
2022-12-14 20:45:23 +08:00
|
|
|
assert.Equal(t, rootCoord.GetAccessCount(), 2)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-09-12 17:54:01 +08:00
|
|
|
assert.Equal(t, id, typeutil.UniqueID(1))
|
2023-06-25 17:20:43 +08:00
|
|
|
schema, err = globalMetaCache.GetCollectionSchema(ctx, dbName, "collection1")
|
2022-12-14 20:45:23 +08:00
|
|
|
assert.Equal(t, rootCoord.GetAccessCount(), 2)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-09-12 17:54:01 +08:00
|
|
|
assert.Equal(t, schema, &schemapb.CollectionSchema{
|
|
|
|
AutoID: true,
|
|
|
|
Fields: []*schemapb.FieldSchema{},
|
2023-01-30 10:19:48 +08:00
|
|
|
Name: "collection1",
|
|
|
|
})
|
2023-08-04 17:47:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMetaCache_GetBasicCollectionInfo(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
rootCoord := &MockRootCoordClientInterface{}
|
2023-09-26 09:57:25 +08:00
|
|
|
queryCoord := &mocks.MockQueryCoordClient{}
|
2023-08-04 17:47:07 +08:00
|
|
|
mgr := newShardClientMgr()
|
|
|
|
err := InitMetaCache(ctx, rootCoord, queryCoord, mgr)
|
|
|
|
assert.NoError(t, err)
|
2023-01-30 10:19:48 +08:00
|
|
|
|
2023-08-04 17:47:07 +08:00
|
|
|
// should be no data race.
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(2)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
info, err := globalMetaCache.GetCollectionInfo(ctx, dbName, "collection1", 1)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, info.collID, int64(1))
|
|
|
|
_ = info.consistencyLevel
|
|
|
|
_ = info.createdTimestamp
|
|
|
|
_ = info.createdUtcTimestamp
|
|
|
|
_ = info.partInfo
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
info, err := globalMetaCache.GetCollectionInfo(ctx, dbName, "collection1", 1)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, info.collID, int64(1))
|
|
|
|
_ = info.consistencyLevel
|
|
|
|
_ = info.createdTimestamp
|
|
|
|
_ = info.createdUtcTimestamp
|
|
|
|
_ = info.partInfo
|
|
|
|
}()
|
|
|
|
wg.Wait()
|
2023-01-30 10:19:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMetaCache_GetCollectionName(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
rootCoord := &MockRootCoordClientInterface{}
|
2023-09-26 09:57:25 +08:00
|
|
|
queryCoord := &mocks.MockQueryCoordClient{}
|
2023-01-30 10:19:48 +08:00
|
|
|
mgr := newShardClientMgr()
|
|
|
|
err := InitMetaCache(ctx, rootCoord, queryCoord, mgr)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2023-01-30 10:19:48 +08:00
|
|
|
|
2023-08-10 18:43:16 +08:00
|
|
|
collection, err := globalMetaCache.GetCollectionName(ctx, GetCurDBNameFromContextOrDefault(ctx), 1)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2023-01-30 10:19:48 +08:00
|
|
|
assert.Equal(t, collection, "collection1")
|
|
|
|
assert.Equal(t, rootCoord.GetAccessCount(), 1)
|
|
|
|
|
|
|
|
// should'nt be accessed to remote root coord.
|
2023-06-25 17:20:43 +08:00
|
|
|
schema, err := globalMetaCache.GetCollectionSchema(ctx, dbName, "collection1")
|
2023-01-30 10:19:48 +08:00
|
|
|
assert.Equal(t, rootCoord.GetAccessCount(), 1)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2023-01-30 10:19:48 +08:00
|
|
|
assert.Equal(t, schema, &schemapb.CollectionSchema{
|
|
|
|
AutoID: true,
|
|
|
|
Fields: []*schemapb.FieldSchema{},
|
|
|
|
Name: "collection1",
|
|
|
|
})
|
2023-08-10 18:43:16 +08:00
|
|
|
collection, err = globalMetaCache.GetCollectionName(ctx, GetCurDBNameFromContextOrDefault(ctx), 1)
|
2023-01-30 10:19:48 +08:00
|
|
|
assert.Equal(t, rootCoord.GetAccessCount(), 1)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2023-01-30 10:19:48 +08:00
|
|
|
assert.Equal(t, collection, "collection1")
|
2023-06-25 17:20:43 +08:00
|
|
|
schema, err = globalMetaCache.GetCollectionSchema(ctx, dbName, "collection2")
|
2023-01-30 10:19:48 +08:00
|
|
|
assert.Equal(t, rootCoord.GetAccessCount(), 2)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2023-01-30 10:19:48 +08:00
|
|
|
assert.Equal(t, schema, &schemapb.CollectionSchema{
|
|
|
|
AutoID: true,
|
|
|
|
Fields: []*schemapb.FieldSchema{},
|
|
|
|
Name: "collection2",
|
2021-09-12 17:54:01 +08:00
|
|
|
})
|
|
|
|
|
2023-01-30 10:19:48 +08:00
|
|
|
// test to get from cache, this should trigger root request
|
2023-08-10 18:43:16 +08:00
|
|
|
collection, err = globalMetaCache.GetCollectionName(ctx, GetCurDBNameFromContextOrDefault(ctx), 1)
|
2023-01-30 10:19:48 +08:00
|
|
|
assert.Equal(t, rootCoord.GetAccessCount(), 2)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2023-01-30 10:19:48 +08:00
|
|
|
assert.Equal(t, collection, "collection1")
|
2023-06-25 17:20:43 +08:00
|
|
|
schema, err = globalMetaCache.GetCollectionSchema(ctx, dbName, "collection1")
|
2023-01-30 10:19:48 +08:00
|
|
|
assert.Equal(t, rootCoord.GetAccessCount(), 2)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2023-01-30 10:19:48 +08:00
|
|
|
assert.Equal(t, schema, &schemapb.CollectionSchema{
|
|
|
|
AutoID: true,
|
|
|
|
Fields: []*schemapb.FieldSchema{},
|
|
|
|
Name: "collection1",
|
|
|
|
})
|
2021-09-12 17:54:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMetaCache_GetCollectionFailure(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2022-05-19 10:13:56 +08:00
|
|
|
rootCoord := &MockRootCoordClientInterface{}
|
2023-09-26 09:57:25 +08:00
|
|
|
queryCoord := &mocks.MockQueryCoordClient{}
|
2022-06-02 12:16:03 +08:00
|
|
|
mgr := newShardClientMgr()
|
2022-08-04 11:04:34 +08:00
|
|
|
err := InitMetaCache(ctx, rootCoord, queryCoord, mgr)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2022-05-19 10:13:56 +08:00
|
|
|
rootCoord.Error = true
|
2021-09-12 17:54:01 +08:00
|
|
|
|
2023-06-25 17:20:43 +08:00
|
|
|
schema, err := globalMetaCache.GetCollectionSchema(ctx, dbName, "collection1")
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.Error(t, err)
|
2021-09-12 17:54:01 +08:00
|
|
|
assert.Nil(t, schema)
|
|
|
|
|
2022-05-19 10:13:56 +08:00
|
|
|
rootCoord.Error = false
|
2021-09-12 17:54:01 +08:00
|
|
|
|
2023-06-25 17:20:43 +08:00
|
|
|
schema, err = globalMetaCache.GetCollectionSchema(ctx, dbName, "collection1")
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-09-12 17:54:01 +08:00
|
|
|
assert.Equal(t, schema, &schemapb.CollectionSchema{
|
|
|
|
AutoID: true,
|
|
|
|
Fields: []*schemapb.FieldSchema{},
|
2023-01-30 10:19:48 +08:00
|
|
|
Name: "collection1",
|
2021-09-12 17:54:01 +08:00
|
|
|
})
|
|
|
|
|
2022-05-19 10:13:56 +08:00
|
|
|
rootCoord.Error = true
|
2021-09-12 17:54:01 +08:00
|
|
|
// should be cached with no error
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-09-12 17:54:01 +08:00
|
|
|
assert.Equal(t, schema, &schemapb.CollectionSchema{
|
|
|
|
AutoID: true,
|
|
|
|
Fields: []*schemapb.FieldSchema{},
|
2023-01-30 10:19:48 +08:00
|
|
|
Name: "collection1",
|
2021-09-12 17:54:01 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMetaCache_GetNonExistCollection(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2022-05-19 10:13:56 +08:00
|
|
|
rootCoord := &MockRootCoordClientInterface{}
|
2023-09-26 09:57:25 +08:00
|
|
|
queryCoord := &mocks.MockQueryCoordClient{}
|
2022-06-02 12:16:03 +08:00
|
|
|
mgr := newShardClientMgr()
|
2022-08-04 11:04:34 +08:00
|
|
|
err := InitMetaCache(ctx, rootCoord, queryCoord, mgr)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-09-12 17:54:01 +08:00
|
|
|
|
2023-06-25 17:20:43 +08:00
|
|
|
id, err := globalMetaCache.GetCollectionID(ctx, dbName, "collection3")
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.Error(t, err)
|
2021-09-12 17:54:01 +08:00
|
|
|
assert.Equal(t, id, int64(0))
|
2023-06-25 17:20:43 +08:00
|
|
|
schema, err := globalMetaCache.GetCollectionSchema(ctx, dbName, "collection3")
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.Error(t, err)
|
2021-01-31 14:55:36 +08:00
|
|
|
assert.Nil(t, schema)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMetaCache_GetPartitionID(t *testing.T) {
|
2021-02-26 17:44:24 +08:00
|
|
|
ctx := context.Background()
|
2022-05-19 10:13:56 +08:00
|
|
|
rootCoord := &MockRootCoordClientInterface{}
|
2023-09-26 09:57:25 +08:00
|
|
|
queryCoord := &mocks.MockQueryCoordClient{}
|
2022-06-02 12:16:03 +08:00
|
|
|
mgr := newShardClientMgr()
|
2022-08-04 11:04:34 +08:00
|
|
|
err := InitMetaCache(ctx, rootCoord, queryCoord, mgr)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-01-31 14:55:36 +08:00
|
|
|
|
2023-06-25 17:20:43 +08:00
|
|
|
id, err := globalMetaCache.GetPartitionID(ctx, dbName, "collection1", "par1")
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-01-31 14:55:36 +08:00
|
|
|
assert.Equal(t, id, typeutil.UniqueID(1))
|
2023-06-25 17:20:43 +08:00
|
|
|
id, err = globalMetaCache.GetPartitionID(ctx, dbName, "collection1", "par2")
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-01-31 14:55:36 +08:00
|
|
|
assert.Equal(t, id, typeutil.UniqueID(2))
|
2023-06-25 17:20:43 +08:00
|
|
|
id, err = globalMetaCache.GetPartitionID(ctx, dbName, "collection2", "par1")
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-09-12 17:54:01 +08:00
|
|
|
assert.Equal(t, id, typeutil.UniqueID(3))
|
2023-06-25 17:20:43 +08:00
|
|
|
id, err = globalMetaCache.GetPartitionID(ctx, dbName, "collection2", "par2")
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-09-12 17:54:01 +08:00
|
|
|
assert.Equal(t, id, typeutil.UniqueID(4))
|
|
|
|
}
|
|
|
|
|
2022-12-14 20:45:23 +08:00
|
|
|
func TestMetaCache_ConcurrentTest1(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
rootCoord := &MockRootCoordClientInterface{}
|
2023-09-26 09:57:25 +08:00
|
|
|
queryCoord := &mocks.MockQueryCoordClient{}
|
2022-12-14 20:45:23 +08:00
|
|
|
mgr := newShardClientMgr()
|
|
|
|
err := InitMetaCache(ctx, rootCoord, queryCoord, mgr)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2022-12-14 20:45:23 +08:00
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
cnt := 100
|
|
|
|
getCollectionCacheFunc := func(wg *sync.WaitGroup) {
|
|
|
|
defer wg.Done()
|
|
|
|
for i := 0; i < cnt; i++ {
|
2023-08-04 17:47:07 +08:00
|
|
|
// GetCollectionSchema will never fail
|
2023-06-25 17:20:43 +08:00
|
|
|
schema, err := globalMetaCache.GetCollectionSchema(ctx, dbName, "collection1")
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2022-12-14 20:45:23 +08:00
|
|
|
assert.Equal(t, schema, &schemapb.CollectionSchema{
|
|
|
|
AutoID: true,
|
|
|
|
Fields: []*schemapb.FieldSchema{},
|
2023-01-30 10:19:48 +08:00
|
|
|
Name: "collection1",
|
2022-12-14 20:45:23 +08:00
|
|
|
})
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getPartitionCacheFunc := func(wg *sync.WaitGroup) {
|
|
|
|
defer wg.Done()
|
|
|
|
for i := 0; i < cnt; i++ {
|
2023-08-04 17:47:07 +08:00
|
|
|
// GetPartitions may fail
|
2023-06-25 17:20:43 +08:00
|
|
|
globalMetaCache.GetPartitions(ctx, dbName, "collection1")
|
2022-12-14 20:45:23 +08:00
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
invalidCacheFunc := func(wg *sync.WaitGroup) {
|
|
|
|
defer wg.Done()
|
|
|
|
for i := 0; i < cnt; i++ {
|
2023-08-04 17:47:07 +08:00
|
|
|
// periodically invalid collection cache
|
2023-06-25 17:20:43 +08:00
|
|
|
globalMetaCache.RemoveCollection(ctx, dbName, "collection1")
|
2022-12-14 20:45:23 +08:00
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go getCollectionCacheFunc(&wg)
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go invalidCacheFunc(&wg)
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go getPartitionCacheFunc(&wg)
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2021-09-12 17:54:01 +08:00
|
|
|
func TestMetaCache_GetPartitionError(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2022-05-19 10:13:56 +08:00
|
|
|
rootCoord := &MockRootCoordClientInterface{}
|
2023-09-26 09:57:25 +08:00
|
|
|
queryCoord := &mocks.MockQueryCoordClient{}
|
2022-06-02 12:16:03 +08:00
|
|
|
mgr := newShardClientMgr()
|
2022-08-04 11:04:34 +08:00
|
|
|
err := InitMetaCache(ctx, rootCoord, queryCoord, mgr)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-09-12 17:54:01 +08:00
|
|
|
|
|
|
|
// Test the case where ShowPartitionsResponse is not aligned
|
2023-06-25 17:20:43 +08:00
|
|
|
id, err := globalMetaCache.GetPartitionID(ctx, dbName, "errorCollection", "par1")
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.Error(t, err)
|
2021-01-31 14:55:36 +08:00
|
|
|
assert.Equal(t, id, typeutil.UniqueID(0))
|
2021-09-12 17:54:01 +08:00
|
|
|
|
2023-06-25 17:20:43 +08:00
|
|
|
partitions, err2 := globalMetaCache.GetPartitions(ctx, dbName, "errorCollection")
|
2021-09-12 17:54:01 +08:00
|
|
|
assert.NotNil(t, err2)
|
|
|
|
assert.Equal(t, len(partitions), 0)
|
|
|
|
|
|
|
|
// Test non existed tables
|
2023-06-25 17:20:43 +08:00
|
|
|
id, err = globalMetaCache.GetPartitionID(ctx, dbName, "nonExisted", "par1")
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.Error(t, err)
|
2021-01-31 14:55:36 +08:00
|
|
|
assert.Equal(t, id, typeutil.UniqueID(0))
|
2021-09-12 17:54:01 +08:00
|
|
|
|
|
|
|
// Test non existed partition
|
2023-06-25 17:20:43 +08:00
|
|
|
id, err = globalMetaCache.GetPartitionID(ctx, dbName, "collection1", "par3")
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.Error(t, err)
|
2021-01-31 14:55:36 +08:00
|
|
|
assert.Equal(t, id, typeutil.UniqueID(0))
|
|
|
|
}
|
2022-04-20 16:15:41 +08:00
|
|
|
|
|
|
|
func TestMetaCache_GetShards(t *testing.T) {
|
2022-08-04 11:04:34 +08:00
|
|
|
var (
|
|
|
|
ctx = context.Background()
|
|
|
|
collectionName = "collection1"
|
2023-08-01 17:33:06 +08:00
|
|
|
collectionID = int64(1)
|
2022-08-04 11:04:34 +08:00
|
|
|
)
|
|
|
|
|
2022-05-19 10:13:56 +08:00
|
|
|
rootCoord := &MockRootCoordClientInterface{}
|
2023-09-26 09:57:25 +08:00
|
|
|
qc := getQueryCoordClient()
|
2022-06-02 12:16:03 +08:00
|
|
|
shardMgr := newShardClientMgr()
|
2022-08-04 11:04:34 +08:00
|
|
|
err := InitMetaCache(ctx, rootCoord, qc, shardMgr)
|
2022-04-20 16:15:41 +08:00
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
t.Run("No collection in meta cache", func(t *testing.T) {
|
2023-08-01 17:33:06 +08:00
|
|
|
shards, err := globalMetaCache.GetShards(ctx, true, dbName, "non-exists", 0)
|
2022-04-20 16:15:41 +08:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Empty(t, shards)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("without shardLeaders in collection info invalid shardLeaders", func(t *testing.T) {
|
2023-02-16 15:38:34 +08:00
|
|
|
qc.EXPECT().GetShardLeaders(mock.Anything, mock.Anything).Return(&querypb.GetShardLeadersResponse{
|
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_UnexpectedError,
|
|
|
|
Reason: "not implemented",
|
|
|
|
},
|
|
|
|
}, nil).Times(1)
|
|
|
|
qc.EXPECT().ShowCollections(mock.Anything, mock.Anything).Return(&querypb.ShowCollectionsResponse{
|
|
|
|
Status: &commonpb.Status{ErrorCode: commonpb.ErrorCode_Success},
|
|
|
|
}, nil)
|
2023-08-01 17:33:06 +08:00
|
|
|
shards, err := globalMetaCache.GetShards(ctx, false, dbName, collectionName, collectionID)
|
2022-04-20 16:15:41 +08:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Empty(t, shards)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("without shardLeaders in collection info", func(t *testing.T) {
|
2023-02-16 15:38:34 +08:00
|
|
|
qc.EXPECT().GetShardLeaders(mock.Anything, mock.Anything).Return(&querypb.GetShardLeadersResponse{
|
2023-09-20 10:57:23 +08:00
|
|
|
Status: merr.Status(nil),
|
2023-02-16 15:38:34 +08:00
|
|
|
Shards: []*querypb.ShardLeadersList{
|
|
|
|
{
|
|
|
|
ChannelName: "channel-1",
|
|
|
|
NodeIds: []int64{1, 2, 3},
|
|
|
|
NodeAddrs: []string{"localhost:9000", "localhost:9001", "localhost:9002"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
qc.EXPECT().ShowCollections(mock.Anything, mock.Anything).Return(&querypb.ShowCollectionsResponse{
|
|
|
|
Status: &commonpb.Status{ErrorCode: commonpb.ErrorCode_Success},
|
|
|
|
}, nil)
|
2023-08-01 17:33:06 +08:00
|
|
|
shards, err := globalMetaCache.GetShards(ctx, true, dbName, collectionName, collectionID)
|
2022-04-20 16:15:41 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEmpty(t, shards)
|
|
|
|
assert.Equal(t, 1, len(shards))
|
2022-05-17 22:35:57 +08:00
|
|
|
assert.Equal(t, 3, len(shards["channel-1"]))
|
2022-04-20 16:15:41 +08:00
|
|
|
|
|
|
|
// get from cache
|
2023-02-16 15:38:34 +08:00
|
|
|
qc.EXPECT().GetShardLeaders(mock.Anything, mock.Anything).Return(&querypb.GetShardLeadersResponse{
|
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_UnexpectedError,
|
|
|
|
Reason: "not implemented",
|
|
|
|
},
|
|
|
|
}, nil)
|
2023-08-01 17:33:06 +08:00
|
|
|
shards, err = globalMetaCache.GetShards(ctx, true, dbName, collectionName, collectionID)
|
2022-08-23 10:44:52 +08:00
|
|
|
|
2022-04-20 16:15:41 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEmpty(t, shards)
|
|
|
|
assert.Equal(t, 1, len(shards))
|
2022-05-17 22:35:57 +08:00
|
|
|
assert.Equal(t, 3, len(shards["channel-1"]))
|
2022-04-20 16:15:41 +08:00
|
|
|
})
|
2022-05-17 11:11:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMetaCache_ClearShards(t *testing.T) {
|
2022-08-04 11:04:34 +08:00
|
|
|
var (
|
|
|
|
ctx = context.TODO()
|
|
|
|
collectionName = "collection1"
|
2023-08-01 17:33:06 +08:00
|
|
|
collectionID = int64(1)
|
2022-08-04 11:04:34 +08:00
|
|
|
)
|
|
|
|
|
2022-05-19 10:13:56 +08:00
|
|
|
rootCoord := &MockRootCoordClientInterface{}
|
2023-09-26 09:57:25 +08:00
|
|
|
qc := getQueryCoordClient()
|
2022-06-02 12:16:03 +08:00
|
|
|
mgr := newShardClientMgr()
|
2022-08-04 11:04:34 +08:00
|
|
|
err := InitMetaCache(ctx, rootCoord, qc, mgr)
|
2022-05-17 11:11:56 +08:00
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
t.Run("Clear with no collection info", func(t *testing.T) {
|
2023-06-25 17:20:43 +08:00
|
|
|
globalMetaCache.DeprecateShardCache(dbName, "collection_not_exist")
|
2022-05-17 11:11:56 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Clear valid collection empty cache", func(t *testing.T) {
|
2023-06-25 17:20:43 +08:00
|
|
|
globalMetaCache.DeprecateShardCache(dbName, collectionName)
|
2022-05-17 11:11:56 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Clear valid collection valid cache", func(t *testing.T) {
|
2023-02-16 15:38:34 +08:00
|
|
|
qc.EXPECT().GetShardLeaders(mock.Anything, mock.Anything).Return(&querypb.GetShardLeadersResponse{
|
2023-09-20 10:57:23 +08:00
|
|
|
Status: merr.Status(nil),
|
2023-02-16 15:38:34 +08:00
|
|
|
Shards: []*querypb.ShardLeadersList{
|
|
|
|
{
|
|
|
|
ChannelName: "channel-1",
|
|
|
|
NodeIds: []int64{1, 2, 3},
|
|
|
|
NodeAddrs: []string{"localhost:9000", "localhost:9001", "localhost:9002"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil).Times(1)
|
|
|
|
qc.EXPECT().ShowCollections(mock.Anything, mock.Anything).Return(&querypb.ShowCollectionsResponse{
|
|
|
|
Status: &commonpb.Status{ErrorCode: commonpb.ErrorCode_Success},
|
|
|
|
}, nil)
|
2023-08-01 17:33:06 +08:00
|
|
|
shards, err := globalMetaCache.GetShards(ctx, true, dbName, collectionName, collectionID)
|
2022-05-17 11:11:56 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, shards)
|
|
|
|
require.Equal(t, 1, len(shards))
|
2022-05-17 22:35:57 +08:00
|
|
|
require.Equal(t, 3, len(shards["channel-1"]))
|
2022-05-17 11:11:56 +08:00
|
|
|
|
2023-06-25 17:20:43 +08:00
|
|
|
globalMetaCache.DeprecateShardCache(dbName, collectionName)
|
2022-05-17 11:11:56 +08:00
|
|
|
|
2023-02-16 15:38:34 +08:00
|
|
|
qc.EXPECT().GetShardLeaders(mock.Anything, mock.Anything).Return(&querypb.GetShardLeadersResponse{
|
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_UnexpectedError,
|
|
|
|
Reason: "not implemented",
|
|
|
|
},
|
|
|
|
}, nil)
|
2023-08-01 17:33:06 +08:00
|
|
|
shards, err = globalMetaCache.GetShards(ctx, true, dbName, collectionName, collectionID)
|
2022-05-17 11:11:56 +08:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Empty(t, shards)
|
|
|
|
})
|
2022-08-04 11:04:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMetaCache_PolicyInfo(t *testing.T) {
|
|
|
|
client := &MockRootCoordClientInterface{}
|
2023-09-26 09:57:25 +08:00
|
|
|
qc := &mocks.MockQueryCoordClient{}
|
2022-08-04 11:04:34 +08:00
|
|
|
mgr := newShardClientMgr()
|
|
|
|
|
|
|
|
t.Run("InitMetaCache", func(t *testing.T) {
|
|
|
|
client.listPolicy = func(ctx context.Context, in *internalpb.ListPolicyRequest) (*internalpb.ListPolicyResponse, error) {
|
|
|
|
return nil, fmt.Errorf("mock error")
|
|
|
|
}
|
|
|
|
err := InitMetaCache(context.Background(), client, qc, mgr)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.Error(t, err)
|
2022-08-04 11:04:34 +08:00
|
|
|
|
|
|
|
client.listPolicy = func(ctx context.Context, in *internalpb.ListPolicyRequest) (*internalpb.ListPolicyResponse, error) {
|
|
|
|
return &internalpb.ListPolicyResponse{
|
2023-09-20 10:57:23 +08:00
|
|
|
Status: merr.Status(nil),
|
2022-08-04 11:04:34 +08:00
|
|
|
PolicyInfos: []string{"policy1", "policy2", "policy3"},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
err = InitMetaCache(context.Background(), client, qc, mgr)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2022-08-04 11:04:34 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("GetPrivilegeInfo", func(t *testing.T) {
|
|
|
|
client.listPolicy = func(ctx context.Context, in *internalpb.ListPolicyRequest) (*internalpb.ListPolicyResponse, error) {
|
|
|
|
return &internalpb.ListPolicyResponse{
|
2023-09-20 10:57:23 +08:00
|
|
|
Status: merr.Status(nil),
|
2022-08-04 11:04:34 +08:00
|
|
|
PolicyInfos: []string{"policy1", "policy2", "policy3"},
|
|
|
|
UserRoles: []string{funcutil.EncodeUserRoleCache("foo", "role1"), funcutil.EncodeUserRoleCache("foo", "role2"), funcutil.EncodeUserRoleCache("foo2", "role2")},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
err := InitMetaCache(context.Background(), client, qc, mgr)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2022-08-04 11:04:34 +08:00
|
|
|
policyInfos := globalMetaCache.GetPrivilegeInfo(context.Background())
|
|
|
|
assert.Equal(t, 3, len(policyInfos))
|
|
|
|
roles := globalMetaCache.GetUserRole("foo")
|
|
|
|
assert.Equal(t, 2, len(roles))
|
|
|
|
})
|
2022-04-20 16:15:41 +08:00
|
|
|
|
2022-08-04 11:04:34 +08:00
|
|
|
t.Run("GetPrivilegeInfo", func(t *testing.T) {
|
|
|
|
client.listPolicy = func(ctx context.Context, in *internalpb.ListPolicyRequest) (*internalpb.ListPolicyResponse, error) {
|
|
|
|
return &internalpb.ListPolicyResponse{
|
2023-09-20 10:57:23 +08:00
|
|
|
Status: merr.Status(nil),
|
2022-08-04 11:04:34 +08:00
|
|
|
PolicyInfos: []string{"policy1", "policy2", "policy3"},
|
|
|
|
UserRoles: []string{funcutil.EncodeUserRoleCache("foo", "role1"), funcutil.EncodeUserRoleCache("foo", "role2"), funcutil.EncodeUserRoleCache("foo2", "role2")},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
err := InitMetaCache(context.Background(), client, qc, mgr)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2022-08-04 11:04:34 +08:00
|
|
|
|
|
|
|
err = globalMetaCache.RefreshPolicyInfo(typeutil.CacheOp{OpType: typeutil.CacheGrantPrivilege, OpKey: "policyX"})
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2022-08-04 11:04:34 +08:00
|
|
|
policyInfos := globalMetaCache.GetPrivilegeInfo(context.Background())
|
|
|
|
assert.Equal(t, 4, len(policyInfos))
|
|
|
|
|
|
|
|
err = globalMetaCache.RefreshPolicyInfo(typeutil.CacheOp{OpType: typeutil.CacheRevokePrivilege, OpKey: "policyX"})
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2022-08-04 11:04:34 +08:00
|
|
|
policyInfos = globalMetaCache.GetPrivilegeInfo(context.Background())
|
|
|
|
assert.Equal(t, 3, len(policyInfos))
|
|
|
|
|
|
|
|
err = globalMetaCache.RefreshPolicyInfo(typeutil.CacheOp{OpType: typeutil.CacheAddUserToRole, OpKey: funcutil.EncodeUserRoleCache("foo", "role3")})
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2022-08-04 11:04:34 +08:00
|
|
|
roles := globalMetaCache.GetUserRole("foo")
|
|
|
|
assert.Equal(t, 3, len(roles))
|
|
|
|
|
|
|
|
err = globalMetaCache.RefreshPolicyInfo(typeutil.CacheOp{OpType: typeutil.CacheRemoveUserFromRole, OpKey: funcutil.EncodeUserRoleCache("foo", "role3")})
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2022-08-04 11:04:34 +08:00
|
|
|
roles = globalMetaCache.GetUserRole("foo")
|
|
|
|
assert.Equal(t, 2, len(roles))
|
|
|
|
|
|
|
|
err = globalMetaCache.RefreshPolicyInfo(typeutil.CacheOp{OpType: typeutil.CacheGrantPrivilege, OpKey: ""})
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.Error(t, err)
|
2022-08-04 11:04:34 +08:00
|
|
|
err = globalMetaCache.RefreshPolicyInfo(typeutil.CacheOp{OpType: 100, OpKey: "policyX"})
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.Error(t, err)
|
2022-08-04 11:04:34 +08:00
|
|
|
})
|
2023-08-07 11:59:07 +08:00
|
|
|
|
|
|
|
t.Run("Delete user or drop role", func(t *testing.T) {
|
|
|
|
client.listPolicy = func(ctx context.Context, in *internalpb.ListPolicyRequest) (*internalpb.ListPolicyResponse, error) {
|
|
|
|
return &internalpb.ListPolicyResponse{
|
2023-09-20 10:57:23 +08:00
|
|
|
Status: merr.Status(nil),
|
2023-08-07 11:59:07 +08:00
|
|
|
PolicyInfos: []string{"policy1", "policy2", "policy3"},
|
|
|
|
UserRoles: []string{funcutil.EncodeUserRoleCache("foo", "role1"), funcutil.EncodeUserRoleCache("foo", "role2"), funcutil.EncodeUserRoleCache("foo2", "role2"), funcutil.EncodeUserRoleCache("foo2", "role3")},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
err := InitMetaCache(context.Background(), client, qc, mgr)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = globalMetaCache.RefreshPolicyInfo(typeutil.CacheOp{OpType: typeutil.CacheDeleteUser, OpKey: "foo"})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
roles := globalMetaCache.GetUserRole("foo")
|
|
|
|
assert.Len(t, roles, 0)
|
|
|
|
|
|
|
|
roles = globalMetaCache.GetUserRole("foo2")
|
|
|
|
assert.Len(t, roles, 2)
|
|
|
|
|
|
|
|
err = globalMetaCache.RefreshPolicyInfo(typeutil.CacheOp{OpType: typeutil.CacheDropRole, OpKey: "role2"})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
roles = globalMetaCache.GetUserRole("foo2")
|
|
|
|
assert.Len(t, roles, 1)
|
|
|
|
assert.Equal(t, "role3", roles[0])
|
|
|
|
|
|
|
|
client.listPolicy = func(ctx context.Context, in *internalpb.ListPolicyRequest) (*internalpb.ListPolicyResponse, error) {
|
|
|
|
return &internalpb.ListPolicyResponse{
|
2023-09-20 10:57:23 +08:00
|
|
|
Status: merr.Status(nil),
|
2023-08-07 11:59:07 +08:00
|
|
|
PolicyInfos: []string{"policy1", "policy2", "policy3"},
|
|
|
|
UserRoles: []string{funcutil.EncodeUserRoleCache("foo", "role1"), funcutil.EncodeUserRoleCache("foo", "role2"), funcutil.EncodeUserRoleCache("foo2", "role2"), funcutil.EncodeUserRoleCache("foo2", "role3")},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
err = globalMetaCache.RefreshPolicyInfo(typeutil.CacheOp{OpType: typeutil.CacheRefresh})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
roles = globalMetaCache.GetUserRole("foo")
|
|
|
|
assert.Len(t, roles, 2)
|
|
|
|
})
|
2022-04-20 16:15:41 +08:00
|
|
|
}
|
2022-05-19 10:13:56 +08:00
|
|
|
|
|
|
|
func TestMetaCache_RemoveCollection(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
rootCoord := &MockRootCoordClientInterface{}
|
2023-09-26 09:57:25 +08:00
|
|
|
queryCoord := &mocks.MockQueryCoordClient{}
|
2022-06-02 12:16:03 +08:00
|
|
|
shardMgr := newShardClientMgr()
|
2022-08-04 11:04:34 +08:00
|
|
|
err := InitMetaCache(ctx, rootCoord, queryCoord, shardMgr)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2022-05-19 10:13:56 +08:00
|
|
|
|
2023-02-16 15:38:34 +08:00
|
|
|
queryCoord.EXPECT().ShowCollections(mock.Anything, mock.Anything).Return(&querypb.ShowCollectionsResponse{
|
2023-09-20 10:57:23 +08:00
|
|
|
Status: merr.Status(nil),
|
2023-02-16 15:38:34 +08:00
|
|
|
CollectionIDs: []UniqueID{1, 2},
|
|
|
|
InMemoryPercentages: []int64{100, 50},
|
|
|
|
}, nil)
|
|
|
|
|
2023-08-01 17:33:06 +08:00
|
|
|
_, err = globalMetaCache.GetCollectionInfo(ctx, dbName, "collection1", 1)
|
2022-05-19 10:13:56 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
// no collectionInfo of collection1, should access RootCoord
|
2022-12-14 20:45:23 +08:00
|
|
|
assert.Equal(t, rootCoord.GetAccessCount(), 1)
|
2022-05-19 10:13:56 +08:00
|
|
|
|
2023-08-01 17:33:06 +08:00
|
|
|
_, err = globalMetaCache.GetCollectionInfo(ctx, dbName, "collection1", 1)
|
2022-05-19 10:13:56 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
// shouldn't access RootCoord again
|
2022-12-14 20:45:23 +08:00
|
|
|
assert.Equal(t, rootCoord.GetAccessCount(), 1)
|
2022-05-19 10:13:56 +08:00
|
|
|
|
2023-06-25 17:20:43 +08:00
|
|
|
globalMetaCache.RemoveCollection(ctx, dbName, "collection1")
|
2022-05-19 10:13:56 +08:00
|
|
|
// no collectionInfo of collection2, should access RootCoord
|
2023-08-01 17:33:06 +08:00
|
|
|
_, err = globalMetaCache.GetCollectionInfo(ctx, dbName, "collection1", 1)
|
2022-05-19 10:13:56 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
// shouldn't access RootCoord again
|
2022-12-14 20:45:23 +08:00
|
|
|
assert.Equal(t, rootCoord.GetAccessCount(), 2)
|
2022-05-19 10:13:56 +08:00
|
|
|
|
|
|
|
globalMetaCache.RemoveCollectionsByID(ctx, UniqueID(1))
|
|
|
|
// no collectionInfo of collection2, should access RootCoord
|
2023-08-01 17:33:06 +08:00
|
|
|
_, err = globalMetaCache.GetCollectionInfo(ctx, dbName, "collection1", 1)
|
2022-05-19 10:13:56 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
// shouldn't access RootCoord again
|
2022-12-14 20:45:23 +08:00
|
|
|
assert.Equal(t, rootCoord.GetAccessCount(), 3)
|
2023-08-04 17:47:07 +08:00
|
|
|
|
|
|
|
globalMetaCache.RemoveCollectionsByID(ctx, UniqueID(1))
|
|
|
|
// no collectionInfo of collection2, should access RootCoord
|
|
|
|
_, err = globalMetaCache.GetCollectionInfo(ctx, dbName, "collection1", 1)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
// no collectionInfo of collection1, should access RootCoord
|
|
|
|
assert.Equal(t, rootCoord.GetAccessCount(), 4)
|
2022-05-19 10:13:56 +08:00
|
|
|
}
|
2023-03-08 15:13:51 +08:00
|
|
|
|
|
|
|
func TestMetaCache_ExpireShardLeaderCache(t *testing.T) {
|
2023-04-07 13:20:28 +08:00
|
|
|
paramtable.Init()
|
|
|
|
paramtable.Get().Save(Params.ProxyCfg.ShardLeaderCacheInterval.Key, "1")
|
|
|
|
|
2023-03-08 15:13:51 +08:00
|
|
|
ctx := context.Background()
|
|
|
|
rootCoord := &MockRootCoordClientInterface{}
|
2023-09-26 09:57:25 +08:00
|
|
|
queryCoord := &mocks.MockQueryCoordClient{}
|
2023-03-08 15:13:51 +08:00
|
|
|
shardMgr := newShardClientMgr()
|
|
|
|
err := InitMetaCache(ctx, rootCoord, queryCoord, shardMgr)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2023-03-08 15:13:51 +08:00
|
|
|
|
|
|
|
queryCoord.EXPECT().ShowCollections(mock.Anything, mock.Anything).Return(&querypb.ShowCollectionsResponse{
|
2023-09-20 10:57:23 +08:00
|
|
|
Status: merr.Status(nil),
|
2023-03-08 15:13:51 +08:00
|
|
|
CollectionIDs: []UniqueID{1},
|
|
|
|
InMemoryPercentages: []int64{100},
|
|
|
|
}, nil)
|
|
|
|
queryCoord.EXPECT().GetShardLeaders(mock.Anything, mock.Anything).Return(&querypb.GetShardLeadersResponse{
|
2023-09-20 10:57:23 +08:00
|
|
|
Status: merr.Status(nil),
|
2023-03-08 15:13:51 +08:00
|
|
|
Shards: []*querypb.ShardLeadersList{
|
|
|
|
{
|
|
|
|
ChannelName: "channel-1",
|
|
|
|
NodeIds: []int64{1, 2, 3},
|
|
|
|
NodeAddrs: []string{"localhost:9000", "localhost:9001", "localhost:9002"},
|
|
|
|
},
|
|
|
|
},
|
2023-03-28 19:06:01 +08:00
|
|
|
}, nil)
|
2023-08-01 17:33:06 +08:00
|
|
|
nodeInfos, err := globalMetaCache.GetShards(ctx, true, dbName, "collection1", 1)
|
2023-03-08 15:13:51 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, nodeInfos["channel-1"], 3)
|
|
|
|
|
2023-03-28 19:06:01 +08:00
|
|
|
queryCoord.ExpectedCalls = nil
|
2023-03-08 15:13:51 +08:00
|
|
|
queryCoord.EXPECT().GetShardLeaders(mock.Anything, mock.Anything).Return(&querypb.GetShardLeadersResponse{
|
2023-09-20 10:57:23 +08:00
|
|
|
Status: merr.Status(nil),
|
2023-03-08 15:13:51 +08:00
|
|
|
Shards: []*querypb.ShardLeadersList{
|
|
|
|
{
|
|
|
|
ChannelName: "channel-1",
|
|
|
|
NodeIds: []int64{1, 2},
|
|
|
|
NodeAddrs: []string{"localhost:9000", "localhost:9001"},
|
|
|
|
},
|
|
|
|
},
|
2023-03-28 19:06:01 +08:00
|
|
|
}, nil)
|
2023-03-08 15:13:51 +08:00
|
|
|
|
|
|
|
assert.Eventually(t, func() bool {
|
2023-08-01 17:33:06 +08:00
|
|
|
nodeInfos, err := globalMetaCache.GetShards(ctx, true, dbName, "collection1", 1)
|
2023-03-08 15:13:51 +08:00
|
|
|
assert.NoError(t, err)
|
2023-03-09 18:25:52 +08:00
|
|
|
return len(nodeInfos["channel-1"]) == 2
|
2023-03-08 15:13:51 +08:00
|
|
|
}, 3*time.Second, 1*time.Second)
|
|
|
|
|
2023-03-28 19:06:01 +08:00
|
|
|
queryCoord.ExpectedCalls = nil
|
2023-03-08 15:13:51 +08:00
|
|
|
queryCoord.EXPECT().GetShardLeaders(mock.Anything, mock.Anything).Return(&querypb.GetShardLeadersResponse{
|
2023-09-20 10:57:23 +08:00
|
|
|
Status: merr.Status(nil),
|
2023-03-08 15:13:51 +08:00
|
|
|
Shards: []*querypb.ShardLeadersList{
|
|
|
|
{
|
|
|
|
ChannelName: "channel-1",
|
|
|
|
NodeIds: []int64{1, 2, 3},
|
|
|
|
NodeAddrs: []string{"localhost:9000", "localhost:9001", "localhost:9002"},
|
|
|
|
},
|
|
|
|
},
|
2023-03-28 19:06:01 +08:00
|
|
|
}, nil)
|
2023-03-08 15:13:51 +08:00
|
|
|
|
|
|
|
assert.Eventually(t, func() bool {
|
2023-08-01 17:33:06 +08:00
|
|
|
nodeInfos, err := globalMetaCache.GetShards(ctx, true, dbName, "collection1", 1)
|
2023-03-08 15:13:51 +08:00
|
|
|
assert.NoError(t, err)
|
2023-03-10 10:19:53 +08:00
|
|
|
return len(nodeInfos["channel-1"]) == 3
|
2023-03-08 15:13:51 +08:00
|
|
|
}, 3*time.Second, 1*time.Second)
|
|
|
|
|
2023-03-28 19:06:01 +08:00
|
|
|
queryCoord.ExpectedCalls = nil
|
2023-03-08 15:13:51 +08:00
|
|
|
queryCoord.EXPECT().GetShardLeaders(mock.Anything, mock.Anything).Return(&querypb.GetShardLeadersResponse{
|
2023-09-20 10:57:23 +08:00
|
|
|
Status: merr.Status(nil),
|
2023-03-08 15:13:51 +08:00
|
|
|
Shards: []*querypb.ShardLeadersList{
|
|
|
|
{
|
|
|
|
ChannelName: "channel-1",
|
|
|
|
NodeIds: []int64{1, 2, 3},
|
|
|
|
NodeAddrs: []string{"localhost:9000", "localhost:9001", "localhost:9002"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ChannelName: "channel-2",
|
|
|
|
NodeIds: []int64{1, 2, 3},
|
|
|
|
NodeAddrs: []string{"localhost:9000", "localhost:9001", "localhost:9002"},
|
|
|
|
},
|
|
|
|
},
|
2023-03-28 19:06:01 +08:00
|
|
|
}, nil)
|
2023-03-08 15:13:51 +08:00
|
|
|
|
|
|
|
assert.Eventually(t, func() bool {
|
2023-08-01 17:33:06 +08:00
|
|
|
nodeInfos, err := globalMetaCache.GetShards(ctx, true, dbName, "collection1", 1)
|
2023-03-08 15:13:51 +08:00
|
|
|
assert.NoError(t, err)
|
2023-03-09 18:25:52 +08:00
|
|
|
return len(nodeInfos["channel-1"]) == 3 && len(nodeInfos["channel-2"]) == 3
|
2023-03-08 15:13:51 +08:00
|
|
|
}, 3*time.Second, 1*time.Second)
|
|
|
|
}
|
2023-03-16 14:25:55 +08:00
|
|
|
|
|
|
|
func TestGlobalMetaCache_ShuffleShardLeaders(t *testing.T) {
|
|
|
|
shards := map[string][]nodeInfo{
|
|
|
|
"channel-1": {
|
|
|
|
{
|
|
|
|
nodeID: 1,
|
|
|
|
address: "localhost:9000",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
nodeID: 2,
|
|
|
|
address: "localhost:9000",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
nodeID: 3,
|
|
|
|
address: "localhost:9000",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
sl := &shardLeaders{
|
|
|
|
deprecated: uatomic.NewBool(false),
|
|
|
|
idx: uatomic.NewInt64(5),
|
|
|
|
shardLeaders: shards,
|
|
|
|
}
|
|
|
|
|
|
|
|
reader := sl.GetReader()
|
|
|
|
result := reader.Shuffle()
|
|
|
|
assert.Len(t, result["channel-1"], 3)
|
|
|
|
assert.Equal(t, int64(1), result["channel-1"][0].nodeID)
|
|
|
|
|
|
|
|
reader = sl.GetReader()
|
|
|
|
result = reader.Shuffle()
|
|
|
|
assert.Len(t, result["channel-1"], 3)
|
|
|
|
assert.Equal(t, int64(2), result["channel-1"][0].nodeID)
|
|
|
|
|
|
|
|
reader = sl.GetReader()
|
|
|
|
result = reader.Shuffle()
|
|
|
|
assert.Len(t, result["channel-1"], 3)
|
|
|
|
assert.Equal(t, int64(3), result["channel-1"][0].nodeID)
|
|
|
|
}
|