blob: 3568b44a82b09f28d3425053bc7426ff199ffa66 [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#
12# Author: Gregory P. Smith <greg@electricrain.com>
13#
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
22
23#
24# import the time.sleep function in a namespace safe way to allow
Barry Warsaw9a0d7792002-12-30 20:53:52 +000025# "from bsddb.db import *"
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +000026#
Barry Warsaw9a0d7792002-12-30 20:53:52 +000027from time import sleep as _sleep
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +000028
Barry Warsawf71de3e2003-01-28 17:20:44 +000029try:
30 # For Python 2.3
31 from bsddb import db
32except ImportError:
33 # For earlier Pythons w/distutils pybsddb
34 from bsddb3 import db
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +000035
Barry Warsaw9a0d7792002-12-30 20:53:52 +000036# always sleep at least N seconds between retrys
37_deadlock_MinSleepTime = 1.0/64
38# never sleep more than N seconds between retrys
39_deadlock_MaxSleepTime = 3.14159
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +000040
Barry Warsaw9a0d7792002-12-30 20:53:52 +000041# Assign a file object to this for a "sleeping" message to be written to it
42# each retry
43_deadlock_VerboseFile = None
44
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +000045
46def DeadlockWrap(function, *_args, **_kwargs):
47 """DeadlockWrap(function, *_args, **_kwargs) - automatically retries
48 function in case of a database deadlock.
49
Martin v. Löwisb2c7aff2002-11-23 11:26:07 +000050 This is a function intended to be used to wrap database calls such
51 that they perform retrys with exponentially backing off sleeps in
52 between when a DBLockDeadlockError exception is raised.
53
54 A 'max_retries' parameter may optionally be passed to prevent it
55 from retrying forever (in which case the exception will be reraised).
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +000056
57 d = DB(...)
58 d.open(...)
59 DeadlockWrap(d.put, "foo", data="bar") # set key "foo" to "bar"
60 """
61 sleeptime = _deadlock_MinSleepTime
Martin v. Löwisb2c7aff2002-11-23 11:26:07 +000062 max_retries = _kwargs.get('max_retries', -1)
63 if _kwargs.has_key('max_retries'):
64 del _kwargs['max_retries']
65 while 1:
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +000066 try:
Barry Warsaw9a0d7792002-12-30 20:53:52 +000067 return function(*_args, **_kwargs)
Barry Warsawf71de3e2003-01-28 17:20:44 +000068 except db.DBLockDeadlockError:
Martin v. Löwisb2c7aff2002-11-23 11:26:07 +000069 if _deadlock_VerboseFile:
Barry Warsaw9a0d7792002-12-30 20:53:52 +000070 _deadlock_VerboseFile.write(
71 'dbutils.DeadlockWrap: sleeping %1.3f\n' % sleeptime)
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +000072 _sleep(sleeptime)
73 # exponential backoff in the sleep time
Barry Warsaw9a0d7792002-12-30 20:53:52 +000074 sleeptime *= 2
75 if sleeptime > _deadlock_MaxSleepTime:
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +000076 sleeptime = _deadlock_MaxSleepTime
Barry Warsaw9a0d7792002-12-30 20:53:52 +000077 max_retries -= 1
Martin v. Löwisb2c7aff2002-11-23 11:26:07 +000078 if max_retries == -1:
79 raise
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +000080
81
82#------------------------------------------------------------------------