2020-10-30 16:27:58 +08:00
|
|
|
package allocator
|
|
|
|
|
|
|
|
import (
|
2020-11-03 14:53:36 +08:00
|
|
|
"context"
|
2021-03-05 10:15:27 +08:00
|
|
|
"errors"
|
2020-11-03 14:53:36 +08:00
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
2021-01-18 19:32:08 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/commonpb"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/masterpb"
|
2021-02-25 14:31:38 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/util/retry"
|
2020-11-16 21:10:43 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/util/typeutil"
|
2021-02-25 14:31:38 +08:00
|
|
|
"google.golang.org/grpc"
|
2020-10-30 16:27:58 +08:00
|
|
|
)
|
|
|
|
|
2020-11-04 17:58:43 +08:00
|
|
|
type Timestamp = typeutil.Timestamp
|
2020-11-03 14:53:36 +08:00
|
|
|
|
|
|
|
const (
|
2020-11-26 16:01:31 +08:00
|
|
|
tsCountPerRPC = 2 << 15
|
2020-11-03 14:53:36 +08:00
|
|
|
)
|
2020-10-30 16:27:58 +08:00
|
|
|
|
2020-11-03 14:53:36 +08:00
|
|
|
type TimestampAllocator struct {
|
|
|
|
Allocator
|
2021-01-29 09:27:26 +08:00
|
|
|
|
|
|
|
masterAddress string
|
|
|
|
masterConn *grpc.ClientConn
|
|
|
|
masterClient masterpb.MasterServiceClient
|
|
|
|
|
|
|
|
countPerRPC uint32
|
2020-11-04 17:58:43 +08:00
|
|
|
lastTsBegin Timestamp
|
|
|
|
lastTsEnd Timestamp
|
2020-12-24 20:55:40 +08:00
|
|
|
PeerID UniqueID
|
2020-10-30 16:27:58 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 21:10:43 +08:00
|
|
|
func NewTimestampAllocator(ctx context.Context, masterAddr string) (*TimestampAllocator, error) {
|
2020-11-03 14:53:36 +08:00
|
|
|
ctx1, cancel := context.WithCancel(ctx)
|
|
|
|
a := &TimestampAllocator{
|
2021-01-29 09:27:26 +08:00
|
|
|
Allocator: Allocator{
|
|
|
|
Ctx: ctx1,
|
|
|
|
CancelFunc: cancel,
|
2020-11-03 14:53:36 +08:00
|
|
|
},
|
2021-01-29 09:27:26 +08:00
|
|
|
masterAddress: masterAddr,
|
|
|
|
countPerRPC: tsCountPerRPC,
|
2020-11-03 14:53:36 +08:00
|
|
|
}
|
2021-01-29 09:27:26 +08:00
|
|
|
a.TChan = &Ticker{
|
|
|
|
UpdateInterval: time.Second,
|
2020-11-03 14:53:36 +08:00
|
|
|
}
|
2021-01-29 09:27:26 +08:00
|
|
|
a.Allocator.SyncFunc = a.syncTs
|
|
|
|
a.Allocator.ProcessFunc = a.processFunc
|
|
|
|
a.Allocator.CheckSyncFunc = a.checkSyncFunc
|
|
|
|
a.Allocator.PickCanDoFunc = a.pickCanDoFunc
|
|
|
|
a.Init()
|
2020-11-03 14:53:36 +08:00
|
|
|
return a, nil
|
2020-10-30 16:27:58 +08:00
|
|
|
}
|
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
func (ta *TimestampAllocator) Start() error {
|
|
|
|
connectMasterFn := func() error {
|
|
|
|
return ta.connectMaster()
|
|
|
|
}
|
|
|
|
err := retry.Retry(10, time.Millisecond*200, connectMasterFn)
|
|
|
|
if err != nil {
|
|
|
|
panic("Timestamp local allocator connect to master failed")
|
|
|
|
}
|
|
|
|
ta.Allocator.Start()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ta *TimestampAllocator) connectMaster() error {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
conn, err := grpc.DialContext(ctx, ta.masterAddress, grpc.WithInsecure(), grpc.WithBlock())
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Connect to master failed, error= %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Printf("Connected to master, master_addr=%s", ta.masterAddress)
|
|
|
|
ta.masterConn = conn
|
|
|
|
ta.masterClient = masterpb.NewMasterServiceClient(conn)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-24 20:55:40 +08:00
|
|
|
func (ta *TimestampAllocator) checkSyncFunc(timeout bool) bool {
|
2021-01-29 09:27:26 +08:00
|
|
|
return timeout || len(ta.ToDoReqs) > 0
|
2020-12-24 20:55:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ta *TimestampAllocator) pickCanDoFunc() {
|
|
|
|
total := uint32(ta.lastTsEnd - ta.lastTsBegin)
|
2020-11-19 21:02:31 +08:00
|
|
|
need := uint32(0)
|
2020-12-24 20:55:40 +08:00
|
|
|
idx := 0
|
2021-01-29 09:27:26 +08:00
|
|
|
for _, req := range ta.ToDoReqs {
|
|
|
|
tReq := req.(*TSORequest)
|
2020-12-24 20:55:40 +08:00
|
|
|
need += tReq.count
|
|
|
|
if need <= total {
|
2021-01-29 09:27:26 +08:00
|
|
|
ta.CanDoReqs = append(ta.CanDoReqs, req)
|
2020-12-24 20:55:40 +08:00
|
|
|
idx++
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
2020-11-19 21:02:31 +08:00
|
|
|
}
|
2021-01-29 09:27:26 +08:00
|
|
|
ta.ToDoReqs = ta.ToDoReqs[idx:]
|
2020-11-19 21:02:31 +08:00
|
|
|
}
|
|
|
|
|
2020-12-24 20:55:40 +08:00
|
|
|
func (ta *TimestampAllocator) syncTs() bool {
|
2020-11-03 14:53:36 +08:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
2021-01-18 19:32:08 +08:00
|
|
|
req := &masterpb.TsoRequest{
|
|
|
|
Base: &commonpb.MsgBase{
|
|
|
|
MsgType: commonpb.MsgType_kRequestTSO,
|
|
|
|
MsgID: 0,
|
|
|
|
Timestamp: 0,
|
|
|
|
SourceID: ta.PeerID,
|
|
|
|
},
|
|
|
|
Count: ta.countPerRPC,
|
2020-10-30 16:27:58 +08:00
|
|
|
}
|
2020-11-03 14:53:36 +08:00
|
|
|
resp, err := ta.masterClient.AllocTimestamp(ctx, req)
|
|
|
|
|
|
|
|
cancel()
|
|
|
|
if err != nil {
|
2020-12-03 19:00:11 +08:00
|
|
|
log.Println("syncTimestamp Failed!!!!!")
|
2020-12-24 20:55:40 +08:00
|
|
|
return false
|
2020-11-03 14:53:36 +08:00
|
|
|
}
|
|
|
|
ta.lastTsBegin = resp.GetTimestamp()
|
|
|
|
ta.lastTsEnd = ta.lastTsBegin + uint64(resp.GetCount())
|
2020-12-24 20:55:40 +08:00
|
|
|
return true
|
2020-11-03 14:53:36 +08:00
|
|
|
}
|
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
func (ta *TimestampAllocator) processFunc(req Request) error {
|
|
|
|
tsoRequest := req.(*TSORequest)
|
2020-11-16 17:01:10 +08:00
|
|
|
tsoRequest.timestamp = ta.lastTsBegin
|
|
|
|
ta.lastTsBegin++
|
2020-11-19 21:02:31 +08:00
|
|
|
return nil
|
2020-10-30 16:27:58 +08:00
|
|
|
}
|
|
|
|
|
2020-11-03 14:53:36 +08:00
|
|
|
func (ta *TimestampAllocator) AllocOne() (Timestamp, error) {
|
|
|
|
ret, err := ta.Alloc(1)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return ret[0], nil
|
2020-10-30 16:27:58 +08:00
|
|
|
}
|
|
|
|
|
2020-11-03 14:53:36 +08:00
|
|
|
func (ta *TimestampAllocator) Alloc(count uint32) ([]Timestamp, error) {
|
2021-01-29 09:27:26 +08:00
|
|
|
req := &TSORequest{
|
|
|
|
BaseRequest: BaseRequest{Done: make(chan error), Valid: false},
|
2020-11-03 14:53:36 +08:00
|
|
|
}
|
|
|
|
req.count = count
|
2021-01-29 09:27:26 +08:00
|
|
|
ta.Reqs <- req
|
2020-11-03 14:53:36 +08:00
|
|
|
req.Wait()
|
|
|
|
|
|
|
|
if !req.IsValid() {
|
2021-03-05 10:15:27 +08:00
|
|
|
return nil, errors.New("alloc time stamp request failed")
|
2020-11-03 14:53:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
start, count := req.timestamp, req.count
|
|
|
|
var ret []Timestamp
|
|
|
|
for i := uint32(0); i < count; i++ {
|
|
|
|
ret = append(ret, start+uint64(i))
|
|
|
|
}
|
|
|
|
return ret, nil
|
2020-10-30 16:27:58 +08:00
|
|
|
}
|
2020-11-19 21:02:31 +08:00
|
|
|
|
|
|
|
func (ta *TimestampAllocator) ClearCache() {
|
|
|
|
|
|
|
|
}
|