You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nifi.apache.org by jo...@apache.org on 2021/01/27 22:04:52 UTC

[nifi] branch main updated: NIFI-8177 This closes #4786. Added timeout to Sender Pool polling to avoid blocking indefinitely

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

joewitt pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/nifi.git


The following commit(s) were added to refs/heads/main by this push:
     new 8c1fe84  NIFI-8177 This closes #4786. Added timeout to Sender Pool polling to avoid blocking indefinitely
8c1fe84 is described below

commit 8c1fe84f62e549b0f87741dc2b83dc7c28aab4e8
Author: exceptionfactory <ex...@gmail.com>
AuthorDate: Wed Jan 27 10:17:38 2021 -0600

    NIFI-8177 This closes #4786. Added timeout to Sender Pool polling to avoid blocking indefinitely
    
    Signed-off-by: Joe Witt <jo...@apache.org>
---
 .../util/put/AbstractPutEventProcessor.java        | 34 ++++++++++++++++++----
 .../processors/standard/util/TestPutTCPCommon.java |  4 +--
 2 files changed, 30 insertions(+), 8 deletions(-)

diff --git a/nifi-nar-bundles/nifi-extension-utils/nifi-processor-utils/src/main/java/org/apache/nifi/processor/util/put/AbstractPutEventProcessor.java b/nifi-nar-bundles/nifi-extension-utils/nifi-processor-utils/src/main/java/org/apache/nifi/processor/util/put/AbstractPutEventProcessor.java
index a5ba8ba..8eed270 100644
--- a/nifi-nar-bundles/nifi-extension-utils/nifi-processor-utils/src/main/java/org/apache/nifi/processor/util/put/AbstractPutEventProcessor.java
+++ b/nifi-nar-bundles/nifi-extension-utils/nifi-processor-utils/src/main/java/org/apache/nifi/processor/util/put/AbstractPutEventProcessor.java
@@ -158,14 +158,17 @@ public abstract class AbstractPutEventProcessor extends AbstractSessionFactoryPr
             .description("FlowFiles that failed to send to the destination are sent out this relationship.")
             .build();
 
+    private static final long SENDER_POOL_POLL_TIMEOUT = 250;
+    private static final TimeUnit SENDER_POOL_POLL_TIMEOUT_UNIT = TimeUnit.MILLISECONDS;
+
     private Set<Relationship> relationships;
     private List<PropertyDescriptor> descriptors;
 
     protected volatile String transitUri;
-    protected volatile BlockingQueue<ChannelSender> senderPool;
+    private volatile BlockingQueue<ChannelSender> senderPool;
 
     protected final BlockingQueue<FlowFileMessageBatch> completeBatches = new LinkedBlockingQueue<>();
-    protected final Set<FlowFileMessageBatch> activeBatches = Collections.synchronizedSet(new HashSet<FlowFileMessageBatch>());
+    protected final Set<FlowFileMessageBatch> activeBatches = Collections.synchronizedSet(new HashSet<>());
 
     @Override
     protected void init(final ProcessorInitializationContext context) {
@@ -222,10 +225,10 @@ public abstract class AbstractPutEventProcessor extends AbstractSessionFactoryPr
     @OnStopped
     public void closeSenders() {
         if (senderPool != null) {
-            ChannelSender sender = senderPool.poll();
+            ChannelSender sender = pollSenderPool();
             while (sender != null) {
                 sender.close();
-                sender = senderPool.poll();
+                sender = pollSenderPool();
             }
         }
     }
@@ -264,7 +267,7 @@ public abstract class AbstractPutEventProcessor extends AbstractSessionFactoryPr
 
         // if a connection hasn't been used with in the threshold then it gets closed
         ChannelSender sender;
-        while ((sender = senderPool.poll()) != null) {
+        while ((sender = pollSenderPool()) != null) {
             numConsidered++;
             if (currentTime > (sender.getLastUsed() + idleThreshold)) {
                 getLogger().debug("Closing idle connection...");
@@ -338,7 +341,7 @@ public abstract class AbstractPutEventProcessor extends AbstractSessionFactoryPr
      * @return ChannelSender - the sender that has been acquired or null if no sender is available and a new sender cannot be created.
      */
     protected ChannelSender acquireSender(final ProcessContext context, final ProcessSession session, final FlowFile flowFile) {
-        ChannelSender sender = senderPool.poll();
+        ChannelSender sender = pollSenderPool();
         if (sender == null) {
             try {
                 getLogger().debug("No available connections, creating a new one...");
@@ -608,4 +611,23 @@ public abstract class AbstractPutEventProcessor extends AbstractSessionFactoryPr
         }
         return delimiter;
     }
+
+    /**
+     * Poll Sender Pool when not empty using a timeout to avoid blocking indefinitely
+     *
+     * @return Channel Sender or null when not found
+     */
+    private ChannelSender pollSenderPool() {
+        ChannelSender channelSender = null;
+
+        if (!senderPool.isEmpty()) {
+            try {
+                channelSender = senderPool.poll(SENDER_POOL_POLL_TIMEOUT, SENDER_POOL_POLL_TIMEOUT_UNIT);
+            } catch (final InterruptedException e) {
+                getLogger().warn("Interrupted while polling for ChannelSender", e);
+            }
+        }
+
+        return channelSender;
+    }
 }
diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/util/TestPutTCPCommon.java b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/util/TestPutTCPCommon.java
index b2a8eb7..0e0acb6 100644
--- a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/util/TestPutTCPCommon.java
+++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/util/TestPutTCPCommon.java
@@ -137,8 +137,8 @@ public abstract class TestPutTCPCommon {
         checkReceivedAllData(recvQueue, VALID_FILES);
         checkInputQueueIsEmpty();
         checkTotalNumConnections(server, 1);
-        runner.setProperty(PutTCP.IDLE_EXPIRATION, "100 ms");
-        Thread.sleep(200);
+        runner.setProperty(PutTCP.IDLE_EXPIRATION, "500 ms");
+        Thread.sleep(1000);
         runner.run(1, false, false);
         runner.clearTransferState();
         sendTestData(VALID_FILES);