-
Notifications
You must be signed in to change notification settings - Fork 269
/
relay.go
136 lines (120 loc) · 4.09 KB
/
relay.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
// Copyright 2019 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 syncer
import (
"context"
"path/filepath"
"strings"
"github.com/go-mysql-org/go-mysql/mysql"
"github.com/pingcap/tiflow/dm/config"
"github.com/pingcap/tiflow/dm/pkg/binlog"
"github.com/pingcap/tiflow/dm/pkg/log"
"github.com/pingcap/tiflow/dm/pkg/terror"
"github.com/pingcap/tiflow/dm/pkg/utils"
"github.com/pingcap/tiflow/dm/syncer/binlogstream"
)
func (s *Syncer) setInitActiveRelayLog(ctx context.Context) error {
if s.binlogType != binlogstream.LocalBinlog {
return nil
}
var (
pos mysql.Position
activeSubDir string
extractPos bool
err error
)
indexPath := filepath.Join(s.cfg.RelayDir, utils.UUIDIndexFilename)
subDirs, err := utils.ParseUUIDIndex(indexPath)
if err != nil {
return terror.Annotatef(err, "UUID index file path %s", indexPath)
}
if len(subDirs) == 0 {
return terror.ErrRelayNoValidRelaySubDir.Generate(s.cfg.RelayDir)
}
checkLocation := s.checkpoint.GlobalPoint()
switch {
case binlog.ComparePosition(checkLocation.Position, binlog.MinPosition) > 0:
// continue from previous checkpoint
pos = checkLocation.Position
extractPos = true
case s.cfg.Mode == config.ModeIncrement:
// fresh start for task-mode increment
pos = mysql.Position{
Name: s.cfg.Meta.BinLogName,
Pos: s.cfg.Meta.BinLogPos,
}
default:
// start from dumper or loader, get current pos from master
pos, _, err = s.fromDB.GetMasterStatus(s.tctx.WithContext(ctx), s.cfg.Flavor)
if err != nil {
return terror.Annotatef(err, "get master status")
}
}
if extractPos {
activeSubDir, _, pos, err = binlog.ExtractPos(pos, subDirs)
if err != nil {
return err
}
} else {
var uuid string
latestSubDir := subDirs[len(subDirs)-1]
uuid, err = s.fromDB.GetServerUUID(ctx, s.cfg.Flavor)
if err != nil {
return terror.WithScope(terror.Annotatef(err, "get server UUID"), terror.ScopeUpstream)
}
// latest should be the current
if !strings.HasPrefix(latestSubDir, uuid) {
return terror.ErrSyncerUnitUUIDNotLatest.Generate(uuid, subDirs)
}
activeSubDir = latestSubDir
}
if len(pos.Name) == 0 {
s.tctx.Logger.Warn("empty position, may because only specify GTID and hasn't saved according binlog position")
return nil
}
err = s.readerHub.UpdateActiveRelayLog(s.cfg.Name, activeSubDir, pos.Name)
s.recordedActiveRelayLog = true
s.tctx.L().Info("current earliest active relay log", log.WrapStringerField("active relay log", s.readerHub.EarliestActiveRelayLog()))
return err
}
func (s *Syncer) updateActiveRelayLog(pos mysql.Position) error {
if s.binlogType != binlogstream.LocalBinlog {
return nil
}
if len(pos.Name) == 0 {
s.tctx.Logger.Warn("empty position, may because only specify GTID and hasn't saved according binlog position")
return nil
}
indexPath := filepath.Join(s.cfg.RelayDir, utils.UUIDIndexFilename)
uuids, err := utils.ParseUUIDIndex(indexPath)
if err != nil {
return terror.Annotatef(err, "UUID index file path %s", indexPath)
}
if len(uuids) == 0 {
return terror.ErrRelayNoValidRelaySubDir.Generate(s.cfg.RelayDir)
}
activeSubDir, _, pos, err := binlog.ExtractPos(pos, uuids)
if err != nil {
return err
}
err = s.readerHub.UpdateActiveRelayLog(s.cfg.Name, activeSubDir, pos.Name)
s.tctx.L().Info("current earliest active relay log", log.WrapStringerField("active relay log", s.readerHub.EarliestActiveRelayLog()))
return err
}
func (s *Syncer) removeActiveRelayLog() {
if s.binlogType != binlogstream.LocalBinlog {
return
}
s.readerHub.RemoveActiveRelayLog(s.cfg.Name)
s.tctx.L().Info("current earliest active relay log", log.WrapStringerField("active relay log", s.readerHub.EarliestActiveRelayLog()))
}