close Warning: Can't use blame annotator:
No changeset 2259 in the repository

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

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