00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00024 #include <cfloat>
00025 #include <assert.h>
00026
00027 #include <GlobalStatistics.h>
00028 #include <Comparator.h>
00029 #include <BootstrapList.h>
00030 #include <GlobalParameters.h>
00031
00032 #include <ChordFingerTable.h>
00033 #include <ChordSuccessorList.h>
00034
00035 #include "Chord.h"
00036
00037 namespace oversim {
00038
00039 Define_Module(Chord);
00040
00041 Chord::Chord()
00042 {
00043 stabilize_timer = fixfingers_timer = join_timer = NULL;
00044 fingerTable = NULL;
00045 }
00046
00047
00048 void Chord::initializeOverlay(int stage)
00049 {
00050
00051
00052 if (stage != MIN_STAGE_OVERLAY)
00053 return;
00054
00055 if (iterativeLookupConfig.merge == true) {
00056 throw cRuntimeError("Chord::initializeOverlay(): "
00057 "Chord doesn't work with iterativeLookupConfig.merge = true!");
00058 }
00059
00060
00061 kbr = true;
00062
00063
00064 useCommonAPIforward = par("useCommonAPIforward");
00065 successorListSize = par("successorListSize");
00066 joinRetry = par("joinRetry");
00067 stabilizeRetry = par("stabilizeRetry");
00068 joinDelay = par("joinDelay");
00069 stabilizeDelay = par("stabilizeDelay");
00070 fixfingersDelay = par("fixfingersDelay");
00071 checkPredecessorDelay = par("checkPredecessorDelay");
00072 aggressiveJoinMode = par("aggressiveJoinMode");
00073 extendedFingerTable = par("extendedFingerTable");
00074 numFingerCandidates = par("numFingerCandidates");
00075 proximityRouting = par("proximityRouting");
00076 memorizeFailedSuccessor = par("memorizeFailedSuccessor");
00077
00078
00079 mergeOptimizationL1 = par("mergeOptimizationL1");
00080 mergeOptimizationL2 = par("mergeOptimizationL2");
00081 mergeOptimizationL3 = par("mergeOptimizationL3");
00082 mergeOptimizationL4 = par("mergeOptimizationL4");
00083
00084 keyLength = OverlayKey::getLength();
00085 missingPredecessorStabRequests = 0;
00086
00087
00088 joinCount = 0;
00089 stabilizeCount = 0;
00090 fixfingersCount = 0;
00091 notifyCount = 0;
00092 newsuccessorhintCount = 0;
00093 joinBytesSent = 0;
00094 stabilizeBytesSent = 0;
00095 notifyBytesSent = 0;
00096 fixfingersBytesSent = 0;
00097 newsuccessorhintBytesSent = 0;
00098
00099 failedSuccessor = TransportAddress::UNSPECIFIED_NODE;
00100
00101
00102 findFriendModules();
00103
00104
00105 WATCH(predecessorNode);
00106 WATCH(thisNode);
00107 WATCH(bootstrapNode);
00108 WATCH(joinRetry);
00109 WATCH(missingPredecessorStabRequests);
00110
00111
00112 join_timer = new cMessage("join_timer");
00113 stabilize_timer = new cMessage("stabilize_timer");
00114 fixfingers_timer = new cMessage("fixfingers_timer");
00115 checkPredecessor_timer = new cMessage("checkPredecessor_timer");
00116 }
00117
00118
00119 Chord::~Chord()
00120 {
00121
00122 cancelAndDelete(join_timer);
00123 cancelAndDelete(stabilize_timer);
00124 cancelAndDelete(fixfingers_timer);
00125 cancelAndDelete(checkPredecessor_timer);
00126 }
00127
00128
00129
00130 void Chord::joinOverlay()
00131 {
00132 changeState(INIT);
00133 changeState(BOOTSTRAP);
00134 }
00135
00136
00137 void Chord::joinForeignPartition(const NodeHandle &node)
00138 {
00139 Enter_Method_Silent();
00140
00141
00142 JoinCall *call = new JoinCall("JoinCall");
00143 call->setBitLength(JOINCALL_L(call));
00144
00145 RoutingType routingType = (defaultRoutingType == FULL_RECURSIVE_ROUTING ||
00146 defaultRoutingType == RECURSIVE_SOURCE_ROUTING) ?
00147 SEMI_RECURSIVE_ROUTING : defaultRoutingType;
00148
00149 sendRouteRpcCall(OVERLAY_COMP, node, thisNode.key,
00150 call, NULL, routingType, joinDelay);
00151 }
00152
00153
00154 void Chord::changeState(int toState)
00155 {
00156
00157
00158
00159
00160 switch (toState) {
00161 case INIT:
00162 state = INIT;
00163
00164 setOverlayReady(false);
00165
00166 if (thisNode.key.isUnspecified()) {
00167 thisNode.key = OverlayKey::random();
00168 callUpdate(thisNode, true);
00169 }
00170
00171
00172 predecessorNode = NodeHandle::UNSPECIFIED_NODE;
00173
00174
00175 initializeFriendModules();
00176
00177 updateTooltip();
00178
00179
00180 if (debugOutput) {
00181 EV << "[Chord::changeState() @ " << thisNode.ip
00182 << " (" << thisNode.key.toString(16) << ")]\n"
00183 << " Entered INIT stage"
00184 << endl;
00185 }
00186
00187 getParentModule()->getParentModule()->bubble("Enter INIT state.");
00188 break;
00189
00190 case BOOTSTRAP:
00191 state = BOOTSTRAP;
00192
00193
00194 cancelEvent(join_timer);
00195
00196
00197 take(join_timer);
00198 scheduleAt(simTime(), join_timer);
00199
00200
00201 if (debugOutput) {
00202 EV << "[Chord::changeState() @ " << thisNode.ip
00203 << " (" << thisNode.key.toString(16) << ")]\n"
00204 << " Entered BOOTSTRAP stage"
00205 << endl;
00206 }
00207 getParentModule()->getParentModule()->bubble("Enter BOOTSTRAP state.");
00208
00209
00210 bootstrapNode = bootstrapList->getBootstrapNode();
00211
00212
00213 if (bootstrapNode.isUnspecified()) {
00214
00215 assert(predecessorNode.isUnspecified());
00216 bootstrapNode = thisNode;
00217 changeState(READY);
00218 updateTooltip();
00219 }
00220 break;
00221
00222 case READY:
00223 state = READY;
00224
00225 setOverlayReady(true);
00226
00227
00228 cancelEvent(stabilize_timer);
00229 scheduleAt(simTime() + stabilizeDelay, stabilize_timer);
00230
00231
00232 cancelEvent(fixfingers_timer);
00233 scheduleAt(simTime() + fixfingersDelay,
00234 fixfingers_timer);
00235
00236
00237 cancelEvent(checkPredecessor_timer);
00238 if (checkPredecessorDelay > 0) {
00239 scheduleAt(simTime() + checkPredecessorDelay,
00240 checkPredecessor_timer);
00241 }
00242
00243
00244 if (debugOutput) {
00245 EV << "[Chord::changeState() @ " << thisNode.ip
00246 << " (" << thisNode.key.toString(16) << ")]\n"
00247 << " Entered READY stage"
00248 << endl;
00249 }
00250 getParentModule()->getParentModule()->bubble("Enter READY state.");
00251 break;
00252 }
00253 }
00254
00255
00256 void Chord::handleTimerEvent(cMessage* msg)
00257 {
00258
00259 if (msg == join_timer) {
00260 handleJoinTimerExpired(msg);
00261 }
00262
00263 else if (msg == stabilize_timer) {
00264 handleStabilizeTimerExpired(msg);
00265 }
00266
00267 else if (msg == fixfingers_timer) {
00268 handleFixFingersTimerExpired(msg);
00269 }
00270
00271 else if (msg == checkPredecessor_timer) {
00272 cancelEvent(checkPredecessor_timer);
00273 scheduleAt(simTime() + checkPredecessorDelay,
00274 checkPredecessor_timer);
00275 if (!predecessorNode.isUnspecified()) pingNode(predecessorNode);
00276 }
00277
00278 else {
00279 error("Chord::handleTimerEvent(): received self message of "
00280 "unknown type!");
00281 }
00282 }
00283
00284
00285 void Chord::handleUDPMessage(BaseOverlayMessage* msg)
00286 {
00287 ChordMessage* chordMsg = check_and_cast<ChordMessage*>(msg);
00288 switch(chordMsg->getCommand()) {
00289 case NEWSUCCESSORHINT:
00290 handleNewSuccessorHint(chordMsg);
00291 break;
00292 default:
00293 error("handleUDPMessage(): Unknown message type!");
00294 break;
00295 }
00296
00297 delete chordMsg;
00298 }
00299
00300
00301 bool Chord::handleRpc(BaseCallMessage* msg)
00302 {
00303 if (state != READY) {
00304 EV << "[Chord::handleRpc() @ " << thisNode.ip
00305 << " (" << thisNode.key.toString(16) << ")]\n"
00306 << " Received RPC call and state != READY"
00307 << endl;
00308 return false;
00309 }
00310
00311
00312 RPC_SWITCH_START( msg )
00313
00314 RPC_DELEGATE( Join, rpcJoin );
00315 RPC_DELEGATE( Notify, rpcNotify );
00316 RPC_DELEGATE( Stabilize, rpcStabilize );
00317 RPC_DELEGATE( Fixfingers, rpcFixfingers );
00318 RPC_SWITCH_END( )
00319
00320 return RPC_HANDLED;
00321 }
00322
00323 void Chord::handleRpcResponse(BaseResponseMessage* msg,
00324 cPolymorphic* context, int rpcId,
00325 simtime_t rtt)
00326 {
00327 RPC_SWITCH_START(msg)
00328 RPC_ON_RESPONSE( Join ) {
00329 handleRpcJoinResponse(_JoinResponse);
00330 EV << "[Chord::handleRpcResponse() @ " << thisNode.ip
00331 << " (" << thisNode.key.toString(16) << ")]\n"
00332 << " Received a Join RPC Response: id=" << rpcId << "\n"
00333 << " msg=" << *_JoinResponse << " rtt=" << rtt
00334 << endl;
00335 break;
00336 }
00337 RPC_ON_RESPONSE( Notify ) {
00338 handleRpcNotifyResponse(_NotifyResponse);
00339 EV << "[Chord::handleRpcResponse() @ " << thisNode.ip
00340 << " (" << thisNode.key.toString(16) << ")]\n"
00341 << " Received a Notify RPC Response: id=" << rpcId << "\n"
00342 << " msg=" << *_NotifyResponse << " rtt=" << rtt
00343 << endl;
00344 break;
00345 }
00346 RPC_ON_RESPONSE( Stabilize ) {
00347 handleRpcStabilizeResponse(_StabilizeResponse);
00348 EV << "[Chord::handleRpcResponse() @ " << thisNode.ip
00349 << " (" << thisNode.key.toString(16) << ")]\n"
00350 << " Received a Stabilize RPC Response: id=" << rpcId << "\n"
00351 << " msg=" << *_StabilizeResponse << " rtt=" << rtt
00352 << endl;
00353 break;
00354 }
00355 RPC_ON_RESPONSE( Fixfingers ) {
00356 handleRpcFixfingersResponse(_FixfingersResponse, SIMTIME_DBL(rtt));
00357 EV << "[Chord::handleRpcResponse() @ " << thisNode.ip
00358 << " (" << thisNode.key.toString(16) << ")]\n"
00359 << " Received a Fixfingers RPC Response: id=" << rpcId << "\n"
00360 << " msg=" << *_FixfingersResponse << " rtt=" << rtt
00361 << endl;
00362 break;
00363 }
00364 RPC_SWITCH_END( )
00365 }
00366
00367 void Chord::handleRpcTimeout(BaseCallMessage* msg,
00368 const TransportAddress& dest,
00369 cPolymorphic* context, int rpcId,
00370 const OverlayKey&)
00371 {
00372 RPC_SWITCH_START(msg)
00373 RPC_ON_CALL( FindNode ) {
00374 EV << "[Chord::handleRpcTimeout() @ " << thisNode.ip
00375 << " (" << thisNode.key.toString(16) << ")]\n"
00376 << " FindNode RPC Call timed out: id=" << rpcId << "\n"
00377 << " msg=" << *_FindNodeCall
00378 << endl;
00379 break;
00380 }
00381 RPC_ON_CALL( Join ) {
00382 EV << "[Chord::handleRpcTimeout() @ " << thisNode.ip
00383 << " (" << thisNode.key.toString(16) << ")]\n"
00384 << " Join RPC Call timed out: id=" << rpcId << "\n"
00385 << " msg=" << *_JoinCall
00386 << endl;
00387 break;
00388 }
00389 RPC_ON_CALL( Notify ) {
00390 EV << "[Chord::handleRpcTimeout() @ " << thisNode.ip
00391 << " (" << thisNode.key.toString(16) << ")]\n"
00392 << " Notify RPC Call timed out: id=" << rpcId << "\n"
00393 << " msg=" << *_NotifyCall
00394 << endl;
00395 if (!handleFailedNode(dest)) joinOverlay();
00396 break;
00397 }
00398 RPC_ON_CALL( Stabilize ) {
00399 EV << "[Chord::handleRpcTimeout() @ " << thisNode.ip
00400 << " (" << thisNode.key.toString(16) << ")]\n"
00401 << " Stabilize RPC Call timed out: id=" << rpcId << "\n"
00402 << " msg=" << *_StabilizeCall
00403 << endl;
00404 if (!handleFailedNode(dest)) joinOverlay();
00405 break;
00406 }
00407 RPC_ON_CALL( Fixfingers ) {
00408 EV << "[Chord::handleRpcTimeout() @ " << thisNode.ip
00409 << " (" << thisNode.key.toString(16) << ")]\n"
00410 << " Fixfingers RPC Call timed out: id=" << rpcId << "\n"
00411 << " msg=" << *_FixfingersCall
00412 << endl;
00413 break;
00414 }
00415 RPC_SWITCH_END( )
00416 }
00417
00418 int Chord::getMaxNumSiblings()
00419 {
00420 return successorListSize;
00421 }
00422
00423 int Chord::getMaxNumRedundantNodes()
00424 {
00425 return extendedFingerTable ? numFingerCandidates : 1;
00426 }
00427
00428
00429 bool Chord::isSiblingFor(const NodeHandle& node,
00430 const OverlayKey& key,
00431 int numSiblings,
00432 bool* err)
00433 {
00434 if (key.isUnspecified())
00435 error("Chord::isSiblingFor(): key is unspecified!");
00436
00437 if (state != READY) {
00438 *err = true;
00439 return false;
00440 }
00441
00442 if (numSiblings > getMaxNumSiblings()) {
00443 opp_error("Chord::isSiblingFor(): numSiblings too big!");
00444 }
00445
00446 if (numSiblings == -1) numSiblings = getMaxNumSiblings();
00447
00448
00449 if ((predecessorNode.isUnspecified()) && (node == thisNode)) {
00450 if(successorList->isEmpty() || (node.key == key)) {
00451 *err = false;
00452 return true;
00453 } else {
00454 *err = true;
00455 return false;
00456 }
00457 }
00458
00459 if ((node == thisNode)
00460 && (key.isBetweenR(predecessorNode.key, thisNode.key))) {
00461
00462 *err = false;
00463 return true;
00464 }
00465
00466 NodeHandle prevNode = predecessorNode;
00467 NodeHandle curNode;
00468
00469 for (int i = -1; i < (int)successorList->getSize();
00470 i++, prevNode = curNode) {
00471
00472 if (i < 0) {
00473 curNode = thisNode;
00474 } else {
00475 curNode = successorList->getSuccessor(i);
00476 }
00477
00478 if (node == curNode) {
00479
00480 if (key.isBetweenR(prevNode.key, curNode.key)) {
00481 if (numSiblings <= ((int)successorList->getSize() - i)) {
00482 *err = false;
00483 return true;
00484 } else {
00485 *err = true;
00486 return false;
00487 }
00488 } else {
00489
00490
00491 if (numSiblings <= 1) {
00492 *err = false;
00493 return false;
00494 } else {
00495
00496
00497 *err = true;
00498 return false;
00499 }
00500 }
00501 }
00502 }
00503
00504
00505 *err = true;
00506 return false;
00507 }
00508
00509 bool Chord::handleFailedNode(const TransportAddress& failed)
00510 {
00511 if (!predecessorNode.isUnspecified() && failed == predecessorNode)
00512 predecessorNode = NodeHandle::UNSPECIFIED_NODE;
00513
00514
00515 TransportAddress oldSuccessor = successorList->getSuccessor();
00516
00517 if (successorList->handleFailedNode(failed))
00518 updateTooltip();
00519
00520 if (fingerTable != NULL)
00521 fingerTable->handleFailedNode(failed);
00522
00523
00524
00525
00526 if ((!predecessorNode.isUnspecified()) &&
00527 oldSuccessor == predecessorNode) {
00528 predecessorIsDead();
00529 callUpdate(predecessorNode, false);
00530 predecessorNode = NodeHandle::UNSPECIFIED_NODE;
00531 }
00532
00533 if (failed == oldSuccessor) {
00534 successorIsDead();
00535
00536 if (memorizeFailedSuccessor) {
00537 failedSuccessor = oldSuccessor;
00538 }
00539 cancelEvent(stabilize_timer);
00540 scheduleAt(simTime(), stabilize_timer);
00541 }
00542
00543 if (state != READY) return true;
00544
00545 return !(successorList->isEmpty());
00546 }
00547
00548 NodeVector* Chord::findNode(const OverlayKey& key,
00549 int numRedundantNodes,
00550 int numSiblings,
00551 BaseOverlayMessage* msg)
00552 {
00553 bool err;
00554 NodeVector* nextHop;
00555
00556 if (state != READY)
00557 return new NodeVector();
00558
00559 if (successorList->isEmpty() && !predecessorNode.isUnspecified()) {
00560 throw new cRuntimeError("Chord: Node is READY, has a "
00561 "predecessor but no successor!");
00562 joinOverlay();
00563 return new NodeVector();
00564 }
00565
00566
00567 if (key.isUnspecified()) {
00568 nextHop = new NodeVector();
00569 nextHop->push_back(thisNode);
00570 }
00571
00572
00573 else if (isSiblingFor(thisNode, key, 1, &err)) {
00574 nextHop = new NodeVector();
00575 nextHop->push_back(thisNode);
00576 for (uint32_t i = 0; i < successorList->getSize(); i++) {
00577 nextHop->push_back(successorList->getSuccessor(i));
00578 }
00579 nextHop->downsizeTo(numSiblings);
00580 }
00581
00582
00583 else if (key.isBetweenR(thisNode.key,
00584 successorList->getSuccessor().key)) {
00585 nextHop = new NodeVector();
00586 for (uint32_t i = 0; i < successorList->getSize(); i++) {
00587 nextHop->push_back(successorList->getSuccessor(i));
00588 }
00589 nextHop->downsizeTo(numRedundantNodes);
00590 }
00591
00592
00593 else {
00594 nextHop = closestPreceedingNode(key);
00595 nextHop->downsizeTo(numRedundantNodes);
00596 }
00597
00598 return nextHop;
00599 }
00600
00601
00602 NodeVector* Chord::closestPreceedingNode(const OverlayKey& key)
00603 {
00604 NodeHandle tempHandle = NodeHandle::UNSPECIFIED_NODE;
00605
00606
00607 for (int j = successorList->getSize() - 1; j >= 0; j--) {
00608 if(successorList->getSuccessor(j).key.isBetweenR(thisNode.key, key)) {
00609 tempHandle = successorList->getSuccessor(j);
00610 break;
00611 }
00612 }
00613
00614 if(tempHandle.isUnspecified()) {
00615 std::stringstream temp;
00616 temp << "Chord::closestPreceedingNode(): Successor list broken "
00617 << thisNode.key << " " << key;
00618 throw cRuntimeError(temp.str().c_str());
00619 }
00620
00621 NodeVector* nextHop;
00622
00623
00624
00625
00626
00627
00628 for (int i = fingerTable->getSize() - 1; i >= 0; i--) {
00629 if (fingerTable->getFinger(i).key.isBetweenLR(tempHandle.key, key)) {
00630 if(!extendedFingerTable) {
00631 nextHop = new NodeVector();
00632 nextHop->push_back(fingerTable->getFinger(i));
00633
00634 EV << "[Chord::closestPreceedingNode() @ " << thisNode.ip
00635 << " (" << thisNode.key.toString(16) << ")]\n"
00636 << " ClosestPreceedingNode: node " << thisNode
00637 << " for key " << key << "\n"
00638 << " finger " << fingerTable->getFinger(i).key
00639 << " better than \n"
00640 << " " << tempHandle.key
00641 << endl;
00642 return nextHop;
00643 } else {
00644 return fingerTable->getFinger(i, key);
00645 }
00646 }
00647 }
00648
00649 nextHop = new NodeVector();
00650 EV << "[Chord::closestPreceedingNode() @ " << thisNode.ip
00651 << " (" << thisNode.key.toString(16) << ")]\n"
00652 << " No finger found"
00653 << endl;
00654
00655
00656 for(int i = successorList->getSize() - 1; i >= 0
00657 && nextHop->size() <= numFingerCandidates ; i--) {
00658 if(successorList->getSuccessor(i).key.isBetween(thisNode.key, key)) {
00659 nextHop->push_back(successorList->getSuccessor(i));
00660 }
00661 }
00662
00663 if(nextHop->size() != 0)
00664 return nextHop;
00665
00666
00667 if ((predecessorNode.isUnspecified()) &&
00668 (successorList->getSuccessor() == thisNode)) {
00669 nextHop->push_back(thisNode);
00670 return nextHop;
00671 }
00672
00673
00674 std::stringstream temp("Error in Chord::closestPreceedingNode()!\n");
00675 temp << thisNode.key << " " << key;
00676 error(temp.str().c_str());
00677 nextHop->push_back(NodeHandle::UNSPECIFIED_NODE);
00678 return nextHop;
00679 }
00680
00681 void Chord::recordOverlaySentStats(BaseOverlayMessage* msg)
00682 {
00683 BaseOverlayMessage* innerMsg;
00684
00685 if (msg->getType() == OVERLAYROUTE)
00686 innerMsg = dynamic_cast<BaseOverlayMessage*>(msg->getEncapsulatedMsg());
00687 else
00688 innerMsg = msg;
00689
00690 switch (innerMsg->getType()) {
00691
00692 case OVERLAYSIGNALING: {
00693 ChordMessage* chordMsg = dynamic_cast<ChordMessage*>(innerMsg);
00694 switch(chordMsg->getCommand()) {
00695 case NEWSUCCESSORHINT:
00696 RECORD_STATS(newsuccessorhintCount++; newsuccessorhintBytesSent +=
00697 msg->getByteLength());
00698 break;
00699 }
00700 break;
00701 }
00702
00703 case RPC: {
00704 if ((dynamic_cast<StabilizeCall*>(innerMsg) != NULL) ||
00705 (dynamic_cast<StabilizeResponse*>(innerMsg) != NULL)) {
00706 RECORD_STATS(stabilizeCount++; stabilizeBytesSent +=
00707 msg->getByteLength());
00708 } else if ((dynamic_cast<NotifyCall*>(innerMsg) != NULL) ||
00709 (dynamic_cast<NotifyResponse*>(innerMsg) != NULL)) {
00710 RECORD_STATS(notifyCount++; notifyBytesSent +=
00711 msg->getByteLength());
00712 } else if ((dynamic_cast<FixfingersCall*>(innerMsg) != NULL) ||
00713 (dynamic_cast<FixfingersResponse*>(innerMsg) != NULL)) {
00714 RECORD_STATS(fixfingersCount++; fixfingersBytesSent +=
00715 msg->getByteLength());
00716 } else if ((dynamic_cast<JoinCall*>(innerMsg) != NULL) ||
00717 (dynamic_cast<JoinResponse*>(innerMsg) != NULL)) {
00718 RECORD_STATS(joinCount++; joinBytesSent += msg->getByteLength());
00719 }
00720 break;
00721 }
00722 }
00723 }
00724
00725
00726 void Chord::finishOverlay()
00727 {
00728
00729 bootstrapList->removeBootstrapNode(thisNode);
00730
00731 simtime_t time = globalStatistics->calcMeasuredLifetime(creationTime);
00732 if(time == 0) return;
00733
00734 globalStatistics->addStdDev("Chord: Sent JOIN Messages/s",
00735 joinCount / time);
00736 globalStatistics->addStdDev("Chord: Sent NEWSUCCESSORHINT Messages/s",
00737 newsuccessorhintCount / time);
00738 globalStatistics->addStdDev("Chord: Sent STABILIZE Messages/s",
00739 stabilizeCount / time);
00740 globalStatistics->addStdDev("Chord: Sent NOTIFY Messages/s",
00741 notifyCount / time);
00742 globalStatistics->addStdDev("Chord: Sent FIX_FINGERS Messages/s",
00743 fixfingersCount / time);
00744 globalStatistics->addStdDev("Chord: Sent JOIN Bytes/s",
00745 joinBytesSent / time);
00746 globalStatistics->addStdDev("Chord: Sent NEWSUCCESSORHINT Bytes/s",
00747 newsuccessorhintBytesSent / time);
00748 globalStatistics->addStdDev("Chord: Sent STABILIZE Bytes/s",
00749 stabilizeBytesSent / time);
00750 globalStatistics->addStdDev("Chord: Sent NOTIFY Bytes/s",
00751 notifyBytesSent / time);
00752 globalStatistics->addStdDev("Chord: Sent FIX_FINGERS Bytes/s",
00753 fixfingersBytesSent / time);
00754 }
00755
00756
00757
00758 void Chord::handleJoinTimerExpired(cMessage* msg)
00759 {
00760
00761 if (state == READY)
00762 return;
00763
00764
00765 if (state != BOOTSTRAP)
00766 changeState(BOOTSTRAP);
00767
00768
00769 joinRetry--;
00770 if (joinRetry == 0) {
00771 joinRetry = par("joinRetry");
00772 changeState(BOOTSTRAP);
00773 return;
00774 }
00775
00776
00777 JoinCall* call = new JoinCall("JoinCall");
00778 call->setBitLength(JOINCALL_L(call));
00779
00780 RoutingType routingType = (defaultRoutingType == FULL_RECURSIVE_ROUTING ||
00781 defaultRoutingType == RECURSIVE_SOURCE_ROUTING) ?
00782 SEMI_RECURSIVE_ROUTING : defaultRoutingType;
00783
00784 sendRouteRpcCall(OVERLAY_COMP, bootstrapNode, thisNode.key,
00785 call, NULL, routingType, joinDelay);
00786
00787
00788 cancelEvent(join_timer);
00789 scheduleAt(simTime() + joinDelay, msg);
00790 }
00791
00792
00793 void Chord::handleStabilizeTimerExpired(cMessage* msg)
00794 {
00795 if (state != READY)
00796 return;
00797
00798
00799 if ((checkPredecessorDelay == 0) &&
00800 (missingPredecessorStabRequests >= stabilizeRetry)) {
00801
00802
00803
00804 predecessorIsDead();
00805 callUpdate(predecessorNode, false);
00806 predecessorNode = NodeHandle::UNSPECIFIED_NODE;
00807 missingPredecessorStabRequests = 0;
00808 updateTooltip();
00809 }
00810
00811 if (!successorList->isEmpty()) {
00812
00813 StabilizeCall* call = new StabilizeCall("StabilizeCall");
00814 call->setBitLength(STABILIZECALL_L(call));
00815
00816 sendUdpRpcCall(successorList->getSuccessor(), call);
00817
00818 missingPredecessorStabRequests++;
00819 }
00820
00821
00822 if (mergeOptimizationL4) {
00823 OverlayKey offset;
00824 for (uint32_t nextFinger = 0; nextFinger < thisNode.key.getLength();
00825 nextFinger++) {
00826 offset = OverlayKey::pow2(nextFinger);
00827
00828
00829 if (offset > successorList->getSuccessor().key - thisNode.key) {
00830 if ((fingerTable->getFinger(nextFinger)).isUnspecified()) {
00831 continue;
00832 } else {
00833 pingNode(fingerTable->getFinger(nextFinger), -1, 0, NULL,
00834 NULL, NULL, nextFinger);
00835 }
00836 }
00837 }
00838 }
00839
00840
00841 cancelEvent(stabilize_timer);
00842 scheduleAt(simTime() + stabilizeDelay, msg);
00843 }
00844
00845
00846 void Chord::handleFixFingersTimerExpired(cMessage* msg)
00847 {
00848 if ((state != READY) || successorList->isEmpty())
00849 return;
00850
00851 OverlayKey offset, lookupKey;
00852 for (uint32_t nextFinger = 0; nextFinger < thisNode.key.getLength();
00853 nextFinger++) {
00854
00855 offset = OverlayKey::pow2(nextFinger);
00856 lookupKey = thisNode.key + offset;
00857
00858
00859 if (offset > successorList->getSuccessor().key - thisNode.key) {
00860
00861 FixfingersCall* call = new FixfingersCall("FixfingersCall");
00862 call->setFinger(nextFinger);
00863 call->setBitLength(FIXFINGERSCALL_L(call));
00864
00865 sendRouteRpcCall(OVERLAY_COMP, lookupKey, call, NULL,
00866 DEFAULT_ROUTING, fixfingersDelay);
00867 } else {
00868
00869 #if 0
00870
00871
00872 fingerTable->removeFinger(nextFinger);
00873 #else
00874 fingerTable->setFinger(nextFinger, successorList->getSuccessor());
00875 #endif
00876 }
00877 }
00878
00879
00880 cancelEvent(fixfingers_timer);
00881 scheduleAt(simTime() + fixfingersDelay, msg);
00882 }
00883
00884
00885 void Chord::handleNewSuccessorHint(ChordMessage* chordMsg)
00886 {
00887 NewSuccessorHintMessage* newSuccessorHintMsg =
00888 check_and_cast<NewSuccessorHintMessage*>(chordMsg);
00889
00890
00891 NodeHandle predecessor = newSuccessorHintMsg->getPreNode();
00892
00893
00894 if (predecessor.key.isBetween(thisNode.key,
00895 successorList->getSuccessor().key)
00896 || (thisNode.key == successorList->getSuccessor().key)) {
00897
00898 successorList->addSuccessor(predecessor);
00899 updateTooltip();
00900 }
00901
00902
00903
00904 if (mergeOptimizationL3) {
00905 if (successorList->getSuccessor() == predecessor) {
00906 StabilizeCall *call = new StabilizeCall("StabilizeCall");
00907 call->setBitLength(STABILIZECALL_L(call));
00908
00909 sendUdpRpcCall(predecessor, call);
00910 } else {
00911 if (successorList->getSuccessor() == newSuccessorHintMsg->
00912 getSrcNode()) {
00913
00914 StabilizeCall *call = new StabilizeCall("StabilizeCall");
00915 call->setBitLength(STABILIZECALL_L(call));
00916
00917 sendUdpRpcCall(predecessor, call);
00918 }
00919 }
00920 }
00921 }
00922
00923
00924 void Chord::rpcJoin(JoinCall* joinCall)
00925 {
00926 NodeHandle requestor = joinCall->getSrcNode();
00927
00928
00929 JoinResponse* joinResponse =
00930 new JoinResponse("JoinResponse");
00931
00932 int sucNum = successorList->getSize();
00933 joinResponse->setSucNum(sucNum);
00934 joinResponse->setSucNodeArraySize(sucNum);
00935
00936 for (int k = 0; k < sucNum; k++) {
00937 joinResponse->setSucNode(k, successorList->getSuccessor(k));
00938 }
00939
00940
00941 if (predecessorNode.isUnspecified() && successorList->isEmpty()) {
00942
00943 joinResponse->setPreNode(thisNode);
00944 } else {
00945 joinResponse->setPreNode(predecessorNode);
00946 }
00947
00948 joinResponse->setBitLength(JOINRESPONSE_L(joinResponse));
00949
00950 sendRpcResponse(joinCall, joinResponse);
00951
00952 if (aggressiveJoinMode) {
00953
00954
00955
00956
00957
00958
00959
00960
00961 if (!predecessorNode.isUnspecified()) {
00962 NewSuccessorHintMessage* newSuccessorHintMsg =
00963 new NewSuccessorHintMessage("NEWSUCCESSORHINT");
00964 newSuccessorHintMsg->setCommand(NEWSUCCESSORHINT);
00965
00966 newSuccessorHintMsg->setSrcNode(thisNode);
00967 newSuccessorHintMsg->setPreNode(requestor);
00968 newSuccessorHintMsg->
00969 setBitLength(NEWSUCCESSORHINT_L(newSuccessorHintMsg));
00970
00971 sendMessageToUDP(predecessorNode, newSuccessorHintMsg);
00972 }
00973
00974 if (predecessorNode.isUnspecified() || (predecessorNode != requestor)) {
00975
00976 if (!predecessorNode.isUnspecified()) {
00977 callUpdate(predecessorNode, false);
00978 }
00979 callUpdate(requestor, true);
00980
00981
00982 predecessorNode = requestor;
00983 }
00984 }
00985
00986
00987 if (successorList->isEmpty())
00988 successorList->addSuccessor(requestor);
00989
00990 updateTooltip();
00991 }
00992
00993 void Chord::handleRpcJoinResponse(JoinResponse* joinResponse)
00994 {
00995
00996 int sucNum = successorListSize - 1;
00997
00998 if (joinResponse->getSucNum() < successorListSize - 1) {
00999 sucNum = joinResponse->getSucNum();
01000 }
01001
01002
01003 for (int k = 0; k < sucNum; k++) {
01004 NodeHandle successor = joinResponse->getSucNode(k);
01005 successorList->addSuccessor(successor);
01006 }
01007
01008
01009
01010
01011
01012 successorList->addSuccessor(joinResponse->getSrcNode());
01013
01014
01015
01016 if (aggressiveJoinMode) {
01017
01018
01019 if (!joinResponse->getPreNode().isUnspecified()) {
01020 if (!predecessorNode.isUnspecified()) {
01021
01022
01023
01024 if (mergeOptimizationL2) {
01025 NewSuccessorHintMessage* newSuccessorHintMsg =
01026 new NewSuccessorHintMessage("NEWSUCCESSORHINT");
01027 newSuccessorHintMsg->setCommand(NEWSUCCESSORHINT);
01028 newSuccessorHintMsg->setSrcNode(thisNode);
01029 newSuccessorHintMsg->setPreNode(joinResponse->getPreNode());
01030 newSuccessorHintMsg->
01031 setBitLength(NEWSUCCESSORHINT_L(newSuccessorHintMsg));
01032
01033 sendMessageToUDP(predecessorNode, newSuccessorHintMsg);
01034 }
01035 }
01036
01037 if (!predecessorNode.isUnspecified()
01038 && !joinResponse->getPreNode().isUnspecified()
01039 && predecessorNode != joinResponse->getPreNode()) {
01040 callUpdate(predecessorNode, false);
01041 }
01042 predecessorNode = joinResponse->getPreNode();
01043 callUpdate(predecessorNode, true);
01044 }
01045 }
01046
01047 updateTooltip();
01048
01049 changeState(READY);
01050
01051
01052 cancelEvent(stabilize_timer);
01053 scheduleAt(simTime(), stabilize_timer);
01054
01055
01056 cancelEvent(fixfingers_timer);
01057 scheduleAt(simTime(), fixfingers_timer);
01058 }
01059
01060
01061 void Chord::rpcStabilize(StabilizeCall* call)
01062 {
01063
01064 if (!predecessorNode.isUnspecified() &&
01065 call->getSrcNode() == predecessorNode) {
01066 missingPredecessorStabRequests = 0;
01067 }
01068
01069
01070 StabilizeResponse* stabilizeResponse =
01071 new StabilizeResponse("StabilizeResponse");
01072 stabilizeResponse->setPreNode(predecessorNode);
01073 stabilizeResponse->setBitLength(STABILIZERESPONSE_L(stabilizeResponse));
01074
01075 sendRpcResponse(call, stabilizeResponse);
01076 }
01077
01078 void Chord::handleRpcStabilizeResponse(StabilizeResponse* stabilizeResponse)
01079 {
01080
01081 const NodeHandle& predecessor = stabilizeResponse->getPreNode();
01082
01083
01084 if ((successorList->isEmpty() ||
01085 predecessor.key.isBetween(thisNode.key,
01086 successorList->getSuccessor().key)) &&
01087 (failedSuccessor.isUnspecified() || failedSuccessor != predecessor)) {
01088
01089 successorList->addSuccessor(predecessor);
01090 updateTooltip();
01091 }
01092
01093
01094 NotifyCall* notifyCall = new NotifyCall("NotifyCall");
01095 notifyCall->setBitLength(NOTIFYCALL_L(notifyCall));
01096 notifyCall->setFailed(failedSuccessor);
01097 failedSuccessor = TransportAddress::UNSPECIFIED_NODE;
01098
01099 sendUdpRpcCall(successorList->getSuccessor(), notifyCall);
01100 }
01101
01102 void Chord::rpcNotify(NotifyCall* call)
01103 {
01104
01105 if (!predecessorNode.isUnspecified() &&
01106 call->getSrcNode() == predecessorNode) {
01107 missingPredecessorStabRequests = 0;
01108 }
01109
01110 bool newPredecessorSet = false;
01111
01112 NodeHandle newPredecessor = call->getSrcNode();
01113
01114
01115 if (predecessorNode.isUnspecified() ||
01116 newPredecessor.key.isBetween(predecessorNode.key, thisNode.key) ||
01117 (!call->getFailed().isUnspecified() &&
01118 call->getFailed() == predecessorNode)) {
01119
01120 if ((predecessorNode.isUnspecified()) ||
01121 (newPredecessor != predecessorNode)) {
01122
01123 if (!predecessorNode.isUnspecified()) {
01124 callUpdate(predecessorNode, false);
01125 }
01126 callUpdate(newPredecessor, true);
01127
01128
01129 if (mergeOptimizationL1) {
01130 if (!predecessorNode.isUnspecified()) {
01131 NewSuccessorHintMessage *newSuccessorHintMsg =
01132 new NewSuccessorHintMessage("NEWSUCCESSORHINT");
01133 newSuccessorHintMsg->setCommand(NEWSUCCESSORHINT);
01134
01135 newSuccessorHintMsg->setSrcNode(thisNode);
01136 newSuccessorHintMsg->setPreNode(newPredecessor);
01137 newSuccessorHintMsg->
01138 setBitLength(NEWSUCCESSORHINT_L(newSuccessorHintMsg));
01139 sendMessageToUDP(predecessorNode, newSuccessorHintMsg);
01140 }
01141 }
01142
01143 predecessorNode = newPredecessor;
01144 if (successorList->isEmpty()) {
01145 successorList->addSuccessor(newPredecessor);
01146 }
01147 updateTooltip();
01148
01149 newPredecessorSet = true;
01150 }
01151 }
01152
01153
01154 NotifyResponse* notifyResponse = new NotifyResponse("NotifyResponse");
01155
01156 int sucNum = successorList->getSize();
01157 notifyResponse->setSucNum(sucNum);
01158 notifyResponse->setSucNodeArraySize(sucNum);
01159
01160
01161
01162 if (mergeOptimizationL3) {
01163 if (!newPredecessorSet && (predecessorNode != newPredecessor)) {
01164
01165 notifyResponse->setPreNode(predecessorNode);
01166 notifyResponse->setPreNodeSet(false);
01167 } else {
01168 notifyResponse->setPreNodeSet(true);
01169 }
01170 }
01171
01172 for (int k = 0; k < sucNum; k++) {
01173 notifyResponse->setSucNode(k, successorList->getSuccessor(k));
01174 }
01175
01176 notifyResponse->setBitLength(NOTIFYRESPONSE_L(notifyResponse));
01177
01178 sendRpcResponse(call, notifyResponse);
01179 }
01180
01181
01182 void Chord::handleRpcNotifyResponse(NotifyResponse* notifyResponse)
01183 {
01184 if (successorList->getSuccessor() != notifyResponse->getSrcNode()) {
01185 EV << "[Chord::handleRpcNotifyResponse() @ " << thisNode.ip
01186 << " (" << thisNode.key.toString(16) << ")]\n"
01187 << " The srcNode of the received NotifyResponse is not our "
01188 << " current successor"
01189 << endl;
01190 return;
01191 }
01192
01193
01194
01195 if (mergeOptimizationL3) {
01196 if (!notifyResponse->getPreNodeSet()) {
01197 StabilizeCall *call = new StabilizeCall("StabilizeCall");
01198 call->setBitLength(STABILIZECALL_L(call));
01199
01200 successorList->addSuccessor(notifyResponse->getPreNode());
01201 if (successorList->getSuccessor() == notifyResponse->getPreNode())
01202 sendUdpRpcCall(notifyResponse->getPreNode(), call);
01203 return;
01204 }
01205 }
01206
01207
01208 successorList->updateList(notifyResponse);
01209
01210 updateTooltip();
01211 }
01212
01213
01214 void Chord::rpcFixfingers(FixfingersCall* call)
01215 {
01216 FixfingersResponse* fixfingersResponse =
01217 new FixfingersResponse("FixfingersResponse");
01218
01219 fixfingersResponse->setSucNodeArraySize(1);
01220 fixfingersResponse->setSucNode(0, thisNode);
01221
01222 if (extendedFingerTable) {
01223 fixfingersResponse->setSucNodeArraySize(((successorList->getSize() + 1
01224 < numFingerCandidates + 1)
01225 ? successorList->getSize() + 1
01226 : numFingerCandidates + 1));
01227 for (unsigned int i = 0;
01228 i < (((successorList->getSize()) < numFingerCandidates)
01229 ? (successorList->getSize()) : numFingerCandidates); i++) {
01230
01231 assert(!successorList->getSuccessor(i).isUnspecified());
01232 fixfingersResponse->setSucNode(i + 1,
01233 successorList->getSuccessor(i));
01234 }
01235 }
01236 fixfingersResponse->setFinger(call->getFinger());
01237 fixfingersResponse->setBitLength(FIXFINGERSRESPONSE_L(fixfingersResponse));
01238
01239 sendRpcResponse(call, fixfingersResponse);
01240 }
01241
01242
01243 void Chord::handleRpcFixfingersResponse(FixfingersResponse* fixfingersResponse,
01244 double rtt)
01245 {
01246
01247 if (!extendedFingerTable) {
01248 fingerTable->setFinger(fixfingersResponse->getFinger(),
01249 fixfingersResponse->getSucNode(0));
01250 } else {
01251 Successors successors;
01252 for (unsigned int i = 0; i < fixfingersResponse->getSucNodeArraySize();
01253 i++) {
01254 if (fixfingersResponse->getSucNode(i).isUnspecified())
01255 continue;
01256 if (fixfingersResponse->getSucNode(i) == thisNode)
01257 break;
01258 successors.insert(std::make_pair(MAXTIME,
01259 fixfingersResponse->getSucNode(i)));
01260 }
01261
01262 if (successors.size() == 0) {
01263 return;
01264 }
01265
01266 fingerTable->setFinger(fixfingersResponse->getFinger(), successors);
01267
01268 #if 0
01269 if (proximityRouting || globalParameters->getTopologyAdaptation()) {
01270 #else
01271 if (proximityRouting) {
01272 #endif
01273 for (unsigned int i = 0;
01274 i < fixfingersResponse->getSucNodeArraySize();
01275 i++) {
01276 if (fixfingersResponse->getSucNode(i).isUnspecified())
01277 continue;
01278 if (fixfingersResponse->getSucNode(i) == thisNode)
01279 break;
01280 pingNode(fixfingersResponse->getSucNode(i), -1, 0, NULL,
01281 NULL, NULL, fixfingersResponse->getFinger(),
01282 INVALID_TRANSPORT, false);
01283 }
01284 }
01285 }
01286 }
01287
01288
01289 void Chord::pingResponse(PingResponse* pingResponse, cPolymorphic* context,
01290 int rpcId, simtime_t rtt)
01291 {
01292 EV << "[Chord::pingResponse() @ " << thisNode.ip
01293 << " (" << thisNode.key.toString(16) << ")]\n"
01294 << " Received a Ping RPC Response: id=" << rpcId << "\n"
01295 << " msg=" << *pingResponse << " rtt=" << rtt
01296 << endl;
01297
01298 if (rpcId != -1)
01299 fingerTable->updateFinger(rpcId, pingResponse->getSrcNode(), rtt);
01300 }
01301
01302 void Chord::pingTimeout(PingCall* pingCall,
01303 const TransportAddress& dest,
01304 cPolymorphic* context, int rpcId)
01305 {
01306 EV << "[Chord::pingTimeout() @ " << thisNode.ip
01307 << " (" << thisNode.key.toString(16) << ")]\n"
01308 << " Ping RPC timeout: id=" << rpcId << endl;
01309
01310 if (!predecessorNode.isUnspecified() && dest == predecessorNode) {
01311 predecessorIsDead();
01312 callUpdate(predecessorNode, false);
01313 predecessorNode = NodeHandle::UNSPECIFIED_NODE;
01314 updateTooltip();
01315 }
01316 if (rpcId != -1 && dest == fingerTable->getFinger(rpcId))
01317 fingerTable->removeFinger(rpcId);
01318 }
01319
01320 void Chord::findFriendModules()
01321 {
01322 fingerTable = check_and_cast<ChordFingerTable*>
01323 (getParentModule()->getSubmodule("fingerTable"));
01324
01325 successorList = check_and_cast<ChordSuccessorList*>
01326 (getParentModule()->getSubmodule("successorList"));
01327 }
01328
01329
01330 void Chord::initializeFriendModules()
01331 {
01332
01333 fingerTable->initializeTable(thisNode.key.getLength(), thisNode, this);
01334
01335
01336 successorList->initializeList(par("successorListSize"), thisNode, this);
01337 }
01338
01339
01340 void Chord::updateTooltip()
01341 {
01342 if (ev.isGUI()) {
01343 std::stringstream ttString;
01344
01345
01346 ttString << predecessorNode << endl << thisNode << endl
01347 << successorList->getSuccessor();
01348
01349 getParentModule()->getParentModule()->getDisplayString().
01350 setTagArg("tt", 0, ttString.str().c_str());
01351 getParentModule()->getDisplayString().
01352 setTagArg("tt", 0, ttString.str().c_str());
01353 getDisplayString().setTagArg("tt", 0, ttString.str().c_str());
01354
01355
01356 showOverlayNeighborArrow(successorList->getSuccessor(), true,
01357 "m=m,50,0,50,0;ls=red,1");
01358 showOverlayNeighborArrow(predecessorNode, false,
01359 "m=m,50,100,50,100;ls=green,1");
01360 }
01361 }
01362
01363 OverlayKey Chord::distance(const OverlayKey& x, const OverlayKey& y) const
01364 {
01365 return KeyUniRingMetric().distance(x, y);
01366 }
01367
01368 void Chord::predecessorIsDead()
01369 {}
01370
01371 void Chord::successorIsDead()
01372 {}
01373
01374 };