user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
Search results ordered by [date|relevance]  view[summary|nested|Atom feed]
thread overview below | download mbox.gz: |
* [PATCH 2/2] extindex: -xcpdb and -compact support
  2021-07-30 12:18  7% [PATCH 0/2] extindex admin updates Eric Wong
@ 2021-07-30 12:18  3% ` Eric Wong
  0 siblings, 0 replies; 2+ results
From: Eric Wong @ 2021-07-30 12:18 UTC (permalink / raw)
  To: meta

Since extindex uses Xapian shards in a similar way to
v2 inboxes, we'll support -xcpdb (reshard+upgrade) and
-compact all the same to give admins tuning+upgrade
options.
---
 lib/PublicInbox/Admin.pm      | 40 ++++++++++++++++++++++--
 lib/PublicInbox/Search.pm     |  2 +-
 lib/PublicInbox/V2Writable.pm |  6 ++--
 lib/PublicInbox/Xapcmd.pm     | 58 ++++++++++++++++++++++-------------
 script/public-inbox-compact   | 16 ++++++----
 script/public-inbox-xcpdb     | 18 ++++++-----
 t/extsearch.t                 | 32 +++++++++++++++++++
 t/indexlevels-mirror.t        |  3 +-
 8 files changed, 133 insertions(+), 42 deletions(-)

diff --git a/lib/PublicInbox/Admin.pm b/lib/PublicInbox/Admin.pm
index d5f867a2..2534958b 100644
--- a/lib/PublicInbox/Admin.pm
+++ b/lib/PublicInbox/Admin.pm
@@ -28,6 +28,27 @@ sub setup_signals {
 	};
 }
 
+sub resolve_eidxdir {
+	my ($cd) = @_;
+	my $try = $cd // '.';
+	my $root_dev_ino;
+	while (1) { # favor v2, first
+		if (-f "$try/ei.lock") {
+			return rel2abs_collapsed($try);
+		} elsif (-d $try) {
+			my @try = stat _;
+			$root_dev_ino //= do {
+				my @root = stat('/') or die "stat /: $!\n";
+				"$root[0]\0$root[1]";
+			};
+			return undef if "$try[0]\0$try[1]" eq $root_dev_ino;
+			$try .= '/..'; # continue, cd up
+		} else {
+			die "`$try' is not a directory\n";
+		}
+	}
+}
+
 sub resolve_inboxdir {
 	my ($cd, $ver) = @_;
 	my $try = $cd // '.';
@@ -107,11 +128,24 @@ sub resolve_inboxes ($;$$) {
 		$cfg or die "--all specified, but $cfgfile not readable\n";
 		@$argv and die "--all specified, but directories specified\n";
 	}
-
+	my (@old, @ibxs, @eidx);
+	if ($opt->{-eidx_ok}) {
+		require PublicInbox::ExtSearchIdx;
+		my $i = -1;
+		@$argv = grep {
+			$i++;
+			if (defined(my $ei = resolve_eidxdir($_))) {
+				$ei = PublicInbox::ExtSearchIdx->new($ei, $opt);
+				push @eidx, $ei;
+				undef;
+			} else {
+				1;
+			}
+		} @$argv;
+	}
 	my $min_ver = $opt->{-min_inbox_version} || 0;
 	# lookup inboxes by st_dev + st_ino instead of {inboxdir} pathnames,
 	# pathnames are not unique due to symlinks and bind mounts
-	my (@old, @ibxs);
 	if ($opt->{all}) {
 		$cfg->each_inbox(sub {
 			my ($ibx) = @_;
@@ -161,7 +195,7 @@ sub resolve_inboxes ($;$$) {
 		die "-V$min_ver inboxes not supported by $0\n\t",
 		    join("\n\t", @old), "\n";
 	}
-	@ibxs;
+	$opt->{-eidx_ok} ? (\@ibxs, \@eidx) : @ibxs;
 }
 
 # TODO: make Devel::Peek optional, only used for daemon
diff --git a/lib/PublicInbox/Search.pm b/lib/PublicInbox/Search.pm
index 7e19e616..e80a5944 100644
--- a/lib/PublicInbox/Search.pm
+++ b/lib/PublicInbox/Search.pm
@@ -187,7 +187,7 @@ sub xdir ($;$) {
 	my ($self, $rdonly) = @_;
 	if ($rdonly || !defined($self->{shard})) {
 		$self->{xpfx};
-	} else { # v2 only:
+	} else { # v2 + extindex only:
 		"$self->{xpfx}/$self->{shard}";
 	}
 }
diff --git a/lib/PublicInbox/V2Writable.pm b/lib/PublicInbox/V2Writable.pm
index 025487d2..1288f47b 100644
--- a/lib/PublicInbox/V2Writable.pm
+++ b/lib/PublicInbox/V2Writable.pm
@@ -54,14 +54,14 @@ sub nproc_shards ($) {
 
 sub count_shards ($) {
 	my ($self) = @_;
+	# always load existing shards in case core count changes:
+	# Also, shard count may change while -watch is running
 	if (my $ibx = $self->{ibx}) {
-		# always load existing shards in case core count changes:
-		# Also, shard count may change while -watch is running
 		my $srch = $ibx->search or return 0;
 		delete $ibx->{search};
 		$srch->{nshard} // 0
 	} else { # ExtSearchIdx
-		$self->{nshard} ||= scalar($self->xdb_shards_flat);
+		$self->{nshard} = scalar($self->xdb_shards_flat);
 	}
 }
 
diff --git a/lib/PublicInbox/Xapcmd.pm b/lib/PublicInbox/Xapcmd.pm
index 9791f02c..e37eece5 100644
--- a/lib/PublicInbox/Xapcmd.pm
+++ b/lib/PublicInbox/Xapcmd.pm
@@ -61,14 +61,14 @@ sub commit_changes ($$$$) {
 	}
 
 	# trigger ->check_inodes in read-only daemons
-	syswrite($im->{lockfh}, '.') if $over_chg;
+	syswrite($im->{lockfh}, '.') if $over_chg && $im;
 
 	remove_tree(@old_shard);
 	$tmp = undef;
 	if (!$opt->{-coarse_lock}) {
 		$opt->{-skip_lock} = 1;
-
-		if ($im->can('count_shards')) {
+		$im //= $ibx if $ibx->can('eidx_sync');
+		if ($im->can('count_shards')) { # v2w or eidx
 			my $pr = $opt->{-progress};
 			my $n = $im->count_shards;
 			if (defined $reshard && $n != $reshard) {
@@ -83,7 +83,11 @@ sub commit_changes ($$$$) {
 		}
 		my $env = $opt->{-idx_env};
 		local %ENV = (%ENV, %$env) if $env;
-		PublicInbox::Admin::index_inbox($ibx, $im, $opt);
+		if ($ibx->can('eidx_sync')) {
+			$ibx->eidx_sync($opt);
+		} else {
+			PublicInbox::Admin::index_inbox($ibx, $im, $opt);
+		}
 	}
 }
 
@@ -103,9 +107,10 @@ sub runnable_or_die ($) {
 	which($exe) or die "$exe not found in PATH\n";
 }
 
-sub prepare_reindex ($$$) {
-	my ($ibx, $im, $opt) = @_;
-	if ($ibx->version == 1) {
+sub prepare_reindex ($$) {
+	my ($ibx, $opt) = @_;
+	if ($ibx->can('eidx_sync')) { # no prep needed for ExtSearchIdx
+	} elsif ($ibx->version == 1) {
 		my $dir = $ibx->search->xdir(1);
 		my $xdb = $PublicInbox::Search::X{Database}->new($dir);
 		if (my $lc = $xdb->get_metadata('last_commit')) {
@@ -172,9 +177,14 @@ sub prepare_run {
 	my ($ibx, $opt) = @_;
 	my $tmp = {}; # old shard dir => File::Temp->newdir object or undef
 	my @queue; # ([old//src,newdir]) - list of args for cpdb() or compact()
-	my $old;
-	if (my $srch = $ibx->search) {
+	my ($old, $misc_ok);
+	if ($ibx->can('eidx_sync')) {
+		$misc_ok = 1;
+		$old = $ibx->xdir(1);
+	} elsif (my $srch = $ibx->search) {
 		$old = $srch->xdir(1);
+	}
+	if (defined $old) {
 		-d $old or die "$old does not exist\n";
 	}
 	my $reshard = $opt->{reshard};
@@ -184,7 +194,7 @@ sub prepare_run {
 
 	# we want temporary directories to be as deep as possible,
 	# so v2 shards can keep "xap$SCHEMA_VERSION" on a separate FS.
-	if ($old && $ibx->version == 1) {
+	if (defined($old) && $ibx->can('version') && $ibx->version == 1) {
 		if (defined $reshard) {
 			warn
 "--reshard=$reshard ignored for v1 $ibx->{inboxdir}\n";
@@ -196,7 +206,7 @@ sub prepare_run {
 		$tmp->{$old} = $wip;
 		nodatacow_dir($wip->dirname);
 		push @queue, [ $old, $wip ];
-	} elsif ($old) {
+	} elsif (defined $old) {
 		opendir my $dh, $old or die "Failed to opendir $old: $!\n";
 		my @old_shards;
 		while (defined(my $dn = readdir($dh))) {
@@ -204,6 +214,7 @@ sub prepare_run {
 				push @old_shards, $dn;
 			} elsif ($dn eq '.' || $dn eq '..') {
 			} elsif ($dn =~ /\Aover\.sqlite3/) {
+			} elsif ($dn eq 'misc' && $misc_ok) {
 			} else {
 				warn "W: skipping unknown dir: $old/$dn\n"
 			}
@@ -239,19 +250,19 @@ sub check_compact () { runnable_or_die($XAPIAN_COMPACT) }
 
 sub _run { # with_umask callback
 	my ($ibx, $cb, $opt) = @_;
-	my $im = $ibx->importer(0);
-	$im->lock_acquire;
+	my $im = $ibx->can('importer') ? $ibx->importer(0) : undef;
+	($im // $ibx)->lock_acquire;
 	my ($tmp, $queue) = prepare_run($ibx, $opt);
 
 	# fine-grained locking if we prepare for reindex
 	if (!$opt->{-coarse_lock}) {
-		prepare_reindex($ibx, $im, $opt);
-		$im->lock_release;
+		prepare_reindex($ibx, $opt);
+		($im // $ibx)->lock_release;
 	}
 
-	$ibx->cleanup;
+	$ibx->cleanup if $ibx->can('cleanup');
 	process_queue($queue, $cb, $opt);
-	$im->lock_acquire if !$opt->{-coarse_lock};
+	($im // $ibx)->lock_acquire if !$opt->{-coarse_lock};
 	commit_changes($ibx, $im, $tmp, $opt);
 }
 
@@ -259,11 +270,16 @@ sub run {
 	my ($ibx, $task, $opt) = @_; # task = 'cpdb' or 'compact'
 	my $cb = \&$task;
 	PublicInbox::Admin::progress_prepare($opt ||= {});
-	defined(my $dir = $ibx->{inboxdir}) or die "no inboxdir defined\n";
-	-d $dir or die "inboxdir=$dir does not exist\n";
+	my $dir;
+	for my $fld (qw(inboxdir topdir)) {
+		my $d = $ibx->{$fld} // next;
+		-d $d or die "$fld=$d does not exist\n";
+		$dir = $d;
+		last;
+	}
 	check_compact() if $opt->{compact} && $ibx->search;
 
-	if (!$opt->{-coarse_lock}) {
+	if (!$ibx->can('eidx_sync') && !$opt->{-coarse_lock}) {
 		# per-epoch ranges for v2
 		# v1:{ from => $OID }, v2:{ from => [ $OID, $OID, $OID ] } }
 		$opt->{reindex} = { from => $ibx->version == 1 ? '' : [] };
@@ -393,7 +409,7 @@ sub cpdb ($$) { # cb_spawn callback
 	PublicInbox::SearchIdx::load_xapian_writable() or die;
 	my $XapianDatabase = $PublicInbox::Search::X{Database};
 	if (ref($old) eq 'ARRAY') {
-		($cur_shard) = ($new =~ m!xap[0-9]+/([0-9]+)\b!);
+		($cur_shard) = ($new =~ m!(?:xap|ei)[0-9]+/([0-9]+)\b!);
 		defined $cur_shard or
 			die "BUG: could not extract shard # from $new";
 		$reshard = $opt->{reshard};
diff --git a/script/public-inbox-compact b/script/public-inbox-compact
index ab1d1e5e..6e34aaeb 100755
--- a/script/public-inbox-compact
+++ b/script/public-inbox-compact
@@ -4,9 +4,9 @@
 use strict;
 use v5.10.1;
 use Getopt::Long qw(:config gnu_getopt no_ignore_case auto_abbrev);
-my $opt = { compact => 1, -coarse_lock => 1 };
+my $opt = { compact => 1, -coarse_lock => 1, -eidx_ok => 1 };
 my $help = <<EOF; # the following should fit w/o scrolling in 80x24 term:
-usage: public-inbox-compact INBOX_DIR
+usage: public-inbox-compact <INBOX_DIR|EXTINDEX_DIR>
 
   Compact Xapian DBs in an inbox
 
@@ -29,9 +29,13 @@ PublicInbox::Admin::progress_prepare($opt);
 
 require PublicInbox::InboxWritable;
 require PublicInbox::Xapcmd;
-my @ibxs = PublicInbox::Admin::resolve_inboxes(\@ARGV, $opt);
-unless (@ibxs) { print STDERR $help; exit 1 }
-foreach (@ibxs) {
-	my $ibx = PublicInbox::InboxWritable->new($_);
+my $cfg = PublicInbox::Config->new;
+my ($ibxs, $eidxs) = PublicInbox::Admin::resolve_inboxes(\@ARGV, $opt, $cfg);
+unless ($ibxs) { print STDERR $help; exit 1 }
+for my $ibx (@$ibxs) {
+	$ibx = PublicInbox::InboxWritable->new($ibx);
 	PublicInbox::Xapcmd::run($ibx, 'compact', $opt);
 }
+for my $eidx (@$eidxs) {
+	PublicInbox::Xapcmd::run($eidx, 'compact', $opt);
+}
diff --git a/script/public-inbox-xcpdb b/script/public-inbox-xcpdb
index 768dc2ba..81d1a85b 100755
--- a/script/public-inbox-xcpdb
+++ b/script/public-inbox-xcpdb
@@ -5,7 +5,7 @@ use strict;
 use v5.10.1;
 use Getopt::Long qw(:config gnu_getopt no_ignore_case auto_abbrev);
 my $help = <<EOF; # the following should fit w/o scrolling in 80x24 term:
-usage: public-inbox-xcpdb [options] INBOX_DIR
+usage: public-inbox-xcpdb [options] <INBOX_DIR|EXTINDEX_DIR>
 
   upgrade or reshard Xapian DB(s) used by public-inbox
 
@@ -26,7 +26,7 @@ index options (see public-inbox-index(1) man page for full description):
 
 See public-inbox-xcpdb(1) man page for full documentation.
 EOF
-my $opt = { quiet => -1, compact => 0, fsync => 1 };
+my $opt = { quiet => -1, compact => 0, fsync => 1, -eidx_ok => 1 };
 GetOptions($opt, qw(
 	fsync|sync! compact|c reshard|R=i
 	max_size|max-size=s batch_size|batch-size=s
@@ -41,8 +41,8 @@ PublicInbox::Admin::require_or_die('-search');
 
 require PublicInbox::Config;
 my $cfg = PublicInbox::Config->new;
-my @ibxs = PublicInbox::Admin::resolve_inboxes(\@ARGV, $opt, $cfg) or
-	die $help;
+my ($ibxs, $eidxs) = PublicInbox::Admin::resolve_inboxes(\@ARGV, $opt, $cfg);
+unless ($ibxs) { print STDERR $help; exit 1 }
 my $idx_env = PublicInbox::Admin::index_prepare($opt, $cfg);
 
 # we only set XAPIAN_FLUSH_THRESHOLD for index, since cpdb doesn't
@@ -56,8 +56,12 @@ if ($opt->{'sequential-shard'} && ($opt->{jobs} // 1) > 1) {
 
 require PublicInbox::InboxWritable;
 require PublicInbox::Xapcmd;
-foreach (@ibxs) {
-	my $ibx = PublicInbox::InboxWritable->new($_);
-	# we rely on --no-renumber to keep docids synched for NNTP
+# we rely on --no-renumber to keep docids synched for NNTP(artnum) + IMAP(UID)
+for my $ibx (@$ibxs) {
+	$ibx = PublicInbox::InboxWritable->new($ibx);
 	PublicInbox::Xapcmd::run($ibx, 'cpdb', $opt);
 }
+
+for my $eidx (@$eidxs) {
+	PublicInbox::Xapcmd::run($eidx, 'cpdb', $opt);
+}
diff --git a/t/extsearch.t b/t/extsearch.t
index 1f62e80c..d933b948 100644
--- a/t/extsearch.t
+++ b/t/extsearch.t
@@ -422,4 +422,36 @@ for my $j (1, 3, 6) {
 	like($dirs[-1], qr!/ei[0-9]+/$max\z!, '-j works');
 }
 
+SKIP: {
+	my $d = "$home/extindex-j1";
+	my $o = { 2 => \(my $err = '') };
+	ok(run_script([qw(-xcpdb -R4), $d]), 'xcpdb R4');
+	my @dirs = glob("$d/ei*/?");
+	for my $i (0..3) {
+		is(grep(m!/ei[0-9]+/$i\z!, @dirs), 1, "shard [$i] created");
+	}
+	for my $i (4..5) {
+		is(grep(m!/ei[0-9]+/$i\z!, @dirs), 0, "no shard [$i]");
+	}
+
+	ok(run_script([qw(-xcpdb -R2), $d]), 'xcpdb -R2');
+	@dirs = glob("$d/ei*/?");
+	for my $i (0..1) {
+		is(grep(m!/ei[0-9]+/$i\z!, @dirs), 1, "shard [$i] kept");
+	}
+	for my $i (2..3) {
+		is(grep(m!/ei[0-9]+/$i\z!, @dirs), 0, "no shard [$i]");
+	}
+	skip 'xapian-compact missing', 4 unless have_xapian_compact;
+	ok(run_script([qw(-compact), $d], undef, $o), 'compact');
+	# n.b. stderr contains xapian-compact output
+
+	my @d2 = glob("$d/ei*/?");
+	is_deeply(\@d2, \@dirs, 'dirs consistent after compact');
+	ok(run_script([qw(-extindex --dedupe --all), $d]),
+		'--dedupe works after compact');
+	ok(run_script([qw(-extindex --gc), $d], undef, $o),
+		'--gc works after compact');
+}
+
 done_testing;
diff --git a/t/indexlevels-mirror.t b/t/indexlevels-mirror.t
index bd140cc4..e606e79b 100644
--- a/t/indexlevels-mirror.t
+++ b/t/indexlevels-mirror.t
@@ -158,7 +158,8 @@ my $import_index_incremental = sub {
 	SKIP: {
 		skip 'xapian-compact missing', 1 if !have_xapian_compact;
 		my $cmd = [ qw(-compact), $mirror ];
-		ok(run_script($cmd, undef, { 2 => \$err}), "compact $level");
+		ok(run_script($cmd, undef, { 2 => \$err}), "compact $level")
+			or diag $err;
 	}
 };
 

^ permalink raw reply related	[relevance 3%]

* [PATCH 0/2] extindex admin updates
@ 2021-07-30 12:18  7% Eric Wong
  2021-07-30 12:18  3% ` [PATCH 2/2] extindex: -xcpdb and -compact support Eric Wong
  0 siblings, 1 reply; 2+ results
From: Eric Wong @ 2021-07-30 12:18 UTC (permalink / raw)
  To: meta

-xcpdb and -compact now support extindex directories
so resharding can and defragmenting can happen

Eric Wong (2):
  admin: index_inbox: drop unnecessary check
  extindex: -xcpdb and -compact support

 lib/PublicInbox/Admin.pm      | 42 ++++++++++++++++++++++---
 lib/PublicInbox/Search.pm     |  2 +-
 lib/PublicInbox/V2Writable.pm |  6 ++--
 lib/PublicInbox/Xapcmd.pm     | 58 ++++++++++++++++++++++-------------
 script/public-inbox-compact   | 16 ++++++----
 script/public-inbox-xcpdb     | 18 ++++++-----
 t/extsearch.t                 | 32 +++++++++++++++++++
 t/indexlevels-mirror.t        |  3 +-
 8 files changed, 134 insertions(+), 43 deletions(-)

^ permalink raw reply	[relevance 7%]

Results 1-2 of 2 | reverse | options above
-- pct% links below jump to the message on this page, permalinks otherwise --
2021-07-30 12:18  7% [PATCH 0/2] extindex admin updates Eric Wong
2021-07-30 12:18  3% ` [PATCH 2/2] extindex: -xcpdb and -compact support Eric Wong

Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).