##// END OF EJS Templates
interfaces: make the `peer` mixin not a Protocol to fix Python 3.10 failures...
interfaces: make the `peer` mixin not a Protocol to fix Python 3.10 failures I can't find any documentation on this, but it appears that Protocol class attributes don't get inherited in subclasses that explicitly subclass a Protocol until Python 3.11, which caused a ton of failures in CI on macOS and Windows (which both test using Python 3.9). The problem started with 1df97507c6b8, and typically manifested as most tests failing to access `ui` on various `peer` classes. Here's a short proof of concept: from __future__ import annotations from typing import ( Protocol, ) class peer(Protocol): limitedarguments: bool = False def __init__(self, arg1, arg2, remotehidden: bool = False) -> None: self.arg1 = arg1 self.arg2 = arg2 class subclass(peer): def __init__(self, arg1, arg2): super(subclass, self).__init__(arg1, arg2, False) sub = subclass(1, 2) print("sub.arg1 is %r" % sub.arg1) When run with Python 3.8.10, 3.9.13, and 3.10.11, the result is: $ py -3.8 prot-test.py Traceback (most recent call last): File "prot-test.py", line 20, in <module> print("sub.arg1 is %r" % sub.arg1) AttributeError: 'subclass' object has no attribute 'arg1' On Python 3.11.9, 3.12.7, and 3.13.0, the result is: $ py -3.11 ../prot-test.py sub.arg1 is 1 Explicitly adding annotations to `peer` like `limitedarguments` didn't help.

File last commit:

r52756:f4733654 default
r53403:199b0e62 default
Show More
repocache.py
140 lines | 4.5 KiB | text/x-python | PythonLexer
Yuya Nishihara
commandserver: preload repository in master server and reuse its file cache...
r41035 # repocache.py - in-memory repository cache for long-running services
#
# Copyright 2018 Yuya Nishihara <yuya@tcha.org>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
Matt Harbison
typing: add `from __future__ import annotations` to most files...
r52756 from __future__ import annotations
Yuya Nishihara
commandserver: preload repository in master server and reuse its file cache...
r41035
import collections
import gc
import threading
from . import (
error,
hg,
obsolete,
scmutil,
util,
)
Augie Fackler
formatting: blacken the codebase...
r43346
Gregory Szorc
py3: use class X: instead of class X(object):...
r49801 class repoloader:
Yuya Nishihara
commandserver: preload repository in master server and reuse its file cache...
r41035 """Load repositories in background thread
This is designed for a forking server. A cached repo cannot be obtained
until the server fork()s a worker and the loader thread stops.
"""
def __init__(self, ui, maxlen):
self._ui = ui.copy()
self._cache = util.lrucachedict(max=maxlen)
# use deque and Event instead of Queue since deque can discard
# old items to keep at most maxlen items.
self._inqueue = collections.deque(maxlen=maxlen)
self._accepting = False
self._newentry = threading.Event()
self._thread = None
def start(self):
assert not self._thread
if self._inqueue.maxlen == 0:
# no need to spawn loader thread as the cache is disabled
return
self._accepting = True
self._thread = threading.Thread(target=self._mainloop)
self._thread.start()
def stop(self):
if not self._thread:
return
self._accepting = False
self._newentry.set()
self._thread.join()
self._thread = None
self._cache.clear()
self._inqueue.clear()
def load(self, path):
"""Request to load the specified repository in background"""
self._inqueue.append(path)
self._newentry.set()
def get(self, path):
"""Return a cached repo if available
This function must be called after fork(), where the loader thread
is stopped. Otherwise, the returned repo might be updated by the
loader thread.
"""
if self._thread and self._thread.is_alive():
Augie Fackler
formatting: blacken the codebase...
r43346 raise error.ProgrammingError(
Martin von Zweigbergk
cleanup: join string literals that are already on one line...
r43387 b'cannot obtain cached repo while loader is active'
Augie Fackler
formatting: blacken the codebase...
r43346 )
Yuya Nishihara
commandserver: preload repository in master server and reuse its file cache...
r41035 return self._cache.peek(path, None)
def _mainloop(self):
while self._accepting:
# Avoid heavy GC after fork(), which would cancel the benefit of
# COW. We assume that GIL is acquired while GC is underway in the
# loader thread. If that isn't true, we might have to move
# gc.collect() to the main thread so that fork() would never stop
# the thread where GC is in progress.
gc.collect()
self._newentry.wait()
while self._accepting:
self._newentry.clear()
try:
path = self._inqueue.popleft()
except IndexError:
break
scmutil.callcatch(self._ui, lambda: self._load(path))
def _load(self, path):
start = util.timer()
# TODO: repo should be recreated if storage configuration changed
try:
# pop before loading so inconsistent state wouldn't be exposed
repo = self._cache.pop(path)
except KeyError:
repo = hg.repository(self._ui, path).unfiltered()
_warmupcache(repo)
Augie Fackler
formatting: blacken the codebase...
r43346 repo.ui.log(
b'repocache',
b'loaded repo into cache: %s (in %.3fs)\n',
path,
util.timer() - start,
)
Yuya Nishihara
commandserver: preload repository in master server and reuse its file cache...
r41035 self._cache.insert(path, repo)
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
commandserver: preload repository in master server and reuse its file cache...
r41035 # TODO: think about proper API of preloading cache
def _warmupcache(repo):
repo.invalidateall()
repo.changelog
repo.obsstore._all
repo.obsstore.successors
repo.obsstore.predecessors
repo.obsstore.children
for name in obsolete.cachefuncs:
obsolete.getrevs(repo, name)
phases: change the way we warm the phasecache in repocache...
r52307 # ensure the phase cache is fully initialized
repo._phasecache.phase(repo, repo.changelog.tiprev())
Yuya Nishihara
commandserver: preload repository in master server and reuse its file cache...
r41035
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
commandserver: preload repository in master server and reuse its file cache...
r41035 # TODO: think about proper API of attaching preloaded attributes
def copycache(srcrepo, destrepo):
"""Copy cached attributes from srcrepo to destrepo"""
destfilecache = destrepo._filecache
srcfilecache = srcrepo._filecache
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if b'changelog' in srcfilecache:
destfilecache[b'changelog'] = ce = srcfilecache[b'changelog']
changelog-delay: move the delay/divert logic inside the (inner) revlog...
r51999 ce.obj.opener = ce.obj._inner.opener = destrepo.svfs
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if b'obsstore' in srcfilecache:
destfilecache[b'obsstore'] = ce = srcfilecache[b'obsstore']
Yuya Nishihara
commandserver: preload repository in master server and reuse its file cache...
r41035 ce.obj.svfs = destrepo.svfs
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if b'_phasecache' in srcfilecache:
destfilecache[b'_phasecache'] = ce = srcfilecache[b'_phasecache']
Yuya Nishihara
commandserver: preload repository in master server and reuse its file cache...
r41035 ce.obj.opener = destrepo.svfs