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

Do not define exception classes in protocol.py

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