2021-04-19 15:16:33 +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-19 11:37:16 +08:00
|
|
|
package datanode
|
|
|
|
|
|
|
|
import (
|
2021-06-11 17:53:37 +08:00
|
|
|
"context"
|
2021-08-13 10:50:09 +08:00
|
|
|
"fmt"
|
2021-05-25 15:35:37 +08:00
|
|
|
"math"
|
2021-08-13 10:50:09 +08:00
|
|
|
"math/rand"
|
2021-05-25 15:35:37 +08:00
|
|
|
"os"
|
2021-08-13 10:50:09 +08:00
|
|
|
"strings"
|
2021-01-19 11:37:16 +08:00
|
|
|
"testing"
|
2021-06-07 11:25:37 +08:00
|
|
|
"time"
|
2021-01-19 11:37:16 +08:00
|
|
|
|
2021-08-13 10:50:09 +08:00
|
|
|
"github.com/golang/protobuf/proto"
|
2021-04-21 18:41:37 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-05-25 15:35:37 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-08-13 10:50:09 +08:00
|
|
|
"go.etcd.io/etcd/clientv3"
|
2021-05-25 15:35:37 +08:00
|
|
|
"go.uber.org/zap"
|
2021-03-05 20:41:34 +08:00
|
|
|
|
2021-08-13 10:50:09 +08:00
|
|
|
etcdkv "github.com/milvus-io/milvus/internal/kv/etcd"
|
2021-05-25 15:35:37 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/log"
|
|
|
|
"github.com/milvus-io/milvus/internal/msgstream"
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/commonpb"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/datapb"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/internalpb"
|
2021-01-19 11:37:16 +08:00
|
|
|
)
|
|
|
|
|
2021-05-18 19:45:00 +08:00
|
|
|
func TestMain(t *testing.M) {
|
2021-08-13 10:50:09 +08:00
|
|
|
rand.Seed(time.Now().Unix())
|
2021-06-19 12:38:06 +08:00
|
|
|
Params.InitAlias("datanode-alias-1")
|
2021-01-19 11:37:16 +08:00
|
|
|
Params.Init()
|
|
|
|
refreshChannelNames()
|
2021-05-25 15:35:37 +08:00
|
|
|
code := t.Run()
|
|
|
|
os.Exit(code)
|
2021-05-18 19:45:00 +08:00
|
|
|
}
|
2021-02-03 17:30:10 +08:00
|
|
|
|
2021-05-18 19:45:00 +08:00
|
|
|
func TestDataNode(t *testing.T) {
|
2021-06-11 17:53:37 +08:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
node := newIDLEDataNodeMock(ctx)
|
2021-07-08 15:52:40 +08:00
|
|
|
node.Init()
|
2021-04-21 18:41:37 +08:00
|
|
|
node.Start()
|
2021-06-16 19:03:57 +08:00
|
|
|
node.Register()
|
2021-02-03 17:30:10 +08:00
|
|
|
|
2021-04-21 18:41:37 +08:00
|
|
|
t.Run("Test WatchDmChannels", func(t *testing.T) {
|
2021-06-18 16:02:05 +08:00
|
|
|
t.Skip()
|
2021-06-11 17:53:37 +08:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
node1 := newIDLEDataNodeMock(ctx)
|
2021-05-25 15:35:37 +08:00
|
|
|
node1.Start()
|
2021-06-04 09:57:54 +08:00
|
|
|
vchannels := []*datapb.VchannelInfo{}
|
2021-06-08 19:25:37 +08:00
|
|
|
for _, ch := range []string{"datanode-01-test-WatchDmChannel",
|
|
|
|
"datanode-02-test-WatchDmChannels"} {
|
2021-05-25 15:35:37 +08:00
|
|
|
log.Debug("InsertChannels", zap.String("name", ch))
|
2021-06-04 09:57:54 +08:00
|
|
|
vchan := &datapb.VchannelInfo{
|
2021-06-08 19:25:37 +08:00
|
|
|
CollectionID: 1,
|
|
|
|
ChannelName: ch,
|
|
|
|
UnflushedSegments: []*datapb.SegmentInfo{},
|
2021-05-25 15:35:37 +08:00
|
|
|
}
|
2021-06-04 09:57:54 +08:00
|
|
|
vchannels = append(vchannels, vchan)
|
2021-05-25 15:35:37 +08:00
|
|
|
}
|
2021-04-21 18:41:37 +08:00
|
|
|
req := &datapb.WatchDmChannelsRequest{
|
2021-05-25 15:35:37 +08:00
|
|
|
Base: &commonpb.MsgBase{
|
|
|
|
MsgType: 0,
|
|
|
|
MsgID: 0,
|
|
|
|
Timestamp: 0,
|
|
|
|
SourceID: Params.NodeID,
|
|
|
|
},
|
|
|
|
Vchannels: vchannels,
|
2021-02-03 17:30:10 +08:00
|
|
|
}
|
2021-04-21 18:41:37 +08:00
|
|
|
|
2021-05-25 15:35:37 +08:00
|
|
|
_, err := node1.WatchDmChannels(node.ctx, req)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, node1.vchan2FlushCh)
|
|
|
|
assert.NotNil(t, node1.vchan2SyncService)
|
2021-06-08 19:25:37 +08:00
|
|
|
sync, ok := node1.vchan2SyncService["datanode-01-test-WatchDmChannel"]
|
2021-05-25 15:35:37 +08:00
|
|
|
assert.True(t, ok)
|
|
|
|
assert.NotNil(t, sync)
|
|
|
|
assert.Equal(t, UniqueID(1), sync.collectionID)
|
|
|
|
assert.Equal(t, 2, len(node1.vchan2SyncService))
|
|
|
|
assert.Equal(t, len(node1.vchan2FlushCh), len(node1.vchan2SyncService))
|
|
|
|
|
|
|
|
_, err = node1.WatchDmChannels(node1.ctx, req)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 2, len(node1.vchan2SyncService))
|
2021-04-21 18:41:37 +08:00
|
|
|
|
2021-06-11 17:53:37 +08:00
|
|
|
cancel()
|
2021-05-25 15:35:37 +08:00
|
|
|
<-node1.ctx.Done()
|
|
|
|
node1.Stop()
|
2021-04-21 18:41:37 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Test GetComponentStates", func(t *testing.T) {
|
|
|
|
stat, err := node.GetComponentStates(node.ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, commonpb.ErrorCode_Success, stat.Status.ErrorCode)
|
|
|
|
})
|
|
|
|
|
2021-05-25 15:35:37 +08:00
|
|
|
t.Run("Test NewDataSyncService", func(t *testing.T) {
|
2021-06-18 16:02:05 +08:00
|
|
|
t.Skip()
|
2021-06-11 17:53:37 +08:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
node2 := newIDLEDataNodeMock(ctx)
|
2021-05-25 15:35:37 +08:00
|
|
|
node2.Start()
|
|
|
|
dmChannelName := "fake-dm-channel-test-NewDataSyncService"
|
2021-06-04 09:57:54 +08:00
|
|
|
|
|
|
|
vchan := &datapb.VchannelInfo{
|
2021-06-08 19:25:37 +08:00
|
|
|
CollectionID: 1,
|
|
|
|
ChannelName: dmChannelName,
|
|
|
|
UnflushedSegments: []*datapb.SegmentInfo{},
|
2021-05-25 15:35:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
require.Equal(t, 0, len(node2.vchan2FlushCh))
|
|
|
|
require.Equal(t, 0, len(node2.vchan2SyncService))
|
|
|
|
|
2021-06-04 09:57:54 +08:00
|
|
|
err := node2.NewDataSyncService(vchan)
|
2021-05-25 15:35:37 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, len(node2.vchan2FlushCh))
|
|
|
|
assert.Equal(t, 1, len(node2.vchan2SyncService))
|
|
|
|
|
2021-06-04 09:57:54 +08:00
|
|
|
err = node2.NewDataSyncService(vchan)
|
2021-05-25 15:35:37 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, len(node2.vchan2FlushCh))
|
|
|
|
assert.Equal(t, 1, len(node2.vchan2SyncService))
|
2021-06-11 17:53:37 +08:00
|
|
|
|
|
|
|
cancel()
|
2021-05-25 15:35:37 +08:00
|
|
|
<-node2.ctx.Done()
|
|
|
|
node2.Stop()
|
|
|
|
})
|
|
|
|
|
2021-04-21 18:41:37 +08:00
|
|
|
t.Run("Test FlushSegments", func(t *testing.T) {
|
2021-06-11 17:53:37 +08:00
|
|
|
t.Skipf("Fix latter")
|
2021-05-25 15:35:37 +08:00
|
|
|
dmChannelName := "fake-dm-channel-test-HEALTHDataNodeMock"
|
|
|
|
|
2021-06-07 11:25:37 +08:00
|
|
|
node1 := newHEALTHDataNodeMock(dmChannelName)
|
2021-05-25 15:35:37 +08:00
|
|
|
|
|
|
|
sync, ok := node1.vchan2SyncService[dmChannelName]
|
|
|
|
assert.True(t, ok)
|
2021-06-21 16:00:22 +08:00
|
|
|
sync.replica.addNewSegment(0, 1, 1, dmChannelName, nil, nil)
|
2021-05-25 15:35:37 +08:00
|
|
|
|
2021-04-21 18:41:37 +08:00
|
|
|
req := &datapb.FlushSegmentsRequest{
|
|
|
|
Base: &commonpb.MsgBase{},
|
|
|
|
DbID: 0,
|
|
|
|
CollectionID: 1,
|
2021-05-27 18:45:24 +08:00
|
|
|
SegmentIDs: []int64{0},
|
2021-02-03 17:30:10 +08:00
|
|
|
}
|
2021-05-27 18:45:24 +08:00
|
|
|
|
2021-05-29 18:04:30 +08:00
|
|
|
status, err := node1.FlushSegments(node1.ctx, req)
|
2021-04-21 18:41:37 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, commonpb.ErrorCode_Success, status.ErrorCode)
|
2021-05-25 15:35:37 +08:00
|
|
|
|
|
|
|
timeTickMsgPack := msgstream.MsgPack{}
|
|
|
|
|
|
|
|
timeTickMsg := &msgstream.TimeTickMsg{
|
|
|
|
BaseMsg: msgstream.BaseMsg{
|
|
|
|
BeginTimestamp: Timestamp(0),
|
|
|
|
EndTimestamp: Timestamp(0),
|
|
|
|
HashValues: []uint32{0},
|
|
|
|
},
|
|
|
|
TimeTickMsg: internalpb.TimeTickMsg{
|
|
|
|
Base: &commonpb.MsgBase{
|
|
|
|
MsgType: commonpb.MsgType_TimeTick,
|
|
|
|
MsgID: UniqueID(0),
|
|
|
|
Timestamp: math.MaxUint64,
|
|
|
|
SourceID: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
timeTickMsgPack.Msgs = append(timeTickMsgPack.Msgs, timeTickMsg)
|
|
|
|
|
|
|
|
// pulsar produce
|
|
|
|
msFactory := msgstream.NewPmsFactory()
|
|
|
|
m := map[string]interface{}{
|
|
|
|
"pulsarAddress": Params.PulsarAddress,
|
|
|
|
"receiveBufSize": 1024,
|
|
|
|
"pulsarBufSize": 1024}
|
|
|
|
err = msFactory.SetParams(m)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
insertStream, _ := msFactory.NewMsgStream(node1.ctx)
|
|
|
|
insertStream.AsProducer([]string{dmChannelName})
|
|
|
|
|
|
|
|
var insertMsgStream msgstream.MsgStream = insertStream
|
|
|
|
insertMsgStream.Start()
|
|
|
|
|
|
|
|
err = insertMsgStream.Broadcast(&timeTickMsgPack)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-05-29 18:04:30 +08:00
|
|
|
err = insertMsgStream.Broadcast(&timeTickMsgPack)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-05-27 18:45:24 +08:00
|
|
|
defer func() {
|
2021-05-29 18:04:30 +08:00
|
|
|
<-node1.ctx.Done()
|
2021-05-27 18:45:24 +08:00
|
|
|
node1.Stop()
|
|
|
|
}()
|
2021-04-21 18:41:37 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Test GetTimeTickChannel", func(t *testing.T) {
|
|
|
|
_, err := node.GetTimeTickChannel(node.ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Test GetStatisticsChannel", func(t *testing.T) {
|
|
|
|
_, err := node.GetStatisticsChannel(node.ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
2021-07-15 10:05:55 +08:00
|
|
|
t.Run("Test BackGroundGC", func(te *testing.T) {
|
2021-08-03 09:59:24 +08:00
|
|
|
te.Skipf("issue #6574")
|
2021-07-15 10:05:55 +08:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
node := newIDLEDataNodeMock(ctx)
|
|
|
|
|
|
|
|
collIDCh := make(chan UniqueID)
|
|
|
|
node.clearSignal = collIDCh
|
2021-08-03 09:59:24 +08:00
|
|
|
go node.BackGroundGC(collIDCh)
|
2021-07-15 10:05:55 +08:00
|
|
|
|
|
|
|
testDataSyncs := []struct {
|
|
|
|
collID UniqueID
|
|
|
|
dmChannelName string
|
|
|
|
}{
|
|
|
|
{1, "fake-dm-backgroundgc-1"},
|
|
|
|
{2, "fake-dm-backgroundgc-2"},
|
|
|
|
{3, "fake-dm-backgroundgc-3"},
|
|
|
|
{4, ""},
|
|
|
|
{1, ""},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, t := range testDataSyncs {
|
|
|
|
if i <= 2 {
|
|
|
|
node.NewDataSyncService(&datapb.VchannelInfo{CollectionID: t.collID, ChannelName: t.dmChannelName})
|
|
|
|
}
|
|
|
|
|
|
|
|
collIDCh <- t.collID
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Eventually(t, func() bool {
|
|
|
|
node.chanMut.Lock()
|
|
|
|
defer node.chanMut.Unlock()
|
|
|
|
return len(node.vchan2FlushCh) == 0
|
|
|
|
}, time.Second, time.Millisecond)
|
|
|
|
|
|
|
|
cancel()
|
|
|
|
})
|
|
|
|
|
2021-06-07 11:25:37 +08:00
|
|
|
t.Run("Test ReleaseDataSyncService", func(t *testing.T) {
|
|
|
|
dmChannelName := "fake-dm-channel-test-NewDataSyncService"
|
|
|
|
|
|
|
|
vchan := &datapb.VchannelInfo{
|
2021-06-08 19:25:37 +08:00
|
|
|
CollectionID: 1,
|
|
|
|
ChannelName: dmChannelName,
|
|
|
|
UnflushedSegments: []*datapb.SegmentInfo{},
|
2021-06-07 11:25:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
err := node.NewDataSyncService(vchan)
|
2021-07-15 10:05:55 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, len(node.vchan2FlushCh))
|
|
|
|
require.Equal(t, 1, len(node.vchan2SyncService))
|
2021-06-07 11:25:37 +08:00
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
|
|
|
node.ReleaseDataSyncService(dmChannelName)
|
|
|
|
assert.Equal(t, 0, len(node.vchan2FlushCh))
|
|
|
|
assert.Equal(t, 0, len(node.vchan2SyncService))
|
|
|
|
|
|
|
|
s, ok := node.vchan2SyncService[dmChannelName]
|
|
|
|
assert.False(t, ok)
|
|
|
|
assert.Nil(t, s)
|
|
|
|
|
|
|
|
})
|
|
|
|
|
2021-07-08 15:52:40 +08:00
|
|
|
t.Run("Test GetChannelName", func(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
node := newIDLEDataNodeMock(ctx)
|
|
|
|
|
|
|
|
testCollIDs := []UniqueID{0, 1, 2, 1}
|
|
|
|
testSegIDs := []UniqueID{10, 11, 12, 13}
|
|
|
|
testchanNames := []string{"a", "b", "c", "d"}
|
|
|
|
|
|
|
|
node.chanMut.Lock()
|
|
|
|
for i, name := range testchanNames {
|
|
|
|
replica := &SegmentReplica{
|
|
|
|
collectionID: testCollIDs[i],
|
|
|
|
newSegments: make(map[UniqueID]*Segment),
|
|
|
|
}
|
|
|
|
|
|
|
|
replica.addNewSegment(testSegIDs[i], testCollIDs[i], 0, name, &internalpb.MsgPosition{}, nil)
|
|
|
|
node.vchan2SyncService[name] = &dataSyncService{collectionID: testCollIDs[i], replica: replica}
|
|
|
|
}
|
|
|
|
node.chanMut.Unlock()
|
|
|
|
|
|
|
|
type Test struct {
|
|
|
|
inCollID UniqueID
|
|
|
|
expectedChannels []string
|
|
|
|
|
|
|
|
inSegID UniqueID
|
|
|
|
expectedChannel string
|
|
|
|
}
|
|
|
|
tests := []Test{
|
|
|
|
{0, []string{"a"}, 10, "a"},
|
|
|
|
{1, []string{"b", "d"}, 11, "b"},
|
|
|
|
{2, []string{"c"}, 12, "c"},
|
|
|
|
{3, []string{}, 13, "d"},
|
|
|
|
{3, []string{}, 100, ""},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
actualChannels := node.getChannelNamesbyCollectionID(test.inCollID)
|
|
|
|
assert.ElementsMatch(t, test.expectedChannels, actualChannels)
|
|
|
|
|
|
|
|
actualChannel := node.getChannelNamebySegmentID(test.inSegID)
|
|
|
|
assert.Equal(t, test.expectedChannel, actualChannel)
|
|
|
|
}
|
|
|
|
|
|
|
|
cancel()
|
|
|
|
})
|
|
|
|
|
2021-06-11 17:53:37 +08:00
|
|
|
cancel()
|
2021-05-28 14:54:31 +08:00
|
|
|
<-node.ctx.Done()
|
2021-04-21 18:41:37 +08:00
|
|
|
node.Stop()
|
2021-02-03 17:30:10 +08:00
|
|
|
}
|
2021-08-13 10:50:09 +08:00
|
|
|
|
|
|
|
func TestWatchChannel(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
node := newIDLEDataNodeMock(ctx)
|
|
|
|
node.Init()
|
|
|
|
node.Start()
|
|
|
|
node.Register()
|
|
|
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
t.Run("test watch channel", func(t *testing.T) {
|
|
|
|
client, err := clientv3.New(clientv3.Config{Endpoints: Params.EtcdEndpoints})
|
|
|
|
assert.Nil(t, err)
|
|
|
|
if assert.NotNil(t, client) {
|
|
|
|
|
|
|
|
kv := etcdkv.NewEtcdKV(client, Params.MetaRootPath)
|
|
|
|
ch := fmt.Sprintf("datanode-etcd-test-channel_%d", rand.Int31())
|
|
|
|
path := fmt.Sprintf("channel/%d/%s", node.NodeID, ch)
|
|
|
|
c := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
ec := kv.WatchWithPrefix(fmt.Sprintf("channel/%d", node.NodeID))
|
|
|
|
cnt := 0
|
|
|
|
for {
|
|
|
|
evt := <-ec
|
|
|
|
for _, event := range evt.Events {
|
|
|
|
if strings.Contains(string(event.Kv.Key), ch) {
|
|
|
|
cnt++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if cnt >= 2 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
vchan := &datapb.VchannelInfo{
|
|
|
|
CollectionID: 1,
|
|
|
|
ChannelName: ch,
|
|
|
|
UnflushedSegments: []*datapb.SegmentInfo{},
|
|
|
|
}
|
|
|
|
info := &datapb.ChannelWatchInfo{
|
|
|
|
State: datapb.ChannelWatchState_Uncomplete,
|
|
|
|
Vchan: vchan,
|
|
|
|
}
|
|
|
|
val, err := proto.Marshal(info)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
err = kv.Save(path, string(val))
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
<-c
|
|
|
|
node.chanMut.RLock()
|
|
|
|
_, has := node.vchan2SyncService[ch]
|
|
|
|
node.chanMut.RUnlock()
|
|
|
|
assert.True(t, has)
|
|
|
|
|
|
|
|
kv.RemoveWithPrefix(fmt.Sprintf("channel/%d", node.NodeID))
|
|
|
|
//TODO there is not way to sync Release done, use sleep for now
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
|
|
|
node.chanMut.RLock()
|
|
|
|
_, has = node.vchan2SyncService[ch]
|
|
|
|
node.chanMut.RUnlock()
|
|
|
|
assert.False(t, has)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|