Logo AND Algorithmique Numérique Distribuée

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