You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by gi...@apache.org on 2022/06/15 04:40:51 UTC

[druid] branch master updated: NettyHttpClient: Fix double-return on certain exceptions. (#12626)

This is an automated email from the ASF dual-hosted git repository.

gian pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/druid.git


The following commit(s) were added to refs/heads/master by this push:
     new 283249c51b NettyHttpClient: Fix double-return on certain exceptions. (#12626)
283249c51b is described below

commit 283249c51b3e748fcd7eef942a2c509904708670
Author: Gian Merlino <gi...@gmail.com>
AuthorDate: Tue Jun 14 21:40:47 2022 -0700

    NettyHttpClient: Fix double-return on certain exceptions. (#12626)
    
    The "exceptionCaught" handler may get called multiple times. We should
    only return the channel to the pool the first time. Returning it more
    than once leads to a warning like "Resource at key[%s] was returned
    multiple times?"
---
 .../org/apache/druid/java/util/http/client/NettyHttpClient.java   | 8 +++++++-
 .../druid/java/util/http/client/pool/ResourceContainer.java       | 1 +
 .../org/apache/druid/java/util/http/client/pool/ResourcePool.java | 8 +++++++-
 3 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/core/src/main/java/org/apache/druid/java/util/http/client/NettyHttpClient.java b/core/src/main/java/org/apache/druid/java/util/http/client/NettyHttpClient.java
index 3e7376222f..f873da994a 100644
--- a/core/src/main/java/org/apache/druid/java/util/http/client/NettyHttpClient.java
+++ b/core/src/main/java/org/apache/druid/java/util/http/client/NettyHttpClient.java
@@ -317,6 +317,8 @@ public class NettyHttpClient extends AbstractHttpClient
               }
             }
 
+            // Ignore return value of setException, since exceptionCaught can be called multiple times and we
+            // only want to report the first one.
             if (event.getCause() instanceof ReadTimeoutException) {
               // ReadTimeoutException thrown by ReadTimeoutHandler is a singleton with a misleading stack trace.
               // No point including it: instead, we replace it with a fresh exception.
@@ -338,7 +340,11 @@ public class NettyHttpClient extends AbstractHttpClient
               log.warn(e, "Error while closing channel");
             }
             finally {
-              channelResourceContainer.returnResource();
+              if (channelResourceContainer.isPresent()) {
+                // exceptionCaught can be called multiple times: we only want to return the channel if it hasn't
+                // already been returned.
+                channelResourceContainer.returnResource();
+              }
             }
           }
 
diff --git a/core/src/main/java/org/apache/druid/java/util/http/client/pool/ResourceContainer.java b/core/src/main/java/org/apache/druid/java/util/http/client/pool/ResourceContainer.java
index 37fc23ea5c..7f18fdd18a 100644
--- a/core/src/main/java/org/apache/druid/java/util/http/client/pool/ResourceContainer.java
+++ b/core/src/main/java/org/apache/druid/java/util/http/client/pool/ResourceContainer.java
@@ -24,5 +24,6 @@ package org.apache.druid.java.util.http.client.pool;
 public interface ResourceContainer<ResourceType>
 {
   ResourceType get();
+  boolean isPresent();
   void returnResource();
 }
diff --git a/core/src/main/java/org/apache/druid/java/util/http/client/pool/ResourcePool.java b/core/src/main/java/org/apache/druid/java/util/http/client/pool/ResourcePool.java
index 7727bacf14..6ed025c768 100644
--- a/core/src/main/java/org/apache/druid/java/util/http/client/pool/ResourcePool.java
+++ b/core/src/main/java/org/apache/druid/java/util/http/client/pool/ResourcePool.java
@@ -119,11 +119,17 @@ public class ResourcePool<K, V> implements Closeable
         return value;
       }
 
+      @Override
+      public boolean isPresent()
+      {
+        return !returned.get();
+      }
+
       @Override
       public void returnResource()
       {
         if (returned.getAndSet(true)) {
-          log.warn(StringUtils.format("Resource at key[%s] was returned multiple times?", key));
+          log.warn("Resource at key[%s] was returned multiple times?", key);
         } else {
           holder.giveBack(value);
         }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org