]> Sergey Matveev's repositories - public-inbox.git/blob - t/xcpdb-reshard.t
treewide: run update-copyrights from gnulib for 2019
[public-inbox.git] / t / xcpdb-reshard.t
1 # Copyright (C) 2019-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use strict;
4 use warnings;
5 use Test::More;
6 use PublicInbox::TestCommon;
7 require_mods(qw(DBD::SQLite Search::Xapian));
8 require_git('2.6');
9 use PublicInbox::MIME;
10 use PublicInbox::InboxWritable;
11 require PublicInbox::Search;
12
13 my $mime = PublicInbox::MIME->create(
14         header => [
15                 From => 'a@example.com',
16                 To => 'test@example.com',
17                 Subject => 'this is a subject',
18                 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
19         ],
20         body => '',
21 );
22
23 my ($this) = (split('/', $0))[-1];
24 my ($tmpdir, $for_destroy) = tmpdir();
25 local $ENV{PI_CONFIG} = "$tmpdir/config";
26 my $ibx = PublicInbox::Inbox->new({
27         inboxdir => "$tmpdir/testbox",
28         name => $this,
29         version => 2,
30         -primary_address => 'test@example.com',
31         indexlevel => 'medium',
32 });
33 my @xcpdb = qw(-xcpdb -q);
34 my $nproc = 8;
35 my $ndoc = 13;
36 my $im = PublicInbox::InboxWritable->new($ibx, {nproc => $nproc})->importer(1);
37 for my $i (1..$ndoc) {
38         $mime->header_set('Message-ID', "<m$i\@example.com>");
39         ok($im->add($mime), "message $i added");
40 }
41 $im->done;
42 my @shards = grep(m!/\d+\z!, glob("$ibx->{inboxdir}/xap*/*"));
43 is(scalar(@shards), $nproc - 1, 'got expected shards');
44 my $orig = $ibx->over->query_xover(1, $ndoc);
45 my %nums = map {; "$_->{num}" => 1 } @$orig;
46
47 # ensure we can go up or down in shards, or stay the same:
48 for my $R (qw(2 4 1 3 3)) {
49         delete $ibx->{search}; # release old handles
50         my $cmd = [@xcpdb, "-R$R", $ibx->{inboxdir}];
51         push @$cmd, '--compact' if $R == 1;
52         ok(run_script($cmd), "xcpdb -R$R");
53         my @new_shards = grep(m!/\d+\z!, glob("$ibx->{inboxdir}/xap*/*"));
54         is(scalar(@new_shards), $R, 'resharded to two shards');
55         my $msgs = $ibx->search->query('s:this');
56         is(scalar(@$msgs), $ndoc, 'got expected docs after resharding');
57         my %by_mid = map {; "$_->{mid}" => $_ } @$msgs;
58         ok($by_mid{"m$_\@example.com"}, "$_ exists") for (1..$ndoc);
59
60         delete $ibx->{search}; # release old handles
61
62         # ensure docids in Xapian match NNTP article numbers
63         my $tot = 0;
64         my %tmp = %nums;
65         my $XapianDatabase = do {
66                 no warnings 'once';
67                 $PublicInbox::Search::X{Database};
68         };
69         foreach my $d (@new_shards) {
70                 my $xdb = $XapianDatabase->new($d);
71                 $tot += $xdb->get_doccount;
72                 my $it = $xdb->postlist_begin('');
73                 my $end = $xdb->postlist_end('');
74                 for (; $it != $end; $it++) {
75                         my $docid = $it->get_docid;
76                         if ($xdb->get_document($docid)) {
77                                 ok(delete($tmp{$docid}), "saw #$docid");
78                         }
79                 }
80         }
81         is(scalar keys %tmp, 0, 'all docids seen');
82 }
83
84 done_testing();
85 1;