Skip to content

Fix for SimplePool.setPoolSize() #3143 #3144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
* demand up to the limit.
*
* @author Gary Russell
* @author Sergey Bogatyrev
* @since 2.2
*
*/
Expand Down Expand Up @@ -102,11 +103,9 @@ public synchronized void setPoolSize(int poolSize) {
break;
}
T item = this.available.poll();
if (item == null) {
this.permits.release();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have stopped to release permits.
Why is that, please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this branch we need to call only acquire(). Original intent was to revert acquire if item==null. But this is absolutely incorrect. For example, if we have a new instance of pool, then "available" collection is empty. But setPoolSize() method should correctly update all structures. After calling setPoolSize(5) everything should contain 5. (permits==5, poolSize==5, and targetPoolSize==5). Of course, I am describing the situation when no objects were allocated. Please, check added test cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added additional checks to test that "permits" field is correctly updated

break;
if (item != null) {
doRemoveItem(item);
}
doRemoveItem(item);
this.poolSize.decrementAndGet();
delta++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicBoolean;
Expand All @@ -30,6 +32,7 @@

/**
* @author Gary Russell
* @author Sergey Bogatyrev
* @since 2.2
*
*/
Expand Down Expand Up @@ -147,6 +150,72 @@ public void testDoubleReturn() {
assertThat(permits.availablePermits()).isEqualTo(2);
}

@Test
public void testSizeUpdateIfNotAllocated() {
SimplePool<String> pool = stringPool(10, new HashSet<>(), new AtomicBoolean());
pool.setWaitTimeout(0);
pool.setPoolSize(5);
assertThat(pool.getPoolSize()).isEqualTo(5);

// allocating all available items to check permits
Set<String> allocatedItems = new HashSet<>();
for (int i = 0; i < 5; i++) {
allocatedItems.add(pool.getItem());
}
assertThat(allocatedItems).hasSize(5);

// no more items can be allocated (indirect check of permits)
try {
pool.getItem();
fail("No more items should be allocated");
}
catch (PoolItemNotAvailableException e) {
// permits state correctly
}
}

@Test
public void testSizeUpdateIfAllocated() {
SimplePool<String> pool = stringPool(10, new HashSet<>(), new AtomicBoolean());
pool.setWaitTimeout(0);
List<String> allocated = new ArrayList<>();
for (int i = 0; i < 10; i++) {
allocated.add(pool.getItem());
}

// release only 2 items
for (int i = 0; i < 2; i++) {
pool.releaseItem(allocated.get(i));
}

// trying to reduce pool size
pool.setPoolSize(5);

// at this moment the actual pool size can be reduced only partially, because
// only 2 items have been released, so 8 items are in use
assertThat(pool.getPoolSize()).isEqualTo(8);
assertThat(pool.getAllocatedCount()).isEqualTo(8);
assertThat(pool.getIdleCount()).isEqualTo(0);

// releasing 3 items
for (int i = 2; i < 5; i++) {
pool.releaseItem(allocated.get(i));
}

// now pool size should be reduced
assertThat(pool.getPoolSize()).isEqualTo(5);
assertThat(pool.getAllocatedCount()).isEqualTo(5);
assertThat(pool.getIdleCount()).isEqualTo(0);

// no more items can be allocated (indirect check of permits)
try {
pool.getItem();
fail("No more items should be allocated");
}
catch (PoolItemNotAvailableException e) {
// permits state correctly
}
}

private SimplePool<String> stringPool(int size, final Set<String> strings,
final AtomicBoolean stale) {
Expand Down