@@ -2581,20 +2581,18 @@ func (s *Server) DropTargets(ctx context.Context, ts *trafficSwitcher, keepData,
2581
2581
lockName := fmt .Sprintf ("%s/%s" , ts .TargetKeyspaceName (), ts .WorkflowName ())
2582
2582
ctx , workflowUnlock , lockErr := s .ts .LockName (ctx , lockName , "DropTargets" )
2583
2583
if lockErr != nil {
2584
- ts .Logger (). Errorf ( "Locking the workflow %s failed: %v " , lockName , lockErr )
2584
+ return defaultErrorHandler ( ts .Logger (), fmt . Sprintf ( "failed to lock the %s workflow " , lockName ) , lockErr )
2585
2585
}
2586
2586
defer workflowUnlock (& err )
2587
2587
ctx , sourceUnlock , lockErr := sw .lockKeyspace (ctx , ts .SourceKeyspaceName (), "DropTargets" )
2588
2588
if lockErr != nil {
2589
- ts .Logger ().Errorf ("Source LockKeyspace failed: %v" , lockErr )
2590
- return nil , lockErr
2589
+ return defaultErrorHandler (ts .Logger (), fmt .Sprintf ("failed to lock the %s keyspace" , ts .SourceKeyspaceName ()), lockErr )
2591
2590
}
2592
2591
defer sourceUnlock (& err )
2593
2592
if ts .TargetKeyspaceName () != ts .SourceKeyspaceName () {
2594
2593
lockCtx , targetUnlock , lockErr := sw .lockKeyspace (ctx , ts .TargetKeyspaceName (), "DropTargets" )
2595
2594
if lockErr != nil {
2596
- ts .Logger ().Errorf ("Target LockKeyspace failed: %v" , lockErr )
2597
- return nil , lockErr
2595
+ return defaultErrorHandler (ts .Logger (), fmt .Sprintf ("failed to lock the %s keyspace" , ts .TargetKeyspaceName ()), lockErr )
2598
2596
}
2599
2597
defer targetUnlock (& err )
2600
2598
ctx = lockCtx
@@ -2779,20 +2777,18 @@ func (s *Server) dropSources(ctx context.Context, ts *trafficSwitcher, removalTy
2779
2777
lockName := fmt .Sprintf ("%s/%s" , ts .TargetKeyspaceName (), ts .WorkflowName ())
2780
2778
ctx , workflowUnlock , lockErr := s .ts .LockName (ctx , lockName , "DropSources" )
2781
2779
if lockErr != nil {
2782
- ts .Logger (). Errorf ( "Locking the workflow %s failed: %v " , lockName , lockErr )
2780
+ return defaultErrorHandler ( ts .Logger (), fmt . Sprintf ( "failed to lock the %s workflow " , lockName ) , lockErr )
2783
2781
}
2784
2782
defer workflowUnlock (& err )
2785
2783
ctx , sourceUnlock , lockErr := sw .lockKeyspace (ctx , ts .SourceKeyspaceName (), "DropSources" )
2786
2784
if lockErr != nil {
2787
- ts .Logger ().Errorf ("Source LockKeyspace failed: %v" , lockErr )
2788
- return nil , lockErr
2785
+ return defaultErrorHandler (ts .Logger (), fmt .Sprintf ("failed to lock the %s keyspace" , ts .SourceKeyspaceName ()), lockErr )
2789
2786
}
2790
2787
defer sourceUnlock (& err )
2791
2788
if ts .TargetKeyspaceName () != ts .SourceKeyspaceName () {
2792
2789
lockCtx , targetUnlock , lockErr := sw .lockKeyspace (ctx , ts .TargetKeyspaceName (), "DropSources" )
2793
2790
if lockErr != nil {
2794
- ts .Logger ().Errorf ("Target LockKeyspace failed: %v" , lockErr )
2795
- return nil , lockErr
2791
+ return defaultErrorHandler (ts .Logger (), fmt .Sprintf ("failed to lock the %s keyspace" , ts .TargetKeyspaceName ()), lockErr )
2796
2792
}
2797
2793
defer targetUnlock (& err )
2798
2794
ctx = lockCtx
@@ -3020,13 +3016,12 @@ func (s *Server) finalizeMigrateWorkflow(ctx context.Context, ts *trafficSwitche
3020
3016
lockName := fmt .Sprintf ("%s/%s" , ts .TargetKeyspaceName (), ts .WorkflowName ())
3021
3017
ctx , workflowUnlock , lockErr := s .ts .LockName (ctx , lockName , "completeMigrateWorkflow" )
3022
3018
if lockErr != nil {
3023
- ts .Logger (). Errorf ( "Locking the workflow %s failed: %v " , lockName , lockErr )
3019
+ return defaultErrorHandler ( ts .Logger (), fmt . Sprintf ( "failed to lock the %s workflow " , lockName ) , lockErr )
3024
3020
}
3025
3021
defer workflowUnlock (& err )
3026
3022
ctx , targetUnlock , lockErr := sw .lockKeyspace (ctx , ts .TargetKeyspaceName (), "completeMigrateWorkflow" )
3027
3023
if lockErr != nil {
3028
- ts .Logger ().Errorf ("Target LockKeyspace failed: %v" , lockErr )
3029
- return nil , lockErr
3024
+ return defaultErrorHandler (ts .Logger (), fmt .Sprintf ("failed to lock the %s keyspace" , ts .TargetKeyspaceName ()), lockErr )
3030
3025
}
3031
3026
defer targetUnlock (& err )
3032
3027
@@ -3193,16 +3188,10 @@ func (s *Server) switchReads(ctx context.Context, req *vtctldatapb.WorkflowSwitc
3193
3188
3194
3189
cellsStr := strings .Join (req .Cells , "," )
3195
3190
3196
- // Consistently handle errors by logging and returning them.
3197
- handleError := func (message string , err error ) (* []string , error ) {
3198
- werr := vterrors .Wrapf (err , message )
3199
- ts .Logger ().Error (werr )
3200
- return nil , werr
3201
- }
3202
-
3203
3191
log .Infof ("Switching reads: %s.%s tablet types: %s, cells: %s, workflow state: %s" , ts .targetKeyspace , ts .workflow , roTypesToSwitchStr , cellsStr , state .String ())
3204
3192
if ! switchReplica && ! switchRdonly {
3205
- return handleError ("invalid tablet types" , vterrors .Errorf (vtrpcpb .Code_INVALID_ARGUMENT , "tablet types must be REPLICA or RDONLY: %s" , roTypesToSwitchStr ))
3193
+ return defaultErrorHandler (ts .Logger (), "invalid tablet types" ,
3194
+ vterrors .Errorf (vtrpcpb .Code_INVALID_ARGUMENT , "tablet types must be REPLICA or RDONLY: %s" , roTypesToSwitchStr ))
3206
3195
}
3207
3196
// For partial (shard-by-shard migrations) or multi-tenant migrations, traffic for all tablet types
3208
3197
// is expected to be switched at once. For other MoveTables migrations where we use table routing rules
@@ -3214,24 +3203,28 @@ func (s *Server) switchReads(ctx context.Context, req *vtctldatapb.WorkflowSwitc
3214
3203
trafficSwitchingIsAllOrNothing = true
3215
3204
case ts .MigrationType () == binlogdatapb .MigrationType_TABLES && ts .IsMultiTenantMigration ():
3216
3205
if direction == DirectionBackward {
3217
- return handleError ("invalid request" , vterrors .Errorf (vtrpcpb .Code_INVALID_ARGUMENT , "requesting reversal of read traffic for multi-tenant migrations is not supported" ))
3206
+ return defaultErrorHandler (ts .Logger (), "invalid request" , vterrors .Errorf (vtrpcpb .Code_INVALID_ARGUMENT ,
3207
+ "requesting reversal of read traffic for multi-tenant migrations is not supported" ))
3218
3208
}
3219
3209
// For multi-tenant migrations, we only support switching traffic to all cells at once
3220
3210
allCells , err := ts .TopoServer ().GetCellInfoNames (ctx )
3221
3211
if err != nil {
3222
3212
return nil , err
3223
3213
}
3224
3214
if len (req .GetCells ()) != 0 && len (req .GetCells ()) != len (allCells ) {
3225
- return handleError ("invalid request" , vterrors .Errorf (vtrpcpb .Code_INVALID_ARGUMENT , "requesting read traffic for multi-tenant migrations must include all cells" ))
3215
+ return defaultErrorHandler (ts .Logger (), "invalid request" , vterrors .Errorf (vtrpcpb .Code_INVALID_ARGUMENT ,
3216
+ "requesting read traffic for multi-tenant migrations must include all cells" ))
3226
3217
}
3227
3218
}
3228
3219
3229
3220
if ! trafficSwitchingIsAllOrNothing {
3230
3221
if direction == DirectionBackward && switchReplica && len (state .ReplicaCellsSwitched ) == 0 {
3231
- return handleError ("invalid request" , vterrors .Errorf (vtrpcpb .Code_FAILED_PRECONDITION , "requesting reversal of read traffic for REPLICAs but REPLICA reads have not been switched" ))
3222
+ return defaultErrorHandler (ts .Logger (), "invalid request" , vterrors .Errorf (vtrpcpb .Code_FAILED_PRECONDITION ,
3223
+ "requesting reversal of read traffic for REPLICAs but REPLICA reads have not been switched" ))
3232
3224
}
3233
3225
if direction == DirectionBackward && switchRdonly && len (state .RdonlyCellsSwitched ) == 0 {
3234
- return handleError ("invalid request" , vterrors .Errorf (vtrpcpb .Code_FAILED_PRECONDITION , "requesting reversal of SwitchReads for RDONLYs but RDONLY reads have not been switched" ))
3226
+ return defaultErrorHandler (ts .Logger (), "invalid request" , vterrors .Errorf (vtrpcpb .Code_FAILED_PRECONDITION ,
3227
+ "requesting reversal of SwitchReads for RDONLYs but RDONLY reads have not been switched" ))
3235
3228
}
3236
3229
}
3237
3230
@@ -3253,7 +3246,7 @@ func (s *Server) switchReads(ctx context.Context, req *vtctldatapb.WorkflowSwitc
3253
3246
// If journals exist notify user and fail.
3254
3247
journalsExist , _ , err := ts .checkJournals (ctx )
3255
3248
if err != nil {
3256
- return handleError ( fmt .Sprintf ("failed to read journal in the %s keyspace" , ts .SourceKeyspaceName ()), err )
3249
+ return defaultErrorHandler ( ts . Logger (), fmt .Sprintf ("failed to read journal in the %s keyspace" , ts .SourceKeyspaceName ()), err )
3257
3250
}
3258
3251
if journalsExist {
3259
3252
log .Infof ("Found a previous journal entry for %d" , ts .id )
@@ -3266,7 +3259,7 @@ func (s *Server) switchReads(ctx context.Context, req *vtctldatapb.WorkflowSwitc
3266
3259
}
3267
3260
3268
3261
if err := ts .validate (ctx ); err != nil {
3269
- return handleError ( "workflow validation failed" , err )
3262
+ return defaultErrorHandler ( ts . Logger (), "workflow validation failed" , err )
3270
3263
}
3271
3264
3272
3265
// For switching reads, locking the source keyspace is sufficient.
@@ -3282,7 +3275,7 @@ func (s *Server) switchReads(ctx context.Context, req *vtctldatapb.WorkflowSwitc
3282
3275
// For reads, locking the source keyspace is sufficient.
3283
3276
ctx , unlock , lockErr := sw .lockKeyspace (ctx , ts .SourceKeyspaceName (), "SwitchReads" , topo .WithTTL (ksLockTTL ))
3284
3277
if lockErr != nil {
3285
- return handleError ( fmt .Sprintf ("failed to lock the %s keyspace" , ts .SourceKeyspaceName ()), lockErr )
3278
+ return defaultErrorHandler ( ts . Logger (), fmt .Sprintf ("failed to lock the %s keyspace" , ts .SourceKeyspaceName ()), lockErr )
3286
3279
}
3287
3280
defer unlock (& err )
3288
3281
confirmKeyspaceLocksHeld := func () error {
@@ -3297,7 +3290,7 @@ func (s *Server) switchReads(ctx context.Context, req *vtctldatapb.WorkflowSwitc
3297
3290
3298
3291
// Remove mirror rules for the specified tablet types.
3299
3292
if err := sw .mirrorTableTraffic (ctx , roTabletTypes , 0 ); err != nil {
3300
- return handleError ( fmt .Sprintf ("failed to remove mirror rules from source keyspace %s to target keyspace %s, workflow %s, for read-only tablet types" ,
3293
+ return defaultErrorHandler ( ts . Logger (), fmt .Sprintf ("failed to remove mirror rules from source keyspace %s to target keyspace %s, workflow %s, for read-only tablet types" ,
3301
3294
ts .SourceKeyspaceName (), ts .TargetKeyspaceName (), ts .WorkflowName ()), err )
3302
3295
}
3303
3296
@@ -3306,36 +3299,36 @@ func (s *Server) switchReads(ctx context.Context, req *vtctldatapb.WorkflowSwitc
3306
3299
case ts .IsMultiTenantMigration ():
3307
3300
err := sw .switchKeyspaceReads (ctx , roTabletTypes )
3308
3301
if err != nil {
3309
- return handleError ( fmt .Sprintf ("failed to switch read traffic, from source keyspace %s to target keyspace %s, workflow %s" ,
3302
+ return defaultErrorHandler ( ts . Logger (), fmt .Sprintf ("failed to switch read traffic, from source keyspace %s to target keyspace %s, workflow %s" ,
3310
3303
ts .SourceKeyspaceName (), ts .TargetKeyspaceName (), ts .WorkflowName ()), err )
3311
3304
}
3312
3305
case ts .isPartialMigration :
3313
3306
ts .Logger ().Infof ("Partial migration, skipping switchTableReads as traffic is all or nothing per shard and overridden for reads AND writes in the ShardRoutingRule created when switching writes." )
3314
3307
default :
3315
3308
err := sw .switchTableReads (ctx , req .Cells , roTabletTypes , rebuildSrvVSchema , direction )
3316
3309
if err != nil {
3317
- return handleError ( "failed to switch read traffic for the tables" , err )
3310
+ return defaultErrorHandler ( ts . Logger (), "failed to switch read traffic for the tables" , err )
3318
3311
}
3319
3312
}
3320
3313
return sw .logs (), nil
3321
3314
}
3322
3315
3323
3316
if err := confirmKeyspaceLocksHeld (); err != nil {
3324
- return handleError ( "locks were lost" , err )
3317
+ return defaultErrorHandler ( ts . Logger (), "locks were lost" , err )
3325
3318
}
3326
3319
ts .Logger ().Infof ("About to switchShardReads: cells: %s, tablet types: %s, direction: %d" , cellsStr , roTypesToSwitchStr , direction )
3327
3320
if err := sw .switchShardReads (ctx , req .Cells , roTabletTypes , direction ); err != nil {
3328
- return handleError ( "failed to switch read traffic for the shards" , err )
3321
+ return defaultErrorHandler ( ts . Logger (), "failed to switch read traffic for the shards" , err )
3329
3322
}
3330
3323
3331
3324
if err := confirmKeyspaceLocksHeld (); err != nil {
3332
- return handleError ( "locks were lost" , err )
3325
+ return defaultErrorHandler ( ts . Logger (), "locks were lost" , err )
3333
3326
}
3334
3327
ts .Logger ().Infof ("switchShardReads Completed: cells: %s, tablet types: %s, direction: %d" , cellsStr , roTypesToSwitchStr , direction )
3335
3328
if err := s .ts .ValidateSrvKeyspace (ctx , ts .targetKeyspace , cellsStr ); err != nil {
3336
3329
err2 := vterrors .Wrapf (err , "after switching shard reads, found SrvKeyspace for %s is corrupt in cell %s" ,
3337
3330
ts .targetKeyspace , cellsStr )
3338
- return handleError ( "failed to validate SrvKeyspace record" , err2 )
3331
+ return defaultErrorHandler ( ts . Logger (), "failed to validate SrvKeyspace record" , err2 )
3339
3332
}
3340
3333
return sw .logs (), nil
3341
3334
}
0 commit comments