-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
412 lines (343 loc) · 11.1 KB
/
client.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
package cdc
import (
"bufio"
"bytes"
"crypto/sha1"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"log/slog"
"net"
"sync"
"time"
)
const maxScanTokenSize = 1024 * 1024
var (
// ErrNotConnected is returned when trying to stop the client from streaming
// CDC events while it is not running.
ErrNotConnected = errors.New("not connected")
)
var errPrefix = []byte("ERR")
var (
defaultLogger = slog.Default()
)
const (
defaultDialTimeout = time.Second * 5
defaultReadTimeout = time.Second * 5
defaultWriteTimeout = time.Second * 5
)
type clientOptions struct {
dialTimeout time.Duration
readTimeout time.Duration
writeTimeout time.Duration
logger *slog.Logger
}
// ClientOption is a function option used to parameterize a CDC client.
type ClientOption func(*clientOptions)
// WithDialTimeout sets the timeout of the dial call when creating the
// connection with the MaxScale protocol listener.
func WithDialTimeout(timeout time.Duration) ClientOption {
return func(co *clientOptions) {
co.readTimeout = timeout
}
}
// WithReadTimeout sets the timeout for all read calls over the connection.
func WithReadTimeout(timeout time.Duration) ClientOption {
return func(co *clientOptions) {
co.readTimeout = timeout
}
}
// WithReadTimeout sets the timeout for all write calls over the connection.
func WithWriteTimeout(timeout time.Duration) ClientOption {
return func(co *clientOptions) {
co.readTimeout = timeout
}
}
// WithLogger sets the logger used by the client.
func WithLogger(logger *slog.Logger) ClientOption {
return func(co *clientOptions) {
co.logger = logger
}
}
// Client represents a MaxScale CDC client.
type Client struct {
address string
user string
password string
uuid string
conn net.Conn
done chan struct{}
wg sync.WaitGroup
clientOptions
}
// NewClient returns a new MaxScale CDC Client given an address to connect to,
// credentials to authenticate and an UUID to register the client.
func NewClient(address, user, password, uuid string, opts ...ClientOption) *Client {
client := &Client{
address: address,
user: user,
password: password,
uuid: uuid,
clientOptions: clientOptions{
dialTimeout: defaultDialTimeout,
readTimeout: defaultReadTimeout,
writeTimeout: defaultWriteTimeout,
logger: defaultLogger,
},
}
for _, opt := range opts {
opt(&client.clientOptions)
}
return client
}
// RequestDataOption is a functional option to parameterize a RequestData call.
type RequestDataOption func(*requestDataOptions)
// WithVersion specifies the version of the table from which the event will be streamed.
func WithVersion(version string) RequestDataOption {
return func(rdo *requestDataOptions) {
rdo.version = version
}
}
// WithGTID specifies the GTID position where the events should start being streamed.
func WithGTID(gtid string) RequestDataOption {
return func(rdo *requestDataOptions) {
rdo.gtid = gtid
}
}
type requestDataOptions struct {
version string
gtid string
}
// RequestData starts fetching events from a given table and database.
// Optionally the version of the schema used and the GTID of the transaction
// from which the event will start being streamed can be set through options.
//
// If the schema file associated to the table is not present on the MaxScale
// filesystem no error is returned. Instead it logs an error and wait until it
// gets created to start streaming events.
//
// Call Stop to close the connection and release the associated ressources when done.
//
// Also see https://mariadb.com/kb/en/mariadb-maxscale-6-change-data-capture-cdc-protocol/#request-data
func (c *Client) RequestData(database, table string, opts ...RequestDataOption) (<-chan Event, error) {
if err := c.connect(); err != nil {
return nil, fmt.Errorf("failed to establish connection: %w", err)
}
if err := c.authenticate(); err != nil {
return nil, fmt.Errorf("failed to authenticate: %w", err)
}
if err := c.register(); err != nil {
return nil, fmt.Errorf("failed to register: %w", err)
}
var options requestDataOptions
for _, opt := range opts {
opt(&options)
}
return c.requestData(database, table, options.version, options.gtid)
}
// Stop closes the open connection if any and wait for all the ressources
// in use to be released.
func (c *Client) Stop() error {
if c.conn == nil {
return ErrNotConnected
}
// Close the connection to stop the blocking read call.
err := c.conn.Close()
// In case the goroutine handling the events was trying to send an event
// on the channel, we tell it to stop so we can close the event channel safely.
close(c.done)
c.wg.Wait()
return err
}
// See https://mariadb.com/kb/en/mariadb-maxscale-6-change-data-capture-cdc-protocol/#connection-and-authentication
func (c *Client) connect() error {
dialer := &net.Dialer{
Timeout: c.dialTimeout,
}
conn, err := dialer.Dial("tcp", c.address)
if err != nil {
return fmt.Errorf("could not connect to %s over TCP: %w", c.address, err)
}
c.conn = conn
return nil
}
// See https://mariadb.com/kb/en/mariadb-maxscale-6-change-data-capture-cdc-protocol/#connection-and-authentication
func (c *Client) authenticate() error {
authMsg, err := c.formatAuthenticationCommand(c.user, c.password)
if err != nil {
return fmt.Errorf("could not format the authentication command: %w", err)
}
if err = c.writeToConnection(authMsg); err != nil {
return fmt.Errorf("could not write the authentication message to the connection: %w", err)
}
return c.checkResponse()
}
// See https://mariadb.com/kb/en/mariadb-maxscale-6-change-data-capture-cdc-protocol/#registration_1
func (c *Client) register() error {
if err := c.writeToConnection([]byte("REGISTER UUID=" + c.uuid + ", TYPE=JSON")); err != nil {
return fmt.Errorf("could not write UUID %s to the connection: %w", c.uuid, err)
}
return c.checkResponse()
}
// See https://mariadb.com/kb/en/mariadb-maxscale-6-change-data-capture-cdc-protocol/#request-data
func (c *Client) requestData(database, table, version, gtid string) (<-chan Event, error) {
events := make(chan Event, 1)
c.done = make(chan struct{}, 1)
cmd, err := c.formatRequestDataCommand(database, table, version, gtid)
if err != nil {
return nil, fmt.Errorf("could not format the REQUEST-DATA command: %w", err)
}
if err := c.writeToConnection(cmd); err != nil {
return nil, fmt.Errorf("could not write the REQUEST-DATA command to the connection: %w", err)
}
if err := c.conn.SetReadDeadline(time.Time{}); err != nil {
return nil, fmt.Errorf("could not reset the read deadline on the connection: %w", err)
}
c.wg.Add(1)
go func() {
defer c.wg.Done()
if err := c.handleEvents(events); err != nil {
c.logger.Error("An error happened while decoding CDC events",
slog.Any("err", err),
slog.String("database", database),
slog.String("table", table),
)
}
close(events)
}()
return events, nil
}
func (c *Client) handleEvents(data chan<- Event) error {
var readSchema bool
scanner := bufio.NewScanner(c.conn)
buf := make([]byte, 0, maxScanTokenSize)
scanner.Buffer(buf, maxScanTokenSize)
for scanner.Scan() {
token := scanner.Bytes()
// If the request for data is rejected, an error will be sent instead of the table schema.
if !readSchema && isErrorResponse(token) {
c.logger.Warn("Failed to read the table schema",
slog.String("error", string(token)),
)
continue
}
if !readSchema {
readSchema = true
}
event, err := c.decodeEvent(token)
if err != nil {
return err
}
// If the client is stopped, we stop sending events on the channel.
// Otherwise it would block forever if no one is receiving from it.
select {
case data <- event:
case <-c.done:
return nil
}
}
return scanner.Err()
}
func (c *Client) decodeEvent(data []byte) (Event, error) {
var (
event Event
err error
)
if isDMLEvent(data) {
if event, err = c.decodeDMLEvent(data); err != nil {
return nil, fmt.Errorf("failed to decode DML event(%s): %w", data, err)
}
} else {
if event, err = c.decodeDDLEvent(data); err != nil {
return nil, fmt.Errorf("failed to decode DDL event(%s): %w", data, err)
}
}
return event, nil
}
func (c *Client) decodeDMLEvent(data []byte) (*DMLEvent, error) {
var event DMLEvent
if err := json.Unmarshal(data, &event); err != nil {
return nil, fmt.Errorf("failed to unmarshal the DML event data into a go value: %w", err)
}
event.Raw = make([]byte, len(data))
copy(event.Raw, data)
return &event, nil
}
func (c *Client) decodeDDLEvent(data []byte) (*DDLEvent, error) {
var event DDLEvent
if err := json.Unmarshal(data, &event); err != nil {
return nil, fmt.Errorf("failed to unmarshal the DDL event data into a go value: %w", err)
}
return &event, nil
}
func (c *Client) formatAuthenticationCommand(user, password string) ([]byte, error) {
var buf bytes.Buffer
_, err := buf.WriteString(user + ":")
if err != nil {
return nil, fmt.Errorf("could not write username %q in the authentication message: %w", user, err)
}
h := sha1.New()
_, err = h.Write([]byte(password))
if err != nil {
return nil, fmt.Errorf("could not hash the user password: %w", err)
}
sha1Password := h.Sum(nil)
_, err = buf.Write(sha1Password)
if err != nil {
return nil, fmt.Errorf("could not write password in the authentication message: %w", err)
}
authMsg := make([]byte, hex.EncodedLen(len(buf.Bytes())))
_ = hex.Encode(authMsg, buf.Bytes())
return authMsg, nil
}
func (c *Client) formatRequestDataCommand(database, table, version, gtid string) ([]byte, error) {
var command bytes.Buffer
if _, err := command.WriteString("REQUEST-DATA " + database + "." + table); err != nil {
return nil, fmt.Errorf("could not write the REQUEST-DATA command to the buffer: %w", err)
}
if version != "" {
if _, err := command.WriteString("." + version); err != nil {
return nil, fmt.Errorf("could not add the version %q to the REQUEST-DATA command in the buffer: %w", version, err)
}
}
if gtid != "" {
if _, err := command.WriteString(" " + gtid); err != nil {
return nil, fmt.Errorf("could not add the GTID %q to the REQUEST-DATA command in the buffer: %w", gtid, err)
}
}
return command.Bytes(), nil
}
func (c *Client) writeToConnection(b []byte) error {
if err := c.conn.SetWriteDeadline(time.Now().Add(c.writeTimeout)); err != nil {
return fmt.Errorf("could not set write deadline to the future write call on the connection: %w", err)
}
if _, err := c.conn.Write(b); err != nil {
return fmt.Errorf("could not write the authentication message to the connection: %w", err)
}
return nil
}
func (c *Client) readResponse() ([]byte, error) {
if err := c.conn.SetReadDeadline(time.Now().Add(c.readTimeout)); err != nil {
return nil, fmt.Errorf("could not set read deadline to the future read call on the connection: %w", err)
}
s := bufio.NewScanner(c.conn)
s.Scan()
return s.Bytes(), s.Err()
}
func (c *Client) checkResponse() error {
resp, err := c.readResponse()
if err != nil {
return err
}
if isErrorResponse(resp) {
return errors.New(string(resp))
}
return nil
}
func isErrorResponse(resp []byte) bool {
return bytes.HasPrefix(resp, errPrefix)
}
func isDMLEvent(data []byte) bool {
return bytes.HasPrefix(data, []byte(`{"domain":`))
}