2021-12-16 10:07:10 +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 11:12:56 +08:00
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
2021-12-16 10:07:10 +08:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2021-04-19 11:12:56 +08:00
|
|
|
//
|
2021-12-16 10:07:10 +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-06-18 21:30:08 +08:00
|
|
|
package rootcoord
|
2021-01-21 10:01:29 +08:00
|
|
|
|
|
|
|
import (
|
2022-07-22 10:20:29 +08:00
|
|
|
"context"
|
2022-08-04 11:04:34 +08:00
|
|
|
"encoding/json"
|
2021-10-21 14:04:36 +08:00
|
|
|
"errors"
|
2021-01-21 10:01:29 +08:00
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
2021-10-26 23:00:27 +08:00
|
|
|
"path"
|
2022-08-04 11:04:34 +08:00
|
|
|
"strings"
|
2021-12-29 14:35:21 +08:00
|
|
|
"sync"
|
2021-01-21 10:01:29 +08:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-08-04 11:04:34 +08:00
|
|
|
"github.com/golang/protobuf/proto"
|
2022-07-22 10:20:29 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/common"
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/kv"
|
2021-10-21 14:04:36 +08:00
|
|
|
etcdkv "github.com/milvus-io/milvus/internal/kv/etcd"
|
2021-10-26 23:00:27 +08:00
|
|
|
memkv "github.com/milvus-io/milvus/internal/kv/mem"
|
2022-08-11 12:12:38 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/metastore"
|
2022-08-20 10:24:51 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/metastore/kv/rootcoord"
|
2022-07-22 10:20:29 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/metastore/model"
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/commonpb"
|
|
|
|
pb "github.com/milvus-io/milvus/internal/proto/etcdpb"
|
2022-07-22 10:20:29 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/internalpb"
|
2022-08-11 12:12:38 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/milvuspb"
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/schemapb"
|
2022-08-11 12:12:38 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util"
|
2021-12-29 14:35:21 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/etcd"
|
2022-08-11 12:12:38 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/funcutil"
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/typeutil"
|
2021-01-21 10:01:29 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2022-08-11 12:12:38 +08:00
|
|
|
"github.com/stretchr/testify/mock"
|
2021-12-29 14:35:21 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-08-11 12:12:38 +08:00
|
|
|
"go.uber.org/zap"
|
2021-01-21 10:01:29 +08:00
|
|
|
)
|
|
|
|
|
2021-04-09 16:10:12 +08:00
|
|
|
type mockTestKV struct {
|
2022-08-04 11:04:34 +08:00
|
|
|
kv.SnapShotKV
|
2021-04-09 16:10:12 +08:00
|
|
|
|
2021-05-18 14:18:02 +08:00
|
|
|
loadWithPrefix func(key string, ts typeutil.Timestamp) ([]string, []string, error)
|
2021-08-18 14:36:10 +08:00
|
|
|
save func(key, value string, ts typeutil.Timestamp) error
|
2021-10-13 15:54:33 +08:00
|
|
|
multiSave func(kvs map[string]string, ts typeutil.Timestamp) error
|
|
|
|
multiSaveAndRemoveWithPrefix func(saves map[string]string, removals []string, ts typeutil.Timestamp) error
|
2021-04-09 16:10:12 +08:00
|
|
|
}
|
|
|
|
|
2021-05-18 14:18:02 +08:00
|
|
|
func (m *mockTestKV) LoadWithPrefix(key string, ts typeutil.Timestamp) ([]string, []string, error) {
|
|
|
|
return m.loadWithPrefix(key, ts)
|
|
|
|
}
|
|
|
|
func (m *mockTestKV) Load(key string, ts typeutil.Timestamp) (string, error) {
|
|
|
|
return "", nil
|
2021-04-09 16:10:12 +08:00
|
|
|
}
|
|
|
|
|
2021-08-18 14:36:10 +08:00
|
|
|
func (m *mockTestKV) Save(key, value string, ts typeutil.Timestamp) error {
|
|
|
|
return m.save(key, value, ts)
|
2021-04-09 16:10:12 +08:00
|
|
|
}
|
|
|
|
|
2021-10-13 15:54:33 +08:00
|
|
|
func (m *mockTestKV) MultiSave(kvs map[string]string, ts typeutil.Timestamp) error {
|
|
|
|
return m.multiSave(kvs, ts)
|
2021-04-09 16:10:12 +08:00
|
|
|
}
|
|
|
|
|
2021-10-13 15:54:33 +08:00
|
|
|
func (m *mockTestKV) MultiSaveAndRemoveWithPrefix(saves map[string]string, removals []string, ts typeutil.Timestamp) error {
|
|
|
|
return m.multiSaveAndRemoveWithPrefix(saves, removals, ts)
|
2021-04-12 15:03:23 +08:00
|
|
|
}
|
2021-04-09 16:10:12 +08:00
|
|
|
|
2021-10-21 14:04:36 +08:00
|
|
|
type mockTestTxnKV struct {
|
|
|
|
kv.TxnKV
|
|
|
|
loadWithPrefix func(key string) ([]string, []string, error)
|
|
|
|
save func(key, value string) error
|
|
|
|
multiSave func(kvs map[string]string) error
|
|
|
|
multiSaveAndRemoveWithPrefix func(saves map[string]string, removals []string) error
|
2022-04-11 19:49:34 +08:00
|
|
|
remove func(key string) error
|
2022-06-17 18:08:12 +08:00
|
|
|
multiRemove func(keys []string) error
|
2022-08-04 11:04:34 +08:00
|
|
|
load func(key string) (string, error)
|
2021-10-21 14:04:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockTestTxnKV) LoadWithPrefix(key string) ([]string, []string, error) {
|
|
|
|
return m.loadWithPrefix(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockTestTxnKV) Save(key, value string) error {
|
|
|
|
return m.save(key, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockTestTxnKV) MultiSave(kvs map[string]string) error {
|
|
|
|
return m.multiSave(kvs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockTestTxnKV) MultiSaveAndRemoveWithPrefix(saves map[string]string, removals []string) error {
|
|
|
|
return m.multiSaveAndRemoveWithPrefix(saves, removals)
|
|
|
|
}
|
|
|
|
|
2022-04-11 19:49:34 +08:00
|
|
|
func (m *mockTestTxnKV) Remove(key string) error {
|
|
|
|
return m.remove(key)
|
|
|
|
}
|
|
|
|
|
2022-06-17 18:08:12 +08:00
|
|
|
func (m *mockTestTxnKV) MultiRemove(keys []string) error {
|
|
|
|
return m.multiRemove(keys)
|
|
|
|
}
|
|
|
|
|
2022-08-04 11:04:34 +08:00
|
|
|
func (m *mockTestTxnKV) Load(key string) (string, error) {
|
|
|
|
return m.load(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateMetaTable(t *testing.T) (*MetaTable, *mockTestKV, *mockTestTxnKV, func()) {
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
randVal := rand.Int()
|
|
|
|
Params.Init()
|
|
|
|
rootPath := fmt.Sprintf("/test/meta/%d", randVal)
|
|
|
|
etcdCli, err := etcd.GetEtcdClient(&Params.EtcdCfg)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
2022-08-20 10:24:51 +08:00
|
|
|
skv, err := rootcoord.NewMetaSnapshot(etcdCli, rootPath, TimestampPrefix, 7)
|
2022-08-04 11:04:34 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NotNil(t, skv)
|
|
|
|
|
|
|
|
txnkv := etcdkv.NewEtcdKV(etcdCli, rootPath)
|
2022-08-20 10:24:51 +08:00
|
|
|
_, err = NewMetaTable(context.TODO(), &rootcoord.Catalog{Txn: txnkv, Snapshot: skv})
|
2022-08-04 11:04:34 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
mockSnapshotKV := &mockTestKV{
|
2022-08-11 12:12:38 +08:00
|
|
|
SnapShotKV: skv,
|
2022-08-04 11:04:34 +08:00
|
|
|
loadWithPrefix: func(key string, ts typeutil.Timestamp) ([]string, []string, error) {
|
|
|
|
return skv.LoadWithPrefix(key, ts)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
mockTxnKV := &mockTestTxnKV{
|
2022-08-11 12:12:38 +08:00
|
|
|
TxnKV: txnkv,
|
2022-08-04 11:04:34 +08:00
|
|
|
loadWithPrefix: func(key string) ([]string, []string, error) { return txnkv.LoadWithPrefix(key) },
|
|
|
|
save: func(key, value string) error { return txnkv.Save(key, value) },
|
|
|
|
multiSave: func(kvs map[string]string) error { return txnkv.MultiSave(kvs) },
|
|
|
|
multiSaveAndRemoveWithPrefix: func(kvs map[string]string, removal []string) error {
|
|
|
|
return txnkv.MultiSaveAndRemoveWithPrefix(kvs, removal)
|
|
|
|
},
|
|
|
|
remove: func(key string) error { return txnkv.Remove(key) },
|
|
|
|
}
|
|
|
|
|
2022-08-20 10:24:51 +08:00
|
|
|
mockMt, err := NewMetaTable(context.TODO(), &rootcoord.Catalog{Txn: mockTxnKV, Snapshot: mockSnapshotKV})
|
2022-08-04 11:04:34 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
return mockMt, mockSnapshotKV, mockTxnKV, func() {
|
|
|
|
etcdCli.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 10:01:29 +08:00
|
|
|
func TestMetaTable(t *testing.T) {
|
2021-05-17 19:15:01 +08:00
|
|
|
const (
|
2021-10-12 17:40:34 +08:00
|
|
|
collName = "testColl"
|
|
|
|
collNameInvalid = "testColl_invalid"
|
|
|
|
aliasName1 = "alias1"
|
|
|
|
aliasName2 = "alias2"
|
|
|
|
collID = typeutil.UniqueID(1)
|
|
|
|
collIDInvalid = typeutil.UniqueID(2)
|
|
|
|
partIDDefault = typeutil.UniqueID(10)
|
|
|
|
partID = typeutil.UniqueID(20)
|
|
|
|
partName = "testPart"
|
|
|
|
partIDInvalid = typeutil.UniqueID(21)
|
|
|
|
segID = typeutil.UniqueID(100)
|
|
|
|
segID2 = typeutil.UniqueID(101)
|
|
|
|
fieldID = typeutil.UniqueID(110)
|
|
|
|
fieldID2 = typeutil.UniqueID(111)
|
|
|
|
indexID = typeutil.UniqueID(10000)
|
|
|
|
indexID2 = typeutil.UniqueID(10001)
|
|
|
|
buildID = typeutil.UniqueID(201)
|
2022-05-10 21:07:53 +08:00
|
|
|
indexName = "testColl_index_110"
|
2021-05-17 19:15:01 +08:00
|
|
|
)
|
2021-05-14 21:26:06 +08:00
|
|
|
|
2021-01-21 10:01:29 +08:00
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
randVal := rand.Int()
|
2021-01-23 17:56:57 +08:00
|
|
|
Params.Init()
|
2021-01-21 10:01:29 +08:00
|
|
|
rootPath := fmt.Sprintf("/test/meta/%d", randVal)
|
|
|
|
|
2021-05-18 14:18:02 +08:00
|
|
|
var vtso typeutil.Timestamp
|
|
|
|
ftso := func() typeutil.Timestamp {
|
|
|
|
vtso++
|
|
|
|
return vtso
|
|
|
|
}
|
|
|
|
|
2022-02-07 10:09:45 +08:00
|
|
|
etcdCli, err := etcd.GetEtcdClient(&Params.EtcdCfg)
|
2021-12-29 14:35:21 +08:00
|
|
|
require.Nil(t, err)
|
2021-05-18 17:12:17 +08:00
|
|
|
defer etcdCli.Close()
|
2021-12-29 14:35:21 +08:00
|
|
|
|
2022-08-20 10:24:51 +08:00
|
|
|
skv, err := rootcoord.NewMetaSnapshot(etcdCli, rootPath, TimestampPrefix, 7)
|
2021-05-18 14:18:02 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NotNil(t, skv)
|
2021-12-29 14:35:21 +08:00
|
|
|
txnKV := etcdkv.NewEtcdKV(etcdCli, rootPath)
|
2022-08-20 10:24:51 +08:00
|
|
|
mt, err := NewMetaTable(context.TODO(), &rootcoord.Catalog{Txn: txnKV, Snapshot: skv})
|
2021-01-21 10:01:29 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
collInfo := &model.Collection{
|
|
|
|
CollectionID: collID,
|
|
|
|
Name: collName,
|
|
|
|
AutoID: false,
|
|
|
|
Fields: []*model.Field{
|
|
|
|
{
|
|
|
|
FieldID: fieldID,
|
|
|
|
Name: "field110",
|
|
|
|
IsPrimaryKey: false,
|
|
|
|
Description: "",
|
|
|
|
DataType: schemapb.DataType_FloatVector,
|
|
|
|
TypeParams: []*commonpb.KeyValuePair{
|
|
|
|
{
|
|
|
|
Key: "field110-k1",
|
|
|
|
Value: "field110-v1",
|
2021-01-21 10:01:29 +08:00
|
|
|
},
|
2022-07-22 10:20:29 +08:00
|
|
|
{
|
|
|
|
Key: "field110-k2",
|
|
|
|
Value: "field110-v2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
IndexParams: []*commonpb.KeyValuePair{
|
|
|
|
{
|
|
|
|
Key: "field110-i1",
|
|
|
|
Value: "field110-v1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "field110-i2",
|
|
|
|
Value: "field110-v2",
|
2021-01-21 10:01:29 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-07-22 10:20:29 +08:00
|
|
|
FieldIDToIndexID: []common.Int64Tuple{
|
2021-02-11 08:41:59 +08:00
|
|
|
{
|
2022-07-22 10:20:29 +08:00
|
|
|
Key: fieldID,
|
|
|
|
Value: indexID,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
CreateTime: 0,
|
|
|
|
Partitions: []*model.Partition{
|
|
|
|
{
|
|
|
|
PartitionID: partIDDefault,
|
|
|
|
PartitionName: Params.CommonCfg.DefaultPartitionName,
|
|
|
|
PartitionCreatedTimestamp: 0,
|
2021-02-11 08:41:59 +08:00
|
|
|
},
|
|
|
|
},
|
2022-08-11 12:12:38 +08:00
|
|
|
VirtualChannelNames: []string{
|
|
|
|
fmt.Sprintf("dmChannel_%dv%d", collID, 0),
|
|
|
|
fmt.Sprintf("dmChannel_%dv%d", collID, 1),
|
|
|
|
},
|
|
|
|
PhysicalChannelNames: []string{
|
|
|
|
funcutil.ToPhysicalChannel(fmt.Sprintf("dmChannel_%dv%d", collID, 0)),
|
|
|
|
funcutil.ToPhysicalChannel(fmt.Sprintf("dmChannel_%dv%d", collID, 1)),
|
|
|
|
},
|
2021-01-21 10:01:29 +08:00
|
|
|
}
|
2022-07-22 10:20:29 +08:00
|
|
|
|
|
|
|
idxInfo := []*model.Index{
|
2021-02-11 08:41:59 +08:00
|
|
|
{
|
2022-05-10 21:07:53 +08:00
|
|
|
IndexName: indexName,
|
2021-05-17 19:15:01 +08:00
|
|
|
IndexID: indexID,
|
2022-07-22 10:20:29 +08:00
|
|
|
FieldID: fieldID,
|
2021-02-11 08:41:59 +08:00
|
|
|
IndexParams: []*commonpb.KeyValuePair{
|
|
|
|
{
|
|
|
|
Key: "field110-i1",
|
|
|
|
Value: "field110-v1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "field110-i2",
|
|
|
|
Value: "field110-v2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-01-21 10:01:29 +08:00
|
|
|
|
2021-12-29 14:35:21 +08:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
2021-01-21 10:01:29 +08:00
|
|
|
t.Run("add collection", func(t *testing.T) {
|
2021-12-29 14:35:21 +08:00
|
|
|
defer wg.Done()
|
2021-08-18 14:36:10 +08:00
|
|
|
ts := ftso()
|
2021-04-08 17:31:39 +08:00
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
err = mt.AddCollection(collInfo, ts, "")
|
2021-01-21 10:01:29 +08:00
|
|
|
assert.Nil(t, err)
|
2021-08-18 14:36:10 +08:00
|
|
|
assert.Equal(t, uint64(1), ts)
|
2021-01-21 10:01:29 +08:00
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
collMeta, err := mt.GetCollectionByName(collName, ts)
|
2021-01-21 10:01:29 +08:00
|
|
|
assert.Nil(t, err)
|
2021-08-18 14:36:10 +08:00
|
|
|
assert.Equal(t, collMeta.CreateTime, ts)
|
2022-07-22 10:20:29 +08:00
|
|
|
assert.Equal(t, collMeta.Partitions[0].PartitionCreatedTimestamp, ts)
|
2021-07-23 14:36:12 +08:00
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
assert.Equal(t, partIDDefault, collMeta.Partitions[0].PartitionID)
|
|
|
|
assert.Equal(t, 1, len(collMeta.Partitions))
|
|
|
|
assert.True(t, mt.HasCollection(collInfo.CollectionID, 0))
|
2021-04-08 17:31:39 +08:00
|
|
|
|
2021-10-12 17:40:34 +08:00
|
|
|
field, err := mt.GetFieldSchema(collName, "field110")
|
2021-04-08 17:31:39 +08:00
|
|
|
assert.Nil(t, err)
|
2022-07-22 10:20:29 +08:00
|
|
|
assert.Equal(t, collInfo.Fields[0].FieldID, field.FieldID)
|
2021-05-14 21:26:06 +08:00
|
|
|
})
|
|
|
|
|
2021-12-29 14:35:21 +08:00
|
|
|
wg.Add(1)
|
2021-09-18 11:13:51 +08:00
|
|
|
t.Run("add alias", func(t *testing.T) {
|
2021-12-29 14:35:21 +08:00
|
|
|
defer wg.Done()
|
2021-09-18 11:13:51 +08:00
|
|
|
ts := ftso()
|
2021-10-12 17:40:34 +08:00
|
|
|
exists := mt.IsAlias(aliasName1)
|
2021-10-08 17:37:53 +08:00
|
|
|
assert.False(t, exists)
|
2021-10-12 17:40:34 +08:00
|
|
|
err = mt.AddAlias(aliasName1, collName, ts)
|
2021-09-18 11:13:51 +08:00
|
|
|
assert.Nil(t, err)
|
2021-09-22 16:20:48 +08:00
|
|
|
aliases := mt.ListAliases(collID)
|
2021-10-12 17:40:34 +08:00
|
|
|
assert.Equal(t, aliases, []string{aliasName1})
|
|
|
|
exists = mt.IsAlias(aliasName1)
|
2021-10-08 17:37:53 +08:00
|
|
|
assert.True(t, exists)
|
2021-09-18 11:13:51 +08:00
|
|
|
})
|
2021-12-29 14:35:21 +08:00
|
|
|
wg.Add(1)
|
2021-09-18 11:13:51 +08:00
|
|
|
t.Run("alter alias", func(t *testing.T) {
|
2021-12-29 14:35:21 +08:00
|
|
|
defer wg.Done()
|
2021-09-18 11:13:51 +08:00
|
|
|
ts := ftso()
|
2021-10-12 17:40:34 +08:00
|
|
|
err = mt.AlterAlias(aliasName1, collName, ts)
|
2021-09-18 11:13:51 +08:00
|
|
|
assert.Nil(t, err)
|
2021-10-12 17:40:34 +08:00
|
|
|
err = mt.AlterAlias(aliasName1, collNameInvalid, ts)
|
|
|
|
assert.NotNil(t, err)
|
2021-09-18 11:13:51 +08:00
|
|
|
})
|
|
|
|
|
2021-12-29 14:35:21 +08:00
|
|
|
wg.Add(1)
|
2021-09-18 11:13:51 +08:00
|
|
|
t.Run("delete alias", func(t *testing.T) {
|
2021-12-29 14:35:21 +08:00
|
|
|
defer wg.Done()
|
2021-09-18 11:13:51 +08:00
|
|
|
ts := ftso()
|
2021-10-12 17:40:34 +08:00
|
|
|
err = mt.DropAlias(aliasName1, ts)
|
2021-09-18 11:13:51 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
})
|
|
|
|
|
2022-06-15 19:14:10 +08:00
|
|
|
wg.Add(1)
|
|
|
|
t.Run("not load alias when load collection meta", func(t *testing.T) {
|
|
|
|
defer wg.Done()
|
|
|
|
ts := ftso()
|
|
|
|
err = mt.AddAlias(aliasName1, collName, ts)
|
|
|
|
assert.Nil(t, err)
|
2022-07-22 10:20:29 +08:00
|
|
|
err = mt.reloadFromCatalog()
|
2022-06-15 19:14:10 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
_, ok := mt.collName2ID[aliasName1]
|
|
|
|
assert.False(t, ok)
|
|
|
|
})
|
|
|
|
|
2021-12-29 14:35:21 +08:00
|
|
|
wg.Add(1)
|
2021-05-14 21:26:06 +08:00
|
|
|
t.Run("add partition", func(t *testing.T) {
|
2021-12-29 14:35:21 +08:00
|
|
|
defer wg.Done()
|
2021-08-18 14:36:10 +08:00
|
|
|
ts := ftso()
|
2021-10-13 15:54:33 +08:00
|
|
|
err = mt.AddPartition(collID, partName, partID, ts, "")
|
2021-05-18 14:18:02 +08:00
|
|
|
assert.Nil(t, err)
|
2021-09-18 11:13:51 +08:00
|
|
|
//assert.Equal(t, ts, uint64(2))
|
2021-07-23 14:36:12 +08:00
|
|
|
|
|
|
|
collMeta, ok := mt.collID2Meta[collID]
|
|
|
|
assert.True(t, ok)
|
2022-07-22 10:20:29 +08:00
|
|
|
assert.Equal(t, 2, len(collMeta.Partitions))
|
|
|
|
assert.Equal(t, collMeta.Partitions[1].PartitionName, partName)
|
|
|
|
assert.Equal(t, ts, collMeta.Partitions[1].PartitionCreatedTimestamp)
|
2021-01-21 10:01:29 +08:00
|
|
|
})
|
|
|
|
|
2021-12-29 14:35:21 +08:00
|
|
|
wg.Add(1)
|
2021-01-21 10:01:29 +08:00
|
|
|
t.Run("add segment index", func(t *testing.T) {
|
2021-12-29 14:35:21 +08:00
|
|
|
defer wg.Done()
|
2022-07-22 10:20:29 +08:00
|
|
|
alreadyExists, err := mt.AddIndex(collName, "field110", idxInfo[0], []typeutil.UniqueID{segID})
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.False(t, alreadyExists)
|
|
|
|
|
|
|
|
index := model.Index{
|
2021-07-05 10:08:02 +08:00
|
|
|
CollectionID: collID,
|
|
|
|
FieldID: fieldID,
|
|
|
|
IndexID: indexID,
|
2022-07-22 10:20:29 +08:00
|
|
|
SegmentIndexes: map[int64]model.SegmentIndex{
|
|
|
|
segID: {
|
|
|
|
Segment: model.Segment{
|
|
|
|
SegmentID: segID,
|
|
|
|
PartitionID: partID,
|
|
|
|
},
|
|
|
|
BuildID: buildID,
|
|
|
|
EnableIndex: true,
|
|
|
|
},
|
|
|
|
},
|
2021-01-21 10:01:29 +08:00
|
|
|
}
|
2022-07-22 10:20:29 +08:00
|
|
|
err = mt.AlterIndex(&index)
|
2021-01-21 10:01:29 +08:00
|
|
|
assert.Nil(t, err)
|
2021-05-15 18:08:08 +08:00
|
|
|
|
|
|
|
// it's legal to add index twice
|
2022-07-22 10:20:29 +08:00
|
|
|
err = mt.AlterIndex(&index)
|
2021-05-15 18:08:08 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
idxID, ok := mt.segID2IndexID[segID]
|
|
|
|
assert.True(t, ok)
|
|
|
|
indexMeta, ok := mt.indexID2Meta[idxID]
|
|
|
|
assert.True(t, ok)
|
|
|
|
segIdx, ok := indexMeta.SegmentIndexes[segID]
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.True(t, segIdx.EnableIndex)
|
2021-01-21 10:01:29 +08:00
|
|
|
})
|
|
|
|
|
2022-05-05 21:33:51 +08:00
|
|
|
wg.Add(1)
|
|
|
|
t.Run("add diff index with same index name", func(t *testing.T) {
|
|
|
|
defer wg.Done()
|
|
|
|
params := []*commonpb.KeyValuePair{
|
|
|
|
{
|
|
|
|
Key: "field110-k1",
|
|
|
|
Value: "field110-v1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "field110-k2",
|
|
|
|
Value: "field110-v2",
|
|
|
|
},
|
|
|
|
}
|
2022-07-22 10:20:29 +08:00
|
|
|
idxInfo := &model.Index{
|
2022-05-05 21:33:51 +08:00
|
|
|
IndexName: "testColl_index_110",
|
|
|
|
IndexID: indexID,
|
|
|
|
IndexParams: params,
|
|
|
|
}
|
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
alreadyExists, err := mt.AddIndex("collTest", "field110", idxInfo, []typeutil.UniqueID{segID})
|
2022-05-05 21:33:51 +08:00
|
|
|
assert.NotNil(t, err)
|
2022-07-22 10:20:29 +08:00
|
|
|
assert.False(t, alreadyExists)
|
2022-05-05 21:33:51 +08:00
|
|
|
})
|
|
|
|
|
2021-12-29 14:35:21 +08:00
|
|
|
wg.Add(1)
|
2021-01-21 10:01:29 +08:00
|
|
|
t.Run("get not indexed segments", func(t *testing.T) {
|
2021-12-29 14:35:21 +08:00
|
|
|
defer wg.Done()
|
2021-01-21 10:01:29 +08:00
|
|
|
params := []*commonpb.KeyValuePair{
|
|
|
|
{
|
|
|
|
Key: "field110-i1",
|
|
|
|
Value: "field110-v1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "field110-i2",
|
|
|
|
Value: "field110-v2",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
tparams := []*commonpb.KeyValuePair{
|
|
|
|
{
|
|
|
|
Key: "field110-k1",
|
|
|
|
Value: "field110-v1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "field110-k2",
|
|
|
|
Value: "field110-v2",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
idxInfo := &model.Index{
|
2021-02-09 13:11:55 +08:00
|
|
|
IndexName: "field110",
|
|
|
|
IndexID: 2000,
|
|
|
|
IndexParams: params,
|
|
|
|
}
|
2021-01-21 10:01:29 +08:00
|
|
|
|
2021-10-21 14:04:36 +08:00
|
|
|
_, _, err := mt.GetNotIndexedSegments("collTest", "field110", idxInfo, nil)
|
2021-01-21 10:01:29 +08:00
|
|
|
assert.NotNil(t, err)
|
2022-07-22 10:20:29 +08:00
|
|
|
seg, field, err := mt.GetNotIndexedSegments(collName, "field110", idxInfo, []typeutil.UniqueID{segID, segID2})
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, 1, len(seg))
|
|
|
|
assert.Equal(t, segID2, seg[0])
|
|
|
|
assert.True(t, EqualKeyPairArray(field.TypeParams, tparams))
|
2021-01-21 10:01:29 +08:00
|
|
|
|
|
|
|
params = []*commonpb.KeyValuePair{
|
|
|
|
{
|
|
|
|
Key: "field110-i1",
|
|
|
|
Value: "field110-v1",
|
|
|
|
},
|
|
|
|
}
|
2021-02-09 13:11:55 +08:00
|
|
|
idxInfo.IndexParams = params
|
2021-02-11 08:41:59 +08:00
|
|
|
idxInfo.IndexID = 2001
|
|
|
|
idxInfo.IndexName = "field110-1"
|
2021-01-21 10:01:29 +08:00
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
seg, field, err = mt.GetNotIndexedSegments(collName, "field110", idxInfo, []typeutil.UniqueID{segID, segID2})
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, 2, len(seg))
|
|
|
|
assert.Equal(t, segID, seg[0])
|
|
|
|
assert.Equal(t, segID2, seg[1])
|
|
|
|
assert.True(t, EqualKeyPairArray(field.TypeParams, tparams))
|
2021-01-21 10:01:29 +08:00
|
|
|
})
|
|
|
|
|
2021-12-29 14:35:21 +08:00
|
|
|
wg.Add(1)
|
2021-01-21 10:01:29 +08:00
|
|
|
t.Run("get index by name", func(t *testing.T) {
|
2021-12-29 14:35:21 +08:00
|
|
|
defer wg.Done()
|
2022-05-10 21:07:53 +08:00
|
|
|
_, idx, err := mt.GetIndexByName(collName, indexName)
|
2021-01-21 10:01:29 +08:00
|
|
|
assert.Nil(t, err)
|
2022-07-22 10:20:29 +08:00
|
|
|
assert.Equal(t, 2, len(idx))
|
2022-05-10 21:07:53 +08:00
|
|
|
assert.Equal(t, idxInfo[0].IndexID, idx[0].IndexID)
|
2021-01-21 10:01:29 +08:00
|
|
|
params := []*commonpb.KeyValuePair{
|
|
|
|
{
|
|
|
|
Key: "field110-i1",
|
|
|
|
Value: "field110-v1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "field110-i2",
|
|
|
|
Value: "field110-v2",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.True(t, EqualKeyPairArray(idx[0].IndexParams, params))
|
|
|
|
|
2021-10-12 17:40:34 +08:00
|
|
|
_, idx, err = mt.GetIndexByName(collName, "idx201")
|
2021-01-21 10:01:29 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Zero(t, len(idx))
|
|
|
|
})
|
|
|
|
|
2021-12-29 14:35:21 +08:00
|
|
|
wg.Add(1)
|
2021-05-14 21:26:06 +08:00
|
|
|
t.Run("drop partition", func(t *testing.T) {
|
2021-12-29 14:35:21 +08:00
|
|
|
defer wg.Done()
|
2021-08-18 14:36:10 +08:00
|
|
|
ts := ftso()
|
2021-10-13 15:54:33 +08:00
|
|
|
id, err := mt.DeletePartition(collID, partName, ts, "")
|
2021-05-14 21:26:06 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, partID, id)
|
2021-02-20 15:38:44 +08:00
|
|
|
})
|
|
|
|
|
2021-12-29 14:35:21 +08:00
|
|
|
wg.Add(1)
|
2021-04-08 17:31:39 +08:00
|
|
|
t.Run("drop collection", func(t *testing.T) {
|
2021-12-29 14:35:21 +08:00
|
|
|
defer wg.Done()
|
2021-08-18 14:36:10 +08:00
|
|
|
ts := ftso()
|
2021-10-13 15:54:33 +08:00
|
|
|
err = mt.DeleteCollection(collIDInvalid, ts, "")
|
2021-04-08 17:31:39 +08:00
|
|
|
assert.NotNil(t, err)
|
2021-09-18 11:13:51 +08:00
|
|
|
ts2 := ftso()
|
2021-10-12 17:40:34 +08:00
|
|
|
err = mt.AddAlias(aliasName2, collName, ts2)
|
2021-09-18 11:13:51 +08:00
|
|
|
assert.Nil(t, err)
|
2021-10-13 15:54:33 +08:00
|
|
|
err = mt.DeleteCollection(collID, ts, "")
|
2021-05-14 21:26:06 +08:00
|
|
|
assert.Nil(t, err)
|
2021-09-18 11:13:51 +08:00
|
|
|
ts3 := ftso()
|
2021-10-12 17:40:34 +08:00
|
|
|
err = mt.DropAlias(aliasName2, ts3)
|
2021-09-18 11:13:51 +08:00
|
|
|
assert.NotNil(t, err)
|
2021-04-08 17:31:39 +08:00
|
|
|
})
|
|
|
|
|
2022-04-11 19:49:34 +08:00
|
|
|
wg.Add(1)
|
|
|
|
t.Run("delete credential", func(t *testing.T) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
err = mt.DeleteCredential("")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
err = mt.DeleteCredential("abcxyz")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
})
|
|
|
|
|
2021-04-09 16:10:12 +08:00
|
|
|
/////////////////////////// these tests should run at last, it only used to hit the error lines ////////////////////////
|
2021-12-29 14:35:21 +08:00
|
|
|
txnkv := etcdkv.NewEtcdKV(etcdCli, rootPath)
|
2022-07-22 10:20:29 +08:00
|
|
|
mockKV := &mockTestKV{
|
|
|
|
loadWithPrefix: func(key string, ts typeutil.Timestamp) ([]string, []string, error) {
|
|
|
|
return nil, nil, nil
|
|
|
|
},
|
|
|
|
}
|
2021-10-21 14:04:36 +08:00
|
|
|
mockTxnKV := &mockTestTxnKV{
|
2022-08-11 12:12:38 +08:00
|
|
|
TxnKV: txnkv,
|
2021-10-21 14:04:36 +08:00
|
|
|
loadWithPrefix: func(key string) ([]string, []string, error) { return txnkv.LoadWithPrefix(key) },
|
|
|
|
save: func(key, value string) error { return txnkv.Save(key, value) },
|
|
|
|
multiSave: func(kvs map[string]string) error { return txnkv.MultiSave(kvs) },
|
|
|
|
multiSaveAndRemoveWithPrefix: func(kvs map[string]string, removal []string) error {
|
|
|
|
return txnkv.MultiSaveAndRemoveWithPrefix(kvs, removal)
|
|
|
|
},
|
2022-04-11 19:49:34 +08:00
|
|
|
remove: func(key string) error { return txnkv.Remove(key) },
|
2021-10-21 14:04:36 +08:00
|
|
|
}
|
2022-07-22 10:20:29 +08:00
|
|
|
|
2022-08-20 10:24:51 +08:00
|
|
|
mt, err = NewMetaTable(context.TODO(), &rootcoord.Catalog{Txn: mockTxnKV, Snapshot: mockKV})
|
2022-07-22 10:20:29 +08:00
|
|
|
assert.Nil(t, err)
|
2021-04-09 16:10:12 +08:00
|
|
|
|
2021-12-29 14:35:21 +08:00
|
|
|
wg.Add(1)
|
2021-04-09 16:10:12 +08:00
|
|
|
t.Run("add collection failed", func(t *testing.T) {
|
2021-12-29 14:35:21 +08:00
|
|
|
defer wg.Done()
|
2021-05-18 14:18:02 +08:00
|
|
|
mockKV.loadWithPrefix = func(key string, ts typeutil.Timestamp) ([]string, []string, error) {
|
2021-04-09 16:10:12 +08:00
|
|
|
return nil, nil, nil
|
|
|
|
}
|
2021-10-13 15:54:33 +08:00
|
|
|
mockKV.multiSave = func(kvs map[string]string, ts typeutil.Timestamp) error {
|
2021-08-18 14:36:10 +08:00
|
|
|
return fmt.Errorf("multi save error")
|
2021-04-09 16:10:12 +08:00
|
|
|
}
|
2022-07-22 10:20:29 +08:00
|
|
|
collInfo.Partitions = []*model.Partition{}
|
|
|
|
assert.Error(t, mt.AddCollection(collInfo, 0, ""))
|
2021-04-09 16:10:12 +08:00
|
|
|
})
|
|
|
|
|
2021-12-29 14:35:21 +08:00
|
|
|
wg.Add(1)
|
2021-04-09 16:10:12 +08:00
|
|
|
t.Run("delete collection failed", func(t *testing.T) {
|
2021-12-29 14:35:21 +08:00
|
|
|
defer wg.Done()
|
2021-10-13 15:54:33 +08:00
|
|
|
mockKV.multiSave = func(kvs map[string]string, ts typeutil.Timestamp) error {
|
2021-08-18 14:36:10 +08:00
|
|
|
return nil
|
2021-04-09 16:10:12 +08:00
|
|
|
}
|
2021-10-13 15:54:33 +08:00
|
|
|
mockKV.multiSaveAndRemoveWithPrefix = func(save map[string]string, keys []string, ts typeutil.Timestamp) error {
|
2021-08-18 14:36:10 +08:00
|
|
|
return fmt.Errorf("multi save and remove with prefix error")
|
2021-04-09 16:10:12 +08:00
|
|
|
}
|
2021-08-18 14:36:10 +08:00
|
|
|
ts := ftso()
|
2022-07-22 10:20:29 +08:00
|
|
|
assert.Error(t, mt.DeleteCollection(collInfo.CollectionID, ts, ""))
|
2021-04-09 16:10:12 +08:00
|
|
|
})
|
|
|
|
|
2021-12-29 14:35:21 +08:00
|
|
|
wg.Add(1)
|
2021-04-09 16:10:12 +08:00
|
|
|
t.Run("get collection failed", func(t *testing.T) {
|
2021-12-29 14:35:21 +08:00
|
|
|
defer wg.Done()
|
2021-08-18 14:36:10 +08:00
|
|
|
mockKV.save = func(key string, value string, ts typeutil.Timestamp) error {
|
|
|
|
return nil
|
2021-04-09 16:10:12 +08:00
|
|
|
}
|
|
|
|
|
2021-08-18 14:36:10 +08:00
|
|
|
ts := ftso()
|
2022-07-22 10:20:29 +08:00
|
|
|
collInfo.Partitions = []*model.Partition{}
|
|
|
|
err = mt.AddCollection(collInfo, ts, "")
|
2021-05-18 14:18:02 +08:00
|
|
|
assert.Nil(t, err)
|
2021-04-09 16:10:12 +08:00
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
mt.collID2Meta = make(map[int64]model.Collection)
|
|
|
|
_, err = mt.GetCollectionByName(collInfo.Name, 0)
|
2021-04-09 16:10:12 +08:00
|
|
|
assert.NotNil(t, err)
|
2022-07-22 10:20:29 +08:00
|
|
|
assert.EqualError(t, err, fmt.Sprintf("can't find collection %s with id %d", collInfo.Name, collInfo.CollectionID))
|
2021-04-10 10:53:58 +08:00
|
|
|
|
|
|
|
})
|
|
|
|
|
2021-12-29 14:35:21 +08:00
|
|
|
wg.Add(1)
|
2021-04-10 10:53:58 +08:00
|
|
|
t.Run("add partition failed", func(t *testing.T) {
|
2021-12-29 14:35:21 +08:00
|
|
|
defer wg.Done()
|
2021-08-18 14:36:10 +08:00
|
|
|
mockKV.save = func(key string, value string, ts typeutil.Timestamp) error {
|
|
|
|
return nil
|
2021-04-10 10:53:58 +08:00
|
|
|
}
|
2021-05-18 14:18:02 +08:00
|
|
|
mockKV.loadWithPrefix = func(key string, ts typeutil.Timestamp) ([]string, []string, error) {
|
2021-04-10 10:53:58 +08:00
|
|
|
return nil, nil, nil
|
|
|
|
}
|
2022-07-22 10:20:29 +08:00
|
|
|
err := mt.reloadFromCatalog()
|
2021-04-10 10:53:58 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2021-08-18 14:36:10 +08:00
|
|
|
ts := ftso()
|
2022-07-22 10:20:29 +08:00
|
|
|
collInfo.Partitions = []*model.Partition{}
|
|
|
|
err = mt.AddCollection(collInfo, ts, "")
|
2021-04-10 10:53:58 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2021-08-18 14:36:10 +08:00
|
|
|
ts = ftso()
|
2021-10-13 15:54:33 +08:00
|
|
|
err = mt.AddPartition(2, "no-part", 22, ts, "")
|
2021-04-10 10:53:58 +08:00
|
|
|
assert.NotNil(t, err)
|
|
|
|
assert.EqualError(t, err, "can't find collection. id = 2")
|
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
coll := mt.collID2Meta[collInfo.CollectionID]
|
|
|
|
coll.Partitions = make([]*model.Partition, Params.RootCoordCfg.MaxPartitionNum)
|
|
|
|
mt.collID2Meta[coll.CollectionID] = coll
|
|
|
|
err = mt.AddPartition(coll.CollectionID, "no-part", 22, ts, "")
|
2021-04-10 10:53:58 +08:00
|
|
|
assert.NotNil(t, err)
|
2021-12-23 18:39:11 +08:00
|
|
|
assert.EqualError(t, err, fmt.Sprintf("maximum partition's number should be limit to %d", Params.RootCoordCfg.MaxPartitionNum))
|
2021-04-10 10:53:58 +08:00
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
coll.Partitions = []*model.Partition{{PartitionID: partID, PartitionName: partName, PartitionCreatedTimestamp: ftso()}}
|
|
|
|
mt.collID2Meta[coll.CollectionID] = coll
|
|
|
|
|
2021-10-13 15:54:33 +08:00
|
|
|
mockKV.multiSave = func(kvs map[string]string, ts typeutil.Timestamp) error {
|
2021-08-18 14:36:10 +08:00
|
|
|
return fmt.Errorf("multi save error")
|
2021-04-10 10:53:58 +08:00
|
|
|
}
|
2022-08-10 10:22:38 +08:00
|
|
|
tmpSaveFunc := mockKV.save
|
|
|
|
mockKV.save = func(key, value string, ts typeutil.Timestamp) error {
|
|
|
|
return errors.New("mock")
|
|
|
|
}
|
2022-07-22 10:20:29 +08:00
|
|
|
assert.Error(t, mt.AddPartition(coll.CollectionID, "no-part", 22, ts, ""))
|
2022-08-10 10:22:38 +08:00
|
|
|
mockKV.save = tmpSaveFunc
|
2022-07-22 10:20:29 +08:00
|
|
|
//err = mt.AddPartition(coll.CollectionID, "no-part", 22, ts, nil)
|
2021-08-17 23:06:10 +08:00
|
|
|
//assert.NotNil(t, err)
|
|
|
|
//assert.EqualError(t, err, "multi save error")
|
2021-04-10 10:53:58 +08:00
|
|
|
|
2021-10-13 15:54:33 +08:00
|
|
|
mockKV.multiSave = func(kvs map[string]string, ts typeutil.Timestamp) error {
|
2021-08-18 14:36:10 +08:00
|
|
|
return nil
|
2021-04-10 10:53:58 +08:00
|
|
|
}
|
2021-07-23 14:36:12 +08:00
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
collInfo.Partitions = []*model.Partition{}
|
2021-08-18 14:36:10 +08:00
|
|
|
ts = ftso()
|
2022-07-22 10:20:29 +08:00
|
|
|
err = mt.AddPartition(coll.CollectionID, partName, 22, ts, "")
|
2021-04-10 10:53:58 +08:00
|
|
|
assert.NotNil(t, err)
|
2021-07-03 14:36:18 +08:00
|
|
|
assert.EqualError(t, err, fmt.Sprintf("partition name = %s already exists", partName))
|
2022-07-22 10:20:29 +08:00
|
|
|
err = mt.AddPartition(coll.CollectionID, "no-part", partID, ts, "")
|
2021-04-10 10:53:58 +08:00
|
|
|
assert.NotNil(t, err)
|
2021-07-03 14:36:18 +08:00
|
|
|
assert.EqualError(t, err, fmt.Sprintf("partition id = %d already exists", partID))
|
2021-04-09 16:10:12 +08:00
|
|
|
})
|
2021-04-12 15:03:23 +08:00
|
|
|
|
2021-12-29 14:35:21 +08:00
|
|
|
wg.Add(1)
|
2021-04-12 15:03:23 +08:00
|
|
|
t.Run("has partition failed", func(t *testing.T) {
|
2021-12-29 14:35:21 +08:00
|
|
|
defer wg.Done()
|
2021-05-18 14:18:02 +08:00
|
|
|
mockKV.loadWithPrefix = func(key string, ts typeutil.Timestamp) ([]string, []string, error) {
|
2021-04-12 15:03:23 +08:00
|
|
|
return nil, nil, nil
|
|
|
|
}
|
2021-10-13 15:54:33 +08:00
|
|
|
mockKV.multiSave = func(kvs map[string]string, ts typeutil.Timestamp) error {
|
2021-08-18 14:36:10 +08:00
|
|
|
return nil
|
2021-04-12 15:03:23 +08:00
|
|
|
}
|
2022-07-22 10:20:29 +08:00
|
|
|
err := mt.reloadFromCatalog()
|
2021-04-12 15:03:23 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
collInfo.Partitions = []*model.Partition{}
|
2021-08-18 14:36:10 +08:00
|
|
|
ts := ftso()
|
2022-07-22 10:20:29 +08:00
|
|
|
err = mt.AddCollection(collInfo, ts, "")
|
2021-04-12 15:03:23 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
assert.False(t, mt.HasPartition(collInfo.CollectionID, "no-partName", 0))
|
2021-04-12 15:03:23 +08:00
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
mt.collID2Meta = make(map[int64]model.Collection)
|
|
|
|
assert.False(t, mt.HasPartition(collInfo.CollectionID, partName, 0))
|
2021-04-12 15:03:23 +08:00
|
|
|
})
|
|
|
|
|
2021-12-29 14:35:21 +08:00
|
|
|
wg.Add(1)
|
2021-04-12 15:03:23 +08:00
|
|
|
t.Run("delete partition failed", func(t *testing.T) {
|
2021-12-29 14:35:21 +08:00
|
|
|
defer wg.Done()
|
2021-05-18 14:18:02 +08:00
|
|
|
mockKV.loadWithPrefix = func(key string, ts typeutil.Timestamp) ([]string, []string, error) {
|
2021-04-12 15:03:23 +08:00
|
|
|
return nil, nil, nil
|
|
|
|
}
|
2021-10-13 15:54:33 +08:00
|
|
|
mockKV.multiSave = func(kvs map[string]string, ts typeutil.Timestamp) error {
|
2021-08-18 14:36:10 +08:00
|
|
|
return nil
|
2021-04-12 15:03:23 +08:00
|
|
|
}
|
2022-07-22 10:20:29 +08:00
|
|
|
err := mt.reloadFromCatalog()
|
2021-04-12 15:03:23 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
collInfo.Partitions = []*model.Partition{{PartitionID: partID, PartitionName: partName, PartitionCreatedTimestamp: ftso()}}
|
2021-08-18 14:36:10 +08:00
|
|
|
ts := ftso()
|
2022-07-22 10:20:29 +08:00
|
|
|
err = mt.AddCollection(collInfo, ts, "")
|
2021-04-12 15:03:23 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2021-08-18 14:36:10 +08:00
|
|
|
ts = ftso()
|
2022-07-22 10:20:29 +08:00
|
|
|
_, err = mt.DeletePartition(collInfo.CollectionID, Params.CommonCfg.DefaultPartitionName, ts, "")
|
2021-04-12 15:03:23 +08:00
|
|
|
assert.NotNil(t, err)
|
|
|
|
assert.EqualError(t, err, "default partition cannot be deleted")
|
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
_, err = mt.DeletePartition(collInfo.CollectionID, "abc", ts, "")
|
2021-04-12 15:03:23 +08:00
|
|
|
assert.NotNil(t, err)
|
|
|
|
assert.EqualError(t, err, "partition abc does not exist")
|
|
|
|
|
2021-10-21 14:04:36 +08:00
|
|
|
mockKV.save = func(key, value string, ts typeutil.Timestamp) error { return errors.New("mocked error") }
|
2022-07-22 10:20:29 +08:00
|
|
|
_, err = mt.DeletePartition(collInfo.CollectionID, partName, ts, "")
|
|
|
|
assert.Error(t, err)
|
2021-04-12 15:03:23 +08:00
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
mt.collID2Meta = make(map[int64]model.Collection)
|
|
|
|
_, err = mt.DeletePartition(collInfo.CollectionID, "abc", ts, "")
|
2021-04-12 15:03:23 +08:00
|
|
|
assert.NotNil(t, err)
|
2022-07-22 10:20:29 +08:00
|
|
|
assert.EqualError(t, err, fmt.Sprintf("can't find collection id = %d", collInfo.CollectionID))
|
2021-04-12 15:03:23 +08:00
|
|
|
})
|
|
|
|
|
2021-12-29 14:35:21 +08:00
|
|
|
wg.Add(1)
|
2021-04-12 15:03:23 +08:00
|
|
|
t.Run("add index failed", func(t *testing.T) {
|
2021-12-29 14:35:21 +08:00
|
|
|
defer wg.Done()
|
2021-05-18 14:18:02 +08:00
|
|
|
mockKV.loadWithPrefix = func(key string, ts typeutil.Timestamp) ([]string, []string, error) {
|
2021-04-12 15:03:23 +08:00
|
|
|
return nil, nil, nil
|
|
|
|
}
|
2021-10-13 15:54:33 +08:00
|
|
|
mockKV.multiSave = func(kvs map[string]string, ts typeutil.Timestamp) error {
|
2021-08-18 14:36:10 +08:00
|
|
|
return nil
|
2021-04-12 15:03:23 +08:00
|
|
|
}
|
2021-08-18 14:36:10 +08:00
|
|
|
mockKV.save = func(key, value string, ts typeutil.Timestamp) error {
|
|
|
|
return nil
|
2021-04-12 15:03:23 +08:00
|
|
|
}
|
2022-07-22 10:20:29 +08:00
|
|
|
err = mt.reloadFromCatalog()
|
2021-04-12 15:03:23 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
collInfo.Partitions = []*model.Partition{}
|
2021-08-18 14:36:10 +08:00
|
|
|
ts := ftso()
|
2022-07-22 10:20:29 +08:00
|
|
|
err = mt.AddCollection(collInfo, ts, "")
|
2021-05-18 14:18:02 +08:00
|
|
|
assert.Nil(t, err)
|
2021-04-12 15:03:23 +08:00
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
index := &model.Index{
|
2021-07-05 10:08:02 +08:00
|
|
|
CollectionID: collID,
|
|
|
|
FieldID: fieldID,
|
2022-07-22 10:20:29 +08:00
|
|
|
IndexID: indexID,
|
|
|
|
SegmentIndexes: map[int64]model.SegmentIndex{
|
|
|
|
segID: {
|
|
|
|
Segment: model.Segment{
|
|
|
|
SegmentID: segID,
|
|
|
|
PartitionID: partID,
|
|
|
|
},
|
|
|
|
BuildID: buildID,
|
|
|
|
CreateTime: 10,
|
|
|
|
EnableIndex: false,
|
|
|
|
},
|
|
|
|
},
|
2021-07-05 10:08:02 +08:00
|
|
|
}
|
2022-07-22 10:20:29 +08:00
|
|
|
err = mt.AlterIndex(index)
|
2021-04-12 15:03:23 +08:00
|
|
|
assert.NotNil(t, err)
|
2022-07-22 10:20:29 +08:00
|
|
|
assert.EqualError(t, err, fmt.Sprintf("index id = %d not found", index.IndexID))
|
2021-04-12 15:03:23 +08:00
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
mt.collID2Meta = make(map[int64]model.Collection)
|
|
|
|
err = mt.AlterIndex(index)
|
2021-04-12 15:03:23 +08:00
|
|
|
assert.NotNil(t, err)
|
2022-07-22 10:20:29 +08:00
|
|
|
assert.EqualError(t, err, fmt.Sprintf("collection id = %d not found", collInfo.CollectionID))
|
2021-04-12 15:03:23 +08:00
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
err = mt.reloadFromCatalog()
|
2021-04-12 15:03:23 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
collInfo.Partitions = []*model.Partition{}
|
2021-08-18 14:36:10 +08:00
|
|
|
ts = ftso()
|
2022-07-22 10:20:29 +08:00
|
|
|
err = mt.AddCollection(collInfo, ts, "")
|
2021-04-12 15:03:23 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
newIdx := *index
|
|
|
|
newIdx.SegmentIndexes = map[int64]model.SegmentIndex{
|
|
|
|
segID: {
|
|
|
|
Segment: model.Segment{
|
|
|
|
SegmentID: segID,
|
|
|
|
PartitionID: partID,
|
|
|
|
},
|
|
|
|
BuildID: buildID,
|
|
|
|
CreateTime: 10,
|
|
|
|
EnableIndex: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
mt.indexID2Meta[indexID] = index
|
|
|
|
mockTxnKV.multiSave = func(kvs map[string]string) error {
|
2021-08-18 14:36:10 +08:00
|
|
|
return fmt.Errorf("save error")
|
2021-04-12 15:03:23 +08:00
|
|
|
}
|
2022-07-22 10:20:29 +08:00
|
|
|
assert.Error(t, mt.AlterIndex(&newIdx))
|
2021-04-12 15:03:23 +08:00
|
|
|
})
|
|
|
|
|
2021-12-29 14:35:21 +08:00
|
|
|
wg.Add(1)
|
2021-04-12 15:03:23 +08:00
|
|
|
t.Run("drop index failed", func(t *testing.T) {
|
2021-12-29 14:35:21 +08:00
|
|
|
defer wg.Done()
|
2021-05-18 14:18:02 +08:00
|
|
|
mockKV.loadWithPrefix = func(key string, ts typeutil.Timestamp) ([]string, []string, error) {
|
2021-04-12 15:03:23 +08:00
|
|
|
return nil, nil, nil
|
|
|
|
}
|
2021-10-13 15:54:33 +08:00
|
|
|
mockKV.multiSave = func(kvs map[string]string, ts typeutil.Timestamp) error {
|
2021-08-18 14:36:10 +08:00
|
|
|
return nil
|
2021-04-12 15:03:23 +08:00
|
|
|
}
|
2021-08-18 14:36:10 +08:00
|
|
|
mockKV.save = func(key string, value string, ts typeutil.Timestamp) error {
|
|
|
|
return nil
|
2021-04-12 15:03:23 +08:00
|
|
|
}
|
2022-07-22 10:20:29 +08:00
|
|
|
err := mt.reloadFromCatalog()
|
2021-04-12 15:03:23 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
collInfo.Partitions = []*model.Partition{}
|
2021-08-18 14:36:10 +08:00
|
|
|
ts := ftso()
|
2022-07-22 10:20:29 +08:00
|
|
|
err = mt.AddCollection(collInfo, ts, "")
|
2021-04-12 15:03:23 +08:00
|
|
|
assert.Nil(t, err)
|
2022-07-22 10:20:29 +08:00
|
|
|
mt.indexID2Meta[indexID] = idxInfo[0]
|
2021-04-12 15:03:23 +08:00
|
|
|
|
2021-10-21 14:04:36 +08:00
|
|
|
_, _, err = mt.DropIndex("abc", "abc", "abc")
|
2021-04-12 15:03:23 +08:00
|
|
|
assert.NotNil(t, err)
|
|
|
|
assert.EqualError(t, err, "collection name = abc not exist")
|
|
|
|
|
|
|
|
mt.collName2ID["abc"] = 2
|
2021-10-21 14:04:36 +08:00
|
|
|
_, _, err = mt.DropIndex("abc", "abc", "abc")
|
2021-04-12 15:03:23 +08:00
|
|
|
assert.NotNil(t, err)
|
|
|
|
assert.EqualError(t, err, "collection name = abc not has meta")
|
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
_, _, err = mt.DropIndex(collInfo.Name, "abc", "abc")
|
2021-04-12 15:03:23 +08:00
|
|
|
assert.NotNil(t, err)
|
2022-07-22 10:20:29 +08:00
|
|
|
assert.EqualError(t, err, fmt.Sprintf("collection %s doesn't have filed abc", collInfo.Name))
|
2021-04-12 15:03:23 +08:00
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
coll := mt.collID2Meta[collInfo.CollectionID]
|
|
|
|
coll.FieldIDToIndexID = []common.Int64Tuple{
|
2021-04-12 15:03:23 +08:00
|
|
|
{
|
2022-07-22 10:20:29 +08:00
|
|
|
Key: fieldID2,
|
|
|
|
Value: indexID2,
|
2021-04-12 15:03:23 +08:00
|
|
|
},
|
|
|
|
{
|
2022-07-22 10:20:29 +08:00
|
|
|
Key: fieldID,
|
|
|
|
Value: indexID,
|
2021-04-12 15:03:23 +08:00
|
|
|
},
|
|
|
|
}
|
2022-07-22 10:20:29 +08:00
|
|
|
mt.collID2Meta[coll.CollectionID] = coll
|
|
|
|
mt.indexID2Meta = make(map[int64]*model.Index)
|
|
|
|
idxID, isDroped, err := mt.DropIndex(collInfo.Name, collInfo.Fields[0].Name, idxInfo[0].IndexName)
|
2021-04-12 15:03:23 +08:00
|
|
|
assert.Zero(t, idxID)
|
|
|
|
assert.False(t, isDroped)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
err = mt.reloadFromCatalog()
|
2021-04-12 15:03:23 +08:00
|
|
|
assert.Nil(t, err)
|
2022-07-22 10:20:29 +08:00
|
|
|
collInfo.Partitions = []*model.Partition{}
|
2021-08-18 14:36:10 +08:00
|
|
|
ts = ftso()
|
2022-07-22 10:20:29 +08:00
|
|
|
err = mt.AddCollection(collInfo, ts, "")
|
|
|
|
mt.indexID2Meta[indexID] = idxInfo[0]
|
|
|
|
|
2021-04-12 15:03:23 +08:00
|
|
|
assert.Nil(t, err)
|
2021-10-21 14:04:36 +08:00
|
|
|
mockTxnKV.multiSaveAndRemoveWithPrefix = func(saves map[string]string, removals []string) error {
|
2021-08-18 14:36:10 +08:00
|
|
|
return fmt.Errorf("multi save and remove with prefix error")
|
2021-04-12 15:03:23 +08:00
|
|
|
}
|
2022-07-22 10:20:29 +08:00
|
|
|
|
|
|
|
_, _, err = mt.DropIndex(collInfo.Name, collInfo.Fields[0].Name, idxInfo[0].IndexName)
|
|
|
|
assert.Error(t, err)
|
2021-04-12 15:03:23 +08:00
|
|
|
})
|
|
|
|
|
2021-12-29 14:35:21 +08:00
|
|
|
wg.Add(1)
|
2021-04-12 15:03:23 +08:00
|
|
|
t.Run("get segment index info by id", func(t *testing.T) {
|
2021-12-29 14:35:21 +08:00
|
|
|
defer wg.Done()
|
2021-05-18 14:18:02 +08:00
|
|
|
mockKV.loadWithPrefix = func(key string, ts typeutil.Timestamp) ([]string, []string, error) {
|
2021-04-12 15:03:23 +08:00
|
|
|
return nil, nil, nil
|
|
|
|
}
|
2021-10-21 14:04:36 +08:00
|
|
|
mockTxnKV.multiSave = func(kvs map[string]string) error {
|
2021-08-18 14:36:10 +08:00
|
|
|
return nil
|
2021-04-12 15:03:23 +08:00
|
|
|
}
|
2021-10-21 14:04:36 +08:00
|
|
|
mockTxnKV.save = func(key, value string) error {
|
2021-08-18 14:36:10 +08:00
|
|
|
return nil
|
2021-04-12 15:03:23 +08:00
|
|
|
}
|
2022-07-22 10:20:29 +08:00
|
|
|
err := mt.reloadFromCatalog()
|
2021-04-12 15:03:23 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
collInfo.Partitions = []*model.Partition{}
|
2021-08-18 14:36:10 +08:00
|
|
|
ts := ftso()
|
2022-07-22 10:20:29 +08:00
|
|
|
err = mt.AddCollection(collInfo, ts, "")
|
2021-04-12 15:03:23 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
alreadyExists, err := mt.AddIndex(collName, "field110", idxInfo[0], []typeutil.UniqueID{segID})
|
2021-04-12 15:03:23 +08:00
|
|
|
assert.Nil(t, err)
|
2022-07-22 10:20:29 +08:00
|
|
|
assert.False(t, alreadyExists)
|
2021-04-12 15:03:23 +08:00
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
segIdxInfo := model.Index{
|
2021-07-05 10:08:02 +08:00
|
|
|
CollectionID: collID,
|
2022-07-22 10:20:29 +08:00
|
|
|
SegmentIndexes: map[int64]model.SegmentIndex{
|
|
|
|
segID: {
|
|
|
|
Segment: model.Segment{
|
|
|
|
SegmentID: segID,
|
|
|
|
PartitionID: partID,
|
|
|
|
},
|
|
|
|
BuildID: buildID,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
FieldID: fieldID,
|
|
|
|
IndexID: indexID,
|
2021-07-05 10:08:02 +08:00
|
|
|
}
|
2022-07-22 10:20:29 +08:00
|
|
|
err = mt.AlterIndex(&segIdxInfo)
|
2021-05-18 14:18:02 +08:00
|
|
|
assert.Nil(t, err)
|
2022-07-22 10:20:29 +08:00
|
|
|
idx, err := mt.GetSegmentIndexInfoByID(segID, segIdxInfo.FieldID, idxInfo[0].IndexName)
|
2021-04-12 15:03:23 +08:00
|
|
|
assert.Nil(t, err)
|
2021-05-24 14:19:52 +08:00
|
|
|
assert.Equal(t, segIdxInfo.IndexID, idx.IndexID)
|
2021-04-12 15:03:23 +08:00
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
_, err = mt.GetSegmentIndexInfoByID(segID, segIdxInfo.FieldID, "abc")
|
2021-04-12 15:03:23 +08:00
|
|
|
assert.NotNil(t, err)
|
2022-07-22 10:20:29 +08:00
|
|
|
assert.EqualError(t, err, fmt.Sprintf("can't find index name = abc on segment = %d, with filed id = %d", segID, segIdxInfo.FieldID))
|
2021-04-12 15:03:23 +08:00
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
_, err = mt.GetSegmentIndexInfoByID(segID, 11, idxInfo[0].IndexName)
|
2021-04-12 15:03:23 +08:00
|
|
|
assert.NotNil(t, err)
|
2022-07-22 10:20:29 +08:00
|
|
|
assert.EqualError(t, err, fmt.Sprintf("can't find index name = %s on segment = %d, with filed id = 11", idxInfo[0].IndexName, segID))
|
2021-04-12 15:03:23 +08:00
|
|
|
})
|
|
|
|
|
2021-12-29 14:35:21 +08:00
|
|
|
wg.Add(1)
|
2021-04-12 15:03:23 +08:00
|
|
|
t.Run("get field schema failed", func(t *testing.T) {
|
2021-12-29 14:35:21 +08:00
|
|
|
defer wg.Done()
|
2021-05-18 14:18:02 +08:00
|
|
|
mockKV.loadWithPrefix = func(key string, ts typeutil.Timestamp) ([]string, []string, error) {
|
2021-04-12 15:03:23 +08:00
|
|
|
return nil, nil, nil
|
|
|
|
}
|
2021-10-13 15:54:33 +08:00
|
|
|
mockKV.multiSave = func(kvs map[string]string, ts typeutil.Timestamp) error {
|
2021-08-18 14:36:10 +08:00
|
|
|
return nil
|
2021-04-12 15:03:23 +08:00
|
|
|
}
|
2021-08-18 14:36:10 +08:00
|
|
|
mockKV.save = func(key string, value string, ts typeutil.Timestamp) error {
|
|
|
|
return nil
|
2021-04-12 15:03:23 +08:00
|
|
|
}
|
2022-07-22 10:20:29 +08:00
|
|
|
err := mt.reloadFromCatalog()
|
2021-04-12 15:03:23 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
collInfo.Partitions = []*model.Partition{}
|
2021-08-18 14:36:10 +08:00
|
|
|
ts := ftso()
|
2022-07-22 10:20:29 +08:00
|
|
|
err = mt.AddCollection(collInfo, ts, "")
|
2021-04-12 15:03:23 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
mt.collID2Meta = make(map[int64]model.Collection)
|
|
|
|
_, err = mt.getFieldSchemaInternal(collInfo.Name, collInfo.Fields[0].Name)
|
2021-04-12 15:03:23 +08:00
|
|
|
assert.NotNil(t, err)
|
2022-07-22 10:20:29 +08:00
|
|
|
assert.EqualError(t, err, fmt.Sprintf("collection %s not found", collInfo.Name))
|
2021-04-12 15:03:23 +08:00
|
|
|
|
|
|
|
mt.collName2ID = make(map[string]int64)
|
2022-07-22 10:20:29 +08:00
|
|
|
_, err = mt.getFieldSchemaInternal(collInfo.Name, collInfo.Fields[0].Name)
|
2021-04-12 15:03:23 +08:00
|
|
|
assert.NotNil(t, err)
|
2022-07-22 10:20:29 +08:00
|
|
|
assert.EqualError(t, err, fmt.Sprintf("collection %s not found", collInfo.Name))
|
2021-04-12 15:03:23 +08:00
|
|
|
})
|
|
|
|
|
2021-12-29 14:35:21 +08:00
|
|
|
wg.Add(1)
|
2021-04-12 15:03:23 +08:00
|
|
|
t.Run("is segment indexed", func(t *testing.T) {
|
2021-12-29 14:35:21 +08:00
|
|
|
defer wg.Done()
|
2021-05-18 14:18:02 +08:00
|
|
|
mockKV.loadWithPrefix = func(key string, ts typeutil.Timestamp) ([]string, []string, error) {
|
2021-04-12 15:03:23 +08:00
|
|
|
return nil, nil, nil
|
|
|
|
}
|
2022-07-22 10:20:29 +08:00
|
|
|
err := mt.reloadFromCatalog()
|
2021-04-12 15:03:23 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
idx := &pb.SegmentIndexInfo{
|
|
|
|
IndexID: 30,
|
|
|
|
FieldID: 31,
|
|
|
|
SegmentID: 32,
|
|
|
|
}
|
|
|
|
idxMeta := make(map[int64]pb.SegmentIndexInfo)
|
|
|
|
idxMeta[idx.IndexID] = *idx
|
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
field := model.Field{
|
2021-04-12 15:03:23 +08:00
|
|
|
FieldID: 31,
|
|
|
|
}
|
|
|
|
assert.False(t, mt.IsSegmentIndexed(idx.SegmentID, &field, nil))
|
|
|
|
field.FieldID = 34
|
|
|
|
assert.False(t, mt.IsSegmentIndexed(idx.SegmentID, &field, nil))
|
|
|
|
})
|
|
|
|
|
2021-12-29 14:35:21 +08:00
|
|
|
wg.Add(1)
|
2021-04-12 15:03:23 +08:00
|
|
|
t.Run("get not indexed segments", func(t *testing.T) {
|
2021-12-29 14:35:21 +08:00
|
|
|
defer wg.Done()
|
2021-05-18 14:18:02 +08:00
|
|
|
mockKV.loadWithPrefix = func(key string, ts typeutil.Timestamp) ([]string, []string, error) {
|
2021-04-12 15:03:23 +08:00
|
|
|
return nil, nil, nil
|
|
|
|
}
|
2022-07-22 10:20:29 +08:00
|
|
|
err := mt.reloadFromCatalog()
|
2021-04-12 15:03:23 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
idx := &model.Index{
|
2021-04-12 15:03:23 +08:00
|
|
|
IndexName: "no-idx",
|
|
|
|
IndexID: 456,
|
|
|
|
IndexParams: []*commonpb.KeyValuePair{
|
|
|
|
{
|
|
|
|
Key: "no-idx-k1",
|
|
|
|
Value: "no-idx-v1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
mt.collName2ID["abc"] = 123
|
2021-10-21 14:04:36 +08:00
|
|
|
_, _, err = mt.GetNotIndexedSegments("abc", "no-field", idx, nil)
|
2021-04-12 15:03:23 +08:00
|
|
|
assert.NotNil(t, err)
|
2022-07-22 10:20:29 +08:00
|
|
|
assert.EqualError(t, err, "collection abc not found")
|
2021-04-12 15:03:23 +08:00
|
|
|
|
2021-10-13 15:54:33 +08:00
|
|
|
mockKV.multiSave = func(kvs map[string]string, ts typeutil.Timestamp) error {
|
2021-08-18 14:36:10 +08:00
|
|
|
return nil
|
2021-04-12 15:03:23 +08:00
|
|
|
}
|
2021-08-18 14:36:10 +08:00
|
|
|
mockKV.save = func(key string, value string, ts typeutil.Timestamp) error {
|
|
|
|
return nil
|
2021-04-12 15:03:23 +08:00
|
|
|
}
|
2022-07-22 10:20:29 +08:00
|
|
|
err = mt.reloadFromCatalog()
|
2021-04-12 15:03:23 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
collInfo.Partitions = []*model.Partition{}
|
2021-08-18 14:36:10 +08:00
|
|
|
ts := ftso()
|
2022-07-22 10:20:29 +08:00
|
|
|
err = mt.AddCollection(collInfo, ts, "")
|
2021-04-12 15:03:23 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
_, _, err = mt.GetNotIndexedSegments(collInfo.Name, "no-field", idx, nil)
|
2021-04-12 15:03:23 +08:00
|
|
|
assert.NotNil(t, err)
|
2022-07-22 10:20:29 +08:00
|
|
|
assert.EqualError(t, err, fmt.Sprintf("collection %s doesn't have filed no-field", collInfo.Name))
|
2021-04-12 15:03:23 +08:00
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
mt.indexID2Meta = make(map[int64]*model.Index)
|
|
|
|
_, _, err = mt.GetNotIndexedSegments(collInfo.Name, collInfo.Fields[0].Name, idx, nil)
|
|
|
|
assert.Nil(t, err)
|
2021-04-12 15:03:23 +08:00
|
|
|
})
|
|
|
|
|
2021-12-29 14:35:21 +08:00
|
|
|
wg.Add(1)
|
2021-04-12 15:03:23 +08:00
|
|
|
t.Run("get index by name failed", func(t *testing.T) {
|
2021-12-29 14:35:21 +08:00
|
|
|
defer wg.Done()
|
2021-05-18 14:18:02 +08:00
|
|
|
mockKV.loadWithPrefix = func(key string, ts typeutil.Timestamp) ([]string, []string, error) {
|
2021-04-12 15:03:23 +08:00
|
|
|
return nil, nil, nil
|
|
|
|
}
|
2022-07-22 10:20:29 +08:00
|
|
|
err := mt.reloadFromCatalog()
|
2021-04-12 15:03:23 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
mt.collName2ID["abc"] = 123
|
2021-04-27 10:30:55 +08:00
|
|
|
_, _, err = mt.GetIndexByName("abc", "hij")
|
2021-04-12 15:03:23 +08:00
|
|
|
assert.NotNil(t, err)
|
|
|
|
assert.EqualError(t, err, "collection abc not found")
|
|
|
|
|
2021-10-21 14:04:36 +08:00
|
|
|
mockTxnKV.multiSave = func(kvs map[string]string) error {
|
2021-08-18 14:36:10 +08:00
|
|
|
return nil
|
2021-04-12 15:03:23 +08:00
|
|
|
}
|
2021-10-21 14:04:36 +08:00
|
|
|
mockTxnKV.save = func(key string, value string) error {
|
2021-08-18 14:36:10 +08:00
|
|
|
return nil
|
2021-04-12 15:03:23 +08:00
|
|
|
}
|
2022-07-22 10:20:29 +08:00
|
|
|
err = mt.reloadFromCatalog()
|
2021-04-12 15:03:23 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
collInfo.Partitions = []*model.Partition{}
|
2021-08-18 14:36:10 +08:00
|
|
|
ts := ftso()
|
2022-07-22 10:20:29 +08:00
|
|
|
err = mt.AddCollection(collInfo, ts, "")
|
2021-04-12 15:03:23 +08:00
|
|
|
assert.Nil(t, err)
|
2022-07-22 10:20:29 +08:00
|
|
|
mt.indexID2Meta = make(map[int64]*model.Index)
|
|
|
|
_, _, err = mt.GetIndexByName(collInfo.Name, idxInfo[0].IndexName)
|
2021-04-12 15:03:23 +08:00
|
|
|
assert.NotNil(t, err)
|
|
|
|
assert.EqualError(t, err, fmt.Sprintf("index id = %d not found", idxInfo[0].IndexID))
|
|
|
|
|
|
|
|
_, err = mt.GetIndexByID(idxInfo[0].IndexID)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
assert.EqualError(t, err, fmt.Sprintf("cannot find index, id = %d", idxInfo[0].IndexID))
|
|
|
|
})
|
2022-04-11 19:49:34 +08:00
|
|
|
|
2022-04-19 16:35:39 +08:00
|
|
|
wg.Add(1)
|
|
|
|
t.Run("add credential failed", func(t *testing.T) {
|
|
|
|
defer wg.Done()
|
2022-04-21 19:57:42 +08:00
|
|
|
mockTxnKV.save = func(key, value string) error {
|
|
|
|
return fmt.Errorf("save error")
|
|
|
|
}
|
2022-04-19 16:35:39 +08:00
|
|
|
err = mt.AddCredential(&internalpb.CredentialInfo{Username: "x", EncryptedPassword: "a\xc5z"})
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
})
|
|
|
|
|
2022-04-11 19:49:34 +08:00
|
|
|
wg.Add(1)
|
|
|
|
t.Run("delete credential failed", func(t *testing.T) {
|
|
|
|
defer wg.Done()
|
|
|
|
mockTxnKV.remove = func(key string) error {
|
|
|
|
return fmt.Errorf("delete error")
|
|
|
|
}
|
|
|
|
err := mt.DeleteCredential("")
|
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
2022-08-04 11:04:34 +08:00
|
|
|
|
2021-12-29 14:35:21 +08:00
|
|
|
wg.Wait()
|
2021-01-21 10:01:29 +08:00
|
|
|
}
|
2021-05-18 17:12:17 +08:00
|
|
|
|
2022-08-04 11:04:34 +08:00
|
|
|
func TestRbacCreateRole(t *testing.T) {
|
|
|
|
mt, _, mockTxnKV, closeCli := generateMetaTable(t)
|
|
|
|
defer closeCli()
|
|
|
|
var err error
|
|
|
|
err = mt.CreateRole(util.DefaultTenant, &milvuspb.RoleEntity{Name: ""})
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
|
|
|
mockTxnKV.save = func(key, value string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
err = mt.CreateRole(util.DefaultTenant, &milvuspb.RoleEntity{Name: "role1"})
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
mockTxnKV.save = func(key, value string) error {
|
|
|
|
return fmt.Errorf("save error")
|
|
|
|
}
|
|
|
|
err = mt.CreateRole(util.DefaultTenant, &milvuspb.RoleEntity{Name: "role2"})
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRbacDropRole(t *testing.T) {
|
|
|
|
mt, _, mockTxnKV, closeCli := generateMetaTable(t)
|
|
|
|
defer closeCli()
|
|
|
|
var err error
|
|
|
|
|
|
|
|
mockTxnKV.remove = func(key string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err = mt.DropRole(util.DefaultTenant, "role1")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
mockTxnKV.remove = func(key string) error {
|
|
|
|
return fmt.Errorf("delete error")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = mt.DropRole(util.DefaultTenant, "role2")
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRbacOperateRole(t *testing.T) {
|
|
|
|
mt, _, mockTxnKV, closeCli := generateMetaTable(t)
|
|
|
|
defer closeCli()
|
|
|
|
var err error
|
|
|
|
|
|
|
|
err = mt.OperateUserRole(util.DefaultTenant, &milvuspb.UserEntity{Name: " "}, &milvuspb.RoleEntity{Name: "role"}, milvuspb.OperateUserRoleType_AddUserToRole)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
|
|
|
err = mt.OperateUserRole(util.DefaultTenant, &milvuspb.UserEntity{Name: "user"}, &milvuspb.RoleEntity{Name: " "}, milvuspb.OperateUserRoleType_AddUserToRole)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
|
|
|
mockTxnKV.save = func(key, value string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
err = mt.OperateUserRole(util.DefaultTenant, &milvuspb.UserEntity{Name: "user"}, &milvuspb.RoleEntity{Name: "role"}, milvuspb.OperateUserRoleType(100))
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
|
|
|
err = mt.OperateUserRole(util.DefaultTenant, &milvuspb.UserEntity{Name: "user"}, &milvuspb.RoleEntity{Name: "role"}, milvuspb.OperateUserRoleType_AddUserToRole)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
mockTxnKV.save = func(key, value string) error {
|
|
|
|
return fmt.Errorf("save error")
|
|
|
|
}
|
|
|
|
err = mt.OperateUserRole(util.DefaultTenant, &milvuspb.UserEntity{Name: "user"}, &milvuspb.RoleEntity{Name: "role"}, milvuspb.OperateUserRoleType_AddUserToRole)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
|
|
|
mockTxnKV.remove = func(key string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
err = mt.OperateUserRole(util.DefaultTenant, &milvuspb.UserEntity{Name: "user"}, &milvuspb.RoleEntity{Name: "role"}, milvuspb.OperateUserRoleType_RemoveUserFromRole)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
mockTxnKV.remove = func(key string) error {
|
|
|
|
return fmt.Errorf("remove error")
|
|
|
|
}
|
|
|
|
err = mt.OperateUserRole(util.DefaultTenant, &milvuspb.UserEntity{Name: "user"}, &milvuspb.RoleEntity{Name: "role"}, milvuspb.OperateUserRoleType_RemoveUserFromRole)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRbacSelectRole(t *testing.T) {
|
|
|
|
mt, _, mockTxnKV, closeCli := generateMetaTable(t)
|
|
|
|
defer closeCli()
|
|
|
|
var err error
|
|
|
|
|
|
|
|
_, err = mt.SelectRole(util.DefaultTenant, &milvuspb.RoleEntity{Name: ""}, false)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
|
|
|
mockTxnKV.loadWithPrefix = func(key string) ([]string, []string, error) {
|
|
|
|
return []string{}, []string{}, fmt.Errorf("load with prefix error")
|
|
|
|
}
|
|
|
|
_, err = mt.SelectRole(util.DefaultTenant, &milvuspb.RoleEntity{Name: "role"}, true)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
|
|
|
mockTxnKV.loadWithPrefix = func(key string) ([]string, []string, error) {
|
|
|
|
return []string{key + "/key1", key + "/key2", key + "/a/err"}, []string{"value1", "value2", "values3"}, nil
|
|
|
|
}
|
|
|
|
mockTxnKV.load = func(key string) (string, error) {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
results, _ := mt.SelectRole(util.DefaultTenant, nil, false)
|
|
|
|
assert.Equal(t, 2, len(results))
|
|
|
|
results, _ = mt.SelectRole(util.DefaultTenant, &milvuspb.RoleEntity{Name: "role"}, false)
|
|
|
|
assert.Equal(t, 1, len(results))
|
|
|
|
|
|
|
|
mockTxnKV.loadWithPrefix = func(key string) ([]string, []string, error) {
|
|
|
|
return []string{}, []string{}, fmt.Errorf("load with prefix error")
|
|
|
|
}
|
|
|
|
_, err = mt.SelectRole(util.DefaultTenant, nil, false)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
|
|
|
mockTxnKV.load = func(key string) (string, error) {
|
|
|
|
return "", fmt.Errorf("load error")
|
|
|
|
}
|
|
|
|
_, err = mt.SelectRole(util.DefaultTenant, &milvuspb.RoleEntity{Name: "role"}, false)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
|
|
|
roleName := "role1"
|
|
|
|
mockTxnKV.load = func(key string) (string, error) {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
mockTxnKV.loadWithPrefix = func(key string) ([]string, []string, error) {
|
|
|
|
return []string{key + "/user1/" + roleName, key + "/user2/role2", key + "/user3/" + roleName, key + "/err"}, []string{"value1", "value2", "values3", "value4"}, nil
|
|
|
|
}
|
|
|
|
results, err = mt.SelectRole(util.DefaultTenant, &milvuspb.RoleEntity{Name: roleName}, true)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, 1, len(results))
|
|
|
|
assert.Equal(t, 2, len(results[0].Users))
|
|
|
|
|
|
|
|
mockTxnKV.loadWithPrefix = func(key string) ([]string, []string, error) {
|
2022-08-20 10:24:51 +08:00
|
|
|
if key == rootcoord.RoleMappingPrefix {
|
2022-08-04 11:04:34 +08:00
|
|
|
return []string{key + "/user1/role2", key + "/user2/role2", key + "/user1/role1", key + "/user2/role1"}, []string{"value1", "value2", "values3", "value4"}, nil
|
2022-08-20 10:24:51 +08:00
|
|
|
} else if key == rootcoord.RolePrefix {
|
2022-08-04 11:04:34 +08:00
|
|
|
return []string{key + "/role1", key + "/role2", key + "/role3"}, []string{"value1", "value2", "values3"}, nil
|
|
|
|
} else {
|
|
|
|
return []string{}, []string{}, fmt.Errorf("load with prefix error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
results, err = mt.SelectRole(util.DefaultTenant, nil, true)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, 3, len(results))
|
|
|
|
for _, result := range results {
|
|
|
|
if result.Role.Name == "role1" {
|
|
|
|
assert.Equal(t, 2, len(result.Users))
|
|
|
|
} else if result.Role.Name == "role2" {
|
|
|
|
assert.Equal(t, 2, len(result.Users))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRbacSelectUser(t *testing.T) {
|
|
|
|
mt, _, mockTxnKV, closeCli := generateMetaTable(t)
|
|
|
|
defer closeCli()
|
|
|
|
var err error
|
|
|
|
|
|
|
|
_, err = mt.SelectUser(util.DefaultTenant, &milvuspb.UserEntity{Name: ""}, false)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
|
|
|
credentialInfo := internalpb.CredentialInfo{
|
|
|
|
EncryptedPassword: "password",
|
|
|
|
}
|
|
|
|
credentialInfoByte, _ := json.Marshal(credentialInfo)
|
|
|
|
|
|
|
|
mockTxnKV.load = func(key string) (string, error) {
|
|
|
|
return string(credentialInfoByte), nil
|
|
|
|
}
|
|
|
|
mockTxnKV.loadWithPrefix = func(key string) ([]string, []string, error) {
|
|
|
|
return []string{key + "/key1", key + "/key2"}, []string{string(credentialInfoByte), string(credentialInfoByte)}, nil
|
|
|
|
}
|
|
|
|
results, err := mt.SelectUser(util.DefaultTenant, &milvuspb.UserEntity{Name: "user"}, false)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, 1, len(results))
|
|
|
|
results, err = mt.SelectUser(util.DefaultTenant, nil, false)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, 2, len(results))
|
|
|
|
|
|
|
|
mockTxnKV.loadWithPrefix = func(key string) ([]string, []string, error) {
|
|
|
|
return []string{key + "/key1", key + "/key2", key + "/a/err"}, []string{"value1", "value2", "values3"}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
mockTxnKV.load = func(key string) (string, error) {
|
|
|
|
return string(credentialInfoByte), nil
|
|
|
|
}
|
|
|
|
results, err = mt.SelectUser(util.DefaultTenant, &milvuspb.UserEntity{Name: "user"}, true)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, 1, len(results))
|
|
|
|
assert.Equal(t, 2, len(results[0].Roles))
|
|
|
|
|
|
|
|
mockTxnKV.loadWithPrefix = func(key string) ([]string, []string, error) {
|
|
|
|
logger.Debug("simfg", zap.String("key", key))
|
2022-08-20 10:24:51 +08:00
|
|
|
if strings.Contains(key, rootcoord.RoleMappingPrefix) {
|
2022-08-04 11:04:34 +08:00
|
|
|
if strings.Contains(key, "user1") {
|
|
|
|
return []string{key + "/role2", key + "/role1", key + "/role3"}, []string{"value1", "value4", "value2"}, nil
|
|
|
|
} else if strings.Contains(key, "user2") {
|
|
|
|
return []string{key + "/role2"}, []string{"value1"}, nil
|
|
|
|
}
|
|
|
|
return []string{}, []string{}, nil
|
2022-08-20 10:24:51 +08:00
|
|
|
} else if key == rootcoord.CredentialPrefix {
|
2022-08-04 11:04:34 +08:00
|
|
|
return []string{key + "/user1", key + "/user2", key + "/user3"}, []string{string(credentialInfoByte), string(credentialInfoByte), string(credentialInfoByte)}, nil
|
|
|
|
} else {
|
|
|
|
return []string{}, []string{}, fmt.Errorf("load with prefix error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
results, err = mt.SelectUser(util.DefaultTenant, nil, true)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, 3, len(results))
|
|
|
|
for _, result := range results {
|
|
|
|
if result.User.Name == "user1" {
|
|
|
|
assert.Equal(t, 3, len(result.Roles))
|
|
|
|
} else if result.User.Name == "user2" {
|
|
|
|
assert.Equal(t, 1, len(result.Roles))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mockTxnKV.loadWithPrefix = func(key string) ([]string, []string, error) {
|
|
|
|
return []string{}, []string{}, nil
|
|
|
|
}
|
|
|
|
_, err = mt.SelectUser(util.DefaultTenant, &milvuspb.UserEntity{Name: "user"}, true)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
mockTxnKV.loadWithPrefix = func(key string) ([]string, []string, error) {
|
|
|
|
return []string{}, []string{}, fmt.Errorf("load with prefix error")
|
|
|
|
}
|
|
|
|
_, err = mt.SelectUser(util.DefaultTenant, &milvuspb.UserEntity{Name: "user"}, true)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
|
|
|
_, err = mt.SelectUser(util.DefaultTenant, nil, true)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRbacOperatePrivilege(t *testing.T) {
|
|
|
|
mt, _, mockTxnKV, closeCli := generateMetaTable(t)
|
|
|
|
defer closeCli()
|
|
|
|
var err error
|
|
|
|
|
|
|
|
entity := &milvuspb.GrantEntity{}
|
|
|
|
err = mt.OperatePrivilege(util.DefaultTenant, entity, milvuspb.OperatePrivilegeType_Grant)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
|
|
|
entity.ObjectName = "col1"
|
|
|
|
err = mt.OperatePrivilege(util.DefaultTenant, entity, milvuspb.OperatePrivilegeType_Grant)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
|
|
|
entity.Object = &milvuspb.ObjectEntity{Name: commonpb.ObjectType_Collection.String()}
|
|
|
|
err = mt.OperatePrivilege(util.DefaultTenant, entity, milvuspb.OperatePrivilegeType_Grant)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
|
|
|
entity.Role = &milvuspb.RoleEntity{Name: "admin"}
|
|
|
|
err = mt.OperatePrivilege(util.DefaultTenant, entity, milvuspb.OperatePrivilegeType_Grant)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
|
|
|
entity.Grantor = &milvuspb.GrantorEntity{}
|
|
|
|
err = mt.OperatePrivilege(util.DefaultTenant, entity, milvuspb.OperatePrivilegeType_Grant)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
|
|
|
entity.Grantor.Privilege = &milvuspb.PrivilegeEntity{Name: commonpb.ObjectPrivilege_PrivilegeLoad.String()}
|
|
|
|
err = mt.OperatePrivilege(util.DefaultTenant, entity, milvuspb.OperatePrivilegeType_Grant)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
|
|
|
err = mt.OperatePrivilege(util.DefaultTenant, entity, 100)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
|
|
|
mockTxnKV.save = func(key, value string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
mockTxnKV.load = func(key string) (string, error) {
|
|
|
|
return "fail", fmt.Errorf("load error")
|
|
|
|
}
|
|
|
|
entity.Grantor.User = &milvuspb.UserEntity{Name: "user2"}
|
|
|
|
err = mt.OperatePrivilege(util.DefaultTenant, entity, milvuspb.OperatePrivilegeType_Revoke)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
err = mt.OperatePrivilege(util.DefaultTenant, entity, milvuspb.OperatePrivilegeType_Grant)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
mockTxnKV.load = func(key string) (string, error) {
|
|
|
|
return "unmarshal", nil
|
|
|
|
}
|
|
|
|
err = mt.OperatePrivilege(util.DefaultTenant, entity, milvuspb.OperatePrivilegeType_Grant)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
|
|
|
mockTxnKV.load = func(key string) (string, error) {
|
2022-08-10 17:20:41 +08:00
|
|
|
return "fail", common.NewKeyNotExistError(key)
|
2022-08-04 11:04:34 +08:00
|
|
|
}
|
|
|
|
err = mt.OperatePrivilege(util.DefaultTenant, entity, milvuspb.OperatePrivilegeType_Grant)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
grantPrivilegeEntity := &milvuspb.GrantPrivilegeEntity{Entities: []*milvuspb.GrantorEntity{
|
|
|
|
{User: &milvuspb.UserEntity{Name: "aaa"}, Privilege: &milvuspb.PrivilegeEntity{Name: commonpb.ObjectPrivilege_PrivilegeLoad.String()}},
|
|
|
|
}}
|
|
|
|
mockTxnKV.load = func(key string) (string, error) {
|
|
|
|
grantPrivilegeEntityByte, _ := proto.Marshal(grantPrivilegeEntity)
|
|
|
|
return string(grantPrivilegeEntityByte), nil
|
|
|
|
}
|
|
|
|
err = mt.OperatePrivilege(util.DefaultTenant, entity, milvuspb.OperatePrivilegeType_Grant)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
entity.Grantor.Privilege = &milvuspb.PrivilegeEntity{Name: commonpb.ObjectPrivilege_PrivilegeRelease.String()}
|
|
|
|
err = mt.OperatePrivilege(util.DefaultTenant, entity, milvuspb.OperatePrivilegeType_Revoke)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
entity.Grantor.Privilege = &milvuspb.PrivilegeEntity{Name: commonpb.ObjectPrivilege_PrivilegeLoad.String()}
|
|
|
|
grantPrivilegeEntity = &milvuspb.GrantPrivilegeEntity{Entities: []*milvuspb.GrantorEntity{
|
|
|
|
{User: &milvuspb.UserEntity{Name: "user2"}, Privilege: &milvuspb.PrivilegeEntity{Name: commonpb.ObjectPrivilege_PrivilegeLoad.String()}},
|
|
|
|
}}
|
|
|
|
mockTxnKV.load = func(key string) (string, error) {
|
|
|
|
grantPrivilegeEntityByte, _ := proto.Marshal(grantPrivilegeEntity)
|
|
|
|
return string(grantPrivilegeEntityByte), nil
|
|
|
|
}
|
|
|
|
err = mt.OperatePrivilege(util.DefaultTenant, entity, milvuspb.OperatePrivilegeType_Grant)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
mockTxnKV.remove = func(key string) error {
|
|
|
|
return fmt.Errorf("remove error")
|
|
|
|
}
|
|
|
|
err = mt.OperatePrivilege(util.DefaultTenant, entity, milvuspb.OperatePrivilegeType_Revoke)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
mockTxnKV.remove = func(key string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
err = mt.OperatePrivilege(util.DefaultTenant, entity, milvuspb.OperatePrivilegeType_Revoke)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
grantPrivilegeEntity = &milvuspb.GrantPrivilegeEntity{Entities: []*milvuspb.GrantorEntity{
|
|
|
|
{User: &milvuspb.UserEntity{Name: "u3"}, Privilege: &milvuspb.PrivilegeEntity{Name: commonpb.ObjectPrivilege_PrivilegeInsert.String()}},
|
|
|
|
}}
|
|
|
|
mockTxnKV.load = func(key string) (string, error) {
|
|
|
|
grantPrivilegeEntityByte, _ := proto.Marshal(grantPrivilegeEntity)
|
|
|
|
return string(grantPrivilegeEntityByte), nil
|
|
|
|
}
|
|
|
|
mockTxnKV.save = func(key, value string) error {
|
|
|
|
return fmt.Errorf("save error")
|
|
|
|
}
|
|
|
|
err = mt.OperatePrivilege(util.DefaultTenant, entity, milvuspb.OperatePrivilegeType_Grant)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRbacSelectGrant(t *testing.T) {
|
|
|
|
mt, _, mockTxnKV, closeCli := generateMetaTable(t)
|
|
|
|
defer closeCli()
|
|
|
|
var err error
|
|
|
|
|
|
|
|
entity := &milvuspb.GrantEntity{}
|
|
|
|
_, err = mt.SelectGrant(util.DefaultTenant, entity)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
|
|
|
//entity.Role = &milvuspb.RoleEntity{Name: "admin"}
|
|
|
|
_, err = mt.SelectGrant(util.DefaultTenant, entity)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
|
|
|
mockTxnKV.load = func(key string) (string, error) {
|
|
|
|
return "unmarshal error", nil
|
|
|
|
}
|
|
|
|
_, err = mt.SelectGrant(util.DefaultTenant, entity)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
|
|
|
entity.Role = &milvuspb.RoleEntity{Name: "role1"}
|
|
|
|
entity.ObjectName = "col1"
|
|
|
|
entity.Object = &milvuspb.ObjectEntity{Name: "Collection"}
|
|
|
|
|
|
|
|
grantPrivilegeEntity := &milvuspb.GrantPrivilegeEntity{Entities: []*milvuspb.GrantorEntity{
|
|
|
|
{User: &milvuspb.UserEntity{Name: "aaa"}, Privilege: &milvuspb.PrivilegeEntity{Name: "111"}},
|
|
|
|
{User: &milvuspb.UserEntity{Name: "bbb"}, Privilege: &milvuspb.PrivilegeEntity{Name: "222"}},
|
2022-08-15 16:40:48 +08:00
|
|
|
{User: &milvuspb.UserEntity{Name: "ccc"}, Privilege: &milvuspb.PrivilegeEntity{Name: "*"}},
|
2022-08-04 11:04:34 +08:00
|
|
|
}}
|
|
|
|
grantPrivilegeEntityByte, _ := proto.Marshal(grantPrivilegeEntity)
|
|
|
|
|
|
|
|
mockTxnKV.load = func(key string) (string, error) {
|
|
|
|
return "unmarshal error", nil
|
|
|
|
}
|
|
|
|
_, err = mt.SelectGrant(util.DefaultTenant, entity)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
|
|
|
mockTxnKV.load = func(key string) (string, error) {
|
|
|
|
return string(grantPrivilegeEntityByte), nil
|
|
|
|
}
|
|
|
|
results, err := mt.SelectGrant(util.DefaultTenant, entity)
|
|
|
|
assert.Nil(t, err)
|
2022-08-15 16:40:48 +08:00
|
|
|
assert.Equal(t, 3, len(results))
|
2022-08-04 11:04:34 +08:00
|
|
|
|
|
|
|
mockTxnKV.load = func(key string) (string, error) {
|
|
|
|
return "", fmt.Errorf("load error")
|
|
|
|
}
|
|
|
|
_, err = mt.SelectGrant(util.DefaultTenant, entity)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
|
|
|
grantPrivilegeEntity2 := &milvuspb.GrantPrivilegeEntity{Entities: []*milvuspb.GrantorEntity{
|
|
|
|
{User: &milvuspb.UserEntity{Name: "ccc"}, Privilege: &milvuspb.PrivilegeEntity{Name: "333"}},
|
|
|
|
}}
|
|
|
|
grantPrivilegeEntityByte2, _ := proto.Marshal(grantPrivilegeEntity2)
|
|
|
|
|
|
|
|
mockTxnKV.loadWithPrefix = func(key string) ([]string, []string, error) {
|
|
|
|
return []string{key + "/collection/col1", key + "/collection/col2", key + "/collection/a/col3"}, []string{string(grantPrivilegeEntityByte), string(grantPrivilegeEntityByte2), string(grantPrivilegeEntityByte2)}, nil
|
|
|
|
}
|
|
|
|
entity.ObjectName = ""
|
|
|
|
entity.Object = nil
|
|
|
|
results, err = mt.SelectGrant(util.DefaultTenant, entity)
|
|
|
|
assert.Nil(t, err)
|
2022-08-15 16:40:48 +08:00
|
|
|
assert.Equal(t, 4, len(results))
|
2022-08-04 11:04:34 +08:00
|
|
|
|
|
|
|
mockTxnKV.loadWithPrefix = func(key string) ([]string, []string, error) {
|
|
|
|
return nil, nil, fmt.Errorf("load with prefix error")
|
|
|
|
}
|
|
|
|
_, err = mt.SelectGrant(util.DefaultTenant, entity)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
|
|
|
mockTxnKV.loadWithPrefix = func(key string) ([]string, []string, error) {
|
|
|
|
return []string{key + "/collection/col1"}, []string{"unmarshal error"}, nil
|
|
|
|
}
|
|
|
|
_, err = mt.SelectGrant(util.DefaultTenant, entity)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRbacListPolicy(t *testing.T) {
|
|
|
|
mt, _, mockTxnKV, closeCli := generateMetaTable(t)
|
|
|
|
defer closeCli()
|
|
|
|
|
|
|
|
mockTxnKV.loadWithPrefix = func(key string) ([]string, []string, error) {
|
|
|
|
return []string{}, []string{}, fmt.Errorf("load with prefix err")
|
|
|
|
}
|
|
|
|
policies, err := mt.ListPolicy(util.DefaultTenant)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
assert.Equal(t, 0, len(policies))
|
|
|
|
|
|
|
|
grantPrivilegeEntity := &milvuspb.GrantPrivilegeEntity{Entities: []*milvuspb.GrantorEntity{
|
|
|
|
{User: &milvuspb.UserEntity{Name: "aaa"}, Privilege: &milvuspb.PrivilegeEntity{Name: "111"}},
|
|
|
|
{User: &milvuspb.UserEntity{Name: "bbb"}, Privilege: &milvuspb.PrivilegeEntity{Name: "222"}},
|
|
|
|
}}
|
|
|
|
grantPrivilegeEntityByte, _ := proto.Marshal(grantPrivilegeEntity)
|
|
|
|
grantPrivilegeEntity2 := &milvuspb.GrantPrivilegeEntity{Entities: []*milvuspb.GrantorEntity{
|
|
|
|
{User: &milvuspb.UserEntity{Name: "ccc"}, Privilege: &milvuspb.PrivilegeEntity{Name: "333"}},
|
|
|
|
}}
|
|
|
|
grantPrivilegeEntityByte2, _ := proto.Marshal(grantPrivilegeEntity2)
|
|
|
|
mockTxnKV.loadWithPrefix = func(key string) ([]string, []string, error) {
|
|
|
|
return []string{key + "/alice/collection/col1", key + "/tom/collection/col2", key + "/tom/collection/a/col2"}, []string{string(grantPrivilegeEntityByte), string(grantPrivilegeEntityByte2), string(grantPrivilegeEntityByte2)}, nil
|
|
|
|
}
|
|
|
|
policies, err = mt.ListPolicy(util.DefaultTenant)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, 3, len(policies))
|
|
|
|
|
|
|
|
mockTxnKV.loadWithPrefix = func(key string) ([]string, []string, error) {
|
|
|
|
return []string{key + "/alice/collection/col1"}, []string{"unmarshal error"}, nil
|
|
|
|
}
|
|
|
|
_, err = mt.ListPolicy(util.DefaultTenant)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRbacListUserRole(t *testing.T) {
|
|
|
|
mt, _, mockTxnKV, closeCli := generateMetaTable(t)
|
|
|
|
defer closeCli()
|
|
|
|
mockTxnKV.loadWithPrefix = func(key string) ([]string, []string, error) {
|
|
|
|
return []string{}, []string{}, fmt.Errorf("load with prefix err")
|
|
|
|
}
|
|
|
|
userRoles, err := mt.ListUserRole(util.DefaultTenant)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
assert.Equal(t, 0, len(userRoles))
|
|
|
|
|
|
|
|
mockTxnKV.loadWithPrefix = func(key string) ([]string, []string, error) {
|
|
|
|
return []string{key + "/user1/role2", key + "/user2/role2", key + "/user1/role1", key + "/user2/role1", key + "/user2/role1/a"}, []string{"value1", "value2", "values3", "value4", "value5"}, nil
|
|
|
|
}
|
|
|
|
userRoles, err = mt.ListUserRole(util.DefaultTenant)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, 4, len(userRoles))
|
|
|
|
}
|
|
|
|
|
2021-05-18 17:12:17 +08:00
|
|
|
func TestMetaWithTimestamp(t *testing.T) {
|
2021-07-06 09:16:03 +08:00
|
|
|
const (
|
|
|
|
collID1 = typeutil.UniqueID(1)
|
|
|
|
collID2 = typeutil.UniqueID(2)
|
|
|
|
collName1 = "t1"
|
|
|
|
collName2 = "t2"
|
|
|
|
partID1 = 11
|
|
|
|
partID2 = 12
|
|
|
|
partName1 = "p1"
|
|
|
|
partName2 = "p2"
|
|
|
|
)
|
2021-05-18 17:12:17 +08:00
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
randVal := rand.Int()
|
|
|
|
Params.Init()
|
|
|
|
rootPath := fmt.Sprintf("/test/meta/%d", randVal)
|
|
|
|
|
|
|
|
var tsoStart typeutil.Timestamp = 100
|
|
|
|
vtso := tsoStart
|
|
|
|
ftso := func() typeutil.Timestamp {
|
|
|
|
vtso++
|
|
|
|
return vtso
|
|
|
|
}
|
2022-02-07 10:09:45 +08:00
|
|
|
etcdCli, err := etcd.GetEtcdClient(&Params.EtcdCfg)
|
2021-05-18 17:12:17 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
defer etcdCli.Close()
|
|
|
|
|
2022-08-20 10:24:51 +08:00
|
|
|
skv, err := rootcoord.NewMetaSnapshot(etcdCli, rootPath, TimestampPrefix, 7)
|
2021-05-18 17:12:17 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NotNil(t, skv)
|
2021-12-29 14:35:21 +08:00
|
|
|
txnKV := etcdkv.NewEtcdKV(etcdCli, rootPath)
|
2022-08-20 10:24:51 +08:00
|
|
|
mt, err := NewMetaTable(context.TODO(), &rootcoord.Catalog{Txn: txnKV, Snapshot: skv})
|
2021-05-18 17:12:17 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
collInfo := &model.Collection{
|
|
|
|
CollectionID: collID1,
|
|
|
|
Name: collName1,
|
2021-05-18 17:12:17 +08:00
|
|
|
}
|
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
collInfo.Partitions = []*model.Partition{{PartitionID: partID1, PartitionName: partName1, PartitionCreatedTimestamp: ftso()}}
|
2021-08-18 14:36:10 +08:00
|
|
|
t1 := ftso()
|
2022-07-22 10:20:29 +08:00
|
|
|
err = mt.AddCollection(collInfo, t1, "")
|
2021-05-18 17:12:17 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
collInfo.CollectionID = collID2
|
|
|
|
collInfo.Partitions = []*model.Partition{{PartitionID: partID2, PartitionName: partName2, PartitionCreatedTimestamp: ftso()}}
|
|
|
|
collInfo.Name = collName2
|
2021-08-18 14:36:10 +08:00
|
|
|
t2 := ftso()
|
2022-07-22 10:20:29 +08:00
|
|
|
err = mt.AddCollection(collInfo, t2, "")
|
2021-05-18 17:12:17 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2021-07-06 09:16:03 +08:00
|
|
|
assert.True(t, mt.HasCollection(collID1, 0))
|
|
|
|
assert.True(t, mt.HasCollection(collID2, 0))
|
2021-05-18 17:12:17 +08:00
|
|
|
|
2021-07-06 09:16:03 +08:00
|
|
|
assert.True(t, mt.HasCollection(collID1, t2))
|
|
|
|
assert.True(t, mt.HasCollection(collID2, t2))
|
2021-05-18 17:12:17 +08:00
|
|
|
|
2021-07-06 09:16:03 +08:00
|
|
|
assert.True(t, mt.HasCollection(collID1, t1))
|
|
|
|
assert.False(t, mt.HasCollection(collID2, t1))
|
2021-05-18 17:12:17 +08:00
|
|
|
|
2021-07-06 09:16:03 +08:00
|
|
|
assert.False(t, mt.HasCollection(collID1, tsoStart))
|
|
|
|
assert.False(t, mt.HasCollection(collID2, tsoStart))
|
2021-05-18 17:12:17 +08:00
|
|
|
|
2021-07-06 09:16:03 +08:00
|
|
|
c1, err := mt.GetCollectionByID(collID1, 0)
|
2021-05-18 17:12:17 +08:00
|
|
|
assert.Nil(t, err)
|
2021-07-06 09:16:03 +08:00
|
|
|
c2, err := mt.GetCollectionByID(collID2, 0)
|
2021-05-18 17:12:17 +08:00
|
|
|
assert.Nil(t, err)
|
2022-07-22 10:20:29 +08:00
|
|
|
assert.Equal(t, collID1, c1.CollectionID)
|
|
|
|
assert.Equal(t, collID2, c2.CollectionID)
|
2021-05-18 17:12:17 +08:00
|
|
|
|
2021-07-06 09:16:03 +08:00
|
|
|
c1, err = mt.GetCollectionByID(collID1, t2)
|
2021-05-18 17:12:17 +08:00
|
|
|
assert.Nil(t, err)
|
2021-07-06 09:16:03 +08:00
|
|
|
c2, err = mt.GetCollectionByID(collID2, t2)
|
2021-05-18 17:12:17 +08:00
|
|
|
assert.Nil(t, err)
|
2022-07-22 10:20:29 +08:00
|
|
|
assert.Equal(t, collID1, c1.CollectionID)
|
|
|
|
assert.Equal(t, collID2, c2.CollectionID)
|
2021-05-18 17:12:17 +08:00
|
|
|
|
2021-07-06 09:16:03 +08:00
|
|
|
c1, err = mt.GetCollectionByID(collID1, t1)
|
2021-05-18 17:12:17 +08:00
|
|
|
assert.Nil(t, err)
|
2021-07-06 09:16:03 +08:00
|
|
|
c2, err = mt.GetCollectionByID(collID2, t1)
|
2021-05-18 17:12:17 +08:00
|
|
|
assert.NotNil(t, err)
|
2022-07-22 10:20:29 +08:00
|
|
|
assert.Equal(t, int64(1), c1.CollectionID)
|
2021-05-18 17:12:17 +08:00
|
|
|
|
2021-07-06 09:16:03 +08:00
|
|
|
c1, err = mt.GetCollectionByID(collID1, tsoStart)
|
2021-05-18 17:12:17 +08:00
|
|
|
assert.NotNil(t, err)
|
2021-07-06 09:16:03 +08:00
|
|
|
c2, err = mt.GetCollectionByID(collID2, tsoStart)
|
2021-05-18 17:12:17 +08:00
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
2021-07-06 09:16:03 +08:00
|
|
|
c1, err = mt.GetCollectionByName(collName1, 0)
|
2021-05-18 17:12:17 +08:00
|
|
|
assert.Nil(t, err)
|
2021-07-06 09:16:03 +08:00
|
|
|
c2, err = mt.GetCollectionByName(collName2, 0)
|
2021-05-18 17:12:17 +08:00
|
|
|
assert.Nil(t, err)
|
2022-07-22 10:20:29 +08:00
|
|
|
assert.Equal(t, int64(1), c1.CollectionID)
|
|
|
|
assert.Equal(t, int64(2), c2.CollectionID)
|
2021-05-18 17:12:17 +08:00
|
|
|
|
2021-07-06 09:16:03 +08:00
|
|
|
c1, err = mt.GetCollectionByName(collName1, t2)
|
2021-05-18 17:12:17 +08:00
|
|
|
assert.Nil(t, err)
|
2021-07-06 09:16:03 +08:00
|
|
|
c2, err = mt.GetCollectionByName(collName2, t2)
|
2021-05-18 17:12:17 +08:00
|
|
|
assert.Nil(t, err)
|
2022-07-22 10:20:29 +08:00
|
|
|
assert.Equal(t, int64(1), c1.CollectionID)
|
|
|
|
assert.Equal(t, int64(2), c2.CollectionID)
|
2021-05-18 17:12:17 +08:00
|
|
|
|
2021-07-06 09:16:03 +08:00
|
|
|
c1, err = mt.GetCollectionByName(collName1, t1)
|
2021-05-18 17:12:17 +08:00
|
|
|
assert.Nil(t, err)
|
2021-07-06 09:16:03 +08:00
|
|
|
c2, err = mt.GetCollectionByName(collName2, t1)
|
2021-05-18 17:12:17 +08:00
|
|
|
assert.NotNil(t, err)
|
2022-07-22 10:20:29 +08:00
|
|
|
assert.Equal(t, int64(1), c1.CollectionID)
|
2021-05-18 17:12:17 +08:00
|
|
|
|
2021-07-06 09:16:03 +08:00
|
|
|
c1, err = mt.GetCollectionByName(collName1, tsoStart)
|
2021-05-18 17:12:17 +08:00
|
|
|
assert.NotNil(t, err)
|
2021-07-06 09:16:03 +08:00
|
|
|
c2, err = mt.GetCollectionByName(collName2, tsoStart)
|
2021-05-18 17:12:17 +08:00
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
getKeys := func(m map[string]*model.Collection) []string {
|
2021-06-03 19:09:33 +08:00
|
|
|
keys := make([]string, 0, len(m))
|
|
|
|
for key := range m {
|
|
|
|
keys = append(keys, key)
|
|
|
|
}
|
|
|
|
return keys
|
|
|
|
}
|
|
|
|
|
2021-05-18 17:12:17 +08:00
|
|
|
s1, err := mt.ListCollections(0)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, 2, len(s1))
|
2021-07-06 09:16:03 +08:00
|
|
|
assert.ElementsMatch(t, getKeys(s1), []string{collName1, collName2})
|
2021-05-18 17:12:17 +08:00
|
|
|
|
|
|
|
s1, err = mt.ListCollections(t2)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, 2, len(s1))
|
2021-07-06 09:16:03 +08:00
|
|
|
assert.ElementsMatch(t, getKeys(s1), []string{collName1, collName2})
|
2021-05-18 17:12:17 +08:00
|
|
|
|
|
|
|
s1, err = mt.ListCollections(t1)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, 1, len(s1))
|
2021-07-06 09:16:03 +08:00
|
|
|
assert.ElementsMatch(t, getKeys(s1), []string{collName1})
|
2021-05-18 17:12:17 +08:00
|
|
|
|
|
|
|
s1, err = mt.ListCollections(tsoStart)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, 0, len(s1))
|
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
p1, err := mt.GetPartitionByName(collID1, partName1, 0)
|
2022-06-10 13:10:08 +08:00
|
|
|
assert.Nil(t, err)
|
2022-07-22 10:20:29 +08:00
|
|
|
p2, err := mt.GetPartitionByName(collID2, partName2, 0)
|
2021-05-18 17:12:17 +08:00
|
|
|
assert.Nil(t, err)
|
2022-07-22 10:20:29 +08:00
|
|
|
assert.Equal(t, int64(partID1), p1)
|
|
|
|
assert.Equal(t, int64(partID2), p2)
|
2021-05-18 17:12:17 +08:00
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
p1, err = mt.GetPartitionByName(collID1, partName1, t2)
|
2021-05-18 17:12:17 +08:00
|
|
|
assert.Nil(t, err)
|
2022-07-22 10:20:29 +08:00
|
|
|
p2, err = mt.GetPartitionByName(collID2, partName2, t2)
|
2021-05-18 17:12:17 +08:00
|
|
|
assert.Nil(t, err)
|
2021-07-03 14:36:18 +08:00
|
|
|
assert.Equal(t, int64(11), p1)
|
|
|
|
assert.Equal(t, int64(12), p2)
|
2021-05-18 17:12:17 +08:00
|
|
|
|
2021-07-06 09:16:03 +08:00
|
|
|
p1, err = mt.GetPartitionByName(1, partName1, t1)
|
2021-05-18 17:12:17 +08:00
|
|
|
assert.Nil(t, err)
|
2021-07-06 09:16:03 +08:00
|
|
|
_, err = mt.GetPartitionByName(2, partName2, t1)
|
2021-05-18 17:12:17 +08:00
|
|
|
assert.NotNil(t, err)
|
2021-07-03 14:36:18 +08:00
|
|
|
assert.Equal(t, int64(11), p1)
|
2021-05-18 17:12:17 +08:00
|
|
|
|
2021-07-06 09:16:03 +08:00
|
|
|
_, err = mt.GetPartitionByName(1, partName1, tsoStart)
|
2021-05-18 17:12:17 +08:00
|
|
|
assert.NotNil(t, err)
|
2021-07-06 09:16:03 +08:00
|
|
|
_, err = mt.GetPartitionByName(2, partName2, tsoStart)
|
2021-05-18 17:12:17 +08:00
|
|
|
assert.NotNil(t, err)
|
2022-05-09 20:47:52 +08:00
|
|
|
|
|
|
|
var cID UniqueID
|
|
|
|
cID, err = mt.GetCollectionIDByName(collName1)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, collID1, cID)
|
|
|
|
|
|
|
|
_, err = mt.GetCollectionIDByName("badname")
|
|
|
|
assert.Error(t, err)
|
2022-06-14 16:18:09 +08:00
|
|
|
|
|
|
|
name, err := mt.GetCollectionNameByID(collID2)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, collName2, name)
|
|
|
|
|
|
|
|
_, err = mt.GetCollectionNameByID(int64(999))
|
|
|
|
assert.Error(t, err)
|
2021-05-18 17:12:17 +08:00
|
|
|
}
|
2021-10-26 23:00:27 +08:00
|
|
|
|
|
|
|
func TestFixIssue10540(t *testing.T) {
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
randVal := rand.Int()
|
|
|
|
Params.Init()
|
|
|
|
rootPath := fmt.Sprintf("/test/meta/%d", randVal)
|
|
|
|
|
2022-02-07 10:09:45 +08:00
|
|
|
etcdCli, err := etcd.GetEtcdClient(&Params.EtcdCfg)
|
2021-10-26 23:00:27 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
defer etcdCli.Close()
|
|
|
|
|
2022-08-20 10:24:51 +08:00
|
|
|
skv, err := rootcoord.NewMetaSnapshot(etcdCli, rootPath, TimestampPrefix, 7)
|
2021-10-26 23:00:27 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NotNil(t, skv)
|
|
|
|
//txnKV := etcdkv.NewEtcdKVWithClient(etcdCli, rootPath)
|
|
|
|
txnKV := memkv.NewMemoryKV()
|
|
|
|
// compose rc7 legace tombstone cases
|
2022-08-20 10:24:51 +08:00
|
|
|
txnKV.Save(path.Join(rootcoord.SegmentIndexMetaPrefix, "2"), string(rootcoord.SuffixSnapshotTombstone))
|
|
|
|
txnKV.Save(path.Join(rootcoord.IndexMetaPrefix, "3"), string(rootcoord.SuffixSnapshotTombstone))
|
2021-10-26 23:00:27 +08:00
|
|
|
|
2022-08-20 10:24:51 +08:00
|
|
|
_, err = NewMetaTable(context.TODO(), &rootcoord.Catalog{Txn: txnKV, Snapshot: skv})
|
2021-10-26 23:00:27 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
}
|
2022-03-30 21:11:28 +08:00
|
|
|
|
|
|
|
func TestMetaTable_GetSegmentIndexInfos(t *testing.T) {
|
|
|
|
meta := &MetaTable{
|
2022-07-22 10:20:29 +08:00
|
|
|
segID2IndexID: make(map[typeutil.UniqueID]typeutil.UniqueID, 1),
|
|
|
|
indexID2Meta: make(map[typeutil.UniqueID]*model.Index, 1),
|
2022-03-30 21:11:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
segID := typeutil.UniqueID(100)
|
|
|
|
_, err := meta.GetSegmentIndexInfos(segID)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
meta.segID2IndexID[segID] = 5
|
|
|
|
meta.indexID2Meta[5] = &model.Index{
|
|
|
|
CollectionID: 1,
|
|
|
|
FieldID: 4,
|
|
|
|
IndexID: 5,
|
|
|
|
SegmentIndexes: map[int64]model.SegmentIndex{
|
|
|
|
segID: {
|
|
|
|
Segment: model.Segment{
|
|
|
|
SegmentID: segID,
|
|
|
|
PartitionID: 2,
|
|
|
|
},
|
|
|
|
BuildID: 6,
|
|
|
|
EnableIndex: true,
|
|
|
|
},
|
2022-03-30 21:11:28 +08:00
|
|
|
},
|
|
|
|
}
|
2022-07-22 10:20:29 +08:00
|
|
|
|
|
|
|
indexMeta, err := meta.GetSegmentIndexInfos(segID)
|
2022-03-30 21:11:28 +08:00
|
|
|
assert.NoError(t, err)
|
2022-07-22 10:20:29 +08:00
|
|
|
segmentIndex, ok := indexMeta.SegmentIndexes[segID]
|
2022-03-30 21:11:28 +08:00
|
|
|
assert.True(t, ok)
|
2022-07-22 10:20:29 +08:00
|
|
|
assert.Equal(t, typeutil.UniqueID(1), indexMeta.CollectionID)
|
|
|
|
segment := segmentIndex.Segment
|
|
|
|
assert.Equal(t, typeutil.UniqueID(2), segment.PartitionID)
|
|
|
|
assert.Equal(t, segID, segment.SegmentID)
|
|
|
|
assert.Equal(t, typeutil.UniqueID(4), indexMeta.FieldID)
|
|
|
|
assert.Equal(t, typeutil.UniqueID(5), indexMeta.IndexID)
|
|
|
|
assert.Equal(t, typeutil.UniqueID(6), segmentIndex.BuildID)
|
|
|
|
assert.Equal(t, true, segmentIndex.EnableIndex)
|
2022-03-30 21:11:28 +08:00
|
|
|
}
|
2022-05-10 21:07:53 +08:00
|
|
|
|
|
|
|
func TestMetaTable_unlockGetCollectionInfo(t *testing.T) {
|
|
|
|
t.Run("normal case", func(t *testing.T) {
|
|
|
|
mt := &MetaTable{
|
|
|
|
collName2ID: map[string]typeutil.UniqueID{"test": 100},
|
2022-07-22 10:20:29 +08:00
|
|
|
collID2Meta: map[typeutil.UniqueID]model.Collection{
|
|
|
|
100: {CollectionID: 100, Name: "test"},
|
2022-05-10 21:07:53 +08:00
|
|
|
},
|
|
|
|
}
|
2022-06-18 12:50:11 +08:00
|
|
|
info, err := mt.getCollectionInfoInternal("test")
|
2022-05-10 21:07:53 +08:00
|
|
|
assert.NoError(t, err)
|
2022-07-22 10:20:29 +08:00
|
|
|
assert.Equal(t, UniqueID(100), info.CollectionID)
|
|
|
|
assert.Equal(t, "test", info.Name)
|
2022-05-10 21:07:53 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("collection name not found", func(t *testing.T) {
|
|
|
|
mt := &MetaTable{collName2ID: nil, collAlias2ID: nil}
|
2022-06-18 12:50:11 +08:00
|
|
|
_, err := mt.getCollectionInfoInternal("test")
|
2022-05-10 21:07:53 +08:00
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("name found, meta not found", func(t *testing.T) {
|
|
|
|
mt := &MetaTable{
|
|
|
|
collName2ID: map[string]typeutil.UniqueID{"test": 100},
|
|
|
|
collAlias2ID: nil,
|
|
|
|
collID2Meta: nil,
|
|
|
|
}
|
2022-06-18 12:50:11 +08:00
|
|
|
_, err := mt.getCollectionInfoInternal("test")
|
2022-05-10 21:07:53 +08:00
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("alias found, meta not found", func(t *testing.T) {
|
|
|
|
mt := &MetaTable{
|
|
|
|
collName2ID: nil,
|
|
|
|
collAlias2ID: map[string]typeutil.UniqueID{"test": 100},
|
|
|
|
collID2Meta: nil,
|
|
|
|
}
|
2022-06-18 12:50:11 +08:00
|
|
|
_, err := mt.getCollectionInfoInternal("test")
|
2022-05-10 21:07:53 +08:00
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMetaTable_checkFieldCanBeIndexed(t *testing.T) {
|
|
|
|
t.Run("field not indexed", func(t *testing.T) {
|
|
|
|
mt := &MetaTable{}
|
2022-07-22 10:20:29 +08:00
|
|
|
collMeta := model.Collection{
|
|
|
|
FieldIDToIndexID: []common.Int64Tuple{{Key: 100, Value: 200}},
|
2022-05-10 21:07:53 +08:00
|
|
|
}
|
2022-07-22 10:20:29 +08:00
|
|
|
fieldSchema := model.Field{
|
2022-05-10 21:07:53 +08:00
|
|
|
FieldID: 101,
|
|
|
|
}
|
2022-07-22 10:20:29 +08:00
|
|
|
idxInfo := &model.Index{}
|
2022-05-10 21:07:53 +08:00
|
|
|
err := mt.checkFieldCanBeIndexed(collMeta, fieldSchema, idxInfo)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("field already indexed", func(t *testing.T) {
|
|
|
|
mt := &MetaTable{
|
2022-07-22 10:20:29 +08:00
|
|
|
indexID2Meta: map[typeutil.UniqueID]*model.Index{
|
2022-05-10 21:07:53 +08:00
|
|
|
200: {IndexID: 200, IndexName: "test"},
|
|
|
|
},
|
|
|
|
}
|
2022-07-22 10:20:29 +08:00
|
|
|
collMeta := model.Collection{
|
|
|
|
Name: "test",
|
|
|
|
FieldIDToIndexID: []common.Int64Tuple{{Key: 100, Value: 200}},
|
2022-05-10 21:07:53 +08:00
|
|
|
}
|
2022-07-22 10:20:29 +08:00
|
|
|
fieldSchema := model.Field{Name: "test", FieldID: 100}
|
|
|
|
idxInfo := &model.Index{IndexName: "not_test"}
|
2022-05-10 21:07:53 +08:00
|
|
|
err := mt.checkFieldCanBeIndexed(collMeta, fieldSchema, idxInfo)
|
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("unexpected", func(t *testing.T) {
|
|
|
|
mt := &MetaTable{
|
|
|
|
// index meta incomplete.
|
2022-07-22 10:20:29 +08:00
|
|
|
indexID2Meta: map[typeutil.UniqueID]*model.Index{},
|
2022-05-10 21:07:53 +08:00
|
|
|
}
|
2022-07-22 10:20:29 +08:00
|
|
|
collMeta := model.Collection{
|
|
|
|
Name: "test",
|
|
|
|
CollectionID: 1000,
|
|
|
|
FieldIDToIndexID: []common.Int64Tuple{{Key: 100, Value: 200}},
|
2022-05-10 21:07:53 +08:00
|
|
|
}
|
2022-07-22 10:20:29 +08:00
|
|
|
fieldSchema := model.Field{Name: "test", FieldID: 100}
|
|
|
|
idxInfo := &model.Index{IndexName: "not_test"}
|
2022-05-10 21:07:53 +08:00
|
|
|
err := mt.checkFieldCanBeIndexed(collMeta, fieldSchema, idxInfo)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMetaTable_checkFieldIndexDuplicate(t *testing.T) {
|
|
|
|
t.Run("index already exists", func(t *testing.T) {
|
|
|
|
mt := &MetaTable{
|
2022-07-22 10:20:29 +08:00
|
|
|
indexID2Meta: map[typeutil.UniqueID]*model.Index{
|
2022-05-10 21:07:53 +08:00
|
|
|
200: {IndexID: 200, IndexName: "test"},
|
|
|
|
},
|
|
|
|
}
|
2022-07-22 10:20:29 +08:00
|
|
|
collMeta := model.Collection{
|
|
|
|
Name: "test",
|
|
|
|
FieldIDToIndexID: []common.Int64Tuple{{Key: 100, Value: 200}},
|
2022-05-10 21:07:53 +08:00
|
|
|
}
|
2022-07-22 10:20:29 +08:00
|
|
|
fieldSchema := model.Field{Name: "test", FieldID: 101}
|
|
|
|
idxInfo := &model.Index{IndexName: "test"}
|
2022-06-21 18:44:12 +08:00
|
|
|
_, _, err := mt.checkFieldIndexDuplicate(collMeta, fieldSchema, idxInfo)
|
2022-05-10 21:07:53 +08:00
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("index parameters mismatch", func(t *testing.T) {
|
|
|
|
mt := &MetaTable{
|
2022-07-22 10:20:29 +08:00
|
|
|
indexID2Meta: map[typeutil.UniqueID]*model.Index{
|
2022-05-10 21:07:53 +08:00
|
|
|
200: {IndexID: 200, IndexName: "test",
|
|
|
|
IndexParams: []*commonpb.KeyValuePair{{Key: "Key", Value: "Value"}}},
|
|
|
|
},
|
|
|
|
}
|
2022-07-22 10:20:29 +08:00
|
|
|
collMeta := model.Collection{
|
|
|
|
Name: "test",
|
|
|
|
FieldIDToIndexID: []common.Int64Tuple{{Key: 100, Value: 200}},
|
2022-05-10 21:07:53 +08:00
|
|
|
}
|
2022-07-22 10:20:29 +08:00
|
|
|
fieldSchema := model.Field{Name: "test", FieldID: 100}
|
|
|
|
idxInfo := &model.Index{IndexName: "test", IndexParams: []*commonpb.KeyValuePair{{Key: "Key", Value: "not_Value"}}}
|
2022-06-21 18:44:12 +08:00
|
|
|
_, _, err := mt.checkFieldIndexDuplicate(collMeta, fieldSchema, idxInfo)
|
2022-05-10 21:07:53 +08:00
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("index parameters match", func(t *testing.T) {
|
|
|
|
mt := &MetaTable{
|
2022-07-22 10:20:29 +08:00
|
|
|
indexID2Meta: map[typeutil.UniqueID]*model.Index{
|
2022-05-10 21:07:53 +08:00
|
|
|
200: {IndexID: 200, IndexName: "test",
|
|
|
|
IndexParams: []*commonpb.KeyValuePair{{Key: "Key", Value: "Value"}}},
|
|
|
|
},
|
|
|
|
}
|
2022-07-22 10:20:29 +08:00
|
|
|
collMeta := model.Collection{
|
|
|
|
Name: "test",
|
|
|
|
FieldIDToIndexID: []common.Int64Tuple{{Key: 100, Value: 200}},
|
2022-05-10 21:07:53 +08:00
|
|
|
}
|
2022-07-22 10:20:29 +08:00
|
|
|
fieldSchema := model.Field{Name: "test", FieldID: 100}
|
|
|
|
idxInfo := &model.Index{IndexName: "test", IndexParams: []*commonpb.KeyValuePair{{Key: "Key", Value: "Value"}}}
|
2022-06-21 18:44:12 +08:00
|
|
|
duplicate, dupIdxInfo, err := mt.checkFieldIndexDuplicate(collMeta, fieldSchema, idxInfo)
|
2022-05-10 21:07:53 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, duplicate)
|
2022-06-21 18:44:12 +08:00
|
|
|
assert.Equal(t, idxInfo.IndexName, dupIdxInfo.IndexName)
|
2022-05-10 21:07:53 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("field not found", func(t *testing.T) {
|
|
|
|
mt := &MetaTable{}
|
2022-07-22 10:20:29 +08:00
|
|
|
collMeta := model.Collection{
|
|
|
|
FieldIDToIndexID: []common.Int64Tuple{{Key: 100, Value: 200}},
|
2022-05-10 21:07:53 +08:00
|
|
|
}
|
2022-07-22 10:20:29 +08:00
|
|
|
fieldSchema := model.Field{
|
2022-05-10 21:07:53 +08:00
|
|
|
FieldID: 101,
|
|
|
|
}
|
2022-07-22 10:20:29 +08:00
|
|
|
idxInfo := &model.Index{}
|
2022-06-21 18:44:12 +08:00
|
|
|
duplicate, dupIdxInfo, err := mt.checkFieldIndexDuplicate(collMeta, fieldSchema, idxInfo)
|
2022-05-10 21:07:53 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, duplicate)
|
2022-06-21 18:44:12 +08:00
|
|
|
assert.Nil(t, dupIdxInfo)
|
2022-05-10 21:07:53 +08:00
|
|
|
})
|
|
|
|
}
|
2022-06-16 20:12:11 +08:00
|
|
|
|
|
|
|
func TestMetaTable_GetInitBuildIDs(t *testing.T) {
|
|
|
|
var (
|
|
|
|
collName = "GetInitBuildID-Coll"
|
|
|
|
indexName = "GetInitBuildID-Index"
|
|
|
|
)
|
|
|
|
mt := &MetaTable{
|
2022-07-22 10:20:29 +08:00
|
|
|
collID2Meta: map[typeutil.UniqueID]model.Collection{
|
2022-06-16 20:12:11 +08:00
|
|
|
1: {
|
2022-07-22 10:20:29 +08:00
|
|
|
FieldIDToIndexID: []common.Int64Tuple{
|
2022-06-16 20:12:11 +08:00
|
|
|
{
|
2022-07-22 10:20:29 +08:00
|
|
|
Key: 1,
|
|
|
|
Value: 1,
|
2022-06-16 20:12:11 +08:00
|
|
|
},
|
|
|
|
{
|
2022-07-22 10:20:29 +08:00
|
|
|
Key: 2,
|
|
|
|
Value: 2,
|
2022-06-16 20:12:11 +08:00
|
|
|
},
|
|
|
|
{
|
2022-07-22 10:20:29 +08:00
|
|
|
Key: 3,
|
|
|
|
Value: 3,
|
2022-06-16 20:12:11 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
collName2ID: map[string]typeutil.UniqueID{
|
|
|
|
"GetInitBuildID-Coll-1": 2,
|
|
|
|
},
|
2022-07-22 10:20:29 +08:00
|
|
|
indexID2Meta: map[typeutil.UniqueID]*model.Index{
|
2022-06-16 20:12:11 +08:00
|
|
|
1: {
|
2022-07-22 10:20:29 +08:00
|
|
|
IndexName: "GetInitBuildID-Index-1",
|
|
|
|
IndexID: 1,
|
|
|
|
SegmentIndexes: map[int64]model.SegmentIndex{
|
|
|
|
4: {
|
|
|
|
Segment: model.Segment{
|
|
|
|
SegmentID: 4,
|
|
|
|
},
|
|
|
|
EnableIndex: true,
|
|
|
|
CreateTime: 5,
|
|
|
|
},
|
|
|
|
},
|
2022-06-21 18:44:12 +08:00
|
|
|
CreateTime: 10,
|
2022-06-16 20:12:11 +08:00
|
|
|
},
|
|
|
|
2: {
|
2022-06-21 18:44:12 +08:00
|
|
|
IndexName: "GetInitBuildID-Index-2",
|
|
|
|
IndexID: 2,
|
|
|
|
CreateTime: 10,
|
2022-06-16 20:12:11 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
t.Run("coll not exist", func(t *testing.T) {
|
|
|
|
buildIDs, err := mt.GetInitBuildIDs("collName", indexName)
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Nil(t, buildIDs)
|
|
|
|
})
|
|
|
|
|
|
|
|
mt.collName2ID[collName] = 1
|
|
|
|
|
|
|
|
t.Run("index not exist", func(t *testing.T) {
|
|
|
|
buildIDs, err := mt.GetInitBuildIDs(collName, indexName)
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Nil(t, buildIDs)
|
|
|
|
})
|
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
mt.indexID2Meta[3] = &model.Index{
|
2022-06-21 18:44:12 +08:00
|
|
|
IndexName: indexName,
|
|
|
|
IndexID: 3,
|
|
|
|
CreateTime: 10,
|
2022-07-22 10:20:29 +08:00
|
|
|
SegmentIndexes: map[int64]model.SegmentIndex{
|
|
|
|
5: {
|
|
|
|
Segment: model.Segment{
|
|
|
|
SegmentID: 5,
|
|
|
|
},
|
2022-06-16 20:12:11 +08:00
|
|
|
EnableIndex: true,
|
2022-06-21 18:44:12 +08:00
|
|
|
CreateTime: 5,
|
2022-06-16 20:12:11 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
mt.segID2IndexID = map[typeutil.UniqueID]typeutil.UniqueID{
|
|
|
|
4: 1,
|
|
|
|
5: 3,
|
|
|
|
}
|
|
|
|
|
2022-06-16 20:12:11 +08:00
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
buildIDs, err := mt.GetInitBuildIDs(collName, indexName)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, len(buildIDs))
|
|
|
|
})
|
|
|
|
}
|
2022-06-17 18:08:12 +08:00
|
|
|
|
|
|
|
func TestMetaTable_GetDroppedIndex(t *testing.T) {
|
|
|
|
mt := &MetaTable{
|
2022-07-22 10:20:29 +08:00
|
|
|
collID2Meta: map[typeutil.UniqueID]model.Collection{
|
2022-06-17 18:08:12 +08:00
|
|
|
1: {
|
2022-07-22 10:20:29 +08:00
|
|
|
FieldIDToIndexID: []common.Int64Tuple{
|
2022-06-17 18:08:12 +08:00
|
|
|
{
|
2022-07-22 10:20:29 +08:00
|
|
|
Key: 1,
|
|
|
|
Value: 1,
|
2022-06-17 18:08:12 +08:00
|
|
|
},
|
|
|
|
{
|
2022-07-22 10:20:29 +08:00
|
|
|
Key: 2,
|
|
|
|
Value: 2,
|
2022-06-17 18:08:12 +08:00
|
|
|
},
|
|
|
|
{
|
2022-07-22 10:20:29 +08:00
|
|
|
Key: 3,
|
|
|
|
Value: 3,
|
2022-06-17 18:08:12 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-07-22 10:20:29 +08:00
|
|
|
indexID2Meta: map[typeutil.UniqueID]*model.Index{
|
2022-06-17 18:08:12 +08:00
|
|
|
1: {
|
|
|
|
IndexName: "GetInitBuildID-Index-1",
|
|
|
|
IndexID: 1,
|
2022-07-22 10:20:29 +08:00
|
|
|
IsDeleted: true,
|
2022-06-17 18:08:12 +08:00
|
|
|
},
|
|
|
|
2: {
|
|
|
|
IndexName: "GetInitBuildID-Index-2",
|
|
|
|
IndexID: 2,
|
2022-07-22 10:20:29 +08:00
|
|
|
IsDeleted: false,
|
2022-06-17 18:08:12 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
droppedIndexID := mt.GetDroppedIndex()
|
|
|
|
indexInfo, ok := droppedIndexID[1]
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, 1, len(indexInfo))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMetaTable_AlignSegmentsMeta(t *testing.T) {
|
|
|
|
var (
|
|
|
|
indexName = "GetDroppedIndex-Index"
|
|
|
|
collID = UniqueID(1)
|
|
|
|
partID = UniqueID(10)
|
|
|
|
indexID = UniqueID(1000)
|
|
|
|
)
|
|
|
|
mt := &MetaTable{
|
2022-08-20 10:24:51 +08:00
|
|
|
catalog: &rootcoord.Catalog{
|
2022-08-11 12:12:38 +08:00
|
|
|
Txn: &mockTestTxnKV{
|
|
|
|
multiRemove: func(keys []string) error {
|
|
|
|
return nil
|
|
|
|
},
|
2022-06-17 18:08:12 +08:00
|
|
|
},
|
|
|
|
},
|
2022-07-22 10:20:29 +08:00
|
|
|
collID2Meta: map[typeutil.UniqueID]model.Collection{
|
2022-06-17 18:08:12 +08:00
|
|
|
collID: {
|
2022-07-22 10:20:29 +08:00
|
|
|
FieldIDToIndexID: []common.Int64Tuple{
|
2022-06-17 18:08:12 +08:00
|
|
|
{
|
2022-07-22 10:20:29 +08:00
|
|
|
Key: 1,
|
|
|
|
Value: 1,
|
2022-06-17 18:08:12 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-07-22 10:20:29 +08:00
|
|
|
partID2IndexedSegID: map[typeutil.UniqueID]map[typeutil.UniqueID]bool{
|
2022-06-17 18:08:12 +08:00
|
|
|
partID: {
|
|
|
|
100: true,
|
|
|
|
101: true,
|
|
|
|
102: true,
|
|
|
|
},
|
|
|
|
},
|
2022-07-22 10:20:29 +08:00
|
|
|
indexID2Meta: map[typeutil.UniqueID]*model.Index{
|
2022-06-17 18:08:12 +08:00
|
|
|
indexID: {
|
|
|
|
IndexName: indexName,
|
|
|
|
IndexID: 1,
|
2022-07-22 10:20:29 +08:00
|
|
|
IsDeleted: true,
|
2022-06-17 18:08:12 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
mt.AlignSegmentsMeta(collID, partID, map[UniqueID]struct{}{101: {}, 102: {}, 103: {}})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("txn error", func(t *testing.T) {
|
|
|
|
txn := &mockTestTxnKV{
|
|
|
|
multiRemove: func(keys []string) error {
|
|
|
|
return fmt.Errorf("error occurred")
|
|
|
|
},
|
|
|
|
}
|
2022-08-20 10:24:51 +08:00
|
|
|
mt.catalog = &rootcoord.Catalog{Txn: txn}
|
2022-06-17 18:08:12 +08:00
|
|
|
mt.AlignSegmentsMeta(collID, partID, map[UniqueID]struct{}{103: {}, 104: {}, 105: {}})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMetaTable_MarkIndexDeleted(t *testing.T) {
|
|
|
|
var (
|
|
|
|
collName = "MarkIndexDeleted-Coll"
|
|
|
|
fieldName = "MarkIndexDeleted-Field"
|
|
|
|
indexName = "MarkIndexDeleted-Index"
|
|
|
|
collID = UniqueID(1)
|
|
|
|
partID = UniqueID(10)
|
|
|
|
fieldID = UniqueID(100)
|
|
|
|
indexID = UniqueID(1000)
|
|
|
|
)
|
|
|
|
mt := &MetaTable{
|
2022-07-22 10:20:29 +08:00
|
|
|
ctx: context.TODO(),
|
|
|
|
collID2Meta: map[typeutil.UniqueID]model.Collection{
|
2022-06-17 18:08:12 +08:00
|
|
|
collID: {
|
2022-07-22 10:20:29 +08:00
|
|
|
FieldIDToIndexID: []common.Int64Tuple{
|
2022-06-17 18:08:12 +08:00
|
|
|
{
|
2022-07-22 10:20:29 +08:00
|
|
|
Key: 101,
|
|
|
|
Value: 1001,
|
2022-06-17 18:08:12 +08:00
|
|
|
},
|
|
|
|
},
|
2022-07-22 10:20:29 +08:00
|
|
|
Fields: []*model.Field{
|
|
|
|
{
|
|
|
|
FieldID: fieldID,
|
|
|
|
Name: fieldName,
|
2022-06-17 18:08:12 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
collName2ID: map[string]typeutil.UniqueID{
|
|
|
|
collName: collID,
|
|
|
|
},
|
2022-07-22 10:20:29 +08:00
|
|
|
partID2IndexedSegID: map[typeutil.UniqueID]map[typeutil.UniqueID]bool{
|
2022-06-17 18:08:12 +08:00
|
|
|
partID: {
|
|
|
|
100: true,
|
|
|
|
101: true,
|
|
|
|
102: true,
|
|
|
|
},
|
|
|
|
},
|
2022-07-22 10:20:29 +08:00
|
|
|
indexID2Meta: map[typeutil.UniqueID]*model.Index{
|
2022-06-17 18:08:12 +08:00
|
|
|
1001: {
|
|
|
|
IndexName: indexName,
|
|
|
|
IndexID: indexID,
|
2022-07-22 10:20:29 +08:00
|
|
|
IsDeleted: true,
|
2022-06-17 18:08:12 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("get collection meta failed", func(t *testing.T) {
|
|
|
|
err := mt.MarkIndexDeleted("collName", fieldName, indexName)
|
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("get field meta failed", func(t *testing.T) {
|
|
|
|
err := mt.MarkIndexDeleted(collName, "fieldName", indexName)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
err = mt.MarkIndexDeleted(collName, fieldName, indexName)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("indexMeta error", func(t *testing.T) {
|
2022-07-22 10:20:29 +08:00
|
|
|
collMeta := model.Collection{
|
|
|
|
FieldIDToIndexID: []common.Int64Tuple{
|
2022-06-17 18:08:12 +08:00
|
|
|
{
|
2022-07-22 10:20:29 +08:00
|
|
|
Key: fieldID,
|
|
|
|
Value: indexID,
|
2022-06-17 18:08:12 +08:00
|
|
|
},
|
|
|
|
},
|
2022-07-22 10:20:29 +08:00
|
|
|
Fields: []*model.Field{
|
|
|
|
{
|
|
|
|
FieldID: fieldID,
|
|
|
|
Name: fieldName,
|
2022-06-17 18:08:12 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
mt.collID2Meta[collID] = collMeta
|
|
|
|
|
|
|
|
err := mt.MarkIndexDeleted(collName, fieldName, indexName)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
mt.indexID2Meta[indexID] = &model.Index{
|
2022-06-17 18:08:12 +08:00
|
|
|
IndexName: "indexName",
|
|
|
|
IndexID: indexID,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = mt.MarkIndexDeleted(collName, fieldName, indexName)
|
|
|
|
assert.NoError(t, err)
|
2022-06-20 15:22:13 +08:00
|
|
|
})
|
2022-06-17 18:08:12 +08:00
|
|
|
|
2022-06-20 15:22:13 +08:00
|
|
|
t.Run("txn save failed", func(t *testing.T) {
|
2022-07-22 10:20:29 +08:00
|
|
|
mt.indexID2Meta[indexID] = &model.Index{
|
2022-06-17 18:08:12 +08:00
|
|
|
IndexName: indexName,
|
|
|
|
IndexID: indexID,
|
|
|
|
}
|
2022-07-22 10:20:29 +08:00
|
|
|
mc := &MockedCatalog{}
|
|
|
|
targetErr := errors.New("alter add index fail")
|
2022-06-17 18:08:12 +08:00
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
mc.On("AlterIndex").Return(targetErr)
|
|
|
|
mt.catalog = mc
|
2022-06-17 18:08:12 +08:00
|
|
|
err := mt.MarkIndexDeleted(collName, fieldName, indexName)
|
|
|
|
assert.Error(t, err)
|
2022-07-22 10:20:29 +08:00
|
|
|
assert.True(t, errors.Is(err, targetErr))
|
2022-06-20 15:22:13 +08:00
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
mc = &MockedCatalog{}
|
|
|
|
mc.On("AlterIndex").Return(nil)
|
|
|
|
mt.catalog = mc
|
2022-06-20 15:22:13 +08:00
|
|
|
err = mt.MarkIndexDeleted(collName, fieldName, indexName)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = mt.MarkIndexDeleted(collName, fieldName, indexName)
|
|
|
|
assert.NoError(t, err)
|
2022-06-17 18:08:12 +08:00
|
|
|
})
|
|
|
|
}
|
2022-06-21 18:44:12 +08:00
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
type MockedCatalog struct {
|
|
|
|
mock.Mock
|
2022-08-20 10:24:51 +08:00
|
|
|
metastore.RootCoordCatalog
|
2022-07-22 10:20:29 +08:00
|
|
|
alterIndexParamsVerification func(ctx context.Context, oldIndex *model.Index, newIndex *model.Index, alterType metastore.AlterType)
|
|
|
|
createIndexParamsVerification func(ctx context.Context, col *model.Collection, index *model.Index)
|
2022-08-11 12:12:38 +08:00
|
|
|
dropIndexParamsVerification func(ctx context.Context, collectionInfo *model.Collection, dropIdxID typeutil.UniqueID)
|
2022-07-22 10:20:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mc *MockedCatalog) ListCollections(ctx context.Context, ts typeutil.Timestamp) (map[string]*model.Collection, error) {
|
|
|
|
args := mc.Called()
|
|
|
|
return args.Get(0).(map[string]*model.Collection), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mc *MockedCatalog) ListIndexes(ctx context.Context) ([]*model.Index, error) {
|
|
|
|
args := mc.Called()
|
|
|
|
return args.Get(0).([]*model.Index), nil
|
|
|
|
}
|
|
|
|
|
2022-08-10 10:22:38 +08:00
|
|
|
func (mc *MockedCatalog) ListAliases(ctx context.Context, ts typeutil.Timestamp) ([]*model.Alias, error) {
|
2022-07-22 10:20:29 +08:00
|
|
|
args := mc.Called()
|
2022-08-10 10:22:38 +08:00
|
|
|
return args.Get(0).([]*model.Alias), nil
|
2022-07-22 10:20:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mc *MockedCatalog) AlterIndex(ctx context.Context, oldIndex *model.Index, newIndex *model.Index, alterType metastore.AlterType) error {
|
|
|
|
if mc.alterIndexParamsVerification != nil {
|
|
|
|
mc.alterIndexParamsVerification(ctx, oldIndex, newIndex, alterType)
|
|
|
|
}
|
|
|
|
args := mc.Called()
|
|
|
|
err := args.Get(0)
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err.(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mc *MockedCatalog) CreateIndex(ctx context.Context, col *model.Collection, index *model.Index) error {
|
|
|
|
if mc.createIndexParamsVerification != nil {
|
|
|
|
mc.createIndexParamsVerification(ctx, col, index)
|
|
|
|
}
|
|
|
|
args := mc.Called()
|
|
|
|
err := args.Get(0)
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err.(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mc *MockedCatalog) DropIndex(ctx context.Context, collectionInfo *model.Collection,
|
2022-08-11 12:12:38 +08:00
|
|
|
dropIdxID typeutil.UniqueID) error {
|
2022-07-22 10:20:29 +08:00
|
|
|
if mc.dropIndexParamsVerification != nil {
|
2022-08-11 12:12:38 +08:00
|
|
|
mc.dropIndexParamsVerification(ctx, collectionInfo, dropIdxID)
|
2022-07-22 10:20:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
args := mc.Called()
|
|
|
|
err := args.Get(0)
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err.(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMetaTable_ReloadFromKV(t *testing.T) {
|
|
|
|
mc := &MockedCatalog{}
|
|
|
|
|
|
|
|
collectionName := "cn"
|
|
|
|
collInfo := &model.Collection{
|
|
|
|
CollectionID: 1,
|
|
|
|
Name: collectionName,
|
|
|
|
AutoID: false,
|
|
|
|
Fields: []*model.Field{
|
|
|
|
{
|
|
|
|
FieldID: 1,
|
|
|
|
Name: "field110",
|
|
|
|
IsPrimaryKey: false,
|
|
|
|
Description: "",
|
|
|
|
DataType: schemapb.DataType_FloatVector,
|
|
|
|
TypeParams: []*commonpb.KeyValuePair{
|
|
|
|
{
|
|
|
|
Key: "field110-k1",
|
|
|
|
Value: "field110-v1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
IndexParams: []*commonpb.KeyValuePair{
|
|
|
|
{
|
|
|
|
Key: "field110-i1",
|
|
|
|
Value: "field110-v1",
|
|
|
|
},
|
|
|
|
},
|
2022-06-21 18:44:12 +08:00
|
|
|
},
|
2022-07-22 10:20:29 +08:00
|
|
|
},
|
|
|
|
FieldIDToIndexID: []common.Int64Tuple{{Key: 1, Value: 1}},
|
|
|
|
Partitions: []*model.Partition{
|
|
|
|
{
|
|
|
|
PartitionID: 1,
|
|
|
|
PartitionName: Params.CommonCfg.DefaultPartitionName,
|
|
|
|
PartitionCreatedTimestamp: 0,
|
2022-06-21 18:44:12 +08:00
|
|
|
},
|
|
|
|
},
|
2022-07-22 10:20:29 +08:00
|
|
|
Aliases: []string{"a", "b"},
|
|
|
|
}
|
|
|
|
collections := map[string]*model.Collection{collectionName: collInfo}
|
|
|
|
mc.On("ListCollections").Return(collections, nil)
|
|
|
|
|
|
|
|
indexes := []*model.Index{
|
|
|
|
{
|
|
|
|
CollectionID: 1,
|
|
|
|
IndexName: "idx",
|
|
|
|
IndexID: 1,
|
|
|
|
FieldID: 1,
|
|
|
|
IndexParams: []*commonpb.KeyValuePair{
|
|
|
|
{
|
|
|
|
Key: "field110-i1",
|
|
|
|
Value: "field110-v1",
|
2022-06-21 18:44:12 +08:00
|
|
|
},
|
2022-07-22 10:20:29 +08:00
|
|
|
},
|
|
|
|
SegmentIndexes: map[int64]model.SegmentIndex{
|
|
|
|
1: {
|
|
|
|
Segment: model.Segment{
|
|
|
|
SegmentID: 1,
|
|
|
|
PartitionID: 1,
|
2022-06-21 18:44:12 +08:00
|
|
|
},
|
2022-07-22 10:20:29 +08:00
|
|
|
BuildID: 1000,
|
|
|
|
EnableIndex: true,
|
2022-06-21 18:44:12 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-07-22 10:20:29 +08:00
|
|
|
}
|
|
|
|
mc.On("ListIndexes").Return(indexes, nil)
|
|
|
|
|
|
|
|
alias1 := *collInfo
|
|
|
|
alias1.Name = collInfo.Aliases[0]
|
|
|
|
|
|
|
|
alias2 := *collInfo
|
|
|
|
alias2.Name = collInfo.Aliases[1]
|
2022-08-10 10:22:38 +08:00
|
|
|
mc.On("ListAliases").Return([]*model.Alias{
|
|
|
|
{
|
|
|
|
CollectionID: collInfo.CollectionID,
|
|
|
|
Name: collInfo.Aliases[0],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
CollectionID: collInfo.CollectionID,
|
|
|
|
Name: collInfo.Aliases[1],
|
|
|
|
},
|
|
|
|
}, nil)
|
2022-07-22 10:20:29 +08:00
|
|
|
|
|
|
|
mt := &MetaTable{}
|
|
|
|
mt.catalog = mc
|
|
|
|
mt.reloadFromCatalog()
|
|
|
|
|
|
|
|
assert.True(t, len(mt.collID2Meta) == 1)
|
|
|
|
assert.Equal(t, mt.collID2Meta[1], *collInfo)
|
|
|
|
|
|
|
|
assert.True(t, len(mt.collName2ID) == 1)
|
|
|
|
|
|
|
|
assert.True(t, len(mt.collAlias2ID) == 2)
|
|
|
|
ret, ok := mt.collAlias2ID[collInfo.Aliases[0]]
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, int64(1), ret)
|
|
|
|
|
|
|
|
assert.True(t, len(mt.partID2IndexedSegID) == 1)
|
|
|
|
ret2, ok := mt.partID2IndexedSegID[1]
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.True(t, len(ret2) == 1)
|
|
|
|
ret3, ok := ret2[typeutil.UniqueID(1)]
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.True(t, ret3)
|
|
|
|
|
|
|
|
assert.True(t, len(mt.segID2IndexID) == 1)
|
|
|
|
segID, ok := mt.segID2IndexID[1]
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, int64(1), segID)
|
|
|
|
|
|
|
|
assert.True(t, len(mt.indexID2Meta) == 1)
|
|
|
|
meta, ok := mt.indexID2Meta[1]
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, indexes[0], meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMetaTable_AddIndex(t *testing.T) {
|
|
|
|
var (
|
|
|
|
collName = "MarkIndexDeleted-Coll"
|
|
|
|
fieldName = "MarkIndexDeleted-Field"
|
|
|
|
fieldName2 = "MarkIndexDeleted-Field2"
|
|
|
|
indexName = "MarkIndexDeleted-Index"
|
|
|
|
collID = UniqueID(1)
|
|
|
|
fieldID = UniqueID(100)
|
|
|
|
fieldID2 = UniqueID(101)
|
|
|
|
indexID = UniqueID(1000)
|
|
|
|
indexID2 = UniqueID(1001)
|
|
|
|
segID = UniqueID(10000)
|
|
|
|
)
|
|
|
|
|
|
|
|
colMeta := model.Collection{
|
|
|
|
CollectionID: collID,
|
|
|
|
Name: collName,
|
|
|
|
FieldIDToIndexID: []common.Int64Tuple{
|
|
|
|
{
|
|
|
|
Key: fieldID,
|
|
|
|
Value: indexID,
|
|
|
|
},
|
2022-06-21 18:44:12 +08:00
|
|
|
},
|
2022-07-22 10:20:29 +08:00
|
|
|
Fields: []*model.Field{
|
|
|
|
{
|
|
|
|
FieldID: fieldID,
|
|
|
|
Name: fieldName,
|
|
|
|
DataType: schemapb.DataType_FloatVector,
|
|
|
|
IndexParams: []*commonpb.KeyValuePair{{Key: "index_type", Value: DefaultIndexType}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
FieldID: fieldID2,
|
|
|
|
Name: fieldName2,
|
|
|
|
DataType: schemapb.DataType_FloatVector,
|
|
|
|
IndexParams: []*commonpb.KeyValuePair{{Key: "index_type", Value: DefaultIndexType}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("test index already exists", func(t *testing.T) {
|
|
|
|
mt := &MetaTable{
|
|
|
|
collID2Meta: map[typeutil.UniqueID]model.Collection{collID: colMeta},
|
|
|
|
collName2ID: map[string]typeutil.UniqueID{collName: collID},
|
|
|
|
}
|
|
|
|
mt.indexID2Meta = map[typeutil.UniqueID]*model.Index{
|
2022-06-21 18:44:12 +08:00
|
|
|
indexID: {
|
|
|
|
IndexName: indexName,
|
|
|
|
IndexID: indexID,
|
|
|
|
IndexParams: []*commonpb.KeyValuePair{{Key: "index_type", Value: DefaultIndexType}},
|
2022-07-22 10:20:29 +08:00
|
|
|
IsDeleted: false,
|
|
|
|
CreateTime: 0,
|
2022-06-21 18:44:12 +08:00
|
|
|
},
|
2022-07-22 10:20:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
mc := &MockedCatalog{}
|
|
|
|
mc.On("AlterIndex").Return(nil)
|
|
|
|
mc.alterIndexParamsVerification = func(ctx context.Context, oldIndex *model.Index, newIndex *model.Index, alterType metastore.AlterType) {
|
|
|
|
assert.NotNil(t, oldIndex)
|
|
|
|
assert.NotNil(t, newIndex)
|
|
|
|
assert.Equal(t, metastore.ADD, alterType)
|
|
|
|
assert.Equal(t, oldIndex.IndexID, newIndex.IndexID)
|
|
|
|
assert.Equal(t, oldIndex.IsDeleted, newIndex.IsDeleted)
|
|
|
|
assert.Equal(t, oldIndex.IndexParams, newIndex.IndexParams)
|
|
|
|
assert.Equal(t, oldIndex.CreateTime, uint64(0))
|
|
|
|
assert.Equal(t, newIndex.CreateTime, uint64(100))
|
|
|
|
}
|
|
|
|
mt.catalog = mc
|
2022-06-21 18:44:12 +08:00
|
|
|
|
2022-07-22 10:20:29 +08:00
|
|
|
idxInfo := &model.Index{
|
2022-06-21 18:44:12 +08:00
|
|
|
IndexName: indexName,
|
|
|
|
IndexID: indexID,
|
|
|
|
IndexParams: []*commonpb.KeyValuePair{{Key: "index_type", Value: DefaultIndexType}},
|
2022-07-22 10:20:29 +08:00
|
|
|
CreateTime: 100,
|
2022-06-21 18:44:12 +08:00
|
|
|
}
|
2022-07-22 10:20:29 +08:00
|
|
|
|
|
|
|
ret, err := mt.AddIndex(collName, fieldName, idxInfo, []UniqueID{segID})
|
|
|
|
assert.True(t, ret)
|
|
|
|
|
|
|
|
idxMeta, ok := mt.indexID2Meta[indexID]
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, uint64(100), idxMeta.CreateTime)
|
2022-06-21 18:44:12 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
2022-07-22 10:20:29 +08:00
|
|
|
|
|
|
|
t.Run("test add index firstly(create index)", func(t *testing.T) {
|
|
|
|
mt := &MetaTable{
|
|
|
|
collID2Meta: map[typeutil.UniqueID]model.Collection{collID: colMeta},
|
|
|
|
collName2ID: map[string]typeutil.UniqueID{collName: collID},
|
|
|
|
indexID2Meta: make(map[typeutil.UniqueID]*model.Index),
|
|
|
|
}
|
|
|
|
|
|
|
|
idxInfo := &model.Index{
|
|
|
|
FieldID: fieldID2,
|
|
|
|
IndexName: indexName,
|
|
|
|
IndexID: indexID2,
|
|
|
|
IndexParams: []*commonpb.KeyValuePair{{Key: "index_type", Value: DefaultIndexType}},
|
|
|
|
CreateTime: 100,
|
|
|
|
}
|
|
|
|
|
|
|
|
mc := &MockedCatalog{}
|
|
|
|
mc.On("CreateIndex").Return(errors.New("create index fail"))
|
|
|
|
mt.catalog = mc
|
|
|
|
ret, err := mt.AddIndex(collName, fieldName2, idxInfo, []UniqueID{segID})
|
|
|
|
assert.False(t, ret)
|
|
|
|
assert.Error(t, err)
|
|
|
|
_, ok := mt.indexID2Meta[indexID2]
|
|
|
|
assert.False(t, ok)
|
|
|
|
|
|
|
|
mc = &MockedCatalog{}
|
|
|
|
mc.On("CreateIndex").Return(nil)
|
|
|
|
mc.createIndexParamsVerification = func(ctx context.Context, col *model.Collection, index *model.Index) {
|
|
|
|
assert.NotNil(t, col)
|
|
|
|
assert.NotNil(t, index)
|
|
|
|
assert.NotEqual(t, colMeta, col)
|
|
|
|
assert.Equal(t, 2, len(col.FieldIDToIndexID))
|
|
|
|
assert.Equal(t, fieldID, col.FieldIDToIndexID[0].Key)
|
|
|
|
assert.Equal(t, fieldID2, col.FieldIDToIndexID[1].Key)
|
|
|
|
assert.Equal(t, indexID2, index.IndexID)
|
|
|
|
assert.Equal(t, uint64(100), index.CreateTime)
|
|
|
|
assert.Equal(t, 1, len(index.SegmentIndexes))
|
|
|
|
assert.Equal(t, segID, index.SegmentIndexes[segID].SegmentID)
|
|
|
|
assert.Equal(t, false, index.SegmentIndexes[segID].EnableIndex)
|
|
|
|
}
|
|
|
|
|
|
|
|
mt.catalog = mc
|
|
|
|
ret, err = mt.AddIndex(collName, fieldName2, idxInfo, []UniqueID{segID})
|
|
|
|
assert.False(t, ret)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
idxMeta, ok := mt.indexID2Meta[indexID2]
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, uint64(100), idxMeta.CreateTime)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
newColMeta, ok := mt.collID2Meta[collID]
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, collID, newColMeta.CollectionID)
|
|
|
|
assert.Equal(t, 2, len(newColMeta.FieldIDToIndexID))
|
|
|
|
assert.Equal(t, fieldID, newColMeta.FieldIDToIndexID[0].Key)
|
|
|
|
assert.Equal(t, indexID, newColMeta.FieldIDToIndexID[0].Value)
|
|
|
|
assert.Equal(t, fieldID2, newColMeta.FieldIDToIndexID[1].Key)
|
|
|
|
assert.Equal(t, indexID2, newColMeta.FieldIDToIndexID[1].Value)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMetaTable_RecycleDroppedIndex(t *testing.T) {
|
|
|
|
colName := "c"
|
|
|
|
fieldName := "f1"
|
|
|
|
indexName1 := "idx1"
|
|
|
|
indexName2 := "idx2"
|
|
|
|
|
|
|
|
colMeta := model.Collection{
|
|
|
|
CollectionID: 1,
|
|
|
|
Name: colName,
|
|
|
|
FieldIDToIndexID: []common.Int64Tuple{
|
|
|
|
{
|
|
|
|
Key: 1,
|
|
|
|
Value: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: 1,
|
|
|
|
Value: 2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Fields: []*model.Field{
|
|
|
|
{
|
|
|
|
FieldID: 1,
|
|
|
|
Name: fieldName,
|
|
|
|
DataType: schemapb.DataType_FloatVector,
|
|
|
|
IndexParams: []*commonpb.KeyValuePair{{Key: "index_type", Value: DefaultIndexType}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Partitions: []*model.Partition{
|
|
|
|
{
|
|
|
|
PartitionID: 1,
|
|
|
|
PartitionName: "p",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
mt := &MetaTable{
|
|
|
|
collID2Meta: map[typeutil.UniqueID]model.Collection{1: colMeta},
|
|
|
|
collName2ID: map[string]typeutil.UniqueID{colName: 1},
|
|
|
|
segID2IndexID: map[typeutil.UniqueID]typeutil.UniqueID{
|
|
|
|
1: 1,
|
|
|
|
},
|
|
|
|
partID2IndexedSegID: map[typeutil.UniqueID]map[typeutil.UniqueID]bool{
|
|
|
|
1: {
|
|
|
|
1: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
mt.indexID2Meta = map[typeutil.UniqueID]*model.Index{
|
|
|
|
1: {
|
|
|
|
IndexName: indexName1,
|
|
|
|
IndexID: 1,
|
|
|
|
IndexParams: []*commonpb.KeyValuePair{{Key: "index_type", Value: DefaultIndexType}},
|
|
|
|
IsDeleted: true,
|
|
|
|
SegmentIndexes: map[int64]model.SegmentIndex{
|
|
|
|
1: {
|
|
|
|
Segment: model.Segment{
|
|
|
|
SegmentID: 1,
|
|
|
|
PartitionID: 1,
|
|
|
|
},
|
|
|
|
BuildID: 1000,
|
|
|
|
EnableIndex: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
2: {
|
|
|
|
IndexName: indexName2,
|
|
|
|
IndexID: 2,
|
|
|
|
IndexParams: []*commonpb.KeyValuePair{{Key: "index_type", Value: DefaultIndexType}},
|
|
|
|
IsDeleted: false,
|
|
|
|
SegmentIndexes: map[int64]model.SegmentIndex{
|
|
|
|
1: {
|
|
|
|
Segment: model.Segment{
|
|
|
|
SegmentID: 1,
|
|
|
|
PartitionID: 1,
|
|
|
|
},
|
|
|
|
BuildID: 1000,
|
|
|
|
EnableIndex: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
mc := &MockedCatalog{}
|
|
|
|
mc.On("DropIndex").Return(nil)
|
2022-08-11 12:12:38 +08:00
|
|
|
mc.dropIndexParamsVerification = func(ctx context.Context, collectionInfo *model.Collection, dropIdxID typeutil.UniqueID) {
|
2022-07-22 10:20:29 +08:00
|
|
|
assert.NotNil(t, collectionInfo)
|
|
|
|
assert.Equal(t, int64(1), dropIdxID)
|
|
|
|
assert.Equal(t, int64(1), collectionInfo.CollectionID)
|
|
|
|
assert.Equal(t, 1, len(collectionInfo.FieldIDToIndexID))
|
|
|
|
assert.Equal(t, int64(1), collectionInfo.FieldIDToIndexID[0].Key)
|
|
|
|
assert.Equal(t, int64(2), collectionInfo.FieldIDToIndexID[0].Value)
|
|
|
|
}
|
|
|
|
mt.catalog = mc
|
|
|
|
|
|
|
|
mt.RecycleDroppedIndex()
|
|
|
|
newColMeta, ok := mt.collID2Meta[1]
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, int64(1), newColMeta.CollectionID)
|
|
|
|
assert.Equal(t, 1, len(newColMeta.FieldIDToIndexID))
|
|
|
|
assert.Equal(t, int64(1), newColMeta.FieldIDToIndexID[0].Key)
|
|
|
|
assert.Equal(t, int64(2), newColMeta.FieldIDToIndexID[0].Value)
|
|
|
|
|
|
|
|
assert.Equal(t, 1, len(mt.indexID2Meta))
|
|
|
|
idxMeta, ok := mt.indexID2Meta[2]
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, false, idxMeta.IsDeleted)
|
|
|
|
|
|
|
|
assert.Equal(t, 0, len(mt.segID2IndexID))
|
|
|
|
assert.Equal(t, 1, len(mt.partID2IndexedSegID))
|
2022-06-21 18:44:12 +08:00
|
|
|
}
|