Skip to content

Commit 0eb6688

Browse files
committed
fix more code
1 parent 9f475e1 commit 0eb6688

4 files changed

Lines changed: 45 additions & 151 deletions

File tree

‎maintainer/barrier.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,9 +366,9 @@ func (b *Barrier) handleEventDone(changefeedID common.ChangeFeedID, dispatcherID
366366
// which means we have sent pass or write action to it
367367
// the writer already synced ddl to downstream
368368
if event.writerDispatcher == dispatcherID {
369-
// Keep the selected event when route precheck/apply fails. Other
369+
// Keep selected events when route precheck/apply fails. Other
370370
// dispatchers may still need resend actions, and route admission is
371-
// responsible for surfacing the conflict to changefeed error handling.
371+
// responsible for reporting the conflict to changefeed error handling.
372372
ready, err := b.precheckRouteEvent(event)
373373
if err != nil || !ready {
374374
return event

‎maintainer/route_admin.go‎

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,15 @@ type routeAdmin struct {
3838
router routing.Router
3939
registry *routing.TargetTableRegistry
4040
schemaStore schemastore.SchemaStore
41-
tableSources map[int64]routeTableAdmission
41+
tableSources map[int64]admission
4242
sourceRefs map[routing.TableKey]int
4343

4444
pendingQueue pendingEventKeyHeap
4545
pendingEvents map[eventKey]*routePendingEvent
46-
// Block-only DDLs that are unrelated to table route, such as column/index changes,
47-
// still reach route admin through BlockTables so same-schema RENAME TABLE can be
48-
// detected from schema snapshots. Cache route-neutral events after the first
49-
// check so later resend/apply paths are handled as low-cost no-ops with minimal
50-
// impact on normal barrier processing.
46+
// Route admin receives every BlockTables event because same-schema RENAME
47+
// TABLE is only discoverable by comparing schema snapshots. Events that do
48+
// not change source or target names, such as column/index DDLs, are cached
49+
// after the first comparison so resend/apply paths stay cheap.
5150
routeNeutralEventCache map[eventKey]struct{}
5251

5352
reportError func(error)
@@ -78,10 +77,10 @@ type routeAdmissionInfo struct {
7877
type routeTransition struct {
7978
commitTs uint64
8079
removeTableIDs []int64
81-
adds []routeTableAdmission
80+
adds []admission
8281
}
8382

84-
type routeTableAdmission struct {
83+
type admission struct {
8584
tableID int64
8685
sourceSchemaID int64
8786
binding routing.RouteBinding
@@ -116,7 +115,7 @@ func newRouteAdmin(
116115
}
117116

118117
start := time.Now()
119-
tableSources := make(map[int64]routeTableAdmission, len(tables))
118+
tableSources := make(map[int64]admission, len(tables))
120119
sourceRefs := make(map[routing.TableKey]int, len(tables))
121120
admin := &routeAdmin{
122121
changefeedID: changefeedID,
@@ -143,7 +142,7 @@ func newRouteAdmin(
143142
}
144143
}
145144

146-
tableSources[t.TableID] = routeTableAdmission{
145+
tableSources[t.TableID] = admission{
147146
tableID: t.TableID,
148147
sourceSchemaID: t.SchemaID,
149148
binding: binding,
@@ -331,6 +330,12 @@ func (a *routeAdmin) buildTransition(info routeAdmissionInfo) (*routeTransition,
331330
if err != nil {
332331
return nil, err
333332
}
333+
if existing, ok := a.tableSources[add.TableID]; ok {
334+
if existing.equal(binding) {
335+
continue
336+
}
337+
builder.addRemove(add.TableID)
338+
}
334339
builder.addBinding(binding)
335340
}
336341

@@ -358,8 +363,7 @@ func (a *routeAdmin) buildTransition(info routeAdmissionInfo) (*routeTransition,
358363
}
359364
existing, exists := a.tableSources[tableID]
360365
if !exists {
361-
return nil, errors.ErrInternalCheckFailed.GenWithStack(
362-
"route registry binding not found for table %d", tableID)
366+
continue
363367
}
364368
binding, err := a.buildBindingForTable(tableID, existing.sourceSchemaID, info.commitTs)
365369
if err != nil {
@@ -377,26 +381,26 @@ func (a *routeAdmin) buildTransition(info routeAdmissionInfo) (*routeTransition,
377381
return builder.finish(), nil
378382
}
379383

380-
func (a *routeAdmin) buildBindingForTable(tableID, schemaID int64, commitTs uint64) (routeTableAdmission, error) {
384+
func (a *routeAdmin) buildBindingForTable(tableID, schemaID int64, commitTs uint64) (admission, error) {
381385
// New-table DDL reaches route admission before the table dispatcher is admitted,
382386
// so the schema lookup must not depend on dispatcher table registration.
383387
tableInfo, err := a.schemaStore.ForceGetTableInfo(a.keyspaceMeta, tableID, commitTs)
384388
if err != nil {
385-
return routeTableAdmission{}, err
389+
return admission{}, err
386390
}
387391
if schemaID == 0 {
388392
if existing, ok := a.tableSources[tableID]; ok {
389393
schemaID = existing.sourceSchemaID
390394
} else {
391-
return routeTableAdmission{}, errors.ErrInternalCheckFailed.GenWithStack(
395+
return admission{}, errors.ErrInternalCheckFailed.GenWithStack(
392396
"schema ID hint is missing for table %d", tableID)
393397
}
394398
}
395399
binding, err := a.route(tableInfo.GetSchemaName(), tableInfo.GetTableName())
396400
if err != nil {
397-
return routeTableAdmission{}, err
401+
return admission{}, err
398402
}
399-
return routeTableAdmission{
403+
return admission{
400404
tableID: tableID,
401405
sourceSchemaID: schemaID,
402406
binding: binding,
@@ -485,6 +489,13 @@ func (a *routeAdmin) buildAdmissionChange(
485489
return change, nextRefs, nil
486490
}
487491

492+
func (a admission) equal(b admission) bool {
493+
return a.tableID == b.tableID &&
494+
a.sourceSchemaID == b.sourceSchemaID &&
495+
a.binding.Source.Equal(b.binding.Source) &&
496+
a.binding.Target.Equal(b.binding.Target)
497+
}
498+
488499
func compareTableKey(a, b routing.TableKey) int {
489500
if a.Schema < b.Schema {
490501
return -1
@@ -522,14 +533,14 @@ func (a *routeAdmin) fail(err error) error {
522533
type routeTransitionBuilder struct {
523534
transition routeTransition
524535
removeSet map[int64]struct{}
525-
addsByID map[int64]routeTableAdmission
536+
addsByID map[int64]admission
526537
}
527538

528539
func newRouteTransitionBuilder(commitTs uint64) *routeTransitionBuilder {
529540
return &routeTransitionBuilder{
530541
transition: routeTransition{commitTs: commitTs},
531542
removeSet: make(map[int64]struct{}),
532-
addsByID: make(map[int64]routeTableAdmission),
543+
addsByID: make(map[int64]admission),
533544
}
534545
}
535546

@@ -546,7 +557,7 @@ func (b *routeTransitionBuilder) hasRemove(tableID int64) bool {
546557
return ok
547558
}
548559

549-
func (b *routeTransitionBuilder) addBinding(binding routeTableAdmission) {
560+
func (b *routeTransitionBuilder) addBinding(binding admission) {
550561
b.addsByID[binding.tableID] = binding
551562
}
552563

‎maintainer/route_admin_test.go‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,10 @@ func TestRouteAdminTracksSourceAdmissionNotTableID(t *testing.T) {
257257
truncateInfo := routeAdmissionInfo{
258258
key: getEventKey(30, false),
259259
commitTs: 30,
260+
blockTables: &heartbeatpb.InfluencedTables{
261+
InfluenceType: heartbeatpb.InfluenceType_Normal,
262+
TableIDs: []int64{3, 4},
263+
},
260264
droppedTables: &heartbeatpb.InfluencedTables{
261265
InfluenceType: heartbeatpb.InfluenceType_Normal,
262266
TableIDs: []int64{3},
@@ -275,6 +279,14 @@ func TestRouteAdminTracksSourceAdmissionNotTableID(t *testing.T) {
275279
_, ok = admin.tableSources[4]
276280
require.True(t, ok)
277281

282+
// Replayed partition DDL can still mention the old physical partition ID in
283+
// BlockTables after its admission state has been replaced.
284+
ready, err = admin.precheck(truncateInfo)
285+
require.NoError(t, err)
286+
require.True(t, ready)
287+
require.NoError(t, admin.apply(truncateInfo))
288+
require.Equal(t, 1, admin.sourceRefs[source])
289+
278290
conflictInfo := routeAdmissionInfo{
279291
key: getEventKey(40, false),
280292
commitTs: 40,

‎tests/integration_tests/table_route_conflict_detection/run.sh‎

Lines changed: 0 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -91,29 +91,6 @@ function run_create_table_conflict_case() {
9191
echo "[$(date)] finish create table conflict case"
9292
}
9393

94-
function run_rename_table_conflict_case() {
95-
local source_a=route_conflict_rename_a
96-
local source_b=route_conflict_rename_b
97-
local target_schema=target_route_conflict_rename
98-
local changefeed_id=route-conflict-rename
99-
local config_file="$WORK_DIR/$changefeed_id.toml"
100-
local start_ts
101-
102-
echo "[$(date)] start rename table conflict case"
103-
write_route_config "$config_file" "$source_a" "$source_b" "$target_schema"
104-
start_ts=$(run_cdc_cli_tso_query "$UP_PD_HOST_1" "$UP_PD_PORT_1")
105-
run_sql "CREATE DATABASE $source_a; CREATE TABLE $source_a.t (id INT PRIMARY KEY);" "$UP_TIDB_HOST" "$UP_TIDB_PORT"
106-
run_sql "CREATE DATABASE $source_b; CREATE TABLE $source_b.tmp (id INT PRIMARY KEY);" "$UP_TIDB_HOST" "$UP_TIDB_PORT"
107-
cdc_cli_changefeed create -c "$changefeed_id" --start-ts="$start_ts" --sink-uri="$SINK_URI" --config="$config_file"
108-
check_table_exists "$target_schema.t_routed" "$DOWN_TIDB_HOST" "$DOWN_TIDB_PORT" 60
109-
check_table_exists "$target_schema.tmp_routed" "$DOWN_TIDB_HOST" "$DOWN_TIDB_PORT" 60
110-
111-
run_sql "RENAME TABLE $source_b.tmp TO $source_b.t;" "$UP_TIDB_HOST" "$UP_TIDB_PORT"
112-
ensure $MAX_RETRIES check_changefeed_state http://${UP_PD_HOST_1}:${UP_PD_PORT_1} "$changefeed_id" failed conflict ""
113-
cdc_cli_changefeed remove -c "$changefeed_id" || true
114-
echo "[$(date)] finish rename table conflict case"
115-
}
116-
11794
function run_multi_rename_table_conflict_case() {
11895
local source_a=route_conflict_multi_rename_a
11996
local source_b=route_conflict_multi_rename_b
@@ -138,107 +115,6 @@ function run_multi_rename_table_conflict_case() {
138115
echo "[$(date)] finish multi-rename table conflict case"
139116
}
140117

141-
function run_drop_table_release_case() {
142-
local source_a=route_conflict_drop_table_a
143-
local source_b=route_conflict_drop_table_b
144-
local target_schema=target_route_conflict_drop_table
145-
local changefeed_id=route-conflict-drop-table
146-
local config_file="$WORK_DIR/$changefeed_id.toml"
147-
local start_ts
148-
149-
echo "[$(date)] start drop table release case"
150-
write_route_config "$config_file" "$source_a" "$source_b" "$target_schema"
151-
start_ts=$(run_cdc_cli_tso_query "$UP_PD_HOST_1" "$UP_PD_PORT_1")
152-
run_sql "CREATE DATABASE $source_a; CREATE TABLE $source_a.t (id INT PRIMARY KEY);" "$UP_TIDB_HOST" "$UP_TIDB_PORT"
153-
cdc_cli_changefeed create -c "$changefeed_id" --start-ts="$start_ts" --sink-uri="$SINK_URI" --config="$config_file"
154-
check_table_exists "$target_schema.t_routed" "$DOWN_TIDB_HOST" "$DOWN_TIDB_PORT" 60
155-
156-
run_sql "DROP TABLE $source_a.t;" "$UP_TIDB_HOST" "$UP_TIDB_PORT"
157-
check_table_not_exists "$target_schema.t_routed" "$DOWN_TIDB_HOST" "$DOWN_TIDB_PORT" 60
158-
run_sql "CREATE DATABASE $source_b; CREATE TABLE $source_b.t (id INT PRIMARY KEY);" "$UP_TIDB_HOST" "$UP_TIDB_PORT"
159-
check_table_exists "$target_schema.t_routed" "$DOWN_TIDB_HOST" "$DOWN_TIDB_PORT" 60
160-
ensure $MAX_RETRIES check_changefeed_state http://${UP_PD_HOST_1}:${UP_PD_PORT_1} "$changefeed_id" normal null ""
161-
cdc_cli_changefeed remove -c "$changefeed_id" || true
162-
echo "[$(date)] finish drop table release case"
163-
}
164-
165-
function run_drop_database_release_case() {
166-
local source_a=route_conflict_drop_database_a
167-
local source_b=route_conflict_drop_database_b
168-
local target_schema=target_route_conflict_drop_database
169-
local changefeed_id=route-conflict-drop-database
170-
local config_file="$WORK_DIR/$changefeed_id.toml"
171-
local start_ts
172-
173-
echo "[$(date)] start drop database release case"
174-
write_route_config "$config_file" "$source_a" "$source_b" "$target_schema"
175-
start_ts=$(run_cdc_cli_tso_query "$UP_PD_HOST_1" "$UP_PD_PORT_1")
176-
run_sql "CREATE DATABASE $source_a; CREATE TABLE $source_a.t (id INT PRIMARY KEY);" "$UP_TIDB_HOST" "$UP_TIDB_PORT"
177-
cdc_cli_changefeed create -c "$changefeed_id" --start-ts="$start_ts" --sink-uri="$SINK_URI" --config="$config_file"
178-
check_table_exists "$target_schema.t_routed" "$DOWN_TIDB_HOST" "$DOWN_TIDB_PORT" 60
179-
180-
run_sql "DROP DATABASE $source_a;" "$UP_TIDB_HOST" "$UP_TIDB_PORT"
181-
check_table_not_exists "$target_schema.t_routed" "$DOWN_TIDB_HOST" "$DOWN_TIDB_PORT" 60
182-
run_sql "CREATE DATABASE $source_b; CREATE TABLE $source_b.t (id INT PRIMARY KEY);" "$UP_TIDB_HOST" "$UP_TIDB_PORT"
183-
check_table_exists "$target_schema.t_routed" "$DOWN_TIDB_HOST" "$DOWN_TIDB_PORT" 60
184-
ensure $MAX_RETRIES check_changefeed_state http://${UP_PD_HOST_1}:${UP_PD_PORT_1} "$changefeed_id" normal null ""
185-
cdc_cli_changefeed remove -c "$changefeed_id" || true
186-
echo "[$(date)] finish drop database release case"
187-
}
188-
189-
function run_rename_table_out_of_filter_release_case() {
190-
local source_a=route_conflict_rename_out_a
191-
local source_b=route_conflict_rename_out_b
192-
local ignored_schema=route_conflict_rename_out_ignored
193-
local target_schema=target_route_conflict_rename_out
194-
local changefeed_id=route-conflict-rename-out
195-
local config_file="$WORK_DIR/$changefeed_id.toml"
196-
local start_ts
197-
198-
echo "[$(date)] start rename table out of filter release case"
199-
write_route_config "$config_file" "$source_a" "$source_b" "$target_schema"
200-
start_ts=$(run_cdc_cli_tso_query "$UP_PD_HOST_1" "$UP_PD_PORT_1")
201-
run_sql "CREATE DATABASE $source_a; CREATE TABLE $source_a.t (id INT PRIMARY KEY); CREATE DATABASE $ignored_schema;" "$UP_TIDB_HOST" "$UP_TIDB_PORT"
202-
# The old table is still in the replication range, so TiCDC writes the RENAME
203-
# to downstream even though the new schema is outside the filter. Prepare the
204-
# downstream schema explicitly and keep this case focused on route admission
205-
# release instead of downstream schema existence.
206-
run_sql "CREATE DATABASE $ignored_schema;" "$DOWN_TIDB_HOST" "$DOWN_TIDB_PORT"
207-
cdc_cli_changefeed create -c "$changefeed_id" --start-ts="$start_ts" --sink-uri="$SINK_URI" --config="$config_file"
208-
check_table_exists "$target_schema.t_routed" "$DOWN_TIDB_HOST" "$DOWN_TIDB_PORT" 60
209-
210-
run_sql "RENAME TABLE $source_a.t TO $ignored_schema.t;" "$UP_TIDB_HOST" "$UP_TIDB_PORT"
211-
check_table_not_exists "$target_schema.t_routed" "$DOWN_TIDB_HOST" "$DOWN_TIDB_PORT" 60
212-
run_sql "CREATE DATABASE $source_b; CREATE TABLE $source_b.t (id INT PRIMARY KEY);" "$UP_TIDB_HOST" "$UP_TIDB_PORT"
213-
check_table_exists "$target_schema.t_routed" "$DOWN_TIDB_HOST" "$DOWN_TIDB_PORT" 60
214-
ensure $MAX_RETRIES check_changefeed_state http://${UP_PD_HOST_1}:${UP_PD_PORT_1} "$changefeed_id" normal null ""
215-
cdc_cli_changefeed remove -c "$changefeed_id" || true
216-
echo "[$(date)] finish rename table out of filter release case"
217-
}
218-
219-
function run_truncate_table_keeps_source_name_case() {
220-
local source_a=route_conflict_truncate_a
221-
local source_b=route_conflict_truncate_b
222-
local target_schema=target_route_conflict_truncate
223-
local changefeed_id=route-conflict-truncate
224-
local config_file="$WORK_DIR/$changefeed_id.toml"
225-
local start_ts
226-
227-
echo "[$(date)] start truncate table keeps source name case"
228-
write_route_config "$config_file" "$source_a" "$source_b" "$target_schema"
229-
start_ts=$(run_cdc_cli_tso_query "$UP_PD_HOST_1" "$UP_PD_PORT_1")
230-
run_sql "CREATE DATABASE $source_a; CREATE TABLE $source_a.t (id INT PRIMARY KEY);" "$UP_TIDB_HOST" "$UP_TIDB_PORT"
231-
cdc_cli_changefeed create -c "$changefeed_id" --start-ts="$start_ts" --sink-uri="$SINK_URI" --config="$config_file"
232-
check_table_exists "$target_schema.t_routed" "$DOWN_TIDB_HOST" "$DOWN_TIDB_PORT" 60
233-
234-
run_sql "TRUNCATE TABLE $source_a.t;" "$UP_TIDB_HOST" "$UP_TIDB_PORT"
235-
check_table_exists "$target_schema.t_routed" "$DOWN_TIDB_HOST" "$DOWN_TIDB_PORT" 60
236-
run_sql "CREATE DATABASE $source_b; CREATE TABLE $source_b.t (id INT PRIMARY KEY);" "$UP_TIDB_HOST" "$UP_TIDB_PORT"
237-
ensure $MAX_RETRIES check_changefeed_state http://${UP_PD_HOST_1}:${UP_PD_PORT_1} "$changefeed_id" failed conflict ""
238-
cdc_cli_changefeed remove -c "$changefeed_id" || true
239-
echo "[$(date)] finish truncate table keeps source name case"
240-
}
241-
242118
function run() {
243119
if [ "$SINK_TYPE" != "mysql" ]; then
244120
echo "table_route_conflict_detection only supports mysql sink"
@@ -252,12 +128,7 @@ function run() {
252128

253129
run_static_conflict_case
254130
run_create_table_conflict_case
255-
run_rename_table_conflict_case
256131
run_multi_rename_table_conflict_case
257-
run_drop_table_release_case
258-
run_drop_database_release_case
259-
run_rename_table_out_of_filter_release_case
260-
run_truncate_table_keeps_source_name_case
261132

262133
cleanup_process "$CDC_BINARY"
263134
}

0 commit comments

Comments
 (0)