1 Star 0 Fork 0

zhoujin826/tidb

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
local_version_provider.go 1.53 KB
一键复制 编辑 原始数据 按行查看 历史
zz-jason 提交于 2017-09-30 15:53 . *: make "unconvert" happy (#4700)
package localstore
import (
"errors"
"sync"
"time"
log "github.com/Sirupsen/logrus"
"github.com/pingcap/tidb/kv"
)
// ErrOverflow is the error returned by CurrentVersion, it describes if
// there're too many versions allocations in a very short period of time, ID
// may conflict.
var ErrOverflow = errors.New("overflow when allocating new version")
// LocalVersionProvider uses local timestamp for version.
type LocalVersionProvider struct {
mu sync.Mutex
lastTimestamp uint64
// logical guaranteed version's monotonic increasing for calls when lastTimestamp
// are equal.
logical uint64
}
const (
timePrecisionOffset = 18
)
func time2TsPhysical(t time.Time) uint64 {
return uint64((t.UnixNano() / int64(time.Millisecond)) << timePrecisionOffset)
}
func version2Second(v kv.Version) int64 {
return int64(v.Ver>>timePrecisionOffset) / 1000
}
// CurrentVersion implements the VersionProvider's GetCurrentVer interface.
func (l *LocalVersionProvider) CurrentVersion() (kv.Version, error) {
l.mu.Lock()
defer l.mu.Unlock()
for {
var ts uint64
ts = time2TsPhysical(time.Now())
if l.lastTimestamp > ts {
log.Error("[kv] invalid physical time stamp")
continue
}
if l.lastTimestamp == ts {
l.logical++
if l.logical >= 1<<timePrecisionOffset {
return kv.Version{}, ErrOverflow
}
return kv.Version{Ver: ts + l.logical}, nil
}
l.lastTimestamp = ts
l.logical = 0
return kv.Version{Ver: ts}, nil
}
}
func localVersionToTimestamp(ver kv.Version) uint64 {
return ver.Ver >> timePrecisionOffset
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zhoujin826/tidb.git
git@gitee.com:zhoujin826/tidb.git
zhoujin826
tidb
tidb
v1.0.1

搜索帮助