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

Last change on this file since 5546 was 5546, checked in by mies, 15 years ago
File size: 13.4 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
194 OverlayInterface::NodeList::iterator i =
195 std::find( nodelist.begin(), nodelist.end(), *(table->get_successor()) );
196 if( i == nodelist.end() )
197 nodelist.push_back( *(table->get_successor()) );
198 }
199 }
200
201 return nodelist;
202}
203
204/// @see CommunicationListener.h
205/// @see OverlayInterface.h
206void Chord::onLinkUp(const LinkID& lnk, const NodeID& remote) {
207 logging_debug("link_up: link=" << lnk.toString() << " remote=" <<
208 remote.toString() );
209 for (vector<NodeID>::iterator i=pending.begin(); i!=pending.end(); i++)
210 if (*i == remote) {
211 pending.erase(i);
212 break;
213 }
214 route_item* item = table->insert(remote);
215
216 // item added to routing table?
217 if (item != NULL) { // yes-> add to routing table
218 logging_info("new routing neighbor: " << remote.toString()
219 << " with link " << lnk.toString());
220 item->info = lnk;
221 } else { // no-> add orphan entry to routing table
222 logging_info("new orphan: " << remote.toString()
223 << " with link " << lnk.toString());
224 table->insert_orphan(remote)->info = lnk;
225 }
226
227 vector<LinkID>::iterator it = std::find(bootstrapLinks.begin(), bootstrapLinks.end(), lnk);
228 if( it != bootstrapLinks.end() ) {
229 // send discovery over bootstrap
230 Message msg;
231 ChordMessage cmsg(ChordMessage::discovery, nodeid, destination);
232 Discovery dmsg;
233 dmsg.setSourceEndpoint(&baseoverlay.getEndpointDescriptor());
234 dmsg.setFollowType(Discovery::normal);
235 dmsg.setTTL((uint8_t) ttl);
236 cmsg.encapsulate(&dmsg);
237 msg.encapsulate(&cmsg);
238 send(&msg, lnk);
239 bootstrapLinks.erase( it );
240 }
241}
242
243/// @see CommunicationListener.h or @see OverlayInterface.h
244void Chord::onLinkDown(const LinkID& lnk, const NodeID& remote) {
245 logging_debug("link_down: link=" << lnk.toString() << " remote=" <<
246 remote.toString() );
247
248 // remove link from routing table
249 route_item* item = table->get(remote);
250 if (item!=NULL) item->info = LinkID::UNSPECIFIED;
251 table->remove(remote);
252}
253
254/// @see CommunicationListener.h
255/// @see OverlayInterface.h
256void Chord::onMessage(const DataMessage& msg, const NodeID& remote,
257 const LinkID& link) {
258
259 // decode message
260 typedef ChordMessage M;
261 M* m = msg.getMessage()->convert<ChordMessage> ();
262 if (m == NULL) return;
263
264 // handle messages
265 switch (m->getType()) {
266
267 // invalid message
268 case M::invalid:
269 break;
270
271 // route message with payload
272 case M::route: {
273 // find next hop
274 const route_item* item = table->get_next_hop(m->getDestination());
275
276 // next hop == myself?
277 if (m->getDestination() == nodeid) { // yes-> route to base overlay
278 logging_debug("Send message to baseoverlay");
279 baseoverlay.incomingRouteMessage( m, item->info, remote );
280 }
281 // no-> route to next hop
282 else {
283 logging_debug("Route chord message to "
284 << item->id.toString() << " (destination=" << m->getDestination() << ")");
285 send(m, item->info);
286 }
287 break;
288 }
289
290 // discovery request
291 case M::discovery: {
292 // decapsulate message
293 Discovery* dmsg = m->decapsulate<Discovery> ();
294 logging_debug("received discovery message with"
295 << " dest=" << m->getDestination().toString()
296 << " ttl=" << (int)dmsg->getTTL()
297 << " type=" << (int)dmsg->getFollowType()
298 );
299
300 // check if source node can be added to routing table and setup link
301 if (m->getSource() != nodeid && table->is_insertable(m->getSource()))
302 setup(*dmsg->getSourceEndpoint(), m->getSource() );
303
304 // delegate discovery message
305 switch (dmsg->getFollowType()) {
306
307 // normal: route discovery message like every other message
308 case Discovery::normal:
309 // closest node? yes-> split to follow successor and predecessor
310 if (table->is_closest_to(m->getDestination())) {
311
312 if (table->get_successor() != NULL) {
313 // send successor message
314 ChordMessage cmsg_s(*m);
315 Discovery dmsg_s(*dmsg);
316 dmsg_s.setFollowType(Discovery::successor);
317 cmsg_s.encapsulate(&dmsg_s);
318 route_item* succ_item = table->get(*table->get_successor());
319 logging_debug("split: routing discovery message to successor "
320 << succ_item->id.toString() );
321 send(&cmsg_s, succ_item->info);
322 }
323
324 // send predecessor message
325 if (table->get_predesessor() != NULL) {
326 ChordMessage cmsg_p(*m);
327 Discovery dmsg_p(*dmsg);
328 dmsg_p.setFollowType(Discovery::predecessor);
329 cmsg_p.encapsulate(&dmsg_p);
330 route_item* pred_item = table->get(
331 *table->get_predesessor());
332 logging_debug("split: routing discovery message to predecessor "
333 << pred_item->id.toString() );
334 send(&cmsg_p, pred_item->info);
335 }
336 }
337 // no-> route message
338 else {
339 // find next hop
340 const route_item* item = table->get_next_hop(
341 m->getDestination());
342 if (item->id == nodeid) break;
343 logging_debug("routing discovery message to " <<
344 item->id.toString() );
345 send(m, item->info);
346 }
347 break;
348
349 // successor mode: follow the successor until TTL is zero
350 case Discovery::successor:
351 case Discovery::predecessor: {
352 // time to live ended? yes-> stop routing
353 if (dmsg->getTTL() == 0) break;
354
355 // decrease time-to-live
356 dmsg->setTTL(dmsg->getTTL() - 1);
357
358 const route_item* item = NULL;
359 if (dmsg->getFollowType() == Discovery::successor &&
360 table->get_successor() != NULL) {
361 item = table->get(*table->get_successor());
362 } else {
363 if (table->get_predesessor()!=NULL)
364 item = table->get(*table->get_predesessor());
365 }
366 if (item == NULL) break;
367 logging_debug("routing discovery message to succ/pred "
368 << item->id.toString() );
369 ChordMessage cmsg(*m);
370 cmsg.encapsulate(dmsg);
371 send(&cmsg, item->info);
372 break;
373 }
374 }
375 break;
376 }
377
378 // leave
379 case M::leave: {
380 if (link!=LinkID::UNSPECIFIED) {
381 route_item* item = table->get(remote);
382 if (item!=NULL) item->info = LinkID::UNSPECIFIED;
383 table->remove(remote);
384 baseoverlay.dropLink(link);
385 }
386 break;
387 }
388 }
389}
390
391void Chord::eventFunction() {
392 stabilize_counter++;
393 if (stabilize_counter == 3) {
394 pending.clear();
395 size_t numNeighbors = 0;
396 for (size_t i = 0; i < table->size(); i++) {
397 route_item* it = (*table)[i];
398 if (it->ref_count != 0 && !it->info.isUnspecified()) numNeighbors++;
399 }
400 logging_info("Running stabilization: #links="
401 << table->size() << " #neighbors=" << numNeighbors );
402 stabilize_counter = 0;
403 stabilize_finger = ((stabilize_finger+1) % table->get_finger_table_size() );
404 logging_debug("Sending discovery message to my neighbors and fingers");
405 const NodeID& disc1 = nodeid;
406 const NodeID& disc2 = table->get_finger_table(stabilize_finger).get_compare().get_center();
407 send_discovery_to(disc1);
408 if (disc1 != disc2) send_discovery_to(disc2);
409 orphan_removal_counter++;
410 if (orphan_removal_counter >= 2) {
411 logging_info("Running orphan removal");
412 orphan_removal_counter = 0;
413 for (size_t i = 0; i < table->size(); i++) {
414 route_item* it = (*table)[i];
415 if (it->ref_count == 0 && !it->info.isUnspecified()) {
416 logging_info("Dropping orphaned link " << it->info.toString() << " to " << it->id.toString());
417 baseoverlay.dropLink(it->info);
418 it->info = LinkID::UNSPECIFIED;
419 }
420 }
421 }
422 }
423 logging_debug("--- chord routing information ----------------------------------");
424 logging_debug("predecessor: " << (table->get_predesessor()==NULL? "<none>" :
425 table->get_predesessor()->toString()) );
426 logging_debug("node_id : " << nodeid.toString() );
427 logging_debug("successor : " << (table->get_successor()==NULL? "<none>" :
428 table->get_successor()->toString()));
429 logging_debug("----------------------------------------------------------------");
430}
431
432}} // namespace ariba, overlay
Note: See TracBrowser for help on using the repository browser.