verify that all journal files are reclaimed after duplicate messages with concurrentStoreAndDispatchQueues=false, durable subs must be unsubscribed also https://issues.apache.org/activemq/browse/AMQ-2584

git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1028277 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gary Tully 2010-10-28 12:36:35 +00:00
parent 8a1f994bf5
commit 4d265fd5d5
1 changed files with 90 additions and 52 deletions

View File

@ -16,19 +16,21 @@
*/ */
package org.apache.activemq.bugs; package org.apache.activemq.bugs;
import java.io.File;
import java.io.FilenameFilter;
import java.util.Arrays;
import java.util.Properties; import java.util.Properties;
import java.util.Vector;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import javax.jms.JMSException; import javax.jms.JMSException;
import javax.jms.Message; import javax.jms.Message;
import javax.jms.MessageConsumer; import javax.jms.MessageConsumer;
import javax.jms.MessageListener; import javax.jms.MessageListener;
import javax.jms.MessageProducer; import javax.jms.MessageProducer;
import javax.jms.Session; import javax.jms.Session;
import javax.jms.TopicSubscriber;
import junit.framework.Test;
import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory; import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.broker.BrokerService; import org.apache.activemq.broker.BrokerService;
@ -53,16 +55,23 @@ public class AMQ2584ConcurrentDlqTest extends org.apache.activemq.TestSupport {
ActiveMQTopic topic; ActiveMQTopic topic;
ActiveMQConnection consumerConnection = null, producerConnection = null, dlqConnection = null; ActiveMQConnection consumerConnection = null, producerConnection = null, dlqConnection = null;
Session consumerSession;
Session producerSession; Session producerSession;
MessageProducer producer; MessageProducer producer;
final int minPercentUsageForStore = 10; Vector<TopicSubscriber> duralbeSubs = new Vector<TopicSubscriber>();
final int numMessages = 1000; final int numMessages = 1000;
final int numDurableSubs = 2;
String data; String data;
private long dlqConsumerLastReceivedTimeStamp;
private AtomicLong dlqReceivedCount = new AtomicLong(0);
// 2 deliveries of each message, 3 producers
CountDownLatch redeliveryConsumerLatch = new CountDownLatch(((2 * numMessages) * numDurableSubs) - 1);
// should get at least numMessages, possibly more
CountDownLatch dlqConsumerLatch = new CountDownLatch((numMessages - 1));
public void testSize() throws Exception { public void testSize() throws Exception {
CountDownLatch redeliveryConsumerLatch = new CountDownLatch(((2*numMessages) *3) -1);
CountDownLatch dlqConsumerLatch = new CountDownLatch((numMessages) -1);
openConsumer(redeliveryConsumerLatch); openConsumer(redeliveryConsumerLatch);
openDlqConsumer(dlqConsumerLatch); openDlqConsumer(dlqConsumerLatch);
@ -76,57 +85,70 @@ public class AMQ2584ConcurrentDlqTest extends org.apache.activemq.TestSupport {
final BrokerView brokerView = broker.getAdminView(); final BrokerView brokerView = broker.getAdminView();
broker.getSystemUsage().getStoreUsage().isFull(); broker.getSystemUsage().getStoreUsage().isFull();
LOG.info("store percent usage: "+brokerView.getStorePercentUsage()); LOG.info("store percent usage: " + brokerView.getStorePercentUsage());
//assertTrue("some store in use", broker.getAdminView().getStorePercentUsage() > minPercentUsageForStore); assertTrue("redelivery consumer got all it needs, remaining: "
assertTrue("redelivery consumer got all it needs", redeliveryConsumerLatch.await(60, TimeUnit.SECONDS)); + redeliveryConsumerLatch.getCount(), redeliveryConsumerLatch.await(60, TimeUnit.SECONDS));
assertTrue("dql consumer got all it needs", dlqConsumerLatch.await(60, TimeUnit.SECONDS)); assertTrue("dql consumer got all it needs", dlqConsumerLatch.await(60, TimeUnit.SECONDS));
closeConsumer(); closeConsumer();
LOG.info("Giving dlq a chance to clear down once topic consumer is closed"); LOG.info("Giving dlq a chance to clear down once topic consumer is closed");
// consumer all of the duplicates that arrived after the first ack
closeDlqConsumer();
//get broker a chance to clean obsolete messages, wait 2*cleanupInterval //get broker a chance to clean obsolete messages, wait 2*cleanupInterval
Thread.sleep(5000); Thread.sleep(5000);
// consumer some of the duplicates that arrived after the first ack FilenameFilter justLogFiles = new FilenameFilter() {
closeDlqConsumer(); public boolean accept(File file, String s) {
int numFiles = ((KahaDBPersistenceAdapter)broker.getPersistenceAdapter()).getDirectory().list().length; return s.endsWith(".log");
LOG.info("num files: " + numFiles); }
assertTrue("kahaDB dir should contain few db files,but definitely less than 10, is: " + numFiles,10>numFiles); };
int numFiles = ((KahaDBPersistenceAdapter) broker.getPersistenceAdapter()).getDirectory().list(justLogFiles).length;
if (numFiles > 2) {
LOG.info(Arrays.toString(((KahaDBPersistenceAdapter) broker.getPersistenceAdapter()).getDirectory().list(justLogFiles)));
}
LOG.info("num files: " + numFiles);
assertEquals("kahaDB dir should contain 1 db file,is: " + numFiles, 1, numFiles);
} }
private void openConsumer(final CountDownLatch latch) throws Exception { private void openConsumer(final CountDownLatch latch) throws Exception {
consumerConnection = (ActiveMQConnection) createConnection(); consumerConnection = (ActiveMQConnection) createConnection();
consumerConnection.setClientID("cliID"); consumerConnection.setClientID("cliID");
consumerConnection.start(); consumerConnection.start();
final Session session = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageListener listener = new MessageListener() { MessageListener listener = new MessageListener() {
public void onMessage(Message message) { public void onMessage(Message message) {
latch.countDown(); latch.countDown();
try { try {
session.recover(); consumerSession.recover();
} catch (Exception ignored) { } catch (Exception ignored) {
ignored.printStackTrace(); ignored.printStackTrace();
} }
} }
}; };
session.createDurableSubscriber(topic, "subName1").setMessageListener(listener); for (int i = 1; i <= numDurableSubs; i++) {
session.createDurableSubscriber(topic, "subName2").setMessageListener(listener); TopicSubscriber sub = consumerSession.createDurableSubscriber(topic, "subName" + i);
session.createDurableSubscriber(topic, "subName3").setMessageListener(listener); sub.setMessageListener(listener);
duralbeSubs.add(sub);
} }
private void openDlqConsumer(final CountDownLatch received)throws Exception{ }
private void openDlqConsumer(final CountDownLatch received) throws Exception {
dlqConnection = (ActiveMQConnection) createConnection(); dlqConnection = (ActiveMQConnection) createConnection();
Session dlqSession = dlqConnection .createSession(false, Session.AUTO_ACKNOWLEDGE); Session dlqSession = dlqConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer dlqConsumer = dlqSession.createConsumer(new ActiveMQQueue("ActiveMQ.DLQ")); MessageConsumer dlqConsumer = dlqSession.createConsumer(new ActiveMQQueue("ActiveMQ.DLQ"));
dlqConsumer.setMessageListener(new MessageListener() { dlqConsumer.setMessageListener(new MessageListener() {
public void onMessage(Message message) { public void onMessage(Message message) {
if (received.getCount() % 200 == 0) { if (received.getCount() > 0 && received.getCount() % 200 == 0) {
LOG.info("remaining on DLQ: " + received.getCount()); LOG.info("remaining on DLQ: " + received.getCount());
} }
received.countDown(); received.countDown();
dlqConsumerLastReceivedTimeStamp = System.currentTimeMillis();
dlqReceivedCount.incrementAndGet();
} }
}); });
dlqConnection.start(); dlqConnection.start();
@ -134,15 +156,34 @@ public class AMQ2584ConcurrentDlqTest extends org.apache.activemq.TestSupport {
private void closeConsumer() throws JMSException { private void closeConsumer() throws JMSException {
if (consumerConnection != null) for (TopicSubscriber sub : duralbeSubs) {
sub.close();
}
if (consumerSession != null) {
for (int i = 1; i <= numDurableSubs; i++) {
consumerSession.unsubscribe("subName" + i);
}
}
if (consumerConnection != null) {
consumerConnection.close(); consumerConnection.close();
consumerConnection = null; consumerConnection = null;
} }
private void closeDlqConsumer() throws JMSException { }
if (dlqConnection != null)
private void closeDlqConsumer() throws JMSException, InterruptedException {
final long limit = System.currentTimeMillis() + 30 * 1000;
if (dlqConsumerLastReceivedTimeStamp > 0) {
while (System.currentTimeMillis() < dlqConsumerLastReceivedTimeStamp + 5000
&& System.currentTimeMillis() < limit) {
LOG.info("waiting for DLQ do drain, receivedCount: " + dlqReceivedCount);
TimeUnit.SECONDS.sleep(1);
}
}
if (dlqConnection != null) {
dlqConnection.close(); dlqConnection.close();
dlqConnection = null; dlqConnection = null;
} }
}
private void sendMessage(boolean filter) throws Exception { private void sendMessage(boolean filter) throws Exception {
if (producerConnection == null) { if (producerConnection == null) {
@ -171,10 +212,6 @@ public class AMQ2584ConcurrentDlqTest extends org.apache.activemq.TestSupport {
if (deleteMessages) { if (deleteMessages) {
broker.setDeleteAllMessagesOnStartup(true); broker.setDeleteAllMessagesOnStartup(true);
} }
KahaDBPersistenceAdapter persistenceAdapter=new KahaDBPersistenceAdapter();
persistenceAdapter.setEnableJournalDiskSyncs(false);
broker.setPersistenceAdapter(persistenceAdapter);
configurePersistenceAdapter(broker.getPersistenceAdapter()); configurePersistenceAdapter(broker.getPersistenceAdapter());
broker.getSystemUsage().getStoreUsage().setLimit(200 * 1000 * 1000); broker.getSystemUsage().getStoreUsage().setLimit(200 * 1000 * 1000);
broker.start(); broker.start();
@ -187,6 +224,7 @@ public class AMQ2584ConcurrentDlqTest extends org.apache.activemq.TestSupport {
properties.put("maxFileLength", maxFileLengthVal); properties.put("maxFileLength", maxFileLengthVal);
properties.put("cleanupInterval", "2000"); properties.put("cleanupInterval", "2000");
properties.put("checkpointInterval", "2000"); properties.put("checkpointInterval", "2000");
properties.put("concurrentStoreAndDispatchQueues", "false");
IntrospectionSupport.setProperties(persistenceAdapter, properties); IntrospectionSupport.setProperties(persistenceAdapter, properties);
} }