tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r1390718 [2/2] - in /tomcat/sandbox/trunk-resources: ./ java/org/apache/catalina/connector/ java/org/apache/catalina/core/ java/org/apache/catalina/valves/ java/org/apache/coyote/ java/org/apache/coyote/ajp/ java/org/apache/coyote/http11/ j...
Date Wed, 26 Sep 2012 20:35:26 GMT
Modified: tomcat/sandbox/trunk-resources/java/org/apache/tomcat/util/buf/B2CConverter.java
URL: http://svn.apache.org/viewvc/tomcat/sandbox/trunk-resources/java/org/apache/tomcat/util/buf/B2CConverter.java?rev=1390718&r1=1390717&r2=1390718&view=diff
==============================================================================
--- tomcat/sandbox/trunk-resources/java/org/apache/tomcat/util/buf/B2CConverter.java (original)
+++ tomcat/sandbox/trunk-resources/java/org/apache/tomcat/util/buf/B2CConverter.java Wed Sep
26 20:35:24 2012
@@ -81,12 +81,21 @@ public class B2CConverter {
         // Encoding names should all be ASCII
         String lowerCaseEnc = enc.toLowerCase(Locale.US);
 
+        return getCharsetLower(lowerCaseEnc);
+    }
+
+    /**
+     * Only to be used when it is known that the encoding name is in lower case.
+     */
+    public static Charset getCharsetLower(String lowerCaseEnc)
+            throws UnsupportedEncodingException{
+
         Charset charset = encodingToCharsetCache.get(lowerCaseEnc);
 
         if (charset == null) {
             // Pre-population of the cache means this must be invalid
             throw new UnsupportedEncodingException(
-                    sm.getString("b2cConverter.unknownEncoding", enc));
+                    sm.getString("b2cConverter.unknownEncoding", lowerCaseEnc));
         }
         return charset;
     }

Modified: tomcat/sandbox/trunk-resources/java/org/apache/tomcat/util/net/NioBlockingSelector.java
URL: http://svn.apache.org/viewvc/tomcat/sandbox/trunk-resources/java/org/apache/tomcat/util/net/NioBlockingSelector.java?rev=1390718&r1=1390717&r2=1390718&view=diff
==============================================================================
--- tomcat/sandbox/trunk-resources/java/org/apache/tomcat/util/net/NioBlockingSelector.java
(original)
+++ tomcat/sandbox/trunk-resources/java/org/apache/tomcat/util/net/NioBlockingSelector.java
Wed Sep 26 20:35:24 2012
@@ -26,7 +26,6 @@ import java.nio.channels.SelectionKey;
 import java.nio.channels.Selector;
 import java.nio.channels.SocketChannel;
 import java.util.Iterator;
-import java.util.concurrent.ConcurrentLinkedQueue;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicInteger;
@@ -34,6 +33,8 @@ import java.util.concurrent.atomic.Atomi
 import org.apache.juli.logging.Log;
 import org.apache.juli.logging.LogFactory;
 import org.apache.tomcat.util.ExceptionUtils;
+import org.apache.tomcat.util.collections.SynchronizedQueue;
+import org.apache.tomcat.util.collections.SynchronizedStack;
 import org.apache.tomcat.util.net.NioEndpoint.KeyAttachment;
 
 public class NioBlockingSelector {
@@ -42,6 +43,9 @@ public class NioBlockingSelector {
 
     private static int threadCounter = 0;
 
+    private final SynchronizedStack<KeyReference> keyReferenceStack =
+            new SynchronizedStack<>();
+
     protected Selector sharedSelector;
 
     protected BlockPoller poller;
@@ -82,7 +86,10 @@ public class NioBlockingSelector {
             throws IOException {
         SelectionKey key = socket.getIOChannel().keyFor(socket.getPoller().getSelector());
         if ( key == null ) throw new IOException("Key no longer registered");
-        KeyReference reference = new KeyReference();
+        KeyReference reference = keyReferenceStack.pop();
+        if (reference == null) {
+            reference = new KeyReference();
+        }
         KeyAttachment att = (KeyAttachment) key.attachment();
         int written = 0;
         boolean timedout = false;
@@ -131,6 +138,7 @@ public class NioBlockingSelector {
                 poller.cancelKey(reference.key);
             }
             reference.key = null;
+            keyReferenceStack.push(reference);
         }
         return written;
     }
@@ -150,7 +158,10 @@ public class NioBlockingSelector {
     public int read(ByteBuffer buf, NioChannel socket, long readTimeout) throws IOException
{
         SelectionKey key = socket.getIOChannel().keyFor(socket.getPoller().getSelector());
         if ( key == null ) throw new IOException("Key no longer registered");
-        KeyReference reference = new KeyReference();
+        KeyReference reference = keyReferenceStack.pop();
+        if (reference == null) {
+            reference = new KeyReference();
+        }
         KeyAttachment att = (KeyAttachment) key.attachment();
         int read = 0;
         boolean timedout = false;
@@ -195,6 +206,7 @@ public class NioBlockingSelector {
                 poller.cancelKey(reference.key);
             }
             reference.key = null;
+            keyReferenceStack.push(reference);
         }
         return read;
     }
@@ -203,10 +215,10 @@ public class NioBlockingSelector {
     protected static class BlockPoller extends Thread {
         protected volatile boolean run = true;
         protected Selector selector = null;
-        protected ConcurrentLinkedQueue<Runnable> events =
-                new ConcurrentLinkedQueue<>();
+        protected final SynchronizedQueue<Runnable> events =
+                new SynchronizedQueue<>();
         public void disable() { run = false; selector.wakeup();}
-        protected AtomicInteger wakeupCounter = new AtomicInteger(0);
+        protected final AtomicInteger wakeupCounter = new AtomicInteger(0);
         public void cancelKey(final SelectionKey key) {
             Runnable r = new Runnable() {
                 @Override
@@ -232,14 +244,15 @@ public class NioBlockingSelector {
         }
 
         public void add(final KeyAttachment key, final int ops, final KeyReference ref) {
+            if ( key == null ) return;
+            NioChannel nch = key.getChannel();
+            if ( nch == null ) return;
+            final SocketChannel ch = nch.getIOChannel();
+            if ( ch == null ) return;
+
             Runnable r = new Runnable() {
                 @Override
                 public void run() {
-                    if ( key == null ) return;
-                    NioChannel nch = key.getChannel();
-                    if ( nch == null ) return;
-                    SocketChannel ch = nch.getIOChannel();
-                    if ( ch == null ) return;
                     SelectionKey sk = ch.keyFor(selector);
                     try {
                         if (sk == null) {
@@ -262,14 +275,15 @@ public class NioBlockingSelector {
         }
 
         public void remove(final KeyAttachment key, final int ops) {
+            if ( key == null ) return;
+            NioChannel nch = key.getChannel();
+            if ( nch == null ) return;
+            final SocketChannel ch = nch.getIOChannel();
+            if ( ch == null ) return;
+
             Runnable r = new Runnable() {
                 @Override
                 public void run() {
-                    if ( key == null ) return;
-                    NioChannel nch = key.getChannel();
-                    if ( nch == null ) return;
-                    SocketChannel ch = nch.getIOChannel();
-                    if ( ch == null ) return;
                     SelectionKey sk = ch.keyFor(selector);
                     try {
                         if (sk == null) {
@@ -402,5 +416,4 @@ public class NioBlockingSelector {
             key = null;
         }
     }
-
 }

Modified: tomcat/sandbox/trunk-resources/java/org/apache/tomcat/util/net/NioEndpoint.java
URL: http://svn.apache.org/viewvc/tomcat/sandbox/trunk-resources/java/org/apache/tomcat/util/net/NioEndpoint.java?rev=1390718&r1=1390717&r2=1390718&view=diff
==============================================================================
--- tomcat/sandbox/trunk-resources/java/org/apache/tomcat/util/net/NioEndpoint.java (original)
+++ tomcat/sandbox/trunk-resources/java/org/apache/tomcat/util/net/NioEndpoint.java Wed Sep
26 20:35:24 2012
@@ -34,7 +34,6 @@ import java.nio.channels.SocketChannel;
 import java.nio.channels.WritableByteChannel;
 import java.util.Iterator;
 import java.util.Set;
-import java.util.concurrent.ConcurrentLinkedQueue;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.RejectedExecutionException;
 import java.util.concurrent.TimeUnit;
@@ -51,6 +50,8 @@ import org.apache.juli.logging.Log;
 import org.apache.juli.logging.LogFactory;
 import org.apache.tomcat.util.ExceptionUtils;
 import org.apache.tomcat.util.IntrospectionUtils;
+import org.apache.tomcat.util.collections.SynchronizedQueue;
+import org.apache.tomcat.util.collections.SynchronizedStack;
 import org.apache.tomcat.util.net.AbstractEndpoint.Handler.SocketState;
 import org.apache.tomcat.util.net.SecureNioChannel.ApplicationBufferHandler;
 import org.apache.tomcat.util.net.jsse.NioX509KeyManager;
@@ -126,157 +127,30 @@ public class NioEndpoint extends Abstrac
     /**
      * Cache for SocketProcessor objects
      */
-    protected ConcurrentLinkedQueue<SocketProcessor> processorCache = new ConcurrentLinkedQueue<SocketProcessor>()
{
-        private static final long serialVersionUID = 1L;
-        protected AtomicInteger size = new AtomicInteger(0);
-        @Override
-        public boolean offer(SocketProcessor sc) {
-            sc.reset(null,null);
-            boolean offer = socketProperties.getProcessorCache()==-1?true:size.get()<socketProperties.getProcessorCache();
-            //avoid over growing our cache or add after we have stopped
-            if ( running && (!paused) && (offer) ) {
-                boolean result = super.offer(sc);
-                if ( result ) {
-                    size.incrementAndGet();
-                }
-                return result;
-            }
-            else return false;
-        }
-
-        @Override
-        public SocketProcessor poll() {
-            SocketProcessor result = super.poll();
-            if ( result != null ) {
-                size.decrementAndGet();
-            }
-            return result;
-        }
-
-        @Override
-        public void clear() {
-            super.clear();
-            size.set(0);
-        }
-    };
-
+    protected final SynchronizedStack<SocketProcessor> processorCache =
+            new SynchronizedStack<>(SynchronizedStack.DEFAULT_SIZE,
+                    socketProperties.getProcessorCache());
 
     /**
      * Cache for key attachment objects
      */
-    protected ConcurrentLinkedQueue<KeyAttachment> keyCache = new ConcurrentLinkedQueue<KeyAttachment>()
{
-        private static final long serialVersionUID = 1L;
-        protected AtomicInteger size = new AtomicInteger(0);
-        @Override
-        public boolean offer(KeyAttachment ka) {
-            ka.reset();
-            boolean offer = socketProperties.getKeyCache()==-1?true:size.get()<socketProperties.getKeyCache();
-            //avoid over growing our cache or add after we have stopped
-            if ( running && (!paused) && (offer) ) {
-                boolean result = super.offer(ka);
-                if ( result ) {
-                    size.incrementAndGet();
-                }
-                return result;
-            }
-            else return false;
-        }
-
-        @Override
-        public KeyAttachment poll() {
-            KeyAttachment result = super.poll();
-            if ( result != null ) {
-                size.decrementAndGet();
-            }
-            return result;
-        }
-
-        @Override
-        public void clear() {
-            super.clear();
-            size.set(0);
-        }
-    };
-
+    protected final SynchronizedStack<KeyAttachment> keyCache =
+            new SynchronizedStack<>(SynchronizedStack.DEFAULT_SIZE,
+                    socketProperties.getKeyCache());
 
     /**
      * Cache for poller events
      */
-    protected ConcurrentLinkedQueue<PollerEvent> eventCache = new ConcurrentLinkedQueue<PollerEvent>()
{
-        private static final long serialVersionUID = 1L;
-        protected AtomicInteger size = new AtomicInteger(0);
-        @Override
-        public boolean offer(PollerEvent pe) {
-            pe.reset();
-            boolean offer = socketProperties.getEventCache()==-1?true:size.get()<socketProperties.getEventCache();
-            //avoid over growing our cache or add after we have stopped
-            if ( running && (!paused) && (offer) ) {
-                boolean result = super.offer(pe);
-                if ( result ) {
-                    size.incrementAndGet();
-                }
-                return result;
-            }
-            else return false;
-        }
-
-        @Override
-        public PollerEvent poll() {
-            PollerEvent result = super.poll();
-            if ( result != null ) {
-                size.decrementAndGet();
-            }
-            return result;
-        }
-
-        @Override
-        public void clear() {
-            super.clear();
-            size.set(0);
-        }
-    };
-
+    protected final SynchronizedStack<PollerEvent> eventCache =
+            new SynchronizedStack<>(SynchronizedStack.DEFAULT_SIZE,
+                    socketProperties.getEventCache());
 
     /**
      * Bytebuffer cache, each channel holds a set of buffers (two, except for SSL holds four)
      */
-    protected ConcurrentLinkedQueue<NioChannel> nioChannels = new ConcurrentLinkedQueue<NioChannel>()
{
-        private static final long serialVersionUID = 1L;
-        protected AtomicInteger size = new AtomicInteger(0);
-        protected AtomicInteger bytes = new AtomicInteger(0);
-        @Override
-        public boolean offer(NioChannel socket) {
-            boolean offer = socketProperties.getBufferPool()==-1?true:size.get()<socketProperties.getBufferPool();
-            offer = offer && (socketProperties.getBufferPoolSize()==-1?true:(bytes.get()+socket.getBufferSize())<socketProperties.getBufferPoolSize());
-            //avoid over growing our cache or add after we have stopped
-            if ( running && (!paused) && (offer) ) {
-                boolean result = super.offer(socket);
-                if ( result ) {
-                    size.incrementAndGet();
-                    bytes.addAndGet(socket.getBufferSize());
-                }
-                return result;
-            }
-            else return false;
-        }
-
-        @Override
-        public NioChannel poll() {
-            NioChannel result = super.poll();
-            if ( result != null ) {
-                size.decrementAndGet();
-                bytes.addAndGet(-result.getBufferSize());
-            }
-            return result;
-        }
-
-        @Override
-        public void clear() {
-            super.clear();
-            size.set(0);
-            bytes.set(0);
-        }
-    };
+    protected final SynchronizedStack<NioChannel> nioChannels =
+            new SynchronizedStack<>(SynchronizedStack.DEFAULT_SIZE,
+                    socketProperties.getBufferPoolSize());
 
 
     // ------------------------------------------------------------- Properties
@@ -663,7 +537,7 @@ public class NioEndpoint extends Abstrac
             Socket sock = socket.socket();
             socketProperties.setProperties(sock);
 
-            NioChannel channel = nioChannels.poll();
+            NioChannel channel = nioChannels.pop();
             if ( channel == null ) {
                 // SSL setup
                 if (sslContext != null) {
@@ -747,7 +621,7 @@ public class NioEndpoint extends Abstrac
                 return false;
             }
             attachment.setCometNotify(false); //will get reset upon next reg
-            SocketProcessor sc = processorCache.poll();
+            SocketProcessor sc = processorCache.pop();
             if ( sc == null ) sc = new SocketProcessor(socket,status);
             else sc.reset(socket,status);
             if ( dispatch && getExecutor()!=null ) getExecutor().execute(sc);
@@ -963,8 +837,8 @@ public class NioEndpoint extends Abstrac
     public class Poller implements Runnable {
 
         protected Selector selector;
-        protected ConcurrentLinkedQueue<Runnable> events =
-                new ConcurrentLinkedQueue<>();
+        protected final SynchronizedQueue<Runnable> events =
+                new SynchronizedQueue<>();
 
         protected volatile boolean close = false;
         protected long nextExpiration = 0;//optimize expiration handling
@@ -1020,7 +894,7 @@ public class NioEndpoint extends Abstrac
         }
 
         public void add(final NioChannel socket, final int interestOps) {
-            PollerEvent r = eventCache.poll();
+            PollerEvent r = eventCache.pop();
             if ( r==null) r = new PollerEvent(socket,null,interestOps);
             else r.reset(socket,null,interestOps);
             if ( (interestOps&OP_CALLBACK) == OP_CALLBACK ) {
@@ -1048,7 +922,9 @@ public class NioEndpoint extends Abstrac
                     r.run();
                     if ( r instanceof PollerEvent ) {
                         ((PollerEvent)r).reset();
-                        eventCache.offer((PollerEvent)r);
+                        if (running && !paused) {
+                            eventCache.push((PollerEvent)r);
+                        }
                     }
                 } catch ( Throwable x ) {
                     log.error("",x);
@@ -1061,11 +937,11 @@ public class NioEndpoint extends Abstrac
         public void register(final NioChannel socket)
         {
             socket.setPoller(this);
-            KeyAttachment key = keyCache.poll();
+            KeyAttachment key = keyCache.pop();
             final KeyAttachment ka = key!=null?key:new KeyAttachment(socket);
             ka.reset(this,socket,getSocketProperties().getSoTimeout());
             ka.setKeepAliveLeft(NioEndpoint.this.getMaxKeepAliveRequests());
-            PollerEvent r = eventCache.poll();
+            PollerEvent r = eventCache.pop();
             ka.interestOps(SelectionKey.OP_READ);//this is what OP_REGISTER turns into.
             if ( r==null) r = new PollerEvent(socket,ka,OP_REGISTER);
             else r.reset(socket,ka,OP_REGISTER);
@@ -1699,9 +1575,13 @@ public class NioEndpoint extends Abstrac
                             try {
                                 if (ka!=null) ka.setComet(false);
                                 socket.getPoller().cancelledKey(key, SocketStatus.ERROR);
-                                nioChannels.offer(socket);
+                                if (running && !paused) {
+                                    nioChannels.push(socket);
+                                }
                                 socket = null;
-                                if ( ka!=null ) keyCache.offer(ka);
+                                if (running && !paused && ka != null) {
+                                    keyCache.push(ka);
+                                }
                                 ka = null;
                             }catch ( Exception x ) {
                                 log.error("",x);
@@ -1716,9 +1596,13 @@ public class NioEndpoint extends Abstrac
                             ka = (KeyAttachment) key.attachment();
                             socket.getPoller().cancelledKey(key, SocketStatus.DISCONNECT);
                         }
-                        nioChannels.offer(socket);
+                        if (running && !paused) {
+                            nioChannels.push(socket);
+                        }
                         socket = null;
-                        if ( ka!=null ) keyCache.offer(ka);
+                        if (running && !paused && ka != null) {
+                            keyCache.push(ka);
+                        }
                         ka = null;
                     } else {
                         final SelectionKey fk = key;
@@ -1759,7 +1643,9 @@ public class NioEndpoint extends Abstrac
                     socket = null;
                     status = null;
                     //return to cache
-                    processorCache.offer(this);
+                    if (running && !paused) {
+                        processorCache.push(this);
+                    }
                 }
             }
         }

Modified: tomcat/sandbox/trunk-resources/java/org/apache/tomcat/util/net/SocketWrapper.java
URL: http://svn.apache.org/viewvc/tomcat/sandbox/trunk-resources/java/org/apache/tomcat/util/net/SocketWrapper.java?rev=1390718&r1=1390717&r2=1390718&view=diff
==============================================================================
--- tomcat/sandbox/trunk-resources/java/org/apache/tomcat/util/net/SocketWrapper.java (original)
+++ tomcat/sandbox/trunk-resources/java/org/apache/tomcat/util/net/SocketWrapper.java Wed
Sep 26 20:35:24 2012
@@ -27,6 +27,16 @@ public class SocketWrapper<E> {
     protected volatile int keepAliveLeft = 100;
     protected boolean async = false;
     protected boolean keptAlive = false;
+    /*
+     * Following cached for speed / reduced GC
+     */
+    private int localPort = -1;
+    private String localName = null;
+    private String localAddr = null;
+    private int remotePort = -1;
+    private String remoteHost = null;
+    private String remoteAddr = null;
+
 
     public SocketWrapper(E socket) {
         this.socket = socket;
@@ -49,4 +59,16 @@ public class SocketWrapper<E> {
     public int decrementKeepAlive() { return (--keepAliveLeft);}
     public boolean isKeptAlive() {return keptAlive;}
     public void setKeptAlive(boolean keptAlive) {this.keptAlive = keptAlive;}
+    public int getLocalPort() { return localPort; }
+    public void setLocalPort(int localPort) {this.localPort = localPort; }
+    public String getLocalName() { return localName; }
+    public void setLocalName(String localName) {this.localName = localName; }
+    public String getLocalAddr() { return localAddr; }
+    public void setLocalAddr(String localAddr) {this.localAddr = localAddr; }
+    public int getRemotePort() { return remotePort; }
+    public void setRemotePort(int remotePort) {this.remotePort = remotePort; }
+    public String getRemoteHost() { return remoteHost; }
+    public void setRemoteHost(String remoteHost) {this.remoteHost = remoteHost; }
+    public String getRemoteAddr() { return remoteAddr; }
+    public void setRemoteAddr(String remoteAddr) {this.remoteAddr = remoteAddr; }
 }

Modified: tomcat/sandbox/trunk-resources/webapps/docs/config/valve.xml
URL: http://svn.apache.org/viewvc/tomcat/sandbox/trunk-resources/webapps/docs/config/valve.xml?rev=1390718&r1=1390717&r2=1390718&view=diff
==============================================================================
--- tomcat/sandbox/trunk-resources/webapps/docs/config/valve.xml (original)
+++ tomcat/sandbox/trunk-resources/webapps/docs/config/valve.xml Wed Sep 26 20:35:24 2012
@@ -128,6 +128,13 @@
         </p>
       </attribute>
 
+      <attribute name="maxLogMessageBufferSize" required="false">
+        <p>Log message buffers are usually recycled and re-used. To prevent
+           excessive memory usage, if a buffer grows beyond this size it will be
+           discarded. The default is <code>256</code> characters. This should
be
+           set to larger than the typical access log message size.</p>
+      </attribute>
+
       <attribute name="pattern" required="false">
         <p>A formatting layout identifying the various information fields
         from the request and response to be logged, or the word
@@ -342,6 +349,26 @@
         </p>
       </attribute>
 
+      <attribute name="locale" required="false">
+        <p>The locale used to format timestamps in the access log
+           lines. Any timestamps configured using an
+           explicit SimpleDateFormat pattern (<code>%{xxx}t</code>)
+           are formatted in this locale. By default the
+           default locale of the Java process is used. Switching the
+           locale after the AccessLogValve is initialized is not supported.
+           Any timestamps using the common log format
+           (<code>CLF</code>) are always formatted in the locale
+           <code>en_US</code>.
+        </p>
+      </attribute>
+
+      <attribute name="maxLogMessageBufferSize" required="false">
+        <p>Log message buffers are usually recycled and re-used. To prevent
+           excessive memory usage, if a buffer grows beyond this size it will be
+           discarded. The default is <code>256</code> characters. This should
be
+           set to larger than the typical access log message size.</p>
+      </attribute>
+
       <attribute name="pattern" required="false">
         <p>A formatting layout identifying the various information fields
         from the request and response to be logged.



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


Mime
View raw message