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.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.QM;
16 import and.Mapping.Simple;
17 import and.Mapping.Utils;
20 public class JaceSuperNodeServer extends UnicastRemoteObject implements
21 JaceSuperNodeInterface {
22 private static final long serialVersionUID = 1L;
26 private ArrayList<GNode> gnodes = null;
27 private Algo al = null ;
28 private int count = 0 ;
29 private boolean inDemand = false, operating = false, authorized = false ;
30 // private boolean daemonListChange ;
34 public JaceSuperNodeServer(int timeBeat) throws RemoteException {
37 gnodes = new ArrayList<GNode>() ;
38 // daemonListChange = true ;
41 public int getSuperNodeBeat() throws RemoteException {
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();
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 +
57 SuperNodeListe.Instance().viewAll();
60 public void sendSurplus(Vector<?> nodes, ArrayList<GNode> _gnodes) throws RemoteException {
61 System.out.println("Recieved " + nodes.size() + " nodes");
63 for (int i = 0; i < nodes.size(); i++) {
64 Register.Instance().getListe().add((Node) nodes.elementAt(i));
66 System.out.println("Adding "
67 + ((Node) nodes.elementAt(i)).getName());
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()
76 .setNbOfNodes(Register.Instance().getSize());
77 new ForwardCount().start();
78 } catch (Exception e) {
79 System.err.println("Error changing Server in SendSurplus : "
84 /** Updating gnodes **/
91 public void setToken() throws RemoteException {
92 System.out.println("I got Token");
94 TokenThread.Instance().setToken();
97 HeartBeatSNode.Instance().getServer().beating(true);
99 System.out.println("Put token to true");
100 } catch (Exception e) {
102 .println("Unable to heartBeat the next SuperNode with the new Token : "
107 public void updateHeart(JaceSuperNodeInterface stub) throws RemoteException {
108 System.out.println("I change to ping a superNode");
110 HeartBeatSNode.Instance().setServer(stub);
113 public synchronized void removeSuperNode(SuperNodeData d)
114 throws RemoteException {
115 SuperNodeListe.Instance().removeSuperNode(d);
118 /****************************************************/
119 /****************************************************/
121 // Register a Daemon in the Register.Instance() of the SuperNode
122 public synchronized void workerRegistering(JaceInterface workerStub,
123 String workerIP, String workerName, int port, GNode g)
124 throws RemoteException {
125 System.out.println("CONNECTION of " + workerName);
128 Node noeud = new Node(workerStub);
129 noeud.setName(workerName);
130 noeud.setIP(workerIP);
131 noeud.setAliveFlag(true);
132 noeud.setAliveTime();
134 noeud.setAppliName(null);
135 noeud.setNbOfBeats(0);
136 noeud.setId( count ) ;
137 noeud.setId( Long.parseLong( workerIP.replace( ".", "" ) ) ) ;
140 g.setId( noeud.getId() ) ;
141 g.setMapped( false ) ;
147 // daemonListChange = true ;
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 ) ;
171 /****************************************************/
172 /****************************************************/
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);
179 noeud.setAliveFlag(true);
180 noeud.setAliveTime();
181 noeud.incrementNbOfBeats();
183 // System.out.println( noeud.getName() + " is pinging me" ) ;
185 // System.out.println( noeud.getName() +
186 // ".................. is not in my list" ) ;
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);
195 // System.out.println( "Super Node is pinging me" ) ;
199 /*********************************/
200 /** Mapping !! Sébastien Miquée **/
201 /*********************************/
204 * Search and return to the requester spawner a register containing
205 * computation nodes plus some extra nodes if needed. This method requires
206 * as parameters the mapping algorithm choice and its parameters, and the
207 * amount of requested nodes and the task to be used, in order to
208 * construct the tasks graph.
209 * @param spawnerIP IP of the requester
210 * @param nbTasks Amount of tasks
211 * @param t Tasks to be executed
212 * @param nbNoeuds Total amount of requested nodes
213 * @param algo Identifier of the mapping algorithm
214 * @param paramAlgo Parameter of the mapping algorithm
216 * @author Sébastien Miquée
219 public Register getRegisterSpawner(String spawnerIP, int nbTasks, Task t,
220 int nbNoeuds, int algo, double paramAlgo) throws RemoteException {
222 // Have we the correct application ?
224 System.err.println( "Problem of class transmission !" ) ;
228 if( t.getDependencies( 0 ) == null )
230 System.err.println( "No redifinition of getDependencies() functions !" ) ;
235 /** Creation of an empty new Register **/
236 Register reg = new Register() ;
240 /** Initialization of Grid architecture (G5K for now) **/
241 Grid grid = Utils.createGridG5k(gnodes);
242 // grid.initClusters();
245 /** Creation of tasks GTask **/
246 ArrayList<GTask> ts = new ArrayList<GTask>();
247 for (int i = 0; i < nbTasks; i++) {
248 ts.add(new GTask( i ) ) ;
251 /** Research of dependencies **/
252 for (int i = 0; i < nbTasks; i++) {
254 dep = t.getDependencies(i);
256 /** Adding dependencies to tasks **/
257 for (int j = 0; j < dep.length; j++) {
259 ts.get(i).addDependance(ts.get(dep[j]));
267 Graph graph = new Graph();
269 for( int i = 0 ; i < ts.size() ; i++)
271 graph.addGTask(ts.get(i));
278 // Thread.sleep(10000) ;
279 // } catch( Exception e ) {}
284 // Thread.sleep( 10000 ) ;
285 // } catch( Exception e ) {}
287 /** Selection of the mapping algorithm **/
292 al = new Simple(graph, grid);
295 al = new QM(graph, grid, paramAlgo);
298 al = new LSM(graph, grid, paramAlgo);
301 al = new DefaultMapping( graph, grid, gnodes ) ;
305 /** Launching the Mapping **/
309 /** Creating the register **/
310 ArrayList<GNode> ag = al.getMapping().getMappedGNodes();
314 for( int i = 0; i < ag.size(); i++ ) {
315 reg.addNode((Node) ag.get(i).getNode());
316 delGNodeFromList( (Node) ag.get(i).getNode(), 2 ) ;
317 // gnodes.remove(ag.get(i));
318 // Register.Instance().removeNode((Node) ag.get(i).getNode());
322 /** Searching extra nodes if any **/
323 if( nbNoeuds > nbTasks )
325 int nb = nbNoeuds - nbTasks ;
328 for( int i = 0 ; i < nb ; i ++ )
330 sup = al.getOtherGNode( gnodes ) ;
331 reg.addNodeBeg( (Node) sup.getNode() ) ;
332 delGNodeFromList( (Node) sup.getNode(), 2 ) ;
338 if (ag.size() != 0) {
339 SuperNodeListe.Instance().forwardCountNode();
344 // System.out.println( "Spawner returned reg: " + reg ) ;
346 /** Mapping distribution over other SuperNodes */
347 SuperNodeListe.Instance().setMapping( al ) ;
351 /** Returning result **/
357 * Suppress a node from the gnodes list if this node is free, or remove
358 * it from the Mapping class of the mapping algorithm if this node is
359 * a computation node. After having done the local suppression of the node
360 * it propagates the suppression to other SuperNodes.
361 * @param _n The fallen node
362 * @param _mode Choice of the list (0: gnodes, 1: mapping, 2:just mapped)
364 * @author Sébastien Miquée
367 public GNode delGNodeFromList( Node _n, int _mode ) throws RemoteException
369 GNode removedGNode = null ;
373 boolean free = false ;
381 /** The dead node is not in the mapping **/
382 if( _mode == 0 || _mode == 2)
384 for( int i = 0 ; i < gnodes.size() ; i++ )
386 if( ((Node)gnodes.get(i).getNode()).getId() == _n.getId() )
388 removedGNode = gnodes.remove( i ) ;
389 Register.Instance().removeNodeOfName( removedGNode.getName() ) ;
395 /** The dead node is in the mapping **/
398 ArrayList<GNode> temp = al.getMapping().getMappedGNodes() ;
400 for( int i = 0 ; i < temp.size() ; i++ )
402 if( ((Node)temp.get(i).getNode()).getId() == _n.getId() )
404 removedGNode = temp.get( i ) ;
405 Register.Instance().removeNodeOfName( removedGNode.getName() ) ;
411 /** Removing the dead node from the Grid **/
412 if( _mode == 0 || _mode == 1 ) {
413 al.getGrid().removeGNode( removedGNode ) ;
416 /** Propagation of the deletion **/
417 SuperNodeListe.Instance().removeGNode( removedGNode, _mode ) ;
426 return removedGNode ;
431 * Determine if this SuperNode can operate on the gnodes list.
432 * It asks other SuperNodes to know if their are working on this list
433 * and set the authorization, or not.
435 * @author Sébastien Miquée
437 private void workingOnGnodes()
449 tmp = tmp && SuperNodeListe.Instance().workingOnGnodes() ;
459 } catch (InterruptedException e) {
469 * Search and return a new node when one failed or when JaceP2P needs another
470 * one. For the first case, the given failed node should not be null, and
471 * null in the other case.
472 * @param _spawnerIP IP of the requester
473 * @param _deadNode The failed node
475 * @author Sébastien Miquée
478 public Node getNewNode( String _spawnerIP, Node _deadNode ) throws RemoteException
481 GNode remp = null, gnode = null ;
483 /** Can we use gnodes ?**/
486 if( _deadNode != null )
490 gnode = delGNodeFromList( _deadNode, 1 ) ;
492 remp = al.replaceNode( gnode, gnodes ) ;
496 System.out.println( "Replacing node found." ) ;
498 remp.setMapped( true ) ;
500 delGNodeFromList( (Node) remp.getNode(), 2 ) ;
504 SuperNodeListe.Instance().forwardCountNode();
506 System.err.println( "Replacing node not found !!" ) ;
509 remp = al.getOtherGNode( gnodes ) ;
513 System.out.println( "Other new node found." ) ;
515 remp.setMapped( true ) ;
519 delGNodeFromList( (Node) remp.getNode(), 2 ) ;
523 SuperNodeListe.Instance().forwardCountNode();
525 System.err.println( "Other new node not found !!" ) ;
530 /** Updating all mapping **/
531 updateMappedNode( gnode, remp ) ;
532 propagateReplaceNode( gnode, remp ) ;
535 /** Free the gnodes use **/
543 * Replace a failed GNode in the mapping by another one.
544 * This method is called by the SuperNodeList, to broadcast the modification.
545 * @param _dead The failed node
546 * @param _remp The replacing node
548 * @author Sébastien Miquée
551 public void updateMappedNode( GNode _dead, GNode _remp )
557 pos = al.getMapping().getIdOfAssociation( _dead ) ;
561 /** Changing the node in the mapping **/
562 al.getMapping().getMapping().get( pos ).setGNode( _remp ) ;
564 System.out.println( "Succesfully replacing the fallen node in the mapping." ) ;
567 /** Changing the status in the grid **/
568 al.getGrid().setMappedStatus( _remp, true ) ;
572 System.err.println( "The new GNode is null!" ) ;
578 * Inform all SuperNodes of the replacement of a failed computing node.
579 * @param _dead The failed node
580 * @param _remp The replacing node
582 * @author Sébastien Miquée
584 private void propagateReplaceNode( GNode _dead, GNode _remp ) throws RemoteException
588 SuperNodeListe.Instance().propagateReplaceNode( _dead, _remp ) ;
592 /**********************************************************/
593 /**********************************************************/
598 * Add a new node in the list.
599 * @param _g The new node
601 * @author Sébastien Miquée
603 public void addGNode( GNode _g ) throws RemoteException
607 boolean free = false ;
624 // public void removeGNode( GNode _g ) throws RemoteException
628 // boolean free = false ;
632 // workingOnGnodes() ;
636 // for( int i = 0 ; i < gnodes.size() ; i++ )
638 // if( ((Node)gnodes.get(i).getNode()).getId() == ((Node)_g.getNode()).getId() )
640 // gnodes.remove( i ) ;
641 // Register.Instance().removeNodeOfName( _g.getName() ) ;
647 // operating = false ;
654 * Set the local mapping with another done on an other SuperNode.
655 * @param _al The new mapping
657 * @author Sébastien Miquée
660 public void setMapping( Algo _al ) throws RemoteException
668 * Allow or deny the use of operations on the gnodes list, in order to
669 * do a mapping operation.
670 * @return The authorization or not to block gnodes
672 * @author Sébastien Miquée
674 public boolean blockForMapping() throws RemoteException
680 } catch (InterruptedException e) {
685 if( operating && ! authorized )
694 * Return the array containing the current not mapped nodes available.
695 * @return The array of available nodes
697 * @author Sébastien Miquée
700 public ArrayList<GNode> getGNodes() throws RemoteException