Skip to content

Commit e11e603

Browse files
committed
simplify code
1 parent 7738343 commit e11e603

23 files changed

Lines changed: 355 additions & 411 deletions

‎coordinator/changefeed/changefeed.go‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ type Changefeed struct {
4646
// the heartbeatpb.MaintainerStatus is read only
4747
status *atomic.Pointer[heartbeatpb.MaintainerStatus]
4848

49-
meteringEpoch *atomic.Uint64
49+
meteringEpoch uint64
5050
meteringLastSeq atomic.Uint64
5151

5252
backoff *Backoff
@@ -306,8 +306,8 @@ func (c *Changefeed) GetLastSavedCheckPointTs() uint64 {
306306
return c.lastSavedCheckpointTs.Load()
307307
}
308308

309-
// SetMeteringEpochSource binds the shared coordinator metering epoch for AddMaintainerRequest.
310-
func (c *Changefeed) SetMeteringEpochSource(epoch *atomic.Uint64) {
309+
// SetMeteringEpoch sets the coordinator metering epoch included in AddMaintainerRequest.
310+
func (c *Changefeed) SetMeteringEpoch(epoch uint64) {
311311
c.meteringEpoch = epoch
312312
}
313313

@@ -331,10 +331,6 @@ func (c *Changefeed) NewAddMaintainerMessage(server node.ID) *messaging.TargetMe
331331
if status := c.GetStatus(); status != nil {
332332
checkpointTs = status.CheckpointTs
333333
}
334-
var meteringEpoch uint64
335-
if c.meteringEpoch != nil {
336-
meteringEpoch = c.meteringEpoch.Load()
337-
}
338334
return messaging.NewSingleTargetMessage(server,
339335
messaging.MaintainerManagerTopic,
340336
&heartbeatpb.AddMaintainerRequest{
@@ -344,7 +340,7 @@ func (c *Changefeed) NewAddMaintainerMessage(server node.ID) *messaging.TargetMe
344340
IsNewChangefeed: c.isNew.Load(),
345341
KeyspaceId: info.KeyspaceID,
346342
MaintainerEpoch: info.Epoch,
347-
MeteringEpoch: meteringEpoch,
343+
MeteringEpoch: c.meteringEpoch,
348344
MeteringLastSeq: c.meteringLastSeq.Load(),
349345
})
350346
}

‎coordinator/changefeed/changefeed_db.go‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"github.com/pingcap/ticdc/pkg/node"
2626
"github.com/pingcap/ticdc/pkg/scheduler/replica"
2727
"github.com/pingcap/ticdc/pkg/util"
28-
"go.uber.org/atomic"
2928
"go.uber.org/zap"
3029
)
3130

@@ -39,7 +38,7 @@ type ChangefeedDB struct {
3938

4039
// stopped changefeeds that failed, stopped or finished
4140
stopped map[common.ChangeFeedID]*Changefeed
42-
meteringEpoch atomic.Uint64
41+
meteringEpoch uint64
4342
lock sync.RWMutex
4443
}
4544

@@ -63,15 +62,20 @@ func (db *ChangefeedDB) Init(allChangefeeds map[common.ChangeFeedID]*Changefeed)
6362
defer db.lock.Unlock()
6463
// Only add the changefeed to the changefeedDB and do nothing
6564
for _, cf := range allChangefeeds {
66-
cf.SetMeteringEpochSource(&db.meteringEpoch)
65+
cf.SetMeteringEpoch(db.meteringEpoch)
6766
db.changefeeds[cf.ID] = cf
6867
db.changefeedDisplayNames[cf.ID.DisplayName] = cf.ID
6968
}
7069
}
7170

7271
// SetMeteringEpoch updates the coordinator metering epoch shared by all changefeeds in this DB.
7372
func (db *ChangefeedDB) SetMeteringEpoch(epoch uint64) {
74-
db.meteringEpoch.Store(epoch)
73+
db.lock.Lock()
74+
defer db.lock.Unlock()
75+
db.meteringEpoch = epoch
76+
for _, cf := range db.changefeeds {
77+
cf.SetMeteringEpoch(epoch)
78+
}
7579
}
7680

7781
func (db *ChangefeedDB) withRLock(action func()) {
@@ -87,7 +91,7 @@ func (db *ChangefeedDB) AddAbsentChangefeed(tasks ...*Changefeed) {
8791
defer db.lock.Unlock()
8892

8993
for _, task := range tasks {
90-
task.SetMeteringEpochSource(&db.meteringEpoch)
94+
task.SetMeteringEpoch(db.meteringEpoch)
9195
db.changefeeds[task.ID] = task
9296
db.changefeedDisplayNames[task.ID.DisplayName] = task.ID
9397
db.AddAbsentWithoutLock(task)
@@ -99,7 +103,7 @@ func (db *ChangefeedDB) AddStoppedChangefeed(task *Changefeed) {
99103
db.lock.Lock()
100104
defer db.lock.Unlock()
101105

102-
task.SetMeteringEpochSource(&db.meteringEpoch)
106+
task.SetMeteringEpoch(db.meteringEpoch)
103107
db.changefeeds[task.ID] = task
104108
db.changefeedDisplayNames[task.ID.DisplayName] = task.ID
105109
db.stopped[task.ID] = task
@@ -110,7 +114,7 @@ func (db *ChangefeedDB) AddReplicatingMaintainer(task *Changefeed, nodeID node.I
110114
db.lock.Lock()
111115
defer db.lock.Unlock()
112116

113-
task.SetMeteringEpochSource(&db.meteringEpoch)
117+
task.SetMeteringEpoch(db.meteringEpoch)
114118
task.SetNodeID(nodeID)
115119
log.Info("add an replicating maintainer",
116120
zap.String("nodeID", nodeID.String()),
@@ -398,7 +402,7 @@ func (db *ChangefeedDB) ReplaceStoppedChangefeed(cf *config.ChangeFeedInfo) {
398402
}
399403
// todo: not create a new changefeed here?
400404
newCf := NewChangefeed(cf.ChangefeedID, cf, oldCf.GetStatus().CheckpointTs, false)
401-
newCf.SetMeteringEpochSource(&db.meteringEpoch)
405+
newCf.SetMeteringEpoch(db.meteringEpoch)
402406
newCf.SetMeteringLastSeq(oldCf.GetMeteringLastSeq())
403407
db.stopped[cf.ChangefeedID] = newCf
404408
db.changefeeds[cf.ChangefeedID] = newCf

‎coordinator/controller.go‎

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,7 @@ type Controller struct {
120120
// semantics so late polls still observe success instead of capture-not-exist.
121121
drainCompleted *drainCompletedState
122122

123-
meteringEnabled atomic.Bool
124-
metering *meteringAccumulator
123+
metering *meteringAccumulator
125124
}
126125

127126
type changefeedChange struct {
@@ -151,6 +150,7 @@ func NewController(
151150
batchSize int,
152151
balanceInterval time.Duration,
153152
pdClient pd.Client,
153+
meteringEnabled bool,
154154
) *Controller {
155155
changefeedDB := changefeed.NewChangefeedDB(version)
156156

@@ -197,9 +197,14 @@ func NewController(
197197
pdClock: appcontext.GetService[pdutil.Clock](appcontext.DefaultPDClock),
198198
drainController: drainController,
199199
writeLease: newCaptureWriteLeaseController(version, selfNode.ID),
200-
metering: newMeteringAccumulator(),
201200
}
202201
c.nodeChanged.changed = false
202+
if meteringEnabled {
203+
c.metering = newMeteringAccumulator()
204+
if version > 0 {
205+
changefeedDB.SetMeteringEpoch(uint64(version))
206+
}
207+
}
203208

204209
c.bootstrapper = bootstrap.NewBootstrapper[heartbeatpb.CoordinatorBootstrapResponse](
205210
bootstrapperID,
@@ -763,7 +768,7 @@ func (c *Controller) handleSingleMaintainerStatus(
763768
return nil
764769
}
765770

766-
if c.meteringEnabled.Load() && c.metering != nil && c.version > 0 &&
771+
if c.metering != nil && c.version > 0 &&
767772
status.MeteringEpoch == uint64(c.version) && status.MeteringSeq > 0 {
768773
if ackedSeq, ok := c.metering.Accept(cfID, status.MeteringSeq, status.MeteringBytes); ok {
769774
cf.SetMeteringLastSeq(ackedSeq)
@@ -797,14 +802,6 @@ func (c *Controller) handleSingleMaintainerStatus(
797802
return change
798803
}
799804

800-
// EnableMetering activates traffic metering acceptance and propagation for this controller term.
801-
func (c *Controller) EnableMetering() {
802-
c.meteringEnabled.Store(true)
803-
if c.version > 0 && c.changefeedDB != nil {
804-
c.changefeedDB.SetMeteringEpoch(uint64(c.version))
805-
}
806-
}
807-
808805
// markChangefeedBootstrapped serializes the acknowledgement with API lifecycle
809806
// operations. Failed persistence is retried by the next maintainer heartbeat.
810807
func (c *Controller) markChangefeedBootstrapped(cf *changefeed.Changefeed, from node.ID, epoch uint64) bool {
@@ -1190,7 +1187,7 @@ func (c *Controller) RemoveChangefeed(ctx context.Context, id common.ChangeFeedI
11901187
}
11911188
}
11921189
if c.metering != nil {
1193-
c.metering.OnChangefeedRemoved(id)
1190+
c.metering.Forget(id)
11941191
}
11951192
return cf.GetStatus().CheckpointTs, nil
11961193
}
@@ -1443,7 +1440,7 @@ func (c *Controller) newBootstrapMessage(id node.ID, addr string) *messaging.Tar
14431440
&heartbeatpb.CoordinatorBootstrapRequest{
14441441
Version: c.version,
14451442
WriteLeaseProtocolVersion: heartbeatpb.LegacyWriteLeaseProtocolVersion,
1446-
MeteringEnabled: c.meteringEnabled.Load(),
1443+
MeteringEnabled: c.metering != nil,
14471444
})
14481445
}
14491446

‎coordinator/coordinator.go‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ func New(node *node.Info,
129129
}
130130
// handle messages from message center
131131
mc.RegisterHandler(messaging.CoordinatorTopic, c.recvMessages)
132+
reporter := createMeteringReporter()
132133
c.controller = NewController(
133134
c.version,
134135
c.nodeInfo,
@@ -138,7 +139,11 @@ func New(node *node.Info,
138139
batchSize,
139140
balanceCheckInterval,
140141
c.pdClient,
142+
reporter != nil,
141143
)
144+
if reporter != nil {
145+
c.metering = newMeteringReporter(reporter, c.controller.metering)
146+
}
142147

143148
nodeManager := appcontext.GetService[*watcher.NodeManager](watcher.NodeManagerName)
144149
nodeManager.RegisterOwnerChangeHandler(
@@ -177,7 +182,6 @@ func (c *coordinator) Run(ctx context.Context) error {
177182
ctx, cancel := context.WithCancel(ctx)
178183
c.cancel = cancel
179184
defer cancel()
180-
c.initializeMetering()
181185
if c.metering != nil {
182186
meteringEnabledGauge.Set(1)
183187
defer func() {
@@ -213,24 +217,19 @@ func (c *coordinator) Run(ctx context.Context) error {
213217
return err
214218
}
215219

216-
func (c *coordinator) initializeMetering() {
220+
func createMeteringReporter() *metering.Reporter {
217221
serverCfg := config.GetGlobalServerConfig()
218222
if serverCfg == nil || serverCfg.Metering == nil || serverCfg.Metering.Type == "" {
219-
return
223+
return nil
220224
}
221225
reporter, err := metering.NewReporter(serverCfg.Metering, serverCfg.ClusterID)
222226
if err != nil {
223227
meteringEnabledGauge.Set(0)
224228
log.Error("failed to initialize traffic metering reporter; disabling metering for this coordinator term",
225229
zap.Error(err))
226-
return
227-
}
228-
var acc *meteringAccumulator
229-
if c.controller != nil {
230-
c.controller.EnableMetering()
231-
acc = c.controller.metering
230+
return nil
232231
}
233-
c.metering = newMeteringReporter(reporter, acc)
232+
return reporter
234233
}
235234

236235
// run handles the following:

0 commit comments

Comments
 (0)