Esempio n. 1
0
 def _process(self, msg):
     request = TpProcessRequest()
     request.ParseFromString(msg.content)
     state = State(self._stream, request.context_id)
     header = TransactionHeader()
     header.ParseFromString(request.header)
     try:
         if not self._stream.is_ready():
             raise ValidatorConnectionError()
         self._find_handler(header).apply(request, state)
         self._stream.send_back(
             message_type=Message.TP_PROCESS_RESPONSE,
             correlation_id=msg.correlation_id,
             content=TpProcessResponse(
                 status=TpProcessResponse.OK
             ).SerializeToString())
     except InvalidTransaction as it:
         LOGGER.warning("Invalid Transaction %s", it)
         try:
             self._stream.send_back(
                 message_type=Message.TP_PROCESS_RESPONSE,
                 correlation_id=msg.correlation_id,
                 content=TpProcessResponse(
                     status=TpProcessResponse.INVALID_TRANSACTION
                 ).SerializeToString())
         except ValidatorConnectionError as vce:
             # TP_PROCESS_REQUEST has made it through the
             # handler.apply and an INVALID_TRANSACTION would have been
             # sent back but the validator has disconnected and so it
             # doesn't care about the response.
             LOGGER.warning("during invalid transaction response: %s", vce)
     except InternalError as ie:
         LOGGER.warning("internal error: %s", ie)
         try:
             self._stream.send_back(
                 message_type=Message.TP_PROCESS_RESPONSE,
                 correlation_id=msg.correlation_id,
                 content=TpProcessResponse(
                     status=TpProcessResponse.INTERNAL_ERROR
                 ).SerializeToString())
         except ValidatorConnectionError as vce:
             # Same as the prior except block, but an internal error has
             # happened, but because of the disconnect the validator
             # probably doesn't care about the response.
             LOGGER.warning("during internal error response: %s", vce)
     except ValidatorConnectionError as vce:
         # Somewhere within handler.apply a future resolved with an
         # error status that the validator has disconnected. There is
         # nothing left to do but reconnect.
         LOGGER.warning("during handler.apply a future was resolved "
                        "with error status: %s", vce)
Esempio n. 2
0
 def create_tp_response(self, status):
     responses = {
         "OK": TpProcessResponse.OK,
         "INVALID_TRANSACTION": TpProcessResponse.INVALID_TRANSACTION,
         "INTERNAL_ERROR": TpProcessResponse.INTERNAL_ERROR
     }
     return TpProcessResponse(status=responses[status])
Esempio n. 3
0
    def _process(self, msg):
        if msg.message_type != Message.TP_PROCESS_REQUEST:
            LOGGER.debug(
                "Transaction Processor recieved invalid message type. "
                "Message type should be TP_PROCESS_REQUEST,"
                " but is %s", Message.MessageType.Name(msg.message_type))
            return

        request = TpProcessRequest()
        request.ParseFromString(msg.content)
        state = Context(self._stream, request.context_id)
        header = request.header
        try:
            if not self._stream.is_ready():
                raise ValidatorConnectionError()
            handler = self._find_handler(header)
            if handler is None:
                return
            handler.apply(request, state)
            self._stream.send_back(
                message_type=Message.TP_PROCESS_RESPONSE,
                correlation_id=msg.correlation_id,
                content=TpProcessResponse(
                    status=TpProcessResponse.OK
                ).SerializeToString())
        except InvalidTransaction as it:
            LOGGER.warning("Invalid Transaction %s", it)
            try:
                self._stream.send_back(
                    message_type=Message.TP_PROCESS_RESPONSE,
                    correlation_id=msg.correlation_id,
                    content=TpProcessResponse(
                        status=TpProcessResponse.INVALID_TRANSACTION,
                        message=str(it),
                        extended_data=it.extended_data
                    ).SerializeToString())
            except ValidatorConnectionError as vce:
                # TP_PROCESS_REQUEST has made it through the
                # handler.apply and an INVALID_TRANSACTION would have been
                # sent back but the validator has disconnected and so it
                # doesn't care about the response.
                LOGGER.warning("during invalid transaction response: %s", vce)
        except InternalError as ie:
            LOGGER.warning("internal error: %s", ie)
            try:
                self._stream.send_back(
                    message_type=Message.TP_PROCESS_RESPONSE,
                    correlation_id=msg.correlation_id,
                    content=TpProcessResponse(
                        status=TpProcessResponse.INTERNAL_ERROR,
                        message=str(ie),
                        extended_data=ie.extended_data
                    ).SerializeToString())
            except ValidatorConnectionError as vce:
                # Same as the prior except block, but an internal error has
                # happened, but because of the disconnect the validator
                # probably doesn't care about the response.
                LOGGER.warning("during internal error response: %s", vce)
        except ValidatorConnectionError as vce:
            # Somewhere within handler.apply a future resolved with an
            # error status that the validator has disconnected. There is
            # nothing left to do but reconnect.
            LOGGER.warning("during handler.apply a future was resolved "
                           "with error status: %s", vce)
        except AuthorizationException as ae:
            LOGGER.warning("AuthorizationException: %s", ae)
            try:
                self._stream.send_back(
                    message_type=Message.TP_PROCESS_RESPONSE,
                    correlation_id=msg.correlation_id,
                    content=TpProcessResponse(
                        status=TpProcessResponse.INVALID_TRANSACTION,
                        message=str(ae),
                    ).SerializeToString())
            except ValidatorConnectionError as vce:
                # TP_PROCESS_REQUEST has made it through the
                # handler.apply and an INVALID_TRANSACTION would have been
                # sent back but the validator has disconnected and so it
                # doesn't care about the response.
                LOGGER.warning("during invalid transaction response: %s", vce)