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

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