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

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