forked from Shopify/ghostferry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
verifier.go
227 lines (182 loc) · 6.04 KB
/
verifier.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
package ghostferry
import (
"database/sql"
"errors"
"fmt"
"sync"
"time"
"github.com/siddontang/go-mysql/schema"
"github.com/sirupsen/logrus"
)
type IncompleteVerificationError struct{}
func (e IncompleteVerificationError) Error() string {
return "verification is incomplete"
}
type VerificationResult struct {
DataCorrect bool
Message string
}
func (e VerificationResult) Error() string {
return e.Message
}
type VerificationResultAndStatus struct {
VerificationResult
StartTime time.Time
DoneTime time.Time
}
func (r VerificationResultAndStatus) IsStarted() bool {
return !r.StartTime.IsZero()
}
func (r VerificationResultAndStatus) IsDone() bool {
return !r.DoneTime.IsZero()
}
// The sole purpose of this interface is to make it easier for one to
// implement their own strategy for verification and hook it up with
// the ControlServer. If there is no such need, one does not need to
// implement this interface.
type Verifier interface {
// Start the verifier in the background during the cutover phase.
// Traditionally, this is called from within the ControlServer.
//
// This method maybe called multiple times and it's up to the verifier
// to decide if it is possible to re-run the verification.
StartInBackground() error
// Wait for the verifier until it finishes verification after it was
// started with the StartInBackground.
//
// A verification is "done" when it verified the dbs (either
// correct or incorrect) OR when it experiences an error.
Wait()
// Returns the result and the status of the verification.
// To check the status, call IsStarted() and IsDone() on
// VerificationResultAndStatus.
//
// If the verification has been completed successfully (without errors) and
// the data checks out to be "correct", the result will be
// VerificationResult{true, ""}, with error = nil.
// Otherwise, the result will be VerificationResult{false, "message"}, with
// error = nil.
//
// If the verification is "done" but experienced an error during the check,
// the result will be VerificationResult{}, with err = yourErr.
Result() (VerificationResultAndStatus, error)
}
type ChecksumTableVerifier struct {
Tables []*schema.Table
DatabaseRewrites map[string]string
TableRewrites map[string]string
SourceDB *sql.DB
TargetDB *sql.DB
started *AtomicBoolean
verificationResultAndStatus VerificationResultAndStatus
verificationErr error
logger *logrus.Entry
wg *sync.WaitGroup
}
func (v *ChecksumTableVerifier) Verify() (VerificationResult, error) {
if v.logger == nil {
v.logger = logrus.WithField("tag", "checksum_verifier")
}
for _, table := range v.Tables {
sourceTable := QuotedTableName(table)
targetDbName := table.Schema
if v.DatabaseRewrites != nil {
if rewrittenName, exists := v.DatabaseRewrites[table.Schema]; exists {
targetDbName = rewrittenName
}
}
targetTableName := table.Name
if v.TableRewrites != nil {
if rewrittenName, exists := v.TableRewrites[table.Name]; exists {
targetTableName = rewrittenName
}
}
targetTable := QuotedTableNameFromString(targetDbName, targetTableName)
logWithTable := v.logger.WithFields(logrus.Fields{
"sourceTable": sourceTable,
"targetTable": targetTable,
})
logWithTable.Info("checking table")
wg := sync.WaitGroup{}
var sourceChecksum, targetChecksum int64
var sourceErr, targetErr error
wg.Add(2)
go func() {
defer wg.Done()
query := fmt.Sprintf("CHECKSUM TABLE %s EXTENDED", sourceTable)
sourceRow := v.SourceDB.QueryRow(query)
sourceChecksum, sourceErr = v.fetchChecksumValueFromRow(sourceRow)
}()
go func() {
defer wg.Done()
query := fmt.Sprintf("CHECKSUM TABLE %s EXTENDED", targetTable)
targetRow := v.TargetDB.QueryRow(query)
targetChecksum, targetErr = v.fetchChecksumValueFromRow(targetRow)
}()
wg.Wait()
if sourceErr != nil {
logWithTable.WithError(sourceErr).Error("failed to checksum table on the source")
return VerificationResult{}, sourceErr
}
if targetErr != nil {
logWithTable.WithError(targetErr).Error("failed to checksum table on the target")
return VerificationResult{}, targetErr
}
logFields := logrus.Fields{
"sourceChecksum": sourceChecksum,
"targetChecksum": targetChecksum,
}
if sourceChecksum == targetChecksum {
logWithTable.WithFields(logFields).Info("tables on source and target verified to match")
} else {
logWithTable.WithFields(logFields).Error("tables on source and target DOES NOT MATCH")
return VerificationResult{false, fmt.Sprintf("data on table %s (%s) mismatched", sourceTable, targetTable)}, nil
}
}
return VerificationResult{true, ""}, nil
}
func (v *ChecksumTableVerifier) fetchChecksumValueFromRow(row *sql.Row) (int64, error) {
var tablename string
var checksum sql.NullInt64
err := row.Scan(&tablename, &checksum)
if err != nil {
return int64(0), err
}
if !checksum.Valid {
return int64(0), fmt.Errorf("cannot find table %s during verification", tablename)
}
return checksum.Int64, nil
}
func (v *ChecksumTableVerifier) StartInBackground() error {
if v.SourceDB == nil || v.TargetDB == nil {
return errors.New("must specify source and target db")
}
if v.started != nil && v.started.Get() && !v.verificationResultAndStatus.IsDone() {
return errors.New("verification is on going")
}
v.started = new(AtomicBoolean)
v.started.Set(true)
// Initialize/reset all variables
v.verificationResultAndStatus = VerificationResultAndStatus{
StartTime: time.Now(),
DoneTime: time.Time{},
}
v.verificationErr = nil
v.logger = logrus.WithField("tag", "checksum_verifier")
v.wg = &sync.WaitGroup{}
v.logger.Info("checksum table verification started")
v.wg.Add(1)
go func() {
defer v.wg.Done()
v.verificationResultAndStatus.VerificationResult, v.verificationErr = v.Verify()
v.verificationResultAndStatus.DoneTime = time.Now()
v.started.Set(false)
}()
return nil
}
func (v *ChecksumTableVerifier) Wait() {
v.wg.Wait()
}
func (v *ChecksumTableVerifier) Result() (VerificationResultAndStatus, error) {
return v.verificationResultAndStatus, v.verificationErr
}