Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 6e9ffcf

Browse files
committed
Harden TAP tests that intentionally corrupt page checksums.
The previous method for doing that was to write zeroes into a predetermined set of page locations. However, there's a roughly 1-in-64K chance that the existing checksum will match by chance, and yesterday several buildfarm animals started to reproducibly see that, resulting in test failures because no checksum mismatch was reported. Since the checksum includes the page LSN, test success depends on the length of the installation's WAL history, which is affected by (at least) the initial catalog contents, the set of locales installed on the system, and the length of the pathname of the test directory. Sooner or later we were going to hit a chance match, and today is that day. Harden these tests by specifically inverting the checksum field and leaving all else alone, thereby guaranteeing that the checksum is incorrect. In passing, fix places that were using seek() to set up for syswrite(), a combination that the Perl docs very explicitly warn against. We've probably escaped problems because no regular buffered I/O is done on these filehandles; but if it ever breaks, we wouldn't deserve or get much sympathy. Although we've only seen problems in HEAD, now that we recognize the environmental dependencies it seems like it might be just a matter of time until someone manages to hit this in back-branch testing. Hence, back-patch to v11 where we started doing this kind of test. Discussion: https://postgr.es/m/3192026.1648185780@sss.pgh.pa.us
1 parent 50e3ed8 commit 6e9ffcf

File tree

3 files changed

+44
-29
lines changed

3 files changed

+44
-29
lines changed

src/bin/pg_basebackup/t/010_pg_basebackup.pl

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
}
3737

3838
$node->set_replication_conf();
39-
system_or_bail 'pg_ctl', '-D', $pgdata, 'reload';
39+
$node->reload;
4040

4141
$node->command_fails(
4242
[ 'pg_basebackup', '-D', "$tempdir/backup" ],
@@ -494,17 +494,13 @@
494494
q{SELECT b INTO corrupt2 FROM generate_series(1,2) AS b; ALTER TABLE corrupt2 SET (autovacuum_enabled=false); SELECT pg_relation_filepath('corrupt2')}
495495
);
496496

497-
# set page header and block sizes
498-
my $pageheader_size = 24;
497+
# get block size for corruption steps
499498
my $block_size = $node->safe_psql('postgres', 'SHOW block_size;');
500499

501500
# induce corruption
502-
system_or_bail 'pg_ctl', '-D', $pgdata, 'stop';
503-
open $file, '+<', "$pgdata/$file_corrupt1";
504-
seek($file, $pageheader_size, 0);
505-
syswrite($file, "\0\0\0\0\0\0\0\0\0");
506-
close $file;
507-
system_or_bail 'pg_ctl', '-D', $pgdata, 'start';
501+
$node->stop;
502+
$node->corrupt_page_checksum($file_corrupt1, 0);
503+
$node->start;
508504

509505
$node->command_checks_all(
510506
[ 'pg_basebackup', '-D', "$tempdir/backup_corrupt" ],
@@ -515,16 +511,12 @@
515511
rmtree("$tempdir/backup_corrupt");
516512

517513
# induce further corruption in 5 more blocks
518-
system_or_bail 'pg_ctl', '-D', $pgdata, 'stop';
519-
open $file, '+<', "$pgdata/$file_corrupt1";
514+
$node->stop;
520515
for my $i (1 .. 5)
521516
{
522-
my $offset = $pageheader_size + $i * $block_size;
523-
seek($file, $offset, 0);
524-
syswrite($file, "\0\0\0\0\0\0\0\0\0");
517+
$node->corrupt_page_checksum($file_corrupt1, $i * $block_size);
525518
}
526-
close $file;
527-
system_or_bail 'pg_ctl', '-D', $pgdata, 'start';
519+
$node->start;
528520

529521
$node->command_checks_all(
530522
[ 'pg_basebackup', '-D', "$tempdir/backup_corrupt2" ],
@@ -535,12 +527,9 @@
535527
rmtree("$tempdir/backup_corrupt2");
536528

537529
# induce corruption in a second file
538-
system_or_bail 'pg_ctl', '-D', $pgdata, 'stop';
539-
open $file, '+<', "$pgdata/$file_corrupt2";
540-
seek($file, $pageheader_size, 0);
541-
syswrite($file, "\0\0\0\0\0\0\0\0\0");
542-
close $file;
543-
system_or_bail 'pg_ctl', '-D', $pgdata, 'start';
530+
$node->stop;
531+
$node->corrupt_page_checksum($file_corrupt2, 0);
532+
$node->start;
544533

545534
$node->command_checks_all(
546535
[ 'pg_basebackup', '-D', "$tempdir/backup_corrupt3" ],

src/bin/pg_checksums/t/002_actions.pl

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ sub check_relation_corruption
1919
my $tablespace = shift;
2020
my $pgdata = $node->data_dir;
2121

22+
# Create table and discover its filesystem location.
2223
$node->safe_psql(
2324
'postgres',
2425
"SELECT a INTO $table FROM generate_series(1,10000) AS a;
@@ -32,9 +33,6 @@ sub check_relation_corruption
3233
my $relfilenode_corrupted = $node->safe_psql('postgres',
3334
"SELECT relfilenode FROM pg_class WHERE relname = '$table';");
3435

35-
# Set page header and block size
36-
my $pageheader_size = 24;
37-
my $block_size = $node->safe_psql('postgres', 'SHOW block_size;');
3836
$node->stop;
3937

4038
# Checksums are correct for single relfilenode as the table is not
@@ -49,10 +47,7 @@ sub check_relation_corruption
4947
);
5048

5149
# Time to create some corruption
52-
open my $file, '+<', "$pgdata/$file_corrupted";
53-
seek($file, $pageheader_size, 0);
54-
syswrite($file, "\0\0\0\0\0\0\0\0\0");
55-
close $file;
50+
$node->corrupt_page_checksum($file_corrupted, 0);
5651

5752
# Checksum checks on single relfilenode fail
5853
$node->command_checks_all(

src/test/perl/PostgresNode.pm

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2346,6 +2346,37 @@ sub pg_recvlogical_upto
23462346

23472347
=pod
23482348
2349+
=item $node->corrupt_page_checksum(self, file, page_offset)
2350+
2351+
Intentionally corrupt the checksum field of one page in a file.
2352+
The server must be stopped for this to work reliably.
2353+
2354+
The file name should be specified relative to the cluster datadir.
2355+
page_offset had better be a multiple of the cluster's block size.
2356+
2357+
=cut
2358+
2359+
sub corrupt_page_checksum
2360+
{
2361+
my ($self, $file, $page_offset) = @_;
2362+
my $pgdata = $self->data_dir;
2363+
my $pageheader;
2364+
2365+
open my $fh, '+<', "$pgdata/$file" or die "open($file) failed: $!";
2366+
binmode $fh;
2367+
sysseek($fh, $page_offset, 0) or die "sysseek failed: $!";
2368+
sysread($fh, $pageheader, 24) or die "sysread failed: $!";
2369+
# This inverts the pd_checksum field (only); see struct PageHeaderData
2370+
$pageheader ^= "\0\0\0\0\0\0\0\0\xff\xff";
2371+
sysseek($fh, $page_offset, 0) or die "sysseek failed: $!";
2372+
syswrite($fh, $pageheader) or die "syswrite failed: $!";
2373+
close $fh;
2374+
2375+
return;
2376+
}
2377+
2378+
=pod
2379+
23492380
=back
23502381
23512382
=cut

0 commit comments

Comments
 (0)