Debian Codegraph @

contrib/pull_logger.py

raw
1 # pull_logger.py - Logs pulls to a JSON-line file in the repo's VFS.
2 #
3 # Copyright 2022 Pacien TRAN-GIRARD <[email protected]>
4 #
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
7
8
9 '''logs pull parameters to a file
10
11 This extension logs the pull parameters, i.e. the remote and common heads,
12 when pulling from the local repository.
13
14 The collected data should give an idea of the state of a pair of repositories
15 and allow replaying past synchronisations between them. This is particularly
16 useful for working on data exchange, bundling and caching-related
17 optimisations.
18
19 The record is a JSON-line file located in the repository's VFS at
20 .hg/pull_log.jsonl.
21
22 Log write failures are not considered fatal: log writes may be skipped for any
23 reason such as insufficient storage or a timeout.
24
25 Some basic log file rotation can be enabled by setting 'rotate-size' to a value
26 greater than 0. This causes the current log file to be moved to
27 .hg/pull_log.jsonl.rotated when this threshold is met, discarding any
28 previously rotated log file.
29
30 The timeouts of the exclusive lock used when writing to the lock file can be
31 configured through the 'timeout.lock' and 'timeout.warn' options of this
32 plugin. Those are not expected to be held for a significant time in practice.::
33
34 [pull-logger]
35 timeout.lock = 300
36 timeout.warn = 100
37 rotate-size = 1kb
38 '''
39
40
41 import json
42 import time
43
44 from mercurial.i18n import _
45 from mercurial.utils import stringutil
46 from mercurial import (
47 error,
48 extensions,
49 lock,
50 registrar,
51 wireprotov1server,
52 )
53
54 EXT_NAME = b'pull-logger'
55 EXT_VERSION_CODE = 0
56
57 LOG_FILE = b'pull_log.jsonl'
58 OLD_LOG_FILE = LOG_FILE + b'.rotated'
59 LOCK_NAME = LOG_FILE + b'.lock'
60
61 configtable = {}
62 configitem = registrar.configitem(configtable)
63 configitem(EXT_NAME, b'timeout.lock', default=600)
64 configitem(EXT_NAME, b'timeout.warn', default=120)
65 configitem(EXT_NAME, b'rotate-size', default=b'100MB')
66
67
68 def wrap_getbundle(orig, repo, proto, others, *args, **kwargs):
69 heads, common = extract_pull_heads(others)
70 log_entry = {
71 'timestamp': time.time(),
72 'logger_version': EXT_VERSION_CODE,
73 'heads': sorted(heads),
74 'common': sorted(common),
75 }
76
77 try:
78 write_to_log(repo, log_entry)
79 except (OSError, error.LockError) as err:
80 msg = stringutil.forcebytestr(err)
81 repo.ui.warn(_(b'unable to append to pull log: %s\n') % msg)
82
83 return orig(repo, proto, others, *args, **kwargs)
84
85
86 def extract_pull_heads(bundle_args):
87 opts = wireprotov1server.options(
88 b'getbundle',
89 wireprotov1server.wireprototypes.GETBUNDLE_ARGUMENTS.keys(),
90 bundle_args.copy(), # this call consumes the args destructively
91 )
92
93 heads = opts.get(b'heads', b'').decode('utf-8').split(' ')
94 common = opts.get(b'common', b'').decode('utf-8').split(' ')
95 return (heads, common)
96
97
98 def write_to_log(repo, entry):
99 locktimeout = repo.ui.configint(EXT_NAME, b'timeout.lock')
100 lockwarntimeout = repo.ui.configint(EXT_NAME, b'timeout.warn')
101 rotatesize = repo.ui.configbytes(EXT_NAME, b'rotate-size')
102
103 with lock.trylock(
104 ui=repo.ui,
105 vfs=repo.vfs,
106 lockname=LOCK_NAME,
107 timeout=locktimeout,
108 warntimeout=lockwarntimeout,
109 ):
110 if rotatesize > 0 and repo.vfs.exists(LOG_FILE):
111 if repo.vfs.stat(LOG_FILE).st_size >= rotatesize:
112 repo.vfs.rename(LOG_FILE, OLD_LOG_FILE)
113
114 with repo.vfs.open(LOG_FILE, b'a+') as logfile:
115 serialised = json.dumps(entry, sort_keys=True)
116 logfile.write(serialised.encode('utf-8'))
117 logfile.write(b'\n')
118 logfile.flush()
119
120
121 def reposetup(ui, repo):
122 if repo.local():
123 repo._wlockfreeprefix.add(LOG_FILE)
124 repo._wlockfreeprefix.add(OLD_LOG_FILE)
125
126
127 def uisetup(ui):
128 del wireprotov1server.commands[b'getbundle']
129 decorator = wireprotov1server.wireprotocommand(
130 name=b'getbundle',
131 args=b'*',
132 permission=b'pull',
133 )
134
135 extensions.wrapfunction(
136 container=wireprotov1server,
137 funcname='getbundle',
138 wrapper=wrap_getbundle,
139 )
140
141 decorator(wireprotov1server.getbundle)