davsclaus commented on code in PR #30623:
URL: https://github.com/apache/camel/pull/30623#discussion_r4666164596
##########
core/camel-support/src/main/java/org/apache/camel/support/cache/ServicePool.java:
##########
@@ -279,44 +395,93 @@ public int size() {
@Override
public void stop() {
- S toStop;
+ stopped.set(true);
+ S toStop = takeService();
+ pool.remove(endpoint, this);
+ if (toStop != null) {
+ singlePoolEvicted.remove(toStop, this);
+ }
+ doStop(toStop);
+ if (evicted && toStop != null) {
+ // an evicted pool whose service was still in use (stopped
with the ServicePool): stop its endpoint as
+ // well when nothing else uses it, as its last release would
have done
+ stopEndpointIfUnused();
+ }
+ }
+
+ /**
+ * Takes the service out of the pool, under the lock that creates the
services, so a creation in progress either
+ * completes before (and its service is taken) or sees the pool stopped
+ */
+ private S takeService() {
lock.lock();
try {
- toStop = s;
+ S answer = s;
s = null;
+ return answer;
} finally {
lock.unlock();
}
- doStop(toStop);
- pool.remove(endpoint);
}
@Override
public void evict(S s) {
- singlePoolEvicted.putIfAbsent(endpoint, this);
+ if (s != this.s || evicted) {
+ // a service of an evicted pool of this endpoint, which an
acquire in progress put in the cache again: its
+ // pool stops it when it is no longer in use
+ return;
+ }
+ // keep track of the evicted pool before it leaves the pool map,
so the users of s can release it
+ singlePoolEvicted.put(s, this);
+ pool.remove(endpoint, this);
+ evicted = true;
+ // stopped after the cache operation (if idle), not here under the
write lock of the cache
+ evictedToStop.add(this);
}
- @Override
- public void cleanUp() {
- cleanupEvicts();
+ /**
+ * Stops the evicted service if it has no more users
+ */
+ private void stopEvicted() {
+ // the releases are summed first: an acquire is counted before its
release, so every release seen here has
+ // its acquire seen in the sum below, and an acquire of a user
that may still use s (it was counted before s
+ // was evicted) is seen as well, so the counts are only equal when
s has no more users
+ long released = releases.sum();
+ if (acquires.sum() != released || !stopped.compareAndSet(false,
true)) {
Review Comment:
Fixed in code instead of a comment, in c9d28d18. The suggested comment does
not hold: `LongAdder.increment()` is only a release write
(`weakCompareAndSetRelease` of the base or a cell), so the volatile `evicted`
gives no ordering here. `retain()` increments `acquires` and then reads
`evicted`, and the eviction writes `evicted` and then sums the counts: with a
release write, the read of `evicted` may pass the increment, so `retain()`
could read false while `stopEvicted()` misses the acquire, and stop the service
in use. `unretain()` has the same pattern (an evicted service would not be
stopped). There is now a `VarHandle.fullFence()` after each increment, and the
comment on the counters explains it. It cannot happen on x86, where the CAS is
a full fence, but the JMM does not promise it. The ServicePool, producer and
consumer cache tests of camel-core pass (41).
_Claude Code on behalf of Claus Ibsen_
--
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]