Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1 | #------------------------------------------------------------------------ |
| 2 | # |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3 | # 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. Smith | f805785 | 2007-09-09 20:25:00 +0000 | [diff] [blame] | 12 | # Author: Gregory P. Smith <greg@krypto.org> |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 13 | # |
| 14 | # Note: I don't know how useful this is in reality since when a |
Martin v. Löwis | b2c7aff | 2002-11-23 11:26:07 +0000 | [diff] [blame] | 15 | # DBLockDeadlockError happens the current transaction is supposed to be |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 16 | # 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 |
Gregory P. Smith | 506f7b5 | 2006-06-15 08:52:32 +0000 | [diff] [blame] | 25 | # "from bsddb.dbutils import *" |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 26 | # |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 27 | from time import sleep as _sleep |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 28 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 29 | import sys |
| 30 | absolute_import = (sys.version_info[0] >= 3) |
| 31 | if absolute_import : |
| 32 | # Because this syntaxis is not valid before Python 2.5 |
| 33 | exec("from . import db") |
| 34 | else : |
| 35 | import db |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 36 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 37 | # always sleep at least N seconds between retrys |
Gregory P. Smith | 506f7b5 | 2006-06-15 08:52:32 +0000 | [diff] [blame] | 38 | _deadlock_MinSleepTime = 1.0/128 |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 39 | # never sleep more than N seconds between retrys |
| 40 | _deadlock_MaxSleepTime = 3.14159 |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 41 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 42 | # Assign a file object to this for a "sleeping" message to be written to it |
| 43 | # each retry |
| 44 | _deadlock_VerboseFile = None |
| 45 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 46 | |
| 47 | def DeadlockWrap(function, *_args, **_kwargs): |
| 48 | """DeadlockWrap(function, *_args, **_kwargs) - automatically retries |
| 49 | function in case of a database deadlock. |
| 50 | |
Martin v. Löwis | b2c7aff | 2002-11-23 11:26:07 +0000 | [diff] [blame] | 51 | This is a function intended to be used to wrap database calls such |
| 52 | that they perform retrys with exponentially backing off sleeps in |
| 53 | between when a DBLockDeadlockError exception is raised. |
| 54 | |
| 55 | A 'max_retries' parameter may optionally be passed to prevent it |
| 56 | from retrying forever (in which case the exception will be reraised). |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 57 | |
| 58 | d = DB(...) |
| 59 | d.open(...) |
| 60 | DeadlockWrap(d.put, "foo", data="bar") # set key "foo" to "bar" |
| 61 | """ |
| 62 | sleeptime = _deadlock_MinSleepTime |
Martin v. Löwis | b2c7aff | 2002-11-23 11:26:07 +0000 | [diff] [blame] | 63 | max_retries = _kwargs.get('max_retries', -1) |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 64 | if _kwargs.has_key('max_retries'): |
Martin v. Löwis | b2c7aff | 2002-11-23 11:26:07 +0000 | [diff] [blame] | 65 | del _kwargs['max_retries'] |
Gregory P. Smith | 506f7b5 | 2006-06-15 08:52:32 +0000 | [diff] [blame] | 66 | while True: |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 67 | try: |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 68 | return function(*_args, **_kwargs) |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 69 | except db.DBLockDeadlockError: |
Martin v. Löwis | b2c7aff | 2002-11-23 11:26:07 +0000 | [diff] [blame] | 70 | if _deadlock_VerboseFile: |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 71 | _deadlock_VerboseFile.write( |
| 72 | 'dbutils.DeadlockWrap: sleeping %1.3f\n' % sleeptime) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 73 | _sleep(sleeptime) |
| 74 | # exponential backoff in the sleep time |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 75 | sleeptime *= 2 |
| 76 | if sleeptime > _deadlock_MaxSleepTime: |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 77 | sleeptime = _deadlock_MaxSleepTime |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 78 | max_retries -= 1 |
Martin v. Löwis | b2c7aff | 2002-11-23 11:26:07 +0000 | [diff] [blame] | 79 | if max_retries == -1: |
| 80 | raise |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 81 | |
| 82 | |
| 83 | #------------------------------------------------------------------------ |