INT-3112 Fix OOM in SimplePool
The SimplePool maintains an 'allocated' set, for the sole reason of preventing a "foreign" (non-managed) object being returned. When a pool item is detected as stale, it is removed from the pool but remains in the 'allocated' set. Add a test to verify the allocated size is reduced when a stale item is popped from the pool. Add a FileTransferringMessageHandler test (where the problem was discovered). Fix a test in TCP to expect a close(). Polishing Do not allow returning null items to just release a permit - it cannot remove the item from allocated. Clients must return the stale item to the pool so it can be refreshed on the next get. Only used by the TCP caching CF when returning a connection when the factory is not running; but should not be allowed. Also, protect against double release - not currently an issue with existing users of SimplePool, but should be protected against. Could cause the permit count to exceed the pool size. Add inUse Set to the pool so we can detect attempts to release an item that has already been released.
This commit is contained in:
@@ -54,6 +54,8 @@ public class SimplePool<T> implements Pool<T> {
|
||||
|
||||
private final Set<T> allocated = Collections.synchronizedSet(new HashSet<T>());
|
||||
|
||||
private final Set<T> inUse = Collections.synchronizedSet(new HashSet<T>());
|
||||
|
||||
private final PoolItemCallback<T> callback;
|
||||
|
||||
/**
|
||||
@@ -127,7 +129,7 @@ public class SimplePool<T> implements Pool<T> {
|
||||
}
|
||||
|
||||
public int getActiveCount() {
|
||||
return this.getAllocatedCount() - this.getIdleCount();
|
||||
return this.inUse.size();
|
||||
}
|
||||
|
||||
public int getAllocatedCount() {
|
||||
@@ -192,32 +194,42 @@ public class SimplePool<T> implements Pool<T> {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Received a stale item, will attempt to get a new one.");
|
||||
}
|
||||
doRemoveItem(item);
|
||||
item = doGetItem();
|
||||
}
|
||||
this.inUse.add(item);
|
||||
return item;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an item to the pool. Item may be null, in which case a subsequent getItem()
|
||||
* will return a new instance.
|
||||
* Returns an item to the pool.
|
||||
*/
|
||||
public synchronized void releaseItem(T item) {
|
||||
Assert.isTrue(item == null || this.allocated.contains(item),
|
||||
Assert.notNull(item, "Item cannot be null");
|
||||
Assert.isTrue(this.allocated.contains(item),
|
||||
"You can only release items that were obtained from the pool");
|
||||
if (this.poolSize.get() > targetPoolSize.get()) {
|
||||
poolSize.decrementAndGet();
|
||||
if (item != null) {
|
||||
doRemoveItem(item);
|
||||
if (this.inUse.contains(item)) {
|
||||
if (this.poolSize.get() > targetPoolSize.get()) {
|
||||
poolSize.decrementAndGet();
|
||||
if (item != null) {
|
||||
doRemoveItem(item);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (logger.isDebugEnabled()){
|
||||
logger.debug("Releasing " + item + " back to the pool");
|
||||
}
|
||||
if (item != null) {
|
||||
this.available.add(item);
|
||||
this.inUse.remove(item);
|
||||
}
|
||||
permits.release();
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (logger.isDebugEnabled()){
|
||||
logger.debug("Releasing " + item + " back to the pool");
|
||||
logger.debug("Ignoring release of " + item + " back to the pool - not in use");
|
||||
}
|
||||
if (item != null) {
|
||||
available.add(item);
|
||||
}
|
||||
permits.release();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -230,6 +242,7 @@ public class SimplePool<T> implements Pool<T> {
|
||||
|
||||
private void doRemoveItem(T item) {
|
||||
this.allocated.remove(item);
|
||||
this.inUse.remove(item);
|
||||
this.callback.removedFromPool(item);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -23,10 +23,13 @@ import static org.junit.Assert.fail;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.Semaphore;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.integration.MessagingException;
|
||||
import org.springframework.integration.test.util.TestUtils;
|
||||
|
||||
/**
|
||||
* @author Gary Russell
|
||||
@@ -51,6 +54,7 @@ public class SimplePoolTests {
|
||||
s3 = pool.getItem();
|
||||
assertNotSame(s1, s3);
|
||||
assertFalse(strings.remove(s1));
|
||||
assertEquals(2, pool.getAllocatedCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -128,6 +132,22 @@ public class SimplePoolTests {
|
||||
pool.releaseItem("Hello, world!");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoubleReturn() {
|
||||
final Set<String> strings = new HashSet<String>();
|
||||
final AtomicBoolean stale = new AtomicBoolean();
|
||||
SimplePool<String> pool = stringPool(2, strings, stale);
|
||||
Semaphore permits = TestUtils.getPropertyValue(pool, "permits", Semaphore.class);
|
||||
assertEquals(2, permits.availablePermits());
|
||||
String s1 = pool.getItem();
|
||||
assertEquals(1, permits.availablePermits());
|
||||
pool.releaseItem(s1);
|
||||
assertEquals(2, permits.availablePermits());
|
||||
pool.releaseItem(s1);
|
||||
assertEquals(2, permits.availablePermits());
|
||||
}
|
||||
|
||||
|
||||
private SimplePool<String> stringPool(int size, final Set<String> strings,
|
||||
final AtomicBoolean stale) {
|
||||
SimplePool<String> pool = new SimplePool<String>(size, new SimplePool.PoolItemCallback<String>() {
|
||||
|
||||
Reference in New Issue
Block a user