clone-bundles: only regenerate the clone bundle when cached ration is low
authorPierre-Yves David <pierre-yves.david@octobus.net>
Mon, 13 Mar 2023 20:01:42 +0100
changeset 50405 5b70b9f5a2f9
parent 50404 971dc2369b04
child 50406 d611805e7374
clone-bundles: only regenerate the clone bundle when cached ration is low See inline documentation for details.
hgext/clonebundles.py
tests/test-clonebundles-autogen.t
--- a/hgext/clonebundles.py	Tue Mar 24 03:25:33 2020 +0100
+++ b/hgext/clonebundles.py	Mon Mar 13 20:01:42 2023 +0100
@@ -206,7 +206,7 @@
 --------------------------------
 
 It is possible to set Mercurial to automatically re-generate clone bundles when
-new content is available.
+enough new content is available.
 
 Mercurial will take care of the process asynchronously. The defined list of
 bundle-type will be generated, uploaded, and advertised. Older bundles will get
@@ -223,6 +223,11 @@
 
 See `hg help bundlespec` for details about available options.
 
+Bundles are not generated on each push. By default new bundles are generated
+when 5% of the repository content is not contained in the cached bundles. This
+option can be controled by the `clone-bundles.trigger.below-bundled-ratio`
+option (default to 0.95).
+
 Bundles Upload and Serving:
 ...........................
 
@@ -311,7 +316,7 @@
 command = registrar.command(cmdtable)
 
 configitem(b'clone-bundles', b'auto-generate.formats', default=list)
-
+configitem(b'clone-bundles', b'trigger.below-bundled-ratio', default=0.95)
 
 configitem(b'clone-bundles', b'upload-command', default=None)
 
@@ -767,6 +772,9 @@
     delete_bundles = []
     repo = repo.filtered(b"immutable")
     targets = repo.ui.configlist(b'clone-bundles', b'auto-generate.formats')
+    ratio = float(
+        repo.ui.config(b'clone-bundles', b'trigger.below-bundled-ratio')
+    )
     revs = len(repo.changelog)
     generic_data = {
         'revs': revs,
@@ -776,14 +784,26 @@
         'op_id': op_id,
     }
     for t in targets:
-        data = generic_data.copy()
-        data['bundle_type'] = t
-        b = RequestedBundle(**data)
-        create_bundles.append(b)
+        if new_bundle_needed(repo, bundles, ratio, t, revs):
+            data = generic_data.copy()
+            data['bundle_type'] = t
+            b = RequestedBundle(**data)
+            create_bundles.append(b)
     delete_bundles.extend(find_outdated_bundles(repo, bundles))
     return create_bundles, delete_bundles
 
 
+def new_bundle_needed(repo, bundles, ratio, bundle_type, revs):
+    """consider the current cached content and trigger new bundles if needed"""
+    threshold = revs * ratio
+    for b in bundles:
+        if not b.valid_for(repo) or b.bundle_type != bundle_type:
+            continue
+        if b.revs > threshold:
+            return False
+    return True
+
+
 def start_one_bundle(repo, bundle):
     """start the generation of a single bundle file
 
--- a/tests/test-clonebundles-autogen.t	Tue Mar 24 03:25:33 2020 +0100
+++ b/tests/test-clonebundles-autogen.t	Mon Mar 13 20:01:42 2023 +0100
@@ -94,3 +94,29 @@
   full-v2-4_revs-6427147b985a_tip-*_txn.hg (glob)
   full-v2-6_revs-b1010e95ea00_tip-*_txn.hg (glob)
   $ ls -1 ../server/.hg/tmp-bundles
+
+Test conditions to get them generated
+=====================================
+
+Check ratio
+
+  $ cat >> ../server/.hg/hgrc << EOF
+  > [clone-bundles]
+  > trigger.below-bundled-ratio = 0.5
+  > EOF
+  $ touch far
+  $ hg -q commit -A -m 'add far'
+  $ hg push
+  pushing to $TESTTMP/server
+  searching for changes
+  adding changesets
+  adding manifests
+  adding file changes
+  added 1 changesets with 1 changes to 1 files
+  $ cat ../server/.hg/clonebundles.manifest
+  file:/*/$TESTTMP/final-upload/full-v2-6_revs-b1010e95ea00_tip-*_txn.hg BUNDLESPEC=v2 REQUIRESNI=true (glob)
+  $ ls -1 ../final-upload
+  full-v2-4_revs-6427147b985a_tip-*_txn.hg (glob)
+  full-v2-6_revs-b1010e95ea00_tip-*_txn.hg (glob)
+  $ ls -1 ../server/.hg/tmp-bundles
+