gnodet-bot commented on code in PR #30623:
URL: https://github.com/apache/camel/pull/30623#discussion_r4660858098
##########
core/camel-support/src/main/java/org/apache/camel/support/cache/ServicePool.java:
##########
@@ -234,42 +265,87 @@ private static <S extends Service> void stop(S s) {
/**
* Pool used for singleton producers or consumers which are thread-safe
and can be shared by multiple worker threads
* at any given time.
+ * <p>
+ * The pool counts the users of its service (acquired and not yet
released), so an evicted service is stopped when
+ * it is no longer in use. An evicted pool is taken out of the pool map at
once, so the next acquire of the endpoint
+ * creates a new pool and service.
*/
private class SinglePool implements Pool<S> {
private final Endpoint endpoint;
private volatile S s;
+ // the acquires and the releases of s, counted apart without
contention between the worker threads (the users of
+ // s are the difference)
+ private final LongAdder acquires = new LongAdder();
+ private final LongAdder releases = new LongAdder();
+ // s has been evicted: it is no longer handed out, and is stopped when
it has no more users
+ private volatile boolean evicted;
+ private final AtomicBoolean stopped = new AtomicBoolean();
SinglePool(Endpoint endpoint) {
this.endpoint = requireNonNull(endpoint);
}
@Override
public S acquire() throws Exception {
- cleanupEvicts();
-
- if (s == null) {
+ if (!retain()) {
+ return null;
+ }
+ S answer = s;
+ if (answer == null) {
+ boolean acquired = false;
lock.lock();
try {
- if (s == null) {
+ answer = s;
+ if (answer == null && !stopped.get()) {
LOG.trace("Creating service from endpoint: {}",
endpoint);
S tempS = creator.apply(endpoint);
endpoint.getCamelContext().addService(tempS, true,
true);
s = tempS;
+ answer = tempS;
}
+ acquired = answer != null;
} finally {
lock.unlock();
+ if (!acquired) {
+ // creating failed, or the pool was stopped meanwhile
+ unretain();
+ }
}
}
- LOG.trace("Acquired service: {}", s);
- return s;
+ LOG.trace("Acquired service: {}", answer);
+ return answer;
}
@Override
- public void release(S s) {
- cleanupEvicts();
+ public boolean reacquire(S s) {
+ return s == this.s && retain();
Review Comment:
⚠️ **Potential null-null equality:** if `s` is `null` and `stopEvicted()`
has already set `this.s = null`, then `null == null` returns `true` and
`retain()` is called — producing a spurious LongAdder increment before `evicted
|| stopped.get()` catches it in `unretain()`. Callers should never pass null,
but a defensive guard makes the invariant explicit:
```suggestion
public boolean reacquire(S s) {
return s != null && s == this.s && retain();
}
```
##########
core/camel-support/src/main/java/org/apache/camel/support/cache/ServicePool.java:
##########
@@ -114,16 +125,34 @@ protected void onEvict(S s) {
* @return the acquired producer/consumer
*/
public S acquire(Endpoint endpoint) throws Exception {
- if (!isStarted()) {
- return null;
+ S s = null;
+ while (s == null) {
+ if (!isStarted()) {
+ return null;
+ }
+ // a single pool returns null when it was evicted or stopped
meanwhile, and is then no longer in the pool
+ // map, so a new pool is used
+ s = getOrCreatePool(endpoint).acquire();
Review Comment:
💡 **No backoff in retry loop:** under pathological capacity=1 contention
(continuous LRU eviction under load), this loop can spin without yielding CPU.
Consider adding `Thread.yield()` between iterations or a maximum retry count to
surface pathological conditions.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]