2002-11-19 04:09:52 -04:00
|
|
|
#------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Copyright (C) 2000 Autonomous Zone Industries
|
|
|
|
#
|
|
|
|
# License: This is free software. You may use this software for any
|
|
|
|
# purpose including modification/redistribution, so long as
|
|
|
|
# this header remains intact and that you do not claim any
|
|
|
|
# rights of ownership or authorship of this software. This
|
|
|
|
# software has been tested, but no warranty is expressed or
|
|
|
|
# implied.
|
|
|
|
#
|
2007-09-09 17:25:00 -03:00
|
|
|
# Author: Gregory P. Smith <greg@krypto.org>
|
2002-11-19 04:09:52 -04:00
|
|
|
#
|
|
|
|
# Note: I don't know how useful this is in reality since when a
|
2002-11-23 07:26:07 -04:00
|
|
|
# DBLockDeadlockError happens the current transaction is supposed to be
|
2002-11-19 04:09:52 -04:00
|
|
|
# aborted. If it doesn't then when the operation is attempted again
|
|
|
|
# the deadlock is still happening...
|
|
|
|
# --Robin
|
|
|
|
#
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# import the time.sleep function in a namespace safe way to allow
|
2006-06-15 05:52:32 -03:00
|
|
|
# "from bsddb.dbutils import *"
|
2002-11-19 04:09:52 -04:00
|
|
|
#
|
2002-12-30 16:53:52 -04:00
|
|
|
from time import sleep as _sleep
|
2002-11-19 04:09:52 -04:00
|
|
|
|
2008-07-23 08:38:42 -03:00
|
|
|
import sys
|
|
|
|
absolute_import = (sys.version_info[0] >= 3)
|
|
|
|
if absolute_import :
|
|
|
|
# Because this syntaxis is not valid before Python 2.5
|
|
|
|
exec("from . import db")
|
|
|
|
else :
|
|
|
|
import db
|
2002-11-19 04:09:52 -04:00
|
|
|
|
2002-12-30 16:53:52 -04:00
|
|
|
# always sleep at least N seconds between retrys
|
2006-06-15 05:52:32 -03:00
|
|
|
_deadlock_MinSleepTime = 1.0/128
|
2002-12-30 16:53:52 -04:00
|
|
|
# never sleep more than N seconds between retrys
|
|
|
|
_deadlock_MaxSleepTime = 3.14159
|
|
|
|
|
|
|
|
# Assign a file object to this for a "sleeping" message to be written to it
|
|
|
|
# each retry
|
|
|
|
_deadlock_VerboseFile = None
|
2002-11-19 04:09:52 -04:00
|
|
|
|
|
|
|
|
|
|
|
def DeadlockWrap(function, *_args, **_kwargs):
|
|
|
|
"""DeadlockWrap(function, *_args, **_kwargs) - automatically retries
|
|
|
|
function in case of a database deadlock.
|
|
|
|
|
2002-11-23 07:26:07 -04:00
|
|
|
This is a function intended to be used to wrap database calls such
|
|
|
|
that they perform retrys with exponentially backing off sleeps in
|
|
|
|
between when a DBLockDeadlockError exception is raised.
|
|
|
|
|
|
|
|
A 'max_retries' parameter may optionally be passed to prevent it
|
|
|
|
from retrying forever (in which case the exception will be reraised).
|
2002-11-19 04:09:52 -04:00
|
|
|
|
|
|
|
d = DB(...)
|
|
|
|
d.open(...)
|
|
|
|
DeadlockWrap(d.put, "foo", data="bar") # set key "foo" to "bar"
|
|
|
|
"""
|
|
|
|
sleeptime = _deadlock_MinSleepTime
|
2002-11-23 07:26:07 -04:00
|
|
|
max_retries = _kwargs.get('max_retries', -1)
|
Merged revisions 75407,75409-75413,75415,75419-75421 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r75407 | antoine.pitrou | 2009-10-14 20:30:52 +0300 (Wed, 14 Oct 2009) | 3 lines
Fix py3k warnings in the aifc module
........
r75409 | antoine.pitrou | 2009-10-14 21:01:33 +0300 (Wed, 14 Oct 2009) | 3 lines
Fix py3k warnings in bsddb
........
r75410 | antoine.pitrou | 2009-10-14 21:09:45 +0300 (Wed, 14 Oct 2009) | 3 lines
Silence a py3k warning claiming to affect Lib/calendar.py
........
r75411 | antoine.pitrou | 2009-10-14 21:12:54 +0300 (Wed, 14 Oct 2009) | 3 lines
Fix a py3k warning in the StringIO module (exhibited in test_codecencodings_cn)
........
r75412 | antoine.pitrou | 2009-10-14 21:27:32 +0300 (Wed, 14 Oct 2009) | 3 lines
Fix py3k warnings in the socket module
........
r75413 | antoine.pitrou | 2009-10-14 21:31:05 +0300 (Wed, 14 Oct 2009) | 3 lines
Fix a py3k warning in the sndhdr module (found with test_email)
........
r75415 | antoine.pitrou | 2009-10-14 21:39:46 +0300 (Wed, 14 Oct 2009) | 3 lines
Silence some py3k warnings claiming to affect _pyio
........
r75419 | antoine.pitrou | 2009-10-14 21:56:11 +0300 (Wed, 14 Oct 2009) | 3 lines
Silence py3k warning claiming to affect the random module
........
r75420 | antoine.pitrou | 2009-10-14 22:04:48 +0300 (Wed, 14 Oct 2009) | 3 lines
Fix py3k warnings in httplib
........
r75421 | antoine.pitrou | 2009-10-14 22:09:48 +0300 (Wed, 14 Oct 2009) | 3 lines
Fix py3k warnings in the uuid module
........
2010-08-03 00:19:00 -03:00
|
|
|
if 'max_retries' in _kwargs:
|
2002-11-23 07:26:07 -04:00
|
|
|
del _kwargs['max_retries']
|
2006-06-15 05:52:32 -03:00
|
|
|
while True:
|
2002-11-19 04:09:52 -04:00
|
|
|
try:
|
2002-12-30 16:53:52 -04:00
|
|
|
return function(*_args, **_kwargs)
|
2003-01-28 13:20:44 -04:00
|
|
|
except db.DBLockDeadlockError:
|
2002-11-23 07:26:07 -04:00
|
|
|
if _deadlock_VerboseFile:
|
2002-12-30 16:53:52 -04:00
|
|
|
_deadlock_VerboseFile.write(
|
|
|
|
'dbutils.DeadlockWrap: sleeping %1.3f\n' % sleeptime)
|
2002-11-19 04:09:52 -04:00
|
|
|
_sleep(sleeptime)
|
|
|
|
# exponential backoff in the sleep time
|
2002-12-30 16:53:52 -04:00
|
|
|
sleeptime *= 2
|
|
|
|
if sleeptime > _deadlock_MaxSleepTime:
|
2002-11-19 04:09:52 -04:00
|
|
|
sleeptime = _deadlock_MaxSleepTime
|
2002-12-30 16:53:52 -04:00
|
|
|
max_retries -= 1
|
2002-11-23 07:26:07 -04:00
|
|
|
if max_retries == -1:
|
|
|
|
raise
|
2002-11-19 04:09:52 -04:00
|
|
|
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------
|