cassandra-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbel...@apache.org
Subject svn commit: r1072173 - in /cassandra/branches/cassandra-0.7: ./ contrib/stress/src/org/apache/cassandra/contrib/stress/util/ src/java/org/apache/cassandra/auth/ src/java/org/apache/cassandra/db/ src/java/org/apache/cassandra/net/ src/java/org/apache/ca...
Date Fri, 18 Feb 2011 22:24:10 GMT
Author: jbellis
Date: Fri Feb 18 22:24:10 2011
New Revision: 1072173

URL: http://svn.apache.org/viewvc?rev=1072173&view=rev
Log:
centralize MessageDigest creation and use threadlocals for MD5s
patch by mdennis; reviewed by jbellis for CASSANDRA-2107

Modified:
    cassandra/branches/cassandra-0.7/CHANGES.txt
    cassandra/branches/cassandra-0.7/contrib/stress/src/org/apache/cassandra/contrib/stress/util/OperationThread.java
    cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/auth/SimpleAuthenticator.java
    cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/db/ColumnFamily.java
    cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/net/MessagingService.java
    cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/service/AntiEntropyService.java
    cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/utils/FBUtilities.java
    cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/utils/GuidGenerator.java
    cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/utils/UUIDGen.java

Modified: cassandra/branches/cassandra-0.7/CHANGES.txt
URL: http://svn.apache.org/viewvc/cassandra/branches/cassandra-0.7/CHANGES.txt?rev=1072173&r1=1072172&r2=1072173&view=diff
==============================================================================
--- cassandra/branches/cassandra-0.7/CHANGES.txt (original)
+++ cassandra/branches/cassandra-0.7/CHANGES.txt Fri Feb 18 22:24:10 2011
@@ -22,6 +22,7 @@
 
 
 0.7.1
+ * refactor MessageDigest creation code. (CASSANDRA-2107)
  * buffer network stack to avoid inefficient small TCP messages while avoiding
    the nagle/delayed ack problem (CASSANDRA-1896)
  * check log4j configuration for changes every 10s (CASSANDRA-1525, 1907)

Modified: cassandra/branches/cassandra-0.7/contrib/stress/src/org/apache/cassandra/contrib/stress/util/OperationThread.java
URL: http://svn.apache.org/viewvc/cassandra/branches/cassandra-0.7/contrib/stress/src/org/apache/cassandra/contrib/stress/util/OperationThread.java?rev=1072173&r1=1072172&r2=1072173&view=diff
==============================================================================
--- cassandra/branches/cassandra-0.7/contrib/stress/src/org/apache/cassandra/contrib/stress/util/OperationThread.java
(original)
+++ cassandra/branches/cassandra-0.7/contrib/stress/src/org/apache/cassandra/contrib/stress/util/OperationThread.java
Fri Feb 18 22:24:10 2011
@@ -17,18 +17,18 @@
  */
 package org.apache.cassandra.contrib.stress.util;
 
-import org.apache.cassandra.contrib.stress.Session;
-import org.apache.cassandra.contrib.stress.Stress;
-import org.apache.cassandra.thrift.Cassandra;
-import org.apache.cassandra.thrift.InvalidRequestException;
-
 import java.math.BigInteger;
 import java.security.MessageDigest;
-import java.security.NoSuchAlgorithmException;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Random;
 
+import org.apache.cassandra.contrib.stress.Session;
+import org.apache.cassandra.contrib.stress.Stress;
+import org.apache.cassandra.thrift.Cassandra;
+import org.apache.cassandra.thrift.InvalidRequestException;
+import org.apache.cassandra.utils.FBUtilities;
+
 public abstract class OperationThread extends Thread
 {
     public final int index;
@@ -146,21 +146,14 @@ public abstract class OperationThread ex
      */
     private String getMD5(String input)
     {
-        try
-        {
-            MessageDigest md = MessageDigest.getInstance("MD5");
-            byte[] messageDigest = md.digest(input.getBytes());
-            StringBuilder hash = new StringBuilder(new BigInteger(1, messageDigest).toString(16));
+        MessageDigest md = FBUtilities.threadLocalMD5Digest();
+        byte[] messageDigest = md.digest(input.getBytes());
+        StringBuilder hash = new StringBuilder(new BigInteger(1, messageDigest).toString(16));
 
-            while (hash.length() < 32)
-                hash.append("0").append(hash);
+        while (hash.length() < 32)
+            hash.append("0").append(hash);
 
-            return hash.toString();
-        }
-        catch (NoSuchAlgorithmException e)
-        {
-            throw new RuntimeException(e);
-        }
+        return hash.toString();
     }
 
     /**

Modified: cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/auth/SimpleAuthenticator.java
URL: http://svn.apache.org/viewvc/cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/auth/SimpleAuthenticator.java?rev=1072173&r1=1072172&r2=1072173&view=diff
==============================================================================
--- cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/auth/SimpleAuthenticator.java
(original)
+++ cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/auth/SimpleAuthenticator.java
Fri Feb 18 22:24:10 2011
@@ -26,7 +26,6 @@ import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.security.MessageDigest;
-import java.security.NoSuchAlgorithmException;
 import java.util.Map;
 import java.util.Properties;
 
@@ -111,16 +110,12 @@ public class SimpleAuthenticator impleme
                     authenticated = password.equals(props.getProperty(username));
                     break;
                 case MD5:
-                    authenticated = MessageDigest.isEqual(MessageDigest.getInstance("MD5").digest(password.getBytes()),
FBUtilities.hexToBytes(props.getProperty(username)));
+                    authenticated = MessageDigest.isEqual(FBUtilities.threadLocalMD5Digest().digest(password.getBytes()),
FBUtilities.hexToBytes(props.getProperty(username)));
                     break;
                 default:
                     throw new RuntimeException("Unknown PasswordMode " + mode);
             }
         }
-        catch (NoSuchAlgorithmException e)
-        {
-            throw new RuntimeException("You requested MD5 checking but the MD5 digest algorithm
is not available: " + e.getMessage());
-        }
         catch (IOException e)
         {
             throw new RuntimeException("Authentication table file given by property " + PASSWD_FILENAME_PROPERTY
+ " could not be opened: " + e.getMessage());

Modified: cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/db/ColumnFamily.java
URL: http://svn.apache.org/viewvc/cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/db/ColumnFamily.java?rev=1072173&r1=1072172&r2=1072173&view=diff
==============================================================================
--- cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/db/ColumnFamily.java (original)
+++ cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/db/ColumnFamily.java Fri
Feb 18 22:24:10 2011
@@ -20,7 +20,6 @@ package org.apache.cassandra.db;
 
 import java.nio.ByteBuffer;
 import java.security.MessageDigest;
-import java.security.NoSuchAlgorithmException;
 import java.util.Collection;
 import java.util.Iterator;
 import java.util.Map;
@@ -364,18 +363,9 @@ public class ColumnFamily implements ICo
 
     public static ByteBuffer digest(ColumnFamily cf)
     {
-        MessageDigest digest;
-        try
-        {
-            digest = MessageDigest.getInstance("MD5");
-        }
-        catch (NoSuchAlgorithmException e)
-        {
-            throw new AssertionError(e);
-        }
+        MessageDigest digest = FBUtilities.threadLocalMD5Digest();
         if (cf != null)
             cf.updateDigest(digest);
-
         return ByteBuffer.wrap(digest.digest());
     }
 

Modified: cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/net/MessagingService.java
URL: http://svn.apache.org/viewvc/cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/net/MessagingService.java?rev=1072173&r1=1072172&r2=1072173&view=diff
==============================================================================
--- cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/net/MessagingService.java
(original)
+++ cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/net/MessagingService.java
Fri Feb 18 22:24:10 2011
@@ -25,7 +25,6 @@ import java.net.*;
 import java.nio.ByteBuffer;
 import java.nio.channels.AsynchronousCloseException;
 import java.nio.channels.ServerSocketChannel;
-import java.security.MessageDigest;
 import java.util.*;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.TimeUnit;
@@ -148,21 +147,6 @@ public final class MessagingService impl
             subscriber.receiveTiming(address, latency);
     }
 
-    public static byte[] hash(String type, byte data[])
-    {
-        byte result[];
-        try
-        {
-            MessageDigest messageDigest = MessageDigest.getInstance(type);
-            result = messageDigest.digest(data);
-        }
-        catch (Exception e)
-        {
-            throw new RuntimeException(e);
-        }
-        return result;
-    }
-
     /** called from gossiper when it notices a node is not responding. */
     public void convict(InetAddress ep)
     {

Modified: cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/service/AntiEntropyService.java
URL: http://svn.apache.org/viewvc/cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/service/AntiEntropyService.java?rev=1072173&r1=1072172&r2=1072173&view=diff
==============================================================================
--- cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/service/AntiEntropyService.java
(original)
+++ cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/service/AntiEntropyService.java
Fri Feb 18 22:24:10 2011
@@ -21,7 +21,6 @@ package org.apache.cassandra.service;
 import java.io.*;
 import java.net.InetAddress;
 import java.security.MessageDigest;
-import java.security.NoSuchAlgorithmException;
 import java.util.*;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
@@ -375,15 +374,7 @@ public class AntiEntropyService
         {
             validated++;
             // MerkleTree uses XOR internally, so we want lots of output bits here
-            MessageDigest digest = null;
-            try
-            {
-                digest = MessageDigest.getInstance("SHA-256");
-            }
-            catch (NoSuchAlgorithmException e)
-            {
-                throw new AssertionError(e);
-            }
+            MessageDigest digest = FBUtilities.newMessageDigest("SHA-256");
             row.update(digest);
             return new MerkleTree.RowHash(row.key.token, digest.digest());
         }

Modified: cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/utils/FBUtilities.java
URL: http://svn.apache.org/viewvc/cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/utils/FBUtilities.java?rev=1072173&r1=1072172&r2=1072173&view=diff
==============================================================================
--- cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/utils/FBUtilities.java
(original)
+++ cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/utils/FBUtilities.java
Fri Feb 18 22:24:10 2011
@@ -26,7 +26,6 @@ import java.net.InetAddress;
 import java.net.URL;
 import java.net.UnknownHostException;
 import java.nio.ByteBuffer;
-import java.nio.charset.CharacterCodingException;
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
 import java.util.*;
@@ -35,12 +34,8 @@ import java.util.concurrent.Future;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicLong;
 
-import com.google.common.base.Charsets;
 import com.google.common.base.Joiner;
-import com.google.common.base.Strings;
-import com.google.common.primitives.Ints;
 import org.apache.commons.collections.iterators.CollatingIterator;
-import org.apache.commons.lang.ArrayUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -65,24 +60,42 @@ public class FBUtilities
 
     private static volatile InetAddress localInetAddress_;
 
-    private static final ThreadLocal<MessageDigest> localMessageDigest = new ThreadLocal<MessageDigest>()
+    private static final ThreadLocal<MessageDigest> localMD5Digest = new ThreadLocal<MessageDigest>()
     {
         @Override
         protected MessageDigest initialValue()
         {
-            try
-            {
-                return MessageDigest.getInstance("MD5");
-            }
-            catch (NoSuchAlgorithmException e)
-            {
-                throw new AssertionError(e);
-            }
+            return newMessageDigest("MD5");
+        }
+
+        @Override
+        public MessageDigest get()
+        {
+            MessageDigest digest = super.get();
+            digest.reset();
+            return digest;
         }
     };
 
     public static final int MAX_UNSIGNED_SHORT = 0xFFFF;
 
+    public static MessageDigest threadLocalMD5Digest()
+    {
+        return localMD5Digest.get();
+    }
+
+    public static MessageDigest newMessageDigest(String algorithm)
+    {
+        try
+        {
+            return MessageDigest.getInstance(algorithm);
+        }
+        catch (NoSuchAlgorithmException nsae)
+        {
+            throw new RuntimeException("the requested digest algorithm (" + algorithm + ")
is not available", nsae);
+        }
+    }
+
     /**
      * Parses a string representing either a fraction, absolute value or percentage.
      */
@@ -219,24 +232,14 @@ public class FBUtilities
 
     public static byte[] hash(ByteBuffer... data)
     {
-    	byte[] result;
-    	try
+        MessageDigest messageDigest = localMD5Digest.get();
+        for(ByteBuffer block : data)
         {
-            MessageDigest messageDigest = localMessageDigest.get();
-            messageDigest.reset();
-            for(ByteBuffer block : data)
-            {
-                messageDigest.update(ByteBufferUtil.clone(block));
-            }
+            messageDigest.update(ByteBufferUtil.clone(block));
+        }
 
-            result = messageDigest.digest();
-    	}
-    	catch (Exception e)
-        {
-            throw new RuntimeException(e);
-    	}
-    	return result;
-	}
+        return messageDigest.digest();
+    }
 
     public static byte[] hexToBytes(String str)
     {

Modified: cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/utils/GuidGenerator.java
URL: http://svn.apache.org/viewvc/cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/utils/GuidGenerator.java?rev=1072173&r1=1072172&r2=1072173&view=diff
==============================================================================
--- cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/utils/GuidGenerator.java
(original)
+++ cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/utils/GuidGenerator.java
Fri Feb 18 22:24:10 2011
@@ -22,7 +22,6 @@ import java.net.InetAddress;
 import java.net.UnknownHostException;
 import java.nio.ByteBuffer;
 import java.security.MessageDigest;
-import java.security.NoSuchAlgorithmException;
 import java.security.SecureRandom;
 import java.util.Random;
 
@@ -34,21 +33,6 @@ public class GuidGenerator {
     private static Random myRand;
     private static SecureRandom mySecureRand;
     private static String s_id;
-    private static final ThreadLocal<MessageDigest> localMessageDigest = new ThreadLocal<MessageDigest>()
-    {
-        @Override
-        protected MessageDigest initialValue()
-        {
-            try
-            {
-                return MessageDigest.getInstance("MD5");
-            }
-            catch (NoSuchAlgorithmException e)
-            {
-                throw new AssertionError(e);
-            }
-        }
-    };
 
     static {
         if (System.getProperty("java.security.egd") == null) {
@@ -104,8 +88,7 @@ public class GuidGenerator {
         				.append(Long.toString(rand));
 
         String valueBeforeMD5 = sbValueBeforeMD5.toString();
-        localMessageDigest.get().reset();
-        return ByteBuffer.wrap(localMessageDigest.get().digest(valueBeforeMD5.getBytes()));
+        return ByteBuffer.wrap(FBUtilities.threadLocalMD5Digest().digest(valueBeforeMD5.getBytes()));
     }
 
     /*

Modified: cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/utils/UUIDGen.java
URL: http://svn.apache.org/viewvc/cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/utils/UUIDGen.java?rev=1072173&r1=1072172&r2=1072173&view=diff
==============================================================================
--- cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/utils/UUIDGen.java (original)
+++ cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/utils/UUIDGen.java Fri
Feb 18 22:24:10 2011
@@ -24,7 +24,6 @@ package org.apache.cassandra.utils;
 import java.net.InetAddress;
 import java.nio.ByteBuffer;
 import java.security.MessageDigest;
-import java.security.NoSuchAlgorithmException;
 import java.util.UUID;
 
 import org.safehaus.uuid.EthernetAddress;
@@ -36,21 +35,14 @@ import org.safehaus.uuid.UUIDGenerator;
 public class UUIDGen
 {
     /** creates a type1 uuid but substitutes hash of the IP where the mac would go. */
-    public static synchronized UUID makeType1UUIDFromHost(InetAddress addr)
+    public static UUID makeType1UUIDFromHost(InetAddress addr)
     {
-        try
-        {
-            MessageDigest digest = MessageDigest.getInstance("MD5");
-            digest.update(addr.getAddress());
-            byte[] md5 = digest.digest();
-            byte[] fauxMac = new byte[6];
-            System.arraycopy(md5, 0, fauxMac, 0, Math.min(md5.length, fauxMac.length));
-            return getUUID(ByteBuffer.wrap(UUIDGenerator.getInstance().generateTimeBasedUUID(new
EthernetAddress(fauxMac)).toByteArray()));
-        }
-        catch (NoSuchAlgorithmException ex)
-        {
-            throw new RuntimeException("Your platform has no support for generating MD5 sums");
-        }
+        MessageDigest digest = FBUtilities.threadLocalMD5Digest();
+        digest.update(addr.getAddress());
+        byte[] md5 = digest.digest();
+        byte[] fauxMac = new byte[6];
+        System.arraycopy(md5, 0, fauxMac, 0, Math.min(md5.length, fauxMac.length));
+        return getUUID(ByteBuffer.wrap(UUIDGenerator.getInstance().generateTimeBasedUUID(new
EthernetAddress(fauxMac)).toByteArray()));
     }
     
     /** creates a type 1 uuid from raw bytes. */



Mime
View raw message