]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - docs/logging.rst
4672e8f90bade5f3776f53383d9d8249b1c40a39
[tahoe-lafs/tahoe-lafs.git] / docs / logging.rst
1 .. -*- coding: utf-8-with-signature -*-
2
3 =============
4 Tahoe Logging
5 =============
6
7 1.  `Overview`_
8 2.  `Realtime Logging`_
9 3.  `Incidents`_
10 4.  `Working with flogfiles`_
11 5.  `Gatherers`_
12
13     1.  `Incident Gatherer`_
14     2.  `Log Gatherer`_
15
16 6.  `Local twistd.log files`_
17 7.  `Adding log messages`_
18 8.  `Log Messages During Unit Tests`_
19
20 Overview
21 ========
22
23 Tahoe uses the Foolscap logging mechanism (known as the "flog" subsystem) to
24 record information about what is happening inside the Tahoe node. This is
25 primarily for use by programmers and grid operators who want to find out what
26 went wrong.
27
28 The Foolscap logging system is documented at
29 `<http://foolscap.lothar.com/docs/logging.html>`__.
30
31 The Foolscap distribution includes a utility named "``flogtool``" that is
32 used to get access to many Foolscap logging features. This command only
33 works when foolscap and its dependencies are installed correctly.
34 Tahoe-LAFS v1.10.0 and later include a ``tahoe debug flogtool`` command
35 that can be used even when foolscap is not installed; to use this, prefix
36 all of the example commands below with ``tahoe debug``.
37
38 For earlier versions since Tahoe-LAFS v1.8.2, installing Foolscap v0.6.1
39 or later and then running ``bin/tahoe @flogtool`` from the root of a
40 Tahoe-LAFS source distribution may work (but only on Unix, not Windows).
41
42
43 Realtime Logging
44 ================
45
46 When you are working on Tahoe code, and want to see what the node is doing,
47 the easiest tool to use is "``flogtool tail``". This connects to the Tahoe
48 node and subscribes to hear about all log events. These events are then
49 displayed to stdout, and optionally saved to a file.
50
51 "``flogtool tail``" connects to the "logport", for which the FURL is stored
52 in ``BASEDIR/private/logport.furl`` . The following command will connect to
53 this port and start emitting log information::
54
55   flogtool tail BASEDIR/private/logport.furl
56
57 The ``--save-to FILENAME`` option will save all received events to a file,
58 where then can be examined later with "``flogtool dump``" or "``flogtool
59 web-viewer``". The ``--catch-up`` option will ask the node to dump all stored
60 events before subscribing to new ones (without ``--catch-up``, you will only
61 hear about events that occur after the tool has connected and subscribed).
62
63 Incidents
64 =========
65
66 Foolscap keeps a short list of recent events in memory. When something goes
67 wrong, it writes all the history it has (and everything that gets logged in
68 the next few seconds) into a file called an "incident". These files go into
69 ``BASEDIR/logs/incidents/`` , in a file named
70 "``incident-TIMESTAMP-UNIQUE.flog.bz2``". The default definition of
71 "something goes wrong" is the generation of a log event at the ``log.WEIRD``
72 level or higher, but other criteria could be implemented.
73
74 The typical "incident report" we've seen in a large Tahoe grid is about 40kB
75 compressed, representing about 1800 recent events.
76
77 These "flogfiles" have a similar format to the files saved by "``flogtool
78 tail --save-to``". They are simply lists of log events, with a small header
79 to indicate which event triggered the incident.
80
81 The "``flogtool dump FLOGFILE``" command will take one of these ``.flog.bz2``
82 files and print their contents to stdout, one line per event. The raw event
83 dictionaries can be dumped by using "``flogtool dump --verbose FLOGFILE``".
84
85 The "``flogtool web-viewer``" command can be used to examine the flogfile in
86 a web browser. It runs a small HTTP server and emits the URL on stdout.  This
87 view provides more structure than the output of "``flogtool dump``": the
88 parent/child relationships of log events is displayed in a nested format.
89 "``flogtool web-viewer``" is still fairly immature.
90
91 Working with flogfiles
92 ======================
93
94 The "``flogtool filter``" command can be used to take a large flogfile
95 (perhaps one created by the log-gatherer, see below) and copy a subset of
96 events into a second file. This smaller flogfile may be easier to work with
97 than the original. The arguments to "``flogtool filter``" specify filtering
98 criteria: a predicate that each event must match to be copied into the target
99 file. ``--before`` and ``--after`` are used to exclude events outside a given
100 window of time. ``--above`` will retain events above a certain severity
101 level. ``--from`` retains events send by a specific tubid.
102 ``--strip-facility`` removes events that were emitted with a given facility
103 (like ``foolscap.negotiation`` or ``tahoe.upload``).
104
105 Gatherers
106 =========
107
108 In a deployed Tahoe grid, it is useful to get log information automatically
109 transferred to a central log-gatherer host. This offloads the (admittedly
110 modest) storage requirements to a different host and provides access to
111 logfiles from multiple nodes (web-API, storage, or helper) in a single place.
112
113 There are two kinds of gatherers: "log gatherer" and "stats gatherer". Each
114 produces a FURL which needs to be placed in the ``NODEDIR/tahoe.cfg`` file of
115 each node that is to publish to the gatherer, under the keys
116 "log_gatherer.furl" and "stats_gatherer.furl" respectively. When the Tahoe
117 node starts, it will connect to the configured gatherers and offer its
118 logport: the gatherer will then use the logport to subscribe to hear about
119 events.
120
121 The gatherer will write to files in its working directory, which can then be
122 examined with tools like "``flogtool dump``" as described above.
123
124 Incident Gatherer
125 -----------------
126
127 The "incident gatherer" only collects Incidents: records of the log events
128 that occurred just before and slightly after some high-level "trigger event"
129 was recorded. Each incident is classified into a "category": a short string
130 that summarizes what sort of problem took place. These classification
131 functions are written after examining a new/unknown incident. The idea is to
132 recognize when the same problem is happening multiple times.
133
134 A collection of classification functions that are useful for Tahoe nodes are
135 provided in ``misc/incident-gatherer/support_classifiers.py`` . There is
136 roughly one category for each ``log.WEIRD``-or-higher level event in the
137 Tahoe source code.
138
139 The incident gatherer is created with the "``flogtool
140 create-incident-gatherer WORKDIR``" command, and started with "``tahoe
141 start``". The generated "``gatherer.tac``" file should be modified to add
142 classifier functions.
143
144 The incident gatherer writes incident names (which are simply the relative
145 pathname of the ``incident-\*.flog.bz2`` file) into ``classified/CATEGORY``.
146 For example, the ``classified/mutable-retrieve-uncoordinated-write-error``
147 file contains a list of all incidents which were triggered by an
148 uncoordinated write that was detected during mutable file retrieval (caused
149 when somebody changed the contents of the mutable file in between the node's
150 mapupdate step and the retrieve step). The ``classified/unknown`` file
151 contains a list of all incidents that did not match any of the classification
152 functions.
153
154 At startup, the incident gatherer will automatically reclassify any incident
155 report which is not mentioned in any of the ``classified/\*`` files. So the
156 usual workflow is to examine the incidents in ``classified/unknown``, add a
157 new classification function, delete ``classified/unknown``, then bound the
158 gatherer with "``tahoe restart WORKDIR``". The incidents which can be
159 classified with the new functions will be added to their own
160 ``classified/FOO`` lists, and the remaining ones will be put in
161 ``classified/unknown``, where the process can be repeated until all events
162 are classifiable.
163
164 The incident gatherer is still fairly immature: future versions will have a
165 web interface and an RSS feed, so operations personnel can track problems in
166 the storage grid.
167
168 In our experience, each incident takes about two seconds to transfer from the
169 node that generated it to the gatherer. The gatherer will automatically catch
170 up to any incidents which occurred while it is offline.
171
172 Log Gatherer
173 ------------
174
175 The "Log Gatherer" subscribes to hear about every single event published by
176 the connected nodes, regardless of severity. This server writes these log
177 events into a large flogfile that is rotated (closed, compressed, and
178 replaced with a new one) on a periodic basis. Each flogfile is named
179 according to the range of time it represents, with names like
180 "``from-2008-08-26-132256--to-2008-08-26-162256.flog.bz2``". The flogfiles
181 contain events from many different sources, making it easier to correlate
182 things that happened on multiple machines (such as comparing a client node
183 making a request with the storage servers that respond to that request).
184
185 Create the Log Gatherer with the "``flogtool create-gatherer WORKDIR``"
186 command, and start it with "``tahoe start``". Then copy the contents of the
187 ``log_gatherer.furl`` file it creates into the ``BASEDIR/tahoe.cfg`` file
188 (under the key ``log_gatherer.furl`` of the section ``[node]``) of all nodes
189 that should be sending it log events. (See `<configuration.rst>`__.)
190
191 The "``flogtool filter``" command, described above, is useful to cut down the
192 potentially large flogfiles into a more focussed form.
193
194 Busy nodes, particularly web-API nodes which are performing recursive
195 deep-size/deep-stats/deep-check operations, can produce a lot of log events.
196 To avoid overwhelming the node (and using an unbounded amount of memory for
197 the outbound TCP queue), publishing nodes will start dropping log events when
198 the outbound queue grows too large. When this occurs, there will be gaps
199 (non-sequential event numbers) in the log-gatherer's flogfiles.
200
201 Local twistd.log files
202 ======================
203
204 [TODO: not yet true, requires foolscap-0.3.1 and a change to ``allmydata.node``]
205
206 In addition to the foolscap-based event logs, certain high-level events will
207 be recorded directly in human-readable text form, in the
208 ``BASEDIR/logs/twistd.log`` file (and its rotated old versions:
209 ``twistd.log.1``, ``twistd.log.2``, etc). This form does not contain as much
210 information as the flogfiles available through the means described
211 previously, but they are immediately available to the curious developer, and
212 are retained until the twistd.log.NN files are explicitly deleted.
213
214 Only events at the ``log.OPERATIONAL`` level or higher are bridged to
215 ``twistd.log`` (i.e. not the ``log.NOISY`` debugging events). In addition,
216 foolscap internal events (like connection negotiation messages) are not
217 bridged to ``twistd.log``.
218
219 Adding log messages
220 ===================
221
222 When adding new code, the Tahoe developer should add a reasonable number of
223 new log events. For details, please see the Foolscap logging documentation,
224 but a few notes are worth stating here:
225
226 * use a facility prefix of "``tahoe.``", like "``tahoe.mutable.publish``"
227
228 * assign each severe (``log.WEIRD`` or higher) event a unique message
229   identifier, as the ``umid=`` argument to the ``log.msg()`` call. The
230   ``misc/coding_tools/make_umid`` script may be useful for this purpose.
231   This will make it easier to write a classification function for these
232   messages.
233
234 * use the ``parent=`` argument whenever the event is causally/temporally
235   clustered with its parent. For example, a download process that involves
236   three sequential hash fetches could announce the send and receipt of those
237   hash-fetch messages with a ``parent=`` argument that ties them to the
238   overall download process. However, each new web-API download request should
239   be unparented.
240
241 * use the ``format=`` argument in preference to the ``message=`` argument.
242   E.g. use ``log.msg(format="got %(n)d shares, need %(k)d", n=n, k=k)``
243   instead of ``log.msg("got %d shares, need %d" % (n,k))``. This will allow
244   later tools to analyze the event without needing to scrape/reconstruct the
245   structured data out of the formatted string.
246
247 * Pass extra information as extra keyword arguments, even if they aren't
248   included in the ``format=`` string. This information will be displayed in
249   the "``flogtool dump --verbose``" output, as well as being available to
250   other tools. The ``umid=`` argument should be passed this way.
251
252 * use ``log.err`` for the catch-all ``addErrback`` that gets attached to the
253   end of any given Deferred chain. When used in conjunction with
254   ``LOGTOTWISTED=1``, ``log.err()`` will tell Twisted about the error-nature
255   of the log message, causing Trial to flunk the test (with an "ERROR"
256   indication that prints a copy of the Failure, including a traceback).
257   Don't use ``log.err`` for events that are ``BAD`` but handled (like hash
258   failures: since these are often deliberately provoked by test code, they
259   should not cause test failures): use ``log.msg(level=BAD)`` for those
260   instead.
261
262
263 Log Messages During Unit Tests
264 ==============================
265
266 If a test is failing and you aren't sure why, start by enabling
267 ``FLOGTOTWISTED=1`` like this::
268
269   make test FLOGTOTWISTED=1
270
271 With ``FLOGTOTWISTED=1``, sufficiently-important log events will be written
272 into ``_trial_temp/test.log``, which may give you more ideas about why the
273 test is failing.
274
275 By default, ``_trial_temp/test.log`` will not receive messages below the
276 ``level=OPERATIONAL`` threshold. You can change the threshold via the ``FLOGLEVEL``
277 variable, e.g.::
278
279   make test FLOGLEVEL=10 FLOGTOTWISTED=1
280
281 (The level numbers are listed in src/allmydata/util/log.py.)
282
283 To look at the detailed foolscap logging messages, run the tests like this::
284
285   make test FLOGFILE=flog.out.bz2 FLOGLEVEL=1 FLOGTOTWISTED=1
286
287 The first environment variable will cause foolscap log events to be written
288 to ``./flog.out.bz2`` (instead of merely being recorded in the circular
289 buffers for the use of remote subscribers or incident reports). The second
290 will cause all log events to be written out, not just the higher-severity
291 ones. The third will cause twisted log events (like the markers that indicate
292 when each unit test is starting and stopping) to be copied into the flogfile,
293 making it easier to correlate log events with unit tests.
294
295 Enabling this form of logging appears to roughly double the runtime of the
296 unit tests. The ``flog.out.bz2`` file is approximately 2MB.
297
298 You can then use "``flogtool dump``" or "``flogtool web-viewer``" on the
299 resulting ``flog.out`` file.
300
301 ("``flogtool tail``" and the log-gatherer are not useful during unit tests,
302 since there is no single Tub to which all the log messages are published).
303
304 It is possible for setting these environment variables to cause spurious test
305 failures in tests with race condition bugs. All known instances of this have
306 been fixed as of Tahoe-LAFS v1.7.1.