2022-09-05 13:29:11 +08:00
|
|
|
package rootcoord
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
2022-10-09 12:32:57 +08:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/milvus-io/milvus/internal/log"
|
|
|
|
|
|
|
|
"go.uber.org/atomic"
|
|
|
|
"go.uber.org/zap"
|
2022-09-05 13:29:11 +08:00
|
|
|
|
|
|
|
"github.com/milvus-io/milvus/internal/tso"
|
|
|
|
|
|
|
|
"github.com/milvus-io/milvus/internal/allocator"
|
|
|
|
)
|
|
|
|
|
|
|
|
type IScheduler interface {
|
|
|
|
Start()
|
|
|
|
Stop()
|
2022-09-24 12:42:51 +08:00
|
|
|
AddTask(t task) error
|
2022-10-09 12:32:57 +08:00
|
|
|
GetMinDdlTs() Timestamp
|
2022-09-05 13:29:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type scheduler struct {
|
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
|
|
|
wg sync.WaitGroup
|
|
|
|
|
2022-10-09 10:06:58 +08:00
|
|
|
idAllocator allocator.Interface
|
2022-09-05 13:29:11 +08:00
|
|
|
tsoAllocator tso.Allocator
|
|
|
|
|
2022-09-24 12:42:51 +08:00
|
|
|
taskChan chan task
|
2022-09-21 15:46:51 +08:00
|
|
|
|
|
|
|
lock sync.Mutex
|
2022-10-09 12:32:57 +08:00
|
|
|
|
|
|
|
minDdlTs atomic.Uint64
|
2022-09-05 13:29:11 +08:00
|
|
|
}
|
|
|
|
|
2022-10-09 10:06:58 +08:00
|
|
|
func newScheduler(ctx context.Context, idAllocator allocator.Interface, tsoAllocator tso.Allocator) *scheduler {
|
2022-09-05 13:29:11 +08:00
|
|
|
ctx1, cancel := context.WithCancel(ctx)
|
|
|
|
// TODO
|
|
|
|
n := 1024 * 10
|
|
|
|
return &scheduler{
|
|
|
|
ctx: ctx1,
|
|
|
|
cancel: cancel,
|
|
|
|
idAllocator: idAllocator,
|
|
|
|
tsoAllocator: tsoAllocator,
|
2022-09-24 12:42:51 +08:00
|
|
|
taskChan: make(chan task, n),
|
2022-10-09 12:32:57 +08:00
|
|
|
minDdlTs: *atomic.NewUint64(0),
|
2022-09-05 13:29:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *scheduler) Start() {
|
|
|
|
s.wg.Add(1)
|
|
|
|
go s.taskLoop()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *scheduler) Stop() {
|
|
|
|
s.cancel()
|
|
|
|
s.wg.Wait()
|
|
|
|
}
|
|
|
|
|
2022-09-24 12:42:51 +08:00
|
|
|
func (s *scheduler) execute(task task) {
|
2022-10-09 12:32:57 +08:00
|
|
|
defer s.setMinDdlTs(task.GetTs()) // we should update ts, whatever task succeeds or not.
|
2022-09-21 15:46:51 +08:00
|
|
|
if err := task.Prepare(task.GetCtx()); err != nil {
|
|
|
|
task.NotifyDone(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err := task.Execute(task.GetCtx())
|
|
|
|
task.NotifyDone(err)
|
|
|
|
}
|
|
|
|
|
2022-09-05 13:29:11 +08:00
|
|
|
func (s *scheduler) taskLoop() {
|
|
|
|
defer s.wg.Done()
|
2022-10-09 12:32:57 +08:00
|
|
|
ticker := time.NewTicker(Params.ProxyCfg.TimeTickInterval)
|
|
|
|
defer ticker.Stop()
|
2022-09-05 13:29:11 +08:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-s.ctx.Done():
|
|
|
|
return
|
2022-10-09 12:32:57 +08:00
|
|
|
case <-ticker.C:
|
|
|
|
s.updateLatestTsoAsMinDdlTs()
|
2022-09-05 13:29:11 +08:00
|
|
|
case task := <-s.taskChan:
|
2022-09-21 15:46:51 +08:00
|
|
|
s.execute(task)
|
2022-09-05 13:29:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-09 12:32:57 +08:00
|
|
|
func (s *scheduler) updateLatestTsoAsMinDdlTs() {
|
|
|
|
if len(s.taskChan) > 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ts, err := s.tsoAllocator.GenerateTSO(1)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("failed to generate tso, ignore to update min ddl ts", zap.Error(err))
|
|
|
|
} else {
|
|
|
|
s.setMinDdlTs(ts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-24 12:42:51 +08:00
|
|
|
func (s *scheduler) setID(task task) error {
|
2022-09-05 13:29:11 +08:00
|
|
|
id, err := s.idAllocator.AllocOne()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
task.SetID(id)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-24 12:42:51 +08:00
|
|
|
func (s *scheduler) setTs(task task) error {
|
2022-09-05 13:29:11 +08:00
|
|
|
ts, err := s.tsoAllocator.GenerateTSO(1)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
task.SetTs(ts)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-24 12:42:51 +08:00
|
|
|
func (s *scheduler) enqueue(task task) {
|
2022-09-05 13:29:11 +08:00
|
|
|
s.taskChan <- task
|
|
|
|
}
|
|
|
|
|
2022-09-24 12:42:51 +08:00
|
|
|
func (s *scheduler) AddTask(task task) error {
|
2022-09-21 15:46:51 +08:00
|
|
|
// make sure that setting ts and enqueue is atomic.
|
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
|
2022-09-05 13:29:11 +08:00
|
|
|
if err := s.setID(task); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := s.setTs(task); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.enqueue(task)
|
|
|
|
return nil
|
|
|
|
}
|
2022-10-09 12:32:57 +08:00
|
|
|
|
|
|
|
func (s *scheduler) GetMinDdlTs() Timestamp {
|
|
|
|
return s.minDdlTs.Load()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *scheduler) setMinDdlTs(ts Timestamp) {
|
|
|
|
s.minDdlTs.Store(ts)
|
|
|
|
}
|