source: source/ariba/overlay/modules/chord/Chord.cpp@ 5870

Last change on this file since 5870 was 5870, checked in by Christoph Mayer, 15 years ago

merge noch nicht fertig

File size: 16.6 KB
Line 
1// [License]
2// The Ariba-Underlay Copyright
3//
4// Copyright (c) 2008-2009, Institute of Telematics, UniversitÀt Karlsruhe (TH)
5//
6// Institute of Telematics
7// UniversitÀt Karlsruhe (TH)
8// Zirkel 2, 76128 Karlsruhe
9// Germany
10//
11// Redistribution and use in source and binary forms, with or without
12// modification, are permitted provided that the following conditions are
13// met:
14//
15// 1. Redistributions of source code must retain the above copyright
16// notice, this list of conditions and the following disclaimer.
17// 2. Redistributions in binary form must reproduce the above copyright
18// notice, this list of conditions and the following disclaimer in the
19// documentation and/or other materials provided with the distribution.
20//
21// THIS SOFTWARE IS PROVIDED BY THE INSTITUTE OF TELEMATICS ``AS IS'' AND
22// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ARIBA PROJECT OR
25// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32//
33// The views and conclusions contained in the software and documentation
34// are those of the authors and should not be interpreted as representing
35// official policies, either expressed or implied, of the Institute of
36// Telematics.
37// [License]
38
39#include "ariba/overlay/BaseOverlay.h"
40#include "ariba/overlay/messages/OverlayMsg.h"
41
42#include "Chord.h"
43#include "detail/chord_routing_table.hpp"
44
45#include "messages/Discovery.h"
46
47namespace ariba {
48namespace overlay {
49
50enum signalMessageTypes {
51 typeDiscovery = OverlayMsg::typeSignalingStart + 0x01,
52 typeLeave = OverlayMsg::typeSignalingStart + 0x02,
53};
54
55typedef chord_routing_table::item route_item;
56
57use_logging_cpp( Chord );
58
59Chord::Chord(BaseOverlay& _baseoverlay, const NodeID& _nodeid,
60 OverlayStructureEvents* _eventsReceiver, const OverlayParameterSet& param) :
61 OverlayInterface(_baseoverlay, _nodeid, _eventsReceiver, param) {
62
63 // create routing table
64<<<<<<< .working
65 this->table = new chord_routing_table(_nodeid, 2);
66=======
67 this->table = new chord_routing_table(_nodeid, 4);
68>>>>>>> .merge-rechts.r5869
69 orphan_removal_counter = 0;
70 discovery_count = 0;
71 stabilize_counter = 0;
72 stabilize_finger = 0;
73}
74
75Chord::~Chord() {
76
77 // delete routing table
78 delete table;
79}
80
81/// helper: sets up a link using the base overlay
82LinkID Chord::setup(const EndpointDescriptor& endpoint, const NodeID& remote ) {
83
84 // check if we already have a connection
85 for (int i=0; i<table->size(); i++)
86 if ((*table)[i]->ref_count > 0 && (*table)[i]->id == remote && !((*table)[i]->info.isUnspecified()))
87 return LinkID::UNSPECIFIED;
88
89 // check if we are already trying to establish a link
90 for (size_t i=0; i<pending.size(); i++)
91 if ( pending[i] == remote ) {
92 logging_debug("Already trying to establish a link to node "
93 << remote.toString() );
94 return LinkID::UNSPECIFIED;
95 }
96
97 // adding node to list of pending connections
98 pending.push_back( remote );
99
100 logging_info("Request to setup link to " << endpoint.toString() );
101
102 // establish link via base overlay
103<<<<<<< .working
104 return baseoverlay.establishLink(endp, node, OverlayInterface::OVERLAY_SERVICE_ID, remoteRelay );
105=======
106 return baseoverlay.establishLink( endpoint, remote,
107 OverlayInterface::OVERLAY_SERVICE_ID );
108>>>>>>> .merge-rechts.r5869
109}
110
111/// helper: sends a message using the "base overlay"
112seqnum_t Chord::send( OverlayMsg* msg, const LinkID& link ) {
113 if (link.isUnspecified()) return 0;
114 msg->setRelayed(true);
115 return baseoverlay.send_link( msg, link );
116}
117
118/// sends a discovery message
119void Chord::send_discovery_to(const NodeID& remote, int ttl) {
120 LinkID link = getNextLinkId(remote);
121 if ( remote == nodeid || link.isUnspecified()) return;
122 if ( table->size() == 0 ) return;
123
124 OverlayMsg msg( typeDiscovery );
125 msg.setRelayed(true);
126 Discovery dmsg( Discovery::normal, (uint8_t)2, baseoverlay.getEndpointDescriptor() );
127 msg.encapsulate(&dmsg);
128
129<<<<<<< .working
130 // get next hop
131 const route_item* item = table->get_next_hop(destination);
132 if (item!=NULL && !item->info.isUnspecified()) send(&cmsg, item->info);
133=======
134 // send to node
135 baseoverlay.send_node( &msg, remote );
136>>>>>>> .merge-rechts.r5869
137}
138
139void Chord::discover_neighbors( const LinkID& link ) {
140 uint8_t ttl = 2;
141 {
142 // send predecessor discovery
143 OverlayMsg msg( typeDiscovery );
144 msg.setRelayed(true);
145 Discovery dmsg( Discovery::predecessor, ttl,
146 baseoverlay.getEndpointDescriptor() );
147 msg.encapsulate(&dmsg);
148 send(&msg, link);
149 }
150 {
151 // send successor discovery
152 OverlayMsg msg( typeDiscovery );
153 msg.setSourceEndpoint( baseoverlay.getEndpointDescriptor() );
154 msg.setRelayed(true);
155 Discovery dmsg( Discovery::successor, ttl,
156 baseoverlay.getEndpointDescriptor() );
157 msg.encapsulate(&dmsg);
158 send(&msg, link);
159 }
160}
161
162
163void Chord::createOverlay() {
164}
165
166void Chord::deleteOverlay() {
167
168}
169
170void Chord::joinOverlay(const EndpointDescriptor& boot) {
171 logging_info( "joining Chord overlay structure through end-point " <<
172 (boot.isUnspecified() ? "local" : boot.toString()) );
173
174 // initiator? no->setup first link
175 if (!boot.isUnspecified())
176 bootstrapLinks.push_back( setup(boot) );
177
178 // timer for stabilization management
179 Timer::setInterval(1000);
180 Timer::start();
181}
182
183void Chord::leaveOverlay() {
184 Timer::stop();
185 for (size_t i = 0; i < table->size(); i++) {
186 route_item* it = (*table)[i];
187 OverlayMsg msg( typeLeave );
188 send( &msg, it->info );
189 }
190}
191
192/// @see OverlayInterface.h
193const EndpointDescriptor& Chord::resolveNode(const NodeID& node) {
194 const route_item* item = table->get(node);
195 if (item == NULL || item->info.isUnspecified()) return EndpointDescriptor::UNSPECIFIED();
196 return baseoverlay.getEndpointDescriptor(item->info);
197}
198
199/// @see OverlayInterface.h
200const LinkID& Chord::getNextLinkId( const NodeID& id ) const {
201 // get next hop
202<<<<<<< .working
203 const route_item* item = table->get_next_hop(destnode);
204=======
205 const route_item* item = table->get_next_hop(id);
206>>>>>>> .merge-rechts.r5869
207
208<<<<<<< .working
209 // message for this node? yes-> delegate to base overlay
210 if (item->id == nodeid || destnode == nodeid)
211 baseoverlay.incomingRouteMessage( msg, LinkID::UNSPECIFIED, nodeid );
212=======
213 // returns a unspecified id when this is itself
214 if (item == NULL || item->id == nodeid)
215 return LinkID::UNSPECIFIED;
216>>>>>>> .merge-rechts.r5869
217
218<<<<<<< .working
219 else { // no-> send to next hop
220 ChordMessage cmsg(ChordMessage::route, nodeid, destnode);
221 cmsg.encapsulate(msg);
222 send(&cmsg, item->info);
223 }
224=======
225 /// return routing info
226 return item->info;
227>>>>>>> .merge-rechts.r5869
228}
229
230<<<<<<< .working
231/// @see OverlayInterface.h
232void Chord::routeMessage(const NodeID& node, const LinkID& link, Message* msg) {
233 logging_debug("Redirect over Chord to node id=" << node.toString()
234 << " link id=" << link.toString() );
235 ChordMessage cmsg(ChordMessage::route, nodeid, node);
236 cmsg.encapsulate(msg);
237 send(&cmsg, link);
238}
239
240/// @see OverlayInterface.h
241const LinkID& Chord::getNextLinkId( const NodeID& id ) const {
242 // get next hop
243 const route_item* item = table->get_next_hop(id);
244
245 // returns a unspecified id when this is itself
246 if (item == NULL || item->id == nodeid)
247 return LinkID::UNSPECIFIED;
248
249 /// return routing info
250 return item->info;
251}
252
253=======
254>>>>>>> .merge-rechts.r5869
255OverlayInterface::NodeList Chord::getKnownNodes(bool deep) const {
256 OverlayInterface::NodeList nodelist;
257
258 if( deep ){
259 // all nodes that I know, fingers, succ/pred
260 for (size_t i = 0; i < table->size(); i++){
261 if ((*table)[i]->ref_count != 0
262 && !(*table)[i]->info.isUnspecified())
263 nodelist.push_back((*table)[i]->id);
264 }
265 } else {
266 // only succ and pred
267 if( table->get_predesessor() != NULL ){
268 nodelist.push_back( *(table->get_predesessor()) );
269 }
270 if( table->get_successor() != NULL ){
271 OverlayInterface::NodeList::iterator i =
272 std::find( nodelist.begin(), nodelist.end(), *(table->get_successor()) );
273 if( i == nodelist.end() )
274 nodelist.push_back( *(table->get_successor()) );
275 }
276 }
277
278 return nodelist;
279}
280
281/// @see CommunicationListener.h
282/// @see OverlayInterface.h
283void Chord::onLinkUp(const LinkID& lnk, const NodeID& remote) {
284 logging_info("link_up: link=" << lnk.toString() << " remote=" <<
285 remote.toString() );
286 for (vector<NodeID>::iterator i=pending.begin(); i!=pending.end(); i++)
287 if (*i == remote) {
288 pending.erase(i);
289 break;
290 }
291/*
292 // check if we already have a connection, yes-> do not handle duplicate!
293 for (int i=0; i<table->size(); i++)
294 if ((*table)[i]->id == remote && !((*table)[i]->info.isUnspecified()) && (*table)[i]->info != lnk) {
295
296 return;
297 }
298*/
299 if (remote==nodeid) {
300 baseoverlay.dropLink(lnk);
301 return;
302 }
303
304 route_item* item = table->insert(remote);
305
306 // item added to routing table?
307 if (item != NULL) { // yes-> add to routing table
308 logging_info("new routing neighbor: " << remote.toString()
309 << " with link " << lnk.toString());
310 // replace with new link
311 if (!item->info.isUnspecified() || item->info!=lnk)
312 baseoverlay.dropLink(item->info);
313 item->info = lnk;
314 // discover neighbors of new overlay neighbor
315 discover_neighbors( lnk );
316 showLinks();
317 } else { // no-> add orphan entry to routing table
318 logging_info("new orphan: " << remote.toString()
319 << " with link " << lnk.toString());
320 table->insert_orphan(remote)->info = lnk;
321 }
322
323 // erase bootstrap link
324 vector<LinkID>::iterator it = std::find(bootstrapLinks.begin(), bootstrapLinks.end(), lnk);
325 if( it != bootstrapLinks.end() ) bootstrapLinks.erase( it );
326}
327
328/// @see CommunicationListener.h or @see OverlayInterface.h
329void Chord::onLinkDown(const LinkID& lnk, const NodeID& remote) {
330 logging_debug("link_down: link=" << lnk.toString() << " remote=" <<
331 remote.toString() );
332
333 // remove link from routing table
334 route_item* item = table->get(remote);
335 if (item!=NULL && item->info==lnk) {
336 item->info = LinkID::UNSPECIFIED;
337 table->remove(remote);
338 }
339}
340
341/// @see CommunicationListener.h
342/// @see OverlayInterface.h
343void Chord::onMessage(const DataMessage& msg, const NodeID& remote,
344 const LinkID& link) {
345
346 // decode message
347 OverlayMsg* m = dynamic_cast<OverlayMsg*>(msg.getMessage());
348 if (m == NULL) return;
349
350 // handle messages
351 switch (m->getType()) {
352
353<<<<<<< .working
354 // invalid message
355 case M::invalid:
356 break;
357
358 // route message with payload
359 case M::route: {
360 // find next hop
361 const route_item* item = table->get_next_hop(m->getDestination());
362
363 // next hop == myself?
364 if (m->getDestination() == nodeid) { // yes-> route to base overlay
365 logging_debug("Send message to baseoverlay");
366 baseoverlay.incomingRouteMessage( m, item->info, remote );
367 }
368 // no-> route to next hop
369 else {
370 logging_debug("Route chord message to "
371 << item->id.toString() << " (destination=" << m->getDestination() << ")");
372 send(m, item->info);
373 }
374 break;
375 }
376
377 // discovery request
378 case M::discovery: {
379=======
380 // discovery request
381 case typeDiscovery: {
382>>>>>>> .merge-rechts.r5869
383 // decapsulate message
384 Discovery* dmsg = m->decapsulate<Discovery> ();
385 logging_debug("Received discovery message with"
386 << " src=" << m->getSourceNode().toString()
387 << " dst=" << m->getDestinationNode().toString()
388 << " ttl=" << (int)dmsg->getTTL()
389 << " type=" << (int)dmsg->getType()
390 );
391
392 // check if source node can be added to routing table and setup link
393 if (m->getSourceNode() != nodeid
394 && table->is_insertable(m->getSourceNode()))
395 setup( dmsg->getEndpoint(), m->getSourceNode() );
396
397 // delegate discovery message
398 switch (dmsg->getType()) {
399
400 // normal: route discovery message like every other message
401 case Discovery::normal: {
402 // closest node? yes-> split to follow successor and predecessor
403 if ( table->is_closest_to(m->getDestinationNode()) ) {
404
405 if (table->get_successor() != NULL) {
406 OverlayMsg omsg(*m);
407 dmsg->setType(Discovery::successor);
408 omsg.encapsulate(dmsg);
409 route_item* succ_item = table->get(*table->get_successor());
410 logging_debug("Discovery split: routing discovery message to successor "
411 << succ_item->id.toString() );
412<<<<<<< .working
413 send(&cmsg_s, succ_item->info);
414=======
415 send(&omsg, succ_item->info);
416>>>>>>> .merge-rechts.r5869
417 }
418
419 // send predecessor message
420 if (table->get_predesessor() != NULL) {
421 OverlayMsg omsg(*m);
422 dmsg->setType(Discovery::predecessor);
423 omsg.encapsulate(dmsg);
424 route_item* pred_item = table->get(
425 *table->get_predesessor());
426 logging_debug("Discovery split: routing discovery message to predecessor "
427 << pred_item->id.toString() );
428<<<<<<< .working
429 send(&cmsg_p, pred_item->info);
430=======
431 send( &omsg, pred_item->info);
432>>>>>>> .merge-rechts.r5869
433 }
434 }
435 // no-> route message
436 else {
437<<<<<<< .working
438 // find next hop
439 const route_item* item = table->get_next_hop(m->getDestination());
440 if (item == NULL || item->id == nodeid) break;
441 logging_debug("routing discovery message to " <<
442 item->id.toString() );
443 send(m, item->info);
444=======
445 baseoverlay.send( m, m->getDestinationNode() );
446>>>>>>> .merge-rechts.r5869
447 }
448 break;
449 }
450
451 // successor mode: follow the successor until TTL is zero
452 case Discovery::successor:
453 case Discovery::predecessor: {
454 // time to live ended? yes-> stop routing
455 if (dmsg->getTTL() == 0 || dmsg->getTTL() > 10) break;
456
457 // decrease time-to-live
458 dmsg->setTTL(dmsg->getTTL() - 1);
459
460 const route_item* item = NULL;
461 if (dmsg->getType() == Discovery::successor &&
462 table->get_successor() != NULL) {
463 item = table->get(*table->get_successor());
464 } else {
465 if (table->get_predesessor()!=NULL)
466 item = table->get(*table->get_predesessor());
467 }
468 if (item == NULL) break;
469 logging_debug("routing discovery message to succ/pred "
470 << item->id.toString() );
471 OverlayMsg omsg(*m);
472 omsg.encapsulate(dmsg);
473 omsg.setDestinationNode(item->id);
474 baseoverlay.send(&omsg, omsg.getDestinationNode());
475 break;
476 }}
477 delete dmsg;
478 break;
479 }
480
481 // leave
482 case typeLeave: {
483 if (link!=LinkID::UNSPECIFIED) {
484 route_item* item = table->get(remote);
485 if (item!=NULL) item->info = LinkID::UNSPECIFIED;
486 table->remove(remote);
487 baseoverlay.dropLink(link);
488 }
489 break;
490 }}
491}
492
493void Chord::eventFunction() {
494 stabilize_counter++;
495 if (stabilize_counter < 0 || stabilize_counter == 4) {
496
497 // reset counter
498 stabilize_counter = 0;
499
500 // clear pending connections
501 pending.clear();
502
503 // get number of real neighbors
504 size_t numNeighbors = 0;
505 for (size_t i = 0; i < table->size(); i++) {
506 route_item* it = (*table)[i];
507 if (it->ref_count != 0 && !it->info.isUnspecified()) numNeighbors++;
508 }
509 logging_info("Running stabilization: #links="
510 << table->size() << " #neighbors=" << numNeighbors );
511
512 // sending discovery
513 logging_debug("Sending discovery message to my neighbors and fingers");
514 stabilize_finger = ((stabilize_finger+1) % table->get_finger_table_size() );
515 const NodeID disc1 = nodeid;
516 const NodeID disc2 = table->get_finger_table(stabilize_finger).get_compare().get_center();
517 send_discovery_to(disc1);
518 if (disc1 != disc2)
519 send_discovery_to(disc2);
520
521 for (int i=0; i<table->size(); i++) {
522 LinkID id = (*table)[i]->info;
523 if (!id.isUnspecified()) discover_neighbors(id);
524 }
525
526 // remove orphan links
527 orphan_removal_counter++;
528<<<<<<< .working
529 if (orphan_removal_counter <0 || orphan_removal_counter >= 4) {
530=======
531 if (orphan_removal_counter <0 || orphan_removal_counter >= 2) {
532>>>>>>> .merge-rechts.r5869
533 logging_info("Running orphan removal");
534 orphan_removal_counter = 0;
535 for (size_t i = 0; i < table->size(); i++) {
536 route_item* it = (*table)[i];
537 if (it->ref_count == 0 && !it->info.isUnspecified()) {
538 logging_info("Dropping orphaned link " << it->info.toString() << " to " << it->id.toString());
539 table->insert(it->id);
540 if (it->ref_count==0) {
541 LinkID id = it->info;
542 it->info = LinkID::UNSPECIFIED;
543 baseoverlay.dropLink(id);
544 }
545 }
546 }
547 }
548 }
549}
550
551void Chord::showLinks() {
552 logging_info("--- chord routing information ----------------------------------");
553 logging_info("predecessor: " << (table->get_predesessor()==NULL? "<none>" :
554 table->get_predesessor()->toString()) );
555 logging_info("node_id : " << nodeid.toString() );
556 logging_info("successor : " << (table->get_successor()==NULL? "<none>" :
557 table->get_successor()->toString()));
558 logging_info("----------------------------------------------------------------");
559}
560
561}} // namespace ariba, overlay
Note: See TracBrowser for help on using the repository browser.