Skip to content

Commit

Permalink
Switch all storages to the new commit protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
jmuchemb committed Jul 1, 2016
1 parent c009f39 commit 4558d93
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 68 deletions.
1 change: 1 addition & 0 deletions src/ZODB/BaseStorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ def tpc_finish(self, transaction, f=None):
self._ude = None
self._transaction = None
self._commit_lock.release()
return self._tid

def _finish(self, tid, u, d, e):
"""Subclasses should redefine this to supply transaction finish actions
Expand Down
7 changes: 4 additions & 3 deletions src/ZODB/DemoStorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import ZODB.utils
import zope.interface

from .ConflictResolution import ConflictResolvingStorage, ResolvedSerial
from .ConflictResolution import ConflictResolvingStorage
from .utils import load_current, maxtid

@zope.interface.implementer(
Expand Down Expand Up @@ -309,7 +309,7 @@ def store(self, oid, serial, data, version, transaction):
if old != serial:
rdata = self.tryToResolveConflict(oid, old, serial, data)
self.changes.store(oid, old, rdata, '', transaction)
return ResolvedSerial
return True

return self.changes.store(oid, serial, data, '', transaction)

Expand Down Expand Up @@ -374,8 +374,9 @@ def tpc_finish(self, transaction, func = lambda tid: None):
self._issued_oids.difference_update(self._stored_oids)
self._stored_oids = set()
self._transaction = None
self.changes.tpc_finish(transaction, func)
tid = self.changes.tpc_finish(transaction, func)
self._commit_lock.release()
return tid

_temporary_blobdirs = {}
def cleanup_temporary_blobdir(
Expand Down
13 changes: 5 additions & 8 deletions src/ZODB/FileStorage/FileStorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
from ZODB.BaseStorage import DataRecord as _DataRecord
from ZODB.BaseStorage import TransactionRecord as _TransactionRecord
from ZODB.ConflictResolution import ConflictResolvingStorage
from ZODB.ConflictResolution import ResolvedSerial
from ZODB.FileStorage.format import CorruptedDataError
from ZODB.FileStorage.format import CorruptedError
from ZODB.FileStorage.format import DATA_HDR
Expand Down Expand Up @@ -535,10 +534,7 @@ def store(self, oid, oldserial, data, version, transaction):
raise FileStorageQuotaError(
"The storage quota has been exceeded.")

if old and oldserial != committed_tid:
return ResolvedSerial
else:
return self._tid
return old and oldserial != committed_tid

def deleteObject(self, oid, oldserial, transaction):
if self._is_read_only:
Expand Down Expand Up @@ -739,15 +735,16 @@ def tpc_finish(self, transaction, f=None):
raise StorageTransactionError(
"tpc_finish called with wrong transaction")
try:
tid = self._tid
if f is not None:
f(self._tid)
u, d, e = self._ude
self._finish(self._tid, u, d, e)
f(tid)
self._finish(tid, *self._ude)
self._clear_temp()
finally:
self._ude = None
self._transaction = None
self._commit_lock.release()
return tid

def _finish(self, tid, u, d, e):
# If self._nextpos is 0, then the transaction didn't write any
Expand Down
4 changes: 2 additions & 2 deletions src/ZODB/FileStorage/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def pack_with_repeated_blob_records():
>>> _ = fs.store(oid, oldserial, blob_record, '', trans)
>>> _ = fs.storeBlob(oid, oldserial, blob_record, 'ablob', '', trans)
>>> fs.tpc_vote(trans)
>>> fs.tpc_finish(trans)
>>> _ = fs.tpc_finish(trans)
>>> time.sleep(.01)
>>> db.pack()
Expand All @@ -158,7 +158,7 @@ def _save_index():
... oid += (1<<16)
... _ = fs.store(ZODB.utils.p64(oid), ZODB.utils.z64, b'x', '', t)
>>> fs.tpc_vote(t)
>>> fs.tpc_finish(t)
>>> _ = fs.tpc_finish(t)
>>> import sys
>>> old_limit = sys.getrecursionlimit()
Expand Down
3 changes: 1 addition & 2 deletions src/ZODB/MappingStorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,6 @@ def store(self, oid, serial, data, version, transaction):

self._tdata[oid] = data

return self._tid

checkCurrentSerialInTransaction = (
ZODB.BaseStorage.checkCurrentSerialInTransaction)

Expand Down Expand Up @@ -307,6 +305,7 @@ def tpc_finish(self, transaction, func = lambda tid: None):
self._transaction = None
del self._tdata
self._commit_lock.release()
return tid

# ZEO.interfaces.IServeable
@ZODB.utils.locked(opened)
Expand Down
5 changes: 3 additions & 2 deletions src/ZODB/tests/IExternalGC.test
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ transaction ourselves.
>>> storage.deleteObject(oid0, s0, txn)
>>> storage.deleteObject(oid1, s1, txn)
>>> storage.tpc_vote(txn)
>>> storage.tpc_finish(txn)
>>> tid = storage.lastTransaction()
>>> tid = storage.tpc_finish(txn)
>>> tid == storage.lastTransaction()
True

Now if we try to load data for the objects, we get a POSKeyError:

Expand Down
4 changes: 1 addition & 3 deletions src/ZODB/tests/StorageTestBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,7 @@ def handle_all_serials(oid, *args):
for arg in args:
if isinstance(arg, bytes):
d[oid] = arg
elif arg is None:
pass
else:
elif arg:
for oid, serial in arg:
if not isinstance(serial, bytes):
raise serial # error from ZEO server
Expand Down
25 changes: 12 additions & 13 deletions src/ZODB/tests/blob_transaction.txt
Original file line number Diff line number Diff line change
Expand Up @@ -381,20 +381,19 @@ stored are discarded.
>>> blob_storage.tpc_begin(t)
>>> with open('blobfile', 'wb') as file:
... _ = file.write(b'This data should go away')
>>> s1 = blob_storage.storeBlob(blob._p_oid, oldserial, olddata, 'blobfile',
... '', t)
>>> bool(blob_storage.storeBlob(blob._p_oid, oldserial, olddata, 'blobfile',
... '', t))
False
>>> new_oid = blob_storage.new_oid()
>>> with open('blobfile2', 'wb') as file:
... _ = file.write(b'This data should go away too')
>>> s2 = blob_storage.storeBlob(new_oid, '\0'*8, olddata, 'blobfile2',
... '', t)

>>> serials = blob_storage.tpc_vote(t)
>>> if s1 is None:
... s1 = [s for (oid, s) in serials if oid == blob._p_oid][0]
>>> if s2 is None:
... s2 = [s for (oid, s) in serials if oid == new_oid][0]

>>> bool(blob_storage.storeBlob(new_oid, '\0'*8, olddata, 'blobfile2',
... '', t))
False
>>> blob_storage.tpc_vote(t)
>>> tid = blob_storage._tid
>>> oldserial < tid
True
>>> blob_storage.tpc_abort(t)

Now, the serial for the existing blob should be the same:
Expand All @@ -404,7 +403,7 @@ Now, the serial for the existing blob should be the same:

And we shouldn't be able to read the data that we saved:

>>> blob_storage.loadBlob(blob._p_oid, s1)
>>> blob_storage.loadBlob(blob._p_oid, tid)
Traceback (most recent call last):
...
POSKeyError: 'No blob file at <BLOB STORAGE PATH>'
Expand All @@ -422,7 +421,7 @@ Similarly, the new object wasn't added to the storage:
...
POSKeyError: 0x...

>>> blob_storage.loadBlob(blob._p_oid, s2)
>>> blob_storage.loadBlob(blob._p_oid, tid)
Traceback (most recent call last):
...
POSKeyError: 'No blob file at <BLOB STORAGE PATH>'
Expand Down
21 changes: 4 additions & 17 deletions src/ZODB/tests/testDemoStorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@
# With the following monkey-patch, we can test the different ways
# to update _p_changed/_p_serial status of committed oids.

from ZODB.ConflictResolution import ResolvedSerial

class DemoStorage(ZODB.DemoStorage.DemoStorage):

delayed_store = False
Expand All @@ -57,12 +55,10 @@ def tpc_begin(self, *args):

def store(self, oid, *args):
s = super(DemoStorage, self).store(oid, *args)
if s != ResolvedSerial:
assert type(s) is bytes, s
return
if not self.delayed_store:
return True
self.__stored.append(oid)
if s and self.delayed_store:
self.__stored.append(oid)
else:
return s

tpc_vote = property(lambda self: self._tpc_vote, lambda *_: None)

Expand All @@ -71,15 +67,6 @@ def _tpc_vote(self, transaction):
assert s is None, s
return self.__stored if self.delayed_store else s

def tpc_finish(self, transaction, func = lambda tid: None):
r = []
def callback(tid):
func(tid)
r.append(tid)
tid = super(DemoStorage, self).tpc_finish(transaction, callback)
assert tid is None, tid
return r[0]

ZODB.DemoStorage.DemoStorage = DemoStorage

class DemoStorageTests(
Expand Down
28 changes: 10 additions & 18 deletions src/ZODB/tests/testFileStorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,33 +415,25 @@ class Broken(MinPO):
module.Broken = Broken

oids = [[self._storage.new_oid(), None] for i in range(3)]
def store(i, data):
oid, revid = oids[i]
self._storage.store(oid, revid, data, "", t)

for i in range(2):
t = transaction.Transaction()
self._storage.tpc_begin(t)

# sometimes data is in this format
j = 0
oid, revid = oids[j]
serial = self._storage.store(
oid, revid, dumps(OOBTree, _protocol), "", t)
oids[j][1] = serial

store(0, dumps(OOBTree, _protocol))
# and it could be from a broken module
j = 1
oid, revid = oids[j]
serial = self._storage.store(
oid, revid, dumps(Broken, _protocol), "", t)
oids[j][1] = serial

store(1, dumps(Broken, _protocol))
# but mostly it looks like this
j = 2
o = MinPO(j)
oid, revid = oids[j]
serial = self._storage.store(oid, revid, zodb_pickle(o), "", t)
oids[j][1] = serial
store(2, zodb_pickle(MinPO(2)))

self._storage.tpc_vote(t)
self._storage.tpc_finish(t)
tid = self._storage.tpc_finish(t)
for oid_revid in oids:
oid_revid[1] = tid

# now break the import of the Broken class
del sys.modules[module_name]
Expand Down

0 comments on commit 4558d93

Please sign in to comment.