Commit dfee4fcc authored by Vincent Pelletier's avatar Vincent Pelletier

Replace isinstance(node, ...Node) by node.getNodeType() == ..._NODE_TYPE (and...

Replace isinstance(node, ...Node) by node.getNodeType() == ..._NODE_TYPE (and similar). This makes things easier to unit test.
Add missing imports.


git-svn-id: https://svn.erp5.org/repos/neo/branches/prototype3@303 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent 47d42d56
......@@ -94,7 +94,7 @@ class ClientEventHandler(EventHandler):
else:
# Connection to a storage node failed
node = app.nm.getNodeByServer(conn.getAddress())
if isinstance(node, StorageNode):
if node.getNodeType() == STORAGE_NODE_TYPE:
self._dealWithStorageFailure(conn, node, TEMPORARILY_DOWN_STATE)
EventHandler.connectionFailed(self, conn)
......@@ -114,7 +114,7 @@ class ClientEventHandler(EventHandler):
self.dispatcher.connectToPrimaryMasterNode(app, conn)
else:
node = app.nm.getNodeByServer(conn.getAddress())
if isinstance(node, StorageNode):
if node.getNodeType() == STORAGE_NODE_TYPE:
# Notify primary master node that a storage node is temporarily down
logging.info("connection to storage node %s closed",
node.getServer())
......@@ -133,7 +133,7 @@ class ClientEventHandler(EventHandler):
self.dispatcher.connectToPrimaryMasterNode(app, conn)
else:
node = app.nm.getNodeByServer(conn.getAddress())
if isinstance(node, StorageNode):
if node.getNodeType() == STORAGE_NODE_TYPE:
# Notify primary master node that a storage node is
# temporarily down.
self._dealWithStorageFailure(conn, node, TEMPORARILY_DOWN_STATE)
......@@ -151,7 +151,7 @@ class ClientEventHandler(EventHandler):
self.dispatcher.connectToPrimaryMasterNode(app, conn)
else:
node = app.nm.getNodeByServer(conn.getAddress())
if isinstance(node, StorageNode):
if node.getNodeType() == STORAGE_NODE_TYPE:
self._dealWithStorageFailure(conn, node, BROKEN_STATE)
EventHandler.peerBroken(self, conn)
......@@ -222,7 +222,7 @@ class ClientEventHandler(EventHandler):
app = self.app
node = app.nm.getNodeByUUID(uuid)
# This must be sent only by primary master node
if not isinstance(node, MasterNode):
if node.getNodeType() != MASTER_NODE_TYPE:
return
# Register new master nodes.
for ip_address, port, uuid in known_master_list:
......@@ -268,7 +268,7 @@ class ClientEventHandler(EventHandler):
pt = app.pt
node = app.nm.getNodeByUUID(uuid)
# This must be sent only by primary master node
if not isinstance(node, MasterNode):
if node.getNodeType() != MASTER_NODE_TYPE:
return
if app.ptid != ptid:
......@@ -298,7 +298,7 @@ class ClientEventHandler(EventHandler):
# This must be sent only by a primary master node.
# Note that this may be sent before I know that it is
# a primary master node.
if not isinstance(node, MasterNode):
if node.getNodeType() != MASTER_NODE_TYPE:
logging.warn('ignoring notify node information from %s',
dump(uuid))
return
......@@ -358,7 +358,7 @@ class ClientEventHandler(EventHandler):
app = self.app
node = app.nm.getNodeByUUID(uuid)
# This must be sent only by primary master node
if not isinstance(node, MasterNode) \
if node.getNodeType() != MASTER_NODE_TYPE \
or app.primary_master_node is None \
or app.primary_master_node.getUUID() != uuid:
return
......
......@@ -23,7 +23,8 @@ from struct import pack, unpack
from neo.config import ConfigurationManager
from neo.protocol import Packet, \
RUNNING_STATE, TEMPORARILY_DOWN_STATE, DOWN_STATE, BROKEN_STATE, \
INVALID_UUID, INVALID_OID, INVALID_TID, INVALID_PTID, CLIENT_NODE_TYPE
INVALID_UUID, INVALID_OID, INVALID_TID, INVALID_PTID, \
CLIENT_NODE_TYPE, MASTER_NODE_TYPE, STORAGE_NODE_TYPE
from neo.node import NodeManager, MasterNode, StorageNode, ClientNode
from neo.event import EventManager
from neo.connection import ListeningConnection, ClientConnection, ServerConnection
......@@ -295,12 +296,12 @@ class Application(object):
for c in self.em.getConnectionList():
if c.getUUID() is not None:
n = self.nm.getNodeByUUID(c.getUUID())
if isinstance(n, (MasterNode, StorageNode)):
if n.getNodeType() in (MASTER_NODE_TYPE, STORAGE_NODE_TYPE):
p = Packet()
node_list = [(node_type, ip_address, port, uuid, state)]
p.notifyNodeInformation(c.getNextId(), node_list)
c.addPacket(p)
elif isinstance(node, (MasterNode, StorageNode)):
elif node.getNodeType() in (MASTER_NODE_TYPE, STORAGE_NODE_TYPE):
for c in self.em.getConnectionList():
if c.getUUID() is not None:
p = Packet()
......@@ -316,7 +317,7 @@ class Application(object):
for c in self.em.getConnectionList():
if c.getUUID() is not None:
n = self.nm.getNodeByUUID(c.getUUID())
if isinstance(n, (ClientNode, StorageNode)):
if n.getNodeType() in (CLIENT_NODE_TYPE, STORAGE_NODE_TYPE):
# Split the packet if too big.
size = len(cell_list)
start = 0
......@@ -358,7 +359,7 @@ class Application(object):
uuid = conn.getUUID()
if uuid is not None:
node = nm.getNodeByUUID(uuid)
if isinstance(node, StorageNode) \
if node.getNodeType() == STORAGE_NODE_TYPE \
and node.getState() == RUNNING_STATE:
p = Packet()
msg_id = conn.getNextId()
......@@ -539,7 +540,7 @@ class Application(object):
uuid = conn.getUUID()
if uuid is not None:
node = nm.getNodeByUUID(uuid)
if isinstance(node, StorageNode):
if node.getNodeType() == STORAGE_NODE_TYPE:
# Split the packet if too huge.
p = Packet()
row_list = []
......@@ -577,7 +578,7 @@ class Application(object):
uuid = conn.getUUID()
if uuid is not None:
node = nm.getNodeByUUID(uuid)
if isinstance(node, StorageNode):
if node.getNodeType() == STORAGE_NODE_TYPE:
self.asking_uuid_dict[uuid] = False
p = Packet()
msg_id = conn.getNextId()
......@@ -603,7 +604,7 @@ class Application(object):
uuid = conn.getUUID()
if uuid is not None:
node = nm.getNodeByUUID(uuid)
if isinstance(node, StorageNode):
if node.getNodeType() == STORAGE_NODE_TYPE:
p = Packet()
p.deleteTransaction(conn.getNextId(), tid)
conn.addPacket(p)
......@@ -658,7 +659,7 @@ class Application(object):
uuid = conn.getUUID()
if uuid is not None:
node = nm.getNodeByUUID(uuid)
if isinstance(node, StorageNode):
if node.getNodeType() == STORAGE_NODE_TYPE:
conn.addPacket(Packet().startOperation(conn.getNextId()))
# Now everything is passive.
......@@ -696,9 +697,9 @@ class Application(object):
uuid = conn.getUUID()
if uuid is not None:
node = nm.getNodeByUUID(uuid)
if isinstance(node, (StorageNode, ClientNode)):
if node.getNodeType() in (STORAGE_NODE_TYPE, CLIENT_NODE_TYPE):
conn.addPacket(Packet().stopOperation(conn.getNextId()))
if isinstance(node, ClientNode):
if node.getNodeType() == CLIENT_NODE_TYPE:
conn.abort()
# Then, go back, and restart.
......
......@@ -104,7 +104,7 @@ class RecoveryEventHandler(MasterEventHandler):
# address but with a different UUID.
if node.getUUID() is None:
# This must be a master node.
if not isinstance(node, MasterNode) or node_type != MASTER_NODE_TYPE:
if node.getNodeType() != MASTER_NODE_TYPE or node_type != MASTER_NODE_TYPE:
# Error. This node uses the same server address as a master
# node.
conn.addPacket(Packet().protocolError(packet.getId(),
......@@ -210,7 +210,7 @@ class RecoveryEventHandler(MasterEventHandler):
# If this is a storage node, ask the last IDs.
node = app.nm.getNodeByUUID(uuid)
if isinstance(node, StorageNode):
if node.getNodeType() == STORAGE_NODE_TYPE:
p = Packet()
msg_id = conn.getNextId()
p.askLastIDs(msg_id)
......@@ -292,7 +292,7 @@ class RecoveryEventHandler(MasterEventHandler):
app = self.app
node = app.nm.getNodeByUUID(uuid)
if not isinstance(node, StorageNode):
if node.getNodeType() != STORAGE_NODE_TYPE:
self.handleUnexpectedPacket(conn, packet)
return
......@@ -320,7 +320,7 @@ class RecoveryEventHandler(MasterEventHandler):
app = self.app
node = app.nm.getNodeByUUID(uuid)
if not isinstance(node, StorageNode):
if node.getNodeType() != STORAGE_NODE_TYPE:
self.handleUnexpectedPacket(conn, packet)
return
if uuid != app.target_uuid:
......
......@@ -20,7 +20,7 @@ from copy import copy
from neo.protocol import MASTER_NODE_TYPE, CLIENT_NODE_TYPE, \
RUNNING_STATE, BROKEN_STATE, TEMPORARILY_DOWN_STATE, DOWN_STATE, \
UP_TO_DATE_STATE, FEEDING_STATE, DISCARDED_STATE
UP_TO_DATE_STATE, FEEDING_STATE, DISCARDED_STATE, STORAGE_NODE_TYPE
from neo.master.handler import MasterEventHandler
from neo.protocol import Packet, INVALID_UUID
from neo.exception import OperationFailure, ElectionFailure
......@@ -77,13 +77,13 @@ class ServiceEventHandler(MasterEventHandler):
node.setState(TEMPORARILY_DOWN_STATE)
logging.debug('broadcasting node information')
app.broadcastNodeInformation(node)
if isinstance(node, ClientNode):
if node.getNodeType() == CLIENT_NODE_TYPE:
# If this node is a client, just forget it.
app.nm.remove(node)
for tid, t in app.finishing_transaction_dict.items():
if t.getConnection() is conn:
del app.finishing_transaction_dict[tid]
elif isinstance(node, StorageNode):
elif node.getNodeType() == STORAGE_NODE_TYPE:
if not app.pt.operational():
# Catastrophic.
raise OperationFailure, 'cannot continue operation'
......@@ -98,13 +98,13 @@ class ServiceEventHandler(MasterEventHandler):
node.setState(TEMPORARILY_DOWN_STATE)
logging.debug('broadcasting node information')
app.broadcastNodeInformation(node)
if isinstance(node, ClientNode):
if node.getNodeType() == CLIENT_NODE_TYPE:
# If this node is a client, just forget it.
app.nm.remove(node)
for tid, t in app.finishing_transaction_dict.items():
if t.getConnection() is conn:
del app.finishing_transaction_dict[tid]
elif isinstance(node, StorageNode):
elif node.getNodeType() == STORAGE_NODE_TYPE:
if not app.pt.operational():
# Catastrophic.
raise OperationFailure, 'cannot continue operation'
......@@ -119,13 +119,13 @@ class ServiceEventHandler(MasterEventHandler):
node.setState(BROKEN_STATE)
logging.debug('broadcasting node information')
app.broadcastNodeInformation(node)
if isinstance(node, ClientNode):
if node.getNodeType() == CLIENT_NODE_TYPE:
# If this node is a client, just forget it.
app.nm.remove(node)
for tid, t in app.finishing_transaction_dict.items():
if t.getConnection() is conn:
del app.finishing_transaction_dict[tid]
elif isinstance(node, StorageNode):
elif node.getNodeType() == STORAGE_NODE_TYPE:
cell_list = app.pt.dropNode(node)
ptid = app.getNextPartitionTableID()
app.broadcastPartitionChanges(ptid, cell_list)
......@@ -179,7 +179,7 @@ class ServiceEventHandler(MasterEventHandler):
# server address but with a different UUID.
if node.getUUID() is None:
# This must be a master node. XXX Why ??
if not isinstance(node, MasterNode) \
if node.getNodeType() != MASTER_NODE_TYPE \
or node_type != MASTER_NODE_TYPE:
# Error. This node uses the same server address as
# a master node.
......@@ -266,7 +266,7 @@ class ServiceEventHandler(MasterEventHandler):
conn.setUUID(uuid)
if isinstance(node, StorageNode):
if node.getNodeType() == STORAGE_NODE_TYPE:
# If this is a storage node, add it into the partition table.
# Note that this does no harm, even if the node is not new.
if old_node is not None:
......@@ -328,7 +328,7 @@ class ServiceEventHandler(MasterEventHandler):
# If this is a storage node or a client node, send the partition table.
node = app.nm.getNodeByUUID(uuid)
if isinstance(node, (StorageNode, ClientNode)):
if node.getNodeType() in (STORAGE_NODE_TYPE, CLIENT_NODE_TYPE):
logging.debug('sending send partition table to %s:%d',
*(conn.getAddress()))
# Split the packet if too huge.
......@@ -345,7 +345,7 @@ class ServiceEventHandler(MasterEventHandler):
conn.addPacket(p)
# If this is a storage node, ask it to start.
if isinstance(node, StorageNode):
if node.getNodeType() == STORAGE_NODE_TYPE:
conn.addPacket(Packet().startOperation(conn.getNextId()))
def handleAnnouncePrimaryMaster(self, conn, packet):
......@@ -417,7 +417,7 @@ class ServiceEventHandler(MasterEventHandler):
logging.debug('broadcasting node information')
app.broadcastNodeInformation(node)
if isinstance(node, StorageNode) \
if node.getNodeType() == STORAGE_NODE_TYPE \
and state in (DOWN_STATE, BROKEN_STATE):
cell_list = app.pt.dropNode(node)
if len(cell_list) != 0:
......@@ -433,7 +433,7 @@ class ServiceEventHandler(MasterEventHandler):
app = self.app
node = app.nm.getNodeByUUID(uuid)
if not isinstance(node, StorageNode):
if node.getNodeType() != STORAGE_NODE_TYPE:
self.handleUnexpectedPacket(conn, packet)
return
......@@ -451,7 +451,7 @@ class ServiceEventHandler(MasterEventHandler):
app = self.app
node = app.nm.getNodeByUUID(uuid)
if not isinstance(node, ClientNode):
if node.getNodeType() != CLIENT_NODE_TYPE:
self.handleUnexpectedPacket(conn, packet)
return
tid = app.getNextTID()
......@@ -467,7 +467,7 @@ class ServiceEventHandler(MasterEventHandler):
app = self.app
node = app.nm.getNodeByUUID(uuid)
if not isinstance(node, ClientNode):
if node.getNodeType() != CLIENT_NODE_TYPE:
self.handleUnexpectedPacket(conn, packet)
return
......@@ -483,7 +483,7 @@ class ServiceEventHandler(MasterEventHandler):
app = self.app
node = app.nm.getNodeByUUID(uuid)
if not isinstance(node, ClientNode):
if node.getNodeType() != CLIENT_NODE_TYPE:
self.handleUnexpectedPacket(conn, packet)
return
......@@ -530,7 +530,7 @@ class ServiceEventHandler(MasterEventHandler):
app = self.app
node = app.nm.getNodeByUUID(uuid)
if not isinstance(node, StorageNode):
if node.getNodeType() != STORAGE_NODE_TYPE:
self.handleUnexpectedPacket(conn, packet)
return
......@@ -553,7 +553,7 @@ class ServiceEventHandler(MasterEventHandler):
uuid = c.getUUID()
if uuid is not None:
node = app.nm.getNodeByUUID(uuid)
if isinstance(node, ClientNode):
if node.getNodeType() == CLIENT_NODE_TYPE:
if c is t.getConnection():
p.notifyTransactionFinished(t.getMessageId(),
tid)
......@@ -562,7 +562,7 @@ class ServiceEventHandler(MasterEventHandler):
p.invalidateObjects(c.getNextId(),
t.getOIDList(), tid)
c.addPacket(p)
elif isinstance(node, StorageNode):
elif node.getNodeType() == STORAGE_NODE_TYPE:
if uuid in t.getUUIDSet():
p.unlockInformation(c.getNextId(), tid)
c.addPacket(p)
......@@ -580,7 +580,7 @@ class ServiceEventHandler(MasterEventHandler):
app = self.app
node = app.nm.getNodeByUUID(uuid)
if not isinstance(node, ClientNode):
if node.getNodeType() != CLIENT_NODE_TYPE:
self.handleUnexpectedPacket(conn, packet)
return
......@@ -627,7 +627,7 @@ class ServiceEventHandler(MasterEventHandler):
self.handleUnexpectedPacket(conn, packet)
return
if not isinstance(node, StorageNode):
if node.getNodeType() != STORAGE_NODE_TYPE:
self.handleUnexpectedPacket(conn, packet)
return
......
......@@ -36,10 +36,10 @@ class VerificationEventHandler(MasterEventHandler):
if node.getState() == RUNNING_STATE:
node.setState(TEMPORARILY_DOWN_STATE)
app.broadcastNodeInformation(node)
if isinstance(node, ClientNode):
if node.getNodeType() == CLIENT_NODE_TYPE:
# If this node is a client, just forget it.
app.nm.remove(node)
elif isinstance(node, StorageNode):
elif node.getNodeType() == STORAGE_NODE_TYPE:
if not app.pt.operational():
# Catastrophic.
raise VerificationFailure, 'cannot continue verification'
......@@ -53,10 +53,10 @@ class VerificationEventHandler(MasterEventHandler):
if node.getState() == RUNNING_STATE:
node.setState(TEMPORARILY_DOWN_STATE)
app.broadcastNodeInformation(node)
if isinstance(node, ClientNode):
if node.getNodeType() == CLIENT_NODE_TYPE:
# If this node is a client, just forget it.
app.nm.remove(node)
elif isinstance(node, StorageNode):
elif node.getNodeType() == STORAGE_NODE_TYPE:
if not app.pt.operational():
# Catastrophic.
raise VerificationFailure, 'cannot continue verification'
......@@ -70,10 +70,10 @@ class VerificationEventHandler(MasterEventHandler):
if node.getState() != BROKEN_STATE:
node.setState(BROKEN_STATE)
app.broadcastNodeInformation(node)
if isinstance(node, ClientNode):
if node.getNodeType() == CLIENT_NODE_TYPE:
# If this node is a client, just forget it.
app.nm.remove(node)
elif isinstance(node, StorageNode):
elif node.getNodeType() == STORAGE_NODE_TYPE:
cell_list = app.pt.dropNode(node)
ptid = app.getNextPartitionTableID()
app.broadcastPartitionChanges(ptid, cell_list)
......@@ -127,7 +127,7 @@ class VerificationEventHandler(MasterEventHandler):
# address but with a different UUID.
if node.getUUID() is None:
# This must be a master node.
if not isinstance(node, MasterNode) or node_type != MASTER_NODE_TYPE:
if node.getNodeType() != MASTER_NODE_TYPE or node_type != MASTER_NODE_TYPE:
# Error. This node uses the same server address as a master
# node.
conn.addPacket(Packet().protocolError(packet.getId(),
......@@ -233,7 +233,7 @@ class VerificationEventHandler(MasterEventHandler):
# If this is a storage node, send the partition table.
node = app.nm.getNodeByUUID(uuid)
if isinstance(node, StorageNode):
if node.getNodeType() == STORAGE_NODE_TYPE:
# Split the packet if too huge.
p = Packet()
row_list = []
......@@ -322,7 +322,7 @@ class VerificationEventHandler(MasterEventHandler):
app = self.app
node = app.nm.getNodeByUUID(uuid)
if not isinstance(node, StorageNode):
if node.getNodeType() != STORAGE_NODE_TYPE:
self.handleUnexpectedPacket(conn, packet)
return
......@@ -345,7 +345,7 @@ class VerificationEventHandler(MasterEventHandler):
tid_list, *(conn.getAddress()))
app = self.app
node = app.nm.getNodeByUUID(uuid)
if not isinstance(node, StorageNode):
if node.getNodeType() != STORAGE_NODE_TYPE:
self.handleUnexpectedPacket(conn, packet)
return
......@@ -367,7 +367,7 @@ class VerificationEventHandler(MasterEventHandler):
oid_list, tid, *(conn.getAddress()))
app = self.app
node = app.nm.getNodeByUUID(uuid)
if not isinstance(node, StorageNode):
if node.getNodeType() != STORAGE_NODE_TYPE:
self.handleUnexpectedPacket(conn, packet)
return
......@@ -395,7 +395,7 @@ class VerificationEventHandler(MasterEventHandler):
logging.info('TID not found: %s', message)
app = self.app
node = app.nm.getNodeByUUID(uuid)
if not isinstance(node, StorageNode):
if node.getNodeType() != STORAGE_NODE_TYPE:
self.handleUnexpectedPacket(conn, packet)
return
......@@ -415,7 +415,7 @@ class VerificationEventHandler(MasterEventHandler):
logging.info('object %s:%s found', dump(oid), dump(tid))
app = self.app
node = app.nm.getNodeByUUID(uuid)
if not isinstance(node, StorageNode):
if node.getNodeType() != STORAGE_NODE_TYPE:
self.handleUnexpectedPacket(conn, packet)
return
......@@ -434,7 +434,7 @@ class VerificationEventHandler(MasterEventHandler):
logging.info('OID not found: %s', message)
app = self.app
node = app.nm.getNodeByUUID(uuid)
if not isinstance(node, StorageNode):
if node.getNodeType() != STORAGE_NODE_TYPE:
self.handleUnexpectedPacket(conn, packet)
return
......
......@@ -133,13 +133,13 @@ class NodeManager(object):
return [n for n in self.node_list if filter(n)]
def getMasterNodeList(self):
return self.getNodeList(filter = lambda node: isinstance(node, MasterNode))
return self.getNodeList(filter = lambda node: node.getNodeType() == MASTER_NODE_TYPE)
def getStorageNodeList(self):
return self.getNodeList(filter = lambda node: isinstance(node, StorageNode))
return self.getNodeList(filter = lambda node: node.getNodeType() == STORAGE_NODE_TYPE)
def getClientNodeList(self):
return self.getNodeList(filter = lambda node: isinstance(node, ClientNode))
return self.getNodeList(filter = lambda node: node.getNodeType() == CLIENT_NODE_TYPE)
def getNodeByServer(self, server):
return self.server_dict.get(server)
......
......@@ -84,7 +84,7 @@ class StorageEventHandler(EventHandler):
if node is None:
raise RuntimeError('I do not know the uuid %r' % dump(uuid))
if not isinstance(node, MasterNode):
if node.getNodeType() != MASTER_NODE_TYPE:
self.handleUnexpectedPacket(conn, packet)
return
......@@ -118,7 +118,7 @@ class StorageEventHandler(EventHandler):
app = self.app
node = app.nm.getNodeByUUID(uuid)
if not isinstance(node, MasterNode) \
if node.getNodeType() != MASTER_NODE_TYPE \
or app.primary_master_node is None \
or app.primary_master_node.getUUID() != uuid:
return
......
......@@ -73,7 +73,7 @@ class OperationEventHandler(StorageEventHandler):
if uuid is not None:
app = self.app
node = app.nm.getNodeByUUID(uuid)
if isinstance(node, ClientNode):
if node.getNodeType() == CLIENT_NODE_TYPE:
for tid, t in app.transaction_dict.items():
if t.getUUID() == uuid:
for o in t.getObjectList():
......
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