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