2021-10-15 18:01:38 +08:00
|
|
|
// Licensed to the LF AI & Data foundation under one
|
|
|
|
// or more contributor license agreements. See the NOTICE file
|
|
|
|
// distributed with this work for additional information
|
|
|
|
// regarding copyright ownership. The ASF licenses this file
|
|
|
|
// to you under the Apache License, Version 2.0 (the
|
|
|
|
// "License"); you may not use this file except in compliance
|
2021-04-19 15:16:33 +08:00
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
2021-10-15 18:01:38 +08:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2021-04-19 15:16:33 +08:00
|
|
|
//
|
2021-10-15 18:01:38 +08:00
|
|
|
// 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-04-19 15:16:33 +08:00
|
|
|
|
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-09-23 10:53:53 +08:00
|
|
|
"strconv"
|
2021-08-13 10:50:09 +08:00
|
|
|
"strings"
|
2021-09-09 16:40:00 +08:00
|
|
|
"sync"
|
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-11-19 13:57:12 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/common"
|
|
|
|
|
2021-09-09 16:40:00 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/msgstream"
|
2021-09-01 10:13:15 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/milvuspb"
|
2021-10-11 18:50:30 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/types"
|
2021-09-01 10:13:15 +08:00
|
|
|
|
|
|
|
"github.com/milvus-io/milvus/internal/util/metricsinfo"
|
2021-11-01 11:01:49 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/sessionutil"
|
2021-09-01 10:13:15 +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"
|
|
|
|
"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"
|
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()
|
2021-09-23 10:53:53 +08:00
|
|
|
// change to specific channel for test
|
|
|
|
Params.TimeTickChannelName = Params.TimeTickChannelName + strconv.Itoa(rand.Int())
|
|
|
|
Params.SegmentStatisticsChannelName = Params.SegmentStatisticsChannelName + strconv.Itoa(rand.Int())
|
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-10-09 15:45:15 +08:00
|
|
|
err := node.Init()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
err = node.Start()
|
|
|
|
assert.Nil(t, err)
|
2021-09-23 18:31:55 +08:00
|
|
|
|
2021-11-01 11:01:49 +08:00
|
|
|
t.Run("Test WatchDmChannels ", func(t *testing.T) {
|
|
|
|
emptyNode := &DataNode{}
|
2021-09-23 18:31:55 +08:00
|
|
|
|
2021-11-01 11:01:49 +08:00
|
|
|
status, err := emptyNode.WatchDmChannels(ctx, &datapb.WatchDmChannelsRequest{})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, commonpb.ErrorCode_Success, status.ErrorCode)
|
2021-09-23 18:31:55 +08:00
|
|
|
})
|
|
|
|
|
2021-10-11 18:50:30 +08:00
|
|
|
t.Run("Test SetRootCoord", func(t *testing.T) {
|
|
|
|
emptyDN := &DataNode{}
|
|
|
|
tests := []struct {
|
|
|
|
inrc types.RootCoord
|
|
|
|
isvalid bool
|
|
|
|
description string
|
|
|
|
}{
|
|
|
|
{nil, false, "nil input"},
|
|
|
|
{&RootCoordFactory{}, true, "valid input"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.description, func(t *testing.T) {
|
|
|
|
err := emptyDN.SetRootCoord(test.inrc)
|
|
|
|
if test.isvalid {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
} else {
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Test SetDataCoord", func(t *testing.T) {
|
|
|
|
emptyDN := &DataNode{}
|
|
|
|
tests := []struct {
|
|
|
|
inrc types.DataCoord
|
|
|
|
isvalid bool
|
|
|
|
description string
|
|
|
|
}{
|
|
|
|
{nil, false, "nil input"},
|
|
|
|
{&DataCoordFactory{}, true, "valid input"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.description, func(t *testing.T) {
|
|
|
|
err := emptyDN.SetDataCoord(test.inrc)
|
|
|
|
if test.isvalid {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
} else {
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
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-10-09 15:45:15 +08:00
|
|
|
err = node2.Start()
|
|
|
|
assert.Nil(t, err)
|
2021-11-05 11:59:02 +08:00
|
|
|
dmChannelName := "fake-by-dev-rootcoord-dml-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
|
|
|
}
|
|
|
|
|
2021-09-28 18:22:16 +08:00
|
|
|
require.Equal(t, 0, len(node2.vchan2FlushChs))
|
2021-05-25 15:35:37 +08:00
|
|
|
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)
|
2021-09-28 18:22:16 +08:00
|
|
|
assert.Equal(t, 1, len(node2.vchan2FlushChs))
|
2021-05-25 15:35:37 +08:00
|
|
|
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)
|
2021-09-28 18:22:16 +08:00
|
|
|
assert.Equal(t, 1, len(node2.vchan2FlushChs))
|
2021-05-25 15:35:37 +08:00
|
|
|
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()
|
2021-10-09 15:45:15 +08:00
|
|
|
err = node2.Stop()
|
|
|
|
assert.Nil(t, err)
|
2021-05-25 15:35:37 +08:00
|
|
|
})
|
|
|
|
|
2021-04-21 18:41:37 +08:00
|
|
|
t.Run("Test FlushSegments", func(t *testing.T) {
|
2021-11-05 11:59:02 +08:00
|
|
|
dmChannelName := "fake-by-dev-rootcoord-dml-channel-test-FlushSegments"
|
2021-05-25 15:35:37 +08:00
|
|
|
|
2021-09-09 16:40:00 +08:00
|
|
|
node1 := newIDLEDataNodeMock(context.TODO())
|
2021-10-09 15:45:15 +08:00
|
|
|
err = node1.Init()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
err = node1.Start()
|
|
|
|
assert.Nil(t, err)
|
2021-09-09 16:40:00 +08:00
|
|
|
defer func() {
|
|
|
|
err := node1.Stop()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
}()
|
|
|
|
|
|
|
|
vchan := &datapb.VchannelInfo{
|
|
|
|
CollectionID: 1,
|
|
|
|
ChannelName: dmChannelName,
|
|
|
|
UnflushedSegments: []*datapb.SegmentInfo{},
|
2021-10-08 19:09:12 +08:00
|
|
|
FlushedSegments: []*datapb.SegmentInfo{},
|
2021-09-09 16:40:00 +08:00
|
|
|
}
|
|
|
|
err := node1.NewDataSyncService(vchan)
|
|
|
|
assert.Nil(t, err)
|
2021-05-25 15:35:37 +08:00
|
|
|
|
2021-09-09 16:40:00 +08:00
|
|
|
service, ok := node1.vchan2SyncService[dmChannelName]
|
2021-05-25 15:35:37 +08:00
|
|
|
assert.True(t, ok)
|
2021-10-09 15:45:15 +08:00
|
|
|
err = service.replica.addNewSegment(0, 1, 1, dmChannelName, &internalpb.MsgPosition{}, &internalpb.MsgPosition{})
|
|
|
|
assert.Nil(t, err)
|
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-09-09 16:40:00 +08:00
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(2)
|
2021-05-25 15:35:37 +08:00
|
|
|
|
2021-09-09 16:40:00 +08:00
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2021-05-25 15:35:37 +08:00
|
|
|
|
2021-09-09 16:40:00 +08:00
|
|
|
status, err := node1.FlushSegments(node1.ctx, req)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, commonpb.ErrorCode_Success, status.ErrorCode)
|
|
|
|
}()
|
2021-05-25 15:35:37 +08:00
|
|
|
|
2021-09-09 16:40:00 +08:00
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2021-05-29 18:04:30 +08:00
|
|
|
|
2021-09-09 16:40:00 +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, err := msFactory.NewMsgStream(node1.ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
insertStream.AsProducer([]string{dmChannelName})
|
|
|
|
insertStream.Start()
|
|
|
|
defer insertStream.Close()
|
|
|
|
|
|
|
|
err = insertStream.Broadcast(&timeTickMsgPack)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = insertStream.Broadcast(&timeTickMsgPack)
|
|
|
|
assert.NoError(t, err)
|
2021-05-27 18:45:24 +08:00
|
|
|
}()
|
2021-09-09 16:40:00 +08:00
|
|
|
|
|
|
|
wg.Wait()
|
2021-10-20 15:02:36 +08:00
|
|
|
// dup call
|
|
|
|
status, err := node1.FlushSegments(node1.ctx, req)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, commonpb.ErrorCode_Success, status.ErrorCode)
|
|
|
|
|
|
|
|
// failure call
|
|
|
|
req = &datapb.FlushSegmentsRequest{
|
|
|
|
Base: &commonpb.MsgBase{},
|
|
|
|
DbID: 0,
|
|
|
|
CollectionID: 1,
|
|
|
|
SegmentIDs: []int64{1},
|
|
|
|
}
|
|
|
|
|
|
|
|
status, err = node1.FlushSegments(node1.ctx, req)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, commonpb.ErrorCode_UnexpectedError, status.ErrorCode)
|
|
|
|
|
|
|
|
req = &datapb.FlushSegmentsRequest{
|
|
|
|
Base: &commonpb.MsgBase{},
|
|
|
|
DbID: 0,
|
|
|
|
CollectionID: 1,
|
|
|
|
SegmentIDs: []int64{},
|
|
|
|
MarkSegmentIDs: []int64{2},
|
|
|
|
}
|
|
|
|
|
|
|
|
status, err = node1.FlushSegments(node1.ctx, req)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, commonpb.ErrorCode_UnexpectedError, status.ErrorCode)
|
|
|
|
|
|
|
|
// manual inject meta error
|
|
|
|
node1.chanMut.Lock()
|
|
|
|
node1.vchan2FlushChs[dmChannelName+"1"] = node1.vchan2FlushChs[dmChannelName]
|
|
|
|
delete(node1.vchan2FlushChs, dmChannelName)
|
|
|
|
node1.chanMut.Unlock()
|
|
|
|
node1.segmentCache.Remove(0)
|
|
|
|
|
|
|
|
req = &datapb.FlushSegmentsRequest{
|
|
|
|
Base: &commonpb.MsgBase{},
|
|
|
|
DbID: 0,
|
|
|
|
CollectionID: 1,
|
|
|
|
SegmentIDs: []int64{0},
|
|
|
|
}
|
|
|
|
|
|
|
|
status, err = node1.FlushSegments(node1.ctx, req)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, commonpb.ErrorCode_UnexpectedError, status.ErrorCode)
|
|
|
|
|
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-09-01 10:13:15 +08:00
|
|
|
t.Run("Test getSystemInfoMetrics", func(t *testing.T) {
|
2021-11-01 11:01:49 +08:00
|
|
|
emptyNode := &DataNode{}
|
|
|
|
emptyNode.session = &sessionutil.Session{ServerID: 1}
|
|
|
|
|
2021-09-01 10:13:15 +08:00
|
|
|
req, err := metricsinfo.ConstructRequestByMetricType(metricsinfo.SystemInfoMetrics)
|
|
|
|
assert.NoError(t, err)
|
2021-11-01 11:01:49 +08:00
|
|
|
resp, err := emptyNode.getSystemInfoMetrics(context.TODO(), req)
|
2021-09-01 10:13:15 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
log.Info("Test DataNode.getSystemInfoMetrics",
|
|
|
|
zap.String("name", resp.ComponentName),
|
|
|
|
zap.String("response", resp.Response))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Test GetMetrics", func(t *testing.T) {
|
2021-11-01 11:01:49 +08:00
|
|
|
node := &DataNode{}
|
|
|
|
node.session = &sessionutil.Session{ServerID: 1}
|
2021-09-01 10:13:15 +08:00
|
|
|
// server is closed
|
|
|
|
node.State.Store(internalpb.StateCode_Abnormal)
|
|
|
|
resp, err := node.GetMetrics(ctx, &milvuspb.GetMetricsRequest{})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEqual(t, commonpb.ErrorCode_Success, resp.Status.ErrorCode)
|
2021-11-01 11:01:49 +08:00
|
|
|
node.State.Store(internalpb.StateCode_Healthy)
|
2021-09-01 10:13:15 +08:00
|
|
|
|
|
|
|
// failed to parse metric type
|
|
|
|
invalidRequest := "invalid request"
|
|
|
|
resp, err = node.GetMetrics(ctx, &milvuspb.GetMetricsRequest{
|
|
|
|
Request: invalidRequest,
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEqual(t, commonpb.ErrorCode_Success, resp.Status.ErrorCode)
|
|
|
|
|
|
|
|
// unsupported metric type
|
|
|
|
unsupportedMetricType := "unsupported"
|
|
|
|
req, err := metricsinfo.ConstructRequestByMetricType(unsupportedMetricType)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
resp, err = node.GetMetrics(ctx, req)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEqual(t, commonpb.ErrorCode_Success, resp.Status.ErrorCode)
|
|
|
|
|
|
|
|
// normal case
|
|
|
|
req, err = metricsinfo.ConstructRequestByMetricType(metricsinfo.SystemInfoMetrics)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
resp, err = node.GetMetrics(node.ctx, req)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, commonpb.ErrorCode_Success, resp.Status.ErrorCode)
|
|
|
|
log.Info("Test DataNode.GetMetrics",
|
|
|
|
zap.String("name", resp.ComponentName),
|
|
|
|
zap.String("response", resp.Response))
|
|
|
|
})
|
|
|
|
|
2021-11-25 09:43:15 +08:00
|
|
|
t.Run("Test BackGroundGC", func(t *testing.T) {
|
2021-07-15 10:05:55 +08:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
node := newIDLEDataNodeMock(ctx)
|
|
|
|
|
2021-11-25 09:43:15 +08:00
|
|
|
vchanNameCh := make(chan string)
|
|
|
|
node.clearSignal = vchanNameCh
|
|
|
|
go node.BackGroundGC(vchanNameCh)
|
2021-07-15 10:05:55 +08:00
|
|
|
|
|
|
|
testDataSyncs := []struct {
|
|
|
|
dmChannelName string
|
|
|
|
}{
|
2021-11-25 09:43:15 +08:00
|
|
|
{"fake-by-dev-rootcoord-dml-backgroundgc-1"},
|
|
|
|
{"fake-by-dev-rootcoord-dml-backgroundgc-2"},
|
|
|
|
{"fake-by-dev-rootcoord-dml-backgroundgc-3"},
|
|
|
|
{""},
|
|
|
|
{""},
|
2021-07-15 10:05:55 +08:00
|
|
|
}
|
|
|
|
|
2021-11-25 09:43:15 +08:00
|
|
|
for i, test := range testDataSyncs {
|
2021-07-15 10:05:55 +08:00
|
|
|
if i <= 2 {
|
|
|
|
|
2021-11-25 09:43:15 +08:00
|
|
|
err = node.NewDataSyncService(&datapb.VchannelInfo{CollectionID: 1, ChannelName: test.dmChannelName})
|
|
|
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
vchanNameCh <- test.dmChannelName
|
|
|
|
}
|
2021-07-15 10:05:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
assert.Eventually(t, func() bool {
|
|
|
|
node.chanMut.Lock()
|
|
|
|
defer node.chanMut.Unlock()
|
2021-09-28 18:22:16 +08:00
|
|
|
return len(node.vchan2FlushChs) == 0
|
2021-07-15 10:05:55 +08:00
|
|
|
}, time.Second, time.Millisecond)
|
|
|
|
|
|
|
|
cancel()
|
|
|
|
})
|
|
|
|
|
2021-06-07 11:25:37 +08:00
|
|
|
t.Run("Test ReleaseDataSyncService", func(t *testing.T) {
|
2021-11-05 11:59:02 +08:00
|
|
|
dmChannelName := "fake-by-dev-rootcoord-dml-channel-test-NewDataSyncService"
|
2021-06-07 11:25:37 +08:00
|
|
|
|
|
|
|
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)
|
2021-09-28 18:22:16 +08:00
|
|
|
require.Equal(t, 1, len(node.vchan2FlushChs))
|
2021-07-15 10:05:55 +08:00
|
|
|
require.Equal(t, 1, len(node.vchan2SyncService))
|
2021-09-26 12:50:24 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2021-06-07 11:25:37 +08:00
|
|
|
|
|
|
|
node.ReleaseDataSyncService(dmChannelName)
|
2021-09-28 18:22:16 +08:00
|
|
|
assert.Equal(t, 0, len(node.vchan2FlushChs))
|
2021-06-07 11:25:37 +08:00
|
|
|
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),
|
|
|
|
}
|
|
|
|
|
2021-10-09 15:45:15 +08:00
|
|
|
err = replica.addNewSegment(testSegIDs[i], testCollIDs[i], 0, name, &internalpb.MsgPosition{}, nil)
|
|
|
|
assert.Nil(t, err)
|
2021-07-08 15:52:40 +08:00
|
|
|
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-10-09 15:45:15 +08:00
|
|
|
err = node.Stop()
|
2021-11-01 11:01:49 +08:00
|
|
|
require.Nil(t, err)
|
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)
|
2021-10-09 15:45:15 +08:00
|
|
|
err := node.Init()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
err = node.Start()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
err = node.Register()
|
|
|
|
assert.Nil(t, err)
|
2021-08-13 10:50:09 +08:00
|
|
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
t.Run("test watch channel", func(t *testing.T) {
|
2021-08-13 14:00:14 +08:00
|
|
|
kv, err := etcdkv.NewEtcdKV(Params.EtcdEndpoints, Params.MetaRootPath)
|
|
|
|
require.NoError(t, err)
|
2021-11-05 11:59:02 +08:00
|
|
|
oldInvalidCh := "datanode-etcd-test-by-dev-rootcoord-dml-channel-invalid"
|
2021-10-14 15:44:34 +08:00
|
|
|
path := fmt.Sprintf("%s/%d/%s", Params.ChannelWatchSubPath, node.NodeID, oldInvalidCh)
|
2021-10-13 17:02:33 +08:00
|
|
|
err = kv.Save(path, string([]byte{23}))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-11-05 11:59:02 +08:00
|
|
|
ch := fmt.Sprintf("datanode-etcd-test-by-dev-rootcoord-dml-channel_%d", rand.Int31())
|
2021-10-14 15:44:34 +08:00
|
|
|
path = fmt.Sprintf("%s/%d/%s", Params.ChannelWatchSubPath, node.NodeID, ch)
|
2021-08-13 14:00:14 +08:00
|
|
|
c := make(chan struct{})
|
|
|
|
go func() {
|
2021-10-14 15:44:34 +08:00
|
|
|
ec := kv.WatchWithPrefix(fmt.Sprintf("%s/%d", Params.ChannelWatchSubPath, node.NodeID))
|
2021-08-20 17:48:13 +08:00
|
|
|
c <- struct{}{}
|
2021-08-13 14:00:14 +08:00
|
|
|
cnt := 0
|
|
|
|
for {
|
|
|
|
evt := <-ec
|
|
|
|
for _, event := range evt.Events {
|
|
|
|
if strings.Contains(string(event.Kv.Key), ch) {
|
|
|
|
cnt++
|
2021-08-13 10:50:09 +08:00
|
|
|
}
|
|
|
|
}
|
2021-08-13 14:00:14 +08:00
|
|
|
if cnt >= 2 {
|
|
|
|
break
|
|
|
|
}
|
2021-08-13 10:50:09 +08:00
|
|
|
}
|
2021-08-13 14:00:14 +08:00
|
|
|
c <- struct{}{}
|
|
|
|
}()
|
2021-08-20 17:48:13 +08:00
|
|
|
// wait for check goroutine start Watch
|
|
|
|
<-c
|
2021-08-13 14:00:14 +08:00
|
|
|
|
|
|
|
vchan := &datapb.VchannelInfo{
|
|
|
|
CollectionID: 1,
|
|
|
|
ChannelName: ch,
|
|
|
|
UnflushedSegments: []*datapb.SegmentInfo{},
|
|
|
|
}
|
|
|
|
info := &datapb.ChannelWatchInfo{
|
|
|
|
State: datapb.ChannelWatchState_Uncomplete,
|
|
|
|
Vchan: vchan,
|
2021-08-13 10:50:09 +08:00
|
|
|
}
|
2021-08-13 14:00:14 +08:00
|
|
|
val, err := proto.Marshal(info)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
err = kv.Save(path, string(val))
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2021-08-20 17:48:13 +08:00
|
|
|
// wait for check goroutine received 2 events
|
2021-08-13 14:00:14 +08:00
|
|
|
<-c
|
|
|
|
node.chanMut.RLock()
|
|
|
|
_, has := node.vchan2SyncService[ch]
|
|
|
|
node.chanMut.RUnlock()
|
|
|
|
assert.True(t, has)
|
|
|
|
|
2021-10-14 15:44:34 +08:00
|
|
|
err = kv.RemoveWithPrefix(fmt.Sprintf("%s/%d", Params.ChannelWatchSubPath, node.NodeID))
|
2021-10-09 15:45:15 +08:00
|
|
|
assert.Nil(t, err)
|
2021-08-13 14:00:14 +08:00
|
|
|
//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)
|
2021-08-13 10:50:09 +08:00
|
|
|
})
|
2021-09-23 18:31:55 +08:00
|
|
|
|
2021-10-13 17:02:33 +08:00
|
|
|
t.Run("handle watch info failed", func(t *testing.T) {
|
|
|
|
node.handleWatchInfo("test1", []byte{23})
|
|
|
|
|
|
|
|
node.chanMut.RLock()
|
|
|
|
_, has := node.vchan2SyncService["test1"]
|
|
|
|
assert.False(t, has)
|
|
|
|
node.chanMut.RUnlock()
|
|
|
|
|
|
|
|
info := datapb.ChannelWatchInfo{
|
|
|
|
Vchan: nil,
|
|
|
|
State: datapb.ChannelWatchState_Uncomplete,
|
|
|
|
}
|
|
|
|
bs, err := proto.Marshal(&info)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
node.handleWatchInfo("test2", bs)
|
|
|
|
|
|
|
|
node.chanMut.RLock()
|
|
|
|
_, has = node.vchan2SyncService["test2"]
|
|
|
|
assert.False(t, has)
|
|
|
|
node.chanMut.RUnlock()
|
|
|
|
|
|
|
|
info = datapb.ChannelWatchInfo{
|
|
|
|
Vchan: &datapb.VchannelInfo{},
|
|
|
|
State: datapb.ChannelWatchState_Uncomplete,
|
|
|
|
}
|
|
|
|
bs, err = proto.Marshal(&info)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
node.msFactory = &FailMessageStreamFactory{
|
|
|
|
node.msFactory,
|
|
|
|
}
|
|
|
|
node.handleWatchInfo("test3", bs)
|
|
|
|
node.chanMut.RLock()
|
|
|
|
_, has = node.vchan2SyncService["test3"]
|
|
|
|
assert.False(t, has)
|
|
|
|
node.chanMut.RUnlock()
|
|
|
|
|
|
|
|
})
|
2021-08-13 10:50:09 +08:00
|
|
|
}
|
2021-11-19 13:57:12 +08:00
|
|
|
|
|
|
|
func TestDataNode_GetComponentStates(t *testing.T) {
|
|
|
|
n := &DataNode{}
|
|
|
|
n.State.Store(internalpb.StateCode_Healthy)
|
|
|
|
resp, err := n.GetComponentStates(context.Background())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, commonpb.ErrorCode_Success, resp.Status.ErrorCode)
|
|
|
|
assert.Equal(t, common.NotRegisteredID, resp.State.NodeID)
|
|
|
|
n.session = &sessionutil.Session{}
|
|
|
|
n.session.UpdateRegistered(true)
|
|
|
|
resp, err = n.GetComponentStates(context.Background())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, commonpb.ErrorCode_Success, resp.Status.ErrorCode)
|
|
|
|
}
|