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

Last change on this file since 5756 was 5756, checked in by mies, 15 years ago
File size: 15.3 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
41#include "Chord.h"
42#include "messages/ChordMessage.h"
43#include "messages/Discovery.h"
44
45#include "detail/chord_routing_table.hpp"
46
47namespace ariba {
48namespace overlay {
49
50typedef chord_routing_table::item route_item;
51
52use_logging_cpp( Chord );
53
54Chord::Chord(BaseOverlay& _baseoverlay, const NodeID& _nodeid,
55 OverlayStructureEvents* _eventsReceiver, const OverlayParameterSet& param) :
56 OverlayInterface(_baseoverlay, _nodeid, _eventsReceiver, param) {
57
58 // create routing table
59 this->table = new chord_routing_table(_nodeid, 2);
60
61 // init counters
62 orphan_removal_counter = 0;
63 discovery_count = 0;
64 stabilize_counter = 0;
65 stabilize_finger = 0;
66}
67
68Chord::~Chord() {
69
70 // delete routing table
71 delete table;
72}
73
74/// helper: sets up a link using the base overlay
75LinkID Chord::setup(const EndpointDescriptor& endp, const NodeID& node, const NodeID& remoteRelay ) {
76 logging_debug("Request to setup link to " << endp.toString() );
77
78 // check if we already have a connection
79 for (int i=0; i<table->size(); i++)
80 if ((*table)[i]->id == node && !((*table)[i]->info.isUnspecified()))
81 return LinkID::UNSPECIFIED;
82
83 // check if we are already trying to establish a link
84 for (size_t i=0; i<pending.size(); i++)
85 if ( pending[i] == node ) {
86 logging_debug("Already trying to establish a link to node " << node.toString() );
87 return LinkID::UNSPECIFIED;
88 }
89
90 // adding node to list of pending connections
91 pending.push_back( node );
92
93 // establish link via base overlay
94 return baseoverlay.establishLink(endp, node, OverlayInterface::OVERLAY_SERVICE_ID, remoteRelay );
95}
96
97/// helper: sends a message using the "base overlay"
98seqnum_t Chord::send(Message* msg, const LinkID& link) {
99 if (link.isUnspecified()) return 0;
100 return baseoverlay.sendMessage(msg, link);
101}
102
103const LinkID& Chord::get_next_hop( const NodeID& id ) const {
104 BOOST_FOREACH( const back_route& br, back_routes )
105 if (br.id==id) return br.link;
106 const route_item* item = table->get_next_hop(id);
107 if (item==NULL) return LinkID::UNSPECIFIED;
108 return item->info;
109}
110
111/// sends a discovery message
112void Chord::send_discovery_to(const NodeID& destination, int ttl) {
113 if ( table->size() == 0 ) return;
114
115 ChordMessage cmsg(ChordMessage::discovery, nodeid, destination);
116 Discovery dmsg;
117 dmsg.setSourceEndpoint(&baseoverlay.getEndpointDescriptor());
118 dmsg.setSourceRelay(baseoverlay.getRelayNode(destination));
119 dmsg.setFollowType(Discovery::normal);
120 dmsg.setTTL((uint8_t) ttl);
121 cmsg.encapsulate(&dmsg);
122
123 // get next hop
124 LinkID next_link = get_next_hop(destination);
125 if (!next_link.isUnspecified()) send(&cmsg, next_link);
126}
127
128void Chord::discover_neighbors( const LinkID& lnk ) {
129 {
130 // send successor discovery
131 ChordMessage cmsg(ChordMessage::discovery, nodeid, nodeid);
132 Discovery dmsg;
133 dmsg.setSourceEndpoint(&baseoverlay.getEndpointDescriptor());
134 dmsg.setSourceRelay(baseoverlay.getRelayNode(nodeid));
135 dmsg.setFollowType(Discovery::successor);
136 dmsg.setTTL((uint8_t)4);
137 cmsg.encapsulate(&dmsg);
138 send(&cmsg, lnk);
139 }
140 {
141 // send predecessor discovery
142 ChordMessage cmsg(ChordMessage::discovery, nodeid, nodeid);
143 Discovery dmsg;
144 dmsg.setSourceEndpoint(&baseoverlay.getEndpointDescriptor());
145 dmsg.setSourceRelay(baseoverlay.getRelayNode(nodeid));
146 dmsg.setFollowType(Discovery::predecessor);
147 dmsg.setTTL((uint8_t)4);
148 cmsg.encapsulate(&dmsg);
149 send(&cmsg, lnk);
150 }
151}
152
153
154void Chord::createOverlay() {
155}
156
157void Chord::deleteOverlay() {
158
159}
160
161void Chord::joinOverlay(const EndpointDescriptor& boot) {
162 logging_info( "joining Chord overlay structure through end-point " <<
163 (boot.isUnspecified() ? "local" : boot.toString()) );
164
165 // initiator? no->setup first link
166 if (!boot.isUnspecified())
167 bootstrapLinks.push_back( setup(boot) );
168
169 // timer for stabilization management
170 Timer::setInterval(1000);
171 Timer::start();
172}
173
174void Chord::leaveOverlay() {
175 Timer::stop();
176 for (size_t i = 0; i < table->size(); i++) {
177 route_item* it = (*table)[i];
178 ChordMessage msg(ChordMessage::leave, nodeid, it->id);
179 send(&msg,it->info);
180 }
181}
182
183const EndpointDescriptor& Chord::resolveNode(const NodeID& node) {
184 const route_item* item = table->get(node);
185 if (item == NULL || item->info.isUnspecified()) return EndpointDescriptor::UNSPECIFIED();
186 return baseoverlay.getEndpointDescriptor(item->info);
187}
188
189void Chord::routeMessage(const NodeID& destnode, Message* msg) {
190 // get next hop
191 LinkID next_link = get_next_hop(destnode);
192
193 // message for this node? yes-> delegate to base overlay
194 if (destnode == nodeid)
195 baseoverlay.incomingRouteMessage( msg, LinkID::UNSPECIFIED, nodeid );
196
197 else { // no-> send to next hop
198 ChordMessage cmsg(ChordMessage::route, nodeid, destnode);
199 cmsg.encapsulate(msg);
200 send(&cmsg, next_link);
201 }
202}
203
204/// @see OverlayInterface.h
205void Chord::routeMessage(const NodeID& node, const LinkID& link, Message* msg) {
206 logging_debug("Redirect over Chord to node id=" << node.toString()
207 << " link id=" << link.toString() );
208 ChordMessage cmsg(ChordMessage::route, nodeid, node);
209 cmsg.encapsulate(msg);
210 send(&cmsg, link);
211}
212
213/// @see OverlayInterface.h
214const LinkID& Chord::getNextLinkId( const NodeID& id ) const {
215 return get_next_hop(id);
216}
217
218OverlayInterface::NodeList Chord::getKnownNodes(bool deep) const {
219 OverlayInterface::NodeList nodelist;
220
221 if( deep ){
222 // all nodes that I know, fingers, succ/pred
223 for (size_t i = 0; i < table->size(); i++){
224 if ((*table)[i]->ref_count != 0
225 && !(*table)[i]->info.isUnspecified())
226 nodelist.push_back((*table)[i]->id);
227 }
228 } else {
229 // only succ and pred
230 if( table->get_predesessor() != NULL ){
231 nodelist.push_back( *(table->get_predesessor()) );
232 }
233 if( table->get_successor() != NULL ){
234 OverlayInterface::NodeList::iterator i =
235 std::find( nodelist.begin(), nodelist.end(), *(table->get_successor()) );
236 if( i == nodelist.end() )
237 nodelist.push_back( *(table->get_successor()) );
238 }
239 }
240
241 return nodelist;
242}
243
244/// @see CommunicationListener.h
245/// @see OverlayInterface.h
246void Chord::onLinkUp(const LinkID& lnk, const NodeID& remote) {
247 logging_debug("link_up: link=" << lnk.toString() << " remote=" <<
248 remote.toString() );
249 for (vector<NodeID>::iterator i=pending.begin(); i!=pending.end(); i++)
250 if (*i == remote) {
251 pending.erase(i);
252 break;
253 }
254 route_item* item = table->insert(remote);
255
256 // item added to routing table?
257 if (item != NULL) { // yes-> add to routing table
258 logging_info("new routing neighbor: " << remote.toString()
259 << " with link " << lnk.toString());
260 // replace with new link
261 if (!item->info.isUnspecified())
262 baseoverlay.dropLink(item->info);
263 item->info = lnk;
264 } else { // no-> add orphan entry to routing table
265 logging_info("new orphan: " << remote.toString()
266 << " with link " << lnk.toString());
267 table->insert_orphan(remote)->info = lnk;
268 }
269
270 discover_neighbors( lnk );
271
272 vector<LinkID>::iterator it = std::find(bootstrapLinks.begin(), bootstrapLinks.end(), lnk);
273 if( it != bootstrapLinks.end() ) {
274 discover_neighbors( lnk );
275 bootstrapLinks.erase( it );
276 }
277}
278
279/// @see CommunicationListener.h or @see OverlayInterface.h
280void Chord::onLinkDown(const LinkID& lnk, const NodeID& remote) {
281 logging_debug("link_down: link=" << lnk.toString() << " remote=" <<
282 remote.toString() );
283
284 // remove link from routing table
285 route_item* item = table->get(remote);
286 if (item!=NULL) item->info = LinkID::UNSPECIFIED;
287 table->remove(remote);
288}
289
290/// @see CommunicationListener.h
291/// @see OverlayInterface.h
292void Chord::onMessage(const DataMessage& msg, const NodeID& remote,
293 const LinkID& link) {
294
295 // decode message
296 typedef ChordMessage M;
297 M* m = msg.getMessage()->convert<ChordMessage> ();
298 if (m == NULL) return;
299
300 // handle messages
301 switch (m->getType()) {
302
303 // invalid message
304 case M::invalid:
305 break;
306
307 // route message with payload
308 case M::route: {
309 // find next hop
310 LinkID next_link = get_next_hop(m->getDestination());
311
312 // add to back-routes
313 back_route br;
314 br.id = m->getSource();
315 br.link = link;
316 br.lastseen = time(NULL);
317 back_routes.push_back(br);
318
319 // next hop == myself?
320 if (m->getDestination() == nodeid) { // yes-> route to base overlay
321 logging_debug("Send message to baseoverlay");
322 baseoverlay.incomingRouteMessage( m, next_link, remote );
323 }
324 // no-> route to next hop
325 else {
326// logging_debug("Route chord message to "
327// << item->id.toString() << " (destination=" << m->getDestination() << ")");
328 send(m, next_link);
329 }
330 break;
331 }
332
333 // discovery request
334 case M::discovery: {
335 // decapsulate message
336 Discovery* dmsg = m->decapsulate<Discovery> ();
337 logging_debug("received discovery message with"
338 << " src=" << m->getSource().toString()
339 << " dst=" << m->getDestination().toString()
340 << " ttl=" << (int)dmsg->getTTL()
341 << " type=" << (int)dmsg->getFollowType()
342 );
343
344 // check if source node can be added to routing table and setup link
345 if (m->getSource() != nodeid && table->is_insertable(m->getSource()))
346 setup(*dmsg->getSourceEndpoint(), m->getSource(), dmsg->getSourceRelay() );
347
348 // delegate discovery message
349 switch (dmsg->getFollowType()) {
350
351 // normal: route discovery message like every other message
352 case Discovery::normal: {
353 // closest node? yes-> split to follow successor and predecessor
354 if (table->is_closest_to(m->getDestination())
355 || (table->get_successor()!=NULL && *table->get_successor() == m->getDestination())
356 || (table->get_predesessor()!=NULL && *table->get_predesessor() == m->getDestination())
357 ) {
358
359 if (table->get_successor() != NULL) {
360 // send successor message
361 ChordMessage cmsg_s(*m);
362 Discovery dmsg_s(*dmsg);
363 dmsg_s.setFollowType(Discovery::successor);
364 cmsg_s.encapsulate(&dmsg_s);
365 route_item* succ_item = table->get(*table->get_successor());
366 logging_debug("Discovery split: routing discovery message to successor "
367 << succ_item->id.toString() );
368 send(&cmsg_s, succ_item->info);
369 }
370
371 // send predecessor message
372 if (table->get_predesessor() != NULL) {
373 ChordMessage cmsg_p(*m);
374 Discovery dmsg_p(*dmsg);
375 dmsg_p.setFollowType(Discovery::predecessor);
376 cmsg_p.encapsulate(&dmsg_p);
377 route_item* pred_item = table->get(
378 *table->get_predesessor());
379 logging_debug("Discovery split: routing discovery message to predecessor "
380 << pred_item->id.toString() );
381 send(&cmsg_p, pred_item->info);
382 }
383 }
384 // no-> route message
385 else {
386 // find next hop
387 LinkID next_link = get_next_hop(m->getDestination());
388 // logging_debug("routing discovery message to " <<
389 // linkitem->id.toString() );
390 send(m, next_link);
391 }
392 break;
393 }
394
395 // successor mode: follow the successor until TTL is zero
396 case Discovery::successor:
397 case Discovery::predecessor: {
398 // time to live ended? yes-> stop routing
399 if (dmsg->getTTL() == 0 || dmsg->getTTL() > 10) break;
400
401 // decrease time-to-live
402 dmsg->setTTL(dmsg->getTTL() - 1);
403
404 const route_item* item = NULL;
405 if (dmsg->getFollowType() == Discovery::successor &&
406 table->get_successor() != NULL) {
407 item = table->get(*table->get_successor());
408 } else {
409 if (table->get_predesessor()!=NULL)
410 item = table->get(*table->get_predesessor());
411 }
412 if (item == NULL) break;
413 logging_debug("routing discovery message to succ/pred "
414 << item->id.toString() );
415 ChordMessage cmsg(*m);
416 Discovery dmsg_p(*dmsg);
417 cmsg.encapsulate(&dmsg_p);
418 send(&cmsg, item->info);
419 break;
420 }}
421 delete dmsg;
422 break;
423 }
424
425 // leave
426 case M::leave: {
427 if (link!=LinkID::UNSPECIFIED) {
428 route_item* item = table->get(remote);
429 if (item!=NULL) item->info = LinkID::UNSPECIFIED;
430 table->remove(remote);
431 baseoverlay.dropLink(link);
432 }
433 break;
434 }
435 }
436 delete m;
437}
438
439void Chord::eventFunction() {
440 stabilize_counter++;
441 if (stabilize_counter < 0 || stabilize_counter == 4) {
442
443 // reset counter
444 stabilize_counter = 0;
445
446 // clear pending connections
447 pending.clear();
448
449 // get number of real neighbors
450 size_t numNeighbors = 0;
451 for (size_t i = 0; i < table->size(); i++) {
452 route_item* it = (*table)[i];
453 if (it->ref_count != 0 && !it->info.isUnspecified()) numNeighbors++;
454 }
455 logging_info("Running stabilization: #links="
456 << table->size() << " #neighbors=" << numNeighbors );
457
458 // sending discovery
459 logging_debug("Sending discovery message to my neighbors and fingers");
460 stabilize_finger = ((stabilize_finger+1) % table->get_finger_table_size() );
461 const NodeID disc1 = nodeid;
462 const NodeID disc2 = table->get_finger_table(stabilize_finger).get_compare().get_center();
463 send_discovery_to(disc1);
464 if (disc1 != disc2)
465 send_discovery_to(disc2);
466
467 for (int i=0; i<table->size(); i++) {
468 LinkID id = (*table)[i]->info;
469 if (!id.isUnspecified()) discover_neighbors(id);
470 }
471
472 // remove orphan links
473 orphan_removal_counter++;
474 if (orphan_removal_counter <0 || orphan_removal_counter >= 4) {
475 back_routes.clear();
476 logging_info("Running orphan removal");
477 orphan_removal_counter = 0;
478 for (size_t i = 0; i < table->size(); i++) {
479 route_item* it = (*table)[i];
480 if (it->ref_count == 0 && !it->info.isUnspecified()) {
481 logging_info("Dropping orphaned link " << it->info.toString() << " to " << it->id.toString());
482 table->insert(it->id);
483 if (it->ref_count==0) {
484 baseoverlay.dropLink(it->info);
485 it->info = LinkID::UNSPECIFIED;
486 }
487 }
488 }
489 }
490 }
491 logging_info("--- chord routing information ----------------------------------");
492 logging_info("predecessor: " << (table->get_predesessor()==NULL? "<none>" :
493 table->get_predesessor()->toString()) );
494 logging_info("node_id : " << nodeid.toString() );
495 logging_info("successor : " << (table->get_successor()==NULL? "<none>" :
496 table->get_successor()->toString()));
497 logging_info("----------------------------------------------------------------");
498}
499
500}} // namespace ariba, overlay
Note: See TracBrowser for help on using the repository browser.