[PATCH] branchcache: fetch source branchcache during clone (issue3378)

Tomasz Kleczek tkleczek at fb.com
Tue Oct 9 16:23:37 CDT 2012


# HG changeset patch
# User Tomasz Kleczek <tomasz.kleczek at fb.com>
# Date 1349295593 25200
# Node ID dcea31df8d40e9ed570d681571069ee5cacf67d9
# Parent  6d7db5794e8cad7da042b6ae6238116c6e59a4d2
branchcache: fetch source branchcache during clone (issue3378)

Recomputing branch cache on clone may be expensive,
therefore if possible we fetch it along with the data.

- If the clone is performed by copying, we just copy branchcache file.
- If we localrepo.clone and streaming then we follow the procedure:
  1. Fetch branchmap from the remote
  2. Fetch the actual data.
  3. Find the latest rev within branch heads (tip at the time of
     branchmap fetch)
  4. Update the cache for the revs in [remotetip+1, tip]

This way we ensure that the branchcache is correct even in case
of races with commits.

diff --git a/mercurial/hg.py b/mercurial/hg.py
--- a/mercurial/hg.py
+++ b/mercurial/hg.py
@@ -336,6 +336,16 @@
 
             destlock = copystore(ui, srcrepo, destpath)
 
+            # Recomputing branch cache might be slow on big repos,
+            # so just copy it
+            dstcachedir = os.path.join(destpath, 'cache')
+            srcbranchcache = srcrepo.sjoin('cache/branchheads')
+            dstbranchcache = os.path.join(dstcachedir, 'branchheads')
+            if os.path.exists(srcbranchcache):
+                if not os.path.exists(dstcachedir):
+                    os.mkdir(dstcachedir)
+                util.copyfile(srcbranchcache, dstbranchcache)
+
             # we need to re-init the repo after manually copying the data
             # into it
             destpeer = peer(ui, peeropts, dest)
diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py
--- a/mercurial/localrepo.py
+++ b/mercurial/localrepo.py
@@ -2461,6 +2461,12 @@
     def stream_in(self, remote, requirements):
         lock = self.lock()
         try:
+            # Save remote branchmap. We will use it later
+            # to speed up branchcache creation
+            rbranchmap = None
+            if remote.capable("branchmap"):
+                rbranchmap = remote.branchmap()
+
             fp = remote.stream_out()
             l = fp.readline()
             try:
@@ -2521,6 +2527,17 @@
             self._applyrequirements(requirements)
             self._writerequirements()
 
+            if rbranchmap:
+                rbheads = []
+                for bheads in rbranchmap.itervalues():
+                    rbheads.extend(bheads)
+
+                self.branchcache = rbranchmap
+                if rbheads:
+                    rtiprev = max((int(self.changelog.rev(node))
+                            for node in rbheads))
+                    self._writebranchcache(self.branchcache,
+                            self[rtiprev].node(), rtiprev)
             self.invalidate()
             return len(self.heads()) + 1
         finally:
diff --git a/tests/test-http-proxy.t b/tests/test-http-proxy.t
--- a/tests/test-http-proxy.t
+++ b/tests/test-http-proxy.t
@@ -99,6 +99,7 @@
   1 files updated, 0 files merged, 0 files removed, 0 files unresolved
   $ cat proxy.log
   * - - [*] "GET http://localhost:$HGPORT/?cmd=capabilities HTTP/1.1" - - (glob)
+  * - - [*] "GET http://localhost:$HGPORT/?cmd=branchmap HTTP/1.1" - - (glob)
   * - - [*] "GET http://localhost:$HGPORT/?cmd=stream_out HTTP/1.1" - - (glob)
   * - - [*] "GET http://localhost:$HGPORT/?cmd=listkeys HTTP/1.1" - - x-hgarg-1:namespace=bookmarks (glob)
   * - - [*] "GET http://localhost:$HGPORT/?cmd=capabilities HTTP/1.1" - - (glob)


More information about the Mercurial-devel mailing list