Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1963ff78dbbf32cec03c93040a28484adb4d85f6
[jaceP2P.git] / src / jaceP2P / JaceSuperNodeServer.java
1 package jaceP2P;
2
3 import java.rmi.RemoteException;
4 import java.rmi.server.UnicastRemoteObject;
5 import java.util.ArrayList;
6 import java.util.Vector;
7
8 import and.Mapping.Algo;
9 import and.Mapping.DefaultMapping;
10 import and.Mapping.GNode;
11 import and.Mapping.GTask;
12 import and.Mapping.Graph;
13 import and.Mapping.Grid;
14 import and.Mapping.LSM;
15 import and.Mapping.Mapping;
16 import and.Mapping.QM;
17 import and.Mapping.Simple;
18 import and.Mapping.Utils;
19
20
21 public class JaceSuperNodeServer extends UnicastRemoteObject implements
22                 JaceSuperNodeInterface {
23         private static final long serialVersionUID = 1L;
24
25         // Attributes
26         private int beat;
27         private ArrayList<GNode> gnodes = null;
28         private Algo al = null ;
29         private int count = 0 ;
30         private boolean inDemand = false, operating = false, authorized = false ;
31 //      private boolean daemonListChange ;
32         
33         // Constructors
34
35         public JaceSuperNodeServer(int timeBeat) throws RemoteException {
36                 super() ;
37                 beat = timeBeat ;
38                 gnodes = new ArrayList<GNode>() ;
39 //              daemonListChange = true ;
40         }
41
42         public int getSuperNodeBeat() throws RemoteException {
43                 return beat;
44         }
45
46         public Vector<?> sendStub(String IP, int port, JaceSuperNodeInterface stub)
47                         throws RemoteException {
48                 SuperNodeListe.Instance().addStubOf(IP, port, stub);
49                 System.out.println("Added new superNode (" + IP
50                                 + ") to list and return List");
51                 return SuperNodeListe.Instance().getListe();
52         }
53
54         public void updateCountNode(String IP, int nb) throws RemoteException {
55                 SuperNodeListe.Instance().modifCountNode(IP, nb);
56                 // System.out.println( "SuperNode " + IP + " has registered " + nb +
57                 // " Daemons" ) ;
58                 SuperNodeListe.Instance().viewAll();
59         }
60
61         public void sendSurplus(Vector<?> nodes) throws RemoteException {
62                 System.out.println("Recieved " + nodes.size() + " nodes");
63
64                 for (int i = 0; i < nodes.size(); i++) {
65                         Register.Instance().getListe().add((Node) nodes.elementAt(i));
66
67                         System.out.println("Adding "
68                                         + ((Node) nodes.elementAt(i)).getName());
69
70                         try {
71                                 ((Node) nodes.elementAt(i)).getStub().updateHeart(
72                                                 LocalHost.Instance().getSuperNodeStub());
73                                 int index = SuperNodeListe.Instance().existSuperNode(
74                                                 LocalHost.Instance().getIP());
75                                 ((SuperNodeData) SuperNodeListe.Instance().getListe()
76                                                 .get(index))
77                                                 .setNbOfNodes(Register.Instance().getSize());
78                                 new ForwardCount().start();
79                         } catch (Exception e) {
80                                 System.out.println("Error changing Server in SendSurplus : "
81                                                 + e);
82                         }
83                 }
84         }
85
86         public void setToken() throws RemoteException {
87                 System.out.println("I got Token");
88
89                 TokenThread.Instance().setToken();
90
91                 try {
92                         HeartBeatSNode.Instance().getServer().beating(true);
93
94                         System.out.println("Put token to true");
95                 } catch (Exception e) {
96                         System.out
97                                         .println("Unable to heartBeat the next SuperNode with the new Token : "
98                                                         + e);
99                 }
100         }
101
102         public void updateHeart(JaceSuperNodeInterface stub) throws RemoteException {
103                 System.out.println("I change to ping a superNode");
104
105                 HeartBeatSNode.Instance().setServer(stub);
106         }
107
108         public synchronized void removeSuperNode(SuperNodeData d)
109                         throws RemoteException {
110                 SuperNodeListe.Instance().removeSuperNode(d);
111         }
112
113         /****************************************************/
114         /****************************************************/
115
116         // Register a Daemon in the Register.Instance() of the SuperNode
117         public synchronized void workerRegistering(JaceInterface workerStub,
118                         String workerIP, String workerName, int port, GNode g)
119                         throws RemoteException {
120                 System.out.println("CONNEXION of " + workerName);
121
122                 // Create the node
123                 Node noeud = new Node(workerStub);
124                 noeud.setName(workerName);
125                 noeud.setIP(workerIP);
126                 noeud.setAliveFlag(true);
127                 noeud.setAliveTime();
128                 noeud.setPort(port);
129                 noeud.setAppliName(null);
130                 noeud.setNbOfBeats(0);
131                 noeud.setId( count ) ;
132                 noeud.setId( Long.parseLong( workerIP.replace( ".", "" ) ) ) ;
133
134                 g.setNode(noeud);
135                 g.setId( noeud.getId() ) ;
136                 
137                 workingOnGnodes() ;
138                 
139                 gnodes.add(g);
140                 
141 //              daemonListChange = true ;
142
143                 // Insert the node in the Register.Instance() of the Super Node
144                 Register.Instance().addNode(noeud);
145
146                 // Register.Instance().viewAll() ;
147                 // SuperNodeListe.Instance().viewAll() ;
148
149                 // Inform the other superNode and tell them the nb of Daemon I have
150                 // registered
151                 int index = SuperNodeListe.Instance().existSuperNode(
152                                 LocalHost.Instance().getIP());
153
154                 ((SuperNodeData) SuperNodeListe.Instance().getListe().get(index))
155                                 .setNbOfNodes(Register.Instance().getSize());
156                 SuperNodeListe.Instance().forwardCountNode();
157                 
158                 SuperNodeListe.Instance().addGNode( g ) ;
159                 
160                 operating = false ;
161         }
162
163         /****************************************************/
164         /****************************************************/
165
166         // HeartBeat that detects if a Daemon is dead or alive
167         public void beating(JaceInterface stub) throws RemoteException {
168                 Node noeud = Register.Instance().getNodeOfStub(stub);
169
170                 if (noeud != null) {
171                         noeud.setAliveFlag(true);
172                         noeud.setAliveTime();
173                         noeud.incrementNbOfBeats();
174                         // -- sm modif
175                         // System.out.println( noeud.getName() + " is pinging me" ) ;
176                 } else {
177                         // System.out.println( noeud.getName() +
178                         // ".................. is not in my list" ) ;
179                 }
180         }
181
182         // HeartBeat that detects if a Super Node is dead or alive
183         public void beating(boolean token) throws RemoteException {
184                 ScanThreadSuperNode.Instance().setAliveTime();
185                 ScanThreadSuperNode.Instance().setToken(token);
186                 // -- sm modif
187                 // System.out.println( "Super Node is pinging me" ) ;
188         }
189
190
191         /*********************************/
192         /** Mapping !! Sébastien Miquée **/
193         /*********************************/
194
195         public Register getRegisterSpawner(String spawnerIP, int nbTasks, Task t,
196                         int nbNoeuds, int algo, double paramAlgo) throws RemoteException {
197
198                 // Have we the correct application ?
199                 if (t == null) {
200                         System.err.println( "Problem of class transmission !" ) ;
201                         return null ;
202                 }
203                 
204                 if( t.getDependencies( 0 ) == null )
205                 {
206                         System.err.println( "No redifinition of getDependencies() functions !" ) ;
207                         return null ;
208                 }
209                 
210
211                 /** Creation of an empty new Register **/
212                 Register reg = new Register() ;
213
214                 workingOnGnodes() ;
215                 
216                 /** Initialization of Grid architecture (G5K for now) **/
217                 Grid grid = Utils.createGridG5k(gnodes);
218                 grid.initClusters();
219                 
220
221                 /** Creation of tasks GTask **/
222                 ArrayList<GTask> ts = new ArrayList<GTask>();
223                 for (int i = 0; i < nbTasks; i++) {
224                         ts.add(new GTask( i ) ) ;
225                 }
226
227                 /** Research of dependencies **/
228                 for (int i = 0; i < nbTasks; i++) {
229                         int dep[] = null;
230                         dep = t.getDependencies(i);
231
232                         /** Adding dependencies to tasks **/
233                         for (int j = 0; j < dep.length; j++) {
234                                 if (dep[j] != -1) {
235                                         ts.get(i).addDependance(ts.get(dep[j]));
236                                 } else {
237                                         break;
238                                 }
239                         }
240                 }
241                 
242
243                 Graph graph = new Graph();
244
245                 for( int i = 0 ; i < ts.size() ; i++) 
246                 {
247                         graph.addGTask(ts.get(i));
248                 }
249
250                 // -- debug !
251                 // graph.print() ;
252
253                 // try {
254                 // Thread.sleep(10000) ;
255                 // } catch( Exception e ) {}
256
257                 // grid.print() ;
258                 // 
259                 // try {
260                 // Thread.sleep( 10000 ) ;
261                 // } catch( Exception e ) {}
262
263                 /** Selection of the mapping algorithm **/
264                 al = null ;
265
266                 switch (algo) {
267                 case 0:
268                         al = new Simple(graph, grid);
269                         break;
270                 case 1:
271                         al = new QM(graph, grid, paramAlgo);
272                         break;
273                 case 2:
274                         al = new LSM(graph, grid, paramAlgo);
275                         break;
276                 default:
277                         al = new DefaultMapping( graph, grid, gnodes ) ;
278                 }
279
280                 if (al != null) {
281                         /** Launching the Mapping **/
282                         al.map();
283
284
285                         /** Transforming mapping in register **/
286                         Mapping mp = al.getMapping();
287                         
288                         
289                         /** Creating the register **/
290                         ArrayList<GNode> ag = mp.getMappedGNodes();
291
292                         authorized = true ;
293                         
294                         for (int i = 0; i < ag.size(); i++) {
295                                 reg.addNode((Node) ag.get(i).getNode());
296                                 delGNodeFromList( (Node) ag.get(i).getNode() ) ;
297 //                              gnodes.remove(ag.get(i));
298 //                              Register.Instance().removeNode((Node) ag.get(i).getNode());
299                         }
300                         
301                         
302                         /** Searching extra nodes if any **/
303                         if( nbNoeuds > nbTasks )
304                         {
305                                 int nb = nbNoeuds - nbTasks ;
306                                 GNode sup = null ;
307                                 
308                                 for( int i = 0 ; i < nb ; i ++ )
309                                 {
310                                         sup = al.getOtherGNode( gnodes ) ;
311                                         reg.addNodeBeg( (Node) sup.getNode() ) ;
312                                         delGNodeFromList( (Node) sup.getNode() ) ;
313                                 }
314                         }
315                         
316                         authorized = false ;
317
318                         if (ag.size() != 0) {
319                                 SuperNodeListe.Instance().forwardCountNode();
320                         }
321
322                 } 
323                 
324 //              daemonListChange = false ;
325
326                 System.out.println( "Spawner returned reg: " + reg ) ;
327                 
328                 /** Mapping distribution over other Super Nodes */
329                 SuperNodeListe.Instance().setMapping( al ) ;
330                 
331                 operating = false ;
332                 
333                 /* Returning result */
334                 return reg ;
335         }
336
337         /*****************************************/
338         /****        Sébastien Miquée         ****/
339         /**                                     **/
340         /**      Recherche nouveau noeud        **/
341         /*****************************************/
342         
343         protected GNode delGNodeFromList( Node _n )
344         {
345                 GNode removedGNode = null ;
346                 
347                 if( _n != null )
348                 {       
349                         boolean free = false ;
350                         
351                         if( ! operating )
352                         {
353                                 workingOnGnodes() ;
354                                 free = true ;
355                         }
356                         
357                         for( int i = 0 ; i < gnodes.size() ; i++ )
358                         {
359                                 if( ((Node)gnodes.get(i).getNode()).getId() == _n.getId() ) 
360                                 {
361                                         removedGNode = gnodes.remove( i ) ;
362                                         Register.Instance().removeNodeOfName( removedGNode.getName() ) ;
363                                         break ;
364                                 }
365                         }
366                 
367 //              daemonListChange = true ;
368                 
369                         SuperNodeListe.Instance().removeGNode( removedGNode ) ;
370                         
371                         if( free )
372                         {
373                                 operating = false ;
374                         }
375                 
376                 }
377                 
378                 return removedGNode ;
379         }
380         
381         
382         private void workingOnGnodes()
383         {
384                 boolean tmp = true ;    
385                 
386                 inDemand = false ; 
387                 operating = false ;
388             
389                 while( ! operating )
390                 {
391                         inDemand = true ;
392                         tmp = true ;
393                         
394                         tmp = tmp && SuperNodeListe.Instance().workingOnGnodes() ;
395
396                         operating = tmp ;
397                         
398                         if( ! tmp )
399                         {
400                                 inDemand = false ;
401                                 
402                                 try {
403                                         Thread.sleep( 10 ) ;
404                                 } catch (InterruptedException e) {
405                                         e.printStackTrace();
406                                 }
407                         }
408                 }
409                 
410                 inDemand = false ;
411         }
412
413                 
414         public Node getNewNode( String _spawnerIP, Node _deadNode ) throws RemoteException
415         {
416                 Node node = null ;
417                 GNode remp = null, gnode = null ;
418                 
419                 /** Can we use gnodes ?**/
420                 workingOnGnodes() ;
421                 
422                 if( _deadNode != null )
423                 {
424                         gnode = delGNodeFromList( _deadNode ) ;
425                 
426                         remp = al.replaceNode( gnode, gnodes ) ;
427                 
428                         if( remp != null )
429                         {
430                                 System.out.println( "Replacing node found." ) ;
431                                 node = (Node) remp.getNode() ;
432                                 
433                                 authorized = true ;
434                                 
435                                 delGNodeFromList( node ) ;
436                                 
437                                 /** Updating all mapping **/
438                                 updateMappedNode( gnode, remp ) ;
439                                 propagateReplaceNode( gnode, remp ) ;
440                                 
441                                 authorized = false ;
442                                 
443                                 SuperNodeListe.Instance().forwardCountNode();
444                         } else {
445                                 System.err.println( "Replacing node not found !!" ) ;
446                         }
447                 } else {
448                         remp = al.getOtherGNode( gnodes ) ;
449                         
450                         if( remp != null )
451                         {
452                                 System.out.println( "Other new node found." ) ;
453                                 node = (Node) remp.getNode() ;
454                                 
455                                 authorized = true ;
456                                 
457                                 delGNodeFromList( node ) ;
458                                 
459                                 authorized = false ;
460                                 
461                                 SuperNodeListe.Instance().forwardCountNode();
462                         } else {
463                                 System.err.println( "Other new node not found !!" ) ;
464                         }
465                 }
466                 
467                 /** Free the gnodes use **/
468                 operating = false ;
469                 
470                 return node ;
471         }
472         
473         @Override
474         public void updateMappedNode( GNode _dead, GNode _remp ) 
475         {
476                 int pos = 0 ;
477                 pos = al.getMapping().getIdOfAssociation( _dead ) ;
478                 
479                 if( pos == -1 )
480                 {
481                         System.err.println( "GNode "+_dead+" does not exist in the mapping!" ) ;
482                         return ;
483                 }
484                 
485                 al.getMapping().getMapping().get( pos ).setGNode( _remp ) ;             
486
487                 System.out.println( "Succesfully replacing the fallen node in the mapping." ) ;
488         }
489
490         
491         /**
492          * Inform all SuperNodes of the replacement of a fallen computing node.
493          * @param _dead The fallen node
494          * @param _remp The replacing node
495          */
496         private void propagateReplaceNode( GNode _dead, GNode _remp ) 
497         {
498                 if( _dead != null && _remp != null )
499                 {       
500                         SuperNodeListe.Instance().propagateReplaceNode( _dead, _remp ) ;
501                 }
502         }
503
504         /**********************************************************/
505         /**********************************************************/
506
507         @Override
508         /**
509          * Add a new node in the list.
510          * @param _g The new node
511          */
512         public void addGNode( GNode _g ) throws RemoteException 
513         {
514                 if( _g != null )
515                 {
516                         workingOnGnodes() ;
517                         
518                         gnodes.add( _g ) ;
519                         
520                         operating = false ;
521                 }
522         }
523         
524
525         @Override
526         public void removeGNode( GNode _g ) throws RemoteException 
527         {
528                 if( _g != null )
529                 {
530                         boolean free = false ;
531                         
532                         if( ! operating )
533                         {
534                                 workingOnGnodes() ;
535                                 free = true ;
536                         }
537                         
538                         for( int i = 0 ; i < gnodes.size() ; i++ )
539                         {
540                                 if( ((Node)gnodes.get(i).getNode()).getId() == ((Node)_g.getNode()).getId() ) 
541                                 {
542                                         gnodes.remove( i ) ;
543                                         Register.Instance().removeNodeOfName( _g.getName() ) ;
544                                         break ;
545                                 }
546                         }
547                         
548                         if( free )
549                                 operating = false ;
550                 }
551                 
552         }
553         
554
555         @Override
556         public void setMapping( Algo _al ) throws RemoteException 
557         {
558                 al = _al ;              
559         }
560
561         
562         @Override
563         /**
564          * Allow or deny the use of operations on the gnodes list, in order to
565          * do a mapping operation.
566          * @author miquee
567          * @return The authorization or not to block gnodes
568          */
569         public boolean blockForMapping() throws RemoteException
570         {
571                 while( inDemand )
572                 {
573                         try {
574                                 Thread.sleep( 10 ) ;
575                         } catch (InterruptedException e) {
576                                 e.printStackTrace();
577                         }
578                 }
579                 
580                 if( operating && ! authorized )
581                 {
582                         return false ;
583                 } else {
584                         return true ;
585                 }
586         }
587         
588 }
589
590 /** ! **/