2021-04-19 11:35:38 +08:00
|
|
|
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
|
|
|
|
//
|
|
|
|
// 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-01-29 17:08:31 +08:00
|
|
|
package dataservice
|
|
|
|
|
|
|
|
import (
|
2021-03-23 16:57:59 +08:00
|
|
|
"context"
|
2021-01-29 17:08:31 +08:00
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
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-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/msgstream"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/commonpb"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/internalpb"
|
2021-01-29 17:08:31 +08:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDataNodeTTWatcher(t *testing.T) {
|
2021-03-23 16:57:59 +08:00
|
|
|
ctx := context.Background()
|
2021-01-29 17:08:31 +08:00
|
|
|
Params.Init()
|
2021-04-13 09:47:02 +08:00
|
|
|
cluster := newDataNodeCluster()
|
2021-01-29 17:08:31 +08:00
|
|
|
defer cluster.ShutDownClients()
|
|
|
|
schema := newTestSchema()
|
|
|
|
allocator := newMockAllocator()
|
|
|
|
meta, err := newMemoryMeta(allocator)
|
|
|
|
assert.Nil(t, err)
|
2021-02-04 15:23:21 +08:00
|
|
|
segAllocator := newSegmentAllocator(meta, allocator)
|
2021-01-29 17:08:31 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
watcher := newDataNodeTimeTickWatcher(meta, segAllocator, cluster)
|
|
|
|
|
|
|
|
id, err := allocator.allocID()
|
|
|
|
assert.Nil(t, err)
|
2021-04-09 09:55:04 +08:00
|
|
|
err = meta.AddCollection(&datapb.CollectionInfo{
|
2021-01-29 17:08:31 +08:00
|
|
|
Schema: schema,
|
|
|
|
ID: id,
|
|
|
|
})
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
sealed bool
|
|
|
|
allocation bool
|
|
|
|
expired bool
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{false, false, true, false},
|
|
|
|
{false, true, true, false},
|
|
|
|
{false, true, false, false},
|
|
|
|
{true, false, true, true},
|
|
|
|
{true, true, false, false},
|
|
|
|
{true, true, true, true},
|
|
|
|
}
|
|
|
|
|
|
|
|
segmentIDs := make([]UniqueID, len(cases))
|
|
|
|
for i, c := range cases {
|
|
|
|
segID, err := allocator.allocID()
|
|
|
|
segmentIDs[i] = segID
|
|
|
|
assert.Nil(t, err)
|
2021-02-02 18:53:10 +08:00
|
|
|
segmentInfo, err := BuildSegment(id, 100, segID, "channel"+strconv.Itoa(i))
|
2021-01-29 17:08:31 +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-29 17:08:31 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
if c.allocation && c.expired {
|
2021-03-23 16:57:59 +08:00
|
|
|
_, _, _, err := segAllocator.AllocSegment(ctx, id, 100, "channel"+strconv.Itoa(i), 100)
|
2021-01-29 17:08:31 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-02 18:53:10 +08:00
|
|
|
time.Sleep(time.Duration(Params.SegIDAssignExpiration+1000) * time.Millisecond)
|
2021-01-29 17:08:31 +08:00
|
|
|
for i, c := range cases {
|
|
|
|
if c.allocation && !c.expired {
|
2021-03-23 16:57:59 +08:00
|
|
|
_, _, _, err := segAllocator.AllocSegment(ctx, id, 100, "channel"+strconv.Itoa(i), 100)
|
2021-01-29 17:08:31 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
}
|
|
|
|
if c.sealed {
|
2021-03-23 16:57:59 +08:00
|
|
|
err := segAllocator.SealSegment(ctx, segmentIDs[i])
|
2021-01-29 17:08:31 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ts, err := allocator.allocTimestamp()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
err = watcher.handleTimeTickMsg(&msgstream.TimeTickMsg{
|
|
|
|
BaseMsg: msgstream.BaseMsg{
|
|
|
|
HashValues: []uint32{0},
|
|
|
|
},
|
2021-03-12 14:22:09 +08:00
|
|
|
TimeTickMsg: internalpb.TimeTickMsg{
|
2021-01-29 17:08:31 +08:00
|
|
|
Base: &commonpb.MsgBase{
|
2021-03-10 14:45:35 +08:00
|
|
|
MsgType: commonpb.MsgType_TimeTick,
|
2021-01-29 17:08:31 +08:00
|
|
|
Timestamp: ts,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.Nil(t, err)
|
|
|
|
for i, c := range cases {
|
|
|
|
_, ok := segAllocator.segments[segmentIDs[i]]
|
|
|
|
assert.EqualValues(t, !c.expected, ok)
|
|
|
|
}
|
|
|
|
}
|