forked from pingcap/ticdc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoordinator.go
More file actions
497 lines (445 loc) · 16.2 KB
/
coordinator.go
File metadata and controls
497 lines (445 loc) · 16.2 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
// Copyright 2024 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 coordinator
import (
"context"
"math"
"time"
"github.com/pingcap/failpoint"
"github.com/pingcap/log"
"github.com/pingcap/ticdc/coordinator/changefeed"
"github.com/pingcap/ticdc/coordinator/gccleaner"
"github.com/pingcap/ticdc/pkg/common"
appcontext "github.com/pingcap/ticdc/pkg/common/context"
"github.com/pingcap/ticdc/pkg/config"
"github.com/pingcap/ticdc/pkg/config/kerneltype"
"github.com/pingcap/ticdc/pkg/errors"
"github.com/pingcap/ticdc/pkg/messaging"
"github.com/pingcap/ticdc/pkg/metrics"
"github.com/pingcap/ticdc/pkg/node"
"github.com/pingcap/ticdc/pkg/pdutil"
"github.com/pingcap/ticdc/pkg/server"
"github.com/pingcap/ticdc/pkg/txnutil/gc"
"github.com/pingcap/ticdc/pkg/util"
"github.com/pingcap/ticdc/server/watcher"
"github.com/pingcap/ticdc/utils/chann"
pd "github.com/tikv/pd/client"
"go.uber.org/atomic"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
)
// Message Flow in Coordinator:
// (from maintainer)
// External Messages Coordinator Controller Storage
// | | | |
// | ----message-----> | | |
// | | | |
// | | ---event.message----> | |
// | | | |
// | | <---state change----- | |
// | | | |
// | | ----update state----------------> |
// | | | |
// | | <---checkpoint ts---- | |
// | | | |
// | | ----save checkpoint ts-------------> |
// | | | |
//
// Flow Description:
// 1. External messages arrive at Coordinator via MessageCenter
// 2. Coordinator forwards messages as events to Controller
// 3. Controller processes events and reports state changes back
// 4. Coordinator updates state in meta store
// 5. Controller reports checkpoint TS
// 6. Coordinator saves checkpoint TS to meta store
// coordinator implements the Coordinator interface
type coordinator struct {
nodeInfo *node.Info
version int64
gcServiceID string
lastTickTime time.Time
controller *Controller
backend changefeed.Backend
mc messaging.MessageCenter
gcManager gc.Manager
gcTickInterval time.Duration
gcCleaner *gccleaner.Cleaner
pdClient pd.Client
pdClock pdutil.Clock
// eventCh is used to receive the event from message center, basically these messages
// are from maintainer.
eventCh *chann.DrainableChann[*Event]
// changefeedChangeCh is used to receive the changefeed change from the controller
changefeedChangeCh chan []*changefeedChange
// msgGuardWaitGroup guards Add/Wait so Stop never races with new recv handlers.
msgGuardWaitGroup util.GuardedWaitGroup
cancel func()
closed atomic.Bool
}
func New(node *node.Info,
pdClient pd.Client,
backend changefeed.Backend,
gcServiceID string,
version int64,
batchSize int,
balanceCheckInterval time.Duration,
) server.Coordinator {
mc := appcontext.GetService[messaging.MessageCenter](appcontext.MessageCenter)
c := &coordinator{
version: version,
nodeInfo: node,
gcServiceID: gcServiceID,
gcManager: gc.NewManager(gcServiceID, pdClient),
gcCleaner: gccleaner.New(pdClient, gcServiceID),
gcTickInterval: time.Minute,
lastTickTime: time.Now(),
eventCh: chann.NewAutoDrainChann[*Event](),
pdClient: pdClient,
pdClock: appcontext.GetService[pdutil.Clock](appcontext.DefaultPDClock),
mc: mc,
changefeedChangeCh: make(chan []*changefeedChange, 1024),
backend: backend,
}
// handle messages from message center
mc.RegisterHandler(messaging.CoordinatorTopic, c.recvMessages)
c.controller = NewController(
c.version,
c.nodeInfo,
c.changefeedChangeCh,
c.backend,
c.eventCh,
batchSize,
balanceCheckInterval,
c.pdClient,
)
nodeManager := appcontext.GetService[*watcher.NodeManager](watcher.NodeManagerName)
nodeManager.RegisterOwnerChangeHandler(
string(c.nodeInfo.ID),
func(newCoordinatorID string) {
if newCoordinatorID != string(c.nodeInfo.ID) {
log.Info("Coordinator changed, and I am not the coordinator, stop myself",
zap.String("selfID", string(c.nodeInfo.ID)),
zap.String("newCoordinatorID", newCoordinatorID))
c.Stop()
}
})
return c
}
func (c *coordinator) recvMessages(ctx context.Context, msg *messaging.TargetMessage) error {
if !c.msgGuardWaitGroup.AddIf(func() bool {
return !c.closed.Load()
}) {
return nil
}
defer c.msgGuardWaitGroup.Done()
select {
case <-ctx.Done():
return context.Cause(ctx)
case c.eventCh.In() <- &Event{message: msg}:
}
return nil
}
// Run spawns two goroutines to handle messages and run the coordinator.
func (c *coordinator) Run(ctx context.Context) error {
ctx, cancel := context.WithCancel(ctx)
c.cancel = cancel
eg, ctx := errgroup.WithContext(ctx)
eg.Go(func() error {
return c.run(ctx)
})
eg.Go(func() error {
return c.runHandleEvent(ctx)
})
eg.Go(func() error {
return c.gcCleaner.Run(ctx)
})
eg.Go(func() error {
return c.controller.collectMetrics(ctx)
})
return eg.Wait()
}
// run handles the following:
// 1. update the gc safepoint to PD
// 2. store the changefeed checkpointTs to meta store
// 3. handle the state changed event
func (c *coordinator) run(ctx context.Context) error {
failpoint.Inject("InjectUpdateGCTickerInterval", func(val failpoint.Value) {
c.gcTickInterval = time.Duration(val.(int) * int(time.Second))
})
failpoint.Inject("coordinator-run-with-error", func() error {
return errors.New("coordinator run with error")
})
gcTicker := time.NewTicker(c.gcTickInterval)
defer gcTicker.Stop()
for {
select {
case <-ctx.Done():
return context.Cause(ctx)
case <-gcTicker.C:
// BeginGCTick must happen before updateGCSafepoint so tasks added in this tick
// will not be undone by the same tick.
c.gcCleaner.BeginGCTick()
err := c.updateGCSafepoint(ctx)
if err == nil {
c.gcCleaner.OnUpdateGCSafepointSucceeded()
}
now := time.Now()
metrics.CoordinatorCounter.Add(float64(now.Sub(c.lastTickTime)) / float64(time.Second))
c.lastTickTime = now
case changes := <-c.changefeedChangeCh:
if err := c.saveCheckpointTs(ctx, changes); err != nil {
return errors.Trace(err)
}
for _, change := range changes {
if change.changeType == ChangeState || change.changeType == ChangeStateAndTs {
if err := c.handleStateChange(ctx, change); err != nil {
return errors.Trace(err)
}
}
}
}
}
}
// runHandleEvent handles messages from the other modules.
func (c *coordinator) runHandleEvent(ctx context.Context) error {
for {
select {
case <-ctx.Done():
return context.Cause(ctx)
case event := <-c.eventCh.Out():
c.controller.HandleEvent(ctx, event)
}
}
}
func (c *coordinator) handleStateChange(
ctx context.Context,
event *changefeedChange,
) error {
cf := c.controller.getChangefeed(event.changefeedID)
if cf == nil {
log.Warn("changefeed not found", zap.String("changefeed", event.changefeedID.String()))
return nil
}
cfInfo, err := cf.GetInfo().Clone()
if err != nil {
return errors.Trace(err)
}
cfInfo.State = event.state
cfInfo.Error = event.err
progress := config.ProgressNone
if event.state == config.StateFailed || event.state == config.StateFinished {
progress = config.ProgressStopping
}
if err = c.backend.UpdateChangefeed(ctx, cfInfo, cf.GetStatus().CheckpointTs, progress); err != nil {
log.Error("failed to update changefeed state",
zap.Error(err))
return errors.Trace(err)
}
cf.SetInfo(cfInfo)
switch event.state {
case config.StateWarning:
c.controller.operatorController.StopChangefeed(ctx, event.changefeedID, false)
c.controller.updateChangefeedEpoch(ctx, event.changefeedID)
c.controller.moveChangefeedToSchedulingQueue(event.changefeedID, false, false)
case config.StateFailed, config.StateFinished:
failpoint.Inject("BlockBeforeStopChangefeed", func() {})
c.controller.operatorController.StopChangefeed(ctx, event.changefeedID, false)
default:
}
return nil
}
// checkStaleCheckpointTs checks if the checkpointTs is stale, if it is, it will send a state change event to the stateChangedCh
func (c *coordinator) checkStaleCheckpointTs(ctx context.Context, changefeed *changefeed.Changefeed, reportedCheckpointTs uint64) {
id := changefeed.ID
err := c.gcManager.CheckStaleCheckpointTs(changefeed.GetKeyspaceID(), id, reportedCheckpointTs)
if err == nil {
return
}
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
errCode, _ := errors.RFCCode(err)
state := config.StateWarning
if errors.IsChangefeedGCFastFailErrorCode(errCode) {
state = config.StateFailed
}
change := newChangefeedChange(changefeed, state, ChangeState, &config.RunningError{
Code: string(errCode),
Message: err.Error(),
})
select {
case <-ctx.Done():
log.Warn("Failed to send state change event to stateChangedCh since context timeout, "+
"there may be a lot of state need to be handled. Try next time",
zap.String("changefeed", id.String()),
zap.Error(context.Cause(ctx)))
case c.changefeedChangeCh <- []*changefeedChange{change}:
}
}
func (c *coordinator) saveCheckpointTs(ctx context.Context, changes []*changefeedChange) error {
statusMap := make(map[common.ChangeFeedID]uint64)
cfsMap := make(map[common.ChangeFeedID]*changefeed.Changefeed)
for _, change := range changes {
if change.changeType == ChangeState {
continue
}
upCf := change.changefeed
reportedCheckpointTs := upCf.GetStatus().CheckpointTs
if upCf.GetLastSavedCheckPointTs() < reportedCheckpointTs {
statusMap[upCf.ID] = reportedCheckpointTs
cfsMap[upCf.ID] = upCf
c.checkStaleCheckpointTs(ctx, upCf, reportedCheckpointTs)
}
}
if len(statusMap) == 0 {
return nil
}
err := c.controller.backend.UpdateChangefeedCheckpointTs(ctx, statusMap)
if err != nil {
log.Error("failed to update checkpointTs", zap.Error(err))
return errors.Trace(err)
}
// update the last saved checkpoint ts and send checkpointTs to maintainer
for id, cp := range statusMap {
cf := cfsMap[id]
cf.SetLastSavedCheckPointTs(cp)
if cf.NeedCheckpointTsMessage() {
msg := cf.NewCheckpointTsMessage(cf.GetLastSavedCheckPointTs())
c.sendMessages([]*messaging.TargetMessage{msg})
}
}
return nil
}
func (c *coordinator) CreateChangefeed(ctx context.Context, info *config.ChangeFeedInfo) error {
if err := c.controller.CreateChangefeed(ctx, info); err != nil {
return err
}
c.gcCleaner.Add(info.ChangefeedID, info.KeyspaceID, gc.EnsureGCServiceCreating)
return nil
}
func (c *coordinator) RemoveChangefeed(ctx context.Context, id common.ChangeFeedID) (uint64, error) {
checkpointTs, err := c.controller.RemoveChangefeed(ctx, id)
if err != nil {
return 0, err
}
if c.controller.calculateGlobalGCSafepoint() != math.MaxUint64 {
return checkpointTs, nil
}
// Delete the cluster-level safepoint as soon as the last changefeed is gone.
// This closes the window where the final coordinator could exit before the
// next periodic GC reconcile tick gets a chance to clean it up.
if err := c.tryDeleteGlobalGCSafepoint(ctx); err != nil {
log.Warn("failed to delete global gc safepoint after removing last changefeed",
zap.String("changefeed", id.String()),
zap.Error(err))
}
return checkpointTs, nil
}
func (c *coordinator) PauseChangefeed(ctx context.Context, id common.ChangeFeedID) error {
return c.controller.PauseChangefeed(ctx, id)
}
func (c *coordinator) ResumeChangefeed(ctx context.Context, id common.ChangeFeedID, newCheckpointTs uint64, overwriteCheckpointTs bool) error {
if err := c.controller.ResumeChangefeed(ctx, id, newCheckpointTs, overwriteCheckpointTs); err != nil {
return err
}
if overwriteCheckpointTs {
cf := c.controller.getChangefeed(id)
if cf != nil {
c.gcCleaner.Add(id, cf.GetKeyspaceID(), gc.EnsureGCServiceResuming)
}
}
return nil
}
func (c *coordinator) UpdateChangefeed(ctx context.Context, change *config.ChangeFeedInfo) error {
return c.controller.UpdateChangefeed(ctx, change)
}
func (c *coordinator) ListChangefeeds(ctx context.Context, keyspace string) ([]*config.ChangeFeedInfo, []*config.ChangeFeedStatus, error) {
return c.controller.ListChangefeeds(ctx, keyspace)
}
func (c *coordinator) GetChangefeed(ctx context.Context, changefeedDisplayName common.ChangeFeedDisplayName) (*config.ChangeFeedInfo, *config.ChangeFeedStatus, error) {
return c.controller.GetChangefeed(ctx, changefeedDisplayName)
}
func (c *coordinator) Initialized() bool {
return c.controller.initialized.Load()
}
func (c *coordinator) Stop() {
if c.closed.CompareAndSwap(false, true) {
c.mc.DeRegisterHandler(messaging.CoordinatorTopic)
// Ensure no handler is still writing to eventCh before closing it.
c.msgGuardWaitGroup.Wait()
c.controller.Stop()
c.cancel()
// close eventCh after cancel, to avoid send or get event from the channel
c.eventCh.CloseAndDrain()
}
}
func (c *coordinator) RequestResolvedTsFromLogCoordinator(ctx context.Context, changefeedDisplayName common.ChangeFeedDisplayName) {
c.controller.RequestResolvedTsFromLogCoordinator(ctx, changefeedDisplayName)
}
func (c *coordinator) sendMessages(msgs []*messaging.TargetMessage) {
for _, msg := range msgs {
err := c.mc.SendCommand(msg)
if err != nil {
log.Error("failed to send coordinator request", zap.Any("msg", msg), zap.Error(err))
continue
}
}
}
func (c *coordinator) tryDeleteGlobalGCSafepoint(ctx context.Context) error {
if !kerneltype.IsClassic() {
return nil
}
return errors.Trace(c.gcManager.TryDeleteServiceGCSafepoint(ctx))
}
func (c *coordinator) updateGlobalGcSafepoint(ctx context.Context) error {
minCheckpointTs := c.controller.calculateGlobalGCSafepoint()
if minCheckpointTs == math.MaxUint64 {
// Once there is no changefeed left, TiCDC should remove the cluster-level
// service safepoint instead of refreshing it to "now - 1".
return c.tryDeleteGlobalGCSafepoint(ctx)
}
// When the changefeed starts up, CDC will do a snapshot read at
// (checkpointTs - 1) from TiKV, so (checkpointTs - 1) should be an upper
// bound for the GC safepoint.
gcSafepointUpperBound := minCheckpointTs - 1
err := c.gcManager.TryUpdateServiceGCSafepoint(ctx, gcSafepointUpperBound)
return errors.Trace(err)
}
func (c *coordinator) updateAllKeyspaceGcBarriers(ctx context.Context) error {
barrierMap := c.controller.calculateKeyspaceGCBarrier()
var retErr error
for meta, barrierTS := range barrierMap {
if err := c.updateKeyspaceGcBarrier(ctx, meta, barrierTS); err != nil {
log.Warn("update keyspace gc barrier failed",
zap.Uint32("keyspaceID", meta.ID), zap.String("keyspaceName", meta.Name),
zap.Uint64("barrierTS", barrierTS), zap.Error(err))
retErr = err
}
}
return retErr
}
func (c *coordinator) updateKeyspaceGcBarrier(
ctx context.Context, meta common.KeyspaceMeta, barrierTS uint64,
) error {
barrierTsUpperBound := barrierTS - 1
err := c.gcManager.TryUpdateKeyspaceGCBarrier(ctx, meta.ID, meta.Name, barrierTsUpperBound)
return errors.Trace(err)
}
// updateGCSafepoint update the gc safepoint
// On next gen, we should update the gc barrier for all keyspaces
// Otherwise we should update the global gc safepoint
func (c *coordinator) updateGCSafepoint(ctx context.Context) error {
if kerneltype.IsNextGen() {
return c.updateAllKeyspaceGcBarriers(ctx)
}
return c.updateGlobalGcSafepoint(ctx)
}