2020-10-15 21:31:50 +08:00
|
|
|
package proxy
|
2020-10-15 16:32:22 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-11-03 14:53:36 +08:00
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
2020-10-15 16:32:22 +08:00
|
|
|
"github.com/apache/pulsar-client-go/pulsar"
|
2020-11-03 14:53:36 +08:00
|
|
|
"github.com/golang/protobuf/proto"
|
2020-10-30 16:27:58 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/errors"
|
2020-10-19 18:31:00 +08:00
|
|
|
pb "github.com/zilliztech/milvus-distributed/internal/proto/message"
|
2020-10-15 16:32:22 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type timeTick struct {
|
2020-11-04 17:58:43 +08:00
|
|
|
lastTick Timestamp
|
|
|
|
currentTick Timestamp
|
2020-10-15 16:32:22 +08:00
|
|
|
interval uint64
|
|
|
|
pulsarProducer pulsar.Producer
|
|
|
|
peer_id int64
|
|
|
|
ctx context.Context
|
2020-11-04 17:58:43 +08:00
|
|
|
areRequestsDelivered func(ts Timestamp) bool
|
|
|
|
getTimestamp func() (Timestamp, error)
|
2020-10-15 16:32:22 +08:00
|
|
|
}
|
|
|
|
|
2020-10-30 16:27:58 +08:00
|
|
|
func (tt *timeTick) tick() error {
|
2020-10-15 16:32:22 +08:00
|
|
|
if tt.lastTick == tt.currentTick {
|
2020-10-30 16:27:58 +08:00
|
|
|
ts, err := tt.getTimestamp()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-10-15 16:32:22 +08:00
|
|
|
}
|
|
|
|
tt.currentTick = ts
|
|
|
|
}
|
|
|
|
if tt.areRequestsDelivered(tt.currentTick) == false {
|
2020-10-30 16:27:58 +08:00
|
|
|
return errors.New("Failed")
|
2020-10-15 16:32:22 +08:00
|
|
|
}
|
|
|
|
tsm := pb.TimeSyncMsg{
|
|
|
|
Timestamp: uint64(tt.currentTick),
|
|
|
|
Peer_Id: tt.peer_id,
|
|
|
|
SyncType: pb.SyncType_READ,
|
|
|
|
}
|
|
|
|
payload, err := proto.Marshal(&tsm)
|
|
|
|
if err != nil {
|
2020-10-30 16:27:58 +08:00
|
|
|
return err
|
2020-10-15 16:32:22 +08:00
|
|
|
}
|
|
|
|
if _, err := tt.pulsarProducer.Send(tt.ctx, &pulsar.ProducerMessage{Payload: payload}); err != nil {
|
2020-10-30 16:27:58 +08:00
|
|
|
return err
|
2020-10-15 16:32:22 +08:00
|
|
|
}
|
|
|
|
tt.lastTick = tt.currentTick
|
2020-10-30 16:27:58 +08:00
|
|
|
return nil
|
2020-10-15 16:32:22 +08:00
|
|
|
}
|
|
|
|
|
2020-11-03 14:53:36 +08:00
|
|
|
func (tt *timeTick) Restart() error {
|
2020-10-15 16:32:22 +08:00
|
|
|
tt.lastTick = 0
|
2020-10-30 16:27:58 +08:00
|
|
|
ts, err := tt.getTimestamp()
|
2020-11-03 14:53:36 +08:00
|
|
|
if err != nil {
|
2020-10-30 16:27:58 +08:00
|
|
|
return err
|
2020-10-15 16:32:22 +08:00
|
|
|
}
|
2020-10-30 16:27:58 +08:00
|
|
|
|
2020-10-15 16:32:22 +08:00
|
|
|
tt.currentTick = ts
|
|
|
|
tick := time.Tick(time.Millisecond * time.Duration(tt.interval))
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-tick:
|
2020-10-30 16:27:58 +08:00
|
|
|
if err := tt.tick(); err != nil {
|
|
|
|
log.Printf("timeTick error")
|
2020-10-15 16:32:22 +08:00
|
|
|
}
|
|
|
|
case <-tt.ctx.Done():
|
|
|
|
tt.pulsarProducer.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2020-10-30 16:27:58 +08:00
|
|
|
return nil
|
2020-10-15 16:32:22 +08:00
|
|
|
}
|