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-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-03 14:53:36 +08:00
|
|
|
Allocator: Allocator{reqs: make(chan request, maxMergeRequests),
|
2020-11-14 11:24:49 +08:00
|
|
|
ctx: ctx1,
|
|
|
|
cancel: cancel,
|
|
|
|
masterAddress: masterAddr,
|
|
|
|
countPerRPC: maxMergeRequests,
|
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
|
|
|
|
return a, nil
|
2020-10-30 16:27:58 +08:00
|
|
|
}
|
|
|
|
|
2020-11-12 12:04:12 +08:00
|
|
|
func (ta *IDAllocator) syncID() {
|
|
|
|
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-12 12:04:12 +08:00
|
|
|
Count: ta.countPerRPC,
|
2020-11-03 14:53:36 +08:00
|
|
|
}
|
2020-11-13 15:17:18 +08:00
|
|
|
resp, err := ta.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-11-12 12:04:12 +08:00
|
|
|
log.Panic("syncID Failed!!!!!")
|
2020-11-03 14:53:36 +08:00
|
|
|
return
|
|
|
|
}
|
2020-11-13 15:17:18 +08:00
|
|
|
ta.idStart = resp.GetID()
|
2020-11-03 14:53:36 +08:00
|
|
|
ta.idEnd = ta.idStart + int64(resp.GetCount())
|
2020-10-30 16:27:58 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-11-12 12:04:12 +08:00
|
|
|
func (ta *IDAllocator) processFunc(req request) {
|
2020-11-03 14:53:36 +08:00
|
|
|
idRequest := req.(*idRequest)
|
|
|
|
idRequest.id = 1
|
2020-11-12 12:04:12 +08:00
|
|
|
fmt.Println("process ID")
|
2020-11-03 14:53:36 +08:00
|
|
|
}
|
2020-10-30 16:27:58 +08:00
|
|
|
|
2020-11-12 12:04:12 +08:00
|
|
|
func (ta *IDAllocator) AllocOne() (UniqueID, error) {
|
2020-11-03 14:53:36 +08:00
|
|
|
ret, _, err := ta.Alloc(1)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return ret, nil
|
2020-10-30 16:27:58 +08:00
|
|
|
}
|
|
|
|
|
2020-11-12 12:04:12 +08:00
|
|
|
func (ta *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
|
|
|
|
ta.reqs <- req
|
|
|
|
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
|
|
|
|
}
|