/
gc_manager.go
138 lines (127 loc) · 4.39 KB
/
gc_manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright 2021 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package gc
import (
"context"
"time"
"github.com/pingcap/failpoint"
"github.com/pingcap/log"
"github.com/pingcap/tiflow/cdc/model"
"github.com/pingcap/tiflow/pkg/config"
cerror "github.com/pingcap/tiflow/pkg/errors"
"github.com/pingcap/tiflow/pkg/pdutil"
"github.com/tikv/client-go/v2/oracle"
pd "github.com/tikv/pd/client"
"go.uber.org/atomic"
"go.uber.org/zap"
)
// gcSafepointUpdateInterval is the minimum interval that CDC can update gc safepoint
var gcSafepointUpdateInterval = 1 * time.Minute
// Manager is an interface for gc manager
type Manager interface {
// TryUpdateGCSafePoint tries to update TiCDC service GC safepoint.
// Manager may skip update when it thinks it is too frequent.
// Set `forceUpdate` to force Manager update.
TryUpdateGCSafePoint(ctx context.Context, checkpointTs model.Ts, forceUpdate bool) error
CheckStaleCheckpointTs(ctx context.Context, changefeedID model.ChangeFeedID, checkpointTs model.Ts) error
}
type gcManager struct {
gcServiceID string
pdClient pd.Client
pdClock pdutil.Clock
gcTTL int64
lastUpdatedTime time.Time
lastSucceededTime time.Time
lastSafePointTs atomic.Uint64
isTiCDCBlockGC atomic.Bool
}
// NewManager creates a new Manager.
func NewManager(gcServiceID string, pdClient pd.Client, pdClock pdutil.Clock) Manager {
serverConfig := config.GetGlobalServerConfig()
failpoint.Inject("InjectGcSafepointUpdateInterval", func(val failpoint.Value) {
gcSafepointUpdateInterval = time.Duration(val.(int) * int(time.Millisecond))
})
return &gcManager{
gcServiceID: gcServiceID,
pdClient: pdClient,
pdClock: pdClock,
lastSucceededTime: time.Now(),
gcTTL: serverConfig.GcTTL,
}
}
func (m *gcManager) TryUpdateGCSafePoint(
ctx context.Context, checkpointTs model.Ts, forceUpdate bool,
) error {
if time.Since(m.lastUpdatedTime) < gcSafepointUpdateInterval && !forceUpdate {
return nil
}
m.lastUpdatedTime = time.Now()
actual, err := SetServiceGCSafepoint(
ctx, m.pdClient, m.gcServiceID, m.gcTTL, checkpointTs)
if err != nil {
log.Warn("updateGCSafePoint failed",
zap.Uint64("safePointTs", checkpointTs),
zap.Error(err))
if time.Since(m.lastSucceededTime) >= time.Second*time.Duration(m.gcTTL) {
return cerror.ErrUpdateServiceSafepointFailed.Wrap(err)
}
return nil
}
failpoint.Inject("InjectActualGCSafePoint", func(val failpoint.Value) {
actual = uint64(val.(int))
})
if actual == checkpointTs {
log.Info("update gc safe point success", zap.Uint64("gcSafePointTs", checkpointTs))
}
if actual > checkpointTs {
log.Warn("update gc safe point failed, the gc safe point is larger than checkpointTs",
zap.Uint64("actual", actual), zap.Uint64("checkpointTs", checkpointTs))
}
// if the min checkpoint ts is equal to the current gc safe point, it
// means that the service gc safe point set by TiCDC is the min service
// gc safe point
m.isTiCDCBlockGC.Store(actual == checkpointTs)
m.lastSafePointTs.Store(actual)
m.lastSucceededTime = time.Now()
minServiceGCSafePointGauge.Set(float64(oracle.ExtractPhysical(actual)))
cdcGCSafePointGauge.Set(float64(oracle.ExtractPhysical(checkpointTs)))
return nil
}
func (m *gcManager) CheckStaleCheckpointTs(
ctx context.Context, changefeedID model.ChangeFeedID, checkpointTs model.Ts,
) error {
gcSafepointUpperBound := checkpointTs - 1
if m.isTiCDCBlockGC.Load() {
pdTime := m.pdClock.CurrentTime()
if pdTime.Sub(
oracle.GetTimeFromTS(gcSafepointUpperBound),
) > time.Duration(m.gcTTL)*time.Second {
return cerror.ErrGCTTLExceeded.
GenWithStackByArgs(
checkpointTs,
changefeedID,
)
}
} else {
// if `isTiCDCBlockGC` is false, it means there is another service gc
// point less than the min checkpoint ts.
if gcSafepointUpperBound < m.lastSafePointTs.Load() {
return cerror.ErrSnapshotLostByGC.
GenWithStackByArgs(
checkpointTs,
m.lastSafePointTs.Load(),
)
}
}
return nil
}