2021-04-19 10:09:43 +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.
|
|
|
|
|
2021-01-15 14:38:36 +08:00
|
|
|
package proxynode
|
2020-11-03 14:53:36 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"container/list"
|
2020-11-05 18:01:33 +08:00
|
|
|
"context"
|
2021-03-08 19:39:36 +08:00
|
|
|
"errors"
|
2021-01-23 20:58:46 +08:00
|
|
|
"strconv"
|
2020-11-03 14:53:36 +08:00
|
|
|
"sync"
|
2020-11-11 09:54:01 +08:00
|
|
|
|
2021-03-08 19:39:36 +08:00
|
|
|
"go.uber.org/zap"
|
2021-03-05 10:15:27 +08:00
|
|
|
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/allocator"
|
|
|
|
"github.com/milvus-io/milvus/internal/log"
|
|
|
|
"github.com/milvus-io/milvus/internal/msgstream"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/internalpb"
|
|
|
|
"github.com/milvus-io/milvus/internal/util/trace"
|
2021-02-23 09:58:06 +08:00
|
|
|
"github.com/opentracing/opentracing-go"
|
|
|
|
oplog "github.com/opentracing/opentracing-go/log"
|
2020-11-03 14:53:36 +08:00
|
|
|
)
|
|
|
|
|
2020-11-14 11:24:49 +08:00
|
|
|
type TaskQueue interface {
|
|
|
|
utChan() <-chan int
|
2021-04-07 10:06:17 +08:00
|
|
|
utEmpty() bool
|
2020-11-14 11:24:49 +08:00
|
|
|
utFull() bool
|
|
|
|
addUnissuedTask(t task) error
|
|
|
|
FrontUnissuedTask() task
|
|
|
|
PopUnissuedTask() task
|
|
|
|
AddActiveTask(t task)
|
|
|
|
PopActiveTask(ts Timestamp) task
|
|
|
|
getTaskByReqID(reqID UniqueID) task
|
|
|
|
TaskDoneTest(ts Timestamp) bool
|
|
|
|
Enqueue(t task) error
|
|
|
|
}
|
|
|
|
|
2020-11-07 16:18:23 +08:00
|
|
|
type BaseTaskQueue struct {
|
2020-11-03 14:53:36 +08:00
|
|
|
unissuedTasks *list.List
|
2020-11-12 11:18:23 +08:00
|
|
|
activeTasks map[Timestamp]task
|
2021-05-31 11:40:31 +08:00
|
|
|
utLock sync.RWMutex
|
|
|
|
atLock sync.RWMutex
|
2020-11-14 11:24:49 +08:00
|
|
|
|
|
|
|
// maxTaskNum should keep still
|
|
|
|
maxTaskNum int64
|
|
|
|
|
|
|
|
utBufChan chan int // to block scheduler
|
2020-11-16 17:01:10 +08:00
|
|
|
|
|
|
|
sched *TaskScheduler
|
2020-11-03 14:53:36 +08:00
|
|
|
}
|
|
|
|
|
2020-11-14 11:24:49 +08:00
|
|
|
func (queue *BaseTaskQueue) utChan() <-chan int {
|
|
|
|
return queue.utBufChan
|
|
|
|
}
|
|
|
|
|
2021-04-07 10:06:17 +08:00
|
|
|
func (queue *BaseTaskQueue) utEmpty() bool {
|
2021-05-31 11:40:31 +08:00
|
|
|
queue.utLock.RLock()
|
|
|
|
defer queue.utLock.RUnlock()
|
2020-11-14 11:24:49 +08:00
|
|
|
return queue.unissuedTasks.Len() == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (queue *BaseTaskQueue) utFull() bool {
|
|
|
|
return int64(queue.unissuedTasks.Len()) >= queue.maxTaskNum
|
2020-11-03 14:53:36 +08:00
|
|
|
}
|
|
|
|
|
2020-11-14 11:24:49 +08:00
|
|
|
func (queue *BaseTaskQueue) addUnissuedTask(t task) error {
|
2020-11-03 14:53:36 +08:00
|
|
|
queue.utLock.Lock()
|
|
|
|
defer queue.utLock.Unlock()
|
2020-11-14 11:24:49 +08:00
|
|
|
|
|
|
|
if queue.utFull() {
|
|
|
|
return errors.New("task queue is full")
|
|
|
|
}
|
2020-11-03 14:53:36 +08:00
|
|
|
queue.unissuedTasks.PushBack(t)
|
2020-11-14 11:24:49 +08:00
|
|
|
queue.utBufChan <- 1
|
|
|
|
return nil
|
2020-11-03 14:53:36 +08:00
|
|
|
}
|
|
|
|
|
2020-11-12 11:18:23 +08:00
|
|
|
func (queue *BaseTaskQueue) FrontUnissuedTask() task {
|
2021-05-31 11:40:31 +08:00
|
|
|
queue.utLock.RLock()
|
|
|
|
defer queue.utLock.RUnlock()
|
2020-11-14 11:24:49 +08:00
|
|
|
|
2020-11-03 14:53:36 +08:00
|
|
|
if queue.unissuedTasks.Len() <= 0 {
|
2021-03-08 19:39:36 +08:00
|
|
|
log.Warn("sorry, but the unissued task list is empty!")
|
2020-11-03 14:53:36 +08:00
|
|
|
return nil
|
|
|
|
}
|
2020-11-14 11:24:49 +08:00
|
|
|
|
2020-11-12 11:18:23 +08:00
|
|
|
return queue.unissuedTasks.Front().Value.(task)
|
2020-11-03 14:53:36 +08:00
|
|
|
}
|
|
|
|
|
2020-11-12 11:18:23 +08:00
|
|
|
func (queue *BaseTaskQueue) PopUnissuedTask() task {
|
2020-11-03 14:53:36 +08:00
|
|
|
queue.utLock.Lock()
|
|
|
|
defer queue.utLock.Unlock()
|
2020-11-14 11:24:49 +08:00
|
|
|
|
2020-11-03 14:53:36 +08:00
|
|
|
if queue.unissuedTasks.Len() <= 0 {
|
2021-03-08 19:39:36 +08:00
|
|
|
log.Warn("sorry, but the unissued task list is empty!")
|
2020-11-03 14:53:36 +08:00
|
|
|
return nil
|
|
|
|
}
|
2020-11-14 11:24:49 +08:00
|
|
|
|
2020-11-03 14:53:36 +08:00
|
|
|
ft := queue.unissuedTasks.Front()
|
2020-11-14 11:24:49 +08:00
|
|
|
queue.unissuedTasks.Remove(ft)
|
|
|
|
|
|
|
|
return ft.Value.(task)
|
2020-11-03 14:53:36 +08:00
|
|
|
}
|
|
|
|
|
2020-11-12 11:18:23 +08:00
|
|
|
func (queue *BaseTaskQueue) AddActiveTask(t task) {
|
2020-11-03 14:53:36 +08:00
|
|
|
queue.atLock.Lock()
|
2020-11-14 14:39:52 +08:00
|
|
|
defer queue.atLock.Unlock()
|
2020-11-14 11:24:49 +08:00
|
|
|
|
2020-11-12 11:18:23 +08:00
|
|
|
ts := t.EndTs()
|
2020-11-03 14:53:36 +08:00
|
|
|
_, ok := queue.activeTasks[ts]
|
|
|
|
if ok {
|
2021-03-08 19:39:36 +08:00
|
|
|
log.Debug("proxynode", zap.Uint64("task with timestamp ts already in active task list! ts:", ts))
|
2020-11-03 14:53:36 +08:00
|
|
|
}
|
2020-11-14 11:24:49 +08:00
|
|
|
|
2020-11-03 14:53:36 +08:00
|
|
|
queue.activeTasks[ts] = t
|
|
|
|
}
|
|
|
|
|
2020-11-12 11:18:23 +08:00
|
|
|
func (queue *BaseTaskQueue) PopActiveTask(ts Timestamp) task {
|
2020-11-03 14:53:36 +08:00
|
|
|
queue.atLock.Lock()
|
2020-11-14 14:39:52 +08:00
|
|
|
defer queue.atLock.Unlock()
|
2020-11-14 11:24:49 +08:00
|
|
|
|
2020-11-03 14:53:36 +08:00
|
|
|
t, ok := queue.activeTasks[ts]
|
|
|
|
if ok {
|
|
|
|
delete(queue.activeTasks, ts)
|
|
|
|
return t
|
|
|
|
}
|
2020-11-14 11:24:49 +08:00
|
|
|
|
2021-03-08 19:39:36 +08:00
|
|
|
log.Debug("proxynode", zap.Uint64("task with timestamp ts already in active task list! ts:", ts))
|
2020-11-03 14:53:36 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-12 17:58:05 +08:00
|
|
|
func (queue *BaseTaskQueue) getTaskByReqID(reqID UniqueID) task {
|
2021-05-31 11:40:31 +08:00
|
|
|
queue.utLock.RLock()
|
|
|
|
defer queue.utLock.RUnlock()
|
2020-11-07 16:18:23 +08:00
|
|
|
for e := queue.unissuedTasks.Front(); e != nil; e = e.Next() {
|
2020-11-12 17:58:05 +08:00
|
|
|
if e.Value.(task).ID() == reqID {
|
2020-11-12 11:18:23 +08:00
|
|
|
return e.Value.(task)
|
2020-11-07 16:18:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-31 11:40:31 +08:00
|
|
|
queue.atLock.RLock()
|
|
|
|
defer queue.atLock.RUnlock()
|
2020-11-07 16:18:23 +08:00
|
|
|
for ats := range queue.activeTasks {
|
2020-11-12 17:58:05 +08:00
|
|
|
if queue.activeTasks[ats].ID() == reqID {
|
2020-11-07 16:18:23 +08:00
|
|
|
return queue.activeTasks[ats]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (queue *BaseTaskQueue) TaskDoneTest(ts Timestamp) bool {
|
2021-05-31 11:40:31 +08:00
|
|
|
queue.utLock.RLock()
|
|
|
|
defer queue.utLock.RUnlock()
|
2020-11-03 14:53:36 +08:00
|
|
|
for e := queue.unissuedTasks.Front(); e != nil; e = e.Next() {
|
2020-11-21 16:54:20 +08:00
|
|
|
if e.Value.(task).EndTs() < ts {
|
2020-11-03 14:53:36 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-31 11:40:31 +08:00
|
|
|
queue.atLock.RLock()
|
|
|
|
defer queue.atLock.RUnlock()
|
2020-11-03 14:53:36 +08:00
|
|
|
for ats := range queue.activeTasks {
|
2020-11-21 16:54:20 +08:00
|
|
|
if ats < ts {
|
2020-11-03 14:53:36 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-11-14 11:24:49 +08:00
|
|
|
func (queue *BaseTaskQueue) Enqueue(t task) error {
|
2021-01-22 09:36:18 +08:00
|
|
|
err := t.OnEnqueue()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-15 19:47:13 +08:00
|
|
|
ts, err := queue.sched.tsoAllocator.AllocOne()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-16 17:01:10 +08:00
|
|
|
t.SetTs(ts)
|
2020-11-23 16:52:17 +08:00
|
|
|
|
2021-03-15 19:47:13 +08:00
|
|
|
reqID, err := queue.sched.idAllocator.AllocOne()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-23 16:52:17 +08:00
|
|
|
t.SetID(reqID)
|
|
|
|
|
2020-11-14 11:24:49 +08:00
|
|
|
return queue.addUnissuedTask(t)
|
|
|
|
}
|
|
|
|
|
2020-11-12 11:18:23 +08:00
|
|
|
type DdTaskQueue struct {
|
2020-11-07 16:18:23 +08:00
|
|
|
BaseTaskQueue
|
|
|
|
lock sync.Mutex
|
|
|
|
}
|
|
|
|
|
2020-11-12 11:18:23 +08:00
|
|
|
type DmTaskQueue struct {
|
2020-11-07 16:18:23 +08:00
|
|
|
BaseTaskQueue
|
|
|
|
}
|
|
|
|
|
2021-05-31 11:40:31 +08:00
|
|
|
func (queue *DmTaskQueue) getPChanStatistics(pchan pChan) (pChanStatistics, error) {
|
|
|
|
queue.atLock.RLock()
|
|
|
|
defer queue.atLock.RUnlock()
|
|
|
|
|
|
|
|
stats := pChanStatistics{
|
|
|
|
minTs: 0,
|
|
|
|
maxTs: ^uint64(0),
|
|
|
|
invalid: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, t := range queue.activeTasks {
|
|
|
|
dmlT, _ := t.(dmlTask)
|
|
|
|
stat, err := dmlT.getStatistics(pchan)
|
|
|
|
if err != nil {
|
|
|
|
return pChanStatistics{invalid: true}, nil
|
|
|
|
}
|
|
|
|
if stat.minTs < stats.minTs {
|
|
|
|
stats.minTs = stat.minTs
|
|
|
|
}
|
|
|
|
if stat.maxTs > stats.maxTs {
|
|
|
|
stats.maxTs = stat.maxTs
|
|
|
|
}
|
|
|
|
stats.invalid = false
|
|
|
|
}
|
|
|
|
|
|
|
|
return stats, nil
|
|
|
|
}
|
|
|
|
|
2020-11-12 11:18:23 +08:00
|
|
|
type DqTaskQueue struct {
|
2020-11-07 16:18:23 +08:00
|
|
|
BaseTaskQueue
|
|
|
|
}
|
|
|
|
|
2020-11-12 11:18:23 +08:00
|
|
|
func (queue *DdTaskQueue) Enqueue(t task) error {
|
2020-11-03 14:53:36 +08:00
|
|
|
queue.lock.Lock()
|
|
|
|
defer queue.lock.Unlock()
|
2020-11-26 16:01:31 +08:00
|
|
|
return queue.BaseTaskQueue.Enqueue(t)
|
2020-11-03 14:53:36 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 17:01:10 +08:00
|
|
|
func NewDdTaskQueue(sched *TaskScheduler) *DdTaskQueue {
|
2020-11-12 11:18:23 +08:00
|
|
|
return &DdTaskQueue{
|
|
|
|
BaseTaskQueue: BaseTaskQueue{
|
|
|
|
unissuedTasks: list.New(),
|
|
|
|
activeTasks: make(map[Timestamp]task),
|
2020-11-14 11:24:49 +08:00
|
|
|
maxTaskNum: 1024,
|
|
|
|
utBufChan: make(chan int, 1024),
|
2020-11-16 17:01:10 +08:00
|
|
|
sched: sched,
|
2020-11-12 11:18:23 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-16 17:01:10 +08:00
|
|
|
func NewDmTaskQueue(sched *TaskScheduler) *DmTaskQueue {
|
2020-11-12 11:18:23 +08:00
|
|
|
return &DmTaskQueue{
|
|
|
|
BaseTaskQueue: BaseTaskQueue{
|
|
|
|
unissuedTasks: list.New(),
|
|
|
|
activeTasks: make(map[Timestamp]task),
|
2020-11-14 11:24:49 +08:00
|
|
|
maxTaskNum: 1024,
|
|
|
|
utBufChan: make(chan int, 1024),
|
2020-11-16 17:01:10 +08:00
|
|
|
sched: sched,
|
2020-11-12 11:18:23 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-16 17:01:10 +08:00
|
|
|
func NewDqTaskQueue(sched *TaskScheduler) *DqTaskQueue {
|
2020-11-12 11:18:23 +08:00
|
|
|
return &DqTaskQueue{
|
|
|
|
BaseTaskQueue: BaseTaskQueue{
|
|
|
|
unissuedTasks: list.New(),
|
|
|
|
activeTasks: make(map[Timestamp]task),
|
2020-11-14 11:24:49 +08:00
|
|
|
maxTaskNum: 1024,
|
|
|
|
utBufChan: make(chan int, 1024),
|
2020-11-16 17:01:10 +08:00
|
|
|
sched: sched,
|
2020-11-12 11:18:23 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-05 18:01:33 +08:00
|
|
|
type TaskScheduler struct {
|
2020-11-14 11:24:49 +08:00
|
|
|
DdQueue TaskQueue
|
2021-05-31 11:40:31 +08:00
|
|
|
DmQueue *DmTaskQueue
|
2020-11-14 11:24:49 +08:00
|
|
|
DqQueue TaskQueue
|
2020-11-03 14:53:36 +08:00
|
|
|
|
2020-11-12 12:04:12 +08:00
|
|
|
idAllocator *allocator.IDAllocator
|
2021-05-21 13:11:21 +08:00
|
|
|
tsoAllocator *TimestampAllocator
|
2020-11-11 09:54:01 +08:00
|
|
|
|
2020-11-05 18:01:33 +08:00
|
|
|
wg sync.WaitGroup
|
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
2021-02-08 14:30:54 +08:00
|
|
|
|
|
|
|
msFactory msgstream.Factory
|
2020-11-03 14:53:36 +08:00
|
|
|
}
|
|
|
|
|
2020-11-11 09:54:01 +08:00
|
|
|
func NewTaskScheduler(ctx context.Context,
|
2020-11-12 12:04:12 +08:00
|
|
|
idAllocator *allocator.IDAllocator,
|
2021-05-21 13:11:21 +08:00
|
|
|
tsoAllocator *TimestampAllocator,
|
2021-02-08 14:30:54 +08:00
|
|
|
factory msgstream.Factory) (*TaskScheduler, error) {
|
2020-11-11 09:54:01 +08:00
|
|
|
ctx1, cancel := context.WithCancel(ctx)
|
|
|
|
s := &TaskScheduler{
|
|
|
|
idAllocator: idAllocator,
|
|
|
|
tsoAllocator: tsoAllocator,
|
|
|
|
ctx: ctx1,
|
|
|
|
cancel: cancel,
|
2021-02-08 14:30:54 +08:00
|
|
|
msFactory: factory,
|
2020-11-11 09:54:01 +08:00
|
|
|
}
|
2020-11-16 17:01:10 +08:00
|
|
|
s.DdQueue = NewDdTaskQueue(s)
|
|
|
|
s.DmQueue = NewDmTaskQueue(s)
|
|
|
|
s.DqQueue = NewDqTaskQueue(s)
|
2020-11-11 09:54:01 +08:00
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2020-11-12 11:18:23 +08:00
|
|
|
func (sched *TaskScheduler) scheduleDdTask() task {
|
2020-11-03 14:53:36 +08:00
|
|
|
return sched.DdQueue.PopUnissuedTask()
|
|
|
|
}
|
|
|
|
|
2020-11-12 11:18:23 +08:00
|
|
|
func (sched *TaskScheduler) scheduleDmTask() task {
|
2020-11-03 14:53:36 +08:00
|
|
|
return sched.DmQueue.PopUnissuedTask()
|
|
|
|
}
|
|
|
|
|
2020-11-12 11:18:23 +08:00
|
|
|
func (sched *TaskScheduler) scheduleDqTask() task {
|
2020-11-03 14:53:36 +08:00
|
|
|
return sched.DqQueue.PopUnissuedTask()
|
|
|
|
}
|
|
|
|
|
2020-11-12 17:58:05 +08:00
|
|
|
func (sched *TaskScheduler) getTaskByReqID(collMeta UniqueID) task {
|
|
|
|
if t := sched.DdQueue.getTaskByReqID(collMeta); t != nil {
|
2020-11-07 16:18:23 +08:00
|
|
|
return t
|
|
|
|
}
|
2020-11-12 17:58:05 +08:00
|
|
|
if t := sched.DmQueue.getTaskByReqID(collMeta); t != nil {
|
2020-11-07 16:18:23 +08:00
|
|
|
return t
|
|
|
|
}
|
2020-11-12 17:58:05 +08:00
|
|
|
if t := sched.DqQueue.getTaskByReqID(collMeta); t != nil {
|
2020-11-07 16:18:23 +08:00
|
|
|
return t
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-14 11:24:49 +08:00
|
|
|
func (sched *TaskScheduler) processTask(t task, q TaskQueue) {
|
2021-03-25 14:41:46 +08:00
|
|
|
span, ctx := trace.StartSpanFromContext(t.TraceCtx(),
|
2021-02-23 09:58:06 +08:00
|
|
|
opentracing.Tags{
|
|
|
|
"Type": t.Name(),
|
|
|
|
"ID": t.ID(),
|
|
|
|
})
|
|
|
|
defer span.Finish()
|
|
|
|
span.LogFields(oplog.Int64("scheduler process PreExecute", t.ID()))
|
|
|
|
err := t.PreExecute(ctx)
|
2020-11-07 16:18:23 +08:00
|
|
|
|
2020-11-14 11:24:49 +08:00
|
|
|
defer func() {
|
2020-11-12 11:18:23 +08:00
|
|
|
t.Notify(err)
|
2020-11-14 11:24:49 +08:00
|
|
|
}()
|
|
|
|
if err != nil {
|
2021-02-23 09:58:06 +08:00
|
|
|
trace.LogError(span, err)
|
2020-11-14 11:24:49 +08:00
|
|
|
return
|
|
|
|
}
|
2020-11-07 16:18:23 +08:00
|
|
|
|
2021-02-23 09:58:06 +08:00
|
|
|
span.LogFields(oplog.Int64("scheduler process AddActiveTask", t.ID()))
|
2020-11-14 11:24:49 +08:00
|
|
|
q.AddActiveTask(t)
|
2021-02-23 09:58:06 +08:00
|
|
|
|
2020-11-16 17:01:10 +08:00
|
|
|
defer func() {
|
2021-02-23 09:58:06 +08:00
|
|
|
span.LogFields(oplog.Int64("scheduler process PopActiveTask", t.ID()))
|
2020-11-16 17:01:10 +08:00
|
|
|
q.PopActiveTask(t.EndTs())
|
|
|
|
}()
|
2021-02-23 09:58:06 +08:00
|
|
|
span.LogFields(oplog.Int64("scheduler process Execute", t.ID()))
|
|
|
|
err = t.Execute(ctx)
|
2020-11-14 11:24:49 +08:00
|
|
|
if err != nil {
|
2021-02-23 09:58:06 +08:00
|
|
|
trace.LogError(span, err)
|
2020-11-14 11:24:49 +08:00
|
|
|
return
|
2020-11-07 16:18:23 +08:00
|
|
|
}
|
2021-02-23 09:58:06 +08:00
|
|
|
span.LogFields(oplog.Int64("scheduler process PostExecute", t.ID()))
|
|
|
|
err = t.PostExecute(ctx)
|
2020-11-05 18:01:33 +08:00
|
|
|
}
|
|
|
|
|
2020-11-14 11:24:49 +08:00
|
|
|
func (sched *TaskScheduler) definitionLoop() {
|
2020-11-05 18:01:33 +08:00
|
|
|
defer sched.wg.Done()
|
|
|
|
for {
|
2020-11-14 11:24:49 +08:00
|
|
|
select {
|
|
|
|
case <-sched.ctx.Done():
|
2020-11-05 18:01:33 +08:00
|
|
|
return
|
2020-11-14 11:24:49 +08:00
|
|
|
case <-sched.DdQueue.utChan():
|
2021-04-07 10:06:17 +08:00
|
|
|
if !sched.DdQueue.utEmpty() {
|
2020-11-14 11:24:49 +08:00
|
|
|
t := sched.scheduleDdTask()
|
|
|
|
sched.processTask(t, sched.DdQueue)
|
|
|
|
}
|
2020-11-03 14:53:36 +08:00
|
|
|
}
|
2020-11-14 11:24:49 +08:00
|
|
|
}
|
|
|
|
}
|
2020-11-05 18:01:33 +08:00
|
|
|
|
2020-11-14 11:24:49 +08:00
|
|
|
func (sched *TaskScheduler) manipulationLoop() {
|
|
|
|
defer sched.wg.Done()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-sched.ctx.Done():
|
|
|
|
return
|
|
|
|
case <-sched.DmQueue.utChan():
|
2021-04-07 10:06:17 +08:00
|
|
|
if !sched.DmQueue.utEmpty() {
|
2020-11-14 11:24:49 +08:00
|
|
|
t := sched.scheduleDmTask()
|
|
|
|
go sched.processTask(t, sched.DmQueue)
|
2020-11-03 14:53:36 +08:00
|
|
|
}
|
2020-11-14 11:24:49 +08:00
|
|
|
}
|
2020-11-05 18:01:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sched *TaskScheduler) queryLoop() {
|
|
|
|
defer sched.wg.Done()
|
2020-11-07 16:18:23 +08:00
|
|
|
|
|
|
|
for {
|
2020-11-14 11:24:49 +08:00
|
|
|
select {
|
|
|
|
case <-sched.ctx.Done():
|
2020-11-07 16:18:23 +08:00
|
|
|
return
|
2020-11-14 11:24:49 +08:00
|
|
|
case <-sched.DqQueue.utChan():
|
2021-04-07 10:06:17 +08:00
|
|
|
if !sched.DqQueue.utEmpty() {
|
2020-11-14 11:24:49 +08:00
|
|
|
t := sched.scheduleDqTask()
|
|
|
|
go sched.processTask(t, sched.DqQueue)
|
2020-11-16 17:01:10 +08:00
|
|
|
} else {
|
2021-03-08 19:39:36 +08:00
|
|
|
log.Debug("query queue is empty ...")
|
2020-11-07 16:18:23 +08:00
|
|
|
}
|
2020-11-14 11:24:49 +08:00
|
|
|
}
|
2020-11-07 16:18:23 +08:00
|
|
|
}
|
2020-11-05 18:01:33 +08:00
|
|
|
}
|
|
|
|
|
2021-06-02 10:17:32 +08:00
|
|
|
type searchResultBuf struct {
|
|
|
|
usedVChans map[interface{}]struct{} // set of vChan
|
|
|
|
receivedVChansSet map[interface{}]struct{} // set of vChan
|
|
|
|
receivedSealedSegmentIDsSet map[interface{}]struct{} // set of UniqueID
|
|
|
|
receivedGlobalSegmentIDsSet map[interface{}]struct{} // set of UniqueID
|
|
|
|
resultBuf []*internalpb.SearchResults
|
|
|
|
}
|
|
|
|
|
|
|
|
func newSearchResultBuf() *searchResultBuf {
|
|
|
|
return &searchResultBuf{
|
|
|
|
usedVChans: make(map[interface{}]struct{}),
|
|
|
|
receivedVChansSet: make(map[interface{}]struct{}),
|
|
|
|
receivedSealedSegmentIDsSet: make(map[interface{}]struct{}),
|
|
|
|
receivedGlobalSegmentIDsSet: make(map[interface{}]struct{}),
|
|
|
|
resultBuf: make([]*internalpb.SearchResults, 0),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func setContain(m1, m2 map[interface{}]struct{}) bool {
|
|
|
|
if len(m1) < len(m2) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for k2 := range m2 {
|
|
|
|
_, ok := m1[k2]
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sr *searchResultBuf) readyToReduce() bool {
|
|
|
|
if !setContain(sr.receivedVChansSet, sr.usedVChans) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return setContain(sr.receivedSealedSegmentIDsSet, sr.receivedGlobalSegmentIDsSet)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sr *searchResultBuf) addPartialResult(result *internalpb.SearchResults) {
|
|
|
|
sr.resultBuf = append(sr.resultBuf, result)
|
|
|
|
|
|
|
|
for _, vchan := range result.ChannelIDsSearched {
|
|
|
|
sr.receivedVChansSet[vchan] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, sealedSegment := range result.SealedSegmentIDsSearched {
|
|
|
|
sr.receivedSealedSegmentIDsSet[sealedSegment] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, globalSegment := range result.GlobalSealedSegmentIDs {
|
|
|
|
sr.receivedGlobalSegmentIDsSet[globalSegment] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-21 16:54:20 +08:00
|
|
|
func (sched *TaskScheduler) queryResultLoop() {
|
|
|
|
defer sched.wg.Done()
|
|
|
|
|
2021-03-19 20:16:04 +08:00
|
|
|
queryResultMsgStream, _ := sched.msFactory.NewQueryMsgStream(sched.ctx)
|
2021-03-05 18:16:50 +08:00
|
|
|
queryResultMsgStream.AsConsumer(Params.SearchResultChannelNames, Params.ProxySubName)
|
2021-03-08 19:39:36 +08:00
|
|
|
log.Debug("proxynode", zap.Strings("search result channel names", Params.SearchResultChannelNames))
|
|
|
|
log.Debug("proxynode", zap.String("proxySubName", Params.ProxySubName))
|
2021-05-24 18:19:43 +08:00
|
|
|
|
2021-01-28 20:51:44 +08:00
|
|
|
queryNodeNum := Params.QueryNodeNum
|
2020-11-21 16:54:20 +08:00
|
|
|
|
|
|
|
queryResultMsgStream.Start()
|
|
|
|
defer queryResultMsgStream.Close()
|
|
|
|
|
2021-06-02 10:17:32 +08:00
|
|
|
queryResultBuf := make(map[UniqueID]*searchResultBuf)
|
2021-05-24 18:19:43 +08:00
|
|
|
retrieveResultBuf := make(map[UniqueID][]*internalpb.RetrieveResults)
|
2020-11-21 16:54:20 +08:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case msgPack, ok := <-queryResultMsgStream.Chan():
|
|
|
|
if !ok {
|
2021-03-08 19:39:36 +08:00
|
|
|
log.Debug("buf chan closed")
|
2020-11-21 16:54:20 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if msgPack == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, tsMsg := range msgPack.Msgs {
|
2021-03-25 14:41:46 +08:00
|
|
|
sp, ctx := trace.StartSpanFromContext(tsMsg.TraceCtx())
|
|
|
|
tsMsg.SetTraceCtx(ctx)
|
2021-05-24 18:19:43 +08:00
|
|
|
if searchResultMsg, srOk := tsMsg.(*msgstream.SearchResultMsg); srOk {
|
|
|
|
reqID := searchResultMsg.Base.MsgID
|
|
|
|
reqIDStr := strconv.FormatInt(reqID, 10)
|
|
|
|
t := sched.getTaskByReqID(reqID)
|
|
|
|
if t == nil {
|
|
|
|
log.Debug("proxynode", zap.String("QueryResult GetTaskByReqID failed, reqID = ", reqIDStr))
|
|
|
|
delete(queryResultBuf, reqID)
|
|
|
|
continue
|
|
|
|
}
|
2021-01-23 20:58:46 +08:00
|
|
|
|
2021-06-02 10:17:32 +08:00
|
|
|
st, ok := t.(*SearchTask)
|
|
|
|
if !ok {
|
|
|
|
delete(queryResultBuf, reqID)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-05-24 18:19:43 +08:00
|
|
|
_, ok = queryResultBuf[reqID]
|
|
|
|
if !ok {
|
2021-06-02 10:17:32 +08:00
|
|
|
queryResultBuf[reqID] = newSearchResultBuf()
|
|
|
|
vchans, err := st.getVChannels()
|
|
|
|
if err != nil {
|
|
|
|
delete(queryResultBuf, reqID)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, vchan := range vchans {
|
|
|
|
queryResultBuf[reqID].usedVChans[vchan] = struct{}{}
|
|
|
|
}
|
2021-05-24 18:19:43 +08:00
|
|
|
}
|
2021-06-02 10:17:32 +08:00
|
|
|
queryResultBuf[reqID].addPartialResult(&searchResultMsg.SearchResults)
|
2021-01-23 20:58:46 +08:00
|
|
|
|
2021-05-24 18:19:43 +08:00
|
|
|
//t := sched.getTaskByReqID(reqID)
|
|
|
|
{
|
|
|
|
colName := t.(*SearchTask).query.CollectionName
|
2021-06-02 10:17:32 +08:00
|
|
|
log.Debug("Getcollection", zap.String("collection name", colName), zap.String("reqID", reqIDStr), zap.Int("answer cnt", len(queryResultBuf[reqID].resultBuf)))
|
2021-05-24 18:19:43 +08:00
|
|
|
}
|
2021-06-02 10:17:32 +08:00
|
|
|
|
|
|
|
if queryResultBuf[reqID].readyToReduce() {
|
|
|
|
st.resultBuf <- queryResultBuf[reqID].resultBuf
|
2021-05-24 18:19:43 +08:00
|
|
|
}
|
2021-06-02 10:17:32 +08:00
|
|
|
|
2021-05-24 18:19:43 +08:00
|
|
|
sp.Finish()
|
2021-01-23 20:58:46 +08:00
|
|
|
}
|
2021-05-24 18:19:43 +08:00
|
|
|
if retrieveResultMsg, rtOk := tsMsg.(*msgstream.RetrieveResultMsg); rtOk {
|
|
|
|
reqID := retrieveResultMsg.Base.MsgID
|
|
|
|
reqIDStr := strconv.FormatInt(reqID, 10)
|
2020-11-21 16:54:20 +08:00
|
|
|
t := sched.getTaskByReqID(reqID)
|
2021-05-24 18:19:43 +08:00
|
|
|
if t == nil {
|
|
|
|
log.Debug("proxynode", zap.String("RetrieveResult GetTaskByReqID failed, reqID = ", reqIDStr))
|
|
|
|
delete(retrieveResultBuf, reqID)
|
|
|
|
continue
|
|
|
|
}
|
2021-01-23 20:58:46 +08:00
|
|
|
|
2021-05-24 18:19:43 +08:00
|
|
|
_, ok = retrieveResultBuf[reqID]
|
|
|
|
if !ok {
|
|
|
|
retrieveResultBuf[reqID] = make([]*internalpb.RetrieveResults, 0)
|
|
|
|
}
|
|
|
|
retrieveResultBuf[reqID] = append(retrieveResultBuf[reqID], &retrieveResultMsg.RetrieveResults)
|
|
|
|
|
|
|
|
{
|
|
|
|
colName := t.(*RetrieveTask).retrieve.CollectionName
|
|
|
|
log.Debug("Getcollection", zap.String("collection name", colName), zap.String("reqID", reqIDStr), zap.Int("answer cnt", len(retrieveResultBuf[reqID])))
|
|
|
|
}
|
|
|
|
if len(retrieveResultBuf[reqID]) == queryNodeNum {
|
|
|
|
t := sched.getTaskByReqID(reqID)
|
|
|
|
if t != nil {
|
|
|
|
rt, ok := t.(*RetrieveTask)
|
|
|
|
if ok {
|
|
|
|
rt.resultBuf <- retrieveResultBuf[reqID]
|
|
|
|
delete(retrieveResultBuf, reqID)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
}
|
2020-11-21 16:54:20 +08:00
|
|
|
}
|
2021-05-24 18:19:43 +08:00
|
|
|
sp.Finish()
|
2020-11-21 16:54:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-sched.ctx.Done():
|
2021-03-08 19:39:36 +08:00
|
|
|
log.Debug("proxynode server is closed ...")
|
2020-11-21 16:54:20 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-11 09:54:01 +08:00
|
|
|
func (sched *TaskScheduler) Start() error {
|
2020-11-14 11:24:49 +08:00
|
|
|
sched.wg.Add(1)
|
2020-11-05 18:01:33 +08:00
|
|
|
go sched.definitionLoop()
|
2020-11-14 11:24:49 +08:00
|
|
|
|
|
|
|
sched.wg.Add(1)
|
2020-11-05 18:01:33 +08:00
|
|
|
go sched.manipulationLoop()
|
2020-11-14 11:24:49 +08:00
|
|
|
|
|
|
|
sched.wg.Add(1)
|
2020-11-05 18:01:33 +08:00
|
|
|
go sched.queryLoop()
|
|
|
|
|
2020-11-21 16:54:20 +08:00
|
|
|
sched.wg.Add(1)
|
|
|
|
go sched.queryResultLoop()
|
|
|
|
|
2020-11-03 14:53:36 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-05 18:01:33 +08:00
|
|
|
func (sched *TaskScheduler) Close() {
|
|
|
|
sched.cancel()
|
|
|
|
sched.wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sched *TaskScheduler) TaskDoneTest(ts Timestamp) bool {
|
2020-11-03 14:53:36 +08:00
|
|
|
ddTaskDone := sched.DdQueue.TaskDoneTest(ts)
|
|
|
|
dmTaskDone := sched.DmQueue.TaskDoneTest(ts)
|
2020-11-28 19:06:48 +08:00
|
|
|
//dqTaskDone := sched.DqQueue.TaskDoneTest(ts)
|
|
|
|
return ddTaskDone && dmTaskDone && true
|
2020-11-03 14:53:36 +08:00
|
|
|
}
|
2021-05-31 11:40:31 +08:00
|
|
|
|
|
|
|
func (sched *TaskScheduler) getPChanStatistics(pchan pChan) (pChanStatistics, error) {
|
|
|
|
return sched.DmQueue.getPChanStatistics(pchan)
|
|
|
|
}
|