Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions docs/agents/ddl/02-job-lifecycle.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,12 @@ Rolling back the DDL transaction does not undo changes a step has made in PD.
Affinity groups follow two rules:

- Groups that the new metadata replaces stay until the job commits as done.
TRUNCATE TABLE and ALTER TABLE ... AFFINITY delete the old groups in the
done-job branch of `transitOneJobStep`.
`ALTER TABLE ... AFFINITY` deletes the old groups in the done-job branch of
`transitOneJobStep`. `DROP TABLE` (including materialized views and logs),
`DROP DATABASE`, `TRUNCATE TABLE`, `TRUNCATE PARTITION`, and materialized view
out-of-place cutover record the retired group IDs in `OldAffinityGroupIDs` on
the finished job arguments so they remain intact through the GC window and
`GCWorker.doGCAffinityGroups` deletes them in PD after the GC safe point.
- When a CREATE TABLE, materialized view shadow creation, batch create,
TRUNCATE TABLE, TRUNCATE PARTITION, or ALTER TABLE ... AFFINITY job ends
cancelled, `deleteAffinityGroupsOfCancelledJob` runs before cancellation commits. It
Expand All @@ -95,7 +99,8 @@ Affinity groups follow two rules:
error count limit.
PD deletion is best effort and uses a 10-second timeout from the DDL owner's
context. If deletion fails or times out, the worker logs the failure and
continues cancellation, so unused groups may remain in PD.
continues cancellation; any leftover orphan groups in PD are later reclaimed
by the DDL owner's background affinity reconciler (`pkg/ddl/affinity_reconcile.go`).

## Schema version + diff and cluster-wide synchronization

Expand Down
2 changes: 2 additions & 0 deletions pkg/ddl/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ go_library(
srcs = [
"add_column.go",
"affinity.go",
"affinity_reconcile.go",
"backfill_metrics.go",
"backfilling.go",
"backfilling_clean_s3.go",
Expand Down Expand Up @@ -257,6 +258,7 @@ go_test(
timeout = "moderate",
srcs = [
"affinity_internal_test.go",
"affinity_reconcile_test.go",
"affinity_test.go",
"attributes_sql_test.go",
"backfill_metrics_test.go",
Expand Down
238 changes: 213 additions & 25 deletions pkg/ddl/affinity.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,75 @@ package ddl
import (
"context"
"fmt"
"slices"
"strconv"
"strings"
"time"

"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/pkg/ddl/label"
"github.com/pingcap/tidb/pkg/ddl/logutil"
"github.com/pingcap/tidb/pkg/domain/affinity"
"github.com/pingcap/tidb/pkg/kv"
"github.com/pingcap/tidb/pkg/meta/model"
"github.com/pingcap/tidb/pkg/parser/ast"
"github.com/pingcap/tidb/pkg/sessionctx/vardef"
"github.com/pingcap/tidb/pkg/tablecodec"
tikv "github.com/tikv/client-go/v2/tikv"
pdhttp "github.com/tikv/pd/client/http"
"go.uber.org/zap"
)

const affinityCleanupTimeout = 10 * time.Second
const (
affinityCleanupTimeout = 10 * time.Second

// Bound OldAffinityGroupIDs persisted in a single DDL job row so large
// operations such as DROP DATABASE cannot exceed kv.TxnEntrySizeLimit
// (kv:8025). Any overflow groups are cleaned up by affinityReconciler once
// GC removes their mysql.gc_delete_range rows.
maxPersistedAffinityGroupIDs = 1024
maxPersistedAffinityGroupBytes = 32 * 1024
ddlJobRowBaseOverheadBytes = 1024
ddlJobEntrySafetyMarginBytes = 1024
ddlJobAffinityJSONEnvelope = 64
)

// affinityPDGate serializes PD affinity group mutations on the DDL owner
// between affinity-changing DDL execution and the background affinityReconciler.
type affinityPDGate struct {
sem chan struct{}
}

func newAffinityPDGate() *affinityPDGate {
return &affinityPDGate{sem: make(chan struct{}, 1)}
}

func (g *affinityPDGate) lock(ctx context.Context) error {
if g == nil {
return nil
}
if ctx != nil && ctx.Err() != nil {
return ctx.Err()
}
if ctx == nil {
g.sem <- struct{}{}
return nil
}
select {
case g.sem <- struct{}{}:
return nil
case <-ctx.Done():
return ctx.Err()
}
}

func (g *affinityPDGate) unlock() {
if g == nil {
return
}
<-g.sem
}

// GetTableAffinityGroupID returns the affinity group ID for a table, including
// the keyspace ID when the table uses a NextGen keyspace codec.
Expand All @@ -55,6 +109,44 @@ func GetPartitionAffinityGroupID(codec tikv.Codec, tableID, partitionID int64) s
return fmt.Sprintf("_tidb_pt_%d_p%d", tableID, partitionID)
}

// ParseAffinityGroupID strictly parses an affinity group ID belonging to the
// namespace of codec (Classic or the current NextGen keyspace).
// It rejects IDs from other keyspaces, Classic IDs under NextGen, NextGen IDs
// under Classic, non-_tidb_ IDs, non-positive IDs, leading zeros, and trailing data.
func ParseAffinityGroupID(codec tikv.Codec, groupID string) (tableID, partitionID int64, isPartition, ok bool) {
prefix := "_tidb_"
if label.UseKeyspaceAwareRules(codec) {
prefix = fmt.Sprintf("_tidb_ks%d_", codec.GetKeyspaceID())
}
rest, hasPrefix := strings.CutPrefix(groupID, prefix)
if !hasPrefix {
return 0, 0, false, false
}

if rawTableID, isTable := strings.CutPrefix(rest, "t_"); isTable {
tid, err := strconv.ParseInt(rawTableID, 10, 64)
if err != nil || tid <= 0 || GetTableAffinityGroupID(codec, tid) != groupID {
return 0, 0, false, false
}
return tid, 0, false, true
}

if rawPart, isPart := strings.CutPrefix(rest, "pt_"); isPart {
rawTableID, rawPartID, found := strings.Cut(rawPart, "_p")
if !found {
return 0, 0, false, false
}
tid, err1 := strconv.ParseInt(rawTableID, 10, 64)
pid, err2 := strconv.ParseInt(rawPartID, 10, 64)
if err1 != nil || err2 != nil || tid <= 0 || pid <= 0 || GetPartitionAffinityGroupID(codec, tid, pid) != groupID {
return 0, 0, false, false
}
return tid, pid, true, true
}

return 0, 0, false, false
}

func buildAffinityGroupKeyRange(codec tikv.Codec, physicalID int64) pdhttp.AffinityGroupKeyRange {
startKey := tablecodec.EncodeTablePrefix(physicalID)
endKey := tablecodec.EncodeTablePrefix(physicalID + 1)
Expand Down Expand Up @@ -142,6 +234,12 @@ func createTableAffinityGroupsInPD(jobCtx *jobContext, tblInfo *model.TableInfo,
return nil
}

failpoint.InjectCall("beforeAffinityDDLPDOp")
if err := jobCtx.affinityPDGate.lock(ctx); err != nil {
return errors.Trace(err)
}
defer jobCtx.affinityPDGate.unlock()

logutil.DDLLogger().Info("creating affinity groups in PD",
zap.Int64("tableID", tblInfo.ID),
zap.Strings("groupIDs", collectAffinityGroupIDs(groups)))
Expand All @@ -152,21 +250,30 @@ func createTableAffinityGroupsInPD(jobCtx *jobContext, tblInfo *model.TableInfo,
return nil
}

// deleteTableAffinityGroupsInPD deletes affinity groups for a table in PD.
// This is a best-effort cleanup operation. Failures are logged but the operation continues.
// TODO: add gc for unused affinity groups, which is similar to the placement rules.
func deleteTableAffinityGroupsInPD(jobCtx *jobContext, tblInfo *model.TableInfo, partitionDefs []model.PartitionDefinition) error {
ctx := jobCtx.stepCtx
if ctx == nil {
ctx = jobCtx.ctx
// collectAffinityGroupIDsForTable returns a sorted slice of affinity group IDs
// for tblInfo (or nil if tblInfo has no affinity configured).
func collectAffinityGroupIDsForTable(codec tikv.Codec, tblInfo *model.TableInfo, partitionDefs []model.PartitionDefinition) ([]string, error) {
if tblInfo == nil || tblInfo.Affinity == nil {
return nil, nil
}
return deleteTableAffinityGroups(ctx, jobCtx.store.GetCodec(), tblInfo, partitionDefs)
groups, err := buildAffinityGroupDefinitions(codec, tblInfo, partitionDefs)
if err != nil {
return nil, errors.Trace(err)
}
ids := collectAffinityGroupIDs(groups)
slices.Sort(ids)
return ids, nil
}

func deleteTableAffinityGroupsWithTimeout(jobCtx *jobContext, tblInfo *model.TableInfo, partitionDefs []model.PartitionDefinition) error {
// Rollback and post-commit cleanup can run after the step context is canceled.
ctx, cancel := context.WithTimeout(jobCtx.ctx, affinityCleanupTimeout)
defer cancel()
failpoint.InjectCall("beforeAffinityDDLPDOp")
if err := jobCtx.affinityPDGate.lock(ctx); err != nil {
return errors.Trace(err)
}
defer jobCtx.affinityPDGate.unlock()
return deleteTableAffinityGroups(ctx, jobCtx.store.GetCodec(), tblInfo, partitionDefs)
}

Expand All @@ -188,41 +295,117 @@ func partitionDefinitionsFromIDs(ids []int64) []model.PartitionDefinition {
return definitions
}

// collectTableAffinityGroupIDs adds the affinity group IDs of a table to groupIDs.
func collectTableAffinityGroupIDs(
// collectTableAffinityGroupIDsCapped adds the affinity group IDs of a table to
// groupIDs up to limit entries. It returns truncated=true when any group ID was
// omitted due to limit.
func collectTableAffinityGroupIDsCapped(
codec tikv.Codec,
tblInfo *model.TableInfo,
groupIDs map[string]struct{},
) error {
limit int,
) (bool, error) {
if tblInfo == nil || tblInfo.Affinity == nil {
return false, nil
}
if limit > 0 && len(groupIDs) >= limit {
return true, nil
}
groups, err := buildAffinityGroupDefinitions(codec, tblInfo, nil)
if err != nil {
return errors.Trace(err)
return false, errors.Trace(err)
}
for id := range groups {
if limit <= 0 || len(groupIDs)+len(groups) <= limit {
for id := range groups {
groupIDs[id] = struct{}{}
}
return false, nil
}
ids := collectAffinityGroupIDs(groups)
slices.Sort(ids)
truncated := false
for _, id := range ids {
if _, exists := groupIDs[id]; exists {
continue
}
if len(groupIDs) >= limit {
truncated = true
break
}
groupIDs[id] = struct{}{}
}
return nil
return truncated, nil
}

func effectiveTxnEntrySizeLimit() uint64 {
limit := kv.TxnEntrySizeLimit.Load()
if varLimit := vardef.TxnEntrySizeLimit.Load(); varLimit > 0 && (limit == 0 || varLimit < limit) {
limit = varLimit
}
return limit
}

func truncateAffinityGroupIDs(groupIDs []string, maxCount, maxBytes int) []string {
if len(groupIDs) == 0 || maxCount <= 0 || maxBytes <= 0 {
return nil
}
usedBytes := 0
keep := 0
for _, id := range groupIDs {
if keep >= maxCount {
break
}
// Each ASCII group ID encodes in JSON as `"id",` (len(id) + 3 bytes).
elemBytes := len(id) + 3
if usedBytes+elemBytes > maxBytes {
break
}
usedBytes += elemBytes
keep++
}
if keep == 0 {
return nil
}
return groupIDs[:keep]
}

// batchDeleteTableAffinityGroups deletes affinity groups for multiple tables in PD.
// This is used for DROP DATABASE to clean up all table affinity groups at once.
// Returns error to let the caller decide whether to continue or fail.
func batchDeleteTableAffinityGroups(jobCtx *jobContext, groupIDs map[string]struct{}) error {
// boundOldAffinityGroupIDsForJob returns a prefix of groupIDs that fits safely
// within both the static cap and the remaining KV entry-size headroom for job.
// Callers must pass job after populating its finished/base args with
// OldAffinityGroupIDs left empty so the base encoded size is measured accurately.
func boundOldAffinityGroupIDsForJob(job *model.Job, groupIDs []string) []string {
if len(groupIDs) == 0 {
return nil
}
entryLimit := effectiveTxnEntrySizeLimit()
if job == nil || entryLimit == 0 {
return truncateAffinityGroupIDs(groupIDs, maxPersistedAffinityGroupIDs, maxPersistedAffinityGroupBytes)
}

ctx := jobCtx.stepCtx
if ctx == nil {
ctx = jobCtx.ctx
origRawArgs := job.RawArgs
baseBytes, err := job.Encode(true)
job.RawArgs = origRawArgs
if err != nil {
return truncateAffinityGroupIDs(groupIDs, maxPersistedAffinityGroupIDs, maxPersistedAffinityGroupBytes)
}
rowOverhead := uint64(ddlJobRowBaseOverheadBytes + ddlJobAffinityJSONEnvelope + len(job.SchemaName) + len(job.TableName))
used := uint64(len(baseBytes)) + rowOverhead
if entryLimit <= used+ddlJobEntrySafetyMarginBytes {
return nil
}
budget := min(entryLimit-used-ddlJobEntrySafetyMarginBytes, uint64(maxPersistedAffinityGroupBytes))
return truncateAffinityGroupIDs(groupIDs, maxPersistedAffinityGroupIDs, int(budget))
}

func sortedAffinityGroupIDs(groupIDs map[string]struct{}) []string {
if len(groupIDs) == 0 {
return nil
}
ids := make([]string, 0, len(groupIDs))
for id := range groupIDs {
ids = append(ids, id)
}

logutil.DDLLogger().Info("deleting affinity groups for batch tables", zap.Strings("groupIDs", ids))
return affinity.DeleteGroupsWithRetry(ctx, ids)
slices.Sort(ids)
return ids
}

// BuildAffinityGroupDefinitionsForTest is exported for testing.
Expand All @@ -233,3 +416,8 @@ func BuildAffinityGroupDefinitionsForTest(
) (map[string][]pdhttp.AffinityGroupKeyRange, error) {
return buildAffinityGroupDefinitions(codec, tblInfo, partitionDefs)
}

// BoundOldAffinityGroupIDsForJobForTest is exported for testing.
func BoundOldAffinityGroupIDsForJobForTest(job *model.Job, groupIDs []string) []string {
return boundOldAffinityGroupIDsForJob(job, groupIDs)
}
Loading
Loading