-
Notifications
You must be signed in to change notification settings - Fork 269
/
migrate.go
604 lines (553 loc) · 17.6 KB
/
migrate.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
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
// Copyright 2022 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 migrate
import (
"context"
"encoding/json"
"fmt"
"net/url"
"strconv"
"strings"
"time"
"github.com/pingcap/errors"
"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/etcd"
"github.com/pingcap/tiflow/pkg/pdutil"
"github.com/pingcap/tiflow/pkg/security"
"github.com/pingcap/tiflow/pkg/txnutil/gc"
pd "github.com/tikv/pd/client"
clientV3 "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/client/v3/concurrency"
"go.uber.org/atomic"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/backoff"
)
const (
// cdcMetaVersion is hard code value indicate the metaVersion of TiCDC
cdcMetaVersion = 1
etcdSessionTTL = 10
campaignTimeoutDuration = 1 * time.Minute
noMetaVersion = -1
migrateLogsWarnDuration = 5 * time.Second
migrationCampaignKey = "ticdc-migration"
oldChangefeedPrefix = "/tidb/cdc/changefeed/info"
oldGcServiceID = "ticdc"
)
type keys map[string]string
func (k keys) addPair(old, new string) {
k[old] = new
}
// Migrator migrates the cdc metadata
type Migrator interface {
// ShouldMigrate checks if we need to migrate metadata
ShouldMigrate(ctx context.Context) (bool, error)
// Migrate migrates the cdc metadata
Migrate(ctx context.Context) error
// WaitMetaVersionMatched wait util migration is done
WaitMetaVersionMatched(ctx context.Context) error
// MarkMigrateDone marks migration is done
MarkMigrateDone()
// IsMigrateDone check if migration is done
IsMigrateDone() bool
}
type migrator struct {
// oldMetaVersion int
newMetaVersion int
metaVersionKey string
// cdc old owner key
oldOwnerKey string
// etcd client
cli etcd.CDCEtcdClient
// all keyPrefixes needed to be migrated or update
// map from oldKeyPrefix to newKeyPrefix
keyPrefixes keys
done atomic.Bool
pdEndpoints []string
config *config.ServerConfig
createPDClientFunc func(ctx context.Context,
pdEndpoints []string,
conf *security.Credential) (pd.Client, error)
}
// NewMigrator returns a cdc metadata
func NewMigrator(cli etcd.CDCEtcdClient,
pdEndpoints []string,
serverConfig *config.ServerConfig,
) Migrator {
metaVersionCDCKey := &etcd.CDCKey{
Tp: etcd.CDCKeyTypeMetaVersion,
ClusterID: cli.GetClusterID(),
}
return &migrator{
newMetaVersion: cdcMetaVersion,
metaVersionKey: metaVersionCDCKey.String(),
oldOwnerKey: "/ticdc/cdc/owner",
cli: cli,
keyPrefixes: make(keys),
pdEndpoints: pdEndpoints,
config: serverConfig,
createPDClientFunc: createPDClient,
}
}
// MarkMigrateDone marks migration is done
func (m *migrator) MarkMigrateDone() {
m.done.Store(true)
}
// IsMigrateDone check if migration is done
func (m *migrator) IsMigrateDone() bool {
return m.done.Load()
}
func createPDClient(ctx context.Context,
pdEndpoints []string,
conf *security.Credential,
) (pd.Client, error) {
grpcTLSOption, err := conf.ToGRPCDialOption()
if err != nil {
return nil, errors.Trace(err)
}
return pd.NewClientWithContext(
ctx, pdEndpoints, conf.PDSecurityOption(),
pd.WithGRPCDialOptions(
grpcTLSOption,
grpc.WithBlock(),
grpc.WithConnectParams(grpc.ConnectParams{
Backoff: backoff.Config{
BaseDelay: time.Second,
Multiplier: 1.1,
Jitter: 0.1,
MaxDelay: 3 * time.Second,
},
MinConnectTimeout: 3 * time.Second,
}),
),
pd.WithForwardingOption(config.EnablePDForwarding),
)
}
// Note: we do not use etcd transaction to migrate key
// as it has the maximum operation limit in a single transaction.
// So we use a double check mechanism to make sure the migration is complete.
// 1. check and put metaVersion
// 2. campaign old owner
// 3. update keys
// 4. check metadata consistency
// 5. update metaVersion
func (m *migrator) migrate(ctx context.Context, etcdNoMetaVersion bool, oldVersion int) error {
pdClient, err := m.createPDClientFunc(ctx,
m.pdEndpoints, m.config.Security)
if err != nil {
return errors.Trace(err)
}
defer pdClient.Close()
upstreamID := pdClient.GetClusterID(ctx)
// 1.1 check metaVersion, if the metaVersion in etcd does not match
// m.oldMetaVersion, it means that someone has migrated the metadata
metaVersion, err := getMetaVersion(ctx, m.cli.GetEtcdClient(), m.cli.GetClusterID())
if err != nil {
log.Error("get meta version failed, etcd meta data migration failed", zap.Error(err))
return cerror.WrapError(cerror.ErrEtcdMigrateFailed, err)
}
if metaVersion > m.newMetaVersion {
log.Panic("meta version in etcd is greater than the meta version in TiCDC",
zap.Int("etcdMetaVersion", metaVersion), zap.Int("cdcMetaVersion", m.newMetaVersion))
}
// if metaVersion in etcd is equal to m.newMetaVersion,
// it means that there is no need to migrate
if !etcdNoMetaVersion && metaVersion == m.newMetaVersion {
log.Warn("meta version no match, no need to migrate")
return nil
}
// 1.2 put metaVersionKey to etcd to panic old version cdc server
if etcdNoMetaVersion {
_, err := m.cli.GetEtcdClient().Put(ctx, m.metaVersionKey, fmt.Sprintf("%d", oldVersion))
if err != nil {
log.Error("put meta version failed, etcd meta data migration failed", zap.Error(err))
return cerror.WrapError(cerror.ErrEtcdMigrateFailed, err)
}
}
// 3.campaign old owner to make sure old keys will not be updates
campaignCtx, cancel := context.WithTimeout(ctx, campaignTimeoutDuration)
defer cancel()
if err := m.campaignOldOwner(campaignCtx); err != nil {
if errors.ErrorEqual(err, context.DeadlineExceeded) {
log.Error("campaign old owner timeout",
zap.Duration("duration", campaignTimeoutDuration))
}
log.Error("campaign old owner failed, etcd meta data migration failed",
zap.Error(err))
return cerror.WrapError(cerror.ErrEtcdMigrateFailed, err)
}
beforeKV := make(map[string][]byte)
// 4.campaign owner successfully, begin to migrate data
for oldPrefix, newPrefix := range m.keyPrefixes {
resp, err := m.cli.GetEtcdClient().Get(ctx, oldPrefix, clientV3.WithPrefix())
if err != nil {
log.Error("get old meta data failed, etcd meta data migration failed",
zap.Error(err))
return cerror.WrapError(cerror.ErrEtcdMigrateFailed, err)
}
for _, v := range resp.Kvs {
oldKey := string(v.Key)
newKey := newPrefix + oldKey[len(oldPrefix):]
beforeKV[newKey] = v.Value
log.Info("migrate key", zap.String("oldKey", oldKey), zap.String("newKey", newKey))
if strings.HasPrefix(string(v.Key), oldChangefeedPrefix) {
info := new(model.ChangeFeedInfo)
err = info.Unmarshal(v.Value)
if err != nil {
log.Error("unmarshal changefeed failed",
zap.String("value", string(v.Value)),
zap.Error(err))
return cerror.WrapError(cerror.ErrEtcdMigrateFailed, err)
}
info.UpstreamID = upstreamID
info.Namespace = model.DefaultNamespace
// changefeed id is a part of etcd key path
// for example: /tidb/cdc/changefeed/info/abcd, abcd is the changefeed
info.ID = strings.TrimPrefix(string(v.Key), oldChangefeedPrefix+"/")
var str string
str, err = info.Marshal()
if err != nil {
log.Error("marshal changefeed failed",
zap.Error(err))
return cerror.WrapError(cerror.ErrEtcdMigrateFailed, err)
}
_, err = m.cli.GetEtcdClient().Put(ctx, newKey, str)
} else {
_, err = m.cli.GetEtcdClient().Put(ctx, newKey, string(v.Value))
}
if err != nil {
log.Error("put new meta data failed, etcd meta data migration failed",
zap.Error(err))
return cerror.WrapError(cerror.ErrEtcdMigrateFailed, err)
}
}
}
// put upstream id
err = m.saveUpstreamInfo(ctx)
if err != nil {
log.Error("save default upstream failed, "+
"etcd meta data migration failed", zap.Error(err))
return cerror.WrapError(cerror.ErrEtcdMigrateFailed, err)
}
err = m.migrateGcServiceSafePoint(ctx, pdClient,
m.config.Security, m.cli.GetGCServiceID(), m.config.GcTTL)
if err != nil {
log.Error("update meta version failed, etcd meta data migration failed", zap.Error(err))
return cerror.WrapError(cerror.ErrEtcdMigrateFailed, err)
}
// 5. update metaVersion
_, err = m.cli.GetEtcdClient().Put(ctx, m.metaVersionKey, fmt.Sprintf("%d", m.newMetaVersion))
if err != nil {
log.Error("update meta version failed, etcd meta data migration failed", zap.Error(err))
return cerror.WrapError(cerror.ErrEtcdMigrateFailed, err)
}
log.Info("etcd data migration successful")
cleanOldData(ctx, m.cli.GetEtcdClient())
log.Info("clean old etcd data successful")
return nil
}
func cleanOldData(ctx context.Context, client *etcd.Client) {
resp, err := client.Get(ctx, "/tidb/cdc", clientV3.WithPrefix())
if err != nil {
log.Warn("query data from etcd failed",
zap.Error(err))
}
for _, kvPair := range resp.Kvs {
key := string(kvPair.Key)
if shouldDelete(key) {
value := string(kvPair.Value)
if strings.HasPrefix(key, oldChangefeedPrefix) {
value = maskChangefeedInfo(kvPair.Value)
}
// 0 is the backup version. For now, we only support version 0
newKey := etcd.MigrateBackupKey(0, key)
log.Info("renaming old etcd data",
zap.String("key", key),
zap.String("newKey", newKey),
zap.String("value", value))
if _, err := client.Put(ctx, newKey,
string(kvPair.Value)); err != nil {
log.Info("put new key failed", zap.String("key", key),
zap.Error(err))
}
if _, err := client.Delete(ctx, key); err != nil {
log.Warn("failed to delete old data",
zap.String("key", key),
zap.Error(err))
}
}
}
}
// old key prefix that should be removed
var oldKeyPrefix = []string{
"/tidb/cdc/changefeed/info",
"/tidb/cdc/job",
"/tidb/cdc/meta/ticdc-delete-etcd-key-count",
"/tidb/cdc/owner",
"/tidb/cdc/capture",
"/tidb/cdc/task/workload",
"/tidb/cdc/task/position",
"/tidb/cdc/task/status",
}
// shouldDelete check if a key should be deleted
func shouldDelete(key string) bool {
for _, prefix := range oldKeyPrefix {
if strings.HasPrefix(key, prefix) {
return true
}
}
return false
}
func maskChangefeedInfo(data []byte) string {
value := string(data)
oldConfig := map[string]any{}
err := json.Unmarshal(data, &oldConfig)
if err != nil {
log.Info("marshal oldConfig failed",
zap.Error(err))
}
sinkURI, ok := oldConfig["sink-uri"]
if ok {
sinkURIParsed, err := url.Parse(sinkURI.(string))
if err != nil {
log.Error("failed to parse sink URI", zap.Error(err))
}
if sinkURIParsed.User != nil && sinkURIParsed.User.String() != "" {
sinkURIParsed.User = url.UserPassword("username", "password")
}
if sinkURIParsed.Host != "" {
sinkURIParsed.Host = "***"
}
oldConfig["sink-uri"] = sinkURIParsed.String()
buf, err := json.Marshal(oldConfig)
if err != nil {
log.Info("marshal oldConfig failed",
zap.Error(err))
}
value = string(buf)
}
return value
}
func (m *migrator) migrateGcServiceSafePoint(ctx context.Context,
pdClient pd.Client,
config *security.Credential,
newGcServiceID string,
ttl int64,
) error {
pc, err := pdutil.NewPDAPIClient(pdClient, config)
if err != nil {
log.Error("create pd api client failed", zap.Error(err))
return errors.Trace(err)
}
defer pc.Close()
gcServiceSafePoints, err := pc.ListGcServiceSafePoint(ctx)
if err != nil {
log.Error("list gc service safepoint failed",
zap.Error(err))
return errors.Trace(err)
}
var cdcGcSafePoint *pdutil.ServiceSafePoint
for _, item := range gcServiceSafePoints.ServiceGCSafepoints {
if item.ServiceID == oldGcServiceID {
cdcGcSafePoint = item
break
}
}
if cdcGcSafePoint != nil {
_, err := gc.SetServiceGCSafepoint(ctx, pdClient, newGcServiceID,
ttl,
cdcGcSafePoint.SafePoint)
if err != nil {
log.Error("set gc service safepoint failed",
zap.Error(err))
return errors.Trace(err)
}
err = gc.RemoveServiceGCSafepoint(ctx, pdClient, oldGcServiceID)
if err != nil {
log.Warn("remove old gc safepoint failed", zap.Error(err))
}
}
return nil
}
func (m *migrator) campaignOldOwner(ctx context.Context) error {
sess, err := concurrency.NewSession(m.cli.GetEtcdClient().Unwrap(),
concurrency.WithTTL(etcdSessionTTL))
if err != nil {
return errors.Trace(err)
}
election := concurrency.NewElection(sess, m.oldOwnerKey)
defer func() {
_ = sess.Close()
}()
if err := election.Campaign(ctx, migrationCampaignKey); err != nil {
return errors.Trace(err)
}
return nil
}
// Migrate migrate etcd meta data
func (m *migrator) Migrate(ctx context.Context) error {
version, err := getMetaVersion(ctx, m.cli.GetEtcdClient(), m.cli.GetClusterID())
if err != nil {
return errors.Trace(err)
}
shouldMigrate := false
oldVersion, newVersion := 0, cdcMetaVersion
if version == noMetaVersion {
if m.cli.GetClusterID() != etcd.DefaultCDCClusterID {
// not default cluster
log.Info("not a default cdc cluster, skip migration data",
zap.String("cluster", m.cli.GetClusterID()))
// put upstream id
err = m.saveUpstreamInfo(ctx)
if err != nil {
log.Error("save default upstream failed, "+
"etcd meta data migration failed",
zap.Error(err))
return cerror.WrapError(cerror.ErrEtcdMigrateFailed, err)
}
_, err := m.cli.GetEtcdClient().
Put(ctx, m.metaVersionKey, fmt.Sprintf("%d", newVersion))
if err != nil {
log.Error("put meta version failed", zap.Error(err))
}
return err
}
shouldMigrate = true
} else if version > newVersion {
log.Panic("meta version in etcd is greater than the meta version in TiCDC",
zap.Int("etcdMetaVersion", version), zap.Int("cdcMetaVersion", m.newMetaVersion))
} else {
oldVersion = version
shouldMigrate = oldVersion < newVersion
}
if !shouldMigrate {
return nil
}
m.keyPrefixes.addPair("/tidb/cdc/changefeed/info",
etcd.DefaultClusterAndNamespacePrefix+etcd.ChangefeedInfoKey)
m.keyPrefixes.addPair("/tidb/cdc/job",
etcd.DefaultClusterAndNamespacePrefix+etcd.ChangefeedStatusKey)
return m.migrate(ctx, version == noMetaVersion, oldVersion)
}
// ShouldMigrate checks if we should migrate etcd metadata
func (m *migrator) ShouldMigrate(ctx context.Context) (bool, error) {
version, err := getMetaVersion(ctx, m.cli.GetEtcdClient(), m.cli.GetClusterID())
if err != nil {
return false, errors.Trace(err)
}
return version != cdcMetaVersion, nil
}
// WaitMetaVersionMatched checks and waits until the metaVersion in etcd
// matched to lock cdcMetaVersion
func (m *migrator) WaitMetaVersionMatched(ctx context.Context) error {
version, err := getMetaVersion(ctx, m.cli.GetEtcdClient(), m.cli.GetClusterID())
if err != nil {
return errors.Trace(err)
}
if version == cdcMetaVersion {
return nil
}
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
warnLogTicker := time.NewTicker(migrateLogsWarnDuration)
defer warnLogTicker.Stop()
start := time.Now()
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
version, err := getMetaVersion(ctx, m.cli.GetEtcdClient(), m.cli.GetClusterID())
if err != nil {
return errors.Trace(err)
}
if version == cdcMetaVersion {
return nil
}
case <-warnLogTicker.C:
log.Warn("meta data migrating last too long",
zap.Duration("duration", time.Since(start)))
}
}
}
// saveUpstreamInfo save the default upstream info to etcd
func (m *migrator) saveUpstreamInfo(ctx context.Context) error {
pdClient, err := m.createPDClientFunc(ctx,
m.pdEndpoints, m.config.Security)
if err != nil {
return errors.Trace(err)
}
defer pdClient.Close()
upstreamID := pdClient.GetClusterID(ctx)
upstreamKey := etcd.CDCKey{
Tp: etcd.CDCKeyTypeUpStream,
ClusterID: m.cli.GetClusterID(),
UpstreamID: upstreamID,
Namespace: model.DefaultNamespace,
}
upstreamKeyStr := upstreamKey.String()
upstreamInfo := &model.UpstreamInfo{
ID: upstreamID,
PDEndpoints: strings.Join(m.pdEndpoints, ","),
KeyPath: m.config.Security.KeyPath,
CertPath: m.config.Security.CertPath,
CAPath: m.config.Security.CAPath,
CertAllowedCN: m.config.Security.CertAllowedCN,
}
upstreamInfoStr, err := upstreamInfo.Marshal()
if err != nil {
return errors.Trace(err)
}
_, err = m.cli.GetEtcdClient().Put(ctx, upstreamKeyStr, string(upstreamInfoStr))
return err
}
func getMetaVersion(ctx context.Context, cli *etcd.Client, clusterID string) (int, error) {
key := etcd.CDCKey{Tp: etcd.CDCKeyTypeMetaVersion, ClusterID: clusterID}
resp, err := cli.Get(ctx, key.String())
if err != nil {
return 0, errors.Trace(err)
}
// means there no metaVersion in etcd
if len(resp.Kvs) == 0 {
return noMetaVersion, nil
}
version, err := strconv.Atoi(string(resp.Kvs[0].Value))
if err != nil {
return 0, errors.Trace(err)
}
return version, nil
}
// NoOpMigrator do nothing
type NoOpMigrator struct{}
// ShouldMigrate checks if we need to migrate metadata
func (f *NoOpMigrator) ShouldMigrate(_ context.Context) (bool, error) {
return false, nil
}
// Migrate migrates the cdc metadata
func (f *NoOpMigrator) Migrate(_ context.Context) error {
return nil
}
// WaitMetaVersionMatched wait util migration is done
func (f *NoOpMigrator) WaitMetaVersionMatched(_ context.Context) error {
return nil
}
// MarkMigrateDone marks migration is done
func (f *NoOpMigrator) MarkMigrateDone() {
}
// IsMigrateDone check if migration is done
func (f *NoOpMigrator) IsMigrateDone() bool {
return true
}