Improve response check time

When doing a NodeLookup, Content Lookup or Connect operation, after sending the message, we used to wait(operation_timeout) time before checking for a result. 
Now what I'm doing is waiting for 100 ms and check for a result every 100ms; I keep a count of total time waited, and only when the total time > operation_timeout do we make a timeout.
So now we can handle the response if it comes in before Operation_timeout time is finished
This commit is contained in:
Joshua Kissoon 2014-04-13 18:57:54 +05:30
parent eaeffeb0ba
commit 4888bf4dd4
5 changed files with 86 additions and 31 deletions

View File

@ -10,11 +10,11 @@ import java.io.File;
public class DefaultConfiguration implements KadConfiguration public class DefaultConfiguration implements KadConfiguration
{ {
private final static long RESTORE_INTERVAL = 60 * 1000; // Default at 1 hour private final static long RESTORE_INTERVAL = 5 * 1000; // Default at 1 hour
private final static long RESPONSE_TIMEOUT = 1500; private final static long RESPONSE_TIMEOUT = 1500;
private final static long OPERATION_TIMEOUT = 3000; private final static long OPERATION_TIMEOUT = 30000;
private final static int CONCURRENCY = 10; private final static int CONCURRENCY = 10;
private final static int K = 2; private final static int K = 10;
private final static int RCSIZE = 3; private final static int RCSIZE = 3;
private final static int STALE = 1; private final static int STALE = 1;
private final static String LOCAL_FOLDER = "kademlia"; private final static String LOCAL_FOLDER = "kademlia";

View File

@ -54,12 +54,24 @@ public class ConnectOperation implements Operation, Receiver
/* Send a connect message to the bootstrap node */ /* Send a connect message to the bootstrap node */
server.sendMessage(this.bootstrapNode, m, this); server.sendMessage(this.bootstrapNode, m, this);
/* Wait for a while */ /* If we haven't finished as yet, wait for a maximum of config.operationTimeout() time */
wait(config.operationTimeout()); int totalTimeWaited = 0;
int timeInterval = 100; // We re-check every 300 milliseconds
while (totalTimeWaited < this.config.operationTimeout())
{
if (error)
{
wait(timeInterval);
totalTimeWaited += timeInterval;
}
else
{
break;
}
}
if (error) if (error)
{ {
/* Means the contact failed */ /* If we still haven't received any responses by then, do a routing timeout */
throw new RoutingException("Bootstrap node did not respond: " + bootstrapNode); throw new RoutingException("Bootstrap node did not respond: " + bootstrapNode);
} }

View File

@ -106,26 +106,44 @@ public class ContentLookupOperation implements Operation, Receiver
this.addNodes(this.localNode.getRoutingTable().getAllNodes()); this.addNodes(this.localNode.getRoutingTable().getAllNodes());
if (!this.askNodesorFinish()) /* If we haven't finished as yet, wait for a maximum of config.operationTimeout() time */
int totalTimeWaited = 0;
int timeInterval = 100; // We re-check every 300 milliseconds
while (totalTimeWaited < this.config.operationTimeout())
{ {
/* If we haven't finished as yet, wait a while */ if (!this.askNodesorFinish())
/**
* @todo Get rid of this wait here!
* We should run this until there are no nodes left to ask from the K closest nodes
* and only pause for short intervals in between
*
* @todo Do the same for the NodeLookupOperation
*/
wait(this.config.operationTimeout());
/* If we still haven't received any responses by then, do a routing timeout */
if (error)
{ {
/* Lets not throw any exception */ wait(timeInterval);
totalTimeWaited += timeInterval;
//throw new RoutingException("Content Lookup Operation Timeout."); }
else
{
break;
} }
} }
if (error)
{
/* If we still haven't received any responses by then, do a routing timeout */
throw new RoutingException("Lookup Timeout.");
}
/**
* @deprecated - replaced by the above code
* We just keep this code in case any problems are encountered later
*/
// if (!this.askNodesorFinish())
// {
// /* If we haven't finished as yet, wait a while */
// wait(this.config.operationTimeout());
//
// /* If we still haven't received any responses by then, do a routing timeout */
// if (error)
// {
// /* Lets not throw any exception */
//
// //throw new RoutingException("Content Lookup Operation Timeout.");
// }
// }
} }
catch (InterruptedException e) catch (InterruptedException e)
{ {

View File

@ -97,17 +97,42 @@ public class NodeLookupOperation implements Operation, Receiver
this.addNodes(this.localNode.getRoutingTable().getAllNodes()); this.addNodes(this.localNode.getRoutingTable().getAllNodes());
if (!this.askNodesorFinish()) /* If we haven't finished as yet, wait for a maximum of config.operationTimeout() time */
int totalTimeWaited = 0;
int timeInterval = 100; // We re-check every 300 milliseconds
while (totalTimeWaited < this.config.operationTimeout())
{ {
/* If we haven't finished as yet, wait a while */ if (!this.askNodesorFinish())
wait(this.config.operationTimeout());
/* If we still haven't received any responses by then, do a routing timeout */
if (error)
{ {
throw new RoutingException("Lookup Timeout."); wait(timeInterval);
totalTimeWaited += timeInterval;
}
else
{
break;
} }
} }
if (error)
{
/* If we still haven't received any responses by then, do a routing timeout */
throw new RoutingException("Lookup Timeout.");
}
/**
* @deprecated - replaced by the above code
* We just keep this code in case any problems are encountered later
*/
// if (!this.askNodesorFinish())
// {
// /* If we haven't finished as yet, wait for a maximum of OPERATION_TIMEOUT time */
// wait(this.config.operationTimeout());
//
// /* If we still haven't received any responses by then, do a routing timeout */
// if (error)
// {
// throw new RoutingException("Lookup Timeout.");
// }
// }
} }
catch (InterruptedException e) catch (InterruptedException e)
{ {