Search in sources :

Example 1 with Declare

use of org.apache.qpid.server.protocol.v1_0.type.transaction.Declare in project qpid-broker-j by apache.

the class TxnCoordinatorReceivingLinkEndpoint method receiveDelivery.

@Override
protected Error receiveDelivery(Delivery delivery) {
    // Only interested in the amqp-value section that holds the message to the coordinator
    try (QpidByteBuffer payload = delivery.getPayload()) {
        List<EncodingRetainingSection<?>> sections = getSectionDecoder().parseAll(payload);
        boolean amqpValueSectionFound = false;
        for (EncodingRetainingSection section : sections) {
            try {
                if (section instanceof AmqpValueSection) {
                    if (amqpValueSectionFound) {
                        throw new ConnectionScopedRuntimeException("Received more than one AmqpValue sections");
                    }
                    amqpValueSectionFound = true;
                    Object command = section.getValue();
                    Session_1_0 session = getSession();
                    AMQPConnection_1_0<?> connection = session.getConnection();
                    connection.receivedComplete();
                    if (command instanceof Declare) {
                        final IdentifiedTransaction txn = connection.createIdentifiedTransaction();
                        _createdTransactions.put(txn.getId(), txn.getServerTransaction());
                        long notificationRepeatPeriod = getSession().getContextValue(Long.class, Session.TRANSACTION_TIMEOUT_NOTIFICATION_REPEAT_PERIOD);
                        connection.registerTransactionTickers(txn.getServerTransaction(), this::doTimeoutAction, notificationRepeatPeriod);
                        Declared state = new Declared();
                        state.setTxnId(Session_1_0.integerToTransactionId(txn.getId()));
                        updateDisposition(delivery.getDeliveryTag(), state, true);
                    } else if (command instanceof Discharge) {
                        Discharge discharge = (Discharge) command;
                        Error error = discharge(discharge.getTxnId(), Boolean.TRUE.equals(discharge.getFail()));
                        final DeliveryState outcome;
                        if (error == null) {
                            outcome = new Accepted();
                        } else if (Arrays.asList(getSource().getOutcomes()).contains(Rejected.REJECTED_SYMBOL)) {
                            final Rejected rejected = new Rejected();
                            rejected.setError(error);
                            outcome = rejected;
                            error = null;
                        } else {
                            outcome = null;
                        }
                        if (error == null) {
                            updateDisposition(delivery.getDeliveryTag(), outcome, true);
                        }
                        return error;
                    } else {
                        throw new ConnectionScopedRuntimeException(String.format("Received unknown command '%s'", command.getClass().getSimpleName()));
                    }
                }
            } finally {
                section.dispose();
            }
        }
        if (!amqpValueSectionFound) {
            throw new ConnectionScopedRuntimeException("Received no AmqpValue section");
        }
    } catch (AmqpErrorException e) {
        return e.getError();
    }
    return null;
}
Also used : EncodingRetainingSection(org.apache.qpid.server.protocol.v1_0.type.messaging.EncodingRetainingSection) Error(org.apache.qpid.server.protocol.v1_0.type.transport.Error) AmqpError(org.apache.qpid.server.protocol.v1_0.type.transport.AmqpError) TransactionError(org.apache.qpid.server.protocol.v1_0.type.transaction.TransactionError) AmqpErrorException(org.apache.qpid.server.protocol.v1_0.type.AmqpErrorException) Rejected(org.apache.qpid.server.protocol.v1_0.type.messaging.Rejected) Declare(org.apache.qpid.server.protocol.v1_0.type.transaction.Declare) Declared(org.apache.qpid.server.protocol.v1_0.type.transaction.Declared) Discharge(org.apache.qpid.server.protocol.v1_0.type.transaction.Discharge) Accepted(org.apache.qpid.server.protocol.v1_0.type.messaging.Accepted) DeliveryState(org.apache.qpid.server.protocol.v1_0.type.DeliveryState) ConnectionScopedRuntimeException(org.apache.qpid.server.util.ConnectionScopedRuntimeException) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer) AmqpValueSection(org.apache.qpid.server.protocol.v1_0.type.messaging.AmqpValueSection)

Example 2 with Declare

use of org.apache.qpid.server.protocol.v1_0.type.transaction.Declare in project qpid-broker-j by apache.

the class DischargeTest method dischargeUnknownTransactionIdWhenSourceSupportsRejectedOutcome.

@Test
@SpecificationTest(section = "4.3", description = "If the coordinator is unable to complete the discharge, the coordinator MUST convey the error to the controller " + "as a transaction-error. If the source for the link to the coordinator supports the rejected outcome, then the " + "message MUST be rejected with this outcome carrying the transaction-error.")
public void dischargeUnknownTransactionIdWhenSourceSupportsRejectedOutcome() throws Exception {
    try (FrameTransport transport = new FrameTransport(_brokerAddress).connect()) {
        final Interaction interaction = transport.newInteraction();
        final Disposition disposition = interaction.negotiateProtocol().consumeResponse().open().consumeResponse(Open.class).begin().consumeResponse(Begin.class).attachRole(Role.SENDER).attachSourceOutcomes(Rejected.REJECTED_SYMBOL).attachTarget(new Coordinator()).attach().consumeResponse(Attach.class).consumeResponse(Flow.class).transferPayloadData(new Declare()).transfer().consumeResponse().getLatestResponse(Disposition.class);
        assertThat(disposition.getSettled(), is(equalTo(true)));
        assertThat(disposition.getState(), is(instanceOf(Declared.class)));
        assertThat(((Declared) disposition.getState()).getTxnId(), is(notNullValue()));
        interaction.consumeResponse(Flow.class);
        final Discharge discharge = new Discharge();
        discharge.setTxnId(new Binary("nonExistingTransaction".getBytes(UTF_8)));
        final Disposition dischargeDisposition = interaction.transferDeliveryId(UnsignedInteger.ONE).transferDeliveryTag(new Binary("discharge".getBytes(UTF_8))).transferPayloadData(discharge).transfer().consumeResponse().getLatestResponse(Disposition.class);
        assertThat(dischargeDisposition.getState(), is(instanceOf(Rejected.class)));
        final Error error = ((Rejected) dischargeDisposition.getState()).getError();
        assertThat(error, is(notNullValue()));
        assertThat(error.getCondition(), is(equalTo(TransactionError.UNKNOWN_ID)));
    }
}
Also used : FrameTransport(org.apache.qpid.tests.protocol.v1_0.FrameTransport) Interaction(org.apache.qpid.tests.protocol.v1_0.Interaction) Attach(org.apache.qpid.server.protocol.v1_0.type.transport.Attach) Disposition(org.apache.qpid.server.protocol.v1_0.type.transport.Disposition) Error(org.apache.qpid.server.protocol.v1_0.type.transport.Error) TransactionError(org.apache.qpid.server.protocol.v1_0.type.transaction.TransactionError) Binary(org.apache.qpid.server.protocol.v1_0.type.Binary) Rejected(org.apache.qpid.server.protocol.v1_0.type.messaging.Rejected) Coordinator(org.apache.qpid.server.protocol.v1_0.type.transaction.Coordinator) Declare(org.apache.qpid.server.protocol.v1_0.type.transaction.Declare) Discharge(org.apache.qpid.server.protocol.v1_0.type.transaction.Discharge) Test(org.junit.Test) SpecificationTest(org.apache.qpid.tests.protocol.SpecificationTest) SpecificationTest(org.apache.qpid.tests.protocol.SpecificationTest)

Example 3 with Declare

use of org.apache.qpid.server.protocol.v1_0.type.transaction.Declare in project qpid-broker-j by apache.

the class DischargeTest method dischargeUnknownTransactionIdWhenSourceDoesNotSupportRejectedOutcome.

@Test
@SpecificationTest(section = "4.3", description = "If the coordinator is unable to complete the discharge, the coordinator MUST convey the error to the controller " + "as a transaction-error. [...] If the source does not support " + "the rejected outcome, the transactional resource MUST detach the link to the coordinator, with the detach " + "performative carrying the transaction-error.")
public void dischargeUnknownTransactionIdWhenSourceDoesNotSupportRejectedOutcome() throws Exception {
    try (FrameTransport transport = new FrameTransport(_brokerAddress).connect()) {
        final Interaction interaction = transport.newInteraction();
        final Disposition disposition = interaction.negotiateProtocol().consumeResponse().open().consumeResponse(Open.class).begin().consumeResponse(Begin.class).attachRole(Role.SENDER).attachSourceOutcomes(Accepted.ACCEPTED_SYMBOL).attachTarget(new Coordinator()).attach().consumeResponse(Attach.class).consumeResponse(Flow.class).transferPayloadData(new Declare()).transfer().consumeResponse().getLatestResponse(Disposition.class);
        assertThat(disposition.getSettled(), is(equalTo(true)));
        assertThat(disposition.getState(), is(instanceOf(Declared.class)));
        assertThat(((Declared) disposition.getState()).getTxnId(), is(notNullValue()));
        interaction.consumeResponse(Flow.class);
        final Discharge discharge = new Discharge();
        discharge.setTxnId(new Binary("nonExistingTransaction".getBytes(UTF_8)));
        final Detach detachResponse = interaction.transferDeliveryId(UnsignedInteger.ONE).transferDeliveryTag(new Binary("discharge".getBytes(UTF_8))).transferPayloadData(discharge).transfer().consumeResponse(Detach.class).getLatestResponse(Detach.class);
        Error error = detachResponse.getError();
        assertThat(error, is(notNullValue()));
        assertThat(error.getCondition(), is(equalTo(TransactionError.UNKNOWN_ID)));
    }
}
Also used : FrameTransport(org.apache.qpid.tests.protocol.v1_0.FrameTransport) Interaction(org.apache.qpid.tests.protocol.v1_0.Interaction) Attach(org.apache.qpid.server.protocol.v1_0.type.transport.Attach) Disposition(org.apache.qpid.server.protocol.v1_0.type.transport.Disposition) Error(org.apache.qpid.server.protocol.v1_0.type.transport.Error) TransactionError(org.apache.qpid.server.protocol.v1_0.type.transaction.TransactionError) Binary(org.apache.qpid.server.protocol.v1_0.type.Binary) Coordinator(org.apache.qpid.server.protocol.v1_0.type.transaction.Coordinator) Declare(org.apache.qpid.server.protocol.v1_0.type.transaction.Declare) Detach(org.apache.qpid.server.protocol.v1_0.type.transport.Detach) Discharge(org.apache.qpid.server.protocol.v1_0.type.transaction.Discharge) Test(org.junit.Test) SpecificationTest(org.apache.qpid.tests.protocol.SpecificationTest) SpecificationTest(org.apache.qpid.tests.protocol.SpecificationTest)

Example 4 with Declare

use of org.apache.qpid.server.protocol.v1_0.type.transaction.Declare in project qpid-broker-j by apache.

the class Interaction method txnDeclare.

public Interaction txnDeclare(final InteractionTransactionalState txnState) throws Exception {
    Transfer transfer = createTransactionTransfer(txnState.getHandle());
    transferPayload(transfer, new Declare());
    sendPerformativeAndChainFuture(transfer, _sessionChannel);
    consumeResponse(Disposition.class);
    Disposition declareTransactionDisposition = getLatestResponse(Disposition.class);
    assertThat(declareTransactionDisposition.getSettled(), is(equalTo(true)));
    assertThat(declareTransactionDisposition.getState(), is(instanceOf(Declared.class)));
    Binary transactionId = ((Declared) declareTransactionDisposition.getState()).getTxnId();
    assertThat(transactionId, is(notNullValue()));
    consumeResponse(Flow.class);
    txnState.setLastTransactionId(transactionId);
    return this;
}
Also used : Transfer(org.apache.qpid.server.protocol.v1_0.type.transport.Transfer) Disposition(org.apache.qpid.server.protocol.v1_0.type.transport.Disposition) Binary(org.apache.qpid.server.protocol.v1_0.type.Binary) Declare(org.apache.qpid.server.protocol.v1_0.type.transaction.Declare) Declared(org.apache.qpid.server.protocol.v1_0.type.transaction.Declared)

Aggregations

Declare (org.apache.qpid.server.protocol.v1_0.type.transaction.Declare)4 Binary (org.apache.qpid.server.protocol.v1_0.type.Binary)3 Discharge (org.apache.qpid.server.protocol.v1_0.type.transaction.Discharge)3 TransactionError (org.apache.qpid.server.protocol.v1_0.type.transaction.TransactionError)3 Disposition (org.apache.qpid.server.protocol.v1_0.type.transport.Disposition)3 Error (org.apache.qpid.server.protocol.v1_0.type.transport.Error)3 Rejected (org.apache.qpid.server.protocol.v1_0.type.messaging.Rejected)2 Coordinator (org.apache.qpid.server.protocol.v1_0.type.transaction.Coordinator)2 Declared (org.apache.qpid.server.protocol.v1_0.type.transaction.Declared)2 Attach (org.apache.qpid.server.protocol.v1_0.type.transport.Attach)2 SpecificationTest (org.apache.qpid.tests.protocol.SpecificationTest)2 FrameTransport (org.apache.qpid.tests.protocol.v1_0.FrameTransport)2 Interaction (org.apache.qpid.tests.protocol.v1_0.Interaction)2 Test (org.junit.Test)2 QpidByteBuffer (org.apache.qpid.server.bytebuffer.QpidByteBuffer)1 AmqpErrorException (org.apache.qpid.server.protocol.v1_0.type.AmqpErrorException)1 DeliveryState (org.apache.qpid.server.protocol.v1_0.type.DeliveryState)1 Accepted (org.apache.qpid.server.protocol.v1_0.type.messaging.Accepted)1 AmqpValueSection (org.apache.qpid.server.protocol.v1_0.type.messaging.AmqpValueSection)1 EncodingRetainingSection (org.apache.qpid.server.protocol.v1_0.type.messaging.EncodingRetainingSection)1