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

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