Commit e6893692 authored by Guido van Rossum's avatar Guido van Rossum

Simple-minded cleanup pass.

- Remove unused imports.

- Got rid of \ continuation lines.

- Remove leading underscores from all names of methods and instance
  variables of ZEOStorage.  There was no usage consistency and I don't
  see any other reason to maintain the existing names.
parent 374c7e96
...@@ -32,11 +32,10 @@ from ZEO.zrpc.server import Dispatcher ...@@ -32,11 +32,10 @@ from ZEO.zrpc.server import Dispatcher
from ZEO.zrpc.connection import ManagedServerConnection, Delay, MTDelay from ZEO.zrpc.connection import ManagedServerConnection, Delay, MTDelay
import zLOG import zLOG
from ZODB.POSException import StorageError, StorageTransactionError, \ from ZODB.POSException import StorageError, StorageTransactionError
TransactionError, ReadOnlyError from ZODB.POSException import TransactionError, ReadOnlyError
from ZODB.referencesf import referencesf from ZODB.referencesf import referencesf
from ZODB.Transaction import Transaction from ZODB.Transaction import Transaction
from ZODB.TmpStore import TmpStore
# We create a special fast pickler! This allows us # We create a special fast pickler! This allows us
# to create slightly more efficient pickles and # to create slightly more efficient pickles and
...@@ -121,62 +120,63 @@ class ZEOStorage: ...@@ -121,62 +120,63 @@ class ZEOStorage:
def __init__(self, server): def __init__(self, server):
self.server = server self.server = server
self.client = None self.client = None
self._conn = None # the connection associated with client self.conn = None # the connection associated with client
self.__storage = None self.storage = None
self.__storage_id = "uninitialized" self.storage_id = "uninitialized"
self._transaction = None self.transaction = None
def notifyConnected(self, conn): def notifyConnected(self, conn):
self._conn = conn self.conn = conn
self.client = ClientStub.ClientStorage(conn) self.client = ClientStub.ClientStorage(conn)
def notifyDisconnected(self): def notifyDisconnected(self):
# When this storage closes, we must ensure that it aborts # When this storage closes, we must ensure that it aborts
# any pending transaction. Not sure if this is the cleanest way. # any pending transaction. Not sure if this is the cleanest way.
if self._transaction is not None: if self.transaction is not None:
self._log("disconnected during transaction %s" % self._transaction) self.log("disconnected during transaction %s" % self.transaction)
self.tpc_abort(self._transaction.id) self.tpc_abort(self.transaction.id)
else: else:
self._log("disconnected") self.log("disconnected")
def __repr__(self): def __repr__(self):
tid = self._transaction and repr(self._transaction.id) tid = self.transaction and repr(self.transaction.id)
if self.__storage: if self.storage:
stid = self.__storage._transaction and \ stid = (self.storage._transaction and
repr(self.__storage._transaction.id) repr(self.storage._transaction.id))
else: else:
stid = None stid = None
name = self.__class__.__name__ name = self.__class__.__name__
return "<%s %X trans=%s s_trans=%s>" % (name, id(self), tid, stid) return "<%s %X trans=%s s_trans=%s>" % (name, id(self), tid, stid)
def _log(self, msg, level=zLOG.INFO, error=None): def log(self, msg, level=zLOG.INFO, error=None):
name = getattr(self.__storage, '__name__', None) name = getattr(self.storage, '__name__', None)
if name is None: if name is None:
name = str(self.__storage) name = str(self.storage)
zLOG.LOG("%s:%s" % (_label, name), level, msg, error=error) zLOG.LOG("%s:%s" % (_label, name), level, msg, error=error)
def setup_delegation(self): def setup_delegation(self):
"""Delegate several methods to the storage""" """Delegate several methods to the storage"""
self.versionEmpty = self.__storage.versionEmpty self.versionEmpty = self.storage.versionEmpty
self.versions = self.__storage.versions self.versions = self.storage.versions
self.history = self.__storage.history self.history = self.storage.history
self.load = self.__storage.load self.load = self.storage.load
self.loadSerial = self.__storage.loadSerial self.loadSerial = self.storage.loadSerial
self.modifiedInVersion = self.__storage.modifiedInVersion self.modifiedInVersion = self.storage.modifiedInVersion
def _check_tid(self, tid, exc=None): def check_tid(self, tid, exc=None):
caller = sys._getframe().f_back.f_code.co_name caller = sys._getframe().f_back.f_code.co_name
if self._transaction is None: if self.transaction is None:
self._log("no current transaction: %s()" % caller, zLOG.PROBLEM) self.log("no current transaction: %s()" % caller, zLOG.PROBLEM)
if exc is not None: if exc is not None:
raise exc(None, tid) raise exc(None, tid)
else: else:
return 0 return 0
if self._transaction.id != tid: if self.transaction.id != tid:
self._log("%s(%s) invalid; current transaction = %s" % \ self.log("%s(%s) invalid; current transaction = %s" %
(caller, repr(tid), repr(self._transaction.id)), zLOG.PROBLEM) (caller, repr(tid), repr(self.transaction.id)),
zLOG.PROBLEM)
if exc is not None: if exc is not None:
raise exc(self._transaction.id, tid) raise exc(self.transaction.id, tid)
else: else:
return 0 return 0
return 1 return 1
...@@ -186,44 +186,44 @@ class ZEOStorage: ...@@ -186,44 +186,44 @@ class ZEOStorage:
This method must be the first one called by the client. This method must be the first one called by the client.
""" """
self._log("register(%r, %s)" % (storage_id, read_only)) self.log("register(%r, %s)" % (storage_id, read_only))
storage = self.server.storages.get(storage_id) storage = self.server.storages.get(storage_id)
if storage is None: if storage is None:
self._log("unknown storage_id: %s" % storage_id) self.log("unknown storage_id: %s" % storage_id)
raise ValueError, "unknown storage: %s" % storage_id raise ValueError, "unknown storage: %s" % storage_id
if not read_only and (self.server.read_only or storage.isReadOnly()): if not read_only and (self.server.read_only or storage.isReadOnly()):
raise ReadOnlyError() raise ReadOnlyError()
self.__storage_id = storage_id self.storage_id = storage_id
self.__storage = storage self.storage = storage
self.setup_delegation() self.setup_delegation()
self.server.register_connection(storage_id, self) self.server.register_connection(storage_id, self)
self._log("registered storage %r: %s" % (storage_id, storage)) self.log("registered storage %r: %s" % (storage_id, storage))
def get_info(self): def get_info(self):
return {'length': len(self.__storage), return {'length': len(self.storage),
'size': self.__storage.getSize(), 'size': self.storage.getSize(),
'name': self.__storage.getName(), 'name': self.storage.getName(),
'supportsUndo': self.__storage.supportsUndo(), 'supportsUndo': self.storage.supportsUndo(),
'supportsVersions': self.__storage.supportsVersions(), 'supportsVersions': self.storage.supportsVersions(),
'supportsTransactionalUndo': 'supportsTransactionalUndo':
self.__storage.supportsTransactionalUndo(), self.storage.supportsTransactionalUndo(),
} }
def get_size_info(self): def get_size_info(self):
return {'length': len(self.__storage), return {'length': len(self.storage),
'size': self.__storage.getSize(), 'size': self.storage.getSize(),
} }
def zeoLoad(self, oid): def zeoLoad(self, oid):
v = self.__storage.modifiedInVersion(oid) v = self.storage.modifiedInVersion(oid)
if v: if v:
pv, sv = self.__storage.load(oid, v) pv, sv = self.storage.load(oid, v)
else: else:
pv = sv = None pv = sv = None
try: try:
p, s = self.__storage.load(oid, '') p, s = self.storage.load(oid, '')
except KeyError: except KeyError:
if sv: if sv:
# Created in version, no non-version data # Created in version, no non-version data
...@@ -250,29 +250,29 @@ class ZEOStorage: ...@@ -250,29 +250,29 @@ class ZEOStorage:
def pack(self, time, wait=None): def pack(self, time, wait=None):
if wait is not None: if wait is not None:
return run_in_thread(self._pack, time) return run_in_thread(self.pack_impl, time)
else: else:
# If the client isn't waiting for a reply, start a thread # If the client isn't waiting for a reply, start a thread
# and forget about it. # and forget about it.
t = threading.Thread(target=self._pack, args=(time,)) t = threading.Thread(target=self.pack_impl, args=(time,))
t.start() t.start()
return None return None
def _pack(self, time): def pack_impl(self, time):
self.__storage.pack(time, referencesf) self.storage.pack(time, referencesf)
# Broadcast new size statistics # Broadcast new size statistics
self.server.invalidate(0, self.__storage_id, (), self.get_size_info()) self.server.invalidate(0, self.storage_id, (), self.get_size_info())
def new_oids(self, n=100): def new_oids(self, n=100):
"""Return a sequence of n new oids, where n defaults to 100""" """Return a sequence of n new oids, where n defaults to 100"""
if n <= 0: if n <= 0:
n = 1 n = 1
return [self.__storage.new_oid() for i in range(n)] return [self.storage.new_oid() for i in range(n)]
def undo(self, transaction_id): def undo(self, transaction_id):
oids = self.__storage.undo(transaction_id) oids = self.storage.undo(transaction_id)
if oids: if oids:
self.server.invalidate(self, self.__storage_id, self.server.invalidate(self, self.storage_id,
map(lambda oid: (oid, ''), oids)) map(lambda oid: (oid, ''), oids))
return oids return oids
return () return ()
...@@ -280,26 +280,26 @@ class ZEOStorage: ...@@ -280,26 +280,26 @@ class ZEOStorage:
# undoLog and undoInfo are potentially slow methods # undoLog and undoInfo are potentially slow methods
def undoInfo(self, first, last, spec): def undoInfo(self, first, last, spec):
return run_in_thread(self.__storage.undoInfo, first, last, spec) return run_in_thread(self.storage.undoInfo, first, last, spec)
def undoLog(self, first, last): def undoLog(self, first, last):
return run_in_thread(self.__storage.undoLog, first, last) return run_in_thread(self.storage.undoLog, first, last)
def tpc_begin(self, id, user, description, ext, tid, status): def tpc_begin(self, id, user, description, ext, tid, status):
if self._transaction is not None: if self.transaction is not None:
if self._transaction.id == id: if self.transaction.id == id:
self._log("duplicate tpc_begin(%s)" % repr(id)) self.log("duplicate tpc_begin(%s)" % repr(id))
return return
else: else:
raise StorageTransactionError("Multiple simultaneous tpc_begin" raise StorageTransactionError("Multiple simultaneous tpc_begin"
" requests from one client.") " requests from one client.")
# (This doesn't require a lock because we're using asyncore) # (This doesn't require a lock because we're using asyncore)
if self.__storage._transaction is None: if self.storage._transaction is None:
self.strategy = ImmediateCommitStrategy(self.__storage, self.strategy = ImmediateCommitStrategy(self.storage,
self.client) self.client)
else: else:
self.strategy = DelayedCommitStrategy(self.__storage, self.strategy = DelayedCommitStrategy(self.storage,
self.wait) self.wait)
t = Transaction() t = Transaction()
...@@ -309,56 +309,56 @@ class ZEOStorage: ...@@ -309,56 +309,56 @@ class ZEOStorage:
t._extension = ext t._extension = ext
self.strategy.tpc_begin(t, tid, status) self.strategy.tpc_begin(t, tid, status)
self._transaction = t self.transaction = t
def tpc_finish(self, id): def tpc_finish(self, id):
if not self._check_tid(id): if not self.check_tid(id):
return return
invalidated = self.strategy.tpc_finish() invalidated = self.strategy.tpc_finish()
if invalidated: if invalidated:
self.server.invalidate(self, self.__storage_id, self.server.invalidate(self, self.storage_id,
invalidated, self.get_size_info()) invalidated, self.get_size_info())
self._transaction = None self.transaction = None
self.strategy = None self.strategy = None
self._handle_waiting() self.handle_waiting()
def tpc_abort(self, id): def tpc_abort(self, id):
if not self._check_tid(id): if not self.check_tid(id):
return return
strategy = self.strategy strategy = self.strategy
strategy.tpc_abort() strategy.tpc_abort()
self._transaction = None self.transaction = None
self.strategy = None self.strategy = None
# When ZEOStorage.notifyDisconnected() calls self.tpc_abort(), # When ZEOStorage.notifyDisconnected() calls self.tpc_abort(),
# it is possible that self.strategy is DelayedCommitStrategy. # it is possible that self.strategy is DelayedCommitStrategy.
# In that case, ZEOStorage.tpc_abort() should *not* call # In that case, ZEOStorage.tpc_abort() should *not* call
# self._handle_waiting(), otherwise there could be two # self.handle_waiting(), otherwise there could be two
# ZEOStorage instances whose strategy is # ZEOStorage instances whose strategy is
# ImmediateCommitStrategy! # ImmediateCommitStrategy!
if isinstance(strategy, ImmediateCommitStrategy): if isinstance(strategy, ImmediateCommitStrategy):
self._handle_waiting() self.handle_waiting()
# XXX else, should we remove ourselves from storage._waiting??? # XXX else, should we remove ourselves from storage._waiting???
# XXX handle new serialnos # XXX handle new serialnos
def storea(self, oid, serial, data, version, id): def storea(self, oid, serial, data, version, id):
self._check_tid(id, exc=StorageTransactionError) self.check_tid(id, exc=StorageTransactionError)
self.strategy.store(oid, serial, data, version) self.strategy.store(oid, serial, data, version)
def vote(self, id): def vote(self, id):
self._check_tid(id, exc=StorageTransactionError) self.check_tid(id, exc=StorageTransactionError)
return self.strategy.tpc_vote() return self.strategy.tpc_vote()
def abortVersion(self, src, id): def abortVersion(self, src, id):
self._check_tid(id, exc=StorageTransactionError) self.check_tid(id, exc=StorageTransactionError)
return self.strategy.abortVersion(src) return self.strategy.abortVersion(src)
def commitVersion(self, src, dest, id): def commitVersion(self, src, dest, id):
self._check_tid(id, exc=StorageTransactionError) self.check_tid(id, exc=StorageTransactionError)
return self.strategy.commitVersion(src, dest) return self.strategy.commitVersion(src, dest)
def transactionalUndo(self, trans_id, id): def transactionalUndo(self, trans_id, id):
self._check_tid(id, exc=StorageTransactionError) self.check_tid(id, exc=StorageTransactionError)
return self.strategy.transactionalUndo(trans_id) return self.strategy.transactionalUndo(trans_id)
# When a delayed transaction is restarted, the dance is # When a delayed transaction is restarted, the dance is
...@@ -372,35 +372,35 @@ class ZEOStorage: ...@@ -372,35 +372,35 @@ class ZEOStorage:
# client will be blocked until it finishes. # client will be blocked until it finishes.
def wait(self): def wait(self):
if self.__storage._transaction: if self.storage._transaction:
d = Delay() d = Delay()
self.__storage._waiting.append((d, self)) self.storage._waiting.append((d, self))
self._log("Transaction blocked waiting for storage. " self.log("Transaction blocked waiting for storage. "
"Clients waiting: %d." % len(self.__storage._waiting)) "Clients waiting: %d." % len(self.storage._waiting))
return d return d
else: else:
self.restart() self.restart()
return None return None
def _handle_waiting(self): def handle_waiting(self):
while self.__storage._waiting: while self.storage._waiting:
delay, zeo_storage = self.__storage._waiting.pop(0) delay, zeo_storage = self.storage._waiting.pop(0)
if self._restart(zeo_storage, delay): if self.restart_other(zeo_storage, delay):
if self.__storage._waiting: if self.storage._waiting:
n = len(self.__storage._waiting) n = len(self.storage._waiting)
self._log("Blocked transaction restarted. " self.log("Blocked transaction restarted. "
"Clients waiting: %d" % n) "Clients waiting: %d" % n)
else: else:
self._log("Blocked transaction restarted.") self.log("Blocked transaction restarted.")
return return
def _restart(self, zeo_storage, delay): def restart_other(self, zeo_storage, delay):
# Return True if the server restarted. # Return True if the server restarted.
# call the restart() method on the appropriate server. # call the restart() method on the appropriate server.
try: try:
zeo_storage.restart(delay) zeo_storage.restart(delay)
except: except:
self._log("Unexpected error handling waiting transaction", self.log("Unexpected error handling waiting transaction",
level=zLOG.WARNING, error=sys.exc_info()) level=zLOG.WARNING, error=sys.exc_info())
zeo_storage._conn.close() zeo_storage._conn.close()
return 0 return 0
...@@ -410,7 +410,7 @@ class ZEOStorage: ...@@ -410,7 +410,7 @@ class ZEOStorage:
def restart(self, delay=None): def restart(self, delay=None):
old_strategy = self.strategy old_strategy = self.strategy
assert isinstance(old_strategy, DelayedCommitStrategy) assert isinstance(old_strategy, DelayedCommitStrategy)
self.strategy = ImmediateCommitStrategy(self.__storage, self.strategy = ImmediateCommitStrategy(self.storage,
self.client) self.client)
resp = old_strategy.restart(self.strategy) resp = old_strategy.restart(self.strategy)
if delay is not None: if delay is not None:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment