blob: 94641edd72e2f49c3b2b2a1edb0684f97670c7c8 [file] [log] [blame]
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +00001#------------------------------------------------------------------------
2#
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +00003# Copyright (C) 2000 Autonomous Zone Industries
4#
5# License: This is free software. You may use this software for any
6# purpose including modification/redistribution, so long as
7# this header remains intact and that you do not claim any
8# rights of ownership or authorship of this software. This
9# software has been tested, but no warranty is expressed or
10# implied.
11#
Gregory P. Smith3fd22da2007-08-28 08:05:56 +000012# Author: Gregory P. Smith <greg@krypto.org>
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +000013#
14# Note: I don't know how useful this is in reality since when a
Martin v. Löwisb2c7aff2002-11-23 11:26:07 +000015# DBLockDeadlockError happens the current transaction is supposed to be
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +000016# aborted. If it doesn't then when the operation is attempted again
17# the deadlock is still happening...
18# --Robin
19#
20#------------------------------------------------------------------------
21
Gregory P. Smith3fd22da2007-08-28 08:05:56 +000022import time
Guido van Rossumcde2ead2006-08-17 23:29:08 +000023from . import db
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +000024
Barry Warsaw9a0d7792002-12-30 20:53:52 +000025# always sleep at least N seconds between retrys
Thomas Wouters0e3f5912006-08-11 14:57:12 +000026_deadlock_MinSleepTime = 1.0/128
Barry Warsaw9a0d7792002-12-30 20:53:52 +000027# never sleep more than N seconds between retrys
28_deadlock_MaxSleepTime = 3.14159
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +000029
Barry Warsaw9a0d7792002-12-30 20:53:52 +000030# Assign a file object to this for a "sleeping" message to be written to it
31# each retry
32_deadlock_VerboseFile = None
33
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +000034
35def DeadlockWrap(function, *_args, **_kwargs):
36 """DeadlockWrap(function, *_args, **_kwargs) - automatically retries
37 function in case of a database deadlock.
38
Martin v. Löwisb2c7aff2002-11-23 11:26:07 +000039 This is a function intended to be used to wrap database calls such
40 that they perform retrys with exponentially backing off sleeps in
41 between when a DBLockDeadlockError exception is raised.
42
43 A 'max_retries' parameter may optionally be passed to prevent it
44 from retrying forever (in which case the exception will be reraised).
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +000045
46 d = DB(...)
47 d.open(...)
48 DeadlockWrap(d.put, "foo", data="bar") # set key "foo" to "bar"
49 """
50 sleeptime = _deadlock_MinSleepTime
Martin v. Löwisb2c7aff2002-11-23 11:26:07 +000051 max_retries = _kwargs.get('max_retries', -1)
Guido van Rossum20435132006-08-21 00:21:47 +000052 if 'max_retries' in _kwargs:
Martin v. Löwisb2c7aff2002-11-23 11:26:07 +000053 del _kwargs['max_retries']
Thomas Wouters0e3f5912006-08-11 14:57:12 +000054 while True:
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +000055 try:
Barry Warsaw9a0d7792002-12-30 20:53:52 +000056 return function(*_args, **_kwargs)
Gregory P. Smith3fd22da2007-08-28 08:05:56 +000057 except db.DBLockDeadlockError as e:
Martin v. Löwisb2c7aff2002-11-23 11:26:07 +000058 if _deadlock_VerboseFile:
Barry Warsaw9a0d7792002-12-30 20:53:52 +000059 _deadlock_VerboseFile.write(
Gregory P. Smith3fd22da2007-08-28 08:05:56 +000060 'bsddb.dbutils.DeadlockWrap: ' +
61 'sleeping %1.3f\n' % sleeptime)
62 time.sleep(sleeptime)
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +000063 # exponential backoff in the sleep time
Barry Warsaw9a0d7792002-12-30 20:53:52 +000064 sleeptime *= 2
65 if sleeptime > _deadlock_MaxSleepTime:
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +000066 sleeptime = _deadlock_MaxSleepTime
Barry Warsaw9a0d7792002-12-30 20:53:52 +000067 max_retries -= 1
Martin v. Löwisb2c7aff2002-11-23 11:26:07 +000068 if max_retries == -1:
Gregory P. Smith3fd22da2007-08-28 08:05:56 +000069 if _deadlock_VerboseFile:
70 _deadlock_VerboseFile.write(
71 'bsddb.dbutils.DeadlockWrap: ' +
72 'max_retries reached, reraising %s\n' % e)
Martin v. Löwisb2c7aff2002-11-23 11:26:07 +000073 raise
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +000074
75
76#------------------------------------------------------------------------