| 1 | n/a | #------------------------------------------------------------------------ |
|---|
| 2 | n/a | # |
|---|
| 3 | n/a | # Copyright (C) 2000 Autonomous Zone Industries |
|---|
| 4 | n/a | # |
|---|
| 5 | n/a | # License: This is free software. You may use this software for any |
|---|
| 6 | n/a | # purpose including modification/redistribution, so long as |
|---|
| 7 | n/a | # this header remains intact and that you do not claim any |
|---|
| 8 | n/a | # rights of ownership or authorship of this software. This |
|---|
| 9 | n/a | # software has been tested, but no warranty is expressed or |
|---|
| 10 | n/a | # implied. |
|---|
| 11 | n/a | # |
|---|
| 12 | n/a | # Author: Gregory P. Smith <greg@krypto.org> |
|---|
| 13 | n/a | # |
|---|
| 14 | n/a | # Note: I don't know how useful this is in reality since when a |
|---|
| 15 | n/a | # DBLockDeadlockError happens the current transaction is supposed to be |
|---|
| 16 | n/a | # aborted. If it doesn't then when the operation is attempted again |
|---|
| 17 | n/a | # the deadlock is still happening... |
|---|
| 18 | n/a | # --Robin |
|---|
| 19 | n/a | # |
|---|
| 20 | n/a | #------------------------------------------------------------------------ |
|---|
| 21 | n/a | |
|---|
| 22 | n/a | |
|---|
| 23 | n/a | # |
|---|
| 24 | n/a | # import the time.sleep function in a namespace safe way to allow |
|---|
| 25 | n/a | # "from bsddb.dbutils import *" |
|---|
| 26 | n/a | # |
|---|
| 27 | 1 | from time import sleep as _sleep |
|---|
| 28 | n/a | |
|---|
| 29 | 1 | import sys |
|---|
| 30 | 1 | absolute_import = (sys.version_info[0] >= 3) |
|---|
| 31 | 1 | if absolute_import : |
|---|
| 32 | n/a | # Because this syntaxis is not valid before Python 2.5 |
|---|
| 33 | 0 | exec("from . import db") |
|---|
| 34 | n/a | else : |
|---|
| 35 | 1 | import db |
|---|
| 36 | n/a | |
|---|
| 37 | n/a | # always sleep at least N seconds between retrys |
|---|
| 38 | 1 | _deadlock_MinSleepTime = 1.0/128 |
|---|
| 39 | n/a | # never sleep more than N seconds between retrys |
|---|
| 40 | 1 | _deadlock_MaxSleepTime = 3.14159 |
|---|
| 41 | n/a | |
|---|
| 42 | n/a | # Assign a file object to this for a "sleeping" message to be written to it |
|---|
| 43 | n/a | # each retry |
|---|
| 44 | 1 | _deadlock_VerboseFile = None |
|---|
| 45 | n/a | |
|---|
| 46 | n/a | |
|---|
| 47 | 1 | def DeadlockWrap(function, *_args, **_kwargs): |
|---|
| 48 | n/a | """DeadlockWrap(function, *_args, **_kwargs) - automatically retries |
|---|
| 49 | n/a | function in case of a database deadlock. |
|---|
| 50 | n/a | |
|---|
| 51 | n/a | This is a function intended to be used to wrap database calls such |
|---|
| 52 | n/a | that they perform retrys with exponentially backing off sleeps in |
|---|
| 53 | n/a | between when a DBLockDeadlockError exception is raised. |
|---|
| 54 | n/a | |
|---|
| 55 | n/a | A 'max_retries' parameter may optionally be passed to prevent it |
|---|
| 56 | n/a | from retrying forever (in which case the exception will be reraised). |
|---|
| 57 | n/a | |
|---|
| 58 | n/a | d = DB(...) |
|---|
| 59 | n/a | d.open(...) |
|---|
| 60 | n/a | DeadlockWrap(d.put, "foo", data="bar") # set key "foo" to "bar" |
|---|
| 61 | n/a | """ |
|---|
| 62 | 4493 | sleeptime = _deadlock_MinSleepTime |
|---|
| 63 | 4493 | max_retries = _kwargs.get('max_retries', -1) |
|---|
| 64 | 4493 | if 'max_retries' in _kwargs: |
|---|
| 65 | 0 | del _kwargs['max_retries'] |
|---|
| 66 | 4493 | while True: |
|---|
| 67 | 4493 | try: |
|---|
| 68 | 4493 | return function(*_args, **_kwargs) |
|---|
| 69 | 329 | except db.DBLockDeadlockError: |
|---|
| 70 | 0 | if _deadlock_VerboseFile: |
|---|
| 71 | 0 | _deadlock_VerboseFile.write( |
|---|
| 72 | 0 | 'dbutils.DeadlockWrap: sleeping %1.3f\n' % sleeptime) |
|---|
| 73 | 0 | _sleep(sleeptime) |
|---|
| 74 | n/a | # exponential backoff in the sleep time |
|---|
| 75 | 0 | sleeptime *= 2 |
|---|
| 76 | 0 | if sleeptime > _deadlock_MaxSleepTime: |
|---|
| 77 | 0 | sleeptime = _deadlock_MaxSleepTime |
|---|
| 78 | 0 | max_retries -= 1 |
|---|
| 79 | 0 | if max_retries == -1: |
|---|
| 80 | 0 | raise |
|---|
| 81 | n/a | |
|---|
| 82 | n/a | |
|---|
| 83 | n/a | #------------------------------------------------------------------------ |
|---|