github.com/pingcap/tidb/parser@v0.0.0-20231013125129-93a834a6bf8d/model/reorg.go (about) 1 // Copyright 2023 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package model 15 16 import ( 17 "encoding/json" 18 19 "github.com/pingcap/errors" 20 "github.com/pingcap/tidb/parser/mysql" 21 "github.com/pingcap/tidb/parser/terror" 22 ) 23 24 // DDLReorgMeta is meta info of DDL reorganization. 25 type DDLReorgMeta struct { 26 SQLMode mysql.SQLMode `json:"sql_mode"` 27 Warnings map[errors.ErrorID]*terror.Error `json:"warnings"` 28 WarningsCount map[errors.ErrorID]int64 `json:"warnings_count"` 29 Location *TimeZoneLocation `json:"location"` 30 ReorgTp ReorgType `json:"reorg_tp"` 31 IsDistReorg bool `json:"is_dist_reorg"` 32 ResourceGroupName string `json:"resource_group_name"` 33 } 34 35 // ReorgType indicates which process is used for the data reorganization. 36 type ReorgType int8 37 38 const ( 39 // ReorgTypeNone means the backfill task is not started yet. 40 ReorgTypeNone ReorgType = iota 41 // ReorgTypeTxn means the index records are backfill with transactions. 42 // All the index KVs are written through the transaction interface. 43 // This is the original backfill implementation. 44 ReorgTypeTxn 45 // ReorgTypeLitMerge means the index records are backfill with lightning. 46 // The index KVs are encoded to SST files and imported to the storage directly. 47 // The incremental index KVs written by DML are redirected to a temporary index. 48 // After the backfill is finished, the temporary index records are merged back to the original index. 49 ReorgTypeLitMerge 50 // ReorgTypeTxnMerge means backfill with transactions and merge incremental changes. 51 // The backfill index KVs are written through the transaction interface. 52 // The incremental index KVs written by DML are redirected to a temporary index. 53 // After the backfill is finished, the temporary index records are merged back to the original index. 54 ReorgTypeTxnMerge 55 ) 56 57 // NeedMergeProcess means the incremental changes need to be merged. 58 func (tp ReorgType) NeedMergeProcess() bool { 59 return tp == ReorgTypeLitMerge || tp == ReorgTypeTxnMerge 60 } 61 62 // String implements fmt.Stringer interface. 63 func (tp ReorgType) String() string { 64 switch tp { 65 case ReorgTypeTxn: 66 return "txn" 67 case ReorgTypeLitMerge: 68 return "ingest" 69 case ReorgTypeTxnMerge: 70 return "txn-merge" 71 } 72 return "" 73 } 74 75 // BackfillState is the state used by the backfill-merge process. 76 type BackfillState byte 77 78 const ( 79 // BackfillStateInapplicable means the backfill-merge process is not used. 80 BackfillStateInapplicable BackfillState = iota 81 // BackfillStateRunning is the state that the backfill process is running. 82 // In this state, the index's write and delete operations are redirected to a temporary index. 83 BackfillStateRunning 84 // BackfillStateReadyToMerge is the state that the temporary index's records are ready to be merged back 85 // to the origin index. 86 // In this state, the index's write and delete operations are copied to a temporary index. 87 // This state is used to make sure that all the TiDB instances are aware of the copy 88 // during the merge(BackfillStateMerging). 89 BackfillStateReadyToMerge 90 // BackfillStateMerging is the state that the temp index is merging back to the origin index. 91 // In this state, the index's write and delete operations are copied to a temporary index. 92 BackfillStateMerging 93 ) 94 95 // String implements fmt.Stringer interface. 96 func (s BackfillState) String() string { 97 switch s { 98 case BackfillStateRunning: 99 return "backfill state running" 100 case BackfillStateReadyToMerge: 101 return "backfill state ready to merge" 102 case BackfillStateMerging: 103 return "backfill state merging" 104 case BackfillStateInapplicable: 105 return "backfill state inapplicable" 106 default: 107 return "backfill state unknown" 108 } 109 } 110 111 // BackfillMeta is meta info of the backfill job. 112 type BackfillMeta struct { 113 IsUnique bool `json:"is_unique"` 114 EndInclude bool `json:"end_include"` 115 Error *terror.Error `json:"err"` 116 117 SQLMode mysql.SQLMode `json:"sql_mode"` 118 Warnings map[errors.ErrorID]*terror.Error `json:"warnings"` 119 WarningsCount map[errors.ErrorID]int64 `json:"warnings_count"` 120 Location *TimeZoneLocation `json:"location"` 121 ReorgTp ReorgType `json:"reorg_tp"` 122 RowCount int64 `json:"row_count"` 123 StartKey []byte `json:"start_key"` 124 EndKey []byte `json:"end_key"` 125 CurrKey []byte `json:"curr_key"` 126 *JobMeta `json:"job_meta"` 127 } 128 129 // Encode encodes BackfillMeta with json format. 130 func (bm *BackfillMeta) Encode() ([]byte, error) { 131 b, err := json.Marshal(bm) 132 return b, errors.Trace(err) 133 } 134 135 // Decode decodes BackfillMeta from the json buffer. 136 func (bm *BackfillMeta) Decode(b []byte) error { 137 err := json.Unmarshal(b, bm) 138 return errors.Trace(err) 139 }