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"
|
2020-11-14 11:24:49 +08:00
|
|
|
"errors"
|
2020-11-03 14:53:36 +08:00
|
|
|
"log"
|
|
|
|
"sync"
|
2020-11-11 09:54:01 +08:00
|
|
|
|
2021-01-18 19:32:08 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/internalpb2"
|
|
|
|
|
2020-11-11 09:54:01 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/allocator"
|
2020-11-21 16:54:20 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/msgstream"
|
2021-01-20 10:02:59 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/msgstream/pulsarms"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/msgstream/util"
|
2020-11-03 14:53:36 +08:00
|
|
|
)
|
|
|
|
|
2020-11-14 11:24:49 +08:00
|
|
|
type TaskQueue interface {
|
|
|
|
utChan() <-chan int
|
2021-01-12 18:03:24 +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
|
2020-11-03 14:53:36 +08:00
|
|
|
utLock sync.Mutex
|
|
|
|
atLock sync.Mutex
|
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-01-12 18:03:24 +08:00
|
|
|
func (queue *BaseTaskQueue) UTEmpty() bool {
|
|
|
|
queue.utLock.Lock()
|
|
|
|
defer queue.utLock.Unlock()
|
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 {
|
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 {
|
2020-11-14 11:24:49 +08:00
|
|
|
log.Panic("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 {
|
|
|
|
log.Fatal("sorry, but the unissued task list is empty!")
|
|
|
|
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 {
|
2020-11-05 18:01:33 +08:00
|
|
|
log.Fatalf("task with timestamp %v already in active task list!", 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
|
|
|
|
2020-11-03 14:53:36 +08:00
|
|
|
log.Fatalf("sorry, but the timestamp %d was not found in the active task list!", ts)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-12 17:58:05 +08:00
|
|
|
func (queue *BaseTaskQueue) getTaskByReqID(reqID UniqueID) task {
|
2020-11-07 16:18:23 +08:00
|
|
|
queue.utLock.Lock()
|
2020-11-14 14:39:52 +08:00
|
|
|
defer queue.utLock.Unlock()
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
queue.atLock.Lock()
|
|
|
|
defer queue.atLock.Unlock()
|
|
|
|
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 {
|
2020-11-03 14:53:36 +08:00
|
|
|
queue.utLock.Lock()
|
|
|
|
defer queue.utLock.Unlock()
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
queue.atLock.Lock()
|
|
|
|
defer queue.atLock.Unlock()
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-11-16 17:01:10 +08:00
|
|
|
ts, _ := queue.sched.tsoAllocator.AllocOne()
|
2021-01-22 09:36:18 +08:00
|
|
|
log.Printf("[NodeImpl] allocate timestamp: %v", ts)
|
2020-11-16 17:01:10 +08:00
|
|
|
t.SetTs(ts)
|
2020-11-23 16:52:17 +08:00
|
|
|
|
|
|
|
reqID, _ := queue.sched.idAllocator.AllocOne()
|
2021-01-22 09:36:18 +08:00
|
|
|
log.Printf("[NodeImpl] allocate reqID: %v", reqID)
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
DmQueue TaskQueue
|
|
|
|
DqQueue TaskQueue
|
2020-11-03 14:53:36 +08:00
|
|
|
|
2020-11-12 12:04:12 +08:00
|
|
|
idAllocator *allocator.IDAllocator
|
2020-11-11 09:54:01 +08:00
|
|
|
tsoAllocator *allocator.TimestampAllocator
|
|
|
|
|
2020-11-05 18:01:33 +08:00
|
|
|
wg sync.WaitGroup
|
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
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,
|
2020-11-11 09:54:01 +08:00
|
|
|
tsoAllocator *allocator.TimestampAllocator) (*TaskScheduler, error) {
|
|
|
|
ctx1, cancel := context.WithCancel(ctx)
|
|
|
|
s := &TaskScheduler{
|
|
|
|
idAllocator: idAllocator,
|
|
|
|
tsoAllocator: tsoAllocator,
|
|
|
|
ctx: ctx1,
|
|
|
|
cancel: cancel,
|
|
|
|
}
|
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) {
|
2020-11-07 16:18:23 +08:00
|
|
|
|
2020-11-14 11:24:49 +08:00
|
|
|
err := t.PreExecute()
|
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-16 17:01:10 +08:00
|
|
|
log.Printf("notify with error: %v", err)
|
2020-11-14 11:24:49 +08:00
|
|
|
}()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-11-07 16:18:23 +08:00
|
|
|
|
2020-11-14 11:24:49 +08:00
|
|
|
q.AddActiveTask(t)
|
2020-11-28 19:06:48 +08:00
|
|
|
log.Printf("task add to active list ...")
|
2020-11-16 17:01:10 +08:00
|
|
|
defer func() {
|
|
|
|
q.PopActiveTask(t.EndTs())
|
|
|
|
log.Printf("pop from active list ...")
|
|
|
|
}()
|
2020-11-07 16:18:23 +08:00
|
|
|
|
2020-11-14 11:24:49 +08:00
|
|
|
err = t.Execute()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("execute definition task failed, error = %v", err)
|
|
|
|
return
|
2020-11-07 16:18:23 +08:00
|
|
|
}
|
2020-11-28 19:06:48 +08:00
|
|
|
log.Printf("task execution done ...")
|
2020-11-14 11:24:49 +08:00
|
|
|
err = t.PostExecute()
|
2020-11-28 19:06:48 +08:00
|
|
|
log.Printf("post execute task done ...")
|
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-01-12 18:03:24 +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-01-12 18:03:24 +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():
|
2020-11-16 17:01:10 +08:00
|
|
|
log.Print("scheduler receive query request ...")
|
2021-01-12 18:03:24 +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 {
|
|
|
|
log.Print("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
|
|
|
}
|
|
|
|
|
2020-11-21 16:54:20 +08:00
|
|
|
func (sched *TaskScheduler) queryResultLoop() {
|
|
|
|
defer sched.wg.Done()
|
|
|
|
|
2021-01-20 10:02:59 +08:00
|
|
|
unmarshal := util.NewUnmarshalDispatcher()
|
|
|
|
queryResultMsgStream := pulsarms.NewPulsarMsgStream(sched.ctx, Params.MsgStreamSearchResultBufSize())
|
2020-11-21 19:12:59 +08:00
|
|
|
queryResultMsgStream.SetPulsarClient(Params.PulsarAddress())
|
2020-12-08 14:41:04 +08:00
|
|
|
queryResultMsgStream.CreatePulsarConsumers(Params.SearchResultChannelNames(),
|
2020-11-21 19:12:59 +08:00
|
|
|
Params.ProxySubName(),
|
2020-11-21 16:54:20 +08:00
|
|
|
unmarshal,
|
2020-11-21 19:12:59 +08:00
|
|
|
Params.MsgStreamSearchResultPulsarBufSize())
|
2020-11-26 16:01:31 +08:00
|
|
|
queryNodeNum := Params.queryNodeNum()
|
2020-11-21 16:54:20 +08:00
|
|
|
|
|
|
|
queryResultMsgStream.Start()
|
|
|
|
defer queryResultMsgStream.Close()
|
|
|
|
|
2021-01-18 19:32:08 +08:00
|
|
|
queryResultBuf := make(map[UniqueID][]*internalpb2.SearchResults)
|
2020-11-21 16:54:20 +08:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case msgPack, ok := <-queryResultMsgStream.Chan():
|
|
|
|
if !ok {
|
|
|
|
log.Print("buf chan closed")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if msgPack == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, tsMsg := range msgPack.Msgs {
|
|
|
|
searchResultMsg, _ := tsMsg.(*msgstream.SearchResultMsg)
|
2021-01-18 19:32:08 +08:00
|
|
|
reqID := searchResultMsg.Base.MsgID
|
2020-11-21 16:54:20 +08:00
|
|
|
_, ok = queryResultBuf[reqID]
|
|
|
|
if !ok {
|
2021-01-18 19:32:08 +08:00
|
|
|
queryResultBuf[reqID] = make([]*internalpb2.SearchResults, 0)
|
2020-11-21 16:54:20 +08:00
|
|
|
}
|
2021-01-18 19:32:08 +08:00
|
|
|
queryResultBuf[reqID] = append(queryResultBuf[reqID], &searchResultMsg.SearchResults)
|
2020-11-26 16:01:31 +08:00
|
|
|
if len(queryResultBuf[reqID]) == queryNodeNum {
|
2020-11-21 16:54:20 +08:00
|
|
|
t := sched.getTaskByReqID(reqID)
|
|
|
|
if t != nil {
|
2021-01-18 19:32:08 +08:00
|
|
|
qt, ok := t.(*SearchTask)
|
2020-11-21 16:54:20 +08:00
|
|
|
if ok {
|
|
|
|
log.Printf("address of query task: %p", qt)
|
|
|
|
qt.resultBuf <- queryResultBuf[reqID]
|
|
|
|
delete(queryResultBuf, reqID)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Printf("task with reqID %v is nil", reqID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-sched.ctx.Done():
|
2021-01-15 14:38:36 +08:00
|
|
|
log.Print("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
|
|
|
}
|