From: Eric Wong (Contractor, The Linux Foundation) <e@80x24.org>
Date: Thu, 5 Apr 2018 09:34:06 +0000 (+0000)
Subject: v2writable: recount partitions after acquiring lock
X-Git-Tag: v1.1.0-pre1~60
X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=21c5042a727524b8477cad5ca1b9abf836dabad5;p=public-inbox.git

v2writable: recount partitions after acquiring lock

The partition count can change if public-inbox-compact runs
while public-inbox-watch or public-inbox-index is running.
---

diff --git a/lib/PublicInbox/V2Writable.pm b/lib/PublicInbox/V2Writable.pm
index 74953d34..e0c8ac37 100644
--- a/lib/PublicInbox/V2Writable.pm
+++ b/lib/PublicInbox/V2Writable.pm
@@ -26,22 +26,14 @@ sub nproc () {
 	int($ENV{NPROC} || `nproc 2>/dev/null` || 2);
 }
 
-sub new {
-	my ($class, $v2ibx, $creat) = @_;
-	my $dir = $v2ibx->{mainrepo} or die "no mainrepo in inbox\n";
-	unless (-d $dir) {
-		if ($creat) {
-			require File::Path;
-			File::Path::mkpath($dir);
-		} else {
-			die "$dir does not exist\n";
-		}
-	}
-
+sub count_partitions ($) {
+	my ($self) = @_;
 	my $nparts = 0;
-	my $xpfx = "$dir/xap" . PublicInbox::Search::SCHEMA_VERSION;
+	my $xpfx = $self->{xpfx};
 
 	# always load existing partitions in case core count changes:
+	# Also, partition count may change while -watch is running
+	# due to -compact
 	if (-d $xpfx) {
 		foreach my $part (<$xpfx/*>) {
 			-d $part && $part =~ m!/\d+\z! or next;
@@ -51,21 +43,37 @@ sub new {
 			};
 		}
 	}
-	$nparts = nproc() if ($nparts == 0);
+	$nparts;
+}
+
+sub new {
+	my ($class, $v2ibx, $creat) = @_;
+	my $dir = $v2ibx->{mainrepo} or die "no mainrepo in inbox\n";
+	unless (-d $dir) {
+		if ($creat) {
+			require File::Path;
+			File::Path::mkpath($dir);
+		} else {
+			die "$dir does not exist\n";
+		}
+	}
 
 	$v2ibx = PublicInbox::InboxWritable->new($v2ibx);
+
+	my $xpfx = "$dir/xap" . PublicInbox::Search::SCHEMA_VERSION;
 	my $self = {
 		-inbox => $v2ibx,
 		im => undef, #  PublicInbox::Import
-		partitions => $nparts,
 		parallel => 1,
 		transact_bytes => 0,
+		xpfx => $xpfx,
 		over => PublicInbox::OverIdxFork->new("$xpfx/over.sqlite3"),
 		lock_path => "$dir/inbox.lock",
 		# limit each repo to 1GB or so
 		rotate_bytes => int((1024 * 1024 * 1024) / $PACKING_FACTOR),
 		last_commit => [],
 	};
+	$self->{partitions} = count_partitions($self) || nproc();
 	bless $self, $class;
 }
 
@@ -206,6 +214,12 @@ sub idx_init {
 		$self->lock_acquire;
 		$over->create($self);
 
+		# -compact can change partition count while -watch is idle
+		my $nparts = count_partitions($self);
+		if ($nparts && $nparts != $self->{partitions}) {
+			$self->{partitions} = $nparts;
+		}
+
 		# need to create all parts before initializing msgmap FD
 		my $max = $self->{partitions} - 1;