user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@yhbt.net>
To: meta@public-inbox.org
Subject: [PATCH] v2writable: newest epochs go first in alternates
Date: Sun, 26 Jan 2020 23:41:02 +0000	[thread overview]
Message-ID: <20200126234102.22958-1-e@yhbt.net> (raw)

New epochs are the most likely to have loose objects.  git won't
be able to take advantage of pack indices and needs to scan
every alternate for the loose object via open/openat syscalls.
Those syscalls will add up some day when we've got hundreds or
thousands of epochs.
---
 lib/PublicInbox/V2Writable.pm | 42 ++++++++++++++++++++++++++---------
 t/v2writable.t                | 24 ++++++++++++++++++++
 2 files changed, 55 insertions(+), 11 deletions(-)

diff --git a/lib/PublicInbox/V2Writable.pm b/lib/PublicInbox/V2Writable.pm
index 9073d9ef..8ad71b54 100644
--- a/lib/PublicInbox/V2Writable.pm
+++ b/lib/PublicInbox/V2Writable.pm
@@ -663,23 +663,43 @@ sub fill_alternates ($$) {
 	unless (-d $all) {
 		PublicInbox::Import::init_bare($all);
 	}
-	my $alt = "$all/objects/info/alternates";
-	my %alts;
-	my @add;
+	my $info_dir = "$all/objects/info";
+	my $alt = "$info_dir/alternates";
+	my (%alt, $new);
+	my $mode = 0644;
 	if (-e $alt) {
 		open(my $fh, '<', $alt) or die "open < $alt: $!\n";
-		%alts = map { chomp; $_ => 1 } (<$fh>);
+		$mode = (stat($fh))[2] & 07777;
+
+		# we assign a sort score to every alternate and favor
+		# the newest (highest numbered) one when we
+		my $score;
+		my $other = 0; # in case admin adds non-epoch repos
+		%alt = map {;
+			if (m!\A\Q../../\E([0-9]+)\.git/objects\z!) {
+				$score = $1 + 0;
+			} else {
+				$score = --$other;
+			}
+			$_ => $score;
+		} split(/\n+/, do { local $/; <$fh> });
 	}
+
 	foreach my $i (0..$epoch) {
 		my $dir = "../../git/$i.git/objects";
-		push @add, $dir if !$alts{$dir} && -d "$pfx/$i.git";
-	}
-	return unless @add;
-	open my $fh, '>>', $alt or die "open >> $alt: $!\n";
-	foreach my $dir (@add) {
-		print $fh "$dir\n" or die "print >> $alt: $!\n";
+		if (!exists($alt{$dir}) && -d "$pfx/$i.git") {
+			$alt{$dir} = $i;
+			$new = 1;
+		}
 	}
-	close $fh or die "close $alt: $!\n";
+	return unless $new;
+
+	my ($fh, $tmp) = tempfile('alt-XXXXXXXX', DIR => $info_dir);
+	print $fh join("\n", sort { $alt{$b} <=> $alt{$a} } keys %alt), "\n"
+		or die "print $tmp: $!\n";
+	chmod($mode, $fh) or die "fchmod $tmp: $!\n";
+	close $fh or die "close $tmp $!\n";
+	rename($tmp, $alt) or die "rename $tmp => $alt: $!\n";
 }
 
 sub git_init {
diff --git a/t/v2writable.t b/t/v2writable.t
index 63f27f96..7ceaa6e4 100644
--- a/t/v2writable.t
+++ b/t/v2writable.t
@@ -284,4 +284,28 @@ eval {
 };
 ok($@, 'V2Writable fails on non-existent dir');
 
+{
+	my $v2w = PublicInbox::V2Writable->new($tmp, 1);
+	ok($v2w, 'creat flag works');
+	$v2w->{parallel} = 0;
+	$v2w->init_inbox(0);
+	my $alt = "$tmp->{inboxdir}/all.git/objects/info/alternates";
+	open my $fh, '>>', $alt or die $!;
+	print $fh "$inboxdir/all.git/objects\n" or die $!;
+	chmod(0664, $fh) or die "fchmod: $!";
+	close $fh or die $!;
+	open $fh, '<', $alt or die $!;
+	my $before = do { local $/; <$fh> };
+
+	ok($v2w->git_init(3), 'init a new epoch');
+	open $fh, '<', $alt or die $!;
+	my $after = do { local $/; <$fh> };
+	ok(index($after, $before) > 0,
+		'old contents preserved after adding epoch');
+	like($after, qr!\A[^\n]+?/3\.git/objects\n!s,
+		'first line is newest epoch');
+	my $mode = (stat($alt))[2] & 07777;
+	is($mode, 0664, sprintf('0%03o', $mode).' is 0664');
+}
+
 done_testing();

                 reply	other threads:[~2020-01-26 23:41 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200126234102.22958-1-e@yhbt.net \
    --to=e@yhbt.net \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).