示例#1
0
    def tpc_begin(self, id, user, description, ext, tid=None, status=" "):
        if self.read_only:
            raise ReadOnlyError()
        if self.transaction is not None:
            if self.transaction.id == id:
                self.log("duplicate tpc_begin(%s)" % repr(id))
                return
            else:
                raise StorageTransactionError("Multiple simultaneous tpc_begin"
                                              " requests from one client.")

        self.transaction = t = transaction.Transaction()
        t.id = id
        t.user = user
        t.description = description
        t._extension = ext

        self.serials = []
        self.invalidated = []
        self.txnlog = CommitLog()
        self.tid = tid
        self.status = status
        self.store_failed = 0
        self.stats.active_txns += 1
示例#2
0
class ZEOStorage:
    """Proxy to underlying storage for a single remote client."""

    # Classes we instantiate.  A subclass might override.
    ClientStorageStubClass = ClientStub.ClientStorage

    # A list of extension methods.  A subclass with extra methods
    # should override.
    extensions = []

    def __init__(self, server, read_only=0, auth_realm=None):
        self.server = server
        # timeout and stats will be initialized in register()
        self.timeout = None
        self.stats = None
        self.connection = None
        self.client = None
        self.storage = None
        self.storage_id = "uninitialized"
        self.transaction = None
        self.read_only = read_only
        self.locked = 0
        self.verifying = 0
        self.store_failed = 0
        self.log_label = _label
        self.authenticated = 0
        self.auth_realm = auth_realm
        self.blob_tempfile = None
        self.blob_log = []
        # The authentication protocol may define extra methods.
        self._extensions = {}
        for func in self.extensions:
            self._extensions[func.func_name] = None

    def finish_auth(self, authenticated):
        if not self.auth_realm:
            return 1
        self.authenticated = authenticated
        return authenticated

    def set_database(self, database):
        self.database = database

    def notifyConnected(self, conn):
        self.connection = conn  # For restart_other() below
        self.client = self.ClientStorageStubClass(conn)
        addr = conn.addr
        if isinstance(addr, type("")):
            label = addr
        else:
            host, port = addr
            label = str(host) + ":" + str(port)
        self.log_label = _label + "/" + label

    def notifyDisconnected(self):
        # When this storage closes, we must ensure that it aborts
        # any pending transaction.
        if self.transaction is not None:
            self.log("disconnected during transaction %s" % self.transaction)
            self._abort()
        else:
            self.log("disconnected")
        if self.stats is not None:
            self.stats.clients -= 1

    def __repr__(self):
        tid = self.transaction and repr(self.transaction.id)
        if self.storage:
            stid = (self.tpc_transaction() and repr(self.tpc_transaction().id))
        else:
            stid = None
        name = self.__class__.__name__
        return "<%s %X trans=%s s_trans=%s>" % (name, id(self), tid, stid)

    def log(self, msg, level=logging.INFO, exc_info=False):
        log(msg, level=level, label=self.log_label, exc_info=exc_info)

    def setup_delegation(self):
        """Delegate several methods to the storage
        """

        storage = self.storage

        info = self.get_info()
        if info['supportsVersions']:
            self.versionEmpty = storage.versionEmpty
            self.versions = storage.versions
            self.modifiedInVersion = storage.modifiedInVersion
        else:
            self.versionEmpty = lambda version: True
            self.versions = lambda max=None: ()
            self.modifiedInVersion = lambda oid: ''

            def commitVersion(*a, **k):
                raise NotImplementedError

            self.commitVersion = self.abortVersion = commitVersion

        if not info['supportsUndo']:
            self.undoLog = self.undoInfo = lambda *a, **k: ()

            def undo(*a, **k):
                raise NotImplementedError

            self.undo = undo

        self.getTid = storage.getTid
        self.history = storage.history
        self.load = storage.load
        self.loadSerial = storage.loadSerial
        record_iternext = getattr(storage, 'record_iternext', None)
        if record_iternext is not None:
            self.record_iternext = record_iternext

        try:
            fn = storage.getExtensionMethods
        except AttributeError:
            pass  # no extension methods
        else:
            d = fn()
            self._extensions.update(d)
            for name in d:
                assert not hasattr(self, name)
                setattr(self, name, getattr(storage, name))
        self.lastTransaction = storage.lastTransaction

        try:
            self.tpc_transaction = storage.tpc_transaction
        except AttributeError:
            if hasattr(storage, '_transaction'):
                log(
                    "Storage %r doesn't have a tpc_transaction method.\n"
                    "See ZEO.interfaces.IServeable."
                    "Falling back to using _transaction attribute, which\n."
                    "is icky.", logging.ERROR)
                self.tpc_transaction = lambda: storage._transaction
            else:
                raise

    def _check_tid(self, tid, exc=None):
        if self.read_only:
            raise ReadOnlyError()
        if self.transaction is None:
            caller = sys._getframe().f_back.f_code.co_name
            self.log("no current transaction: %s()" % caller,
                     level=logging.WARNING)
            if exc is not None:
                raise exc(None, tid)
            else:
                return 0
        if self.transaction.id != tid:
            caller = sys._getframe().f_back.f_code.co_name
            self.log(
                "%s(%s) invalid; current transaction = %s" %
                (caller, repr(tid), repr(self.transaction.id)),
                logging.WARNING)
            if exc is not None:
                raise exc(self.transaction.id, tid)
            else:
                return 0
        return 1

    def getAuthProtocol(self):
        """Return string specifying name of authentication module to use.

        The module name should be auth_%s where %s is auth_protocol."""
        protocol = self.server.auth_protocol
        if not protocol or protocol == 'none':
            return None
        return protocol

    def register(self, storage_id, read_only):
        """Select the storage that this client will use

        This method must be the first one called by the client.
        For authenticated storages this method will be called by the client
        immediately after authentication is finished.
        """
        if self.auth_realm and not self.authenticated:
            raise AuthError("Client was never authenticated with server!")

        if self.storage is not None:
            self.log("duplicate register() call")
            raise ValueError("duplicate register() call")
        storage = self.server.storages.get(storage_id)
        if storage is None:
            self.log("unknown storage_id: %s" % storage_id)
            raise ValueError("unknown storage: %s" % storage_id)

        if not read_only and (self.read_only or storage.isReadOnly()):
            raise ReadOnlyError()

        self.read_only = self.read_only or read_only
        self.storage_id = storage_id
        self.storage = storage
        self.setup_delegation()
        self.timeout, self.stats = self.server.register_connection(
            storage_id, self)

    def get_info(self):
        storage = self.storage

        try:
            supportsVersions = storage.supportsVersions
        except AttributeError:
            supportsVersions = False
        else:
            supportsVersions = supportsVersions()

        try:
            supportsUndo = storage.supportsUndo
        except AttributeError:
            supportsUndo = False
        else:
            supportsUndo = supportsUndo()

        return {
            'length': len(storage),
            'size': storage.getSize(),
            'name': storage.getName(),
            'supportsUndo': supportsUndo,
            'supportsVersions': supportsVersions,
            'extensionMethods': self.getExtensionMethods(),
            'supports_record_iternext': hasattr(self, 'record_iternext'),
        }

    def get_size_info(self):
        return {
            'length': len(self.storage),
            'size': self.storage.getSize(),
        }

    def getExtensionMethods(self):
        return self._extensions

    def loadEx(self, oid, version):
        self.stats.loads += 1
        if version:
            oversion = self.storage.modifiedInVersion(oid)
            if oversion == version:
                data, serial = self.storage.load(oid, version)
                return data, serial, version

        data, serial = self.storage.load(oid, '')
        return data, serial, ''

    def loadBefore(self, oid, tid):
        self.stats.loads += 1
        return self.storage.loadBefore(oid, tid)

    def zeoLoad(self, oid):
        self.stats.loads += 1
        v = self.storage.modifiedInVersion(oid)
        if v:
            pv, sv = self.storage.load(oid, v)
        else:
            pv = sv = None
        try:
            p, s = self.storage.load(oid, '')
        except KeyError:
            if sv:
                # Created in version, no non-version data
                p = s = None
            else:
                raise
        return p, s, v, pv, sv

    def getInvalidations(self, tid):
        invtid, invlist = self.server.get_invalidations(self.storage_id, tid)
        if invtid is None:
            return None
        self.log("Return %d invalidations up to tid %s" %
                 (len(invlist), u64(invtid)))
        return invtid, invlist

    def verify(self, oid, version, tid):
        try:
            t = self.getTid(oid)
        except KeyError:
            self.client.invalidateVerify((oid, ""))
        else:
            if tid != t:
                # This will invalidate non-version data when the
                # client only has invalid version data.  Since this is
                # an uncommon case, we avoid the cost of checking
                # whether the serial number matches the current
                # non-version data.
                self.client.invalidateVerify((oid, version))

    def zeoVerify(self, oid, s, sv):
        if not self.verifying:
            self.verifying = 1
            self.stats.verifying_clients += 1
        try:
            os = self.getTid(oid)
        except KeyError:
            self.client.invalidateVerify((oid, ''))
            # It's not clear what we should do now.  The KeyError
            # could be caused by an object uncreation, in which case
            # invalidation is right.  It could be an application bug
            # that left a dangling reference, in which case it's bad.
        else:
            # If the client has version data, the logic is a bit more
            # complicated.  If the current serial number matches the
            # client serial number, then the non-version data must
            # also be valid.  If the current serialno is for a
            # version, then the non-version data can't change.

            # If the version serialno isn't valid, then the
            # non-version serialno may or may not be valid.  Rather
            # than trying to figure it whether it is valid, we just
            # invalidate it.  Sending an invalidation for the
            # non-version data implies invalidating the version data
            # too, since an update to non-version data can only occur
            # after the version is aborted or committed.
            if sv:
                if sv != os:
                    self.client.invalidateVerify((oid, ''))
            else:
                if s != os:
                    self.client.invalidateVerify((oid, ''))

    def endZeoVerify(self):
        if self.verifying:
            self.stats.verifying_clients -= 1
        self.verifying = 0
        self.client.endVerify()

    def pack(self, time, wait=1):
        # Yes, you can pack a read-only server or storage!
        if wait:
            return run_in_thread(self._pack_impl, time)
        else:
            # If the client isn't waiting for a reply, start a thread
            # and forget about it.
            t = threading.Thread(target=self._pack_impl, args=(time, ))
            t.start()
            return None

    def _pack_impl(self, time):
        self.log("pack(time=%s) started..." % repr(time))
        self.storage.pack(time, referencesf)
        self.log("pack(time=%s) complete" % repr(time))
        # Broadcast new size statistics
        self.server.invalidate(0, self.storage_id, None, (),
                               self.get_size_info())

    def new_oids(self, n=100):
        """Return a sequence of n new oids, where n defaults to 100"""
        if self.read_only:
            raise ReadOnlyError()
        if n <= 0:
            n = 1
        return [self.storage.new_oid() for i in range(n)]

    # undoLog and undoInfo are potentially slow methods

    def undoInfo(self, first, last, spec):
        return run_in_thread(self.storage.undoInfo, first, last, spec)

    def undoLog(self, first, last):
        return run_in_thread(self.storage.undoLog, first, last)

    def tpc_begin(self, id, user, description, ext, tid=None, status=" "):
        if self.read_only:
            raise ReadOnlyError()
        if self.transaction is not None:
            if self.transaction.id == id:
                self.log("duplicate tpc_begin(%s)" % repr(id))
                return
            else:
                raise StorageTransactionError("Multiple simultaneous tpc_begin"
                                              " requests from one client.")

        self.transaction = t = transaction.Transaction()
        t.id = id
        t.user = user
        t.description = description
        t._extension = ext

        self.serials = []
        self.invalidated = []
        self.txnlog = CommitLog()
        self.tid = tid
        self.status = status
        self.store_failed = 0
        self.stats.active_txns += 1

    def tpc_finish(self, id):
        if not self._check_tid(id):
            return
        assert self.locked
        self.stats.active_txns -= 1
        self.stats.commits += 1
        self.storage.tpc_finish(self.transaction)
        tid = self.storage.lastTransaction()
        if self.invalidated:
            self.server.invalidate(self, self.storage_id, tid,
                                   self.invalidated, self.get_size_info())
        self._clear_transaction()
        # Return the tid, for cache invalidation optimization
        return tid

    def tpc_abort(self, id):
        if not self._check_tid(id):
            return
        self.stats.active_txns -= 1
        self.stats.aborts += 1
        if self.locked:
            self.storage.tpc_abort(self.transaction)
        self._clear_transaction()

    def _clear_transaction(self):
        # Common code at end of tpc_finish() and tpc_abort()
        self.transaction = None
        self.txnlog.close()
        if self.locked:
            self.locked = 0
            self.timeout.end(self)
            self.stats.lock_time = None
            self.log("Transaction released storage lock", BLATHER)
            # _handle_waiting() can start another transaction (by
            # restarting a waiting one) so must be done last
            self._handle_waiting()

    def _abort(self):
        # called when a connection is closed unexpectedly
        if not self.locked:
            # Delete (d, zeo_storage) from the _waiting list, if found.
            waiting = self.storage._waiting
            for i in range(len(waiting)):
                d, z = waiting[i]
                if z is self:
                    del waiting[i]
                    self.log("Closed connection removed from waiting list."
                             " Clients waiting: %d." % len(waiting))
                    break

        if self.transaction:
            self.stats.active_txns -= 1
            self.stats.aborts += 1
            self.tpc_abort(self.transaction.id)

    # The public methods of the ZEO client API do not do the real work.
    # They defer work until after the storage lock has been acquired.
    # Most of the real implementations are in methods beginning with
    # an _.

    def storea(self, oid, serial, data, version, id):
        self._check_tid(id, exc=StorageTransactionError)
        self.stats.stores += 1
        self.txnlog.store(oid, serial, data, version)

    def storeBlobStart(self):
        assert self.blob_tempfile is None
        self.blob_tempfile = tempfile.mkstemp(
            dir=self.storage.temporaryDirectory())

    def storeBlobChunk(self, chunk):
        os.write(self.blob_tempfile[0], chunk)

    def storeBlobEnd(self, oid, serial, data, version, id):
        fd, tempname = self.blob_tempfile
        self.blob_tempfile = None
        os.close(fd)
        self.blob_log.append((oid, serial, data, tempname, version))

    def storeBlobShared(self, oid, serial, data, filename, version, id):
        # Reconstruct the full path from the filename in the OID directory
        filename = os.path.join(self.storage.fshelper.getPathForOID(oid),
                                filename)
        self.blob_log.append((oid, serial, data, filename, version))

    def sendBlob(self, oid, serial):
        self.client.storeBlob(oid, serial, self.storage.loadBlob(oid, serial))

    # The following four methods return values, so they must acquire
    # the storage lock and begin the transaction before returning.

    def vote(self, id):
        self._check_tid(id, exc=StorageTransactionError)
        if self.locked:
            return self._vote()
        else:
            return self._wait(lambda: self._vote())

    def abortVersion(self, src, id):
        self._check_tid(id, exc=StorageTransactionError)
        if self.locked:
            return self._abortVersion(src)
        else:
            return self._wait(lambda: self._abortVersion(src))

    def commitVersion(self, src, dest, id):
        self._check_tid(id, exc=StorageTransactionError)
        if self.locked:
            return self._commitVersion(src, dest)
        else:
            return self._wait(lambda: self._commitVersion(src, dest))

    def undo(self, trans_id, id):
        self._check_tid(id, exc=StorageTransactionError)
        if self.locked:
            return self._undo(trans_id)
        else:
            return self._wait(lambda: self._undo(trans_id))

    def _tpc_begin(self, txn, tid, status):
        self.locked = 1
        self.timeout.begin(self)
        self.stats.lock_time = time.time()
        self.storage.tpc_begin(txn, tid, status)

    def _store(self, oid, serial, data, version):
        err = None
        try:
            newserial = self.storage.store(oid, serial, data, version,
                                           self.transaction)
        except (SystemExit, KeyboardInterrupt):
            raise
        except Exception, err:
            self.store_failed = 1
            if isinstance(err, ConflictError):
                self.stats.conflicts += 1
                self.log(
                    "conflict error oid=%s msg=%s" % (oid_repr(oid), str(err)),
                    BLATHER)
            if not isinstance(err, TransactionError):
                # Unexpected errors are logged and passed to the client
                self.log("store error: %s, %s" % sys.exc_info()[:2],
                         logging.ERROR,
                         exc_info=True)
            # Try to pickle the exception.  If it can't be pickled,
            # the RPC response would fail, so use something else.
            pickler = cPickle.Pickler()
            pickler.fast = 1
            try:
                pickler.dump(err, 1)
            except:
                msg = "Couldn't pickle storage exception: %s" % repr(err)
                self.log(msg, logging.ERROR)
                err = StorageServerError(msg)
            # The exception is reported back as newserial for this oid
            newserial = [(oid, err)]
        else:
示例#3
0
class ZEOStorage:
    """Proxy to underlying storage for a single remote client."""

    # Classes we instantiate.  A subclass might override.
    ClientStorageStubClass = ClientStub.ClientStorage

    # A list of extension methods.  A subclass with extra methods
    # should override.
    extensions = []

    def __init__(self, server, read_only=0, auth_realm=None):
        self.server = server
        # timeout and stats will be initialized in register()
        self.timeout = None
        self.stats = None
        self.connection = None
        self.client = None
        self.storage = None
        self.storage_id = "uninitialized"
        self.transaction = None
        self.read_only = read_only
        self.locked = 0
        self.verifying = 0
        self.store_failed = 0
        self.log_label = _label
        self.authenticated = 0
        self.auth_realm = auth_realm
        # The authentication protocol may define extra methods.
        self._extensions = {}
        for func in self.extensions:
            self._extensions[func.func_name] = None

    def finish_auth(self, authenticated):
        if not self.auth_realm:
            return 1
        self.authenticated = authenticated
        return authenticated

    def set_database(self, database):
        self.database = database

    def notifyConnected(self, conn):
        self.connection = conn # For restart_other() below
        self.client = self.ClientStorageStubClass(conn)
        addr = conn.addr
        if isinstance(addr, type("")):
            label = addr
        else:
            host, port = addr
            label = str(host) + ":" + str(port)
        self.log_label = _label + "/" + label

    def notifyDisconnected(self):
        # When this storage closes, we must ensure that it aborts
        # any pending transaction.
        if self.transaction is not None:
            self.log("disconnected during transaction %s" % self.transaction)
            self._abort()
        else:
            self.log("disconnected")
        if self.stats is not None:
            self.stats.clients -= 1

    def __repr__(self):
        tid = self.transaction and repr(self.transaction.id)
        if self.storage:
            stid = (self.storage._transaction and
                    repr(self.storage._transaction.id))
        else:
            stid = None
        name = self.__class__.__name__
        return "<%s %X trans=%s s_trans=%s>" % (name, id(self), tid, stid)

    def log(self, msg, level=logging.INFO, exc_info=False):
        log(msg, level=level, label=self.log_label, exc_info=exc_info)

    def setup_delegation(self):
        """Delegate several methods to the storage"""
        self.versionEmpty = self.storage.versionEmpty
        self.versions = self.storage.versions
        self.getSerial = self.storage.getSerial
        self.history = self.storage.history
        self.load = self.storage.load
        self.loadSerial = self.storage.loadSerial
        self.modifiedInVersion = self.storage.modifiedInVersion
        try:
            fn = self.storage.getExtensionMethods
        except AttributeError:
            # We must be running with a ZODB which
            # predates adding getExtensionMethods to
            # BaseStorage. Eventually this try/except
            # can be removed
            pass
        else:
            d = fn()
            self._extensions.update(d)
            for name in d.keys():
                assert not hasattr(self, name)
                setattr(self, name, getattr(self.storage, name))
        self.lastTransaction = self.storage.lastTransaction

    def _check_tid(self, tid, exc=None):
        if self.read_only:
            raise ReadOnlyError()
        if self.transaction is None:
            caller = sys._getframe().f_back.f_code.co_name
            self.log("no current transaction: %s()" % caller,
                     level=logging.WARNING)
            if exc is not None:
                raise exc(None, tid)
            else:
                return 0
        if self.transaction.id != tid:
            caller = sys._getframe().f_back.f_code.co_name
            self.log("%s(%s) invalid; current transaction = %s" %
                     (caller, repr(tid), repr(self.transaction.id)),
                     logging.WARNING)
            if exc is not None:
                raise exc(self.transaction.id, tid)
            else:
                return 0
        return 1

    def getAuthProtocol(self):
        """Return string specifying name of authentication module to use.

        The module name should be auth_%s where %s is auth_protocol."""
        protocol = self.server.auth_protocol
        if not protocol or protocol == 'none':
            return None
        return protocol

    def register(self, storage_id, read_only):
        """Select the storage that this client will use

        This method must be the first one called by the client.
        For authenticated storages this method will be called by the client
        immediately after authentication is finished.
        """
        if self.auth_realm and not self.authenticated:
            raise AuthError("Client was never authenticated with server!")

        if self.storage is not None:
            self.log("duplicate register() call")
            raise ValueError("duplicate register() call")
        storage = self.server.storages.get(storage_id)
        if storage is None:
            self.log("unknown storage_id: %s" % storage_id)
            raise ValueError("unknown storage: %s" % storage_id)

        if not read_only and (self.read_only or storage.isReadOnly()):
            raise ReadOnlyError()

        self.read_only = self.read_only or read_only
        self.storage_id = storage_id
        self.storage = storage
        self.setup_delegation()
        self.timeout, self.stats = self.server.register_connection(storage_id,
                                                                   self)

    def get_info(self):
        return {'length': len(self.storage),
                'size': self.storage.getSize(),
                'name': self.storage.getName(),
                'supportsUndo': self.storage.supportsUndo(),
                'supportsVersions': self.storage.supportsVersions(),
                'extensionMethods': self.getExtensionMethods(),
                }

    def get_size_info(self):
        return {'length': len(self.storage),
                'size': self.storage.getSize(),
                }

    def getExtensionMethods(self):
        return self._extensions

    def loadEx(self, oid, version):
        self.stats.loads += 1
        return self.storage.loadEx(oid, version)

    def loadBefore(self, oid, tid):
        self.stats.loads += 1
        return self.storage.loadBefore(oid, tid)

    def zeoLoad(self, oid):
        self.stats.loads += 1
        v = self.storage.modifiedInVersion(oid)
        if v:
            pv, sv = self.storage.load(oid, v)
        else:
            pv = sv = None
        try:
            p, s = self.storage.load(oid, '')
        except KeyError:
            if sv:
                # Created in version, no non-version data
                p = s = None
            else:
                raise
        return p, s, v, pv, sv

    def getInvalidations(self, tid):
        invtid, invlist = self.server.get_invalidations(tid)
        if invtid is None:
            return None
        self.log("Return %d invalidations up to tid %s"
                 % (len(invlist), u64(invtid)))
        return invtid, invlist

    def verify(self, oid, version, tid):
        try:
            t = self.storage.getTid(oid)
        except KeyError:
            self.client.invalidateVerify((oid, ""))
        else:
            if tid != t:
                # This will invalidate non-version data when the
                # client only has invalid version data.  Since this is
                # an uncommon case, we avoid the cost of checking
                # whether the serial number matches the current
                # non-version data.
                self.client.invalidateVerify((oid, version))

    def zeoVerify(self, oid, s, sv):
        if not self.verifying:
            self.verifying = 1
            self.stats.verifying_clients += 1
        try:
            os = self.storage.getTid(oid)
        except KeyError:
            self.client.invalidateVerify((oid, ''))
            # It's not clear what we should do now.  The KeyError
            # could be caused by an object uncreation, in which case
            # invalidation is right.  It could be an application bug
            # that left a dangling reference, in which case it's bad.
        else:
            # If the client has version data, the logic is a bit more
            # complicated.  If the current serial number matches the
            # client serial number, then the non-version data must
            # also be valid.  If the current serialno is for a
            # version, then the non-version data can't change.

            # If the version serialno isn't valid, then the
            # non-version serialno may or may not be valid.  Rather
            # than trying to figure it whether it is valid, we just
            # invalidate it.  Sending an invalidation for the
            # non-version data implies invalidating the version data
            # too, since an update to non-version data can only occur
            # after the version is aborted or committed.
            if sv:
                if sv != os:
                    self.client.invalidateVerify((oid, ''))
            else:
                if s != os:
                    self.client.invalidateVerify((oid, ''))

    def endZeoVerify(self):
        if self.verifying:
            self.stats.verifying_clients -= 1
        self.verifying = 0
        self.client.endVerify()

    def pack(self, time, wait=1):
        # Yes, you can pack a read-only server or storage!
        if wait:
            return run_in_thread(self._pack_impl, time)
        else:
            # If the client isn't waiting for a reply, start a thread
            # and forget about it.
            t = threading.Thread(target=self._pack_impl, args=(time,))
            t.start()
            return None

    def _pack_impl(self, time):
        self.log("pack(time=%s) started..." % repr(time))
        self.storage.pack(time, referencesf)
        self.log("pack(time=%s) complete" % repr(time))
        # Broadcast new size statistics
        self.server.invalidate(0, self.storage_id, None,
                               (), self.get_size_info())

    def new_oids(self, n=100):
        """Return a sequence of n new oids, where n defaults to 100"""
        if self.read_only:
            raise ReadOnlyError()
        if n <= 0:
            n = 1
        return [self.storage.new_oid() for i in range(n)]

    # undoLog and undoInfo are potentially slow methods

    def undoInfo(self, first, last, spec):
        return run_in_thread(self.storage.undoInfo, first, last, spec)

    def undoLog(self, first, last):
        return run_in_thread(self.storage.undoLog, first, last)

    def tpc_begin(self, id, user, description, ext, tid=None, status=" "):
        if self.read_only:
            raise ReadOnlyError()
        if self.transaction is not None:
            if self.transaction.id == id:
                self.log("duplicate tpc_begin(%s)" % repr(id))
                return
            else:
                raise StorageTransactionError("Multiple simultaneous tpc_begin"
                                              " requests from one client.")

        self.transaction = t = transaction.Transaction()
        t.id = id
        t.user = user
        t.description = description
        t._extension = ext

        self.serials = []
        self.invalidated = []
        self.txnlog = CommitLog()
        self.tid = tid
        self.status = status
        self.store_failed = 0
        self.stats.active_txns += 1

    def tpc_finish(self, id):
        if not self._check_tid(id):
            return
        assert self.locked
        self.stats.active_txns -= 1
        self.stats.commits += 1
        self.storage.tpc_finish(self.transaction)
        tid = self.storage.lastTransaction()
        if self.invalidated:
            self.server.invalidate(self, self.storage_id, tid,
                                   self.invalidated, self.get_size_info())
        self._clear_transaction()
        # Return the tid, for cache invalidation optimization
        return tid

    def tpc_abort(self, id):
        if not self._check_tid(id):
            return
        self.stats.active_txns -= 1
        self.stats.aborts += 1
        if self.locked:
            self.storage.tpc_abort(self.transaction)
        self._clear_transaction()

    def _clear_transaction(self):
        # Common code at end of tpc_finish() and tpc_abort()
        self.transaction = None
        self.txnlog.close()
        if self.locked:
            self.locked = 0
            self.timeout.end(self)
            self.stats.lock_time = None
            self.log("Transaction released storage lock", BLATHER)
            # _handle_waiting() can start another transaction (by
            # restarting a waiting one) so must be done last
            self._handle_waiting()

    def _abort(self):
        # called when a connection is closed unexpectedly
        if not self.locked:
            # Delete (d, zeo_storage) from the _waiting list, if found.
            waiting = self.storage._waiting
            for i in range(len(waiting)):
                d, z = waiting[i]
                if z is self:
                    del waiting[i]
                    self.log("Closed connection removed from waiting list."
                             " Clients waiting: %d." % len(waiting))
                    break

        if self.transaction:
            self.stats.active_txns -= 1
            self.stats.aborts += 1
            self.tpc_abort(self.transaction.id)

    # The public methods of the ZEO client API do not do the real work.
    # They defer work until after the storage lock has been acquired.
    # Most of the real implementations are in methods beginning with
    # an _.

    def storea(self, oid, serial, data, version, id):
        self._check_tid(id, exc=StorageTransactionError)
        self.stats.stores += 1
        self.txnlog.store(oid, serial, data, version)

    # The following four methods return values, so they must acquire
    # the storage lock and begin the transaction before returning.

    def vote(self, id):
        self._check_tid(id, exc=StorageTransactionError)
        if self.locked:
            return self._vote()
        else:
            return self._wait(lambda: self._vote())

    def abortVersion(self, src, id):
        self._check_tid(id, exc=StorageTransactionError)
        if self.locked:
            return self._abortVersion(src)
        else:
            return self._wait(lambda: self._abortVersion(src))

    def commitVersion(self, src, dest, id):
        self._check_tid(id, exc=StorageTransactionError)
        if self.locked:
            return self._commitVersion(src, dest)
        else:
            return self._wait(lambda: self._commitVersion(src, dest))

    def undo(self, trans_id, id):
        self._check_tid(id, exc=StorageTransactionError)
        if self.locked:
            return self._undo(trans_id)
        else:
            return self._wait(lambda: self._undo(trans_id))

    def _tpc_begin(self, txn, tid, status):
        self.locked = 1
        self.timeout.begin(self)
        self.stats.lock_time = time.time()
        self.storage.tpc_begin(txn, tid, status)

    def _store(self, oid, serial, data, version):
        err = None
        try:
            newserial = self.storage.store(oid, serial, data, version,
                                           self.transaction)
        except (SystemExit, KeyboardInterrupt):
            raise
        except Exception, err:
            self.store_failed = 1
            if isinstance(err, ConflictError):
                self.stats.conflicts += 1
                self.log("conflict error oid=%s msg=%s" %
                         (oid_repr(oid), str(err)), BLATHER)
            if not isinstance(err, TransactionError):
                # Unexpected errors are logged and passed to the client
                self.log("store error: %s, %s" % sys.exc_info()[:2],
                         logging.ERROR, exc_info=True)
            # Try to pickle the exception.  If it can't be pickled,
            # the RPC response would fail, so use something else.
            pickler = cPickle.Pickler()
            pickler.fast = 1
            try:
                pickler.dump(err, 1)
            except:
                msg = "Couldn't pickle storage exception: %s" % repr(err)
                self.log(msg, logging.ERROR)
                err = StorageServerError(msg)
            # The exception is reported back as newserial for this oid
            newserial = err
        else: