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

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