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

Last change on this file since 5772 was 5761, checked in by mies, 15 years ago
File size: 15.5 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 bool found = false;
314 BOOST_FOREACH( back_route br, back_routes) {
315 if (br.id == m->getSource()) {
316 br.link = link;
317 br.lastseen = time(NULL);
318 found = true;
319 break;
320 }
321 }
322 if (!found) {
323 back_route br;
324 br.id = m->getSource();
325 br.link = link;
326 br.lastseen = time(NULL);
327 back_routes.push_back(br);
328 }
329
330 // next hop == myself?
331 if (m->getDestination() == nodeid) { // yes-> route to base overlay
332 logging_debug("Send message to baseoverlay");
333 baseoverlay.incomingRouteMessage( m, next_link, remote );
334 }
335 // no-> route to next hop
336 else {
337// logging_debug("Route chord message to "
338// << item->id.toString() << " (destination=" << m->getDestination() << ")");
339 send(m, next_link);
340 }
341 break;
342 }
343
344 // discovery request
345 case M::discovery: {
346 // decapsulate message
347 Discovery* dmsg = m->decapsulate<Discovery> ();
348 logging_debug("received discovery message with"
349 << " src=" << m->getSource().toString()
350 << " dst=" << m->getDestination().toString()
351 << " ttl=" << (int)dmsg->getTTL()
352 << " type=" << (int)dmsg->getFollowType()
353 );
354
355 // check if source node can be added to routing table and setup link
356 if (m->getSource() != nodeid && table->is_insertable(m->getSource()))
357 setup(*dmsg->getSourceEndpoint(), m->getSource(), dmsg->getSourceRelay() );
358
359 // delegate discovery message
360 switch (dmsg->getFollowType()) {
361
362 // normal: route discovery message like every other message
363 case Discovery::normal: {
364 // closest node? yes-> split to follow successor and predecessor
365 if (table->is_closest_to(m->getDestination())
366 || (table->get_successor()!=NULL && *table->get_successor() == m->getDestination())
367 || (table->get_predesessor()!=NULL && *table->get_predesessor() == m->getDestination())
368 ) {
369
370 if (table->get_successor() != NULL) {
371 // send successor message
372 ChordMessage cmsg_s(*m);
373 Discovery dmsg_s(*dmsg);
374 dmsg_s.setFollowType(Discovery::successor);
375 cmsg_s.encapsulate(&dmsg_s);
376 route_item* succ_item = table->get(*table->get_successor());
377 logging_debug("Discovery split: routing discovery message to successor "
378 << succ_item->id.toString() );
379 send(&cmsg_s, succ_item->info);
380 }
381
382 // send predecessor message
383 if (table->get_predesessor() != NULL) {
384 ChordMessage cmsg_p(*m);
385 Discovery dmsg_p(*dmsg);
386 dmsg_p.setFollowType(Discovery::predecessor);
387 cmsg_p.encapsulate(&dmsg_p);
388 route_item* pred_item = table->get(
389 *table->get_predesessor());
390 logging_debug("Discovery split: routing discovery message to predecessor "
391 << pred_item->id.toString() );
392 send(&cmsg_p, pred_item->info);
393 }
394 }
395 // no-> route message
396 else {
397 // find next hop
398 LinkID next_link = get_next_hop(m->getDestination());
399 // logging_debug("routing discovery message to " <<
400 // linkitem->id.toString() );
401 send(m, next_link);
402 }
403 break;
404 }
405
406 // successor mode: follow the successor until TTL is zero
407 case Discovery::successor:
408 case Discovery::predecessor: {
409 // time to live ended? yes-> stop routing
410 if (dmsg->getTTL() == 0 || dmsg->getTTL() > 10) break;
411
412 // decrease time-to-live
413 dmsg->setTTL(dmsg->getTTL() - 1);
414
415 const route_item* item = NULL;
416 if (dmsg->getFollowType() == Discovery::successor &&
417 table->get_successor() != NULL) {
418 item = table->get(*table->get_successor());
419 } else {
420 if (table->get_predesessor()!=NULL)
421 item = table->get(*table->get_predesessor());
422 }
423 if (item == NULL) break;
424 logging_debug("routing discovery message to succ/pred "
425 << item->id.toString() );
426 ChordMessage cmsg(*m);
427 Discovery dmsg_p(*dmsg);
428 cmsg.encapsulate(&dmsg_p);
429 send(&cmsg, item->info);
430 break;
431 }}
432 delete dmsg;
433 break;
434 }
435
436 // leave
437 case M::leave: {
438 if (link!=LinkID::UNSPECIFIED) {
439 route_item* item = table->get(remote);
440 if (item!=NULL) item->info = LinkID::UNSPECIFIED;
441 table->remove(remote);
442 baseoverlay.dropLink(link);
443 }
444 break;
445 }
446 }
447 delete m;
448}
449
450void Chord::eventFunction() {
451 stabilize_counter++;
452 if (stabilize_counter < 0 || stabilize_counter == 4) {
453
454 // reset counter
455 stabilize_counter = 0;
456
457 // clear pending connections
458 pending.clear();
459
460 // get number of real neighbors
461 size_t numNeighbors = 0;
462 for (size_t i = 0; i < table->size(); i++) {
463 route_item* it = (*table)[i];
464 if (it->ref_count != 0 && !it->info.isUnspecified()) numNeighbors++;
465 }
466 logging_info("Running stabilization: #links="
467 << table->size() << " #neighbors=" << numNeighbors );
468
469 // sending discovery
470 logging_debug("Sending discovery message to my neighbors and fingers");
471 stabilize_finger = ((stabilize_finger+1) % table->get_finger_table_size() );
472 const NodeID disc1 = nodeid;
473 const NodeID disc2 = table->get_finger_table(stabilize_finger).get_compare().get_center();
474 send_discovery_to(disc1);
475 if (disc1 != disc2)
476 send_discovery_to(disc2);
477
478 for (int i=0; i<table->size(); i++) {
479 LinkID id = (*table)[i]->info;
480 if (!id.isUnspecified()) discover_neighbors(id);
481 }
482
483 // remove orphan links
484 orphan_removal_counter++;
485 if (orphan_removal_counter <0 || orphan_removal_counter >= 4) {
486 back_routes.clear();
487 logging_info("Running orphan removal");
488 orphan_removal_counter = 0;
489 for (size_t i = 0; i < table->size(); i++) {
490 route_item* it = (*table)[i];
491 if (it->ref_count == 0 && !it->info.isUnspecified()) {
492 logging_info("Dropping orphaned link " << it->info.toString() << " to " << it->id.toString());
493 table->insert(it->id);
494 if (it->ref_count==0) {
495 baseoverlay.dropLink(it->info);
496 it->info = LinkID::UNSPECIFIED;
497 }
498 }
499 }
500 }
501 }
502 logging_info("--- chord routing information ----------------------------------");
503 logging_info("predecessor: " << (table->get_predesessor()==NULL? "<none>" :
504 table->get_predesessor()->toString()) );
505 logging_info("node_id : " << nodeid.toString() );
506 logging_info("successor : " << (table->get_successor()==NULL? "<none>" :
507 table->get_successor()->toString()));
508 logging_info("----------------------------------------------------------------");
509}
510
511}} // namespace ariba, overlay
Note: See TracBrowser for help on using the repository browser.