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

*: fix data race on the SchemaOutOfDateRetryTimes/SchemaOutOfDateRetryInterval #31815

Merged
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions ddl/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6869,8 +6869,8 @@ func (s *testSerialDBSuite) TestAddIndexFailOnCaseWhenCanExit(c *C) {

func init() {
// Make sure it will only be executed once.
domain.SchemaOutOfDateRetryInterval = int64(50 * time.Millisecond)
domain.SchemaOutOfDateRetryTimes = int32(50)
domain.SchemaOutOfDateRetryInterval.Store(50 * time.Millisecond)
domain.SchemaOutOfDateRetryTimes.Store(50)
}

func (s *testSerialDBSuite) TestCreateTableWithIntegerLengthWaring(c *C) {
Expand Down
6 changes: 3 additions & 3 deletions ddl/failtest/fail_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,10 @@ func TestFailSchemaSyncer(t *testing.T) {
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int)")
defer tk.MustExec("drop table if exists t")
originalRetryTimes := domain.SchemaOutOfDateRetryTimes
domain.SchemaOutOfDateRetryTimes = 1
originalRetryTimes := domain.SchemaOutOfDateRetryTimes.Load()
domain.SchemaOutOfDateRetryTimes.Store(1)
defer func() {
domain.SchemaOutOfDateRetryTimes = originalRetryTimes
domain.SchemaOutOfDateRetryTimes.Store(originalRetryTimes)
}()
require.True(t, s.dom.SchemaValidator.IsStarted())
mockSyncer, ok := s.dom.DDL().SchemaSyncer().(*ddl.MockSchemaSyncer)
Expand Down
12 changes: 6 additions & 6 deletions domain/domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,14 +322,14 @@ func SubTestDomain(t *testing.T) {

// For schema check, it tests for getting the result of "ResultUnknown".
schemaChecker := NewSchemaChecker(dom, is.SchemaMetaVersion(), nil)
originalRetryTime := SchemaOutOfDateRetryTimes
originalRetryInterval := SchemaOutOfDateRetryInterval
originalRetryTime := SchemaOutOfDateRetryTimes.Load()
originalRetryInterval := SchemaOutOfDateRetryInterval.Load()
// Make sure it will retry one time and doesn't take a long time.
SchemaOutOfDateRetryTimes = 1
SchemaOutOfDateRetryInterval = int64(time.Millisecond * 1)
SchemaOutOfDateRetryTimes.Store(1)
SchemaOutOfDateRetryInterval.Store(time.Millisecond * 1)
defer func() {
SchemaOutOfDateRetryTimes = originalRetryTime
SchemaOutOfDateRetryInterval = originalRetryInterval
SchemaOutOfDateRetryTimes.Store(originalRetryTime)
SchemaOutOfDateRetryInterval.Store(originalRetryInterval)
}()
dom.SchemaValidator.Stop()
_, err = schemaChecker.Check(uint64(123456))
Expand Down
12 changes: 6 additions & 6 deletions domain/schema_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
package domain

import (
"sync/atomic"
"time"

"github.com/pingcap/tidb/metrics"
"github.com/tikv/client-go/v2/tikv"
"github.com/tikv/client-go/v2/txnkv/transaction"
atomicutil "go.uber.org/atomic"
)

// SchemaChecker is used for checking schema-validity.
Expand All @@ -38,9 +38,9 @@ func (i intSchemaVer) SchemaMetaVersion() int64 {

var (
// SchemaOutOfDateRetryInterval is the backoff time before retrying.
SchemaOutOfDateRetryInterval = int64(500 * time.Millisecond)
SchemaOutOfDateRetryInterval = atomicutil.NewDuration(500 * time.Millisecond)
// SchemaOutOfDateRetryTimes is the max retry count when the schema is out of date.
SchemaOutOfDateRetryTimes = int32(10)
SchemaOutOfDateRetryTimes = atomicutil.NewInt32(10)
)

// NewSchemaChecker creates a new schema checker.
Expand All @@ -59,8 +59,8 @@ func (s *SchemaChecker) Check(txnTS uint64) (*transaction.RelatedSchemaChange, e

// CheckBySchemaVer checks if the schema version valid or not at txnTS.
func (s *SchemaChecker) CheckBySchemaVer(txnTS uint64, startSchemaVer tikv.SchemaVer) (*transaction.RelatedSchemaChange, error) {
schemaOutOfDateRetryInterval := atomic.LoadInt64(&SchemaOutOfDateRetryInterval)
schemaOutOfDateRetryTimes := int(atomic.LoadInt32(&SchemaOutOfDateRetryTimes))
schemaOutOfDateRetryInterval := SchemaOutOfDateRetryInterval.Load()
schemaOutOfDateRetryTimes := int(SchemaOutOfDateRetryTimes.Load())
for i := 0; i < schemaOutOfDateRetryTimes; i++ {
relatedChange, CheckResult := s.SchemaValidator.Check(txnTS, startSchemaVer.SchemaMetaVersion(), s.relatedTableIDs)
switch CheckResult {
Expand All @@ -70,7 +70,7 @@ func (s *SchemaChecker) CheckBySchemaVer(txnTS uint64, startSchemaVer tikv.Schem
metrics.SchemaLeaseErrorCounter.WithLabelValues("changed").Inc()
return relatedChange, ErrInfoSchemaChanged
case ResultUnknown:
time.Sleep(time.Duration(schemaOutOfDateRetryInterval))
time.Sleep(schemaOutOfDateRetryInterval)
}

}
Expand Down
10 changes: 6 additions & 4 deletions session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2162,11 +2162,13 @@ func (s *testSchemaSuiteBase) TearDownSuite(c *C) {
}

func (s *testSchemaSerialSuite) TestLoadSchemaFailed(c *C) {
atomic.StoreInt32(&domain.SchemaOutOfDateRetryTimes, int32(3))
atomic.StoreInt64(&domain.SchemaOutOfDateRetryInterval, int64(20*time.Millisecond))
originalRetryTime := domain.SchemaOutOfDateRetryTimes.Load()
originalRetryInterval := domain.SchemaOutOfDateRetryInterval.Load()
domain.SchemaOutOfDateRetryTimes.Store(3)
domain.SchemaOutOfDateRetryInterval.Store(20 * time.Millisecond)
defer func() {
atomic.StoreInt32(&domain.SchemaOutOfDateRetryTimes, 10)
atomic.StoreInt64(&domain.SchemaOutOfDateRetryInterval, int64(500*time.Millisecond))
domain.SchemaOutOfDateRetryTimes.Store(originalRetryTime)
domain.SchemaOutOfDateRetryInterval.Store(originalRetryInterval)
}()

tk := testkit.NewTestKitWithInit(c, s.store)
Expand Down