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.Mapping;
16 import and.Mapping.QM;
17 import and.Mapping.Simple;
18 import and.Mapping.Utils;
21 public class JaceSuperNodeServer extends UnicastRemoteObject implements
22 JaceSuperNodeInterface {
23 private static final long serialVersionUID = 1L;
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 ;
35 public JaceSuperNodeServer(int timeBeat) throws RemoteException {
38 gnodes = new ArrayList<GNode>() ;
39 // daemonListChange = true ;
42 public int getSuperNodeBeat() throws RemoteException {
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();
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 +
58 SuperNodeListe.Instance().viewAll();
61 public void sendSurplus(Vector<?> nodes, ArrayList<GNode> _gnodes) throws RemoteException {
62 System.out.println("Recieved " + nodes.size() + " nodes");
64 for (int i = 0; i < nodes.size(); i++) {
65 Register.Instance().getListe().add((Node) nodes.elementAt(i));
67 System.out.println("Adding "
68 + ((Node) nodes.elementAt(i)).getName());
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()
77 .setNbOfNodes(Register.Instance().getSize());
78 new ForwardCount().start();
79 } catch (Exception e) {
80 System.err.println("Error changing Server in SendSurplus : "
85 /** Updating gnodes **/
92 public void setToken() throws RemoteException {
93 System.out.println("I got Token");
95 TokenThread.Instance().setToken();
98 HeartBeatSNode.Instance().getServer().beating(true);
100 System.out.println("Put token to true");
101 } catch (Exception e) {
103 .println("Unable to heartBeat the next SuperNode with the new Token : "
108 public void updateHeart(JaceSuperNodeInterface stub) throws RemoteException {
109 System.out.println("I change to ping a superNode");
111 HeartBeatSNode.Instance().setServer(stub);
114 public synchronized void removeSuperNode(SuperNodeData d)
115 throws RemoteException {
116 SuperNodeListe.Instance().removeSuperNode(d);
119 /****************************************************/
120 /****************************************************/
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("CONNECTION of " + workerName);
129 Node noeud = new Node(workerStub);
130 noeud.setName(workerName);
131 noeud.setIP(workerIP);
132 noeud.setAliveFlag(true);
133 noeud.setAliveTime();
135 noeud.setAppliName(null);
136 noeud.setNbOfBeats(0);
137 noeud.setId( count ) ;
138 noeud.setId( Long.parseLong( workerIP.replace( ".", "" ) ) ) ;
141 g.setId( noeud.getId() ) ;
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 public Register getRegisterSpawner(String spawnerIP, int nbTasks, Task t,
205 int nbNoeuds, int algo, double paramAlgo) throws RemoteException {
207 // Have we the correct application ?
209 System.err.println( "Problem of class transmission !" ) ;
213 if( t.getDependencies( 0 ) == null )
215 System.err.println( "No redifinition of getDependencies() functions !" ) ;
220 /** Creation of an empty new Register **/
221 Register reg = new Register() ;
225 /** Initialization of Grid architecture (G5K for now) **/
226 Grid grid = Utils.createGridG5k(gnodes);
230 /** Creation of tasks GTask **/
231 ArrayList<GTask> ts = new ArrayList<GTask>();
232 for (int i = 0; i < nbTasks; i++) {
233 ts.add(new GTask( i ) ) ;
236 /** Research of dependencies **/
237 for (int i = 0; i < nbTasks; i++) {
239 dep = t.getDependencies(i);
241 /** Adding dependencies to tasks **/
242 for (int j = 0; j < dep.length; j++) {
244 ts.get(i).addDependance(ts.get(dep[j]));
252 Graph graph = new Graph();
254 for( int i = 0 ; i < ts.size() ; i++)
256 graph.addGTask(ts.get(i));
263 // Thread.sleep(10000) ;
264 // } catch( Exception e ) {}
269 // Thread.sleep( 10000 ) ;
270 // } catch( Exception e ) {}
272 /** Selection of the mapping algorithm **/
277 al = new Simple(graph, grid);
280 al = new QM(graph, grid, paramAlgo);
283 al = new LSM(graph, grid, paramAlgo);
286 al = new DefaultMapping( graph, grid, gnodes ) ;
290 /** Launching the Mapping **/
294 /** Transforming mapping in register **/
295 Mapping mp = al.getMapping();
298 /** Creating the register **/
299 ArrayList<GNode> ag = mp.getMappedGNodes();
303 for( int i = 0; i < ag.size(); i++ ) {
304 reg.addNode((Node) ag.get(i).getNode());
305 delGNodeFromList( (Node) ag.get(i).getNode(), 0 ) ;
306 // gnodes.remove(ag.get(i));
307 // Register.Instance().removeNode((Node) ag.get(i).getNode());
311 /** Searching extra nodes if any **/
312 if( nbNoeuds > nbTasks )
314 int nb = nbNoeuds - nbTasks ;
317 for( int i = 0 ; i < nb ; i ++ )
319 sup = al.getOtherGNode( gnodes ) ;
320 reg.addNodeBeg( (Node) sup.getNode() ) ;
321 delGNodeFromList( (Node) sup.getNode(), 0 ) ;
327 if (ag.size() != 0) {
328 SuperNodeListe.Instance().forwardCountNode();
333 // System.out.println( "Spawner returned reg: " + reg ) ;
335 /** Mapping distribution over other Super Nodes */
336 SuperNodeListe.Instance().setMapping( al ) ;
340 /** Returning result **/
346 public GNode delGNodeFromList( Node _n, int _mode ) throws RemoteException
348 GNode removedGNode = null ;
352 boolean free = false ;
360 /** The dead node is not in the mapping **/
363 for( int i = 0 ; i < gnodes.size() ; i++ )
365 if( ((Node)gnodes.get(i).getNode()).getId() == _n.getId() )
367 removedGNode = gnodes.remove( i ) ;
368 Register.Instance().removeNodeOfName( removedGNode.getName() ) ;
374 /** The dead node is in the mapping **/
377 ArrayList<GNode> temp = al.getMapping().getMappedGNodes() ;
379 for( int i = 0 ; i < temp.size() ; i++ )
381 if( ((Node)temp.get(i).getNode()).getId() == _n.getId() )
383 removedGNode = temp.get( i ) ;
384 Register.Instance().removeNodeOfName( removedGNode.getName() ) ;
390 /** Removing the dead node from the Grid **/
391 al.getGrid().removeGNode( removedGNode ) ;
393 SuperNodeListe.Instance().removeGNode( removedGNode, _mode ) ;
402 return removedGNode ;
406 private void workingOnGnodes()
418 tmp = tmp && SuperNodeListe.Instance().workingOnGnodes() ;
428 } catch (InterruptedException e) {
438 public Node getNewNode( String _spawnerIP, Node _deadNode ) throws RemoteException
441 GNode remp = null, gnode = null ;
443 /** Can we use gnodes ?**/
446 if( _deadNode != null )
450 gnode = delGNodeFromList( _deadNode, 1 ) ;
452 remp = al.replaceNode( gnode, gnodes ) ;
456 System.out.println( "Replacing node found." ) ;
457 node = (Node) remp.getNode() ;
459 delGNodeFromList( node, 0 ) ;
461 /** Updating all mapping **/
462 updateMappedNode( gnode, remp ) ;
463 propagateReplaceNode( gnode, remp ) ;
467 SuperNodeListe.Instance().forwardCountNode();
469 System.err.println( "Replacing node not found !!" ) ;
472 remp = al.getOtherGNode( gnodes ) ;
476 System.out.println( "Other new node found." ) ;
477 node = (Node) remp.getNode() ;
481 delGNodeFromList( node, 0 ) ;
485 SuperNodeListe.Instance().forwardCountNode();
487 System.err.println( "Other new node not found !!" ) ;
491 /** Free the gnodes use **/
498 public void updateMappedNode( GNode _dead, GNode _remp )
501 pos = al.getMapping().getIdOfAssociation( _dead ) ;
505 System.err.println( "GNode "+_dead+" does not exist in the mapping!" ) ;
509 al.getMapping().getMapping().get( pos ).setGNode( _remp ) ;
511 System.out.println( "Succesfully replacing the fallen node in the mapping." ) ;
516 * Inform all SuperNodes of the replacement of a fallen computing node.
517 * @param _dead The fallen node
518 * @param _remp The replacing node
520 private void propagateReplaceNode( GNode _dead, GNode _remp ) throws RemoteException
522 if( _dead != null && _remp != null )
524 SuperNodeListe.Instance().propagateReplaceNode( _dead, _remp ) ;
528 /**********************************************************/
529 /**********************************************************/
533 * Add a new node in the list.
534 * @param _g The new node
536 public void addGNode( GNode _g ) throws RemoteException
540 boolean free = false ;
557 // public void removeGNode( GNode _g ) throws RemoteException
561 // boolean free = false ;
565 // workingOnGnodes() ;
569 // for( int i = 0 ; i < gnodes.size() ; i++ )
571 // if( ((Node)gnodes.get(i).getNode()).getId() == ((Node)_g.getNode()).getId() )
573 // gnodes.remove( i ) ;
574 // Register.Instance().removeNodeOfName( _g.getName() ) ;
580 // operating = false ;
587 public void setMapping( Algo _al ) throws RemoteException
595 * Allow or deny the use of operations on the gnodes list, in order to
596 * do a mapping operation.
598 * @return The authorization or not to block gnodes
600 public boolean blockForMapping() throws RemoteException
606 } catch (InterruptedException e) {
611 if( operating && ! authorized )
620 public ArrayList<GNode> getGNodes() throws RemoteException