Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

meta/autoid: make step variable atomic to fix data race tests #57976

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions pkg/meta/autoid/autoid.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"math"
"strconv"
"sync"
"sync/atomic"
"time"

"github.com/pingcap/errors"
Expand Down Expand Up @@ -109,9 +110,6 @@ func AutoRandomRangeBitsNormalize(rangeBits int) (ret uint64, err error) {
return uint64(rangeBits), nil
}

// Test needs to change it, so it's a variable.
var step = int64(30000)

// AllocatorType is the type of allocator for generating auto-id. Different type of allocators use different key-value pairs.
type AllocatorType uint8

Expand Down Expand Up @@ -274,14 +272,18 @@ type allocator struct {
sequence *model.SequenceInfo
}

// GetStep is only used by tests
// Test needs to change it, so it's a variable.
// Don't use it directly, use the GetStep/SetStep function.
var defaultStep = int64(30000)

// GetStep gets the defautStep value.
func GetStep() int64 {
return step
return atomic.LoadInt64(&defaultStep)
}

// SetStep is only used by tests
func SetStep(s int64) {
step = s
atomic.StoreInt64(&defaultStep, s)
}

// Base implements autoid.Allocator Base interface.
Expand Down Expand Up @@ -565,7 +567,7 @@ func NextStep(curStep int64, consumeDur time.Duration) int64 {
})
failpoint.Inject("mockAutoIDChange", func(val failpoint.Value) {
if val.(bool) {
failpoint.Return(step)
failpoint.Return(GetStep())
}
})

Expand Down Expand Up @@ -627,7 +629,7 @@ func NewAllocator(r Requirement, dbID, tbID int64, isUnsigned bool,
dbID: dbID,
tbID: tbID,
isUnsigned: isUnsigned,
step: step,
step: GetStep(),
lastAllocTime: time.Now(),
allocType: allocType,
}
Expand All @@ -646,7 +648,7 @@ func NewAllocator(r Requirement, dbID, tbID int64, isUnsigned bool,
// Now that the autoid and rowid allocator are separated, the AUTO_ID_CACHE 1 setting should not make
// the rowid allocator do not use cache.
alloc.customStep = false
alloc.step = step
alloc.step = GetStep()
}
}

Expand Down