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