-
Notifications
You must be signed in to change notification settings - Fork 218
Expand file tree
/
Copy pathsql.go
More file actions
238 lines (210 loc) · 5.65 KB
/
sql.go
File metadata and controls
238 lines (210 loc) · 5.65 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
/*
* Radon
*
* Copyright 2018 The Radon Authors.
* Code is licensed under the GPLv3.
*
*/
package binlog
import (
"io"
"os"
"path"
"time"
"config"
"xbase"
"xbase/sync2"
"github.com/xelabs/go-mysqlstack/common"
"github.com/xelabs/go-mysqlstack/xlog"
)
// SQLWorker tuple.
type SQLWorker struct {
log *xlog.Log
rfile xbase.RotateFile
// gtid is timestamp,
// (time.Now().UTC().UnixNano(), format as '1514254947594569594'
binDir string
currFile *os.File
id int64
seekTimestamp int64
stop sync2.AtomicBool
currPos sync2.AtomicInt64
currTimestamp sync2.AtomicInt64
currBinName sync2.AtomicString
}
// NewSQLWorker creates the new SQLWorker.
func NewSQLWorker(log *xlog.Log, conf *config.BinlogConfig, ts int64) *SQLWorker {
return &SQLWorker{
log: log,
binDir: conf.LogDir,
seekTimestamp: ts,
rfile: xbase.NewRotateFile(conf.LogDir, prefix, extension, conf.MaxSize),
}
}
func (sql *SQLWorker) close() {
sql.stop.Set(true)
if sql.currFile != nil {
sql.currFile.Close()
}
sql.rfile.Close()
sql.currPos.Set(0)
sql.currBinName.Set("")
sql.log.Info("sqlworker.closed")
}
// RelayName returns the current binlog which are read.
func (sql *SQLWorker) RelayName() string {
return sql.currBinName.Get()
}
// RelayPosition returns the current binlog position which are read.
func (sql *SQLWorker) RelayPosition() int64 {
return sql.currPos.Get()
}
// RelayGTID returns the last event timestamp have read.
func (sql *SQLWorker) RelayGTID() int64 {
return sql.currTimestamp.Get()
}
// SeekGTID returns the timestamp which we started.
func (sql *SQLWorker) SeekGTID() int64 {
return sql.seekTimestamp
}
func (sql *SQLWorker) setID(id int64) {
sql.id = id
}
func (sql *SQLWorker) readOneEvent() (*Event, error) {
// No any binlog files in binlog dir, we return the EOF error.
if sql.currBinName.Get() == "" {
return nil, io.EOF
}
pos := sql.currPos.Get()
// 1. Read the event length datas, 4bytes.
lenDatas := make([]byte, 4)
_, err := sql.currFile.ReadAt(lenDatas, sql.currPos.Get())
if err != nil {
return nil, err
}
buf := common.ReadBuffer(lenDatas)
len, err := buf.ReadU32()
if err != nil {
return nil, err
}
// 2. Read the event datas.
datas := make([]byte, len)
_, err = sql.currFile.ReadAt(datas, sql.currPos.Get()+4)
if err != nil {
return nil, err
}
// 3. Unpack the event.
event, err := unpackEvent(datas)
if err != nil {
return nil, err
}
// Set the position at last.
sql.currPos.Add(4)
sql.currPos.Add(int64(len))
endLogPos := sql.currPos.Get()
event.Pos = pos
event.LogName = sql.currBinName.Get()
event.EndLogPos = endLogPos
return event, nil
}
func (sql *SQLWorker) seekToEvent(ts int64) error {
prevPos := sql.currPos.Get()
for !sql.stop.Get() {
event, err := sql.readOneEvent()
if err != nil {
// We have got the end of the current binlog.
if err == io.EOF {
return nil
}
return err
}
// Find the first larger event, we should stop.
if event.Timestamp > uint64(ts) {
// Reset the position to the previous.
sql.currPos.Set(prevPos)
return nil
}
// Reset the position.
prevPos = sql.currPos.Get()
}
return nil
}
// Init used to init the sql current position and seek to the right event.
func (sql *SQLWorker) Init() error {
log := sql.log
if sql.currBinName.Get() == "" {
currLogInfo, err := sql.rfile.GetCurrLogInfo(sql.seekTimestamp)
if err != nil {
log.Error("binlog.sql.init.get.current[seekts:%v].loginfo.error:%v", sql.seekTimestamp, err)
return err
}
if currLogInfo.Name != "" {
log.Info("sqlworker.init.currlog[%v].seekts[%v, %v]", currLogInfo.Name, sql.seekTimestamp, time.Unix(0, sql.seekTimestamp))
file, err := os.Open(path.Join(sql.binDir, currLogInfo.Name))
if err != nil {
return err
}
sql.currPos.Set(0)
sql.currFile = file
sql.currBinName.Set(currLogInfo.Name)
return sql.seekToEvent(sql.seekTimestamp)
}
}
return nil
}
func (sql *SQLWorker) checkNextFileExists() bool {
log := sql.log
logInfo, err := sql.rfile.GetNextLogInfo(sql.currBinName.Get())
if err != nil {
log.Error("binlog.sql.get.next.log.curr[%s].error:%v", sql.currBinName.Get(), err)
return false
}
if logInfo.Name == "" {
return false
}
if logInfo.Name != sql.currBinName.Get() {
// Here, we make sure the next file is exists, but we check that whether a new write(stale) to the sql.currBinName binlog file.
if sql.currBinName.Get() != "" {
fileInfo, err := os.Lstat(path.Join(sql.binDir, sql.currBinName.Get()))
if err != nil {
log.Error("binlog.sql.check.next.file.stat[%s].error:%v", sql.currBinName.Get(), err)
return false
}
size := fileInfo.Size()
if sql.currPos.Get() < size {
log.Warning("binlog.sql.found.stale.write.size[%v].currpos[%v]", size, sql.currPos.Get())
return false
}
}
// Rotate to the next binlog file.
file, err := os.Open(path.Join(sql.binDir, logInfo.Name))
if err != nil {
log.Error("binlog.sql.check.next.file.error:%v", err)
return false
}
sql.currFile.Close()
sql.currFile = file
sql.currPos.Set(0)
sql.currBinName.Set(logInfo.Name)
return true
}
// We don't have next the binlog file.
return false
}
// NextEvent used to read the next event.
// If we get the end of the current binlog file and don't have next binlog, just returns (nil,nil).
func (sql *SQLWorker) NextEvent() (*Event, error) {
event, err := sql.readOneEvent()
if err != nil {
if err == io.EOF {
if !sql.checkNextFileExists() {
return nil, nil
}
// Changed to the next binlog file, read next event from the new file.
return sql.NextEvent()
}
return nil, err
}
sql.currTimestamp.Set(int64(event.Timestamp))
return event, nil
}