@@ -137,7 +137,18 @@ public class SockIOPool {
137137 new HashMap <String ,SockIOPool >();
138138
139139 // avoid recurring construction
140- private static MessageDigest MD5 = null ;
140+ private static ThreadLocal <MessageDigest > MD5 = new ThreadLocal <MessageDigest >() {
141+ @ Override
142+ protected MessageDigest initialValue () {
143+ try {
144+ return MessageDigest .getInstance ( "MD5" );
145+ }
146+ catch ( NoSuchAlgorithmException e ) {
147+ log .error ( "++++ no md5 algorithm found" );
148+ throw new IllegalStateException ( "++++ no md5 algorythm found" );
149+ }
150+ }
151+ };
141152
142153 // Constants
143154 private static final Integer ZERO = new Integer ( 0 );
@@ -508,19 +519,10 @@ private static long newCompatHashingAlg( String key ) {
508519 * @return
509520 */
510521 private static long md5HashingAlg ( String key ) {
511- if ( MD5 == null ) {
512- try {
513- MD5 = MessageDigest .getInstance ( "MD5" );
514- }
515- catch ( NoSuchAlgorithmException e ) {
516- log .error ( "++++ no md5 algorithm found" );
517- throw new IllegalStateException ( "++++ no md5 algorythm found" );
518- }
519- }
520-
521- MD5 .reset ();
522- MD5 .update ( key .getBytes () );
523- byte [] bKey = MD5 .digest ();
522+ MessageDigest md5 = MD5 .get ();
523+ md5 .reset ();
524+ md5 .update ( key .getBytes () );
525+ byte [] bKey = md5 .digest ();
524526 long res = ((long )(bKey [3 ]&0xFF ) << 24 ) | ((long )(bKey [2 ]&0xFF ) << 16 ) | ((long )(bKey [1 ]&0xFF ) << 8 ) | (long )(bKey [0 ]&0xFF );
525527 return res ;
526528 }
@@ -534,7 +536,10 @@ private static long md5HashingAlg( String key ) {
534536 private long getHash ( String key , Integer hashCode ) {
535537
536538 if ( hashCode != null ) {
537- return hashCode .longValue ();
539+ if ( hashingAlg == CONSISTENT_HASH )
540+ return hashCode .longValue () & 0xffffffffL ;
541+ else
542+ return hashCode .longValue ();
538543 }
539544 else {
540545 switch ( hashingAlg ) {
@@ -677,16 +682,7 @@ private void populateConsistentBuckets() {
677682 // store buckets in tree map
678683 this .consistentBuckets = new TreeMap <Long ,String >();
679684
680- if ( MD5 == null ) {
681- try {
682- MD5 = MessageDigest .getInstance ( "MD5" );
683- }
684- catch ( NoSuchAlgorithmException e ) {
685- log .error ( "++++ no md5 algorithm found" );
686- throw new IllegalStateException ( "++++ no md5 algorythm found" );
687- }
688- }
689-
685+ MessageDigest md5 = MD5 .get ();
690686 if ( this .totalWeight <= 0 && this .weights != null ) {
691687 for ( int i = 0 ; i < this .weights .length ; i ++ )
692688 this .totalWeight += ( this .weights [i ] == null ) ? 1 : this .weights [i ];
@@ -703,7 +699,7 @@ else if ( this.weights == null ) {
703699 double factor = Math .floor ( ((double )(40 * this .servers .length * thisWeight )) / (double )this .totalWeight );
704700
705701 for ( long j = 0 ; j < factor ; j ++ ) {
706- byte [] d = MD5 .digest ( (servers [i ] + "-" + j ).getBytes () );
702+ byte [] d = md5 .digest ( ( servers [i ] + "-" + j ).getBytes () );
707703 for ( int h = 0 ; h < 4 ; h ++ ) {
708704 Long k =
709705 ((long )(d [3 +h *4 ]&0xFF ) << 24 )
@@ -1143,12 +1139,10 @@ private void checkIn( SockIO socket, boolean addToAvail ) {
11431139 log .debug ( "++++ removing socket (" + socket .toString () + ") from busy pool for host: " + host );
11441140 removeSocketFromPool ( busyPool , host , socket );
11451141
1146- if ( socket .isConnected () ) {
1142+ if ( socket .isConnected () && addToAvail ) {
11471143 // add to avail pool
1148- if ( addToAvail ) {
1149- log .debug ( "++++ returning socket (" + socket .toString () + " to avail pool for host: " + host );
1150- addSocketToPool ( availPool , host , socket );
1151- }
1144+ log .debug ( "++++ returning socket (" + socket .toString () + " to avail pool for host: " + host );
1145+ addSocketToPool ( availPool , host , socket );
11521146 }
11531147 else {
11541148 deadPool .put ( socket , ZERO );
0 commit comments