3 import java.rmi.RemoteException;
4 import java.rmi.server.UnicastRemoteObject;
5 import java.util.ArrayList;
6 import java.util.Vector;
8 import and.Mapping.Algo;
9 import and.Mapping.DefaultMapping;
10 import and.Mapping.FT_AIAC_QM;
11 import and.Mapping.FT_FEC;
12 import and.Mapping.GNode;
13 import and.Mapping.GTask;
14 import and.Mapping.Graph;
15 import and.Mapping.Grid;
16 import and.Mapping.Maheve;
17 import and.Mapping.Simple;
20 public class JaceSuperNodeServer extends UnicastRemoteObject implements
21 JaceSuperNodeInterface {
22 private static final long serialVersionUID = 1L;
26 private ArrayList<GNode> gnodes = null;
27 private ArrayList<Algo> algos = null ;
28 private boolean inDemand = false, operating = false, authorized = false ;
32 public JaceSuperNodeServer(int timeBeat) throws RemoteException {
35 gnodes = new ArrayList<GNode>() ;
36 algos = new ArrayList<Algo>() ;
39 public int getSuperNodeBeat() throws RemoteException {
43 public Vector<?> sendStub(String IP, int port, JaceSuperNodeInterface stub)
44 throws RemoteException {
45 SuperNodeListe.Instance().addStubOf(IP, port, stub);
46 System.out.println("Added new superNode (" + IP
47 + ") to list and return List");
48 return SuperNodeListe.Instance().getListe();
51 public void updateCountNode(String IP, int nb) throws RemoteException {
52 SuperNodeListe.Instance().modifCountNode(IP, nb);
53 // System.out.println( "SuperNode " + IP + " has registered " + nb +
55 SuperNodeListe.Instance().viewAll();
58 public void sendSurplus(Vector<?> nodes, ArrayList<GNode> _gnodes) throws RemoteException {
59 System.out.println("Recieved " + nodes.size() + " nodes");
61 for (int i = 0; i < nodes.size(); i++) {
62 Register.Instance().getListe().add((Node) nodes.elementAt(i));
64 System.out.println("Adding "
65 + ((Node) nodes.elementAt(i)).getName());
68 ((Node) nodes.elementAt(i)).getStub().updateHeart(
69 LocalHost.Instance().getSuperNodeStub());
70 int index = SuperNodeListe.Instance().existSuperNode(
71 LocalHost.Instance().getIP());
72 ((SuperNodeData) SuperNodeListe.Instance().getListe()
74 .setNbOfNodes(Register.Instance().getSize());
75 new ForwardCount().start();
76 } catch (Exception e) {
77 System.err.println("Error changing Server in SendSurplus : "
82 /** Updating gnodes **/
89 public void setToken() throws RemoteException {
90 System.out.println("I got Token");
92 TokenThread.Instance().setToken();
95 HeartBeatSNode.Instance().getServer().beating(true);
97 System.out.println("Put token to true");
98 } catch (Exception e) {
100 .println("Unable to heartBeat the next SuperNode with the new Token : "
105 public void updateHeart(JaceSuperNodeInterface stub) throws RemoteException {
106 System.out.println("I change to ping a superNode");
108 HeartBeatSNode.Instance().setServer(stub);
111 public synchronized void removeSuperNode(SuperNodeData d)
112 throws RemoteException {
113 SuperNodeListe.Instance().removeSuperNode(d);
116 /****************************************************/
117 /****************************************************/
119 // Register a Daemon in the Register.Instance() of the SuperNode
120 public synchronized void workerRegistering(JaceInterface workerStub,
121 String workerIP, String workerName, int port, GNode g)
122 throws RemoteException {
123 System.out.println("CONNECTION of " + workerName);
126 Node noeud = new Node(workerStub);
127 noeud.setName(workerName);
128 noeud.setIP(g.getIP());
129 noeud.setAliveFlag(true);
130 noeud.setAliveTime();
132 noeud.setAppliName(null);
133 noeud.setNbOfBeats(0);
135 String sid = workerIP.replace( ".", "" ) + port ;
136 Long id = Long.parseLong( sid ) ;
141 g.setMapped( false ) ;
149 // Insert the node in the Register.Instance() of the Super Node
150 Register.Instance().addNode(noeud);
152 // Register.Instance().viewAll() ;
153 // SuperNodeListe.Instance().viewAll() ;
155 // Inform the other superNode and tell them the nb of Daemon I have
157 int index = SuperNodeListe.Instance().existSuperNode(
158 LocalHost.Instance().getIP());
160 ((SuperNodeData) SuperNodeListe.Instance().getListe().get(index))
161 .setNbOfNodes(Register.Instance().getSize());
162 SuperNodeListe.Instance().forwardCountNode();
165 SuperNodeListe.Instance().addGNode( g ) ;
173 * Update all mapping algorithms with a new node.
174 * @param _g The new node
176 * @author Sébastien Miquée
178 private synchronized void updateGrids( GNode _g )
182 for( int i = 0 ; i < algos.size() ; i++ )
184 algos.get( i ).getGrid().addGNode( _g ) ;
190 /****************************************************/
191 /****************************************************/
193 // HeartBeat that detects if a Daemon is dead or alive
194 public void beating(JaceInterface stub) throws RemoteException {
195 Node noeud = Register.Instance().getNodeOfStub(stub);
198 noeud.setAliveFlag(true);
199 noeud.setAliveTime();
200 noeud.incrementNbOfBeats();
202 // System.out.println( noeud.getName() + " is pinging me" ) ;
204 // System.out.println( noeud.getName() +
205 // ".................. is not in my list" ) ;
209 // HeartBeat that detects if a Super Node is dead or alive
210 public void beating(boolean token) throws RemoteException {
211 ScanThreadSuperNode.Instance().setAliveTime();
212 ScanThreadSuperNode.Instance().setToken(token);
214 // System.out.println( "Super Node is pinging me" ) ;
218 /*********************************/
219 /** Mapping !! Sébastien Miquée **/
220 /*********************************/
223 * Search and return to the requester spawner a register containing
224 * computation nodes plus some extra nodes if needed. This method requires
225 * as parameters the mapping algorithm choice and its parameters, and the
226 * amount of requested nodes and the task to be used, in order to
227 * construct the tasks graph.
228 * @param spawnerIP IP of the requester
229 * @param nbTasks Amount of tasks
230 * @param t Tasks to be executed
231 * @param nbNoeuds Total amount of requested nodes
232 * @param algo Identifier of the mapping algorithm
233 * @param paramAlgo Parameter of the mapping algorithm
235 * @author Sébastien Miquée
238 public synchronized Register getRegisterSpawner(String spawnerIP, int nbTasks, Task t,
239 int nbNoeuds, int algo, double paramAlgo) throws RemoteException {
241 // Have we the correct application ?
243 System.err.println( "Problem of class transmission!" ) ;
247 if( t.getDependencies( 0 ) == null )
249 System.err.println( "No redifinition of getDependencies() function!" ) ;
254 /** Creation of an empty new Register **/
255 Register reg = new Register() ;
259 /** Initialization of Grid architecture (G5K for now) **/
260 Grid grid = new Grid() ;
262 for( int i = 0 ; i < gnodes.size() ; i++ )
264 grid.addGNode( gnodes.get( i ) ) ;
268 /** Informing about the grid heterogeneity **/
269 System.out.println( "\nHeterogeneity of the grid : " + grid.getHeterogenityDegre() + "\n" ) ;
272 /** Creation of tasks GTask **/
273 ArrayList<GTask> ts = new ArrayList<GTask>();
274 for( int i = 0; i < nbTasks; i++ ) {
275 ts.add( new GTask( i ) ) ;
278 /** Research of dependencies **/
279 for( int i = 0; i < nbTasks; i++ ) {
281 dep = t.getDependencies( i ) ;
283 /** Adding dependencies to tasks **/
284 for( int j = 0; j < dep.length; j++ ) {
286 ts.get( i ).addDependance( ts.get( dep[ j ] ) ) ;
293 /** Creation of the application interaction graph **/
294 Graph graph = new Graph();
296 for( int i = 0 ; i < ts.size() ; i++)
298 graph.addGTask(ts.get(i));
301 /** TODO Loading algorithm class with parameters in a configuration file **/
302 /** Selection of the mapping algorithm **/
307 al = new Simple(graph, grid);
310 al = new FT_AIAC_QM(graph, grid, paramAlgo);
313 al = new FT_FEC(graph, grid, paramAlgo);
316 al = new Maheve( graph, grid ) ;
319 al = new DefaultMapping( graph, grid, gnodes ) ;
324 /** Launching the Mapping **/
328 /** Creating the register **/
329 ArrayList<GNode> ag = al.getMapping().getMappedGNodes();
331 System.out.println("NB returned nodes : "+ag.size());
334 /** Setting the algorithm's identifier **/
335 al.setIdS( spawnerIP ) ;
337 /** Adding the algorithm in the list **/
340 int idAlgo = searchAlgo( spawnerIP ) ;
342 /** Mapping distribution over other SuperNodes */
343 SuperNodeListe.Instance().setMapping( al ) ;
349 for( int i = 0; i < ag.size(); i++ ) {
350 reg.addNode((Node) ag.get(i).getNode());
352 ag.get(i).setMapped( true ) ;
353 setMapped( ag.get(i), 1 ) ;
354 Register.Instance().removeNode((Node) ag.get(i).getNode());
358 /** Searching extra nodes if any **/
359 if( nbNoeuds > nbTasks )
361 int nb = nbNoeuds - nbTasks ;
364 for( int i = 0 ; i < nb ; i ++ )
366 sup = algos.get(idAlgo).getOtherGNode( gnodes ) ;
367 algos.get(idAlgo).getMapping().addOtherNode( sup ) ;
368 reg.addNodeBeg( (Node) sup.getNode() ) ;
369 setMapped( sup, 1 ) ;
370 Register.Instance().removeNode((Node) sup.getNode());
376 if (ag.size() != 0) {
377 SuperNodeListe.Instance().forwardCountNode();
384 /** Returning result **/
389 public synchronized void setMapped( GNode _g, int _mode ) throws RemoteException
393 _g.setMapped( true ) ;
395 for( int i = 0 ; i < gnodes.size() ; i++ )
397 if( _g.getId() == gnodes.get( i ).getId() )
404 for( int i = 0 ; i < algos.size() ; i++ )
406 algos.get( i ).getGrid().setMappedStatus( _g, true ) ;
413 SuperNodeListe.Instance().setMappedGNode( _g, 0 ) ;
418 System.err.println( "GNode is null is setMapped!" ) ;
423 * Suppress a node from the gnodes list if this node is free, or remove
424 * it from the Mapping class of the mapping algorithm if this node is
425 * a computation node. After having done the local suppression of the node
426 * it propagates the suppression to other SuperNodes.
427 * @param _n The fallen node
428 * @param _mode Choice of the list (0: gnodes, 1: mapping, 2:just mapped)
430 * @author Sébastien Miquée
433 public synchronized GNode delGNodeFromList( Node _n, int _mode, String _spawnerIp ) throws RemoteException
435 GNode removedGNode = null ;
439 boolean free = false ;
451 for( int i = 0 ; i < gnodes.size() ; i++ )
453 if( _n.getId() == ((Node) gnodes.get(i).getNode()).getId() )
455 dead = gnodes.remove( i ) ;
462 for( int i = 0 ; i < algos.size() ; i++ )
464 algos.get( i ).getGrid().removeGNode( dead ) ;
465 Register.Instance().removeNodeOfName( dead.getName() ) ;
466 SuperNodeListe.Instance().removeGNode( dead, _mode, _spawnerIp ) ;
469 System.err.println( "The dead node signaled does not exist!" ) ;
480 return removedGNode ;
485 * Search the position of the mapping algorithm in the list.
486 * @param _id Identifier of the algorithm
487 * @return The position in the list
489 private int searchAlgo( String _id )
493 for( int i = 0 ; i < algos.size() ; i++ )
495 if( algos.get( i ).getIdS().equals( _id ) )
506 * Determine if this SuperNode can operate on the gnodes list.
507 * It asks other SuperNodes to know if their are working on this list
508 * and set the authorization, or not.
510 * @author Sébastien Miquée
512 private synchronized void workingOnGnodes()
524 tmp = tmp && SuperNodeListe.Instance().workingOnGnodes() ;
534 } catch (InterruptedException e) {
544 * Search and return a new node when one failed or when JaceP2P needs another
545 * one. For the first case, the given failed node should not be null, and
546 * null in the other case.
547 * @param _spawnerIP IP of the requester
548 * @param _rank The task number number of the dead node
550 * @author Sébastien Miquée
553 public synchronized Node getNewNode( String _spawnerIP, int _rank ) throws RemoteException
556 int idAlgo = searchAlgo( _spawnerIP ) ;
560 System.err.println( "No algorithm found for this Spawner!" ) ;
564 /** We have to replace a Spawner **/
569 /** We have to replace a computing node **/
570 idNode = algos.get( idAlgo ).getMapping().getIdOfAssociation( _rank ) ;
575 System.err.println( "No tasks corresponds to rank "+_rank+"!" ) ;
583 dead = algos.get( idAlgo ).getMapping().getAssociation( idNode ).getGNode() ;
586 if( dead == null && idNode >= 0 )
588 System.err.println( "The GNode corresponding to the task "+_rank+" is null!" ) ;
589 System.err.println( "Trying to find a new node for this task (not optimal)..." ) ;
592 return getNewNode( _spawnerIP, dead ) ;
596 * Search and return a new node when one failed or when JaceP2P needs another
597 * one. For the first case, the given failed node should not be null, and
598 * null in the other case.
599 * @param _spawnerIP IP of the requester
600 * @param _deadNode The failed node
602 * @author Sébastien Miquée
605 public synchronized Node getNewNode( String _spawnerIP, GNode _deadNode ) throws RemoteException
610 int idAlgo = searchAlgo( _spawnerIP ) ;
614 System.err.println( "No algorithm found for this Spawner!" ) ;
619 /** Can we use gnodes ?**/
622 if( _deadNode != null )
626 removeMappedGNode( _deadNode, 1 ) ;
630 remp = algos.get( idAlgo ).replaceNode( _deadNode, gnodes ) ;
634 System.out.println( "Replacing node found." ) ;
638 remp.setMapped( true ) ;
640 setMapped( remp, 1 ) ;
641 Register.Instance().removeNodeOfName( remp.getName() ) ;
644 /** Updating all mapping **/
645 updateMappedNode( _deadNode, remp, _spawnerIP ) ;
646 propagateReplaceNode( _deadNode, remp, _spawnerIP ) ;
650 SuperNodeListe.Instance().forwardCountNode();
652 System.err.println( "Replacing node not found !!" ) ;
655 remp = algos.get( idAlgo ).getOtherGNode( gnodes ) ;
659 System.out.println( "Other new node found." ) ;
661 remp.setMapped( true ) ;
665 setMapped( remp, 1 ) ;
666 Register.Instance().removeNodeOfName( remp.getName() ) ;
670 SuperNodeListe.Instance().forwardCountNode();
672 System.err.println( "Other new node not found !!" ) ;
678 node = (Node) remp.getNode() ;
680 System.err.println( "SuperNode: No new node found!" ) ;
684 /** Free the gnodes use **/
691 public synchronized void removeMappedGNode( GNode _g, int _mode ) throws RemoteException
695 for( int i = 0 ; i < algos.size() ; i++ )
697 algos.get( i ).getGrid().removeGNode( _g ) ;
702 SuperNodeListe.Instance().removeGNode( _g, 0 ) ;
705 System.err.println( "GNode to be deleted is null!" ) ;
710 * Replace a failed GNode in the mapping by another one.
711 * This method is called by the SuperNodeList, to broadcast the modification.
712 * @param _dead The failed node
713 * @param _remp The replacing node
715 * @author Sébastien Miquée
718 public synchronized void updateMappedNode( GNode _dead, GNode _remp, String _spawnerIP )
724 int idAlgo = searchAlgo( _spawnerIP ) ;
728 System.err.println( "No algorithm found for this Spawner!" ) ;
732 pos = algos.get( idAlgo ).getMapping().getIdOfAssociation( _dead ) ;
736 /** Changing the node in the mapping **/
737 if( algos.get( idAlgo ).getMapping().getMapping().get( pos ).setGNode( _remp ) )
739 System.out.println( "Succesfully replacing the fallen node in the mapping." ) ;
741 System.err.println( "Failed in replacing the fallen node in the mapping!" ) ;
744 System.err.println( "The dead node was not found in the mapping!" ) ;
748 System.err.println( "The new GNode is null!" ) ;
754 * Inform all SuperNodes of the replacement of a failed computing node.
755 * @param _dead The failed node
756 * @param _remp The replacing node
758 * @author Sébastien Miquée
760 private synchronized void propagateReplaceNode( GNode _dead, GNode _remp, String _spawnerIP ) throws RemoteException
764 SuperNodeListe.Instance().propagateReplaceNode( _dead, _remp, _spawnerIP ) ;
766 System.err.println( "The replacement node is null!" ) ;
770 /**********************************************************/
771 /**********************************************************/
776 * Add a new node in the list.
777 * @param _g The new node
779 * @author Sébastien Miquée
781 public void addGNode( GNode _g ) throws RemoteException
785 boolean free = false ;
804 * Set the local mapping with another done on an other SuperNode.
805 * @param _al The new mapping
807 * @author Sébastien Miquée
810 public synchronized void setMapping( Algo _al ) throws RemoteException
812 if( searchAlgo( _al.getIdS() ) == -1 )
816 System.err.println( "I already have this mapping algorithm!" ) ;
823 * Allow or deny the use of operations on the gnodes list, in order to
824 * do a mapping operation.
825 * @return The authorization or not to block gnodes
827 * @author Sébastien Miquée
829 public boolean blockForMapping() throws RemoteException
835 } catch (InterruptedException e) {
840 if( operating && ! authorized )
849 * Return the array containing the current not mapped nodes available.
850 * @return The array of available nodes
852 * @author Sébastien Miquée
855 public ArrayList<GNode> getGNodes() throws RemoteException
862 * Remove a mapping algorithm of the algorithms list.
863 * @param _id The algorithm identifier
864 * @param _mode Indicate if the information should be transmitted
866 * @author Sébastien Miquée
868 public void removeAlgo( String _id, int _mode ) throws RemoteException
872 pos = searchAlgo( _id ) ;
876 algos.remove( pos ) ;
880 SuperNodeListe.Instance().removeAlgo( _id ) ;
883 System.err.println( "The mapping algorithm requested for deletion does not exist!" ) ;
889 public Algo getAlgo( String _spID ) throws RemoteException
894 pos = searchAlgo( _spID ) ;
898 ret = algos.get( pos ) ;