From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.1 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 1950320194 for ; Mon, 28 Nov 2022 05:32:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1669613562; bh=2+OGcGyifFU4hfn26I6VDAYo8B6Z+x+ZH/l1eGoLwHI=; h=From:To:Subject:Date:In-Reply-To:References:From; b=IR6VbIA5AUj8/GYUTQ0cfOloLfE7JAEvUKRn1Cc+QCkBsK5bv3GEmfdYvGQzJf7fY uBIDQ1wOZpAdq90BYVzjoYdnW1mtlXbVbLCS+iaEBbceksUavBRHhs2iT0hqJvCTjT BosqKbYQnFsGAwUE4/kWySBffdaEyJJbeD4vJqw8= From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 45/95] lei_mirror: support --objstore and forkgroups Date: Mon, 28 Nov 2022 05:31:42 +0000 Message-Id: <20221128053232.291618-46-e@80x24.org> In-Reply-To: <20221128053232.291618-1-e@80x24.org> References: <20221128053232.291618-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: The {forkgroup} directive of grokmirror 2.x manifest.js.gz can facilitate more space savings and improved pack performance with pack.islands. --- lib/PublicInbox/Fetch.pm | 15 +-- lib/PublicInbox/LeiMirror.pm | 200 +++++++++++++++++++++++++++++++++-- script/public-inbox-clone | 3 +- 3 files changed, 194 insertions(+), 24 deletions(-) diff --git a/lib/PublicInbox/Fetch.pm b/lib/PublicInbox/Fetch.pm index 06ed775f..3dbb0b55 100644 --- a/lib/PublicInbox/Fetch.pm +++ b/lib/PublicInbox/Fetch.pm @@ -15,19 +15,6 @@ use File::Temp (); sub new { bless {}, __PACKAGE__ } -sub fetch_args ($$) { - my ($lei, $opt) = @_; - my @cmd; # (git --git-dir=...) to be added by caller - $opt->{$_} = $lei->{$_} for (0..2); - # we support "-c $key=$val" for arbitrary git config options - # e.g.: git -c http.proxy=socks5h://127.0.0.1:9050 - push(@cmd, '-c', $_) for @{$lei->{opt}->{c} // []}; - push @cmd, 'fetch'; - push @cmd, '-q' if $lei->{opt}->{quiet}; - push @cmd, '-v' if $lei->{opt}->{verbose}; - @cmd; -} - sub remote_url ($$) { my ($lei, $dir) = @_; my $rn = $lei->{opt}->{'try-remote'} // [ 'origin', '_grokmirror' ]; @@ -205,7 +192,7 @@ EOM if (-d $d) { $fp2->[0] = get_fingerprint2($d) if $fp2; $cmd = [ @$torsocks, 'git', "--git-dir=$d", - fetch_args($lei, $opt) ]; + PublicInbox::LeiMirror::fetch_args($lei, $opt)]; } else { my $e_uri = $ibx_uri->clone; my ($epath) = ($d =~ m!(/git/[0-9]+\.git)\z!); diff --git a/lib/PublicInbox/LeiMirror.pm b/lib/PublicInbox/LeiMirror.pm index de4cdc22..799939b5 100644 --- a/lib/PublicInbox/LeiMirror.pm +++ b/lib/PublicInbox/LeiMirror.pm @@ -18,8 +18,10 @@ use PublicInbox::Config; use PublicInbox::Inbox; use PublicInbox::LeiCurl; use PublicInbox::OnDestroy; +use Digest::SHA qw(sha256_hex); our $LIVE; # pid => callback +my $update_ref_stdin = $ENV{GIT_CAN_UPDATE_REF_STDIN} // 1; sub _wq_done_wait { # dwaitpid callback (via wq_eof) my ($arg, $pid) = @_; @@ -249,7 +251,173 @@ sub start_clone { reap_live() while keys(%$LIVE) >= $jobs; $self->{lei}->qerr("# @$cmd"); return if $self->{dry_run}; - $LIVE->{spawn($cmd, undef, $opt)} = [ \&reap_clone, $self, $cmd, $fini ] + $LIVE->{spawn($cmd, undef, $opt)} = [ \&reap_cmd, $self, $cmd, $fini ] +} + +sub fetch_args ($$) { + my ($lei, $opt) = @_; + my @cmd; # (git --git-dir=...) to be added by caller + $opt->{$_} = $lei->{$_} for (0..2); + # we support "-c $key=$val" for arbitrary git config options + # e.g.: git -c http.proxy=socks5h://127.0.0.1:9050 + push(@cmd, '-c', $_) for @{$lei->{opt}->{c} // []}; + push @cmd, 'fetch'; + push @cmd, '-q' if $lei->{opt}->{quiet} || + ($lei->{opt}->{jobs} // 1) > 1; + push @cmd, '-v' if $lei->{opt}->{verbose}; + @cmd; +} + +sub fgrp_update_old ($) { # for git <1.8.5 + my ($fgrp) = @_; + my $cmd = [ 'git', "--git-dir=$fgrp->{cur_dst}", + fetch_args($fgrp->{lei}, my $opt = {}) ]; + $fgrp->{lei}->qerr("# @$cmd"); + $LIVE->{spawn($cmd, undef, $opt)} = [ \&reap_cmd, $fgrp, $cmd ]; + my $jobs = $fgrp->{lei}->{opt}->{jobs} // 1; + reap_live() while keys(%$LIVE) >= $jobs; +} + +sub upr { # feed `git update-ref --stdin -z' verbosely + my ($fgrp, $w, $op, $ref, $oid) = @_; + $fgrp->{lei}->qerr("# $op $ref $oid"); + print $w "$op $ref\0$oid\0" or die "print(w): $!"; +} + +sub fgrp_update { + my ($fgrp) = @_; + my $srcfh = delete $fgrp->{srcfh} or return; + my $dstfh = delete $fgrp->{dstfh} or return; + seek($srcfh, SEEK_SET, 0) or die "seek(src): $!"; + seek($dstfh, SEEK_SET, 0) or die "seek(dst): $!"; + my %src = map { chomp; split(/\0/) } (<$srcfh>); + close $srcfh; + my %dst = map { chomp; split(/\0/) } (<$dstfh>); + close $dstfh; + pipe(my ($r, $w)) or die "pipe: $!"; + my $cmd = [ 'git', "--git-dir=$fgrp->{cur_dst}", + qw(update-ref --stdin -z) ]; + $fgrp->{lei}->qerr("# @$cmd"); + my $opt = { 0 => $r, 1 => $fgrp->{lei}->{1}, 2 => $fgrp->{lei}->{2} }; + my $pid = spawn($cmd, undef, $opt); + close $r or die "close(r): $!"; + for my $ref (keys %dst) { + my $new = delete $src{$ref}; + my $old = $dst{$ref}; + if (defined $new) { + upr($fgrp, $w, 'update', $ref, $new) if $new ne $old; + } else { + upr($fgrp, $w, 'delete', $ref, $old); + } + } + while (my ($ref, $oid) = each %src) { + upr($fgrp, $w, 'create', $ref, $oid); + } + if (close($w)) { # git >= 1.8.5 + $LIVE->{$pid} = [ \&reap_cmd, $fgrp, $cmd ]; + my $jobs = $fgrp->{lei}->{opt}->{jobs} // 1; + reap_live() while keys(%$LIVE) >= $jobs; + } else { # git <1.8.5 w/o update-ref --stdin + warn "E: close(update-ref --stdin): $!\n"; + $update_ref_stdin = 0; + waitpid($pid, 0) // die "waitpid(update-ref --stdin): $!"; + fgrp_update_old($fgrp); + } +} + +sub fgrp_fetched { + my ($fgrp) = @_; + return if $fgrp->{dry_run} || !$LIVE; + my $rn = $fgrp->{-remote}; + my %opt = map { $_ => $fgrp->{lei}->{$_} } (0..2); + my $cmd = [ 'git', "--git-dir=$fgrp->{-osdir}", + qw(pack-refs --all --prune) ]; + $fgrp->{lei}->qerr("# @$cmd"); + $LIVE->{spawn($cmd, undef, \%opt)} = [ \&reap_cmd, $fgrp, $cmd ]; + my $jobs = $fgrp->{lei}->{opt}->{jobs} // 1; + reap_live() while keys(%$LIVE) >= $jobs; + + $update_ref_stdin or return fgrp_update_old($fgrp); + + 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/" ]; + 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 ]; + reap_live() while keys(%$LIVE) >= $jobs; + + my $dst = [ 'git', "--git-dir=$fgrp->{cur_dst}", 'for-each-ref', + '--format=%(refname)%00%(objectname)' ]; + 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 ]; + reap_live() while keys(%$LIVE) >= $jobs; +} + +sub fgrp_fetch { + my ($fgrp, $pfx, $fini) = @_; + my $cmd = [ @$pfx, 'git', "--git-dir=$fgrp->{-osdir}", + fetch_args($fgrp->{lei}, my $opt = {}), + $fgrp->{-remote} ]; + $fgrp->{-fini} = $fini; + my $jobs = $fgrp->{lei}->{opt}->{jobs} // 1; + reap_live() while keys(%$LIVE) >= $jobs; + $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 ]; +} + +# keep this idempotent for future use by public-inbox-fetch +sub forkgroup_prep { + my ($self, $uri) = @_; + $self->{-ent} // return; + my $os = $self->{-objstore} // return; + my $fg = $self->{-ent}->{forkgroup} // return; + my $dir = "$os/$fg.git"; + my @cmd = ('git', "--git-dir=$dir", 'config'); + my $opt = +{ map { $_ => $self->{lei}->{$_} } (0..2) }; + if (!-d $dir) { + PublicInbox::Import::init_bare($dir); + for ('repack.useDeltaIslands=true', + 'pack.island=refs/remotes/([^/]+)/') { + run_die([@cmd, split(/=/, $_, 2)], undef, $opt); + } + } + my $key = $self->{-key} // die 'BUG: no -key'; + my ($bn) = ($key =~ m{/([a-z0-9_,;=!\+\{\}\|][^/]*)(?:\.git)?\z}i); + my $rn = "$bn-".substr(sha256_hex($key), 0, 16); + for ("url=$uri", "fetch=+refs/*:refs/remotes/$rn/*") { + my @kv = split(/=/, $_, 2); + $kv[0] = "remote.$rn.$kv[0]"; + run_die([@cmd, @kv], undef, $opt); + } + if (!-d $self->{cur_dst}) { + my $alt = File::Spec->rel2abs("$dir/objects"); + PublicInbox::Import::init_bare($self->{cur_dst}); + my $o = "$self->{cur_dst}/objects"; + my $f = "$o/info/alternates"; + my $l = File::Spec->abs2rel($alt, File::Spec->rel2abs($o)); + open my $fh, '+>>', $f or die "open($f): $!"; + seek($fh, SEEK_SET, 0) or die "seek($f): $!"; + chomp(my @cur = <$fh>); + if (!grep(/\A\Q$l\E\z/, @cur)) { + say $fh $l or die "say($f): $!"; + } + close $fh or die "close($f): $!"; + @cmd = ('git', "--git-dir=$self->{cur_dst}", + qw(remote add --mirror=fetch origin), "$uri"); + my $pid = spawn(\@cmd, undef, $opt); + waitpid($pid, 0) // die "waitpid(@cmd): $!"; + die "E: @cmd: \$?=$?" if ($? && ($? >> 8) != 3); + } + bless { %$self, -osdir => $dir, -remote => $rn }, __PACKAGE__; } sub clone_v1 { @@ -263,10 +431,15 @@ sub clone_v1 { my $dst = $self->{cur_dst} // $self->{dst}; my $fini = PublicInbox::OnDestroy->new($$, \&v1_done, $self); my $cmd = [ @$pfx, clone_cmd($lei, my $opt = {}), "$uri", $dst ]; - my $ref = $self->{-ent} ? $self->{-ent}->{reference} : undef; - defined($ref) && -e "$self->{dst}$ref" and - push @$cmd, '--reference', "$self->{dst}$ref"; - start_clone($self, $cmd, $opt, $fini); + 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"; + } + } + $fgrp ? fgrp_fetch($fgrp, $pfx, $fini) : + start_clone($self, $cmd, $opt, $fini); if (!$self->{-is_epoch} && $lei->{opt}->{'inbox-config'} =~ /\A(?:always|v1)\z/s) { @@ -357,7 +530,7 @@ EOM } } -sub reap_clone { # async, called via SIGCHLD +sub reap_cmd { # async, called via SIGCHLD my ($self, $cmd) = @_; my $cerr = $?; $? = 0; # don't let it influence normal exit @@ -377,8 +550,12 @@ sub v1_done { # called via OnDestroy } my $o = "$dst/objects"; if (open(my $fh, '<', "$o/info/alternates")) { + my $base = File::Spec->rel2abs($o); chomp(my @l = <$fh>); - for (@l) { $_ = File::Spec->abs2rel($_, $o)."\n" } + for (@l) { + $_ = File::Spec->abs2rel($_, $base) if m!\A/!; + $_ .= "\n"; + } my $f = File::Temp->new(TEMPLATE => '.XXXX', DIR => "$o/info"); print $f @l; $f->flush or die "flush($f): $!"; @@ -417,7 +594,7 @@ sub reap_live { my $pid = waitpid(-1, 0) // die "waitpid(-1): $!"; if (my $x = delete $LIVE->{$pid}) { my $cb = shift @$x; - $cb->(@$x); + $cb->(@$x) if $cb; } else { warn "reaped unknown PID=$pid ($?)\n"; } @@ -454,7 +631,7 @@ failed to extract epoch number from $src } } if (!$want || $want->{$nr}) { - my $etask = bless { %$task }, __PACKAGE__; + my $etask = bless { %$task, -key => $key }, __PACKAGE__; $etask->{-ent} = $ent; # may have {reference} $etask->{cur_src} = $src; $etask->{cur_dst} = $edst; @@ -664,6 +841,7 @@ EOM die("BUG: no `$name' in manifest"); $task->{cur_src} = "$uri"; $task->{cur_dst} = $task->{dst}; + $task->{-key} = $name; if ($n > 1) { $task->{cur_dst} .= $name; $task->{cur_src} .= $name; @@ -702,6 +880,10 @@ sub do_mirror { # via wq_io_do or public-inbox-clone $ic =~ /\A(?:v1|v2|always|never)\z/s or die <<""; --inbox-config must be one of `always', `v2', `v1', or `never' + if (defined(my $os = $lei->{opt}->{objstore})) { + $os = 'objstore' if $os eq ''; # --objstore w/o args + $self->{-objstore} = "$self->{dst}/$os"; + } local $LIVE; my $iv = $lei->{opt}->{'inbox-version'} // return start_clone_url($self); diff --git a/script/public-inbox-clone b/script/public-inbox-clone index 2900f232..59f01b54 100755 --- a/script/public-inbox-clone +++ b/script/public-inbox-clone @@ -14,6 +14,7 @@ usage: public-inbox-clone INBOX_URL [DESTINATION] options: --epoch=RANGE range of v2 epochs to clone (e.g `2..5', `~0', `~1..') + --objstore [DIR] share storage for coderepos --torsocks VAL whether or not to wrap git and curl commands with torsocks (default: `auto') Must be one of: `auto', `no' or `yes' @@ -23,7 +24,7 @@ options: -C DIR chdir to specified directory EOF GetOptions($opt, qw(help|h quiet|q verbose|v+ C=s@ c=s@ include|I=s@ exclude=s@ - inbox-config=s inbox-version=i + inbox-config=s inbox-version=i objstore:s dry-run|n jobs|j=i no-torsocks torsocks=s epoch=s)) or die $help; if ($opt->{help}) { print $help; exit }; require PublicInbox::Admin; # loads Config