| 1 |
# shallowrepo.py - shallow repository that uses remote filelogs |
| 2 |
# |
| 3 |
# Copyright 2013 Facebook, Inc. |
| 4 |
# |
| 5 |
# This software may be used and distributed according to the terms of the |
| 6 |
# GNU General Public License version 2 or any later version. |
| 7 |
|
| 8 |
from __future__ import annotations |
| 9 |
|
| 10 |
import os |
| 11 |
|
| 12 |
from mercurial.i18n import _ |
| 13 |
from mercurial.node import hex, nullrev |
| 14 |
from mercurial import ( |
| 15 |
encoding, |
| 16 |
error, |
| 17 |
localrepo, |
| 18 |
match, |
| 19 |
scmutil, |
| 20 |
sparse, |
| 21 |
util, |
| 22 |
) |
| 23 |
from mercurial.utils import procutil |
| 24 |
from . import ( |
| 25 |
connectionpool, |
| 26 |
constants, |
| 27 |
contentstore, |
| 28 |
datapack, |
| 29 |
fileserverclient, |
| 30 |
historypack, |
| 31 |
metadatastore, |
| 32 |
remotefilectx, |
| 33 |
remotefilelog, |
| 34 |
shallowutil, |
| 35 |
) |
| 36 |
|
| 37 |
|
| 38 |
# These make*stores functions are global so that other extensions can replace |
| 39 |
# them. |
| 40 |
def makelocalstores(repo): |
| 41 |
"""In-repo stores, like .hg/store/data; can not be discarded.""" |
| 42 |
localpath = os.path.join(repo.svfs.vfs.base, b'data') |
| 43 |
if not os.path.exists(localpath): |
| 44 |
os.makedirs(localpath) |
| 45 |
|
| 46 |
# Instantiate local data stores |
| 47 |
localcontent = contentstore.remotefilelogcontentstore( |
| 48 |
repo, localpath, repo.name, shared=False |
| 49 |
) |
| 50 |
localmetadata = metadatastore.remotefilelogmetadatastore( |
| 51 |
repo, localpath, repo.name, shared=False |
| 52 |
) |
| 53 |
return localcontent, localmetadata |
| 54 |
|
| 55 |
|
| 56 |
def makecachestores(repo): |
| 57 |
"""Typically machine-wide, cache of remote data; can be discarded.""" |
| 58 |
# Instantiate shared cache stores |
| 59 |
cachepath = shallowutil.getcachepath(repo.ui) |
| 60 |
cachecontent = contentstore.remotefilelogcontentstore( |
| 61 |
repo, cachepath, repo.name, shared=True |
| 62 |
) |
| 63 |
cachemetadata = metadatastore.remotefilelogmetadatastore( |
| 64 |
repo, cachepath, repo.name, shared=True |
| 65 |
) |
| 66 |
|
| 67 |
repo.sharedstore = cachecontent |
| 68 |
repo.shareddatastores.append(cachecontent) |
| 69 |
repo.sharedhistorystores.append(cachemetadata) |
| 70 |
|
| 71 |
return cachecontent, cachemetadata |
| 72 |
|
| 73 |
|
| 74 |
def makeremotestores(repo, cachecontent, cachemetadata): |
| 75 |
"""These stores fetch data from a remote server.""" |
| 76 |
# Instantiate remote stores |
| 77 |
repo.fileservice = fileserverclient.fileserverclient(repo) |
| 78 |
remotecontent = contentstore.remotecontentstore( |
| 79 |
repo.ui, repo.fileservice, cachecontent |
| 80 |
) |
| 81 |
remotemetadata = metadatastore.remotemetadatastore( |
| 82 |
repo.ui, repo.fileservice, cachemetadata |
| 83 |
) |
| 84 |
return remotecontent, remotemetadata |
| 85 |
|
| 86 |
|
| 87 |
def makepackstores(repo): |
| 88 |
"""Packs are more efficient (to read from) cache stores.""" |
| 89 |
# Instantiate pack stores |
| 90 |
packpath = shallowutil.getcachepackpath(repo, constants.FILEPACK_CATEGORY) |
| 91 |
packcontentstore = datapack.datapackstore(repo.ui, packpath) |
| 92 |
packmetadatastore = historypack.historypackstore(repo.ui, packpath) |
| 93 |
|
| 94 |
repo.shareddatastores.append(packcontentstore) |
| 95 |
repo.sharedhistorystores.append(packmetadatastore) |
| 96 |
shallowutil.reportpackmetrics( |
| 97 |
repo.ui, b'filestore', packcontentstore, packmetadatastore |
| 98 |
) |
| 99 |
return packcontentstore, packmetadatastore |
| 100 |
|
| 101 |
|
| 102 |
def makeunionstores(repo): |
| 103 |
"""Union stores iterate the other stores and return the first result.""" |
| 104 |
repo.shareddatastores = [] |
| 105 |
repo.sharedhistorystores = [] |
| 106 |
|
| 107 |
packcontentstore, packmetadatastore = makepackstores(repo) |
| 108 |
cachecontent, cachemetadata = makecachestores(repo) |
| 109 |
localcontent, localmetadata = makelocalstores(repo) |
| 110 |
remotecontent, remotemetadata = makeremotestores( |
| 111 |
repo, cachecontent, cachemetadata |
| 112 |
) |
| 113 |
|
| 114 |
# Instantiate union stores |
| 115 |
repo.contentstore = contentstore.unioncontentstore( |
| 116 |
packcontentstore, |
| 117 |
cachecontent, |
| 118 |
localcontent, |
| 119 |
remotecontent, |
| 120 |
writestore=localcontent, |
| 121 |
) |
| 122 |
repo.metadatastore = metadatastore.unionmetadatastore( |
| 123 |
packmetadatastore, |
| 124 |
cachemetadata, |
| 125 |
localmetadata, |
| 126 |
remotemetadata, |
| 127 |
writestore=localmetadata, |
| 128 |
) |
| 129 |
|
| 130 |
fileservicedatawrite = cachecontent |
| 131 |
fileservicehistorywrite = cachemetadata |
| 132 |
repo.fileservice.setstore( |
| 133 |
repo.contentstore, |
| 134 |
repo.metadatastore, |
| 135 |
fileservicedatawrite, |
| 136 |
fileservicehistorywrite, |
| 137 |
) |
| 138 |
shallowutil.reportpackmetrics( |
| 139 |
repo.ui, b'filestore', packcontentstore, packmetadatastore |
| 140 |
) |
| 141 |
|
| 142 |
|
| 143 |
def wraprepo(repo): |
| 144 |
class shallowrepository(repo.__class__): |
| 145 |
@util.propertycache |
| 146 |
def name(self): |
| 147 |
return self.ui.config(b'remotefilelog', b'reponame') |
| 148 |
|
| 149 |
@util.propertycache |
| 150 |
def fallbackpath(self): |
| 151 |
path = repo.ui.config( |
| 152 |
b"remotefilelog", |
| 153 |
b"fallbackpath", |
| 154 |
repo.ui.config(b'paths', b'default'), |
| 155 |
) |
| 156 |
if not path: |
| 157 |
raise error.Abort( |
| 158 |
b"no remotefilelog server " |
| 159 |
b"configured - is your .hg/hgrc trusted?" |
| 160 |
) |
| 161 |
|
| 162 |
return path |
| 163 |
|
| 164 |
def maybesparsematch(self, *revs, **kwargs): |
| 165 |
""" |
| 166 |
A wrapper that allows the remotefilelog to invoke sparsematch() if |
| 167 |
this is a sparse repository, or returns None if this is not a |
| 168 |
sparse repository. |
| 169 |
""" |
| 170 |
if revs: |
| 171 |
ret = sparse.matcher(repo, revs=revs) |
| 172 |
else: |
| 173 |
ret = sparse.matcher(repo) |
| 174 |
|
| 175 |
if ret.always(): |
| 176 |
return None |
| 177 |
return ret |
| 178 |
|
| 179 |
def file(self, f, writable=False): |
| 180 |
if f[0] == b'/': |
| 181 |
f = f[1:] |
| 182 |
|
| 183 |
if self.shallowmatch(f): |
| 184 |
return remotefilelog.remotefilelog(self.svfs, f, self) |
| 185 |
else: |
| 186 |
return super().file(f, writable=writable) |
| 187 |
|
| 188 |
def filectx(self, path, *args, **kwargs): |
| 189 |
if self.shallowmatch(path): |
| 190 |
return remotefilectx.remotefilectx(self, path, *args, **kwargs) |
| 191 |
else: |
| 192 |
return super().filectx(path, *args, **kwargs) |
| 193 |
|
| 194 |
@localrepo.unfilteredmethod |
| 195 |
def commitctx(self, ctx, error=False, origctx=None): |
| 196 |
"""Add a new revision to current repository. |
| 197 |
Revision information is passed via the context argument. |
| 198 |
""" |
| 199 |
|
| 200 |
# some contexts already have manifest nodes, they don't need any |
| 201 |
# prefetching (for example if we're just editing a commit message |
| 202 |
# we can reuse manifest |
| 203 |
if not ctx.manifestnode(): |
| 204 |
# prefetch files that will likely be compared |
| 205 |
m1 = ctx.p1().manifest() |
| 206 |
files = [] |
| 207 |
for f in ctx.modified() + ctx.added(): |
| 208 |
fparent1 = m1.get(f, self.nullid) |
| 209 |
if fparent1 != self.nullid: |
| 210 |
files.append((f, hex(fparent1))) |
| 211 |
self.fileservice.prefetch(files) |
| 212 |
return super().commitctx(ctx, error=error, origctx=origctx) |
| 213 |
|
| 214 |
def backgroundprefetch( |
| 215 |
self, revs, base=None, repack=False, pats=None, opts=None |
| 216 |
): |
| 217 |
"""Runs prefetch in background with optional repack""" |
| 218 |
cmd = [procutil.hgexecutable(), b'-R', repo.origroot, b'prefetch'] |
| 219 |
if repack: |
| 220 |
cmd.append(b'--repack') |
| 221 |
if revs: |
| 222 |
cmd += [b'-r', revs] |
| 223 |
# We know this command will find a binary, so don't block |
| 224 |
# on it starting. |
| 225 |
kwargs = {} |
| 226 |
if repo.ui.configbool(b'devel', b'remotefilelog.bg-wait'): |
| 227 |
kwargs['record_wait'] = repo.ui.atexit |
| 228 |
|
| 229 |
procutil.runbgcommand( |
| 230 |
cmd, encoding.environ, ensurestart=False, **kwargs |
| 231 |
) |
| 232 |
|
| 233 |
def prefetch(self, revs, base=None, pats=None, opts=None): |
| 234 |
"""Prefetches all the necessary file revisions for the given revs |
| 235 |
Optionally runs repack in background |
| 236 |
""" |
| 237 |
with repo._lock( |
| 238 |
repo.svfs, |
| 239 |
b'prefetchlock', |
| 240 |
True, |
| 241 |
None, |
| 242 |
None, |
| 243 |
_(b'prefetching in %s') % repo.origroot, |
| 244 |
): |
| 245 |
self._prefetch(revs, base, pats, opts) |
| 246 |
|
| 247 |
def _prefetch(self, revs, base=None, pats=None, opts=None): |
| 248 |
fallbackpath = self.fallbackpath |
| 249 |
if fallbackpath: |
| 250 |
# If we know a rev is on the server, we should fetch the server |
| 251 |
# version of those files, since our local file versions might |
| 252 |
# become obsolete if the local commits are stripped. |
| 253 |
localrevs = repo.revs(b'outgoing(%s)', fallbackpath) |
| 254 |
if base is not None and base != nullrev: |
| 255 |
serverbase = list( |
| 256 |
repo.revs( |
| 257 |
b'first(reverse(::%s) - %ld)', base, localrevs |
| 258 |
) |
| 259 |
) |
| 260 |
if serverbase: |
| 261 |
base = serverbase[0] |
| 262 |
else: |
| 263 |
localrevs = repo |
| 264 |
|
| 265 |
mfl = repo.manifestlog |
| 266 |
mfrevlog = mfl.getstorage(b'') |
| 267 |
if base is not None: |
| 268 |
mfdict = mfl[repo[base].manifestnode()].read() |
| 269 |
skip = set(mfdict.items()) |
| 270 |
else: |
| 271 |
skip = set() |
| 272 |
|
| 273 |
# Copy the skip set to start large and avoid constant resizing, |
| 274 |
# and since it's likely to be very similar to the prefetch set. |
| 275 |
files = skip.copy() |
| 276 |
serverfiles = skip.copy() |
| 277 |
visited = set() |
| 278 |
visited.add(nullrev) |
| 279 |
revcount = len(revs) |
| 280 |
progress = self.ui.makeprogress(_(b'prefetching'), total=revcount) |
| 281 |
progress.update(0) |
| 282 |
for rev in sorted(revs): |
| 283 |
ctx = repo[rev] |
| 284 |
if pats: |
| 285 |
m = scmutil.match(ctx, pats, opts) |
| 286 |
sparsematch = repo.maybesparsematch(rev) |
| 287 |
|
| 288 |
mfnode = ctx.manifestnode() |
| 289 |
mfrev = mfrevlog.rev(mfnode) |
| 290 |
|
| 291 |
# Decompressing manifests is expensive. |
| 292 |
# When possible, only read the deltas. |
| 293 |
mfdict = mfl[mfnode].read_any_fast_delta(visited)[1] |
| 294 |
|
| 295 |
diff = mfdict.items() |
| 296 |
if pats: |
| 297 |
diff = (pf for pf in diff if m(pf[0])) |
| 298 |
if sparsematch: |
| 299 |
diff = (pf for pf in diff if sparsematch(pf[0])) |
| 300 |
if rev not in localrevs: |
| 301 |
serverfiles.update(diff) |
| 302 |
else: |
| 303 |
files.update(diff) |
| 304 |
|
| 305 |
visited.add(mfrev) |
| 306 |
progress.increment() |
| 307 |
|
| 308 |
files.difference_update(skip) |
| 309 |
serverfiles.difference_update(skip) |
| 310 |
progress.complete() |
| 311 |
|
| 312 |
# Fetch files known to be on the server |
| 313 |
if serverfiles: |
| 314 |
results = [(path, hex(fnode)) for (path, fnode) in serverfiles] |
| 315 |
repo.fileservice.prefetch(results, force=True) |
| 316 |
|
| 317 |
# Fetch files that may or may not be on the server |
| 318 |
if files: |
| 319 |
results = [(path, hex(fnode)) for (path, fnode) in files] |
| 320 |
repo.fileservice.prefetch(results) |
| 321 |
|
| 322 |
def close(self): |
| 323 |
super().close() |
| 324 |
self.connectionpool.close() |
| 325 |
|
| 326 |
repo.__class__ = shallowrepository |
| 327 |
|
| 328 |
repo.shallowmatch = match.always() |
| 329 |
|
| 330 |
makeunionstores(repo) |
| 331 |
|
| 332 |
repo.includepattern = repo.ui.configlist( |
| 333 |
b"remotefilelog", b"includepattern", None |
| 334 |
) |
| 335 |
repo.excludepattern = repo.ui.configlist( |
| 336 |
b"remotefilelog", b"excludepattern", None |
| 337 |
) |
| 338 |
if not hasattr(repo, 'connectionpool'): |
| 339 |
repo.connectionpool = connectionpool.connectionpool(repo) |
| 340 |
|
| 341 |
if repo.includepattern or repo.excludepattern: |
| 342 |
repo.shallowmatch = match.match( |
| 343 |
repo.root, b'', None, repo.includepattern, repo.excludepattern |
| 344 |
) |