2021-10-14 19:24:49 +08:00
|
|
|
// Licensed to the LF AI & Data foundation under one
|
|
|
|
// or more contributor license agreements. See the NOTICE file
|
|
|
|
// distributed with this work for additional information
|
|
|
|
// regarding copyright ownership. The ASF licenses this file
|
|
|
|
// to you under the Apache License, Version 2.0 (the
|
|
|
|
// "License"); you may not use this file except in compliance
|
2021-04-19 15:15:33 +08:00
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
2021-10-14 19:24:49 +08:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2021-04-19 15:15:33 +08:00
|
|
|
//
|
2021-10-14 19:24:49 +08:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2021-04-19 15:15:33 +08:00
|
|
|
|
2020-11-03 14:53:36 +08:00
|
|
|
package allocator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-04-01 13:37:18 +08:00
|
|
|
"fmt"
|
2020-11-03 14:53:36 +08:00
|
|
|
"sync"
|
|
|
|
"time"
|
2021-03-05 10:15:27 +08:00
|
|
|
|
2023-02-26 11:31:49 +08:00
|
|
|
"github.com/cockroachdb/errors"
|
2021-04-01 13:37:18 +08:00
|
|
|
"go.uber.org/zap"
|
2023-09-21 09:45:27 +08:00
|
|
|
|
|
|
|
"github.com/milvus-io/milvus/pkg/log"
|
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-10-18 21:29:00 +08:00
|
|
|
// Request defines an interface which has Wait and Notify methods.
|
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-10-18 21:29:00 +08:00
|
|
|
// BaseRequest implements Request interface.
|
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-10-18 21:29:00 +08:00
|
|
|
// Wait is blocked until the request is allocated or an error occurs.
|
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-10-18 21:29:00 +08:00
|
|
|
// Notify is used to send error to the requester.
|
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-10-18 21:30:56 +08:00
|
|
|
// IDRequest implements Request and is used to get global unique Identities.
|
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-10-20 20:30:37 +08:00
|
|
|
// SyncRequest embeds BaseRequest and is used to force synchronize from RootCoordinator.
|
2021-01-29 09:27:26 +08:00
|
|
|
type SyncRequest struct {
|
|
|
|
BaseRequest
|
2020-11-03 14:53:36 +08:00
|
|
|
}
|
|
|
|
|
2021-10-18 21:30:56 +08:00
|
|
|
// TickerChan defines an interface.
|
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-10-18 21:30:56 +08:00
|
|
|
// EmptyTicker implements TickerChan, but it will never issue a signal in Chan.
|
2021-01-29 09:27:26 +08:00
|
|
|
type EmptyTicker struct {
|
2020-11-03 14:53:36 +08:00
|
|
|
tChan <-chan time.Time
|
|
|
|
}
|
|
|
|
|
2021-10-20 15:42:45 +08:00
|
|
|
// Chan returns a read-only channel from which you can only receive time.Time type data.
|
2021-10-18 21:30:56 +08:00
|
|
|
// As for EmptyTicker, you will never read data from Chan.
|
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-10-21 19:46:13 +08:00
|
|
|
// Init does nothing.
|
2021-01-29 09:27:26 +08:00
|
|
|
func (t *EmptyTicker) Init() {
|
2020-11-03 14:53:36 +08:00
|
|
|
}
|
|
|
|
|
2021-10-21 19:25:11 +08:00
|
|
|
// Reset does nothing.
|
2021-01-29 09:27:26 +08:00
|
|
|
func (t *EmptyTicker) Reset() {
|
2020-11-03 14:53:36 +08:00
|
|
|
}
|
|
|
|
|
2021-10-21 20:47:11 +08:00
|
|
|
// Close does nothing.
|
2021-01-29 09:27:26 +08:00
|
|
|
func (t *EmptyTicker) Close() {
|
2020-11-03 14:53:36 +08:00
|
|
|
}
|
|
|
|
|
2021-10-18 21:30:56 +08:00
|
|
|
// Ticker implements TickerChan and is a simple wrapper for time.TimeTicker.
|
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-10-18 21:30:56 +08:00
|
|
|
// Init initialize the inner member `ticker` whose type is a pointer to time.Ticker.
|
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-10-20 15:48:38 +08:00
|
|
|
// Reset resets the inner member `ticker`.
|
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-10-18 21:30:56 +08:00
|
|
|
// Close closes the inner member `ticker`.
|
2021-01-29 09:27:26 +08:00
|
|
|
func (t *Ticker) Close() {
|
2020-11-03 14:53:36 +08:00
|
|
|
t.ticker.Stop()
|
|
|
|
}
|
|
|
|
|
2021-10-18 21:30:56 +08:00
|
|
|
// Chan return a read-only channel from which you can only receive time.Time type data
|
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
|
|
|
|
}
|
|
|
|
|
2021-12-21 09:18:48 +08:00
|
|
|
// Allocator allocates from a global allocator by its given member functions
|
2022-10-09 10:06:58 +08:00
|
|
|
type CachedAllocator 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
|
|
|
}
|
|
|
|
|
2021-11-02 21:14:40 +08:00
|
|
|
// Start starts the loop of checking whether to synchronize with the global allocator.
|
2022-10-09 10:06:58 +08:00
|
|
|
func (ta *CachedAllocator) 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-11-02 21:14:40 +08:00
|
|
|
// Init mainly initialize internal members.
|
2022-10-09 10:06:58 +08:00
|
|
|
func (ta *CachedAllocator) Init() {
|
2021-01-29 09:27:26 +08:00
|
|
|
ta.ForceSyncChan = make(chan Request, maxConcurrentRequests)
|
|
|
|
ta.Reqs = make(chan Request, maxConcurrentRequests)
|
2020-11-19 21:02:31 +08:00
|
|
|
}
|
|
|
|
|
2022-10-09 10:06:58 +08:00
|
|
|
func (ta *CachedAllocator) mainLoop() {
|
2020-11-03 14:53:36 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-09 10:06:58 +08:00
|
|
|
func (ta *CachedAllocator) 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
|
|
|
}
|
|
|
|
|
2022-10-09 10:06:58 +08:00
|
|
|
func (ta *CachedAllocator) 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
|
|
|
}
|
|
|
|
|
2022-10-09 10:06:58 +08:00
|
|
|
func (ta *CachedAllocator) 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
|
|
|
}
|
|
|
|
|
2022-10-09 10:06:58 +08:00
|
|
|
func (ta *CachedAllocator) 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 {
|
2022-03-02 15:35:55 +08:00
|
|
|
log.Warn("Allocator has some reqs to fail",
|
2024-01-05 16:12:48 +08:00
|
|
|
zap.String("Role", ta.Role),
|
|
|
|
zap.Int("reqLen", len(ta.ToDoReqs)))
|
2021-04-01 13:37:18 +08:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2022-10-09 10:06:58 +08:00
|
|
|
func (ta *CachedAllocator) 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
|
|
|
}
|
|
|
|
|
2022-10-09 10:06:58 +08:00
|
|
|
func (ta *CachedAllocator) 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-02 21:14:40 +08:00
|
|
|
// Close mainly stop the internal coroutine and recover resources.
|
2022-10-09 10:06:58 +08:00
|
|
|
func (ta *CachedAllocator) 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
|
|
|
|
2021-11-02 21:14:40 +08:00
|
|
|
// CleanCache is used to force synchronize with global allocator.
|
2022-10-09 10:06:58 +08:00
|
|
|
func (ta *CachedAllocator) 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
|
|
|
}
|