our $NPROC_MAX_DEFAULT = 4;
sub detect_nproc () {
- for my $nproc (qw(nproc gnproc)) { # GNU coreutils nproc
- `$nproc 2>/dev/null` =~ /^(\d+)$/ and return $1;
- }
-
# getconf(1) is POSIX, but *NPROCESSORS* vars are not
for (qw(_NPROCESSORS_ONLN NPROCESSORS_ONLN)) {
`getconf $_ 2>/dev/null` =~ /^(\d+)$/ and return $1;
}
+ for my $nproc (qw(nproc gnproc)) { # GNU coreutils nproc
+ `$nproc 2>/dev/null` =~ /^(\d+)$/ and return $1;
+ }
# should we bother with `sysctl hw.ncpu`? Those only give
# us total processor count, not online processor count.
rotate_bytes => int((1024 * 1024 * 1024) / $PACKING_FACTOR),
last_commit => [], # git epoch -> commit
};
- $self->{over}->{-no_sync} = 1 if $v2ibx->{-no_sync};
+ $self->{over}->{-no_fsync} = 1 if $v2ibx->{-no_fsync};
$self->{shards} = count_shards($self) || nproc_shards($creat);
bless $self, $class;
}
$self->{ibx}->with_umask(\&_add, $self, $eml, $check_cb);
}
+sub batch_bytes ($) {
+ my ($self) = @_;
+ ($self->{parallel} ? $self->{shards} : 1) *
+ $PublicInbox::SearchIdx::BATCH_BYTES;
+}
+
# indexes a message, returns true if checkpointing is needed
sub do_idx ($$$$) {
my ($self, $msgref, $mime, $smsg) = @_;
my $idx = idx_shard($self, $smsg->{num} % $self->{shards});
$idx->index_raw($msgref, $mime, $smsg);
my $n = $self->{transact_bytes} += $smsg->{raw_bytes};
- $n >= ($PublicInbox::SearchIdx::BATCH_BYTES * $self->{shards});
+ $n >= batch_bytes($self);
}
sub _add {
# for SQLite:
my $mm = $self->{mm} = PublicInbox::Msgmap->new_file(
"$self->{ibx}->{inboxdir}/msgmap.sqlite3",
- $self->{ibx}->{-no_sync} ? 2 : 1);
+ $self->{ibx}->{-no_fsync} ? 2 : 1);
$mm->{dbh}->begin_work;
}
my ($bref, $oid, $type, $size, $smsg) = @_;
my $self = $smsg->{v2w};
my $idx = idx_shard($self, $smsg->{num} % $self->{shards});
- $idx->begin_txn_lazy;
- $idx->add_message(PublicInbox::Eml->new($bref), $smsg);
+ $smsg->{raw_bytes} = $size;
+ $idx->index_raw($bref, undef, $smsg);
$self->{transact_bytes} += $size;
}
-sub index_seq_shard ($$$) {
- my ($self, $sync, $off) = @_;
+sub index_xap_step ($$$;$) {
+ my ($self, $sync, $beg, $step) = @_;
+ my $end = $sync->{art_end};
+ return if $beg > $end; # nothing to do
+
+ $step //= $self->{shards};
my $ibx = $self->{ibx};
- my $max = $ibx->mm->max or return;
- my $all = $ibx->git;
- my $over = $ibx->over;
- my $batch_bytes = $PublicInbox::SearchIdx::BATCH_BYTES;
if (my $pr = $sync->{-opt}->{-progress}) {
- $pr->("Xapian indexlevel=$ibx->{indexlevel} % $off\n");
+ $pr->("Xapian indexlevel=$ibx->{indexlevel} ".
+ "$beg..$end (% $step)\n");
}
- for (my $num = $off; $num <= $max; $num += $self->{shards}) {
- my $smsg = $over->get_art($num) or next;
+ my $batch_bytes = batch_bytes($self);
+ for (my $num = $beg; $num <= $end; $num += $step) {
+ my $smsg = $ibx->over->get_art($num) or next;
$smsg->{v2w} = $self;
- $all->cat_async($smsg->{blob}, \&index_xap_only, $smsg);
+ $ibx->git->cat_async($smsg->{blob}, \&index_xap_only, $smsg);
if ($self->{transact_bytes} >= $batch_bytes) {
${$sync->{nr}} = $num;
reindex_checkpoint($self, $sync);
update_last_commit($self, $git, $i, $stk->{latest_cmt});
}
+sub xapian_only {
+ my ($self, $opt, $sync, $art_beg) = @_;
+ my $seq = $opt->{sequentialshard};
+ $art_beg //= 0;
+ local $self->{parallel} = 0 if $seq;
+ $self->idx_init($opt); # acquire lock
+ if (my $art_end = $self->{ibx}->mm->max) {
+ $sync //= {
+ need_checkpoint => \(my $bool = 0),
+ -opt => $opt,
+ v2w => $self,
+ nr => \(my $nr = 0),
+ -regen_fmt => "%u/?\n",
+ };
+ $sync->{art_end} = $art_end;
+ if ($seq || !$self->{parallel}) {
+ my $shard_end = $self->{shards} - 1;
+ for (0..$shard_end) {
+ index_xap_step($self, $sync, $art_beg + $_)
+ }
+ } else { # parallel (maybe)
+ index_xap_step($self, $sync, $art_beg, 1);
+ }
+ }
+ $self->{ibx}->git->cat_async_wait;
+ $self->done;
+}
+
# public, called by public-inbox-index
sub index_sync {
my ($self, $opt) = @_;
- $opt ||= {};
+ $opt //= $_[1] //= {};
+ goto \&xapian_only if $opt->{xapianonly};
+
my $pr = $opt->{-progress};
my $epoch_max;
my $latest = git_dir_latest($self, \$epoch_max);
return unless defined $latest;
my $seq = $opt->{sequentialshard};
+ my $art_beg; # the NNTP article number we start xapian_only at
my $idxlevel = $self->{ibx}->{indexlevel};
local $self->{ibx}->{indexlevel} = 'basic' if $seq;
$self->{mm}->{dbh}->begin_work;
$sync->{mm_tmp} =
$self->{mm}->tmp_clone($self->{ibx}->{inboxdir});
+
+ # xapian_only works incrementally w/o --reindex
+ if ($seq && !$opt->{reindex}) {
+ $art_beg = $sync->{mm_tmp}->max;
+ $art_beg++ if defined($art_beg);
+ }
}
if ($sync->{index_max_size} = $self->{ibx}->{index_max_size}) {
$sync->{index_oid} = \&index_oid;
$pr->('all.git '.sprintf($sync->{-regen_fmt}, $$nr)) if $pr;
}
- if ($seq) { # deal with Xapian shards sequentially
- my $end = $self->{shards} - 1;
+ # deal with Xapian shards sequentially
+ if ($seq && delete($sync->{mm_tmp})) {
$self->{ibx}->{indexlevel} = $idxlevel;
- delete $sync->{mm_tmp};
- $self->idx_init($opt); # re-acquire lock
- index_seq_shard($self, $sync, $_) for (0..$end);
- $self->{ibx}->git->cat_async_wait;
- $self->done;
+ xapian_only($self, $opt, $sync, $art_beg);
}
# reindex does not pick up new changes, so we rerun w/o it: