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 | # |
| 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ö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 |
| 25 | # "from bsddb3.db import *" |
| 26 | # |
| 27 | from time import sleep |
| 28 | _sleep = sleep |
| 29 | del sleep |
| 30 | |
Martin v. Löwis | b2c7aff | 2002-11-23 11:26:07 +0000 | [diff] [blame] | 31 | import _bsddb |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 32 | |
Martin v. Löwis | b2c7aff | 2002-11-23 11:26:07 +0000 | [diff] [blame] | 33 | _deadlock_MinSleepTime = 1.0/64 # always sleep at least N seconds between retrys |
| 34 | _deadlock_MaxSleepTime = 3.14159 # never sleep more than N seconds between retrys |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 35 | |
Martin v. Löwis | b2c7aff | 2002-11-23 11:26:07 +0000 | [diff] [blame] | 36 | _deadlock_VerboseFile = None # Assign a file object to this for a "sleeping" |
| 37 | # message to be written to it each retry |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 38 | |
| 39 | def DeadlockWrap(function, *_args, **_kwargs): |
| 40 | """DeadlockWrap(function, *_args, **_kwargs) - automatically retries |
| 41 | function in case of a database deadlock. |
| 42 | |
Martin v. Löwis | b2c7aff | 2002-11-23 11:26:07 +0000 | [diff] [blame] | 43 | This is a function intended to be used to wrap database calls such |
| 44 | that they perform retrys with exponentially backing off sleeps in |
| 45 | between when a DBLockDeadlockError exception is raised. |
| 46 | |
| 47 | A 'max_retries' parameter may optionally be passed to prevent it |
| 48 | 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] | 49 | |
| 50 | d = DB(...) |
| 51 | d.open(...) |
| 52 | DeadlockWrap(d.put, "foo", data="bar") # set key "foo" to "bar" |
| 53 | """ |
| 54 | sleeptime = _deadlock_MinSleepTime |
Martin v. Löwis | b2c7aff | 2002-11-23 11:26:07 +0000 | [diff] [blame] | 55 | max_retries = _kwargs.get('max_retries', -1) |
| 56 | if _kwargs.has_key('max_retries'): |
| 57 | del _kwargs['max_retries'] |
| 58 | while 1: |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 59 | try: |
| 60 | return apply(function, _args, _kwargs) |
| 61 | except _db.DBLockDeadlockError: |
Martin v. Löwis | b2c7aff | 2002-11-23 11:26:07 +0000 | [diff] [blame] | 62 | if _deadlock_VerboseFile: |
| 63 | _deadlock_VerboseFile.write('dbutils.DeadlockWrap: sleeping %1.3f\n' % sleeptime) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 64 | _sleep(sleeptime) |
| 65 | # exponential backoff in the sleep time |
| 66 | sleeptime = sleeptime * 2 |
| 67 | if sleeptime > _deadlock_MaxSleepTime : |
| 68 | sleeptime = _deadlock_MaxSleepTime |
Martin v. Löwis | b2c7aff | 2002-11-23 11:26:07 +0000 | [diff] [blame] | 69 | max_retries = max_retries - 1 |
| 70 | if max_retries == -1: |
| 71 | raise |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 72 | |
| 73 | |
| 74 | #------------------------------------------------------------------------ |