Skip to content

Commit a262d63

Browse files
committed
adopt ai suggestion
Signed-off-by: bufferflies <1045931706@qq.com>
1 parent 4b29385 commit a262d63

File tree

3 files changed

+25
-22
lines changed

3 files changed

+25
-22
lines changed

pkg/tso/allocator.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ func (a *Allocator) IsInitialize() bool {
167167
// UpdateTSO is used to update the TSO in memory and the time window in etcd.
168168
func (a *Allocator) UpdateTSO() (err error) {
169169
for i := range maxUpdateTSORetryCount {
170-
err = a.timestampOracle.updateTimestamp(a.timestampOracle.tsoMux.physical, true)
170+
current, _ := a.timestampOracle.getTSO()
171+
err = a.timestampOracle.updateTimestamp(current, true)
171172
if err == nil {
172173
return nil
173174
}

pkg/tso/tso.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func (t *timestampOracle) getTSO() (time.Time, int64) {
119119
return t.tsoMux.physical, t.tsoMux.logical
120120
}
121121

122-
// generateTSO will add the TSO's logical part with the given count and returns the new TSO result.
122+
// generateTSO will add the TSO's logical part with the given count and returns the new TSO result with the current physical time for update.
123123
func (t *timestampOracle) generateTSO(ctx context.Context, count int64) (physical int64, logical int64, current time.Time) {
124124
defer trace.StartRegion(ctx, "timestampOracle.generateTSO").End()
125125
t.tsoMux.Lock()
@@ -208,7 +208,8 @@ func (t *timestampOracle) syncTimestamp() error {
208208
zap.Time("last", last), zap.Time("last-saved", lastSavedTime),
209209
zap.Time("save", save), zap.Time("next", next))
210210
// save into memory
211-
t.setTSOPhysical(t.tsoMux.physical, next, true)
211+
current, _ := t.getTSO()
212+
t.setTSOPhysical(current, next, true)
212213
return nil
213214
}
214215

pkg/tso/tso_test.go

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,21 @@ import (
2727
"github.com/tikv/pd/pkg/election"
2828
)
2929

30-
type MokElection struct{}
30+
type MockElection struct{}
3131

32-
func (*MokElection) ID() uint64 { return 0 }
33-
func (*MokElection) Name() string { return "" }
34-
func (*MokElection) MemberValue() string { return "" }
35-
func (*MokElection) Client() *clientv3.Client { return nil }
36-
func (*MokElection) IsServing() bool { return true }
37-
func (*MokElection) PromoteSelf() {}
38-
func (*MokElection) Campaign(_ context.Context, _ int64) error {
32+
func (*MockElection) ID() uint64 { return 0 }
33+
func (*MockElection) Name() string { return "" }
34+
func (*MockElection) MemberValue() string { return "" }
35+
func (*MockElection) Client() *clientv3.Client { return nil }
36+
func (*MockElection) IsServing() bool { return true }
37+
func (*MockElection) PromoteSelf() {}
38+
func (*MockElection) Campaign(_ context.Context, _ int64) error {
3939
return nil
4040
}
41-
func (*MokElection) Resign() {}
42-
func (*MokElection) GetServingUrls() []string { return nil }
43-
func (*MokElection) GetElectionPath() string { return "" }
44-
func (*MokElection) GetLeadership() *election.Leadership { return nil }
41+
func (*MockElection) Resign() {}
42+
func (*MockElection) GetServingUrls() []string { return nil }
43+
func (*MockElection) GetElectionPath() string { return "" }
44+
func (*MockElection) GetLeadership() *election.Leadership { return nil }
4545

4646
func TestGenerateTSO(t *testing.T) {
4747
re := require.New(t)
@@ -57,7 +57,7 @@ func TestGenerateTSO(t *testing.T) {
5757
updatePhysicalInterval: 5 * time.Second,
5858
maxResetTSGap: func() time.Duration { return time.Hour },
5959
metrics: newTSOMetrics("test"),
60-
member: &MokElection{},
60+
member: &MockElection{},
6161
}
6262

6363
// update physical time interval failed due to reach the lastSavedTime, it needs to save storage first, but this behavior is not allowed.
@@ -86,20 +86,20 @@ func TestCurrentGetTSO(t *testing.T) {
8686
updatePhysicalInterval: 5 * time.Second,
8787
maxResetTSGap: func() time.Duration { return time.Hour },
8888
metrics: newTSOMetrics("test"),
89-
member: &MokElection{},
89+
member: &MockElection{},
9090
}
9191

9292
runDuration := 5 * time.Second
9393
timestampOracle.lastSavedTime.Store(current.Add(runDuration))
9494
runCtx, runCancel := context.WithTimeout(ctx, runDuration-time.Second)
9595
defer runCancel()
9696
wg := sync.WaitGroup{}
97-
wg.Add(100)
97+
wg.Add(10)
9898
changes := atomic.Int32{}
9999
totalTso := atomic.Int32{}
100-
for i := range 100 {
100+
for i := range 10 {
101101
go func(i int) {
102-
physical := timestampOracle.tsoMux.physical
102+
pre, _ := timestampOracle.getTSO()
103103
defer wg.Done()
104104
for {
105105
select {
@@ -110,9 +110,10 @@ func TestCurrentGetTSO(t *testing.T) {
110110
totalTso.Add(1)
111111
re.NoError(err)
112112
if i == 0 {
113-
if physical != timestampOracle.tsoMux.physical {
113+
physical, _ := timestampOracle.getTSO()
114+
if pre != physical {
114115
changes.Add(1)
115-
physical = timestampOracle.tsoMux.physical
116+
pre = physical
116117
}
117118
}
118119
}

0 commit comments

Comments
 (0)