|
| 1 | +// Copyright 2025 PingCAP, Inc. Licensed under Apache-2.0. |
| 2 | + |
| 3 | +package gc |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "os" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/pingcap/errors" |
| 11 | + "github.com/pingcap/failpoint" |
| 12 | + "github.com/pingcap/log" |
| 13 | + pd "github.com/tikv/pd/client" |
| 14 | + "go.uber.org/zap" |
| 15 | +) |
| 16 | + |
| 17 | +// globalManager implements Manager using the global GC safepoint mechanism. |
| 18 | +// It uses the deprecated pd.Client.UpdateServiceGCSafePoint API for backward compatibility. |
| 19 | +type globalManager struct { |
| 20 | + pdClient pd.Client |
| 21 | +} |
| 22 | + |
| 23 | +// Ensure globalManager implements Manager interface. |
| 24 | +var _ Manager = (*globalManager)(nil) |
| 25 | + |
| 26 | +// newGlobalManager creates a new globalManager instance. |
| 27 | +func newGlobalManager(pdClient pd.Client) *globalManager { |
| 28 | + return &globalManager{ |
| 29 | + pdClient: pdClient, |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +// GetGCSafePoint returns the current GC safe point. |
| 34 | +func (m *globalManager) GetGCSafePoint(ctx context.Context) (uint64, error) { |
| 35 | + safePoint, err := m.pdClient.UpdateGCSafePoint(ctx, 0) |
| 36 | + if err != nil { |
| 37 | + return 0, errors.Trace(err) |
| 38 | + } |
| 39 | + return safePoint, nil |
| 40 | +} |
| 41 | + |
| 42 | +// SetServiceSafePoint sets the global service safe point using the deprecated API. |
| 43 | +// This maintains backward compatibility with existing BR behavior. |
| 44 | +func (m *globalManager) SetServiceSafePoint(ctx context.Context, sp BRServiceSafePoint) error { |
| 45 | + log.Debug("update PD safePoint limit with TTL", zap.Object("safePoint", sp)) |
| 46 | + |
| 47 | + lastSafePoint, err := m.pdClient.UpdateServiceGCSafePoint(ctx, sp.ID, sp.TTL, sp.BackupTS-1) |
| 48 | + if err == nil { |
| 49 | + // Integration tests use this to distinguish global vs keyspace GC protection. |
| 50 | + failpoint.Inject("hint-gc-global-set-safepoint", func(v failpoint.Value) { |
| 51 | + if sigFile, ok := v.(string); ok { |
| 52 | + // Write the service ID so the test can match PD output precisely. |
| 53 | + if writeErr := os.WriteFile(sigFile, []byte(sp.ID), 0o644); writeErr != nil { |
| 54 | + log.Warn("failed to write failpoint signal file", zap.Error(writeErr), zap.String("file", sigFile)) |
| 55 | + } |
| 56 | + } |
| 57 | + // Provide a small observation window for test scripts. |
| 58 | + time.Sleep(3 * time.Second) |
| 59 | + }) |
| 60 | + } |
| 61 | + if lastSafePoint > sp.BackupTS-1 && sp.TTL > 0 { |
| 62 | + log.Warn("service GC safe point lost, we may fail to back up if GC lifetime isn't long enough", |
| 63 | + zap.Uint64("lastSafePoint", lastSafePoint), |
| 64 | + zap.Object("safePoint", sp), |
| 65 | + ) |
| 66 | + } |
| 67 | + return errors.Trace(err) |
| 68 | +} |
| 69 | + |
| 70 | +// DeleteServiceSafePoint removes the service safe point by setting TTL to 0. |
| 71 | +func (m *globalManager) DeleteServiceSafePoint(ctx context.Context, sp BRServiceSafePoint) error { |
| 72 | + // Setting TTL to 0 effectively removes the service safe point |
| 73 | + _, err := m.pdClient.UpdateServiceGCSafePoint(ctx, sp.ID, 0, 0) |
| 74 | + if err == nil { |
| 75 | + failpoint.Inject("hint-gc-global-delete-safepoint", func(v failpoint.Value) { |
| 76 | + if sigFile, ok := v.(string); ok { |
| 77 | + if writeErr := os.WriteFile(sigFile, []byte(sp.ID), 0o644); writeErr != nil { |
| 78 | + log.Warn("failed to write failpoint signal file", zap.Error(writeErr), zap.String("file", sigFile)) |
| 79 | + } |
| 80 | + } |
| 81 | + }) |
| 82 | + } |
| 83 | + return errors.Trace(err) |
| 84 | +} |
0 commit comments