forked from go-mysql-org/go-mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplication_test.go
440 lines (346 loc) · 11.6 KB
/
replication_test.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package replication
import (
"context"
"flag"
"fmt"
"os"
"path"
"sync"
"testing"
"time"
. "github.com/pingcap/check"
uuid "github.com/satori/go.uuid"
"github.com/go-mysql-org/go-mysql/client"
"github.com/go-mysql-org/go-mysql/mysql"
)
// Use docker mysql to test, mysql is 3306, mariadb is 3316
var testHost = flag.String("host", "127.0.0.1", "MySQL master host")
var testOutputLogs = flag.Bool("out", false, "output binlog event")
func TestBinLogSyncer(t *testing.T) {
TestingT(t)
}
type testSyncerSuite struct {
b *BinlogSyncer
c *client.Conn
wg sync.WaitGroup
flavor string
}
var _ = Suite(&testSyncerSuite{})
func (t *testSyncerSuite) SetUpSuite(c *C) {
}
func (t *testSyncerSuite) TearDownSuite(c *C) {
}
func (t *testSyncerSuite) SetUpTest(c *C) {
}
func (t *testSyncerSuite) TearDownTest(c *C) {
if t.b != nil {
t.b.Close()
t.b = nil
}
if t.c != nil {
t.c.Close()
t.c = nil
}
}
func (t *testSyncerSuite) testExecute(c *C, query string) {
_, err := t.c.Execute(query)
c.Assert(err, IsNil)
}
func (t *testSyncerSuite) testSync(c *C, s *BinlogStreamer) {
t.wg.Add(1)
go func() {
defer t.wg.Done()
if s == nil {
return
}
eventCount := 0
for {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
e, err := s.GetEvent(ctx)
cancel()
if err == context.DeadlineExceeded {
eventCount += 1
return
}
c.Assert(err, IsNil)
if *testOutputLogs {
e.Dump(os.Stdout)
os.Stdout.Sync()
}
}
}()
//use mixed format
t.testExecute(c, "SET SESSION binlog_format = 'MIXED'")
str := `DROP TABLE IF EXISTS test_replication`
t.testExecute(c, str)
str = `CREATE TABLE test_replication (
id BIGINT(64) UNSIGNED NOT NULL AUTO_INCREMENT,
str VARCHAR(256),
f FLOAT,
d DOUBLE,
de DECIMAL(10,2),
i INT,
bi BIGINT,
e enum ("e1", "e2"),
b BIT(8),
y YEAR,
da DATE,
ts TIMESTAMP,
dt DATETIME,
tm TIME,
t TEXT,
bb BLOB,
se SET('a', 'b', 'c'),
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8`
t.testExecute(c, str)
//use row format
t.testExecute(c, "SET SESSION binlog_format = 'ROW'")
t.testExecute(c, `INSERT INTO test_replication (str, f, i, e, b, y, da, ts, dt, tm, de, t, bb, se)
VALUES ("3", -3.14, 10, "e1", 0b0011, 1985,
"2012-05-07", "2012-05-07 14:01:01", "2012-05-07 14:01:01",
"14:01:01", -45363.64, "abc", "12345", "a,b")`)
id := 100
if t.flavor == mysql.MySQLFlavor {
t.testExecute(c, "SET SESSION binlog_row_image = 'MINIMAL'")
t.testExecute(c, fmt.Sprintf(`INSERT INTO test_replication (id, str, f, i, bb, de) VALUES (%d, "4", -3.14, 100, "abc", -45635.64)`, id))
t.testExecute(c, fmt.Sprintf(`UPDATE test_replication SET f = -12.14, de = 555.34 WHERE id = %d`, id))
t.testExecute(c, fmt.Sprintf(`DELETE FROM test_replication WHERE id = %d`, id))
}
// check whether we can create the table including the json field
str = `DROP TABLE IF EXISTS test_json`
t.testExecute(c, str)
str = `CREATE TABLE test_json (
id BIGINT(64) UNSIGNED NOT NULL AUTO_INCREMENT,
c1 JSON,
c2 DECIMAL(10, 0),
PRIMARY KEY (id)
) ENGINE=InnoDB`
if _, err := t.c.Execute(str); err == nil {
t.testExecute(c, `INSERT INTO test_json (c2) VALUES (1)`)
t.testExecute(c, `INSERT INTO test_json (c1, c2) VALUES ('{"key1": "value1", "key2": "value2"}', 1)`)
}
t.testExecute(c, "DROP TABLE IF EXISTS test_json_v2")
str = `CREATE TABLE test_json_v2 (
id INT,
c JSON,
PRIMARY KEY (id)
) ENGINE=InnoDB`
if _, err := t.c.Execute(str); err == nil {
tbls := []string{
// Refer: https://github.com/shyiko/mysql-binlog-connector-java/blob/c8e81c879710dc19941d952f9031b0a98f8b7c02/src/test/java/com/github/shyiko/mysql/binlog/event/deserialization/json/JsonBinaryValueIntegrationTest.java#L84
// License: https://github.com/shyiko/mysql-binlog-connector-java#license
`INSERT INTO test_json_v2 VALUES (0, NULL)`,
`INSERT INTO test_json_v2 VALUES (1, '{\"a\": 2}')`,
`INSERT INTO test_json_v2 VALUES (2, '[1,2]')`,
`INSERT INTO test_json_v2 VALUES (3, '{\"a\":\"b\", \"c\":\"d\",\"ab\":\"abc\", \"bc\": [\"x\", \"y\"]}')`,
`INSERT INTO test_json_v2 VALUES (4, '[\"here\", [\"I\", \"am\"], \"!!!\"]')`,
`INSERT INTO test_json_v2 VALUES (5, '\"scalar string\"')`,
`INSERT INTO test_json_v2 VALUES (6, 'true')`,
`INSERT INTO test_json_v2 VALUES (7, 'false')`,
`INSERT INTO test_json_v2 VALUES (8, 'null')`,
`INSERT INTO test_json_v2 VALUES (9, '-1')`,
`INSERT INTO test_json_v2 VALUES (10, CAST(CAST(1 AS UNSIGNED) AS JSON))`,
`INSERT INTO test_json_v2 VALUES (11, '32767')`,
`INSERT INTO test_json_v2 VALUES (12, '32768')`,
`INSERT INTO test_json_v2 VALUES (13, '-32768')`,
`INSERT INTO test_json_v2 VALUES (14, '-32769')`,
`INSERT INTO test_json_v2 VALUES (15, '2147483647')`,
`INSERT INTO test_json_v2 VALUES (16, '2147483648')`,
`INSERT INTO test_json_v2 VALUES (17, '-2147483648')`,
`INSERT INTO test_json_v2 VALUES (18, '-2147483649')`,
`INSERT INTO test_json_v2 VALUES (19, '18446744073709551615')`,
`INSERT INTO test_json_v2 VALUES (20, '18446744073709551616')`,
`INSERT INTO test_json_v2 VALUES (21, '3.14')`,
`INSERT INTO test_json_v2 VALUES (22, '{}')`,
`INSERT INTO test_json_v2 VALUES (23, '[]')`,
`INSERT INTO test_json_v2 VALUES (24, CAST(CAST('2015-01-15 23:24:25' AS DATETIME) AS JSON))`,
`INSERT INTO test_json_v2 VALUES (25, CAST(CAST('23:24:25' AS TIME) AS JSON))`,
`INSERT INTO test_json_v2 VALUES (125, CAST(CAST('23:24:25.12' AS TIME(3)) AS JSON))`,
`INSERT INTO test_json_v2 VALUES (225, CAST(CAST('23:24:25.0237' AS TIME(3)) AS JSON))`,
`INSERT INTO test_json_v2 VALUES (26, CAST(CAST('2015-01-15' AS DATE) AS JSON))`,
`INSERT INTO test_json_v2 VALUES (27, CAST(TIMESTAMP'2015-01-15 23:24:25' AS JSON))`,
`INSERT INTO test_json_v2 VALUES (127, CAST(TIMESTAMP'2015-01-15 23:24:25.12' AS JSON))`,
`INSERT INTO test_json_v2 VALUES (227, CAST(TIMESTAMP'2015-01-15 23:24:25.0237' AS JSON))`,
`INSERT INTO test_json_v2 VALUES (327, CAST(UNIX_TIMESTAMP('2015-01-15 23:24:25') AS JSON))`,
`INSERT INTO test_json_v2 VALUES (28, CAST(ST_GeomFromText('POINT(1 1)') AS JSON))`,
`INSERT INTO test_json_v2 VALUES (29, CAST('[]' AS CHAR CHARACTER SET 'ascii'))`,
// TODO: 30 and 31 are BIT type from JSON_TYPE, may support later.
`INSERT INTO test_json_v2 VALUES (30, CAST(x'cafe' AS JSON))`,
`INSERT INTO test_json_v2 VALUES (31, CAST(x'cafebabe' AS JSON))`,
`INSERT INTO test_json_v2 VALUES (100, CONCAT('{\"', REPEAT('a', 64 * 1024 - 1), '\":123}'))`,
}
for _, query := range tbls {
t.testExecute(c, query)
}
// If MySQL supports JSON, it must supports GEOMETRY.
t.testExecute(c, "DROP TABLE IF EXISTS test_geo")
str = `CREATE TABLE test_geo (g GEOMETRY)`
_, err = t.c.Execute(str)
c.Assert(err, IsNil)
tbls = []string{
`INSERT INTO test_geo VALUES (POINT(1, 1))`,
`INSERT INTO test_geo VALUES (LINESTRING(POINT(0,0), POINT(1,1), POINT(2,2)))`,
// TODO: add more geometry tests
}
for _, query := range tbls {
t.testExecute(c, query)
}
}
str = `DROP TABLE IF EXISTS test_parse_time`
t.testExecute(c, str)
// Must allow zero time.
t.testExecute(c, `SET sql_mode=''`)
str = `CREATE TABLE test_parse_time (
a1 DATETIME,
a2 DATETIME(3),
a3 DATETIME(6),
b1 TIMESTAMP,
b2 TIMESTAMP(3) ,
b3 TIMESTAMP(6))`
t.testExecute(c, str)
t.testExecute(c, `INSERT INTO test_parse_time VALUES
("2014-09-08 17:51:04.123456", "2014-09-08 17:51:04.123456", "2014-09-08 17:51:04.123456",
"2014-09-08 17:51:04.123456","2014-09-08 17:51:04.123456","2014-09-08 17:51:04.123456"),
("0000-00-00 00:00:00.000000", "0000-00-00 00:00:00.000000", "0000-00-00 00:00:00.000000",
"0000-00-00 00:00:00.000000", "0000-00-00 00:00:00.000000", "0000-00-00 00:00:00.000000"),
("2014-09-08 17:51:04.000456", "2014-09-08 17:51:04.000456", "2014-09-08 17:51:04.000456",
"2014-09-08 17:51:04.000456","2014-09-08 17:51:04.000456","2014-09-08 17:51:04.000456")`)
t.wg.Wait()
}
func (t *testSyncerSuite) setupTest(c *C, flavor string) {
var port uint16 = 3306
switch flavor {
case mysql.MariaDBFlavor:
port = 3316
}
t.flavor = flavor
var err error
if t.c != nil {
t.c.Close()
}
t.c, err = client.Connect(fmt.Sprintf("%s:%d", *testHost, port), "root", "", "")
if err != nil {
c.Skip(err.Error())
}
_, err = t.c.Execute("CREATE DATABASE IF NOT EXISTS test")
c.Assert(err, IsNil)
_, err = t.c.Execute("USE test")
c.Assert(err, IsNil)
if t.b != nil {
t.b.Close()
}
cfg := BinlogSyncerConfig{
ServerID: 100,
Flavor: flavor,
Host: *testHost,
Port: port,
User: "root",
Password: "",
UseDecimal: true,
}
t.b = NewBinlogSyncer(cfg)
}
func (t *testSyncerSuite) testPositionSync(c *C) {
//get current master binlog file and position
r, err := t.c.Execute("SHOW MASTER STATUS")
c.Assert(err, IsNil)
binFile, _ := r.GetString(0, 0)
binPos, _ := r.GetInt(0, 1)
s, err := t.b.StartSync(mysql.Position{Name: binFile, Pos: uint32(binPos)})
c.Assert(err, IsNil)
// check we have set Slave_UUID
r, err = t.c.Execute("SHOW SLAVE HOSTS")
c.Assert(err, IsNil)
slaveUUID, _ := r.GetString(0, 4)
c.Assert(slaveUUID, HasLen, 36)
// Test re-sync.
time.Sleep(100 * time.Millisecond)
_ = t.b.c.SetReadDeadline(time.Now().Add(time.Millisecond))
time.Sleep(100 * time.Millisecond)
t.testSync(c, s)
}
func (t *testSyncerSuite) TestMysqlPositionSync(c *C) {
t.setupTest(c, mysql.MySQLFlavor)
t.testPositionSync(c)
}
func (t *testSyncerSuite) TestMysqlGTIDSync(c *C) {
t.setupTest(c, mysql.MySQLFlavor)
r, err := t.c.Execute("SELECT @@gtid_mode")
c.Assert(err, IsNil)
modeOn, _ := r.GetString(0, 0)
if modeOn != "ON" {
c.Skip("GTID mode is not ON")
}
r, err = t.c.Execute("SHOW GLOBAL VARIABLES LIKE 'SERVER_UUID'")
c.Assert(err, IsNil)
var masterUuid uuid.UUID
if s, _ := r.GetString(0, 1); len(s) > 0 && s != "NONE" {
masterUuid, err = uuid.FromString(s)
c.Assert(err, IsNil)
}
set, _ := mysql.ParseMysqlGTIDSet(fmt.Sprintf("%s:%d-%d", masterUuid.String(), 1, 2))
s, err := t.b.StartSyncGTID(set)
c.Assert(err, IsNil)
t.testSync(c, s)
}
func (t *testSyncerSuite) TestMariadbPositionSync(c *C) {
t.setupTest(c, mysql.MariaDBFlavor)
t.testPositionSync(c)
}
func (t *testSyncerSuite) TestMariadbGTIDSync(c *C) {
t.setupTest(c, mysql.MariaDBFlavor)
// get current master gtid binlog pos
r, err := t.c.Execute("SELECT @@gtid_binlog_pos")
c.Assert(err, IsNil)
str, _ := r.GetString(0, 0)
set, _ := mysql.ParseMariadbGTIDSet(str)
s, err := t.b.StartSyncGTID(set)
c.Assert(err, IsNil)
t.testSync(c, s)
}
func (t *testSyncerSuite) TestMariadbAnnotateRows(c *C) {
t.setupTest(c, mysql.MariaDBFlavor)
t.b.cfg.DumpCommandFlag = BINLOG_SEND_ANNOTATE_ROWS_EVENT
t.testPositionSync(c)
}
func (t *testSyncerSuite) TestMysqlSemiPositionSync(c *C) {
t.setupTest(c, mysql.MySQLFlavor)
t.b.cfg.SemiSyncEnabled = true
t.testPositionSync(c)
}
func (t *testSyncerSuite) TestMysqlBinlogCodec(c *C) {
t.setupTest(c, mysql.MySQLFlavor)
t.testExecute(c, "RESET MASTER")
var wg sync.WaitGroup
wg.Add(1)
defer wg.Wait()
go func() {
defer wg.Done()
t.testSync(c, nil)
t.testExecute(c, "FLUSH LOGS")
t.testSync(c, nil)
}()
binlogDir := "./var"
os.RemoveAll(binlogDir)
err := t.b.StartBackup(binlogDir, mysql.Position{Name: "", Pos: uint32(0)}, 2*time.Second)
c.Assert(err, IsNil)
p := NewBinlogParser()
p.SetVerifyChecksum(true)
f := func(e *BinlogEvent) error {
if *testOutputLogs {
e.Dump(os.Stdout)
os.Stdout.Sync()
}
return nil
}
dir, err := os.Open(binlogDir)
c.Assert(err, IsNil)
defer dir.Close()
files, err := dir.Readdirnames(-1)
c.Assert(err, IsNil)
for _, file := range files {
err = p.ParseFile(path.Join(binlogDir, file), 0, f)
c.Assert(err, IsNil)
}
}