Skip to content

Commit 63650b6

Browse files
author
Richard Jones
committed
implemented whole-database locking
1 parent e5cc6b8 commit 63650b6

File tree

7 files changed

+112
-70
lines changed

7 files changed

+112
-70
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ are given with the most recent entry first.
33

44
2003-01-?? 0.5.4
55
- key the templates cache off full path, not filename
6+
- implemented whole-database locking
67

78

89
2002-12-11 0.5.3

roundup/backends/back_anydbm.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717
#
18-
#$Id: back_anydbm.py,v 1.94 2002-12-11 01:03:38 richard Exp $
18+
#$Id: back_anydbm.py,v 1.95 2002-12-12 09:31:04 richard Exp $
1919
'''
2020
This module defines a backend that saves the hyperdatabase in a database
2121
chosen by anydbm. It is guaranteed to always be available in python
@@ -28,7 +28,7 @@
2828
from blobfiles import FileStorage
2929
from sessions import Sessions
3030
from roundup.indexer import Indexer
31-
from locking import acquire_lock, release_lock
31+
from roundup.backends import locking
3232
from roundup.hyperdb import String, Password, Date, Interval, Link, \
3333
Multilink, DatabaseError, Boolean, Number
3434

@@ -72,6 +72,12 @@ def __init__(self, config, journaltag=None):
7272
# ensure files are group readable and writable
7373
os.umask(0002)
7474

75+
# lock it
76+
lockfilenm = os.path.join(self.dir, 'lock')
77+
self.lockfile = locking.acquire_lock(lockfilenm)
78+
self.lockfile.write(str(os.getpid()))
79+
self.lockfile.flush()
80+
7581
def post_init(self):
7682
''' Called once the schema initialisation has finished.
7783
'''
@@ -203,20 +209,13 @@ def opendb(self, name, mode):
203209
mode)
204210
return dbm.open(path, mode)
205211

206-
def lockdb(self, name):
207-
''' Lock a database file
208-
'''
209-
path = os.path.join(os.getcwd(), self.dir, '%s.lock'%name)
210-
return acquire_lock(path)
211-
212212
#
213213
# Node IDs
214214
#
215215
def newid(self, classname):
216216
''' Generate a new id for the given class
217217
'''
218218
# open the ids DB - create if if doesn't exist
219-
lock = self.lockdb('_ids')
220219
db = self.opendb('_ids', 'c')
221220
if db.has_key(classname):
222221
newid = db[classname] = str(int(db[classname]) + 1)
@@ -225,18 +224,15 @@ def newid(self, classname):
225224
newid = str(self.getclass(classname).count()+1)
226225
db[classname] = newid
227226
db.close()
228-
release_lock(lock)
229227
return newid
230228

231229
def setid(self, classname, setid):
232230
''' Set the id counter: used during import of database
233231
'''
234232
# open the ids DB - create if if doesn't exist
235-
lock = self.lockdb('_ids')
236233
db = self.opendb('_ids', 'c')
237234
db[classname] = str(setid)
238235
db.close()
239-
release_lock(lock)
240236

241237
#
242238
# Nodes
@@ -696,7 +692,11 @@ def rollback(self):
696692
def close(self):
697693
''' Nothing to do
698694
'''
699-
pass
695+
if self.lockfile is not None:
696+
locking.release_lock(self.lockfile)
697+
if self.lockfile is not None:
698+
self.lockfile.close()
699+
self.lockfile = None
700700

701701
_marker = []
702702
class Class(hyperdb.Class):
@@ -1326,7 +1326,7 @@ def is_retired(self, nodeid):
13261326

13271327
def destroy(self, nodeid):
13281328
'''Destroy a node.
1329-
1329+
13301330
WARNING: this method should never be used except in extremely rare
13311331
situations where there could never be links to the node being
13321332
deleted
@@ -1906,13 +1906,12 @@ def import_list(self, propnames, proplist):
19061906
def get(self, nodeid, propname, default=_marker, cache=1):
19071907
''' trap the content propname and get it from the file
19081908
'''
1909-
1910-
poss_msg = 'Possibly a access right configuration problem.'
1909+
poss_msg = 'Possibly an access right configuration problem.'
19111910
if propname == 'content':
19121911
try:
19131912
return self.db.getfile(self.classname, nodeid, None)
19141913
except IOError, (strerror):
1915-
# BUG: by catching this we donot see an error in the log.
1914+
# XXX by catching this we donot see an error in the log.
19161915
return 'ERROR reading file: %s%s\n%s\n%s'%(
19171916
self.classname, nodeid, poss_msg, strerror)
19181917
if default is not _marker:

roundup/backends/back_gadfly.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# $Id: back_gadfly.py,v 1.29 2002-10-07 00:52:51 richard Exp $
1+
# $Id: back_gadfly.py,v 1.30 2002-12-12 09:31:04 richard Exp $
22
''' Gadlfy relational database hypderb backend.
33
44
About Gadfly
@@ -36,6 +36,7 @@
3636
from roundup import hyperdb, date, password, roundupdb, security
3737
from roundup.hyperdb import String, Password, Date, Interval, Link, \
3838
Multilink, DatabaseError, Boolean, Number
39+
from roundup.backends import locking
3940

4041
# basic RDBMS backen implementation
4142
from roundup.backends import rdbms_common
@@ -51,6 +52,13 @@ class Database(rdbms_common.Database):
5152

5253
def open_connection(self):
5354
db = getattr(self.config, 'GADFLY_DATABASE', ('database', self.dir))
55+
56+
# lock it
57+
lockfilenm = os.path.join(db[1], db[0]) + '.lck'
58+
self.lockfile = locking.acquire_lock(lockfilenm)
59+
self.lockfile.write(str(os.getpid()))
60+
self.lockfile.flush()
61+
5462
if len(db) == 2:
5563
# ensure files are group readable and writable
5664
os.umask(0002)

0 commit comments

Comments
 (0)