AMQ-3872 AMQ-3305 - deal with redelivery of acked messages pending transaction completion, getting dropped by subscriptions, invisible till broker restart

git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1354449 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gary Tully 2012-06-27 11:53:58 +00:00
parent 551efc90ec
commit e900fb41db
2 changed files with 109 additions and 3 deletions

View File

@ -1607,6 +1607,7 @@ public class Queue extends BaseDestination implements Task, UsageListener {
@Override @Override
public void afterRollback() throws Exception { public void afterRollback() throws Exception {
reference.setAcked(false); reference.setAcked(false);
wakeup();
} }
}); });
} }
@ -1879,7 +1880,7 @@ public class Queue extends BaseDestination implements Task, UsageListener {
continue; continue;
} }
if (!fullConsumers.contains(s) && !s.isFull()) { if (!fullConsumers.contains(s) && !s.isFull()) {
if (dispatchSelector.canSelect(s, node) && assignMessageGroup(s, (QueueMessageReference)node)) { if (dispatchSelector.canSelect(s, node) && assignMessageGroup(s, (QueueMessageReference)node) && !((QueueMessageReference) node).isAcked() ) {
// Dispatch it. // Dispatch it.
s.add(node); s.add(node);
target = s; target = s;
@ -1894,8 +1895,7 @@ public class Queue extends BaseDestination implements Task, UsageListener {
} }
} }
// make sure it gets dispatched again // make sure it gets dispatched again
if (!node.isDropped() && !((QueueMessageReference) node).isAcked() && if (!node.isDropped()) {
(!node.isDropped() || s.getConsumerInfo().isBrowser())) {
interestCount++; interestCount++;
} }
} }

View File

@ -16,6 +16,8 @@
*/ */
package org.apache.activemq.broker; package org.apache.activemq.broker;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import javax.jms.JMSException; import javax.jms.JMSException;
import javax.management.InstanceNotFoundException; import javax.management.InstanceNotFoundException;
@ -745,6 +747,110 @@ public class XARecoveryBrokerTest extends BrokerRestartTestSupport {
assertEquals("there are no prepared tx", 0, dataArrayResponse.getData().length); assertEquals("there are no prepared tx", 0, dataArrayResponse.getData().length);
} }
public void testQueuePersistentPreparedAcksAvailableAfterRollbackPrefetchOne() throws Exception {
ActiveMQDestination destination = createDestination();
// Setup the producer and send the message.
StubConnection connection = createConnection();
ConnectionInfo connectionInfo = createConnectionInfo();
SessionInfo sessionInfo = createSessionInfo(connectionInfo);
ProducerInfo producerInfo = createProducerInfo(sessionInfo);
connection.send(connectionInfo);
connection.send(sessionInfo);
connection.send(producerInfo);
int numMessages = 1;
for (int i = 0; i < numMessages; i++) {
Message message = createMessage(producerInfo, destination);
message.setPersistent(true);
connection.send(message);
}
final int messageCount = expectedMessageCount(numMessages, destination);
// Begin the transaction.
XATransactionId txid = createXATransaction(sessionInfo);
connection.send(createBeginTransaction(connectionInfo, txid));
// use consumer per destination for the composite dest case
// bc the same composite dest is used for sending so there
// will be duplicate message ids in the mix which a single
// consumer (PrefetchSubscription) cannot handle in a tx
// atm. The matching is based on messageId rather than messageId
// and destination
Set<ConsumerInfo> consumerInfos = new HashSet<ConsumerInfo>();
for (ActiveMQDestination dest : destinationList(destination)) {
ConsumerInfo consumerInfo = createConsumerInfo(sessionInfo, dest);
consumerInfo.setPrefetchSize(numMessages);
consumerInfos.add(consumerInfo);
}
for (ConsumerInfo info : consumerInfos) {
connection.send(info);
}
Message message = null;
for (ConsumerInfo info : consumerInfos) {
for (int i = 0; i < numMessages; i++) {
message = receiveMessage(connection);
assertNotNull(message);
connection.send(createAck(info, message, 1, MessageAck.DELIVERED_ACK_TYPE));
}
MessageAck ack = createAck(info, message, numMessages, MessageAck.STANDARD_ACK_TYPE);
ack.setTransactionId(txid);
connection.send(ack);
}
connection.request(createPrepareTransaction(connectionInfo, txid));
// reconnect
connection.send(connectionInfo.createRemoveCommand());
connection = createConnection();
connection.send(connectionInfo);
// validate recovery
TransactionInfo recoverInfo = new TransactionInfo(connectionInfo.getConnectionId(), null, TransactionInfo.RECOVER);
DataArrayResponse dataArrayResponse = (DataArrayResponse) connection.request(recoverInfo);
assertEquals("there is a prepared tx", 1, dataArrayResponse.getData().length);
assertEquals("it matches", txid, dataArrayResponse.getData()[0]);
connection.send(sessionInfo);
for (ConsumerInfo info : consumerInfos) {
connection.send(info);
}
// no redelivery, exactly once semantics while prepared
message = receiveMessage(connection);
assertNull(message);
assertNoMessagesLeft(connection);
// rollback so we get redelivery
connection.request(createRollbackTransaction(connectionInfo, txid));
LOG.info("new tx for redelivery");
txid = createXATransaction(sessionInfo);
connection.send(createBeginTransaction(connectionInfo, txid));
for (ConsumerInfo info : consumerInfos) {
for (int i = 0; i < numMessages; i++) {
message = receiveMessage(connection);
assertNotNull("unexpected null on:" + i, message);
MessageAck ack = createAck(info, message, 1, MessageAck.STANDARD_ACK_TYPE);
ack.setTransactionId(txid);
connection.send(ack);
}
}
// Commit
connection.request(createCommitTransaction1Phase(connectionInfo, txid));
// validate recovery complete
dataArrayResponse = (DataArrayResponse) connection.request(recoverInfo);
assertEquals("there are no prepared tx", 0, dataArrayResponse.getData().length);
}
public void initCombosForTestTopicPersistentPreparedAcksAvailableAfterRestartAndRollback() { public void initCombosForTestTopicPersistentPreparedAcksAvailableAfterRestartAndRollback() {
addCombinationValues("prioritySupport", new Boolean[]{Boolean.FALSE, Boolean.TRUE}); addCombinationValues("prioritySupport", new Boolean[]{Boolean.FALSE, Boolean.TRUE});
} }