DHT.cc

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2007 Institut fuer Telematik, Universitaet Karlsruhe (TH)
00003 //
00004 // This program is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU General Public License
00006 // as published by the Free Software Foundation; either version 2
00007 // of the License, or (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with this program; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 //
00018 
00024 #include <IPAddressResolver.h>
00025 
00026 #include "DHT.h"
00027 
00028 #include <RpcMacros.h>
00029 #include <BaseRpc.h>
00030 #include <GlobalStatistics.h>
00031 
00032 Define_Module(DHT);
00033 
00034 using namespace std;
00035 
00036 DHT::DHT()
00037 {
00038     dataStorage = NULL;
00039 }
00040 
00041 DHT::~DHT()
00042 {
00043     std::map<unsigned int, BaseCallMessage*>::iterator it;
00044 
00045     for (it = rpcIdMap.begin(); it != rpcIdMap.end(); it++) {
00046         cancelAndDelete(it->second);
00047         it->second = NULL;
00048     }
00049 
00050     std::map<int, GetMapEntry>::iterator it2;
00051 
00052     for (it2 = getMap.begin(); it2 != getMap.end(); it2++) {
00053         cancelAndDelete(it2->second.callMsg);
00054         it2->second.callMsg = NULL;
00055     }
00056 
00057     std::map<int, PutMapEntry>::iterator it3;
00058 
00059     for (it3 = putMap.begin(); it3 != putMap.end(); it3++) {
00060         cancelAndDelete(it3->second.callMsg);
00061         it3->second.callMsg = NULL;
00062     }
00063 
00064     rpcIdMap.clear();
00065     getMap.clear();
00066     putMap.clear();
00067 
00068     if (dataStorage != NULL) {
00069         dataStorage->clear();
00070     }
00071 }
00072 
00073 void DHT::initializeApp(int stage)
00074 {
00075     if (stage != MIN_STAGE_APP)
00076         return;
00077 
00078     dataStorage = check_and_cast<DHTDataStorage*>
00079                       (getParentModule()->getSubmodule("dhtDataStorage"));
00080 
00081     numReplica = par("numReplica");
00082     numGetRequests = par("numGetRequests");
00083     ratioIdentical = par("ratioIdentical");
00084 
00085     if (numReplica > overlay->getMaxNumSiblings()) {
00086         opp_error("DHT::initialize(): numReplica bigger than what this "
00087                   "overlay can handle (%d)", overlay->getMaxNumSiblings());
00088     }
00089 
00090     maintenanceMessages = 0;
00091     normalMessages = 0;
00092     numBytesMaintenance = 0;
00093     numBytesNormal = 0;
00094     WATCH(maintenanceMessages);
00095     WATCH(normalMessages);
00096     WATCH(numBytesNormal);
00097     WATCH(numBytesMaintenance);
00098     WATCH_MAP(rpcIdMap);
00099 }
00100 
00101 void DHT::handleTimerEvent(cMessage* msg)
00102 {
00103     DHTTtlTimer* msg_timer;
00104 
00105     if (msg->isName("ttl_timer")) {
00106         msg_timer = check_and_cast<DHTTtlTimer*> (msg);
00107 
00108         EV << "[DHT::handleTimerEvent()]\n"
00109            << "    received timer ttl, key: "
00110            << msg_timer->getKey().toString(16)
00111            << "\n (overlay->getThisNode().key = "
00112            << overlay->getThisNode().key.toString(16) << ")"
00113            << endl;
00114 
00115         dataStorage->removeData(msg_timer->getKey());
00116         delete msg_timer;
00117     }
00118 }
00119 
00120 bool DHT::handleRpc(BaseCallMessage* msg)
00121 {
00122     // delegate messages
00123     RPC_SWITCH_START( msg )
00124         // RPC_DELEGATE( <messageName>[Call|Response], <methodToCall> )
00125         RPC_DELEGATE( DHTPut, handlePutRequest );
00126         RPC_DELEGATE( DHTGet, handleGetRequest );
00127         RPC_DELEGATE( DHTputCAPI, handlePutCAPIRequest ); //requests coming from an upper tier
00128         RPC_DELEGATE( DHTgetCAPI, handleGetCAPIRequest );
00129         RPC_DELEGATE( DHTdump, handleDumpDhtRequest );
00130     RPC_SWITCH_END( )
00131 
00132     return RPC_HANDLED;
00133 }
00134 
00135 void DHT::handleRpcResponse(BaseResponseMessage* msg, cPolymorphic* context,
00136                             int rpcId, simtime_t rtt)
00137 {
00138     RPC_SWITCH_START(msg)
00139         RPC_ON_RESPONSE(DHTPut){
00140         handlePutResponse(_DHTPutResponse, rpcId);
00141         EV << "[DHT::handleRpcResponse()]\n"
00142            << "    DHT Put RPC Response received: id=" << rpcId
00143            << " msg=" << *_DHTPutResponse << " rtt=" << rtt
00144            << endl;
00145         break;
00146     }
00147     RPC_ON_RESPONSE(DHTGet) {
00148         handleGetResponse(_DHTGetResponse, rpcId);
00149         EV << "[DHT::handleRpcResponse()]\n"
00150            << "    DHT Get RPC Response received: id=" << rpcId
00151            << " msg=" << *_DHTGetResponse << " rtt=" << rtt
00152            << endl;
00153         break;
00154     }
00155     RPC_ON_RESPONSE(Lookup) {
00156         handleLookupResponse(_LookupResponse);
00157         EV << "[DHT::handleRpcResponse()]\n"
00158            << "    Replica Set RPC Response received: id=" << rpcId
00159            << " msg=" << *_LookupResponse << " rtt=" << rtt
00160            << endl;
00161         break;
00162     }
00163     RPC_SWITCH_END()
00164 }
00165 
00166 void DHT::handleRpcTimeout(BaseCallMessage* msg, const TransportAddress& dest,
00167                            cPolymorphic* context, int rpcId,
00168                            const OverlayKey& destKey)
00169 {
00170 
00171     RPC_SWITCH_START(msg)
00172     RPC_ON_CALL(DHTPut){
00173         EV << "[DHT::handleRpcResponse()]\n"
00174            << "    DHTPut Timeout"
00175            << endl;
00176 
00177         std::map<int, PutMapEntry>::iterator it2 =
00178                 putMap.find(rpcId);
00179 
00180         if (it2 == putMap.end()) //unknown request
00181             return;
00182 
00183         it2->second.numFailed++;
00184 
00185         if (it2->second.numFailed / (double)it2->second.numSent >= 0.5) {
00186             DHTputCAPIResponse* capiPutRespMsg = new DHTputCAPIResponse();
00187             capiPutRespMsg->setKey(_DHTPutCall->getKey());
00188             capiPutRespMsg->setIsSuccess(false);
00189             sendRpcResponse(it2->second.callMsg, capiPutRespMsg);
00190             it2->second.callMsg = NULL;
00191             putMap.erase(rpcId);
00192         }
00193 
00194         break;
00195     }
00196     RPC_ON_CALL(DHTGet) {
00197         EV << "[DHT::handleRpcResponse()]\n"
00198            << "    DHTGet Timeout"
00199            << endl;
00200 
00201         std::map<unsigned int, BaseCallMessage*>::iterator it =
00202             rpcIdMap.find(_DHTGetCall->getNonce());
00203         std::map<int, GetMapEntry>::iterator it2 =
00204             getMap.find(rpcId);
00205 
00206         if (it2 == getMap.end()) { //unknown request
00207             return;
00208         }
00209 
00210         if (it != rpcIdMap.end()) {
00211             //we have sent a 'real' get request
00212             rpcIdMap.erase(_DHTGetCall->getNonce());
00213             //ask anyone else, if possible
00214 
00215             if ((it2->second.hashVector != NULL)
00216                 && (it2->second.hashVector->size()> 0)) {
00217 
00218                 DHTGetCall* getCall = new DHTGetCall();
00219                 getCall->setKey(_DHTGetCall->getKey());
00220                 getCall->setIsHash(false);
00221                 getCall->setBitLength(GETCALL_L(getCall));
00222                 RECORD_STATS(normalMessages++;
00223                              numBytesNormal += getCall->getByteLength());
00224 
00225                 int nonce = sendRouteRpcCall(TIER1_COMP,
00226                                              it2->second.hashVector->back(),
00227                                              getCall, NULL, DEFAULT_ROUTING,
00228                                              -1, 0, rpcId);
00229                 rpcIdMap.insert(make_pair(nonce, (BaseCallMessage*)NULL));
00230                 it2->second.hashVector->pop_back();
00231             } else {
00232                 //no one else
00233                 DHTgetCAPIResponse* capiGetRespMsg = new DHTgetCAPIResponse();
00234                 capiGetRespMsg->setKey(_DHTGetCall->getKey());
00235                 capiGetRespMsg->setValue(BinaryValue::UNSPECIFIED_VALUE);
00236                 capiGetRespMsg->setIsSuccess(false);
00237                 sendRpcResponse(it2->second.callMsg,
00238                                 capiGetRespMsg);
00239                 //cout << "DHT: GET failed: no one else" << endl;
00240                 getMap.erase(rpcId);
00241                 return;
00242             }
00243 
00244         } else {
00245             //try to ask another one of the replica list for the hash
00246             if (it2->second.replica.size() > 0) {
00247                 DHTGetCall* getCall = new DHTGetCall();
00248                 getCall->setKey(_DHTGetCall->getKey());
00249                 getCall->setIsHash(true);
00250                 getCall->setBitLength(GETCALL_L(getCall));
00251 
00252                 RECORD_STATS(normalMessages++;
00253                              numBytesNormal += getCall->getByteLength());
00254 
00255                 sendRouteRpcCall(TIER1_COMP, it2->second.replica.back(),
00256                                  getCall, NULL, DEFAULT_ROUTING, -1, 0,
00257                                  rpcId);
00258                 it2->second.replica.pop_back();
00259             } else {
00260                 //no one else to ask, see what we can do with what we have
00261                 if (it2->second.numResponses > 0) {
00262                     unsigned int maxCount = 0;
00263                     ReplicaVector* hashVector = NULL;
00264                     std::map<BinaryValue, ReplicaVector>::iterator itHashes;
00265                     for (itHashes = it2->second.hashes.begin();
00266                          itHashes != it2->second.hashes.end(); itHashes++) {
00267 
00268                         if (itHashes->second.size()> maxCount) {
00269                             maxCount = itHashes->second.size();
00270                             hashVector = &(itHashes->second);
00271                         }
00272                     }
00273 
00274                     if ((double)maxCount/(double)it2->second.numResponses >= ratioIdentical) {
00275                         it2->second.hashVector = hashVector;
00276                     }
00277                 }
00278 
00279                 if ((it2->second.hashVector != NULL)
00280                      && (it2->second.hashVector->size()> 0)) {
00281 
00282                     DHTGetCall* getCall = new DHTGetCall();
00283                     getCall->setKey(_DHTGetCall->getKey());
00284                     getCall->setIsHash(false);
00285                     getCall->setBitLength(GETCALL_L(getCall));
00286                     RECORD_STATS(normalMessages++;
00287                                  numBytesNormal += getCall->getByteLength());
00288                     int nonce = sendRouteRpcCall(TIER1_COMP,
00289                                                  it2->second.hashVector->back(),
00290                                                  getCall, NULL, DEFAULT_ROUTING,
00291                                                  -1, 0, rpcId);
00292                     rpcIdMap.insert(make_pair(nonce, (BaseCallMessage*)NULL));
00293                     it2->second.hashVector->pop_back();
00294                 } else {
00295                     DHTgetCAPIResponse* capiGetRespMsg = new DHTgetCAPIResponse();
00296                     capiGetRespMsg->setKey(_DHTGetCall->getKey());
00297                     capiGetRespMsg->setIsSuccess(false);
00298                     sendRpcResponse(it2->second.callMsg, capiGetRespMsg);
00299                     //cout << "DHT: GET failed: timeout (no one else)" << endl;
00300                     getMap.erase(rpcId);
00301                 }
00302             }
00303         }
00304         break;
00305     }
00306     RPC_SWITCH_END( )
00307 }
00308 
00309 void DHT::handleUpperMessage(cMessage* msg)
00310 {
00311     error("DHT::handleUpperMessage(): Received message with unknown type!");
00312 
00313     delete msg;
00314 }
00315 
00316 void DHT::handlePutRequest(DHTPutCall* dhtMsg)
00317 {
00318     std::string tempString = "PUT_REQUEST received: "
00319             + std::string(dhtMsg->getKey().toString(16));
00320     getParentModule()->getParentModule()->bubble(tempString.c_str());
00321 
00322     if (!(dataStorage->isModifiable(dhtMsg->getKey()))) {
00323         //check if the put request came from the right node
00324         NodeHandle sourceNode = dataStorage->getSourceNode(dhtMsg->getKey());
00325         if (((!sourceNode.isUnspecified())
00326                 && (!dhtMsg->getSrcNode().isUnspecified()) && (sourceNode
00327                 != dhtMsg->getSrcNode())) || ((dhtMsg->getMaintenance())
00328                 && (dhtMsg->getOwnerNode() == sourceNode))) {
00329             // TODO: set owner
00330             DHTPutResponse* responseMsg = new DHTPutResponse();
00331             responseMsg->setKey(dhtMsg->getKey());
00332             tempString = "Error, not allowed to modify this key";
00333             responseMsg->setValue(BinaryValue(tempString));
00334             responseMsg->setBitLength(PUTRESPONSE_L(responseMsg));
00335             RECORD_STATS(normalMessages++;
00336                          numBytesNormal += responseMsg->getByteLength());
00337             sendRpcResponse(dhtMsg, responseMsg);
00338             return;
00339         }
00340 
00341     }
00342 
00343     // remove data item from local data storage
00344     cancelAndDelete(dataStorage->getTtlMessage(dhtMsg->getKey()));
00345     dataStorage->removeData(dhtMsg->getKey());
00346     if (dhtMsg->getValue().size() > 0) {
00347         // add ttl timer
00348         DHTTtlTimer *timerMsg = new DHTTtlTimer("ttl_timer");
00349         timerMsg->setKey(dhtMsg->getKey());
00350         scheduleAt(simTime() + dhtMsg->getTtl(), timerMsg);
00351         // storage data item in local data storage
00352         bool err;
00353         dataStorage->addData(dhtMsg->getKey(), dhtMsg->getValue(), timerMsg,
00354                              dhtMsg->getIsModifiable(), dhtMsg->getSrcNode(),
00355                              overlay->isSiblingFor(overlay->getThisNode(),
00356                                                    dhtMsg->getKey(),
00357                                                    1, &err));
00358     }
00359 
00360     // send back
00361     DHTPutResponse* responseMsg = new DHTPutResponse();
00362     responseMsg->setKey(dhtMsg->getKey());
00363 
00364     responseMsg->setValue(dhtMsg->getValue());
00365     responseMsg->setBitLength(PUTRESPONSE_L(responseMsg));
00366     RECORD_STATS(normalMessages++; numBytesNormal += responseMsg->getByteLength());
00367 
00368     sendRpcResponse(dhtMsg, responseMsg);
00369 }
00370 
00371 void DHT::handleGetRequest(DHTGetCall* dhtMsg)
00372 {
00373     std::string tempString = "GET_REQUEST received: "
00374             + std::string(dhtMsg->getKey().toString(16));
00375 
00376     getParentModule()->getParentModule()->bubble(tempString.c_str());
00377 
00378     BinaryValue storedValue = dataStorage->getData(dhtMsg->getKey());
00379 
00380     // send back
00381     DHTGetResponse* responseMsg = new DHTGetResponse();
00382     responseMsg->setKey(dhtMsg->getKey());
00383     responseMsg->setIsHash(dhtMsg->getIsHash());
00384 
00385     if (storedValue.isUnspecified()) {
00386         responseMsg->setValue(BinaryValue::UNSPECIFIED_VALUE);
00387     } else {
00388         if (dhtMsg->getIsHash()) {
00389             CSHA1 sha1;
00390             char temp[41];
00391             temp[0] = '\0';
00392             sha1.Reset();
00393             sha1.Update((uint8_t*) (&(*storedValue.begin())),
00394                         storedValue.size());
00395             sha1.Final();
00396             sha1.ReportHash(temp, CSHA1::REPORT_HEX);
00397 
00398             responseMsg->setValue(BinaryValue((char*) temp));
00399         } else {
00400             responseMsg->setValue(storedValue);
00401         }
00402     }
00403 
00404     responseMsg->setBitLength(GETRESPONSE_L(responseMsg));
00405     RECORD_STATS(normalMessages++;
00406                  numBytesNormal += responseMsg->getByteLength());
00407     sendRpcResponse(dhtMsg, responseMsg);
00408 }
00409 
00410 void DHT::handlePutCAPIRequest(DHTputCAPICall* capiPutMsg)
00411 {
00412     //asks the replica list
00413     LookupCall* replicaMsg = new LookupCall();
00414     replicaMsg->setKey(capiPutMsg->getKey());
00415     replicaMsg->setNumSiblings(numReplica);
00416     int nonce = sendInternalRpcCall(OVERLAY_COMP, replicaMsg);
00417     rpcIdMap.insert(make_pair(nonce, capiPutMsg));
00418 }
00419 
00420 void DHT::handleGetCAPIRequest(DHTgetCAPICall* capiGetMsg)
00421 {
00422     LookupCall* replicaMsg = new LookupCall();
00423     replicaMsg->setKey(capiGetMsg->getKey());
00424     replicaMsg->setNumSiblings(numReplica);
00425     int nonce = sendInternalRpcCall(OVERLAY_COMP, replicaMsg);
00426     rpcIdMap.insert(make_pair(nonce, capiGetMsg));
00427     lastGetCall = simTime();
00428 }
00429 
00430 void DHT::handleDumpDhtRequest(DHTdumpCall* call)
00431 {
00432     DHTdumpResponse* response = new DHTdumpResponse();
00433     DhtDumpVector* dumpVector = dataStorage->dumpDht();
00434 
00435     response->setRecordArraySize(dumpVector->size());
00436 
00437     for (uint32_t i = 0; i < dumpVector->size(); i++) {
00438         response->setRecord(i, (*dumpVector)[i]);
00439     }
00440 
00441     delete dumpVector;
00442 
00443     sendRpcResponse(call, response);
00444 }
00445 
00446 void DHT::handlePutResponse(DHTPutResponse* dhtMsg, int rpcId)
00447 {
00448     std::map<int, PutMapEntry>::iterator it2 =
00449             putMap.find(rpcId);
00450 
00451     if (it2 == putMap.end()) //unknown request
00452         return;
00453 
00454     it2->second.numResponses++;
00455 
00456     if (it2->second.numResponses / (double)it2->second.numSent > 0.5) {
00457         DHTputCAPIResponse* capiPutRespMsg = new DHTputCAPIResponse();
00458         capiPutRespMsg->setKey(dhtMsg->getKey());
00459         capiPutRespMsg->setValue(dhtMsg->getValue());
00460         capiPutRespMsg->setIsSuccess(true);
00461         sendRpcResponse(it2->second.callMsg, capiPutRespMsg);
00462         it2->second.callMsg = NULL;
00463         putMap.erase(rpcId);
00464     }
00465 }
00466 
00467 void DHT::handleGetResponse(DHTGetResponse* dhtMsg, int rpcId)
00468 {
00469     std::map<unsigned int, BaseCallMessage*>::iterator it =
00470             rpcIdMap.find(dhtMsg->getNonce());
00471     std::map<int, GetMapEntry>::iterator it2 =
00472             getMap.find(rpcId);
00473 
00474     if (it2 == getMap.end()) //unknown request
00475         return;
00476 
00477     if (it != rpcIdMap.end()) {
00478         //we have sent a 'real' get request
00479         rpcIdMap.erase(dhtMsg->getNonce());
00480         if (!dhtMsg->getIsHash()) {
00481             DHTgetCAPIResponse* capiGetRespMsg = new DHTgetCAPIResponse();
00482             capiGetRespMsg->setKey(dhtMsg->getKey());
00483             capiGetRespMsg->setValue(dhtMsg->getValue());
00484             capiGetRespMsg->setIsSuccess(true);
00485             sendRpcResponse(it2->second.callMsg, capiGetRespMsg);
00486             getMap.erase(rpcId);
00487             return;
00488         }
00489     } else {
00490         if (dhtMsg->getIsHash()) {
00491             std::map<BinaryValue, ReplicaVector>::iterator itHashes =
00492                     it2->second.hashes.find(dhtMsg->getValue());
00493 
00494             if (itHashes == it2->second.hashes.end()) {
00495                 //new hash
00496                 ReplicaVector vect = ReplicaVector();
00497                 vect.push_back(dhtMsg->getSrcNode());
00498                 it2->second.hashes.insert(make_pair(dhtMsg->getValue(), vect));
00499             } else {
00500                 itHashes->second.push_back(dhtMsg->getSrcNode());
00501             }
00502 
00503             it2->second.numResponses++;
00504 
00505             //we've got all the answers we wanted
00506             unsigned int maxCount = 0;
00507             ReplicaVector* hashVector = NULL;
00508             for (itHashes = it2->second.hashes.begin(); itHashes
00509             != it2->second.hashes.end(); itHashes++) {
00510                 if (itHashes->second.size() > maxCount) {
00511                     maxCount = itHashes->second.size();
00512                     hashVector = &(itHashes->second);
00513                 }
00514             }
00515 
00516             if ((double) maxCount / (double) it2->second.numAvailableReplica
00517                     >= ratioIdentical) {
00518                 it2->second.hashVector = hashVector;
00519             } else if (it2->second.numResponses >= numGetRequests){
00520                 //we'll try to ask some other nodes
00521                 if (it2->second.replica.size() > 0) {
00522                     DHTGetCall* getCall = new DHTGetCall();
00523                     getCall->setKey(dhtMsg->getKey());
00524                     getCall->setIsHash(true);
00525                         getCall->setBitLength(GETCALL_L(getCall));
00526                     RECORD_STATS(normalMessages++;
00527                                      numBytesNormal += getCall->getByteLength());
00528                     sendRouteRpcCall(TIER1_COMP,
00529                                      it2->second.replica.back(), getCall,
00530                                      NULL, DEFAULT_ROUTING, -1, 0,
00531                                      it2->second.callMsg->getNonce());
00532                     it2->second.replica.pop_back();
00533                 } else { //we don't have anyone else to ask
00534                     DHTgetCAPIResponse* capiGetRespMsg =
00535                         new DHTgetCAPIResponse();
00536                     capiGetRespMsg->setKey(dhtMsg->getKey());
00537                     capiGetRespMsg->setValue(BinaryValue::UNSPECIFIED_VALUE);
00538                     capiGetRespMsg->setIsSuccess(false);
00539                     sendRpcResponse(it2->second.callMsg, capiGetRespMsg);
00540                     //cout << "DHT: GET failed: hash (no one else)" << endl;
00541                     getMap.erase(rpcId);
00542                     return;
00543                 }
00544 
00545             }
00546         }
00547     }
00548 
00549     if (it2->second.hashVector != NULL) {
00550         //we have already received all the response and chosen a hash
00551         if (it2->second.hashVector->size() > 0) {
00552             DHTGetCall* getCall = new DHTGetCall();
00553             getCall->setKey(dhtMsg->getKey());
00554             getCall->setIsHash(false);
00555             getCall->setBitLength(GETCALL_L(getCall));
00556             RECORD_STATS(normalMessages++;
00557                          numBytesNormal += getCall->getByteLength());
00558             int nonce = sendRouteRpcCall(TIER1_COMP,
00559                                          it2->second.hashVector->back(),
00560                                          getCall, NULL, DEFAULT_ROUTING,
00561                                          -1, 0, it2->second.callMsg->getNonce());
00562             rpcIdMap.insert(make_pair(nonce, (BaseCallMessage*)NULL));
00563             it2->second.hashVector->pop_back();
00564         } else { //we don't have anyone else to ask
00565             DHTgetCAPIResponse* capiGetRespMsg = new DHTgetCAPIResponse();
00566             capiGetRespMsg->setKey(dhtMsg->getKey());
00567             capiGetRespMsg->setValue(BinaryValue::UNSPECIFIED_VALUE);
00568             capiGetRespMsg->setIsSuccess(false);
00569             sendRpcResponse(it2->second.callMsg, capiGetRespMsg);
00570             //cout << "DHT: GET failed: hash2 (no one else)" << endl;
00571             getMap.erase(rpcId);
00572         }
00573     }
00574 }
00575 
00576 void DHT::update(const NodeHandle& node, bool joined)
00577 {
00578     OverlayKey key;
00579     DHTPutCall* dhtMsg;
00580     bool err = false;
00581     DHTData entry;
00582     std::map<OverlayKey, DHTData>::iterator it = dataStorage->begin();
00583     for (unsigned int i = 0; i < dataStorage->getSize(); i++) {
00584         key = it->first;
00585         entry = it->second;
00586         if (joined) {
00587             if (entry.responsible && (overlay->isSiblingFor(node, key,
00588                                                             numReplica, &err)
00589                     || err)) { // hack for Chord, if we've got a new predecessor
00590 
00591                 dhtMsg = new DHTPutCall();
00592                 dhtMsg->setKey(key);
00593                 dhtMsg->setValue(entry.value);
00594                 dhtMsg->setTtl((int)SIMTIME_DBL(entry.ttlMessage->getArrivalTime()
00595                         - simTime()));
00596                 dhtMsg->setIsModifiable(entry.is_modifiable);
00597                 dhtMsg->setMaintenance(true);
00598                 dhtMsg->setBitLength(PUTCALL_L(dhtMsg));
00599                 RECORD_STATS(maintenanceMessages++;
00600                         numBytesMaintenance += dhtMsg->getByteLength());
00601                 sendRouteRpcCall(TIER1_COMP, node, dhtMsg);
00602             }
00603 
00604             if (err) {
00605                 EV << "[DHT::update()]\n"
00606                    << "    Unable to know if key: " << key
00607                    << " is in range of node: " << node
00608                    << endl;
00609             }
00610         } else {
00611 #if 0
00612             //the update concerns a node who has left
00613             //replicate
00614             LookupCall* replicaMsg = new LookupCall();
00615             replicaMsg->setKey(key);
00616             replicaMsg->setNumSiblings(numReplica);
00617             int nonce = sendInternalRpcCall(OVERLAY_COMP,
00618                                             replicaMsg);
00619             dhtMsg = new DHTPutCall();
00620             dhtMsg->setKey(key);
00621             dhtMsg->setValue(entry.value);
00622             dhtMsg->setTtl((int)(entry.ttlMessage->getArrivalTime()
00623                     - simTime()));
00624             dhtMsg->setIsModifiable(entry.is_modifiable);
00625             dhtMsg->setMaintenance(true);
00626             dhtMsg->setBitLength(PUTCALL_L(dhtMsg));
00627 
00628             rpcIdMap.insert(make_pair(nonce, dhtMsg));
00629 #endif
00630         }
00631 
00632         entry.responsible = overlay->isSiblingFor(overlay->getThisNode(),
00633                                                   key, 1, &err);
00634         it++;
00635     }
00636 }
00637 
00638 void DHT::handleLookupResponse(LookupResponse* lookupMsg)
00639 {
00640     std::map<unsigned int, BaseCallMessage*>::iterator it =
00641             rpcIdMap.find(lookupMsg->getNonce());
00642 
00643     if (it == rpcIdMap.end() || it->second == NULL)
00644         return;
00645 
00646     if (dynamic_cast<DHTputCAPICall*> (it->second)) {
00647 
00648         #if 0
00649         cout << "DHT::handleLookupResponse(): PUT "
00650              << lookupMsg->getKey() << " ("
00651              << overlay->getThisNode().key << ")" << endl;
00652 
00653         for (unsigned int i = 0; i < lookupMsg->getSiblingsArraySize(); i++) {
00654             cout << i << ": " << lookupMsg->getSiblings(i) << endl;
00655         }
00656 #endif
00657 
00658         DHTputCAPICall* capiPutMsg = dynamic_cast<DHTputCAPICall*> (it->second);
00659         rpcIdMap.erase(lookupMsg->getNonce());
00660 
00661         if ((lookupMsg->getIsValid() == false)
00662                 || (lookupMsg->getSiblingsArraySize() == 0)) {
00663 
00664             EV << "[DHT::handleLookupResponse()]\n"
00665                << "    Unable to get replica list : invalid lookup"
00666                << endl;
00667             DHTputCAPIResponse* capiPutRespMsg = new DHTputCAPIResponse();
00668             capiPutRespMsg->setKey(lookupMsg->getKey());
00669             capiPutRespMsg->setIsSuccess(false);
00670             sendRpcResponse(capiPutMsg, capiPutRespMsg);
00671             return;
00672         }
00673 
00674         for (unsigned int i = 0; i < lookupMsg->getSiblingsArraySize(); i++) {
00675             DHTPutCall* dhtMsg = new DHTPutCall();
00676             dhtMsg->setKey(capiPutMsg->getKey());
00677             dhtMsg->setValue(capiPutMsg->getValue());
00678             dhtMsg->setTtl(capiPutMsg->getTtl());
00679             dhtMsg->setIsModifiable(capiPutMsg->getIsModifiable());
00680             dhtMsg->setMaintenance(false);
00681             dhtMsg->setBitLength(PUTCALL_L(dhtMsg));
00682             RECORD_STATS(normalMessages++;
00683                          numBytesNormal += dhtMsg->getByteLength());
00684             sendRouteRpcCall(TIER1_COMP, lookupMsg->getSiblings(i),
00685                              dhtMsg, NULL, DEFAULT_ROUTING, -1,
00686                              0, capiPutMsg->getNonce());
00687         }
00688 
00689         PutMapEntry mapEntry;
00690         mapEntry.callMsg = capiPutMsg;
00691         mapEntry.numResponses = 0;
00692         mapEntry.numFailed = 0;
00693         mapEntry.numSent = lookupMsg->getSiblingsArraySize();
00694 
00695         putMap.insert(make_pair(capiPutMsg->getNonce(), mapEntry));
00696     }
00697     else if (dynamic_cast<DHTgetCAPICall*>(it->second)) {
00698 
00699 #if 0
00700         cout << "DHT::handleLookupResponse(): GET "
00701              << lookupMsg->getKey() << " ("
00702              << overlay->getThisNode().key << ")" << endl;
00703 
00704         for (unsigned int i = 0; i < lookupMsg->getSiblingsArraySize(); i++) {
00705             cout << i << ": " << lookupMsg->getSiblings(i) << endl;
00706         }
00707 #endif
00708 
00709         DHTgetCAPICall* capiGetMsg = dynamic_cast<DHTgetCAPICall*>(it->second);
00710         rpcIdMap.erase(lookupMsg->getNonce());
00711 
00712         if ((lookupMsg->getIsValid() == false)
00713                 || (lookupMsg->getSiblingsArraySize() == 0)) {
00714 
00715             EV << "[DHT::handleLookupResponse()]\n"
00716                << "    Unable to get replica list : invalid lookup"
00717                << endl;
00718             DHTgetCAPIResponse* capiGetRespMsg = new DHTgetCAPIResponse();
00719             capiGetRespMsg->setKey(lookupMsg->getKey());
00720             capiGetRespMsg->setValue(BinaryValue::UNSPECIFIED_VALUE);
00721             capiGetRespMsg->setIsSuccess(false);
00722             sendRpcResponse(capiGetMsg, capiGetRespMsg);
00723             return;
00724         }
00725 
00726         GetMapEntry mapEntry;
00727         mapEntry.numSent = 0;
00728 
00729         for (unsigned int i = 0; i < lookupMsg->getSiblingsArraySize(); i++) {
00730             if (i < (unsigned int)numGetRequests) {
00731                 DHTGetCall* dhtMsg = new DHTGetCall();
00732                 dhtMsg->setKey(capiGetMsg->getKey());
00733                 dhtMsg->setIsHash(true);
00734                 dhtMsg->setBitLength(GETCALL_L(dhtMsg));
00735                 RECORD_STATS(normalMessages++;
00736                              numBytesNormal += dhtMsg->getByteLength());
00737                 sendRouteRpcCall(TIER1_COMP, lookupMsg->getSiblings(i), dhtMsg,
00738                                  NULL, DEFAULT_ROUTING, -1, 0,
00739                                  capiGetMsg->getNonce());
00740                 mapEntry.numSent++;
00741             } else {
00742                 //We don't send, we just store the remaining keys
00743                 mapEntry.replica.push_back(lookupMsg->getSiblings(i));
00744             }
00745         }
00746 
00747         mapEntry.numAvailableReplica = lookupMsg->getSiblingsArraySize();
00748         mapEntry.callMsg = capiGetMsg;
00749         mapEntry.numResponses = 0;
00750         mapEntry.hashVector = NULL;
00751 
00752         getMap.insert(make_pair(capiGetMsg->getNonce(), mapEntry));
00753     }
00754     else if (dynamic_cast<DHTPutCall*>(it->second)) {
00755         DHTPutCall* putMsg = dynamic_cast<DHTPutCall*>(it->second);
00756         rpcIdMap.erase(lookupMsg->getNonce());
00757 
00758         if ((lookupMsg->getIsValid() == false)
00759                 || (lookupMsg->getSiblingsArraySize() == 0)) {
00760 
00761             EV << "[DHT::handleLookupResponse()]\n"
00762                << "    Unable to get replica list : invalid lookup"
00763                << endl;
00764             delete putMsg;
00765             return;
00766         }
00767 
00768         for( unsigned int i = 0; i < lookupMsg->getSiblingsArraySize(); i++ ) {
00769             RECORD_STATS(maintenanceMessages++;
00770                          numBytesMaintenance += putMsg->getByteLength());
00771 
00772             sendRouteRpcCall(TIER1_COMP, lookupMsg->getSiblings(i),
00773                              new DHTPutCall(*putMsg));
00774         }
00775 
00776         delete putMsg;
00777     }
00778 }
00779 
00780 void DHT::finishApp()
00781 {
00782     simtime_t time = globalStatistics->calcMeasuredLifetime(creationTime);
00783 
00784     if (time != 0) {
00785         globalStatistics->addStdDev("DHT: Sent Maintenance Messages/s",
00786                                     maintenanceMessages / time);
00787         globalStatistics->addStdDev("DHT: Sent Normal Messages/s",
00788                                     normalMessages / time);
00789         globalStatistics->addStdDev("DHT: Sent Maintenance Bytes/s",
00790                                     numBytesMaintenance / time);
00791         globalStatistics->addStdDev("DHT: Sent Normal Bytes/s",
00792                                     numBytesNormal / time);
00793     }
00794 }

Generated on Wed Mar 25 16:32:48 2009 for OverSim by  doxygen 1.5.5