Author: markt
Date: Thu Nov 17 16:40:02 2011
New Revision: 1203253

URL: http://svn.apache.org/viewvc?rev=1203253&view=rev
Log:
Refactor acceptor pause and shutdown to remove need for 'random' waits in
the shutdown code.
Removes the need for the fastShutdown attribute

Modified:
    tomcat/trunk/java/org/apache/tomcat/util/net/AbstractEndpoint.java
    tomcat/trunk/java/org/apache/tomcat/util/net/AprEndpoint.java
    tomcat/trunk/java/org/apache/tomcat/util/net/JIoEndpoint.java
    tomcat/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java
    tomcat/trunk/test/org/apache/catalina/startup/TomcatBaseTest.java
    tomcat/trunk/webapps/docs/config/ajp.xml
    tomcat/trunk/webapps/docs/config/http.xml

Modified: tomcat/trunk/java/org/apache/tomcat/util/net/AbstractEndpoint.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/net/AbstractEndpoint.java?rev=1203253&r1=1203252&r2=1203253&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/net/AbstractEndpoint.java 
(original)
+++ tomcat/trunk/java/org/apache/tomcat/util/net/AbstractEndpoint.java Thu Nov 
17 16:40:02 2011
@@ -30,6 +30,7 @@ import javax.net.ssl.KeyManagerFactory;
 
 import org.apache.juli.logging.Log;
 import org.apache.tomcat.util.IntrospectionUtils;
+import org.apache.tomcat.util.net.AbstractEndpoint.Acceptor.AcceptorState;
 import org.apache.tomcat.util.res.StringManager;
 import org.apache.tomcat.util.threads.LimitLatch;
 import org.apache.tomcat.util.threads.ResizableExecutor;
@@ -74,6 +75,25 @@ public abstract class AbstractEndpoint {
         UNBOUND, BOUND_ON_INIT, BOUND_ON_START
     }
 
+    public abstract static class Acceptor implements Runnable {
+        public enum AcceptorState {
+            NEW, RUNNING, PAUSED, ENDED
+        }
+
+        protected volatile AcceptorState state = AcceptorState.NEW;
+        public final AcceptorState getState() {
+            return state;
+        }
+
+        private String threadName;
+        protected final void setThreadName(final String threadName) {
+            this.threadName = threadName;
+        }
+        protected final String getThreadName() {
+            return threadName;
+        }
+    }
+
     private static final int INITIAL_ERROR_DELAY = 50;
     private static final int MAX_ERROR_DELAY = 1600;
 
@@ -109,25 +129,33 @@ public abstract class AbstractEndpoint {
         return socketProperties;
     }
 
+    /**
+     * Threads used to accept new connections and pass them to worker threads.
+     */
+    protected Acceptor[] acceptors;
+
 
     // ----------------------------------------------------------------- 
Properties
 
     /**
-     * A flag that can be used to speed up Tomcat shutdown by testing
-     * environments where we control external connections to Tomcat. Set it to
-     * {@code true} if it is known that there are no active or pending
-     * connections and all requests have already been processed. The default
-     * value is {@code false}.
+     * Acceptor thread count.
      */
-    private boolean fastShutdown = false;
-
-    public void setFastShutdown(boolean fastShutdown) {
-        this.fastShutdown = fastShutdown;
+    protected int acceptorThreadCount = 0;
+    public void setAcceptorThreadCount(int acceptorThreadCount) {
+        this.acceptorThreadCount = acceptorThreadCount;
     }
+    public int getAcceptorThreadCount() { return acceptorThreadCount; }
+
 
-    public boolean isFastShutdown() {
-        return fastShutdown;
+    /**
+     * Priority of the acceptor threads.
+     */
+    protected int acceptorThreadPriority = Thread.NORM_PRIORITY;
+    public void setAcceptorThreadPriority(int acceptorThreadPriority) {
+        this.acceptorThreadPriority = acceptorThreadPriority;
     }
+    public int getAcceptorThreadPriority() { return acceptorThreadPriority; }
+
 
     private int maxConnections = 10000;
     public void setMaxConnections(int maxCon) {
@@ -480,6 +508,16 @@ public abstract class AbstractEndpoint {
             if (getLog().isDebugEnabled()) {
                 getLog().debug("Socket unlock completed for:"+saddr);
             }
+
+            // Wait for upto 1000ms acceptor threads to unlock
+            long waitLeft = 1000;
+            for (Acceptor acceptor : acceptors) {
+                while (waitLeft > 0 &&
+                        acceptor.getState() == AcceptorState.RUNNING) {
+                    Thread.sleep(50);
+                    waitLeft -= 50;
+                }
+            }
         } catch(Exception e) {
             if (getLog().isDebugEnabled()) {
                 getLog().debug(sm.getString("endpoint.debug.unlock", "" + 
getPort()), e);
@@ -525,24 +563,33 @@ public abstract class AbstractEndpoint {
         startInternal();
     }
 
+    protected final void startAcceptorThreads() {
+        int count = getAcceptorThreadCount();
+        acceptors = new Acceptor[count];
+
+        for (int i = 0; i < count; i++) {
+            acceptors[i] = createAcceptor();
+            Thread t = new Thread(acceptors[i], getName() + "-Acceptor-" + i);
+            t.setPriority(getAcceptorThreadPriority());
+            t.setDaemon(getDaemon());
+            t.start();
+        }
+    }
+
+
+    /**
+     * Hook to allow Endpoints to provide a specific Acceptor implementation.
+     */
+    protected abstract Acceptor createAcceptor();
+
+
     /**
      * Pause the endpoint, which will stop it accepting new connections.
      */
     public void pause() {
         if (running && !paused) {
             paused = true;
-            if (isFastShutdown()) {
-                // unlockAccept will also be called by stopInternal(),
-                // so when shutting down it can be skipped here.
-                return;
-            }
             unlockAccept();
-            // Heuristic: Sleep for a while to ensure pause of the endpoint
-            try {
-                Thread.sleep(1000);
-            } catch (InterruptedException e) {
-                // Ignore
-            }
         }
     }
 

Modified: tomcat/trunk/java/org/apache/tomcat/util/net/AprEndpoint.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/net/AprEndpoint.java?rev=1203253&r1=1203252&r2=1203253&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/net/AprEndpoint.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/util/net/AprEndpoint.java Thu Nov 17 
16:40:02 2011
@@ -40,6 +40,7 @@ import org.apache.tomcat.jni.SSLSocket;
 import org.apache.tomcat.jni.Socket;
 import org.apache.tomcat.jni.Status;
 import org.apache.tomcat.util.ExceptionUtils;
+import org.apache.tomcat.util.net.AbstractEndpoint.Acceptor.AcceptorState;
 import org.apache.tomcat.util.net.AbstractEndpoint.Handler.SocketState;
 
 
@@ -91,9 +92,6 @@ public class AprEndpoint extends Abstrac
     protected long sslContext = 0;
 
 
-    private Acceptor acceptors[] = null;
-
-
     protected ConcurrentLinkedQueue<SocketWrapper<Long>> waitingRequests =
         new ConcurrentLinkedQueue<SocketWrapper<Long>>();
 
@@ -165,14 +163,6 @@ public class AprEndpoint extends Abstrac
 
 
     /**
-     * Acceptor thread count.
-     */
-    protected int acceptorThreadCount = 0;
-    public void setAcceptorThreadCount(int acceptorThreadCount) { 
this.acceptorThreadCount = acceptorThreadCount; }
-    public int getAcceptorThreadCount() { return acceptorThreadCount; }
-
-
-    /**
      * Sendfile thread count.
      */
     protected int sendfileThreadCount = 0;
@@ -619,15 +609,7 @@ public class AprEndpoint extends Abstrac
                 }
             }
 
-            // Start acceptor threads
-            acceptors = new Acceptor[acceptorThreadCount];
-            for (int i = 0; i < acceptorThreadCount; i++) {
-                acceptors[i] = new Acceptor();
-                acceptors[i].setName(getName() + "-Acceptor-" + i);
-                acceptors[i].setPriority(threadPriority);
-                acceptors[i].setDaemon(getDaemon());
-                acceptors[i].start();
-            }
+            startAcceptorThreads();
 
             // Start async timeout thread
             Thread timeoutThread = new Thread(new AsyncTimeout(),
@@ -651,25 +633,28 @@ public class AprEndpoint extends Abstrac
         if (running) {
             running = false;
             unlockAccept();
-            for (int i = 0; i < acceptors.length; i++) {
-                long s = System.currentTimeMillis() + 10000;
-                while (acceptors[i].isAlive() && serverSock != 0) {
+            for (AbstractEndpoint.Acceptor acceptor : acceptors) {
+                long waitLeft = 10000;
+                while (waitLeft > 0 &&
+                        acceptor.getState() != AcceptorState.ENDED &&
+                        serverSock != 0) {
                     try {
-                        acceptors[i].interrupt();
-                        acceptors[i].join(1000);
+                        Thread.sleep(50);
                     } catch (InterruptedException e) {
-                        // Ignore
-                    }
-                    if (System.currentTimeMillis() >= s) {
-                        
log.warn(sm.getString("endpoint.warn.unlockAcceptorFailed",
-                                 acceptors[i].getName()));
-                        // If the Acceptor is still running force
-                        // the hard socket close.
-                        if (serverSock != 0) {
-                            Socket.shutdown(serverSock, 
Socket.APR_SHUTDOWN_READ);
-                            serverSock = 0;
-                        }
+                        // Ignore and clean the interrupt flag
+                        Thread.interrupted();
                     }
+                    waitLeft -= 50;
+                }
+                if (waitLeft == 0) {
+                    log.warn(sm.getString("endpoint.warn.unlockAcceptorFailed",
+                            acceptor.getThreadName()));
+                   // If the Acceptor is still running force
+                   // the hard socket close.
+                   if (serverSock != 0) {
+                       Socket.shutdown(serverSock, Socket.APR_SHUTDOWN_READ);
+                       serverSock = 0;
+                   }
                 }
             }
             for (int i = 0; i < pollers.length; i++) {
@@ -738,6 +723,11 @@ public class AprEndpoint extends Abstrac
 
     // ------------------------------------------------------ Protected Methods
 
+    @Override
+    protected AbstractEndpoint.Acceptor createAcceptor() {
+        return new Acceptor();
+    }
+
 
     /**
      * Process the specified connection.
@@ -957,10 +947,11 @@ public class AprEndpoint extends Abstrac
     /**
      * Server socket acceptor thread.
      */
-    protected class Acceptor extends Thread {
+    protected class Acceptor extends AbstractEndpoint.Acceptor {
 
         private final Log log = LogFactory.getLog(AprEndpoint.Acceptor.class);
 
+
         /**
          * The background thread that listens for incoming TCP/IP connections 
and
          * hands them off to an appropriate processor.
@@ -975,6 +966,7 @@ public class AprEndpoint extends Abstrac
 
                 // Loop if endpoint is paused
                 while (paused && running) {
+                    state = AcceptorState.PAUSED;
                     try {
                         Thread.sleep(1000);
                     } catch (InterruptedException e) {
@@ -985,6 +977,8 @@ public class AprEndpoint extends Abstrac
                 if (!running) {
                     break;
                 }
+                state = AcceptorState.RUNNING;
+
                 try {
                     //if we have reached max connections, wait
                     countUpOrAwaitConnection();
@@ -1036,13 +1030,10 @@ public class AprEndpoint extends Abstrac
                         }
                     }
                 }
-
                 // The processor will recycle itself when it finishes
-
             }
-
+            state = AcceptorState.ENDED;
         }
-
     }
 
 

Modified: tomcat/trunk/java/org/apache/tomcat/util/net/JIoEndpoint.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/net/JIoEndpoint.java?rev=1203253&r1=1203252&r2=1203253&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/net/JIoEndpoint.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/util/net/JIoEndpoint.java Thu Nov 17 
16:40:02 2011
@@ -76,13 +76,6 @@ public class JIoEndpoint extends Abstrac
     // ------------------------------------------------------------- Properties
 
     /**
-     * Acceptor thread count.
-     */
-    protected int acceptorThreadCount = 0;
-    public void setAcceptorThreadCount(int acceptorThreadCount) { 
this.acceptorThreadCount = acceptorThreadCount; }
-    public int getAcceptorThreadCount() { return acceptorThreadCount; }
-
-    /**
      * Handling of accepted sockets.
      */
     protected Handler handler = null;
@@ -174,8 +167,7 @@ public class JIoEndpoint extends Abstrac
     /**
      * Server socket acceptor thread.
      */
-    protected class Acceptor implements Runnable {
-
+    protected class Acceptor extends AbstractEndpoint.Acceptor {
 
         /**
          * The background thread that listens for incoming TCP/IP connections 
and
@@ -191,6 +183,7 @@ public class JIoEndpoint extends Abstrac
 
                 // Loop if endpoint is paused
                 while (paused && running) {
+                    state = AcceptorState.PAUSED;
                     try {
                         Thread.sleep(1000);
                     } catch (InterruptedException e) {
@@ -201,6 +194,8 @@ public class JIoEndpoint extends Abstrac
                 if (!running) {
                     break;
                 }
+                state = AcceptorState.RUNNING;
+
                 try {
                     //if we have reached max connections, wait
                     countUpOrAwaitConnection();
@@ -252,6 +247,7 @@ public class JIoEndpoint extends Abstrac
                 }
                 // The processor will recycle itself when it finishes
             }
+            state = AcceptorState.ENDED;
         }
     }
 
@@ -405,14 +401,7 @@ public class JIoEndpoint extends Abstrac
 
             initializeConnectionLatch();
 
-            // Start acceptor threads
-            for (int i = 0; i < acceptorThreadCount; i++) {
-                Thread acceptorThread = new Thread(new Acceptor(),
-                        getName() + "-Acceptor-" + i);
-                acceptorThread.setPriority(threadPriority);
-                acceptorThread.setDaemon(getDaemon());
-                acceptorThread.start();
-            }
+            startAcceptorThreads();
 
             // Start async timeout thread
             Thread timeoutThread = new Thread(new AsyncTimeout(),
@@ -457,6 +446,12 @@ public class JIoEndpoint extends Abstrac
     }
 
 
+    @Override
+    protected AbstractEndpoint.Acceptor createAcceptor() {
+        return new Acceptor();
+    }
+
+
     /**
      * Configure the socket.
      */

Modified: tomcat/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java?rev=1203253&r1=1203252&r2=1203253&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java Thu Nov 17 
16:40:02 2011
@@ -301,13 +301,6 @@ public class NioEndpoint extends Abstrac
 
 
     /**
-     * Priority of the acceptor threads.
-     */
-    protected int acceptorThreadPriority = Thread.NORM_PRIORITY;
-    public void setAcceptorThreadPriority(int acceptorThreadPriority) { 
this.acceptorThreadPriority = acceptorThreadPriority; }
-    public int getAcceptorThreadPriority() { return acceptorThreadPriority; }
-
-    /**
      * Priority of the poller threads.
      */
     protected int pollerThreadPriority = Thread.NORM_PRIORITY;
@@ -337,14 +330,6 @@ public class NioEndpoint extends Abstrac
 
 
     /**
-     * Acceptor thread count.
-     */
-    protected int acceptorThreadCount = 1;
-    public void setAcceptorThreadCount(int acceptorThreadCount) { 
this.acceptorThreadCount = acceptorThreadCount; }
-    public int getAcceptorThreadCount() { return acceptorThreadCount; }
-
-
-    /**
      * Poller thread count.
      */
     protected int pollerThreadCount = 
Runtime.getRuntime().availableProcessors();
@@ -536,13 +521,7 @@ public class NioEndpoint extends Abstrac
                 pollerThread.start();
             }
 
-            // Start acceptor threads
-            for (int i = 0; i < acceptorThreadCount; i++) {
-                Thread acceptorThread = new Thread(new Acceptor(), getName() + 
"-Acceptor-" + i);
-                acceptorThread.setPriority(threadPriority);
-                acceptorThread.setDaemon(getDaemon());
-                acceptorThread.start();
-            }
+            startAcceptorThreads();
         }
     }
 
@@ -631,6 +610,12 @@ public class NioEndpoint extends Abstrac
     }
 
 
+    @Override
+    protected AbstractEndpoint.Acceptor createAcceptor() {
+        return new Acceptor();
+    }
+
+
     /**
      * Process the specified connection.
      */
@@ -742,7 +727,7 @@ public class NioEndpoint extends Abstrac
     /**
      * Server socket acceptor thread.
      */
-    protected class Acceptor implements Runnable {
+    protected class Acceptor extends AbstractEndpoint.Acceptor {
         /**
          * The background thread that listens for incoming TCP/IP connections 
and
          * hands them off to an appropriate processor.
@@ -757,6 +742,7 @@ public class NioEndpoint extends Abstrac
 
                 // Loop if endpoint is paused
                 while (paused && running) {
+                    state = AcceptorState.PAUSED;
                     try {
                         Thread.sleep(1000);
                     } catch (InterruptedException e) {
@@ -767,6 +753,8 @@ public class NioEndpoint extends Abstrac
                 if (!running) {
                     break;
                 }
+                state = AcceptorState.RUNNING;
+
                 try {
                     //if we have reached max connections, wait
                     countUpOrAwaitConnection();
@@ -829,6 +817,7 @@ public class NioEndpoint extends Abstrac
                     log.error(sm.getString("endpoint.accept.fail"), t);
                 }
             }//while
+            state = AcceptorState.ENDED;
         }//run
     }
 

Modified: tomcat/trunk/test/org/apache/catalina/startup/TomcatBaseTest.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/startup/TomcatBaseTest.java?rev=1203253&r1=1203252&r2=1203253&view=diff
==============================================================================
--- tomcat/trunk/test/org/apache/catalina/startup/TomcatBaseTest.java (original)
+++ tomcat/trunk/test/org/apache/catalina/startup/TomcatBaseTest.java Thu Nov 
17 16:40:02 2011
@@ -158,10 +158,6 @@ public abstract class TomcatBaseTest ext
     @Override
     public void tearDown() throws Exception {
         try {
-            // Speed up Tomcat shutdown
-            if (tomcat.connector != null) {
-                tomcat.connector.setProperty("fastShutdown", "true");
-            }
             // Some tests may call tomcat.destroy(), some tests may just call
             // tomcat.stop(), some not call either method. Make sure that 
stop()
             // & destroy() are called as necessary.

Modified: tomcat/trunk/webapps/docs/config/ajp.xml
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/config/ajp.xml?rev=1203253&r1=1203252&r2=1203253&view=diff
==============================================================================
--- tomcat/trunk/webapps/docs/config/ajp.xml (original)
+++ tomcat/trunk/webapps/docs/config/ajp.xml Thu Nov 17 16:40:02 2011
@@ -273,6 +273,13 @@
       <code>1</code>.</p>
     </attribute>
 
+    <attribute name="acceptorThreadPriority" required="false">
+      <p>The priority of the acceptor threads. The threads used to accept
+      new connections. The default value is
+      <code>java.lang.Thread#NORM_PRIORITY</code>. See the JavaDoc for the
+      java.lang.Thread class for more details on what this priority means.</p>
+    </attribute>
+
     <attribute name="address" required="false">
       <p>For servers with more than one IP address, this attribute
       specifies which address will be used for listening on the specified
@@ -321,17 +328,6 @@
       provide the thread pool.</p>
     </attribute>
 
-    <attribute name="fastShutdown" required="false">
-      <p>This flag can be used to speed up Tomcat shutdown in testing
-      environments where Tomcat is started and stopped many times in a row,
-      but where all connections to Tomcat are known and controlled. It can be
-      used if it is known that there are no active connections and all requests
-      have already been processed.
-      If it is set to <code>true</code>, it turns off small delay when
-      pausing Connector Endpoint. The default value is
-      <code>false</code>.</p>
-    </attribute>
-
     <attribute name="keepAliveTimeout" required="false">
       <p>The number of milliseconds this <strong>Connector</strong> will wait 
for
        another AJP request before closing the connection.

Modified: tomcat/trunk/webapps/docs/config/http.xml
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/config/http.xml?rev=1203253&r1=1203252&r2=1203253&view=diff
==============================================================================
--- tomcat/trunk/webapps/docs/config/http.xml (original)
+++ tomcat/trunk/webapps/docs/config/http.xml Thu Nov 17 16:40:02 2011
@@ -267,6 +267,13 @@
       <code>1</code>.</p>
     </attribute>
 
+    <attribute name="acceptorThreadPriority" required="false">
+      <p>The priority of the acceptor threads. The threads used to accept
+      new connections. The default value is
+      <code>java.lang.Thread#NORM_PRIORITY</code>. See the JavaDoc for the
+      java.lang.Thread class for more details on what this priority means.</p>
+    </attribute>
+
     <attribute name="address" required="false">
       <p>For servers with more than one IP address, this attribute
       specifies which address will be used for listening on the specified
@@ -355,17 +362,6 @@
       provide the thread pool.</p>
     </attribute>
 
-    <attribute name="fastShutdown" required="false">
-      <p>This flag can be used to speed up Tomcat shutdown in testing
-      environments where Tomcat is started and stopped many times in a row,
-      but where all connections to Tomcat are known and controlled. It can be
-      used if it is known that there are no active connections and all requests
-      have already been processed.
-      If it is set to <code>true</code>, it turns off small delay when
-      pausing Connector Endpoint. The default value is
-      <code>false</code>.</p>
-    </attribute>
-
     <attribute name="keepAliveTimeout" required="false">
       <p>The number of milliseconds this <strong>Connector</strong> will wait
       for another HTTP request before closing the connection. The default value
@@ -605,13 +601,6 @@
 
     <attributes>
 
-      <attribute name="acceptorThreadPriority" required="false">
-        <p>(int)The priority of the acceptor threads. The threads used to 
accept
-        new connections. The default value is
-        <code>java.lang.Thread#NORM_PRIORITY</code>. See the JavaDoc for the
-        java.lang.Thread class for more details on what this priority 
means.</p>
-      </attribute>
-
       <attribute name="pollerThreadCount" required="false">
         <p>(int)The number of threads to be used to run for the polling events.
         Default value is <code>1</code> per processor. Can't see a reason to go



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to