|
14 | 14 | package syncer
|
15 | 15 |
|
16 | 16 | import (
|
| 17 | + "context" |
| 18 | + "database/sql" |
| 19 | + "fmt" |
| 20 | + "time" |
| 21 | + |
| 22 | + "github.com/pingcap/dm/dm/config" |
| 23 | + "github.com/pingcap/dm/pkg/utils" |
| 24 | + |
17 | 25 | . "github.com/pingcap/check"
|
| 26 | + "github.com/pingcap/tidb-tools/pkg/filter" |
18 | 27 | gouuid "github.com/satori/go.uuid"
|
19 | 28 | "github.com/siddontang/go-mysql/mysql"
|
20 |
| - |
21 |
| - "github.com/pingcap/dm/pkg/utils" |
| 29 | + "github.com/siddontang/go-mysql/replication" |
22 | 30 | )
|
23 | 31 |
|
24 |
| -func (s *testSyncerSuite) TestGetServerUUID(c *C) { |
| 32 | +var _ = Suite(&testDBSuite{}) |
| 33 | + |
| 34 | +type testDBSuite struct { |
| 35 | + db *sql.DB |
| 36 | + syncer *replication.BinlogSyncer |
| 37 | + streamer *replication.BinlogStreamer |
| 38 | + cfg *config.SubTaskConfig |
| 39 | +} |
| 40 | + |
| 41 | +func (s *testDBSuite) SetUpSuite(c *C) { |
| 42 | + s.cfg = &config.SubTaskConfig{ |
| 43 | + From: getDBConfigFromEnv(), |
| 44 | + To: getDBConfigFromEnv(), |
| 45 | + ServerID: 102, |
| 46 | + MetaSchema: "db_test", |
| 47 | + Name: "db_ut", |
| 48 | + Mode: config.ModeIncrement, |
| 49 | + Flavor: "mysql", |
| 50 | + } |
| 51 | + s.cfg.From.Adjust() |
| 52 | + s.cfg.To.Adjust() |
| 53 | + |
| 54 | + dir := c.MkDir() |
| 55 | + s.cfg.RelayDir = dir |
| 56 | + |
| 57 | + var err error |
| 58 | + dbAddr := fmt.Sprintf("%s:%s@tcp(%s:%d)/?charset=utf8", s.cfg.From.User, s.cfg.From.Password, s.cfg.From.Host, s.cfg.From.Port) |
| 59 | + s.db, err = sql.Open("mysql", dbAddr) |
| 60 | + c.Assert(err, IsNil) |
| 61 | + |
| 62 | + s.resetBinlogSyncer(c) |
| 63 | + _, err = s.db.Exec("SET GLOBAL binlog_format = 'ROW';") |
| 64 | + c.Assert(err, IsNil) |
| 65 | +} |
| 66 | + |
| 67 | +func (s *testDBSuite) resetBinlogSyncer(c *C) { |
| 68 | + cfg := replication.BinlogSyncerConfig{ |
| 69 | + ServerID: uint32(s.cfg.ServerID), |
| 70 | + Flavor: "mysql", |
| 71 | + Host: s.cfg.From.Host, |
| 72 | + Port: uint16(s.cfg.From.Port), |
| 73 | + User: s.cfg.From.User, |
| 74 | + Password: s.cfg.From.Password, |
| 75 | + UseDecimal: true, |
| 76 | + VerifyChecksum: true, |
| 77 | + } |
| 78 | + if s.cfg.Timezone != "" { |
| 79 | + timezone, err2 := time.LoadLocation(s.cfg.Timezone) |
| 80 | + c.Assert(err2, IsNil) |
| 81 | + cfg.TimestampStringLocation = timezone |
| 82 | + } |
| 83 | + |
| 84 | + if s.syncer != nil { |
| 85 | + s.syncer.Close() |
| 86 | + } |
| 87 | + |
| 88 | + pos, _, err := utils.GetMasterStatus(s.db, "mysql") |
| 89 | + c.Assert(err, IsNil) |
| 90 | + |
| 91 | + s.syncer = replication.NewBinlogSyncer(cfg) |
| 92 | + s.streamer, err = s.syncer.StartSync(pos) |
| 93 | + c.Assert(err, IsNil) |
| 94 | +} |
| 95 | + |
| 96 | +func (s *testDBSuite) TestGetServerUUID(c *C) { |
25 | 97 | uuid, err := utils.GetServerUUID(s.db, "mysql")
|
26 | 98 | c.Assert(err, IsNil)
|
27 | 99 | _, err = gouuid.FromString(uuid)
|
28 | 100 | c.Assert(err, IsNil)
|
29 | 101 | }
|
30 | 102 |
|
31 |
| -func (s *testSyncerSuite) TestGetServerID(c *C) { |
| 103 | +func (s *testDBSuite) TestGetServerID(c *C) { |
32 | 104 | id, err := utils.GetServerID(s.db)
|
33 | 105 | c.Assert(err, IsNil)
|
34 | 106 | c.Assert(id, Greater, int64(0))
|
35 | 107 | }
|
36 | 108 |
|
37 |
| -func (s *testSyncerSuite) TestBinaryLogs(c *C) { |
| 109 | +func (s *testDBSuite) TestBinaryLogs(c *C) { |
38 | 110 | files, err := getBinaryLogs(s.db)
|
39 | 111 | c.Assert(err, IsNil)
|
40 | 112 | c.Assert(files, Not(HasLen), 0)
|
@@ -64,3 +136,113 @@ func (s *testSyncerSuite) TestBinaryLogs(c *C) {
|
64 | 136 | c.Assert(remainingSize, Equals, files[fileNum].size)
|
65 | 137 |
|
66 | 138 | }
|
| 139 | + |
| 140 | +func (s *testDBSuite) TestTimezone(c *C) { |
| 141 | + s.cfg.BWList = &filter.Rules{ |
| 142 | + DoDBs: []string{"~^tztest_.*"}, |
| 143 | + IgnoreDBs: []string{"stest", "~^foo.*"}, |
| 144 | + } |
| 145 | + |
| 146 | + createSQLs := []string{ |
| 147 | + "create database if not exists tztest_1", |
| 148 | + "create table if not exists tztest_1.t_1(id int, a timestamp)", |
| 149 | + } |
| 150 | + |
| 151 | + testCases := []struct { |
| 152 | + sqls []string |
| 153 | + timezone string |
| 154 | + }{ |
| 155 | + { |
| 156 | + []string{ |
| 157 | + "insert into tztest_1.t_1(id, a) values (1, '1990-04-15 01:30:12')", |
| 158 | + "insert into tztest_1.t_1(id, a) values (2, '1990-04-15 02:30:12')", |
| 159 | + "insert into tztest_1.t_1(id, a) values (3, '1990-04-15 03:30:12')", |
| 160 | + }, |
| 161 | + "Asia/Shanghai", |
| 162 | + }, |
| 163 | + { |
| 164 | + []string{ |
| 165 | + "insert into tztest_1.t_1(id, a) values (4, '1990-04-15 01:30:12')", |
| 166 | + "insert into tztest_1.t_1(id, a) values (5, '1990-04-15 02:30:12')", |
| 167 | + "insert into tztest_1.t_1(id, a) values (6, '1990-04-15 03:30:12')", |
| 168 | + }, |
| 169 | + "America/Phoenix", |
| 170 | + }, |
| 171 | + } |
| 172 | + queryTs := "select unix_timestamp(a) from `tztest_1`.`t_1` where id = ?" |
| 173 | + |
| 174 | + dropSQLs := []string{ |
| 175 | + "drop table tztest_1.t_1", |
| 176 | + "drop database tztest_1", |
| 177 | + } |
| 178 | + |
| 179 | + defer func() { |
| 180 | + for _, sql := range dropSQLs { |
| 181 | + _, err := s.db.Exec(sql) |
| 182 | + c.Assert(err, IsNil) |
| 183 | + } |
| 184 | + }() |
| 185 | + |
| 186 | + for _, sql := range createSQLs { |
| 187 | + _, err := s.db.Exec(sql) |
| 188 | + c.Assert(err, IsNil) |
| 189 | + } |
| 190 | + |
| 191 | + for _, testCase := range testCases { |
| 192 | + s.cfg.Timezone = testCase.timezone |
| 193 | + syncer := NewSyncer(s.cfg) |
| 194 | + syncer.genRouter() |
| 195 | + s.resetBinlogSyncer(c) |
| 196 | + |
| 197 | + // we should not use `sql.DB.Exec` to do query which depends on session variables |
| 198 | + // because `sql.DB.Exec` will choose a underlying Conn for every query from the connection pool |
| 199 | + // and different Conn using different session |
| 200 | + // ref: `sql.DB.Conn` |
| 201 | + // and `set @@global` is also not reasonable, because it can not affect sessions already exist |
| 202 | + // if we must ensure multi queries use the same session, we should use a transaction |
| 203 | + txn, err := s.db.Begin() |
| 204 | + c.Assert(err, IsNil) |
| 205 | + txn.Exec("set @@session.time_zone = ?", testCase.timezone) |
| 206 | + txn.Exec("set @@session.sql_mode = ''") |
| 207 | + for _, sql := range testCase.sqls { |
| 208 | + _, err = txn.Exec(sql) |
| 209 | + c.Assert(err, IsNil) |
| 210 | + } |
| 211 | + err = txn.Commit() |
| 212 | + c.Assert(err, IsNil) |
| 213 | + |
| 214 | + location, err := time.LoadLocation(testCase.timezone) |
| 215 | + c.Assert(err, IsNil) |
| 216 | + |
| 217 | + idx := 0 |
| 218 | + for { |
| 219 | + if idx >= len(testCase.sqls) { |
| 220 | + break |
| 221 | + } |
| 222 | + e, err := s.streamer.GetEvent(context.Background()) |
| 223 | + c.Assert(err, IsNil) |
| 224 | + switch ev := e.Event.(type) { |
| 225 | + case *replication.RowsEvent: |
| 226 | + skip, err := syncer.skipDMLEvent(string(ev.Table.Schema), string(ev.Table.Table), e.Header.EventType) |
| 227 | + c.Assert(err, IsNil) |
| 228 | + if skip { |
| 229 | + continue |
| 230 | + } |
| 231 | + |
| 232 | + rowid := ev.Rows[0][0].(int32) |
| 233 | + var ts sql.NullInt64 |
| 234 | + err2 := s.db.QueryRow(queryTs, rowid).Scan(&ts) |
| 235 | + c.Assert(err2, IsNil) |
| 236 | + c.Assert(ts.Valid, IsTrue) |
| 237 | + |
| 238 | + raw := ev.Rows[0][1].(string) |
| 239 | + data, err := time.ParseInLocation("2006-01-02 15:04:05", raw, location) |
| 240 | + c.Assert(err, IsNil) |
| 241 | + c.Assert(data.Unix(), DeepEquals, ts.Int64) |
| 242 | + idx++ |
| 243 | + default: |
| 244 | + continue |
| 245 | + } |
| 246 | + } |
| 247 | + } |
| 248 | +} |
0 commit comments