From 0d49b6121e36d05f1625c32c88921cd4f7ab265f Mon Sep 17 00:00:00 2001 From: markt Date: Sat, 27 Nov 2010 10:41:26 +0000 Subject: [PATCH] Using SecureRandom makes digesting the ID unnecessary. Dropping the digest gives ~20% performance gain on ID generation. git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1039648 13f79535-47bb-0310-9956-ffa450edef68 --- java/org/apache/catalina/session/ManagerBase.java | 94 ---------------------- .../apache/catalina/session/mbeans-descriptors.xml | 10 --- test/org/apache/catalina/session/Benchmarks.java | 12 +-- webapps/docs/config/manager.xml | 7 -- 4 files changed, 4 insertions(+), 119 deletions(-) diff --git a/java/org/apache/catalina/session/ManagerBase.java b/java/org/apache/catalina/session/ManagerBase.java index b523c43a0..779ffe6a2 100644 --- a/java/org/apache/catalina/session/ManagerBase.java +++ b/java/org/apache/catalina/session/ManagerBase.java @@ -30,8 +30,6 @@ import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Method; import java.security.AccessController; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; import java.security.PrivilegedAction; import java.security.SecureRandom; import java.util.ArrayList; @@ -88,38 +86,12 @@ public abstract class ManagerBase extends LifecycleMBeanBase protected volatile boolean randomFileCurrentIsValid = true; /** - * The default message digest algorithm to use if we cannot use - * the requested one. - */ - protected static final String DEFAULT_ALGORITHM = "MD5"; - - - /** - * The message digest algorithm to be used when generating session - * identifiers. This must be an algorithm supported by the - * java.security.MessageDigest class on your platform. - */ - protected String algorithm = DEFAULT_ALGORITHM; - - - /** * The Container with which this Manager is associated. */ protected Container container; /** - * Queue of MessageDigest objects to be used when creating session - * identifiers. If the queue is empty when a MessageDigest is required, a - * new MessageDigest object is created. This is designed this way since - * MessageDigest objects are not thread-safe and sync'ing on a single object - * is slow(er). - */ - protected Queue digests = - new ConcurrentLinkedQueue(); - - - /** * The distributable flag for Sessions created by this Manager. If this * flag is set to true, any user attributes added to a * session controlled by this Manager must be Serializable. @@ -293,30 +265,6 @@ public abstract class ManagerBase extends LifecycleMBeanBase // ------------------------------------------------------------- Properties /** - * Return the message digest algorithm for this Manager. - */ - public String getAlgorithm() { - - return (this.algorithm); - - } - - - /** - * Set the message digest algorithm for this Manager. - * - * @param algorithm The new message digest algorithm - */ - public void setAlgorithm(String algorithm) { - - String oldAlgorithm = this.algorithm; - this.algorithm = algorithm; - support.firePropertyChange("algorithm", oldAlgorithm, this.algorithm); - - } - - - /** * Return the Container with which this Manager is associated. */ @Override @@ -361,40 +309,6 @@ public abstract class ManagerBase extends LifecycleMBeanBase /** - * Return the MessageDigest object to be used for calculating - * session identifiers. If none has been created yet, initialize - * one the first time this method is called. - */ - protected MessageDigest createDigest() { - - MessageDigest result; - - long t1=System.currentTimeMillis(); - if (log.isDebugEnabled()) - log.debug(sm.getString("managerBase.getting", algorithm)); - try { - result = MessageDigest.getInstance(algorithm); - } catch (NoSuchAlgorithmException e) { - log.error(sm.getString("managerBase.digest", algorithm), e); - try { - result = MessageDigest.getInstance(DEFAULT_ALGORITHM); - } catch (NoSuchAlgorithmException f) { - log.error(sm.getString("managerBase.digest", - DEFAULT_ALGORITHM), e); - result = null; - } - } - if (log.isDebugEnabled()) - log.debug(sm.getString("managerBase.gotten")); - long t2=System.currentTimeMillis(); - if( log.isDebugEnabled() ) - log.debug("getDigest() " + (t2-t1)); - - return result; - } - - - /** * Return the distributable flag for the sessions supported by * this Manager. */ @@ -1148,14 +1062,6 @@ public abstract class ManagerBase extends LifecycleMBeanBase while (resultLenBytes < this.sessionIdLength) { getRandomBytes(random); - MessageDigest md = digests.poll(); - if (md == null) { - // If this fails, NPEs will follow. This should never fail - // since if it falls back to the default digest - md = createDigest(); - } - random = md.digest(random); - digests.add(md); for (int j = 0; j < random.length && resultLenBytes < this.sessionIdLength; j++) { diff --git a/java/org/apache/catalina/session/mbeans-descriptors.xml b/java/org/apache/catalina/session/mbeans-descriptors.xml index bbf4da1e6..447adadc0 100644 --- a/java/org/apache/catalina/session/mbeans-descriptors.xml +++ b/java/org/apache/catalina/session/mbeans-descriptors.xml @@ -28,11 +28,6 @@ type="int" writeable="false"/> - - - - - -

Name of the Message Digest algorithm used to calculate - session identifiers produced by this Manager. This value must - be supported by the java.security.MessageDigest class. - If not specified, the default value is "MD5".

-
-

A String value that is utilized when seeding the random number generator used to create session identifiers for this Manager. -- 2.11.0