def _abortVersion(self, ver): t = Transaction() t.note("abort %r" % ver) self._storage.tpc_begin(t) oids = self._storage.abortVersion(ver, t) self._storage.tpc_vote(t) self._storage.tpc_finish(t) return oids
def _commitVersion(self, src, dst): t = Transaction() t.note("commit %r to %r" % (src, dst)) self._storage.tpc_begin(t) oids = self._storage.commitVersion(src, dst, t) self._storage.tpc_vote(t) self._storage.tpc_finish(t) return oids
def _undo(self, tid, oid=None): # Undo a tid that affects a single object (oid). # XXX This is very specialized t = Transaction() t.note("undo") self._storage.tpc_begin(t) oids = self._storage.transactionalUndo(tid, t) self._storage.tpc_vote(t) self._storage.tpc_finish(t) if oid is not None: self.assertEqual(len(oids), 1) self.assertEqual(oids[0], oid) return self._storage.lastTransaction()
def _initroot(self): try: self._storage.load(ZERO, '') except KeyError: import PersistentMapping from ZODB.Transaction import Transaction file = StringIO() p = cPickle.Pickler(file, 1) p.dump((PersistentMapping.PersistentMapping, None)) p.dump({'_container': {}}) t=Transaction() t.description='initial database creation' self._storage.tpc_begin(t) self._storage.store(ZERO, None, file.getvalue(), '', t) self._storage.tpc_vote(t) self._storage.tpc_finish(t)
def checkUndoCommitVersion(self): def load_value(oid, version=''): data, revid = self._storage.load(oid, version) return zodb_unpickle(data).value # create a bunch of packable transactions oid = self._storage.new_oid() revid = '\000' * 8 for i in range(4): revid = self._x_dostore(oid, revid, description='packable%d' % i) pt = time.time() time.sleep(1) oid1 = self._storage.new_oid() version = 'version' revid1 = self._x_dostore(oid1, data=MinPO(0), description='create1') revid2 = self._x_dostore(oid1, data=MinPO(1), revid=revid1, version=version, description='version1') revid3 = self._x_dostore(oid1, data=MinPO(2), revid=revid2, version=version, description='version2') self._x_dostore(description='create2') t = Transaction() t.description = 'commit version' self._storage.tpc_begin(t) self._storage.commitVersion(version, '', t) self._storage.tpc_vote(t) self._storage.tpc_finish(t) info = self._storage.undoInfo() t_id = info[0]['id'] self.assertEqual(load_value(oid1), 2) self.assertEqual(load_value(oid1, version), 2) self._storage.pack(pt, referencesf) t = Transaction() t.description = 'undo commit version' self._storage.tpc_begin(t) self._storage.transactionalUndo(t_id, t) self._storage.tpc_vote(t) self._storage.tpc_finish(t) self.assertEqual(load_value(oid1), 0) self.assertEqual(load_value(oid1, version), 2)
def main(): if len(sys.argv) not in (3, 4): sys.stderr.write("Usage: timeout.py address delay [storage-name]\n" % sys.argv[0]) sys.exit(2) hostport = sys.argv[1] delay = float(sys.argv[2]) if sys.argv[3:]: name = sys.argv[3] else: name = "1" if "/" in hostport: address = hostport else: if ":" in hostport: i = hostport.index(":") host, port = hostport[:i], hostport[i+1:] else: host, port = "", hostport port = int(port) address = (host, port) print "Connecting to %s..." % repr(address) storage = ClientStorage(address, name) print "Connected. Now starting a transaction..." oid = storage.new_oid() version = "" revid = ZERO data = MinPO("timeout.py") pickled_data = zodb_pickle(data) t = Transaction() t.user = "******" storage.tpc_begin(t) storage.store(oid, revid, pickled_data, version, t) print "Stored. Now voting..." storage.tpc_vote(t) print "Voted; now sleeping %s..." % delay time.sleep(delay) print "Done."
def main(): if len(sys.argv) not in (3, 4): sys.stderr.write("Usage: timeout.py address delay [storage-name]\n" % sys.argv[0]) sys.exit(2) hostport = sys.argv[1] delay = float(sys.argv[2]) if sys.argv[3:]: name = sys.argv[3] else: name = "1" if "/" in hostport: address = hostport else: if ":" in hostport: i = hostport.index(":") host, port = hostport[:i], hostport[i + 1:] else: host, port = "", hostport port = int(port) address = (host, port) print "Connecting to %s..." % repr(address) storage = ClientStorage(address, name) print "Connected. Now starting a transaction..." oid = storage.new_oid() version = "" revid = ZERO data = MinPO("timeout.py") pickled_data = zodb_pickle(data) t = Transaction() t.user = "******" storage.tpc_begin(t) storage.store(oid, revid, pickled_data, version, t) print "Stored. Now voting..." storage.tpc_vote(t) print "Voted; now sleeping %s..." % delay time.sleep(delay) print "Done."
def _dostore(self, oid=None, revid=None, data=None, version=None, already_pickled=0, user=None, description=None): """Do a complete storage transaction. The defaults are: - oid=None, ask the storage for a new oid - revid=None, use a revid of ZERO - data=None, pickle up some arbitrary data (the integer 7) - version=None, use the empty string version Returns the object's new revision id. """ if oid is None: oid = self._storage.new_oid() if revid is None: revid = ZERO if data is None: data = MinPO(7) if type(data) == types.IntType: data = MinPO(data) if not already_pickled: data = zodb_pickle(data) if version is None: version = '' # Begin the transaction t = Transaction() if user is not None: t.user = user if description is not None: t.description = description try: self._storage.tpc_begin(t) # Store an object r1 = self._storage.store(oid, revid, data, version, t) # Finish the transaction r2 = self._storage.tpc_vote(t) revid = handle_serials(oid, r1, r2) self._storage.tpc_finish(t) except: self._storage.tpc_abort(t) raise return revid
def replay(self): ZERO = '\0' * 8 t0 = now() t = Transaction() self._storage.tpc_begin(t) for obj in self._objects: oid = obj.oid revid = _revids.get(oid, ZERO) # BAW: simulate a pickle of the given size data = 'x' * obj.size # BAW: ignore versions for now newrevid = self._storage.store(p64(oid), revid, data, '', t) _revids[oid] = newrevid if self._aborttime: self._storage.tpc_abort(t) origdelta = self._aborttime - self._begintime else: self._storage.tpc_vote(t) self._storage.tpc_finish(t) origdelta = self._finishtime - self._begintime t1 = now() # Shows how many seconds behind (positive) or ahead (negative) of the # original reply our local update took self._replaydelta = t1 - t0 - origdelta
def checkUndoAbortVersion(self): def load_value(oid, version=''): data, revid = self._storage.load(oid, version) return zodb_unpickle(data).value # create a bunch of packable transactions oid = self._storage.new_oid() revid = '\000' * 8 for i in range(3): revid = self._x_dostore(oid, revid, description='packable%d' % i) pt = time.time() time.sleep(1) oid1 = self._storage.new_oid() version = 'version' revid1 = self._x_dostore(oid1, data=MinPO(0), description='create1') revid2 = self._x_dostore(oid1, data=MinPO(1), revid=revid1, version=version, description='version1') revid3 = self._x_dostore(oid1, data=MinPO(2), revid=revid2, version=version, description='version2') self._x_dostore(description='create2') t = Transaction() t.description = 'abort version' self._storage.tpc_begin(t) self._storage.abortVersion(version, t) self._storage.tpc_vote(t) self._storage.tpc_finish(t) info = self._storage.undoInfo() t_id = info[0]['id'] self.assertEqual(load_value(oid1), 0) # after abort, we should see non-version data self.assertEqual(load_value(oid1, version), 0) t = Transaction() t.description = 'undo abort version' self._storage.tpc_begin(t) self._storage.transactionalUndo(t_id, t) self._storage.tpc_vote(t) self._storage.tpc_finish(t) self.assertEqual(load_value(oid1), 0) # t undo will re-create the version self.assertEqual(load_value(oid1, version), 2) info = self._storage.undoInfo() t_id = info[0]['id'] self._storage.pack(pt, referencesf) t = Transaction() t.description = 'undo undo' self._storage.tpc_begin(t) self._storage.transactionalUndo(t_id, t) self._storage.tpc_vote(t) self._storage.tpc_finish(t) # undo of undo will put as back where we started self.assertEqual(load_value(oid1), 0) # after abort, we should see non-version data self.assertEqual(load_value(oid1, version), 0)
def checkSimpleTransactionalUndo(self): eq = self.assertEqual oid = self._storage.new_oid() revid = self._dostore(oid, data=MinPO(23)) revid = self._dostore(oid, revid=revid, data=MinPO(24)) revid = self._dostore(oid, revid=revid, data=MinPO(25)) info = self._storage.undoInfo() tid = info[0]['id'] # Now start an undo transaction t = Transaction() t.note('undo1') self._storage.tpc_begin(t) oids = self._storage.transactionalUndo(tid, t) self._storage.tpc_vote(t) self._storage.tpc_finish(t) eq(len(oids), 1) eq(oids[0], oid) data, revid = self._storage.load(oid, '') eq(zodb_unpickle(data), MinPO(24)) # Do another one info = self._storage.undoInfo() tid = info[2]['id'] t = Transaction() t.note('undo2') self._storage.tpc_begin(t) oids = self._storage.transactionalUndo(tid, t) self._storage.tpc_vote(t) self._storage.tpc_finish(t) eq(len(oids), 1) eq(oids[0], oid) data, revid = self._storage.load(oid, '') eq(zodb_unpickle(data), MinPO(23)) # Try to undo the first record info = self._storage.undoInfo() tid = info[4]['id'] t = Transaction() t.note('undo3') self._storage.tpc_begin(t) oids = self._storage.transactionalUndo(tid, t) self._storage.tpc_vote(t) self._storage.tpc_finish(t) eq(len(oids), 1) eq(oids[0], oid) # This should fail since we've undone the object's creation self.assertRaises(KeyError, self._storage.load, oid, '') # And now let's try to redo the object's creation info = self._storage.undoInfo() tid = info[0]['id'] t = Transaction() self._storage.tpc_begin(t) oids = self._storage.transactionalUndo(tid, t) self._storage.tpc_vote(t) self._storage.tpc_finish(t) eq(len(oids), 1) eq(oids[0], oid) data, revid = self._storage.load(oid, '') eq(zodb_unpickle(data), MinPO(23)) self._iterate()