clone-bundles: add an option to generate bundles in the background
authorPierre-Yves David <pierre-yves.david@octobus.net>
Tue, 14 Mar 2023 05:30:34 +0100
changeset 50472 3973b1dc3ee3
parent 50471 10318b88b1d6
child 50473 4077d6222cf1
clone-bundles: add an option to generate bundles in the background This is what the "on-change" version have been doing from the start, it seems useful to also offer this option in the command.
hgext/clonebundles.py
tests/test-clonebundles-autogen.t
--- a/hgext/clonebundles.py	Tue Mar 14 05:09:16 2023 +0100
+++ b/hgext/clonebundles.py	Tue Mar 14 05:30:34 2023 +0100
@@ -942,8 +942,23 @@
     repo.__class__ = autobundlesrepo
 
 
-@command(b'admin::clone-bundles-refresh', [], b'')
-def cmd_admin_clone_bundles_refresh(ui, repo: localrepo.localrepository):
+@command(
+    b'admin::clone-bundles-refresh',
+    [
+        (
+            b'',
+            b'background',
+            False,
+            _(b'start bundle generation in the background'),
+        ),
+    ],
+    b'',
+)
+def cmd_admin_clone_bundles_refresh(
+    ui,
+    repo: localrepo.localrepository,
+    background=False,
+):
     """generate clone bundles according to the configuration
 
     This runs the logic for automatic generation, removing outdated bundles and
@@ -955,28 +970,39 @@
     op_id = b"%d_acbr" % os.getpid()
     create, delete = auto_bundle_needed_actions(repo, bundles, op_id)
 
-    # we clean up outdated bundle before generating new one to keep the last
-    # two version of the bundle around for a while and avoid having to deal
-    # client that just got served a manifest.
-    for o in delete:
-        delete_bundle(repo, o)
-    update_bundle_list(repo, del_bundles=delete)
+    # if some bundles are scheduled for creation in the background, they will
+    # deal with garbage collection too, so no need to synchroniously do it.
+    #
+    # However if no bundles are scheduled for creation, we need to explicitly do
+    # it here.
+    if not (background and create):
+        # we clean up outdated bundles before generating new ones to keep the
+        # last two versions of the bundle around for a while and avoid having to
+        # deal with clients that just got served a manifest.
+        for o in delete:
+            delete_bundle(repo, o)
+        update_bundle_list(repo, del_bundles=delete)
 
     if create:
         fpath = repo.vfs.makedirs(b'tmp-bundles')
-    for requested_bundle in create:
-        if debug:
-            msg = b'clone-bundles: starting bundle generation: %s\n'
-            repo.ui.write(msg % requested_bundle.bundle_type)
-        fname = requested_bundle.suggested_filename
-        fpath = repo.vfs.join(b'tmp-bundles', fname)
-        generating_bundle = requested_bundle.generating(fpath)
-        update_bundle_list(repo, new_bundles=[generating_bundle])
-        requested_bundle.generate_bundle(repo, fpath)
-        result = upload_bundle(repo, generating_bundle)
-        update_bundle_list(repo, new_bundles=[result])
-        update_ondisk_manifest(repo)
-        cleanup_tmp_bundle(repo, generating_bundle)
+
+    if background:
+        for requested_bundle in create:
+            start_one_bundle(repo, requested_bundle)
+    else:
+        for requested_bundle in create:
+            if debug:
+                msg = b'clone-bundles: starting bundle generation: %s\n'
+                repo.ui.write(msg % requested_bundle.bundle_type)
+            fname = requested_bundle.suggested_filename
+            fpath = repo.vfs.join(b'tmp-bundles', fname)
+            generating_bundle = requested_bundle.generating(fpath)
+            update_bundle_list(repo, new_bundles=[generating_bundle])
+            requested_bundle.generate_bundle(repo, fpath)
+            result = upload_bundle(repo, generating_bundle)
+            update_bundle_list(repo, new_bundles=[result])
+            update_ondisk_manifest(repo)
+            cleanup_tmp_bundle(repo, generating_bundle)
 
 
 @command(b'admin::clone-bundles-clear', [], b'')
--- a/tests/test-clonebundles-autogen.t	Tue Mar 14 05:09:16 2023 +0100
+++ b/tests/test-clonebundles-autogen.t	Tue Mar 14 05:30:34 2023 +0100
@@ -335,3 +335,26 @@
   $ cat ../server/.hg/clonebundles.manifest
   $ ls -1 ../final-upload
   $ ls -1 ../server/.hg/tmp-bundles
+
+background generation
+---------------------
+
+generate bundle using background subprocess
+(since we are in devel mode, the command will still wait for the background
+process to end)
+
+  $ hg -R ../server/ admin::clone-bundles-refresh --background
+  11 changesets found
+  11 changesets found
+  clone-bundles: starting bundle generation: v1
+  clone-bundles: starting bundle generation: v2
+
+bundles should have been generated
+
+  $ cat ../server/.hg/clonebundles.manifest
+  file:/*/$TESTTMP/final-upload/full-v1-11_revs-4226b1cd5fda_tip-*_acbr.hg BUNDLESPEC=v1 REQUIRESNI=true (glob)
+  file:/*/$TESTTMP/final-upload/full-v2-11_revs-4226b1cd5fda_tip-*_acbr.hg BUNDLESPEC=v2 REQUIRESNI=true (glob)
+  $ ls -1 ../final-upload
+  full-v1-11_revs-4226b1cd5fda_tip-*_acbr.hg (glob)
+  full-v2-11_revs-4226b1cd5fda_tip-*_acbr.hg (glob)
+  $ ls -1 ../server/.hg/tmp-bundles