close Warning: Can't use blame annotator:
No changeset 2259 in the repository

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

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

merge from bootstrap branch

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