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

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