]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/V2Writable.pm
favor `getconf _NPROCESSORS_ONLN` over GNU nproc
[public-inbox.git] / lib / PublicInbox / V2Writable.pm
index 344edbbad2010668fd8170f7af9b28c9a9e4fbfa..dfcb489723d29a6faf0283efb133585e4a5949e6 100644 (file)
@@ -35,14 +35,13 @@ my $PACKING_FACTOR = 0.4;
 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.
@@ -122,7 +121,7 @@ sub new {
                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;
 }
@@ -152,6 +151,12 @@ sub add {
        $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) = @_;
@@ -160,7 +165,7 @@ sub do_idx ($$$$) {
        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 {
@@ -292,7 +297,7 @@ sub _idx_init { # with_umask callback
        # 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;
 }
 
@@ -875,7 +880,8 @@ sub reindex_checkpoint ($$) {
 
        $self->{ibx}->git->cleanup; # *async_wait
        ${$sync->{need_checkpoint}} = 0;
-       $sync->{mm_tmp}->atfork_prepare;
+       my $mm_tmp = $sync->{mm_tmp};
+       $mm_tmp->atfork_prepare if $mm_tmp;
        $self->done; # release lock
 
        if (my $pr = $sync->{-opt}->{-progress}) {
@@ -884,7 +890,7 @@ sub reindex_checkpoint ($$) {
 
        # allow -watch or -mda to write...
        $self->idx_init; # reacquire lock
-       $sync->{mm_tmp}->atfork_parent;
+       $mm_tmp->atfork_parent if $mm_tmp;
 }
 
 sub index_oid { # cat_async callback
@@ -1085,7 +1091,10 @@ sub sync_prepare ($$$) {
                }
                $all->cat_async_wait;
        }
-       return 0 if (!$regen_max && !keys(%{$self->{unindex_range}}));
+       if (!$regen_max && !keys(%{$self->{unindex_range}})) {
+               $sync->{-regen_fmt} = "%u/?\n";
+               return 0;
+       }
 
        # reindex should NOT see new commits anymore, if we do,
        # it's a problem and we need to notice it via die()
@@ -1177,6 +1186,38 @@ sub sync_ranges ($$$) {
        $ranges;
 }
 
+sub index_xap_only { # git->cat_async callback
+       my ($bref, $oid, $type, $size, $smsg) = @_;
+       my $self = $smsg->{v2w};
+       my $idx = idx_shard($self, $smsg->{num} % $self->{shards});
+       $smsg->{raw_bytes} = $size;
+       $idx->index_raw($bref, undef, $smsg);
+       $self->{transact_bytes} += $size;
+}
+
+sub index_xap_step ($$$;$) {
+       my ($self, $sync, $beg, $step) = @_;
+       my $ibx = $self->{ibx};
+       my $all = $ibx->git;
+       my $over = $ibx->over;
+       my $batch_bytes = batch_bytes($self);
+       $step //= $self->{shards};
+       my $end = $sync->{art_end};
+       if (my $pr = $sync->{-opt}->{-progress}) {
+               $pr->("Xapian indexlevel=$ibx->{indexlevel} ".
+                       "$beg..$end (% $step)\n");
+       }
+       for (my $num = $beg; $num <= $end; $num += $step) {
+               my $smsg = $over->get_art($num) or next;
+               $smsg->{v2w} = $self;
+               $all->cat_async($smsg->{blob}, \&index_xap_only, $smsg);
+               if ($self->{transact_bytes} >= $batch_bytes) {
+                       ${$sync->{nr}} = $num;
+                       reindex_checkpoint($self, $sync);
+               }
+       }
+}
+
 sub index_epoch ($$$) {
        my ($self, $sync, $i) = @_;
 
@@ -1210,14 +1251,46 @@ sub index_epoch ($$$) {
        update_last_commit($self, $git, $i, $stk->{latest_cmt});
 }
 
+sub xapian_only {
+       my ($self, $opt, $sync) = @_;
+       my $seq = $opt->{sequentialshard};
+       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;
+                       index_xap_step($self, $sync, $_) for (0..$shard_end);
+               } else { # parallel (maybe)
+                       index_xap_step($self, $sync, 0, 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 $idxlevel = $self->{ibx}->{indexlevel};
+       local $self->{ibx}->{indexlevel} = 'basic' if $seq;
+
        $self->idx_init($opt); # acquire lock
        fill_alternates($self, $epoch_max);
        $self->{over}->rethread_prepare($opt);
@@ -1244,13 +1317,19 @@ sub index_sync {
        }
        # work forwards through history
        index_epoch($self, $sync, $_) for (0..$epoch_max);
+       $self->{over}->rethread_done($opt);
        $self->done;
 
        if (my $nr = $sync->{nr}) {
                my $pr = $sync->{-opt}->{-progress};
                $pr->('all.git '.sprintf($sync->{-regen_fmt}, $$nr)) if $pr;
        }
-       $self->{over}->rethread_done($opt);
+
+       if ($seq) { # deal with Xapian shards sequentially
+               $self->{ibx}->{indexlevel} = $idxlevel;
+               delete $sync->{mm_tmp};
+               xapian_only($self, $opt, $sync);
+       }
 
        # reindex does not pick up new changes, so we rerun w/o it:
        if ($opt->{reindex}) {