2020-11-20 17:10:24 +08:00
|
|
|
package master
|
2020-10-29 09:31:08 +08:00
|
|
|
|
|
|
|
import (
|
2020-11-05 19:11:12 +08:00
|
|
|
"log"
|
2020-10-29 09:31:08 +08:00
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/errors"
|
2020-11-16 21:10:43 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/kv"
|
2020-11-07 16:18:23 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/util/tsoutil"
|
2020-10-29 09:31:08 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/util/typeutil"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Allocator is a Timestamp Oracle allocator.
|
|
|
|
type Allocator interface {
|
|
|
|
// Initialize is used to initialize a TSO allocator.
|
|
|
|
// It will synchronize TSO with etcd and initialize the
|
|
|
|
// memory for later allocation work.
|
|
|
|
Initialize() error
|
|
|
|
// UpdateTSO is used to update the TSO in memory and the time window in etcd.
|
|
|
|
UpdateTSO() error
|
|
|
|
// SetTSO sets the physical part with given tso. It's mainly used for BR restore
|
|
|
|
// and can not forcibly set the TSO smaller than now.
|
|
|
|
SetTSO(tso uint64) error
|
|
|
|
// GenerateTSO is used to generate a given number of TSOs.
|
|
|
|
// Make sure you have initialized the TSO allocator before calling.
|
2020-10-30 16:27:58 +08:00
|
|
|
GenerateTSO(count uint32) (uint64, error)
|
2020-10-29 09:31:08 +08:00
|
|
|
// Reset is used to reset the TSO allocator.
|
|
|
|
Reset()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GlobalTSOAllocator is the global single point TSO allocator.
|
|
|
|
type GlobalTSOAllocator struct {
|
2020-11-07 16:18:23 +08:00
|
|
|
tso *timestampOracle
|
2020-10-29 09:31:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewGlobalTSOAllocator creates a new global TSO allocator.
|
2020-12-07 15:22:20 +08:00
|
|
|
func NewGlobalTSOAllocator(key string, kvBase kv.TxnBase) *GlobalTSOAllocator {
|
2020-11-12 12:04:12 +08:00
|
|
|
var saveInterval = 3 * time.Second
|
2020-10-29 09:31:08 +08:00
|
|
|
return &GlobalTSOAllocator{
|
2020-11-07 16:18:23 +08:00
|
|
|
tso: ×tampOracle{
|
|
|
|
kvBase: kvBase,
|
2020-10-29 09:31:08 +08:00
|
|
|
saveInterval: saveInterval,
|
2020-11-03 14:53:36 +08:00
|
|
|
maxResetTSGap: func() time.Duration { return 3 * time.Second },
|
|
|
|
key: key,
|
2020-10-29 09:31:08 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize will initialize the created global TSO allocator.
|
|
|
|
func (gta *GlobalTSOAllocator) Initialize() error {
|
2020-11-12 11:18:23 +08:00
|
|
|
return gta.tso.InitTimestamp()
|
2020-10-29 09:31:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateTSO is used to update the TSO in memory and the time window in etcd.
|
|
|
|
func (gta *GlobalTSOAllocator) UpdateTSO() error {
|
2020-11-07 16:18:23 +08:00
|
|
|
return gta.tso.UpdateTimestamp()
|
2020-10-29 09:31:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetTSO sets the physical part with given tso.
|
|
|
|
func (gta *GlobalTSOAllocator) SetTSO(tso uint64) error {
|
2020-11-07 16:18:23 +08:00
|
|
|
return gta.tso.ResetUserTimestamp(tso)
|
2020-10-29 09:31:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateTSO is used to generate a given number of TSOs.
|
|
|
|
// Make sure you have initialized the TSO allocator before calling.
|
2020-10-30 16:27:58 +08:00
|
|
|
func (gta *GlobalTSOAllocator) GenerateTSO(count uint32) (uint64, error) {
|
2020-11-12 12:04:12 +08:00
|
|
|
var physical, logical int64
|
2020-10-29 09:31:08 +08:00
|
|
|
if count == 0 {
|
2020-10-30 16:27:58 +08:00
|
|
|
return 0, errors.New("tso count should be positive")
|
2020-10-29 09:31:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
maxRetryCount := 10
|
|
|
|
|
|
|
|
for i := 0; i < maxRetryCount; i++ {
|
2020-11-07 16:18:23 +08:00
|
|
|
current := (*atomicObject)(atomic.LoadPointer(>a.tso.TSO))
|
2020-11-13 10:00:32 +08:00
|
|
|
if current == nil || current.physical.Equal(typeutil.ZeroTime) {
|
2020-10-29 09:31:08 +08:00
|
|
|
// If it's leader, maybe SyncTimestamp hasn't completed yet
|
2020-11-05 19:11:12 +08:00
|
|
|
log.Println("sync hasn't completed yet, wait for a while")
|
2020-10-29 09:31:08 +08:00
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-10-30 16:27:58 +08:00
|
|
|
physical = current.physical.UnixNano() / int64(time.Millisecond)
|
|
|
|
logical = atomic.AddInt64(¤t.logical, int64(count))
|
|
|
|
if logical >= maxLogical {
|
2020-11-05 19:11:12 +08:00
|
|
|
log.Println("logical part outside of max logical interval, please check ntp time",
|
2020-10-29 09:31:08 +08:00
|
|
|
zap.Int("retry-count", i))
|
|
|
|
time.Sleep(UpdateTimestampStep)
|
|
|
|
continue
|
|
|
|
}
|
2020-10-30 16:27:58 +08:00
|
|
|
return tsoutil.ComposeTS(physical, logical), nil
|
2020-10-29 09:31:08 +08:00
|
|
|
}
|
2020-10-30 16:27:58 +08:00
|
|
|
return 0, errors.New("can not get timestamp")
|
2020-10-29 09:31:08 +08:00
|
|
|
}
|
|
|
|
|
2020-11-12 11:18:23 +08:00
|
|
|
func (gta *GlobalTSOAllocator) Alloc(count uint32) (typeutil.Timestamp, error) {
|
|
|
|
//return gta.tso.SyncTimestamp()
|
|
|
|
start, err := gta.GenerateTSO(count)
|
|
|
|
if err != nil {
|
|
|
|
return typeutil.ZeroTimestamp, err
|
|
|
|
}
|
|
|
|
//ret := make([]typeutil.Timestamp, count)
|
|
|
|
//for i:=uint32(0); i < count; i++{
|
|
|
|
// ret[i] = start + uint64(i)
|
|
|
|
//}
|
|
|
|
return start, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gta *GlobalTSOAllocator) AllocOne() (typeutil.Timestamp, error) {
|
|
|
|
return gta.GenerateTSO(1)
|
|
|
|
}
|
|
|
|
|
2020-10-29 09:31:08 +08:00
|
|
|
// Reset is used to reset the TSO allocator.
|
|
|
|
func (gta *GlobalTSOAllocator) Reset() {
|
2020-11-07 16:18:23 +08:00
|
|
|
gta.tso.ResetTimestamp()
|
2020-10-29 09:31:08 +08:00
|
|
|
}
|