user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 57/95] lei_mirror: forkgroups use `git fetch --multiple'
Date: Mon, 28 Nov 2022 05:31:54 +0000	[thread overview]
Message-ID: <20221128053232.291618-58-e@80x24.org> (raw)
In-Reply-To: <20221128053232.291618-1-e@80x24.org>

This offloads network parallelization and safety off to git
itself while reducing the amount of unnecessary process spawning
we do.  This also improves readability of pack-refs invocations
and reduces the need for them.

To prevent heavily-forked repos from hitting system command-line
size limits, we group refs to be updated in the "fgrptmp" group.
---
 lib/PublicInbox/LeiMirror.pm | 160 +++++++++++++++++++++++------------
 1 file changed, 108 insertions(+), 52 deletions(-)

diff --git a/lib/PublicInbox/LeiMirror.pm b/lib/PublicInbox/LeiMirror.pm
index 00732128..8b55a7da 100644
--- a/lib/PublicInbox/LeiMirror.pm
+++ b/lib/PublicInbox/LeiMirror.pm
@@ -316,7 +316,7 @@ sub fgrp_update {
 	}
 	close($w) or warn "E: close(update-ref --stdin): $! (need git 1.8.5+)\n";
 	$LIVE->{$pid} = [ \&reap_cmd, $fgrp, $cmd ];
-	do_reap($fgrp);
+	pack_refs($fgrp, $fgrp->{cur_dst});
 }
 
 sub pack_refs {
@@ -324,49 +324,101 @@ sub pack_refs {
 	do_reap($self);
 	my $cmd = [ 'git', "--git-dir=$git_dir", qw(pack-refs --all --prune) ];
 	$self->{lei}->qerr("# @$cmd");
+	return if $self->{dry_run};
 	my $opt = { 1 => $self->{lei}->{1}, 2 => $self->{lei}->{2} };
 	$LIVE->{spawn($cmd, undef, $opt)} = [ \&reap_cmd, $self, $cmd ];
 }
 
-sub fgrp_fetched {
-	my ($fgrp) = @_;
-	return if $fgrp->{dry_run} || !$LIVE;
-	my $rn = $fgrp->{-remote};
-	my %opt = map { $_ => $fgrp->{lei}->{$_} } (0..2);
-	pack_refs($fgrp, $fgrp->{-osdir}); # objstore refs always packed
-
-	my $update_ref = PublicInbox::OnDestroy->new($$, \&fgrp_update, $fgrp);
-
-	my $src = [ 'git', "--git-dir=$fgrp->{-osdir}", 'for-each-ref',
-		"--format=refs/%(refname:lstrip=3)%00%(objectname)",
-		"refs/remotes/$rn/" ];
-	do_reap($fgrp);
-	open($fgrp->{srcfh}, '+>', undef) or die "open(src): $!";
-	$fgrp->{lei}->qerr("# @$src >SRC");
-	my $pid = spawn($src, undef, { %opt, 1 => $fgrp->{srcfh} });
-	$LIVE->{$pid} = [ \&reap_cmd, $fgrp, $src, $update_ref ];
-
-	my $dst = [ 'git', "--git-dir=$fgrp->{cur_dst}", 'for-each-ref',
-		'--format=%(refname)%00%(objectname)' ];
-	do_reap($fgrp);
-	open($fgrp->{dstfh}, '+>', undef) or die "open(dst): $!";
-	$fgrp->{lei}->qerr("# @$dst >DST");
-	$pid = spawn($dst, undef, { %opt, 1 => $fgrp->{dstfh} });
-	$LIVE->{$pid} = [ \&reap_cmd, $fgrp, $dst, $update_ref ];
-}
-
-sub fgrp_fetch {
-	my ($fgrp, $fini) = @_;
-	my $cmd = [ @{$fgrp->{-torsocks}}, 'git', "--git-dir=$fgrp->{-osdir}",
-			fetch_args($fgrp->{lei}, my $opt = {}), '--no-tags',
-			$fgrp->{-remote} ];
-	$fgrp->{-fini} = $fini;
-	do_reap($fgrp);
-	$fgrp->{lei}->qerr("# @$cmd");
-	return if $fgrp->{dry_run};
-	my $fgrp_fini = PublicInbox::OnDestroy->new($$, \&fgrp_fetched, $fgrp);
-	my $pid = spawn($cmd, undef, $opt);
-	$LIVE->{$pid} = [ \&reap_cmd, $fgrp, $cmd, $fgrp_fini ];
+sub fgrpv_done {
+	my ($fgrpv) = @_;
+	return if !$LIVE;
+	my $pid;
+	my $first = $fgrpv->[0] // die 'BUG: no fgrpv->[0]';
+	pack_refs($first, $first->{-osdir}); # objstore refs always packed
+	for my $fgrp (@$fgrpv) {
+		my $rn = $fgrp->{-remote};
+		my %opt = map { $_ => $fgrp->{lei}->{$_} } (0..2);
+
+		my $update_ref = $fgrp->{dry_run} ? undef :
+			PublicInbox::OnDestroy->new($$, \&fgrp_update, $fgrp);
+
+		my $src = [ 'git', "--git-dir=$fgrp->{-osdir}", 'for-each-ref',
+			"--format=refs/%(refname:lstrip=3)%00%(objectname)",
+			"refs/remotes/$rn/" ];
+		do_reap($fgrp);
+		$fgrp->{lei}->qerr("# @$src >SRC");
+		if ($update_ref) {
+			open(my $fh, '+>', undef) or die "open(src): $!";
+			$pid = spawn($src, undef, { %opt, 1 => $fh });
+			$fgrp->{srcfh} = $fh;
+			$LIVE->{$pid} = [ \&reap_cmd, $fgrp, $src, $update_ref ]
+		}
+		my $dst = [ 'git', "--git-dir=$fgrp->{cur_dst}", 'for-each-ref',
+			'--format=%(refname)%00%(objectname)' ];
+		do_reap($fgrp);
+		$fgrp->{lei}->qerr("# @$dst >DST");
+		if ($update_ref) {
+			open(my $fh, '+>', undef) or die "open(dst): $!";
+			$pid = spawn($dst, undef, { %opt, 1 => $fh });
+			$fgrp->{dstfh} = $fh;
+			$LIVE->{$pid} = [ \&reap_cmd, $fgrp, $dst, $update_ref ]
+		}
+	}
+}
+
+sub fgrp_fetch_all {
+	my ($self) = @_;
+	my $todo = delete $self->{fgrp_todo} or return;
+	keys(%$todo) or return;
+
+	# Rely on the fgrptmp remote groups in the config file rather
+	# than listing all remotes since the remote name list may exceed
+	# system argv limits:
+	my $grp = 'fgrptmp';
+
+	my @git = (@{$self->{-torsocks}}, 'git');
+	my $j = $self->{lei}->{opt}->{jobs};
+	my $opt = {};
+	my @fetch = do {
+		local $self->{lei}->{opt}->{jobs} = 1;
+		(fetch_args($self->{lei}, $opt),
+			qw(--no-tags --multiple));
+	};
+	push(@fetch, "-j$j") if $j;
+	my $pid;
+	while (my ($osdir, $fgrpv) = each %$todo) {
+		my $f = "$osdir/config";
+
+		# clobber group from previous run atomically
+		my $cmd = ['git', "--git-dir=$osdir", qw(config -f),
+				$f, '--unset-all', "remotes.$grp"];
+		$self->{lei}->qerr("# @$cmd");
+		if (!$self->{dry_run}) {
+			$pid = spawn($cmd);
+			waitpid($pid, 0) // die "waitpid: $!";
+			die "E: @$cmd: \$?=$?" if ($? && ($? >> 8) != 5);
+
+			# update the config atomically via O_APPEND while
+			# respecting git-config locking
+			sysopen(my $lk, "$f.lock", O_CREAT|O_EXCL|O_WRONLY)
+				or die "open($f.lock): $!";
+			open my $fh, '>>', $f or die "open(>>$f): $!";
+			$fh->autoflush(1);
+			my $buf = join('', "[remotes]\n",
+				map { "\t$grp = $_->{-remote}\n" } @$fgrpv);
+			print $fh $buf or die "print($f): $!";
+			close $fh or die "close($f): $!";
+			unlink("$f.lock") or die "unlink($f.lock): $!";
+		}
+
+		$cmd = [ @git, "--git-dir=$osdir", @fetch, $grp ];
+		do_reap($self);
+		$self->{lei}->qerr("# @$cmd");
+		my $end = PublicInbox::OnDestroy->new($$, \&fgrpv_done, $fgrpv);
+		return if $self->{dry_run};
+		$pid = spawn($cmd, undef, $opt);
+		$LIVE->{$pid} = [ \&reap_cmd, $self, $cmd, $end ];
+	}
 }
 
 # keep this idempotent for future use by public-inbox-fetch
@@ -400,7 +452,6 @@ sub forkgroup_prep {
 			run_die([@cmd, @kv], undef, $opt);
 		}
 	}
-	$self->{-do_pack_refs} = 1; # likely coderepo
 	if (!-d $self->{cur_dst} && !$self->{dry_run}) {
 		my $alt = File::Spec->rel2abs("$dir/objects");
 		PublicInbox::Import::init_bare($self->{cur_dst});
@@ -440,18 +491,21 @@ sub clone_v1 {
 	$self->{-torsocks} //= $curl->torsocks($lei, $uri) or return;
 	my $dst = $self->{cur_dst} // $self->{dst};
 	my $fini = PublicInbox::OnDestroy->new($$, \&v1_done, $self);
-	my $cmd = [ @{$self->{-torsocks}}, clone_cmd($lei, my $opt = {}),
-		"$uri", $dst ];
-	my $fgrp = forkgroup_prep($self, $uri);
-	if (!defined($fgrp) && defined($self->{-ent})) {
-		if (defined(my $ref = $self->{-ent}->{reference})) {
-			-e "$self->{dst}$ref" and
-				push @$cmd, '--reference', "$self->{dst}$ref";
+	if (my $fgrp = forkgroup_prep($self, $uri)) {
+		$fgrp->{-fini} = $fini;
+		push @{$self->{fgrp_todo}->{$fgrp->{-osdir}}}, $fgrp;
+	} else { # normal fetch
+		my $cmd = [ @{$self->{-torsocks}},
+				clone_cmd($lei, my $opt = {}), "$uri", $dst ];
+		if (defined($self->{-ent})) {
+			if (defined(my $ref = $self->{-ent}->{reference})) {
+				-e "$self->{dst}$ref" and
+					push @$cmd, '--reference',
+						"$self->{dst}$ref";
+			}
 		}
-	}
-	$fgrp ? fgrp_fetch($fgrp, $fini) :
 		start_clone($self, $cmd, $opt, $fini);
-
+	}
 	if (!$self->{-is_epoch} && $lei->{opt}->{'inbox-config'} =~
 				/\A(?:always|v1)\z/s) {
 		_get_txt_start($self, '_/text/config/raw', $fini);
@@ -576,7 +630,6 @@ sub v1_done { # called via OnDestroy
 			ft_rename($ft, $fn, 0666, $fh);
 		}
 	}
-	pack_refs($self, $dst) if delete $self->{-do_pack_refs};
 	eval { set_description($self) };
 	warn $@ if $@;
 	return if ($self->{-is_epoch} ||
@@ -770,6 +823,7 @@ EOM
 			last; # restart %$todo iteration
 		}
 	}
+	fgrp_fetch_all($self);
 	do_reap($self, 1);
 }
 
@@ -793,6 +847,7 @@ sub try_manifest {
 	my $uri = URI->new($self->{src});
 	my $lei = $self->{lei};
 	my $curl = $self->{curl} //= PublicInbox::LeiCurl->new($lei) or return;
+	$self->{-torsocks} //= $curl->torsocks($lei, $uri) or return;
 	my $path = $uri->path;
 	chop($path) eq '/' or die "BUG: $uri not canonicalized";
 	$uri->path($path . '/manifest.js.gz');
@@ -814,6 +869,7 @@ sub try_manifest {
 	return $lei->child_error(1, $multi) if !ref($multi);
 	my $v2 = delete $multi->{v2};
 	local $self->{todo} = {};
+	local $self->{fgrp_todo} = {}; # { objstore_dir => [fgrp, ...] }
 	if ($v2) {
 		for my $name (sort keys %$v2) {
 			my $epochs = delete $v2->{$name};

  parent reply	other threads:[~2022-11-28  5:32 UTC|newest]

Thread overview: 96+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-28  5:30 [PATCH 00/95] clone: multi-inbox/repo support Eric Wong
2022-11-28  5:30 ` [PATCH 01/95] clone: support multi-inbox clone Eric Wong
2022-11-28  5:30 ` [PATCH 02/95] clone: support --include and --exclude with multi-clone Eric Wong
2022-11-28  5:31 ` [PATCH 03/95] clone: parallelize v2 epoch clones Eric Wong
2022-11-28  5:31 ` [PATCH 04/95] lei_mirror: async config retrieval for v2 w/ manifest Eric Wong
2022-11-28  5:31 ` [PATCH 05/95] lei_mirror: rely on DESTROY to index v2 inbox Eric Wong
2022-11-28  5:31 ` [PATCH 06/95] lei_mirror: rely on global process reaper Eric Wong
2022-11-28  5:31 ` [PATCH 07/95] clone: support parallel v1 clones Eric Wong
2022-11-28  5:31 ` [PATCH 08/95] lei_mirror: default to single job by default Eric Wong
2022-11-28  5:31 ` [PATCH 09/95] lei_mirror: move directory creation to v2-only path Eric Wong
2022-11-28  5:31 ` [PATCH 10/95] lei_mirror: retrieve description text asynchronously, too Eric Wong
2022-11-28  5:31 ` [PATCH 11/95] switch inotify/kevent stuff to v5.12 Eric Wong
2022-11-28  5:31 ` [PATCH 12/95] manifest: update module blurb + v5.12 Eric Wong
2022-11-28  5:31 ` [PATCH 13/95] lei_mirror: simplify _get_txt_start callers Eric Wong
2022-11-28  5:31 ` [PATCH 14/95] lei_mirror: elide description retrieval for v1|coderepo Eric Wong
2022-11-28  5:31 ` [PATCH 15/95] lei_mirror: add a hint for skipped epoch permissions Eric Wong
2022-11-28  5:31 ` [PATCH 16/95] lei_mirror: consolidate clone process management Eric Wong
2022-11-28  5:31 ` [PATCH 17/95] lei_mirror: load File::Path unconditionally Eric Wong
2022-11-28  5:31 ` [PATCH 18/95] lei_mirror: load most modules up-front Eric Wong
2022-11-28  5:31 ` [PATCH 19/95] lei_mirror: set gitweb.owner from manifest Eric Wong
2022-11-28  5:31 ` [PATCH 20/95] clone: support --dry-run / -n flag Eric Wong
2022-11-28  5:31 ` [PATCH 21/95] lei_mirror: initialize placeholders with "head" from manifest Eric Wong
2022-11-28  5:31 ` [PATCH 22/95] lei_mirror: support {reference} for v1 manifest clones Eric Wong
2022-11-28  5:31 ` [PATCH 23/95] lei_mirror: reduce noise on interrupted clones Eric Wong
2022-11-28  5:31 ` [PATCH 24/95] clone: support --inbox-config option Eric Wong
2022-11-28  5:31 ` [PATCH 25/95] lei_mirror: retrieve v2 description properly Eric Wong
2022-11-28  5:31 ` [PATCH 26/95] lei_mirror: reduce scope of v2 lock Eric Wong
2022-11-28  5:31 ` [PATCH 27/95] lei_mirror: allow --epoch on mixed v1/v2 clones Eric Wong
2022-11-28  5:31 ` [PATCH 28/95] lei_mirror: fix infinite loop in dependency resolution Eric Wong
2022-11-28  5:31 ` [PATCH 29/95] lei_mirror: defend against infinite loops Eric Wong
2022-11-28  5:31 ` [PATCH 30/95] lei_mirror: do not fetch descriptions if using manifest Eric Wong
2022-11-28  5:31 ` [PATCH 31/95] lei_mirror: require PublicInbox::Lock at use Eric Wong
2022-11-28  5:31 ` [PATCH 32/95] lei_mirror: fix glob semantics to match end-of-path Eric Wong
2022-11-28  5:31 ` [PATCH 33/95] lei_mirror: differentiate -entv vs -ent Eric Wong
2022-11-28  5:31 ` [PATCH 34/95] lei_mirror: support manifest {references} for v2 epochs Eric Wong
2022-11-28  5:31 ` [PATCH 35/95] lei_mirror: simplify v2 code paths Eric Wong
2022-11-28  5:31 ` [PATCH 36/95] clone: support --inbox-version Eric Wong
2022-11-28  5:31 ` [PATCH 37/95] lei_mirror: require Perl v5.12+ Eric Wong
2022-11-28  5:31 ` [PATCH 38/95] lei_mirror: ensure curl exits 22 on HTTP 404 responses Eric Wong
2022-11-28  5:31 ` [PATCH 39/95] lei_mirror: cleanup File::Temp OO usage Eric Wong
2022-11-28  5:31 ` [PATCH 40/95] lei_mirror: add `index' target to generated Makefile Eric Wong
2022-11-28  5:31 ` [PATCH 41/95] lei_mirror: do not write Makefile for --inbox-config=never Eric Wong
2022-11-28  5:31 ` [PATCH 42/95] lei_mirror: hoist out dump_manifest sub Eric Wong
2022-11-28  5:31 ` [PATCH 43/95] lei_mirror: avoid convoluted lazy_cb usage Eric Wong
2022-11-28  5:31 ` [PATCH 44/95] lei_mirror: simplify clone_v2_prep Eric Wong
2022-11-28  5:31 ` [PATCH 45/95] lei_mirror: support --objstore and forkgroups Eric Wong
2022-11-28  5:31 ` [PATCH 46/95] lei_mirror: cleanup process reaping logic Eric Wong
2022-11-28  5:31 ` [PATCH 47/95] lei_mirror: ensure git <1.8.5 fallback can use torsocks Eric Wong
2022-11-28  5:31 ` [PATCH 48/95] clone: flesh out --objstore behavior and document Eric Wong
2022-11-28  5:31 ` [PATCH 49/95] lei_mirror: always pack refs for coderepos Eric Wong
2022-11-28  5:31 ` [PATCH 50/95] lei_mirror: set description for non-inboxes, too Eric Wong
2022-11-28  5:31 ` [PATCH 51/95] lei_mirror: force --no-tags when fetching forkgroups Eric Wong
2022-11-28  5:31 ` [PATCH 52/95] lei_mirror: preserve permissions of existing alternates file Eric Wong
2022-11-28  5:31 ` [PATCH 53/95] lei_mirror: do not show ref updates w/o --verbose Eric Wong
2022-11-28  5:31 ` [PATCH 54/95] lei_mirror: drop git <1.8.5 support Eric Wong
2022-11-28  5:31 ` [PATCH 55/95] lei_mirror: make basename more descriptive Eric Wong
2022-11-28  5:31 ` [PATCH 56/95] lei_mirror: fix --dry-run for forkgroups Eric Wong
2022-11-28  5:31 ` Eric Wong [this message]
2022-11-28  5:31 ` [PATCH 58/95] clone: move --dry-run handling to lei_mirror Eric Wong
2022-11-28  5:31 ` [PATCH 59/95] clone: drop unnecessary requires Eric Wong
2022-11-28  5:31 ` [PATCH 60/95] clone: use v5.12 Eric Wong
2022-11-28  5:31 ` [PATCH 61/95] clone: require `--objstore=' for default location Eric Wong
2022-11-28  5:31 ` [PATCH 62/95] lei_mirror: shorten remote names Eric Wong
2022-11-28  5:32 ` [PATCH 63/95] fetch: use v5.12 Eric Wong
2022-11-28  5:32 ` [PATCH 64/95] fetch: eliminate File::Temp->filename var Eric Wong
2022-11-28  5:32 ` [PATCH 65/95] lei_mirror: properly pack-refs in non-forkgroup repos Eric Wong
2022-11-28  5:32 ` [PATCH 66/95] lei_mirror: show child error error code Eric Wong
2022-11-28  5:32 ` [PATCH 67/95] on_destroy: support ->cancel callback Eric Wong
2022-11-28  5:32 ` [PATCH 68/95] lei_mirror: support resuming multi-repo clones Eric Wong
2022-11-28  5:32 ` [PATCH 69/95] lei_mirror: check fingerprints before fetching Eric Wong
2022-11-28  5:32 ` [PATCH 70/95] clone: support loading manifest.js.gz from destination Eric Wong
2022-11-28  5:32 ` [PATCH 71/95] lei_mirror: delay configuring forkgroups Eric Wong
2022-11-28  5:32 ` [PATCH 72/95] clone: canonicalize destination path from CLI Eric Wong
2022-11-28  5:32 ` [PATCH 73/95] clone|fetch: support passing --prune(-tags) to `git fetch' Eric Wong
2022-11-28  5:32 ` [PATCH 74/95] lei_mirror: avoid needless FD passing Eric Wong
2022-11-28  5:32 ` [PATCH 75/95] clone: support --keep-going/-k like make(1) Eric Wong
2022-11-28  5:32 ` [PATCH 76/95] lei_mirror: don't warn on missing manifest on initial clone Eric Wong
2022-11-28  5:32 ` [PATCH 77/95] lei_mirror: respect `./' and `../' prefixes for CLI args Eric Wong
2022-11-28  5:32 ` [PATCH 78/95] lei_mirror: --manifest= affects destination, too Eric Wong
2022-11-28  5:32 ` [PATCH 79/95] lei_mirror: update fingerprints when writing local manifest.js.gz Eric Wong
2022-11-28  5:32 ` [PATCH 80/95] lei_mirror: remove janky mirror.done stamp file Eric Wong
2022-11-28  5:32 ` [PATCH 81/95] lei_mirror: simplify most process spawning Eric Wong
2022-11-28  5:32 ` [PATCH 82/95] lei_mirror: run v1_done earlier on forkgroup done Eric Wong
2022-11-28  5:32 ` [PATCH 83/95] lei_mirror: simplify forkgroup-related subs Eric Wong
2022-11-28  5:32 ` [PATCH 84/95] lei_mirror: shorten scope mirror objects Eric Wong
2022-11-28  5:32 ` [PATCH 85/95] lei_mirror: set {head} from manifest Eric Wong
2022-11-28  5:32 ` [PATCH 86/95] lei_mirror: support {symlinks} " Eric Wong
2022-11-28  5:32 ` [PATCH 87/95] lei_mirror: eliminate circular references Eric Wong
2022-11-28  5:32 ` [PATCH 88/95] lei_mirror: use curl -z/--timecond if manifest exists Eric Wong
2022-11-28  5:32 ` [PATCH 89/95] lei_mirror: avoid redundant curl `-f' use Eric Wong
2022-11-28  5:32 ` [PATCH 90/95] lei_mirror: omit trailing slash for git remote.*.url Eric Wong
2022-11-28  5:32 ` [PATCH 91/95] lei_mirror: set info/web/last-modified from manifest Eric Wong
2022-11-28  5:32 ` [PATCH 92/95] lei_mirror: don't clobber inbox.config.example if it exists Eric Wong
2022-11-28  5:32 ` [PATCH 93/95] lei_mirror: break out of fgrp fetch iteration early Eric Wong
2022-11-28  5:32 ` [PATCH 94/95] clone: support --project-list= for cgit Eric Wong
2022-11-28  5:32 ` [PATCH 95/95] lei_mirror: handle forkgroup changes Eric Wong

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: https://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=20221128053232.291618-58-e@80x24.org \
    --to=e@80x24.org \
    --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).