@ -43,6 +43,9 @@
package de.anomic.plasma ;
import java.lang.StringBuffer ;
import java.util.HashMap ;
import de.anomic.server.serverDate ;
import de.anomic.server.logging.serverLog ;
import de.anomic.yacy.yacyClient ;
import de.anomic.yacy.yacyCore ;
@ -50,6 +53,9 @@ import de.anomic.yacy.yacySeed;
public class plasmaDHTTransfer extends Thread {
public static final int TRANSFER_MODE_DISTRIBUTION = 0 ;
public static final int TRANSFER_MODE_FLUSH = 1 ;
// connection properties
private boolean gzipBody4Transfer = false ;
private int timeout4Transfer = 60000 ;
@ -57,6 +63,7 @@ public class plasmaDHTTransfer extends Thread {
// status fields
private boolean stopped = false ;
private long transferTime = 0 ;
private long payloadSize = 0 ;
private int transferStatus = plasmaDHTChunk . chunkStatus_UNDEFINED ;
private String transferStatusMessage = "" ;
@ -68,9 +75,17 @@ public class plasmaDHTTransfer extends Thread {
// other fields
private int maxRetry ;
private int transferMode = TRANSFER_MODE_DISTRIBUTION ;
serverLog log ;
public plasmaDHTTransfer ( serverLog log , yacySeed destSeed , plasmaDHTChunk dhtChunk , boolean gzipBody , int timeout , int retries ) {
public plasmaDHTTransfer (
serverLog log ,
yacySeed destSeed ,
plasmaDHTChunk dhtChunk ,
boolean gzipBody ,
int timeout ,
int retries
) {
super ( new ThreadGroup ( "TransferIndexThreadGroup" ) , "TransferIndexWorker_" + destSeed . getName ( ) ) ;
this . log = log ;
this . gzipBody4Transfer = gzipBody ;
@ -80,6 +95,10 @@ public class plasmaDHTTransfer extends Thread {
this . seed = destSeed ;
}
public void setTransferMode ( int mode ) {
this . transferMode = mode ;
}
public void run ( ) {
try {
this . uploadIndex ( ) ;
@ -90,6 +109,8 @@ public class plasmaDHTTransfer extends Thread {
private boolean isAborted ( ) {
if ( this . stopped | | Thread . currentThread ( ) . isInterrupted ( ) ) {
this . transferStatus = plasmaDHTChunk . chunkStatus_INTERRUPTED ;
this . transferStatusMessage = "aborted" ;
return true ;
}
return false ;
@ -103,6 +124,10 @@ public class plasmaDHTTransfer extends Thread {
return this . transferTime ;
}
public long getPayloadSize ( ) {
return this . payloadSize ;
}
public int getStatus ( ) {
return this . transferStatus ;
}
@ -129,39 +154,77 @@ public class plasmaDHTTransfer extends Thread {
// transfering seleted words to remote peer
this . transferStatusMessage = "Running: Transfering chunk to target " + this . seed . hash + "/" + this . seed . getName ( ) ;
String error = yacyClient . transferIndex ( this . seed , this . dhtChunk . containers ( ) , this . dhtChunk . urlCacheMap ( ) , this . gzipBody4Transfer , this . timeout4Transfer ) ;
HashMap result = yacyClient . transferIndex ( this . seed , this . dhtChunk . containers ( ) , this . dhtChunk . urlCacheMap ( ) , this . gzipBody4Transfer , this . timeout4Transfer ) ;
String error = ( String ) result . get ( "result" ) ;
if ( error = = null ) {
// words successfully transfered
this . transferTime = System . currentTimeMillis ( ) - start ;
this . log . logInfo ( "Index transfer of " + this . dhtChunk . indexCount ( ) + " words [" + this . dhtChunk . firstContainer ( ) . wordHash ( ) + " .. " + this . dhtChunk . lastContainer ( ) . wordHash ( ) + "]" + " to peer " + this . seed . getName ( ) + ":" + this . seed . hash + " in " + ( this . transferTime / 1000 ) + " seconds successful ("
+ ( 1000 * this . dhtChunk . indexCount ( ) / ( this . transferTime + 1 ) ) + " words/s)" ) ;
retryCount = 0 ;
this . payloadSize = ( ( Integer ) result . get ( "payloadSize" ) ) . intValue ( ) ;
this . log . logInfo ( "Index transfer of " + this . dhtChunk . indexCount ( ) +
" words [" + this . dhtChunk . firstContainer ( ) . wordHash ( ) + " .. " + this . dhtChunk . lastContainer ( ) . wordHash ( ) + "]" +
" to peer " + this . seed . getName ( ) + ":" + this . seed . hash +
" in " + ( this . transferTime / 1000 ) +
" seconds successful (" + ( 1000 * this . dhtChunk . indexCount ( ) / ( this . transferTime + 1 ) ) +
" words/s, " + this . payloadSize + " Bytes)" ) ;
// if the peer has set a pause time and we are in flush mode (index transfer)
// then we pause for a while now
if ( this . transferMode = = TRANSFER_MODE_FLUSH ) {
long pause = getBusyTime ( result ) ;
if ( pause ! = - 1 ) {
this . transferStatusMessage = "Finished: Transfer of chunk to target " + this . seed . hash + "/" + this . seed . getName ( ) + ". Pausing " + pause + " ms." ;
this . pause ( pause ) ;
}
} else {
this . transferStatusMessage = "Finished: Transfer of chunk to target " + this . seed . hash + "/" + this . seed . getName ( ) ;
}
// transfer of chunk finished
this . transferStatus = plasmaDHTChunk . chunkStatus_COMPLETE ;
retryCount = 0 ;
break ;
}
// inc retry counter
retryCount + + ;
// testing if we were aborted ...
if ( this . isAborted ( ) ) return ;
// we have lost the connection to the remote peer. Adding peer to disconnected list
StringBuffer failMessage = new StringBuffer ( "Index transfer to peer " + this . seed . getName ( ) + ":" + this . seed . hash + " failed:'" + error + "'" ) ;
if ( ! error . equals ( "busy" ) ) {
boolean reconnectNeeded = false ;
long pauseTime = 1 ;
if ( error . equals ( "busy" ) ) {
// get pause time that was requested by the remote peer
pauseTime = getBusyTime ( result ) ;
if ( pauseTime = = - 1 ) pauseTime = 60000 ;
this . transferStatusMessage = "Peer " + this . seed . getName ( ) + ":" + this . seed . hash + " is busy. Waiting " + pauseTime + " ms." ;
this . log . logInfo ( this . transferStatusMessage ) ;
} else {
this . transferStatusMessage = "Transfer to peer " + this . seed . getName ( ) + ":" + this . seed . hash + " failed:'" + error + "', Trying to reconnect ..." ;
// force disconnection of peer
yacyCore . peerActions . peerDeparture ( this . seed ) ;
failMessage . append ( ", disconnected peer" ) ;
this . log . logWarning ( this . transferStatusMessage ) ;
// calculate pause time
pauseTime = retryCount * 10000 ;
reconnectNeeded = true ;
}
this . log . logWarning ( failMessage . toString ( ) ) ;
// if the retry counter limit was not exceeded we'll retry it in a few seconds
this . transferStatusMessage = "Disconnected peer: " + ( ( retryCount > 5 ) ? error + ". Transfer aborted" : "Retry " + retryCount ) ;
if ( retryCount > this . maxRetry ) {
this . transferStatusMessage = "Transfer aborted. Retry limit reached." ;
this . transferStatus = plasmaDHTChunk . chunkStatus_FAILED ;
return ;
}
Thread . sleep ( retryCount * 5000 ) ;
// sleep for a while
this . pause ( pauseTime ) ;
// reconnect to peer if needed
if ( reconnectNeeded ) {
/ * loop until
* - we have successfully done a peer ping or
@ -179,7 +242,7 @@ public class plasmaDHTTransfer extends Thread {
retryCount + + ;
this . transferStatusMessage = "Disconnected peer: Peer ping failed. " + ( ( retryCount > 5 ) ? "Transfer aborted." : "Retry " + retryCount ) ;
if ( retryCount > this . maxRetry ) return ;
Thread . sleep ( retryCount * 5 000) ;
this . pause ( retryCount * 10 000) ;
continue ;
}
@ -190,3 +253,26 @@ public class plasmaDHTTransfer extends Thread {
}
}
}
private long getBusyTime ( HashMap result ) {
int pause = - 1 ;
HashMap transferRWIResult = ( HashMap ) result . get ( "resultTransferRWI" ) ;
if ( transferRWIResult ! = null & & transferRWIResult . containsKey ( "pause" ) ) {
String pauseStr = ( String ) transferRWIResult . get ( "pause" ) ;
try { pause = Integer . valueOf ( pauseStr ) . intValue ( ) ; } catch ( NumberFormatException numEx ) { }
if ( pause < = 0 ) pause = 60000 ;
else if ( pause > 1800000 ) pause = 1800000 ;
}
return pause ;
}
private void pause ( long sleepTime ) throws InterruptedException {
long sleepCounter = sleepTime / 1000 ;
long sleepRest = sleepTime % 1000 ;
while ( ! this . isAborted ( ) & & sleepCounter > 0 ) {
sleepCounter - - ;
Thread . sleep ( 1000 ) ;
}
if ( sleepRest > 0 ) Thread . sleep ( sleepRest ) ;
}
}