2021-10-15 18:07:09 +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-08-25 11:41:52 +08:00
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
2021-10-15 18:07:09 +08:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2021-08-25 11:41:52 +08:00
|
|
|
//
|
2021-10-15 18:07:09 +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-08-25 11:41:52 +08:00
|
|
|
|
|
|
|
package datanode
|
|
|
|
|
|
|
|
import (
|
2021-10-11 16:31:44 +08:00
|
|
|
"context"
|
2021-10-19 11:04:34 +08:00
|
|
|
"errors"
|
2021-08-25 11:41:52 +08:00
|
|
|
"testing"
|
2021-10-11 16:31:44 +08:00
|
|
|
"time"
|
2021-08-25 11:41:52 +08:00
|
|
|
|
2021-08-28 10:12:00 +08:00
|
|
|
"github.com/bits-and-blooms/bloom/v3"
|
2021-11-02 18:16:32 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/common"
|
2021-10-19 11:04:34 +08:00
|
|
|
memkv "github.com/milvus-io/milvus/internal/kv/mem"
|
|
|
|
"github.com/milvus-io/milvus/internal/msgstream"
|
2021-10-11 16:31:44 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/schemapb"
|
|
|
|
"github.com/milvus-io/milvus/internal/util/flowgraph"
|
2021-08-25 11:41:52 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2021-09-27 14:38:00 +08:00
|
|
|
type mockReplica struct {
|
|
|
|
Replica
|
|
|
|
|
|
|
|
newSegments map[UniqueID]*Segment
|
|
|
|
normalSegments map[UniqueID]*Segment
|
|
|
|
flushedSegments map[UniqueID]*Segment
|
|
|
|
}
|
|
|
|
|
2021-10-11 16:31:44 +08:00
|
|
|
func newMockReplica() *mockReplica {
|
|
|
|
return &mockReplica{
|
|
|
|
newSegments: make(map[int64]*Segment),
|
|
|
|
normalSegments: make(map[int64]*Segment),
|
|
|
|
flushedSegments: make(map[int64]*Segment),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-29 10:27:58 +08:00
|
|
|
func (replica *mockReplica) filterSegments(channelName string, partitionID UniqueID) []*Segment {
|
2021-09-27 14:38:00 +08:00
|
|
|
results := make([]*Segment, 0)
|
|
|
|
for _, value := range replica.newSegments {
|
|
|
|
results = append(results, value)
|
|
|
|
}
|
|
|
|
for _, value := range replica.normalSegments {
|
|
|
|
results = append(results, value)
|
|
|
|
}
|
|
|
|
for _, value := range replica.flushedSegments {
|
|
|
|
results = append(results, value)
|
|
|
|
}
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
2021-10-18 12:34:34 +08:00
|
|
|
func (replica *mockReplica) getCollectionID() UniqueID {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2021-10-11 16:31:44 +08:00
|
|
|
func (replica *mockReplica) getCollectionSchema(collectionID UniqueID, ts Timestamp) (*schemapb.CollectionSchema, error) {
|
2021-10-19 11:04:34 +08:00
|
|
|
if ts == 0 {
|
|
|
|
return nil, errors.New("mocked error")
|
|
|
|
}
|
2021-10-11 16:31:44 +08:00
|
|
|
return &schemapb.CollectionSchema{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (replica *mockReplica) getCollectionAndPartitionID(segID UniqueID) (collID, partitionID UniqueID, err error) {
|
2021-10-19 11:04:34 +08:00
|
|
|
if segID == -1 {
|
|
|
|
return -1, -1, errors.New("mocked error")
|
|
|
|
}
|
2021-10-11 16:31:44 +08:00
|
|
|
return 0, 1, nil
|
|
|
|
}
|
|
|
|
|
2021-10-19 11:04:34 +08:00
|
|
|
func (replica *mockReplica) hasSegment(segID UniqueID, countFlushed bool) bool {
|
|
|
|
_, has := replica.newSegments[segID]
|
|
|
|
if has {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
_, has = replica.normalSegments[segID]
|
|
|
|
if has {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if !countFlushed {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
_, has = replica.flushedSegments[segID]
|
|
|
|
return has
|
|
|
|
}
|
|
|
|
|
2021-09-08 10:41:59 +08:00
|
|
|
func TestFlowGraphDeleteNode_newDeleteNode(te *testing.T) {
|
|
|
|
tests := []struct {
|
2021-10-13 11:16:32 +08:00
|
|
|
ctx context.Context
|
|
|
|
config *nodeConfig
|
2021-09-08 10:41:59 +08:00
|
|
|
|
|
|
|
description string
|
|
|
|
}{
|
2021-10-13 11:16:32 +08:00
|
|
|
{context.Background(), &nodeConfig{}, "pointer of SegmentReplica"},
|
2021-09-08 10:41:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
te.Run(test.description, func(t *testing.T) {
|
2021-11-25 09:43:15 +08:00
|
|
|
dn, err := newDeleteNode(test.ctx, nil, make(chan string, 1), test.config)
|
2021-10-11 16:31:44 +08:00
|
|
|
assert.Nil(t, err)
|
2021-09-08 10:41:59 +08:00
|
|
|
|
2021-09-09 15:36:01 +08:00
|
|
|
assert.NotNil(t, dn)
|
|
|
|
assert.Equal(t, "deleteNode", dn.Name())
|
|
|
|
dn.Close()
|
2021-09-08 10:41:59 +08:00
|
|
|
})
|
|
|
|
}
|
2021-08-25 11:41:52 +08:00
|
|
|
}
|
2021-08-28 10:12:00 +08:00
|
|
|
|
2021-10-11 16:31:44 +08:00
|
|
|
func genMockReplica(segIDs []int64, pks []int64, chanName string) *mockReplica {
|
2021-08-28 10:12:00 +08:00
|
|
|
buf := make([]byte, 8)
|
2021-10-11 16:31:44 +08:00
|
|
|
filter0 := bloom.NewWithEstimates(1000000, 0.01)
|
2021-08-28 10:12:00 +08:00
|
|
|
for i := 0; i < 3; i++ {
|
2021-11-02 18:16:32 +08:00
|
|
|
common.Endian.PutUint64(buf, uint64(pks[i]))
|
2021-10-11 16:31:44 +08:00
|
|
|
filter0.Add(buf)
|
2021-08-28 10:12:00 +08:00
|
|
|
}
|
2021-10-11 16:31:44 +08:00
|
|
|
|
|
|
|
filter1 := bloom.NewWithEstimates(1000000, 0.01)
|
2021-08-28 10:12:00 +08:00
|
|
|
for i := 3; i < 5; i++ {
|
2021-11-02 18:16:32 +08:00
|
|
|
common.Endian.PutUint64(buf, uint64(pks[i]))
|
2021-10-11 16:31:44 +08:00
|
|
|
filter1.Add(buf)
|
2021-08-28 10:12:00 +08:00
|
|
|
}
|
2021-10-11 16:31:44 +08:00
|
|
|
|
|
|
|
seg0 := &Segment{
|
|
|
|
segmentID: segIDs[0],
|
|
|
|
channelName: chanName,
|
|
|
|
pkFilter: filter0,
|
|
|
|
}
|
|
|
|
seg1 := &Segment{
|
|
|
|
segmentID: segIDs[1],
|
|
|
|
channelName: chanName,
|
|
|
|
pkFilter: filter0,
|
|
|
|
}
|
|
|
|
seg2 := &Segment{
|
|
|
|
segmentID: segIDs[2],
|
|
|
|
channelName: chanName,
|
|
|
|
pkFilter: filter0,
|
|
|
|
}
|
|
|
|
seg3 := &Segment{
|
|
|
|
segmentID: segIDs[3],
|
|
|
|
channelName: chanName,
|
2021-09-27 14:38:00 +08:00
|
|
|
pkFilter: filter1,
|
2021-08-28 10:12:00 +08:00
|
|
|
}
|
2021-10-11 16:31:44 +08:00
|
|
|
seg4 := &Segment{
|
|
|
|
segmentID: segIDs[4],
|
|
|
|
channelName: chanName,
|
2021-09-27 14:38:00 +08:00
|
|
|
pkFilter: filter1,
|
2021-08-28 10:12:00 +08:00
|
|
|
}
|
2021-10-11 16:31:44 +08:00
|
|
|
seg5 := &Segment{
|
|
|
|
segmentID: segIDs[4],
|
2021-09-27 14:38:00 +08:00
|
|
|
channelName: "test_error",
|
2021-10-11 16:31:44 +08:00
|
|
|
pkFilter: filter1,
|
2021-09-27 14:38:00 +08:00
|
|
|
}
|
2021-10-11 16:31:44 +08:00
|
|
|
|
|
|
|
replica := newMockReplica()
|
|
|
|
replica.newSegments[segIDs[0]] = seg0
|
|
|
|
replica.newSegments[segIDs[1]] = seg1
|
|
|
|
replica.normalSegments[segIDs[2]] = seg2
|
|
|
|
replica.normalSegments[segIDs[3]] = seg3
|
|
|
|
replica.flushedSegments[segIDs[4]] = seg4
|
|
|
|
replica.flushedSegments[segIDs[4]] = seg5
|
|
|
|
|
|
|
|
return replica
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFlowGraphDeleteNode_Operate(t *testing.T) {
|
|
|
|
t.Run("Test deleteNode Operate invalid Msg", func(te *testing.T) {
|
|
|
|
invalidInTests := []struct {
|
|
|
|
in []Msg
|
|
|
|
desc string
|
|
|
|
}{
|
|
|
|
{[]Msg{},
|
|
|
|
"Invalid input length == 0"},
|
|
|
|
{[]Msg{&flowGraphMsg{}, &flowGraphMsg{}, &flowGraphMsg{}},
|
|
|
|
"Invalid input length == 3"},
|
|
|
|
{[]Msg{&flowGraphMsg{}},
|
|
|
|
"Invalid input length == 1 but input message is not msgStreamMsg"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range invalidInTests {
|
|
|
|
te.Run(test.desc, func(t *testing.T) {
|
2021-10-18 12:34:34 +08:00
|
|
|
dn := deleteNode{}
|
2021-10-11 16:31:44 +08:00
|
|
|
rt := dn.Operate(test.in)
|
|
|
|
assert.Empty(t, rt)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const (
|
|
|
|
chanName = "channel-test"
|
|
|
|
)
|
|
|
|
var (
|
|
|
|
segIDs = []int64{11, 22, 33, 44, 55}
|
|
|
|
pks = []int64{3, 17, 44, 190, 425}
|
|
|
|
)
|
|
|
|
replica := genMockReplica(segIDs, pks, chanName)
|
2021-10-19 11:04:34 +08:00
|
|
|
kv := memkv.NewMemoryKV()
|
2021-12-01 10:11:39 +08:00
|
|
|
fm := NewRendezvousFlushManager(NewAllocatorFactory(), kv, replica, func(*segmentFlushPack) {}, emptyFlushAndDropFunc)
|
2021-10-11 16:31:44 +08:00
|
|
|
t.Run("Test get segment by primary keys", func(te *testing.T) {
|
2021-10-13 11:16:32 +08:00
|
|
|
c := &nodeConfig{
|
|
|
|
replica: replica,
|
|
|
|
allocator: &allocator{},
|
|
|
|
vChannelName: chanName,
|
|
|
|
}
|
|
|
|
|
2021-11-25 09:43:15 +08:00
|
|
|
dn, err := newDeleteNode(context.Background(), fm, make(chan string, 1), c)
|
2021-10-11 16:31:44 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
results := dn.filterSegmentByPK(0, pks)
|
|
|
|
expected := map[int64][]int64{
|
|
|
|
pks[0]: segIDs[0:3],
|
|
|
|
pks[1]: segIDs[0:3],
|
|
|
|
pks[2]: segIDs[0:3],
|
|
|
|
pks[3]: segIDs[3:5],
|
|
|
|
pks[4]: segIDs[3:5],
|
|
|
|
}
|
|
|
|
for key, value := range expected {
|
|
|
|
assert.ElementsMatch(t, value, results[key])
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-10-19 11:04:34 +08:00
|
|
|
t.Run("Test deleteNode Operate valid Msg with failure", func(te *testing.T) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
chanName := "datanode-test-FlowGraphDeletenode-operate"
|
|
|
|
testPath := "/test/datanode/root/meta"
|
|
|
|
assert.NoError(t, clearEtcd(testPath))
|
|
|
|
Params.MetaRootPath = testPath
|
|
|
|
Params.DeleteBinlogRootPath = testPath
|
|
|
|
|
|
|
|
c := &nodeConfig{
|
|
|
|
replica: replica,
|
|
|
|
allocator: NewAllocatorFactory(),
|
|
|
|
vChannelName: chanName,
|
|
|
|
}
|
2021-11-25 09:43:15 +08:00
|
|
|
delNode, err := newDeleteNode(ctx, fm, make(chan string, 1), c)
|
2021-10-19 11:04:34 +08:00
|
|
|
assert.Nil(te, err)
|
|
|
|
|
2021-11-04 15:36:19 +08:00
|
|
|
msg := genFlowGraphDeleteMsg(pks, chanName)
|
2021-10-19 11:04:34 +08:00
|
|
|
msg.segmentsToFlush = segIDs
|
|
|
|
// this will fail since ts = 0 will trigger mocked error
|
|
|
|
var fgMsg flowgraph.Msg = &msg
|
|
|
|
delNode.Operate([]flowgraph.Msg{fgMsg})
|
|
|
|
})
|
|
|
|
t.Run("Test deleteNode Operate valid Msg with failure", func(te *testing.T) {
|
2021-10-11 16:31:44 +08:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
chanName := "datanode-test-FlowGraphDeletenode-operate"
|
|
|
|
testPath := "/test/datanode/root/meta"
|
|
|
|
assert.NoError(t, clearEtcd(testPath))
|
|
|
|
Params.MetaRootPath = testPath
|
|
|
|
Params.DeleteBinlogRootPath = testPath
|
|
|
|
|
2021-10-13 11:16:32 +08:00
|
|
|
c := &nodeConfig{
|
|
|
|
replica: replica,
|
|
|
|
allocator: NewAllocatorFactory(),
|
|
|
|
vChannelName: chanName,
|
|
|
|
}
|
2021-11-25 09:43:15 +08:00
|
|
|
delNode, err := newDeleteNode(ctx, fm, make(chan string, 1), c)
|
2021-10-11 16:31:44 +08:00
|
|
|
assert.Nil(te, err)
|
|
|
|
|
2021-11-04 15:36:19 +08:00
|
|
|
msg := genFlowGraphDeleteMsg(pks, chanName)
|
2021-10-18 12:34:34 +08:00
|
|
|
msg.segmentsToFlush = segIDs
|
2021-10-19 11:04:34 +08:00
|
|
|
|
|
|
|
msg.endPositions[0].Timestamp = 100 // set to normal timestamp
|
2021-10-11 16:31:44 +08:00
|
|
|
var fgMsg flowgraph.Msg = &msg
|
|
|
|
delNode.Operate([]flowgraph.Msg{fgMsg})
|
2021-10-19 11:04:34 +08:00
|
|
|
|
|
|
|
msg.deleteMessages = []*msgstream.DeleteMsg{}
|
|
|
|
// send again shall trigger empty buffer flush
|
|
|
|
delNode.Operate([]flowgraph.Msg{fgMsg})
|
2021-10-11 16:31:44 +08:00
|
|
|
})
|
2021-08-28 10:12:00 +08:00
|
|
|
}
|