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/msgstream"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/commonpb"
|
2021-05-10 17:03:24 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/datapb"
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/internalpb"
|
2021-01-29 17:08:31 +08:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2021-04-23 10:15:30 +08:00
|
|
|
func TestWatcher(t *testing.T) {
|
|
|
|
const collID = UniqueID(0)
|
|
|
|
const partID = UniqueID(100)
|
|
|
|
|
2021-01-29 17:08:31 +08:00
|
|
|
Params.Init()
|
2021-04-23 10:15:30 +08:00
|
|
|
|
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)
|
|
|
|
|
2021-04-23 10:15:30 +08:00
|
|
|
collInfo := &datapb.CollectionInfo{
|
2021-01-29 17:08:31 +08:00
|
|
|
Schema: schema,
|
2021-04-23 10:15:30 +08:00
|
|
|
ID: collID,
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("Test DataNodeTimeTickWatcher", func(t *testing.T) {
|
|
|
|
datanodeWatcher := newDataNodeTimeTickWatcher(meta, segAllocator, cluster)
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
go datanodeWatcher.StartBackgroundLoop(ctx)
|
2021-01-29 17:08:31 +08:00
|
|
|
|
2021-04-23 10:15:30 +08:00
|
|
|
err = meta.AddCollection(collInfo)
|
2021-01-29 17:08:31 +08:00
|
|
|
assert.Nil(t, err)
|
2021-04-23 10:15:30 +08:00
|
|
|
|
|
|
|
cases := []struct {
|
2021-05-10 17:03:24 +08:00
|
|
|
sealed bool
|
|
|
|
expired bool
|
|
|
|
expected bool
|
2021-04-23 10:15:30 +08:00
|
|
|
}{
|
2021-05-10 17:03:24 +08:00
|
|
|
{false, true, false},
|
|
|
|
{false, true, false},
|
|
|
|
{false, false, false},
|
|
|
|
{true, true, true},
|
|
|
|
{true, false, false},
|
|
|
|
{true, true, true},
|
2021-01-29 17:08:31 +08:00
|
|
|
}
|
|
|
|
|
2021-05-10 17:03:24 +08:00
|
|
|
segIDs := make([]UniqueID, 0)
|
|
|
|
for i := range cases {
|
|
|
|
segID, _, _, err := segAllocator.AllocSegment(ctx, collID, partID, "channel"+strconv.Itoa(i), 100)
|
2021-04-23 10:15:30 +08:00
|
|
|
assert.Nil(t, err)
|
2021-05-10 17:03:24 +08:00
|
|
|
segIDs = append(segIDs, segID)
|
2021-01-29 17:08:31 +08:00
|
|
|
}
|
|
|
|
|
2021-04-23 10:15:30 +08:00
|
|
|
time.Sleep(time.Duration(Params.SegIDAssignExpiration+1000) * time.Millisecond)
|
|
|
|
for i, c := range cases {
|
2021-05-10 17:03:24 +08:00
|
|
|
if !c.expired {
|
2021-04-23 10:15:30 +08:00
|
|
|
_, _, _, err := segAllocator.AllocSegment(ctx, collID, partID, "channel"+strconv.Itoa(i), 100)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
}
|
|
|
|
if c.sealed {
|
|
|
|
err := segAllocator.SealSegment(ctx, segIDs[i])
|
|
|
|
assert.Nil(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ts, err := allocator.allocTimestamp()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
ttMsg := &msgstream.TimeTickMsg{
|
|
|
|
BaseMsg: msgstream.BaseMsg{
|
|
|
|
HashValues: []uint32{0},
|
2021-01-29 17:08:31 +08:00
|
|
|
},
|
2021-04-23 10:15:30 +08:00
|
|
|
TimeTickMsg: internalpb.TimeTickMsg{
|
|
|
|
Base: &commonpb.MsgBase{
|
|
|
|
MsgType: commonpb.MsgType_TimeTick,
|
|
|
|
Timestamp: ts,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
datanodeWatcher.Watch(ttMsg)
|
|
|
|
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
|
|
|
// check flushed segments been removed from segAllocator
|
|
|
|
for i, c := range cases {
|
|
|
|
ok := segAllocator.HasSegment(ctx, segIDs[i])
|
|
|
|
assert.EqualValues(t, !c.expected, ok)
|
|
|
|
}
|
2021-01-29 17:08:31 +08:00
|
|
|
})
|
|
|
|
}
|