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

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