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

Last change on this file since 5670 was 5670, checked in by mies, 15 years ago
File size: 15.0 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)2);
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)2);
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 table->insert_orphan(remote)->info = lnk;
263 }
264
265 vector<LinkID>::iterator it = std::find(bootstrapLinks.begin(), bootstrapLinks.end(), lnk);
266 if( it != bootstrapLinks.end() ) {
267 discover_neighbors( lnk );
268 bootstrapLinks.erase( it );
269 }
270}
271
272/// @see CommunicationListener.h or @see OverlayInterface.h
273void Chord::onLinkDown(const LinkID& lnk, const NodeID& remote) {
274 logging_debug("link_down: link=" << lnk.toString() << " remote=" <<
275 remote.toString() );
276
277 // remove link from routing table
278 route_item* item = table->get(remote);
279 if (item!=NULL) item->info = LinkID::UNSPECIFIED;
280 table->remove(remote);
281}
282
283/// @see CommunicationListener.h
284/// @see OverlayInterface.h
285void Chord::onMessage(const DataMessage& msg, const NodeID& remote,
286 const LinkID& link) {
287
288 // decode message
289 typedef ChordMessage M;
290 M* m = msg.getMessage()->convert<ChordMessage> ();
291 if (m == NULL) return;
292
293 // handle messages
294 switch (m->getType()) {
295
296 // invalid message
297 case M::invalid:
298 break;
299
300 // route message with payload
301 case M::route: {
302 // find next hop
303 const route_item* item = table->get_next_hop(m->getDestination());
304
305 // next hop == myself?
306 if (m->getDestination() == nodeid) { // yes-> route to base overlay
307 logging_debug("Send message to baseoverlay");
308 baseoverlay.incomingRouteMessage( m, item->info, remote );
309 }
310 // no-> route to next hop
311 else {
312 logging_debug("Route chord message to "
313 << item->id.toString() << " (destination=" << m->getDestination() << ")");
314 send(m, item->info);
315 }
316 break;
317 }
318
319 // discovery request
320 case M::discovery: {
321 // decapsulate message
322 Discovery* dmsg = m->decapsulate<Discovery> ();
323 logging_info("received discovery message with"
324 << " src=" << m->getSource().toString()
325 << " dst=" << m->getDestination().toString()
326 << " ttl=" << (int)dmsg->getTTL()
327 << " type=" << (int)dmsg->getFollowType()
328 );
329
330 // check if source node can be added to routing table and setup link
331 if (m->getSource() != nodeid && table->is_insertable(m->getSource()))
332 setup(*dmsg->getSourceEndpoint(), m->getSource() );
333
334 // delegate discovery message
335 switch (dmsg->getFollowType()) {
336
337 // normal: route discovery message like every other message
338 case Discovery::normal: {
339 // closest node? yes-> split to follow successor and predecessor
340 if (table->is_closest_to(m->getDestination())) {
341
342 if (table->get_successor() != NULL) {
343 // send successor message
344 ChordMessage cmsg_s(*m);
345 Discovery dmsg_s(*dmsg);
346 dmsg_s.setFollowType(Discovery::successor);
347 cmsg_s.encapsulate(&dmsg_s);
348 route_item* succ_item = table->get(*table->get_successor());
349 logging_info("Discovery split: routing discovery message to successor "
350 << succ_item->id.toString() );
351 send(&cmsg_s, succ_item->info);
352 }
353
354 // send predecessor message
355 if (table->get_predesessor() != NULL) {
356 ChordMessage cmsg_p(*m);
357 Discovery dmsg_p(*dmsg);
358 dmsg_p.setFollowType(Discovery::predecessor);
359 cmsg_p.encapsulate(&dmsg_p);
360 route_item* pred_item = table->get(
361 *table->get_predesessor());
362 logging_info("Discovery split: routing discovery message to predecessor "
363 << pred_item->id.toString() );
364 send(&cmsg_p, pred_item->info);
365 }
366 }
367 // no-> route message
368 else {
369 // find next hop
370 const route_item* item = table->get_next_hop(
371 m->getDestination(),true);
372 if (item == NULL || item->id == nodeid) break;
373 logging_debug("routing discovery message to " <<
374 item->id.toString() );
375 ChordMessage cmsg_p(*m);
376 Discovery dmsg_p(*dmsg);
377 cmsg_p.encapsulate(&dmsg_p);
378 send(&cmsg_p, item->info);
379 }
380 break;
381 }
382
383 // successor mode: follow the successor until TTL is zero
384 case Discovery::successor:
385 case Discovery::predecessor: {
386 // time to live ended? yes-> stop routing
387 if (dmsg->getTTL() == 0) break;
388
389 // decrease time-to-live
390 dmsg->setTTL(dmsg->getTTL() - 1);
391
392 const route_item* item = NULL;
393 if (dmsg->getFollowType() == Discovery::successor &&
394 table->get_successor() != NULL) {
395 item = table->get(*table->get_successor());
396 } else {
397 if (table->get_predesessor()!=NULL)
398 item = table->get(*table->get_predesessor());
399 }
400 if (item == NULL) break;
401 logging_debug("routing discovery message to succ/pred "
402 << item->id.toString() );
403 ChordMessage cmsg(*m);
404 Discovery dmsg_p(*dmsg);
405 cmsg.encapsulate(&dmsg_p);
406 send(&cmsg, item->info);
407 break;
408 }
409 }
410 delete dmsg;
411 break;
412 }
413
414 // leave
415 case M::leave: {
416 if (link!=LinkID::UNSPECIFIED) {
417 route_item* item = table->get(remote);
418 if (item!=NULL) item->info = LinkID::UNSPECIFIED;
419 table->remove(remote);
420 baseoverlay.dropLink(link);
421 }
422 break;
423 }
424 }
425}
426
427void Chord::eventFunction() {
428 stabilize_counter++;
429 if (stabilize_counter < 0 || stabilize_counter == 1) {
430
431 // reset counter
432 stabilize_counter = 0;
433
434 // clear pending connections
435 pending.clear();
436
437 // get number of real neighbors
438 size_t numNeighbors = 0;
439 for (size_t i = 0; i < table->size(); i++) {
440 route_item* it = (*table)[i];
441 if (it->ref_count != 0 && !it->info.isUnspecified()) numNeighbors++;
442 }
443 logging_info("Running stabilization: #links="
444 << table->size() << " #neighbors=" << numNeighbors );
445
446 // sending discovery
447 logging_debug("Sending discovery message to my neighbors and fingers");
448 stabilize_finger = ((stabilize_finger+1) % table->get_finger_table_size() );
449 const NodeID disc1 = nodeid;
450 const NodeID disc2 = table->get_finger_table(stabilize_finger).get_compare().get_center();
451 send_discovery_to(disc1);
452
453 if (table->get_successor() != NULL) {
454 route_item* succ_item = table->get(*table->get_successor());
455 if (!succ_item->info.isUnspecified()) discover_neighbors(succ_item->info);
456 }
457
458 // send predecessor message
459 if (table->get_predesessor() != NULL) {
460 route_item* pred_item = table->get(*table->get_predesessor());
461 if (!succ_item->info.isUnspecified()) discover_neighbors(pred_item->info);
462 }
463
464 if (disc1 != disc2)
465 send_discovery_to(disc2);
466
467 // remove orphan links
468 orphan_removal_counter++;
469 if (orphan_removal_counter <0 || orphan_removal_counter >= 2) {
470 logging_info("Running orphan removal");
471 orphan_removal_counter = 0;
472 for (size_t i = 0; i < table->size(); i++) {
473 route_item* it = (*table)[i];
474 if (it->ref_count == 0 && !it->info.isUnspecified()) {
475 logging_info("Dropping orphaned link " << it->info.toString() << " to " << it->id.toString());
476// baseoverlay.dropLink(it->info);
477// it->info = LinkID::UNSPECIFIED;
478 }
479 }
480 }
481 }
482 logging_info("--- chord routing information ----------------------------------");
483 logging_info("predecessor: " << (table->get_predesessor()==NULL? "<none>" :
484 table->get_predesessor()->toString()) );
485 logging_info("node_id : " << nodeid.toString() );
486 logging_info("successor : " << (table->get_successor()==NULL? "<none>" :
487 table->get_successor()->toString()));
488 logging_info("----------------------------------------------------------------");
489}
490
491}} // namespace ariba, overlay
Note: See TracBrowser for help on using the repository browser.