2020-10-30 16:27:58 +08:00
|
|
|
package allocator
|
|
|
|
|
|
|
|
import (
|
2020-11-03 14:53:36 +08:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2020-11-14 11:24:49 +08:00
|
|
|
"strconv"
|
2020-11-03 14:53:36 +08:00
|
|
|
"time"
|
|
|
|
|
2020-11-14 11:24:49 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/conf"
|
|
|
|
|
2020-11-04 17:58:43 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/util/typeutil"
|
|
|
|
|
2020-11-03 14:53:36 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/internalpb"
|
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 (
|
|
|
|
tsCountPerRPC = 2 << 18 * 10
|
|
|
|
defaultUpdateInterval = 1000 * time.Millisecond
|
|
|
|
)
|
2020-10-30 16:27:58 +08:00
|
|
|
|
2020-11-03 14:53:36 +08:00
|
|
|
type TimestampAllocator struct {
|
|
|
|
Allocator
|
2020-11-04 17:58:43 +08:00
|
|
|
lastTsBegin Timestamp
|
|
|
|
lastTsEnd Timestamp
|
2020-10-30 16:27:58 +08:00
|
|
|
}
|
|
|
|
|
2020-11-03 14:53:36 +08:00
|
|
|
func NewTimestampAllocator(ctx context.Context) (*TimestampAllocator, error) {
|
2020-11-14 11:24:49 +08:00
|
|
|
|
|
|
|
masterAddr := conf.Config.Etcd.Address
|
|
|
|
masterAddr += ":"
|
|
|
|
masterAddr += strconv.FormatInt(int64(conf.Config.Master.Port), 10)
|
|
|
|
|
2020-11-03 14:53:36 +08:00
|
|
|
ctx1, cancel := context.WithCancel(ctx)
|
|
|
|
a := &TimestampAllocator{
|
|
|
|
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 = &ticker{
|
|
|
|
updateInterval: time.Second,
|
|
|
|
}
|
|
|
|
a.Allocator.syncFunc = a.syncTs
|
|
|
|
a.Allocator.processFunc = a.processFunc
|
|
|
|
return a, nil
|
2020-10-30 16:27:58 +08:00
|
|
|
}
|
|
|
|
|
2020-11-03 14:53:36 +08:00
|
|
|
func (ta *TimestampAllocator) syncTs() {
|
|
|
|
fmt.Println("sync TS")
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
req := &internalpb.TsoRequest{
|
2020-11-13 15:17:18 +08:00
|
|
|
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-10-30 16:27:58 +08:00
|
|
|
}
|
2020-11-03 14:53:36 +08:00
|
|
|
resp, err := ta.masterClient.AllocTimestamp(ctx, req)
|
2020-11-14 14:39:52 +08:00
|
|
|
log.Printf("resp: %v", resp)
|
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
|
|
|
|
}
|
|
|
|
ta.lastTsBegin = resp.GetTimestamp()
|
|
|
|
ta.lastTsEnd = ta.lastTsBegin + uint64(resp.GetCount())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ta *TimestampAllocator) processFunc(req request) {
|
|
|
|
if req == nil {
|
|
|
|
fmt.Println("Occur nil!!!!")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
tsoRequest := req.(*tsoRequest)
|
2020-11-16 17:01:10 +08:00
|
|
|
tsoRequest.timestamp = ta.lastTsBegin
|
|
|
|
ta.lastTsBegin++
|
2020-11-03 14:53:36 +08:00
|
|
|
fmt.Println("process tso")
|
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) {
|
|
|
|
//req := tsoReqPool.Get().(*tsoRequest)
|
|
|
|
req := &tsoRequest{
|
|
|
|
baseRequest: baseRequest{done: make(chan error), valid: false},
|
|
|
|
}
|
|
|
|
req.count = count
|
|
|
|
ta.reqs <- req
|
|
|
|
fmt.Println("YYYYY ", len(ta.reqs))
|
|
|
|
req.Wait()
|
|
|
|
|
|
|
|
if !req.IsValid() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|