/
gomaxscale.go
419 lines (362 loc) · 9.61 KB
/
gomaxscale.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
package gomaxscale
import (
"bytes"
"crypto/sha1"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net"
"strconv"
"time"
)
const initBufferSize = 1024
// Options stores all available options to connect with MaxScale.
type Options struct {
auth struct {
user string
password string
}
timeouts struct {
timeRef func() time.Time
read time.Duration
write time.Duration
}
uuid string
version *int
gtid string // Requested GTID position
bufferSize int
logger logger
}
// WithAuth sets the authentication options.
func WithAuth(user, password string) func(*Options) {
return func(o *Options) {
o.auth.user = user
o.auth.password = password
}
}
// WithGTID sets the GTID position.
func WithGTID(gtid string) func(*Options) {
return func(o *Options) {
o.gtid = gtid
}
}
// WithTimeout sets connection timeouts.
func WithTimeout(readTimeout, writeTimeout time.Duration) func(*Options) {
return func(o *Options) {
o.timeouts.read = readTimeout
o.timeouts.write = writeTimeout
}
}
// WithUUID sets the UUID of the client.
func WithUUID(uuid string) func(*Options) {
return func(o *Options) {
o.uuid = uuid
}
}
// WithVersion sets the binlog version to use.
func WithVersion(version int) func(*Options) {
return func(o *Options) {
o.version = &version
}
}
// WithBufferSize sets the buffer size for the data stream.
func WithBufferSize(bufferSize int) func(*Options) {
return func(o *Options) {
o.bufferSize = bufferSize
}
}
// WithLogger sets the logger to report issues when processing the data stream.
func WithLogger(logger logger) func(*Options) {
return func(o *Options) {
o.logger = logger
}
}
type logger interface {
Print(v ...interface{})
Printf(format string, v ...interface{})
}
// Consumer is a Go implementation of a MaxScale CDC consumer.
type Consumer struct {
address string
database string
table string
options Options
events chan Event
done chan bool
}
// NewConsumer creates a new Consumer instance.
func NewConsumer(address, database, table string, optFuncs ...func(*Options)) *Consumer {
var opts Options
opts.uuid = "XXX-YYY_YYY"
opts.timeouts.read = 2 * time.Second
opts.timeouts.write = 2 * time.Second
opts.timeouts.timeRef = time.Now
opts.bufferSize = 4096
opts.logger = log.Default()
for _, f := range optFuncs {
f(&opts)
}
return &Consumer{
address: address,
database: database,
table: table,
options: opts,
}
}
// Start connects to MaxScale and starts consuming events.
//
// https://mariadb.com/kb/en/mariadb-maxscale-6-change-data-capture-cdc-protocol/
func (g *Consumer) Start() (*DataStream, error) {
g.options.logger.Print("⚠️ This is an older version of the library with some protocol misconceptions. " +
"Please upgrade to a newer version.")
// if the consumer is already running, close it before starting a new one
if g.done != nil {
g.Close()
}
g.events = make(chan Event)
g.done = make(chan bool)
conn, err := net.Dial("tcp", g.address)
if err != nil {
return nil, err
}
//
// Authentication
//
var auth bytes.Buffer
if _, err := auth.WriteString(g.options.auth.user); err != nil {
g.disconnect(conn)
return nil, fmt.Errorf("failed to write username in authentication: %w", err)
}
if _, err := auth.WriteString(":"); err != nil {
g.disconnect(conn)
return nil, fmt.Errorf("failed to write separator in authentication: %w", err)
}
password := sha1.Sum([]byte(g.options.auth.password))
if _, err := auth.Write(password[:]); err != nil {
g.disconnect(conn)
return nil, fmt.Errorf("failed to write password in authentication: %w", err)
}
if err := g.writeInitRequest(conn, []byte(hex.EncodeToString(auth.Bytes()))); err != nil {
g.disconnect(conn)
return nil, fmt.Errorf("failed to authenticate: %w", err)
}
//
// Registration
//
if err := g.writeInitRequest(conn, []byte("REGISTER UUID="+g.options.uuid+", TYPE=JSON")); err != nil {
g.disconnect(conn)
return nil, fmt.Errorf("failed to register: %w", err)
}
//
// Data stream
//
var requestDataStream bytes.Buffer
requestDataStream.WriteString("REQUEST-DATA " + g.database + "." + g.table)
if g.options.version != nil {
requestDataStream.WriteString("." + strconv.FormatInt(int64(*g.options.version), 10))
}
if g.options.gtid != "" {
requestDataStream.WriteString(" " + g.options.gtid)
}
if _, err := conn.Write(requestDataStream.Bytes()); err != nil {
g.disconnect(conn)
return nil, fmt.Errorf("failed to request data stream: %w", err)
}
stream := stream{
conn: conn,
readTimeout: g.options.timeouts.read,
timeRef: g.options.timeouts.timeRef,
bufferSize: g.options.bufferSize,
}
//
// Initial event
//
response, err := stream.scan()
if err != nil {
g.disconnect(conn)
return nil, fmt.Errorf("failed to read initial data stream: %w", err)
}
var dataStream DataStream
if err := json.Unmarshal(response, &dataStream); err != nil {
g.disconnect(conn)
return nil, fmt.Errorf("failed to decode initial data stream '%s': %w", string(response), err)
}
//
// Events
//
go func() {
for {
select {
case <-g.done:
g.disconnect(conn)
return
default:
response, err := stream.scan()
if err != nil {
errCause := err
for errors.Unwrap(errCause) != nil {
errCause = errors.Unwrap(errCause)
}
if errCause == io.EOF {
g.disconnect(conn)
return
}
if netErr, ok := errCause.(net.Error); !ok || !netErr.Timeout() {
g.options.logger.Printf("failed to read from data stream: %s", err)
}
continue
} else if len(response) == 0 {
continue
}
var event Event
if err := json.Unmarshal(response, &event); err != nil {
g.options.logger.Printf("failed to decode '%s' from data stream: %s", string(response), err)
continue
}
event.RawData = response
g.events <- event
}
}
}()
return &dataStream, nil
}
// Process starts consuming events and trigerring the callback function for each
// event.
func (g *Consumer) Process(eventFunc func(Event)) {
for event := range g.events {
eventFunc(event)
}
}
// Close closes the connection to MaxScale.
func (g *Consumer) Close() {
defer func() {
// done channel could be already closed (generating a panic) if MaxScale
// server closed the connection
_ = recover()
}()
if g.done == nil {
return
}
g.done <- true
for {
// wait for the last event to be processed
if _, ok := <-g.events; !ok {
break
}
time.Sleep(time.Millisecond * 100)
}
}
func (g *Consumer) writeInitRequest(conn net.Conn, b []byte) error {
if err := conn.SetWriteDeadline(g.options.timeouts.timeRef().Add(g.options.timeouts.write)); err != nil {
return fmt.Errorf("failed to set write timeout: %w", err)
}
if _, err := conn.Write(b); err != nil {
return fmt.Errorf("failed to write: %w", err)
}
if err := conn.SetReadDeadline(g.options.timeouts.timeRef().Add(g.options.timeouts.read)); err != nil {
return fmt.Errorf("failed to set read timeout: %w", err)
}
response := make([]byte, initBufferSize)
n, err := conn.Read(response)
if err != nil {
return fmt.Errorf("failed to read response: %w", err)
}
response = response[:n]
return checkResponseError(response)
}
func (g *Consumer) disconnect(conn net.Conn) {
if err := conn.Close(); err != nil {
g.options.logger.Printf("failed to close connection: %s", err)
}
close(g.events)
close(g.done)
}
type stream struct {
conn net.Conn
readTimeout time.Duration
timeRef func() time.Time
buffer []byte
bufferSize int
}
func (s *stream) scan() ([]byte, error) {
var response bytes.Buffer
var identation int
var loops int
for {
if err := s.conn.SetReadDeadline(s.timeRef().Add(s.readTimeout)); err != nil {
return nil, fmt.Errorf("failed to set read timeout: %w", err)
}
tmpBuffer := make([]byte, s.bufferSize)
n, err := s.conn.Read(tmpBuffer)
if err != nil {
return nil, fmt.Errorf("failed to read response: %w", err)
}
tmpBuffer = tmpBuffer[:n]
if s.buffer != nil {
tmpBuffer = append(s.buffer, tmpBuffer...)
s.buffer = s.buffer[:0]
}
var i int
for _, b := range tmpBuffer {
if b == '{' {
identation++
} else if b == '}' {
identation--
}
i++
if identation == 0 {
break
}
}
response.Write(tmpBuffer[:i])
if identation == 0 {
if i < len(tmpBuffer) {
s.buffer = tmpBuffer[i:]
}
break
}
loops++
if loops > 100 {
return nil, errors.New("too many network iterations to find a json object")
}
}
return bytes.TrimSpace(response.Bytes()), checkResponseError(response.Bytes())
}
func checkResponseError(response []byte) error {
if bytes.Contains(bytes.ToLower(response), []byte("err")) {
return fmt.Errorf("error raised from maxscale: %s", string(response))
}
return nil
}
// DataStream is the first event response with the target table information.
//
// https://avro.apache.org/docs/1.11.0/spec.html#schemas
type DataStream struct {
Namespace string `json:"namespace"`
Type string `json:"type"`
Name string `json:"name"`
Table string `json:"table"`
Database string `json:"database"`
Version int `json:"version"`
GTID string `json:"gtid"`
Fields []struct {
Name string `json:"name"`
Type interface{} `json:"type"`
RealType string `json:"real_type"`
Length int `json:"length"`
Unsigned bool `json:"unsigned"`
} `json:"fields"`
}
// Event is a MaxScale event.
type Event struct {
Domain int `json:"domain"`
ServerID int `json:"server_id"`
Sequence int `json:"sequence"`
EventNumber int `json:"event_number"`
Timestamp int64 `json:"timestamp"`
Type string `json:"event_type"`
RawData []byte `json:"-"`
}