This repository has been archived by the owner on Nov 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 188
/
file.go
419 lines (365 loc) · 12.9 KB
/
file.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
// 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 writer
import (
"os"
"path/filepath"
"sync"
"time"
"github.com/pingcap/errors"
"github.com/pingcap/parser"
gmysql "github.com/siddontang/go-mysql/mysql"
"github.com/siddontang/go-mysql/replication"
"github.com/siddontang/go/sync2"
"github.com/pingcap/dm/pkg/binlog"
"github.com/pingcap/dm/pkg/binlog/common"
"github.com/pingcap/dm/pkg/binlog/event"
bw "github.com/pingcap/dm/pkg/binlog/writer"
"github.com/pingcap/dm/pkg/log"
)
// FileConfig is the configuration used by the FileWriter.
type FileConfig struct {
RelayDir string // directory to store relay log files.
Filename string // the startup relay log filename, if not set then a fake RotateEvent must be the first event.
}
// FileWriter implements Writer interface.
type FileWriter struct {
cfg *FileConfig
mu sync.Mutex
stage common.Stage
// underlying binlog writer,
// it will be created/started until needed.
out *bw.FileWriter
// the parser often used to verify events's statement through parsing them.
parser *parser.Parser
filename sync2.AtomicString // current binlog filename
}
// NewFileWriter creates a FileWriter instances.
func NewFileWriter(cfg *FileConfig, parser2 *parser.Parser) Writer {
w := &FileWriter{
cfg: cfg,
parser: parser2,
}
w.filename.Set(cfg.Filename) // set the startup filename
return w
}
// Start implements Writer.Start.
func (w *FileWriter) Start() error {
w.mu.Lock()
defer w.mu.Unlock()
if w.stage != common.StageNew {
return errors.Errorf("stage %s, expect %s, already started", w.stage, common.StageNew)
}
w.stage = common.StagePrepared
return nil
}
// Close implements Writer.Close.
func (w *FileWriter) Close() error {
w.mu.Lock()
defer w.mu.Unlock()
if w.stage != common.StagePrepared {
return errors.Errorf("stage %s, expect %s, can not close", w.stage, common.StagePrepared)
}
var err error
if w.out != nil {
err = w.out.Close()
}
w.stage = common.StageClosed
return errors.Trace(err)
}
// Recover implements Writer.Recover.
func (w *FileWriter) Recover() (*RecoverResult, error) {
w.mu.Lock()
defer w.mu.Unlock()
if w.stage != common.StagePrepared {
return nil, errors.Errorf("stage %s, expect %s, please start the writer first", w.stage, common.StagePrepared)
}
return w.doRecovering()
}
// WriteEvent implements Writer.WriteEvent.
func (w *FileWriter) WriteEvent(ev *replication.BinlogEvent) (*Result, error) {
w.mu.Lock()
defer w.mu.Unlock()
if w.stage != common.StagePrepared {
return nil, errors.Errorf("stage %s, expect %s, please start the writer first", w.stage, common.StagePrepared)
}
switch ev.Event.(type) {
case *replication.FormatDescriptionEvent:
return w.handleFormatDescriptionEvent(ev)
case *replication.RotateEvent:
return w.handleRotateEvent(ev)
default:
return w.handleEventDefault(ev)
}
}
// Flush implements Writer.Flush.
func (w *FileWriter) Flush() error {
w.mu.Lock()
defer w.mu.Unlock()
if w.stage != common.StagePrepared {
return errors.Errorf("stage %s, expect %s, please start the writer first", w.stage, common.StagePrepared)
}
if w.out != nil {
return w.out.Flush()
}
return errors.New("no underlying writer opened")
}
// offset returns the current offset of the binlog file.
// it is only used for testing now.
func (w *FileWriter) offset() int64 {
if w.out == nil {
return 0
}
status := w.out.Status().(*bw.FileWriterStatus)
return status.Offset
}
// handle FormatDescriptionEvent:
// 1. close the previous binlog file
// 2. open/create a new binlog file
// 3. write the binlog file header if not exists
// 4. write the FormatDescriptionEvent if not exists one
func (w *FileWriter) handleFormatDescriptionEvent(ev *replication.BinlogEvent) (*Result, error) {
// close the previous binlog file
if w.out != nil {
log.Infof("[relay] closing previous underlying binlog writer with status %v", w.out.Status())
err := w.out.Close()
if err != nil {
return nil, errors.Annotate(err, "close previous underlying binlog writer")
}
}
// verify filename
err := binlog.VerifyBinlogFilename(w.filename.Get())
if err != nil {
return nil, errors.Trace(err)
}
// open/create a new binlog file
filename := filepath.Join(w.cfg.RelayDir, w.filename.Get())
outCfg := &bw.FileWriterConfig{
Filename: filename,
}
out := bw.NewFileWriter(outCfg)
err = out.Start()
if err != nil {
return nil, errors.Annotatef(err, "start underlying binlog writer for %s", filename)
}
w.out = out.(*bw.FileWriter)
log.Infof("[relay] open underlying binlog writer with status %v", w.out.Status())
// write the binlog file header if not exists
exist, err := checkBinlogHeaderExist(filename)
if err != nil {
return nil, errors.Annotatef(err, "check binlog file header for %s", filename)
} else if !exist {
err = w.out.Write(replication.BinLogFileHeader)
if err != nil {
return nil, errors.Annotatef(err, "write binlog file header for %s", filename)
}
}
// write the FormatDescriptionEvent if not exists one
exist, err = checkFormatDescriptionEventExist(filename)
if err != nil {
return nil, errors.Annotatef(err, "check FormatDescriptionEvent for %s", filename)
} else if !exist {
err = w.out.Write(ev.RawData)
if err != nil {
return nil, errors.Annotatef(err, "write FormatDescriptionEvent %+v for %s", ev.Header, filename)
}
}
return &Result{
Ignore: exist, // ignore if exists
}, nil
}
// handle RotateEvent:
// 1. update binlog filename if needed
// 2. write the RotateEvent if not fake
// NOTE: we only see fake event for RotateEvent in MySQL source code,
// if see fake event for other event type, then handle them.
// NOTE: we do not create a new binlog file when received a RotateEvent,
// instead, we create a new binlog file when received a FormatDescriptionEvent.
// because a binlog file without any events has no meaning.
func (w *FileWriter) handleRotateEvent(ev *replication.BinlogEvent) (result *Result, err error) {
rotateEv, ok := ev.Event.(*replication.RotateEvent)
if !ok {
return nil, errors.NotValidf("except RotateEvent, but got %+v", ev.Header)
}
var currFile = w.filename.Get()
defer func() {
if err == nil {
// update binlog filename if needed
nextFile := string(rotateEv.NextLogName)
if nextFile > currFile {
// record the next filename, but not create it.
// even it's a fake RotateEvent, we still need to record it,
// because if we do not specify the filename when creating the writer (like Auto-Position),
// we can only receive a fake RotateEvent before the FormatDescriptionEvent.
w.filename.Set(nextFile)
}
}
}()
// write the RotateEvent if not fake
if ev.Header.Timestamp == 0 || ev.Header.LogPos == 0 {
// skip fake rotate event
return &Result{
Ignore: true,
}, nil
} else if w.out == nil {
// if not open a binlog file yet, then non-fake RotateEvent can't be handled
return nil, errors.Errorf("non-fake RotateEvent %+v received, but no binlog file opened", ev.Header)
}
result, err = w.handlePotentialHoleOrDuplicate(ev)
if err != nil {
return nil, errors.Trace(err)
} else if result.Ignore {
return result, nil
}
err = w.out.Write(ev.RawData)
if err != nil {
return nil, errors.Annotatef(err, "write RotateEvent %+v for %s", ev.Header, filepath.Join(w.cfg.RelayDir, currFile))
}
return &Result{
Ignore: false,
}, nil
}
// handle non-special event:
// 1. handle a potential hole if exists
// 2. handle any duplicate events if exist
// 3. write the non-duplicate event
func (w *FileWriter) handleEventDefault(ev *replication.BinlogEvent) (*Result, error) {
result, err := w.handlePotentialHoleOrDuplicate(ev)
if err != nil {
return nil, errors.Trace(err)
} else if result.Ignore {
return result, nil
}
// write the non-duplicate event
err = w.out.Write(ev.RawData)
return &Result{
Ignore: false,
}, errors.Annotatef(err, "write event %+v", ev.Header)
}
// handlePotentialHoleOrDuplicate combines handleFileHoleExist and handleDuplicateEventsExist.
func (w *FileWriter) handlePotentialHoleOrDuplicate(ev *replication.BinlogEvent) (*Result, error) {
// handle a potential hole
mayDuplicate, err := w.handleFileHoleExist(ev)
if err != nil {
return nil, errors.Annotatef(err, "handle a potential hole in %s before %+v",
w.filename.Get(), ev.Header)
}
if mayDuplicate {
// handle any duplicate events if exist
result, err2 := w.handleDuplicateEventsExist(ev)
if err2 != nil {
return nil, errors.Annotatef(err2, "handle a potential duplicate event %+v in %s",
ev.Header, w.filename.Get())
}
if result.Ignore {
// duplicate, and can ignore it. now, we assume duplicate events can all be ignored
return result, nil
}
}
return &Result{
Ignore: false,
}, nil
}
// handleFileHoleExist tries to handle a potential hole after this event wrote.
// A hole exists often because some binlog events not sent by the master.
// If no hole exists, then ev may be a duplicate event.
// NOTE: handle cases when file size > 4GB
func (w *FileWriter) handleFileHoleExist(ev *replication.BinlogEvent) (bool, error) {
// 1. detect whether a hole exists
evStartPos := int64(ev.Header.LogPos - ev.Header.EventSize)
outFs, ok := w.out.Status().(*bw.FileWriterStatus)
if !ok {
return false, errors.Errorf("invalid status type %T of the underlying writer", w.out.Status())
}
fileOffset := outFs.Offset
holeSize := evStartPos - fileOffset
if holeSize <= 0 {
// no hole exists, but duplicate events may exists, this should be handled in another place.
return holeSize < 0, nil
}
log.Infof("[relay] hole exist from %d to %d in %s", fileOffset, evStartPos, w.filename.Get())
// 2. generate dummy event
var (
header = &replication.EventHeader{
Timestamp: uint32(time.Now().Unix()),
ServerID: ev.Header.ServerID,
}
latestPos = uint32(fileOffset)
eventSize = uint32(holeSize)
)
dummyEv, err := event.GenDummyEvent(header, latestPos, eventSize)
if err != nil {
return false, errors.Annotatef(err, "generate dummy event at %d with size %d", latestPos, eventSize)
}
// 3. write the dummy event
err = w.out.Write(dummyEv.RawData)
return false, errors.Annotatef(err, "write dummy event %+v to fill the hole", dummyEv.Header)
}
// handleDuplicateEventsExist tries to handle a potential duplicate event in the binlog file.
func (w *FileWriter) handleDuplicateEventsExist(ev *replication.BinlogEvent) (*Result, error) {
filename := filepath.Join(w.cfg.RelayDir, w.filename.Get())
duplicate, err := checkIsDuplicateEvent(filename, ev)
if err != nil {
return nil, errors.Annotatef(err, "check event %+v whether duplicate in %s", ev.Header, filename)
} else if duplicate {
log.Infof("[relay] event %+v is duplicate in %s", ev.Header, w.filename.Get())
}
return &Result{
Ignore: duplicate,
}, nil
}
// doRecovering tries to recover the current binlog file.
// 1. read events from the file
// 2.
// a. update the position with the event's position if the transaction finished
// b. update the GTID set with the event's GTID if the transaction finished
// 3. truncate any incomplete events/transactions
// now, we think a transaction finished if we received a XIDEvent or DDL in QueryEvent
// NOTE: handle cases when file size > 4GB
func (w *FileWriter) doRecovering() (*RecoverResult, error) {
filename := filepath.Join(w.cfg.RelayDir, w.filename.Get())
// get latest pos/GTID set for all completed transactions from the file
latestPos, latestGTIDs, err := getTxnPosGTIDs(filename, w.parser)
if err != nil {
return nil, errors.Annotatef(err, "get latest pos/GTID set from %s", filename)
}
// in most cases, we think the file is fine, so compare the size is simpler.
fs, err := os.Stat(filename)
if err != nil {
return nil, errors.Annotatef(err, "get stat for %s", filename)
}
if fs.Size() == latestPos {
return &RecoverResult{
Recovered: false, // no recovering for the file
LatestPos: gmysql.Position{Name: w.filename.Get(), Pos: uint32(latestPos)},
LatestGTIDs: latestGTIDs,
}, nil
} else if fs.Size() < latestPos {
return nil, errors.Errorf("latest pos %d greater than file size %d, should not happen", latestPos, fs.Size())
}
// truncate the file
f, err := os.OpenFile(filename, os.O_WRONLY, 0644)
if err != nil {
return nil, errors.Annotatef(err, "open %s", filename)
}
defer f.Close()
err = f.Truncate(latestPos)
if err != nil {
return nil, errors.Annotatef(err, "truncate %s to %d", filename, latestPos)
}
return &RecoverResult{
Recovered: true,
LatestPos: gmysql.Position{Name: w.filename.Get(), Pos: uint32(latestPos)},
LatestGTIDs: latestGTIDs,
}, nil
}