annotate mercurial/dirstateguard.py @ 39559:07b58266bce3

wireprotov2: implement commands as a generator of objects Previously, wire protocol version 2 inherited version 1's model of having separate types to represent the results of different wire protocol commands. As I implemented more powerful commands in future commits, I found I was using a common pattern of returning a special type to hold a generator. This meant the command function required a closure to do most of the work. That made logic flow more difficult to follow. I also noticed that many commands were effectively a sequence of objects to be CBOR encoded. I think it makes sense to define version 2 commands as generators. This way, commands can simply emit the data structures they wish to send to the client. This eliminates the need for a closure in command functions and removes encoding from the bodies of commands. As part of this commit, the handling of response objects has been moved into the serverreactor class. This puts the reactor in the driver's seat with regards to CBOR encoding and error handling. Having error handling in the function that emits frames is particularly important because exceptions in that function can lead to things getting in a bad state: I'm fairly certain that uncaught exceptions in the frame generator were causing deadlocks. I also introduced a dedicated error type for explicit error reporting in command handlers. This will be used in subsequent commits. There's still a bit of work to be done here, especially around formalizing the error handling "protocol." I've added yet another TODO to track this so we don't forget. Test output changed because we're using generators and no longer know we are at the end of the data until we hit the end of the generator. This means we can't emit the end-of-stream flag until we've exhausted the generator. Hence the introduction of 0-sized end-of-stream frames. Differential Revision: https://phab.mercurial-scm.org/D4472
author Gregory Szorc <gregory.szorc@gmail.com>
date Wed, 05 Sep 2018 09:06:40 -0700
parents ad24b581e4d9
children b74481038438
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
30488
751639bf6fc4 dirstateguard: move to new module so I can break some layering violations
Augie Fackler <augie@google.com>
parents: 30473
diff changeset
1 # dirstateguard.py - class to allow restoring dirstate after failure
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
2 #
4635
63b9d2deed48 Updated copyright notices and add "and others" to "hg version"
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4633
diff changeset
3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
4 #
8225
46293a0c7e9f updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents: 8210
diff changeset
5 # This software may be used and distributed according to the terms of the
10263
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 10249
diff changeset
6 # GNU General Public License version 2 or any later version.
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
7
28322
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
8 from __future__ import absolute_import
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
9
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
10 from .i18n import _
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
11
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
12 from . import (
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
13 error,
38869
ad24b581e4d9 narrow: call narrowspec.{save,restore,clear}backup directly
Martin von Zweigbergk <martinvonz@google.com>
parents: 33793
diff changeset
14 narrowspec,
33793
bbbbd3c30bfc util: add base class for transactional context managers
Martin von Zweigbergk <martinvonz@google.com>
parents: 33619
diff changeset
15 util,
28322
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
16 )
27624
d46db4390715 resolve: suggest the next action
timeless <timeless@mozdev.org>
parents: 27622
diff changeset
17
33793
bbbbd3c30bfc util: add base class for transactional context managers
Martin von Zweigbergk <martinvonz@google.com>
parents: 33619
diff changeset
18 class dirstateguard(util.transactional):
24991
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
19 '''Restore dirstate at unexpected failure.
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
20
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
21 At the construction, this class does:
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
22
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
23 - write current ``repo.dirstate`` out, and
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
24 - save ``.hg/dirstate`` into the backup file
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
25
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
26 This restores ``.hg/dirstate`` from backup file, if ``release()``
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
27 is invoked before ``close()``.
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
28
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
29 This just removes the backup file at ``close()`` before ``release()``.
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
30 '''
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
31
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
32 def __init__(self, repo, name):
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
33 self._repo = repo
30182
144d8fe266d9 cmdutil: satisfy expections in dirstateguard.__del__, even if __init__ fails
Mads Kiilerich <madski@unity3d.com>
parents: 30151
diff changeset
34 self._active = False
144d8fe266d9 cmdutil: satisfy expections in dirstateguard.__del__, even if __init__ fails
Mads Kiilerich <madski@unity3d.com>
parents: 30151
diff changeset
35 self._closed = False
33440
ec306bc6915b dirstate: update backup functions to take full backup filename
Adam Simpkins <simpkins@fb.com>
parents: 30488
diff changeset
36 self._backupname = 'dirstate.backup.%s.%d' % (name, id(self))
38869
ad24b581e4d9 narrow: call narrowspec.{save,restore,clear}backup directly
Martin von Zweigbergk <martinvonz@google.com>
parents: 33793
diff changeset
37 self._narrowspecbackupname = ('narrowspec.backup.%s.%d' %
ad24b581e4d9 narrow: call narrowspec.{save,restore,clear}backup directly
Martin von Zweigbergk <martinvonz@google.com>
parents: 33793
diff changeset
38 (name, id(self)))
33440
ec306bc6915b dirstate: update backup functions to take full backup filename
Adam Simpkins <simpkins@fb.com>
parents: 30488
diff changeset
39 repo.dirstate.savebackup(repo.currenttransaction(), self._backupname)
38869
ad24b581e4d9 narrow: call narrowspec.{save,restore,clear}backup directly
Martin von Zweigbergk <martinvonz@google.com>
parents: 33793
diff changeset
40 narrowspec.savebackup(repo, self._narrowspecbackupname)
24991
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
41 self._active = True
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
42
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
43 def __del__(self):
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
44 if self._active: # still active
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
45 # this may occur, even if this class is used correctly:
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
46 # for example, releasing other resources like transaction
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
47 # may raise exception before ``dirstateguard.release`` in
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
48 # ``release(tr, ....)``.
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
49 self._abort()
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
50
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
51 def close(self):
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
52 if not self._active: # already inactivated
33440
ec306bc6915b dirstate: update backup functions to take full backup filename
Adam Simpkins <simpkins@fb.com>
parents: 30488
diff changeset
53 msg = (_("can't close already inactivated backup: %s")
ec306bc6915b dirstate: update backup functions to take full backup filename
Adam Simpkins <simpkins@fb.com>
parents: 30488
diff changeset
54 % self._backupname)
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26579
diff changeset
55 raise error.Abort(msg)
24991
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
56
29137
d115cbf5980b dirstate: make backup methods public
Mateusz Kwapich <mitrandir@fb.com>
parents: 29103
diff changeset
57 self._repo.dirstate.clearbackup(self._repo.currenttransaction(),
33440
ec306bc6915b dirstate: update backup functions to take full backup filename
Adam Simpkins <simpkins@fb.com>
parents: 30488
diff changeset
58 self._backupname)
38869
ad24b581e4d9 narrow: call narrowspec.{save,restore,clear}backup directly
Martin von Zweigbergk <martinvonz@google.com>
parents: 33793
diff changeset
59 narrowspec.clearbackup(self._repo, self._narrowspecbackupname)
24991
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
60 self._active = False
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
61 self._closed = True
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
62
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
63 def _abort(self):
38869
ad24b581e4d9 narrow: call narrowspec.{save,restore,clear}backup directly
Martin von Zweigbergk <martinvonz@google.com>
parents: 33793
diff changeset
64 narrowspec.restorebackup(self._repo, self._narrowspecbackupname)
29137
d115cbf5980b dirstate: make backup methods public
Mateusz Kwapich <mitrandir@fb.com>
parents: 29103
diff changeset
65 self._repo.dirstate.restorebackup(self._repo.currenttransaction(),
33440
ec306bc6915b dirstate: update backup functions to take full backup filename
Adam Simpkins <simpkins@fb.com>
parents: 30488
diff changeset
66 self._backupname)
24991
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
67 self._active = False
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
68
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
69 def release(self):
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
70 if not self._closed:
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
71 if not self._active: # already inactivated
33440
ec306bc6915b dirstate: update backup functions to take full backup filename
Adam Simpkins <simpkins@fb.com>
parents: 30488
diff changeset
72 msg = (_("can't release already inactivated backup: %s")
ec306bc6915b dirstate: update backup functions to take full backup filename
Adam Simpkins <simpkins@fb.com>
parents: 30488
diff changeset
73 % self._backupname)
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26579
diff changeset
74 raise error.Abort(msg)
24991
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
75 self._abort()