2021-05-21 18:30:41 +08:00
|
|
|
// Copyright (C) 2019-2020 Zilliz. All rights reserved.//
|
2021-04-19 11:35:38 +08:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// 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-01-22 19:43:27 +08:00
|
|
|
"math"
|
|
|
|
"testing"
|
|
|
|
|
2021-05-21 18:30:41 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/commonpb"
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/datapb"
|
2021-04-09 09:55:04 +08:00
|
|
|
|
2021-01-22 19:43:27 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAllocSegment(t *testing.T) {
|
2021-03-23 16:57:59 +08:00
|
|
|
ctx := context.Background()
|
2021-01-22 19:43:27 +08:00
|
|
|
Params.Init()
|
|
|
|
mockAllocator := newMockAllocator()
|
|
|
|
meta, err := newMemoryMeta(mockAllocator)
|
|
|
|
assert.Nil(t, err)
|
2021-06-03 19:06:33 +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-01-22 19:43:27 +08:00
|
|
|
collID, err := mockAllocator.allocID()
|
2021-01-23 14:41:29 +08:00
|
|
|
assert.Nil(t, err)
|
2021-04-09 09:55:04 +08:00
|
|
|
err = meta.AddCollection(&datapb.CollectionInfo{
|
2021-01-22 19:43:27 +08:00
|
|
|
ID: collID,
|
|
|
|
Schema: schema,
|
|
|
|
})
|
|
|
|
assert.Nil(t, err)
|
|
|
|
cases := []struct {
|
|
|
|
collectionID UniqueID
|
|
|
|
partitionID UniqueID
|
|
|
|
channelName string
|
2021-05-21 18:30:41 +08:00
|
|
|
requestRows int64
|
2021-01-22 19:43:27 +08:00
|
|
|
expectResult bool
|
|
|
|
}{
|
|
|
|
{collID, 100, "c1", 100, true},
|
|
|
|
{collID, 100, "c1", math.MaxInt64, false},
|
|
|
|
}
|
|
|
|
for _, c := range cases {
|
2021-06-03 19:06:33 +08:00
|
|
|
id, count, expireTime, err := segmentManager.AllocSegment(ctx, c.collectionID, c.partitionID, c.channelName, c.requestRows)
|
2021-01-22 19:43:27 +08:00
|
|
|
if c.expectResult {
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.EqualValues(t, c.requestRows, count)
|
|
|
|
assert.NotEqualValues(t, 0, id)
|
|
|
|
assert.NotEqualValues(t, 0, expireTime)
|
|
|
|
} else {
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-21 18:30:41 +08:00
|
|
|
func TestLoadSegmentsFromMeta(t *testing.T) {
|
2021-01-22 19:43:27 +08:00
|
|
|
Params.Init()
|
|
|
|
mockAllocator := newMockAllocator()
|
|
|
|
meta, err := newMemoryMeta(mockAllocator)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2021-01-23 14:41:29 +08:00
|
|
|
schema := newTestSchema()
|
2021-01-22 19:43:27 +08:00
|
|
|
collID, err := mockAllocator.allocID()
|
2021-01-23 14:41:29 +08:00
|
|
|
assert.Nil(t, err)
|
2021-04-09 09:55:04 +08:00
|
|
|
err = meta.AddCollection(&datapb.CollectionInfo{
|
2021-01-22 19:43:27 +08:00
|
|
|
ID: collID,
|
|
|
|
Schema: schema,
|
|
|
|
})
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
err = meta.AddSegment(sealedSegment)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
err = meta.AddSegment(growingSegment)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
err = meta.AddSegment(flushedSegment)
|
2021-01-22 19:43:27 +08:00
|
|
|
assert.Nil(t, err)
|
2021-02-02 18:53:10 +08:00
|
|
|
|
2021-06-03 19:06:33 +08:00
|
|
|
segmentManager := newSegmentManager(meta, mockAllocator)
|
|
|
|
segments := segmentManager.stats
|
2021-05-21 18:30:41 +08:00
|
|
|
assert.EqualValues(t, 2, len(segments))
|
|
|
|
assert.NotNil(t, segments[1])
|
2021-06-03 19:06:33 +08:00
|
|
|
assert.NotNil(t, segments[2])
|
2021-05-21 18:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSaveSegmentsToMeta(t *testing.T) {
|
|
|
|
Params.Init()
|
|
|
|
mockAllocator := newMockAllocator()
|
|
|
|
meta, err := newMemoryMeta(mockAllocator)
|
2021-01-22 19:43:27 +08:00
|
|
|
assert.Nil(t, err)
|
2021-05-21 18:30:41 +08:00
|
|
|
|
|
|
|
schema := newTestSchema()
|
|
|
|
collID, err := mockAllocator.allocID()
|
2021-02-02 18:53:10 +08:00
|
|
|
assert.Nil(t, err)
|
2021-05-21 18:30:41 +08:00
|
|
|
err = meta.AddCollection(&datapb.CollectionInfo{
|
|
|
|
ID: collID,
|
|
|
|
Schema: schema,
|
|
|
|
})
|
2021-01-22 19:43:27 +08:00
|
|
|
assert.Nil(t, err)
|
2021-05-21 18:30:41 +08:00
|
|
|
|
2021-06-03 19:06:33 +08:00
|
|
|
segmentManager := newSegmentManager(meta, mockAllocator)
|
|
|
|
segID, _, expireTs, err := segmentManager.AllocSegment(context.Background(), collID, 0, "c1", 1000)
|
2021-05-21 18:30:41 +08:00
|
|
|
assert.Nil(t, err)
|
2021-06-03 19:06:33 +08:00
|
|
|
segStatus := segmentManager.stats[segID]
|
2021-05-21 18:30:41 +08:00
|
|
|
assert.NotNil(t, segStatus)
|
2021-06-03 19:06:33 +08:00
|
|
|
err = segmentManager.SealAllSegments(context.Background(), collID)
|
2021-05-21 18:30:41 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
segment, err := meta.GetSegment(segID)
|
2021-01-22 19:43:27 +08:00
|
|
|
assert.Nil(t, err)
|
2021-05-21 18:30:41 +08:00
|
|
|
assert.EqualValues(t, segment.LastExpireTime, expireTs)
|
|
|
|
assert.EqualValues(t, segStatus.total, segment.MaxRowNum)
|
|
|
|
assert.EqualValues(t, commonpb.SegmentState_Sealed, segment.State)
|
2021-01-22 19:43:27 +08:00
|
|
|
}
|