2021-08-25 11:41:52 +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.
|
|
|
|
|
|
|
|
package datanode
|
|
|
|
|
|
|
|
import (
|
2021-08-28 10:12:00 +08:00
|
|
|
"encoding/binary"
|
2021-08-25 11:41:52 +08:00
|
|
|
"testing"
|
|
|
|
|
2021-08-28 10:12:00 +08:00
|
|
|
"github.com/bits-and-blooms/bloom/v3"
|
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-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-09-08 10:41:59 +08:00
|
|
|
func TestFlowGraphDeleteNode_newDeleteNode(te *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
replica Replica
|
|
|
|
|
|
|
|
description string
|
|
|
|
}{
|
2021-09-09 15:36:01 +08:00
|
|
|
{&SegmentReplica{}, "pointer of SegmentReplica"},
|
2021-09-08 10:41:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
te.Run(test.description, func(t *testing.T) {
|
2021-09-28 18:22:16 +08:00
|
|
|
dn := newDeleteNode(test.replica, "", make(chan *flushMsg))
|
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-09-08 10:41:59 +08:00
|
|
|
func TestFlowGraphDeleteNode_Operate(te *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
invalidIn []Msg
|
|
|
|
validIn []Msg
|
|
|
|
|
|
|
|
description string
|
|
|
|
}{
|
|
|
|
{[]Msg{}, nil,
|
|
|
|
"Invalid input length == 0"},
|
2021-09-26 10:43:57 +08:00
|
|
|
{[]Msg{&flowGraphMsg{}, &flowGraphMsg{}, &flowGraphMsg{}}, nil,
|
2021-09-08 10:41:59 +08:00
|
|
|
"Invalid input length == 3"},
|
2021-09-26 10:43:57 +08:00
|
|
|
{[]Msg{&flowGraphMsg{}}, nil,
|
2021-09-08 10:41:59 +08:00
|
|
|
"Invalid input length == 1 but input message is not msgStreamMsg"},
|
|
|
|
{nil, []Msg{&MsgStreamMsg{}},
|
|
|
|
"valid input"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
te.Run(test.description, func(t *testing.T) {
|
2021-09-28 18:22:16 +08:00
|
|
|
flushCh := make(chan *flushMsg, 10)
|
|
|
|
dn := deleteNode{flushCh: flushCh}
|
2021-09-08 10:41:59 +08:00
|
|
|
if test.invalidIn != nil {
|
|
|
|
rt := dn.Operate(test.invalidIn)
|
|
|
|
assert.Empty(t, rt)
|
|
|
|
} else {
|
2021-09-28 18:22:16 +08:00
|
|
|
flushCh <- &flushMsg{0, 100, 10, 1}
|
2021-09-08 10:41:59 +08:00
|
|
|
rt := dn.Operate(test.validIn)
|
|
|
|
assert.Empty(t, rt)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-08-25 11:41:52 +08:00
|
|
|
}
|
2021-08-28 10:12:00 +08:00
|
|
|
|
2021-09-08 10:41:59 +08:00
|
|
|
func Test_GetSegmentsByPKs(t *testing.T) {
|
2021-08-28 10:12:00 +08:00
|
|
|
buf := make([]byte, 8)
|
|
|
|
filter1 := bloom.NewWithEstimates(1000000, 0.01)
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
binary.BigEndian.PutUint64(buf, uint64(i))
|
|
|
|
filter1.Add(buf)
|
|
|
|
}
|
|
|
|
filter2 := bloom.NewWithEstimates(1000000, 0.01)
|
|
|
|
for i := 3; i < 5; i++ {
|
|
|
|
binary.BigEndian.PutUint64(buf, uint64(i))
|
|
|
|
filter2.Add(buf)
|
|
|
|
}
|
|
|
|
segment1 := &Segment{
|
2021-09-27 14:38:00 +08:00
|
|
|
segmentID: 1,
|
|
|
|
channelName: "test",
|
|
|
|
pkFilter: filter1,
|
2021-08-28 10:12:00 +08:00
|
|
|
}
|
|
|
|
segment2 := &Segment{
|
2021-09-27 14:38:00 +08:00
|
|
|
segmentID: 2,
|
|
|
|
channelName: "test",
|
|
|
|
pkFilter: filter1,
|
2021-08-28 10:12:00 +08:00
|
|
|
}
|
|
|
|
segment3 := &Segment{
|
2021-09-27 14:38:00 +08:00
|
|
|
segmentID: 3,
|
|
|
|
channelName: "test",
|
|
|
|
pkFilter: filter1,
|
2021-08-28 10:12:00 +08:00
|
|
|
}
|
|
|
|
segment4 := &Segment{
|
2021-09-27 14:38:00 +08:00
|
|
|
segmentID: 4,
|
|
|
|
channelName: "test",
|
|
|
|
pkFilter: filter2,
|
2021-08-28 10:12:00 +08:00
|
|
|
}
|
|
|
|
segment5 := &Segment{
|
2021-09-27 14:38:00 +08:00
|
|
|
segmentID: 5,
|
|
|
|
channelName: "test",
|
|
|
|
pkFilter: filter2,
|
|
|
|
}
|
|
|
|
segment6 := &Segment{
|
|
|
|
segmentID: 5,
|
|
|
|
channelName: "test_error",
|
|
|
|
pkFilter: filter2,
|
2021-08-28 10:12:00 +08:00
|
|
|
}
|
2021-09-27 14:38:00 +08:00
|
|
|
mockReplica := &mockReplica{}
|
|
|
|
mockReplica.newSegments = make(map[int64]*Segment)
|
|
|
|
mockReplica.normalSegments = make(map[int64]*Segment)
|
|
|
|
mockReplica.flushedSegments = make(map[int64]*Segment)
|
|
|
|
mockReplica.newSegments[segment1.segmentID] = segment1
|
|
|
|
mockReplica.newSegments[segment2.segmentID] = segment2
|
|
|
|
mockReplica.normalSegments[segment3.segmentID] = segment3
|
|
|
|
mockReplica.normalSegments[segment4.segmentID] = segment4
|
|
|
|
mockReplica.flushedSegments[segment5.segmentID] = segment5
|
|
|
|
mockReplica.flushedSegments[segment6.segmentID] = segment6
|
2021-09-28 18:22:16 +08:00
|
|
|
dn := newDeleteNode(mockReplica, "test", make(chan *flushMsg))
|
2021-10-08 12:04:56 +08:00
|
|
|
results := dn.filterSegmentByPK(0, []int64{0, 1, 2, 3, 4})
|
2021-08-28 10:12:00 +08:00
|
|
|
expected := map[int64][]int64{
|
2021-09-27 14:38:00 +08:00
|
|
|
0: {1, 2, 3},
|
|
|
|
1: {1, 2, 3},
|
|
|
|
2: {1, 2, 3},
|
|
|
|
3: {4, 5},
|
|
|
|
4: {4, 5},
|
|
|
|
}
|
|
|
|
for key, value := range expected {
|
2021-09-27 17:07:58 +08:00
|
|
|
assert.ElementsMatch(t, value, results[key])
|
2021-09-27 14:38:00 +08:00
|
|
|
}
|
2021-08-28 10:12:00 +08:00
|
|
|
}
|