1 /* Copyright (c) 2006-2020. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
8 import org.simgrid.msg.Msg;
9 import org.simgrid.msg.Comm;
10 import org.simgrid.msg.Host;
11 import org.simgrid.msg.Task;
12 import org.simgrid.msg.Process;
13 import org.simgrid.msg.MsgException;
14 import org.simgrid.msg.TimeoutException;
15 public class Node extends Process {
17 protected String mailbox;
19 protected String predMailbox;
20 protected int nextFingerToFix;
21 protected Comm commReceive;
22 ///Last time I changed a finger or my predecessor
23 protected double lastChangeDate;
24 private int[] fingers;
26 public Node(Host host, String name, String[] args) {
27 super(host,name,args);
31 public void main(String[] args) throws MsgException {
32 if (args.length != 2 && args.length != 4) {
33 Msg.info("You need to provide 2 or 4 arguments.");
36 double initTime = Msg.getClock();
41 double nextStabilizeDate = initTime + Common.PERIODIC_STABILIZE_DELAY;
42 double nextFixFingersDate = initTime + Common.PERIODIC_FIX_FINGERS_DELAY;
43 double nextCheckPredecessorDate = initTime + Common.PERIODIC_CHECK_PREDECESSOR_DELAY;
44 double nextLookupDate = initTime + Common.PERIODIC_LOOKUP_DELAY;
47 id = Integer.parseInt(args[0]);
49 fingers = new int[Common.NB_BITS];
50 for (i = 0; i < Common.NB_BITS; i++) {
56 if (args.length == 2) {
57 deadline = Integer.parseInt(args[1]);
61 int knownId = Integer.parseInt(args[1]);
62 deadline = Integer.parseInt(args[3]);
63 Msg.debug("Hey! Let's join the system with the id " + id + ".");
65 joinSuccess = join(knownId);
69 Msg.info("I couldn't join the ring");
73 double currentClock = Msg.getClock();
74 while (currentClock < (initTime + deadline) && currentClock < Common.MAX_SIMULATION_TIME) {
75 if (commReceive == null) {
76 commReceive = Task.irecv(this.mailbox);
79 if (!commReceive.test()) {
80 if (currentClock >= nextStabilizeDate) {
82 nextStabilizeDate = Msg.getClock() + Common.PERIODIC_STABILIZE_DELAY;
83 } else if (currentClock >= nextFixFingersDate) {
85 nextFixFingersDate = Msg.getClock() + Common.PERIODIC_FIX_FINGERS_DELAY;
86 } else if (currentClock >= nextCheckPredecessorDate) {
87 this.checkPredecessor();
88 nextCheckPredecessorDate = Msg.getClock() + Common.PERIODIC_CHECK_PREDECESSOR_DELAY;
89 } else if (currentClock >= nextLookupDate) {
91 nextLookupDate = Msg.getClock() + Common.PERIODIC_LOOKUP_DELAY;
95 currentClock = Msg.getClock();
97 handleTask(commReceive.getTask());
98 currentClock = Msg.getClock();
102 catch (Exception e) {
103 currentClock = Msg.getClock();
108 if (commReceive != null) {
113 private void handleTask(Task task) {
114 if (task instanceof FindSuccessorTask) {
115 FindSuccessorTask fTask = (FindSuccessorTask)task;
116 Msg.debug("Receiving a 'Find Successor' request from " + fTask.getIssuerHostName() + " for id " +
117 fTask.getRequestId());
118 // is my successor the successor?
119 if (isInInterval(fTask.getRequestId(), this.id + 1, fingers[0])) {
120 Msg.debug("Send the request to " + fTask.getAnswerTo() + " with answer " + fingers[0]);
121 FindSuccessorAnswerTask answer = new FindSuccessorAnswerTask(getHost().getName(), mailbox, fingers[0]);
122 answer.dsend(fTask.getAnswerTo());
124 // otherwise, forward the request to the closest preceding finger in my table
125 int closest = closestPrecedingNode(fTask.getRequestId());
126 Msg.debug("Forward the request to " + closest);
127 fTask.dsend(Integer.toString(closest));
129 } else if (task instanceof GetPredecessorTask) {
130 GetPredecessorTask gTask = (GetPredecessorTask)(task);
131 Msg.debug("Receiving a 'Get Predecessor' request from " + gTask.getIssuerHostName());
132 GetPredecessorAnswerTask answer = new GetPredecessorAnswerTask(getHost().getName(), mailbox, predId);
133 answer.dsend(gTask.getAnswerTo());
134 } else if (task instanceof NotifyTask) {
135 NotifyTask nTask = (NotifyTask)task;
136 notify(nTask.getRequestId());
138 Msg.debug("Ignoring unexpected task of type:" + task);
142 private void leave() {
143 Msg.debug("Well Guys! I Think it's time for me to quit ;)");
144 // TODO: Notify my successor and predecessor.
147 /** @brief Initializes the current node as the first one of the system */
148 private void create() {
149 Msg.debug("Create a new Chord ring...");
153 // Makes the current node join the ring, knowing the id of a node already in the ring
154 private boolean join(int knownId) {
155 Msg.info("Joining the ring with id " + this.id + " knowing node " + knownId);
157 int successorId = remoteFindSuccessor(knownId, this.id);
158 if (successorId == -1) {
159 Msg.info("Cannot join the ring.");
161 setFinger(0, successorId);
163 return successorId != -1;
166 private void setPredecessor(int predecessorId) {
167 if (predecessorId != predId) {
168 predId = predecessorId;
169 if (predecessorId != -1) {
170 predMailbox = Integer.toString(predId);
172 lastChangeDate = Msg.getClock();
177 * @brief Asks another node its predecessor.
178 * @param askTo the node to ask to
179 * @return the id of its predecessor node, or -1 if the request failed(or if the node does not know its predecessor)
181 private int remoteGetPredecessor(int askTo) {
182 int predecessorId = -1;
183 boolean stop = false;
184 Msg.debug("Sending a 'Get Predecessor' request to " + askTo);
185 String mailboxTo = Integer.toString(askTo);
186 GetPredecessorTask sendTask = new GetPredecessorTask(getHost().getName(), this.mailbox);
188 sendTask.send(mailboxTo, Common.TIMEOUT);
190 if (commReceive == null) {
191 commReceive = Task.irecv(this.mailbox);
193 commReceive.waitCompletion(Common.TIMEOUT);
194 Task taskReceived = commReceive.getTask();
195 if (taskReceived instanceof GetPredecessorAnswerTask) {
196 predecessorId = ((GetPredecessorAnswerTask) taskReceived).getAnswerId();
199 handleTask(taskReceived);
204 catch (MsgException e) {
205 Msg.debug("Failed to send the Get Predecessor request");
209 return predecessorId;
213 * @brief Makes the current node find the successor node of an id.
214 * @param node the current node
215 * @param id the id to find
216 * @return the id of the successor node, or -1 if the request failed
218 private int findSuccessor(int id) {
219 if (isInInterval(id, this.id + 1, fingers[0])) {
223 int closest = this.closestPrecedingNode(id);
224 return remoteFindSuccessor(closest, id);
227 // Asks another node the successor node of an id.
228 private int remoteFindSuccessor(int askTo, int id) {
230 boolean stop = false;
231 String askToMailbox = Integer.toString(askTo);
232 Task sendTask = new FindSuccessorTask(getHost().getName(), this.mailbox, id);
233 Msg.debug("Sending a 'Find Successor' request to " + askToMailbox + " for id " + id);
235 sendTask.send(askToMailbox, Common.TIMEOUT);
237 if (commReceive == null) {
238 commReceive = Task.irecv(this.mailbox);
240 commReceive.waitCompletion(Common.TIMEOUT);
241 Task task = commReceive.getTask();
242 if (task instanceof FindSuccessorAnswerTask) {
243 //TODO: Check if this this our answer.
244 FindSuccessorAnswerTask fTask = (FindSuccessorAnswerTask) task;
246 successor = fTask.getAnswerId();
253 catch (TimeoutException e) {
254 Msg.debug("Failed to send the 'Find Successor' request");
256 catch (MsgException e) {
257 Msg.debug("Failed to receive Find Successor");
264 // This function is called periodically. It checks the immediate successor of the current node.
265 private void stabilize() {
266 Msg.debug("Stabilizing node");
268 int successorId = fingers[0];
269 if (successorId != this.id){
270 candidateId = remoteGetPredecessor(successorId);
272 candidateId = predId;
274 //This node is a candidate to become my new successor
275 if (candidateId != -1 && isInInterval(candidateId, this.id + 1, successorId - 1)) {
276 setFinger(0, candidateId);
278 if (successorId != this.id) {
279 remoteNotify(successorId, this.id);
284 * @brief Notifies the current node that its predecessor may have changed.
285 * @param candidate_id the possible new predecessor
287 private void notify(int predecessorCandidateId) {
288 if (predId == -1 || isInInterval(predecessorCandidateId, predId + 1, this.id - 1 )) {
289 setPredecessor(predecessorCandidateId);
294 * @brief Notifies a remote node that its predecessor may have changed.
295 * @param notify_id id of the node to notify
296 * @param candidate_id the possible new predecessor
298 private void remoteNotify(int notifyId, int predecessorCandidateId) {
299 Msg.debug("Sending a 'Notify' request to " + notifyId);
300 Task sentTask = new NotifyTask(getHost().getName(), this.mailbox, predecessorCandidateId);
301 sentTask.dsend(Integer.toString(notifyId));
304 // This function is called periodically.
305 // It refreshes the finger table of the current node.
306 private void fixFingers() {
307 Msg.debug("Fixing fingers");
308 int successorId = findSuccessor(id + (1 << nextFingerToFix));
309 if (successorId != -1) {
310 if (successorId != fingers[nextFingerToFix]) {
311 setFinger(nextFingerToFix, successorId);
313 nextFingerToFix = (nextFingerToFix + 1) % Common.NB_BITS;
317 // This function is called periodically.
318 // It checks whether the predecessor has failed
319 private void checkPredecessor() {
323 // Performs a find successor request to a random id.
324 private void randomLookup() {
330 * @brief Returns the closest preceding finger of an id with respect to the finger table of the current node.
331 * @param id the id to find
332 * @return the closest preceding finger of that id
334 private int closestPrecedingNode(int id) {
335 for (int i = Common.NB_BITS - 1; i >= 0; i--) {
336 if (isInInterval(fingers[i], this.id + 1, id - 1)) {
344 * @brief Returns whether an id belongs to the interval [start, end].
346 * The parameters are noramlized to make sure they are between 0 and nb_keys - 1).
347 * 1 belongs to [62, 3]
348 * 1 does not belong to [3, 62]
349 * 63 belongs to [62, 3]
350 * 63 does not belong to [3, 62]
351 * 24 belongs to [21, 29]
352 * 24 does not belong to [29, 21]
354 * @param id id to check
355 * @param start lower bound
356 * @param end upper bound
357 * @return a non-zero value if id in in [start, end]
359 private static boolean isInInterval(int id, int start, int end) {
360 int normId = normalize(id);
361 int normStart = normalize(start);
362 int normEnd = normalize(end);
364 // make sure end >= start and id >= start
365 if (normEnd < normStart) {
366 normEnd += Common.NB_KEYS;
368 if (normId < normStart) {
369 normId += Common.NB_KEYS;
371 return (normId <= normEnd);
375 * @brief Turns an id into an equivalent id in [0, nb_keys).
377 * @return the corresponding normalized id
379 private static int normalize(int id) {
380 return id & (Common.NB_KEYS - 1);
384 * @brief Sets a finger of the current node.
385 * @param finger_index index of the finger to set (0 to nb_bits - 1)
386 * @param id the id to set for this finger
388 private void setFinger(int fingerIndex, int id) {
389 if (id != fingers[fingerIndex]) {
390 fingers[fingerIndex] = id;
391 lastChangeDate = Msg.getClock();