pingcap / tidb

TiDB - the open-source, cloud-native, distributed SQL database designed for modern applications.
https://pingcap.com
Apache License 2.0
37.41k stars 5.85k forks source link

DATA RACE at the TableCommon.writableConstraints #54377

Open hawkingrei opened 5 months ago

hawkingrei commented 5 months ago

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

2. What did you expect to see? (Required)

3. What did you see instead (Required)

==================
WARNING: DATA RACE
Read at 0x00c0098d55e8 by goroutine 34941:
  reflect.Value.Pointer()
      GOROOT/src/reflect/value.go:2182 +0x1a4
  github.com/pingcap/tidb/pkg/infoschema/internal.sizeOf()
      pkg/infoschema/internal/sizer.go:47 +0x6d3
  github.com/pingcap/tidb/pkg/infoschema/internal.sizeOf()
      pkg/infoschema/internal/sizer.go:64 +0xaa7
  github.com/pingcap/tidb/pkg/infoschema/internal.sizeOf()
      pkg/infoschema/internal/sizer.go:64 +0xaa7
  github.com/pingcap/tidb/pkg/infoschema/internal.sizeOf()
      pkg/infoschema/internal/sizer.go:93 +0x604
  github.com/pingcap/tidb/pkg/infoschema/internal.sizeOf()
      pkg/infoschema/internal/sizer.go:118 +0xf31
  github.com/pingcap/tidb/pkg/infoschema/internal.sizeOf()
      pkg/infoschema/internal/sizer.go:64 +0xaa7
  github.com/pingcap/tidb/pkg/infoschema/internal.sizeOf()
      pkg/infoschema/internal/sizer.go:93 +0x604
  github.com/pingcap/tidb/pkg/infoschema/internal.sizeOf()
      pkg/infoschema/internal/sizer.go:141 +0x207
  github.com/pingcap/tidb/pkg/infoschema/internal.sizeOf()
      pkg/infoschema/internal/sizer.go:64 +0xaa7
  github.com/pingcap/tidb/pkg/infoschema/internal.Sizeof()
      pkg/infoschema/internal/sizer.go:28 +0x1b7
  github.com/pingcap/tidb/pkg/infoschema.(*entry[go.shape.struct { github.com/pingcap/tidb/pkg/infoschema.tableID int64; github.com/pingcap/tidb/pkg/infoschema.schemaVersion int64 },go.shape.interface { AddRecord(github.com/pingcap/tidb/pkg/table/context.MutateContext, []github.com/pingcap/tidb/pkg/types.Datum, ...github.com/pingcap/tidb/pkg/table.AddRecordOption) (github.com/pingcap/tidb/pkg/kv.Handle, error); Allocators(github.com/pingcap/tidb/pkg/table/context.AllocatorContext) github.com/pingcap/tidb/pkg/meta/autoid.Allocators; Cols() []*github.com/pingcap/tidb/pkg/table.Column; DeletableCols() []*github.com/pingcap/tidb/pkg/table.Column; FullHiddenColsAndVisibleCols() []*github.com/pingcap/tidb/pkg/table.Column; GetPartitionedTable() github.com/pingcap/tidb/pkg/table.PartitionedTable; HiddenCols() []*github.com/pingcap/tidb/pkg/table.Column; IndexPrefix() github.com/pingcap/tidb/pkg/kv.Key; Indices() []github.com/pingcap/tidb/pkg/table.Index; Meta() *github.com/pingcap/tidb/pkg/parser/model.TableInfo; RecordPrefix() github.com/pingcap/tidb/pkg/kv.Key; RemoveRecord(github.com/pingcap/tidb/pkg/table/context.MutateContext, github.com/pingcap/tidb/pkg/kv.Handle, []github.com/pingcap/tidb/pkg/types.Datum) error; Type() github.com/pingcap/tidb/pkg/table.Type; UpdateRecord(context.Context, github.com/pingcap/tidb/pkg/table/context.MutateContext, github.com/pingcap/tidb/pkg/kv.Handle, []github.com/pingcap/tidb/pkg/types.Datum, []github.com/pingcap/tidb/pkg/types.Datum, []bool) error; VisibleCols() []*github.com/pingcap/tidb/pkg/table.Column; WritableCols() []*github.com/pingcap/tidb/pkg/table.Column }]).Size()
      pkg/infoschema/sieve.go:36 +0x67
  github.com/pingcap/tidb/pkg/infoschema.(*Sieve[go.shape.struct { github.com/pingcap/tidb/pkg/infoschema.tableID int64; github.com/pingcap/tidb/pkg/infoschema.schemaVersion int64 },go.shape.interface { AddRecord(github.com/pingcap/tidb/pkg/table/context.MutateContext, []github.com/pingcap/tidb/pkg/types.Datum, ...github.com/pingcap/tidb/pkg/table.AddRecordOption) (github.com/pingcap/tidb/pkg/kv.Handle, error); Allocators(github.com/pingcap/tidb/pkg/table/context.AllocatorContext) github.com/pingcap/tidb/pkg/meta/autoid.Allocators; Cols() []*github.com/pingcap/tidb/pkg/table.Column; DeletableCols() []*github.com/pingcap/tidb/pkg/table.Column; FullHiddenColsAndVisibleCols() []*github.com/pingcap/tidb/pkg/table.Column; GetPartitionedTable() github.com/pingcap/tidb/pkg/table.PartitionedTable; HiddenCols() []*github.com/pingcap/tidb/pkg/table.Column; IndexPrefix() github.com/pingcap/tidb/pkg/kv.Key; Indices() []github.com/pingcap/tidb/pkg/table.Index; Meta() *github.com/pingcap/tidb/pkg/parser/model.TableInfo; RecordPrefix() github.com/pingcap/tidb/pkg/kv.Key; RemoveRecord(github.com/pingcap/tidb/pkg/table/context.MutateContext, github.com/pingcap/tidb/pkg/kv.Handle, []github.com/pingcap/tidb/pkg/types.Datum) error; Type() github.com/pingcap/tidb/pkg/table.Type; UpdateRecord(context.Context, github.com/pingcap/tidb/pkg/table/context.MutateContext, github.com/pingcap/tidb/pkg/kv.Handle, []github.com/pingcap/tidb/pkg/types.Datum, []github.com/pingcap/tidb/pkg/types.Datum, []bool) error; VisibleCols() []*github.com/pingcap/tidb/pkg/table.Column; WritableCols() []*github.com/pingcap/tidb/pkg/table.Column }]).Set()
      pkg/infoschema/sieve.go:130 +0x3bd
  github.com/pingcap/tidb/pkg/infoschema.(*infoschemaV2).tableByID()
      pkg/infoschema/infoschema_v2.go:419 +0x4fd
  github.com/pingcap/tidb/pkg/infoschema.(*infoschemaV2).TableByID()
      pkg/infoschema/infoschema_v2.go:385 +0x35
  github.com/pingcap/tidb/pkg/infoschema.allocByID()
      pkg/infoschema/infoschema_v2.go:912 +0x75
  github.com/pingcap/tidb/pkg/infoschema.dropTableForUpdate()
      pkg/infoschema/builder.go:388 +0x124
  github.com/pingcap/tidb/pkg/infoschema.(*Builder).applyTableUpdateV2()
      pkg/infoschema/infoschema_v2.go:931 +0x289
  github.com/pingcap/tidb/pkg/infoschema.applyTableUpdate()
      pkg/infoschema/infoschema_v2.go:822 +0x50
  github.com/pingcap/tidb/pkg/infoschema.applyDefaultAction()
      pkg/infoschema/builder.go:315 +0x5b
  github.com/pingcap/tidb/pkg/infoschema.(*Builder).ApplyDiff()
      pkg/infoschema/builder.go:102 +0x756
  github.com/pingcap/tidb/pkg/domain.(*Domain).tryLoadSchemaDiffs()
      pkg/domain/domain.go:493 +0x61a
  github.com/pingcap/tidb/pkg/domain.(*Domain).loadInfoSchema()
      pkg/domain/domain.go:288 +0x7bb
  github.com/pingcap/tidb/pkg/domain.(*Domain).Reload()
      pkg/domain/domain.go:628 +0x258
  github.com/pingcap/tidb/pkg/domain.(*Domain).loadSchemaInLoop()
      pkg/domain/domain.go:941 +0x33a
  github.com/pingcap/tidb/pkg/domain.(*Domain).Init.func2()
      pkg/domain/domain.go:1310 +0x54
  github.com/pingcap/tidb/pkg/util.(*WaitGroupEnhancedWrapper).Run.func1()
      pkg/util/wait_group_wrapper.go:99 +0xd2
Previous write at 0x00c0098d55e8 by goroutine 63:
  github.com/pingcap/tidb/pkg/table/tables.(*TableCommon).WritableConstraint.func1()
      pkg/table/tables/tables.go:410 +0x90
  sync.(*Once).doSlow()
      GOROOT/src/sync/once.go:74 +0xf0
  sync.(*Once).Do()
      GOROOT/src/sync/once.go:65 +0x44
  github.com/pingcap/tidb/pkg/table/tables.(*TableCommon).WritableConstraint()
      pkg/table/tables/tables.go:408 +0xae
  github.com/pingcap/tidb/pkg/table/tables.(*TableCommon).CheckRowConstraint()
      pkg/table/tables/tables.go:427 +0x84
  github.com/pingcap/tidb/pkg/table/tables.(*TableCommon).UpdateRecord()
      pkg/table/tables/tables.go:576 +0x2967
  github.com/pingcap/tidb/pkg/table/tables.(*partition).UpdateRecord()
      <autogenerated>:1 +0x124
  github.com/pingcap/tidb/pkg/table/tables.partitionedTableUpdateRecord()
      pkg/table/tables/partition.go:1823 +0x12e2
  github.com/pingcap/tidb/pkg/table/tables.(*partitionedTable).UpdateRecord()
      pkg/table/tables/partition.go:1722 +0x112
  github.com/pingcap/tidb/pkg/executor.updateRecord()
      pkg/executor/write.go:197 +0x196b
  github.com/pingcap/tidb/pkg/executor.(*UpdateExec).exec()
      pkg/executor/update.go:208 +0x9af
  github.com/pingcap/tidb/pkg/executor.(*UpdateExec).updateRows()
      pkg/executor/update.go:332 +0x12aa
  github.com/pingcap/tidb/pkg/executor.(*UpdateExec).Next()
      pkg/executor/update.go:252 +0x417
  github.com/pingcap/tidb/pkg/executor/internal/exec.Next()
      pkg/executor/internal/exec/executor.go:410 +0x418
  github.com/pingcap/tidb/pkg/executor.(*ExecStmt).next()
      pkg/executor/adapter.go:1250 +0x84
  github.com/pingcap/tidb/pkg/executor.(*ExecStmt).handleNoDelayExecutor()
      pkg/executor/adapter.go:999 +0x55b
  github.com/pingcap/tidb/pkg/executor.(*ExecStmt).handleNoDelay()
      pkg/executor/adapter.go:833 +0x3ca
  github.com/pingcap/tidb/pkg/executor.(*ExecStmt).Exec()
      pkg/executor/adapter.go:597 +0x1326
  github.com/pingcap/tidb/pkg/session.runStmt()
      pkg/session/session.go:2296 +0x5d3
  github.com/pingcap/tidb/pkg/session.(*session).ExecuteStmt()
      pkg/session/session.go:2157 +0x1ac4
  github.com/pingcap/tidb/pkg/session.(*session).ExecutePreparedStmt()
      pkg/session/session.go:2493 +0x264
  github.com/pingcap/tidb/pkg/testkit.(*TestKit).ExecWithContext()
      pkg/testkit/testkit.go:427 +0xb52
  github.com/pingcap/tidb/pkg/testkit.(*TestKit).MustExecWithContext()
      pkg/testkit/testkit.go:166 +0xab
  github.com/pingcap/tidb/pkg/testkit.(*TestKit).MustExec()
      pkg/testkit/testkit.go:161 +0x128
  pkg/ddl/tests/partition/partition_test.testPartitionDropIndex()
      pkg/ddl/tests/partition/db_partition_test.go:2428 +0x60f
  github.com/pingcap/tidb/pkg/sessionctx/variable.glob..func596()
      pkg/sessionctx/variable/sysvar.go:3145 +0x58
  github.com/pingcap/tidb/pkg/sessionctx/variable.(*SysVar).Validate()
      pkg/sessionctx/variable/variable.go:305 +0x123
  github.com/pingcap/tidb/pkg/session.(*session).SetGlobalSysVar()
      pkg/session/session.go:1343 +0xd2
  github.com/pingcap/tidb/pkg/executor.(*SetExecutor).setSysVariable()
      pkg/executor/set.go:155 +0x74d
  github.com/pingcap/tidb/pkg/executor.(*SetExecutor).Next()
      pkg/executor/set.go:105 +0xee6
  github.com/pingcap/tidb/pkg/executor/internal/exec.Next()
      pkg/executor/internal/exec/executor.go:410 +0x418
  github.com/pingcap/tidb/pkg/executor.(*ExecStmt).next()
      pkg/executor/adapter.go:1250 +0x84
  github.com/pingcap/tidb/pkg/executor.(*ExecStmt).handleNoDelayExecutor()
      pkg/executor/adapter.go:999 +0x55b
  github.com/pingcap/tidb/pkg/executor.(*ExecStmt).handleNoDelay()
      pkg/executor/adapter.go:833 +0x3ca
  github.com/pingcap/tidb/pkg/executor.(*ExecStmt).Exec()
      pkg/executor/adapter.go:597 +0x1326
  github.com/pingcap/tidb/pkg/session.runStmt()
      pkg/session/session.go:2296 +0x5d3
  github.com/pingcap/tidb/pkg/session.(*session).ExecuteStmt()
      pkg/session/session.go:2157 +0x1ac4
  github.com/pingcap/tidb/pkg/testkit.(*TestKit).ExecWithContext()
      pkg/testkit/testkit.go:394 +0xe29
  github.com/pingcap/tidb/pkg/testkit.(*TestKit).MustExecWithContext()
      pkg/testkit/testkit.go:166 +0xab
  github.com/pingcap/tidb/pkg/testkit.(*TestKit).MustExec()
      pkg/testkit/testkit.go:161 +0x128
  github.com/pingcap/tidb/pkg/testkit.(*TestKit).RefreshSession()
      pkg/testkit/testkit.go:133 +0x1b8
  github.com/pingcap/tidb/pkg/testkit.NewTestKit()
      pkg/testkit/testkit.go:84 +0x49d
  pkg/ddl/tests/partition/partition_test.testPartitionDropIndex()
      pkg/ddl/tests/partition/db_partition_test.go:2390 +0x7d
  pkg/ddl/tests/partition/partition_test.TestPartitionDropPrimaryKeyAndDropIndex()
      pkg/ddl/tests/partition/db_partition_test.go:2380 +0x89
  github.com/pingcap/tidb/pkg/sessionctx/variable.parseSchemaCacheSize()
      pkg/sessionctx/variable/varsutil.go:607 +0x17c
  github.com/pingcap/tidb/pkg/sessionctx/variable.glob..func597()
      pkg/sessionctx/variable/sysvar.go:3153 +0x50
  github.com/pingcap/tidb/pkg/domain.(*Domain).rebuildSysVarCache()
      pkg/domain/sysvar_cache.go:146 +0x974
  github.com/pingcap/tidb/pkg/sessionctx/variable.glob..func596()
      pkg/sessionctx/variable/sysvar.go:3145 +0x58
  github.com/pingcap/tidb/pkg/sessionctx/variable.(*SysVar).ValidateWithRelaxedValidation()
      pkg/sessionctx/variable/variable.go:365 +0x23e
  github.com/pingcap/tidb/pkg/domain.(*Domain).rebuildSysVarCache()
      pkg/domain/sysvar_cache.go:145 +0x904
  github.com/pingcap/tidb/pkg/domain.(*Domain).LoadSysVarCacheLoop()
      pkg/domain/domain.go:1761 +0x93
  github.com/pingcap/tidb/pkg/session.bootstrapSessionImpl()
      pkg/session/session.go:3484 +0x864
  github.com/pingcap/tidb/pkg/sessionctx/variable.glob..func597()
      pkg/sessionctx/variable/sysvar.go:3153 +0x50
  github.com/pingcap/tidb/pkg/domain.(*Domain).rebuildSysVarCache()
      pkg/domain/sysvar_cache.go:146 +0x974
  github.com/pingcap/tidb/pkg/sessionctx/variable.glob..func596()
      pkg/sessionctx/variable/sysvar.go:3145 +0x58
  github.com/pingcap/tidb/pkg/sessionctx/variable.(*SysVar).ValidateWithRelaxedValidation()
      pkg/sessionctx/variable/variable.go:365 +0x23e
  github.com/pingcap/tidb/pkg/domain.(*Domain).rebuildSysVarCache()
      pkg/domain/sysvar_cache.go:145 +0x904
  github.com/pingcap/tidb/pkg/domain.(*Domain).rebuildSysVarCacheIfNeeded()
      pkg/domain/sysvar_cache.go:50 +0x1d5
  github.com/pingcap/tidb/pkg/domain.(*Domain).GetSessionCache()
      pkg/domain/sysvar_cache.go:61 +0x50
  github.com/pingcap/tidb/pkg/session.(*session).loadCommonGlobalVariablesIfNeeded()
      pkg/session/session.go:3856 +0x2ce
  github.com/pingcap/tidb/pkg/session.(*session).ExecuteStmt()
      pkg/session/session.go:2019 +0x184
  github.com/pingcap/tidb/pkg/session.(*session).ExecuteInternal()
      pkg/session/session.go:1530 +0x3b9
  github.com/pingcap/tidb/pkg/domain.(*Domain).LoadPrivilegeLoop()
      pkg/domain/domain.go:1704 +0x108
  github.com/pingcap/tidb/pkg/session.bootstrapSessionImpl()
      pkg/session/session.go:3477 +0x804
  github.com/pingcap/tidb/pkg/session.BootstrapSession()
      pkg/session/session.go:3376 +0xad
  github.com/pingcap/tidb/pkg/testkit.bootstrap()
      pkg/testkit/mockstore.go:254 +0x94
  github.com/pingcap/tidb/pkg/testkit.CreateMockStoreAndDomain()
      pkg/testkit/mockstore.go:225 +0xd2
  github.com/pingcap/tidb/pkg/testkit.CreateMockStore()
      pkg/testkit/mockstore.go:70 +0x3dd
  pkg/ddl/tests/partition/partition_test.TestPartitionDropPrimaryKeyAndDropIndex()
      pkg/ddl/tests/partition/db_partition_test.go:2375 +0x3c
  testing.tRunner()
      GOROOT/src/testing/testing.go:1595 +0x261
  testing.(*T).Run.func1()
      GOROOT/src/testing/testing.go:1648 +0x44
Goroutine 34941 (running) created at:
  github.com/pingcap/tidb/pkg/util.(*WaitGroupEnhancedWrapper).Run()
      pkg/util/wait_group_wrapper.go:94 +0x145
  github.com/pingcap/tidb/pkg/domain.(*Domain).Init()
      pkg/domain/domain.go:1309 +0x1ea4
  github.com/pingcap/tidb/pkg/session.(*domainMap).Get.func1()
      pkg/session/tidb.go:92 +0x559
  github.com/pingcap/tidb/pkg/util.RunWithRetry()
      pkg/util/misc.go:70 +0x84
  github.com/pingcap/tidb/pkg/session.(*domainMap).Get()
      pkg/session/tidb.go:79 +0x2eb
  github.com/pingcap/tidb/pkg/session.createSessionWithOpt()
      pkg/session/session.go:3696 +0x6c
  github.com/pingcap/tidb/pkg/session.createSession()
      pkg/session/session.go:3688 +0x35
  github.com/pingcap/tidb/pkg/session.createSessionsImpl()
      pkg/session/session.go:3673 +0x81
  github.com/pingcap/tidb/pkg/session.createSessions()
      pkg/session/session.go:3660 +0x44
  github.com/pingcap/tidb/pkg/session.bootstrapSessionImpl()
      pkg/session/session.go:3444 +0x4cd
  github.com/pingcap/tidb/pkg/session.BootstrapSession()
      pkg/session/session.go:3376 +0xad
  github.com/pingcap/tidb/pkg/testkit.bootstrap()
      pkg/testkit/mockstore.go:254 +0x94
  github.com/pingcap/tidb/pkg/testkit.CreateMockStoreAndDomain()
      pkg/testkit/mockstore.go:225 +0xd2
  github.com/pingcap/tidb/pkg/testkit.CreateMockStore()
      pkg/testkit/mockstore.go:70 +0x3dd
  pkg/ddl/tests/partition/partition_test.TestPartitionDropPrimaryKeyAndDropIndex()
      pkg/ddl/tests/partition/db_partition_test.go:2375 +0x3c
  testing.tRunner()
      GOROOT/src/testing/testing.go:1595 +0x261
  testing.(*T).Run.func1()
      GOROOT/src/testing/testing.go:1648 +0x44
Goroutine 63 (running) created at:
  testing.(*T).Run()
      GOROOT/src/testing/testing.go:1648 +0x845
  testing.runTests.func1()
      GOROOT/src/testing/testing.go:2054 +0x84
  testing.tRunner()
      GOROOT/src/testing/testing.go:1595 +0x261
  testing.runTests()
      GOROOT/src/testing/testing.go:2052 +0x8ad
  testing.(*M).Run()
      GOROOT/src/testing/testing.go:1925 +0xcd7
  go.uber.org/goleak.VerifyTestMain()
      external/org_uber_go_goleak/testmain.go:53 +0x64
  pkg/ddl/tests/partition/partition_test.TestMain()
      pkg/ddl/tests/partition/main_test.go:56 +0x54a
  main.main()
      bazel-out/k8-fastbuild/bin/pkg/ddl/tests/partition/partition_test_/testmain.go:225 +0x6dc
================== 

4. What is your TiDB version? (Required)

River2000i commented 5 months ago

let me research for the issue

River2000i commented 5 months ago

/assign

River2000i commented 4 months ago

i guest the DATA RACE occur when update TableCommon such as https://github.com/pingcap/tidb/blob/cb237b6c8b9aa07a32da8745d495e3f58a38e00e/pkg/table/tables/tables.go#L361-L363

the domain will update TableCache when drop index in this case, it will read the size of TableCommon in https://github.com/pingcap/tidb/blob/7b6294178af731384510c29afd2ff4b46b3c3426/pkg/infoschema/infoschema_v2.go#L399