You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by or...@apache.org on 2016/05/11 15:11:01 UTC

svn commit: r1743387 - in /qpid/java/branches/6.0.x: ./ client/src/main/java/org/apache/qpid/client/AMQSession.java client/src/main/java/org/apache/qpid/client/util/FlowControllingBlockingQueue.java

Author: orudyy
Date: Wed May 11 15:11:01 2016
New Revision: 1743387

URL: http://svn.apache.org/viewvc?rev=1743387&view=rev
Log:
QPID-7237: [Java Client] Use single thread thread-pool to perform flow control on no-ack sessions

* Avoids spawning new thread for each state change
* Coalesce flow control tasks for no-ack sessions
* Change the lower prefetch threshold to be half of upper prefetch threshold when the same values for thresholds are specified

merged from trunk using
svn merge -c 1742900,1743383  ^/qpid/java/trunk

Modified:
    qpid/java/branches/6.0.x/   (props changed)
    qpid/java/branches/6.0.x/client/src/main/java/org/apache/qpid/client/AMQSession.java
    qpid/java/branches/6.0.x/client/src/main/java/org/apache/qpid/client/util/FlowControllingBlockingQueue.java

Propchange: qpid/java/branches/6.0.x/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Wed May 11 15:11:01 2016
@@ -9,5 +9,5 @@
 /qpid/branches/java-broker-vhost-refactor/java:1493674-1494547
 /qpid/branches/java-network-refactor/qpid/java:805429-821809
 /qpid/branches/qpid-2935/qpid/java:1061302-1072333
-/qpid/java/trunk:1715445-1715447,1715586,1715940,1716086-1716087,1716127-1716128,1716141,1716153,1716155,1716194,1716204,1716209,1716227,1716277,1716357,1716368,1716370,1716374,1716432,1716444-1716445,1716455,1716461,1716474,1716489,1716497,1716515,1716555,1716602,1716606-1716610,1716619,1716636,1717269,1717299,1717401,1717446,1717449,1717626,1717691,1717735,1717780,1718744,1718889,1718893,1718918,1718922,1719026,1719028,1719033,1719037,1719047,1719051,1720340,1720664,1721151,1721198,1722019-1722020,1722246,1722339,1722416,1722674,1722678,1722683,1722711,1723064,1723194,1723563,1724216,1724251,1724257,1724292,1724375,1724397,1724432,1724582,1724603,1724780,1724843-1724844,1725295,1725569,1725760,1726176,1726244-1726246,1726249,1726358,1726436,1726449,1726456,1726646,1726653,1726755,1726778,1727532,1727555,1727608,1727951,1727954,1728089,1728167,1728302,1728497,1728501,1728524,1728639,1728651,1728772,1729215,1729297,1729347,1729356,1729406,1729408,1729412,1729515,1729638,1729656-1729
 657,1729783,1729828,1729832,1729841,1729851,1729886,1729904,1729973,1730019,1730025,1730052,1730072,1730088,1730494,1730499,1730547,1730559,1730567,1730578,1730585,1730651,1730697,1730712-1730713,1730805,1731029,1731110,1731210,1731225,1731444,1731551,1731612,1732184,1732452,1732461,1732525,1732812,1733467,1734452,1736478,1736751,1736838,1737804,1737835,1737853,1737984,1737992,1738119,1738135,1738231,1738271,1738607,1738610,1738731,1738914,1741702,1742257,1742284,1742544,1742926
+/qpid/java/trunk:1715445-1715447,1715586,1715940,1716086-1716087,1716127-1716128,1716141,1716153,1716155,1716194,1716204,1716209,1716227,1716277,1716357,1716368,1716370,1716374,1716432,1716444-1716445,1716455,1716461,1716474,1716489,1716497,1716515,1716555,1716602,1716606-1716610,1716619,1716636,1717269,1717299,1717401,1717446,1717449,1717626,1717691,1717735,1717780,1718744,1718889,1718893,1718918,1718922,1719026,1719028,1719033,1719037,1719047,1719051,1720340,1720664,1721151,1721198,1722019-1722020,1722246,1722339,1722416,1722674,1722678,1722683,1722711,1723064,1723194,1723563,1724216,1724251,1724257,1724292,1724375,1724397,1724432,1724582,1724603,1724780,1724843-1724844,1725295,1725569,1725760,1726176,1726244-1726246,1726249,1726358,1726436,1726449,1726456,1726646,1726653,1726755,1726778,1727532,1727555,1727608,1727951,1727954,1728089,1728167,1728302,1728497,1728501,1728524,1728639,1728651,1728772,1729215,1729297,1729347,1729356,1729406,1729408,1729412,1729515,1729638,1729656-1729
 657,1729783,1729828,1729832,1729841,1729851,1729886,1729904,1729973,1730019,1730025,1730052,1730072,1730088,1730494,1730499,1730547,1730559,1730567,1730578,1730585,1730651,1730697,1730712-1730713,1730805,1731029,1731110,1731210,1731225,1731444,1731551,1731612,1732184,1732452,1732461,1732525,1732812,1733467,1734452,1736478,1736751,1736838,1737804,1737835,1737853,1737984,1737992,1738119,1738135,1738231,1738271,1738607,1738610,1738731,1738914,1741702,1742257,1742284,1742544,1742900,1742926,1743383
 /qpid/trunk/qpid:796646-796653

Modified: qpid/java/branches/6.0.x/client/src/main/java/org/apache/qpid/client/AMQSession.java
URL: http://svn.apache.org/viewvc/qpid/java/branches/6.0.x/client/src/main/java/org/apache/qpid/client/AMQSession.java?rev=1743387&r1=1743386&r2=1743387&view=diff
==============================================================================
--- qpid/java/branches/6.0.x/client/src/main/java/org/apache/qpid/client/AMQSession.java (original)
+++ qpid/java/branches/6.0.x/client/src/main/java/org/apache/qpid/client/AMQSession.java Wed May 11 15:11:01 2016
@@ -28,6 +28,10 @@ import java.util.concurrent.ConcurrentHa
 import java.util.concurrent.ConcurrentLinkedQueue;
 import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.ThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicInteger;
@@ -154,10 +158,10 @@ public abstract class AMQSession<C exten
     private int _ticket;
 
     /** Holds the high mark for prefetched message, at which the session is suspended. */
-    private int _prefetchHighMark;
+    private final int _prefetchHighMark;
 
     /** Holds the low mark for prefetched messages, below which the session is resumed. */
-    private int _prefetchLowMark;
+    private final int _prefetchLowMark;
 
     /** Holds the message listener, if any, which is attached to this session. */
     private MessageListener _messageListener = null;
@@ -293,6 +297,8 @@ public abstract class AMQSession<C exten
         return _messageFactoryRegistry;
     }
 
+    private final ExecutorService _flowControlNoAckTaskPool;
+
     /**
      * Consumers associated with this session
      */
@@ -355,78 +361,85 @@ public abstract class AMQSession<C exten
         _messageEncryptionHelper = new MessageEncryptionHelper(this);
         _channelId = channelId;
         _messageFactoryRegistry = MessageFactoryRegistry.newDefaultRegistry(this);
-        _prefetchHighMark = defaultPrefetchHighMark;
-        _prefetchLowMark = defaultPrefetchLowMark;
 
         if (_acknowledgeMode == NO_ACKNOWLEDGE)
         {
-            _queue =
-                    new FlowControllingBlockingQueue<Dispatchable>(_prefetchHighMark, _prefetchLowMark,
-                                                     new FlowControllingBlockingQueue.ThresholdListener()
-                                                     {
-                                                         private final AtomicBoolean _suspendState = new AtomicBoolean();
-
-                                                         public void aboveThreshold(int currentValue)
-                                                         {
-                                                             // If the session has been closed don't waste time creating a thread to do
-                                                             // flow control
-                                                             if (!(AMQSession.this.isClosed() || AMQSession.this.isClosing()))
-                                                             {
-                                                                 // Only execute change if previous state
-                                                                 // was False
-                                                                 if (!_suspendState.getAndSet(true))
-                                                                 {
-                                                                     if (_logger.isDebugEnabled())
-                                                                     {
-                                                                         _logger.debug(
-                                                                                 "Above threshold(" + _prefetchHighMark
-                                                                                 + ") so suspending channel. Current value is " + currentValue);
-                                                                     }
-                                                                     try
-                                                                     {
-                                                                         Threading.getThreadFactory().createThread(new SuspenderRunner(_suspendState)).start();
-                                                                     }
-                                                                     catch (Exception e)
-                                                                     {
-                                                                         throw new RuntimeException("Failed to create thread", e);
-                                                                     }
-                                                                 }
-                                                             }
-                                                         }
-
-                                                         public void underThreshold(int currentValue)
-                                                         {
-                                                             // If the session has been closed don't waste time creating a thread to do
-                                                             // flow control
-                                                             if (!(AMQSession.this.isClosed() || AMQSession.this.isClosing()))
-                                                             {
-                                                                 // Only execute change if previous state
-                                                                 // was true
-                                                                 if (_suspendState.getAndSet(false))
-                                                                 {
-                                                                     if (_logger.isDebugEnabled())
-                                                                     {
-
-                                                                         _logger.debug(
-                                                                                 "Below threshold(" + _prefetchLowMark
-                                                                                 + ") so unsuspending channel. Current value is " + currentValue);
-                                                                     }
-                                                                     try
-                                                                     {
-                                                                         Threading.getThreadFactory().createThread(new SuspenderRunner(_suspendState)).start();
-                                                                     }
-                                                                     catch (Exception e)
-                                                                     {
-                                                                         throw new RuntimeException("Failed to create thread", e);
-                                                                     }
-                                                                 }
-                                                             }
-                                                         }
-                                                     });
+            _prefetchHighMark = defaultPrefetchHighMark;
+            _prefetchLowMark = defaultPrefetchLowMark == defaultPrefetchHighMark && defaultPrefetchHighMark > 0
+                   ? Math.max(defaultPrefetchHighMark / 2, 1)
+                    : defaultPrefetchLowMark;
+
+            // we coalesce suspend jobs using single threaded pool executor with queue length of one
+            // and discarding policy
+            _flowControlNoAckTaskPool = new ThreadPoolExecutor(1, 1,
+                                                               0L, TimeUnit.MILLISECONDS,
+                                                               new LinkedBlockingQueue<Runnable>(1),
+                                                               new ThreadFactory()
+            {
+                @Override
+                public Thread newThread(final Runnable r)
+                {
+                    Thread thread = new Thread(r, "Connection_" + _connection.getConnectionNumber() + "_session_" + _channelId);
+                    if (!thread.isDaemon())
+                    {
+                        thread.setDaemon(true);
+                    }
+
+                    return thread;
+                }
+            }, new ThreadPoolExecutor.DiscardPolicy());
+
+            final FlowControllingBlockingQueue.ThresholdListener listener =
+                    new FlowControllingBlockingQueue.ThresholdListener()
+                    {
+                        private final AtomicBoolean _suspendState = new AtomicBoolean();
+
+                        public void aboveThreshold(int currentValue)
+                        {
+                            if (!(AMQSession.this.isClosed() || AMQSession.this.isClosing()))
+                            {
+                                // Only execute change if previous state was false
+                                if (!_suspendState.getAndSet(true))
+                                {
+                                    _logger.debug(
+                                            "Above threshold ({}) so suspending channel. Current value is {}",
+                                            _prefetchHighMark,
+                                            currentValue);
+
+                                    doSuspend();
+                                }
+                            }
+                        }
+
+                        public void underThreshold(int currentValue)
+                        {
+                            if (!(AMQSession.this.isClosed() || AMQSession.this.isClosing()))
+                            {
+                                // Only execute change if previous state was true
+                                if (_suspendState.getAndSet(false))
+                                {
+                                    _logger.debug(
+                                            "Below threshold ({}) so unsuspending channel. Current value is {}",
+                                            _prefetchLowMark,
+                                            currentValue);
+                                    doSuspend();
+                                }
+                            }
+                        }
+
+                        private void doSuspend()
+                        {
+                            _flowControlNoAckTaskPool.execute(new SuspenderRunner(_suspendState));
+                        }
+                    };
+            _queue = new FlowControllingBlockingQueue<>(_prefetchHighMark, _prefetchLowMark, listener);
         }
         else
         {
-            _queue = new FlowControllingBlockingQueue<Dispatchable>(_prefetchHighMark, null);
+            _prefetchHighMark = defaultPrefetchHighMark;
+            _prefetchLowMark = defaultPrefetchLowMark;
+            _flowControlNoAckTaskPool = null;
+            _queue = new FlowControllingBlockingQueue<>(_prefetchHighMark, null);
         }
 
         // Add creation logging to tie in with the existing close logging
@@ -775,6 +788,7 @@ public abstract class AMQSession<C exten
             }
             finally
             {
+                shutdownFlowControlNoAckTaskPool();
                 _connection.deregisterSession(_channelId);
             }
         }
@@ -794,7 +808,7 @@ public abstract class AMQSession<C exten
         // with a null cause
         // When we are closing the Session due to a protocol session error we simply create a new AMQException
         // with the correct error code and text this is cleary WRONG as the instanceof check below will fail.
-        // We need to determin here if the connection should be
+        // We need to determine here if the connection should be
 
         if (e instanceof AMQDisconnectedException)
         {
@@ -822,6 +836,7 @@ public abstract class AMQSession<C exten
 
             _connection.deregisterSession(_channelId);
             closeProducersAndConsumers(amqe);
+            shutdownFlowControlNoAckTaskPool();
         }
 
     }
@@ -3179,7 +3194,7 @@ public abstract class AMQSession<C exten
      * @throws QpidException If the session cannot be suspended for any reason.
      * TODO  Be aware of possible changes to parameter order as versions change.
      */
-    protected void suspendChannel(boolean suspend) throws QpidException // , FailoverException
+    protected void suspendChannel(boolean suspend) throws QpidException
     {
         synchronized (_suspensionLock)
         {
@@ -3636,7 +3651,7 @@ public abstract class AMQSession<C exten
                 synchronized (_suspensionLock)
                 {
                     // If the session has closed by the time we get here
-                    // then we should not attempt to write to the sesion/channel.
+                    // then we should not attempt to write to the session/channel.
                     if (!(AMQSession.this.isClosed() || AMQSession.this.isClosing()))
                     {
                         suspendChannel(_suspend.get());
@@ -3785,5 +3800,14 @@ public abstract class AMQSession<C exten
     {
         _queue.clear();
     }
+
+    private void shutdownFlowControlNoAckTaskPool()
+    {
+        if (_flowControlNoAckTaskPool != null)
+        {
+            _flowControlNoAckTaskPool.shutdown();
+        }
+    }
+
 }
 

Modified: qpid/java/branches/6.0.x/client/src/main/java/org/apache/qpid/client/util/FlowControllingBlockingQueue.java
URL: http://svn.apache.org/viewvc/qpid/java/branches/6.0.x/client/src/main/java/org/apache/qpid/client/util/FlowControllingBlockingQueue.java?rev=1743387&r1=1743386&r2=1743387&view=diff
==============================================================================
--- qpid/java/branches/6.0.x/client/src/main/java/org/apache/qpid/client/util/FlowControllingBlockingQueue.java (original)
+++ qpid/java/branches/6.0.x/client/src/main/java/org/apache/qpid/client/util/FlowControllingBlockingQueue.java Wed May 11 15:11:01 2016
@@ -88,9 +88,21 @@ public class FlowControllingBlockingQueu
         _flowControlHighThreshold = highThreshold;
         _flowControlLowThreshold = lowThreshold;
         _listener = listener;
-        if (highThreshold == 0)
+        if (highThreshold <= 0)
         {
-        	disableFlowControl = true;
+            disableFlowControl = true;
+        }
+        else if (lowThreshold > highThreshold)
+        {
+            throw new IllegalArgumentException(String.format(
+                    "Invalid low threshold %d : it should be less or equal high threshold %d",
+                    lowThreshold,
+                    highThreshold));
+        }
+        else if (lowThreshold < 1)
+        {
+            throw new IllegalArgumentException(String.format("Invalid low threshold %d: it should be greater than 0",
+                                                             lowThreshold));
         }
     }
 



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