Skip to content

Commit 6bf5194

Browse files
committed
simplify code
1 parent 247945f commit 6bf5194

7 files changed

Lines changed: 58 additions & 78 deletions

File tree

‎pkg/sink/codec/canal/canal_json_decoder.go‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,15 @@ type decoder struct {
8989
cache *common.TableInfoProvider
9090
}
9191

92-
// NewDecoder returns a canal-json decoder, reusing externalStorage when provided.
92+
// NewDecoder returns a canal-json decoder.
9393
func NewDecoder(
94-
ctx context.Context, codecConfig *common.Config, db *sql.DB, externalStorage ...storeapi.Storage,
94+
ctx context.Context, codecConfig *common.Config, db *sql.DB,
9595
) (common.EventDecoder, error) {
96-
var storage storeapi.Storage
97-
if len(externalStorage) > 0 {
98-
storage = externalStorage[0]
99-
}
100-
var err error
101-
if storage == nil && codecConfig.LargeMessageHandle.EnableClaimCheck() {
96+
var (
97+
storage storeapi.Storage
98+
err error
99+
)
100+
if codecConfig.LargeMessageHandle.EnableClaimCheck() {
102101
storageURI := codecConfig.LargeMessageHandle.ClaimCheckStorageURI
103102
storage, err = util.GetExternalStorageWithDefaultTimeout(ctx, storageURI)
104103
if err != nil {

‎pkg/sink/codec/canal/canal_json_test.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,8 +1460,8 @@ func TestDDLInvalidatesCache(t *testing.T) {
14601460

14611461
t1 := commonType.WrapTableInfo("test", &model.TableInfo{ID: 1, Name: ast.NewCIStr("t1"), UpdateTS: 100})
14621462
t2 := commonType.WrapTableInfo("other", &model.TableInfo{ID: 2, Name: ast.NewCIStr("t2"), UpdateTS: 100})
1463-
provider.WriteTableInfo(t1)
1464-
provider.WriteTableInfo(t2)
1463+
provider.WriteTableInfo(t1, 0)
1464+
provider.WriteTableInfo(t2, 0)
14651465

14661466
msg, err := encoder.EncodeDDLEvent(&commonEvent.DDLEvent{
14671467
FinishedTs: 120,

‎pkg/sink/codec/common/ddl.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func GetBlockedTables(
170170
}
171171
switch s := stmt.(type) {
172172
case *ast.RenameTableStmt:
173-
if action == timodel.ActionRenameTable && len(s.TableToTables) > 0 && s.TableToTables[0].OldTable != nil {
173+
if action == timodel.ActionRenameTable {
174174
oldTable := s.TableToTables[0].OldTable
175175
ddl.ExtraSchemaName = cmp.Or(oldTable.Schema.O, ddl.SchemaName)
176176
ddl.ExtraTableName = oldTable.Name.O
@@ -180,7 +180,7 @@ func GetBlockedTables(
180180
blockedTableIDs = appendBlockedTableIDs(accessor, blockedTableIDs, ddl.SchemaName, pair.NewTable)
181181
}
182182
case *ast.AlterTableStmt:
183-
if action == timodel.ActionRenameTable && s.Table != nil {
183+
if action == timodel.ActionRenameTable {
184184
ddl.ExtraSchemaName = cmp.Or(s.Table.Schema.O, ddl.SchemaName)
185185
ddl.ExtraTableName = s.Table.Name.O
186186
}

‎pkg/sink/codec/common/table_info_provider.go‎

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,15 @@
1414
package common
1515

1616
import (
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.
8077
func (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.
206199
func (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.
220213
func (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.
237225
func (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

‎pkg/sink/codec/common/table_info_provider_test.go‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ func TestRecordDDL(t *testing.T) {
2929
t1 := commonType.WrapTableInfo("test", &timodel.TableInfo{ID: 1, Name: ast.NewCIStr("t1"), UpdateTS: 100})
3030
t2 := commonType.WrapTableInfo("test", &timodel.TableInfo{ID: 2, Name: ast.NewCIStr("t2"), UpdateTS: 100})
3131
t3 := commonType.WrapTableInfo("other", &timodel.TableInfo{ID: 3, Name: ast.NewCIStr("t3"), UpdateTS: 100})
32-
provider.WriteTableInfo(t1)
33-
provider.WriteTableInfo(t2)
34-
provider.WriteTableInfo(t3)
32+
provider.WriteTableInfo(t1, 0)
33+
provider.WriteTableInfo(t2, 0)
34+
provider.WriteTableInfo(t3, 0)
3535

3636
// Normal DDL invalidates only its target table.
3737
provider.RecordDDL(&commonEvent.DDLEvent{
@@ -54,9 +54,9 @@ func TestRecordDDL(t *testing.T) {
5454
} {
5555
t.Run(action.String(), func(t *testing.T) {
5656
p := NewTableInfoProvider()
57-
p.WriteTableInfo(t1)
58-
p.WriteTableInfo(t2)
59-
p.WriteTableInfo(t3)
57+
p.WriteTableInfo(t1, 0)
58+
p.WriteTableInfo(t2, 0)
59+
p.WriteTableInfo(t3, 0)
6060

6161
p.RecordDDL(&commonEvent.DDLEvent{
6262
FinishedTs: 120,
@@ -95,9 +95,9 @@ func TestReleaseBefore(t *testing.T) {
9595
// A resumed decoder can report a lower version than one already seen.
9696
newT1 := commonType.WrapTableInfo("test", &timodel.TableInfo{ID: 3, Name: ast.NewCIStr("t1"), UpdateTS: 50})
9797

98-
provider.WriteTableInfoAt(oldestT1, 20)
99-
provider.WriteTableInfoAt(oldT1, 100)
100-
provider.WriteTableInfoAt(newT1, 300)
98+
provider.WriteTableInfo(oldestT1, 20)
99+
provider.WriteTableInfo(oldT1, 100)
100+
provider.WriteTableInfo(newT1, 300)
101101

102102
// Retention follows effectiveTs while ReadAt resolves by version, so both
103103
// floors stay: newT1 survives a watermark above its version, and ReadAt at a

‎pkg/sink/codec/open/decoder.go‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,15 @@ type decoder struct {
5656
upstreamTiDB *sql.DB
5757
}
5858

59-
// NewDecoder creates a new decoder, reusing externalStorage when provided.
59+
// NewDecoder creates a new decoder.
6060
func NewDecoder(
61-
ctx context.Context, config *common.Config, db *sql.DB, externalStorage ...storeapi.Storage,
61+
ctx context.Context, config *common.Config, db *sql.DB,
6262
) (common.EventDecoder, error) {
63-
var storage storeapi.Storage
64-
if len(externalStorage) > 0 {
65-
storage = externalStorage[0]
66-
}
67-
var err error
68-
if storage == nil && config.LargeMessageHandle.EnableClaimCheck() {
63+
var (
64+
storage storeapi.Storage
65+
err error
66+
)
67+
if config.LargeMessageHandle.EnableClaimCheck() {
6968
storageURI := config.LargeMessageHandle.ClaimCheckStorageURI
7069
storage, err = util.GetExternalStorageWithDefaultTimeout(ctx, storageURI)
7170
if err != nil {

‎pkg/sink/codec/simple/decoder.go‎

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,15 @@ type Decoder struct {
5959
CachedDMLMessages []*common.DMLMessage
6060
}
6161

62-
// NewDecoder returns a simple decoder, reusing externalStorage when provided.
62+
// NewDecoder returns a simple decoder.
6363
func NewDecoder(
64-
ctx context.Context, config *common.Config, db *sql.DB, externalStorage ...storeapi.Storage,
64+
ctx context.Context, config *common.Config, db *sql.DB,
6565
) (common.EventDecoder, error) {
66-
var storage storeapi.Storage
67-
if len(externalStorage) > 0 {
68-
storage = externalStorage[0]
69-
}
70-
var err error
71-
if storage == nil && config.LargeMessageHandle.EnableClaimCheck() {
66+
var (
67+
storage storeapi.Storage
68+
err error
69+
)
70+
if config.LargeMessageHandle.EnableClaimCheck() {
7271
storageURI := config.LargeMessageHandle.ClaimCheckStorageURI
7372
storage, err = util.GetExternalStorageWithDefaultTimeout(ctx, storageURI)
7473
if err != nil {
@@ -313,12 +312,12 @@ func (d *Decoder) NextDDLEvent() *commonEvent.DDLEvent {
313312
ddl.BlockedTables = common.GetBlockedTables(d.memo, ddl)
314313
}
315314

316-
if !ddl.IsBootstrap && ddl.FinishedTs != 0 {
317-
d.memo.WriteTableInfoAt(ddl.TableInfo, ddl.FinishedTs)
318-
} else {
319-
d.memo.WriteTableInfo(ddl.TableInfo)
315+
var effectiveTs uint64
316+
if !ddl.IsBootstrap {
317+
effectiveTs = ddl.FinishedTs
320318
}
321-
d.memo.WriteTableInfo(ddl.MultipleTableInfos[1])
319+
d.memo.WriteTableInfo(ddl.TableInfo, effectiveTs)
320+
d.memo.WriteTableInfo(ddl.MultipleTableInfos[1], 0)
322321

323322
for ele := d.cachedMessages.Front(); ele != nil; {
324323
msg := ele.Value.(*message)

0 commit comments

Comments
 (0)