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

Last change on this file since 5555 was 5555, checked in by mies, 15 years ago
File size: 13.5 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(1000);
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, nodeid);
232 Discovery dmsg;
233 dmsg.setSourceEndpoint(&baseoverlay.getEndpointDescriptor());
234 dmsg.setFollowType(Discovery::normal);
235 dmsg.setTTL((uint8_t) 4);
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 ChordMessage cmsg_p(*m);
346 Discovery dmsg_p(*dmsg);
347 cmsg_p.encapsulate(&dmsg_p);
348 send(&cmsg_p, item->info);
349 }
350 break;
351
352 // successor mode: follow the successor until TTL is zero
353 case Discovery::successor:
354 case Discovery::predecessor: {
355 // time to live ended? yes-> stop routing
356 if (dmsg->getTTL() == 0) break;
357
358 // decrease time-to-live
359 dmsg->setTTL(dmsg->getTTL() - 1);
360
361 const route_item* item = NULL;
362 if (dmsg->getFollowType() == Discovery::successor &&
363 table->get_successor() != NULL) {
364 item = table->get(*table->get_successor());
365 } else {
366 if (table->get_predesessor()!=NULL)
367 item = table->get(*table->get_predesessor());
368 }
369 if (item == NULL) break;
370 logging_debug("routing discovery message to succ/pred "
371 << item->id.toString() );
372 ChordMessage cmsg(*m);
373 cmsg.encapsulate(dmsg);
374 send(&cmsg, item->info);
375 break;
376 }
377 }
378 break;
379 }
380
381 // leave
382 case M::leave: {
383 if (link!=LinkID::UNSPECIFIED) {
384 route_item* item = table->get(remote);
385 if (item!=NULL) item->info = LinkID::UNSPECIFIED;
386 table->remove(remote);
387 baseoverlay.dropLink(link);
388 }
389 break;
390 }
391 }
392}
393
394void Chord::eventFunction() {
395 stabilize_counter++;
396 if (stabilize_counter == 3) {
397 pending.clear();
398 size_t numNeighbors = 0;
399 for (size_t i = 0; i < table->size(); i++) {
400 route_item* it = (*table)[i];
401 if (it->ref_count != 0 && !it->info.isUnspecified()) numNeighbors++;
402 }
403 logging_info("Running stabilization: #links="
404 << table->size() << " #neighbors=" << numNeighbors );
405 stabilize_counter = 0;
406 stabilize_finger = ((stabilize_finger+1) % table->get_finger_table_size() );
407 logging_debug("Sending discovery message to my neighbors and fingers");
408 const NodeID& disc1 = nodeid;
409 const NodeID& disc2 = table->get_finger_table(stabilize_finger).get_compare().get_center();
410 send_discovery_to(disc1);
411 if (disc1 != disc2) send_discovery_to(disc2);
412 orphan_removal_counter++;
413 if (orphan_removal_counter >= 2) {
414 logging_info("Running orphan removal");
415 orphan_removal_counter = 0;
416 for (size_t i = 0; i < table->size(); i++) {
417 route_item* it = (*table)[i];
418 if (it->ref_count == 0 && !it->info.isUnspecified()) {
419 logging_info("Dropping orphaned link " << it->info.toString() << " to " << it->id.toString());
420 baseoverlay.dropLink(it->info);
421 it->info = LinkID::UNSPECIFIED;
422 }
423 }
424 }
425 }
426 logging_debug("--- chord routing information ----------------------------------");
427 logging_debug("predecessor: " << (table->get_predesessor()==NULL? "<none>" :
428 table->get_predesessor()->toString()) );
429 logging_debug("node_id : " << nodeid.toString() );
430 logging_debug("successor : " << (table->get_successor()==NULL? "<none>" :
431 table->get_successor()->toString()));
432 logging_debug("----------------------------------------------------------------");
433}
434
435}} // namespace ariba, overlay
Note: See TracBrowser for help on using the repository browser.