Mercurial > hg
view tests/test-remotefilelog-http.t @ 47010:76ae43d5b1db stable
repoview: fix memory leak of filtered repo classes
The leak occurs in long-running server processes with
extensions, and is measured at 110kB per request.
Before this change, the contents of the `_filteredrepotypes`
cache are not properly garbage collected, despite it begin
a `WeakKeyDictionary`.
Extensions have a tendency to generate a new repository class
for each `localrepo` instantiation. Server processes based
on `hgwebdir_mod` will instantiate a new `localrepo` for each
HTTP request that involves a repository.
As a result, with a testing process that repeatedly opens a
repository with several extensions activated
(`topic` notably among them), we see a steady increase in
resident memory of 110kB per repository instantiation before this
change. This is also true, if we call `gc.collect()` at each
instantiation, like `hgwebdir_mod` does, or not.
The cause of the leak is that the *values* aren't weak references.
This change uses `weakref.ref` for the values, and this makes
in our measurements the resident size increase drop to 5kB per
repository instantiation, with no explicit call of `gc.collect()`
at all.
There is currently no reason to believe that this remaining leak
of 5kB is related to or even due to Mercurial core.
We've also seen evidence that `ui.ui` instances weren't properly
garbage collected before the change (with the change, they are).
This could explain why the figures are relatively high.
In theory, the collection of weak references could lead to
much more misses in the cache, so we measured the impact on
the original case that was motivation for introducing that cache
in 7e89bd0cfb86 (see also issue5043): `hg convert` of the
mozilla-central repository. The bad news here is that there is a
major memory leak there, both with and without the present changeset.
There were no more cache misses, and we could see no
more memory leak with this change: the resident size after importing
roughly 100000 changesets was at 12.4GB before, and 12.5GB after.
The small increase is mentioned for completeness only, and we
believe that it should be ignored, at least as long as the main
leak isn't fixed. At less than 1% of the main leak, even finding out
whether it is merely noise would be wasteful.
Original context where this was spotted and first mitigated:
https://foss.heptapod.net/heptapod/heptapod/-/issues/466
The leak reduction was also obtained in Heptapod inner HTTP server,
which amounts to the same as `hgwebdir_mod` for these questions.
The measurements done with Python 3.9, similar figures seen with 3.8.
More work on our side would be needed to give measurements with 2.7,
because of testing server process does not support it.
author | Georges Racinet <georges.racinet@octobus.net> |
---|---|
date | Fri, 23 Apr 2021 18:30:53 +0200 |
parents | a495435d980e |
children | 1d075b857c90 |
line wrap: on
line source
#require no-windows $ . "$TESTDIR/remotefilelog-library.sh" $ hg init master $ cd master $ cat >> .hg/hgrc <<EOF > [remotefilelog] > server=True > EOF $ echo x > x $ echo y > y $ hg commit -qAm x $ hg serve -p $HGPORT -d --pid-file=../hg1.pid -E ../error.log -A ../access.log Build a query string for later use: $ GET=`hg debugdata -m 0 | $PYTHON -c \ > 'import sys ; print([("?cmd=x_rfl_getfile&file=%s&node=%s" % tuple(s.split("\0"))) for s in sys.stdin.read().splitlines()][0])'` $ cd .. $ cat hg1.pid >> $DAEMON_PIDS $ hgcloneshallow http://localhost:$HGPORT/ shallow -q 2 files fetched over 1 fetches - (2 misses, 0.00% hit ratio) over *s (glob) $ grep getfile access.log * "GET /?cmd=batch HTTP/1.1" 200 - x-hgarg-1:cmds=x_rfl_getfile+*node%3D1406e74118627694268417491f018a4a883152f0* (glob) Clear filenode cache so we can test fetching with a modified batch size $ rm -r $TESTTMP/hgcache Now do a fetch with a large batch size so we're sure it works $ hgcloneshallow http://localhost:$HGPORT/ shallow-large-batch \ > --config remotefilelog.batchsize=1000 -q 2 files fetched over 1 fetches - (2 misses, 0.00% hit ratio) over *s (glob) The 'remotefilelog' capability should *not* be exported over http(s), as the getfile method it offers doesn't work with http. $ get-with-headers.py localhost:$HGPORT '?cmd=capabilities' | grep lookup | identifyrflcaps x_rfl_getfile x_rfl_getflogheads $ get-with-headers.py localhost:$HGPORT '?cmd=hello' | grep lookup | identifyrflcaps x_rfl_getfile x_rfl_getflogheads $ get-with-headers.py localhost:$HGPORT '?cmd=this-command-does-not-exist' | head -n 1 400 no such method: this-command-does-not-exist $ get-with-headers.py localhost:$HGPORT '?cmd=x_rfl_getfiles' | head -n 1 400 no such method: x_rfl_getfiles Verify serving from a shallow clone doesn't allow for remotefile fetches. This also serves to test the error handling for our batchable getfile RPC. $ cd shallow $ hg serve -p $HGPORT1 -d --pid-file=../hg2.pid -E ../error2.log $ cd .. $ cat hg2.pid >> $DAEMON_PIDS This GET should work, because this server is serving master, which is a full clone. $ get-with-headers.py localhost:$HGPORT "$GET" 200 Script output follows 0\x00x\x9c3b\xa8\xe0\x12a{\xee(\x91T6E\xadE\xdcS\x9e\xb1\xcb\xab\xc30\xe8\x03\x03\x91 \xe4\xc6\xfb\x99J,\x17\x0c\x9f-\xcb\xfcR7c\xf3c\x97r\xbb\x10\x06\x00\x96m\x121 (no-eol) (esc) This GET should fail using the in-band signalling mechanism, because it's not a full clone. Note that it's also plausible for servers to refuse to serve file contents for other reasons, like the file contents not being visible to the current user. $ get-with-headers.py localhost:$HGPORT1 "$GET" 200 Script output follows 1\x00cannot fetch remote files from shallow repo (no-eol) (esc) Clones should work with httppostargs turned on $ cd master $ hg --config experimental.httppostargs=1 serve -p $HGPORT2 -d --pid-file=../hg3.pid -E ../error3.log $ cd .. $ cat hg3.pid >> $DAEMON_PIDS Clear filenode cache so we can test fetching with a modified batch size $ rm -r $TESTTMP/hgcache $ hgcloneshallow http://localhost:$HGPORT2/ shallow-postargs -q 2 files fetched over 1 fetches - (2 misses, 0.00% hit ratio) over *s (glob) All error logs should be empty: $ cat error.log $ cat error2.log $ cat error3.log