1414package common
1515
1616import (
17+ "maps"
1718 "slices"
1819 "sort"
1920 "sync"
2021 "sync/atomic"
2122
22- "github.com/pingcap/log"
2323 "github.com/pingcap/ticdc/pkg/common"
2424 commonEvent "github.com/pingcap/ticdc/pkg/common/event"
2525 timodel "github.com/pingcap/tidb/pkg/meta/model"
26- "go.uber.org/zap"
2726)
2827
2928// TableInfoProvider keeps the TableInfo of every table version. Decoders may
@@ -36,7 +35,6 @@ type TableInfoProvider struct {
3635 invalidations []uint64
3736 releasedWatermark atomic.Uint64
3837
39- idMu sync.RWMutex
4038 tableIDs map [accessKey ]int64
4139 currentTableID int64
4240 blockedTableIDs map [accessKey ]map [int64 ]struct {}
@@ -56,38 +54,33 @@ func (c *TableInfoProvider) ReleasedWatermark() uint64 {
5654 return c .releasedWatermark .Load ()
5755}
5856
59- // WriteTableInfo stores the table info under its own version.
60- func (c * TableInfoProvider ) WriteTableInfo (info * common.TableInfo ) {
57+ // WriteTableInfo stores the table info under its own version. Retention is
58+ // ordered by effectiveTs, because a DDL entry is read by its update ts but may
59+ // be needed until the job finishes; a zero effectiveTs uses the update ts.
60+ func (c * TableInfoProvider ) WriteTableInfo (info * common.TableInfo , effectiveTs uint64 ) {
6161 if info == nil {
6262 return
6363 }
64- c .WriteTableInfoAt (info , info .GetUpdateTS ())
65- }
66-
67- // WriteTableInfoAt stores the table info under its own version while ordering
68- // version retention by effectiveTs.
69- func (c * TableInfoProvider ) WriteTableInfoAt (info * common.TableInfo , effectiveTs uint64 ) {
70- if info == nil {
64+ schema , table := info .TableName .Schema , info .TableName .Table
65+ if schema == "" || table == "" {
7166 return
7267 }
7368 if effectiveTs == 0 {
7469 effectiveTs = info .GetUpdateTS ()
7570 }
76- c .writeVersion (info .TableName .Schema , info .TableName .Table , info .GetUpdateTS (), effectiveTs , info )
71+ c .mu .Lock ()
72+ defer c .mu .Unlock ()
73+ c .table (schema , table ).set (info .GetUpdateTS (), effectiveTs , info )
7774}
7875
7976// Write stores the table info under the version the message belongs to.
8077func (c * TableInfoProvider ) Write (schema , table string , version uint64 , info * common.TableInfo ) {
81- c .writeVersion (schema , table , version , version , info )
82- }
83-
84- func (c * TableInfoProvider ) writeVersion (schema , table string , version , effectiveTs uint64 , info * common.TableInfo ) {
8578 if schema == "" || table == "" || info == nil {
8679 return
8780 }
8881 c .mu .Lock ()
8982 defer c .mu .Unlock ()
90- c .table (schema , table ).set (version , effectiveTs , info )
83+ c .table (schema , table ).set (version , version , info )
9184}
9285
9386// Read returns the table info of exactly the requested version.
@@ -204,8 +197,8 @@ func (c *TableInfoProvider) table(schema, table string) *tableVersions {
204197
205198// AllocateTableID allocates a physical table ID scoped to this provider.
206199func (c * TableInfoProvider ) AllocateTableID (schema , table string ) int64 {
207- c .idMu .Lock ()
208- defer c .idMu .Unlock ()
200+ c .mu .Lock ()
201+ defer c .mu .Unlock ()
209202
210203 key := accessKey {schema , table }
211204 if tableID , ok := c .tableIDs [key ]; ok {
@@ -218,32 +211,22 @@ func (c *TableInfoProvider) AllocateTableID(schema, table string) int64 {
218211
219212// AddBlockTableID registers a physical table ID as blocked by DDLs on the table.
220213func (c * TableInfoProvider ) AddBlockTableID (schema , table string , physicalTableID int64 ) {
221- c .idMu .Lock ()
222- defer c .idMu .Unlock ()
214+ c .mu .Lock ()
215+ defer c .mu .Unlock ()
223216
224217 key := accessKey {schema , table }
225218 if _ , ok := c .blockedTableIDs [key ]; ! ok {
226219 c .blockedTableIDs [key ] = make (map [int64 ]struct {})
227220 }
228-
229- if _ , exists := c .blockedTableIDs [key ][physicalTableID ]; ! exists {
230- c .blockedTableIDs [key ][physicalTableID ] = struct {}{}
231- log .Info ("add table id to blocked list" ,
232- zap .String ("schema" , schema ), zap .String ("table" , table ), zap .Int64 ("tableID" , physicalTableID ))
233- }
221+ c .blockedTableIDs [key ][physicalTableID ] = struct {}{}
234222}
235223
236224// GetBlockedTables returns the physical table IDs blocked by DDLs on the table.
237225func (c * TableInfoProvider ) GetBlockedTables (schema , table string ) []int64 {
238- c .idMu .RLock ()
239- defer c .idMu .RUnlock ()
226+ c .mu .RLock ()
227+ defer c .mu .RUnlock ()
240228
241- blocked := c .blockedTableIDs [accessKey {schema , table }]
242- result := make ([]int64 , 0 , len (blocked ))
243- for item := range blocked {
244- result = append (result , item )
245- }
246- return result
229+ return slices .Collect (maps .Keys (c .blockedTableIDs [accessKey {schema , table }]))
247230}
248231
249232// tableVersions keeps the versions of one table in ascending order. A table has
0 commit comments