git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1464165 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Timothy A. Bish 2013-04-03 19:22:16 +00:00
parent 14a93811c6
commit 8e388b86d6
3 changed files with 195 additions and 20 deletions

View File

@ -58,14 +58,19 @@ public class MemoryUsage extends Usage<MemoryUsage> {
/**
* @throws InterruptedException
*/
@Override
public void waitForSpace() throws InterruptedException {
if (parent != null) {
parent.waitForSpace();
}
synchronized (usageMutex) {
for (int i = 0; percentUsage >= 100; i++) {
while (percentUsage >= 100 && isStarted()) {
usageMutex.wait();
}
if (percentUsage >= 100 && !isStarted()) {
throw new InterruptedException("waitForSpace stopped during wait.");
}
}
}
@ -74,6 +79,7 @@ public class MemoryUsage extends Usage<MemoryUsage> {
* @throws InterruptedException
* @return true if space
*/
@Override
public boolean waitForSpace(long timeout) throws InterruptedException {
if (parent != null) {
if (!parent.waitForSpace(timeout)) {
@ -88,6 +94,7 @@ public class MemoryUsage extends Usage<MemoryUsage> {
}
}
@Override
public boolean isFull() {
if (parent != null && parent.isFull()) {
return true;
@ -125,7 +132,7 @@ public class MemoryUsage extends Usage<MemoryUsage> {
}
setPercentUsage(percentUsage);
if (parent != null) {
((MemoryUsage)parent).increaseUsage(value);
parent.increaseUsage(value);
}
}
@ -149,10 +156,12 @@ public class MemoryUsage extends Usage<MemoryUsage> {
}
}
@Override
protected long retrieveUsage() {
return usage;
}
@Override
public long getUsage() {
return usage;
}

View File

@ -262,6 +262,7 @@ public abstract class Usage<T extends Usage> implements Service {
if (!listeners.isEmpty()) {
// Let the listeners know on a separate thread
Runnable listenerNotifier = new Runnable() {
@Override
public void run() {
for (Iterator<UsageListener> iter = listeners.iterator(); iter.hasNext();) {
UsageListener l = iter.next();
@ -290,6 +291,7 @@ public abstract class Usage<T extends Usage> implements Service {
+ (parent != null ? ";Parent:" + parent.toString() : "");
}
@Override
@SuppressWarnings("unchecked")
public void start() {
if (started.compareAndSet(false, true)){
@ -306,6 +308,7 @@ public abstract class Usage<T extends Usage> implements Service {
}
}
@Override
@SuppressWarnings("unchecked")
public void stop() {
if (started.compareAndSet(true, false)){
@ -348,6 +351,7 @@ public abstract class Usage<T extends Usage> implements Service {
if (parent != null) {
Runnable r = new Runnable() {
@Override
public void run() {
synchronized (usageMutex) {
if (percentUsage >= 100) {
@ -415,7 +419,12 @@ public abstract class Usage<T extends Usage> implements Service {
public void setExecutor (ThreadPoolExecutor executor) {
this.executor = executor;
}
public ThreadPoolExecutor getExecutor() {
return executor;
}
public boolean isStarted() {
return started.get();
}
}

View File

@ -0,0 +1,157 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.bugs;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.broker.region.policy.PolicyEntry;
import org.apache.activemq.broker.region.policy.PolicyMap;
import org.apache.activemq.broker.region.policy.VMPendingQueueMessageStoragePolicy;
import org.apache.activemq.broker.region.policy.VMPendingSubscriberMessageStoragePolicy;
import org.apache.activemq.command.ActiveMQDestination;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AMQ4361Test {
private static final Logger LOG = LoggerFactory.getLogger(AMQ4361Test.class);
private BrokerService service;
private String brokerUrlString;
@Before
public void setUp() throws Exception {
service = new BrokerService();
service.setDeleteAllMessagesOnStartup(true);
service.setUseJmx(false);
PolicyMap policyMap = new PolicyMap();
PolicyEntry policy = new PolicyEntry();
policy.setMemoryLimit(1);
policy.setPendingSubscriberPolicy(new VMPendingSubscriberMessageStoragePolicy());
policy.setPendingQueuePolicy(new VMPendingQueueMessageStoragePolicy());
policy.setProducerFlowControl(true);
policyMap.setDefaultEntry(policy);
service.setDestinationPolicy(policyMap);
service.setAdvisorySupport(false);
brokerUrlString = service.addConnector("tcp://localhost:0").getPublishableConnectString();
service.start();
service.waitUntilStarted();
}
@After
public void tearDown() throws Exception {
if (service != null) {
service.stop();
service.waitUntilStopped();
}
}
@Test
public void testCloseWhenHunk() throws Exception {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerUrlString);
connectionFactory.setProducerWindowSize(1024);
// TINY QUEUE is flow controlled after 1024 bytes
final ActiveMQDestination destination =
ActiveMQDestination.createDestination("queue://TINY_QUEUE", (byte) 0xff);
Connection connection = connectionFactory.createConnection();
connection.start();
final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
final MessageProducer producer = session.createProducer(destination);
producer.setTimeToLive(0);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
final AtomicReference<Exception> publishException = new AtomicReference<Exception>(null);
final AtomicReference<Exception> closeException = new AtomicReference<Exception>(null);
final AtomicLong lastLoop = new AtomicLong(System.currentTimeMillis() + 100);
Thread pubThread = new Thread(new Runnable() {
@Override
public void run() {
try {
byte[] data = new byte[1000];
new Random(0xdeadbeef).nextBytes(data);
for (int i = 0; i < 10000; i++) {
lastLoop.set(System.currentTimeMillis());
ObjectMessage objMsg = session.createObjectMessage();
objMsg.setObject(data);
producer.send(destination, objMsg);
}
} catch (Exception e) {
publishException.set(e);
}
}
}, "PublishingThread");
pubThread.start();
// wait for publisher to deadlock
while (System.currentTimeMillis() - lastLoop.get() < 2000) {
Thread.sleep(100);
}
LOG.info("Publisher deadlock detected.");
Thread closeThread = new Thread(new Runnable() {
@Override
public void run() {
try {
LOG.info("Attempting close..");
producer.close();
} catch (Exception e) {
closeException.set(e);
}
}
}, "ClosingThread");
closeThread.start();
try {
closeThread.join(30000);
} catch (InterruptedException ie) {
assertFalse("Closing thread didn't complete in 10 seconds", true);
}
try {
pubThread.join(30000);
} catch (InterruptedException ie) {
assertFalse("Publishing thread didn't complete in 10 seconds", true);
}
assertNull(closeException.get());
assertNotNull(publishException.get());
}
}