2021-01-19 11:37:16 +08:00
|
|
|
package datanode
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2021-03-16 17:55:42 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/internalpb"
|
2021-01-19 11:37:16 +08:00
|
|
|
)
|
|
|
|
|
2021-01-28 15:20:24 +08:00
|
|
|
func TestReplica_Collection(t *testing.T) {
|
|
|
|
Factory := &MetaFactory{}
|
2021-02-04 20:31:23 +08:00
|
|
|
collID := UniqueID(100)
|
|
|
|
collMetaMock := Factory.CollectionMetaFactory(collID, "test-coll-name-0")
|
2021-01-28 15:20:24 +08:00
|
|
|
|
2021-02-04 20:31:23 +08:00
|
|
|
t.Run("get_collection_num", func(t *testing.T) {
|
2021-01-28 15:20:24 +08:00
|
|
|
replica := newReplica()
|
2021-02-04 20:31:23 +08:00
|
|
|
assert.Zero(t, replica.getCollectionNum())
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
replica = new(CollectionSegmentReplica)
|
2021-02-04 20:31:23 +08:00
|
|
|
assert.Zero(t, replica.getCollectionNum())
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
replica = &CollectionSegmentReplica{
|
2021-02-04 20:31:23 +08:00
|
|
|
collections: map[UniqueID]*Collection{
|
|
|
|
0: {id: 0},
|
|
|
|
1: {id: 1},
|
|
|
|
2: {id: 2},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.Equal(t, 3, replica.getCollectionNum())
|
|
|
|
})
|
2021-01-28 15:20:24 +08:00
|
|
|
|
2021-02-04 20:31:23 +08:00
|
|
|
t.Run("add_collection", func(t *testing.T) {
|
|
|
|
replica := newReplica()
|
|
|
|
require.Zero(t, replica.getCollectionNum())
|
2021-01-28 15:20:24 +08:00
|
|
|
|
2021-02-04 20:31:23 +08:00
|
|
|
err := replica.addCollection(collID, nil)
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Zero(t, replica.getCollectionNum())
|
2021-01-28 15:20:24 +08:00
|
|
|
|
2021-02-04 20:31:23 +08:00
|
|
|
err = replica.addCollection(collID, collMetaMock.Schema)
|
2021-01-28 15:20:24 +08:00
|
|
|
assert.NoError(t, err)
|
2021-02-04 20:31:23 +08:00
|
|
|
assert.Equal(t, 1, replica.getCollectionNum())
|
|
|
|
assert.True(t, replica.hasCollection(collID))
|
|
|
|
coll, err := replica.getCollectionByID(collID)
|
2021-01-28 15:20:24 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, coll)
|
2021-02-04 20:31:23 +08:00
|
|
|
assert.Equal(t, collID, coll.GetID())
|
|
|
|
assert.Equal(t, collMetaMock.Schema.GetName(), coll.GetName())
|
|
|
|
assert.Equal(t, collMetaMock.Schema, coll.GetSchema())
|
2021-01-28 15:20:24 +08:00
|
|
|
|
2021-02-04 20:31:23 +08:00
|
|
|
sameID := collID
|
|
|
|
otherSchema := Factory.CollectionMetaFactory(sameID, "test-coll-name-1").GetSchema()
|
|
|
|
err = replica.addCollection(sameID, otherSchema)
|
|
|
|
assert.Error(t, err)
|
2021-01-28 15:20:24 +08:00
|
|
|
|
|
|
|
})
|
|
|
|
|
2021-02-04 20:31:23 +08:00
|
|
|
t.Run("remove_collection", func(t *testing.T) {
|
2021-01-28 15:20:24 +08:00
|
|
|
replica := newReplica()
|
2021-02-04 20:31:23 +08:00
|
|
|
require.False(t, replica.hasCollection(collID))
|
|
|
|
require.Zero(t, replica.getCollectionNum())
|
2021-01-28 15:20:24 +08:00
|
|
|
|
2021-02-04 20:31:23 +08:00
|
|
|
err := replica.removeCollection(collID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = replica.addCollection(collID, collMetaMock.Schema)
|
2021-01-28 15:20:24 +08:00
|
|
|
require.NoError(t, err)
|
2021-02-04 20:31:23 +08:00
|
|
|
require.True(t, replica.hasCollection(collID))
|
|
|
|
require.Equal(t, 1, replica.getCollectionNum())
|
2021-01-28 15:20:24 +08:00
|
|
|
|
2021-02-04 20:31:23 +08:00
|
|
|
err = replica.removeCollection(collID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, replica.hasCollection(collID))
|
|
|
|
assert.Zero(t, replica.getCollectionNum())
|
|
|
|
err = replica.removeCollection(collID)
|
2021-01-28 15:20:24 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
2021-02-04 20:31:23 +08:00
|
|
|
t.Run("get_collection_by_id", func(t *testing.T) {
|
2021-01-28 15:20:24 +08:00
|
|
|
replica := newReplica()
|
2021-02-04 20:31:23 +08:00
|
|
|
require.False(t, replica.hasCollection(collID))
|
2021-01-28 15:20:24 +08:00
|
|
|
|
2021-02-04 20:31:23 +08:00
|
|
|
coll, err := replica.getCollectionByID(collID)
|
2021-01-28 15:20:24 +08:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Nil(t, coll)
|
|
|
|
|
2021-02-04 20:31:23 +08:00
|
|
|
err = replica.addCollection(collID, collMetaMock.Schema)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, replica.hasCollection(collID))
|
|
|
|
require.Equal(t, 1, replica.getCollectionNum())
|
2021-01-28 15:20:24 +08:00
|
|
|
|
2021-02-04 20:31:23 +08:00
|
|
|
coll, err = replica.getCollectionByID(collID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, coll)
|
|
|
|
assert.Equal(t, collID, coll.GetID())
|
|
|
|
assert.Equal(t, collMetaMock.Schema.GetName(), coll.GetName())
|
|
|
|
assert.Equal(t, collMetaMock.Schema, coll.GetSchema())
|
2021-01-28 15:20:24 +08:00
|
|
|
})
|
2021-01-19 11:37:16 +08:00
|
|
|
}
|
|
|
|
|
2021-01-28 15:20:24 +08:00
|
|
|
func TestReplica_Segment(t *testing.T) {
|
|
|
|
t.Run("Test segment", func(t *testing.T) {
|
|
|
|
replica := newReplica()
|
|
|
|
assert.False(t, replica.hasSegment(0))
|
2021-01-19 11:37:16 +08:00
|
|
|
|
2021-02-04 11:19:48 +08:00
|
|
|
err := replica.addSegment(0, 1, 2, "insert-01")
|
2021-01-28 15:20:24 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, replica.hasSegment(0))
|
2021-01-19 11:37:16 +08:00
|
|
|
|
2021-01-28 15:20:24 +08:00
|
|
|
seg, err := replica.getSegmentByID(0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, seg)
|
|
|
|
assert.Equal(t, UniqueID(1), seg.collectionID)
|
|
|
|
assert.Equal(t, UniqueID(2), seg.partitionID)
|
2021-01-19 11:37:16 +08:00
|
|
|
|
2021-01-28 15:20:24 +08:00
|
|
|
assert.Equal(t, int64(0), seg.numRows)
|
2021-01-19 11:37:16 +08:00
|
|
|
|
2021-01-28 15:20:24 +08:00
|
|
|
err = replica.updateStatistics(0, 100)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(100), seg.numRows)
|
2021-01-19 11:37:16 +08:00
|
|
|
|
2021-01-28 15:20:24 +08:00
|
|
|
update, err := replica.getSegmentStatisticsUpdates(0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, UniqueID(0), update.SegmentID)
|
|
|
|
assert.Equal(t, int64(100), update.NumRows)
|
2021-03-16 17:55:42 +08:00
|
|
|
assert.NotNil(t, update.StartPosition)
|
|
|
|
assert.Nil(t, update.EndPosition)
|
2021-03-02 15:47:37 +08:00
|
|
|
|
2021-03-16 17:55:42 +08:00
|
|
|
err = replica.setIsFlushed(0)
|
|
|
|
assert.NoError(t, err)
|
2021-04-19 10:34:56 +08:00
|
|
|
err = replica.setStartPosition(0, &internalpb.MsgPosition{})
|
|
|
|
assert.NoError(t, err)
|
2021-03-16 17:55:42 +08:00
|
|
|
err = replica.setEndPosition(0, &internalpb.MsgPosition{})
|
|
|
|
assert.NoError(t, err)
|
2021-03-02 15:47:37 +08:00
|
|
|
update, err = replica.getSegmentStatisticsUpdates(0)
|
|
|
|
assert.NoError(t, err)
|
2021-03-16 17:55:42 +08:00
|
|
|
assert.Nil(t, update.StartPosition)
|
|
|
|
assert.NotNil(t, update.EndPosition)
|
2021-04-19 10:34:56 +08:00
|
|
|
|
|
|
|
err = replica.removeSegment(0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, replica.hasSegment(0))
|
2021-01-28 15:20:24 +08:00
|
|
|
})
|
2021-01-19 11:37:16 +08:00
|
|
|
|
2021-01-28 15:20:24 +08:00
|
|
|
t.Run("Test errors", func(t *testing.T) {
|
|
|
|
replica := newReplica()
|
|
|
|
require.False(t, replica.hasSegment(0))
|
2021-01-19 11:37:16 +08:00
|
|
|
|
2021-01-28 15:20:24 +08:00
|
|
|
seg, err := replica.getSegmentByID(0)
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Nil(t, seg)
|
2021-01-19 11:37:16 +08:00
|
|
|
|
2021-04-19 10:34:56 +08:00
|
|
|
err = replica.setIsFlushed(0)
|
|
|
|
assert.Error(t, err)
|
|
|
|
err = replica.setStartPosition(0, &internalpb.MsgPosition{})
|
|
|
|
assert.Error(t, err)
|
|
|
|
err = replica.setStartPosition(0, nil)
|
|
|
|
assert.Error(t, err)
|
|
|
|
err = replica.setEndPosition(0, &internalpb.MsgPosition{})
|
|
|
|
assert.Error(t, err)
|
|
|
|
err = replica.setEndPosition(0, nil)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
2021-01-28 15:20:24 +08:00
|
|
|
err = replica.updateStatistics(0, 0)
|
|
|
|
assert.Error(t, err)
|
2021-01-19 11:37:16 +08:00
|
|
|
|
2021-01-28 15:20:24 +08:00
|
|
|
update, err := replica.getSegmentStatisticsUpdates(0)
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Nil(t, update)
|
|
|
|
})
|
2021-01-19 11:37:16 +08:00
|
|
|
}
|