PastryLeafSet.cc

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2006 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 "PastryLeafSet.h"
00025 #include "PastryTypes.h"
00026 #include "NodeVector.h"
00027 
00028 #if 0
00029 #define LEAF_TEST() \
00030     do {\
00031         uint32_t count = 0;\
00032         uint32_t i = 0, j = leaves.size() - 1;\
00033         while (leaves[i].isUnspecified() && i < j) ++i;\
00034         while (leaves[j].isUnspecified() && i < j) --j;\
00035         if (i == j) break;\
00036         if (!owner.key.isBetween(leaves[(leaves.size() / 2) - 1].key,\
00037                                  leaves[leaves.size() / 2].key))\
00038                 ASSERT2(false, "leafset broken!");\
00039         for (uint32_t ci = i + 1; ci < j; ++ci) {\
00040             if (leaves[ci - 1].key >= leaves[ci].key) {\
00041                 count++;\
00042             }\
00043         }\
00044         if (leaves[j].key >= leaves[i].key) count++;\
00045         ASSERT2(count <= 1, "leafset broken!");\
00046     } while (false);
00047 #else
00048 #define LEAF_TEST() {}
00049 #endif
00050 
00051 Define_Module(PastryLeafSet);
00052 
00053 void PastryLeafSet::earlyInit(void)
00054 {
00055     WATCH_VECTOR(leaves);
00056 }
00057 
00058 void PastryLeafSet::initializeSet(uint32_t numberOfLeaves,
00059                                   uint32_t bitsPerDigit,
00060                                   simtime_t repairTimeout,
00061                                   const NodeHandle& owner,
00062                                   BasePastry *overlay)
00063 {
00064     if (numberOfLeaves % 2) throw "numberOfLeaves must be even.";
00065 
00066     this->owner = owner;
00067     this->numberOfLeaves = numberOfLeaves;
00068     this->repairTimeout = repairTimeout;
00069     this->bitsPerDigit = bitsPerDigit;
00070     this->overlay = overlay;
00071 
00072     if (!leaves.empty()) leaves.clear();
00073 
00074     // fill Set with unspecified node handles
00075     for (uint32_t i = numberOfLeaves; i>0; i--)
00076         leaves.push_back(NodeHandle::UNSPECIFIED_NODE);
00077 
00078     // initialize iterators to mark the beginning of bigger/smaller keys
00079     // in the set
00080     bigger = leaves.begin() + (numberOfLeaves >> 1);
00081     smaller = bigger - 1;
00082 
00083     // reset repair marker:
00084     if (!awaitingRepair.empty()) awaitingRepair.clear();
00085 
00086     newLeafs = false;
00087     isFull = false;
00088     wasFull = false;
00089 }
00090 
00091 const NodeHandle& PastryLeafSet::getSuccessor(void) const
00092 {
00093     return *bigger;
00094 }
00095 
00096 const NodeHandle& PastryLeafSet::getPredecessor(void) const
00097 {
00098     return *smaller;
00099 }
00100 
00101 bool PastryLeafSet::isValid(void) const
00102 {
00103     return (!(smaller->isUnspecified() || bigger->isUnspecified()));
00104 }
00105 
00106 const NodeHandle& PastryLeafSet::getDestinationNode(
00107     const OverlayKey& destination)
00108 {
00109     std::vector<NodeHandle>::const_iterator i;
00110     const OverlayKey* smallest;
00111     const OverlayKey* biggest;
00112     const NodeHandle* ret = &NodeHandle::UNSPECIFIED_NODE;
00113 
00114     // check whether destination is inside leafSet:
00115 
00116     smallest = &(getSmallestKey());
00117     biggest = &(getBiggestKey());
00118     if (smallest->isUnspecified()) smallest = &(owner.key);
00119     if (biggest->isUnspecified()) biggest = &(owner.key);
00120 
00121     if (!destination.isBetweenLR(*smallest, *biggest)) return *ret;
00122 
00123     // find the closest node:
00124 
00125     for (i = leaves.begin(); i != leaves.end(); i++) {
00126         if (i->isUnspecified()) continue;
00127 
00128         // note for next line:
00129         // * dereferences iterator, & gets address of element.
00130         if (isCloser(*i, destination, *ret)) ret = &(*i);
00131     }
00132 
00133     return *ret;
00134 }
00135 
00136 bool PastryLeafSet::isClosestNode(const OverlayKey& destination) const
00137 {
00138     // check for simple cases first
00139     if (owner.key == destination) {
00140         return true;
00141     }
00142 
00143     if (bigger->isUnspecified() && smaller->isUnspecified()) {
00144         return true;
00145     }
00146 
00147     // check if the next bigger or smaller node in the set is closer
00148     // than own node
00149     bool biggerIsCloser = false;
00150     bool smallerIsCloser = false;
00151 
00152     if (! bigger->isUnspecified()) {
00153         biggerIsCloser = isCloser(*bigger, destination);
00154     }
00155     if (! smaller->isUnspecified()) {
00156         smallerIsCloser = isCloser(*smaller, destination);
00157     }
00158 
00159     // return true if both are not closer
00160     return ((!biggerIsCloser) && (!smallerIsCloser));
00161 }
00162 
00163 void PastryLeafSet::dumpToStateMessage(PastryStateMessage* msg) const
00164 {
00165     uint32_t i = 0;
00166     uint32_t size = 0;
00167     std::vector<NodeHandle>::const_iterator it;
00168 
00169     msg->setLeafSetArraySize(numberOfLeaves);
00170     for (it = leaves.begin(); it != leaves.end(); it++) {
00171         if (!it->isUnspecified()) {
00172             ++size;
00173             msg->setLeafSet(i++, *it);
00174         }
00175     }
00176     msg->setLeafSetArraySize(size);
00177 }
00178 
00179 //TODO ugly duplication of code
00180 void PastryLeafSet::dumpToStateMessage(PastryLeafsetMessage* msg) const
00181 {
00182     uint32_t i = 0;
00183     uint32_t size = 0;
00184     std::vector<NodeHandle>::const_iterator it;
00185 
00186     msg->setLeafSetArraySize(numberOfLeaves);
00187     for (it = leaves.begin(); it != leaves.end(); it++) {
00188         if (!it->isUnspecified()) {
00189             ++size;
00190             msg->setLeafSet(i++, *it);
00191         }
00192     }
00193     msg->setLeafSetArraySize(size);
00194 }
00195 
00196 const TransportAddress& PastryLeafSet::getRandomNode()
00197 {
00198     //std::vector<NodeHandle>::iterator i;
00199     uint32_t rnd;
00200     int i;
00201 
00202     rnd = intuniform(0, numberOfLeaves - 1, 0);
00203     i = rnd;
00204     //while (rnd < leaves.size()) {
00205     while (i < (int)leaves.size()) {
00206         if (!leaves[i].isUnspecified()) return leaves[i];
00207         else i++;//rnd++;
00208     }
00209     i = rnd;
00210     while (i >= 0) {
00211         if (!leaves[i].isUnspecified()) return leaves[i];
00212         else i--;
00213     }
00214     EV << "Leafset::getRandomNode() returns UNSPECIFIED_NODE"
00215           "Leafset empty??" << endl;
00216     return TransportAddress::UNSPECIFIED_NODE;
00217 
00218 }
00219 
00220 NodeVector* PastryLeafSet::createSiblingVector(const OverlayKey& key,
00221         int numSiblings) const
00222 {
00223     std::vector<NodeHandle>::const_iterator it;
00224 
00225     // create temporary comparator
00226     KeyDistanceComparator<KeyRingMetric>* comp =
00227         new KeyDistanceComparator<KeyRingMetric>( key );
00228 
00229     // create result vector
00230     NodeVector* result = new NodeVector( numSiblings, comp );
00231 
00232     result->add(owner);
00233 
00234     for (it = leaves.begin(); it != leaves.end(); it++) {
00235         if (!it->isUnspecified()) {
00236             result->add(*it);
00237         }
00238     }
00239 
00240     delete comp;
00241 
00242     if (!isValid()) {
00243         return result;
00244     }
00245 
00246     // if the leafset is not full, we could have a very small network
00247     // => return true (FIXME hack)
00248     if (leaves.front().isUnspecified() || leaves.back().isUnspecified()) {
00249         return result;
00250     }
00251 
00252     if ((result->contains(getBiggestKey())) ||
00253         (result->contains(getSmallestKey()))) {
00254         delete result;
00255         return NULL;
00256     }
00257 
00258     return result;
00259 }
00260 
00261 bool PastryLeafSet::mergeNode(const NodeHandle& node, simtime_t prox)
00262 {
00263     std::vector<NodeHandle>::iterator it, it_left, it_right;
00264     const OverlayKey* last_left = &(owner.key);
00265     const OverlayKey* last_right = &(owner.key);
00266 
00267     it_left = smaller;
00268     it_right = bigger;
00269 
00270     // avoid duplicates
00271     for (it = leaves.begin(); it != leaves.end(); ++it) {
00272         if (it->isUnspecified()) {
00273             isFull = false;
00274             continue;
00275         }
00276         if (it->key == node.key) return false;
00277     }
00278 
00279     // look for correct position in left and right half of leafset
00280     while (true) {
00281         if(!isFull) {
00282             // both sides free
00283             if(it_left->key.isUnspecified() &&
00284                it_right->key.isUnspecified()) {
00285                 insertLeaf(it_left, node);
00286                 return true;
00287             }
00288             if (it_left->key.isUnspecified() &&
00289                 !node.key.isBetween(*last_right, it_right->key)) {
00290                 // end of smaller entries found
00291                 insertLeaf(it_left, node);
00292                 return true;
00293             }
00294             if (it_right->key.isUnspecified() &&
00295                 !node.key.isBetween(it_left->key, *last_left)) {
00296                 // end of bigger entries found
00297                 insertLeaf(it_right, node);
00298                 return true;
00299             }
00300         }
00301         // left side
00302         if (node.key.isBetween(it_left->key, *last_left)) {
00303             // found correct position for inserting the new entry between
00304             // existing ones
00305             insertLeaf(it_left, node);
00306             return true;
00307         }
00308         // right side
00309         if (node.key.isBetween(*last_right, it_right->key)) {
00310             // found correct position for inserting the new entry between
00311             // existing ones
00312             insertLeaf(it_right, node);
00313             return true;
00314         }
00315 
00316         last_right = &(it_right->key);
00317         ++it_right;
00318 
00319         if (it_right == leaves.end()) break;
00320 
00321         last_left = &(it_left->key);
00322         --it_left;
00323     }
00324     return false;
00325 }
00326 
00327 void PastryLeafSet::insertLeaf(std::vector<NodeHandle>::iterator& it,
00328                                const NodeHandle& node)
00329 {
00330     LEAF_TEST();
00331     bool issmaller = (it <= smaller);
00332     if (issmaller) {
00333         if (!leaves.front().isUnspecified())  {
00334             overlay->callUpdate(leaves.front(), false);
00335         }
00336         overlay->callUpdate(node, true);
00337 
00338         leaves.insert(++it, node);
00339         NodeHandle& temp = leaves.front();
00340         if (!temp.isUnspecified() && leaves.back().isUnspecified()) {
00341             leaves.back() = temp;
00342         }
00343         leaves.erase(leaves.begin());
00344 
00345 
00346     } else {
00347         if (!leaves.back().isUnspecified())  {
00348             overlay->callUpdate(leaves.back(), false);
00349         }
00350         overlay->callUpdate(node, true);
00351 
00352         leaves.insert(it, node);
00353         NodeHandle& temp = leaves.back();
00354         if (!temp.isUnspecified() && leaves.front().isUnspecified()) {
00355             leaves.front() = temp;
00356         }
00357         leaves.pop_back();
00358     }
00359 
00360     if (!leaves.front().isUnspecified() &&
00361         !leaves.back().isUnspecified()) {
00362         isFull = true;
00363     } else isFull = false;
00364 
00365     newLeafs = true;
00366     bigger = leaves.begin() + (numberOfLeaves >> 1);
00367     smaller = bigger - 1;
00368 
00369     // ensure balance in leafset
00370     if (!isFull) {
00371         balanceLeafSet();
00372     }
00373     LEAF_TEST();
00374 }
00375 
00376 bool PastryLeafSet::balanceLeafSet()
00377 {
00378     if (isFull ||
00379         (!leaves.front().isUnspecified() &&
00380          !(leaves.end() - 2)->isUnspecified()) ||
00381         (!leaves.back().isUnspecified() &&
00382          !(leaves.begin() + 1)->isUnspecified()))
00383         return false;
00384 
00385     std::vector<NodeHandle>::iterator it_left, it_right;
00386 
00387     for (it_left = smaller, it_right = bigger;
00388          it_right != leaves.end(); --it_left, ++it_right) {
00389         if (it_left->isUnspecified()) {
00390             if (it_right->isUnspecified() ||
00391                 (it_right + 1) == leaves.end() ||
00392                 (it_right + 1)->isUnspecified()) return false;
00393             *it_left = *(it_right + 1);
00394             *(it_right + 1) = NodeHandle::UNSPECIFIED_NODE;
00395             return true;
00396         } else if (it_right->isUnspecified()) {
00397 
00398             if (it_left == leaves.begin() ||
00399                 (it_left - 1)->isUnspecified()) return false;
00400             *it_right = *(it_left - 1);
00401             *(it_left - 1) = NodeHandle::UNSPECIFIED_NODE;
00402             return true;
00403         }
00404     }
00405     return false; // should not happen
00406 }
00407 
00408 void PastryLeafSet::dumpToVector(std::vector<TransportAddress>& affected) const
00409 {
00410     std::vector<NodeHandle>::const_iterator it;
00411 
00412     for (it = leaves.begin(); it != leaves.end(); it++)
00413         if (!it->isUnspecified())
00414             affected.push_back(*it);
00415 }
00416 
00417 const NodeHandle& PastryLeafSet::getSmallestNode(void) const
00418 {
00419     std::vector<NodeHandle>::const_iterator i = leaves.begin();
00420     while ((i->isUnspecified()) && (i != smaller)) i++;
00421     return *i;
00422 }
00423 
00424 const OverlayKey& PastryLeafSet::getSmallestKey(void) const
00425 {
00426     return getSmallestNode().key;
00427 }
00428 
00429 const NodeHandle& PastryLeafSet::getBiggestNode(void) const
00430 {
00431     std::vector<NodeHandle>::const_iterator i = leaves.end()-1;
00432     while ((i->isUnspecified()) && (i != bigger)) i--;
00433     return *i;
00434 }
00435 
00436 const OverlayKey& PastryLeafSet::getBiggestKey(void) const
00437 {
00438     return getBiggestNode().key;
00439 }
00440 
00441 void PastryLeafSet::findCloserNodes(const OverlayKey& destination,
00442                                     NodeVector* nodes)
00443 {
00444     std::vector<NodeHandle>::const_iterator it;
00445 
00446     for (it = leaves.begin(); it != leaves.end(); it++)
00447         if (!it->isUnspecified())
00448             nodes->add(*it);
00449 
00450 }
00451 
00452 const NodeHandle& PastryLeafSet::findCloserNode(const OverlayKey& destination,
00453                                                 bool optimize)
00454 {
00455     std::vector<NodeHandle>::const_iterator i;
00456     const NodeHandle* ret = &NodeHandle::UNSPECIFIED_NODE;
00457 
00458     // this will only be called after getDestinationNode() returned
00459     // NodeHandle::UNSPECIFIED_NODE, so a closer Node can only be the biggest
00460     // or the smallest node in the LeafSet.
00461 
00462     const NodeHandle& smallest = getSmallestNode();
00463     const NodeHandle& biggest = getBiggestNode();
00464 
00465     if ((!smallest.isUnspecified()) &&
00466             (specialCloserCondition(smallest, destination, *ret))) {
00467         if (optimize) ret = &smallest;
00468         else return smallest;
00469     }
00470 
00471     if ((!biggest.isUnspecified()) &&
00472             (specialCloserCondition(biggest, destination, *ret))) {
00473         if (optimize) ret = &biggest;
00474         else return biggest;
00475     }
00476 
00477     return *ret;
00478 }
00479 
00480 const TransportAddress& PastryLeafSet::failedNode(const TransportAddress& failed)
00481 {
00482     std::vector<NodeHandle>::iterator i;
00483     const TransportAddress* ask;
00484     bool left = true;
00485 
00486     // search failed node in leafset:
00487     for (i = leaves.begin(); i != leaves.end(); i++) {
00488         if (i == bigger) left = false;
00489         if ((! i->isUnspecified()) && (i->ip == failed.ip)) break;
00490     }
00491 
00492     // failed node not in leafset:
00493     if (i == leaves.end()) return TransportAddress::UNSPECIFIED_NODE;
00494 
00495     overlay->callUpdate(*i, false);
00496 
00497     // remove failed node:
00498     leaves.erase(i);
00499     newLeafs = true;
00500 
00501     wasFull = isFull;
00502     isFull = false;
00503 
00504     // insert UNSPECIFIED_NODE at front or back and return correct node
00505     // to ask for repair:
00506     if (left) {
00507         leaves.insert(leaves.begin(), NodeHandle::UNSPECIFIED_NODE);
00508         bigger = leaves.begin() + (numberOfLeaves >> 1);
00509         smaller = bigger - 1;
00510         ask = static_cast<const TransportAddress*>(&(getSmallestNode()));
00511     } else {
00512         leaves.push_back(NodeHandle::UNSPECIFIED_NODE);
00513         bigger = leaves.begin() + (numberOfLeaves >> 1);
00514         smaller = bigger - 1;
00515         ask = static_cast<const TransportAddress*>(&(getBiggestNode()));
00516     }
00517 
00518     balanceLeafSet();
00519     LEAF_TEST();
00520 
00521     if (! ask->isUnspecified())
00522         awaitingRepair[*ask] = PLSRepairData(simTime(), left);
00523 
00524     return *ask;
00525 }
00526 
00527 const TransportAddress& PastryLeafSet::repair(const PastryStateMessage* msg,
00528                                               const PastryStateMsgProximity* prox)
00529 {
00530     std::map<TransportAddress, PLSRepairData>::iterator it;
00531     const TransportAddress* ask;
00532     bool left;
00533 
00534     simtime_t now = simTime();
00535 
00536     // first eliminate outdated entries in awaitingRepair:
00537     for (it = awaitingRepair.begin(); it != awaitingRepair.end();) {
00538         if (it->second.ts < (now - repairTimeout)) {
00539             awaitingRepair.erase(it++);
00540         }
00541         else it++;
00542     }
00543 
00544     // don't expect any more repair messages:
00545     if (awaitingRepair.empty()) return TransportAddress::UNSPECIFIED_NODE;
00546 
00547     // look for source node in our list:
00548     if ( (it = awaitingRepair.find(msg->getSender())) == awaitingRepair.end() )
00549         return TransportAddress::UNSPECIFIED_NODE;
00550 
00551     // which side of the LeafSet is affected:
00552     left = it->second.left;
00553 
00554     // remove source node from list:
00555     awaitingRepair.erase(it);
00556 
00557     // merge info from repair message:
00558     if (mergeState(msg, prox) || isFull || !wasFull) {
00559         EV << "[PastryLeafSet::repair()]\n"
00560            << "    LeafSet repair was successful."
00561            << endl;
00562         return TransportAddress::UNSPECIFIED_NODE;
00563     } else {
00564         // repair did not succeed, try again:
00565         ask = &( left ? getSmallestNode() : getBiggestNode() );
00566         if (ask->isUnspecified() || *ask == msg->getSender()) {
00567             EV << "[PastryLeafSet::repair()]\n"
00568                << "    LeafSet giving up repair attempt."
00569                << endl;
00570             return TransportAddress::UNSPECIFIED_NODE;
00571         } else {
00572             awaitingRepair[*ask] = PLSRepairData(simTime(), left);
00573         }
00574         return *ask;
00575     }
00576 }
00577 
00578 PastryNewLeafsMessage* PastryLeafSet::getNewLeafsMessage(void)
00579 {
00580     std::vector<NodeHandle>::const_iterator it;
00581     PastryNewLeafsMessage* msg;
00582     uint32_t i = 0;
00583 
00584     if (! newLeafs) return NULL;
00585     newLeafs = false;
00586 
00587     msg = new PastryNewLeafsMessage("PastryNewLeafs");
00588 
00589     msg->setLeafsArraySize(numberOfLeaves);
00590     for (it = leaves.begin(); it != leaves.end(); it++)
00591         msg->setLeafs(i++, *it);
00592 
00593     msg->setBitLength(PASTRYNEWLEAFS_L(msg));
00594     return msg;
00595 }

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