90 Star 491 Fork 149

平凯星辰(北京)科技有限公司/tidb

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
oracle.go 2.32 KB
Copy Edit Raw Blame History
// Copyright 2018 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package mockoracle
import (
"sync"
"time"
"github.com/juju/errors"
"github.com/pingcap/tidb/store/tikv/oracle"
"golang.org/x/net/context"
)
var errStopped = errors.New("stopped")
// MockOracle is a mock oracle for test.
type MockOracle struct {
sync.RWMutex
stop bool
offset time.Duration
lastTS uint64
}
// Enable enables the Oracle
func (o *MockOracle) Enable() {
o.Lock()
defer o.Unlock()
o.stop = false
}
// Disable disables the Oracle
func (o *MockOracle) Disable() {
o.Lock()
defer o.Unlock()
o.stop = true
}
func (o *MockOracle) setOffset(offset time.Duration) {
o.Lock()
defer o.Unlock()
o.offset = offset
}
// AddOffset adds the offset of the oracle.
func (o *MockOracle) AddOffset(d time.Duration) {
o.Lock()
defer o.Unlock()
o.offset += d
}
// GetTimestamp implements oracle.Oracle interface.
func (o *MockOracle) GetTimestamp(context.Context) (uint64, error) {
o.Lock()
defer o.Unlock()
if o.stop {
return 0, errors.Trace(errStopped)
}
physical := oracle.GetPhysical(time.Now().Add(o.offset))
ts := oracle.ComposeTS(physical, 0)
if oracle.ExtractPhysical(o.lastTS) == physical {
ts = o.lastTS + 1
}
o.lastTS = ts
return ts, nil
}
type mockOracleFuture struct {
o *MockOracle
ctx context.Context
}
func (m *mockOracleFuture) Wait() (uint64, error) {
return m.o.GetTimestamp(m.ctx)
}
// GetTimestampAsync implements oracle.Oracle interface.
func (o *MockOracle) GetTimestampAsync(ctx context.Context) oracle.Future {
return &mockOracleFuture{o, ctx}
}
// IsExpired implements oracle.Oracle interface.
func (o *MockOracle) IsExpired(lockTimestamp uint64, TTL uint64) bool {
o.RLock()
defer o.RUnlock()
return oracle.GetPhysical(time.Now().Add(o.offset)) >= oracle.ExtractPhysical(lockTimestamp)+int64(TTL)
}
// Close implements oracle.Oracle interface.
func (o *MockOracle) Close() {
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/pingcap/tidb.git
git@gitee.com:pingcap/tidb.git
pingcap
tidb
tidb
v2.0.0

Search