Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
New development version.
[jaceP2P.git] / src / jaceP2P / UpdateRegisterConcernedThread.java
1 package jaceP2P;
2
3 import java.util.Vector;
4
5 class UpdateRegisterConcernedThread extends Thread {
6         Register reg = null;
7         int requete;
8         int i, rank;
9         Task tache = null;
10         @SuppressWarnings("unchecked")
11         Vector neighbors;
12         int nbOfDeamonsPerThread;
13         Node oldNode, node;
14
15         @SuppressWarnings("unchecked")
16         public UpdateRegisterConcernedThread(Vector neighbors, Register r, int i,
17                         int rank, Node noeud, Node tmpNode) {
18
19                 this.reg = r;
20                 this.rank = rank;
21                 this.i = i;
22                 //his.tache = tache;
23                 this.neighbors = neighbors;
24                 oldNode = noeud;
25                 node = tmpNode;
26                 nbOfDeamonsPerThread = JaceSpawner.Instance().getNbOfDeamonsPerThread();
27         }
28
29         public void run() {
30                 ListeTask listOfTasks = Register.Instance().getListeOfTasks().clone();
31 //              boolean retry = false ;
32 //              int nbRetry = 0 ;
33 //              int nbRetryMax = 2 ;
34
35                 String name = "" ;
36
37                 // listOfTasks.viewAll();
38                 for (int index = i * nbOfDeamonsPerThread; index < i
39                 * nbOfDeamonsPerThread + nbOfDeamonsPerThread
40                 && index < neighbors.size(); index++) 
41                 {
42
43 //                      retry = true ;
44 //                      nbRetry = 0 ;
45
46 //                      while( retry )
47 //                      {
48                                 try {
49                                         if (((Integer) neighbors.elementAt(index)).intValue() == rank) {
50                                                 Register g = new Register();
51                                                 ListeTask newListOfTasks = new ListeTask();
52                                                 g.setAppliName(reg.getAppliName());
53                                                 g.setParams(reg.getParams());
54
55                                                 g.setSpawnerStub(JaceSpawner.Instance()
56                                                                 .getSpawnerResponsibleOn(rank));
57                                                 g.setNbOfTasks(reg.getNbOfTasks());
58
59                                                 for (int j = 0; j < neighbors.size(); j++) {
60                                                         TaskId id = listOfTasks
61                                                         .getTaskIdOfRank(((Integer) neighbors
62                                                                         .elementAt(j)).intValue()) ;
63                                                         newListOfTasks.addTask( id ) ;
64                                                         if( id.getHostStub() != null ) {
65                                                                 Node noeud = reg.getNodeOfStub( id.getHostStub() ) ;
66 //                                                              Node n2 = noeud ;
67                                                                 if( noeud != null )
68                                                                 {
69                                                                         g.addNode( noeud ) ;
70                                                                 } else {
71                                                                         noeud = reg.getNodeOfName( id.getHostName() ) ;
72                                                                         
73                                                                         if( noeud != null )
74                                                                         {
75                                                                                 g.addNode( noeud ) ;
76                                                                         } else {
77                                                                                 System.err.println( "UpdateRegisterConcernedThread: cannot found the corresponding node !" ) ;
78                                                                         }
79                                                                 }
80                                                         }
81                                                 }
82                                                 
83                                                 g.setListeOfTasks(newListOfTasks);
84
85                                                 TaskId neighborTask = listOfTasks
86                                                 .getTaskIdOfRank((((Integer) neighbors
87                                                                 .elementAt(index)).intValue() + 1)
88                                                                 % listOfTasks.getSize());
89                                                 JaceInterface stubVoisin = neighborTask.getHostStub();
90                                                 JaceInterface stub = listOfTasks.getTaskIdOfRank(
91                                                                 ((Integer) neighbors.elementAt(index)).intValue())
92                                                                 .getHostStub();
93                                                 if( stub == null || stubVoisin == null ) 
94                                                 {
95                                                         System.out.println("stub NULL");
96                                                 } else {
97                                                         name = listOfTasks.getTaskIdOfRank(
98                                                                         ((Integer) neighbors.elementAt(index))
99                                                                         .intValue()).getHostName();
100
101                                                         Node noeud = null ;
102                                                         
103                                                         try {
104                                                                 // whatever the case, update the Register on each Node
105
106                                                                 // JaceSpawnerInterface
107                                                                 // ja=Register.Instance().getSpawnerStub();
108                                                                 // System.out.println("avant update register");
109
110                                                                 stub.updateRegister( g, stubVoisin, 0 ) ;
111
112                                                                 // System.out.println(" after update register");
113                                                                 // if updateRegister worked, that means Node is
114                                                                 // Alive
115                                                                 noeud = Register.Instance()
116                                                                 .getNodeOfStub(stub);
117                                                                 if (noeud != null) {
118                                                                         noeud.setAliveFlag(true);
119                                                                         noeud.setAliveTime();
120
121                                                                 }
122 //                                                              retry = false ;
123                                                         } catch( Exception e ) {
124                                                                 System.err
125                                                                 .println(name
126                                                                                 + " ("
127                                                                                 + LocalHost.Instance()
128                                                                                 .resolve(name)
129                                                                                 + " rank="
130                                                                                 + index
131                                                                                 + ") cannot be reached by UpdateRegisterConcernedThread for new node "
132                                                                                 + "     size : "
133                                                                                 + Register.Instance().getSize()
134                                                                                 + " :" + e);
135 //                                                              try{
136 //                                                                      Thread.sleep( (nbRetry + 1) * 2000 ) ;
137 //                                                              }catch(Exception sl){}
138 //                                                              nbRetry++ ;
139
140 //                                                              if( nbRetry > nbRetryMax )
141 //                                                              {
142 //                                                                      retry = false ;
143 //                                                                      System.err.println( "Cannot retry anymore!" ) ;
144 //                                                                      if( noeud != null )
145 //                                                                      {
146 //                                                                              try {
147 //                                                                                      JaceSpawner.Instance().signalDeadNode( noeud.getStub(), index ) ;
148 //                                                                              } catch( Exception ex ) {
149 //                                                                                      JaceSpawner.Instance().signalDeadNode( null, index ) ;
150 //                                                                              }
151 //                                                                      } else {
152 //                                                                              JaceSpawner.Instance().signalDeadNode( null, index ) ;
153 //                                                                      }
154 //                                                              }
155                                                         }
156                                                 }
157                                         } else {
158                                                 //int tag;
159                                                 TaskId id = listOfTasks
160                                                 .getTaskIdOfRank(((Integer) neighbors
161                                                                 .elementAt(index)).intValue());
162                                                 name = id.getHostName();
163
164                                                 try {
165
166                                                         JaceInterface stub = id.getHostStub() ;
167                                                         stub.updateRegister( oldNode, node, rank ) ;
168 //                                                      retry = false ;
169                                                 } catch (Exception e) {
170                                                         System.err
171                                                         .println(name
172                                                                         + " ("
173                                                                         + LocalHost.Instance().resolve(name)
174                                                                         + " rank="
175                                                                         + index
176                                                                         + ") cannot be reached by UpdateRegisterConcernedThread size : "
177                                                                         + Register.Instance().getSize() + " :"
178                                                                         + e);
179
180 //                                                      try{
181 //                                                              Thread.sleep( (nbRetry + 1) * 2000 ) ;
182 //                                                      }catch(Exception sl){}
183 //                                                      nbRetry++ ;
184
185 //                                                      if( nbRetry > nbRetryMax )
186 //                                                      {
187 //                                                              retry = false ;
188 //                                                              System.err.println( "Cannot retry anymore!" ) ;
189 //                                                      }
190                                                 }
191                                         }
192                                 } catch( Exception e2 ) {
193                                         System.err.println("Error in updateRegisterConcernedThread :"
194                                                         + e2);
195                                 }
196 //                      }
197
198 //                      if( nbRetry > nbRetryMax )
199 //                      {
200 //                              
201 //                      }
202                 }
203                 // System.out.println("at the end of an updateRegisterCon. j="+i);
204                 // Register.Instance().getListeOfTasks().viewAll();
205         }
206
207         @SuppressWarnings("unused")
208         private Vector<Integer> getDependencies(int id, int jaceSize) {
209                 // get computing dependencies
210                 Vector<Integer> neighbors = new Vector<Integer>();
211                 int[] dep = tache.getDependencies(id);
212                 for (int z = 0; z < taille(dep); z++)
213                         neighbors.add(dep[z]);
214                 // System.out.println("la liste des voisins de calcul de: "+id);
215                 // for(int z=0;z<neighbors.size();z++)
216                 // System.out.print(((Integer)neighbors.elementAt(z)).intValue()+" ");
217                 // System.out.println();
218                 // get convergence neighbors
219                 int d = 0;
220                 while (Math.pow(2, d) < jaceSize) {
221                         if (id < Math.pow(2, d) && ((id + Math.pow(2, d)) < jaceSize))
222                                 if (!neighbors.contains((Object) ((int) (id + Math.pow(2, d)))))
223                                         neighbors.add((int) (id + Math.pow(2, d)));
224                         if (id < Math.pow(2, d + 1) && id >= Math.pow(2, d))
225                                 if (!neighbors.contains((Object) ((int) (id - Math.pow(2, d)))))
226                                         neighbors.add((int) (id - Math.pow(2, d)));
227                         d++;
228                 }
229
230                 // get backup neighbors
231                 int nb = reg.getNumBackupNeighbors();
232                 int rankOfBackTask;
233                 int tmp;
234                 for (int j = 1; j <= nb; j++) {
235                         // ------------ 1 - for backups "j + n" (to the right of j)
236                         rankOfBackTask = (id + j) % jaceSize;
237                         if (!neighbors.contains((Object) rankOfBackTask))
238                                 neighbors.add(rankOfBackTask);
239
240                         // ------------ 2 - for backups "j - n" (to the left of j)
241                         tmp = id - j;
242                         if (tmp >= 0) {
243                                 rankOfBackTask = tmp % jaceSize;
244                         } else {
245                                 rankOfBackTask = jaceSize - (Math.abs(tmp) % jaceSize);
246                         }
247                         if (!neighbors.contains((Object) rankOfBackTask))
248                                 neighbors.add(rankOfBackTask);
249                 }
250                 // adds itself
251                 neighbors.add(id);
252                 return neighbors;
253
254         }
255
256         public static int taille(int[] vect) {
257                 int taille = 0;
258                 int x = 0;
259                 while (x < vect.length && vect[x] >= 0) {
260                         taille++;
261                         x++;
262                 }
263                 return x;
264         }
265 }