allthingssecurity commented on code in PR #30623:
URL: https://github.com/apache/camel/pull/30623#discussion_r4660949322


##########
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:
   Good catch, done in 32229d1fa5d. It is a bit more than a stray count: on a 
pool whose service is not created yet, reacquire(null) returned true. No caller 
passes null today, but the guard costs nothing. 
testReacquireOfNullIsNotCountedAsAUser covers it (it calls the package-private 
method through reflection, as the test is in another package).



##########
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:
   I left this one as is. The loop does not spin on a flag: it retries only 
when the pool it got was evicted or stopped by another thread in the meantime, 
and that pool is then removed from the map, so the next round creates a new 
pool (and a new producer). Each retry is caused by progress of another thread, 
and stopping the cache ends it through the isStarted() check. A yield or a 
retry limit would not change that, and a limit would make acquire fail under 
load where it now succeeds.



-- 
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]

Reply via email to