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

Last change on this file since 3695 was 3695, checked in by mies, 15 years ago

fixed orphaned routing table entries problem with chord

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