2020-11-03 14:53:36 +08:00
|
|
|
package allocator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2021-03-05 10:15:27 +08:00
|
|
|
|
|
|
|
"errors"
|
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 {
|
2020-11-03 14:53:36 +08:00
|
|
|
Wait()
|
|
|
|
Notify(error)
|
|
|
|
IsValid() bool
|
|
|
|
}
|
|
|
|
|
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-01-29 09:27:26 +08:00
|
|
|
func (req *BaseRequest) Wait() {
|
|
|
|
err := <-req.Done
|
|
|
|
req.Valid = err == nil
|
2020-11-03 14:53:36 +08:00
|
|
|
}
|
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
func (req *BaseRequest) IsValid() bool {
|
|
|
|
return req.Valid
|
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-01-29 09:27:26 +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-01-29 09:27:26 +08:00
|
|
|
SyncFunc func() bool
|
|
|
|
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()
|
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 {
|
2020-11-19 21:02:31 +08:00
|
|
|
|
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
|
|
|
|
ta.ToDoReqs = ta.ToDoReqs[0:0]
|
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-01-29 09:27:26 +08:00
|
|
|
ret := 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-01-29 09:27:26 +08:00
|
|
|
ta.SyncReqs = ta.SyncReqs[0:0]
|
2020-11-19 21:02:31 +08:00
|
|
|
}
|
|
|
|
|
2020-12-24 20:55:40 +08:00
|
|
|
func (ta *Allocator) failRemainRequest() {
|
2021-01-29 09:27:26 +08:00
|
|
|
for _, req := range ta.ToDoReqs {
|
2020-12-24 20:55:40 +08:00
|
|
|
if req != nil {
|
|
|
|
req.Notify(errors.New("failed: unexpected error"))
|
|
|
|
}
|
|
|
|
}
|
2021-01-29 09:27:26 +08:00
|
|
|
ta.ToDoReqs = []Request{}
|
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()
|
2020-11-03 14:53:36 +08:00
|
|
|
ta.revokeRequest(errors.New("closing"))
|
|
|
|
}
|
2020-11-19 21:02:31 +08:00
|
|
|
|
|
|
|
func (ta *Allocator) CleanCache() {
|
2021-01-29 09:27:26 +08:00
|
|
|
req := &SyncRequest{BaseRequest: BaseRequest{Done: make(chan error), Valid: false}}
|
|
|
|
ta.ForceSyncChan <- req
|
2020-11-19 21:02:31 +08:00
|
|
|
req.Wait()
|
|
|
|
}
|