@@ -81,7 +81,7 @@ func closePDSchedule(ctx context.Context) error {
81
81
return infosync .SetPDScheduleConfig (ctx , closeMap )
82
82
}
83
83
84
- func savePDSchedule (ctx context.Context , job * model.Job ) error {
84
+ func savePDSchedule (ctx context.Context , args * model.FlashbackClusterArgs ) error {
85
85
retValue , err := infosync .GetPDScheduleConfig (ctx )
86
86
if err != nil {
87
87
return err
@@ -90,7 +90,7 @@ func savePDSchedule(ctx context.Context, job *model.Job) error {
90
90
for _ , key := range pdScheduleKey {
91
91
saveValue [key ] = retValue [key ]
92
92
}
93
- job . Args [ pdScheduleArgsOffset ] = & saveValue
93
+ args . PDScheduleValue = saveValue
94
94
return nil
95
95
}
96
96
@@ -158,40 +158,21 @@ func ValidateFlashbackTS(ctx context.Context, sctx sessionctx.Context, flashBack
158
158
return gcutil .ValidateSnapshotWithGCSafePoint (flashBackTS , gcSafePoint )
159
159
}
160
160
161
- func getTiDBTTLJobEnable (sess sessionctx.Context ) (string , error ) {
162
- val , err := sess .GetSessionVars ().GlobalVarsAccessor .GetGlobalSysVar (variable . TiDBTTLJobEnable )
161
+ func getGlobalSysVarAsBool (sess sessionctx.Context , name string ) (bool , error ) {
162
+ val , err := sess .GetSessionVars ().GlobalVarsAccessor .GetGlobalSysVar (name )
163
163
if err != nil {
164
- return "" , errors .Trace (err )
164
+ return false , errors .Trace (err )
165
165
}
166
- return val , nil
166
+ return variable . TiDBOptOn ( val ) , nil
167
167
}
168
168
169
- func setTiDBTTLJobEnable (ctx context.Context , sess sessionctx.Context , value string ) error {
170
- return sess .GetSessionVars ().GlobalVarsAccessor .SetGlobalSysVar (ctx , variable .TiDBTTLJobEnable , value )
171
- }
172
-
173
- func setTiDBEnableAutoAnalyze (ctx context.Context , sess sessionctx.Context , value string ) error {
174
- return sess .GetSessionVars ().GlobalVarsAccessor .SetGlobalSysVar (ctx , variable .TiDBEnableAutoAnalyze , value )
175
- }
176
-
177
- func getTiDBEnableAutoAnalyze (sess sessionctx.Context ) (string , error ) {
178
- val , err := sess .GetSessionVars ().GlobalVarsAccessor .GetGlobalSysVar (variable .TiDBEnableAutoAnalyze )
179
- if err != nil {
180
- return "" , errors .Trace (err )
169
+ func setGlobalSysVarFromBool (ctx context.Context , sess sessionctx.Context , name string , value bool ) error {
170
+ sv := variable .On
171
+ if ! value {
172
+ sv = variable .Off
181
173
}
182
- return val , nil
183
- }
184
-
185
- func setTiDBSuperReadOnly (ctx context.Context , sess sessionctx.Context , value string ) error {
186
- return sess .GetSessionVars ().GlobalVarsAccessor .SetGlobalSysVar (ctx , variable .TiDBSuperReadOnly , value )
187
- }
188
174
189
- func getTiDBSuperReadOnly (sess sessionctx.Context ) (string , error ) {
190
- val , err := sess .GetSessionVars ().GlobalVarsAccessor .GetGlobalSysVar (variable .TiDBSuperReadOnly )
191
- if err != nil {
192
- return "" , errors .Trace (err )
193
- }
194
- return val , nil
175
+ return sess .GetSessionVars ().GlobalVarsAccessor .SetGlobalSysVar (ctx , name , sv )
195
176
}
196
177
197
178
func isFlashbackSupportedDDLAction (action model.ActionType ) bool {
@@ -231,13 +212,13 @@ func checkAndSetFlashbackClusterInfo(ctx context.Context, se sessionctx.Context,
231
212
if err = closePDSchedule (ctx ); err != nil {
232
213
return err
233
214
}
234
- if err = setTiDBEnableAutoAnalyze (ctx , se , variable .Off ); err != nil {
215
+ if err = setGlobalSysVarFromBool (ctx , se , variable .TiDBEnableAutoAnalyze , false ); err != nil {
235
216
return err
236
217
}
237
- if err = setTiDBSuperReadOnly (ctx , se , variable .On ); err != nil {
218
+ if err = setGlobalSysVarFromBool (ctx , se , variable .TiDBSuperReadOnly , true ); err != nil {
238
219
return err
239
220
}
240
- if err = setTiDBTTLJobEnable (ctx , se , variable .Off ); err != nil {
221
+ if err = setGlobalSysVarFromBool (ctx , se , variable .TiDBTTLJobEnable , false ); err != nil {
241
222
return err
242
223
}
243
224
@@ -354,16 +335,18 @@ type keyRangeMayExclude struct {
354
335
exclude bool
355
336
}
356
337
357
- // appendContinuousKeyRanges merges not exclude continuous key ranges and appends
338
+ // mergeContinuousKeyRanges merges not exclude continuous key ranges and appends
358
339
// to given []kv.KeyRange, assuming the gap between key ranges has no data.
359
340
//
360
341
// Precondition: schemaKeyRanges is sorted by start key. schemaKeyRanges are
361
342
// non-overlapping.
362
- func appendContinuousKeyRanges ( result []kv. KeyRange , schemaKeyRanges []keyRangeMayExclude ) []kv.KeyRange {
343
+ func mergeContinuousKeyRanges ( schemaKeyRanges []keyRangeMayExclude ) []kv.KeyRange {
363
344
var (
364
345
continuousStart , continuousEnd kv.Key
365
346
)
366
347
348
+ result := make ([]kv.KeyRange , 0 , 1 )
349
+
367
350
for _ , r := range schemaKeyRanges {
368
351
if r .exclude {
369
352
if continuousStart != nil {
@@ -398,9 +381,6 @@ func getFlashbackKeyRanges(ctx context.Context, sess sessionctx.Context, flashba
398
381
is := sess .GetDomainInfoSchema ().(infoschema.InfoSchema )
399
382
schemas := is .AllSchemas ()
400
383
401
- // The semantic of keyRanges(output).
402
- keyRanges := make ([]kv.KeyRange , 0 )
403
-
404
384
// get snapshot schema IDs.
405
385
flashbackSnapshotMeta := meta .NewReader (sess .GetStore ().GetSnapshot (kv .NewVersion (flashbackTS )))
406
386
snapshotSchemas , err := flashbackSnapshotMeta .ListDatabases ()
@@ -453,7 +433,7 @@ func getFlashbackKeyRanges(ctx context.Context, sess sessionctx.Context, flashba
453
433
return bytes .Compare (a .r .StartKey , b .r .StartKey )
454
434
})
455
435
456
- keyRanges = appendContinuousKeyRanges ( keyRanges , schemaKeyRanges )
436
+ keyRanges := mergeContinuousKeyRanges ( schemaKeyRanges )
457
437
458
438
startKey := tablecodec .EncodeMetaKeyPrefix ([]byte ("DBs" ))
459
439
keyRanges = append (keyRanges , kv.KeyRange {
@@ -681,7 +661,7 @@ func flashbackToVersion(
681
661
).RunOnRange (ctx , startKey , endKey )
682
662
}
683
663
684
- func splitRegionsByKeyRanges (ctx context.Context , store kv.Storage , keyRanges []kv .KeyRange ) {
664
+ func splitRegionsByKeyRanges (ctx context.Context , store kv.Storage , keyRanges []model .KeyRange ) {
685
665
if s , ok := store .(kv.SplittableStore ); ok {
686
666
for _ , keys := range keyRanges {
687
667
for {
@@ -713,18 +693,14 @@ func (w *worker) onFlashbackCluster(jobCtx *jobContext, job *model.Job) (ver int
713
693
return ver , errors .Errorf ("Not support flashback cluster in non-TiKV env" )
714
694
}
715
695
716
- var flashbackTS , lockedRegions , startTS , commitTS uint64
717
- var pdScheduleValue map [string ]any
718
- var autoAnalyzeValue , readOnlyValue , ttlJobEnableValue string
719
- var gcEnabledValue bool
720
- var keyRanges []kv.KeyRange
721
- if err := job .DecodeArgs (& flashbackTS , & pdScheduleValue , & gcEnabledValue , & autoAnalyzeValue , & readOnlyValue , & lockedRegions , & startTS , & commitTS , & ttlJobEnableValue , & keyRanges ); err != nil {
696
+ args , err := model .GetFlashbackClusterArgs (job )
697
+ if err != nil {
722
698
job .State = model .JobStateCancelled
723
699
return ver , errors .Trace (err )
724
700
}
725
701
726
702
var totalRegions , completedRegions atomic.Uint64
727
- totalRegions .Store (lockedRegions )
703
+ totalRegions .Store (args . LockedRegionCnt )
728
704
729
705
sess , err := w .sessPool .Get ()
730
706
if err != nil {
@@ -736,54 +712,63 @@ func (w *worker) onFlashbackCluster(jobCtx *jobContext, job *model.Job) (ver int
736
712
switch job .SchemaState {
737
713
// Stage 1, check and set FlashbackClusterJobID, and update job args.
738
714
case model .StateNone :
739
- if err = savePDSchedule (w .ctx , job ); err != nil {
715
+ if err = savePDSchedule (w .ctx , args ); err != nil {
740
716
job .State = model .JobStateCancelled
741
717
return ver , errors .Trace (err )
742
718
}
743
- gcEnableValue , err := gcutil .CheckGCEnable (sess )
719
+
720
+ args .EnableGC , err = gcutil .CheckGCEnable (sess )
744
721
if err != nil {
745
722
job .State = model .JobStateCancelled
746
723
return ver , errors .Trace (err )
747
724
}
748
- job . Args [ gcEnabledOffset ] = & gcEnableValue
749
- autoAnalyzeValue , err = getTiDBEnableAutoAnalyze (sess )
725
+
726
+ args . EnableAutoAnalyze , err = getGlobalSysVarAsBool (sess , variable . TiDBEnableAutoAnalyze )
750
727
if err != nil {
751
728
job .State = model .JobStateCancelled
752
729
return ver , errors .Trace (err )
753
730
}
754
- job . Args [ autoAnalyzeOffset ] = & autoAnalyzeValue
755
- readOnlyValue , err = getTiDBSuperReadOnly (sess )
731
+
732
+ args . SuperReadOnly , err = getGlobalSysVarAsBool (sess , variable . TiDBSuperReadOnly )
756
733
if err != nil {
757
734
job .State = model .JobStateCancelled
758
735
return ver , errors .Trace (err )
759
736
}
760
- job . Args [ readOnlyOffset ] = & readOnlyValue
761
- ttlJobEnableValue , err = getTiDBTTLJobEnable (sess )
737
+
738
+ args . EnableTTLJob , err = getGlobalSysVarAsBool (sess , variable . TiDBTTLJobEnable )
762
739
if err != nil {
763
740
job .State = model .JobStateCancelled
764
741
return ver , errors .Trace (err )
765
742
}
766
- job .Args [ttlJobEnableOffSet ] = & ttlJobEnableValue
743
+
744
+ job .FillArgs (args )
767
745
job .SchemaState = model .StateDeleteOnly
768
746
return ver , nil
769
747
// Stage 2, check flashbackTS, close GC and PD schedule, get flashback key ranges.
770
748
case model .StateDeleteOnly :
771
- if err = checkAndSetFlashbackClusterInfo (w .ctx , sess , jobCtx .store , jobCtx .metaMut , job , flashbackTS ); err != nil {
749
+ if err = checkAndSetFlashbackClusterInfo (w .ctx , sess , jobCtx .store , jobCtx .metaMut , job , args . FlashbackTS ); err != nil {
772
750
job .State = model .JobStateCancelled
773
751
return ver , errors .Trace (err )
774
752
}
775
753
// We should get startTS here to avoid lost startTS when TiDB crashed during send prepare flashback RPC.
776
- startTS , err = jobCtx .store .GetOracle ().GetTimestamp (w .ctx , & oracle.Option {TxnScope : oracle .GlobalTxnScope })
754
+ args . StartTS , err = jobCtx .store .GetOracle ().GetTimestamp (w .ctx , & oracle.Option {TxnScope : oracle .GlobalTxnScope })
777
755
if err != nil {
778
756
job .State = model .JobStateCancelled
779
757
return ver , errors .Trace (err )
780
758
}
781
- job .Args [startTSOffset ] = startTS
782
- keyRanges , err = getFlashbackKeyRanges (w .ctx , sess , flashbackTS )
759
+ keyRanges , err := getFlashbackKeyRanges (w .ctx , sess , args .FlashbackTS )
783
760
if err != nil {
784
761
return ver , errors .Trace (err )
785
762
}
786
- job .Args [keyRangesOffset ] = keyRanges
763
+ args .FlashbackKeyRanges = make ([]model.KeyRange , len (keyRanges ))
764
+ for i , keyRange := range keyRanges {
765
+ args .FlashbackKeyRanges [i ] = model.KeyRange {
766
+ StartKey : keyRange .StartKey ,
767
+ EndKey : keyRange .EndKey ,
768
+ }
769
+ }
770
+
771
+ job .FillArgs (args )
787
772
job .SchemaState = model .StateWriteOnly
788
773
return updateSchemaVersion (jobCtx , job )
789
774
// Stage 3, lock related key ranges.
@@ -794,27 +779,27 @@ func (w *worker) onFlashbackCluster(jobCtx *jobContext, job *model.Job) (ver int
794
779
return updateSchemaVersion (jobCtx , job )
795
780
}
796
781
// Split region by keyRanges, make sure no unrelated key ranges be locked.
797
- splitRegionsByKeyRanges (w .ctx , jobCtx .store , keyRanges )
782
+ splitRegionsByKeyRanges (w .ctx , jobCtx .store , args . FlashbackKeyRanges )
798
783
totalRegions .Store (0 )
799
- for _ , r := range keyRanges {
784
+ for _ , r := range args . FlashbackKeyRanges {
800
785
if err = flashbackToVersion (w .ctx , jobCtx .store ,
801
786
func (ctx context.Context , r tikvstore.KeyRange ) (rangetask.TaskStat , error ) {
802
- stats , err := SendPrepareFlashbackToVersionRPC (ctx , jobCtx .store .(tikv.Storage ), flashbackTS , startTS , r )
787
+ stats , err := SendPrepareFlashbackToVersionRPC (ctx , jobCtx .store .(tikv.Storage ), args . FlashbackTS , args . StartTS , r )
803
788
totalRegions .Add (uint64 (stats .CompletedRegions ))
804
789
return stats , err
805
790
}, r .StartKey , r .EndKey ); err != nil {
806
791
logutil .DDLLogger ().Warn ("Get error when do flashback" , zap .Error (err ))
807
792
return ver , err
808
793
}
809
794
}
810
- job . Args [ totalLockedRegionsOffset ] = totalRegions .Load ()
795
+ args . LockedRegionCnt = totalRegions .Load ()
811
796
812
797
// We should get commitTS here to avoid lost commitTS when TiDB crashed during send flashback RPC.
813
- commitTS , err = jobCtx .store .GetOracle ().GetTimestamp (w .ctx , & oracle.Option {TxnScope : oracle .GlobalTxnScope })
798
+ args . CommitTS , err = jobCtx .store .GetOracle ().GetTimestamp (w .ctx , & oracle.Option {TxnScope : oracle .GlobalTxnScope })
814
799
if err != nil {
815
800
return ver , errors .Trace (err )
816
801
}
817
- job .Args [ commitTSOffset ] = commitTS
802
+ job .FillArgs ( args )
818
803
job .SchemaState = model .StateWriteReorganization
819
804
return ver , nil
820
805
// Stage 4, get key ranges and send flashback RPC.
@@ -827,11 +812,11 @@ func (w *worker) onFlashbackCluster(jobCtx *jobContext, job *model.Job) (ver int
827
812
return ver , nil
828
813
}
829
814
830
- for _ , r := range keyRanges {
815
+ for _ , r := range args . FlashbackKeyRanges {
831
816
if err = flashbackToVersion (w .ctx , jobCtx .store ,
832
817
func (ctx context.Context , r tikvstore.KeyRange ) (rangetask.TaskStat , error ) {
833
818
// Use same startTS as prepare phase to simulate 1PC txn.
834
- stats , err := SendFlashbackToVersionRPC (ctx , jobCtx .store .(tikv.Storage ), flashbackTS , startTS , commitTS , r )
819
+ stats , err := SendFlashbackToVersionRPC (ctx , jobCtx .store .(tikv.Storage ), args . FlashbackTS , args . StartTS , args . CommitTS , r )
835
820
completedRegions .Add (uint64 (stats .CompletedRegions ))
836
821
logutil .DDLLogger ().Info ("flashback cluster stats" ,
837
822
zap .Uint64 ("complete regions" , completedRegions .Load ()),
@@ -858,44 +843,47 @@ func finishFlashbackCluster(w *worker, job *model.Job) error {
858
843
return nil
859
844
}
860
845
861
- var flashbackTS , lockedRegions , startTS , commitTS uint64
862
- var pdScheduleValue map [string ]any
863
- var autoAnalyzeValue , readOnlyValue , ttlJobEnableValue string
864
- var gcEnabled bool
865
-
866
- if err := job .DecodeArgs (& flashbackTS , & pdScheduleValue , & gcEnabled , & autoAnalyzeValue , & readOnlyValue , & lockedRegions , & startTS , & commitTS , & ttlJobEnableValue ); err != nil {
846
+ args , err := model .GetFlashbackClusterArgs (job )
847
+ if err != nil {
867
848
return errors .Trace (err )
868
849
}
850
+
869
851
sess , err := w .sessPool .Get ()
870
852
if err != nil {
871
853
return errors .Trace (err )
872
854
}
873
855
defer w .sessPool .Put (sess )
874
856
875
857
err = kv .RunInNewTxn (w .ctx , w .store , true , func (context.Context , kv.Transaction ) error {
876
- if err = recoverPDSchedule (w .ctx , pdScheduleValue ); err != nil {
877
- return err
858
+ if err = recoverPDSchedule (w .ctx , args . PDScheduleValue ); err != nil {
859
+ return errors . Trace ( err )
878
860
}
879
- if gcEnabled {
861
+
862
+ if args .EnableGC {
880
863
if err = gcutil .EnableGC (sess ); err != nil {
881
- return err
864
+ return errors . Trace ( err )
882
865
}
883
866
}
884
- if err = setTiDBSuperReadOnly (w .ctx , sess , readOnlyValue ); err != nil {
885
- return err
867
+
868
+ if err = setGlobalSysVarFromBool (w .ctx , sess , variable .TiDBSuperReadOnly , args .SuperReadOnly ); err != nil {
869
+ return errors .Trace (err )
886
870
}
887
871
888
872
if job .IsCancelled () {
889
873
// only restore `tidb_ttl_job_enable` when flashback failed
890
- if err = setTiDBTTLJobEnable (w .ctx , sess , ttlJobEnableValue ); err != nil {
891
- return err
874
+ if err = setGlobalSysVarFromBool (w .ctx , sess , variable . TiDBTTLJobEnable , args . EnableTTLJob ); err != nil {
875
+ return errors . Trace ( err )
892
876
}
893
877
}
894
878
895
- return setTiDBEnableAutoAnalyze (w .ctx , sess , autoAnalyzeValue )
879
+ if err := setGlobalSysVarFromBool (w .ctx , sess , variable .TiDBEnableAutoAnalyze , args .EnableAutoAnalyze ); err != nil {
880
+ return errors .Trace (err )
881
+ }
882
+
883
+ return nil
896
884
})
897
885
if err != nil {
898
- return err
886
+ return errors . Trace ( err )
899
887
}
900
888
901
889
return nil
0 commit comments