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

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