2021-12-16 10:03:23 +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-09-18 09:13:50 +08:00
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
2021-12-16 10:03:23 +08:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2021-09-18 09:13:50 +08:00
|
|
|
//
|
2021-12-16 10:03:23 +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-09-18 09:13:50 +08:00
|
|
|
|
|
|
|
package rootcoord
|
|
|
|
|
|
|
|
import (
|
2022-08-01 16:36:33 +08:00
|
|
|
"container/heap"
|
2021-09-18 09:13:50 +08:00
|
|
|
"context"
|
2021-12-09 11:03:08 +08:00
|
|
|
"errors"
|
2022-08-01 16:36:33 +08:00
|
|
|
"math/rand"
|
2021-12-29 14:35:21 +08:00
|
|
|
"sync"
|
2021-09-18 09:13:50 +08:00
|
|
|
"testing"
|
|
|
|
|
2022-03-03 21:57:56 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/mq/msgstream"
|
|
|
|
"github.com/milvus-io/milvus/internal/mq/msgstream/mqwrapper"
|
2022-04-07 22:05:32 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/dependency"
|
2021-09-18 09:13:50 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/funcutil"
|
2021-12-09 11:03:08 +08:00
|
|
|
|
2021-09-18 09:13:50 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-12-09 11:03:08 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-09-18 09:13:50 +08:00
|
|
|
)
|
|
|
|
|
2022-08-01 16:36:33 +08:00
|
|
|
func TestDmlMsgStream(t *testing.T) {
|
|
|
|
t.Run("RefCnt", func(t *testing.T) {
|
|
|
|
|
|
|
|
dms := &dmlMsgStream{refcnt: 0}
|
|
|
|
assert.Equal(t, int64(0), dms.RefCnt())
|
|
|
|
assert.Equal(t, int64(0), dms.Used())
|
|
|
|
|
|
|
|
dms.IncRefcnt()
|
|
|
|
assert.Equal(t, int64(1), dms.RefCnt())
|
|
|
|
dms.BookUsage()
|
|
|
|
assert.Equal(t, int64(1), dms.Used())
|
|
|
|
|
|
|
|
dms.DecRefCnt()
|
|
|
|
assert.Equal(t, int64(0), dms.RefCnt())
|
|
|
|
assert.Equal(t, int64(1), dms.Used())
|
|
|
|
|
|
|
|
dms.DecRefCnt()
|
|
|
|
assert.Equal(t, int64(0), dms.RefCnt())
|
|
|
|
assert.Equal(t, int64(1), dms.Used())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChannelsHeap(t *testing.T) {
|
|
|
|
chanNum := 16
|
|
|
|
var h channelsHeap
|
|
|
|
h = make([]*dmlMsgStream, 0, chanNum)
|
|
|
|
|
|
|
|
for i := int64(0); i < int64(chanNum); i++ {
|
|
|
|
dms := &dmlMsgStream{
|
|
|
|
refcnt: 0,
|
|
|
|
used: 0,
|
|
|
|
idx: i,
|
|
|
|
pos: int(i),
|
|
|
|
}
|
|
|
|
h = append(h, dms)
|
|
|
|
}
|
|
|
|
|
|
|
|
check := func(h channelsHeap) bool {
|
|
|
|
for i := 0; i < chanNum; i++ {
|
|
|
|
if h[i].pos != i {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if i*2+1 < chanNum {
|
|
|
|
if !h.Less(i, i*2+1) {
|
|
|
|
t.Log("left", i)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if i*2+2 < chanNum {
|
|
|
|
if !h.Less(i, i*2+2) {
|
|
|
|
t.Log("right", i)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
heap.Init(&h)
|
|
|
|
|
|
|
|
assert.True(t, check(h))
|
|
|
|
|
|
|
|
// add usage for all
|
|
|
|
for i := 0; i < chanNum; i++ {
|
|
|
|
h[0].BookUsage()
|
|
|
|
h[0].IncRefcnt()
|
|
|
|
heap.Fix(&h, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.True(t, check(h))
|
|
|
|
for i := 0; i < chanNum; i++ {
|
|
|
|
assert.EqualValues(t, 1, h[i].RefCnt())
|
|
|
|
assert.EqualValues(t, 1, h[i].Used())
|
|
|
|
}
|
|
|
|
|
|
|
|
randIdx := rand.Intn(chanNum)
|
|
|
|
|
|
|
|
target := h[randIdx]
|
|
|
|
h[randIdx].DecRefCnt()
|
|
|
|
heap.Fix(&h, randIdx)
|
|
|
|
assert.EqualValues(t, 0, target.pos)
|
|
|
|
|
|
|
|
next := heap.Pop(&h).(*dmlMsgStream)
|
|
|
|
|
|
|
|
assert.Equal(t, target, next)
|
|
|
|
}
|
|
|
|
|
2021-09-18 09:13:50 +08:00
|
|
|
func TestDmlChannels(t *testing.T) {
|
|
|
|
const (
|
|
|
|
dmlChanPrefix = "rootcoord-dml"
|
|
|
|
totalDmlChannelNum = 2
|
|
|
|
)
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2022-04-07 22:05:32 +08:00
|
|
|
factory := dependency.NewDefaultFactory(true)
|
2021-09-18 09:13:50 +08:00
|
|
|
Params.Init()
|
|
|
|
|
2021-11-25 10:07:15 +08:00
|
|
|
dml := newDmlChannels(ctx, factory, dmlChanPrefix, totalDmlChannelNum)
|
|
|
|
chanNames := dml.listChannels()
|
2021-09-18 09:13:50 +08:00
|
|
|
assert.Equal(t, 0, len(chanNames))
|
|
|
|
|
|
|
|
randStr := funcutil.RandomString(8)
|
2021-11-25 10:07:15 +08:00
|
|
|
assert.Panics(t, func() { dml.addChannels(randStr) })
|
|
|
|
assert.Panics(t, func() { dml.broadcast([]string{randStr}, nil) })
|
|
|
|
assert.Panics(t, func() { dml.broadcastMark([]string{randStr}, nil) })
|
|
|
|
assert.Panics(t, func() { dml.removeChannels(randStr) })
|
2021-09-18 09:13:50 +08:00
|
|
|
|
2022-08-18 14:24:50 +08:00
|
|
|
chans0 := dml.getChannelNames(2)
|
|
|
|
dml.addChannels(chans0...)
|
2021-11-25 10:07:15 +08:00
|
|
|
assert.Equal(t, 2, dml.getChannelNum())
|
2021-09-18 09:13:50 +08:00
|
|
|
|
2022-08-18 14:24:50 +08:00
|
|
|
chans1 := dml.getChannelNames(1)
|
|
|
|
dml.addChannels(chans1...)
|
2021-11-25 10:07:15 +08:00
|
|
|
assert.Equal(t, 2, dml.getChannelNum())
|
2021-09-18 09:13:50 +08:00
|
|
|
|
2022-09-29 15:54:54 +08:00
|
|
|
chans2 := dml.getChannelNames(totalDmlChannelNum + 1)
|
|
|
|
assert.Nil(t, chans2)
|
|
|
|
|
2022-08-18 14:24:50 +08:00
|
|
|
dml.removeChannels(chans1...)
|
2021-11-25 10:07:15 +08:00
|
|
|
assert.Equal(t, 2, dml.getChannelNum())
|
2021-09-18 09:13:50 +08:00
|
|
|
|
2022-08-18 14:24:50 +08:00
|
|
|
dml.removeChannels(chans0...)
|
2021-11-25 10:07:15 +08:00
|
|
|
assert.Equal(t, 0, dml.getChannelNum())
|
2021-09-18 09:13:50 +08:00
|
|
|
}
|
2021-12-09 11:03:08 +08:00
|
|
|
|
|
|
|
func TestDmChannelsFailure(t *testing.T) {
|
2021-12-29 14:35:21 +08:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
2021-12-09 11:03:08 +08:00
|
|
|
t.Run("Test newDmlChannels", func(t *testing.T) {
|
2021-12-29 14:35:21 +08:00
|
|
|
defer wg.Done()
|
2021-12-09 11:03:08 +08:00
|
|
|
mockFactory := &FailMessageStreamFactory{}
|
|
|
|
assert.Panics(t, func() { newDmlChannels(context.TODO(), mockFactory, "test-newdmlchannel-root", 1) })
|
|
|
|
})
|
|
|
|
|
2021-12-29 14:35:21 +08:00
|
|
|
wg.Add(1)
|
2021-12-09 11:03:08 +08:00
|
|
|
t.Run("Test broadcast", func(t *testing.T) {
|
2021-12-29 14:35:21 +08:00
|
|
|
defer wg.Done()
|
2021-12-09 11:03:08 +08:00
|
|
|
mockFactory := &FailMessageStreamFactory{errBroadcast: true}
|
|
|
|
dml := newDmlChannels(context.TODO(), mockFactory, "test-newdmlchannel-root", 1)
|
2022-08-18 14:24:50 +08:00
|
|
|
chanName0 := dml.getChannelNames(1)[0]
|
2021-12-09 11:03:08 +08:00
|
|
|
dml.addChannels(chanName0)
|
|
|
|
require.Equal(t, 1, dml.getChannelNum())
|
|
|
|
|
|
|
|
err := dml.broadcast([]string{chanName0}, nil)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
v, err := dml.broadcastMark([]string{chanName0}, nil)
|
|
|
|
assert.Empty(t, v)
|
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
2021-12-29 14:35:21 +08:00
|
|
|
wg.Wait()
|
2021-12-09 11:03:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// FailMessageStreamFactory mock MessageStreamFactory failure
|
|
|
|
type FailMessageStreamFactory struct {
|
|
|
|
msgstream.Factory
|
|
|
|
errBroadcast bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FailMessageStreamFactory) NewMsgStream(ctx context.Context) (msgstream.MsgStream, error) {
|
|
|
|
if f.errBroadcast {
|
|
|
|
return &FailMsgStream{errBroadcast: true}, nil
|
|
|
|
}
|
|
|
|
return nil, errors.New("mocked failure")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FailMessageStreamFactory) NewTtMsgStream(ctx context.Context) (msgstream.MsgStream, error) {
|
|
|
|
return nil, errors.New("mocked failure")
|
|
|
|
}
|
|
|
|
|
|
|
|
type FailMsgStream struct {
|
|
|
|
msgstream.MsgStream
|
|
|
|
errBroadcast bool
|
|
|
|
}
|
|
|
|
|
2022-10-25 13:23:30 +08:00
|
|
|
func (ms *FailMsgStream) Start() {}
|
|
|
|
func (ms *FailMsgStream) Close() {}
|
|
|
|
func (ms *FailMsgStream) Chan() <-chan *msgstream.MsgPack { return nil }
|
|
|
|
func (ms *FailMsgStream) AsProducer(channels []string) {}
|
|
|
|
func (ms *FailMsgStream) AsReader(channels []string, subName string) {}
|
|
|
|
func (ms *FailMsgStream) AsConsumer(channels []string, subName string, position mqwrapper.SubscriptionInitialPosition) {
|
2021-12-09 11:03:08 +08:00
|
|
|
}
|
|
|
|
func (ms *FailMsgStream) SetRepackFunc(repackFunc msgstream.RepackFunc) {}
|
|
|
|
func (ms *FailMsgStream) ComputeProduceChannelIndexes(tsMsgs []msgstream.TsMsg) [][]int32 { return nil }
|
|
|
|
func (ms *FailMsgStream) GetProduceChannels() []string { return nil }
|
|
|
|
func (ms *FailMsgStream) Produce(*msgstream.MsgPack) error { return nil }
|
|
|
|
func (ms *FailMsgStream) ProduceMark(*msgstream.MsgPack) (map[string][]msgstream.MessageID, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
func (ms *FailMsgStream) Broadcast(*msgstream.MsgPack) error {
|
|
|
|
if ms.errBroadcast {
|
|
|
|
return errors.New("broadcast error")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (ms *FailMsgStream) BroadcastMark(*msgstream.MsgPack) (map[string][]msgstream.MessageID, error) {
|
|
|
|
if ms.errBroadcast {
|
|
|
|
return nil, errors.New("broadcastMark error")
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
2022-03-15 14:45:22 +08:00
|
|
|
func (ms *FailMsgStream) Consume() *msgstream.MsgPack { return nil }
|
|
|
|
func (ms *FailMsgStream) Seek(offset []*msgstream.MsgPosition) error { return nil }
|
|
|
|
|
|
|
|
func (ms *FailMsgStream) GetLatestMsgID(channel string) (msgstream.MessageID, error) {
|
2021-12-09 11:03:08 +08:00
|
|
|
return nil, nil
|
|
|
|
}
|