2020-10-30 16:27:58 +08:00
|
|
|
package allocator
|
|
|
|
|
|
|
|
import (
|
2020-11-03 14:53:36 +08:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/internalpb"
|
2020-11-04 17:58:43 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/util/typeutil"
|
2020-10-30 16:27:58 +08:00
|
|
|
)
|
|
|
|
|
2020-11-19 21:02:31 +08:00
|
|
|
const (
|
|
|
|
IDCountPerRPC = 200000
|
|
|
|
)
|
|
|
|
|
2020-11-04 17:58:43 +08:00
|
|
|
type UniqueID = typeutil.UniqueID
|
|
|
|
|
2020-11-12 12:04:12 +08:00
|
|
|
type IDAllocator struct {
|
2020-11-03 14:53:36 +08:00
|
|
|
Allocator
|
2020-10-30 16:27:58 +08:00
|
|
|
|
2020-11-04 17:58:43 +08:00
|
|
|
idStart UniqueID
|
|
|
|
idEnd UniqueID
|
2020-10-30 16:27:58 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 21:10:43 +08:00
|
|
|
func NewIDAllocator(ctx context.Context, masterAddr string) (*IDAllocator, error) {
|
2020-11-14 11:24:49 +08:00
|
|
|
|
2020-11-03 14:53:36 +08:00
|
|
|
ctx1, cancel := context.WithCancel(ctx)
|
2020-11-12 12:04:12 +08:00
|
|
|
a := &IDAllocator{
|
2020-11-19 21:02:31 +08:00
|
|
|
Allocator: Allocator{reqs: make(chan request, maxConcurrentRequests),
|
2020-11-14 11:24:49 +08:00
|
|
|
ctx: ctx1,
|
|
|
|
cancel: cancel,
|
|
|
|
masterAddress: masterAddr,
|
2020-11-19 21:02:31 +08:00
|
|
|
countPerRPC: IDCountPerRPC,
|
2020-11-03 14:53:36 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
a.tChan = &emptyTicker{}
|
2020-11-12 12:04:12 +08:00
|
|
|
a.Allocator.syncFunc = a.syncID
|
2020-11-03 14:53:36 +08:00
|
|
|
a.Allocator.processFunc = a.processFunc
|
2020-11-19 21:02:31 +08:00
|
|
|
a.Allocator.checkFunc = a.checkFunc
|
|
|
|
a.init()
|
2020-11-03 14:53:36 +08:00
|
|
|
return a, nil
|
2020-10-30 16:27:58 +08:00
|
|
|
}
|
|
|
|
|
2020-11-19 21:02:31 +08:00
|
|
|
func (ia *IDAllocator) syncID() {
|
2020-11-12 12:04:12 +08:00
|
|
|
fmt.Println("syncID")
|
2020-11-03 14:53:36 +08:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
2020-11-13 15:17:18 +08:00
|
|
|
req := &internalpb.IDRequest{
|
|
|
|
PeerID: 1,
|
2020-11-03 14:53:36 +08:00
|
|
|
Role: internalpb.PeerRole_Proxy,
|
2020-11-19 21:02:31 +08:00
|
|
|
Count: ia.countPerRPC,
|
2020-11-03 14:53:36 +08:00
|
|
|
}
|
2020-11-19 21:02:31 +08:00
|
|
|
resp, err := ia.masterClient.AllocID(ctx, req)
|
2020-10-30 16:27:58 +08:00
|
|
|
|
2020-11-03 14:53:36 +08:00
|
|
|
cancel()
|
|
|
|
if err != nil {
|
2020-12-03 19:00:11 +08:00
|
|
|
log.Println("syncID Failed!!!!!")
|
2020-11-03 14:53:36 +08:00
|
|
|
return
|
|
|
|
}
|
2020-11-19 21:02:31 +08:00
|
|
|
ia.idStart = resp.GetID()
|
|
|
|
ia.idEnd = ia.idStart + int64(resp.GetCount())
|
|
|
|
}
|
2020-10-30 16:27:58 +08:00
|
|
|
|
2020-11-19 21:02:31 +08:00
|
|
|
func (ia *IDAllocator) checkFunc(timeout bool) bool {
|
|
|
|
if timeout {
|
|
|
|
return timeout
|
|
|
|
}
|
|
|
|
need := uint32(0)
|
|
|
|
for _, req := range ia.toDoReqs {
|
|
|
|
iReq := req.(*idRequest)
|
|
|
|
need += iReq.count
|
|
|
|
}
|
|
|
|
return ia.idStart+int64(need) >= ia.idEnd
|
2020-10-30 16:27:58 +08:00
|
|
|
}
|
|
|
|
|
2020-11-19 21:02:31 +08:00
|
|
|
func (ia *IDAllocator) processFunc(req request) error {
|
2020-11-03 14:53:36 +08:00
|
|
|
idRequest := req.(*idRequest)
|
2020-11-19 21:02:31 +08:00
|
|
|
idRequest.id = ia.idStart
|
|
|
|
ia.idStart++
|
|
|
|
return nil
|
2020-11-03 14:53:36 +08:00
|
|
|
}
|
2020-10-30 16:27:58 +08:00
|
|
|
|
2020-11-19 21:02:31 +08:00
|
|
|
func (ia *IDAllocator) AllocOne() (UniqueID, error) {
|
|
|
|
ret, _, err := ia.Alloc(1)
|
2020-11-03 14:53:36 +08:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return ret, nil
|
2020-10-30 16:27:58 +08:00
|
|
|
}
|
|
|
|
|
2020-11-19 21:02:31 +08:00
|
|
|
func (ia *IDAllocator) Alloc(count uint32) (UniqueID, UniqueID, error) {
|
2020-11-03 14:53:36 +08:00
|
|
|
req := &idRequest{baseRequest: baseRequest{done: make(chan error), valid: false}}
|
|
|
|
|
|
|
|
req.count = count
|
2020-11-19 21:02:31 +08:00
|
|
|
ia.reqs <- req
|
2020-11-03 14:53:36 +08:00
|
|
|
req.Wait()
|
2020-10-30 16:27:58 +08:00
|
|
|
|
2020-11-03 14:53:36 +08:00
|
|
|
if !req.IsValid() {
|
|
|
|
return 0, 0, nil
|
|
|
|
}
|
2020-11-04 17:58:43 +08:00
|
|
|
start, count := req.id, req.count
|
2020-11-03 14:53:36 +08:00
|
|
|
return start, start + int64(count), nil
|
|
|
|
}
|