| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import os |
| 4 |
import time |
| 5 |
|
| 6 |
from mercurial.i18n import _ |
| 7 |
from mercurial.node import short |
| 8 |
from mercurial import ( |
| 9 |
encoding, |
| 10 |
error, |
| 11 |
lock as lockmod, |
| 12 |
mdiff, |
| 13 |
policy, |
| 14 |
scmutil, |
| 15 |
util, |
| 16 |
vfs, |
| 17 |
) |
| 18 |
from mercurial.utils import procutil |
| 19 |
from . import ( |
| 20 |
constants, |
| 21 |
contentstore, |
| 22 |
datapack, |
| 23 |
historypack, |
| 24 |
metadatastore, |
| 25 |
shallowutil, |
| 26 |
) |
| 27 |
|
| 28 |
osutil = policy.importmod('osutil') |
| 29 |
|
| 30 |
|
| 31 |
class RepackAlreadyRunning(error.Abort): |
| 32 |
pass |
| 33 |
|
| 34 |
|
| 35 |
def backgroundrepack(repo, incremental=True, packsonly=False): |
| 36 |
cmd = [procutil.hgexecutable(), b'-R', repo.origroot, b'repack'] |
| 37 |
msg = _(b"(running background repack)\n") |
| 38 |
if incremental: |
| 39 |
cmd.append(b'--incremental') |
| 40 |
msg = _(b"(running background incremental repack)\n") |
| 41 |
if packsonly: |
| 42 |
cmd.append(b'--packsonly') |
| 43 |
repo.ui.warn(msg) |
| 44 |
# We know this command will find a binary, so don't block on it starting. |
| 45 |
kwargs = {} |
| 46 |
if repo.ui.configbool(b'devel', b'remotefilelog.bg-wait'): |
| 47 |
kwargs['record_wait'] = repo.ui.atexit |
| 48 |
|
| 49 |
procutil.runbgcommand(cmd, encoding.environ, ensurestart=False, **kwargs) |
| 50 |
|
| 51 |
|
| 52 |
def fullrepack(repo, options=None): |
| 53 |
"""If ``packsonly`` is True, stores creating only loose objects are skipped.""" |
| 54 |
if hasattr(repo, 'shareddatastores'): |
| 55 |
datasource = contentstore.unioncontentstore(*repo.shareddatastores) |
| 56 |
historysource = metadatastore.unionmetadatastore( |
| 57 |
*repo.sharedhistorystores, allowincomplete=True |
| 58 |
) |
| 59 |
|
| 60 |
packpath = shallowutil.getcachepackpath( |
| 61 |
repo, constants.FILEPACK_CATEGORY |
| 62 |
) |
| 63 |
_runrepack( |
| 64 |
repo, |
| 65 |
datasource, |
| 66 |
historysource, |
| 67 |
packpath, |
| 68 |
constants.FILEPACK_CATEGORY, |
| 69 |
options=options, |
| 70 |
) |
| 71 |
|
| 72 |
if hasattr(repo.manifestlog, 'datastore'): |
| 73 |
localdata, shareddata = _getmanifeststores(repo) |
| 74 |
lpackpath, ldstores, lhstores = localdata |
| 75 |
spackpath, sdstores, shstores = shareddata |
| 76 |
|
| 77 |
# Repack the shared manifest store |
| 78 |
datasource = contentstore.unioncontentstore(*sdstores) |
| 79 |
historysource = metadatastore.unionmetadatastore( |
| 80 |
*shstores, allowincomplete=True |
| 81 |
) |
| 82 |
_runrepack( |
| 83 |
repo, |
| 84 |
datasource, |
| 85 |
historysource, |
| 86 |
spackpath, |
| 87 |
constants.TREEPACK_CATEGORY, |
| 88 |
options=options, |
| 89 |
) |
| 90 |
|
| 91 |
# Repack the local manifest store |
| 92 |
datasource = contentstore.unioncontentstore( |
| 93 |
*ldstores, allowincomplete=True |
| 94 |
) |
| 95 |
historysource = metadatastore.unionmetadatastore( |
| 96 |
*lhstores, allowincomplete=True |
| 97 |
) |
| 98 |
_runrepack( |
| 99 |
repo, |
| 100 |
datasource, |
| 101 |
historysource, |
| 102 |
lpackpath, |
| 103 |
constants.TREEPACK_CATEGORY, |
| 104 |
options=options, |
| 105 |
) |
| 106 |
|
| 107 |
|
| 108 |
def incrementalrepack(repo, options=None): |
| 109 |
"""This repacks the repo by looking at the distribution of pack files in the |
| 110 |
repo and performing the most minimal repack to keep the repo in good shape. |
| 111 |
""" |
| 112 |
if hasattr(repo, 'shareddatastores'): |
| 113 |
packpath = shallowutil.getcachepackpath( |
| 114 |
repo, constants.FILEPACK_CATEGORY |
| 115 |
) |
| 116 |
_incrementalrepack( |
| 117 |
repo, |
| 118 |
repo.shareddatastores, |
| 119 |
repo.sharedhistorystores, |
| 120 |
packpath, |
| 121 |
constants.FILEPACK_CATEGORY, |
| 122 |
options=options, |
| 123 |
) |
| 124 |
|
| 125 |
if hasattr(repo.manifestlog, 'datastore'): |
| 126 |
localdata, shareddata = _getmanifeststores(repo) |
| 127 |
lpackpath, ldstores, lhstores = localdata |
| 128 |
spackpath, sdstores, shstores = shareddata |
| 129 |
|
| 130 |
# Repack the shared manifest store |
| 131 |
_incrementalrepack( |
| 132 |
repo, |
| 133 |
sdstores, |
| 134 |
shstores, |
| 135 |
spackpath, |
| 136 |
constants.TREEPACK_CATEGORY, |
| 137 |
options=options, |
| 138 |
) |
| 139 |
|
| 140 |
# Repack the local manifest store |
| 141 |
_incrementalrepack( |
| 142 |
repo, |
| 143 |
ldstores, |
| 144 |
lhstores, |
| 145 |
lpackpath, |
| 146 |
constants.TREEPACK_CATEGORY, |
| 147 |
allowincompletedata=True, |
| 148 |
options=options, |
| 149 |
) |
| 150 |
|
| 151 |
|
| 152 |
def _getmanifeststores(repo): |
| 153 |
shareddatastores = repo.manifestlog.shareddatastores |
| 154 |
localdatastores = repo.manifestlog.localdatastores |
| 155 |
sharedhistorystores = repo.manifestlog.sharedhistorystores |
| 156 |
localhistorystores = repo.manifestlog.localhistorystores |
| 157 |
|
| 158 |
sharedpackpath = shallowutil.getcachepackpath( |
| 159 |
repo, constants.TREEPACK_CATEGORY |
| 160 |
) |
| 161 |
localpackpath = shallowutil.getlocalpackpath( |
| 162 |
repo.svfs.vfs.base, constants.TREEPACK_CATEGORY |
| 163 |
) |
| 164 |
|
| 165 |
return ( |
| 166 |
(localpackpath, localdatastores, localhistorystores), |
| 167 |
(sharedpackpath, shareddatastores, sharedhistorystores), |
| 168 |
) |
| 169 |
|
| 170 |
|
| 171 |
def _topacks(packpath, files, constructor): |
| 172 |
paths = list(os.path.join(packpath, p) for p in files) |
| 173 |
packs = list(constructor(p) for p in paths) |
| 174 |
return packs |
| 175 |
|
| 176 |
|
| 177 |
def _deletebigpacks(repo, folder, files): |
| 178 |
"""Deletes packfiles that are bigger than ``packs.maxpacksize``. |
| 179 |
|
| 180 |
Returns ``files` with the removed files omitted.""" |
| 181 |
maxsize = repo.ui.configbytes(b"packs", b"maxpacksize") |
| 182 |
if maxsize <= 0: |
| 183 |
return files |
| 184 |
|
| 185 |
# This only considers datapacks today, but we could broaden it to include |
| 186 |
# historypacks. |
| 187 |
VALIDEXTS = [b".datapack", b".dataidx"] |
| 188 |
|
| 189 |
# Either an oversize index or datapack will trigger cleanup of the whole |
| 190 |
# pack: |
| 191 |
oversized = { |
| 192 |
os.path.splitext(path)[0] |
| 193 |
for path, ftype, stat in files |
| 194 |
if (stat.st_size > maxsize and (os.path.splitext(path)[1] in VALIDEXTS)) |
| 195 |
} |
| 196 |
|
| 197 |
for rootfname in oversized: |
| 198 |
rootpath = os.path.join(folder, rootfname) |
| 199 |
for ext in VALIDEXTS: |
| 200 |
path = rootpath + ext |
| 201 |
repo.ui.debug( |
| 202 |
b'removing oversize packfile %s (%s)\n' |
| 203 |
% (path, util.bytecount(os.stat(path).st_size)) |
| 204 |
) |
| 205 |
os.unlink(path) |
| 206 |
return [row for row in files if os.path.basename(row[0]) not in oversized] |
| 207 |
|
| 208 |
|
| 209 |
def _incrementalrepack( |
| 210 |
repo, |
| 211 |
datastore, |
| 212 |
historystore, |
| 213 |
packpath, |
| 214 |
category, |
| 215 |
allowincompletedata=False, |
| 216 |
options=None, |
| 217 |
): |
| 218 |
shallowutil.mkstickygroupdir(repo.ui, packpath) |
| 219 |
|
| 220 |
files = osutil.listdir(packpath, stat=True) |
| 221 |
files = _deletebigpacks(repo, packpath, files) |
| 222 |
datapacks = _topacks( |
| 223 |
packpath, _computeincrementaldatapack(repo.ui, files), datapack.datapack |
| 224 |
) |
| 225 |
datapacks.extend( |
| 226 |
s for s in datastore if not isinstance(s, datapack.datapackstore) |
| 227 |
) |
| 228 |
|
| 229 |
historypacks = _topacks( |
| 230 |
packpath, |
| 231 |
_computeincrementalhistorypack(repo.ui, files), |
| 232 |
historypack.historypack, |
| 233 |
) |
| 234 |
historypacks.extend( |
| 235 |
s |
| 236 |
for s in historystore |
| 237 |
if not isinstance(s, historypack.historypackstore) |
| 238 |
) |
| 239 |
|
| 240 |
# ``allhistory{files,packs}`` contains all known history packs, even ones we |
| 241 |
# don't plan to repack. They are used during the datapack repack to ensure |
| 242 |
# good ordering of nodes. |
| 243 |
allhistoryfiles = _allpackfileswithsuffix( |
| 244 |
files, historypack.PACKSUFFIX, historypack.INDEXSUFFIX |
| 245 |
) |
| 246 |
allhistorypacks = _topacks( |
| 247 |
packpath, |
| 248 |
(f for f, mode, stat in allhistoryfiles), |
| 249 |
historypack.historypack, |
| 250 |
) |
| 251 |
allhistorypacks.extend( |
| 252 |
s |
| 253 |
for s in historystore |
| 254 |
if not isinstance(s, historypack.historypackstore) |
| 255 |
) |
| 256 |
_runrepack( |
| 257 |
repo, |
| 258 |
contentstore.unioncontentstore( |
| 259 |
*datapacks, allowincomplete=allowincompletedata |
| 260 |
), |
| 261 |
metadatastore.unionmetadatastore(*historypacks, allowincomplete=True), |
| 262 |
packpath, |
| 263 |
category, |
| 264 |
fullhistory=metadatastore.unionmetadatastore( |
| 265 |
*allhistorypacks, allowincomplete=True |
| 266 |
), |
| 267 |
options=options, |
| 268 |
) |
| 269 |
|
| 270 |
|
| 271 |
def _computeincrementaldatapack(ui, files): |
| 272 |
opts = { |
| 273 |
b'gencountlimit': ui.configint(b'remotefilelog', b'data.gencountlimit'), |
| 274 |
b'generations': ui.configlist(b'remotefilelog', b'data.generations'), |
| 275 |
b'maxrepackpacks': ui.configint( |
| 276 |
b'remotefilelog', b'data.maxrepackpacks' |
| 277 |
), |
| 278 |
b'repackmaxpacksize': ui.configbytes( |
| 279 |
b'remotefilelog', b'data.repackmaxpacksize' |
| 280 |
), |
| 281 |
b'repacksizelimit': ui.configbytes( |
| 282 |
b'remotefilelog', b'data.repacksizelimit' |
| 283 |
), |
| 284 |
} |
| 285 |
|
| 286 |
packfiles = _allpackfileswithsuffix( |
| 287 |
files, datapack.PACKSUFFIX, datapack.INDEXSUFFIX |
| 288 |
) |
| 289 |
return _computeincrementalpack(packfiles, opts) |
| 290 |
|
| 291 |
|
| 292 |
def _computeincrementalhistorypack(ui, files): |
| 293 |
opts = { |
| 294 |
b'gencountlimit': ui.configint( |
| 295 |
b'remotefilelog', b'history.gencountlimit' |
| 296 |
), |
| 297 |
b'generations': ui.configlist( |
| 298 |
b'remotefilelog', b'history.generations', [b'100MB'] |
| 299 |
), |
| 300 |
b'maxrepackpacks': ui.configint( |
| 301 |
b'remotefilelog', b'history.maxrepackpacks' |
| 302 |
), |
| 303 |
b'repackmaxpacksize': ui.configbytes( |
| 304 |
b'remotefilelog', b'history.repackmaxpacksize', b'400MB' |
| 305 |
), |
| 306 |
b'repacksizelimit': ui.configbytes( |
| 307 |
b'remotefilelog', b'history.repacksizelimit' |
| 308 |
), |
| 309 |
} |
| 310 |
|
| 311 |
packfiles = _allpackfileswithsuffix( |
| 312 |
files, historypack.PACKSUFFIX, historypack.INDEXSUFFIX |
| 313 |
) |
| 314 |
return _computeincrementalpack(packfiles, opts) |
| 315 |
|
| 316 |
|
| 317 |
def _allpackfileswithsuffix(files, packsuffix, indexsuffix): |
| 318 |
result = [] |
| 319 |
fileset = {fn for fn, mode, stat in files} |
| 320 |
for filename, mode, stat in files: |
| 321 |
if not filename.endswith(packsuffix): |
| 322 |
continue |
| 323 |
|
| 324 |
prefix = filename[: -len(packsuffix)] |
| 325 |
|
| 326 |
# Don't process a pack if it doesn't have an index. |
| 327 |
if (prefix + indexsuffix) not in fileset: |
| 328 |
continue |
| 329 |
result.append((prefix, mode, stat)) |
| 330 |
|
| 331 |
return result |
| 332 |
|
| 333 |
|
| 334 |
def _computeincrementalpack(files, opts): |
| 335 |
"""Given a set of pack files along with the configuration options, this |
| 336 |
function computes the list of files that should be packed as part of an |
| 337 |
incremental repack. |
| 338 |
|
| 339 |
It tries to strike a balance between keeping incremental repacks cheap (i.e. |
| 340 |
packing small things when possible, and rolling the packs up to the big ones |
| 341 |
over time). |
| 342 |
""" |
| 343 |
|
| 344 |
limits = list( |
| 345 |
sorted((util.sizetoint(s) for s in opts[b'generations']), reverse=True) |
| 346 |
) |
| 347 |
limits.append(0) |
| 348 |
|
| 349 |
# Group the packs by generation (i.e. by size) |
| 350 |
generations = [] |
| 351 |
for i in range(len(limits)): |
| 352 |
generations.append([]) |
| 353 |
|
| 354 |
sizes = {} |
| 355 |
for prefix, mode, stat in files: |
| 356 |
size = stat.st_size |
| 357 |
if size > opts[b'repackmaxpacksize']: |
| 358 |
continue |
| 359 |
|
| 360 |
sizes[prefix] = size |
| 361 |
for i, limit in enumerate(limits): |
| 362 |
if size > limit: |
| 363 |
generations[i].append(prefix) |
| 364 |
break |
| 365 |
|
| 366 |
# Steps for picking what packs to repack: |
| 367 |
# 1. Pick the largest generation with > gencountlimit pack files. |
| 368 |
# 2. Take the smallest three packs. |
| 369 |
# 3. While total-size-of-packs < repacksizelimit: add another pack |
| 370 |
|
| 371 |
# Find the largest generation with more than gencountlimit packs |
| 372 |
genpacks = [] |
| 373 |
for i, limit in enumerate(limits): |
| 374 |
if len(generations[i]) > opts[b'gencountlimit']: |
| 375 |
# Sort to be smallest last, for easy popping later |
| 376 |
genpacks.extend( |
| 377 |
sorted(generations[i], reverse=True, key=lambda x: sizes[x]) |
| 378 |
) |
| 379 |
break |
| 380 |
|
| 381 |
# Take as many packs from the generation as we can |
| 382 |
chosenpacks = genpacks[-3:] |
| 383 |
genpacks = genpacks[:-3] |
| 384 |
repacksize = sum(sizes[n] for n in chosenpacks) |
| 385 |
while ( |
| 386 |
repacksize < opts[b'repacksizelimit'] |
| 387 |
and genpacks |
| 388 |
and len(chosenpacks) < opts[b'maxrepackpacks'] |
| 389 |
): |
| 390 |
chosenpacks.append(genpacks.pop()) |
| 391 |
repacksize += sizes[chosenpacks[-1]] |
| 392 |
|
| 393 |
return chosenpacks |
| 394 |
|
| 395 |
|
| 396 |
def _runrepack( |
| 397 |
repo, data, history, packpath, category, fullhistory=None, options=None |
| 398 |
): |
| 399 |
shallowutil.mkstickygroupdir(repo.ui, packpath) |
| 400 |
|
| 401 |
def isold(repo, filename, node): |
| 402 |
"""Check if the file node is older than a limit. |
| 403 |
Unless a limit is specified in the config the default limit is taken. |
| 404 |
""" |
| 405 |
filectx = repo.filectx(filename, fileid=node) |
| 406 |
filetime = repo[filectx.linkrev()].date() |
| 407 |
|
| 408 |
ttl = repo.ui.configint(b'remotefilelog', b'nodettl') |
| 409 |
|
| 410 |
limit = time.time() - ttl |
| 411 |
return filetime[0] < limit |
| 412 |
|
| 413 |
garbagecollect = repo.ui.configbool(b'remotefilelog', b'gcrepack') |
| 414 |
if not fullhistory: |
| 415 |
fullhistory = history |
| 416 |
packer = repacker( |
| 417 |
repo, |
| 418 |
data, |
| 419 |
history, |
| 420 |
fullhistory, |
| 421 |
category, |
| 422 |
gc=garbagecollect, |
| 423 |
isold=isold, |
| 424 |
options=options, |
| 425 |
) |
| 426 |
|
| 427 |
with datapack.mutabledatapack(repo.ui, packpath) as dpack: |
| 428 |
with historypack.mutablehistorypack(repo.ui, packpath) as hpack: |
| 429 |
try: |
| 430 |
packer.run(dpack, hpack) |
| 431 |
except error.LockHeld: |
| 432 |
raise RepackAlreadyRunning( |
| 433 |
_( |
| 434 |
b"skipping repack - another repack " |
| 435 |
b"is already running" |
| 436 |
) |
| 437 |
) |
| 438 |
|
| 439 |
|
| 440 |
def keepset(repo, keyfn, lastkeepkeys=None): |
| 441 |
"""Computes a keepset which is not garbage collected. |
| 442 |
'keyfn' is a function that maps filename, node to a unique key. |
| 443 |
'lastkeepkeys' is an optional argument and if provided the keepset |
| 444 |
function updates lastkeepkeys with more keys and returns the result. |
| 445 |
""" |
| 446 |
if not lastkeepkeys: |
| 447 |
keepkeys = set() |
| 448 |
else: |
| 449 |
keepkeys = lastkeepkeys |
| 450 |
|
| 451 |
# We want to keep: |
| 452 |
# 1. Working copy parent |
| 453 |
# 2. Draft commits |
| 454 |
# 3. Parents of draft commits |
| 455 |
# 4. Pullprefetch and bgprefetchrevs revsets if specified |
| 456 |
revs = [b'.', b'draft()', b'parents(draft())'] |
| 457 |
prefetchrevs = repo.ui.config(b'remotefilelog', b'pullprefetch', None) |
| 458 |
if prefetchrevs: |
| 459 |
revs.append(b'(%s)' % prefetchrevs) |
| 460 |
prefetchrevs = repo.ui.config(b'remotefilelog', b'bgprefetchrevs', None) |
| 461 |
if prefetchrevs: |
| 462 |
revs.append(b'(%s)' % prefetchrevs) |
| 463 |
revs = b'+'.join(revs) |
| 464 |
|
| 465 |
revs = [b'sort((%s), "topo")' % revs] |
| 466 |
keep = scmutil.revrange(repo, revs) |
| 467 |
|
| 468 |
processed = set() |
| 469 |
lastmanifest = None |
| 470 |
|
| 471 |
# process the commits in toposorted order starting from the oldest |
| 472 |
for r in reversed(keep._list): |
| 473 |
delta_from, m = repo[r].manifestctx().read_any_fast_delta(processed) |
| 474 |
if delta_from is None and lastmanifest is not None: |
| 475 |
# could not find a delta, compute one. |
| 476 |
# XXX (is this really faster?) |
| 477 |
full = m |
| 478 |
if lastmanifest: |
| 479 |
m = m.diff(lastmanifest) |
| 480 |
lastmanifest = full |
| 481 |
processed.add(r) |
| 482 |
|
| 483 |
# populate keepkeys with keys from the current manifest |
| 484 |
if type(m) is dict: |
| 485 |
# m is a result of diff of two manifests and is a dictionary that |
| 486 |
# maps filename to ((newnode, newflag), (oldnode, oldflag)) tuple |
| 487 |
for filename, diff in m.items(): |
| 488 |
if diff[0][0] is not None: |
| 489 |
keepkeys.add(keyfn(filename, diff[0][0])) |
| 490 |
else: |
| 491 |
# m is a manifest object |
| 492 |
for filename, filenode in m.items(): |
| 493 |
keepkeys.add(keyfn(filename, filenode)) |
| 494 |
|
| 495 |
return keepkeys |
| 496 |
|
| 497 |
|
| 498 |
class repacker: |
| 499 |
"""Class for orchestrating the repack of data and history information into a |
| 500 |
new format. |
| 501 |
""" |
| 502 |
|
| 503 |
def __init__( |
| 504 |
self, |
| 505 |
repo, |
| 506 |
data, |
| 507 |
history, |
| 508 |
fullhistory, |
| 509 |
category, |
| 510 |
gc=False, |
| 511 |
isold=None, |
| 512 |
options=None, |
| 513 |
): |
| 514 |
self.repo = repo |
| 515 |
self.data = data |
| 516 |
self.history = history |
| 517 |
self.fullhistory = fullhistory |
| 518 |
self.unit = constants.getunits(category) |
| 519 |
self.garbagecollect = gc |
| 520 |
self.options = options |
| 521 |
if self.garbagecollect: |
| 522 |
if not isold: |
| 523 |
raise ValueError(b"Function 'isold' is not properly specified") |
| 524 |
# use (filename, node) tuple as a keepset key |
| 525 |
self.keepkeys = keepset(repo, lambda f, n: (f, n)) |
| 526 |
self.isold = isold |
| 527 |
|
| 528 |
def run(self, targetdata, targethistory): |
| 529 |
ledger = repackledger() |
| 530 |
|
| 531 |
with lockmod.lock( |
| 532 |
repacklockvfs(self.repo), b"repacklock", desc=None, timeout=0 |
| 533 |
): |
| 534 |
self.repo.hook(b'prerepack') |
| 535 |
|
| 536 |
# Populate ledger from source |
| 537 |
self.data.markledger(ledger, options=self.options) |
| 538 |
self.history.markledger(ledger, options=self.options) |
| 539 |
|
| 540 |
# Run repack |
| 541 |
self.repackdata(ledger, targetdata) |
| 542 |
self.repackhistory(ledger, targethistory) |
| 543 |
|
| 544 |
# Call cleanup on each source |
| 545 |
for source in ledger.sources: |
| 546 |
source.cleanup(ledger) |
| 547 |
|
| 548 |
def _chainorphans(self, ui, filename, nodes, orphans, deltabases): |
| 549 |
"""Reorderes ``orphans`` into a single chain inside ``nodes`` and |
| 550 |
``deltabases``. |
| 551 |
|
| 552 |
We often have orphan entries (nodes without a base that aren't |
| 553 |
referenced by other nodes -- i.e., part of a chain) due to gaps in |
| 554 |
history. Rather than store them as individual fulltexts, we prefer to |
| 555 |
insert them as one chain sorted by size. |
| 556 |
""" |
| 557 |
if not orphans: |
| 558 |
return nodes |
| 559 |
|
| 560 |
def getsize(node, default=0): |
| 561 |
meta = self.data.getmeta(filename, node) |
| 562 |
if constants.METAKEYSIZE in meta: |
| 563 |
return meta[constants.METAKEYSIZE] |
| 564 |
else: |
| 565 |
return default |
| 566 |
|
| 567 |
# Sort orphans by size; biggest first is preferred, since it's more |
| 568 |
# likely to be the newest version assuming files grow over time. |
| 569 |
# (Sort by node first to ensure the sort is stable.) |
| 570 |
orphans = sorted(orphans) |
| 571 |
orphans = list(sorted(orphans, key=getsize, reverse=True)) |
| 572 |
if ui.debugflag: |
| 573 |
ui.debug( |
| 574 |
b"%s: orphan chain: %s\n" |
| 575 |
% (filename, b", ".join([short(s) for s in orphans])) |
| 576 |
) |
| 577 |
|
| 578 |
# Create one contiguous chain and reassign deltabases. |
| 579 |
for i, node in enumerate(orphans): |
| 580 |
if i == 0: |
| 581 |
deltabases[node] = (self.repo.nullid, 0) |
| 582 |
else: |
| 583 |
parent = orphans[i - 1] |
| 584 |
deltabases[node] = (parent, deltabases[parent][1] + 1) |
| 585 |
nodes = [n for n in nodes if n not in orphans] |
| 586 |
nodes += orphans |
| 587 |
return nodes |
| 588 |
|
| 589 |
def repackdata(self, ledger, target): |
| 590 |
ui = self.repo.ui |
| 591 |
maxchainlen = ui.configint(b'packs', b'maxchainlen', 1000) |
| 592 |
|
| 593 |
byfile = {} |
| 594 |
for entry in ledger.entries.values(): |
| 595 |
if entry.datasource: |
| 596 |
byfile.setdefault(entry.filename, {})[entry.node] = entry |
| 597 |
|
| 598 |
count = 0 |
| 599 |
repackprogress = ui.makeprogress( |
| 600 |
_(b"repacking data"), unit=self.unit, total=len(byfile) |
| 601 |
) |
| 602 |
for filename, entries in sorted(byfile.items()): |
| 603 |
repackprogress.update(count) |
| 604 |
|
| 605 |
ancestors = {} |
| 606 |
nodes = list(node for node in entries) |
| 607 |
nohistory = [] |
| 608 |
buildprogress = ui.makeprogress( |
| 609 |
_(b"building history"), unit=b'nodes', total=len(nodes) |
| 610 |
) |
| 611 |
for i, node in enumerate(nodes): |
| 612 |
if node in ancestors: |
| 613 |
continue |
| 614 |
buildprogress.update(i) |
| 615 |
try: |
| 616 |
ancestors.update( |
| 617 |
self.fullhistory.getancestors( |
| 618 |
filename, node, known=ancestors |
| 619 |
) |
| 620 |
) |
| 621 |
except KeyError: |
| 622 |
# Since we're packing data entries, we may not have the |
| 623 |
# corresponding history entries for them. It's not a big |
| 624 |
# deal, but the entries won't be delta'd perfectly. |
| 625 |
nohistory.append(node) |
| 626 |
buildprogress.complete() |
| 627 |
|
| 628 |
# Order the nodes children first, so we can produce reverse deltas |
| 629 |
orderednodes = list(reversed(self._toposort(ancestors))) |
| 630 |
if len(nohistory) > 0: |
| 631 |
ui.debug( |
| 632 |
b'repackdata: %d nodes without history\n' % len(nohistory) |
| 633 |
) |
| 634 |
orderednodes.extend(sorted(nohistory)) |
| 635 |
|
| 636 |
# Filter orderednodes to just the nodes we want to serialize (it |
| 637 |
# currently also has the edge nodes' ancestors). |
| 638 |
orderednodes = list( |
| 639 |
filter(lambda node: node in nodes, orderednodes) |
| 640 |
) |
| 641 |
|
| 642 |
# Garbage collect old nodes: |
| 643 |
if self.garbagecollect: |
| 644 |
neworderednodes = [] |
| 645 |
for node in orderednodes: |
| 646 |
# If the node is old and is not in the keepset, we skip it, |
| 647 |
# and mark as garbage collected |
| 648 |
if (filename, node) not in self.keepkeys and self.isold( |
| 649 |
self.repo, filename, node |
| 650 |
): |
| 651 |
entries[node].gced = True |
| 652 |
continue |
| 653 |
neworderednodes.append(node) |
| 654 |
orderednodes = neworderednodes |
| 655 |
|
| 656 |
# Compute delta bases for nodes: |
| 657 |
deltabases = {} |
| 658 |
nobase = set() |
| 659 |
referenced = set() |
| 660 |
nodes = set(nodes) |
| 661 |
processprogress = ui.makeprogress( |
| 662 |
_(b"processing nodes"), unit=b'nodes', total=len(orderednodes) |
| 663 |
) |
| 664 |
for i, node in enumerate(orderednodes): |
| 665 |
processprogress.update(i) |
| 666 |
# Find delta base |
| 667 |
# TODO: allow delta'ing against most recent descendant instead |
| 668 |
# of immediate child |
| 669 |
deltatuple = deltabases.get(node, None) |
| 670 |
if deltatuple is None: |
| 671 |
deltabase, chainlen = self.repo.nullid, 0 |
| 672 |
deltabases[node] = (self.repo.nullid, 0) |
| 673 |
nobase.add(node) |
| 674 |
else: |
| 675 |
deltabase, chainlen = deltatuple |
| 676 |
referenced.add(deltabase) |
| 677 |
|
| 678 |
# Use available ancestor information to inform our delta choices |
| 679 |
ancestorinfo = ancestors.get(node) |
| 680 |
if ancestorinfo: |
| 681 |
p1, p2, linknode, copyfrom = ancestorinfo |
| 682 |
|
| 683 |
# The presence of copyfrom means we're at a point where the |
| 684 |
# file was copied from elsewhere. So don't attempt to do any |
| 685 |
# deltas with the other file. |
| 686 |
if copyfrom: |
| 687 |
p1 = self.repo.nullid |
| 688 |
|
| 689 |
if chainlen < maxchainlen: |
| 690 |
# Record this child as the delta base for its parents. |
| 691 |
# This may be non optimal, since the parents may have |
| 692 |
# many children, and this will only choose the last one. |
| 693 |
# TODO: record all children and try all deltas to find |
| 694 |
# best |
| 695 |
if p1 != self.repo.nullid: |
| 696 |
deltabases[p1] = (node, chainlen + 1) |
| 697 |
if p2 != self.repo.nullid: |
| 698 |
deltabases[p2] = (node, chainlen + 1) |
| 699 |
|
| 700 |
# experimental config: repack.chainorphansbysize |
| 701 |
if ui.configbool(b'repack', b'chainorphansbysize'): |
| 702 |
orphans = nobase - referenced |
| 703 |
orderednodes = self._chainorphans( |
| 704 |
ui, filename, orderednodes, orphans, deltabases |
| 705 |
) |
| 706 |
|
| 707 |
# Compute deltas and write to the pack |
| 708 |
for i, node in enumerate(orderednodes): |
| 709 |
deltabase, chainlen = deltabases[node] |
| 710 |
# Compute delta |
| 711 |
# TODO: Optimize the deltachain fetching. Since we're |
| 712 |
# iterating over the different version of the file, we may |
| 713 |
# be fetching the same deltachain over and over again. |
| 714 |
if deltabase != self.repo.nullid: |
| 715 |
deltaentry = self.data.getdelta(filename, node) |
| 716 |
delta, deltabasename, origdeltabase, meta = deltaentry |
| 717 |
size = meta.get(constants.METAKEYSIZE) |
| 718 |
if ( |
| 719 |
deltabasename != filename |
| 720 |
or origdeltabase != deltabase |
| 721 |
or size is None |
| 722 |
): |
| 723 |
deltabasetext = self.data.get(filename, deltabase) |
| 724 |
original = self.data.get(filename, node) |
| 725 |
size = len(original) |
| 726 |
delta = mdiff.textdiff(deltabasetext, original) |
| 727 |
else: |
| 728 |
delta = self.data.get(filename, node) |
| 729 |
size = len(delta) |
| 730 |
meta = self.data.getmeta(filename, node) |
| 731 |
|
| 732 |
# TODO: don't use the delta if it's larger than the fulltext |
| 733 |
if constants.METAKEYSIZE not in meta: |
| 734 |
meta[constants.METAKEYSIZE] = size |
| 735 |
target.add(filename, node, deltabase, delta, meta) |
| 736 |
|
| 737 |
entries[node].datarepacked = True |
| 738 |
|
| 739 |
processprogress.complete() |
| 740 |
count += 1 |
| 741 |
|
| 742 |
repackprogress.complete() |
| 743 |
target.close(ledger=ledger) |
| 744 |
|
| 745 |
def repackhistory(self, ledger, target): |
| 746 |
ui = self.repo.ui |
| 747 |
|
| 748 |
byfile = {} |
| 749 |
for entry in ledger.entries.values(): |
| 750 |
if entry.historysource: |
| 751 |
byfile.setdefault(entry.filename, {})[entry.node] = entry |
| 752 |
|
| 753 |
progress = ui.makeprogress( |
| 754 |
_(b"repacking history"), unit=self.unit, total=len(byfile) |
| 755 |
) |
| 756 |
for filename, entries in sorted(byfile.items()): |
| 757 |
ancestors = {} |
| 758 |
nodes = list(node for node in entries) |
| 759 |
|
| 760 |
for node in nodes: |
| 761 |
if node in ancestors: |
| 762 |
continue |
| 763 |
ancestors.update( |
| 764 |
self.history.getancestors(filename, node, known=ancestors) |
| 765 |
) |
| 766 |
|
| 767 |
# Order the nodes children first |
| 768 |
orderednodes = reversed(self._toposort(ancestors)) |
| 769 |
|
| 770 |
# Write to the pack |
| 771 |
dontprocess = set() |
| 772 |
for node in orderednodes: |
| 773 |
p1, p2, linknode, copyfrom = ancestors[node] |
| 774 |
|
| 775 |
# If the node is marked dontprocess, but it's also in the |
| 776 |
# explicit entries set, that means the node exists both in this |
| 777 |
# file and in another file that was copied to this file. |
| 778 |
# Usually this happens if the file was copied to another file, |
| 779 |
# then the copy was deleted, then reintroduced without copy |
| 780 |
# metadata. The original add and the new add have the same hash |
| 781 |
# since the content is identical and the parents are null. |
| 782 |
if node in dontprocess and node not in entries: |
| 783 |
# If copyfrom == filename, it means the copy history |
| 784 |
# went to come other file, then came back to this one, so we |
| 785 |
# should continue processing it. |
| 786 |
if p1 != self.repo.nullid and copyfrom != filename: |
| 787 |
dontprocess.add(p1) |
| 788 |
if p2 != self.repo.nullid: |
| 789 |
dontprocess.add(p2) |
| 790 |
continue |
| 791 |
|
| 792 |
if copyfrom: |
| 793 |
dontprocess.add(p1) |
| 794 |
|
| 795 |
target.add(filename, node, p1, p2, linknode, copyfrom) |
| 796 |
|
| 797 |
if node in entries: |
| 798 |
entries[node].historyrepacked = True |
| 799 |
|
| 800 |
progress.increment() |
| 801 |
|
| 802 |
progress.complete() |
| 803 |
target.close(ledger=ledger) |
| 804 |
|
| 805 |
def _toposort(self, ancestors): |
| 806 |
def parentfunc(node): |
| 807 |
p1, p2, linknode, copyfrom = ancestors[node] |
| 808 |
parents = [] |
| 809 |
if p1 != self.repo.nullid: |
| 810 |
parents.append(p1) |
| 811 |
if p2 != self.repo.nullid: |
| 812 |
parents.append(p2) |
| 813 |
return parents |
| 814 |
|
| 815 |
sortednodes = shallowutil.sortnodes(ancestors.keys(), parentfunc) |
| 816 |
return sortednodes |
| 817 |
|
| 818 |
|
| 819 |
class repackledger: |
| 820 |
"""Storage for all the bookkeeping that happens during a repack. It contains |
| 821 |
the list of revisions being repacked, what happened to each revision, and |
| 822 |
which source store contained which revision originally (for later cleanup). |
| 823 |
""" |
| 824 |
|
| 825 |
def __init__(self): |
| 826 |
self.entries = {} |
| 827 |
self.sources = {} |
| 828 |
self.created = set() |
| 829 |
|
| 830 |
def markdataentry(self, source, filename, node): |
| 831 |
"""Mark the given filename+node revision as having a data rev in the |
| 832 |
given source. |
| 833 |
""" |
| 834 |
entry = self._getorcreateentry(filename, node) |
| 835 |
entry.datasource = True |
| 836 |
entries = self.sources.get(source) |
| 837 |
if not entries: |
| 838 |
entries = set() |
| 839 |
self.sources[source] = entries |
| 840 |
entries.add(entry) |
| 841 |
|
| 842 |
def markhistoryentry(self, source, filename, node): |
| 843 |
"""Mark the given filename+node revision as having a history rev in the |
| 844 |
given source. |
| 845 |
""" |
| 846 |
entry = self._getorcreateentry(filename, node) |
| 847 |
entry.historysource = True |
| 848 |
entries = self.sources.get(source) |
| 849 |
if not entries: |
| 850 |
entries = set() |
| 851 |
self.sources[source] = entries |
| 852 |
entries.add(entry) |
| 853 |
|
| 854 |
def _getorcreateentry(self, filename, node): |
| 855 |
key = (filename, node) |
| 856 |
value = self.entries.get(key) |
| 857 |
if not value: |
| 858 |
value = repackentry(filename, node) |
| 859 |
self.entries[key] = value |
| 860 |
|
| 861 |
return value |
| 862 |
|
| 863 |
def addcreated(self, value): |
| 864 |
self.created.add(value) |
| 865 |
|
| 866 |
|
| 867 |
class repackentry: |
| 868 |
"""Simple class representing a single revision entry in the repackledger.""" |
| 869 |
|
| 870 |
__slots__ = ( |
| 871 |
'filename', |
| 872 |
'node', |
| 873 |
'datasource', |
| 874 |
'historysource', |
| 875 |
'datarepacked', |
| 876 |
'historyrepacked', |
| 877 |
'gced', |
| 878 |
) |
| 879 |
|
| 880 |
def __init__(self, filename, node): |
| 881 |
self.filename = filename |
| 882 |
self.node = node |
| 883 |
# If the revision has a data entry in the source |
| 884 |
self.datasource = False |
| 885 |
# If the revision has a history entry in the source |
| 886 |
self.historysource = False |
| 887 |
# If the revision's data entry was repacked into the repack target |
| 888 |
self.datarepacked = False |
| 889 |
# If the revision's history entry was repacked into the repack target |
| 890 |
self.historyrepacked = False |
| 891 |
# If garbage collected |
| 892 |
self.gced = False |
| 893 |
|
| 894 |
|
| 895 |
def repacklockvfs(repo): |
| 896 |
if hasattr(repo, 'name'): |
| 897 |
# Lock in the shared cache so repacks across multiple copies of the same |
| 898 |
# repo are coordinated. |
| 899 |
sharedcachepath = shallowutil.getcachepackpath( |
| 900 |
repo, constants.FILEPACK_CATEGORY |
| 901 |
) |
| 902 |
return vfs.vfs(sharedcachepath) |
| 903 |
else: |
| 904 |
return repo.svfs |