Commit 48e32052 authored by Grégory Wisniewski's avatar Grégory Wisniewski

Group error packets as done for other packets.

git-svn-id: https://svn.erp5.org/repos/neo/trunk@1790 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent 3154624a
...@@ -27,7 +27,7 @@ from neo.connector import getConnectorHandler ...@@ -27,7 +27,7 @@ from neo.connector import getConnectorHandler
from neo.bootstrap import BootstrapManager from neo.bootstrap import BootstrapManager
from neo.pt import PartitionTable from neo.pt import PartitionTable
from neo import protocol from neo import protocol
from neo.protocol import NodeTypes, NodeStates, Packets from neo.protocol import NodeTypes, NodeStates, Packets, Errors
class Dispatcher: class Dispatcher:
"""Dispatcher use to redirect master request to handler""" """Dispatcher use to redirect master request to handler"""
...@@ -160,7 +160,7 @@ class Application(object): ...@@ -160,7 +160,7 @@ class Application(object):
pass pass
row_list.append((offset, row)) row_list.append((offset, row))
except IndexError: except IndexError:
p = protocol.protocolError('invalid partition table offset') p = Errors.ProtocolError('invalid partition table offset')
conn.notify(p) conn.notify(p)
return return
p = Packets.AnswerPartitionList(self.ptid, row_list) p = Packets.AnswerPartitionList(self.ptid, row_list)
......
...@@ -19,7 +19,7 @@ from neo import logging ...@@ -19,7 +19,7 @@ from neo import logging
from neo.handler import EventHandler from neo.handler import EventHandler
from neo import protocol from neo import protocol
from neo.protocol import Packets from neo.protocol import Packets, Errors
from neo.exception import PrimaryFailure from neo.exception import PrimaryFailure
from neo.util import dump from neo.util import dump
...@@ -62,7 +62,7 @@ class AdminEventHandler(EventHandler): ...@@ -62,7 +62,7 @@ class AdminEventHandler(EventHandler):
raise protocol.ProtocolError('invalid uuid') raise protocol.ProtocolError('invalid uuid')
if node.getState() == state and modify_partition_table is False: if node.getState() == state and modify_partition_table is False:
# no change # no change
p = protocol.ack('no change') p = Errors.Ack('no change')
conn.answer(p) conn.answer(p)
return return
# forward to primary master node # forward to primary master node
...@@ -213,8 +213,8 @@ class MasterRequestEventHandler(EventHandler): ...@@ -213,8 +213,8 @@ class MasterRequestEventHandler(EventHandler):
Packets.AnswerNodeState(uuid, state)) Packets.AnswerNodeState(uuid, state))
def ack(self, conn, msg): def ack(self, conn, msg):
self.__answerNeoCTL(conn, protocol.ack(msg)) self.__answerNeoCTL(conn, Errors.Ack(msg))
def protocolError(self, conn, msg): def protocolError(self, conn, msg):
self.__answerNeoCTL(conn, protocol.protocolError(msg)) self.__answerNeoCTL(conn, Errors.ProtocolError(msg))
...@@ -16,8 +16,7 @@ ...@@ -16,8 +16,7 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
from neo import logging from neo import logging
from neo import protocol from neo.protocol import NodeStates, ErrorCodes, Packets, Errors
from neo.protocol import NodeStates, ErrorCodes, Packets
from neo.protocol import PacketMalformedError, UnexpectedPacketError, \ from neo.protocol import PacketMalformedError, UnexpectedPacketError, \
BrokenNodeDisallowedError, NotReadyError, ProtocolError BrokenNodeDisallowedError, NotReadyError, ProtocolError
...@@ -34,7 +33,7 @@ class EventHandler(object): ...@@ -34,7 +33,7 @@ class EventHandler(object):
"""Called when a packet is malformed.""" """Called when a packet is malformed."""
args = (conn.getAddress()[0], conn.getAddress()[1], message) args = (conn.getAddress()[0], conn.getAddress()[1], message)
logging.error('malformed packet from %s:%d: %s', *args) logging.error('malformed packet from %s:%d: %s', *args)
response = protocol.protocolError(message) response = Errors.ProtocolError(message)
conn.notify(response) conn.notify(response)
conn.abort() conn.abort()
self.peerBroken(conn) self.peerBroken(conn)
...@@ -48,7 +47,7 @@ class EventHandler(object): ...@@ -48,7 +47,7 @@ class EventHandler(object):
message = 'unexpected packet: %s in %s' % (message, message = 'unexpected packet: %s in %s' % (message,
self.__class__.__name__) self.__class__.__name__)
logging.error(message) logging.error(message)
conn.answer(protocol.protocolError(message)) conn.answer(Errors.ProtocolError(message))
conn.abort() conn.abort()
self.peerBroken(conn) self.peerBroken(conn)
...@@ -67,24 +66,24 @@ class EventHandler(object): ...@@ -67,24 +66,24 @@ class EventHandler(object):
except PacketMalformedError, e: except PacketMalformedError, e:
self._packetMalformed(conn, *e.args) self._packetMalformed(conn, *e.args)
except BrokenNodeDisallowedError: except BrokenNodeDisallowedError:
conn.answer(protocol.brokenNodeDisallowedError('go away')) conn.answer(Errors.Broken('go away'))
conn.abort() conn.abort()
except NotReadyError, message: except NotReadyError, message:
if not message.args: if not message.args:
message = 'Retry Later' message = 'Retry Later'
message = str(message) message = str(message)
conn.answer(protocol.notReady(message)) conn.answer(Errors.NotReady(message))
conn.abort() conn.abort()
except ProtocolError, message: except ProtocolError, message:
message = str(message) message = str(message)
conn.answer(protocol.protocolError(message)) conn.answer(Errors.ProtocolError(message))
conn.abort() conn.abort()
def checkClusterName(self, name): def checkClusterName(self, name):
# raise an exception if the fiven name mismatch the current cluster name # raise an exception if the fiven name mismatch the current cluster name
if self.app.name != name: if self.app.name != name:
logging.error('reject an alien cluster') logging.error('reject an alien cluster')
raise protocol.ProtocolError('invalid cluster name') raise ProtocolError('invalid cluster name')
# Network level handlers # Network level handlers
......
...@@ -17,9 +17,9 @@ ...@@ -17,9 +17,9 @@
from neo import logging from neo import logging
from neo import protocol
from neo.master.handlers import MasterHandler from neo.master.handlers import MasterHandler
from neo.protocol import ClusterStates, NodeStates, Packets, ProtocolError from neo.protocol import ClusterStates, NodeStates, Packets, ProtocolError
from neo.protocol import Errors
from neo.util import dump from neo.util import dump
CLUSTER_STATE_WORKFLOW = { CLUSTER_STATE_WORKFLOW = {
...@@ -57,7 +57,7 @@ class AdministrationHandler(MasterHandler): ...@@ -57,7 +57,7 @@ class AdministrationHandler(MasterHandler):
self.app.changeClusterState(state) self.app.changeClusterState(state)
# answer # answer
conn.answer(protocol.ack('cluster state changed')) conn.answer(Errors.Ack('cluster state changed'))
if state == ClusterStates.STOPPING: if state == ClusterStates.STOPPING:
self.app.cluster_state = state self.app.cluster_state = state
self.app.shutdown() self.app.shutdown()
...@@ -74,13 +74,13 @@ class AdministrationHandler(MasterHandler): ...@@ -74,13 +74,13 @@ class AdministrationHandler(MasterHandler):
node.setState(state) node.setState(state)
# get message for self # get message for self
if state != NodeStates.RUNNING: if state != NodeStates.RUNNING:
p = protocol.ack('node state changed') p = Errors.Ack('node state changed')
conn.answer(p) conn.answer(p)
app.shutdown() app.shutdown()
if node.getState() == state: if node.getState() == state:
# no change, just notify admin node # no change, just notify admin node
p = protocol.ack('node state changed') p = Errors.Ack('node state changed')
conn.answer(p) conn.answer(p)
return return
...@@ -119,7 +119,7 @@ class AdministrationHandler(MasterHandler): ...@@ -119,7 +119,7 @@ class AdministrationHandler(MasterHandler):
node.setState(state) node.setState(state)
# /!\ send the node information *after* the partition table change # /!\ send the node information *after* the partition table change
p = protocol.ack('state changed') p = Errors.Ack('state changed')
conn.answer(p) conn.answer(p)
app.broadcastNodesInformation([node]) app.broadcastNodesInformation([node])
...@@ -139,7 +139,7 @@ class AdministrationHandler(MasterHandler): ...@@ -139,7 +139,7 @@ class AdministrationHandler(MasterHandler):
# nothing to do # nothing to do
if not uuid_set: if not uuid_set:
logging.warning('No nodes added') logging.warning('No nodes added')
p = protocol.ack('no nodes added') p = Errors.Ack('no nodes added')
conn.answer(p) conn.answer(p)
return return
uuids = ', '.join([dump(uuid) for uuid in uuid_set]) uuids = ', '.join([dump(uuid) for uuid in uuid_set])
...@@ -158,5 +158,5 @@ class AdministrationHandler(MasterHandler): ...@@ -158,5 +158,5 @@ class AdministrationHandler(MasterHandler):
s_conn.notify(Packets.StartOperation()) s_conn.notify(Packets.StartOperation())
# broadcast the new partition table # broadcast the new partition table
app.broadcastPartitionChanges(cell_list) app.broadcastPartitionChanges(cell_list)
p = protocol.ack('node added') p = Errors.Ack('node added')
conn.answer(p) conn.answer(p)
#
# Copyright (C) 2006-2010 Nexedi SA # Copyright (C) 2006-2010 Nexedi SA
# #
# This program is free software; you can redistribute it and/or # This program is free software; you can redistribute it and/or
...@@ -1406,26 +1406,25 @@ class PacketRegistry(dict): ...@@ -1406,26 +1406,25 @@ class PacketRegistry(dict):
# build a "singleton" # build a "singleton"
Packets = PacketRegistry() Packets = PacketRegistry()
def register(code):
def wrapper(registry, message=''):
return Error(code, message)
return wrapper
def _error(error_code, error_message): class ErrorRegistry(dict):
return Error(error_code, error_message) """
Error packet packet registry
def ack(message): """
return _error(ErrorCodes.ACK, message)
def protocolError(error_message):
return _error(ErrorCodes.PROTOCOL_ERROR, 'protocol error: ' + error_message)
def notReady(error_message):
return _error(ErrorCodes.NOT_READY, 'not ready: ' + error_message)
def brokenNodeDisallowedError(error_message): def __init__(self):
return _error(ErrorCodes.BROKEN_NODE, dict.__init__(self)
'broken node disallowed error: ' + error_message)
def oidNotFound(error_message): Ack = register(ErrorCodes.ACK)
return _error(ErrorCodes.OID_NOT_FOUND, 'oid not found: ' + error_message) ProtocolError = register(ErrorCodes.PROTOCOL_ERROR)
TidNotFound = register(ErrorCodes.TID_NOT_FOUND)
OidNotFound = register(ErrorCodes.OID_NOT_FOUND)
NotReady = register(ErrorCodes.NOT_READY)
Broken = register(ErrorCodes.BROKEN_NODE)
def tidNotFound(error_message): Errors = ErrorRegistry()
return _error(ErrorCodes.TID_NOT_FOUND, 'tid not found: ' + error_message)
...@@ -21,7 +21,7 @@ from neo.handler import EventHandler ...@@ -21,7 +21,7 @@ from neo.handler import EventHandler
from neo import protocol from neo import protocol
from neo.util import dump from neo.util import dump
from neo.exception import PrimaryFailure, OperationFailure from neo.exception import PrimaryFailure, OperationFailure
from neo.protocol import NodeStates, Packets from neo.protocol import NodeStates, Packets, Errors
class BaseMasterHandler(EventHandler): class BaseMasterHandler(EventHandler):
...@@ -92,7 +92,7 @@ class BaseClientAndStorageOperationHandler(EventHandler): ...@@ -92,7 +92,7 @@ class BaseClientAndStorageOperationHandler(EventHandler):
app = self.app app = self.app
t = app.dm.getTransaction(tid) t = app.dm.getTransaction(tid)
if t is None: if t is None:
p = protocol.tidNotFound('%s does not exist' % dump(tid)) p = Errors.TidNotFound('%s does not exist' % dump(tid))
else: else:
p = Packets.AnswerTransactionInformation(tid, t[1], t[2], t[3], p = Packets.AnswerTransactionInformation(tid, t[1], t[2], t[3],
t[0]) t[0])
...@@ -113,6 +113,6 @@ class BaseClientAndStorageOperationHandler(EventHandler): ...@@ -113,6 +113,6 @@ class BaseClientAndStorageOperationHandler(EventHandler):
compression, checksum, data) compression, checksum, data)
else: else:
logging.debug('oid = %s not found', dump(oid)) logging.debug('oid = %s not found', dump(oid))
p = protocol.oidNotFound('%s does not exist' % dump(oid)) p = Errors.OidNotFound('%s does not exist' % dump(oid))
conn.answer(p) conn.answer(p)
...@@ -18,8 +18,7 @@ ...@@ -18,8 +18,7 @@
from neo import logging from neo import logging
from neo.storage.handlers import BaseMasterHandler from neo.storage.handlers import BaseMasterHandler
from neo import protocol from neo.protocol import Packets, Errors, ProtocolError
from neo.protocol import Packets
from neo.util import dump from neo.util import dump
from neo.exception import OperationFailure from neo.exception import OperationFailure
...@@ -38,7 +37,7 @@ class VerificationHandler(BaseMasterHandler): ...@@ -38,7 +37,7 @@ class VerificationHandler(BaseMasterHandler):
offset_list = xrange(0, self.app.pt.getPartitions()) offset_list = xrange(0, self.app.pt.getPartitions())
else: else:
if max(offset_list) >= self.app.pt.getPartitions(): if max(offset_list) >= self.app.pt.getPartitions():
raise protocol.ProtocolError('invalid partition table offset') raise ProtocolError('invalid partition table offset')
# build a table with requested partitions # build a table with requested partitions
row_list = [(offset, [(cell.getUUID(), cell.getState()) row_list = [(offset, [(cell.getUUID(), cell.getState())
...@@ -73,7 +72,7 @@ class VerificationHandler(BaseMasterHandler): ...@@ -73,7 +72,7 @@ class VerificationHandler(BaseMasterHandler):
app = self.app app = self.app
t = app.dm.getTransaction(tid, all=True) t = app.dm.getTransaction(tid, all=True)
if t is None: if t is None:
p = protocol.tidNotFound('%s does not exist' % dump(tid)) p = Errors.TidNotFound('%s does not exist' % dump(tid))
else: else:
p = Packets.AnswerTransactionInformation(tid, t[1], t[2], t[3], p = Packets.AnswerTransactionInformation(tid, t[1], t[2], t[3],
t[0]) t[0])
...@@ -83,7 +82,7 @@ class VerificationHandler(BaseMasterHandler): ...@@ -83,7 +82,7 @@ class VerificationHandler(BaseMasterHandler):
if self.app.dm.objectPresent(oid, tid): if self.app.dm.objectPresent(oid, tid):
p = Packets.AnswerObjectPresent(oid, tid) p = Packets.AnswerObjectPresent(oid, tid)
else: else:
p = protocol.oidNotFound( p = Errors.OidNotFound(
'%s:%s do not exist' % (dump(oid), dump(tid))) '%s:%s do not exist' % (dump(oid), dump(tid)))
conn.answer(p) conn.answer(p)
......
...@@ -21,8 +21,7 @@ from ZODB.POSException import StorageTransactionError, UndoError, ConflictError ...@@ -21,8 +21,7 @@ from ZODB.POSException import StorageTransactionError, UndoError, ConflictError
from neo.tests import NeoTestBase from neo.tests import NeoTestBase
from neo.client.app import Application from neo.client.app import Application
from neo.client.exception import NEOStorageError, NEOStorageNotFoundError from neo.client.exception import NEOStorageError, NEOStorageNotFoundError
from neo import protocol from neo.protocol import Packet, Packets, Errors, INVALID_TID, INVALID_SERIAL
from neo.protocol import Packets, INVALID_TID, INVALID_SERIAL
from neo.util import makeChecksum from neo.util import makeChecksum
def _getMasterConnection(self): def _getMasterConnection(self):
...@@ -78,7 +77,7 @@ class ClientApplicationTests(NeoTestBase): ...@@ -78,7 +77,7 @@ class ClientApplicationTests(NeoTestBase):
self.assertEquals(len(calls), 1) self.assertEquals(len(calls), 1)
# client connection got queue as first parameter # client connection got queue as first parameter
packet = calls[0].getParam(1) packet = calls[0].getParam(1)
self.assertTrue(isinstance(packet, protocol.Packet)) self.assertTrue(isinstance(packet, Packet))
self.assertEquals(packet.getType(), packet_type) self.assertEquals(packet.getType(), packet_type)
if decode: if decode:
return packet.decode() return packet.decode()
...@@ -215,7 +214,7 @@ class ClientApplicationTests(NeoTestBase): ...@@ -215,7 +214,7 @@ class ClientApplicationTests(NeoTestBase):
an_object = (1, oid, tid1, tid2, 0, makeChecksum('OBJ'), 'OBJ') an_object = (1, oid, tid1, tid2, 0, makeChecksum('OBJ'), 'OBJ')
# connection to SN close # connection to SN close
self.assertTrue(oid not in mq) self.assertTrue(oid not in mq)
packet = protocol.oidNotFound('') packet = Errors.OidNotFound('')
packet.setId(0) packet.setId(0)
cell = Mock({ 'getUUID': '\x00' * 16}) cell = Mock({ 'getUUID': '\x00' * 16})
conn = Mock({'getUUID': '\x10' * 16, conn = Mock({'getUUID': '\x10' * 16,
...@@ -232,7 +231,7 @@ class ClientApplicationTests(NeoTestBase): ...@@ -232,7 +231,7 @@ class ClientApplicationTests(NeoTestBase):
Application._waitMessage = _waitMessage Application._waitMessage = _waitMessage
# object not found in NEO -> NEOStorageNotFoundError # object not found in NEO -> NEOStorageNotFoundError
self.assertTrue(oid not in mq) self.assertTrue(oid not in mq)
packet = protocol.oidNotFound('') packet = Errors.OidNotFound('')
packet.setId(0) packet.setId(0)
cell = Mock({ 'getUUID': '\x00' * 16}) cell = Mock({ 'getUUID': '\x00' * 16})
conn = Mock({ conn = Mock({
...@@ -274,7 +273,7 @@ class ClientApplicationTests(NeoTestBase): ...@@ -274,7 +273,7 @@ class ClientApplicationTests(NeoTestBase):
tid2 = self.makeTID(2) tid2 = self.makeTID(2)
# object not found in NEO -> NEOStorageNotFoundError # object not found in NEO -> NEOStorageNotFoundError
self.assertTrue(oid not in mq) self.assertTrue(oid not in mq)
packet = protocol.oidNotFound('') packet = Errors.OidNotFound('')
packet.setId(0) packet.setId(0)
cell = Mock({ 'getUUID': '\x00' * 16}) cell = Mock({ 'getUUID': '\x00' * 16})
conn = Mock({ conn = Mock({
...@@ -313,7 +312,7 @@ class ClientApplicationTests(NeoTestBase): ...@@ -313,7 +312,7 @@ class ClientApplicationTests(NeoTestBase):
tid2 = self.makeTID(2) tid2 = self.makeTID(2)
# object not found in NEO -> NEOStorageNotFoundError # object not found in NEO -> NEOStorageNotFoundError
self.assertTrue(oid not in mq) self.assertTrue(oid not in mq)
packet = protocol.oidNotFound('') packet = Errors.OidNotFound('')
packet.setId(0) packet.setId(0)
cell = Mock({ 'getUUID': '\x00' * 16}) cell = Mock({ 'getUUID': '\x00' * 16})
conn = Mock({ conn = Mock({
...@@ -734,7 +733,7 @@ class ClientApplicationTests(NeoTestBase): ...@@ -734,7 +733,7 @@ class ClientApplicationTests(NeoTestBase):
self.askFinishTransaction(app) self.askFinishTransaction(app)
# undo 1 -> no previous revision # undo 1 -> no previous revision
u1p1 = Packets.AnswerTransactionInformation(tid1, '', '', '', (oid1, )) u1p1 = Packets.AnswerTransactionInformation(tid1, '', '', '', (oid1, ))
u1p2 = protocol.oidNotFound('oid not found') u1p2 = Errors.OidNotFound('oid not found')
# undo 2 -> not end tid # undo 2 -> not end tid
u2p1 = Packets.AnswerTransactionInformation(tid2, '', '', '', (oid2, )) u2p1 = Packets.AnswerTransactionInformation(tid2, '', '', '', (oid2, ))
u2p2 = Packets.AnswerObject(oid2, tid2, tid3, 0, makeChecksum('O2V1'), 'O2V1') u2p2 = Packets.AnswerObject(oid2, tid2, tid3, 0, makeChecksum('O2V1'), 'O2V1')
......
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
import unittest import unittest
from neo import protocol from neo import protocol
from neo.protocol import NodeTypes, NodeStates, CellStates from neo.protocol import NodeTypes, NodeStates, CellStates
from neo.protocol import ErrorCodes, Packets from neo.protocol import ErrorCodes, Packets, Errors
from neo.tests import NeoTestBase from neo.tests import NeoTestBase
class ProtocolTests(NeoTestBase): class ProtocolTests(NeoTestBase):
...@@ -34,33 +34,33 @@ class ProtocolTests(NeoTestBase): ...@@ -34,33 +34,33 @@ class ProtocolTests(NeoTestBase):
return self.ltid return self.ltid
def test_03_protocolError(self): def test_03_protocolError(self):
p = protocol.protocolError("bad protocol") p = Errors.ProtocolError("bad protocol")
error_code, error_msg = p.decode() error_code, error_msg = p.decode()
self.assertEqual(error_code, ErrorCodes.PROTOCOL_ERROR) self.assertEqual(error_code, ErrorCodes.PROTOCOL_ERROR)
self.assertEqual(error_msg, "protocol error: bad protocol") self.assertEqual(error_msg, "bad protocol")
def test_05_notReady(self): def test_05_notReady(self):
p = protocol.notReady("wait") p = Errors.NotReady("wait")
error_code, error_msg = p.decode() error_code, error_msg = p.decode()
self.assertEqual(error_code, ErrorCodes.NOT_READY) self.assertEqual(error_code, ErrorCodes.NOT_READY)
self.assertEqual(error_msg, "not ready: wait") self.assertEqual(error_msg, "wait")
def test_06_brokenNodeDisallowedError(self): def test_06_brokenNodeDisallowedError(self):
p = protocol.brokenNodeDisallowedError("broken") p = Errors.Broken("broken")
error_code, error_msg = p.decode() error_code, error_msg = p.decode()
self.assertEqual(error_code, ErrorCodes.BROKEN_NODE) self.assertEqual(error_code, ErrorCodes.BROKEN_NODE)
self.assertEqual(error_msg, "broken node disallowed error: broken") self.assertEqual(error_msg, "broken")
def test_07_oidNotFound(self): def test_07_oidNotFound(self):
p = protocol.oidNotFound("no oid") p = Errors.OidNotFound("no oid")
error_code, error_msg = p.decode() error_code, error_msg = p.decode()
self.assertEqual(error_msg, "oid not found: no oid") self.assertEqual(error_msg, "no oid")
def test_08_oidNotFound(self): def test_08_oidNotFound(self):
p = protocol.tidNotFound("no tid") p = Errors.TidNotFound("no tid")
error_code, error_msg = p.decode() error_code, error_msg = p.decode()
self.assertEqual(error_code, ErrorCodes.TID_NOT_FOUND) self.assertEqual(error_code, ErrorCodes.TID_NOT_FOUND)
self.assertEqual(error_msg, "tid not found: no tid") self.assertEqual(error_msg, "no tid")
def test_09_ping(self): def test_09_ping(self):
p = Packets.Ping() p = Packets.Ping()
......
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