3 import java.util.ArrayList;
6 public class BackupsManager {
7 final int MAX_COUNT_NOT_ALIVE = 3;
10 public static BackupsManager Instance;
11 public int myRank = -1;
12 public ArrayList<Backup> liste = new ArrayList<Backup>();
13 public ArrayList<Backup> liste_Convg = new ArrayList<Backup>();
16 private BackupsManager() {
19 public static BackupsManager Instance() {
20 if (Instance == null) {
21 Instance = new BackupsManager();
26 // retourne le nb de Backups ds la liste)
27 public synchronized int size() {
31 public synchronized void clean() {
32 for (int i = 0; i < liste.size(); i++) {
33 ((Backup) liste.get(i)).setIteration(-1);
34 ((Backup) liste.get(i)).setData(null);
35 ((Backup) liste_Convg.get(i)).setIteration(-1);
36 ((Backup) liste_Convg.get(i)).setData(null);
40 public synchronized void purge() {
47 public synchronized int getMyRank() {
51 public synchronized void initialize(int req) {
52 ListeTask tskList = Register.Instance().getListeOfTasks();
54 TaskId t = tskList.getTaskIdOfHostStub(LocalHost.Instance().getStub());
56 System.out.println("no corresponding task !!!!!!!!!!!!!!!!");
58 // what can we do ?????????????????
63 // get the number of backup nodes there are for each task i
64 int numBackNodes = Register.Instance().getNumBackupNeighbors();
65 System.out.println("numBackNodes : " + numBackNodes);
66 int numOfTasks = Register.Instance().getNbOfTasks();
67 System.out.println("nombre de taches=" + numOfTasks);
70 int lastBackupRankConv;
72 // ****************************** STEP 1 : Create the empty
76 for (int i = 1; i <= numBackNodes; i++) {
77 // ------------ 1 - for backups "i + n" (to the right of i)
78 rankOfBackTask = (myRank + i) % numOfTasks;
79 // System.out.println("i : " + i + ", rankOfBackTask : " +
81 Backup b_right = new Backup(rankOfBackTask);
82 addBackupTask(b_right, 0); // erase if exists so no redondancy
83 Backup b_rightConv = new Backup(rankOfBackTask);
84 addBackupTask(b_rightConv, 1); // erase if exists so no redondancy
86 // ------------ 2 - for backups "i - n" (to the left of i)
89 rankOfBackTask = tmp % numOfTasks;
91 rankOfBackTask = numOfTasks - (Math.abs(tmp) % numOfTasks);
93 // System.out.println("i : " + i + ", rankOfBackTask : " +
95 Backup b_left = new Backup(rankOfBackTask);
96 addBackupTask(b_left, 0); // erase if exists so no redondancy
97 Backup b_leftConv = new Backup(rankOfBackTask);
98 addBackupTask(b_leftConv, 1); // erase if exists so no redondancy
101 // ****************************** STEP 2 : get the Backup for my task
102 // ** get an eventual Backup (if there is one) to restart on me (try 3
108 while (j < 3 && res == -1) {
109 // scan all backup Nodes to know the rank of task
110 // which Backup is the most recent for my tasks
111 lastBackupRank = getLastRemoteBackupRank(); // return -1 if no
113 lastBackupRankConv = getLastRemoteBackupRankConvg();
114 // Knowing the Node which has the last (most recent) Backup for
116 // I get this Backup in order to restart it
117 System.out.println("I am going to get the Backup on "
119 System.out.println("I am going to get the Backup Conv on "
120 + lastBackupRankConv);
121 res = restartMyTask(lastBackupRank, lastBackupRankConv);
123 System.out.println("Backup successfully got and restarted");
126 .println("FAILED to get or restart the Backup at try "
128 + "... I retry to get a Backup on another Node ("
129 + (2 - j) + " times again)");
134 // get backups of all neighboring nodes
137 TaskId id = Register.Instance().getListeOfTasks()
138 .getTaskIdOfHostStub(LocalHost.Instance().getStub());
139 int myRank = id.getRank();
140 JaceInterface destStub;
141 for (int i = 0; i < BackupsManager.Instance().size(); i++) {
142 destRank = BackupsManager.Instance().getBackupTaskAtIndex(i, 0)
144 destTaskId = Register.Instance().getListeOfTasks()
145 .getTaskIdOfRank(destRank);
146 destStub = destTaskId.getHostStub();
147 new GetBackupForNewNode(destStub, myRank).start();
151 restartMyTask(-1, -1);
154 public class GetBackupForNewNode extends Thread {
158 public GetBackupForNewNode(JaceInterface destStub, int myRank) {
160 this.myRank = myRank;
166 stub.getBackupForNewNode(myRank);
167 } catch (Exception e) {
172 public synchronized void addBackupTask(Backup t, int tag) {
174 // regarder si existe cette tache ds le vecteur
176 int isIn = existBackupTaskOfRank(t.getTaskRank(), tag);
178 if (isIn == -1) { // si elle y est pas on l'isere
183 else { // si elle y est on remplace l'ancienne
188 int isIn = existBackupTaskOfRank(t.getTaskRank(), tag);
191 liste_Convg.add( t ) ;
193 liste_Convg.set(isIn, t);
197 public int existBackupTaskOfRank(int rank, int tag) {
201 while ((existe == -1) && (index < liste.size())) {
202 if (rank == ((Backup) liste.get(index)).getTaskRank()) {
208 while ((existe == -1) && (index < liste_Convg.size())) {
209 if (rank == ((Backup) liste_Convg.get(index)).getTaskRank()) {
217 // returns rank of the task that has my most recent Backup
218 private int getLastRemoteBackupRank() {
221 JaceInterface stub = null;
224 int lastBackupRank = -1;
225 ArrayList<Integer> v;
231 // ask for ite number of Backups of all my Backup Nodes
232 for (int i = 0; i < liste.size(); i++) {
235 rank = ((Backup) liste.get(i)).getTaskRank();
236 task = Register.Instance().getListeOfTasks().getTaskIdOfRank(rank);
238 System.out.println("la tache " + rank
239 + "n'est pas trouvee dans le registre de " + myRank);
241 stub = task.getHostStub();
243 System.out.println("stub is null, this node is dead !!!!");
246 // if node not dead, try 3 times to know
247 // the ite number of its Backup for me
251 // threads or not, for the invokation ?
253 v = stub.getIterationOfBackup(myRank, 0);
254 ite = ((Integer) v.get(0)).intValue();
256 timeStep = ((Integer) v.get(1)).intValue();
257 System.out.println(" data ite = " + ite + " timeStep="
258 + timeStep + " available from task " + rank);
260 } catch (Exception e) {
261 System.out.println("The Node " + task.getHostIP()
262 + " does not answer at try " + count);
264 } while ((ack == false) && (count < MAX_COUNT_NOT_ALIVE));
265 if (lastStep < timeStep) {
268 lastBackupRank = rank;
269 } else if (lastStep == timeStep && ite > lastIte) {
271 lastBackupRank = rank;
275 System.out.println("last backup rank =" + lastBackupRank);
276 return lastBackupRank;
279 private int getLastRemoteBackupRankConvg() {
282 JaceInterface stub = null;
285 int lastBackupRank = -1;
288 ArrayList<Integer> v;
291 // ask for ite number of Backups of all my Backup Nodes
292 for (int i = 0; i < liste_Convg.size(); i++) {
295 rank = ((Backup) liste_Convg.get(i)).getTaskRank();
296 task = Register.Instance().getListeOfTasks().getTaskIdOfRank(rank);
297 stub = task.getHostStub();
299 System.out.println("stub is null, this node is dead !!!!");
302 // if node not dead, try 3 times to know
303 // the ite number of its Backup for me
307 // threads or not, for the invokation ?
309 v = stub.getIterationOfBackup(myRank, 1);
310 ite = ((Integer) v.get(0)).intValue();
311 timeStep = ((Integer) v.get(1)).intValue();
312 System.out.println(" conv ite = " + ite + " timeStep="
313 + timeStep + " available from task " + rank);
314 // System.out.println("ite = " + ite +
315 // " available for task " + rank);
317 } catch (Exception e) {
318 System.out.println("The Node " + task.getHostIP()
319 + " does not answer at try " + count);
321 } while ((ack == false) && (count < MAX_COUNT_NOT_ALIVE));
322 if (lastStep < timeStep) {
325 lastBackupRank = rank;
326 } else if (lastStep == timeStep && ite > lastIte) {
328 lastBackupRank = rank;
333 return lastBackupRank;
336 // return 0 if good (if I successfully got a Backup),
337 // return -1 elsewhere
338 private synchronized int restartMyTask(int lastBackupRank,
339 int lastBackupRankConv) {
340 Backup newBackup = null;
341 Backup newBackupConvg = null;
343 JaceInterface stub = null;
345 // If no Backups, lastBackupRank = -1, we start the thread at beginning
346 if (lastBackupRank == -1) {
349 // get the lastBackupRank on the corresponding BackupNode
350 task = Register.Instance().getListeOfTasks().getTaskIdOfRank(
352 stub = task.getHostStub();
354 newBackup = stub.getRemoteBackup(myRank, 0);
355 // System.out.println("got new back normale for "+myRank);
356 } catch (Exception e) {
357 System.out.println("the node " + task.getHostIP()
358 + " does not answer");
359 // exit in order to get back and get a previous Backup
360 // since this one which failed was the most recent
365 if (lastBackupRankConv == -1) {
366 newBackupConvg = null;
368 // get the lastBackupRank on the corresponding BackupNode
369 task = Register.Instance().getListeOfTasks().getTaskIdOfRank(
371 stub = task.getHostStub();
374 newBackupConvg = stub.getRemoteBackup(myRank, 1);
375 // System.out.println("got new back conv for "+myRank);
376 } catch (Exception e) {
377 System.out.println("the node " + task.getHostIP()
378 + " does not answer");
379 // exit in order to get back and get a previous Backup
380 // since this one which failed was the most recent
385 // in JaceSession, clean the taskObject (Task)
386 // and the taskThread (Thread)
387 JaceSession.Instance().init();
388 // create 1 TaskLauncher that will (re)start the local task within a
390 TaskLauncher launcher = new TaskLauncher();
391 launcher.loadOrReloadTask(newBackup, newBackupConvg);
395 public Backup getBackupTaskOfRank(int rank, int tag) {
398 if (liste.isEmpty()) {
401 is = existBackupTaskOfRank(rank, tag);
403 // System.out.println("chercher un backup normal");
404 return (Backup) liste.get(is);
406 System.out.println("cette tache ou ce backup existe pas");
411 if (liste_Convg.isEmpty()) {
414 is = existBackupTaskOfRank(rank, tag);
416 // System.out.println("chercher un backup convg");
417 return (Backup) liste_Convg.get(is);
419 System.out.println("cette tache ou ce backup existe pas");
426 public synchronized Backup getBackupTaskAtIndex(int index, int tag) {
428 if (index < liste.size()) {
429 return (Backup) liste.get(index);
431 System.out.println("cette task n'existe pas");
434 else if (index < liste_Convg.size()) {
435 return (Backup) liste_Convg.get(index);
437 System.out.println("cette task n'existe pas");