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 degree 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 ) ;
317 ((Maheve) al).setNbSave((int)paramAlgo);
320 al = new DefaultMapping( graph, grid, gnodes ) ;
325 /** Launching the Mapping **/
329 /** Creating the register **/
330 ArrayList<GNode> ag = al.getMapping().getMappedGNodes();
332 System.out.println("NB returned nodes : "+ag.size());
335 /** Setting the algorithm's identifier **/
336 al.setIdS( spawnerIP ) ;
338 /** Adding the algorithm in the list **/
341 int idAlgo = searchAlgo( spawnerIP ) ;
343 /** Mapping distribution over other SuperNodes */
344 SuperNodeListe.Instance().setMapping( al ) ;
350 for( int i = 0; i < ag.size(); i++ ) {
351 reg.addNode((Node) ag.get(i).getNode());
353 ag.get(i).setMapped( true ) ;
354 setMapped( ag.get(i), 1 ) ;
355 Register.Instance().removeNode((Node) ag.get(i).getNode());
359 /** Searching extra nodes if any **/
360 if( nbNoeuds > nbTasks )
362 int nb = nbNoeuds - nbTasks ;
365 for( int i = 0 ; i < nb ; i ++ )
367 sup = algos.get(idAlgo).getOtherGNode( gnodes ) ;
368 algos.get(idAlgo).getMapping().addOtherNode( sup ) ;
369 reg.addNodeBeg( (Node) sup.getNode() ) ;
370 setMapped( sup, 1 ) ;
371 Register.Instance().removeNode((Node) sup.getNode());
377 if (ag.size() != 0) {
378 SuperNodeListe.Instance().forwardCountNode();
385 /** Returning result **/
390 public synchronized void setMapped( GNode _g, int _mode ) throws RemoteException
394 _g.setMapped( true ) ;
396 for( int i = 0 ; i < gnodes.size() ; i++ )
398 if( _g.getId() == gnodes.get( i ).getId() )
405 for( int i = 0 ; i < algos.size() ; i++ )
407 algos.get( i ).getGrid().setMappedStatus( _g, true ) ;
414 SuperNodeListe.Instance().setMappedGNode( _g, 0 ) ;
419 System.err.println( "GNode is null is setMapped!" ) ;
424 * Suppress a node from the gnodes list if this node is free, or remove
425 * it from the Mapping class of the mapping algorithm if this node is
426 * a computation node. After having done the local suppression of the node
427 * it propagates the suppression to other SuperNodes.
428 * @param _n The fallen node
429 * @param _mode Choice of the list (0: gnodes, 1: mapping, 2:just mapped)
431 * @author Sébastien Miquée
434 public synchronized GNode delGNodeFromList( Node _n, int _mode, String _spawnerIp ) throws RemoteException
436 GNode removedGNode = null ;
440 boolean free = false ;
452 for( int i = 0 ; i < gnodes.size() ; i++ )
454 if( _n.getId() == ((Node) gnodes.get(i).getNode()).getId() )
456 dead = gnodes.remove( i ) ;
463 for( int i = 0 ; i < algos.size() ; i++ )
465 algos.get( i ).getGrid().removeGNode( dead ) ;
466 Register.Instance().removeNodeOfName( dead.getName() ) ;
467 SuperNodeListe.Instance().removeGNode( dead, _mode, _spawnerIp ) ;
470 System.err.println( "The dead node signaled does not exist!" ) ;
481 return removedGNode ;
486 * Search the position of the mapping algorithm in the list.
487 * @param _id Identifier of the algorithm
488 * @return The position in the list
490 private int searchAlgo( String _id )
494 for( int i = 0 ; i < algos.size() ; i++ )
496 if( algos.get( i ).getIdS().equals( _id ) )
507 * Determine if this SuperNode can operate on the gnodes list.
508 * It asks other SuperNodes to know if their are working on this list
509 * and set the authorization, or not.
511 * @author Sébastien Miquée
513 private synchronized void workingOnGnodes()
525 tmp = tmp && SuperNodeListe.Instance().workingOnGnodes() ;
535 } catch (InterruptedException e) {
545 * Search and return a new node when one failed or when JaceP2P needs another
546 * one. For the first case, the given failed node should not be null, and
547 * null in the other case.
548 * @param _spawnerIP IP of the requester
549 * @param _rank The task number number of the dead node
551 * @author Sébastien Miquée
554 public synchronized Node getNewNode( String _spawnerIP, int _rank ) throws RemoteException
557 int idAlgo = searchAlgo( _spawnerIP ) ;
561 System.err.println( "No algorithm found for this Spawner!" ) ;
565 /** We have to replace a Spawner **/
570 /** We have to replace a computing node **/
571 idNode = algos.get( idAlgo ).getMapping().getIdOfAssociation( _rank ) ;
576 System.err.println( "No tasks corresponds to rank "+_rank+"!" ) ;
584 dead = algos.get( idAlgo ).getMapping().getAssociation( idNode ).getGNode() ;
587 if( dead == null && idNode >= 0 )
589 System.err.println( "The GNode corresponding to the task "+_rank+" is null!" ) ;
590 System.err.println( "Trying to find a new node for this task (not optimal)..." ) ;
593 return getNewNode( _spawnerIP, dead ) ;
597 * Search and return a new node when one failed or when JaceP2P needs another
598 * one. For the first case, the given failed node should not be null, and
599 * null in the other case.
600 * @param _spawnerIP IP of the requester
601 * @param _deadNode The failed node
603 * @author Sébastien Miquée
606 public synchronized Node getNewNode( String _spawnerIP, GNode _deadNode ) throws RemoteException
611 int idAlgo = searchAlgo( _spawnerIP ) ;
615 System.err.println( "No algorithm found for this Spawner!" ) ;
620 /** Can we use gnodes ?**/
623 if( _deadNode != null )
627 removeMappedGNode( _deadNode, 1 ) ;
631 remp = algos.get( idAlgo ).replaceNode( _deadNode, gnodes ) ;
635 System.out.println( "Replacing node found." ) ;
639 remp.setMapped( true ) ;
641 setMapped( remp, 1 ) ;
642 Register.Instance().removeNodeOfName( remp.getName() ) ;
645 /** Updating all mapping **/
646 updateMappedNode( _deadNode, remp, _spawnerIP ) ;
647 propagateReplaceNode( _deadNode, remp, _spawnerIP ) ;
651 SuperNodeListe.Instance().forwardCountNode();
653 System.err.println( "Replacing node not found !!" ) ;
656 remp = algos.get( idAlgo ).getOtherGNode( gnodes ) ;
660 System.out.println( "Other new node found." ) ;
662 remp.setMapped( true ) ;
666 setMapped( remp, 1 ) ;
667 Register.Instance().removeNodeOfName( remp.getName() ) ;
671 SuperNodeListe.Instance().forwardCountNode();
673 System.err.println( "Other new node not found !!" ) ;
679 node = (Node) remp.getNode() ;
681 System.err.println( "SuperNode: No new node found!" ) ;
685 /** Free the gnodes use **/
692 public synchronized void removeMappedGNode( GNode _g, int _mode ) throws RemoteException
696 for( int i = 0 ; i < algos.size() ; i++ )
698 algos.get( i ).getGrid().removeGNode( _g ) ;
703 SuperNodeListe.Instance().removeGNode( _g, 0 ) ;
706 System.err.println( "GNode to be deleted is null!" ) ;
711 * Replace a failed GNode in the mapping by another one.
712 * This method is called by the SuperNodeList, to broadcast the modification.
713 * @param _dead The failed node
714 * @param _remp The replacing node
716 * @author Sébastien Miquée
719 public synchronized void updateMappedNode( GNode _dead, GNode _remp, String _spawnerIP )
725 int idAlgo = searchAlgo( _spawnerIP ) ;
729 System.err.println( "No algorithm found for this Spawner!" ) ;
733 pos = algos.get( idAlgo ).getMapping().getIdOfAssociation( _dead ) ;
737 /** Changing the node in the mapping **/
738 if( algos.get( idAlgo ).getMapping().getMapping().get( pos ).setGNode( _remp ) )
740 System.out.println( "Succesfully replacing the fallen node in the mapping." ) ;
742 System.err.println( "Failed in replacing the fallen node in the mapping!" ) ;
745 System.err.println( "The dead node was not found in the mapping!" ) ;
749 System.err.println( "The new GNode is null!" ) ;
755 * Inform all SuperNodes of the replacement of a failed computing node.
756 * @param _dead The failed node
757 * @param _remp The replacing node
759 * @author Sébastien Miquée
761 private synchronized void propagateReplaceNode( GNode _dead, GNode _remp, String _spawnerIP ) throws RemoteException
765 SuperNodeListe.Instance().propagateReplaceNode( _dead, _remp, _spawnerIP ) ;
767 System.err.println( "The replacement node is null!" ) ;
771 /**********************************************************/
772 /**********************************************************/
777 * Add a new node in the list.
778 * @param _g The new node
780 * @author Sébastien Miquée
782 public void addGNode( GNode _g ) throws RemoteException
786 boolean free = false ;
805 * Set the local mapping with another done on an other SuperNode.
806 * @param _al The new mapping
808 * @author Sébastien Miquée
811 public synchronized void setMapping( Algo _al ) throws RemoteException
813 if( searchAlgo( _al.getIdS() ) == -1 )
817 System.err.println( "I already have this mapping algorithm!" ) ;
824 * Allow or deny the use of operations on the gnodes list, in order to
825 * do a mapping operation.
826 * @return The authorization or not to block gnodes
828 * @author Sébastien Miquée
830 public boolean blockForMapping() throws RemoteException
836 } catch (InterruptedException e) {
841 if( operating && ! authorized )
850 * Return the array containing the current not mapped nodes available.
851 * @return The array of available nodes
853 * @author Sébastien Miquée
856 public ArrayList<GNode> getGNodes() throws RemoteException
863 * Remove a mapping algorithm of the algorithms list.
864 * @param _id The algorithm identifier
865 * @param _mode Indicate if the information should be transmitted
867 * @author Sébastien Miquée
869 public void removeAlgo( String _id, int _mode ) throws RemoteException
873 pos = searchAlgo( _id ) ;
877 algos.remove( pos ) ;
881 SuperNodeListe.Instance().removeAlgo( _id ) ;
884 System.err.println( "The mapping algorithm requested for deletion does not exist!" ) ;
890 public Algo getAlgo( String _spID ) throws RemoteException
895 pos = searchAlgo( _spID ) ;
899 ret = algos.get( pos ) ;