2023-03-27 00:42:00 +08:00
|
|
|
package segments
|
|
|
|
|
|
|
|
import (
|
2023-05-24 23:03:27 +08:00
|
|
|
"context"
|
2023-03-27 00:42:00 +08:00
|
|
|
"testing"
|
|
|
|
|
2023-04-06 19:14:32 +08:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
2023-06-09 01:28:37 +08:00
|
|
|
"github.com/milvus-io/milvus-proto/go-api/v2/schemapb"
|
2023-11-07 01:44:18 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/datapb"
|
2023-03-27 00:42:00 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/querypb"
|
|
|
|
storage "github.com/milvus-io/milvus/internal/storage"
|
2023-06-25 14:38:44 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/initcore"
|
2023-04-06 19:14:32 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/paramtable"
|
2023-03-27 00:42:00 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type SegmentSuite struct {
|
|
|
|
suite.Suite
|
2023-08-01 10:33:04 +08:00
|
|
|
rootPath string
|
2023-06-25 14:38:44 +08:00
|
|
|
chunkManager storage.ChunkManager
|
2023-03-27 00:42:00 +08:00
|
|
|
|
|
|
|
// Data
|
|
|
|
manager *Manager
|
|
|
|
collectionID int64
|
|
|
|
partitionID int64
|
|
|
|
segmentID int64
|
|
|
|
collection *Collection
|
2023-11-07 01:44:18 +08:00
|
|
|
sealed Segment
|
|
|
|
growing Segment
|
2023-03-27 00:42:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *SegmentSuite) SetupSuite() {
|
|
|
|
paramtable.Init()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *SegmentSuite) SetupTest() {
|
|
|
|
var err error
|
2023-06-25 14:38:44 +08:00
|
|
|
ctx := context.Background()
|
|
|
|
msgLength := 100
|
|
|
|
|
2023-08-01 10:33:04 +08:00
|
|
|
suite.rootPath = suite.T().Name()
|
|
|
|
chunkManagerFactory := NewTestChunkManagerFactory(paramtable.Get(), suite.rootPath)
|
2023-06-25 14:38:44 +08:00
|
|
|
suite.chunkManager, _ = chunkManagerFactory.NewPersistentStorageChunkManager(ctx)
|
|
|
|
initcore.InitRemoteChunkManager(paramtable.Get())
|
|
|
|
|
2023-03-27 00:42:00 +08:00
|
|
|
suite.collectionID = 100
|
|
|
|
suite.partitionID = 10
|
|
|
|
suite.segmentID = 1
|
|
|
|
|
|
|
|
suite.manager = NewManager()
|
2023-04-26 10:14:41 +08:00
|
|
|
schema := GenTestCollectionSchema("test-reduce", schemapb.DataType_Int64)
|
|
|
|
indexMeta := GenTestIndexMeta(suite.collectionID, schema)
|
2023-07-14 10:28:30 +08:00
|
|
|
suite.manager.Collection.PutOrRef(suite.collectionID,
|
2023-04-26 10:14:41 +08:00
|
|
|
schema,
|
|
|
|
indexMeta,
|
2023-03-27 00:42:00 +08:00
|
|
|
&querypb.LoadMetaInfo{
|
|
|
|
LoadType: querypb.LoadType_LoadCollection,
|
|
|
|
CollectionID: suite.collectionID,
|
|
|
|
PartitionIDs: []int64{suite.partitionID},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
suite.collection = suite.manager.Collection.Get(suite.collectionID)
|
|
|
|
|
|
|
|
suite.sealed, err = NewSegment(suite.collection,
|
|
|
|
suite.segmentID,
|
|
|
|
suite.partitionID,
|
|
|
|
suite.collectionID,
|
|
|
|
"dml",
|
|
|
|
SegmentTypeSealed,
|
|
|
|
0,
|
|
|
|
nil,
|
|
|
|
nil,
|
2023-11-07 01:44:18 +08:00
|
|
|
datapb.SegmentLevel_Legacy,
|
2023-03-27 00:42:00 +08:00
|
|
|
)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
2023-06-25 14:38:44 +08:00
|
|
|
binlogs, _, err := SaveBinLog(ctx,
|
|
|
|
suite.collectionID,
|
|
|
|
suite.partitionID,
|
|
|
|
suite.segmentID,
|
|
|
|
msgLength,
|
|
|
|
schema,
|
|
|
|
suite.chunkManager,
|
|
|
|
)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
for _, binlog := range binlogs {
|
2023-11-07 01:44:18 +08:00
|
|
|
err = suite.sealed.(*LocalSegment).LoadFieldData(binlog.FieldID, int64(msgLength), binlog, false)
|
2023-06-25 14:38:44 +08:00
|
|
|
suite.Require().NoError(err)
|
|
|
|
}
|
|
|
|
|
2023-03-27 00:42:00 +08:00
|
|
|
suite.growing, err = NewSegment(suite.collection,
|
|
|
|
suite.segmentID+1,
|
|
|
|
suite.partitionID,
|
|
|
|
suite.collectionID,
|
|
|
|
"dml",
|
|
|
|
SegmentTypeGrowing,
|
|
|
|
0,
|
|
|
|
nil,
|
|
|
|
nil,
|
2023-11-07 01:44:18 +08:00
|
|
|
datapb.SegmentLevel_Legacy,
|
2023-03-27 00:42:00 +08:00
|
|
|
)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
2023-11-07 01:44:18 +08:00
|
|
|
insertMsg, err := genInsertMsg(suite.collection, suite.partitionID, suite.growing.ID(), msgLength)
|
2023-03-27 00:42:00 +08:00
|
|
|
suite.Require().NoError(err)
|
2023-06-25 14:38:44 +08:00
|
|
|
insertRecord, err := storage.TransferInsertMsgToInsertRecord(suite.collection.Schema(), insertMsg)
|
2023-03-27 00:42:00 +08:00
|
|
|
suite.Require().NoError(err)
|
2023-04-21 11:46:32 +08:00
|
|
|
err = suite.growing.Insert(insertMsg.RowIDs, insertMsg.Timestamps, insertRecord)
|
|
|
|
suite.Require().NoError(err)
|
2023-03-27 00:42:00 +08:00
|
|
|
|
|
|
|
suite.manager.Segment.Put(SegmentTypeSealed, suite.sealed)
|
|
|
|
suite.manager.Segment.Put(SegmentTypeGrowing, suite.growing)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *SegmentSuite) TearDownTest() {
|
2023-06-25 14:38:44 +08:00
|
|
|
ctx := context.Background()
|
2023-09-09 10:35:16 +08:00
|
|
|
suite.sealed.Release()
|
|
|
|
suite.growing.Release()
|
2023-03-27 00:42:00 +08:00
|
|
|
DeleteCollection(suite.collection)
|
2023-08-01 10:33:04 +08:00
|
|
|
suite.chunkManager.RemoveWithPrefix(ctx, suite.rootPath)
|
2023-03-27 00:42:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *SegmentSuite) TestDelete() {
|
|
|
|
pks, err := storage.GenInt64PrimaryKeys(0, 1)
|
|
|
|
suite.NoError(err)
|
|
|
|
|
|
|
|
// Test for sealed
|
|
|
|
rowNum := suite.sealed.RowNum()
|
|
|
|
err = suite.sealed.Delete(pks, []uint64{1000, 1000})
|
|
|
|
suite.NoError(err)
|
|
|
|
|
|
|
|
suite.Equal(rowNum-int64(len(pks)), suite.sealed.RowNum())
|
|
|
|
suite.Equal(rowNum, suite.sealed.InsertCount())
|
|
|
|
|
|
|
|
// Test for growing
|
|
|
|
rowNum = suite.growing.RowNum()
|
|
|
|
err = suite.growing.Delete(pks, []uint64{1000, 1000})
|
|
|
|
suite.NoError(err)
|
|
|
|
|
|
|
|
suite.Equal(rowNum-int64(len(pks)), suite.growing.RowNum())
|
|
|
|
suite.Equal(rowNum, suite.growing.InsertCount())
|
|
|
|
}
|
|
|
|
|
2023-04-23 09:00:32 +08:00
|
|
|
func (suite *SegmentSuite) TestHasRawData() {
|
|
|
|
has := suite.growing.HasRawData(simpleFloatVecField.id)
|
|
|
|
suite.True(has)
|
|
|
|
has = suite.sealed.HasRawData(simpleFloatVecField.id)
|
|
|
|
suite.True(has)
|
|
|
|
}
|
|
|
|
|
2023-08-29 15:46:27 +08:00
|
|
|
func (suite *SegmentSuite) TestCASVersion() {
|
|
|
|
segment := suite.sealed
|
|
|
|
|
|
|
|
curVersion := segment.Version()
|
|
|
|
suite.False(segment.CASVersion(curVersion-1, curVersion+1))
|
|
|
|
suite.NotEqual(curVersion+1, segment.Version())
|
|
|
|
|
|
|
|
suite.True(segment.CASVersion(curVersion, curVersion+1))
|
|
|
|
suite.Equal(curVersion+1, segment.Version())
|
|
|
|
}
|
|
|
|
|
2023-05-26 10:03:27 +08:00
|
|
|
func (suite *SegmentSuite) TestSegmentReleased() {
|
2023-09-09 10:35:16 +08:00
|
|
|
suite.sealed.Release()
|
2023-05-26 10:03:27 +08:00
|
|
|
|
2023-11-07 01:44:18 +08:00
|
|
|
sealed := suite.sealed.(*LocalSegment)
|
|
|
|
|
|
|
|
sealed.ptrLock.RLock()
|
|
|
|
suite.False(sealed.isValid())
|
|
|
|
sealed.ptrLock.RUnlock()
|
|
|
|
suite.EqualValues(0, sealed.InsertCount())
|
|
|
|
suite.EqualValues(0, sealed.RowNum())
|
|
|
|
suite.EqualValues(0, sealed.MemSize())
|
|
|
|
suite.False(sealed.HasRawData(101))
|
2023-05-26 10:03:27 +08:00
|
|
|
}
|
|
|
|
|
2023-03-27 00:42:00 +08:00
|
|
|
func TestSegment(t *testing.T) {
|
|
|
|
suite.Run(t, new(SegmentSuite))
|
|
|
|
}
|