-
Notifications
You must be signed in to change notification settings - Fork 1.1k
GH-3826: Fix SimplePool for resizing from MAX #3829
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* Copyright 2002-2021 the original author or authors. | ||
* Copyright 2002-2022 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. | ||
|
@@ -48,19 +48,19 @@ public class SimplePool<T> implements Pool<T> { | |
|
||
protected final Log logger = LogFactory.getLog(getClass()); // NOSONAR final | ||
|
||
private final Semaphore permits = new Semaphore(0); | ||
private final PoolSemaphore permits = new PoolSemaphore(0); | ||
|
||
private final AtomicInteger poolSize = new AtomicInteger(); | ||
|
||
private final AtomicInteger targetPoolSize = new AtomicInteger(); | ||
|
||
private long waitTimeout = Long.MAX_VALUE; | ||
|
||
private final BlockingQueue<T> available = new LinkedBlockingQueue<T>(); | ||
private final BlockingQueue<T> available = new LinkedBlockingQueue<>(); | ||
|
||
private final Set<T> allocated = Collections.synchronizedSet(new HashSet<T>()); | ||
private final Set<T> allocated = Collections.synchronizedSet(new HashSet<>()); | ||
|
||
private final Set<T> inUse = Collections.synchronizedSet(new HashSet<T>()); | ||
private final Set<T> inUse = Collections.synchronizedSet(new HashSet<>()); | ||
|
||
private final PoolItemCallback<T> callback; | ||
|
||
|
@@ -105,21 +105,27 @@ public synchronized void setPoolSize(int poolSize) { | |
this.permits.release(delta); | ||
} | ||
else { | ||
while (delta < 0) { | ||
if (!this.permits.tryAcquire()) { | ||
break; | ||
} | ||
this.permits.reducePermits(-delta); | ||
|
||
int inUseSize = this.inUse.size(); | ||
int newPoolSize = Math.max(poolSize, inUseSize); | ||
this.poolSize.set(newPoolSize); | ||
|
||
for (int i = this.available.size(); i > newPoolSize - inUseSize; i--) { | ||
T item = this.available.poll(); | ||
if (item != null) { | ||
doRemoveItem(item); | ||
} | ||
this.poolSize.decrementAndGet(); | ||
delta++; | ||
else { | ||
break; | ||
} | ||
} | ||
|
||
int inUseDelta = poolSize - inUseSize; | ||
if (inUseDelta < 0 && this.logger.isDebugEnabled()) { | ||
this.logger.debug(String.format("Pool is overcommitted by %d; items will be removed when returned", | ||
-inUseDelta)); | ||
} | ||
} | ||
if (delta < 0 && this.logger.isDebugEnabled()) { | ||
this.logger.debug(String.format("Pool is overcommitted by %d; items will be removed when returned", | ||
-delta)); | ||
} | ||
} | ||
|
||
|
@@ -266,6 +272,19 @@ public synchronized void close() { | |
removeAllIdleItems(); | ||
} | ||
|
||
private static class PoolSemaphore extends Semaphore { | ||
|
||
public PoolSemaphore(int permits) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be package protected (will solve checkstyle too). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah... Saw that. Trying to fix the Thank you if you are going to fix this! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, will do. |
||
super(permits); | ||
} | ||
|
||
@Override | ||
public void reducePermits(int reduction) { // NOSONAR increases visibility | ||
super.reducePermits(reduction); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* User of the pool provide an implementation of this interface; called during | ||
* various pool operations. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Guess this should help in the default init case we had