2021-10-25 20:21:18 +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 20:21:18 +08:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2021-04-19 11:35:38 +08:00
|
|
|
//
|
2021-10-25 20:21:18 +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-23 14:41:29 +08:00
|
|
|
|
|
|
|
import (
|
2021-08-23 17:59:51 +08:00
|
|
|
"context"
|
2021-10-24 22:39:09 +08:00
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2021-01-23 14:41:29 +08:00
|
|
|
"testing"
|
|
|
|
|
2021-04-09 09:55:04 +08:00
|
|
|
"github.com/golang/protobuf/proto"
|
2021-09-08 11:35:59 +08:00
|
|
|
memkv "github.com/milvus-io/milvus/internal/kv/mem"
|
2021-05-21 14:51:25 +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-10-12 23:46:35 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/internalpb"
|
2021-01-23 14:41:29 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2021-04-23 10:15:30 +08:00
|
|
|
func TestMeta_Basic(t *testing.T) {
|
|
|
|
const collID = UniqueID(0)
|
|
|
|
const partID0 = UniqueID(100)
|
|
|
|
const partID1 = UniqueID(101)
|
|
|
|
const channelName = "c1"
|
2021-08-23 17:59:51 +08:00
|
|
|
ctx := context.Background()
|
2021-04-23 10:15:30 +08:00
|
|
|
|
2021-01-23 14:41:29 +08:00
|
|
|
mockAllocator := newMockAllocator()
|
|
|
|
meta, err := newMemoryMeta(mockAllocator)
|
|
|
|
assert.Nil(t, err)
|
2021-04-23 10:15:30 +08:00
|
|
|
|
2021-01-23 14:41:29 +08:00
|
|
|
testSchema := newTestSchema()
|
2021-04-23 10:15:30 +08:00
|
|
|
collInfo := &datapb.CollectionInfo{
|
|
|
|
ID: collID,
|
|
|
|
Schema: testSchema,
|
|
|
|
Partitions: []UniqueID{partID0, partID1},
|
|
|
|
}
|
|
|
|
collInfoWoPartition := &datapb.CollectionInfo{
|
|
|
|
ID: collID,
|
2021-01-23 14:41:29 +08:00
|
|
|
Schema: testSchema,
|
2021-04-23 10:15:30 +08:00
|
|
|
Partitions: []UniqueID{},
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("Test Collection", func(t *testing.T) {
|
2021-07-07 14:02:01 +08:00
|
|
|
meta.AddCollection(collInfo)
|
2021-04-23 10:15:30 +08:00
|
|
|
// check has collection
|
2021-07-07 14:02:01 +08:00
|
|
|
collInfo := meta.GetCollection(collID)
|
|
|
|
assert.NotNil(t, collInfo)
|
2021-04-23 10:15:30 +08:00
|
|
|
|
|
|
|
// check partition info
|
|
|
|
assert.EqualValues(t, collID, collInfo.ID)
|
|
|
|
assert.EqualValues(t, testSchema, collInfo.Schema)
|
|
|
|
assert.EqualValues(t, 2, len(collInfo.Partitions))
|
|
|
|
assert.EqualValues(t, partID0, collInfo.Partitions[0])
|
|
|
|
assert.EqualValues(t, partID1, collInfo.Partitions[1])
|
2021-01-23 14:41:29 +08:00
|
|
|
})
|
2021-01-23 20:22:59 +08:00
|
|
|
|
2021-04-23 10:15:30 +08:00
|
|
|
t.Run("Test Segment", func(t *testing.T) {
|
2021-07-07 14:02:01 +08:00
|
|
|
meta.AddCollection(collInfoWoPartition)
|
2021-04-23 10:15:30 +08:00
|
|
|
// create seg0 for partition0, seg0/seg1 for partition1
|
2021-08-23 17:59:51 +08:00
|
|
|
segID0_0, err := mockAllocator.allocID(ctx)
|
2021-04-23 10:15:30 +08:00
|
|
|
assert.Nil(t, err)
|
2021-07-07 14:02:01 +08:00
|
|
|
segInfo0_0 := buildSegment(collID, partID0, segID0_0, channelName)
|
2021-08-23 17:59:51 +08:00
|
|
|
segID1_0, err := mockAllocator.allocID(ctx)
|
2021-04-23 10:15:30 +08:00
|
|
|
assert.Nil(t, err)
|
2021-07-07 14:02:01 +08:00
|
|
|
segInfo1_0 := buildSegment(collID, partID1, segID1_0, channelName)
|
2021-08-23 17:59:51 +08:00
|
|
|
segID1_1, err := mockAllocator.allocID(ctx)
|
2021-04-23 10:15:30 +08:00
|
|
|
assert.Nil(t, err)
|
2021-07-07 14:02:01 +08:00
|
|
|
segInfo1_1 := buildSegment(collID, partID1, segID1_1, channelName)
|
2021-01-23 20:22:59 +08:00
|
|
|
|
2021-04-23 10:15:30 +08:00
|
|
|
// check AddSegment
|
|
|
|
err = meta.AddSegment(segInfo0_0)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
err = meta.AddSegment(segInfo1_0)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
err = meta.AddSegment(segInfo1_1)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
// check GetSegment
|
2021-07-07 14:02:01 +08:00
|
|
|
info0_0 := meta.GetSegment(segID0_0)
|
|
|
|
assert.NotNil(t, info0_0)
|
2021-04-23 10:15:30 +08:00
|
|
|
assert.True(t, proto.Equal(info0_0, segInfo0_0))
|
2021-07-07 14:02:01 +08:00
|
|
|
info1_0 := meta.GetSegment(segID1_0)
|
|
|
|
assert.NotNil(t, info1_0)
|
2021-04-23 10:15:30 +08:00
|
|
|
assert.True(t, proto.Equal(info1_0, segInfo1_0))
|
|
|
|
|
|
|
|
// check GetSegmentsOfCollection
|
|
|
|
segIDs := meta.GetSegmentsOfCollection(collID)
|
|
|
|
assert.EqualValues(t, 3, len(segIDs))
|
|
|
|
assert.Contains(t, segIDs, segID0_0)
|
|
|
|
assert.Contains(t, segIDs, segID1_0)
|
|
|
|
assert.Contains(t, segIDs, segID1_1)
|
|
|
|
|
|
|
|
// check GetSegmentsOfPartition
|
|
|
|
segIDs = meta.GetSegmentsOfPartition(collID, partID0)
|
|
|
|
assert.EqualValues(t, 1, len(segIDs))
|
|
|
|
assert.Contains(t, segIDs, segID0_0)
|
|
|
|
segIDs = meta.GetSegmentsOfPartition(collID, partID1)
|
|
|
|
assert.EqualValues(t, 2, len(segIDs))
|
|
|
|
assert.Contains(t, segIDs, segID1_0)
|
|
|
|
assert.Contains(t, segIDs, segID1_1)
|
|
|
|
|
|
|
|
// check DropSegment
|
|
|
|
err = meta.DropSegment(segID1_0)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
segIDs = meta.GetSegmentsOfPartition(collID, partID1)
|
|
|
|
assert.EqualValues(t, 1, len(segIDs))
|
|
|
|
assert.Contains(t, segIDs, segID1_1)
|
|
|
|
|
2021-07-07 14:02:01 +08:00
|
|
|
err = meta.SetState(segID0_0, commonpb.SegmentState_Sealed)
|
2021-04-23 10:15:30 +08:00
|
|
|
assert.Nil(t, err)
|
2021-07-07 14:02:01 +08:00
|
|
|
err = meta.SetState(segID0_0, commonpb.SegmentState_Flushed)
|
2021-04-23 10:15:30 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2021-07-07 14:02:01 +08:00
|
|
|
info0_0 = meta.GetSegment(segID0_0)
|
|
|
|
assert.NotNil(t, info0_0)
|
2021-05-21 14:51:25 +08:00
|
|
|
assert.EqualValues(t, commonpb.SegmentState_Flushed, info0_0.State)
|
2021-01-23 20:22:59 +08:00
|
|
|
})
|
2021-02-02 14:25:58 +08:00
|
|
|
|
2021-09-08 11:35:59 +08:00
|
|
|
t.Run("Test segment with kv fails", func(t *testing.T) {
|
|
|
|
// inject error for `Save`
|
|
|
|
memoryKV := memkv.NewMemoryKV()
|
|
|
|
fkv := &saveFailKV{TxnKV: memoryKV}
|
2021-10-02 23:09:56 +08:00
|
|
|
meta, err := newMeta(fkv)
|
2021-09-08 11:35:59 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
err = meta.AddSegment(NewSegmentInfo(&datapb.SegmentInfo{}))
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
|
|
|
fkv2 := &removeFailKV{TxnKV: memoryKV}
|
2021-10-02 23:09:56 +08:00
|
|
|
meta, err = newMeta(fkv2)
|
2021-09-08 11:35:59 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
// nil, since no segment yet
|
|
|
|
err = meta.DropSegment(0)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
// nil, since Save error not injected
|
|
|
|
err = meta.AddSegment(NewSegmentInfo(&datapb.SegmentInfo{}))
|
|
|
|
assert.Nil(t, err)
|
|
|
|
// error injected
|
|
|
|
err = meta.DropSegment(0)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
})
|
|
|
|
|
2021-04-23 10:15:30 +08:00
|
|
|
t.Run("Test GetCount", func(t *testing.T) {
|
|
|
|
const rowCount0 = 100
|
|
|
|
const rowCount1 = 300
|
|
|
|
|
|
|
|
// no segment
|
2021-07-07 14:02:01 +08:00
|
|
|
nums := meta.GetNumRowsOfCollection(collID)
|
2021-04-23 10:15:30 +08:00
|
|
|
assert.EqualValues(t, 0, nums)
|
|
|
|
|
|
|
|
// add seg1 with 100 rows
|
2021-08-23 17:59:51 +08:00
|
|
|
segID0, err := mockAllocator.allocID(ctx)
|
2021-04-23 10:15:30 +08:00
|
|
|
assert.Nil(t, err)
|
2021-07-07 14:02:01 +08:00
|
|
|
segInfo0 := buildSegment(collID, partID0, segID0, channelName)
|
2021-06-04 11:45:45 +08:00
|
|
|
segInfo0.NumOfRows = rowCount0
|
2021-04-23 10:15:30 +08:00
|
|
|
err = meta.AddSegment(segInfo0)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
// add seg2 with 300 rows
|
2021-08-23 17:59:51 +08:00
|
|
|
segID1, err := mockAllocator.allocID(ctx)
|
2021-04-23 10:15:30 +08:00
|
|
|
assert.Nil(t, err)
|
2021-07-07 14:02:01 +08:00
|
|
|
segInfo1 := buildSegment(collID, partID0, segID1, channelName)
|
2021-06-04 11:45:45 +08:00
|
|
|
segInfo1.NumOfRows = rowCount1
|
2021-04-23 10:15:30 +08:00
|
|
|
err = meta.AddSegment(segInfo1)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
// check partition/collection statistics
|
2021-07-07 14:02:01 +08:00
|
|
|
nums = meta.GetNumRowsOfPartition(collID, partID0)
|
2021-06-03 19:06:33 +08:00
|
|
|
assert.EqualValues(t, (rowCount0 + rowCount1), nums)
|
2021-07-07 14:02:01 +08:00
|
|
|
nums = meta.GetNumRowsOfCollection(collID)
|
2021-06-03 19:06:33 +08:00
|
|
|
assert.EqualValues(t, (rowCount0 + rowCount1), nums)
|
2021-04-23 10:15:30 +08:00
|
|
|
})
|
2021-02-02 14:25:58 +08:00
|
|
|
}
|
2021-05-21 18:30:41 +08:00
|
|
|
|
|
|
|
func TestGetUnFlushedSegments(t *testing.T) {
|
|
|
|
mockAllocator := newMockAllocator()
|
|
|
|
meta, err := newMemoryMeta(mockAllocator)
|
|
|
|
assert.Nil(t, err)
|
2021-07-12 17:24:25 +08:00
|
|
|
s1 := &datapb.SegmentInfo{
|
2021-05-21 18:30:41 +08:00
|
|
|
ID: 0,
|
|
|
|
CollectionID: 0,
|
|
|
|
PartitionID: 0,
|
|
|
|
State: commonpb.SegmentState_Growing,
|
2021-07-12 17:24:25 +08:00
|
|
|
}
|
|
|
|
err = meta.AddSegment(NewSegmentInfo(s1))
|
2021-05-21 18:30:41 +08:00
|
|
|
assert.Nil(t, err)
|
2021-07-12 17:24:25 +08:00
|
|
|
s2 := &datapb.SegmentInfo{
|
2021-05-21 18:30:41 +08:00
|
|
|
ID: 1,
|
|
|
|
CollectionID: 0,
|
|
|
|
PartitionID: 0,
|
|
|
|
State: commonpb.SegmentState_Flushed,
|
2021-07-12 17:24:25 +08:00
|
|
|
}
|
|
|
|
err = meta.AddSegment(NewSegmentInfo(s2))
|
2021-05-21 18:30:41 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
segments := meta.GetUnFlushedSegments()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
assert.EqualValues(t, 1, len(segments))
|
|
|
|
assert.EqualValues(t, 0, segments[0].ID)
|
|
|
|
assert.NotEqualValues(t, commonpb.SegmentState_Flushed, segments[0].State)
|
|
|
|
}
|
2021-10-12 23:46:35 +08:00
|
|
|
|
|
|
|
func TestUpdateFlushSegmentsInfo(t *testing.T) {
|
|
|
|
t.Run("normal", func(t *testing.T) {
|
|
|
|
meta, err := newMeta(memkv.NewMemoryKV())
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2021-10-19 14:32:41 +08:00
|
|
|
segment1 := &SegmentInfo{SegmentInfo: &datapb.SegmentInfo{ID: 1, State: commonpb.SegmentState_Growing, Binlogs: []*datapb.FieldBinlog{{FieldID: 1, Binlogs: []string{"binlog0"}}},
|
|
|
|
Statslogs: []*datapb.FieldBinlog{{FieldID: 1, Binlogs: []string{"statslog0"}}}}}
|
2021-10-12 23:46:35 +08:00
|
|
|
err = meta.AddSegment(segment1)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
err = meta.UpdateFlushSegmentsInfo(1, true, []*datapb.FieldBinlog{{FieldID: 1, Binlogs: []string{"binlog1"}}},
|
2021-10-19 14:32:41 +08:00
|
|
|
[]*datapb.FieldBinlog{{FieldID: 1, Binlogs: []string{"statslog1"}}},
|
|
|
|
[]*datapb.DeltaLogInfo{{RecordEntries: 1, TimestampFrom: 100, TimestampTo: 200, DeltaLogSize: 1000}},
|
2021-10-12 23:46:35 +08:00
|
|
|
[]*datapb.CheckPoint{{SegmentID: 1, NumOfRows: 10}}, []*datapb.SegmentStartPosition{{SegmentID: 1, StartPosition: &internalpb.MsgPosition{MsgID: []byte{1, 2, 3}}}})
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
updated := meta.GetSegment(1)
|
|
|
|
expected := &SegmentInfo{SegmentInfo: &datapb.SegmentInfo{
|
|
|
|
ID: 1, State: commonpb.SegmentState_Flushing, NumOfRows: 10,
|
|
|
|
StartPosition: &internalpb.MsgPosition{MsgID: []byte{1, 2, 3}},
|
|
|
|
Binlogs: []*datapb.FieldBinlog{{FieldID: 1, Binlogs: []string{"binlog0", "binlog1"}}},
|
2021-10-19 14:32:41 +08:00
|
|
|
Statslogs: []*datapb.FieldBinlog{{FieldID: 1, Binlogs: []string{"statslog0", "statslog1"}}},
|
|
|
|
Deltalogs: []*datapb.DeltaLogInfo{{RecordEntries: 1, TimestampFrom: 100, TimestampTo: 200, DeltaLogSize: 1000}},
|
2021-10-12 23:46:35 +08:00
|
|
|
}}
|
|
|
|
assert.EqualValues(t, expected, updated)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("update non-existed segment", func(t *testing.T) {
|
|
|
|
meta, err := newMeta(memkv.NewMemoryKV())
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2021-10-19 14:32:41 +08:00
|
|
|
err = meta.UpdateFlushSegmentsInfo(1, false, nil, nil, nil, nil, nil)
|
2021-10-12 23:46:35 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("update checkpoints and start position of non existed segment", func(t *testing.T) {
|
|
|
|
meta, err := newMeta(memkv.NewMemoryKV())
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
segment1 := &SegmentInfo{SegmentInfo: &datapb.SegmentInfo{ID: 1, State: commonpb.SegmentState_Growing}}
|
|
|
|
err = meta.AddSegment(segment1)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2021-10-19 14:32:41 +08:00
|
|
|
err = meta.UpdateFlushSegmentsInfo(1, false, nil, nil, nil, []*datapb.CheckPoint{{SegmentID: 2, NumOfRows: 10}},
|
|
|
|
|
2021-10-12 23:46:35 +08:00
|
|
|
[]*datapb.SegmentStartPosition{{SegmentID: 2, StartPosition: &internalpb.MsgPosition{MsgID: []byte{1, 2, 3}}}})
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Nil(t, meta.GetSegment(2))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("test save etcd failed", func(t *testing.T) {
|
|
|
|
kv := memkv.NewMemoryKV()
|
|
|
|
failedKv := &saveFailKV{kv}
|
|
|
|
meta, err := newMeta(failedKv)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
segmentInfo := &SegmentInfo{
|
|
|
|
SegmentInfo: &datapb.SegmentInfo{
|
|
|
|
ID: 1,
|
|
|
|
NumOfRows: 0,
|
|
|
|
State: commonpb.SegmentState_Growing,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
meta.segments.SetSegment(1, segmentInfo)
|
|
|
|
|
|
|
|
err = meta.UpdateFlushSegmentsInfo(1, true, []*datapb.FieldBinlog{{FieldID: 1, Binlogs: []string{"binlog"}}},
|
2021-10-19 14:32:41 +08:00
|
|
|
[]*datapb.FieldBinlog{{FieldID: 1, Binlogs: []string{"statslog"}}},
|
|
|
|
[]*datapb.DeltaLogInfo{{RecordEntries: 1, TimestampFrom: 100, TimestampTo: 200, DeltaLogSize: 1000}},
|
2021-10-12 23:46:35 +08:00
|
|
|
[]*datapb.CheckPoint{{SegmentID: 1, NumOfRows: 10}}, []*datapb.SegmentStartPosition{{SegmentID: 1, StartPosition: &internalpb.MsgPosition{MsgID: []byte{1, 2, 3}}}})
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
assert.Equal(t, "mocked fail", err.Error())
|
|
|
|
segmentInfo = meta.GetSegment(1)
|
|
|
|
assert.EqualValues(t, 0, segmentInfo.NumOfRows)
|
|
|
|
assert.Equal(t, commonpb.SegmentState_Growing, segmentInfo.State)
|
|
|
|
assert.Nil(t, segmentInfo.Binlogs)
|
|
|
|
assert.Nil(t, segmentInfo.StartPosition)
|
|
|
|
})
|
|
|
|
}
|
2021-10-24 22:39:09 +08:00
|
|
|
|
|
|
|
func TestSaveHandoffMeta(t *testing.T) {
|
|
|
|
meta, err := newMeta(memkv.NewMemoryKV())
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
info := &datapb.SegmentInfo{
|
|
|
|
ID: 100,
|
|
|
|
State: commonpb.SegmentState_Flushed,
|
|
|
|
}
|
|
|
|
segmentInfo := &SegmentInfo{
|
|
|
|
SegmentInfo: info,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = meta.saveSegmentInfo(segmentInfo)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
keys, _, err := meta.client.LoadWithPrefix(handoffSegmentPrefix)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, 1, len(keys))
|
|
|
|
segmentID, err := strconv.ParseInt(filepath.Base(keys[0]), 10, 64)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, 100, int(segmentID))
|
|
|
|
}
|