view hgext/fastannotate/__init__.py @ 39772:ae531f5e583c

testing: add interface unit tests for file storage Our strategy for supporting alternate storage backends is to define interfaces for everything then "code to the interface." We already have interfaces for various primitives, including file and manifest storage. What we don't have is generic unit tests for those interfaces. Up to this point we've been relying on high-level integration tests (mainly in the form of existing .t tests) to test alternate storage backends. And my experience with developing the "simple store" test extension is that such testing is very tedious: it takes several minutes to run all tests and when you find a failure, it is often non-trivial to debug. This commit starts to change that. This commit introduces the mercurial.testing.storage module. It contains testing code for storage. Currently, it defines some unittest.TestCase classes for testing the file storage interfaces. It also defines some factory functions that allow a caller to easily spawn a custom TestCase "bound" to a specific file storage backend implementation. A new .py test has been added. It simply defines a callable to produce filelog and transaction instances on demand and then "registers" the various test classes so the filelog class can be tested with the storage interface unit tests. As part of writing the tests, I identified a couple of apparent bugs in revlog.py and filelog.py! These are tracked with inline TODO comments. Writing the tests makes it more obvious where the storage interface is lacking. For example, we raise either IndexError or error.LookupError for missing revisions depending on whether we use an integer revision or a node. Also, we raise error.RevlogError in various places when we should be raising a storage-agnostic error type. The storage interfaces are currently far from perfect and there is much work to be done to improve them. But at least with this commit we finally have the start of unit tests that can be used to "qualify" the behavior of a storage backend. And when implementing and debugging new storage backends, we now have an obvious place to define new tests and have obvious places to insert breakpoints to facilitate debugging. This should be invaluable when implementing new storage backends. I added the mercurial.testing package because these interface conformance tests are generic and need to be usable by all storage backends. Having the code live in tests/ would make it difficult for storage backends implemented in extensions to test their interface conformance. First, it would require obtaining a copy of Mercurial's storage test code in order to test. Second, it would make testing against multiple Mercurial versions difficult, as you would need to import N copies of the storage testing code in order to achieve test coverage. By making the test code part of the Mercurial distribution itself, extensions can `import mercurial.testing.*` to access and run the test code. The test will run against whatever Mercurial version is active. FWIW I've always wanted to move parts of run-tests.py into the mercurial.* package to make the testing story simpler (e.g. imagine an `hg debugruntests` command that could invoke the test harness). While I have no plans to do that in the near future, establishing the mercurial.testing package does provide a natural home for that code should someone do this in the future. Differential Revision: https://phab.mercurial-scm.org/D4650
author Gregory Szorc <gregory.szorc@gmail.com>
date Tue, 18 Sep 2018 16:52:11 -0700
parents 57d4754e44b8
children c841e8855cd3
line wrap: on
line source

# Copyright 2016-present Facebook. All Rights Reserved.
#
# fastannotate: faster annotate implementation using linelog
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
"""yet another annotate implementation that might be faster (EXPERIMENTAL)

The fastannotate extension provides a 'fastannotate' command that makes
use of the linelog data structure as a cache layer and is expected to
be faster than the vanilla 'annotate' if the cache is present.

In most cases, fastannotate requires a setup that mainbranch is some pointer
that always moves forward, to be most efficient.

Using fastannotate together with linkrevcache would speed up building the
annotate cache greatly. Run "debugbuildlinkrevcache" before
"debugbuildannotatecache".

::

    [fastannotate]
    # specify the main branch head. the internal linelog will only contain
    # the linear (ignoring p2) "mainbranch". since linelog cannot move
    # backwards without a rebuild, this should be something that always moves
    # forward, usually it is "master" or "@".
    mainbranch = master

    # fastannotate supports different modes to expose its feature.
    # a list of combination:
    # - fastannotate: expose the feature via the "fastannotate" command which
    #   deals with everything in a most efficient way, and provides extra
    #   features like --deleted etc.
    # - fctx: replace fctx.annotate implementation. note:
    #     a. it is less efficient than the "fastannotate" command
    #     b. it will make it practically impossible to access the old (disk
    #        side-effect free) annotate implementation
    #     c. it implies "hgweb".
    # - hgweb: replace hgweb's annotate implementation. conflict with "fctx".
    # (default: fastannotate)
    modes = fastannotate

    # default format when no format flags are used (default: number)
    defaultformat = changeset, user, date

    # serve the annotate cache via wire protocol (default: False)
    # tip: the .hg/fastannotate directory is portable - can be rsynced
    server = True

    # build annotate cache on demand for every client request (default: True)
    # disabling it could make server response faster, useful when there is a
    # cronjob building the cache.
    serverbuildondemand = True

    # update local annotate cache from remote on demand
    client = False

    # path to use when connecting to the remote server (default: default)
    remotepath = default

    # minimal length of the history of a file required to fetch linelog from
    # the server. (default: 10)
    clientfetchthreshold = 10

    # use flock instead of the file existence lock
    # flock may not work well on some network filesystems, but they avoid
    # creating and deleting files frequently, which is faster when updating
    # the annotate cache in batch. if you have issues with this option, set it
    # to False. (default: True if flock is supported, False otherwise)
    useflock = True

    # for "fctx" mode, always follow renames regardless of command line option.
    # this is a BC with the original command but will reduced the space needed
    # for annotate cache, and is useful for client-server setup since the
    # server will only provide annotate cache with default options (i.e. with
    # follow). do not affect "fastannotate" mode. (default: True)
    forcefollow = True

    # for "fctx" mode, always treat file as text files, to skip the "isbinary"
    # check. this is consistent with the "fastannotate" command and could help
    # to avoid a file fetch if remotefilelog is used. (default: True)
    forcetext = True

    # use unfiltered repo for better performance.
    unfilteredrepo = True

    # sacrifice correctness in some corner cases for performance. it does not
    # affect the correctness of the annotate cache being built. the option
    # is experimental and may disappear in the future (default: False)
    perfhack = True
"""

# TODO from import:
# * `branch` is probably the wrong term, throughout the code.
#
# * replace the fastannotate `modes` configuration with a collection
#   of booleans.
#
# * Use the templater instead of bespoke formatting
#
# * rename the config knob for updating the local cache from a remote server
#
# * move `flock` based locking to a common area
#
# * revise wireprotocol for sharing annotate files
#
# * figure out a sensible default for `mainbranch` (with the caveat
#   that we probably also want to figure out a better term than
#   `branch`, see above)
#
# * format changes to the revmap file (maybe use length-encoding
#   instead of null-terminated file paths at least?)
from __future__ import absolute_import

from mercurial.i18n import _
from mercurial import (
    configitems,
    error as hgerror,
    localrepo,
    registrar,
)

from . import (
    commands,
    context,
    protocol,
)

# Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
# extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
# be specifying the version(s) of Mercurial they are tested with, or
# leave the attribute unspecified.
testedwith = 'ships-with-hg-core'

cmdtable = commands.cmdtable

configtable = {}
configitem = registrar.configitem(configtable)

configitem('fastannotate', 'modes', default=['fastannotate'])
configitem('fastannotate', 'server', default=False)
configitem('fastannotate', 'useflock', default=configitems.dynamicdefault)
configitem('fastannotate', 'client', default=False)
configitem('fastannotate', 'unfilteredrepo', default=True)
configitem('fastannotate', 'defaultformat', default=['number'])
configitem('fastannotate', 'perfhack', default=False)
configitem('fastannotate', 'mainbranch')
configitem('fastannotate', 'forcetext', default=True)
configitem('fastannotate', 'forcefollow', default=True)
configitem('fastannotate', 'clientfetchthreshold', default=10)
configitem('fastannotate', 'serverbuildondemand', default=True)
configitem('fastannotate', 'remotepath', default='default')

def _flockavailable():
    try:
        import fcntl
        fcntl.flock
    except StandardError:
        return False
    else:
        return True

def uisetup(ui):
    modes = set(ui.configlist('fastannotate', 'modes'))
    if 'fctx' in modes:
        modes.discard('hgweb')
    for name in modes:
        if name == 'fastannotate':
            commands.registercommand()
        elif name == 'hgweb':
            from . import support
            support.replacehgwebannotate()
        elif name == 'fctx':
            from . import support
            support.replacefctxannotate()
            commands.wrapdefault()
        else:
            raise hgerror.Abort(_('fastannotate: invalid mode: %s') % name)

    if ui.configbool('fastannotate', 'server'):
        protocol.serveruisetup(ui)

    if ui.configbool('fastannotate', 'useflock', _flockavailable()):
        context.pathhelper.lock = context.pathhelper._lockflock

def extsetup(ui):
    # fastannotate has its own locking, without depending on repo lock
    # TODO: avoid mutating this unless the specific repo has it enabled
    localrepo.localrepository._wlockfreeprefix.add('fastannotate/')

def reposetup(ui, repo):
    if ui.configbool('fastannotate', 'client'):
        protocol.clientreposetup(ui, repo)