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