Commit 39ae4a2f authored by Julien Muchembled's avatar Julien Muchembled

Do not define exception classes in protocol.py

parent df2bf949
......@@ -15,11 +15,10 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from neo.lib import logging
from neo.lib.exception import NotReadyError, PrimaryFailure, ProtocolError
from neo.lib.handler import EventHandler
from neo.lib.protocol import uuid_str, \
NodeTypes, NotReadyError, Packets, ProtocolError
from neo.lib.protocol import uuid_str, NodeTypes, Packets
from neo.lib.pt import PartitionTable
from neo.lib.exception import PrimaryFailure
NOT_CONNECTED_MESSAGE = 'Not connected to a primary master.'
......
......@@ -21,9 +21,9 @@ from msgpack.exceptions import OutOfData, UnpackValueError
from . import attributeTracker, logging
from .connector import ConnectorException, ConnectorDelayedConnection
from .exception import PacketMalformedError
from .locking import RLock
from .protocol import uuid_str, Errors, PacketMalformedError, Packets, \
Unpacker
from .protocol import uuid_str, Errors, Packets, Unpacker
try:
msgpack.Unpacker().read_bytes(1)
......
......@@ -29,3 +29,30 @@ class StoppedOperation(NeoException):
class NodeNotReady(NeoException):
pass
class ProtocolError(NeoException):
""" Base class for protocol errors, close the connection """
class PacketMalformedError(ProtocolError):
pass
class UnexpectedPacketError(ProtocolError):
pass
class NotReadyError(ProtocolError):
pass
class BackendNotImplemented(NeoException):
""" Method not implemented by backend storage """
class NonReadableCell(NeoException):
"""Read-access to a cell that is actually non-readable
This happens in case of race condition at processing partition table
updates: client's PT is older or newer than storage's. The latter case is
possible because the master must validate any end of replication, which
means that the storage node can't anticipate the PT update (concurrently,
there may be a first tweaks that moves the replicated cell to another node,
and a second one that moves it back).
On such event, the client must retry, preferably another cell.
"""
......@@ -19,10 +19,9 @@ from collections import deque
from operator import itemgetter
from . import logging
from .connection import ConnectionClosed
from .exception import PrimaryElected
from .protocol import (NodeStates, NodeTypes, Packets, uuid_str,
Errors, BackendNotImplemented, NonReadableCell, NotReadyError,
PacketMalformedError, ProtocolError, UnexpectedPacketError)
from .exception import (BackendNotImplemented, NonReadableCell, NotReadyError,
PacketMalformedError, PrimaryElected, ProtocolError, UnexpectedPacketError)
from .protocol import NodeStates, NodeTypes, Packets, uuid_str, Errors
from .util import cached_property
......
......@@ -18,9 +18,9 @@ import errno, json, os
from time import time
from . import attributeTracker, logging
from .exception import NotReadyError, ProtocolError
from .handler import DelayEvent, EventQueue
from .protocol import formatNodeList, uuid_str, \
NodeTypes, NodeStates, NotReadyError, ProtocolError
from .protocol import formatNodeList, uuid_str, NodeTypes, NodeStates
class Node(object):
......
......@@ -233,34 +233,6 @@ uuid_str = (lambda ns: lambda uuid:
ns[uuid >> 24] + str(uuid & 0xffffff) if uuid else str(uuid)
)({v: str(k)[0] for k, v in UUID_NAMESPACES.iteritems()})
class ProtocolError(Exception):
""" Base class for protocol errors, close the connection """
class PacketMalformedError(ProtocolError):
"""Close the connection"""
class UnexpectedPacketError(ProtocolError):
"""Close the connection"""
class NotReadyError(ProtocolError):
""" Just close the connection """
class BackendNotImplemented(Exception):
""" Method not implemented by backend storage """
class NonReadableCell(Exception):
"""Read-access to a cell that is actually non-readable
This happens in case of race condition at processing partition table
updates: client's PT is older or newer than storage's. The latter case is
possible because the master must validate any end of replication, which
means that the storage node can't anticipate the PT update (concurrently,
there may be a first tweaks that moves the replicated cell to another node,
and a second one that moves it back).
On such event, the client must retry, preferably another cell.
"""
class Packet(object):
"""
......
......@@ -15,7 +15,8 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from neo.lib.handler import DelayEvent
from neo.lib.protocol import Packets, ProtocolError, MAX_TID, Errors
from neo.lib.exception import ProtocolError
from neo.lib.protocol import Packets, MAX_TID, Errors
from ..app import monotonic_time
from . import MasterHandler
......
......@@ -15,10 +15,10 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from neo.lib import logging
from neo.lib.exception import PrimaryElected
from neo.lib.exception import NotReadyError, PrimaryElected, ProtocolError
from neo.lib.handler import EventHandler
from neo.lib.protocol import CellStates, ClusterStates, NodeStates, \
NodeTypes, NotReadyError, Packets, ProtocolError, uuid_str
NodeTypes, Packets, uuid_str
from ..app import monotonic_time
class IdentificationHandler(EventHandler):
......
......@@ -15,9 +15,8 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from neo.lib import logging
from neo.lib.protocol import (CellStates, ClusterStates, Packets, ProtocolError,
uuid_str)
from neo.lib.exception import StoppedOperation
from neo.lib.exception import ProtocolError, StoppedOperation
from neo.lib.protocol import CellStates, ClusterStates, Packets, uuid_str
from neo.lib.pt import PartitionTableException
from neo.lib.util import dump
from . import BaseServiceHandler
......
......@@ -16,7 +16,8 @@
from neo.lib import logging
from neo.lib.connection import ClientConnection
from neo.lib.protocol import Packets, ProtocolError, ClusterStates, NodeStates
from neo.lib.exception import ProtocolError
from neo.lib.protocol import Packets, ClusterStates, NodeStates
from .app import monotonic_time
from .handlers import MasterHandler
......
......@@ -18,8 +18,9 @@ from collections import deque
from time import time
from struct import pack, unpack
from neo.lib import logging
from neo.lib.exception import ProtocolError
from neo.lib.handler import DelayEvent, EventQueue
from neo.lib.protocol import ProtocolError, uuid_str, ZERO_OID, ZERO_TID
from neo.lib.protocol import uuid_str, ZERO_OID, ZERO_TID
from neo.lib.util import dump, u64, addTID, tidFromTime
class Transaction(object):
......
......@@ -162,13 +162,14 @@ class Log(object):
self._protocol_date = date
g = {}
exec bz2.decompress(text) in g
for x in 'uuid_str', 'Packets', 'PacketMalformedError':
for x in 'uuid_str', 'Packets':
setattr(self, x, g[x])
x = {}
try:
Unpacker = g['Unpacker']
except KeyError:
unpackb = None
self.PacketMalformedError = g['PacketMalformedError']
else:
from msgpack import ExtraData, UnpackException
def unpackb(data):
......
......@@ -31,8 +31,9 @@ from ..app import option_defaults
from . import buildDatabaseManager, DatabaseFailure
from .manager import DatabaseManager, Fallback
from neo.lib import compress, logging, patch, util
from neo.lib.exception import BackendNotImplemented
from neo.lib.interfaces import implements
from neo.lib.protocol import BackendNotImplemented, MAX_TID
from neo.lib.protocol import MAX_TID
patch.speedupFileStorageTxnLookup()
......
......@@ -20,8 +20,9 @@ from contextlib import contextmanager
from copy import copy
from functools import wraps
from neo.lib import logging, util
from neo.lib.exception import NonReadableCell
from neo.lib.interfaces import abstract, requires
from neo.lib.protocol import CellStates, NonReadableCell, MAX_TID, ZERO_TID
from neo.lib.protocol import CellStates, MAX_TID, ZERO_TID
from . import DatabaseFailure
READABLE = CellStates.UP_TO_DATE, CellStates.FEEDING
......
......@@ -17,9 +17,8 @@
import weakref
from neo.lib import logging
from neo.lib.handler import EventHandler
from neo.lib.exception import PrimaryFailure, StoppedOperation
from neo.lib.protocol import (uuid_str,
NodeStates, NodeTypes, Packets, ProtocolError)
from neo.lib.exception import PrimaryFailure, ProtocolError, StoppedOperation
from neo.lib.protocol import uuid_str, NodeStates, NodeTypes, Packets
class BaseHandler(EventHandler):
......
......@@ -15,9 +15,10 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from neo.lib import logging
from neo.lib.exception import NonReadableCell, ProtocolError
from neo.lib.handler import DelayEvent
from neo.lib.util import dump, makeChecksum, add64
from neo.lib.protocol import Packets, Errors, NonReadableCell, ProtocolError, \
from neo.lib.protocol import Packets, Errors, \
ZERO_HASH, ZERO_TID, INVALID_PARTITION
from ..transactions import ConflictError, NotRegisteredError
from . import BaseHandler
......
......@@ -15,9 +15,9 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from neo.lib import logging
from neo.lib.exception import NotReadyError, ProtocolError
from neo.lib.handler import EventHandler
from neo.lib.protocol import NodeTypes, NotReadyError, Packets
from neo.lib.protocol import ProtocolError
from neo.lib.protocol import NodeTypes, Packets
from .storage import StorageOperationHandler
from .client import ClientOperationHandler, ClientReadOnlyOperationHandler
......
......@@ -16,7 +16,8 @@
from . import BaseMasterHandler
from neo.lib import logging
from neo.lib.protocol import Packets, ProtocolError, ZERO_TID
from neo.lib.exception import ProtocolError
from neo.lib.protocol import Packets, ZERO_TID
class InitializationHandler(BaseMasterHandler):
......
......@@ -17,8 +17,9 @@
import weakref
from functools import wraps
from neo.lib.connection import ConnectionClosed
from neo.lib.exception import ProtocolError
from neo.lib.handler import DelayEvent, EventHandler
from neo.lib.protocol import Errors, Packets, ProtocolError, ZERO_HASH
from neo.lib.protocol import Errors, Packets, ZERO_HASH
def checkConnectionIsReplicatorConnection(func):
def wrapper(self, conn, *args, **kw):
......
......@@ -16,10 +16,10 @@
from time import time
from neo.lib import logging
from neo.lib.exception import NonReadableCell, ProtocolError
from neo.lib.handler import DelayEvent, EventQueue
from neo.lib.util import cached_property, dump
from neo.lib.protocol import Packets, ProtocolError, NonReadableCell, \
uuid_str, MAX_TID, ZERO_TID
from neo.lib.protocol import Packets, uuid_str, MAX_TID, ZERO_TID
class ConflictError(Exception):
"""
......
......@@ -38,8 +38,8 @@ except ImportError:
from functools import wraps
from inspect import isclass
from .mock import Mock
from neo.lib import debug, logging, protocol
from neo.lib.protocol import NodeTypes, Packets, UUID_NAMESPACES
from neo.lib import debug, logging
from neo.lib.protocol import NodeTypes, Packet, Packets, UUID_NAMESPACES
from neo.lib.util import cached_property
from time import time, sleep
from struct import pack, unpack
......@@ -432,10 +432,6 @@ class NeoUnitTestBase(NeoTestBase):
conn.connecting = False
return conn
def checkProtocolErrorRaised(self, method, *args, **kwargs):
""" Check if the ProtocolError exception was raised """
self.assertRaises(protocol.ProtocolError, method, *args, **kwargs)
def checkAborted(self, conn):
""" Ensure the connection was aborted """
self.assertEqual(len(conn.mockGetNamedCalls('abort')), 1)
......@@ -461,7 +457,7 @@ class NeoUnitTestBase(NeoTestBase):
calls = conn.mockGetNamedCalls("answer")
self.assertEqual(len(calls), 1)
packet = calls.pop().getParam(0)
self.assertTrue(isinstance(packet, protocol.Packet))
self.assertTrue(isinstance(packet, Packet))
self.assertEqual(type(packet), Packets.Error)
return packet
......@@ -470,7 +466,7 @@ class NeoUnitTestBase(NeoTestBase):
calls = conn.mockGetNamedCalls('ask')
self.assertEqual(len(calls), 1)
packet = calls.pop().getParam(0)
self.assertTrue(isinstance(packet, protocol.Packet))
self.assertTrue(isinstance(packet, Packet))
self.assertEqual(type(packet), packet_type)
return packet
......@@ -479,7 +475,7 @@ class NeoUnitTestBase(NeoTestBase):
calls = conn.mockGetNamedCalls('answer')
self.assertEqual(len(calls), 1)
packet = calls.pop().getParam(0)
self.assertTrue(isinstance(packet, protocol.Packet))
self.assertTrue(isinstance(packet, Packet))
self.assertEqual(type(packet), packet_type)
return packet
......@@ -487,7 +483,7 @@ class NeoUnitTestBase(NeoTestBase):
""" Check if a notify-packet with the right type is sent """
calls = conn.mockGetNamedCalls('send')
packet = calls.pop(packet_number).getParam(0)
self.assertTrue(isinstance(packet, protocol.Packet))
self.assertTrue(isinstance(packet, Packet))
self.assertEqual(type(packet), packet_type)
return packet
......
......@@ -19,8 +19,9 @@ from ..mock import Mock, ReturnValues
from .. import NeoUnitTestBase
from neo.storage.app import Application
from neo.storage.handlers.client import ClientOperationHandler
from neo.lib.util import p64
from neo.lib.exception import ProtocolError
from neo.lib.protocol import INVALID_TID, Packets
from neo.lib.util import p64
class StorageClientHandlerTests(NeoUnitTestBase):
......@@ -65,7 +66,8 @@ class StorageClientHandlerTests(NeoUnitTestBase):
app.pt = Mock()
self.fakeDM()
conn = self._getConnection()
self.checkProtocolErrorRaised(self.operation.askTIDs, conn, 1, 1, None)
self.assertRaises(ProtocolError, self.operation.askTIDs,
conn, 1, 1, None)
self.assertEqual(len(app.pt.mockGetNamedCalls('getCellList')), 0)
self.assertEqual(len(app.dm.mockGetNamedCalls('getTIDList')), 0)
......@@ -84,8 +86,8 @@ class StorageClientHandlerTests(NeoUnitTestBase):
# invalid offsets => error
dm = self.fakeDM()
conn = self._getConnection()
self.checkProtocolErrorRaised(self.operation.askObjectHistory, conn,
1, 1, None)
self.assertRaises(ProtocolError, self.operation.askObjectHistory,
conn, 1, 1, None)
self.assertEqual(len(dm.mockGetNamedCalls('getObjectHistory')), 0)
def test_askObjectUndoSerial(self):
......
......@@ -19,8 +19,9 @@ from ..mock import Mock
from .. import NeoUnitTestBase
from neo.storage.app import Application
from neo.storage.handlers.master import MasterOperationHandler
from neo.lib.exception import ProtocolError
from neo.lib.protocol import CellStates
from neo.lib.pt import PartitionTable
from neo.lib.protocol import CellStates, ProtocolError
class StorageMasterHandlerTests(NeoUnitTestBase):
......
......@@ -17,9 +17,9 @@
import unittest
from .mock import Mock
from . import NeoUnitTestBase
from neo.lib.handler import EventHandler
from neo.lib.protocol import PacketMalformedError, UnexpectedPacketError, \
from neo.lib.exception import PacketMalformedError, UnexpectedPacketError, \
NotReadyError, ProtocolError
from neo.lib.handler import EventHandler
class HandlerTests(NeoUnitTestBase):
......
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