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

Last change on this file since 5874 was 5871, checked in by Christoph Mayer, 15 years ago

merge noch nicht fertig

File size: 13.7 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#include "ariba/overlay/messages/OverlayMsg.h"
41
42#include "Chord.h"
43#include "detail/chord_routing_table.hpp"
44
45#include "messages/Discovery.h"
46
47namespace ariba {
48namespace overlay {
49
50enum signalMessageTypes {
51 typeDiscovery = OverlayMsg::typeSignalingStart + 0x01,
52 typeLeave = OverlayMsg::typeSignalingStart + 0x02,
53};
54
55typedef chord_routing_table::item route_item;
56
57use_logging_cpp( Chord );
58
59Chord::Chord(BaseOverlay& _baseoverlay, const NodeID& _nodeid,
60 OverlayStructureEvents* _eventsReceiver, const OverlayParameterSet& param) :
61 OverlayInterface(_baseoverlay, _nodeid, _eventsReceiver, param) {
62
63 // create routing table
64 this->table = new chord_routing_table(_nodeid, 4);
65 orphan_removal_counter = 0;
66 discovery_count = 0;
67 stabilize_counter = 0;
68 stabilize_finger = 0;
69}
70
71Chord::~Chord() {
72
73 // delete routing table
74 delete table;
75}
76
77/// helper: sets up a link using the base overlay
78LinkID Chord::setup(const EndpointDescriptor& endpoint, const NodeID& remote ) {
79
80 // check if we already have a connection
81 for (int i=0; i<table->size(); i++)
82 if ((*table)[i]->ref_count > 0 && (*table)[i]->id == remote && !((*table)[i]->info.isUnspecified()))
83 return LinkID::UNSPECIFIED;
84
85 // check if we are already trying to establish a link
86 for (size_t i=0; i<pending.size(); i++)
87 if ( pending[i] == remote ) {
88 logging_debug("Already trying to establish a link to node "
89 << remote.toString() );
90 return LinkID::UNSPECIFIED;
91 }
92
93 // adding node to list of pending connections
94 pending.push_back( remote );
95
96 logging_info("Request to setup link to " << endpoint.toString() );
97
98 // establish link via base overlay
99 return baseoverlay.establishLink( endpoint, remote,
100 OverlayInterface::OVERLAY_SERVICE_ID );
101}
102
103/// helper: sends a message using the "base overlay"
104seqnum_t Chord::send( OverlayMsg* msg, const LinkID& link ) {
105 if (link.isUnspecified()) return 0;
106 msg->setRelayed(true);
107 return baseoverlay.send_link( msg, link );
108}
109
110/// sends a discovery message
111void Chord::send_discovery_to(const NodeID& remote, int ttl) {
112 LinkID link = getNextLinkId(remote);
113 if ( remote == nodeid || link.isUnspecified()) return;
114 if ( table->size() == 0 ) return;
115
116 OverlayMsg msg( typeDiscovery );
117 msg.setRelayed(true);
118 Discovery dmsg( Discovery::normal, (uint8_t)2, baseoverlay.getEndpointDescriptor() );
119 msg.encapsulate(&dmsg);
120
121 // send to node
122 baseoverlay.send_node( &msg, remote );
123}
124
125void Chord::discover_neighbors( const LinkID& link ) {
126 uint8_t ttl = 2;
127 {
128 // send predecessor discovery
129 OverlayMsg msg( typeDiscovery );
130 msg.setRelayed(true);
131 Discovery dmsg( Discovery::predecessor, ttl,
132 baseoverlay.getEndpointDescriptor() );
133 msg.encapsulate(&dmsg);
134 send(&msg, link);
135 }
136 {
137 // send successor discovery
138 OverlayMsg msg( typeDiscovery );
139 msg.setSourceEndpoint( baseoverlay.getEndpointDescriptor() );
140 msg.setRelayed(true);
141 Discovery dmsg( Discovery::successor, ttl,
142 baseoverlay.getEndpointDescriptor() );
143 msg.encapsulate(&dmsg);
144 send(&msg, link);
145 }
146}
147
148
149void Chord::createOverlay() {
150}
151
152void Chord::deleteOverlay() {
153
154}
155
156void Chord::joinOverlay(const EndpointDescriptor& boot) {
157 logging_info( "joining Chord overlay structure through end-point " <<
158 (boot.isUnspecified() ? "local" : boot.toString()) );
159
160 // initiator? no->setup first link
161 if (!boot.isUnspecified())
162 bootstrapLinks.push_back( setup(boot) );
163
164 // timer for stabilization management
165 Timer::setInterval(1000);
166 Timer::start();
167}
168
169void Chord::leaveOverlay() {
170 Timer::stop();
171 for (size_t i = 0; i < table->size(); i++) {
172 route_item* it = (*table)[i];
173 OverlayMsg msg( typeLeave );
174 send( &msg, it->info );
175 }
176}
177
178/// @see OverlayInterface.h
179const EndpointDescriptor& Chord::resolveNode(const NodeID& node) {
180 const route_item* item = table->get(node);
181 if (item == NULL || item->info.isUnspecified()) return EndpointDescriptor::UNSPECIFIED();
182 return baseoverlay.getEndpointDescriptor(item->info);
183}
184
185/// @see OverlayInterface.h
186const LinkID& Chord::getNextLinkId( const NodeID& id ) const {
187 // get next hop
188 const route_item* item = table->get_next_hop(id);
189
190 // returns a unspecified id when this is itself
191 if (item == NULL || item->id == nodeid)
192 return LinkID::UNSPECIFIED;
193
194 /// return routing info
195 return item->info;
196}
197
198OverlayInterface::NodeList Chord::getKnownNodes(bool deep) const {
199 OverlayInterface::NodeList nodelist;
200
201 if( deep ){
202 // all nodes that I know, fingers, succ/pred
203 for (size_t i = 0; i < table->size(); i++){
204 if ((*table)[i]->ref_count != 0
205 && !(*table)[i]->info.isUnspecified())
206 nodelist.push_back((*table)[i]->id);
207 }
208 } else {
209 // only succ and pred
210 if( table->get_predesessor() != NULL ){
211 nodelist.push_back( *(table->get_predesessor()) );
212 }
213 if( table->get_successor() != NULL ){
214 OverlayInterface::NodeList::iterator i =
215 std::find( nodelist.begin(), nodelist.end(), *(table->get_successor()) );
216 if( i == nodelist.end() )
217 nodelist.push_back( *(table->get_successor()) );
218 }
219 }
220
221 return nodelist;
222}
223
224/// @see CommunicationListener.h
225/// @see OverlayInterface.h
226void Chord::onLinkUp(const LinkID& lnk, const NodeID& remote) {
227 logging_info("link_up: link=" << lnk.toString() << " remote=" <<
228 remote.toString() );
229 for (vector<NodeID>::iterator i=pending.begin(); i!=pending.end(); i++)
230 if (*i == remote) {
231 pending.erase(i);
232 break;
233 }
234/*
235 // check if we already have a connection, yes-> do not handle duplicate!
236 for (int i=0; i<table->size(); i++)
237 if ((*table)[i]->id == remote && !((*table)[i]->info.isUnspecified()) && (*table)[i]->info != lnk) {
238
239 return;
240 }
241*/
242 if (remote==nodeid) {
243 baseoverlay.dropLink(lnk);
244 return;
245 }
246
247 route_item* item = table->insert(remote);
248
249 // item added to routing table?
250 if (item != NULL) { // yes-> add to routing table
251 logging_info("new routing neighbor: " << remote.toString()
252 << " with link " << lnk.toString());
253 // replace with new link
254 if (!item->info.isUnspecified() || item->info!=lnk)
255 baseoverlay.dropLink(item->info);
256 item->info = lnk;
257 // discover neighbors of new overlay neighbor
258 discover_neighbors( lnk );
259 showLinks();
260 } else { // no-> add orphan entry to routing table
261 logging_info("new orphan: " << remote.toString()
262 << " with link " << lnk.toString());
263 table->insert_orphan(remote)->info = lnk;
264 }
265
266 // erase bootstrap link
267 vector<LinkID>::iterator it = std::find(bootstrapLinks.begin(), bootstrapLinks.end(), lnk);
268 if( it != bootstrapLinks.end() ) bootstrapLinks.erase( it );
269}
270
271/// @see CommunicationListener.h or @see OverlayInterface.h
272void Chord::onLinkDown(const LinkID& lnk, const NodeID& remote) {
273 logging_debug("link_down: link=" << lnk.toString() << " remote=" <<
274 remote.toString() );
275
276 // remove link from routing table
277 route_item* item = table->get(remote);
278 if (item!=NULL && item->info==lnk) {
279 item->info = LinkID::UNSPECIFIED;
280 table->remove(remote);
281 }
282}
283
284/// @see CommunicationListener.h
285/// @see OverlayInterface.h
286void Chord::onMessage(const DataMessage& msg, const NodeID& remote,
287 const LinkID& link) {
288
289 // decode message
290 OverlayMsg* m = dynamic_cast<OverlayMsg*>(msg.getMessage());
291 if (m == NULL) return;
292
293 // handle messages
294 switch (m->getType()) {
295
296 // discovery request
297 case typeDiscovery: {
298 // decapsulate message
299 Discovery* dmsg = m->decapsulate<Discovery> ();
300 logging_debug("Received discovery message with"
301 << " src=" << m->getSourceNode().toString()
302 << " dst=" << m->getDestinationNode().toString()
303 << " ttl=" << (int)dmsg->getTTL()
304 << " type=" << (int)dmsg->getType()
305 );
306
307 // check if source node can be added to routing table and setup link
308 if (m->getSourceNode() != nodeid
309 && table->is_insertable(m->getSourceNode()))
310 setup( dmsg->getEndpoint(), m->getSourceNode() );
311
312 // delegate discovery message
313 switch (dmsg->getType()) {
314
315 // normal: route discovery message like every other message
316 case Discovery::normal: {
317 // closest node? yes-> split to follow successor and predecessor
318 if ( table->is_closest_to(m->getDestinationNode()) ) {
319
320 if (table->get_successor() != NULL) {
321 OverlayMsg omsg(*m);
322 dmsg->setType(Discovery::successor);
323 omsg.encapsulate(dmsg);
324 route_item* succ_item = table->get(*table->get_successor());
325 logging_debug("Discovery split: routing discovery message to successor "
326 << succ_item->id.toString() );
327 send(&omsg, succ_item->info);
328 }
329
330 // send predecessor message
331 if (table->get_predesessor() != NULL) {
332 OverlayMsg omsg(*m);
333 dmsg->setType(Discovery::predecessor);
334 omsg.encapsulate(dmsg);
335 route_item* pred_item = table->get(
336 *table->get_predesessor());
337 logging_debug("Discovery split: routing discovery message to predecessor "
338 << pred_item->id.toString() );
339 send( &omsg, pred_item->info);
340 }
341 }
342 // no-> route message
343 else {
344 baseoverlay.send( m, m->getDestinationNode() );
345 }
346 break;
347 }
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 || dmsg->getTTL() > 10) break;
354
355 // decrease time-to-live
356 dmsg->setTTL(dmsg->getTTL() - 1);
357
358 const route_item* item = NULL;
359 if (dmsg->getType() == 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 OverlayMsg omsg(*m);
370 omsg.encapsulate(dmsg);
371 omsg.setDestinationNode(item->id);
372 baseoverlay.send(&omsg, omsg.getDestinationNode());
373 break;
374 }}
375 delete dmsg;
376 break;
377 }
378
379 // leave
380 case typeLeave: {
381 if (link!=LinkID::UNSPECIFIED) {
382 route_item* item = table->get(remote);
383 if (item!=NULL) item->info = LinkID::UNSPECIFIED;
384 table->remove(remote);
385 baseoverlay.dropLink(link);
386 }
387 break;
388 }}
389}
390
391void Chord::eventFunction() {
392 stabilize_counter++;
393 if (stabilize_counter < 0 || stabilize_counter == 4) {
394
395 // reset counter
396 stabilize_counter = 0;
397
398 // clear pending connections
399 pending.clear();
400
401 // get number of real neighbors
402 size_t numNeighbors = 0;
403 for (size_t i = 0; i < table->size(); i++) {
404 route_item* it = (*table)[i];
405 if (it->ref_count != 0 && !it->info.isUnspecified()) numNeighbors++;
406 }
407 logging_info("Running stabilization: #links="
408 << table->size() << " #neighbors=" << numNeighbors );
409
410 // sending discovery
411 logging_debug("Sending discovery message to my neighbors and fingers");
412 stabilize_finger = ((stabilize_finger+1) % table->get_finger_table_size() );
413 const NodeID disc1 = nodeid;
414 const NodeID disc2 = table->get_finger_table(stabilize_finger).get_compare().get_center();
415 send_discovery_to(disc1);
416 if (disc1 != disc2)
417 send_discovery_to(disc2);
418
419 for (int i=0; i<table->size(); i++) {
420 LinkID id = (*table)[i]->info;
421 if (!id.isUnspecified()) discover_neighbors(id);
422 }
423
424 // remove orphan links
425 orphan_removal_counter++;
426 if (orphan_removal_counter <0 || orphan_removal_counter >= 2) {
427 logging_info("Running orphan removal");
428 orphan_removal_counter = 0;
429 for (size_t i = 0; i < table->size(); i++) {
430 route_item* it = (*table)[i];
431 if (it->ref_count == 0 && !it->info.isUnspecified()) {
432 logging_info("Dropping orphaned link " << it->info.toString() << " to " << it->id.toString());
433 table->insert(it->id);
434 if (it->ref_count==0) {
435 LinkID id = it->info;
436 it->info = LinkID::UNSPECIFIED;
437 baseoverlay.dropLink(id);
438 }
439 }
440 }
441 }
442 }
443}
444
445void Chord::showLinks() {
446 logging_info("--- chord routing information ----------------------------------");
447 logging_info("predecessor: " << (table->get_predesessor()==NULL? "<none>" :
448 table->get_predesessor()->toString()) );
449 logging_info("node_id : " << nodeid.toString() );
450 logging_info("successor : " << (table->get_successor()==NULL? "<none>" :
451 table->get_successor()->toString()));
452 logging_info("----------------------------------------------------------------");
453}
454
455}} // namespace ariba, overlay
Note: See TracBrowser for help on using the repository browser.