Contents
advertise pre-generated bundles to seed clones
"clonebundles" is a server-side extension used to advertise the existence of pre-generated, externally hosted bundle files to clients that are cloning so that cloning can be faster, more reliable, and require less resources on the server. "pullbundles" is a related feature for sending pre-generated bundle files to clients as part of pull operations.
Cloning can be a CPU and I/O intensive operation on servers. Traditionally, the server, in response to a client's request to clone, dynamically generates a bundle containing the entire repository content and sends it to the client. There is no caching on the server and the server will have to redundantly generate the same outgoing bundle in response to each clone request. For servers with large repositories or with high clone volume, the load from clones can make scaling the server challenging and costly.
This extension provides server operators the ability to offload potentially expensive clone load to an external service. Pre-generated bundles also allow using more CPU intensive compression, reducing the effective bandwidth requirements.
Here's how clone bundles work:
Instead of the server generating full repository bundles for every clone request, it generates full bundles once and they are subsequently reused to bootstrap new clones. The server may still transfer data at clone time. However, this is only data that has been added/changed since the bundle was created. For large, established repositories, this can reduce server load for clones to less than 1% of original.
Here's how pullbundles work:
To work, this extension requires the following of server operators:
Strictly speaking, using a static file hosting server isn't required: a server operator could use a dynamic service for retrieving bundle data. However, static file hosting services are simple and scalable and should be sufficient for most needs.
Bundle files can be generated with the hg bundle command. Typically hg bundle --all is used to produce a bundle of the entire repository.
The bundlespec option stream (see hg help bundlespec) can be used to produce a special streaming clonebundle, typically using hg bundle --all --type="none-streamv2". These are bundle files that are extremely efficient to produce and consume (read: fast). However, they are larger than traditional bundle formats and require that clients support the exact set of repository data store formats in use by the repository that created them. Typically, a newer server can serve data that is compatible with older clients. However, streaming clone bundles don't have this guarantee. Server operators need to be aware that newer versions of Mercurial may produce streaming clone bundles incompatible with older Mercurial versions.
A server operator is responsible for creating a .hg/clonebundles.manifest file containing the list of available bundle files suitable for seeding clones. If this file does not exist, the repository will not advertise the existence of clone bundles when clients connect. For pull bundles, .hg/pullbundles.manifest is used.
The manifest file contains a newline (n) delimited list of entries.
Each line in this file defines an available bundle. Lines have the format:
<URL> [<key>=<value>[ <key>=<value>]]
That is, a URL followed by an optional, space-delimited list of key=value pairs describing additional properties of this bundle. Both keys and values are URI encoded.
For pull bundles, the URL is a path under the .hg directory of the repository.
Keys in UPPERCASE are reserved for use by Mercurial and are defined below. All non-uppercase keys can be used by site installations. An example use for custom properties is to use the datacenter attribute to define which data center a file is hosted in. Clients could then prefer a server in the data center closest to them.
The following reserved keys are currently defined:
A "bundle specification" string that describes the type of the bundle.
These are string values that are accepted by the "--type" argument of hg bundle.
The values are parsed in strict mode, which means they must be of the "<compression>-<type>" form. See mercurial.exchange.parsebundlespec() for more details.
hg debugbundle --spec can be used to print the bundle specification string for a bundle file. The output of this command can be used verbatim for the value of BUNDLESPEC (it is already escaped).
Clients will automatically filter out specifications that are unknown or unsupported so they won't attempt to download something that likely won't apply.
The actual value doesn't impact client behavior beyond filtering: clients will still sniff the bundle type from the header of downloaded files.
Use of this key is highly recommended, as it allows clients to easily skip unsupported bundles. If this key is not defined, an old client may attempt to apply a bundle that it is incapable of reading.
Whether Server Name Indication (SNI) is required to connect to the URL. SNI allows servers to use multiple certificates on the same IP. It is somewhat common in CDNs and other hosting providers. Older Python versions do not support SNI. Defining this attribute enables clients with older Python versions to filter this entry without experiencing an opaque SSL failure at connection time.
If this is defined, it is important to advertise a non-SNI fallback URL or clients running old Python releases may not be able to clone with the clonebundles facility.
Value should be "true".
Value specifies expected memory requirements to decode the payload. Values can have suffixes for common bytes sizes. e.g. "64MB".
This key is often used with zstd-compressed bundles using a high compression level / window size, which can require 100+ MB of memory to decode.
Manifests can contain multiple entries. Assuming metadata is defined, clients will filter entries from the manifest that they don't support. The remaining entries are optionally sorted by client preferences (ui.clonebundleprefers config option). The client then attempts to fetch the bundle at the first URL in the remaining list.
Errors when downloading a bundle will fail the entire clone operation: clients do not automatically fall back to a traditional clone. The reason for this is that if a server is using clone bundles, it is probably doing so because the feature is necessary to help it scale. In other words, there is an assumption that clone load will be offloaded to another service and that the Mercurial server isn't responsible for serving this clone load. If that other service experiences issues and clients start mass falling back to the original Mercurial server, the added clone load could overwhelm the server due to unexpected load and effectively take it offline. Not having clients automatically fall back to cloning from the original server mitigates this scenario.
Because there is no automatic Mercurial server fallback on failure of the bundle hosting service, it is important for server operators to view the bundle hosting service as an extension of the Mercurial server in terms of availability and service level agreements: if the bundle hosting service goes down, so does the ability for clients to clone. Note: clients will see a message informing them how to bypass the clone bundles facility when a failure occurs. So server operators should prepare for some people to follow these instructions when a failure occurs, thus driving more load to the original Mercurial server when the bundle hosting service fails.
It is possible to transmit clonebundles inline in case repositories are accessed over SSH. This avoids having to setup an external HTTPS server and results in the same access control as already present for the SSH setup.
Inline clonebundles should be placed into the .hg/bundle-cache directory. A clonebundle at .hg/bundle-cache/mybundle.bundle is referred to in the clonebundles.manifest file as peer-bundle-cache://mybundle.bundle.
It is possible to set Mercurial to automatically re-generate clone bundles when 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 decommissioned as newer ones replace them.
The extension can generate multiple variants of the clone bundle. Each different variant will be defined by the "bundle-spec" they use:
[clone-bundles] auto-generate.formats= zstd-v2, gzip-v2
See hg help bundlespec for details about available options.
By default, new bundles are generated when 5% of the repository contents or at least 1000 revisions are not contained in the cached bundles. This option can be controlled by the clone-bundles.trigger.below-bundled-ratio option (default 0.95) and the clone-bundles.trigger.revs option (default 1000):
[clone-bundles] trigger.below-bundled-ratio=0.95 trigger.revs=1000
This logic can be manually triggered using the admin::clone-bundles-refresh command, or automatically on each repository change if clone-bundles.auto-generate.on-change is set to yes:
[clone-bundles] auto-generate.on-change=yes auto-generate.formats= zstd-v2, gzip-v2
The simplest way to serve the generated bundle is through the Mercurial protocol. However it is not the most efficient as request will still be served by that main server. It is useful in case where authentication is complexe or when an efficient mirror system is already in use anyway. See the inline clonebundles section above for details about inline clonebundles
To automatically serve generated bundle through inline clonebundle, simply set the following option:
auto-generate.serve-inline=yes
Enabling this option disable the managed upload and serving explained below.
This is the most efficient way to serve automatically generated clone bundles, but requires some setup.
The generated bundles need to be made available to users through a "public" URL. This should be donne through clone-bundles.upload-command configuration. The value of this command should be a shell command. It will have access to the bundle file path through the $HGCB_BUNDLE_PATH variable. And the expected basename in the "public" URL is accessible at:
[clone-bundles] upload-command=sftp put $HGCB_BUNDLE_PATH sftp://bundles.host/clone-bundles/$HGCB_BUNDLE_BASENAME
If the file was already uploaded, the command must still succeed.
After upload, the file should be available at an url defined by clone-bundles.url-template.
[clone-bundles] url-template=https://bundles.host/cache/clone-bundles/{basename}
When new bundles are generated, the older ones are no longer necessary and can be removed from storage. This is done through the clone-bundles.delete-command configuration. The command is given the url of the artifact to delete through the $HGCB_BUNDLE_URL environment variable.
[clone-bundles] delete-command=sftp rm sftp://bundles.host/clone-bundles/$HGCB_BUNDLE_BASENAME
If the file was already deleted, the command must still succeed.
remove existing clone bundle caches:
hg admin::clone-bundles-clear
See hg help admin::clone-bundles-refresh for details on how to regenerate them.
This command will only affect bundles currently available, it will not affect bundles being asynchronously generated.
generate clone bundles according to the configuration:
hg admin::clone-bundles-refresh
This runs the logic for automatic generation, removing outdated bundles and generating new ones if necessary. See hg help -e clone-bundles for details about how to configure this feature.
Options:
--background | start bundle generation in the background |