3 import java.util.Vector;
5 public class BackupsManager {
6 final int MAX_COUNT_NOT_ALIVE = 3;
9 public static BackupsManager Instance;
10 public int myRank = -1;
11 public Vector<Backup> liste = new Vector<Backup>();
12 public Vector<Backup> liste_Convg = new Vector<Backup>();
15 private BackupsManager() {
18 public static BackupsManager Instance() {
19 if (Instance == null) {
20 Instance = new BackupsManager();
25 // retourne le nb de Backups ds la liste)
26 public synchronized int size() {
30 public synchronized void clean() {
31 for (int i = 0; i < liste.size(); i++) {
32 ((Backup) liste.get(i)).setIteration(-1);
33 ((Backup) liste.get(i)).setData(null);
34 ((Backup) liste_Convg.get(i)).setIteration(-1);
35 ((Backup) liste_Convg.get(i)).setData(null);
39 public synchronized void purge() {
46 public synchronized int getMyRank() {
50 public synchronized void initialize(int req) {
51 ListeTask tskList = Register.Instance().getListeOfTasks();
53 TaskId t = tskList.getTaskIdOfHostStub(LocalHost.Instance().getStub());
55 System.out.println("no corresponding task !!!!!!!!!!!!!!!!");
57 // what can we do ?????????????????
62 // get the number of backup nodes there are for each task i
63 int numBackNodes = Register.Instance().getNumBackupNeighbors();
64 System.out.println("numBackNodes : " + numBackNodes);
65 int numOfTasks = Register.Instance().getNbOfTasks();
66 System.out.println("nombre de taches=" + numOfTasks);
69 int lastBackupRankConv;
71 // ****************************** STEP 1 : Create the empty
75 for (int i = 1; i <= numBackNodes; i++) {
76 // ------------ 1 - for backups "i + n" (to the right of i)
77 rankOfBackTask = (myRank + i) % numOfTasks;
78 // System.out.println("i : " + i + ", rankOfBackTask : " +
80 Backup b_right = new Backup(rankOfBackTask);
81 addBackupTask(b_right, 0); // erase if exists so no redondancy
82 Backup b_rightConv = new Backup(rankOfBackTask);
83 addBackupTask(b_rightConv, 1); // erase if exists so no redondancy
85 // ------------ 2 - for backups "i - n" (to the left of i)
88 rankOfBackTask = tmp % numOfTasks;
90 rankOfBackTask = numOfTasks - (Math.abs(tmp) % numOfTasks);
92 // System.out.println("i : " + i + ", rankOfBackTask : " +
94 Backup b_left = new Backup(rankOfBackTask);
95 addBackupTask(b_left, 0); // erase if exists so no redondancy
96 Backup b_leftConv = new Backup(rankOfBackTask);
97 addBackupTask(b_leftConv, 1); // erase if exists so no redondancy
100 // ****************************** STEP 2 : get the Backup for my task
101 // ** get an eventual Backup (if there is one) to restart on me (try 3
107 while (j < 3 && res == -1) {
108 // scan all backup Nodes to know the rank of task
109 // which Backup is the most recent for my tasks
110 lastBackupRank = getLastRemoteBackupRank(); // return -1 if no
112 lastBackupRankConv = getLastRemoteBackupRankConvg();
113 // Knowing the Node which has the last (most recent) Backup for
115 // I get this Backup in order to restart it
116 System.out.println("I am going to get the Backup on "
118 System.out.println("I am going to get the Backup Conv on "
119 + lastBackupRankConv);
120 res = restartMyTask(lastBackupRank, lastBackupRankConv);
122 System.out.println("Backup successfully got and restarted");
125 .println("FAILED to get or restart the Backup at try "
127 + "... I retry to get a Backup on another Node ("
128 + (2 - j) + " times again)");
133 // get backups of all neighboring nodes
136 TaskId id = Register.Instance().getListeOfTasks()
137 .getTaskIdOfHostStub(LocalHost.Instance().getStub());
138 int myRank = id.getRank();
139 JaceInterface destStub;
140 for (int i = 0; i < BackupsManager.Instance().size(); i++) {
141 destRank = BackupsManager.Instance().getBackupTaskAtIndex(i, 0)
143 destTaskId = Register.Instance().getListeOfTasks()
144 .getTaskIdOfRank(destRank);
145 destStub = destTaskId.getHostStub();
146 new GetBackupForNewNode(destStub, myRank).start();
150 restartMyTask(-1, -1);
153 public class GetBackupForNewNode extends Thread {
157 public GetBackupForNewNode(JaceInterface destStub, int myRank) {
159 this.myRank = myRank;
165 stub.getBackupForNewNode(myRank);
166 } catch (Exception e) {
171 public synchronized void addBackupTask(Backup t, int tag) {
173 // regarder si existe cette tache ds le vecteur
175 int isIn = existBackupTaskOfRank(t.getTaskRank(), tag);
177 if (isIn == -1) { // si elle y est pas on l'isere
182 else { // si elle y est on remplace l'ancienne
187 int isIn = existBackupTaskOfRank(t.getTaskRank(), tag);
190 liste_Convg.addElement(t);
192 liste_Convg.set(isIn, t);
196 public int existBackupTaskOfRank(int rank, int tag) {
200 while ((existe == -1) && (index < liste.size())) {
201 if (rank == ((Backup) liste.get(index)).getTaskRank()) {
207 while ((existe == -1) && (index < liste_Convg.size())) {
208 if (rank == ((Backup) liste_Convg.get(index)).getTaskRank()) {
216 // returns rank of the task that has my most recent Backup
217 private int getLastRemoteBackupRank() {
220 JaceInterface stub = null;
223 int lastBackupRank = -1;
230 // ask for ite number of Backups of all my Backup Nodes
231 for (int i = 0; i < liste.size(); i++) {
234 rank = ((Backup) liste.get(i)).getTaskRank();
235 task = Register.Instance().getListeOfTasks().getTaskIdOfRank(rank);
237 System.out.println("la tache " + rank
238 + "n'est pas trouvee dans le registre de " + myRank);
240 stub = task.getHostStub();
242 System.out.println("stub is null, this node is dead !!!!");
245 // if node not dead, try 3 times to know
246 // the ite number of its Backup for me
250 // threads or not, for the invokation ?
252 v = stub.getIterationOfBackup(myRank, 0);
253 ite = ((Integer) v.get(0)).intValue();
255 timeStep = ((Integer) v.get(1)).intValue();
256 System.out.println(" data ite = " + ite + " timeStep="
257 + timeStep + " available from task " + rank);
259 } catch (Exception e) {
260 System.out.println("The Node " + task.getHostIP()
261 + " does not answer at try " + count);
263 } while ((ack == false) && (count < MAX_COUNT_NOT_ALIVE));
264 if (lastStep < timeStep) {
267 lastBackupRank = rank;
268 } else if (lastStep == timeStep && ite > lastIte) {
270 lastBackupRank = rank;
274 System.out.println("last backup rank =" + lastBackupRank);
275 return lastBackupRank;
278 private int getLastRemoteBackupRankConvg() {
281 JaceInterface stub = null;
284 int lastBackupRank = -1;
290 // ask for ite number of Backups of all my Backup Nodes
291 for (int i = 0; i < liste_Convg.size(); i++) {
294 rank = ((Backup) liste_Convg.get(i)).getTaskRank();
295 task = Register.Instance().getListeOfTasks().getTaskIdOfRank(rank);
296 stub = task.getHostStub();
298 System.out.println("stub is null, this node is dead !!!!");
301 // if node not dead, try 3 times to know
302 // the ite number of its Backup for me
306 // threads or not, for the invokation ?
308 v = stub.getIterationOfBackup(myRank, 1);
309 ite = ((Integer) v.get(0)).intValue();
310 timeStep = ((Integer) v.get(1)).intValue();
311 System.out.println(" conv ite = " + ite + " timeStep="
312 + timeStep + " available from task " + rank);
313 // System.out.println("ite = " + ite +
314 // " available for task " + rank);
316 } catch (Exception e) {
317 System.out.println("The Node " + task.getHostIP()
318 + " does not answer at try " + count);
320 } while ((ack == false) && (count < MAX_COUNT_NOT_ALIVE));
321 if (lastStep < timeStep) {
324 lastBackupRank = rank;
325 } else if (lastStep == timeStep && ite > lastIte) {
327 lastBackupRank = rank;
332 return lastBackupRank;
335 // return 0 if good (if I successfully got a Backup),
336 // return -1 elsewhere
337 private synchronized int restartMyTask(int lastBackupRank,
338 int lastBackupRankConv) {
339 Backup newBackup = null;
340 Backup newBackupConvg = null;
342 JaceInterface stub = null;
344 // If no Backups, lastBackupRank = -1, we start the thread at beginning
345 if (lastBackupRank == -1) {
348 // get the lastBackupRank on the corresponding BackupNode
349 task = Register.Instance().getListeOfTasks().getTaskIdOfRank(
351 stub = task.getHostStub();
353 newBackup = stub.getRemoteBackup(myRank, 0);
354 // System.out.println("got new back normale for "+myRank);
355 } catch (Exception e) {
356 System.out.println("the node " + task.getHostIP()
357 + " does not answer");
358 // exit in order to get back and get a previous Backup
359 // since this one which failed was the most recent
364 if (lastBackupRankConv == -1) {
365 newBackupConvg = null;
367 // get the lastBackupRank on the corresponding BackupNode
368 task = Register.Instance().getListeOfTasks().getTaskIdOfRank(
370 stub = task.getHostStub();
373 newBackupConvg = stub.getRemoteBackup(myRank, 1);
374 // System.out.println("got new back conv for "+myRank);
375 } catch (Exception e) {
376 System.out.println("the node " + task.getHostIP()
377 + " does not answer");
378 // exit in order to get back and get a previous Backup
379 // since this one which failed was the most recent
384 // in JaceSession, clean the taskObject (Task)
385 // and the taskThread (Thread)
386 JaceSession.Instance().init();
387 // create 1 TaskLauncher that will (re)start the local task within a
389 TaskLauncher launcher = new TaskLauncher();
390 launcher.loadOrReloadTask(newBackup, newBackupConvg);
394 public Backup getBackupTaskOfRank(int rank, int tag) {
397 if (liste.isEmpty()) {
400 is = existBackupTaskOfRank(rank, tag);
402 // System.out.println("chercher un backup normal");
403 return (Backup) liste.get(is);
405 System.out.println("cette tache ou ce backup existe pas");
410 if (liste_Convg.isEmpty()) {
413 is = existBackupTaskOfRank(rank, tag);
415 // System.out.println("chercher un backup convg");
416 return (Backup) liste_Convg.get(is);
418 System.out.println("cette tache ou ce backup existe pas");
425 public synchronized Backup getBackupTaskAtIndex(int index, int tag) {
427 if (index < liste.size()) {
428 return (Backup) liste.get(index);
430 System.out.println("cette task n'existe pas");
433 else if (index < liste_Convg.size()) {
434 return (Backup) liste_Convg.get(index);
436 System.out.println("cette task n'existe pas");