File: TableSyncChunk.t

package info (click to toggle)
percona-toolkit 3.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 68,916 kB
  • sloc: perl: 241,287; sql: 22,868; sh: 19,746; javascript: 6,799; makefile: 353; awk: 38; python: 30; sed: 1
file content (405 lines) | stat: -rw-r--r-- 11,603 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/perl

BEGIN {
   die "The PERCONA_TOOLKIT_BRANCH environment variable is not set.\n"
      unless $ENV{PERCONA_TOOLKIT_BRANCH} && -d $ENV{PERCONA_TOOLKIT_BRANCH};
   unshift @INC, "$ENV{PERCONA_TOOLKIT_BRANCH}/lib";
};

use strict;
use warnings FATAL => 'all';
use English qw(-no_match_vars);
use Test::More;

# Open a connection to MySQL, or skip the rest of the tests.
use DSNParser;
use Sandbox;
use PerconaTest;

my $dp  = new DSNParser(opts=>$dsn_opts);
my $sb  = new Sandbox(basedir => '/tmp', DSNParser => $dp);
my $dbh = $sb->get_dbh_for('master');

if ( $dbh ) {
   plan tests => 35;
}
else {
   plan skip_all => 'Cannot connect to MySQL';
}

$sb->create_dbs($dbh, ['test']);

use TableSyncChunk;
use Quoter;
use ChangeHandler;
use TableChecksum;
use TableChunker;
use TableParser;
use VersionParser;
use TableSyncer;
use MasterSlave;
use Retry;

my $mysql = $sb->_use_for('master');

diag(`$mysql < $trunk/t/lib/samples/before-TableSyncChunk.sql`);

my $q  = new Quoter();
my $tp = new TableParser(Quoter => $q);
my $ms = new MasterSlave(OptionParser=>1,DSNParser=>1,Quoter=>1);
my $rr = new Retry();
my $chunker    = new TableChunker( Quoter => $q, TableParser => $tp );
my $checksum   = new TableChecksum( Quoter => $q );
my $syncer     = new TableSyncer(
   MasterSlave   => $ms,
   TableChecksum => $checksum,
   Quoter        => $q,
   Retry         => $rr,
);

my $ddl;
my $tbl_struct;
my %args;
my @rows;
my $src = {
   db  => 'test',
   tbl => 'test1',
   dbh => $dbh,
};
my $dst = {
   db  => 'test',
   tbl => 'test1',
   dbh => $dbh,
};

my $ch = new ChangeHandler(
   Quoter    => new Quoter(),
   right_db  => 'test',
   right_tbl => 'test1',
   left_db   => 'test',
   left_tbl  => 'test1',
   replace   => 0,
   actions   => [ sub { push @rows, $_[0] }, ],
   queue     => 0,
);

my $t = new TableSyncChunk(
   TableChunker  => $chunker,
   Quoter        => $q,
);
isa_ok($t, 'TableSyncChunk');

$ddl        = $tp->get_create_table($dbh, 'test', 'test1');
$tbl_struct = $tp->parse($ddl);
%args       = (
   src           => $src,
   dst           => $dst,
   dbh           => $dbh,
   db            => 'test',
   tbl           => 'test1',
   tbl_struct    => $tbl_struct,
   cols          => $tbl_struct->{cols},
   chunk_col     => 'a',
   chunk_index   => 'PRIMARY',
   chunk_size    => 2,
   where         => 'a>2',
   crc_col       => '__crc',
   index_hint    => 'USE INDEX (`PRIMARY`)',
   ChangeHandler => $ch,
);
$t->prepare_to_sync(%args);

# Test with FNV_64 just to make sure there are no errors
eval { $dbh->do('select fnv_64(1)') };
SKIP: {
   skip 'No FNV_64 function installed', 1 if $EVAL_ERROR;

   $t->set_checksum_queries(
      $syncer->make_checksum_queries(%args, function => 'FNV_64')
   );
   is(
      $t->get_sql(
         where      => 'foo=1',
         database   => 'test',
         table      => 'test1', 
      ),
      q{SELECT /*test.test1:1/1*/ 0 AS chunk_num, COUNT(*) AS cnt, }
      . q{COALESCE(LOWER(CONV(BIT_XOR(CAST(FNV_64(`a`, `b`) AS UNSIGNED)), 10, 16)), 0) AS }
      . q{crc FROM `test`.`test1` USE INDEX (`PRIMARY`) WHERE (1=1) AND ((foo=1))},
      'First nibble SQL with FNV_64 (with USE INDEX)',
   );
}

$t->set_checksum_queries(
   $syncer->make_checksum_queries(%args, function => 'SHA1')
);
is_deeply(
   $t->{chunks},
   [
      # it should really chunk in chunks of 1, but table stats are bad.
      '1=1',
   ],
   'Chunks with WHERE'
);

unlike(
   $t->get_sql(
      where    => 'foo=1',
      database => 'test',
      table    => 'test1',
   ),
   qr/SQL_BUFFER_RESULT/,
   'No buffering',
);

# SQL_BUFFER_RESULT only appears in the row query, state 1 or 2.
$t->prepare_to_sync(%args, buffer_in_mysql => 1);
$t->{state} = 1;
like(
   $t->get_sql(
      where    => 'foo=1',
      database => 'test',
      table    => 'test1', 
   ),
   qr/SELECT ..rows in chunk.. SQL_BUFFER_RESULT/,
   'Has SQL_BUFFER_RESULT',
);

# Remove the WHERE so we get enough rows to make chunks.
$args{where} = undef;
$t->prepare_to_sync(%args);
$t->set_checksum_queries(
   $syncer->make_checksum_queries(%args, function => 'SHA1')
);
is_deeply(
   $t->{chunks},
   [
      "`a` < '3'",
      "`a` >= '3'",
   ],
   'Chunks'
);

like(
   $t->get_sql(
      where    => 'foo=1',
      database => 'test',
      table    => 'test1',
   ),
   qr/SELECT .*?CONCAT_WS.*?`a` < '3'/,
   'First chunk SQL (without index hint)',
);

is_deeply($t->key_cols(), [qw(chunk_num)], 'Key cols in state 0');
$t->done_with_rows();

like($t->get_sql(
      quoter     => $q,
      where      => 'foo=1',
      database   => 'test',
      table      => 'test1',
      index_hint => 'USE INDEX (`PRIMARY`)',
   ),
   qr/SELECT .*?CONCAT_WS.*?FROM `test`\.`test1` USE INDEX \(`PRIMARY`\) WHERE.*?`a` >= '3'/,
   'Second chunk SQL (with index hint)',
);

$t->done_with_rows();
ok($t->done(), 'Now done');

# Now start over, and this time "find some bad chunks," as it were.

$t->prepare_to_sync(%args);
$t->set_checksum_queries(
   $syncer->make_checksum_queries(%args, function => 'SHA1')
);
throws_ok(
   sub { $t->not_in_left() },
   qr/in state 0/,
   'not_in_(side) illegal in state 0',
);

# "find a bad row"
$t->same_row(
   lr => { chunk_num => 0, cnt => 0, crc => 'abc' },
   rr => { chunk_num => 0, cnt => 1, crc => 'abc' },
);
ok($t->pending_changes(), 'Pending changes found');
is($t->{state}, 1, 'Working inside chunk');
$t->done_with_rows();
is($t->{state}, 2, 'Now in state to fetch individual rows');
ok($t->pending_changes(), 'Pending changes not done yet');
is(
   $t->get_sql(
      database => 'test',
      table    => 'test1',
   ),
   "SELECT /*rows in chunk*/ `a`, `b`, SHA1(CONCAT_WS('#', `a`, `b`)) AS __crc FROM "
      . "`test`.`test1` USE INDEX (`PRIMARY`) WHERE (`a` < '3')"
      . " ORDER BY `a`",
   'SQL now working inside chunk'
);
ok($t->{state}, 'Still working inside chunk');
is(scalar(@rows), 0, 'No bad row triggered');

$t->not_in_left(rr => {a => 1});

is_deeply(\@rows,
   ["DELETE FROM `test`.`test1` WHERE `a`='1' LIMIT 1"],
   'Working inside chunk, got a bad row',
);

# Should cause it to fetch back from the DB to figure out the right thing to do
$t->not_in_right(lr => {a => 1});
is_deeply(\@rows,
   [
   "DELETE FROM `test`.`test1` WHERE `a`='1' LIMIT 1",
   "INSERT INTO `test`.`test1`(`a`, `b`) VALUES ('1', 'en')",
   ],
   'Missing row fetched back from DB',
);

# Shouldn't cause anything to happen
$t->same_row( lr => {a => 1, __crc => 'foo'}, rr => {a => 1, __crc => 'foo'} );

is_deeply(\@rows,
   [
   "DELETE FROM `test`.`test1` WHERE `a`='1' LIMIT 1",
   "INSERT INTO `test`.`test1`(`a`, `b`) VALUES ('1', 'en')",
   ],
   'No more rows added',
);

$t->same_row( lr => {a => 1, __crc => 'foo'}, rr => {a => 1, __crc => 'bar'} );

is_deeply(\@rows,
   [
      "DELETE FROM `test`.`test1` WHERE `a`='1' LIMIT 1",
      "INSERT INTO `test`.`test1`(`a`, `b`) VALUES ('1', 'en')",
      "UPDATE `test`.`test1` SET `b`='en' WHERE `a`='1' LIMIT 1",
   ],
   'Row added to update differing row',
);

$t->done_with_rows();
is($t->{state}, 0, 'Now not working inside chunk');
is($t->pending_changes(), 0, 'No pending changes');

# ###########################################################################
# Test can_sync().
# ###########################################################################
# With the introduction of char chunking (issue 568), this table can
# be chunked.  But to keep the spirit of this test the same, we drop
# the index which makes the table unchunkable again.
$dbh->do('alter table test.test6 drop index a');
$ddl        = $tp->get_create_table($dbh, 'test', 'test6');
$tbl_struct = $tp->parse($ddl);
is_deeply(
   [ $t->can_sync(tbl_struct=>$tbl_struct) ],
   [],
   'Cannot sync table1 (no good single column index)'
);

$ddl        = $tp->get_create_table($dbh, 'test', 'test5');
$tbl_struct = $tp->parse($ddl);
is_deeply(
   [ $t->can_sync(tbl_struct=>$tbl_struct) ],
   [],
   'Cannot sync table5 (no indexes)'
);

# create table test3(a int not null primary key, b int not null, unique(b));

$ddl        = $tp->get_create_table($dbh, 'test', 'test3');
$tbl_struct = $tp->parse($ddl);
is_deeply(
   [ $t->can_sync(tbl_struct=>$tbl_struct) ],
   [ 1,
     chunk_col   => 'a',
     chunk_index => 'PRIMARY',
   ],
   'Can sync table3, chooses best col and index'
);

is_deeply(
   [ $t->can_sync(tbl_struct=>$tbl_struct, chunk_col=>'b') ],
   [ 1,
     chunk_col   => 'b',
     chunk_index => 'b',
   ],
   'Can sync table3 with requested col'
);

is_deeply(
   [ $t->can_sync(tbl_struct=>$tbl_struct, chunk_index=>'b') ],
   [ 1,
     chunk_col   => 'b',
     chunk_index => 'b',
   ],
   'Can sync table3 with requested index'
);
 
is_deeply(
   [ $t->can_sync(tbl_struct=>$tbl_struct, chunk_col=>'b', chunk_index=>'b') ],
   [ 1,
     chunk_col   => 'b',
     chunk_index => 'b',
   ],
   'Can sync table3 with requested col and index'
);

is_deeply(
   [ $t->can_sync(tbl_struct=>$tbl_struct, chunk_col=>'b', chunk_index=>'PRIMARY') ],
   [],
   'Cannot sync table3 with requested col and index'
);


# #############################################################################
# Issue 560: mk-table-sync generates impossible WHERE
# Issue 996: might not chunk inside of mk-table-checksum's boundaries
# #############################################################################
$t->prepare_to_sync(%args, index_hint => undef, replicate => 'test.checksum');
is(
   $t->get_sql(
      where    => 'x > 1 AND x <= 9',  # e.g. range from mk-table-checksum
      database => 'test',
      table    => 'test1', 
   ),
   "SELECT /*test.test1:1/2*/ 0 AS chunk_num, COUNT(*) AS cnt, COALESCE(LOWER(CONCAT(LPAD(CONV(BIT_XOR(CAST(CONV(SUBSTRING(\@crc, 1, 16), 16, 10) AS UNSIGNED)), 10, 16), 16, '0'), LPAD(CONV(BIT_XOR(CAST(CONV(SUBSTRING(\@crc, 17, 16), 16, 10) AS UNSIGNED)), 10, 16), 16, '0'), LPAD(CONV(BIT_XOR(CAST(CONV(SUBSTRING(\@crc := SHA1(CONCAT_WS('#', `a`, `b`)), 33, 8), 16, 10) AS UNSIGNED)), 10, 16), 8, '0'))), 0) AS crc FROM `test`.`test1`  WHERE (`a` < '3') AND ((x > 1 AND x <= 9))",
   'Chunk within chunk (chunk sql)'
);

# The above test shows that we can chunk (a<3) inside a given range (x>1 AND x<=9).
# That tests issue 996.  Issue 560 was really an issue with nibbling within a chunk,
# so there's a test similar to this one in TableSyncNibble.t.

$t->{state} = 2;
is(
   $t->get_sql(
      where    => 'x > 1 AND x <= 9',
      database => 'test',
      table    => 'test1', 
   ),
   "SELECT /*rows in chunk*/ `a`, `b`, SHA1(CONCAT_WS('#', `a`, `b`)) AS __crc FROM `test`.`test1`  WHERE (`a` < '3') AND (x > 1 AND x <= 9) ORDER BY `a`",
   'Chunk within chunk (row sql)'
);

$t->{state} = 0;
$t->done_with_rows();
is(
   $t->get_sql(
      where    => 'x > 1 AND x <= 9',
      database => 'test',
      table    => 'test1', 
   ),
   "SELECT /*test.test1:2/2*/ 1 AS chunk_num, COUNT(*) AS cnt, COALESCE(LOWER(CONCAT(LPAD(CONV(BIT_XOR(CAST(CONV(SUBSTRING(\@crc, 1, 16), 16, 10) AS UNSIGNED)), 10, 16), 16, '0'), LPAD(CONV(BIT_XOR(CAST(CONV(SUBSTRING(\@crc, 17, 16), 16, 10) AS UNSIGNED)), 10, 16), 16, '0'), LPAD(CONV(BIT_XOR(CAST(CONV(SUBSTRING(\@crc := SHA1(CONCAT_WS('#', `a`, `b`)), 33, 8), 16, 10) AS UNSIGNED)), 10, 16), 8, '0'))), 0) AS crc FROM `test`.`test1`  WHERE (`a` >= '3') AND ((x > 1 AND x <= 9))",
   'Second chunk within chunk'
);

# #############################################################################
# Done.
# #############################################################################
$sb->wipe_clean($dbh);
ok($sb->ok(), "Sandbox servers") or BAIL_OUT(__FILE__ . " broke the sandbox");
exit;