2020-11-03 14:53:36 +08:00
|
|
|
package allocator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-04-01 13:37:18 +08:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-11-03 14:53:36 +08:00
|
|
|
"sync"
|
|
|
|
"time"
|
2021-03-05 10:15:27 +08:00
|
|
|
|
2021-04-01 13:37:18 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/log"
|
|
|
|
"go.uber.org/zap"
|
2020-11-03 14:53:36 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-11-19 21:02:31 +08:00
|
|
|
maxConcurrentRequests = 10000
|
2020-11-03 14:53:36 +08:00
|
|
|
)
|
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
type Request interface {
|
2021-03-29 15:14:51 +08:00
|
|
|
Wait() error
|
2020-11-03 14:53:36 +08:00
|
|
|
Notify(error)
|
|
|
|
}
|
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
type BaseRequest struct {
|
|
|
|
Done chan error
|
|
|
|
Valid bool
|
2020-11-03 14:53:36 +08:00
|
|
|
}
|
|
|
|
|
2021-03-29 15:14:51 +08:00
|
|
|
func (req *BaseRequest) Wait() error {
|
2021-01-29 09:27:26 +08:00
|
|
|
err := <-req.Done
|
2021-03-29 15:14:51 +08:00
|
|
|
return err
|
2020-11-03 14:53:36 +08:00
|
|
|
}
|
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
func (req *BaseRequest) Notify(err error) {
|
|
|
|
req.Done <- err
|
2020-11-03 14:53:36 +08:00
|
|
|
}
|
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
type IDRequest struct {
|
|
|
|
BaseRequest
|
2020-11-04 17:58:43 +08:00
|
|
|
id UniqueID
|
2020-11-03 14:53:36 +08:00
|
|
|
count uint32
|
|
|
|
}
|
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
type TSORequest struct {
|
|
|
|
BaseRequest
|
2020-11-04 17:58:43 +08:00
|
|
|
timestamp Timestamp
|
2020-11-03 14:53:36 +08:00
|
|
|
count uint32
|
|
|
|
}
|
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
type SyncRequest struct {
|
|
|
|
BaseRequest
|
2020-11-03 14:53:36 +08:00
|
|
|
}
|
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
type TickerChan interface {
|
2020-11-03 14:53:36 +08:00
|
|
|
Chan() <-chan time.Time
|
|
|
|
Close()
|
|
|
|
Init()
|
|
|
|
Reset()
|
|
|
|
}
|
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
type EmptyTicker struct {
|
2020-11-03 14:53:36 +08:00
|
|
|
tChan <-chan time.Time
|
|
|
|
}
|
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
func (t *EmptyTicker) Chan() <-chan time.Time {
|
2020-11-03 14:53:36 +08:00
|
|
|
return t.tChan
|
|
|
|
}
|
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
func (t *EmptyTicker) Init() {
|
2020-11-03 14:53:36 +08:00
|
|
|
}
|
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
func (t *EmptyTicker) Reset() {
|
2020-11-03 14:53:36 +08:00
|
|
|
}
|
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
func (t *EmptyTicker) Close() {
|
2020-11-03 14:53:36 +08:00
|
|
|
}
|
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
type Ticker struct {
|
2020-11-03 14:53:36 +08:00
|
|
|
ticker *time.Ticker
|
2021-04-12 14:11:06 +08:00
|
|
|
UpdateInterval time.Duration
|
2020-11-03 14:53:36 +08:00
|
|
|
}
|
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
func (t *Ticker) Init() {
|
|
|
|
t.ticker = time.NewTicker(t.UpdateInterval)
|
2020-11-03 14:53:36 +08:00
|
|
|
}
|
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
func (t *Ticker) Reset() {
|
|
|
|
t.ticker.Reset(t.UpdateInterval)
|
2020-11-03 14:53:36 +08:00
|
|
|
}
|
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
func (t *Ticker) Close() {
|
2020-11-03 14:53:36 +08:00
|
|
|
t.ticker.Stop()
|
|
|
|
}
|
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
func (t *Ticker) Chan() <-chan time.Time {
|
2020-11-03 14:53:36 +08:00
|
|
|
return t.ticker.C
|
|
|
|
}
|
|
|
|
|
|
|
|
type Allocator struct {
|
2021-01-29 09:27:26 +08:00
|
|
|
Ctx context.Context
|
|
|
|
CancelFunc context.CancelFunc
|
2020-11-03 14:53:36 +08:00
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
wg sync.WaitGroup
|
2020-11-03 14:53:36 +08:00
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
Reqs chan Request
|
|
|
|
ToDoReqs []Request
|
|
|
|
CanDoReqs []Request
|
|
|
|
SyncReqs []Request
|
2020-11-03 14:53:36 +08:00
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
TChan TickerChan
|
|
|
|
ForceSyncChan chan Request
|
2020-11-19 21:02:31 +08:00
|
|
|
|
2021-04-01 13:37:18 +08:00
|
|
|
SyncFunc func() (bool, error)
|
2021-01-29 09:27:26 +08:00
|
|
|
ProcessFunc func(req Request) error
|
2020-11-19 21:02:31 +08:00
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
CheckSyncFunc func(timeout bool) bool
|
|
|
|
PickCanDoFunc func()
|
2021-04-01 13:37:18 +08:00
|
|
|
SyncErr error
|
|
|
|
Role string
|
2020-11-03 14:53:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ta *Allocator) Start() error {
|
2021-01-29 09:27:26 +08:00
|
|
|
ta.TChan.Init()
|
2020-11-03 14:53:36 +08:00
|
|
|
ta.wg.Add(1)
|
|
|
|
go ta.mainLoop()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
func (ta *Allocator) Init() {
|
|
|
|
ta.ForceSyncChan = make(chan Request, maxConcurrentRequests)
|
|
|
|
ta.Reqs = make(chan Request, maxConcurrentRequests)
|
2020-11-19 21:02:31 +08:00
|
|
|
}
|
|
|
|
|
2020-11-03 14:53:36 +08:00
|
|
|
func (ta *Allocator) mainLoop() {
|
|
|
|
defer ta.wg.Done()
|
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
loopCtx, loopCancel := context.WithCancel(ta.Ctx)
|
2020-11-03 14:53:36 +08:00
|
|
|
defer loopCancel()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
2021-01-29 09:27:26 +08:00
|
|
|
case first := <-ta.ForceSyncChan:
|
|
|
|
ta.SyncReqs = append(ta.SyncReqs, first)
|
|
|
|
pending := len(ta.ForceSyncChan)
|
2020-11-19 21:02:31 +08:00
|
|
|
for i := 0; i < pending; i++ {
|
2021-01-29 09:27:26 +08:00
|
|
|
ta.SyncReqs = append(ta.SyncReqs, <-ta.ForceSyncChan)
|
2020-11-19 21:02:31 +08:00
|
|
|
}
|
|
|
|
ta.sync(true)
|
|
|
|
ta.finishSyncRequest()
|
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
case <-ta.TChan.Chan():
|
2020-12-24 20:55:40 +08:00
|
|
|
ta.pickCanDo()
|
|
|
|
ta.finishRequest()
|
|
|
|
if ta.sync(true) {
|
|
|
|
ta.pickCanDo()
|
|
|
|
ta.finishRequest()
|
|
|
|
}
|
|
|
|
ta.failRemainRequest()
|
2020-11-19 21:02:31 +08:00
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
case first := <-ta.Reqs:
|
|
|
|
ta.ToDoReqs = append(ta.ToDoReqs, first)
|
|
|
|
pending := len(ta.Reqs)
|
2020-11-19 21:02:31 +08:00
|
|
|
for i := 0; i < pending; i++ {
|
2021-01-29 09:27:26 +08:00
|
|
|
ta.ToDoReqs = append(ta.ToDoReqs, <-ta.Reqs)
|
2020-11-03 14:53:36 +08:00
|
|
|
}
|
2020-12-24 20:55:40 +08:00
|
|
|
ta.pickCanDo()
|
2020-11-19 21:02:31 +08:00
|
|
|
ta.finishRequest()
|
2020-12-24 20:55:40 +08:00
|
|
|
if ta.sync(false) {
|
|
|
|
ta.pickCanDo()
|
|
|
|
ta.finishRequest()
|
|
|
|
}
|
|
|
|
ta.failRemainRequest()
|
2020-11-03 14:53:36 +08:00
|
|
|
|
|
|
|
case <-loopCtx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-24 20:55:40 +08:00
|
|
|
func (ta *Allocator) pickCanDo() {
|
2021-01-29 09:27:26 +08:00
|
|
|
if ta.PickCanDoFunc == nil {
|
2020-11-19 21:02:31 +08:00
|
|
|
return
|
|
|
|
}
|
2021-01-29 09:27:26 +08:00
|
|
|
ta.PickCanDoFunc()
|
2020-12-24 20:55:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ta *Allocator) sync(timeout bool) bool {
|
2021-01-29 09:27:26 +08:00
|
|
|
if ta.SyncFunc == nil || ta.CheckSyncFunc == nil {
|
|
|
|
ta.CanDoReqs = ta.ToDoReqs
|
2021-04-01 13:37:18 +08:00
|
|
|
ta.ToDoReqs = nil
|
2020-12-24 20:55:40 +08:00
|
|
|
return true
|
|
|
|
}
|
2021-01-29 09:27:26 +08:00
|
|
|
if !timeout && len(ta.ToDoReqs) == 0 {
|
2020-12-24 20:55:40 +08:00
|
|
|
return false
|
|
|
|
}
|
2021-01-29 09:27:26 +08:00
|
|
|
if !ta.CheckSyncFunc(timeout) {
|
2020-12-24 20:55:40 +08:00
|
|
|
return false
|
2020-11-19 21:02:31 +08:00
|
|
|
}
|
|
|
|
|
2021-04-01 13:37:18 +08:00
|
|
|
var ret bool
|
|
|
|
ret, ta.SyncErr = ta.SyncFunc()
|
2020-11-19 21:02:31 +08:00
|
|
|
|
|
|
|
if !timeout {
|
2021-01-29 09:27:26 +08:00
|
|
|
ta.TChan.Reset()
|
2020-11-03 14:53:36 +08:00
|
|
|
}
|
2020-12-24 20:55:40 +08:00
|
|
|
return ret
|
2020-11-03 14:53:36 +08:00
|
|
|
}
|
|
|
|
|
2020-11-19 21:02:31 +08:00
|
|
|
func (ta *Allocator) finishSyncRequest() {
|
2021-01-29 09:27:26 +08:00
|
|
|
for _, req := range ta.SyncReqs {
|
2020-11-19 21:02:31 +08:00
|
|
|
if req != nil {
|
|
|
|
req.Notify(nil)
|
2020-11-03 14:53:36 +08:00
|
|
|
}
|
|
|
|
}
|
2021-04-01 13:37:18 +08:00
|
|
|
ta.SyncReqs = nil
|
2020-11-19 21:02:31 +08:00
|
|
|
}
|
|
|
|
|
2020-12-24 20:55:40 +08:00
|
|
|
func (ta *Allocator) failRemainRequest() {
|
2021-04-01 13:37:18 +08:00
|
|
|
var err error
|
|
|
|
if ta.SyncErr != nil {
|
|
|
|
err = fmt.Errorf("%s failRemainRequest err:%w", ta.Role, ta.SyncErr)
|
|
|
|
} else {
|
|
|
|
errMsg := fmt.Sprintf("%s failRemainRequest unexpected error", ta.Role)
|
|
|
|
err = errors.New(errMsg)
|
|
|
|
}
|
|
|
|
if len(ta.ToDoReqs) > 0 {
|
|
|
|
log.Debug("Allocator has some reqs to fail",
|
|
|
|
zap.Any("Role", ta.Role),
|
|
|
|
zap.Any("reqLen", len(ta.ToDoReqs)))
|
|
|
|
}
|
2021-01-29 09:27:26 +08:00
|
|
|
for _, req := range ta.ToDoReqs {
|
2020-12-24 20:55:40 +08:00
|
|
|
if req != nil {
|
2021-04-01 13:37:18 +08:00
|
|
|
req.Notify(err)
|
2020-12-24 20:55:40 +08:00
|
|
|
}
|
|
|
|
}
|
2021-04-01 13:37:18 +08:00
|
|
|
ta.ToDoReqs = nil
|
2020-12-24 20:55:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ta *Allocator) finishRequest() {
|
2021-01-29 09:27:26 +08:00
|
|
|
for _, req := range ta.CanDoReqs {
|
2020-11-19 21:02:31 +08:00
|
|
|
if req != nil {
|
2021-01-29 09:27:26 +08:00
|
|
|
err := ta.ProcessFunc(req)
|
2020-11-19 21:02:31 +08:00
|
|
|
req.Notify(err)
|
|
|
|
}
|
|
|
|
}
|
2021-01-29 09:27:26 +08:00
|
|
|
ta.CanDoReqs = []Request{}
|
2020-11-03 14:53:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ta *Allocator) revokeRequest(err error) {
|
2021-01-29 09:27:26 +08:00
|
|
|
n := len(ta.Reqs)
|
2020-11-03 14:53:36 +08:00
|
|
|
for i := 0; i < n; i++ {
|
2021-01-29 09:27:26 +08:00
|
|
|
req := <-ta.Reqs
|
2020-11-03 14:53:36 +08:00
|
|
|
req.Notify(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ta *Allocator) Close() {
|
2021-01-29 09:27:26 +08:00
|
|
|
ta.CancelFunc()
|
2020-11-03 14:53:36 +08:00
|
|
|
ta.wg.Wait()
|
2021-01-29 09:27:26 +08:00
|
|
|
ta.TChan.Close()
|
2021-04-01 13:37:18 +08:00
|
|
|
errMsg := fmt.Sprintf("%s is closing", ta.Role)
|
|
|
|
ta.revokeRequest(errors.New(errMsg))
|
2020-11-03 14:53:36 +08:00
|
|
|
}
|
2020-11-19 21:02:31 +08:00
|
|
|
|
|
|
|
func (ta *Allocator) CleanCache() {
|
2021-04-09 14:07:03 +08:00
|
|
|
req := &SyncRequest{
|
|
|
|
BaseRequest: BaseRequest{
|
|
|
|
Done: make(chan error),
|
|
|
|
Valid: false,
|
|
|
|
},
|
|
|
|
}
|
2021-01-29 09:27:26 +08:00
|
|
|
ta.ForceSyncChan <- req
|
2021-03-29 15:14:51 +08:00
|
|
|
_ = req.Wait()
|
2020-11-19 21:02:31 +08:00
|
|
|
}
|