def _call(addr, context, msg_id, topic, msg, timeout=None): # timeout_response is how long we wait for a response timeout = timeout or FLAGS.rpc_response_timeout # The msg_id is used to track replies. msg_id = str(uuid.uuid4().hex) # Replies always come into the reply service. # We require that FLAGS.host is a FQDN, IP, or resolvable hostname. reply_topic = "zmq_replies.%s" % FLAGS.host LOG.debug(_("Creating payload")) # Curry the original request into a reply method. mcontext = RpcContext.marshal(context) payload = { 'method': '-reply', 'args': { 'msg_id': msg_id, 'context': mcontext, 'topic': reply_topic, 'msg': [mcontext, msg] } } LOG.debug(_("Creating queue socket for reply waiter")) # Messages arriving async. # TODO(ewindisch): have reply consumer with dynamic subscription mgmt with Timeout(timeout, exception=rpc_common.Timeout): try: msg_waiter = ZmqSocket( "ipc://%s/zmq_topic_zmq_replies" % FLAGS.rpc_zmq_ipc_dir, zmq.SUB, subscribe=msg_id, bind=False ) LOG.debug(_("Sending cast")) _cast(addr, context, msg_id, topic, payload) LOG.debug(_("Cast sent; Waiting reply")) # Blocks until receives reply msg = msg_waiter.recv() LOG.debug(_("Received message: %s"), msg) LOG.debug(_("Unpacking response")) responses = _deserialize(msg[-1]) # ZMQError trumps the Timeout error. except zmq.ZMQError: raise RPCException("ZMQ Socket Error") finally: if 'msg_waiter' in vars(): msg_waiter.close() # It seems we don't need to do all of the following, # but perhaps it would be useful for multicall? # One effect of this is that we're checking all # responses for Exceptions. for resp in responses: if isinstance(resp, types.DictType) and 'exc' in resp: raise rpc_common.deserialize_remote_exception(FLAGS, resp['exc']) return responses[-1]
def __call__(self, data): """The consume() callback will call this. Store the result.""" if data['failure']: failure = data['failure'] self._result = rpc_common.deserialize_remote_exception(failure) elif data.get('ending', False): self._got_ending = True else: self._result = data['result']
def test_deserialize_remote_exception_bad_module(self): failure = { 'class': 'popen2', 'module': 'os', 'kwargs': {'cmd': '/bin/echo failed'}, 'message': 'foo', } serialized = json.dumps(failure) after_exc = rpc_common.deserialize_remote_exception(FLAGS, serialized) self.assertTrue(isinstance(after_exc, rpc_common.RemoteError))
def test_deserialize_remote_exception_bad_module(self): failure = { 'class': 'popen2', 'module': 'os', 'kwargs': { 'cmd': '/bin/echo failed' }, 'message': 'foo', } serialized = json.dumps(failure) after_exc = rpc_common.deserialize_remote_exception(serialized) self.assertTrue(isinstance(after_exc, rpc_common.RemoteError))
def test_deserialize_remote_exception(self): failure = { 'class': 'NovaException', 'module': 'nova.exception', 'message': 'test message', 'tb': ['raise NovaException'], } serialized = json.dumps(failure) after_exc = rpc_common.deserialize_remote_exception(FLAGS, serialized) self.assertTrue(isinstance(after_exc, exception.NovaException)) self.assertTrue('test message' in unicode(after_exc)) #assure the traceback was added self.assertTrue('raise NovaException' in unicode(after_exc))
def test_deserialize_remote_exception_user_defined_exception(self): """Ensure a user defined exception can be deserialized.""" self.flags(allowed_rpc_exception_modules=[self.__class__.__module__]) failure = { 'class': 'FakeUserDefinedException', 'module': self.__class__.__module__, 'tb': ['raise FakeUserDefinedException'], } serialized = json.dumps(failure) after_exc = rpc_common.deserialize_remote_exception(FLAGS, serialized) self.assertTrue(isinstance(after_exc, FakeUserDefinedException)) #assure the traceback was added self.assertTrue('raise FakeUserDefinedException' in unicode(after_exc))
def test_deserialize_remote_exception(self): failure = { 'class': 'NovaException', 'module': 'nova.exception', 'message': 'test message', 'tb': ['raise NovaException'], } serialized = json.dumps(failure) after_exc = rpc_common.deserialize_remote_exception(serialized) self.assertTrue(isinstance(after_exc, exception.NovaException)) self.assertTrue('test message' in unicode(after_exc)) #assure the traceback was added self.assertTrue('raise NovaException' in unicode(after_exc))
def test_deserialize_remote_exception_user_defined_exception(self): """Ensure a user defined exception can be deserialized.""" self.flags(allowed_rpc_exception_modules=[self.__class__.__module__]) failure = { 'class': 'FakeUserDefinedException', 'module': self.__class__.__module__, 'tb': ['raise FakeUserDefinedException'], } serialized = json.dumps(failure) after_exc = rpc_common.deserialize_remote_exception(serialized) self.assertTrue(isinstance(after_exc, FakeUserDefinedException)) #assure the traceback was added self.assertTrue('raise FakeUserDefinedException' in unicode(after_exc))
def test_deserialize_remote_exception_cannot_recreate(self): """Ensure a RemoteError is returned on initialization failure. If an exception cannot be recreated with it's original class then a RemoteError with the exception informations should still be returned. """ self.flags(allowed_rpc_exception_modules=[self.__class__.__module__]) failure = { 'class': 'FakeIDontExistException', 'module': self.__class__.__module__, 'tb': ['raise FakeIDontExistException'], } serialized = json.dumps(failure) after_exc = rpc_common.deserialize_remote_exception(FLAGS, serialized) self.assertTrue(isinstance(after_exc, rpc_common.RemoteError)) #assure the traceback was added self.assertTrue('raise FakeIDontExistException' in unicode(after_exc))
def test_deserialize_remote_exception_cannot_recreate(self): """Ensure a RemoteError is returned on initialization failure. If an exception cannot be recreated with it's original class then a RemoteError with the exception informations should still be returned. """ self.flags(allowed_rpc_exception_modules=[self.__class__.__module__]) failure = { 'class': 'FakeIDontExistException', 'module': self.__class__.__module__, 'tb': ['raise FakeIDontExistException'], } serialized = json.dumps(failure) after_exc = rpc_common.deserialize_remote_exception(serialized) self.assertTrue(isinstance(after_exc, rpc_common.RemoteError)) #assure the traceback was added self.assertTrue('raise FakeIDontExistException' in unicode(after_exc))
def _call(addr, context, msg_id, topic, msg, timeout=None): # timeout_response is how long we wait for a response timeout = timeout or FLAGS.rpc_response_timeout # The msg_id is used to track replies. msg_id = str(uuid.uuid4().hex) # Replies always come into the reply service. # We require that FLAGS.host is a FQDN, IP, or resolvable hostname. reply_topic = "zmq_replies.%s" % FLAGS.host LOG.debug(_("Creating payload")) # Curry the original request into a reply method. mcontext = RpcContext.marshal(context) payload = { 'method': '-reply', 'args': { 'msg_id': msg_id, 'context': mcontext, 'topic': reply_topic, 'msg': [mcontext, msg] } } LOG.debug(_("Creating queue socket for reply waiter")) # Messages arriving async. # TODO(ewindisch): have reply consumer with dynamic subscription mgmt with Timeout(timeout, exception=rpc_common.Timeout): try: msg_waiter = ZmqSocket("ipc://%s/zmq_topic_zmq_replies" % FLAGS.rpc_zmq_ipc_dir, zmq.SUB, subscribe=msg_id, bind=False) LOG.debug(_("Sending cast")) _cast(addr, context, msg_id, topic, payload) LOG.debug(_("Cast sent; Waiting reply")) # Blocks until receives reply msg = msg_waiter.recv() LOG.debug(_("Received message: %s"), msg) LOG.debug(_("Unpacking response")) responses = _deserialize(msg[-1]) # ZMQError trumps the Timeout error. except zmq.ZMQError: raise RPCException("ZMQ Socket Error") finally: if 'msg_waiter' in vars(): msg_waiter.close() # It seems we don't need to do all of the following, # but perhaps it would be useful for multicall? # One effect of this is that we're checking all # responses for Exceptions. for resp in responses: if isinstance(resp, types.DictType) and 'exc' in resp: raise rpc_common.deserialize_remote_exception(FLAGS, resp['exc']) return responses[-1]