Commit 868be91a authored by Grégory Wisniewski's avatar Grégory Wisniewski

Define is(Master|Storage|Client|Admin) method on Node class to avoid check with

constants from protocol module. Use them in some files, more to come.


git-svn-id: https://svn.erp5.org/repos/neo/branches/prototype3@961 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent 6910e589
...@@ -22,10 +22,8 @@ from struct import pack, unpack ...@@ -22,10 +22,8 @@ from struct import pack, unpack
from neo.config import ConfigurationManager from neo.config import ConfigurationManager
from neo import protocol from neo import protocol
from neo.protocol import \ from neo.protocol import RUNNING_STATE, TEMPORARILY_DOWN_STATE, DOWN_STATE, \
RUNNING_STATE, TEMPORARILY_DOWN_STATE, DOWN_STATE, \ UUID_NAMESPACES, BOOTING
CLIENT_NODE_TYPE, MASTER_NODE_TYPE, STORAGE_NODE_TYPE, \
UUID_NAMESPACES, ADMIN_NODE_TYPE, BOOTING
from neo.node import NodeManager, MasterNode, StorageNode, ClientNode, AdminNode from neo.node import NodeManager, MasterNode, StorageNode, ClientNode, AdminNode
from neo.event import EventManager from neo.event import EventManager
from neo.connection import ListeningConnection, ClientConnection, ServerConnection from neo.connection import ListeningConnection, ClientConnection, ServerConnection
...@@ -75,7 +73,7 @@ class Application(object): ...@@ -75,7 +73,7 @@ class Application(object):
self.primary_master_node = None self.primary_master_node = None
# Generate an UUID for self # Generate an UUID for self
self.uuid = self.getNewUUID(MASTER_NODE_TYPE) self.uuid = self.getNewUUID(protocol.MASTER_NODE_TYPE)
# The last OID. # The last OID.
self.loid = None self.loid = None
...@@ -275,20 +273,20 @@ class Application(object): ...@@ -275,20 +273,20 @@ class Application(object):
# The server address may be None. # The server address may be None.
address = node.getServer() address = node.getServer()
if node_type == CLIENT_NODE_TYPE: if node.isClient():
# Only to master nodes and storage nodes. # Only to master nodes and storage nodes.
for c in self.em.getConnectionList(): for c in self.em.getConnectionList():
if c.getUUID() is not None: if c.getUUID() is not None:
n = self.nm.getNodeByUUID(c.getUUID()) n = self.nm.getNodeByUUID(c.getUUID())
if n.getNodeType() in (MASTER_NODE_TYPE, STORAGE_NODE_TYPE, ADMIN_NODE_TYPE): if n.isMaster() or n.isStorage() or n.isAdmin():
node_list = [(node_type, address, uuid, state)] node_list = [(node_type, address, uuid, state)]
c.notify(protocol.notifyNodeInformation(node_list)) c.notify(protocol.notifyNodeInformation(node_list))
elif node.getNodeType() in (MASTER_NODE_TYPE, STORAGE_NODE_TYPE): elif node.isMaster() or node.isStorage():
for c in self.em.getConnectionList(): for c in self.em.getConnectionList():
if c.getUUID() is not None: if c.getUUID() is not None:
node_list = [(node_type, address, uuid, state)] node_list = [(node_type, address, uuid, state)]
c.notify(protocol.notifyNodeInformation(node_list)) c.notify(protocol.notifyNodeInformation(node_list))
elif node.getNodeType() != ADMIN_NODE_TYPE: elif not node.isAdmin():
raise RuntimeError('unknown node type') raise RuntimeError('unknown node type')
def broadcastPartitionChanges(self, ptid, cell_list): def broadcastPartitionChanges(self, ptid, cell_list):
...@@ -298,7 +296,7 @@ class Application(object): ...@@ -298,7 +296,7 @@ class Application(object):
for c in self.em.getConnectionList(): for c in self.em.getConnectionList():
if c.getUUID() is not None: if c.getUUID() is not None:
n = self.nm.getNodeByUUID(c.getUUID()) n = self.nm.getNodeByUUID(c.getUUID())
if n.getNodeType() in (CLIENT_NODE_TYPE, STORAGE_NODE_TYPE, ADMIN_NODE_TYPE): if n.isClient() or n.isStorage() or n.isAdmin():
# Split the packet if too big. # Split the packet if too big.
size = len(cell_list) size = len(cell_list)
start = 0 start = 0
...@@ -332,7 +330,7 @@ class Application(object): ...@@ -332,7 +330,7 @@ class Application(object):
""" Send informations on all nodes through the given connection """ """ Send informations on all nodes through the given connection """
node_list = [] node_list = []
for n in self.nm.getNodeList(): for n in self.nm.getNodeList():
if n.getNodeType() != ADMIN_NODE_TYPE: if not n.isAdmin():
try: try:
address = n.getServer() address = n.getServer()
except TypeError: except TypeError:
...@@ -488,7 +486,7 @@ class Application(object): ...@@ -488,7 +486,7 @@ class Application(object):
uuid = conn.getUUID() uuid = conn.getUUID()
if uuid is not None: if uuid is not None:
node = nm.getNodeByUUID(uuid) node = nm.getNodeByUUID(uuid)
if node.getNodeType() == STORAGE_NODE_TYPE: if node.isStorage():
self.asking_uuid_dict[uuid] = False self.asking_uuid_dict[uuid] = False
conn.ask(protocol.askUnfinishedTransactions()) conn.ask(protocol.askUnfinishedTransactions())
...@@ -510,7 +508,7 @@ class Application(object): ...@@ -510,7 +508,7 @@ class Application(object):
uuid = conn.getUUID() uuid = conn.getUUID()
if uuid is not None: if uuid is not None:
node = nm.getNodeByUUID(uuid) node = nm.getNodeByUUID(uuid)
if node.getNodeType() == STORAGE_NODE_TYPE: if node.isStorage():
conn.notify(protocol.deleteTransaction(tid)) conn.notify(protocol.deleteTransaction(tid))
else: else:
for conn in em.getConnectionList(): for conn in em.getConnectionList():
...@@ -574,10 +572,9 @@ class Application(object): ...@@ -574,10 +572,9 @@ class Application(object):
logging.critical('No longer operational, so stopping the service') logging.critical('No longer operational, so stopping the service')
for conn in em.getConnectionList(): for conn in em.getConnectionList():
node = nm.getNodeByUUID(conn.getUUID()) node = nm.getNodeByUUID(conn.getUUID())
if node is not None and node.getNodeType() in \ if node is not None and (node.isStorage() or node.isClient()):
(STORAGE_NODE_TYPE, CLIENT_NODE_TYPE):
conn.notify(protocol.stopOperation()) conn.notify(protocol.stopOperation())
if node.getNodeType() == CLIENT_NODE_TYPE: if node.isClient():
conn.abort() conn.abort()
# Then, go back, and restart. # Then, go back, and restart.
...@@ -646,16 +643,15 @@ class Application(object): ...@@ -646,16 +643,15 @@ class Application(object):
if conn.isListeningConnection() or node is None: if conn.isListeningConnection() or node is None:
# not identified or listening, keep the identification handler # not identified or listening, keep the identification handler
continue continue
node_type = node.getNodeType()
conn.notify(notification_packet) conn.notify(notification_packet)
if node_type in (ADMIN_NODE_TYPE, MASTER_NODE_TYPE): if node.isAdmin() or node.isMaster():
# those node types keep their own handler # those node types keep their own handler
continue continue
if node_type == CLIENT_NODE_TYPE: if node.isClient():
if state != protocol.RUNNING: if state != protocol.RUNNING:
conn.close() conn.close()
handler = client.ClientServiceHandler handler = client.ClientServiceHandler
elif node_type == STORAGE_NODE_TYPE: elif node.isStorage():
handler = storage_handler handler = storage_handler
handler = handler(self) handler = handler(self)
conn.setHandler(handler) conn.setHandler(handler)
...@@ -739,7 +735,7 @@ class Application(object): ...@@ -739,7 +735,7 @@ class Application(object):
logging.info("asking all clients to shutdown") logging.info("asking all clients to shutdown")
for c in self.em.getConnectionList(): for c in self.em.getConnectionList():
node = self.nm.getNodeByUUID(c.getUUID()) node = self.nm.getNodeByUUID(c.getUUID())
if node.getType() == CLIENT_NODE_TYPE: if node.isClient():
node_list = [(node.getType(), node.getServer(), node_list = [(node.getType(), node.getServer(),
node.getUUID(), DOWN_STATE)] node.getUUID(), DOWN_STATE)]
c.notify(protocol.notifyNodeInformation(node_list)) c.notify(protocol.notifyNodeInformation(node_list))
...@@ -747,7 +743,7 @@ class Application(object): ...@@ -747,7 +743,7 @@ class Application(object):
logging.info("asking all remaining nodes to shutdown") logging.info("asking all remaining nodes to shutdown")
for c in self.em.getConnectionList(): for c in self.em.getConnectionList():
node = self.nm.getNodeByUUID(c.getUUID()) node = self.nm.getNodeByUUID(c.getUUID())
if node.getType() in (STORAGE_NODE_TYPE, MASTER_NODE_TYPE): if node.isStorage() or node.isMaster():
node_list = [(node.getType(), node.getServer(), node_list = [(node.getType(), node.getServer(),
node.getUUID(), DOWN_STATE)] node.getUUID(), DOWN_STATE)]
c.notify(protocol.notifyNodeInformation(node_list)) c.notify(protocol.notifyNodeInformation(node_list))
......
...@@ -18,10 +18,8 @@ ...@@ -18,10 +18,8 @@
import logging import logging
from neo import protocol from neo import protocol
from neo.protocol import CLIENT_NODE_TYPE, RUNNING_STATE, \ from neo.protocol import UP_TO_DATE_STATE, FEEDING_STATE, \
UP_TO_DATE_STATE, FEEDING_STATE, DISCARDED_STATE, \ DISCARDED_STATE, OUT_OF_DATE_STATE, INTERNAL_ERROR_CODE
STORAGE_NODE_TYPE, OUT_OF_DATE_STATE, \
INTERNAL_ERROR_CODE
from neo.master.handlers import BaseServiceHandler from neo.master.handlers import BaseServiceHandler
from neo.protocol import UnexpectedPacketError from neo.protocol import UnexpectedPacketError
from neo.exception import OperationFailure from neo.exception import OperationFailure
...@@ -33,7 +31,7 @@ class StorageServiceHandler(BaseServiceHandler): ...@@ -33,7 +31,7 @@ class StorageServiceHandler(BaseServiceHandler):
def connectionCompleted(self, conn): def connectionCompleted(self, conn):
node = self.app.nm.getNodeByUUID(conn.getUUID()) node = self.app.nm.getNodeByUUID(conn.getUUID())
if node.getState() == RUNNING_STATE: if node.getState() == protocol.RUNNING_STATE:
conn.notify(protocol.startOperation()) conn.notify(protocol.startOperation())
def _nodeLost(self, conn, node): def _nodeLost(self, conn, node):
...@@ -70,14 +68,14 @@ class StorageServiceHandler(BaseServiceHandler): ...@@ -70,14 +68,14 @@ class StorageServiceHandler(BaseServiceHandler):
uuid = c.getUUID() uuid = c.getUUID()
if uuid is not None: if uuid is not None:
node = app.nm.getNodeByUUID(uuid) node = app.nm.getNodeByUUID(uuid)
if node.getNodeType() == CLIENT_NODE_TYPE: if node.isClient():
if c is t.getConnection(): if c is t.getConnection():
p = protocol.notifyTransactionFinished(tid) p = protocol.notifyTransactionFinished(tid)
c.notify(p, t.getMessageId()) c.notify(p, t.getMessageId())
else: else:
p = protocol.invalidateObjects(t.getOIDList(), tid) p = protocol.invalidateObjects(t.getOIDList(), tid)
c.notify(p) c.notify(p)
elif node.getNodeType() == STORAGE_NODE_TYPE: elif node.isStorage():
if uuid in t.getUUIDSet(): if uuid in t.getUUIDSet():
p = protocol.unlockInformation(tid) p = protocol.unlockInformation(tid)
c.notify(p) c.notify(p)
......
...@@ -86,26 +86,59 @@ class Node(object): ...@@ -86,26 +86,59 @@ class Node(object):
uuid = self.getUUID() uuid = self.getUUID()
return '%s (%s:%s)' % (dump(uuid), address, port) return '%s (%s:%s)' % (dump(uuid), address, port)
def isMaster(self):
return False
def isStorage(self):
return False
def isClient(self):
return False
def isAdmin(self):
return False
class MasterNode(Node): class MasterNode(Node):
"""This class represents a master node.""" """This class represents a master node."""
def getNodeType(self): def getNodeType(self):
return MASTER_NODE_TYPE return MASTER_NODE_TYPE
def isMaster(self):
return True
class StorageNode(Node): class StorageNode(Node):
"""This class represents a storage node.""" """This class represents a storage node."""
def getNodeType(self): def getNodeType(self):
return STORAGE_NODE_TYPE return STORAGE_NODE_TYPE
def isStorage(self):
return True
class ClientNode(Node): class ClientNode(Node):
"""This class represents a client node.""" """This class represents a client node."""
def getNodeType(self): def getNodeType(self):
return CLIENT_NODE_TYPE return CLIENT_NODE_TYPE
def isClient(self):
return True
class AdminNode(Node): class AdminNode(Node):
"""This class represents an admin node.""" """This class represents an admin node."""
def getNodeType(self): def getNodeType(self):
return ADMIN_NODE_TYPE return ADMIN_NODE_TYPE
def isAdmin(self):
return True
NODE_TYPE_MAPPING = { NODE_TYPE_MAPPING = {
protocol.MASTER_NODE_TYPE: MasterNode, protocol.MASTER_NODE_TYPE: MasterNode,
protocol.STORAGE_NODE_TYPE: StorageNode, protocol.STORAGE_NODE_TYPE: StorageNode,
...@@ -160,15 +193,15 @@ class NodeManager(object): ...@@ -160,15 +193,15 @@ class NodeManager(object):
return filter(node_filter, self.node_list) return filter(node_filter, self.node_list)
def getMasterNodeList(self): def getMasterNodeList(self):
node_filter = lambda node: node.getNodeType() == MASTER_NODE_TYPE node_filter = lambda node: node.isMaster()
return self.getNodeList(node_filter=node_filter) return self.getNodeList(node_filter=node_filter)
def getStorageNodeList(self): def getStorageNodeList(self):
node_filter = lambda node: node.getNodeType() == STORAGE_NODE_TYPE node_filter = lambda node: node.isStorage()
return self.getNodeList(node_filter=node_filter) return self.getNodeList(node_filter=node_filter)
def getClientNodeList(self): def getClientNodeList(self):
node_filter = lambda node: node.getNodeType() == CLIENT_NODE_TYPE node_filter = lambda node: node.isClient()
return self.getNodeList(node_filter=node_filter) return self.getNodeList(node_filter=node_filter)
def getNodeByServer(self, server): def getNodeByServer(self, server):
......
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