15
15
package ddl
16
16
17
17
import (
18
- "bytes"
19
18
"context"
20
19
"encoding/hex"
21
20
"encoding/json"
22
21
"fmt"
23
22
"runtime"
24
- "slices"
25
23
"strconv"
26
24
"strings"
27
25
"sync/atomic"
@@ -658,102 +656,6 @@ func (d *ddl) getTableByTxn(r autoid.Requirement, schemaID, tableID int64) (*mod
658
656
return dbInfo , tbl , err
659
657
}
660
658
661
- const (
662
- addDDLJobSQL = "insert into mysql.tidb_ddl_job(job_id, reorg, schema_ids, table_ids, job_meta, type, processing) values"
663
- updateDDLJobSQL = "update mysql.tidb_ddl_job set job_meta = %s where job_id = %d"
664
- )
665
-
666
- func insertDDLJobs2Table (ctx context.Context , se * sess.Session , jobWs ... * JobWrapper ) error {
667
- failpoint .Inject ("mockAddBatchDDLJobsErr" , func (val failpoint.Value ) {
668
- if val .(bool ) {
669
- failpoint .Return (errors .Errorf ("mockAddBatchDDLJobsErr" ))
670
- }
671
- })
672
- if len (jobWs ) == 0 {
673
- return nil
674
- }
675
- var sql bytes.Buffer
676
- sql .WriteString (addDDLJobSQL )
677
- for i , jobW := range jobWs {
678
- // TODO remove this check when all job type pass args in this way.
679
- if jobW .JobArgs != nil {
680
- jobW .FillArgs (jobW .JobArgs )
681
- }
682
- injectModifyJobArgFailPoint (jobWs )
683
- b , err := jobW .Encode (true )
684
- if err != nil {
685
- return err
686
- }
687
- if i != 0 {
688
- sql .WriteString ("," )
689
- }
690
- fmt .Fprintf (& sql , "(%d, %t, %s, %s, %s, %d, %t)" , jobW .ID , jobW .MayNeedReorg (),
691
- strconv .Quote (job2SchemaIDs (jobW )), strconv .Quote (job2TableIDs (jobW )),
692
- util .WrapKey2String (b ), jobW .Type , jobW .Started ())
693
- }
694
- se .GetSessionVars ().SetDiskFullOpt (kvrpcpb .DiskFullOpt_AllowedOnAlmostFull )
695
- _ , err := se .Execute (ctx , sql .String (), "insert_job" )
696
- logutil .DDLLogger ().Debug ("add job to mysql.tidb_ddl_job table" , zap .String ("sql" , sql .String ()))
697
- return errors .Trace (err )
698
- }
699
-
700
- func makeStringForIDs (ids []int64 ) string {
701
- set := make (map [int64 ]struct {}, len (ids ))
702
- for _ , id := range ids {
703
- set [id ] = struct {}{}
704
- }
705
-
706
- s := make ([]string , 0 , len (set ))
707
- for id := range set {
708
- s = append (s , strconv .FormatInt (id , 10 ))
709
- }
710
- slices .Sort (s )
711
- return strings .Join (s , "," )
712
- }
713
-
714
- func job2SchemaIDs (jobW * JobWrapper ) string {
715
- switch jobW .Type {
716
- case model .ActionRenameTables :
717
- var ids []int64
718
- arg := jobW .JobArgs .(* model.RenameTablesArgs )
719
- ids = make ([]int64 , 0 , len (arg .RenameTableInfos )* 2 )
720
- for _ , info := range arg .RenameTableInfos {
721
- ids = append (ids , info .OldSchemaID , info .NewSchemaID )
722
- }
723
- return makeStringForIDs (ids )
724
- case model .ActionRenameTable :
725
- oldSchemaID := jobW .JobArgs .(* model.RenameTableArgs ).OldSchemaID
726
- ids := []int64 {oldSchemaID , jobW .SchemaID }
727
- return makeStringForIDs (ids )
728
- case model .ActionExchangeTablePartition :
729
- args := jobW .JobArgs .(* model.ExchangeTablePartitionArgs )
730
- return makeStringForIDs ([]int64 {jobW .SchemaID , args .PTSchemaID })
731
- default :
732
- return strconv .FormatInt (jobW .SchemaID , 10 )
733
- }
734
- }
735
-
736
- func job2TableIDs (jobW * JobWrapper ) string {
737
- switch jobW .Type {
738
- case model .ActionRenameTables :
739
- var ids []int64
740
- arg := jobW .JobArgs .(* model.RenameTablesArgs )
741
- ids = make ([]int64 , 0 , len (arg .RenameTableInfos ))
742
- for _ , info := range arg .RenameTableInfos {
743
- ids = append (ids , info .TableID )
744
- }
745
- return makeStringForIDs (ids )
746
- case model .ActionExchangeTablePartition :
747
- args := jobW .JobArgs .(* model.ExchangeTablePartitionArgs )
748
- return makeStringForIDs ([]int64 {jobW .TableID , args .PTTableID })
749
- case model .ActionTruncateTable :
750
- newTableID := jobW .JobArgs .(* model.TruncateTableArgs ).NewTableID
751
- return strconv .FormatInt (jobW .TableID , 10 ) + "," + strconv .FormatInt (newTableID , 10 )
752
- default :
753
- return strconv .FormatInt (jobW .TableID , 10 )
754
- }
755
- }
756
-
757
659
func updateDDLJob2Table (
758
660
ctx context.Context ,
759
661
se * sess.Session ,
@@ -764,7 +666,7 @@ func updateDDLJob2Table(
764
666
if err != nil {
765
667
return err
766
668
}
767
- sql := fmt .Sprintf (updateDDLJobSQL , util .WrapKey2String (b ), job .ID )
669
+ sql := fmt .Sprintf ("update mysql.tidb_ddl_job set job_meta = %s where job_id = %d" , util .WrapKey2String (b ), job .ID )
768
670
_ , err = se .Execute (ctx , sql , "update_job" )
769
671
return errors .Trace (err )
770
672
}
0 commit comments