File: CompareResults.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 (782 lines) | stat: -rw-r--r-- 17,448 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
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
#!/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;

use VersionParser;
use Quoter;
use TableParser;
use DSNParser;
use QueryParser;
use TableSyncer;
use TableChecksum;
use TableSyncGroupBy;
use MockSyncStream;
use MockSth;
use Outfile;
use RowDiff;
use ChangeHandler;
use ReportFormatter;
use Transformers;
use Retry;
use Sandbox;
use CompareResults;
use PerconaTest;

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

if ( !$dbh1 ) {
   plan skip_all => "Cannot connect to sandbox master";
}
elsif ( !$dbh2 ) {
   plan skip_all => "Cannot connect to sandbox slave";
}

Transformers->import(qw(make_checksum));

my $q  = new Quoter();
my $qp = new QueryParser();
my $tp = new TableParser(Quoter => $q);
my $tc = new TableChecksum(Quoter => $q);
my $of = new Outfile();
my $rr = new Retry();
my $ts = new TableSyncer(
   Quoter        => $q,
   TableChecksum => $tc,
   Retry         => $rr,
   MasterSlave   => 1,
);
my %modules = (
   Quoter        => $q,
   TableParser   => $tp,
   TableSyncer   => $ts,
   QueryParser   => $qp,
   Outfile       => $of,
);

my $plugin = new TableSyncGroupBy(Quoter => $q);

my $cr;
my $i;
my $report;
my @events;
my $hosts = [
   { dbh => $dbh1, name => 'master' },
   { dbh => $dbh2, name => 'slave'  },
];

sub proc {
   my ( $when, %args ) = @_;
   die "I don't know when $when is"
      unless $when eq 'before_execute'
          || $when eq 'execute'
          || $when eq 'after_execute';
   for my $i ( 0..$#events ) {
      $events[$i] = $cr->$when(
         event    => $events[$i],
         dbh      => $hosts->[$i]->{dbh},
         %args,
      );
   }
};

sub get_id {
   return make_checksum(@_);
}

# #############################################################################
# Test the checksum method.
# #############################################################################

$sb->load_file('master', "t/lib/samples/compare-results.sql");

$cr = new CompareResults(
   method     => 'checksum',
   'base-dir' => '/dev/null',  # not used with checksum method
   plugins    => [$plugin],
   get_id     => \&get_id,
   %modules,
);

isa_ok($cr, 'CompareResults');

@events = (
   {
      arg         => 'select * from test.t where i>0',
      fingerprint => 'select * from test.t where i>?',
      sampleno    => 1,
   },
   {
      arg         => 'select * from test.t where i>0',
      fingerprint => 'select * from test.t where i>?',
      sampleno    => 1,
   },
);

is_deeply(
   $dbh1->selectrow_arrayref("SHOW TABLES FROM test LIKE 'dropme'"),
   ['dropme'],
   'checksum: temp table exists'
);

proc('before_execute', db=>'test', 'temp-table'=>'dropme');

is(
   $events[0]->{wrapped_query},
   'CREATE TEMPORARY TABLE `test`.`dropme` AS select * from test.t where i>0',
   'checksum: before_execute() wraps query in CREATE TEMPORARY TABLE'
);

is_deeply(
   $dbh1->selectall_arrayref("SHOW TABLES FROM test LIKE 'dropme'"),
   [],
   'checksum: before_execute() drops temp table'
);

ok(
   !exists $events[0]->{Query_time},
   "checksum: Query_time doesn't exist before execute()"
);

proc('execute');

ok(
   exists $events[0]->{Query_time},
   "checksum: Query_time exists after exectue()"
);

like(
   $events[0]->{Query_time},
   qr/^[\d.]+$/,
   "checksum: Query_time is a number ($events[0]->{Query_time})"
);

is(
   $events[0]->{wrapped_query},
   'CREATE TEMPORARY TABLE `test`.`dropme` AS select * from test.t where i>0',
   "checksum: execute() doesn't unwrap query"
);

is_deeply(
   $dbh1->selectall_arrayref('select * from test.dropme'),
   [[1],[2],[3]],
   'checksum: Result set selected into the temp table'
);

ok(
   !exists $events[0]->{row_count},
   "checksum: row_count doesn't exist before after_execute()"
);

ok(
   !exists $events[0]->{checksum},
   "checksum: checksum doesn't exist before after_execute()"
);

proc('after_execute');

is(
   $events[0]->{wrapped_query},
   'CREATE TEMPORARY TABLE `test`.`dropme` AS select * from test.t where i>0',
   'checksum: after_execute() left wrapped query'
);

is_deeply(
   $dbh1->selectall_arrayref("SHOW TABLES FROM test LIKE 'dropme'"),
   [],
   'checksum: after_execute() drops temp table'
);

is_deeply(
   [ $cr->compare(
      events => \@events,
      hosts  => $hosts,
   ) ],
   [
      different_row_counts    => 0,
      different_checksums     => 0,
      different_column_counts => 0,
      different_column_types  => 0,
   ],
   'checksum: compare, no differences'
);

is(
   $events[0]->{row_count},
   3,
   "checksum: correct row_count after after_execute()"
);

is(
   $events[0]->{checksum},
   '251493421',
   "checksum: correct checksum after after_execute()"
);

ok(
   !exists $events[0]->{wrapped_query},
   'checksum: wrapped query removed after compare'
);

# Make checksums differ.
$dbh2->do('update test.t set i = 99 where i=1');

proc('before_execute', db=>'test', 'temp-table'=>'dropme');
proc('execute');
proc('after_execute');

is_deeply(
   [ $cr->compare(
      events => \@events,
      hosts  => $hosts,
   ) ],
   [
      different_row_counts    => 0,
      different_checksums     => 1,
      different_column_counts => 0,
      different_column_types  => 0,
   ],
   'checksum: compare, different checksums' 
);

# Make row counts differ, too.
$dbh2->do('insert into test.t values (4)');

proc('before_execute', db=>'test', 'temp-table'=>'dropme');
proc('execute');
proc('after_execute');

is_deeply(
   [ $cr->compare(
      events => \@events,
      hosts  => $hosts,
   ) ],
   [
      different_row_counts    => 1,
      different_checksums     => 1,
      different_column_counts => 0,
      different_column_types  => 0,
   ],
   'checksum: compare, different checksums and row counts'
);

$report = <<EOF;
# Checksum differences
# Query ID           host1     host2
# ================== ========= ==========
# D2D386B840D3BEEA-1 $events[0]->{checksum} $events[1]->{checksum}

# Row count differences
# Query ID           host1 host2
# ================== ===== =====
# D2D386B840D3BEEA-1     3     4
EOF

is(
   $cr->report(hosts => $hosts),
   $report,
   'checksum: report'
);

my %samples = $cr->samples($events[0]->{fingerprint});
is_deeply(
   \%samples,
   {
      1 => 'select * from test.t where i>0',
   },
   'checksum: samples'
);

# #############################################################################
# Test the rows method.
# #############################################################################
my $tmpdir = '/tmp/mk-upgrade-res';
SKIP: {

diag(`rm -rf $tmpdir 2>/dev/null; mkdir $tmpdir`);

$sb->load_file('master', "t/lib/samples/compare-results.sql");

$cr = new CompareResults(
   method     => 'rows',
   'base-dir' => $tmpdir,
   plugins    => [$plugin],
   get_id     => \&get_id,
   %modules,
);

isa_ok($cr, 'CompareResults');

@events = (
   {
      arg => 'select * from test.t',
      db  => 'test',
   },
   {
      arg => 'select * from test.t',
      db  => 'test',
   },
);

is_deeply(
   $dbh1->selectrow_arrayref("SHOW TABLES FROM test LIKE 'dropme'"),
   ['dropme'],
   'rows: temp table exists'
);

proc('before_execute');

is(
   $events[0]->{arg},
   'select * from test.t',
   "rows: before_execute() doesn't wrap query and doesn't require tmp table"
);

is_deeply(
   $dbh1->selectrow_arrayref("SHOW TABLES FROM test LIKE 'dropme'"),
   ['dropme'],
   "rows: before_execute() doesn't drop temp table"
);

ok(
   !exists $events[0]->{Query_time},
   "rows: Query_time doesn't exist before execute()"
);

ok(
   !exists $events[0]->{results_sth},
   "rows: results_sth doesn't exist before execute()"
);

proc('execute');

ok(
   exists $events[0]->{Query_time},
   "rows: query_time exists after exectue()"
);

ok(
   exists $events[0]->{results_sth},
   "rows: results_sth exists after exectue()"
);

like(
   $events[0]->{Query_time},
   qr/^[\d.]+$/,
   "rows: Query_time is a number ($events[0]->{Query_time})"
);

ok(
   !exists $events[0]->{row_count},
   "rows: row_count doesn't exist before after_execute()"
);

is_deeply(
   $cr->after_execute(event=>$events[0]),
   $events[0],
   "rows: after_execute() doesn't modify the event"
);

is_deeply(
   [ $cr->compare(
      events => \@events,
      hosts  => $hosts,
   ) ],
   [
      different_row_counts    => 0,
      different_column_values => 0,
      different_column_counts => 0,
      different_column_types  => 0,
   ],
   'rows: compare, no differences'
);

is(
   $events[0]->{row_count},
   3,
   "rows: compare() sets row_count"
);

is(
   $events[1]->{row_count},
   3,
   "rows: compare() sets row_count"
);

# Make the result set differ.
$dbh2->do('insert into test.t values (5)');

proc('before_execute');
proc('execute');

is_deeply(
   [ $cr->compare(
      events => \@events,
      hosts  => $hosts,
   ) ],
   [
      different_row_counts    => 1,
      different_column_values => 0,
      different_column_counts => 0,
      different_column_types  => 0,
   ],
   'rows: compare, different row counts'
);

# Use test.t2 and make a column value differ.
@events = (
   {
      arg         => 'select * from test.t2',
      db          => 'test',
      fingerprint => 'select * from test.t2',
      sampleno    => 3,
   },
   {
      arg         => 'select * from test.t2',
      db          => 'test',
      fingerprint => 'select * from test.t2',
      sampleno    => 3,
   },
);

$dbh2->do("update test.t2 set c='should be c' where i=3");

is_deeply(
   $dbh2->selectrow_arrayref('select c from test.t2 where i=3'),
   ['should be c'],
   'rows: column value is different'
);

proc('before_execute');
proc('execute');

is_deeply(
   [ $cr->compare(
      events => \@events,
      hosts  => $hosts,
   ) ],
   [
      different_row_counts    => 0,
      different_column_values => 1,
      different_column_counts => 0,
      different_column_types  => 0,
   ],
   'rows: compare, different column values'
);

is_deeply(
   $dbh1->selectall_arrayref('show indexes from test.mk_upgrade_left'),
   [],
   'Did not add indexes'
);

$report = <<EOF;
# Column value differences
# Query ID           Column host1 host2
# ================== ====== ===== ===========
# CFC309761E9131C5-3 c      c     should be c

# Row count differences
# Query ID           host1 host2
# ================== ===== =====
# B8B721D77EA1FD78-0     3     4
EOF

is(
   $cr->report(hosts => $hosts),
   $report,
   'rows: report'
);

%samples = $cr->samples($events[0]->{fingerprint});
is_deeply(
   \%samples,
   {
      3 => 'select * from test.t2'
   },
   'rows: samples'
);

# #############################################################################
# Test max-different-rows.
# #############################################################################
$cr->reset();
$dbh2->do("update test.t2 set c='should be a' where i=1");
$dbh2->do("update test.t2 set c='should be b' where i=2");
proc('before_execute');
proc('execute');

is_deeply(
   [ $cr->compare(
      events => \@events,
      hosts  => $hosts,
      'max-different-rows' => 1,
      'add-indexes'        => 1,
   ) ],
   [
      different_row_counts    => 0,
      different_column_values => 1,
      different_column_counts => 0,
      different_column_types  => 0,
   ],
   'rows: compare, stop at max-different-rows'
);

# I don't know why but several months ago this test started
# failing although nothing afaik was changed.  This module
# is only used in pt-upgrade and that tool passes its tests.
SKIP: {
   skip "Fix this test", 1;
is_deeply(
   $dbh1->selectall_arrayref('show indexes from test.mk_upgrade_left'),
   [['mk_upgrade_left','0','i','1','i','A',undef,undef, undef,'YES','BTREE','']],
   'Added indexes'
);
}

$report = <<EOF;
# Column value differences
# Query ID           Column host1 host2
# ================== ====== ===== ===========
# CFC309761E9131C5-3 c      a     should be a
EOF

is(
   $cr->report(hosts => $hosts),
   $report,
   'rows: report max-different-rows'
);

# #############################################################################
# Double check that outfiles have correct contents.
# #############################################################################

# This test uses the results from the max-different-rows test above.

my @outfile = split(/[\t\n]+/, `cat /tmp/mk-upgrade-res/left-outfile.txt`);
is_deeply(
	\@outfile,
	[qw(1 a 2 b 3 c)],
   'Left outfile'
);

@outfile = split(/[\t\n]+/, `cat /tmp/mk-upgrade-res/right-outfile.txt`);
is_deeply(
	\@outfile,
	['1', 'should be a', '2', 'should be b', '3', 'should be c'],
   'Right outfile'
);

# #############################################################################
# Test float-precision.
# #############################################################################
@events = (
   {
      arg         => 'select * from test.t3',
      db          => 'test',
      fingerprint => 'select * from test.t3',
      sampleno    => 3,
   },
   {
      arg         => 'select * from test.t3',
      db          => 'test',
      fingerprint => 'select * from test.t3',
      sampleno    => 3,
   },
);

$cr->reset();
$dbh2->do('update test.t3 set f=1.12346 where 1');
proc('before_execute');
proc('execute');

is_deeply(
   [ $cr->compare(
      events => \@events,
      hosts  => $hosts,
   ) ],
   [
      different_row_counts    => 0,
      different_column_values => 1,
      different_column_counts => 0,
      different_column_types  => 0,
   ],
   'rows: compare, different without float-precision'
);

proc('before_execute');
proc('execute');

is_deeply(
   [ $cr->compare(
      events => \@events,
      hosts  => $hosts,
      'float-precision' => 3
   ) ],
   [
      different_row_counts    => 0,
      different_column_values => 0,
      different_column_counts => 0,
      different_column_types  => 0,
   ],
   'rows: compare, not different with float-precision'
);

# #############################################################################
# Test when left has more rows than right.
# #############################################################################
$cr->reset();
$dbh1->do('update test.t3 set f=0 where 1');
$dbh1->do('SET SQL_LOG_BIN=0');
$dbh1->do('insert into test.t3 values (2.0),(3.0)');
$dbh1->do('SET SQL_LOG_BIN=1');
$sb->wait_for_slaves();

my $left_n_rows = $dbh1->selectcol_arrayref('select count(*) from test.t3')->[0];
my $right_n_rows = $dbh2->selectcol_arrayref('select count(*) from test.t3')->[0];
ok(
   $left_n_rows == 3 && $right_n_rows == 1,
   'Left has extra rows'
);

proc('before_execute');
proc('execute');

is_deeply(
   [ $cr->compare(
      events => \@events,
      hosts  => $hosts,
      'float-precision' => 3
   ) ],
   [
      different_row_counts    => 1,
      different_column_values => 0,
      different_column_counts => 0,
      different_column_types  => 0,
   ],
   'rows: compare, left with more rows'
);

$report = <<EOF;
# Row count differences
# Query ID           host1 host2
# ================== ===== =====
# D56E6FABA26D1F1C-3     3     1
EOF

is(
   $cr->report(hosts => $hosts),
   $report,
   'rows: report, left with more rows'
);
}

# #############################################################################
# Try to compare without having done the actions.
# #############################################################################
@events = (
   {
      arg => 'select * from test.t',
      db  => 'test',
   },
   {
      arg => 'select * from test.t',
      db  => 'test',
   },
);

$cr = new CompareResults(
   method     => 'checksum',
   'base-dir' => '/dev/null',  # not used with checksum method
   plugins    => [$plugin],
   get_id     => \&get_id,
   %modules,
);

my @diffs;
eval {
   @diffs = $cr->compare(events => \@events, hosts => $hosts);
};

is(
   $EVAL_ERROR,
   '',
   "compare() checksums without actions doesn't die"
);

is_deeply(
   \@diffs,
   [
      different_row_counts    => 0,
      different_checksums     => 0,
      different_column_counts => 0,
      different_column_types  => 0,
   ],
   'No differences after bad compare()'
);

SKIP: {

$cr = new CompareResults(
   method     => 'rows',
   'base-dir' => $tmpdir,
   plugins    => [$plugin],
   get_id     => \&get_id,
   %modules,
);

eval {
   @diffs = $cr->compare(events => \@events, hosts => $hosts);
};

is(
   $EVAL_ERROR,
   '',
   "compare() rows without actions doesn't die"
);

is_deeply(
   \@diffs,
   [
      different_row_counts    => 0,
      different_column_values => 0,
      different_column_counts => 0,
      different_column_types  => 0,
   ],
   'No differences after bad compare()'
);

}

# #############################################################################
# Done.
# #############################################################################
my $output = '';
{
   local *STDERR;
   open STDERR, '>', \$output;
   $cr->_d('Complete test coverage');
}
like(
   $output,
   qr/Complete test coverage/,
   '_d() works'
);
diag(`rm -rf $tmpdir`);
diag(`rm -rf /tmp/*outfile.txt`);
$sb->wipe_clean($dbh1);
$sb->wipe_clean($dbh2);
ok($sb->ok(), "Sandbox servers") or BAIL_OUT(__FILE__ . " broke the sandbox");
done_testing;
exit;