2021-01-22 19:43:27 +08:00
|
|
|
package dataservice
|
|
|
|
|
|
|
|
import (
|
2021-03-23 16:57:59 +08:00
|
|
|
"context"
|
2021-02-02 18:53:10 +08:00
|
|
|
"log"
|
2021-01-22 19:43:27 +08:00
|
|
|
"math"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-04-09 09:55:04 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/datapb"
|
|
|
|
|
2021-02-02 18:53:10 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/util/tsoutil"
|
|
|
|
|
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-02-04 15:23:21 +08:00
|
|
|
segAllocator := newSegmentAllocator(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)
|
2021-01-25 15:17:17 +08:00
|
|
|
id, err := mockAllocator.allocID()
|
|
|
|
assert.Nil(t, err)
|
2021-02-02 18:53:10 +08:00
|
|
|
segmentInfo, err := BuildSegment(collID, 100, id, "c1")
|
2021-01-22 19:43:27 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
err = meta.AddSegment(segmentInfo)
|
|
|
|
assert.Nil(t, err)
|
2021-03-23 16:57:59 +08:00
|
|
|
err = segAllocator.OpenSegment(ctx, segmentInfo)
|
2021-01-22 19:43:27 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
collectionID UniqueID
|
|
|
|
partitionID UniqueID
|
|
|
|
channelName string
|
|
|
|
requestRows int
|
|
|
|
expectResult bool
|
|
|
|
}{
|
|
|
|
{collID, 100, "c1", 100, true},
|
|
|
|
{collID, 100, "c1", math.MaxInt64, false},
|
|
|
|
}
|
|
|
|
for _, c := range cases {
|
2021-03-23 16:57:59 +08:00
|
|
|
id, count, expireTime, err := segAllocator.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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSealSegment(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-02-04 15:23:21 +08:00
|
|
|
segAllocator := newSegmentAllocator(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)
|
|
|
|
var lastSegID UniqueID
|
|
|
|
for i := 0; i < 10; i++ {
|
2021-01-25 15:17:17 +08:00
|
|
|
id, err := mockAllocator.allocID()
|
|
|
|
assert.Nil(t, err)
|
2021-02-02 18:53:10 +08:00
|
|
|
segmentInfo, err := BuildSegment(collID, 100, id, "c"+strconv.Itoa(i))
|
2021-01-22 19:43:27 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
err = meta.AddSegment(segmentInfo)
|
|
|
|
assert.Nil(t, err)
|
2021-03-23 16:57:59 +08:00
|
|
|
err = segAllocator.OpenSegment(ctx, segmentInfo)
|
2021-01-22 19:43:27 +08:00
|
|
|
assert.Nil(t, err)
|
2021-04-09 09:55:04 +08:00
|
|
|
lastSegID = segmentInfo.ID
|
2021-01-22 19:43:27 +08:00
|
|
|
}
|
|
|
|
|
2021-03-23 16:57:59 +08:00
|
|
|
err = segAllocator.SealSegment(ctx, lastSegID)
|
2021-01-22 19:43:27 +08:00
|
|
|
assert.Nil(t, err)
|
2021-03-23 16:57:59 +08:00
|
|
|
segAllocator.SealAllSegments(ctx, collID)
|
|
|
|
sealedSegments, err := segAllocator.GetSealedSegments(ctx)
|
2021-01-22 19:43:27 +08:00
|
|
|
assert.Nil(t, err)
|
2021-01-23 14:41:29 +08:00
|
|
|
assert.EqualValues(t, 10, len(sealedSegments))
|
2021-01-22 19:43:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestExpireSegment(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-02-04 15:23:21 +08:00
|
|
|
segAllocator := newSegmentAllocator(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)
|
2021-01-25 15:17:17 +08:00
|
|
|
id, err := mockAllocator.allocID()
|
|
|
|
assert.Nil(t, err)
|
2021-02-02 18:53:10 +08:00
|
|
|
segmentInfo, err := BuildSegment(collID, 100, id, "c1")
|
2021-01-22 19:43:27 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
err = meta.AddSegment(segmentInfo)
|
|
|
|
assert.Nil(t, err)
|
2021-03-23 16:57:59 +08:00
|
|
|
err = segAllocator.OpenSegment(ctx, segmentInfo)
|
2021-01-22 19:43:27 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2021-03-23 16:57:59 +08:00
|
|
|
id1, _, et, err := segAllocator.AllocSegment(ctx, collID, 100, "c1", 10)
|
2021-02-02 18:53:10 +08:00
|
|
|
ts2, _ := tsoutil.ParseTS(et)
|
|
|
|
log.Printf("physical ts: %s", ts2.String())
|
2021-01-22 19:43:27 +08:00
|
|
|
assert.Nil(t, err)
|
2021-02-02 18:53:10 +08:00
|
|
|
|
2021-01-22 19:43:27 +08:00
|
|
|
ts, err := mockAllocator.allocTimestamp()
|
|
|
|
assert.Nil(t, err)
|
2021-02-02 18:53:10 +08:00
|
|
|
t1, _ := tsoutil.ParseTS(ts)
|
|
|
|
log.Printf("before ts: %s", t1.String())
|
|
|
|
time.Sleep(time.Duration(Params.SegIDAssignExpiration+1000) * time.Millisecond)
|
|
|
|
ts, err = mockAllocator.allocTimestamp()
|
|
|
|
assert.Nil(t, err)
|
2021-03-23 16:57:59 +08:00
|
|
|
err = segAllocator.ExpireAllocations(ctx, ts)
|
2021-01-22 19:43:27 +08:00
|
|
|
assert.Nil(t, err)
|
2021-03-23 16:57:59 +08:00
|
|
|
expired, err := segAllocator.IsAllocationsExpired(ctx, id1, ts)
|
2021-02-02 18:53:10 +08:00
|
|
|
if et > ts {
|
|
|
|
tsPhy, _ := tsoutil.ParseTS(ts)
|
|
|
|
log.Printf("ts %s", tsPhy.String())
|
|
|
|
}
|
2021-01-22 19:43:27 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.True(t, expired)
|
|
|
|
assert.EqualValues(t, 0, len(segAllocator.segments[id1].allocations))
|
|
|
|
}
|