Skip to content

Commit 700f5e8

Browse files
committed
fix infinite loop bug when all servers die
1 parent ce9f3a4 commit 700f5e8

File tree

1 file changed

+43
-38
lines changed

1 file changed

+43
-38
lines changed

src/com/danga/MemCached/SockIOPool.java

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -715,29 +715,22 @@ public SockIO getSock( String key, Integer hashCode ) {
715715
if ( buckets.size() == 1 ) {
716716
SockIO sock = getConnection( (String) buckets.get( 0 ) );
717717
if ( sock != null && sock.isConnected() ) {
718-
719718
if ( aliveCheck ) {
720-
if ( sock.isAlive() ) {
721-
return sock;
722-
}
723-
else {
719+
if ( !sock.isAlive() ) {
724720
sock.close();
725721
try { sock.trueClose(); } catch ( IOException ioe ) { log.error( "failed to close dead socket" ); }
726722
sock = null;
727723
}
728724
}
729-
else {
730-
return sock;
731-
}
732725
}
733726
else {
734727
sock = null;
735728
}
736729

737-
if ( !failover )
738-
return null;
730+
return sock;
739731
}
740732

733+
// from here on, we are working w/ multiple servers
741734
int tries = 0;
742735

743736
// generate hashcode
@@ -813,41 +806,53 @@ public SockIO getSock( String key, Integer hashCode ) {
813806
// log that we tried
814807
triedBucket[ bucket ] = true;
815808

816-
// if we failed to get a socket from this server
817-
// then we try again by adding an incrementer to the
818-
// current key and then rehashing
819-
int rehashTries = 0;
820-
while ( triedBucket[ bucket ] ) {
809+
// if we have not already tried all buckets, then
810+
// rehash and try again
811+
boolean needRehash = false;
812+
for ( boolean b : triedBucket ) {
813+
if ( ! b ) {
814+
needRehash = true;
815+
break;
816+
}
817+
}
818+
819+
if ( needRehash ) {
820+
// if we failed to get a socket from this server
821+
// then we try again by adding an incrementer to the
822+
// current key and then rehashing
823+
int rehashTries = 0;
824+
while ( triedBucket[ bucket ] ) {
821825

822-
int keyTry = tries + rehashTries;
823-
String newKey = String.format( "%s%s", keyTry, key );
826+
int keyTry = tries + rehashTries;
827+
String newKey = String.format( "%s%s", keyTry, key );
824828

825-
log.debug( "rehashing with: " + keyTry );
826-
switch ( hashingAlg ) {
827-
case NATIVE_HASH:
828-
hv = newKey.hashCode();
829-
break;
829+
log.debug( "rehashing with: " + keyTry );
830+
switch ( hashingAlg ) {
831+
case NATIVE_HASH:
832+
hv = newKey.hashCode();
833+
break;
830834

831-
case OLD_COMPAT_HASH:
832-
hv = origCompatHashingAlg( newKey );
833-
break;
835+
case OLD_COMPAT_HASH:
836+
hv = origCompatHashingAlg( newKey );
837+
break;
834838

835-
case NEW_COMPAT_HASH:
836-
hv = newCompatHashingAlg( newKey );
837-
break;
839+
case NEW_COMPAT_HASH:
840+
hv = newCompatHashingAlg( newKey );
841+
break;
838842

839-
default:
840-
// use the native hash as a default
841-
hv = newKey.hashCode();
842-
hashingAlg = NATIVE_HASH;
843-
break;
844-
}
843+
default:
844+
// use the native hash as a default
845+
hv = newKey.hashCode();
846+
hashingAlg = NATIVE_HASH;
847+
break;
848+
}
845849

846-
rehashTries++;
850+
rehashTries++;
847851

848-
// new bucket
849-
bucket = hv % bucketSize;
850-
if ( bucket < 0 ) bucket *= -1;
852+
// new bucket
853+
bucket = hv % bucketSize;
854+
if ( bucket < 0 ) bucket *= -1;
855+
}
851856
}
852857
}
853858

0 commit comments

Comments
 (0)