2021-11-10 23:55:48 +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-06 20:49:04 +08:00
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
2021-11-10 23:55:48 +08:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2021-09-06 20:49:04 +08:00
|
|
|
//
|
2021-11-10 23:55:48 +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-06 20:49:04 +08:00
|
|
|
|
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-03-15 11:17:55 +08:00
|
|
|
"fmt"
|
2021-09-06 20:49:04 +08:00
|
|
|
"math/rand"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
2023-03-15 11:17:55 +08:00
|
|
|
"time"
|
|
|
|
|
2021-09-06 20:49:04 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-06-25 17:20:43 +08:00
|
|
|
"github.com/stretchr/testify/mock"
|
2023-04-06 19:14:32 +08:00
|
|
|
|
2023-06-09 01:28:37 +08:00
|
|
|
"github.com/milvus-io/milvus-proto/go-api/v2/commonpb"
|
|
|
|
"github.com/milvus-io/milvus-proto/go-api/v2/msgpb"
|
2023-04-11 11:00:31 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/mq/msgstream"
|
2023-04-06 19:14:32 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/funcutil"
|
2021-09-06 20:49:04 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBaseTaskQueue(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
var unissuedTask task
|
|
|
|
var activeTask task
|
|
|
|
|
|
|
|
tsoAllocatorIns := newMockTsoAllocator()
|
2022-10-09 10:06:58 +08:00
|
|
|
queue := newBaseTaskQueue(tsoAllocatorIns)
|
2021-09-06 20:49:04 +08:00
|
|
|
assert.NotNil(t, queue)
|
|
|
|
|
|
|
|
assert.True(t, queue.utEmpty())
|
|
|
|
assert.False(t, queue.utFull())
|
|
|
|
|
|
|
|
st := newDefaultMockTask()
|
|
|
|
stID := st.ID()
|
|
|
|
|
|
|
|
// no task in queue
|
|
|
|
|
|
|
|
unissuedTask = queue.FrontUnissuedTask()
|
|
|
|
assert.Nil(t, unissuedTask)
|
|
|
|
|
|
|
|
unissuedTask = queue.getTaskByReqID(stID)
|
|
|
|
assert.Nil(t, unissuedTask)
|
|
|
|
|
|
|
|
unissuedTask = queue.PopUnissuedTask()
|
|
|
|
assert.Nil(t, unissuedTask)
|
|
|
|
|
|
|
|
// task enqueue, only one task in queue
|
|
|
|
|
|
|
|
err = queue.Enqueue(st)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.False(t, queue.utEmpty())
|
|
|
|
assert.False(t, queue.utFull())
|
|
|
|
assert.Equal(t, 1, queue.unissuedTasks.Len())
|
|
|
|
assert.Equal(t, 1, len(queue.utChan()))
|
|
|
|
|
|
|
|
unissuedTask = queue.FrontUnissuedTask()
|
|
|
|
assert.NotNil(t, unissuedTask)
|
|
|
|
|
|
|
|
unissuedTask = queue.getTaskByReqID(unissuedTask.ID())
|
|
|
|
assert.NotNil(t, unissuedTask)
|
|
|
|
|
|
|
|
unissuedTask = queue.PopUnissuedTask()
|
|
|
|
assert.NotNil(t, unissuedTask)
|
|
|
|
assert.True(t, queue.utEmpty())
|
|
|
|
assert.False(t, queue.utFull())
|
|
|
|
|
|
|
|
// test active list, no task in queue
|
|
|
|
|
|
|
|
activeTask = queue.getTaskByReqID(unissuedTask.ID())
|
|
|
|
assert.Nil(t, activeTask)
|
|
|
|
|
|
|
|
activeTask = queue.PopActiveTask(unissuedTask.ID())
|
|
|
|
assert.Nil(t, activeTask)
|
|
|
|
|
|
|
|
// test active list, no task in unissued list, only one task in active list
|
|
|
|
|
|
|
|
queue.AddActiveTask(unissuedTask)
|
|
|
|
|
|
|
|
activeTask = queue.getTaskByReqID(unissuedTask.ID())
|
|
|
|
assert.NotNil(t, activeTask)
|
|
|
|
|
|
|
|
activeTask = queue.PopActiveTask(unissuedTask.ID())
|
|
|
|
assert.NotNil(t, activeTask)
|
|
|
|
|
|
|
|
// test utFull
|
2021-09-15 19:09:50 +08:00
|
|
|
queue.setMaxTaskNum(10) // not accurate, full also means utBufChan block
|
|
|
|
for i := 0; i < int(queue.getMaxTaskNum()); i++ {
|
2021-09-06 20:49:04 +08:00
|
|
|
err = queue.Enqueue(newDefaultMockTask())
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-09-06 20:49:04 +08:00
|
|
|
}
|
|
|
|
assert.True(t, queue.utFull())
|
|
|
|
err = queue.Enqueue(newDefaultMockTask())
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.Error(t, err)
|
2021-09-06 20:49:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDdTaskQueue(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
var unissuedTask task
|
|
|
|
var activeTask task
|
|
|
|
|
|
|
|
tsoAllocatorIns := newMockTsoAllocator()
|
2022-10-09 10:06:58 +08:00
|
|
|
queue := newDdTaskQueue(tsoAllocatorIns)
|
2021-09-06 20:49:04 +08:00
|
|
|
assert.NotNil(t, queue)
|
|
|
|
|
|
|
|
assert.True(t, queue.utEmpty())
|
|
|
|
assert.False(t, queue.utFull())
|
|
|
|
|
|
|
|
st := newDefaultMockDdlTask()
|
|
|
|
stID := st.ID()
|
|
|
|
|
|
|
|
// no task in queue
|
|
|
|
|
|
|
|
unissuedTask = queue.FrontUnissuedTask()
|
|
|
|
assert.Nil(t, unissuedTask)
|
|
|
|
|
|
|
|
unissuedTask = queue.getTaskByReqID(stID)
|
|
|
|
assert.Nil(t, unissuedTask)
|
|
|
|
|
|
|
|
unissuedTask = queue.PopUnissuedTask()
|
|
|
|
assert.Nil(t, unissuedTask)
|
|
|
|
|
|
|
|
// task enqueue, only one task in queue
|
|
|
|
|
|
|
|
err = queue.Enqueue(st)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.False(t, queue.utEmpty())
|
|
|
|
assert.False(t, queue.utFull())
|
|
|
|
assert.Equal(t, 1, queue.unissuedTasks.Len())
|
|
|
|
assert.Equal(t, 1, len(queue.utChan()))
|
|
|
|
|
|
|
|
unissuedTask = queue.FrontUnissuedTask()
|
|
|
|
assert.NotNil(t, unissuedTask)
|
|
|
|
|
|
|
|
unissuedTask = queue.getTaskByReqID(unissuedTask.ID())
|
|
|
|
assert.NotNil(t, unissuedTask)
|
|
|
|
|
|
|
|
unissuedTask = queue.PopUnissuedTask()
|
|
|
|
assert.NotNil(t, unissuedTask)
|
|
|
|
assert.True(t, queue.utEmpty())
|
|
|
|
assert.False(t, queue.utFull())
|
|
|
|
|
|
|
|
// test active list, no task in queue
|
|
|
|
|
|
|
|
activeTask = queue.getTaskByReqID(unissuedTask.ID())
|
|
|
|
assert.Nil(t, activeTask)
|
|
|
|
|
|
|
|
activeTask = queue.PopActiveTask(unissuedTask.ID())
|
|
|
|
assert.Nil(t, activeTask)
|
|
|
|
|
|
|
|
// test active list, no task in unissued list, only one task in active list
|
|
|
|
|
|
|
|
queue.AddActiveTask(unissuedTask)
|
|
|
|
|
|
|
|
activeTask = queue.getTaskByReqID(unissuedTask.ID())
|
|
|
|
assert.NotNil(t, activeTask)
|
|
|
|
|
|
|
|
activeTask = queue.PopActiveTask(unissuedTask.ID())
|
|
|
|
assert.NotNil(t, activeTask)
|
|
|
|
|
|
|
|
// test utFull
|
2021-09-15 19:09:50 +08:00
|
|
|
queue.setMaxTaskNum(10) // not accurate, full also means utBufChan block
|
|
|
|
for i := 0; i < int(queue.getMaxTaskNum()); i++ {
|
2021-09-06 20:49:04 +08:00
|
|
|
err = queue.Enqueue(newDefaultMockDdlTask())
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-09-06 20:49:04 +08:00
|
|
|
}
|
|
|
|
assert.True(t, queue.utFull())
|
|
|
|
err = queue.Enqueue(newDefaultMockDdlTask())
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.Error(t, err)
|
2021-09-06 20:49:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// test the logic of queue
|
|
|
|
func TestDmTaskQueue_Basic(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
var unissuedTask task
|
|
|
|
var activeTask task
|
|
|
|
|
|
|
|
tsoAllocatorIns := newMockTsoAllocator()
|
2022-10-09 10:06:58 +08:00
|
|
|
queue := newDmTaskQueue(tsoAllocatorIns)
|
2021-09-06 20:49:04 +08:00
|
|
|
assert.NotNil(t, queue)
|
|
|
|
|
|
|
|
assert.True(t, queue.utEmpty())
|
|
|
|
assert.False(t, queue.utFull())
|
|
|
|
|
|
|
|
st := newDefaultMockDmlTask()
|
|
|
|
stID := st.ID()
|
|
|
|
|
|
|
|
// no task in queue
|
|
|
|
unissuedTask = queue.FrontUnissuedTask()
|
|
|
|
assert.Nil(t, unissuedTask)
|
|
|
|
|
|
|
|
unissuedTask = queue.getTaskByReqID(stID)
|
|
|
|
assert.Nil(t, unissuedTask)
|
|
|
|
|
|
|
|
unissuedTask = queue.PopUnissuedTask()
|
|
|
|
assert.Nil(t, unissuedTask)
|
|
|
|
|
|
|
|
// task enqueue, only one task in queue
|
|
|
|
|
|
|
|
err = queue.Enqueue(st)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.False(t, queue.utEmpty())
|
|
|
|
assert.False(t, queue.utFull())
|
|
|
|
assert.Equal(t, 1, queue.unissuedTasks.Len())
|
|
|
|
assert.Equal(t, 1, len(queue.utChan()))
|
|
|
|
|
|
|
|
unissuedTask = queue.FrontUnissuedTask()
|
|
|
|
assert.NotNil(t, unissuedTask)
|
|
|
|
|
|
|
|
unissuedTask = queue.getTaskByReqID(unissuedTask.ID())
|
|
|
|
assert.NotNil(t, unissuedTask)
|
|
|
|
|
|
|
|
unissuedTask = queue.PopUnissuedTask()
|
|
|
|
assert.NotNil(t, unissuedTask)
|
|
|
|
assert.True(t, queue.utEmpty())
|
|
|
|
assert.False(t, queue.utFull())
|
|
|
|
|
|
|
|
// test active list, no task in queue
|
|
|
|
|
|
|
|
activeTask = queue.getTaskByReqID(unissuedTask.ID())
|
|
|
|
assert.Nil(t, activeTask)
|
|
|
|
|
|
|
|
activeTask = queue.PopActiveTask(unissuedTask.ID())
|
|
|
|
assert.Nil(t, activeTask)
|
|
|
|
|
|
|
|
// test active list, no task in unissued list, only one task in active list
|
|
|
|
|
|
|
|
queue.AddActiveTask(unissuedTask)
|
|
|
|
|
|
|
|
activeTask = queue.getTaskByReqID(unissuedTask.ID())
|
|
|
|
assert.NotNil(t, activeTask)
|
|
|
|
|
|
|
|
activeTask = queue.PopActiveTask(unissuedTask.ID())
|
|
|
|
assert.NotNil(t, activeTask)
|
|
|
|
|
|
|
|
// test utFull
|
2021-09-15 19:09:50 +08:00
|
|
|
queue.setMaxTaskNum(10) // not accurate, full also means utBufChan block
|
|
|
|
for i := 0; i < int(queue.getMaxTaskNum()); i++ {
|
2021-09-06 20:49:04 +08:00
|
|
|
err = queue.Enqueue(newDefaultMockDmlTask())
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-09-06 20:49:04 +08:00
|
|
|
}
|
|
|
|
assert.True(t, queue.utFull())
|
|
|
|
err = queue.Enqueue(newDefaultMockDmlTask())
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.Error(t, err)
|
2021-09-06 20:49:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// test the timestamp statistics
|
|
|
|
func TestDmTaskQueue_TimestampStatistics(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
var unissuedTask task
|
|
|
|
|
|
|
|
tsoAllocatorIns := newMockTsoAllocator()
|
2022-10-09 10:06:58 +08:00
|
|
|
queue := newDmTaskQueue(tsoAllocatorIns)
|
2021-09-06 20:49:04 +08:00
|
|
|
assert.NotNil(t, queue)
|
|
|
|
|
|
|
|
st := newDefaultMockDmlTask()
|
|
|
|
stPChans := st.pchans
|
|
|
|
|
|
|
|
err = queue.Enqueue(st)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
stats, err := queue.getPChanStatsInfo()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, len(stPChans), len(stats))
|
|
|
|
unissuedTask = queue.FrontUnissuedTask()
|
|
|
|
assert.NotNil(t, unissuedTask)
|
|
|
|
for _, stat := range stats {
|
|
|
|
assert.Equal(t, unissuedTask.BeginTs(), stat.minTs)
|
|
|
|
assert.Equal(t, unissuedTask.EndTs(), stat.maxTs)
|
|
|
|
}
|
|
|
|
|
|
|
|
unissuedTask = queue.PopUnissuedTask()
|
|
|
|
assert.NotNil(t, unissuedTask)
|
|
|
|
assert.True(t, queue.utEmpty())
|
|
|
|
|
|
|
|
queue.AddActiveTask(unissuedTask)
|
|
|
|
|
|
|
|
queue.PopActiveTask(unissuedTask.ID())
|
|
|
|
|
|
|
|
stats, err = queue.getPChanStatsInfo()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Zero(t, len(stats))
|
|
|
|
}
|
|
|
|
|
2023-03-15 11:17:55 +08:00
|
|
|
// test the timestamp statistics
|
|
|
|
func TestDmTaskQueue_TimestampStatistics2(t *testing.T) {
|
|
|
|
tsoAllocatorIns := newMockTsoAllocator()
|
|
|
|
queue := newDmTaskQueue(tsoAllocatorIns)
|
|
|
|
assert.NotNil(t, queue)
|
|
|
|
|
|
|
|
prefix := funcutil.GenRandomStr()
|
|
|
|
insertNum := 100
|
|
|
|
|
|
|
|
var processWg sync.WaitGroup
|
|
|
|
processWg.Add(1)
|
|
|
|
processCtx, processCancel := context.WithCancel(context.TODO())
|
|
|
|
processCount := insertNum
|
|
|
|
var processCountMut sync.RWMutex
|
|
|
|
go func() {
|
|
|
|
defer processWg.Done()
|
|
|
|
var workerWg sync.WaitGroup
|
|
|
|
workerWg.Add(insertNum)
|
|
|
|
for processCtx.Err() == nil {
|
|
|
|
if queue.utEmpty() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
utTask := queue.PopUnissuedTask()
|
|
|
|
go func(ut task) {
|
|
|
|
defer workerWg.Done()
|
|
|
|
assert.NotNil(t, ut)
|
|
|
|
queue.AddActiveTask(ut)
|
|
|
|
dur := time.Duration(50+rand.Int()%10) * time.Millisecond
|
|
|
|
time.Sleep(dur)
|
|
|
|
queue.PopActiveTask(ut.ID())
|
|
|
|
processCountMut.Lock()
|
|
|
|
defer processCountMut.Unlock()
|
|
|
|
processCount--
|
|
|
|
}(utTask)
|
|
|
|
}
|
|
|
|
workerWg.Wait()
|
|
|
|
}()
|
|
|
|
|
|
|
|
var currPChanStats map[pChan]*pChanStatistics
|
|
|
|
var wgSchedule sync.WaitGroup
|
|
|
|
scheduleCtx, scheduleCancel := context.WithCancel(context.TODO())
|
|
|
|
schedule := func() {
|
|
|
|
defer wgSchedule.Done()
|
|
|
|
ticker := time.NewTicker(time.Millisecond * 10)
|
|
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-scheduleCtx.Done():
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
|
|
|
stats, err := queue.getPChanStatsInfo()
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2023-03-15 11:17:55 +08:00
|
|
|
if currPChanStats == nil {
|
|
|
|
currPChanStats = stats
|
|
|
|
} else {
|
|
|
|
// assure minTs and maxTs will not go back
|
|
|
|
for p, stat := range stats {
|
|
|
|
curInfo, ok := currPChanStats[p]
|
|
|
|
if ok {
|
|
|
|
fmt.Println("stat.minTs", stat.minTs, " ", "curInfo.minTs:", curInfo.minTs)
|
|
|
|
fmt.Println("stat.maxTs", stat.maxTs, " ", "curInfo.minTs:", curInfo.maxTs)
|
|
|
|
assert.True(t, stat.minTs >= curInfo.minTs)
|
|
|
|
curInfo.minTs = stat.minTs
|
|
|
|
assert.True(t, stat.maxTs >= curInfo.maxTs)
|
|
|
|
curInfo.maxTs = stat.maxTs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
wgSchedule.Add(1)
|
|
|
|
go schedule()
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(insertNum)
|
|
|
|
for i := 0; i < insertNum; i++ {
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
time.Sleep(time.Millisecond)
|
|
|
|
st := newDefaultMockDmlTask()
|
|
|
|
vChannels := make([]string, 2)
|
|
|
|
vChannels[0] = prefix + "_1"
|
|
|
|
vChannels[1] = prefix + "_2"
|
|
|
|
st.vchans = vChannels
|
|
|
|
st.pchans = vChannels
|
|
|
|
err := queue.Enqueue(st)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
2023-09-21 09:45:27 +08:00
|
|
|
// time.Sleep(time.Millisecond*100)
|
2023-03-15 11:17:55 +08:00
|
|
|
needLoop := true
|
|
|
|
for needLoop {
|
|
|
|
processCountMut.RLock()
|
|
|
|
needLoop = processCount != 0
|
|
|
|
processCountMut.RUnlock()
|
|
|
|
}
|
|
|
|
processCancel()
|
|
|
|
processWg.Wait()
|
|
|
|
|
|
|
|
scheduleCancel()
|
|
|
|
wgSchedule.Wait()
|
|
|
|
|
|
|
|
stats, err := queue.getPChanStatsInfo()
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2023-03-15 11:17:55 +08:00
|
|
|
assert.Zero(t, len(stats))
|
|
|
|
}
|
|
|
|
|
2021-09-06 20:49:04 +08:00
|
|
|
func TestDqTaskQueue(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
var unissuedTask task
|
|
|
|
var activeTask task
|
|
|
|
|
|
|
|
tsoAllocatorIns := newMockTsoAllocator()
|
2022-10-09 10:06:58 +08:00
|
|
|
queue := newDqTaskQueue(tsoAllocatorIns)
|
2021-09-06 20:49:04 +08:00
|
|
|
assert.NotNil(t, queue)
|
|
|
|
|
|
|
|
assert.True(t, queue.utEmpty())
|
|
|
|
assert.False(t, queue.utFull())
|
|
|
|
|
|
|
|
st := newDefaultMockDqlTask()
|
|
|
|
stID := st.ID()
|
|
|
|
|
|
|
|
// no task in queue
|
|
|
|
|
|
|
|
unissuedTask = queue.FrontUnissuedTask()
|
|
|
|
assert.Nil(t, unissuedTask)
|
|
|
|
|
|
|
|
unissuedTask = queue.getTaskByReqID(stID)
|
|
|
|
assert.Nil(t, unissuedTask)
|
|
|
|
|
|
|
|
unissuedTask = queue.PopUnissuedTask()
|
|
|
|
assert.Nil(t, unissuedTask)
|
|
|
|
|
|
|
|
// task enqueue, only one task in queue
|
|
|
|
|
|
|
|
err = queue.Enqueue(st)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.False(t, queue.utEmpty())
|
|
|
|
assert.False(t, queue.utFull())
|
|
|
|
assert.Equal(t, 1, queue.unissuedTasks.Len())
|
|
|
|
assert.Equal(t, 1, len(queue.utChan()))
|
|
|
|
|
|
|
|
unissuedTask = queue.FrontUnissuedTask()
|
|
|
|
assert.NotNil(t, unissuedTask)
|
|
|
|
|
|
|
|
unissuedTask = queue.getTaskByReqID(unissuedTask.ID())
|
|
|
|
assert.NotNil(t, unissuedTask)
|
|
|
|
|
|
|
|
unissuedTask = queue.PopUnissuedTask()
|
|
|
|
assert.NotNil(t, unissuedTask)
|
|
|
|
assert.True(t, queue.utEmpty())
|
|
|
|
assert.False(t, queue.utFull())
|
|
|
|
|
|
|
|
// test active list, no task in queue
|
|
|
|
|
|
|
|
activeTask = queue.getTaskByReqID(unissuedTask.ID())
|
|
|
|
assert.Nil(t, activeTask)
|
|
|
|
|
|
|
|
activeTask = queue.PopActiveTask(unissuedTask.ID())
|
|
|
|
assert.Nil(t, activeTask)
|
|
|
|
|
|
|
|
// test active list, no task in unissued list, only one task in active list
|
|
|
|
|
|
|
|
queue.AddActiveTask(unissuedTask)
|
|
|
|
|
|
|
|
activeTask = queue.getTaskByReqID(unissuedTask.ID())
|
|
|
|
assert.NotNil(t, activeTask)
|
|
|
|
|
|
|
|
activeTask = queue.PopActiveTask(unissuedTask.ID())
|
|
|
|
assert.NotNil(t, activeTask)
|
|
|
|
|
|
|
|
// test utFull
|
2021-09-15 19:09:50 +08:00
|
|
|
queue.setMaxTaskNum(10) // not accurate, full also means utBufChan block
|
|
|
|
for i := 0; i < int(queue.getMaxTaskNum()); i++ {
|
2021-09-06 20:49:04 +08:00
|
|
|
err = queue.Enqueue(newDefaultMockDqlTask())
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-09-06 20:49:04 +08:00
|
|
|
}
|
|
|
|
assert.True(t, queue.utFull())
|
|
|
|
err = queue.Enqueue(newDefaultMockDqlTask())
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.Error(t, err)
|
2021-09-06 20:49:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTaskScheduler(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
tsoAllocatorIns := newMockTsoAllocator()
|
2021-09-09 19:02:08 +08:00
|
|
|
factory := newSimpleMockMsgStreamFactory()
|
2021-09-06 20:49:04 +08:00
|
|
|
|
2022-10-09 10:06:58 +08:00
|
|
|
sched, err := newTaskScheduler(ctx, tsoAllocatorIns, factory)
|
2021-09-06 20:49:04 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, sched)
|
|
|
|
|
|
|
|
err = sched.Start()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer sched.Close()
|
|
|
|
|
|
|
|
stats, err := sched.getPChanStatistics()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 0, len(stats))
|
|
|
|
|
|
|
|
ddNum := rand.Int() % 10
|
|
|
|
dmNum := rand.Int() % 10
|
|
|
|
dqNum := rand.Int() % 10
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
for i := 0; i < ddNum; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
err := sched.ddQueue.Enqueue(newDefaultMockDdlTask())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
for i := 0; i < dmNum; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
err := sched.dmQueue.Enqueue(newDefaultMockDmlTask())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
for i := 0; i < dqNum; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
err := sched.dqQueue.Enqueue(newDefaultMockDqlTask())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|
2023-04-11 11:00:31 +08:00
|
|
|
|
|
|
|
func TestTaskScheduler_concurrentPushAndPop(t *testing.T) {
|
|
|
|
collectionID := UniqueID(0)
|
|
|
|
collectionName := "col-0"
|
|
|
|
channels := []pChan{"mock-chan-0", "mock-chan-1"}
|
2023-06-25 17:20:43 +08:00
|
|
|
cache := NewMockCache(t)
|
|
|
|
cache.On("GetCollectionID",
|
|
|
|
mock.Anything, // context.Context
|
|
|
|
mock.AnythingOfType("string"),
|
|
|
|
mock.AnythingOfType("string"),
|
|
|
|
).Return(collectionID, nil)
|
2023-04-11 11:00:31 +08:00
|
|
|
globalMetaCache = cache
|
|
|
|
tsoAllocatorIns := newMockTsoAllocator()
|
|
|
|
factory := newSimpleMockMsgStreamFactory()
|
|
|
|
scheduler, err := newTaskScheduler(context.Background(), tsoAllocatorIns, factory)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
run := func(wg *sync.WaitGroup) {
|
|
|
|
defer wg.Done()
|
|
|
|
chMgr := newMockChannelsMgr()
|
|
|
|
chMgr.getChannelsFunc = func(collectionID UniqueID) ([]pChan, error) {
|
|
|
|
return channels, nil
|
|
|
|
}
|
|
|
|
it := &insertTask{
|
|
|
|
ctx: context.Background(),
|
|
|
|
insertMsg: &msgstream.InsertMsg{
|
|
|
|
InsertRequest: msgpb.InsertRequest{
|
|
|
|
Base: &commonpb.MsgBase{},
|
|
|
|
CollectionName: collectionName,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
chMgr: chMgr,
|
|
|
|
}
|
|
|
|
err := scheduler.dmQueue.Enqueue(it)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
task := scheduler.scheduleDmTask()
|
|
|
|
scheduler.dmQueue.AddActiveTask(task)
|
|
|
|
chMgr.getChannelsFunc = func(collectionID UniqueID) ([]pChan, error) {
|
|
|
|
return nil, fmt.Errorf("mock err")
|
|
|
|
}
|
|
|
|
scheduler.dmQueue.PopActiveTask(task.ID()) // assert no panic
|
|
|
|
}
|
|
|
|
|
|
|
|
wg := &sync.WaitGroup{}
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go run(wg)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|