@@ -16,7 +16,7 @@ package maintainer
1616import (
1717 "container/heap"
1818 "fmt"
19- "sort "
19+ "slices "
2020
2121 "github.com/pingcap/log"
2222 "github.com/pingcap/ticdc/downstreamadapter/routing"
@@ -37,6 +37,7 @@ type routeConflictDetector struct {
3737 router routing.Router
3838 registry * routing.TargetTableRegistry
3939 schemaStore schemastore.SchemaStore
40+ tables map [int64 ]routeTableBinding
4041
4142 pendingQueue pendingEventKeyHeap
4243 pendingEvents map [eventKey ]* routePendingEvent
@@ -62,9 +63,16 @@ type routeDDLInfo struct {
6263}
6364
6465type routeTransition struct {
65- commitTs uint64
66- removes []int64
67- adds []routing.RouteBinding
66+ commitTs uint64
67+ removeTableIDs []int64
68+ removes []routing.TableKey
69+ adds []routeTableBinding
70+ }
71+
72+ type routeTableBinding struct {
73+ tableID int64
74+ sourceSchemaID int64
75+ binding routing.RouteBinding
6876}
6977
7078func newRouteConflictDetector (
@@ -96,18 +104,14 @@ func newRouteConflictDetector(
96104 if err != nil {
97105 return nil , err
98106 }
99- registry , err := routing .NewTargetTableRegistry (nil )
100- if err != nil {
101- return nil , err
102- }
103- registry .SetChangefeedID (changefeedID )
104107
105108 return & routeConflictDetector {
106109 changefeedID : changefeedID ,
107110 keyspaceMeta : keyspaceMeta ,
108111 router : router ,
109- registry : registry ,
112+ registry : routing . NewTargetTableRegistry ( changefeedID ) ,
110113 schemaStore : appcontext.GetService [schemastore.SchemaStore ](appcontext .SchemaStore ),
114+ tables : make (map [int64 ]routeTableBinding ),
111115 pendingEvents : make (map [eventKey ]* routePendingEvent ),
112116 reportError : reportError ,
113117 }, nil
@@ -117,29 +121,24 @@ func (d *routeConflictDetector) initializeFromTables(tables []commonEvent.Table,
117121 if d == nil {
118122 return nil
119123 }
120- bindings := make ([]routing.RouteBinding , 0 , len (tables ))
124+ registry := routing .NewTargetTableRegistry (d .changefeedID , len (tables ))
125+ currentTables := make (map [int64 ]routeTableBinding , len (tables ))
121126 for _ , t := range tables {
122- tableInfo , err := d .schemaStore . GetTableInfo ( d . keyspaceMeta , t .TableID , startTs )
127+ binding , err := d .buildBindingForTable ( t . TableID , t .SchemaID , startTs )
123128 if err != nil {
124129 return err
125130 }
126- result , err := d .router .RouteName (tableInfo .GetSchemaName (), tableInfo .GetTableName ())
127- if err != nil {
131+ if err := registry .Add (binding .binding ); err != nil {
128132 return err
129133 }
130- bindings = append (bindings , routing .NewRouteBinding (tableInfo , t .TableID , t .SchemaID , result ))
131- }
132-
133- registry , err := routing .NewTargetTableRegistry (bindings )
134- if err != nil {
135- return err
134+ currentTables [t .TableID ] = binding
136135 }
137- registry .SetChangefeedID (d .changefeedID )
138136 d .registry = registry
137+ d .tables = currentTables
139138
140139 log .Info ("route registry initialized" ,
141140 zap .String ("changefeed" , d .changefeedID .Name ()),
142- zap .Int ("bindingCount" , len (bindings )))
141+ zap .Int ("bindingCount" , len (currentTables )))
143142 return nil
144143}
145144
@@ -164,9 +163,6 @@ func (d *routeConflictDetector) precheck(info routeDDLInfo) (bool, error) {
164163 if pending .prechecked {
165164 return true , nil
166165 }
167- if err := d .registry .ValidateReplace (pending .transition .removes , pending .transition .adds ); err != nil {
168- return false , d .fail (err )
169- }
170166 pending .prechecked = true
171167 log .Info ("route transition prechecked" ,
172168 zap .String ("changefeed" , d .changefeedID .Name ()),
@@ -192,15 +188,19 @@ func (d *routeConflictDetector) apply(info routeDDLInfo) error {
192188 "route transition apply out of order, changefeed=%s, commitTs=%d" ,
193189 d .changefeedID .Name (), info .commitTs ))
194190 }
195- if ! pending .prechecked {
196- if err := d .registry .ValidateReplace (pending .transition .removes , pending .transition .adds ); err != nil {
197- return d .fail (err )
198- }
199- pending .prechecked = true
191+ adds := make ([]routing.RouteBinding , 0 , len (pending .transition .adds ))
192+ for _ , add := range pending .transition .adds {
193+ adds = append (adds , add .binding )
200194 }
201- if err := d .registry .Replace (pending .transition .removes , pending . transition . adds ); err != nil {
195+ if err := d .registry .ApplyTransition (pending .transition .removes , adds ); err != nil {
202196 return d .fail (err )
203197 }
198+ for _ , tableID := range pending .transition .removeTableIDs {
199+ delete (d .tables , tableID )
200+ }
201+ for _ , add := range pending .transition .adds {
202+ d .tables [add .tableID ] = add
203+ }
204204 d .popPendingHead (info .key )
205205 log .Info ("route transition applied" ,
206206 zap .String ("changefeed" , d .changefeedID .Name ()),
@@ -259,15 +259,19 @@ func (d *routeConflictDetector) buildTransition(info routeDDLInfo) (*routeTransi
259259 switch info .droppedTables .InfluenceType {
260260 case heartbeatpb .InfluenceType_Normal :
261261 for _ , tableID := range info .droppedTables .TableIDs {
262- builder .addRemove (tableID )
262+ if existing , ok := d .tables [tableID ]; ok {
263+ builder .addRemove (tableID , existing .binding .Source )
264+ }
263265 }
264266 case heartbeatpb .InfluenceType_DB :
265- for _ , b := range d .registry .GetBindingsBySchemaID (info .droppedTables .SchemaID ) {
266- builder .addRemove (b .ReplicaTableID )
267+ for _ , existing := range d .tables {
268+ if existing .sourceSchemaID == info .droppedTables .SchemaID {
269+ builder .addRemove (existing .tableID , existing .binding .Source )
270+ }
267271 }
268272 case heartbeatpb .InfluenceType_All :
269- for _ , b := range d .registry . Snapshot () {
270- builder .addRemove (b . ReplicaTableID )
273+ for _ , existing := range d .tables {
274+ builder .addRemove (existing . tableID , existing . binding . Source )
271275 }
272276 }
273277 }
@@ -281,11 +285,15 @@ func (d *routeConflictDetector) buildTransition(info routeDDLInfo) (*routeTransi
281285 }
282286
283287 for _ , change := range info .updatedSchema {
288+ existing , exists := d .tables [change .TableID ]
289+ if ! exists {
290+ return nil , fmt .Errorf ("route registry binding not found for table %d" , change .TableID )
291+ }
284292 binding , err := d .buildBindingForTable (change .TableID , change .NewSchemaID , info .commitTs )
285293 if err != nil {
286294 return nil , err
287295 }
288- builder .addRemove (change .TableID )
296+ builder .addRemove (change .TableID , existing . binding . Source )
289297 builder .addBinding (binding )
290298 }
291299
@@ -294,18 +302,18 @@ func (d *routeConflictDetector) buildTransition(info routeDDLInfo) (*routeTransi
294302 if builder .hasRemove (tableID ) || builder .hasAdd (tableID ) {
295303 continue
296304 }
297- existing , exists := d .registry . GetBindingByReplicaID ( tableID )
305+ existing , exists := d .tables [ tableID ]
298306 if ! exists {
299307 return nil , fmt .Errorf ("route registry binding not found for table %d" , tableID )
300308 }
301- binding , err := d .buildBindingForTable (tableID , existing .SourceSchemaID , info .commitTs )
309+ binding , err := d .buildBindingForTable (tableID , existing .sourceSchemaID , info .commitTs )
302310 if err != nil {
303311 return nil , err
304312 }
305- if existing .Target != binding .Target ||
306- existing .Source .Schema != binding .Source .Schema ||
307- existing .Source .Table != binding .Source .Table {
308- builder .addRemove (tableID )
313+ if existing .binding . Target != binding . binding .Target ||
314+ existing .binding . Source .Schema != binding . binding .Source .Schema ||
315+ existing .binding . Source .Table != binding . binding .Source .Table {
316+ builder .addRemove (tableID , existing . binding . Source )
309317 builder .addBinding (binding )
310318 }
311319 }
@@ -314,23 +322,27 @@ func (d *routeConflictDetector) buildTransition(info routeDDLInfo) (*routeTransi
314322 return builder .finish (), nil
315323}
316324
317- func (d * routeConflictDetector ) buildBindingForTable (tableID , schemaID int64 , commitTs uint64 ) (routing. RouteBinding , error ) {
325+ func (d * routeConflictDetector ) buildBindingForTable (tableID , schemaID int64 , commitTs uint64 ) (routeTableBinding , error ) {
318326 tableInfo , err := d .schemaStore .GetTableInfo (d .keyspaceMeta , tableID , commitTs )
319327 if err != nil {
320- return routing. RouteBinding {}, err
328+ return routeTableBinding {}, err
321329 }
322330 if schemaID == 0 {
323- if existing , ok := d .registry . GetBindingByReplicaID ( tableID ) ; ok {
324- schemaID = existing .SourceSchemaID
331+ if existing , ok := d .tables [ tableID ] ; ok {
332+ schemaID = existing .sourceSchemaID
325333 } else {
326- return routing. RouteBinding {}, fmt .Errorf ("schema ID hint is missing for table %d" , tableID )
334+ return routeTableBinding {}, fmt .Errorf ("schema ID hint is missing for table %d" , tableID )
327335 }
328336 }
329- result , err := d .router .RouteName (tableInfo .GetSchemaName (), tableInfo .GetTableName ())
337+ binding , err := d .router .Route (tableInfo .GetSchemaName (), tableInfo .GetTableName ())
330338 if err != nil {
331- return routing. RouteBinding {}, err
339+ return routeTableBinding {}, err
332340 }
333- return routing .NewRouteBinding (tableInfo , tableID , schemaID , result ), nil
341+ return routeTableBinding {
342+ tableID : tableID ,
343+ sourceSchemaID : schemaID ,
344+ binding : binding ,
345+ }, nil
334346}
335347
336348func (d * routeConflictDetector ) fail (err error ) error {
@@ -347,32 +359,33 @@ func (d *routeConflictDetector) fail(err error) error {
347359type routeTransitionBuilder struct {
348360 transition routeTransition
349361 removeSet map [int64 ]struct {}
350- addsByID map [int64 ]routing. RouteBinding
362+ addsByID map [int64 ]routeTableBinding
351363}
352364
353365func newRouteTransitionBuilder (commitTs uint64 ) * routeTransitionBuilder {
354366 return & routeTransitionBuilder {
355367 transition : routeTransition {commitTs : commitTs },
356368 removeSet : make (map [int64 ]struct {}),
357- addsByID : make (map [int64 ]routing. RouteBinding ),
369+ addsByID : make (map [int64 ]routeTableBinding ),
358370 }
359371}
360372
361- func (b * routeTransitionBuilder ) addRemove (tableID int64 ) {
373+ func (b * routeTransitionBuilder ) addRemove (tableID int64 , source routing. TableKey ) {
362374 if _ , ok := b .removeSet [tableID ]; ok {
363375 return
364376 }
365377 b .removeSet [tableID ] = struct {}{}
366- b .transition .removes = append (b .transition .removes , tableID )
378+ b .transition .removeTableIDs = append (b .transition .removeTableIDs , tableID )
379+ b .transition .removes = append (b .transition .removes , source )
367380}
368381
369382func (b * routeTransitionBuilder ) hasRemove (tableID int64 ) bool {
370383 _ , ok := b .removeSet [tableID ]
371384 return ok
372385}
373386
374- func (b * routeTransitionBuilder ) addBinding (binding routing. RouteBinding ) {
375- b .addsByID [binding .ReplicaTableID ] = binding
387+ func (b * routeTransitionBuilder ) addBinding (binding routeTableBinding ) {
388+ b .addsByID [binding .tableID ] = binding
376389}
377390
378391func (b * routeTransitionBuilder ) hasAdd (tableID int64 ) bool {
@@ -386,7 +399,7 @@ func (b *routeTransitionBuilder) finish() *routeTransition {
386399 for id := range b .addsByID {
387400 ids = append (ids , id )
388401 }
389- sort . Slice (ids , func ( i , j int ) bool { return ids [ i ] < ids [ j ] } )
402+ slices . Sort (ids )
390403 for _ , id := range ids {
391404 b .transition .adds = append (b .transition .adds , b .addsByID [id ])
392405 }
0 commit comments