annotate tests/hghave @ 40021:c537144fdbef

wireprotov2: support response caching One of the things I've learned from managing VCS servers over the years is that they are hard to scale. It is well known that some companies have very beefy (read: very expensive) servers to power their VCS needs. It is also known that specialized servers for various VCS exist in order to facilitate scaling servers. (Mercurial is in this boat.) One of the aspects that make a VCS server hard to scale is the high CPU load incurred by constant client clone/pull operations. To alleviate the scaling pain associated with data retrieval operations, I want to integrate caching into the Mercurial wire protocol server as robustly as possible such that servers can aggressively cache responses and defer as much server load as possible. This commit represents the initial implementation of a general caching layer in wire protocol version 2. We define a new interface and behavior for a wire protocol cacher in repository.py. (This is probably where a reviewer should look first to understand what is going on.) The bulk of the added code is in wireprotov2server.py, where we define how a command can opt in to being cached and integrate caching into command dispatching. From a very high-level: * A command can declare itself as cacheable by providing a callable that can be used to derive a cache key. * At dispatch time, if a command is cacheable, we attempt to construct a cacher and use it for serving the request and/or caching the request. * The dispatch layer handles the bulk of the business logic for caching, making cachers mostly "dumb content stores." * The mechanism for invalidating cached entries (one of the harder parts about caching in general) is by varying the cache key when state changes. As such, cachers don't need to be concerned with cache invalidation. Initially, we've hooked up support for caching "manifestdata" and "filedata" commands. These are the simplest to cache, as they should be immutable over time. Caching of commands related to changeset data is a bit harder (because cache validation is impacted by changes to bookmarks, phases, etc). This will be implemented later. (Strictly speaking, censoring a file should invalidate caches. I've added an inline TODO to track this edge case.) To prove it works, this commit implements a test-only extension providing in-memory caching backed by an lrucachedict. A new test showing this extension behaving properly is added. FWIW, the cacher is ~50 lines of code, demonstrating the relative ease with which a cache can be added to a server. While the test cacher is not suitable for production workloads, just for kicks I performed a clone of just the changeset and manifest data for the mozilla-unified repository. With a fully warmed cache (of just the manifest data since changeset data is not cached), server-side CPU usage dropped from ~73s to ~28s. That's pretty significant and demonstrates the potential that response caching has on server scalability! Differential Revision: https://phab.mercurial-scm.org/D4773
author Gregory Szorc <gregory.szorc@gmail.com>
date Wed, 26 Sep 2018 17:16:56 -0700
parents b1b35a9051c3
children 47ef023d0165
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4881
c51c9bc4579d Add hghave utility and run-tests.py support.
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
c51c9bc4579d Add hghave utility and run-tests.py support.
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
2 """Test the running system for features availability. Exit with zero
5084
80309fa23cdb hghave: feature absence can be checked by prefixing with 'no-'
Patrick Mezard <pmezard@gmail.com>
parents: 5081
diff changeset
3 if all features are there, non-zero otherwise. If a feature name is
80309fa23cdb hghave: feature absence can be checked by prefixing with 'no-'
Patrick Mezard <pmezard@gmail.com>
parents: 5081
diff changeset
4 prefixed with "no-", the absence of feature is tested.
4881
c51c9bc4579d Add hghave utility and run-tests.py support.
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
5 """
28283
544444991c83 hghave: use print function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28047
diff changeset
6
29161
f8b274df33dc py3: make tests/hghave use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29141
diff changeset
7 from __future__ import absolute_import, print_function
28283
544444991c83 hghave: use print function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28047
diff changeset
8
29161
f8b274df33dc py3: make tests/hghave use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29141
diff changeset
9 import hghave
4881
c51c9bc4579d Add hghave utility and run-tests.py support.
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
10 import optparse
29161
f8b274df33dc py3: make tests/hghave use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29141
diff changeset
11 import os
f8b274df33dc py3: make tests/hghave use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29141
diff changeset
12 import sys
9446
57d682d7d2da test-gendoc: test documentation generation
Martin Geisler <mg@lazybytes.net>
parents: 9395
diff changeset
13
16966
23f621ca04b5 tests/hghave: extract hghave.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 16891
diff changeset
14 checks = hghave.checks
4881
c51c9bc4579d Add hghave utility and run-tests.py support.
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
15
c51c9bc4579d Add hghave utility and run-tests.py support.
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
16 def list_features():
29141
ba8999547f81 hghave: switch from iteritems to items
timeless <timeless@mozdev.org>
parents: 28283
diff changeset
17 for name, feature in sorted(checks.items()):
4881
c51c9bc4579d Add hghave utility and run-tests.py support.
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
18 desc = feature[1]
28283
544444991c83 hghave: use print function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28047
diff changeset
19 print(name + ':', desc)
4881
c51c9bc4579d Add hghave utility and run-tests.py support.
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
20
8059
41a2c5cbcb6a hghave: checking that all targets are Exception-free
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 7823
diff changeset
21 def test_features():
41a2c5cbcb6a hghave: checking that all targets are Exception-free
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 7823
diff changeset
22 failed = 0
29141
ba8999547f81 hghave: switch from iteritems to items
timeless <timeless@mozdev.org>
parents: 28283
diff changeset
23 for name, feature in checks.items():
8059
41a2c5cbcb6a hghave: checking that all targets are Exception-free
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 7823
diff changeset
24 check, _ = feature
41a2c5cbcb6a hghave: checking that all targets are Exception-free
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 7823
diff changeset
25 try:
41a2c5cbcb6a hghave: checking that all targets are Exception-free
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 7823
diff changeset
26 check()
28047
863075fd4cd0 misc: use modern exception syntax
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 26068
diff changeset
27 except Exception as e:
28283
544444991c83 hghave: use print function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28047
diff changeset
28 print("feature %s failed: %s" % (name, e))
8059
41a2c5cbcb6a hghave: checking that all targets are Exception-free
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 7823
diff changeset
29 failed += 1
41a2c5cbcb6a hghave: checking that all targets are Exception-free
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 7823
diff changeset
30 return failed
41a2c5cbcb6a hghave: checking that all targets are Exception-free
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 7823
diff changeset
31
4881
c51c9bc4579d Add hghave utility and run-tests.py support.
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
32 parser = optparse.OptionParser("%prog [options] [features]")
8059
41a2c5cbcb6a hghave: checking that all targets are Exception-free
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 7823
diff changeset
33 parser.add_option("--test-features", action="store_true",
41a2c5cbcb6a hghave: checking that all targets are Exception-free
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 7823
diff changeset
34 help="test available features")
4881
c51c9bc4579d Add hghave utility and run-tests.py support.
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
35 parser.add_option("--list-features", action="store_true",
c51c9bc4579d Add hghave utility and run-tests.py support.
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
36 help="list available features")
c51c9bc4579d Add hghave utility and run-tests.py support.
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
37
26068
05e7f57c74ac hghave: remove quiet option
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26067
diff changeset
38 def _loadaddon():
25732
b94df10cc3b5 hghave: allow adding customized features at runtime
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22762
diff changeset
39 if 'TESTDIR' in os.environ:
b94df10cc3b5 hghave: allow adding customized features at runtime
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22762
diff changeset
40 # loading from '.' isn't needed, because `hghave` should be
b94df10cc3b5 hghave: allow adding customized features at runtime
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22762
diff changeset
41 # running at TESTTMP in this case
b94df10cc3b5 hghave: allow adding customized features at runtime
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22762
diff changeset
42 path = os.environ['TESTDIR']
b94df10cc3b5 hghave: allow adding customized features at runtime
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22762
diff changeset
43 else:
b94df10cc3b5 hghave: allow adding customized features at runtime
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22762
diff changeset
44 path = '.'
b94df10cc3b5 hghave: allow adding customized features at runtime
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22762
diff changeset
45
b94df10cc3b5 hghave: allow adding customized features at runtime
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22762
diff changeset
46 if not os.path.exists(os.path.join(path, 'hghaveaddon.py')):
b94df10cc3b5 hghave: allow adding customized features at runtime
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22762
diff changeset
47 return
b94df10cc3b5 hghave: allow adding customized features at runtime
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22762
diff changeset
48
b94df10cc3b5 hghave: allow adding customized features at runtime
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22762
diff changeset
49 sys.path.insert(0, path)
b94df10cc3b5 hghave: allow adding customized features at runtime
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22762
diff changeset
50 try:
b94df10cc3b5 hghave: allow adding customized features at runtime
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22762
diff changeset
51 import hghaveaddon
29231
b1b35a9051c3 hghave: silence future pyflakes warning of unused import
Yuya Nishihara <yuya@tcha.org>
parents: 29161
diff changeset
52 assert hghaveaddon # silence pyflakes
28047
863075fd4cd0 misc: use modern exception syntax
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 26068
diff changeset
53 except BaseException as inst:
26068
05e7f57c74ac hghave: remove quiet option
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26067
diff changeset
54 sys.stderr.write('failed to import hghaveaddon.py from %r: %s\n'
05e7f57c74ac hghave: remove quiet option
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26067
diff changeset
55 % (path, inst))
25732
b94df10cc3b5 hghave: allow adding customized features at runtime
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22762
diff changeset
56 sys.exit(2)
b94df10cc3b5 hghave: allow adding customized features at runtime
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22762
diff changeset
57 sys.path.pop(0)
b94df10cc3b5 hghave: allow adding customized features at runtime
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22762
diff changeset
58
4881
c51c9bc4579d Add hghave utility and run-tests.py support.
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
59 if __name__ == '__main__':
c51c9bc4579d Add hghave utility and run-tests.py support.
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
60 options, args = parser.parse_args()
26068
05e7f57c74ac hghave: remove quiet option
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26067
diff changeset
61 _loadaddon()
4881
c51c9bc4579d Add hghave utility and run-tests.py support.
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
62 if options.list_features:
c51c9bc4579d Add hghave utility and run-tests.py support.
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
63 list_features()
c51c9bc4579d Add hghave utility and run-tests.py support.
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
64 sys.exit(0)
5081
ea7b982b6c08 Remove trailing spaces
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5074
diff changeset
65
8059
41a2c5cbcb6a hghave: checking that all targets are Exception-free
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 7823
diff changeset
66 if options.test_features:
41a2c5cbcb6a hghave: checking that all targets are Exception-free
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 7823
diff changeset
67 sys.exit(test_features())
41a2c5cbcb6a hghave: checking that all targets are Exception-free
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 7823
diff changeset
68
26068
05e7f57c74ac hghave: remove quiet option
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26067
diff changeset
69 hghave.require(args)