|
| 1 | +// Copyright 2026 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 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package ddl |
| 16 | + |
| 17 | +import ( |
| 18 | + "sort" |
| 19 | + |
| 20 | + ddlutil "github.com/pingcap/tidb/ddl/util" |
| 21 | + "github.com/pingcap/tidb/domain/infosync" |
| 22 | + "github.com/pingcap/tidb/infoschema" |
| 23 | + "github.com/pingcap/tidb/meta/autoid" |
| 24 | + "github.com/pingcap/tidb/parser/model" |
| 25 | + "github.com/pingcap/tidb/sessionctx/variable" |
| 26 | + "github.com/pingcap/tidb/tablecodec" |
| 27 | +) |
| 28 | + |
| 29 | +// GetForceMergeRangesForGCDeleteRange builds the PD force-merge ranges that |
| 30 | +// should be reported for one gc_delete_range task. |
| 31 | +func GetForceMergeRangesForGCDeleteRange( |
| 32 | + historyJob *model.Job, |
| 33 | + dr ddlutil.DelRangeTask, |
| 34 | + gcLogicalTableCache map[int64]struct{}, |
| 35 | +) []infosync.ForceMergeKeyRange { |
| 36 | + if !variable.EnableDropTableForceMerge.Load() || historyJob == nil { |
| 37 | + return nil |
| 38 | + } |
| 39 | + if !supportForceMergeForGCDeleteRange(historyJob.Type) { |
| 40 | + return nil |
| 41 | + } |
| 42 | + |
| 43 | + var tblInfo *model.TableInfo |
| 44 | + if historyJob.BinlogInfo != nil { |
| 45 | + tblInfo = historyJob.BinlogInfo.TableInfo |
| 46 | + } |
| 47 | + if shouldSkipForceMerge(historyJob, tblInfo) { |
| 48 | + return nil |
| 49 | + } |
| 50 | + |
| 51 | + // Reuse the gc_delete_range task range directly for the physical table/partition |
| 52 | + // that has just been deleted. |
| 53 | + ranges := []infosync.ForceMergeKeyRange{buildForceMergeKeyRange(dr.StartKey, dr.EndKey)} |
| 54 | + if shouldReportLogicalTableRange(historyJob, tblInfo, gcLogicalTableCache) { |
| 55 | + // DROP/TRUNCATE on a partitioned table only creates gc_delete_range tasks for |
| 56 | + // physical partitions. Append the logical table range once as well so PD can |
| 57 | + // force merge the partitioned table's global index keyspace. |
| 58 | + ranges = append(ranges, buildForceMergeTableKeyRange(historyJob.TableID)) |
| 59 | + } |
| 60 | + return ranges |
| 61 | +} |
| 62 | + |
| 63 | +func supportForceMergeForGCDeleteRange(actionType model.ActionType) bool { |
| 64 | + switch actionType { |
| 65 | + case model.ActionDropSchema, |
| 66 | + model.ActionDropTable, model.ActionTruncateTable, |
| 67 | + model.ActionDropTablePartition, model.ActionTruncateTablePartition: |
| 68 | + return true |
| 69 | + } |
| 70 | + return false |
| 71 | +} |
| 72 | + |
| 73 | +func shouldSkipForceMerge(historyJob *model.Job, tblInfo *model.TableInfo) bool { |
| 74 | + if historyJob == nil { |
| 75 | + return true |
| 76 | + } |
| 77 | + |
| 78 | + tableName := historyJob.TableName |
| 79 | + if tblInfo != nil && tableName == "" { |
| 80 | + tableName = tblInfo.Name.L |
| 81 | + } |
| 82 | + return shouldSkipForceMergeTable(historyJob.SchemaName, tableName, tblInfo) |
| 83 | +} |
| 84 | + |
| 85 | +func shouldSkipForceMergeTable(schemaName string, tableName string, tblInfo *model.TableInfo) bool { |
| 86 | + if tblInfo != nil { |
| 87 | + if !tblInfo.IsBaseTable() { |
| 88 | + return true |
| 89 | + } |
| 90 | + if tblInfo.TempTableType != model.TempTableNone { |
| 91 | + return true |
| 92 | + } |
| 93 | + if tblInfo.ID&autoid.SystemSchemaIDFlag != 0 { |
| 94 | + return true |
| 95 | + } |
| 96 | + } |
| 97 | + return isSystemTable(schemaName, tableName) |
| 98 | +} |
| 99 | + |
| 100 | +func shouldReportLogicalTableRange( |
| 101 | + historyJob *model.Job, |
| 102 | + tblInfo *model.TableInfo, |
| 103 | + gcLogicalTableCache map[int64]struct{}, |
| 104 | +) bool { |
| 105 | + // Partition-level gc_delete_range tasks do not cover the logical table ID range. |
| 106 | + // That range must be reported separately for DROP/TRUNCATE of partitioned tables |
| 107 | + // because global indexes are stored under the logical table ID prefix. |
| 108 | + if historyJob == nil || historyJob.TableID <= 0 || tblInfo == nil || tblInfo.GetPartitionInfo() == nil { |
| 109 | + return false |
| 110 | + } |
| 111 | + if historyJob.Type != model.ActionDropTable && historyJob.Type != model.ActionTruncateTable { |
| 112 | + return false |
| 113 | + } |
| 114 | + if gcLogicalTableCache != nil { |
| 115 | + if _, ok := gcLogicalTableCache[historyJob.TableID]; ok { |
| 116 | + return false |
| 117 | + } |
| 118 | + gcLogicalTableCache[historyJob.TableID] = struct{}{} |
| 119 | + } |
| 120 | + return true |
| 121 | +} |
| 122 | + |
| 123 | +func buildForceMergeKeyRange(startKey []byte, endKey []byte) infosync.ForceMergeKeyRange { |
| 124 | + return infosync.ForceMergeKeyRange{ |
| 125 | + StartKey: append([]byte(nil), startKey...), |
| 126 | + EndKey: append([]byte(nil), endKey...), |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +// GetMergeEmptyRegionsKeyRanges scans the current infoschema in |
| 131 | +// [minTableID, maxTableID) and returns the missing table ID keyspace ranges. |
| 132 | +func GetMergeEmptyRegionsKeyRanges( |
| 133 | + is infoschema.InfoSchema, |
| 134 | + minTableID int64, |
| 135 | +) (maxTableID int64, ranges []infosync.ForceMergeKeyRange) { |
| 136 | + if is == nil { |
| 137 | + return 0, nil |
| 138 | + } |
| 139 | + if minTableID < 1 { |
| 140 | + minTableID = 1 |
| 141 | + } |
| 142 | + |
| 143 | + occupiedTableIDs, maxTableID := collectMergeEmptyRegionsOccupiedTableIDs(is, minTableID) |
| 144 | + if maxTableID == 0 || minTableID >= maxTableID { |
| 145 | + return maxTableID, nil |
| 146 | + } |
| 147 | + return maxTableID, buildMergeEmptyRegionsKeyRanges(occupiedTableIDs, minTableID, maxTableID) |
| 148 | +} |
| 149 | + |
| 150 | +func collectMergeEmptyRegionsOccupiedTableIDs(is infoschema.InfoSchema, minTableID int64) ([]int64, int64) { |
| 151 | + occupiedTableIDs := make([]int64, 0) |
| 152 | + maxTableID := int64(0) |
| 153 | + for _, dbInfo := range is.AllSchemas() { |
| 154 | + schemaName := dbInfo.Name.L |
| 155 | + for _, tbl := range is.SchemaTables(dbInfo.Name) { |
| 156 | + tblInfo := tbl.Meta() |
| 157 | + if !mergeEmptyRegionsTableOccupiesKeyspace(tblInfo) { |
| 158 | + continue |
| 159 | + } |
| 160 | + |
| 161 | + tableIDs := appendMergeEmptyRegionsTableIDs(nil, tblInfo) |
| 162 | + for _, tableID := range tableIDs { |
| 163 | + if tableID >= minTableID { |
| 164 | + occupiedTableIDs = append(occupiedTableIDs, tableID) |
| 165 | + } |
| 166 | + } |
| 167 | + // Keep skipped live tables as occupied keyspace so the background scan |
| 168 | + // never force merges through system or temporary table ranges in the |
| 169 | + // current scan window. |
| 170 | + if shouldSkipForceMergeTable(schemaName, tblInfo.Name.L, tblInfo) { |
| 171 | + continue |
| 172 | + } |
| 173 | + for _, tableID := range tableIDs { |
| 174 | + if tableID > maxTableID { |
| 175 | + maxTableID = tableID |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + return occupiedTableIDs, maxTableID |
| 181 | +} |
| 182 | + |
| 183 | +func buildMergeEmptyRegionsKeyRanges( |
| 184 | + occupiedTableIDs []int64, |
| 185 | + minTableID int64, |
| 186 | + maxTableID int64, |
| 187 | +) []infosync.ForceMergeKeyRange { |
| 188 | + if minTableID < 1 || minTableID >= maxTableID { |
| 189 | + return nil |
| 190 | + } |
| 191 | + |
| 192 | + sort.Slice(occupiedTableIDs, func(i, j int) bool { |
| 193 | + return occupiedTableIDs[i] < occupiedTableIDs[j] |
| 194 | + }) |
| 195 | + |
| 196 | + ranges := make([]infosync.ForceMergeKeyRange, 0) |
| 197 | + nextRangeStart := minTableID |
| 198 | + for _, occupiedTableID := range occupiedTableIDs { |
| 199 | + if occupiedTableID < nextRangeStart { |
| 200 | + continue |
| 201 | + } |
| 202 | + if occupiedTableID >= maxTableID { |
| 203 | + break |
| 204 | + } |
| 205 | + if occupiedTableID > nextRangeStart { |
| 206 | + ranges = append(ranges, buildMergeEmptyRegionsTableIDRange(nextRangeStart, occupiedTableID-1)) |
| 207 | + } |
| 208 | + nextRangeStart = occupiedTableID + 1 |
| 209 | + } |
| 210 | + if nextRangeStart < maxTableID { |
| 211 | + ranges = append(ranges, buildMergeEmptyRegionsTableIDRange(nextRangeStart, maxTableID-1)) |
| 212 | + } |
| 213 | + return ranges |
| 214 | +} |
| 215 | + |
| 216 | +func mergeEmptyRegionsTableOccupiesKeyspace(tblInfo *model.TableInfo) bool { |
| 217 | + return tblInfo != nil && tblInfo.IsBaseTable() |
| 218 | +} |
| 219 | + |
| 220 | +func appendMergeEmptyRegionsTableIDs(dst []int64, tblInfo *model.TableInfo) []int64 { |
| 221 | + if tblInfo == nil { |
| 222 | + return dst |
| 223 | + } |
| 224 | + |
| 225 | + pi := tblInfo.GetPartitionInfo() |
| 226 | + if pi != nil && len(pi.Definitions) > 0 { |
| 227 | + for _, def := range pi.Definitions { |
| 228 | + if def.ID > 0 { |
| 229 | + dst = append(dst, def.ID) |
| 230 | + } |
| 231 | + } |
| 232 | + // Partitioned table global indexes live under the logical table ID prefix. |
| 233 | + if hasGlobalIndex(tblInfo) && tblInfo.ID > 0 { |
| 234 | + dst = append(dst, tblInfo.ID) |
| 235 | + } |
| 236 | + return dst |
| 237 | + } |
| 238 | + if tblInfo.ID > 0 { |
| 239 | + dst = append(dst, tblInfo.ID) |
| 240 | + } |
| 241 | + return dst |
| 242 | +} |
| 243 | + |
| 244 | +func buildMergeEmptyRegionsTableIDRange(startTableID int64, endTableID int64) infosync.ForceMergeKeyRange { |
| 245 | + return infosync.ForceMergeKeyRange{ |
| 246 | + StartKey: tablecodec.EncodeTablePrefix(startTableID), |
| 247 | + EndKey: tablecodec.EncodeTablePrefix(endTableID + 1), |
| 248 | + } |
| 249 | +} |
| 250 | + |
| 251 | +func buildForceMergeTableKeyRange(tableID int64) infosync.ForceMergeKeyRange { |
| 252 | + return infosync.ForceMergeKeyRange{ |
| 253 | + StartKey: tablecodec.EncodeTablePrefix(tableID), |
| 254 | + EndKey: tablecodec.EncodeTablePrefix(tableID + 1), |
| 255 | + } |
| 256 | +} |
0 commit comments