Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
de7b8761de4ef6496c938546869a64b680cb2c38
[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 daemonListChange ;
31         
32         // Constructors
33
34         public JaceSuperNodeServer(int timeBeat) throws RemoteException {
35                 super() ;
36                 beat = timeBeat ;
37                 gnodes = new ArrayList<GNode>() ;
38 //              daemonListChange = true ;
39         }
40
41         public int getSuperNodeBeat() throws RemoteException {
42                 return beat;
43         }
44
45         public Vector<?> sendStub(String IP, int port, JaceSuperNodeInterface stub)
46                         throws RemoteException {
47                 SuperNodeListe.Instance().addStubOf(IP, port, stub);
48                 System.out.println("Added new superNode (" + IP
49                                 + ") to list and return List");
50                 return SuperNodeListe.Instance().getListe();
51         }
52
53         public void updateCountNode(String IP, int nb) throws RemoteException {
54                 SuperNodeListe.Instance().modifCountNode(IP, nb);
55                 // System.out.println( "SuperNode " + IP + " has registered " + nb +
56                 // " Daemons" ) ;
57                 SuperNodeListe.Instance().viewAll();
58         }
59
60         public void sendSurplus(Vector<?> nodes) throws RemoteException {
61                 System.out.println("Recieved " + nodes.size() + " nodes");
62
63                 for (int i = 0; i < nodes.size(); i++) {
64                         Register.Instance().getListe().add((Node) nodes.elementAt(i));
65
66                         System.out.println("Adding "
67                                         + ((Node) nodes.elementAt(i)).getName());
68
69                         try {
70                                 ((Node) nodes.elementAt(i)).getStub().updateHeart(
71                                                 LocalHost.Instance().getSuperNodeStub());
72                                 int index = SuperNodeListe.Instance().existSuperNode(
73                                                 LocalHost.Instance().getIP());
74                                 ((SuperNodeData) SuperNodeListe.Instance().getListe()
75                                                 .get(index))
76                                                 .setNbOfNodes(Register.Instance().getSize());
77                                 new ForwardCount().start();
78                         } catch (Exception e) {
79                                 System.out.println("Error changing Server in SendSurplus : "
80                                                 + e);
81                         }
82                 }
83         }
84
85         public void setToken() throws RemoteException {
86                 System.out.println("I got Token");
87
88                 TokenThread.Instance().setToken();
89
90                 try {
91                         HeartBeatSNode.Instance().getServer().beating(true);
92
93                         System.out.println("Put token to true");
94                 } catch (Exception e) {
95                         System.out
96                                         .println("Unable to heartBeat the next SuperNode with the new Token : "
97                                                         + e);
98                 }
99         }
100
101         public void updateHeart(JaceSuperNodeInterface stub) throws RemoteException {
102                 System.out.println("I change to ping a superNode");
103
104                 HeartBeatSNode.Instance().setServer(stub);
105         }
106
107         public synchronized void removeSuperNode(SuperNodeData d)
108                         throws RemoteException {
109                 SuperNodeListe.Instance().removeSuperNode(d);
110         }
111
112         /****************************************************/
113         /****************************************************/
114
115         // Register a Daemon in the Register.Instance() of the SuperNode
116         public synchronized void workerRegistering(JaceInterface workerStub,
117                         String workerIP, String workerName, int port, GNode g)
118                         throws RemoteException {
119                 System.out.println("CONNEXION of " + workerName);
120
121                 // Create the node
122                 Node noeud = new Node(workerStub);
123                 noeud.setName(workerName);
124                 noeud.setIP(workerIP);
125                 noeud.setAliveFlag(true);
126                 noeud.setAliveTime();
127                 noeud.setPort(port);
128                 noeud.setAppliName(null);
129                 noeud.setNbOfBeats(0);
130                 noeud.setId( count ) ;
131                 noeud.setId( Long.parseLong( workerIP.replace( ".", "" ) ) ) ;
132
133                 g.setNode(noeud);
134                 g.setId( noeud.getId() ) ;
135                 gnodes.add(g);
136                 
137 //              daemonListChange = true ;
138
139                 // Insert the node in the Register.Instance() of the Super Node
140                 Register.Instance().addNode(noeud);
141
142                 // Register.Instance().viewAll() ;
143                 // SuperNodeListe.Instance().viewAll() ;
144
145                 // Inform the other superNode and tell them the nb of Daemon I have
146                 // registered
147                 int index = SuperNodeListe.Instance().existSuperNode(
148                                 LocalHost.Instance().getIP());
149
150                 ((SuperNodeData) SuperNodeListe.Instance().getListe().get(index))
151                                 .setNbOfNodes(Register.Instance().getSize());
152                 SuperNodeListe.Instance().forwardCountNode();
153                 
154                 SuperNodeListe.Instance().addGNode( g ) ;
155         }
156
157         /****************************************************/
158         /****************************************************/
159
160         // HeartBeat that detects if a Daemon is dead or alive
161         public void beating(JaceInterface stub) throws RemoteException {
162                 Node noeud = Register.Instance().getNodeOfStub(stub);
163
164                 if (noeud != null) {
165                         noeud.setAliveFlag(true);
166                         noeud.setAliveTime();
167                         noeud.incrementNbOfBeats();
168                         // -- sm modif
169                         // System.out.println( noeud.getName() + " is pinging me" ) ;
170                 } else {
171                         // System.out.println( noeud.getName() +
172                         // ".................. is not in my list" ) ;
173                 }
174         }
175
176         // HeartBeat that detects if a Super Node is dead or alive
177         public void beating(boolean token) throws RemoteException {
178                 ScanThreadSuperNode.Instance().setAliveTime();
179                 ScanThreadSuperNode.Instance().setToken(token);
180                 // -- sm modif
181                 // System.out.println( "Super Node is pinging me" ) ;
182         }
183
184
185         /*********************************/
186         /** Mapping !! Sébastien Miquée **/
187         /*********************************/
188
189         public Register getRegisterSpawner(String spawnerIP, int nbTasks, Task t,
190                         int nbNoeuds, int algo, double paramAlgo) throws RemoteException {
191
192                 // Have we the correct application ?
193                 if (t == null) {
194                         System.err.println( "Problem of class transmission !" ) ;
195                         return null ;
196                 }
197                 
198                 if( t.getDependencies( 0 ) == null )
199                 {
200                         System.err.println( "No redifinition of getDependencies() functions !" ) ;
201                         return null ;
202                 }
203                 
204
205                 /** Creation of an empty new Register **/
206                 Register reg = new Register() ;
207
208                 /** Initialization of Grid architecture (G5K for now) **/
209                 Grid grid = Utils.createGridG5k(gnodes);
210                 grid.initClusters();
211
212                 /** Creation of tasks GTask **/
213                 ArrayList<GTask> ts = new ArrayList<GTask>();
214                 for (int i = 0; i < nbTasks; i++) {
215                         ts.add(new GTask( i ) ) ;
216                 }
217
218                 /** Research of dependencies **/
219                 for (int i = 0; i < nbTasks; i++) {
220                         int dep[] = null;
221                         dep = t.getDependencies(i);
222
223                         /** Adding dependencies to tasks **/
224                         for (int j = 0; j < dep.length; j++) {
225                                 if (dep[j] != -1) {
226                                         ts.get(i).addDependance(ts.get(dep[j]));
227                                 } else {
228                                         break;
229                                 }
230                         }
231                 }
232
233                 Graph graph = new Graph();
234
235                 for( int i = 0 ; i < ts.size() ; i++) 
236                 {
237                         graph.addGTask(ts.get(i));
238                 }
239
240                 // -- debug !
241                 // graph.print() ;
242
243                 // try {
244                 // Thread.sleep(10000) ;
245                 // } catch( Exception e ) {}
246
247                 // grid.print() ;
248                 // 
249                 // try {
250                 // Thread.sleep( 10000 ) ;
251                 // } catch( Exception e ) {}
252
253                 /** Selection of the mapping algorithm **/
254                 al = null ;
255
256                 switch (algo) {
257                 case 0:
258                         al = new Simple(graph, grid);
259                         break;
260                 case 1:
261                         al = new QM(graph, grid, paramAlgo);
262                         break;
263                 case 2:
264                         al = new LSM(graph, grid, paramAlgo);
265                         break;
266                 default:
267                         al = new DefaultMapping( graph, grid, gnodes ) ;
268                 }
269
270                 if (al != null) {
271                         /** Launching the Mapping **/
272                         al.map();
273
274                         /** Transforming mapping in register **/
275                         Mapping mp = al.getMapping();
276
277                         /** Creating the register **/
278                         ArrayList<GNode> ag = mp.getMappedGNodes();
279
280                         for (int i = 0; i < ag.size(); i++) {
281                                 reg.addNode((Node) ag.get(i).getNode());
282                                 gnodes.remove(ag.get(i));
283                                 Register.Instance().removeNode((Node) ag.get(i).getNode());
284                         }
285
286                         if (ag.size() != 0) {
287                                 SuperNodeListe.Instance().forwardCountNode();
288                         }
289
290                 } 
291 //              else {
292 //                      al = new DefaultMapping( graph, grid, gnodes ) ;
293 //                      /** Launching the Mapping **/
294 //                      al.map();
295 //
296 //                      /** Transforming mapping in register **/
297 //                      Mapping mp = al.getMapping();
298 //
299 //                      /** Creating the register **/
300 //                      ArrayList<GNode> ag = mp.getMappedGNodes();
301 //
302 //                      for (int i = 0; i < ag.size(); i++) {
303 //                              reg.addNode((Node) ag.get(i).getNode());
304 //                              gnodes.remove(ag.get(i));
305 //                              Register.Instance().removeNode((Node) ag.get(i).getNode());
306 //                      }
307 //
308 //                      if (ag.size() != 0) {
309 //                              SuperNodeListe.Instance().forwardCountNode();
310 //                      }
311 //                      return getRegisterSpawner(spawnerIP, nbTasks);
312 //              }
313                 
314 //              daemonListChange = false ;
315
316                 System.out.println( "Spawner returned reg: " + reg ) ;
317                 
318                 /* Mapping distribution over other Super Nodes */
319                 SuperNodeListe.Instance().setMapping( al ) ;
320                 
321                 /* Returning result */
322                 return reg ;
323         }
324
325         /*****************************************/
326         /****        Sébastien Miquée         ****/
327         /**                                     **/
328         /**      Recherche nouveau noeud        **/
329         /*****************************************/
330         
331         protected GNode delGNodeFromList( Node _n )
332         {
333                 GNode deadGNode = null ;
334                 
335                 for( int i = 0 ; i < gnodes.size() ; i++ )
336                 {
337                         if( ((Node)gnodes.get(i).getNode()).getId() == _n.getId() ) 
338                         {
339                                 deadGNode = gnodes.remove( i ) ;
340                                 break ;
341                         }
342                 }
343                 
344 //              daemonListChange = true ;
345                 
346                 SuperNodeListe.Instance().removeGNode( deadGNode ) ;
347                 
348                 return deadGNode ;
349         }
350
351         
352         public Node getNewNode( String _spawnerIP, Node _deadNode ) throws RemoteException
353         {
354                 Node node = null ;
355                 GNode remp = null, gnode = null ;
356                 
357                 
358                 if( _deadNode != null )
359                 {
360                         gnode = delGNodeFromList( _deadNode ) ;
361                 
362                         /* TODO */
363                         Mapping mp = al.getMapping() ;
364                 
365 //              ArrayList <GNode> mapped = mp.getMappedGNodes() ;
366                 
367                         mp.removeGNode( gnode ) ;
368                 
369                         /*****************/
370                         // mettre directement dans Algo !
371 //              for( int i = 0 ; i < mapped.size() ; i++ )
372 //              {
373 //                      gnode = mapped.get( i ) ;
374 //                      tmp = (Node) gnode.getNode() ;
375 //                      if( tmp.getId() == _deadNode.getId() )
376 //                      {
377 //                              mapped.remove( i ) ;
378 //                              break ;
379 //                      }
380 //              }
381                 
382                         remp = al.replaceNode( gnode, gnodes ) ;
383                 
384                         if( remp != null )
385                         {
386                                 System.out.println( "Replacing node found." ) ;
387                                 node = (Node) remp.getNode() ;
388                                 delGNodeFromList( node ) ;
389 //                              gnodes.remove( remp ) ;
390 //                              Register.Instance().removeNode( node );
391                                 SuperNodeListe.Instance().forwardCountNode();
392                         } else {
393                                 System.err.println( "Replacing node not found !!" ) ;
394                         }
395                 } else {
396                         remp = al.getOtherGNode() ;
397                         
398                         if( remp != null )
399                         {
400                                 System.out.println( "Other new node found." ) ;
401                                 node = (Node) remp.getNode() ;
402                                 delGNodeFromList( node ) ;
403 //                              gnodes.remove( remp ) ;
404 //                              Register.Instance().removeNode( node );
405                                 SuperNodeListe.Instance().forwardCountNode();
406                         } else {
407                                 System.err.println( "Other new node not found !!" ) ;
408                         }
409                 }
410                 
411                 return node ;
412         }
413         
414         
415         /**********************************************************/
416         /**********************************************************/
417         
418         
419 //      public Node getNewNode(String spawnerIP) throws RemoteException {
420 //              boolean found = false;
421 //              int i = 0;
422 //              Node tmpNode = null;
423 //              SuperNodeData d = null;
424 //              //int passage = 0;
425 //              String snode_IP;
426 //              JaceSuperNodeInterface stub = null;
427 //
428 //              try {
429 //                      System.out.println("\n" + spawnerIP
430 //                                      + " (spawner) requests a new node : ");
431 //                      while (i < Register.Instance().getSize() && found == false) {
432 //                              tmpNode = Register.Instance().getNodeAt(i);
433 //
434 //                              if (tmpNode.getAppliName() == null
435 //                                              && tmpNode.getAliveFlag() == true) {
436 //                                      tmpNode.setAppliName("notnull");
437 //                                      found = true;
438 //
439 //                                      // enlever maintenant le noeud du register de
440 //                                      // SuperNode??????
441 //                                      // System.out.println("je remove le noeud car il va beater le spawner (getNewNode)");
442 //                                      // Register.Instance().removeNodeAt(i);
443 //                                      Register.Instance().removeNode(tmpNode);
444 //                                      // Register.Instance().removeNode(tmpNode.getIP());
445 //
446 //                                      int index = SuperNodeListe.Instance().existSuperNode(
447 //                                                      LocalHost.Instance().getIP());
448 //
449 //                                      ((SuperNodeData) SuperNodeListe.Instance().getListe()
450 //                                                      .elementAt(index)).setNbOfNodes(Register.Instance()
451 //                                                      .getSize());
452 //                                      SuperNodeListe.Instance().forwardCountNode();
453 //                              }
454 //                              i++;
455 //                      }
456 //              } catch (Exception e1) {
457 //                      System.out.println("... plante en cherchant chez moi");
458 //              }
459 //
460 //              // Register.Instance().viewAll();
461 //              SuperNodeListe.Instance().viewAll();
462 //
463 //              try {
464 //                      // si pas assez de noeud sur ce superNode,
465 //                      if (found == false) {
466 //                              System.out.println("pas de noeud dispo chez moi");
467 //
468 //                              SuperNodeListe snodeListTmp = SuperNodeListe.Instance().clone();
469 //                              // while ( (found == false) && (passage <
470 //                              // SuperNodeListe.Instance().getSize()) ) {
471 //                              while ((found == false) && (snodeListTmp.getSize() > 0)) {
472 //                                      // System.out.println("passage = " + passage);
473 //                                      // d = SuperNodeListe.Instance().getBestSuperNodeData();
474 //                                      d = snodeListTmp.getBestSuperNodeData();
475 //                                      System.out.println("KKKKKKKKKKKKKKK ......... le best c "
476 //                                                      + d.getIP() + " il en a " + d.getNbOfNodes());
477 //                                      if (d != null) {
478 //                                              snode_IP = d.getIP();
479 //                                              // si c moi, je passe au suivant
480 //                                              if (LocalHost.Instance().getIP().equals(snode_IP)) {
481 //                                                      // passage++;
482 //                                                      System.out
483 //                                                                      .println("OUUUUPS,   c moi dc je tente un autre");
484 //                                                      snodeListTmp.removeSuperNode(d);
485 //                                                      continue;
486 //                                              }
487 //
488 //                                              stub = d.getStub();
489 //
490 //                                              if (stub != null) {
491 //                                                      try {
492 //                                                              Register tmpReg = stub.reserveLocalNodes(1);
493 //                                                              if (tmpReg != null) {
494 //                                                                      // for (int j = 0; j < tmpReg.getSize();
495 //                                                                      // j++) {
496 //                                                                      System.out.println("IL EN A 1 !!!!!!!");
497 //                                                                      tmpNode = tmpReg.getNodeAt(0);
498 //                                                                      found = true;
499 //                                                                      System.out.println("le snode " + snode_IP
500 //                                                                                      + " me reserve le noeud demande");
501 //                                                              } else {
502 //                                                                      System.out
503 //                                                                                      .println("MERDE !!!  pas de noeud dispo sur "
504 //                                                                                                      + snode_IP);
505 //                                                                      System.out
506 //                                                                                      .println("Je demande un noeud a un autre");
507 //                                                              }
508 //
509 //                                                      } catch (Exception e) {
510 //                                                              System.out
511 //                                                                              .println("le snode est mort, je demande les noeuds a un autre");
512 //                                                              // remettre localement a 0 le nb de noeuds de ce
513 //                                                              // superNode
514 //                                                              SuperNodeListe.Instance().modifCountNode(
515 //                                                                              snode_IP, 0);
516 //                                                              snodeListTmp = SuperNodeListe.Instance()
517 //                                                                              .clone();
518 //                                                      }
519 //                                              }
520 //                                      } else {
521 //                                              System.out
522 //                                                              .println("PUTAIN !!!! aucun noeud encore dispo sur les snode");
523 //                                              // passage = SuperNodeListe.Instance().getSize();
524 //                                      }
525 //                                      // passage++;
526 //                                      snodeListTmp.removeSuperNode(d);
527 //                              }
528 //                      }
529 //              } catch (Exception e2) {
530 //                      System.out.println("plante en cherchant chez les autres");
531 //              }
532 //
533 //              // si pas assez de noeud sur tous les superNode,
534 //              if (found == false) {
535 //                      System.out.println("aucun noeud dispo dans le systeme");
536 //              } else {
537 //                      System.out.println("je lui donne son noeud");
538 //              }
539 //
540 //              return tmpNode;
541 //      }
542
543 //      public Register reserveLocalNodes(int nb) throws RemoteException {
544 //              Register reg = null;
545 //              System.out
546 //                              .println("\nA superNode or a Spawner asks me for a register of "
547 //                                              + nb + " Daemons");
548 //
549 //              int count = 0;
550 //              int i = 0;
551 //              Node tmpNode;
552 //
553 //              if (Register.Instance().getSize() == 0) {
554 //                      return new Register();
555 //              } else {
556 //                      reg = new Register();
557 //
558 //                      while (i < Register.Instance().getSize() && count < nb) {
559 //                              tmpNode = Register.Instance().getNodeAt(i);
560 //
561 //                              if (tmpNode.getAppliName() == null
562 //                                              && tmpNode.getAliveFlag() == true
563 //                              /* && tmpNode.getNbOfBeats() > 10 */) {
564 //                                      // if node available, alive and not recently connected, then
565 //                                      tmpNode.setAppliName("notnull");
566 //                                      reg.addNode(tmpNode);
567 //
568 //                                      // remove now the node from SuperNode or later ?
569 //                                      // System.out.println("I remove the Node from SuperNode beating it will now beat the Spawner");
570 //                                      Register.Instance().removeNode(tmpNode);
571 //                                      count++;
572 //                              }
573 //                              // increment counter only y Node NOT REMOVED
574 //                              else {
575 //                                      i++;
576 //                              }
577 //                      }
578 //
579 //                      // Register.Instance().viewAll();
580 //                      System.out.println("Number of Daemons reserved on me : " + count);
581 //                      // Inform other SuperNodes that some Daemons initialy registered on
582 //                      // me
583 //                      // are no longer available because reserved for the spawner that
584 //                      // invoked me
585 //                      int index = SuperNodeListe.Instance().existSuperNode(
586 //                                      LocalHost.Instance().getIP());
587 //                      ((SuperNodeData) SuperNodeListe.Instance().getListe().elementAt(
588 //                                      index)).setNbOfNodes(Register.Instance().getSize());
589 //
590 //                      if (count != 0) {
591 //                              SuperNodeListe.Instance().forwardCountNode();
592 //                      }
593 //
594 //                      System.out.println("I return " + count + " nodes");
595 //                      // System.out.println( reg ) ;
596 //                      // SuperNodeListe.Instance().viewAll() ;
597 //                      return reg;
598 //              }
599 //      }
600
601         @Override
602         public void addGNode( GNode _g ) throws RemoteException 
603         {
604                 if( _g != null )
605                         gnodes.add( _g ) ; 
606         }
607         
608
609         @Override
610         public void removeGNode( GNode _g ) throws RemoteException 
611         {
612                 if( _g != null )
613                 {
614                         for( int i = 0 ; i < gnodes.size() ; i++ )
615                         {
616                                 if( ((Node)gnodes.get(i).getNode()).getId() == ((Node)_g.getNode()).getId() ) 
617                                 {
618                                         gnodes.remove( i ) ;
619                                         break ;
620                                 }
621                         }
622                 }
623                 
624         }
625         
626
627         @Override
628         public void setMapping( Algo _al ) throws RemoteException 
629         {
630                 al = _al ;              
631         }
632         
633 }
634
635 /** ! **/