Skip to content

Commit c35e477

Browse files
committed
refactor the code once more
1 parent 249b944 commit c35e477

5 files changed

Lines changed: 126 additions & 62 deletions

File tree

‎downstreamadapter/routing/registry.go‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,9 @@ func (r *TargetTableRegistry) Remove(source TableKey) {
9494
// removed before the new binding can claim its target.
9595
//
9696
// This method validates the whole transition before mutating the registry. If any
97-
// check fails, both indexes remain unchanged.
98-
func (r *TargetTableRegistry) ApplyTransition(removes []TableKey, adds []RouteBinding) error {
97+
// check fails, both indexes remain unchanged. If mutate is false, this method
98+
// only validates the transition and leaves both indexes unchanged.
99+
func (r *TargetTableRegistry) ApplyTransition(removes []TableKey, adds []RouteBinding, mutate bool) error {
99100
removeSet := make(map[TableKey]struct{}, len(removes))
100101
for _, source := range removes {
101102
removeSet[source] = struct{}{}
@@ -169,6 +170,10 @@ func (r *TargetTableRegistry) ApplyTransition(removes []TableKey, adds []RouteBi
169170
addedTargets[add.Target] = add.Source
170171
}
171172

173+
if !mutate {
174+
return nil
175+
}
176+
172177
// All validation above is side-effect free. Only after the complete transition
173178
// is known to be valid do we update both indexes.
174179
for _, source := range removes {

‎downstreamadapter/routing/registry_test.go‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ func TestTargetTableRegistryApplyTransition(t *testing.T) {
100100
require.NoError(t, r.ApplyTransition(
101101
[]TableKey{{Schema: "db1", Table: "t1"}},
102102
[]RouteBinding{newRouteBinding("db1", "t1_new", "archive", "orders")},
103+
true,
103104
))
104105

105106
require.Len(t, r.target2Source, 2)
@@ -117,6 +118,7 @@ func TestTargetTableRegistryApplyTransition(t *testing.T) {
117118
err := r.ApplyTransition(
118119
[]TableKey{{Schema: "db1", Table: "t1"}},
119120
[]RouteBinding{newRouteBinding("db3", "t3", "archive", "customers")},
121+
true,
120122
)
121123
require.Error(t, err)
122124
require.True(t, errors.ErrTableRouteConflict.Equal(err))
@@ -132,7 +134,7 @@ func TestTargetTableRegistryApplyTransition(t *testing.T) {
132134
err := r.ApplyTransition(nil, []RouteBinding{
133135
newRouteBinding("db1", "t1", "archive", "orders"),
134136
newRouteBinding("db2", "t2", "archive", "orders"),
135-
})
137+
}, true)
136138
require.Error(t, err)
137139
require.True(t, errors.ErrTableRouteConflict.Equal(err))
138140
require.Empty(t, r.target2Source)
@@ -145,11 +147,31 @@ func TestTargetTableRegistryApplyTransition(t *testing.T) {
145147
err := r.ApplyTransition(nil, []RouteBinding{
146148
newRouteBinding("db1", "t1", "archive", "orders"),
147149
newRouteBinding("db1", "t1", "archive", "orders_new"),
148-
})
150+
}, true)
149151
require.Error(t, err)
150152
require.True(t, errors.ErrInternalCheckFailed.Equal(err))
151153
require.Empty(t, r.target2Source)
152154
})
155+
156+
t.Run("check-only mode does not mutate", func(t *testing.T) {
157+
t.Parallel()
158+
r := NewTargetTableRegistry(changefeedID, 0)
159+
require.NoError(t, r.Add(newRouteBinding("db1", "t1", "archive", "orders")))
160+
161+
err := r.ApplyTransition(nil, []RouteBinding{
162+
newRouteBinding("db2", "t2", "archive", "customers"),
163+
}, false)
164+
require.NoError(t, err)
165+
require.Len(t, r.target2Source, 1)
166+
167+
require.NoError(t, r.Add(newRouteBinding("db2", "t2", "archive", "customers")))
168+
err = r.ApplyTransition(nil, []RouteBinding{
169+
newRouteBinding("db3", "t3", "archive", "customers"),
170+
}, false)
171+
require.Error(t, err)
172+
require.True(t, errors.ErrTableRouteConflict.Equal(err))
173+
require.Len(t, r.target2Source, 2)
174+
})
153175
}
154176

155177
func TestValidateNoStaticRouteConflict(t *testing.T) {

‎maintainer/maintainer_controller_bootstrap.go‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,16 +135,18 @@ func (c *Controller) FinishBootstrap(
135135
// Step 4: Handle any remaining working tasks (likely dropped tables)
136136
c.handleRemainingWorkingTasks(workingTaskMap, redoWorkingTaskMap)
137137

138-
// Step 5: Initialize and start sub components
139-
c.initializeComponents(allNodesResp)
140-
141-
// Step 6: Initialize route router and target registry
138+
// Step 5: Initialize route router and target registry before barrier starts
139+
// handling bootstrap block states. The barrier captures the detector pointer
140+
// at construction time.
142141
detector, err := newRouteConflictDetector(c.changefeedID, c.keyspaceMeta, c.replicaConfig, c.reportError, tables)
143142
if err != nil {
144143
return nil, err
145144
}
146145
c.routeDetector = detector
147146

147+
// Step 6: Initialize and start sub components
148+
c.initializeComponents(allNodesResp)
149+
148150
// Step 7: Mark the controller as bootstrapped
149151
c.bootstrapped = true
150152

‎maintainer/route_conflict_detector.go‎

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,11 @@ func newRouteConflictDetector(
107107
return nil, err
108108
}
109109

110-
// binding, err := d.buildBindingForTable(t.TableID, t.SchemaID, startTs)
111-
// if err != nil {
112-
// return err
113-
// }
114-
// currentTables[t.TableID] = binding
110+
currentTables[t.TableID] = routeTableBinding{
111+
tableID: t.TableID,
112+
sourceSchemaID: t.SchemaID,
113+
binding: binding,
114+
}
115115
}
116116

117117
// todo: this log may can be removed after test huge number of tables.
@@ -153,6 +153,10 @@ func (d *routeConflictDetector) precheck(info routeDDLInfo) (bool, error) {
153153
if pending.prechecked {
154154
return true, nil
155155
}
156+
adds := routeTransitionAdds(pending.transition)
157+
if err := d.registry.ApplyTransition(pending.transition.removes, adds, false); err != nil {
158+
return false, d.fail(err)
159+
}
156160
pending.prechecked = true
157161
log.Info("route transition prechecked",
158162
zap.String("changefeed", d.changefeedID.Name()),
@@ -178,11 +182,8 @@ func (d *routeConflictDetector) apply(info routeDDLInfo) error {
178182
"route transition apply out of order, changefeed=%s, commitTs=%d",
179183
d.changefeedID.Name(), info.commitTs))
180184
}
181-
adds := make([]routing.RouteBinding, 0, len(pending.transition.adds))
182-
for _, add := range pending.transition.adds {
183-
adds = append(adds, add.binding)
184-
}
185-
if err := d.registry.ApplyTransition(pending.transition.removes, adds); err != nil {
185+
adds := routeTransitionAdds(pending.transition)
186+
if err := d.registry.ApplyTransition(pending.transition.removes, adds, true); err != nil {
186187
return d.fail(err)
187188
}
188189
for _, tableID := range pending.transition.removeTableIDs {
@@ -200,6 +201,14 @@ func (d *routeConflictDetector) apply(info routeDDLInfo) error {
200201
return nil
201202
}
202203

204+
func routeTransitionAdds(transition *routeTransition) []routing.RouteBinding {
205+
adds := make([]routing.RouteBinding, 0, len(transition.adds))
206+
for _, add := range transition.adds {
207+
adds = append(adds, add.binding)
208+
}
209+
return adds
210+
}
211+
203212
func (d *routeConflictDetector) needsCheck(info routeDDLInfo) bool {
204213
if d == nil || d.registry == nil || info.isSyncPoint {
205214
return false

‎maintainer/route_conflict_detector_test.go‎

Lines changed: 70 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ import (
2222
"github.com/pingcap/ticdc/heartbeatpb"
2323
"github.com/pingcap/ticdc/logservice/schemastore"
2424
"github.com/pingcap/ticdc/pkg/common"
25+
appcontext "github.com/pingcap/ticdc/pkg/common/context"
2526
commonEvent "github.com/pingcap/ticdc/pkg/common/event"
2627
"github.com/pingcap/ticdc/pkg/config"
2728
"github.com/pingcap/ticdc/pkg/filter"
2829
"github.com/stretchr/testify/require"
2930
)
3031

31-
func TestRouteConflictDetectorApplyReportsConflict(t *testing.T) {
32+
func TestRouteConflictDetectorPrecheckReportsConflict(t *testing.T) {
3233
detector := newTestRouteConflictDetector(t, "target", routing.TablePlaceholder)
3334

3435
var reportedErr error
@@ -44,16 +45,16 @@ func TestRouteConflictDetectorApplyReportsConflict(t *testing.T) {
4445
},
4546
}
4647
ready, err := detector.precheck(info)
47-
require.True(t, ready)
48-
require.NoError(t, err)
49-
50-
err = detector.apply(info)
5148
require.Error(t, err)
49+
require.False(t, ready)
5250
require.Same(t, err, reportedErr)
5351
require.Contains(t, err.Error(), "table route conflict")
5452
require.Contains(t, err.Error(), "source `db1`.`t`")
5553
require.Contains(t, err.Error(), "source `db2`.`t`")
5654
require.Contains(t, err.Error(), "target `target`.`t`")
55+
56+
_, ok := detector.tables[2]
57+
require.False(t, ok)
5758
}
5859

5960
func TestRouteConflictDetectorApplyUpdatesRegistry(t *testing.T) {
@@ -69,64 +70,89 @@ func TestRouteConflictDetectorApplyUpdatesRegistry(t *testing.T) {
6970
ready, err := detector.precheck(info)
7071
require.NoError(t, err)
7172
require.True(t, ready)
73+
74+
_, ok := detector.tables[2]
75+
require.False(t, ok)
76+
7277
require.NoError(t, detector.apply(info))
7378

7479
binding, ok := detector.tables[2]
7580
require.True(t, ok)
7681
require.Equal(t, routing.TableKey{Schema: "db2_target", Table: "t"}, binding.binding.Target)
7782
}
7883

79-
func newTestRouteConflictDetector(t *testing.T, targetSchema, targetTable string) *routeConflictDetector {
80-
t.Helper()
84+
func TestRouteConflictDetectorDropReleasesBootstrapBinding(t *testing.T) {
85+
detector := newTestRouteConflictDetector(t, "target", routing.TablePlaceholder)
8186

82-
cfID := common.NewChangeFeedIDWithName("test-changefeed", common.DefaultKeyspaceName)
83-
router, err := routing.NewRouter(cfID, false, []*config.DispatchRule{
84-
{
85-
Matcher: []string{"*.*"},
86-
TargetSchema: targetSchema,
87-
TargetTable: targetTable,
87+
dropInfo := routeDDLInfo{
88+
key: getEventKey(10, false),
89+
commitTs: 10,
90+
droppedTables: &heartbeatpb.InfluencedTables{
91+
InfluenceType: heartbeatpb.InfluenceType_Normal,
92+
TableIDs: []int64{1},
93+
},
94+
}
95+
ready, err := detector.precheck(dropInfo)
96+
require.NoError(t, err)
97+
require.True(t, ready)
98+
require.NoError(t, detector.apply(dropInfo))
99+
100+
_, ok := detector.tables[1]
101+
require.False(t, ok)
102+
103+
addInfo := routeDDLInfo{
104+
key: getEventKey(20, false),
105+
commitTs: 20,
106+
addedTables: []*heartbeatpb.Table{
107+
{SchemaID: 2, TableID: 2},
88108
},
89-
})
109+
}
110+
ready, err = detector.precheck(addInfo)
90111
require.NoError(t, err)
112+
require.True(t, ready)
113+
require.NoError(t, detector.apply(addInfo))
114+
}
115+
116+
func newTestRouteConflictDetector(t *testing.T, targetSchema, targetTable string) *routeConflictDetector {
117+
t.Helper()
91118

119+
cfID := common.NewChangeFeedIDWithName("test-changefeed", common.DefaultKeyspaceName)
92120
store := &fakeRouteSchemaStore{
93121
tables: map[int64]*common.TableInfo{
94122
1: newTestRouteTableInfo(1, "db1", "t"),
95123
2: newTestRouteTableInfo(2, "db2", "t"),
96124
},
97125
}
98-
initialBinding := newTestRouteBinding(t, router, store.tables[1], 1, 1)
99-
registry := routing.NewTargetTableRegistry(cfID, 0)
100-
require.NoError(t, registry.Add(initialBinding.binding))
101-
102-
return &routeConflictDetector{
103-
changefeedID: cfID,
104-
router: router,
105-
registry: registry,
106-
schemaStore: store,
107-
tables: map[int64]routeTableBinding{
108-
1: initialBinding,
126+
appcontext.SetService[schemastore.SchemaStore](appcontext.SchemaStore, store)
127+
128+
detector, err := newRouteConflictDetector(
129+
cfID,
130+
common.KeyspaceMeta{},
131+
&config.ReplicaConfig{
132+
Sink: &config.SinkConfig{
133+
DispatchRules: []*config.DispatchRule{
134+
{
135+
Matcher: []string{"*.*"},
136+
TargetSchema: targetSchema,
137+
TargetTable: targetTable,
138+
},
139+
},
140+
},
109141
},
110-
pendingEvents: make(map[eventKey]*routePendingEvent),
111-
}
112-
}
113-
114-
func newTestRouteBinding(
115-
t *testing.T,
116-
router routing.Router,
117-
tableInfo *common.TableInfo,
118-
replicaTableID int64,
119-
sourceSchemaID int64,
120-
) routeTableBinding {
121-
t.Helper()
122-
123-
binding, err := router.Route(tableInfo.GetSchemaName(), tableInfo.GetTableName())
142+
nil,
143+
[]commonEvent.Table{
144+
{
145+
SchemaID: 1,
146+
TableID: 1,
147+
SchemaTableName: &commonEvent.SchemaTableName{
148+
SchemaName: "db1",
149+
TableName: "t",
150+
},
151+
},
152+
},
153+
)
124154
require.NoError(t, err)
125-
return routeTableBinding{
126-
tableID: replicaTableID,
127-
sourceSchemaID: sourceSchemaID,
128-
binding: binding,
129-
}
155+
return detector
130156
}
131157

132158
func newTestRouteTableInfo(tableID int64, schema, table string) *common.TableInfo {

0 commit comments

Comments
 (0)