@matheusbsbs / matheus-bagliano / commits / 28cb5e66dd
Fork 0

maintenance: add prefetch task

When working with very large repositories, an incremental 'git fetch' command can download a large amount of data. If there are many other users pushing to a common repo, then this data can rival the initial pack-file size of a 'git clone' of a medium-size repo. Users may want to keep the data on their local repos as close as possible to the data on the remote repos by fetching periodically in the background. This can break up a large daily fetch into several smaller hourly fetches. The task is called "prefetch" because it is work done in advance of a foreground fetch to make that 'git fetch' command much faster. However, if we simply ran 'git fetch <remote>' in the background, then the user running a foreground 'git fetch <remote>' would lose some important feedback when a new branch appears or an existing branch updates. This is especially true if a remote branch is force-updated and this isn't noticed by the user because it occurred in the background. Further, the functionality of 'git push --force-with-lease' becomes suspect. When running 'git fetch <remote> <options>' in the background, use the following options for careful updating: 1. --no-tags prevents getting a new tag when a user wants to see the new tags appear in their foreground fetches. 2. --refmap= removes the configured refspec which usually updates refs/remotes/<remote>/* with the refs advertised by the remote. While this looks confusing, this was documented and tested by b40a50264ac (fetch: document and test --refmap="", 2020-01-21), including this sentence in the documentation: Providing an empty `<refspec>` to the `--refmap` option causes Git to ignore the configured refspecs and rely entirely on the refspecs supplied as command-line arguments. 3. By adding a new refspec "+refs/heads/*:refs/prefetch/<remote>/*" we can ensure that we actually load the new values somewhere in our refspace while not updating refs/heads or refs/remotes. By storing these refs here, the commit-graph job will update the commit-graph with the commits from these hidden refs. 4. --prune will delete the refs/prefetch/<remote> refs that no longer appear on the remote. 5. --no-write-fetch-head prevents updating FETCH_HEAD. We've been using this step as a critical background job in Scalar [1] (and VFS for Git). This solved a pain point that was showing up in user reports: fetching was a pain! Users do not like waiting to download the data that was created while they were away from their machines. After implementing background fetch, the foreground fetch commands sped up significantly because they mostly just update refs and download a small amount of new data. The effect is especially dramatic when paried with --no-show-forced-udpates (through fetch.showForcedUpdates=false). [1] https://github.com/microsoft/scalar/blob/master/Scalar.Common/Maintenance/FetchStep.cs Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Sep 25, 2020 at 12:33 UTC 28cb5e66ddaed47aa8789fa326fec0339831b80b
3 files changed +92
Documentation/git-maintenance.txt
+15
@@ -47,6 +47,21 @@ commit-graph::
47 `commit-graph-chain` file. They will be deleted by a later run based
48 on the expiration delay.
49
50 +prefetch::
51 + The `prefetch` task updates the object directory with the latest
52 + objects from all registered remotes. For each remote, a `git fetch`
53 + command is run. The refmap is custom to avoid updating local or remote
54 + branches (those in `refs/heads` or `refs/remotes`). Instead, the
55 + remote refs are stored in `refs/prefetch/<remote>/`. Also, tags are
56 + not updated.
57 ++
58 +This is done to avoid disrupting the remote-tracking branches. The end users
59 +expect these refs to stay unmoved unless they initiate a fetch. With prefetch
60 +task, however, the objects necessary to complete a later real fetch would
61 +already be obtained, so the real fetch would go faster. In the ideal case,
62 +it will just become an update to bunch of remote-tracking branches without
63 +any object transfer.
64 +
65 gc::
66 Clean up unnecessary files and optimize the local repository. "GC"
67 stands for "garbage collection," but this task performs many
builtin/gc.c
+51
@@ -29,6 +29,7 @@
29 #include "tree.h"
30 #include "promisor-remote.h"
31 #include "refs.h"
32 +#include "remote.h"
33
34 #define FAILED_RUN "failed to run %s"
35
@@ -816,6 +817,51 @@ static int maintenance_task_commit_graph(struct maintenance_run_opts *opts)
817 return 0;
818 }
819
820 +static int fetch_remote(const char *remote, struct maintenance_run_opts *opts)
821 +{
822 + struct child_process child = CHILD_PROCESS_INIT;
823 +
824 + child.git_cmd = 1;
825 + strvec_pushl(&child.args, "fetch", remote, "--prune", "--no-tags",
826 + "--no-write-fetch-head", "--recurse-submodules=no",
827 + "--refmap=", NULL);
828 +
829 + if (opts->quiet)
830 + strvec_push(&child.args, "--quiet");
831 +
832 + strvec_pushf(&child.args, "+refs/heads/*:refs/prefetch/%s/*", remote);
833 +
834 + return !!run_command(&child);
835 +}
836 +
837 +static int append_remote(struct remote *remote, void *cbdata)
838 +{
839 + struct string_list *remotes = (struct string_list *)cbdata;
840 +
841 + string_list_append(remotes, remote->name);
842 + return 0;
843 +}
844 +
845 +static int maintenance_task_prefetch(struct maintenance_run_opts *opts)
846 +{
847 + int result = 0;
848 + struct string_list_item *item;
849 + struct string_list remotes = STRING_LIST_INIT_DUP;
850 +
851 + if (for_each_remote(append_remote, &remotes)) {
852 + error(_("failed to fill remotes"));
853 + result = 1;
854 + goto cleanup;
855 + }
856 +
857 + for_each_string_list_item(item, &remotes)
858 + result |= fetch_remote(item->string, opts);
859 +
860 +cleanup:
861 + string_list_clear(&remotes, 0);
862 + return result;
863 +}
864 +
865 static int maintenance_task_gc(struct maintenance_run_opts *opts)
866 {
867 struct child_process child = CHILD_PROCESS_INIT;
@@ -854,6 +900,7 @@ struct maintenance_task {
900 };
901
902 enum maintenance_task_label {
903 + TASK_PREFETCH,
904 TASK_GC,
905 TASK_COMMIT_GRAPH,
906
@@ -862,6 +909,10 @@ enum maintenance_task_label {
909 };
910
911 static struct maintenance_task tasks[] = {
912 + [TASK_PREFETCH] = {
913 + "prefetch",
914 + maintenance_task_prefetch,
915 + },
916 [TASK_GC] = {
917 "gc",
918 maintenance_task_gc,
t/t7900-maintenance.sh
+26
@@ -62,4 +62,30 @@ test_expect_success 'run --task duplicate' '
62 test_i18ngrep "cannot be selected multiple times" err
63 '
64
65 +test_expect_success 'run --task=prefetch with no remotes' '
66 + git maintenance run --task=prefetch 2>err &&
67 + test_must_be_empty err
68 +'
69 +
70 +test_expect_success 'prefetch multiple remotes' '
71 + git clone . clone1 &&
72 + git clone . clone2 &&
73 + git remote add remote1 "file://$(pwd)/clone1" &&
74 + git remote add remote2 "file://$(pwd)/clone2" &&
75 + git -C clone1 switch -c one &&
76 + git -C clone2 switch -c two &&
77 + test_commit -C clone1 one &&
78 + test_commit -C clone2 two &&
79 + GIT_TRACE2_EVENT="$(pwd)/run-prefetch.txt" git maintenance run --task=prefetch 2>/dev/null &&
80 + fetchargs="--prune --no-tags --no-write-fetch-head --recurse-submodules=no --refmap= --quiet" &&
81 + test_subcommand git fetch remote1 $fetchargs +refs/heads/\\*:refs/prefetch/remote1/\\* <run-prefetch.txt &&
82 + test_subcommand git fetch remote2 $fetchargs +refs/heads/\\*:refs/prefetch/remote2/\\* <run-prefetch.txt &&
83 + test_path_is_missing .git/refs/remotes &&
84 + git log prefetch/remote1/one &&
85 + git log prefetch/remote2/two &&
86 + git fetch --all &&
87 + test_cmp_rev refs/remotes/remote1/one refs/prefetch/remote1/one &&
88 + test_cmp_rev refs/remotes/remote2/two refs/prefetch/remote2/two
89 +'
90 +
91 test_done