2020-11-03 14:53:36 +08:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/list"
|
2020-11-05 18:01:33 +08:00
|
|
|
"context"
|
2020-11-03 14:53:36 +08:00
|
|
|
"log"
|
|
|
|
"sync"
|
2020-11-11 09:54:01 +08:00
|
|
|
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/allocator"
|
2020-11-03 14:53:36 +08:00
|
|
|
)
|
|
|
|
|
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-07 16:18:23 +08:00
|
|
|
func (queue *BaseTaskQueue) Empty() bool {
|
2020-11-03 14:53:36 +08:00
|
|
|
queue.utLock.Lock()
|
|
|
|
defer queue.utLock.Unlock()
|
|
|
|
queue.atLock.Lock()
|
|
|
|
defer queue.atLock.Unlock()
|
|
|
|
return queue.unissuedTasks.Len() <= 0 && len(queue.activeTasks) <= 0
|
|
|
|
}
|
|
|
|
|
2020-11-12 11:18:23 +08:00
|
|
|
func (queue *BaseTaskQueue) AddUnissuedTask(t task) {
|
2020-11-03 14:53:36 +08:00
|
|
|
queue.utLock.Lock()
|
|
|
|
defer queue.utLock.Unlock()
|
|
|
|
queue.unissuedTasks.PushBack(t)
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
if queue.unissuedTasks.Len() <= 0 {
|
|
|
|
log.Fatal("sorry, but the unissued task list is empty!")
|
|
|
|
return nil
|
|
|
|
}
|
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()
|
|
|
|
if queue.unissuedTasks.Len() <= 0 {
|
|
|
|
log.Fatal("sorry, but the unissued task list is empty!")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ft := queue.unissuedTasks.Front()
|
2020-11-12 11:18:23 +08:00
|
|
|
return queue.unissuedTasks.Remove(ft).(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()
|
|
|
|
defer queue.atLock.Lock()
|
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
|
|
|
}
|
|
|
|
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()
|
|
|
|
defer queue.atLock.Lock()
|
|
|
|
t, ok := queue.activeTasks[ts]
|
|
|
|
if ok {
|
|
|
|
delete(queue.activeTasks, ts)
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
log.Fatalf("sorry, but the timestamp %d was not found in the active task list!", ts)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-12 11:18:23 +08:00
|
|
|
func (queue *BaseTaskQueue) getTaskByReqId(reqId UniqueID) task {
|
2020-11-07 16:18:23 +08:00
|
|
|
queue.utLock.Lock()
|
|
|
|
defer queue.utLock.Lock()
|
|
|
|
for e := queue.unissuedTasks.Front(); e != nil; e = e.Next() {
|
2020-11-12 11:18:23 +08:00
|
|
|
if e.Value.(task).Id() == reqId {
|
|
|
|
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 11:18:23 +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-12 11:18:23 +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 {
|
|
|
|
if ats >= ts {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
// TODO: set Ts, ReqId, ProxyId
|
|
|
|
queue.AddUnissuedTask(t)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-12 11:18:23 +08:00
|
|
|
func (queue *DmTaskQueue) Enqueue(t task) error {
|
2020-11-03 14:53:36 +08:00
|
|
|
// TODO: set Ts, ReqId, ProxyId
|
|
|
|
queue.AddUnissuedTask(t)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-12 11:18:23 +08:00
|
|
|
func (queue *DqTaskQueue) Enqueue(t task) error {
|
2020-11-03 14:53:36 +08:00
|
|
|
// TODO: set Ts, ReqId, ProxyId
|
|
|
|
queue.AddUnissuedTask(t)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-12 11:18:23 +08:00
|
|
|
func NewDdTaskQueue() *DdTaskQueue {
|
|
|
|
return &DdTaskQueue{
|
|
|
|
BaseTaskQueue: BaseTaskQueue{
|
|
|
|
unissuedTasks: list.New(),
|
|
|
|
activeTasks: make(map[Timestamp]task),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDmTaskQueue() *DmTaskQueue {
|
|
|
|
return &DmTaskQueue{
|
|
|
|
BaseTaskQueue: BaseTaskQueue{
|
|
|
|
unissuedTasks: list.New(),
|
|
|
|
activeTasks: make(map[Timestamp]task),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDqTaskQueue() *DqTaskQueue {
|
|
|
|
return &DqTaskQueue{
|
|
|
|
BaseTaskQueue: BaseTaskQueue{
|
|
|
|
unissuedTasks: list.New(),
|
|
|
|
activeTasks: make(map[Timestamp]task),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-05 18:01:33 +08:00
|
|
|
type TaskScheduler struct {
|
2020-11-12 11:18:23 +08:00
|
|
|
DdQueue *DdTaskQueue
|
|
|
|
DmQueue *DmTaskQueue
|
|
|
|
DqQueue *DqTaskQueue
|
2020-11-03 14:53:36 +08:00
|
|
|
|
2020-11-11 09:54:01 +08:00
|
|
|
idAllocator *allocator.IdAllocator
|
|
|
|
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,
|
|
|
|
idAllocator *allocator.IdAllocator,
|
|
|
|
tsoAllocator *allocator.TimestampAllocator) (*TaskScheduler, error) {
|
|
|
|
ctx1, cancel := context.WithCancel(ctx)
|
|
|
|
s := &TaskScheduler{
|
2020-11-12 11:18:23 +08:00
|
|
|
DdQueue: NewDdTaskQueue(),
|
|
|
|
DmQueue: NewDmTaskQueue(),
|
|
|
|
DqQueue: NewDqTaskQueue(),
|
2020-11-11 09:54:01 +08:00
|
|
|
idAllocator: idAllocator,
|
|
|
|
tsoAllocator: tsoAllocator,
|
|
|
|
ctx: ctx1,
|
|
|
|
cancel: cancel,
|
|
|
|
}
|
|
|
|
|
|
|
|
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 11:18:23 +08:00
|
|
|
func (sched *TaskScheduler) getTaskByReqId(reqId UniqueID) task {
|
2020-11-07 16:18:23 +08:00
|
|
|
if t := sched.DdQueue.getTaskByReqId(reqId); t != nil {
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
if t := sched.DmQueue.getTaskByReqId(reqId); t != nil {
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
if t := sched.DqQueue.getTaskByReqId(reqId); t != nil {
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-05 18:01:33 +08:00
|
|
|
func (sched *TaskScheduler) definitionLoop() {
|
|
|
|
defer sched.wg.Done()
|
|
|
|
defer sched.cancel()
|
2020-11-07 16:18:23 +08:00
|
|
|
|
|
|
|
for {
|
|
|
|
if sched.DdQueue.Empty() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
//sched.DdQueue.atLock.Lock()
|
|
|
|
t := sched.scheduleDdTask()
|
|
|
|
|
2020-11-12 11:18:23 +08:00
|
|
|
err := t.PreExecute()
|
2020-11-07 16:18:23 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-11-12 11:18:23 +08:00
|
|
|
err = t.Execute()
|
2020-11-07 16:18:23 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("execute definition task failed, error = %v", err)
|
|
|
|
}
|
2020-11-12 11:18:23 +08:00
|
|
|
t.Notify(err)
|
2020-11-07 16:18:23 +08:00
|
|
|
|
|
|
|
sched.DdQueue.AddActiveTask(t)
|
|
|
|
|
2020-11-12 11:18:23 +08:00
|
|
|
t.WaitToFinish()
|
|
|
|
t.PostExecute()
|
2020-11-07 16:18:23 +08:00
|
|
|
|
2020-11-12 11:18:23 +08:00
|
|
|
sched.DdQueue.PopActiveTask(t.EndTs())
|
2020-11-07 16:18:23 +08:00
|
|
|
}
|
2020-11-05 18:01:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sched *TaskScheduler) manipulationLoop() {
|
|
|
|
defer sched.wg.Done()
|
|
|
|
defer sched.cancel()
|
|
|
|
|
|
|
|
for {
|
|
|
|
if sched.DmQueue.Empty() {
|
|
|
|
continue
|
2020-11-03 14:53:36 +08:00
|
|
|
}
|
2020-11-05 18:01:33 +08:00
|
|
|
|
|
|
|
sched.DmQueue.atLock.Lock()
|
|
|
|
t := sched.scheduleDmTask()
|
|
|
|
|
2020-11-12 11:18:23 +08:00
|
|
|
if err := t.PreExecute(); err != nil {
|
2020-11-05 18:01:33 +08:00
|
|
|
return
|
2020-11-03 14:53:36 +08:00
|
|
|
}
|
2020-11-05 18:01:33 +08:00
|
|
|
|
|
|
|
go func() {
|
2020-11-12 11:18:23 +08:00
|
|
|
err := t.Execute()
|
2020-11-05 18:01:33 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("execute manipulation task failed, error = %v", err)
|
2020-11-03 14:53:36 +08:00
|
|
|
}
|
2020-11-12 11:18:23 +08:00
|
|
|
t.Notify(err)
|
2020-11-05 18:01:33 +08:00
|
|
|
}()
|
|
|
|
|
|
|
|
sched.DmQueue.AddActiveTask(t)
|
|
|
|
sched.DmQueue.atLock.Unlock()
|
|
|
|
|
|
|
|
go func() {
|
2020-11-12 11:18:23 +08:00
|
|
|
t.WaitToFinish()
|
|
|
|
t.PostExecute()
|
2020-11-05 18:01:33 +08:00
|
|
|
|
|
|
|
// remove from active list
|
2020-11-12 11:18:23 +08:00
|
|
|
sched.DmQueue.PopActiveTask(t.EndTs())
|
2020-11-05 18:01:33 +08:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sched *TaskScheduler) queryLoop() {
|
|
|
|
defer sched.wg.Done()
|
|
|
|
defer sched.cancel()
|
2020-11-07 16:18:23 +08:00
|
|
|
|
|
|
|
for {
|
|
|
|
if sched.DqQueue.Empty() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
sched.DqQueue.atLock.Lock()
|
|
|
|
t := sched.scheduleDqTask()
|
|
|
|
|
2020-11-12 11:18:23 +08:00
|
|
|
if err := t.PreExecute(); err != nil {
|
2020-11-07 16:18:23 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
2020-11-12 11:18:23 +08:00
|
|
|
err := t.Execute()
|
2020-11-07 16:18:23 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("execute query task failed, error = %v", err)
|
|
|
|
}
|
2020-11-12 11:18:23 +08:00
|
|
|
t.Notify(err)
|
2020-11-07 16:18:23 +08:00
|
|
|
}()
|
|
|
|
|
|
|
|
sched.DqQueue.AddActiveTask(t)
|
|
|
|
sched.DqQueue.atLock.Unlock()
|
|
|
|
|
|
|
|
go func() {
|
2020-11-12 11:18:23 +08:00
|
|
|
t.WaitToFinish()
|
|
|
|
t.PostExecute()
|
2020-11-07 16:18:23 +08:00
|
|
|
|
|
|
|
// remove from active list
|
2020-11-12 11:18:23 +08:00
|
|
|
sched.DqQueue.PopActiveTask(t.EndTs())
|
2020-11-07 16:18:23 +08:00
|
|
|
}()
|
|
|
|
}
|
2020-11-05 18:01:33 +08:00
|
|
|
}
|
|
|
|
|
2020-11-11 09:54:01 +08:00
|
|
|
func (sched *TaskScheduler) Start() error {
|
2020-11-05 18:01:33 +08:00
|
|
|
sched.wg.Add(3)
|
|
|
|
|
|
|
|
go sched.definitionLoop()
|
|
|
|
go sched.manipulationLoop()
|
|
|
|
go sched.queryLoop()
|
|
|
|
|
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)
|
|
|
|
dqTaskDone := sched.DqQueue.TaskDoneTest(ts)
|
|
|
|
return ddTaskDone && dmTaskDone && dqTaskDone
|
|
|
|
}
|