2021-01-22 12:57:23 +08:00
|
|
|
package proxyservice
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
2021-02-23 09:58:06 +08:00
|
|
|
|
|
|
|
"github.com/opentracing/opentracing-go"
|
|
|
|
oplog "github.com/opentracing/opentracing-go/log"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/util/trace"
|
2021-01-22 12:57:23 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type TaskScheduler struct {
|
|
|
|
RegisterLinkTaskQueue TaskQueue
|
|
|
|
RegisterNodeTaskQueue TaskQueue
|
|
|
|
InvalidateCollectionMetaCacheTaskQueue TaskQueue
|
|
|
|
|
|
|
|
wg sync.WaitGroup
|
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewTaskScheduler(ctx context.Context) *TaskScheduler {
|
|
|
|
ctx1, cancel := context.WithCancel(ctx)
|
|
|
|
|
|
|
|
return &TaskScheduler{
|
|
|
|
RegisterLinkTaskQueue: NewBaseTaskQueue(),
|
|
|
|
RegisterNodeTaskQueue: NewBaseTaskQueue(),
|
|
|
|
InvalidateCollectionMetaCacheTaskQueue: NewBaseTaskQueue(),
|
|
|
|
ctx: ctx1,
|
|
|
|
cancel: cancel,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sched *TaskScheduler) scheduleRegisterLinkTask() task {
|
|
|
|
return sched.RegisterLinkTaskQueue.PopTask()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sched *TaskScheduler) scheduleRegisterNodeTask() task {
|
|
|
|
return sched.RegisterNodeTaskQueue.PopTask()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sched *TaskScheduler) scheduleInvalidateCollectionMetaCacheTask() task {
|
|
|
|
return sched.InvalidateCollectionMetaCacheTaskQueue.PopTask()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sched *TaskScheduler) processTask(t task, q TaskQueue) {
|
2021-02-23 09:58:06 +08:00
|
|
|
span, ctx := trace.StartSpanFromContext(t.Ctx(),
|
|
|
|
opentracing.Tags{
|
|
|
|
"Type": t.Name(),
|
|
|
|
})
|
|
|
|
defer span.Finish()
|
|
|
|
span.LogFields(oplog.String("scheduler process PreExecute", t.Name()))
|
|
|
|
err := t.PreExecute(ctx)
|
2021-01-22 12:57:23 +08:00
|
|
|
|
|
|
|
defer func() {
|
2021-02-23 09:58:06 +08:00
|
|
|
trace.LogError(span, err)
|
2021-01-22 12:57:23 +08:00
|
|
|
t.Notify(err)
|
|
|
|
}()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-23 09:58:06 +08:00
|
|
|
span.LogFields(oplog.String("scheduler process Execute", t.Name()))
|
|
|
|
err = t.Execute(ctx)
|
2021-01-22 12:57:23 +08:00
|
|
|
if err != nil {
|
2021-02-23 09:58:06 +08:00
|
|
|
trace.LogError(span, err)
|
2021-01-22 12:57:23 +08:00
|
|
|
return
|
|
|
|
}
|
2021-02-23 09:58:06 +08:00
|
|
|
span.LogFields(oplog.String("scheduler process PostExecute", t.Name()))
|
|
|
|
err = t.PostExecute(ctx)
|
2021-01-22 12:57:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sched *TaskScheduler) registerLinkLoop() {
|
|
|
|
defer sched.wg.Done()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-sched.ctx.Done():
|
|
|
|
return
|
|
|
|
case <-sched.RegisterLinkTaskQueue.Chan():
|
|
|
|
if !sched.RegisterLinkTaskQueue.Empty() {
|
|
|
|
t := sched.scheduleRegisterLinkTask()
|
|
|
|
go sched.processTask(t, sched.RegisterLinkTaskQueue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sched *TaskScheduler) registerNodeLoop() {
|
|
|
|
defer sched.wg.Done()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-sched.ctx.Done():
|
|
|
|
return
|
|
|
|
case <-sched.RegisterNodeTaskQueue.Chan():
|
|
|
|
if !sched.RegisterNodeTaskQueue.Empty() {
|
|
|
|
t := sched.scheduleRegisterNodeTask()
|
|
|
|
go sched.processTask(t, sched.RegisterNodeTaskQueue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sched *TaskScheduler) invalidateCollectionMetaCacheLoop() {
|
|
|
|
defer sched.wg.Done()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-sched.ctx.Done():
|
|
|
|
return
|
|
|
|
case <-sched.InvalidateCollectionMetaCacheTaskQueue.Chan():
|
|
|
|
if !sched.InvalidateCollectionMetaCacheTaskQueue.Empty() {
|
|
|
|
t := sched.scheduleInvalidateCollectionMetaCacheTask()
|
|
|
|
go sched.processTask(t, sched.InvalidateCollectionMetaCacheTaskQueue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sched *TaskScheduler) Start() {
|
|
|
|
sched.wg.Add(1)
|
|
|
|
go sched.registerLinkLoop()
|
|
|
|
|
|
|
|
sched.wg.Add(1)
|
|
|
|
go sched.registerNodeLoop()
|
|
|
|
|
|
|
|
sched.wg.Add(1)
|
|
|
|
go sched.invalidateCollectionMetaCacheLoop()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sched *TaskScheduler) Close() {
|
|
|
|
sched.cancel()
|
|
|
|
sched.wg.Wait()
|
|
|
|
}
|