@@ -137,7 +137,18 @@ public class SockIOPool {
137
137
new HashMap <String ,SockIOPool >();
138
138
139
139
// 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
+ };
141
152
142
153
// Constants
143
154
private static final Integer ZERO = new Integer ( 0 );
@@ -508,19 +519,10 @@ private static long newCompatHashingAlg( String key ) {
508
519
* @return
509
520
*/
510
521
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 ();
524
526
long res = ((long )(bKey [3 ]&0xFF ) << 24 ) | ((long )(bKey [2 ]&0xFF ) << 16 ) | ((long )(bKey [1 ]&0xFF ) << 8 ) | (long )(bKey [0 ]&0xFF );
525
527
return res ;
526
528
}
@@ -534,7 +536,10 @@ private static long md5HashingAlg( String key ) {
534
536
private long getHash ( String key , Integer hashCode ) {
535
537
536
538
if ( hashCode != null ) {
537
- return hashCode .longValue ();
539
+ if ( hashingAlg == CONSISTENT_HASH )
540
+ return hashCode .longValue () & 0xffffffffL ;
541
+ else
542
+ return hashCode .longValue ();
538
543
}
539
544
else {
540
545
switch ( hashingAlg ) {
@@ -677,16 +682,7 @@ private void populateConsistentBuckets() {
677
682
// store buckets in tree map
678
683
this .consistentBuckets = new TreeMap <Long ,String >();
679
684
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 ();
690
686
if ( this .totalWeight <= 0 && this .weights != null ) {
691
687
for ( int i = 0 ; i < this .weights .length ; i ++ )
692
688
this .totalWeight += ( this .weights [i ] == null ) ? 1 : this .weights [i ];
@@ -703,7 +699,7 @@ else if ( this.weights == null ) {
703
699
double factor = Math .floor ( ((double )(40 * this .servers .length * thisWeight )) / (double )this .totalWeight );
704
700
705
701
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 () );
707
703
for ( int h = 0 ; h < 4 ; h ++ ) {
708
704
Long k =
709
705
((long )(d [3 +h *4 ]&0xFF ) << 24 )
@@ -1143,12 +1139,10 @@ private void checkIn( SockIO socket, boolean addToAvail ) {
1143
1139
log .debug ( "++++ removing socket (" + socket .toString () + ") from busy pool for host: " + host );
1144
1140
removeSocketFromPool ( busyPool , host , socket );
1145
1141
1146
- if ( socket .isConnected () ) {
1142
+ if ( socket .isConnected () && addToAvail ) {
1147
1143
// 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 );
1152
1146
}
1153
1147
else {
1154
1148
deadPool .put ( socket , ZERO );
0 commit comments