2021-10-25 19:44:37 +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:35:38 +08:00
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
2021-10-25 19:44:37 +08:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2021-04-19 11:35:38 +08:00
|
|
|
//
|
2021-10-25 19:44:37 +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-22 10:42:07 +08:00
|
|
|
package datacoord
|
2021-01-22 19:43:27 +08:00
|
|
|
|
|
|
|
import (
|
2021-03-23 16:57:59 +08:00
|
|
|
"context"
|
2021-09-07 13:59:58 +08:00
|
|
|
"math"
|
|
|
|
"sync"
|
2021-01-22 19:43:27 +08:00
|
|
|
"testing"
|
2021-08-19 14:08:10 +08:00
|
|
|
"time"
|
2021-01-22 19:43:27 +08:00
|
|
|
|
2023-07-07 19:38:26 +08:00
|
|
|
"github.com/cockroachdb/errors"
|
2022-12-20 14:09:25 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-07-07 19:38:26 +08:00
|
|
|
"github.com/stretchr/testify/mock"
|
2022-12-20 14:09:25 +08:00
|
|
|
|
2023-06-09 01:28:37 +08:00
|
|
|
"github.com/milvus-io/milvus-proto/go-api/v2/commonpb"
|
|
|
|
"github.com/milvus-io/milvus-proto/go-api/v2/schemapb"
|
2023-07-24 14:11:07 +08:00
|
|
|
etcdkv "github.com/milvus-io/milvus/internal/kv/etcd"
|
2023-07-07 19:38:26 +08:00
|
|
|
mockkv "github.com/milvus-io/milvus/internal/kv/mocks"
|
2022-08-20 10:24:51 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/metastore/kv/datacoord"
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/datapb"
|
2023-07-24 14:11:07 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/etcd"
|
2023-04-06 19:14:32 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/metautil"
|
2023-09-05 10:31:48 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/paramtable"
|
2021-01-22 19:43:27 +08:00
|
|
|
)
|
|
|
|
|
2021-09-07 13:59:58 +08:00
|
|
|
func TestManagerOptions(t *testing.T) {
|
|
|
|
// ctx := context.Background()
|
2023-09-05 10:31:48 +08:00
|
|
|
paramtable.Init()
|
2021-09-07 13:59:58 +08:00
|
|
|
mockAllocator := newMockAllocator()
|
2022-11-03 14:41:35 +08:00
|
|
|
meta, err := newMemoryMeta()
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2023-07-24 14:11:07 +08:00
|
|
|
segmentManager, _ := newSegmentManager(meta, mockAllocator)
|
2021-09-07 13:59:58 +08:00
|
|
|
t.Run("test with alloc helper", func(t *testing.T) {
|
|
|
|
opt := withAllocHelper(allocHelper{})
|
|
|
|
opt.apply(segmentManager)
|
|
|
|
|
|
|
|
assert.True(t, segmentManager.helper.afterCreateSegment == nil)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("test withCalUpperLimitPolicy", func(t *testing.T) {
|
|
|
|
opt := withCalUpperLimitPolicy(defaultCalUpperLimitPolicy())
|
|
|
|
assert.NotNil(t, opt)
|
|
|
|
|
2023-09-21 09:45:27 +08:00
|
|
|
// manual set nil``
|
2021-09-07 13:59:58 +08:00
|
|
|
segmentManager.estimatePolicy = nil
|
|
|
|
opt.apply(segmentManager)
|
|
|
|
assert.True(t, segmentManager.estimatePolicy != nil)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("test withAllocPolicy", func(t *testing.T) {
|
2021-12-17 22:34:42 +08:00
|
|
|
opt := withAllocPolicy(defaultAllocatePolicy())
|
2021-09-07 13:59:58 +08:00
|
|
|
assert.NotNil(t, opt)
|
|
|
|
// manual set nil
|
|
|
|
segmentManager.allocPolicy = nil
|
|
|
|
opt.apply(segmentManager)
|
|
|
|
assert.True(t, segmentManager.allocPolicy != nil)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("test withSegmentSealPolicy", func(t *testing.T) {
|
|
|
|
opt := withSegmentSealPolices(defaultSegmentSealPolicy()...)
|
|
|
|
assert.NotNil(t, opt)
|
|
|
|
// manual set nil
|
|
|
|
segmentManager.segmentSealPolicies = []segmentSealPolicy{}
|
|
|
|
opt.apply(segmentManager)
|
|
|
|
assert.True(t, len(segmentManager.segmentSealPolicies) > 0)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("test withChannelSealPolicies", func(t *testing.T) {
|
|
|
|
opt := withChannelSealPolices(getChannelOpenSegCapacityPolicy(1000))
|
|
|
|
assert.NotNil(t, opt)
|
2023-03-03 14:13:49 +08:00
|
|
|
// manual set nil
|
2021-09-07 13:59:58 +08:00
|
|
|
segmentManager.channelSealPolicies = []channelSealPolicy{}
|
|
|
|
opt.apply(segmentManager)
|
|
|
|
assert.True(t, len(segmentManager.channelSealPolicies) > 0)
|
|
|
|
})
|
|
|
|
t.Run("test withFlushPolicy", func(t *testing.T) {
|
|
|
|
opt := withFlushPolicy(defaultFlushPolicy())
|
|
|
|
assert.NotNil(t, opt)
|
|
|
|
// manual set nil
|
|
|
|
segmentManager.flushPolicy = nil
|
|
|
|
opt.apply(segmentManager)
|
|
|
|
assert.True(t, segmentManager.flushPolicy != nil)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-01-22 19:43:27 +08:00
|
|
|
func TestAllocSegment(t *testing.T) {
|
2021-03-23 16:57:59 +08:00
|
|
|
ctx := context.Background()
|
2023-09-05 10:31:48 +08:00
|
|
|
paramtable.Init()
|
2023-07-24 14:11:07 +08:00
|
|
|
Params.Save(Params.DataCoordCfg.AllocLatestExpireAttempt.Key, "1")
|
2021-01-22 19:43:27 +08:00
|
|
|
mockAllocator := newMockAllocator()
|
2022-11-03 14:41:35 +08:00
|
|
|
meta, err := newMemoryMeta()
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2023-07-24 14:11:07 +08:00
|
|
|
segmentManager, _ := newSegmentManager(meta, mockAllocator)
|
2021-01-22 19:43:27 +08:00
|
|
|
|
2021-01-23 14:41:29 +08:00
|
|
|
schema := newTestSchema()
|
2021-08-23 17:59:51 +08:00
|
|
|
collID, err := mockAllocator.allocID(ctx)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2022-10-10 20:31:22 +08:00
|
|
|
meta.AddCollection(&collectionInfo{ID: collID, Schema: schema})
|
2021-07-23 21:58:33 +08:00
|
|
|
|
|
|
|
t.Run("normal allocation", func(t *testing.T) {
|
|
|
|
allocations, err := segmentManager.AllocSegment(ctx, collID, 100, "c1", 100)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-07-23 21:58:33 +08:00
|
|
|
assert.EqualValues(t, 1, len(allocations))
|
|
|
|
assert.EqualValues(t, 100, allocations[0].NumOfRows)
|
|
|
|
assert.NotEqualValues(t, 0, allocations[0].SegmentID)
|
|
|
|
assert.NotEqualValues(t, 0, allocations[0].ExpireTime)
|
|
|
|
})
|
2021-09-07 13:59:58 +08:00
|
|
|
|
2022-06-07 16:56:07 +08:00
|
|
|
t.Run("allocation fails 1", func(t *testing.T) {
|
|
|
|
failsAllocator := &FailsAllocator{
|
|
|
|
allocTsSucceed: true,
|
2023-07-24 14:11:07 +08:00
|
|
|
allocIDSucceed: false,
|
2022-06-07 16:56:07 +08:00
|
|
|
}
|
2023-07-24 14:11:07 +08:00
|
|
|
segmentManager, err := newSegmentManager(meta, failsAllocator)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = segmentManager.AllocSegment(ctx, collID, 100, "c2", 100)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.Error(t, err)
|
2022-06-07 16:56:07 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("allocation fails 2", func(t *testing.T) {
|
|
|
|
failsAllocator := &FailsAllocator{
|
2023-07-24 14:11:07 +08:00
|
|
|
allocTsSucceed: false,
|
2022-06-07 16:56:07 +08:00
|
|
|
allocIDSucceed: true,
|
|
|
|
}
|
2023-07-24 14:11:07 +08:00
|
|
|
segmentManager, err := newSegmentManager(meta, failsAllocator)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.Error(t, err)
|
2023-07-24 14:11:07 +08:00
|
|
|
assert.Nil(t, segmentManager)
|
2022-06-07 16:56:07 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-07-24 14:11:07 +08:00
|
|
|
func TestLastExpireReset(t *testing.T) {
|
2023-09-21 09:45:27 +08:00
|
|
|
// set up meta on dc
|
2023-07-24 14:11:07 +08:00
|
|
|
ctx := context.Background()
|
2023-09-05 10:31:48 +08:00
|
|
|
paramtable.Init()
|
2023-07-24 14:11:07 +08:00
|
|
|
Params.Save(Params.DataCoordCfg.AllocLatestExpireAttempt.Key, "1")
|
|
|
|
Params.Save(Params.DataCoordCfg.SegmentMaxSize.Key, "1")
|
2023-09-26 09:57:25 +08:00
|
|
|
mockAllocator := newRootCoordAllocator(newMockRootCoordClient())
|
2023-07-24 14:11:07 +08:00
|
|
|
etcdCli, _ := etcd.GetEtcdClient(
|
|
|
|
Params.EtcdCfg.UseEmbedEtcd.GetAsBool(),
|
|
|
|
Params.EtcdCfg.EtcdUseSSL.GetAsBool(),
|
|
|
|
Params.EtcdCfg.Endpoints.GetAsStrings(),
|
|
|
|
Params.EtcdCfg.EtcdTLSCert.GetValue(),
|
|
|
|
Params.EtcdCfg.EtcdTLSKey.GetValue(),
|
|
|
|
Params.EtcdCfg.EtcdTLSCACert.GetValue(),
|
|
|
|
Params.EtcdCfg.EtcdTLSMinVersion.GetValue())
|
|
|
|
rootPath := "/test/segment/last/expire"
|
|
|
|
metaKV := etcdkv.NewEtcdKV(etcdCli, rootPath)
|
|
|
|
metaKV.RemoveWithPrefix("")
|
|
|
|
catalog := datacoord.NewCatalog(metaKV, "", "")
|
|
|
|
meta, err := newMeta(context.TODO(), catalog, nil)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
// add collection
|
|
|
|
channelName := "c1"
|
|
|
|
schema := newTestSchema()
|
|
|
|
collID, err := mockAllocator.allocID(ctx)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
meta.AddCollection(&collectionInfo{ID: collID, Schema: schema})
|
2023-09-18 09:53:22 +08:00
|
|
|
initSegment := &SegmentInfo{
|
|
|
|
SegmentInfo: &datapb.SegmentInfo{
|
|
|
|
ID: 1,
|
|
|
|
InsertChannel: "ch1",
|
|
|
|
State: commonpb.SegmentState_Growing,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
meta.AddSegment(context.TODO(), initSegment)
|
2023-07-24 14:11:07 +08:00
|
|
|
|
2023-09-21 09:45:27 +08:00
|
|
|
// assign segments, set max segment to only 1MB, equalling to 10485 rows
|
2023-07-24 14:11:07 +08:00
|
|
|
var bigRows, smallRows int64 = 10000, 1000
|
|
|
|
segmentManager, _ := newSegmentManager(meta, mockAllocator)
|
2023-09-18 09:53:22 +08:00
|
|
|
initSegment.SegmentInfo.State = commonpb.SegmentState_Dropped
|
|
|
|
meta.segments.SetSegment(1, initSegment)
|
2023-07-24 14:11:07 +08:00
|
|
|
allocs, _ := segmentManager.AllocSegment(context.Background(), collID, 0, channelName, bigRows)
|
|
|
|
segmentID1, expire1 := allocs[0].SegmentID, allocs[0].ExpireTime
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
allocs, _ = segmentManager.AllocSegment(context.Background(), collID, 0, channelName, bigRows)
|
|
|
|
segmentID2, expire2 := allocs[0].SegmentID, allocs[0].ExpireTime
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
allocs, _ = segmentManager.AllocSegment(context.Background(), collID, 0, channelName, smallRows)
|
|
|
|
segmentID3, expire3 := allocs[0].SegmentID, allocs[0].ExpireTime
|
|
|
|
|
2023-09-21 09:45:27 +08:00
|
|
|
// simulate handleTimeTick op on dataCoord
|
2023-07-24 14:11:07 +08:00
|
|
|
meta.SetCurrentRows(segmentID1, bigRows)
|
|
|
|
meta.SetCurrentRows(segmentID2, bigRows)
|
|
|
|
meta.SetCurrentRows(segmentID3, smallRows)
|
|
|
|
segmentManager.tryToSealSegment(expire1, channelName)
|
|
|
|
assert.Equal(t, commonpb.SegmentState_Sealed, meta.GetSegment(segmentID1).GetState())
|
|
|
|
assert.Equal(t, commonpb.SegmentState_Sealed, meta.GetSegment(segmentID2).GetState())
|
|
|
|
assert.Equal(t, commonpb.SegmentState_Growing, meta.GetSegment(segmentID3).GetState())
|
|
|
|
|
2023-09-21 09:45:27 +08:00
|
|
|
// pretend that dataCoord break down
|
2023-07-24 14:11:07 +08:00
|
|
|
metaKV.Close()
|
|
|
|
etcdCli.Close()
|
|
|
|
|
2023-09-21 09:45:27 +08:00
|
|
|
// dataCoord restart
|
2023-07-24 14:11:07 +08:00
|
|
|
newEtcdCli, _ := etcd.GetEtcdClient(Params.EtcdCfg.UseEmbedEtcd.GetAsBool(), Params.EtcdCfg.EtcdUseSSL.GetAsBool(),
|
|
|
|
Params.EtcdCfg.Endpoints.GetAsStrings(), Params.EtcdCfg.EtcdTLSCert.GetValue(),
|
|
|
|
Params.EtcdCfg.EtcdTLSKey.GetValue(), Params.EtcdCfg.EtcdTLSCACert.GetValue(), Params.EtcdCfg.EtcdTLSMinVersion.GetValue())
|
|
|
|
newMetaKV := etcdkv.NewEtcdKV(newEtcdCli, rootPath)
|
|
|
|
defer newMetaKV.RemoveWithPrefix("")
|
|
|
|
newCatalog := datacoord.NewCatalog(newMetaKV, "", "")
|
|
|
|
restartedMeta, err := newMeta(context.TODO(), newCatalog, nil)
|
|
|
|
restartedMeta.AddCollection(&collectionInfo{ID: collID, Schema: schema})
|
|
|
|
assert.Nil(t, err)
|
|
|
|
newSegmentManager, _ := newSegmentManager(restartedMeta, mockAllocator)
|
2023-09-21 09:45:27 +08:00
|
|
|
// reset row number to avoid being cleaned by empty segment
|
2023-07-24 14:11:07 +08:00
|
|
|
restartedMeta.SetCurrentRows(segmentID1, bigRows)
|
|
|
|
restartedMeta.SetCurrentRows(segmentID2, bigRows)
|
|
|
|
restartedMeta.SetCurrentRows(segmentID3, smallRows)
|
|
|
|
|
2023-09-21 09:45:27 +08:00
|
|
|
// verify lastExpire of growing and sealed segments
|
2023-07-24 14:11:07 +08:00
|
|
|
segment1, segment2, segment3 := restartedMeta.GetSegment(segmentID1), restartedMeta.GetSegment(segmentID2), restartedMeta.GetSegment(segmentID3)
|
2023-09-21 09:45:27 +08:00
|
|
|
// segmentState should not be altered but growing segment's lastExpire has been reset to the latest
|
2023-07-24 14:11:07 +08:00
|
|
|
assert.Equal(t, commonpb.SegmentState_Sealed, segment1.GetState())
|
|
|
|
assert.Equal(t, commonpb.SegmentState_Sealed, segment2.GetState())
|
|
|
|
assert.Equal(t, commonpb.SegmentState_Growing, segment3.GetState())
|
|
|
|
assert.Equal(t, expire1, segment1.GetLastExpireTime())
|
|
|
|
assert.Equal(t, expire2, segment2.GetLastExpireTime())
|
|
|
|
assert.True(t, segment3.GetLastExpireTime() > expire3)
|
|
|
|
flushableSegIds, _ := newSegmentManager.GetFlushableSegments(context.Background(), channelName, expire3)
|
|
|
|
assert.ElementsMatch(t, []UniqueID{segmentID1, segmentID2}, flushableSegIds) // segment1 and segment2 can be flushed
|
|
|
|
newAlloc, err := newSegmentManager.AllocSegment(context.Background(), collID, 0, channelName, 2000)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, segmentID3, newAlloc[0].SegmentID) // segment3 still can be used to allocate
|
|
|
|
}
|
|
|
|
|
2022-06-07 16:56:07 +08:00
|
|
|
func TestAllocSegmentForImport(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2023-09-05 10:31:48 +08:00
|
|
|
paramtable.Init()
|
2022-06-07 16:56:07 +08:00
|
|
|
mockAllocator := newMockAllocator()
|
2022-11-03 14:41:35 +08:00
|
|
|
meta, err := newMemoryMeta()
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2023-07-24 14:11:07 +08:00
|
|
|
segmentManager, _ := newSegmentManager(meta, mockAllocator)
|
2022-06-07 16:56:07 +08:00
|
|
|
|
|
|
|
schema := newTestSchema()
|
|
|
|
collID, err := mockAllocator.allocID(ctx)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2022-10-10 20:31:22 +08:00
|
|
|
meta.AddCollection(&collectionInfo{ID: collID, Schema: schema})
|
2022-06-07 16:56:07 +08:00
|
|
|
|
|
|
|
t.Run("normal allocation", func(t *testing.T) {
|
2022-06-27 13:56:17 +08:00
|
|
|
allocation, err := segmentManager.allocSegmentForImport(ctx, collID, 100, "c1", 100, 0)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2022-06-07 16:56:07 +08:00
|
|
|
assert.NotNil(t, allocation)
|
|
|
|
assert.EqualValues(t, 100, allocation.NumOfRows)
|
|
|
|
assert.NotEqualValues(t, 0, allocation.SegmentID)
|
|
|
|
assert.NotEqualValues(t, 0, allocation.ExpireTime)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("allocation fails 1", func(t *testing.T) {
|
|
|
|
failsAllocator := &FailsAllocator{
|
|
|
|
allocTsSucceed: true,
|
|
|
|
}
|
2023-07-24 14:11:07 +08:00
|
|
|
segmentManager, _ := newSegmentManager(meta, failsAllocator)
|
2022-06-27 13:56:17 +08:00
|
|
|
_, err := segmentManager.allocSegmentForImport(ctx, collID, 100, "c1", 100, 0)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.Error(t, err)
|
2022-06-07 16:56:07 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("allocation fails 2", func(t *testing.T) {
|
|
|
|
failsAllocator := &FailsAllocator{
|
|
|
|
allocIDSucceed: true,
|
|
|
|
}
|
2023-07-24 14:11:07 +08:00
|
|
|
segmentManager, _ := newSegmentManager(meta, failsAllocator)
|
2022-06-27 13:56:17 +08:00
|
|
|
_, err := segmentManager.allocSegmentForImport(ctx, collID, 100, "c1", 100, 0)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.Error(t, err)
|
2021-09-07 13:59:58 +08:00
|
|
|
})
|
2021-01-22 19:43:27 +08:00
|
|
|
}
|
|
|
|
|
2021-05-21 18:30:41 +08:00
|
|
|
func TestLoadSegmentsFromMeta(t *testing.T) {
|
2021-08-23 17:59:51 +08:00
|
|
|
ctx := context.Background()
|
2023-09-05 10:31:48 +08:00
|
|
|
paramtable.Init()
|
2021-01-22 19:43:27 +08:00
|
|
|
mockAllocator := newMockAllocator()
|
2022-11-03 14:41:35 +08:00
|
|
|
meta, err := newMemoryMeta()
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-01-22 19:43:27 +08:00
|
|
|
|
2021-01-23 14:41:29 +08:00
|
|
|
schema := newTestSchema()
|
2021-08-23 17:59:51 +08:00
|
|
|
collID, err := mockAllocator.allocID(ctx)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2022-10-10 20:31:22 +08:00
|
|
|
meta.AddCollection(&collectionInfo{ID: collID, Schema: schema})
|
2021-01-22 19:43:27 +08:00
|
|
|
|
2021-05-21 18:30:41 +08:00
|
|
|
sealedSegment := &datapb.SegmentInfo{
|
|
|
|
ID: 1,
|
|
|
|
CollectionID: collID,
|
|
|
|
PartitionID: 0,
|
|
|
|
InsertChannel: "",
|
|
|
|
State: commonpb.SegmentState_Sealed,
|
|
|
|
MaxRowNum: 100,
|
|
|
|
LastExpireTime: 1000,
|
|
|
|
}
|
|
|
|
growingSegment := &datapb.SegmentInfo{
|
|
|
|
ID: 2,
|
|
|
|
CollectionID: collID,
|
|
|
|
PartitionID: 0,
|
|
|
|
InsertChannel: "",
|
|
|
|
State: commonpb.SegmentState_Growing,
|
|
|
|
MaxRowNum: 100,
|
|
|
|
LastExpireTime: 1000,
|
|
|
|
}
|
|
|
|
flushedSegment := &datapb.SegmentInfo{
|
|
|
|
ID: 3,
|
|
|
|
CollectionID: collID,
|
|
|
|
PartitionID: 0,
|
|
|
|
InsertChannel: "",
|
|
|
|
State: commonpb.SegmentState_Flushed,
|
|
|
|
MaxRowNum: 100,
|
|
|
|
LastExpireTime: 1000,
|
|
|
|
}
|
2023-09-18 09:53:22 +08:00
|
|
|
err = meta.AddSegment(context.TODO(), NewSegmentInfo(sealedSegment))
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2023-09-18 09:53:22 +08:00
|
|
|
err = meta.AddSegment(context.TODO(), NewSegmentInfo(growingSegment))
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2023-09-18 09:53:22 +08:00
|
|
|
err = meta.AddSegment(context.TODO(), NewSegmentInfo(flushedSegment))
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-02-02 18:53:10 +08:00
|
|
|
|
2023-07-24 14:11:07 +08:00
|
|
|
segmentManager, _ := newSegmentManager(meta, mockAllocator)
|
2021-07-12 17:24:25 +08:00
|
|
|
segments := segmentManager.segments
|
2021-05-21 18:30:41 +08:00
|
|
|
assert.EqualValues(t, 2, len(segments))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSaveSegmentsToMeta(t *testing.T) {
|
2023-09-05 10:31:48 +08:00
|
|
|
paramtable.Init()
|
2021-05-21 18:30:41 +08:00
|
|
|
mockAllocator := newMockAllocator()
|
2022-11-03 14:41:35 +08:00
|
|
|
meta, err := newMemoryMeta()
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-05-21 18:30:41 +08:00
|
|
|
|
|
|
|
schema := newTestSchema()
|
2021-08-23 17:59:51 +08:00
|
|
|
collID, err := mockAllocator.allocID(context.Background())
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2022-10-10 20:31:22 +08:00
|
|
|
meta.AddCollection(&collectionInfo{ID: collID, Schema: schema})
|
2023-07-24 14:11:07 +08:00
|
|
|
segmentManager, _ := newSegmentManager(meta, mockAllocator)
|
2021-07-23 21:58:33 +08:00
|
|
|
allocations, err := segmentManager.AllocSegment(context.Background(), collID, 0, "c1", 1000)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-07-23 21:58:33 +08:00
|
|
|
assert.EqualValues(t, 1, len(allocations))
|
2023-01-31 12:41:53 +08:00
|
|
|
_, err = segmentManager.SealAllSegments(context.Background(), collID, nil, false)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2023-03-03 14:13:49 +08:00
|
|
|
segment := meta.GetHealthySegment(allocations[0].SegmentID)
|
2022-04-25 11:07:47 +08:00
|
|
|
assert.NotNil(t, segment)
|
|
|
|
assert.EqualValues(t, segment.LastExpireTime, allocations[0].ExpireTime)
|
|
|
|
assert.EqualValues(t, commonpb.SegmentState_Sealed, segment.State)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSaveSegmentsToMetaWithSpecificSegments(t *testing.T) {
|
2023-09-05 10:31:48 +08:00
|
|
|
paramtable.Init()
|
2022-04-25 11:07:47 +08:00
|
|
|
mockAllocator := newMockAllocator()
|
2022-11-03 14:41:35 +08:00
|
|
|
meta, err := newMemoryMeta()
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2022-04-25 11:07:47 +08:00
|
|
|
|
|
|
|
schema := newTestSchema()
|
|
|
|
collID, err := mockAllocator.allocID(context.Background())
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2022-10-10 20:31:22 +08:00
|
|
|
meta.AddCollection(&collectionInfo{ID: collID, Schema: schema})
|
2023-07-24 14:11:07 +08:00
|
|
|
segmentManager, _ := newSegmentManager(meta, mockAllocator)
|
2022-04-25 11:07:47 +08:00
|
|
|
allocations, err := segmentManager.AllocSegment(context.Background(), collID, 0, "c1", 1000)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2022-04-25 11:07:47 +08:00
|
|
|
assert.EqualValues(t, 1, len(allocations))
|
2023-01-31 12:41:53 +08:00
|
|
|
_, err = segmentManager.SealAllSegments(context.Background(), collID, []int64{allocations[0].SegmentID}, false)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2023-03-03 14:13:49 +08:00
|
|
|
segment := meta.GetHealthySegment(allocations[0].SegmentID)
|
2021-07-07 14:02:01 +08:00
|
|
|
assert.NotNil(t, segment)
|
2021-07-23 21:58:33 +08:00
|
|
|
assert.EqualValues(t, segment.LastExpireTime, allocations[0].ExpireTime)
|
2021-05-21 18:30:41 +08:00
|
|
|
assert.EqualValues(t, commonpb.SegmentState_Sealed, segment.State)
|
2021-01-22 19:43:27 +08:00
|
|
|
}
|
2021-07-12 17:24:25 +08:00
|
|
|
|
|
|
|
func TestDropSegment(t *testing.T) {
|
2023-09-05 10:31:48 +08:00
|
|
|
paramtable.Init()
|
2021-07-12 17:24:25 +08:00
|
|
|
mockAllocator := newMockAllocator()
|
2022-11-03 14:41:35 +08:00
|
|
|
meta, err := newMemoryMeta()
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-07-12 17:24:25 +08:00
|
|
|
|
|
|
|
schema := newTestSchema()
|
2021-08-23 17:59:51 +08:00
|
|
|
collID, err := mockAllocator.allocID(context.Background())
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2022-10-10 20:31:22 +08:00
|
|
|
meta.AddCollection(&collectionInfo{ID: collID, Schema: schema})
|
2023-07-24 14:11:07 +08:00
|
|
|
segmentManager, _ := newSegmentManager(meta, mockAllocator)
|
2021-07-23 21:58:33 +08:00
|
|
|
allocations, err := segmentManager.AllocSegment(context.Background(), collID, 0, "c1", 1000)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-07-23 21:58:33 +08:00
|
|
|
assert.EqualValues(t, 1, len(allocations))
|
|
|
|
segID := allocations[0].SegmentID
|
2023-03-03 14:13:49 +08:00
|
|
|
segment := meta.GetHealthySegment(segID)
|
2021-07-12 17:24:25 +08:00
|
|
|
assert.NotNil(t, segment)
|
|
|
|
|
|
|
|
segmentManager.DropSegment(context.Background(), segID)
|
2023-03-03 14:13:49 +08:00
|
|
|
segment = meta.GetHealthySegment(segID)
|
2021-07-12 17:24:25 +08:00
|
|
|
assert.NotNil(t, segment)
|
|
|
|
}
|
2021-07-23 21:58:33 +08:00
|
|
|
|
|
|
|
func TestAllocRowsLargerThanOneSegment(t *testing.T) {
|
2023-09-05 10:31:48 +08:00
|
|
|
paramtable.Init()
|
2021-07-23 21:58:33 +08:00
|
|
|
mockAllocator := newMockAllocator()
|
2022-11-03 14:41:35 +08:00
|
|
|
meta, err := newMemoryMeta()
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-07-23 21:58:33 +08:00
|
|
|
|
|
|
|
schema := newTestSchema()
|
2021-08-23 17:59:51 +08:00
|
|
|
collID, err := mockAllocator.allocID(context.Background())
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2022-10-10 20:31:22 +08:00
|
|
|
meta.AddCollection(&collectionInfo{ID: collID, Schema: schema})
|
2021-07-23 21:58:33 +08:00
|
|
|
|
2023-09-21 09:45:27 +08:00
|
|
|
mockPolicy := func(schema *schemapb.CollectionSchema) (int, error) {
|
2021-07-23 21:58:33 +08:00
|
|
|
return 1, nil
|
|
|
|
}
|
2023-07-24 14:11:07 +08:00
|
|
|
segmentManager, _ := newSegmentManager(meta, mockAllocator, withCalUpperLimitPolicy(mockPolicy))
|
2021-07-23 21:58:33 +08:00
|
|
|
allocations, err := segmentManager.AllocSegment(context.TODO(), collID, 0, "c1", 2)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-07-23 21:58:33 +08:00
|
|
|
assert.EqualValues(t, 2, len(allocations))
|
|
|
|
assert.EqualValues(t, 1, allocations[0].NumOfRows)
|
|
|
|
assert.EqualValues(t, 1, allocations[1].NumOfRows)
|
|
|
|
}
|
2021-08-12 10:48:08 +08:00
|
|
|
|
|
|
|
func TestExpireAllocation(t *testing.T) {
|
2023-09-05 10:31:48 +08:00
|
|
|
paramtable.Init()
|
2021-08-12 10:48:08 +08:00
|
|
|
mockAllocator := newMockAllocator()
|
2022-11-03 14:41:35 +08:00
|
|
|
meta, err := newMemoryMeta()
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-08-12 10:48:08 +08:00
|
|
|
|
|
|
|
schema := newTestSchema()
|
2021-08-23 17:59:51 +08:00
|
|
|
collID, err := mockAllocator.allocID(context.Background())
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2022-10-10 20:31:22 +08:00
|
|
|
meta.AddCollection(&collectionInfo{ID: collID, Schema: schema})
|
2021-08-12 10:48:08 +08:00
|
|
|
|
2023-09-21 09:45:27 +08:00
|
|
|
mockPolicy := func(schema *schemapb.CollectionSchema) (int, error) {
|
2021-08-12 10:48:08 +08:00
|
|
|
return 10000000, nil
|
|
|
|
}
|
2023-07-24 14:11:07 +08:00
|
|
|
segmentManager, _ := newSegmentManager(meta, mockAllocator, withCalUpperLimitPolicy(mockPolicy))
|
2021-08-12 10:48:08 +08:00
|
|
|
// alloc 100 times and expire
|
|
|
|
var maxts Timestamp
|
|
|
|
var id int64 = -1
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
allocs, err := segmentManager.AllocSegment(context.TODO(), collID, 0, "ch1", 100)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-08-12 10:48:08 +08:00
|
|
|
assert.EqualValues(t, 1, len(allocs))
|
|
|
|
if id == -1 {
|
|
|
|
id = allocs[0].SegmentID
|
|
|
|
} else {
|
|
|
|
assert.EqualValues(t, id, allocs[0].SegmentID)
|
|
|
|
}
|
|
|
|
if allocs[0].ExpireTime > maxts {
|
|
|
|
maxts = allocs[0].ExpireTime
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-03 14:13:49 +08:00
|
|
|
segment := meta.GetHealthySegment(id)
|
2021-08-12 10:48:08 +08:00
|
|
|
assert.NotNil(t, segment)
|
|
|
|
assert.EqualValues(t, 100, len(segment.allocations))
|
2021-09-27 22:42:02 +08:00
|
|
|
err = segmentManager.ExpireAllocations("ch1", maxts)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2023-03-03 14:13:49 +08:00
|
|
|
segment = meta.GetHealthySegment(id)
|
2021-08-12 10:48:08 +08:00
|
|
|
assert.NotNil(t, segment)
|
|
|
|
assert.EqualValues(t, 0, len(segment.allocations))
|
|
|
|
}
|
2021-08-19 14:08:10 +08:00
|
|
|
|
2023-01-31 12:41:53 +08:00
|
|
|
func TestCleanExpiredBulkloadSegment(t *testing.T) {
|
|
|
|
t.Run("expiredBulkloadSegment", func(t *testing.T) {
|
2023-09-05 10:31:48 +08:00
|
|
|
paramtable.Init()
|
2023-01-31 12:41:53 +08:00
|
|
|
mockAllocator := newMockAllocator()
|
|
|
|
meta, err := newMemoryMeta()
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2023-01-31 12:41:53 +08:00
|
|
|
|
|
|
|
schema := newTestSchema()
|
|
|
|
collID, err := mockAllocator.allocID(context.Background())
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2023-01-31 12:41:53 +08:00
|
|
|
meta.AddCollection(&collectionInfo{ID: collID, Schema: schema})
|
2023-07-24 14:11:07 +08:00
|
|
|
segmentManager, _ := newSegmentManager(meta, mockAllocator)
|
2023-01-31 12:41:53 +08:00
|
|
|
allocation, err := segmentManager.allocSegmentForImport(context.TODO(), collID, 0, "c1", 2, 1)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2023-01-31 12:41:53 +08:00
|
|
|
|
|
|
|
ids, err := segmentManager.GetFlushableSegments(context.TODO(), "c1", allocation.ExpireTime)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2023-01-31 12:41:53 +08:00
|
|
|
assert.EqualValues(t, len(ids), 0)
|
|
|
|
|
|
|
|
assert.EqualValues(t, len(segmentManager.segments), 1)
|
|
|
|
|
|
|
|
ids, err = segmentManager.GetFlushableSegments(context.TODO(), "c1", allocation.ExpireTime+1)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2023-01-31 12:41:53 +08:00
|
|
|
assert.Empty(t, ids)
|
|
|
|
assert.EqualValues(t, len(ids), 0)
|
|
|
|
|
|
|
|
assert.EqualValues(t, len(segmentManager.segments), 0)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-19 14:08:10 +08:00
|
|
|
func TestGetFlushableSegments(t *testing.T) {
|
|
|
|
t.Run("get flushable segments between small interval", func(t *testing.T) {
|
2023-09-05 10:31:48 +08:00
|
|
|
paramtable.Init()
|
2021-08-19 14:08:10 +08:00
|
|
|
mockAllocator := newMockAllocator()
|
2022-11-03 14:41:35 +08:00
|
|
|
meta, err := newMemoryMeta()
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-08-19 14:08:10 +08:00
|
|
|
|
|
|
|
schema := newTestSchema()
|
2021-08-23 17:59:51 +08:00
|
|
|
collID, err := mockAllocator.allocID(context.Background())
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2022-10-10 20:31:22 +08:00
|
|
|
meta.AddCollection(&collectionInfo{ID: collID, Schema: schema})
|
2023-07-24 14:11:07 +08:00
|
|
|
segmentManager, _ := newSegmentManager(meta, mockAllocator)
|
2021-08-19 14:08:10 +08:00
|
|
|
allocations, err := segmentManager.AllocSegment(context.TODO(), collID, 0, "c1", 2)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-08-19 14:08:10 +08:00
|
|
|
assert.EqualValues(t, 1, len(allocations))
|
|
|
|
|
2023-01-31 12:41:53 +08:00
|
|
|
ids, err := segmentManager.SealAllSegments(context.TODO(), collID, nil, false)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-08-19 14:08:10 +08:00
|
|
|
assert.EqualValues(t, 1, len(ids))
|
|
|
|
assert.EqualValues(t, allocations[0].SegmentID, ids[0])
|
|
|
|
|
2021-12-15 10:53:16 +08:00
|
|
|
meta.SetCurrentRows(allocations[0].SegmentID, 1)
|
2021-08-19 14:08:10 +08:00
|
|
|
ids, err = segmentManager.GetFlushableSegments(context.TODO(), "c1", allocations[0].ExpireTime)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-08-19 14:08:10 +08:00
|
|
|
assert.EqualValues(t, 1, len(ids))
|
|
|
|
assert.EqualValues(t, allocations[0].SegmentID, ids[0])
|
|
|
|
|
|
|
|
meta.SetLastFlushTime(allocations[0].SegmentID, time.Now())
|
|
|
|
ids, err = segmentManager.GetFlushableSegments(context.TODO(), "c1", allocations[0].ExpireTime)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-08-19 14:08:10 +08:00
|
|
|
assert.Empty(t, ids)
|
|
|
|
|
|
|
|
meta.SetLastFlushTime(allocations[0].SegmentID, time.Now().Local().Add(-flushInterval))
|
|
|
|
ids, err = segmentManager.GetFlushableSegments(context.TODO(), "c1", allocations[0].ExpireTime)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-08-19 14:08:10 +08:00
|
|
|
assert.EqualValues(t, 1, len(ids))
|
|
|
|
assert.EqualValues(t, allocations[0].SegmentID, ids[0])
|
2022-06-24 15:16:14 +08:00
|
|
|
|
|
|
|
meta.SetCurrentRows(allocations[0].SegmentID, 0)
|
|
|
|
ids, err = segmentManager.GetFlushableSegments(context.TODO(), "c1", allocations[0].ExpireTime)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2022-06-24 15:16:14 +08:00
|
|
|
assert.Empty(t, ids)
|
2023-03-03 14:13:49 +08:00
|
|
|
assert.Nil(t, meta.GetHealthySegment(allocations[0].SegmentID))
|
2021-08-19 14:08:10 +08:00
|
|
|
})
|
|
|
|
}
|
2021-09-07 13:59:58 +08:00
|
|
|
|
|
|
|
func TestTryToSealSegment(t *testing.T) {
|
|
|
|
t.Run("normal seal with segment policies", func(t *testing.T) {
|
2023-09-05 10:31:48 +08:00
|
|
|
paramtable.Init()
|
2021-09-07 13:59:58 +08:00
|
|
|
mockAllocator := newMockAllocator()
|
2022-11-03 14:41:35 +08:00
|
|
|
meta, err := newMemoryMeta()
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-09-07 13:59:58 +08:00
|
|
|
|
|
|
|
schema := newTestSchema()
|
|
|
|
collID, err := mockAllocator.allocID(context.Background())
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2022-10-10 20:31:22 +08:00
|
|
|
meta.AddCollection(&collectionInfo{ID: collID, Schema: schema})
|
2023-09-21 09:45:27 +08:00
|
|
|
segmentManager, _ := newSegmentManager(meta, mockAllocator, withSegmentSealPolices(sealByLifetimePolicy(math.MinInt64))) // always seal
|
2021-09-07 13:59:58 +08:00
|
|
|
allocations, err := segmentManager.AllocSegment(context.TODO(), collID, 0, "c1", 2)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-09-07 13:59:58 +08:00
|
|
|
assert.EqualValues(t, 1, len(allocations))
|
|
|
|
|
|
|
|
ts, err := segmentManager.allocator.allocTimestamp(context.Background())
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-09-07 13:59:58 +08:00
|
|
|
err = segmentManager.tryToSealSegment(ts, "c1")
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-09-07 13:59:58 +08:00
|
|
|
|
|
|
|
for _, seg := range segmentManager.meta.segments.segments {
|
|
|
|
assert.Equal(t, commonpb.SegmentState_Sealed, seg.GetState())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("normal seal with channel seal policies", func(t *testing.T) {
|
2023-09-05 10:31:48 +08:00
|
|
|
paramtable.Init()
|
2021-09-07 13:59:58 +08:00
|
|
|
mockAllocator := newMockAllocator()
|
2022-11-03 14:41:35 +08:00
|
|
|
meta, err := newMemoryMeta()
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-09-07 13:59:58 +08:00
|
|
|
|
|
|
|
schema := newTestSchema()
|
|
|
|
collID, err := mockAllocator.allocID(context.Background())
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2022-10-10 20:31:22 +08:00
|
|
|
meta.AddCollection(&collectionInfo{ID: collID, Schema: schema})
|
2023-09-21 09:45:27 +08:00
|
|
|
segmentManager, _ := newSegmentManager(meta, mockAllocator, withChannelSealPolices(getChannelOpenSegCapacityPolicy(-1))) // always seal
|
2021-09-07 13:59:58 +08:00
|
|
|
allocations, err := segmentManager.AllocSegment(context.TODO(), collID, 0, "c1", 2)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-09-07 13:59:58 +08:00
|
|
|
assert.EqualValues(t, 1, len(allocations))
|
|
|
|
|
|
|
|
ts, err := segmentManager.allocator.allocTimestamp(context.Background())
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-09-07 13:59:58 +08:00
|
|
|
err = segmentManager.tryToSealSegment(ts, "c1")
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-09-07 13:59:58 +08:00
|
|
|
|
|
|
|
for _, seg := range segmentManager.meta.segments.segments {
|
|
|
|
assert.Equal(t, commonpb.SegmentState_Sealed, seg.GetState())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("normal seal with both segment & channel seal policy", func(t *testing.T) {
|
2023-09-05 10:31:48 +08:00
|
|
|
paramtable.Init()
|
2021-09-07 13:59:58 +08:00
|
|
|
mockAllocator := newMockAllocator()
|
2022-11-03 14:41:35 +08:00
|
|
|
meta, err := newMemoryMeta()
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-09-07 13:59:58 +08:00
|
|
|
|
|
|
|
schema := newTestSchema()
|
|
|
|
collID, err := mockAllocator.allocID(context.Background())
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2022-10-10 20:31:22 +08:00
|
|
|
meta.AddCollection(&collectionInfo{ID: collID, Schema: schema})
|
2023-07-24 14:11:07 +08:00
|
|
|
segmentManager, _ := newSegmentManager(meta, mockAllocator,
|
2021-09-07 13:59:58 +08:00
|
|
|
withSegmentSealPolices(sealByLifetimePolicy(math.MinInt64)),
|
2023-09-21 09:45:27 +08:00
|
|
|
withChannelSealPolices(getChannelOpenSegCapacityPolicy(-1))) // always seal
|
2021-09-07 13:59:58 +08:00
|
|
|
allocations, err := segmentManager.AllocSegment(context.TODO(), collID, 0, "c1", 2)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-09-07 13:59:58 +08:00
|
|
|
assert.EqualValues(t, 1, len(allocations))
|
|
|
|
|
|
|
|
ts, err := segmentManager.allocator.allocTimestamp(context.Background())
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-09-07 13:59:58 +08:00
|
|
|
err = segmentManager.tryToSealSegment(ts, "c1")
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-09-07 13:59:58 +08:00
|
|
|
|
|
|
|
for _, seg := range segmentManager.meta.segments.segments {
|
|
|
|
assert.Equal(t, commonpb.SegmentState_Sealed, seg.GetState())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-12-20 14:09:25 +08:00
|
|
|
t.Run("test sealByMaxBinlogFileNumberPolicy", func(t *testing.T) {
|
2023-09-05 10:31:48 +08:00
|
|
|
paramtable.Init()
|
2022-12-20 14:09:25 +08:00
|
|
|
mockAllocator := newMockAllocator()
|
|
|
|
meta, err := newMemoryMeta()
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2022-12-20 14:09:25 +08:00
|
|
|
|
|
|
|
schema := newTestSchema()
|
|
|
|
collID, err := mockAllocator.allocID(context.Background())
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2022-12-20 14:09:25 +08:00
|
|
|
meta.AddCollection(&collectionInfo{ID: collID, Schema: schema})
|
2023-07-24 14:11:07 +08:00
|
|
|
segmentManager, _ := newSegmentManager(meta, mockAllocator)
|
2022-12-20 14:09:25 +08:00
|
|
|
allocations, err := segmentManager.AllocSegment(context.TODO(), collID, 0, "c1", 2)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2022-12-20 14:09:25 +08:00
|
|
|
assert.EqualValues(t, 1, len(allocations))
|
|
|
|
|
|
|
|
ts, err := segmentManager.allocator.allocTimestamp(context.Background())
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2022-12-20 14:09:25 +08:00
|
|
|
|
|
|
|
// No seal polices
|
|
|
|
{
|
|
|
|
err = segmentManager.tryToSealSegment(ts, "c1")
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2022-12-20 14:09:25 +08:00
|
|
|
segments := segmentManager.meta.segments.segments
|
|
|
|
assert.Equal(t, 1, len(segments))
|
|
|
|
for _, seg := range segments {
|
|
|
|
assert.Equal(t, commonpb.SegmentState_Growing, seg.GetState())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not trigger seal
|
|
|
|
{
|
|
|
|
segmentManager.segmentSealPolicies = []segmentSealPolicy{sealByMaxBinlogFileNumberPolicy(2)}
|
|
|
|
segments := segmentManager.meta.segments.segments
|
|
|
|
assert.Equal(t, 1, len(segments))
|
|
|
|
for _, seg := range segments {
|
2023-02-03 17:07:52 +08:00
|
|
|
seg.Statslogs = []*datapb.FieldBinlog{
|
2022-12-20 14:09:25 +08:00
|
|
|
{
|
|
|
|
FieldID: 2,
|
|
|
|
Binlogs: []*datapb.Binlog{
|
|
|
|
{
|
|
|
|
EntriesNum: 10,
|
|
|
|
LogID: 3,
|
|
|
|
LogPath: metautil.BuildInsertLogPath("", 1, 1, seg.ID, 2, 3),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = segmentManager.tryToSealSegment(ts, "c1")
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2022-12-20 14:09:25 +08:00
|
|
|
seg = segmentManager.meta.segments.segments[seg.ID]
|
|
|
|
assert.Equal(t, commonpb.SegmentState_Growing, seg.GetState())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trigger seal
|
|
|
|
{
|
|
|
|
segmentManager.segmentSealPolicies = []segmentSealPolicy{sealByMaxBinlogFileNumberPolicy(2)}
|
|
|
|
segments := segmentManager.meta.segments.segments
|
|
|
|
assert.Equal(t, 1, len(segments))
|
|
|
|
for _, seg := range segments {
|
2023-02-03 17:07:52 +08:00
|
|
|
seg.Statslogs = []*datapb.FieldBinlog{
|
2022-12-20 14:09:25 +08:00
|
|
|
{
|
|
|
|
FieldID: 1,
|
|
|
|
Binlogs: []*datapb.Binlog{
|
|
|
|
{
|
|
|
|
EntriesNum: 10,
|
|
|
|
LogID: 1,
|
|
|
|
LogPath: metautil.BuildInsertLogPath("", 1, 1, seg.ID, 1, 3),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
EntriesNum: 20,
|
|
|
|
LogID: 2,
|
|
|
|
LogPath: metautil.BuildInsertLogPath("", 1, 1, seg.ID, 1, 2),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = segmentManager.tryToSealSegment(ts, "c1")
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2022-12-20 14:09:25 +08:00
|
|
|
seg = segmentManager.meta.segments.segments[seg.ID]
|
|
|
|
assert.Equal(t, commonpb.SegmentState_Sealed, seg.GetState())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-09-07 13:59:58 +08:00
|
|
|
t.Run("seal with segment policy with kv fails", func(t *testing.T) {
|
2023-09-05 10:31:48 +08:00
|
|
|
paramtable.Init()
|
2021-09-07 13:59:58 +08:00
|
|
|
mockAllocator := newMockAllocator()
|
2023-01-06 14:33:36 +08:00
|
|
|
memoryKV := NewMetaMemoryKV()
|
|
|
|
catalog := datacoord.NewCatalog(memoryKV, "", "")
|
|
|
|
meta, err := newMeta(context.TODO(), catalog, nil)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-09-07 13:59:58 +08:00
|
|
|
|
|
|
|
schema := newTestSchema()
|
|
|
|
collID, err := mockAllocator.allocID(context.Background())
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2022-10-10 20:31:22 +08:00
|
|
|
meta.AddCollection(&collectionInfo{ID: collID, Schema: schema})
|
2023-09-21 09:45:27 +08:00
|
|
|
segmentManager, _ := newSegmentManager(meta, mockAllocator, withSegmentSealPolices(sealByLifetimePolicy(math.MinInt64))) // always seal
|
2021-09-07 13:59:58 +08:00
|
|
|
allocations, err := segmentManager.AllocSegment(context.TODO(), collID, 0, "c1", 2)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-09-07 13:59:58 +08:00
|
|
|
assert.EqualValues(t, 1, len(allocations))
|
|
|
|
|
2023-07-07 19:38:26 +08:00
|
|
|
metakv := mockkv.NewMetaKv(t)
|
|
|
|
metakv.EXPECT().Save(mock.Anything, mock.Anything).Return(errors.New("failed")).Maybe()
|
|
|
|
metakv.EXPECT().MultiSave(mock.Anything).Return(errors.New("failed")).Maybe()
|
|
|
|
metakv.EXPECT().LoadWithPrefix(mock.Anything).Return(nil, nil, nil).Maybe()
|
|
|
|
segmentManager.meta.catalog = &datacoord.Catalog{MetaKv: metakv}
|
2021-09-07 13:59:58 +08:00
|
|
|
|
|
|
|
ts, err := segmentManager.allocator.allocTimestamp(context.Background())
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-09-07 13:59:58 +08:00
|
|
|
err = segmentManager.tryToSealSegment(ts, "c1")
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.Error(t, err)
|
2021-09-07 13:59:58 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("seal with channel policy with kv fails", func(t *testing.T) {
|
2023-09-05 10:31:48 +08:00
|
|
|
paramtable.Init()
|
2021-09-07 13:59:58 +08:00
|
|
|
mockAllocator := newMockAllocator()
|
2023-01-06 14:33:36 +08:00
|
|
|
memoryKV := NewMetaMemoryKV()
|
|
|
|
catalog := datacoord.NewCatalog(memoryKV, "", "")
|
|
|
|
meta, err := newMeta(context.TODO(), catalog, nil)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-09-07 13:59:58 +08:00
|
|
|
|
|
|
|
schema := newTestSchema()
|
|
|
|
collID, err := mockAllocator.allocID(context.Background())
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2022-10-10 20:31:22 +08:00
|
|
|
meta.AddCollection(&collectionInfo{ID: collID, Schema: schema})
|
2023-09-21 09:45:27 +08:00
|
|
|
segmentManager, _ := newSegmentManager(meta, mockAllocator, withChannelSealPolices(getChannelOpenSegCapacityPolicy(-1))) // always seal
|
2021-09-07 13:59:58 +08:00
|
|
|
allocations, err := segmentManager.AllocSegment(context.TODO(), collID, 0, "c1", 2)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-09-07 13:59:58 +08:00
|
|
|
assert.EqualValues(t, 1, len(allocations))
|
|
|
|
|
2023-07-07 19:38:26 +08:00
|
|
|
metakv := mockkv.NewMetaKv(t)
|
|
|
|
metakv.EXPECT().Save(mock.Anything, mock.Anything).Return(errors.New("failed")).Maybe()
|
|
|
|
metakv.EXPECT().MultiSave(mock.Anything).Return(errors.New("failed")).Maybe()
|
|
|
|
metakv.EXPECT().LoadWithPrefix(mock.Anything).Return(nil, nil, nil).Maybe()
|
|
|
|
segmentManager.meta.catalog = &datacoord.Catalog{MetaKv: metakv}
|
2021-09-07 13:59:58 +08:00
|
|
|
|
|
|
|
ts, err := segmentManager.allocator.allocTimestamp(context.Background())
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-09-07 13:59:58 +08:00
|
|
|
err = segmentManager.tryToSealSegment(ts, "c1")
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.Error(t, err)
|
2021-09-07 13:59:58 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAllocationPool(t *testing.T) {
|
|
|
|
t.Run("normal get&put", func(t *testing.T) {
|
|
|
|
allocPool = sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
return &Allocation{}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
allo := getAllocation(100)
|
|
|
|
assert.EqualValues(t, 100, allo.NumOfRows)
|
|
|
|
assert.EqualValues(t, 0, allo.ExpireTime)
|
|
|
|
assert.EqualValues(t, 0, allo.SegmentID)
|
|
|
|
|
|
|
|
putAllocation(allo)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("put nil", func(t *testing.T) {
|
2021-12-14 15:31:07 +08:00
|
|
|
var allo *Allocation
|
2021-09-07 13:59:58 +08:00
|
|
|
allocPool = sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
return &Allocation{}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
putAllocation(allo)
|
|
|
|
allo = getAllocation(100)
|
|
|
|
assert.EqualValues(t, 100, allo.NumOfRows)
|
|
|
|
assert.EqualValues(t, 0, allo.ExpireTime)
|
|
|
|
assert.EqualValues(t, 0, allo.SegmentID)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("put something else", func(t *testing.T) {
|
|
|
|
allocPool = sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
return &Allocation{}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
allocPool.Put(&struct{}{})
|
|
|
|
allo := getAllocation(100)
|
|
|
|
assert.EqualValues(t, 100, allo.NumOfRows)
|
|
|
|
assert.EqualValues(t, 0, allo.ExpireTime)
|
|
|
|
assert.EqualValues(t, 0, allo.SegmentID)
|
|
|
|
})
|
|
|
|
}
|
2021-11-12 00:22:42 +08:00
|
|
|
|
|
|
|
func TestSegmentManager_DropSegmentsOfChannel(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
meta *meta
|
|
|
|
segments []UniqueID
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
channel string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
want []UniqueID
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"test drop segments",
|
|
|
|
fields{
|
|
|
|
meta: &meta{
|
|
|
|
segments: &SegmentsInfo{
|
|
|
|
segments: map[int64]*SegmentInfo{
|
|
|
|
1: {
|
|
|
|
SegmentInfo: &datapb.SegmentInfo{
|
|
|
|
ID: 1,
|
|
|
|
InsertChannel: "ch1",
|
2021-11-29 22:35:41 +08:00
|
|
|
State: commonpb.SegmentState_Flushed,
|
2021-11-12 00:22:42 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
2: {
|
|
|
|
SegmentInfo: &datapb.SegmentInfo{
|
|
|
|
ID: 2,
|
|
|
|
InsertChannel: "ch2",
|
2021-11-29 22:35:41 +08:00
|
|
|
State: commonpb.SegmentState_Flushed,
|
2021-11-12 00:22:42 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
segments: []UniqueID{1, 2},
|
|
|
|
},
|
|
|
|
args{
|
|
|
|
"ch1",
|
|
|
|
},
|
|
|
|
[]UniqueID{2},
|
|
|
|
},
|
2021-11-12 17:31:11 +08:00
|
|
|
{
|
|
|
|
"test drop segments with dropped segment",
|
|
|
|
fields{
|
|
|
|
meta: &meta{
|
|
|
|
segments: &SegmentsInfo{
|
|
|
|
segments: map[int64]*SegmentInfo{
|
|
|
|
1: {
|
|
|
|
SegmentInfo: &datapb.SegmentInfo{
|
|
|
|
ID: 1,
|
|
|
|
InsertChannel: "ch1",
|
|
|
|
State: commonpb.SegmentState_Dropped,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
2: {
|
|
|
|
SegmentInfo: &datapb.SegmentInfo{
|
|
|
|
ID: 2,
|
|
|
|
InsertChannel: "ch2",
|
2021-11-29 22:35:41 +08:00
|
|
|
State: commonpb.SegmentState_Growing,
|
2021-11-12 17:31:11 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
segments: []UniqueID{1, 2, 3},
|
|
|
|
},
|
|
|
|
args{
|
|
|
|
"ch1",
|
|
|
|
},
|
|
|
|
[]UniqueID{2},
|
|
|
|
},
|
2021-11-12 00:22:42 +08:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
s := &SegmentManager{
|
|
|
|
meta: tt.fields.meta,
|
|
|
|
segments: tt.fields.segments,
|
|
|
|
}
|
|
|
|
s.DropSegmentsOfChannel(context.TODO(), tt.args.channel)
|
|
|
|
assert.ElementsMatch(t, tt.want, s.segments)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|