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) 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.out.println("Error changing Server in SendSurplus : "
86 public void setToken() throws RemoteException {
87 System.out.println("I got Token");
89 TokenThread.Instance().setToken();
92 HeartBeatSNode.Instance().getServer().beating(true);
94 System.out.println("Put token to true");
95 } catch (Exception e) {
97 .println("Unable to heartBeat the next SuperNode with the new Token : "
102 public void updateHeart(JaceSuperNodeInterface stub) throws RemoteException {
103 System.out.println("I change to ping a superNode");
105 HeartBeatSNode.Instance().setServer(stub);
108 public synchronized void removeSuperNode(SuperNodeData d)
109 throws RemoteException {
110 SuperNodeListe.Instance().removeSuperNode(d);
113 /****************************************************/
114 /****************************************************/
116 // Register a Daemon in the Register.Instance() of the SuperNode
117 public synchronized void workerRegistering(JaceInterface workerStub,
118 String workerIP, String workerName, int port, GNode g)
119 throws RemoteException {
120 System.out.println("CONNEXION of " + workerName);
123 Node noeud = new Node(workerStub);
124 noeud.setName(workerName);
125 noeud.setIP(workerIP);
126 noeud.setAliveFlag(true);
127 noeud.setAliveTime();
129 noeud.setAppliName(null);
130 noeud.setNbOfBeats(0);
131 noeud.setId( count ) ;
132 noeud.setId( Long.parseLong( workerIP.replace( ".", "" ) ) ) ;
135 g.setId( noeud.getId() ) ;
141 // daemonListChange = true ;
143 // Insert the node in the Register.Instance() of the Super Node
144 Register.Instance().addNode(noeud);
146 // Register.Instance().viewAll() ;
147 // SuperNodeListe.Instance().viewAll() ;
149 // Inform the other superNode and tell them the nb of Daemon I have
151 int index = SuperNodeListe.Instance().existSuperNode(
152 LocalHost.Instance().getIP());
154 ((SuperNodeData) SuperNodeListe.Instance().getListe().get(index))
155 .setNbOfNodes(Register.Instance().getSize());
156 SuperNodeListe.Instance().forwardCountNode();
158 SuperNodeListe.Instance().addGNode( g ) ;
163 /****************************************************/
164 /****************************************************/
166 // HeartBeat that detects if a Daemon is dead or alive
167 public void beating(JaceInterface stub) throws RemoteException {
168 Node noeud = Register.Instance().getNodeOfStub(stub);
171 noeud.setAliveFlag(true);
172 noeud.setAliveTime();
173 noeud.incrementNbOfBeats();
175 // System.out.println( noeud.getName() + " is pinging me" ) ;
177 // System.out.println( noeud.getName() +
178 // ".................. is not in my list" ) ;
182 // HeartBeat that detects if a Super Node is dead or alive
183 public void beating(boolean token) throws RemoteException {
184 ScanThreadSuperNode.Instance().setAliveTime();
185 ScanThreadSuperNode.Instance().setToken(token);
187 // System.out.println( "Super Node is pinging me" ) ;
191 /*********************************/
192 /** Mapping !! Sébastien Miquée **/
193 /*********************************/
195 public Register getRegisterSpawner(String spawnerIP, int nbTasks, Task t,
196 int nbNoeuds, int algo, double paramAlgo) throws RemoteException {
198 // Have we the correct application ?
200 System.err.println( "Problem of class transmission !" ) ;
204 if( t.getDependencies( 0 ) == null )
206 System.err.println( "No redifinition of getDependencies() functions !" ) ;
211 /** Creation of an empty new Register **/
212 Register reg = new Register() ;
216 /** Initialization of Grid architecture (G5K for now) **/
217 Grid grid = Utils.createGridG5k(gnodes);
221 /** Creation of tasks GTask **/
222 ArrayList<GTask> ts = new ArrayList<GTask>();
223 for (int i = 0; i < nbTasks; i++) {
224 ts.add(new GTask( i ) ) ;
227 /** Research of dependencies **/
228 for (int i = 0; i < nbTasks; i++) {
230 dep = t.getDependencies(i);
232 /** Adding dependencies to tasks **/
233 for (int j = 0; j < dep.length; j++) {
235 ts.get(i).addDependance(ts.get(dep[j]));
243 Graph graph = new Graph();
245 for( int i = 0 ; i < ts.size() ; i++)
247 graph.addGTask(ts.get(i));
254 // Thread.sleep(10000) ;
255 // } catch( Exception e ) {}
260 // Thread.sleep( 10000 ) ;
261 // } catch( Exception e ) {}
263 /** Selection of the mapping algorithm **/
268 al = new Simple(graph, grid);
271 al = new QM(graph, grid, paramAlgo);
274 al = new LSM(graph, grid, paramAlgo);
277 al = new DefaultMapping( graph, grid, gnodes ) ;
281 /** Launching the Mapping **/
285 /** Transforming mapping in register **/
286 Mapping mp = al.getMapping();
289 /** Creating the register **/
290 ArrayList<GNode> ag = mp.getMappedGNodes();
294 for (int i = 0; i < ag.size(); i++) {
295 reg.addNode((Node) ag.get(i).getNode());
296 delGNodeFromList( (Node) ag.get(i).getNode() ) ;
297 // gnodes.remove(ag.get(i));
298 // Register.Instance().removeNode((Node) ag.get(i).getNode());
302 /** Searching extra nodes if any **/
303 if( nbNoeuds > nbTasks )
305 int nb = nbNoeuds - nbTasks ;
308 for( int i = 0 ; i < nb ; i ++ )
310 sup = al.getOtherGNode( gnodes ) ;
311 reg.addNodeBeg( (Node) sup.getNode() ) ;
312 delGNodeFromList( (Node) sup.getNode() ) ;
318 if (ag.size() != 0) {
319 SuperNodeListe.Instance().forwardCountNode();
324 // daemonListChange = false ;
326 System.out.println( "Spawner returned reg: " + reg ) ;
328 /** Mapping distribution over other Super Nodes */
329 SuperNodeListe.Instance().setMapping( al ) ;
333 /* Returning result */
337 /*****************************************/
338 /**** Sébastien Miquée ****/
340 /** Recherche nouveau noeud **/
341 /*****************************************/
343 protected GNode delGNodeFromList( Node _n )
345 GNode removedGNode = null ;
349 boolean free = false ;
357 for( int i = 0 ; i < gnodes.size() ; i++ )
359 if( ((Node)gnodes.get(i).getNode()).getId() == _n.getId() )
361 removedGNode = gnodes.remove( i ) ;
362 Register.Instance().removeNodeOfName( removedGNode.getName() ) ;
367 // daemonListChange = true ;
369 SuperNodeListe.Instance().removeGNode( removedGNode ) ;
378 return removedGNode ;
382 private void workingOnGnodes()
394 tmp = tmp && SuperNodeListe.Instance().workingOnGnodes() ;
404 } catch (InterruptedException e) {
414 public Node getNewNode( String _spawnerIP, Node _deadNode ) throws RemoteException
417 GNode remp = null, gnode = null ;
419 /** Can we use gnodes ?**/
422 if( _deadNode != null )
424 gnode = delGNodeFromList( _deadNode ) ;
426 remp = al.replaceNode( gnode, gnodes ) ;
430 System.out.println( "Replacing node found." ) ;
431 node = (Node) remp.getNode() ;
435 delGNodeFromList( node ) ;
437 /** Updating all mapping **/
438 updateMappedNode( gnode, remp ) ;
439 propagateReplaceNode( gnode, remp ) ;
443 SuperNodeListe.Instance().forwardCountNode();
445 System.err.println( "Replacing node not found !!" ) ;
448 remp = al.getOtherGNode( gnodes ) ;
452 System.out.println( "Other new node found." ) ;
453 node = (Node) remp.getNode() ;
457 delGNodeFromList( node ) ;
461 SuperNodeListe.Instance().forwardCountNode();
463 System.err.println( "Other new node not found !!" ) ;
467 /** Free the gnodes use **/
474 public void updateMappedNode( GNode _dead, GNode _remp )
477 pos = al.getMapping().getIdOfAssociation( _dead ) ;
481 System.err.println( "GNode "+_dead+" does not exist in the mapping!" ) ;
485 al.getMapping().getMapping().get( pos ).setGNode( _remp ) ;
487 System.out.println( "Succesfully replacing the fallen node in the mapping." ) ;
492 * Inform all SuperNodes of the replacement of a fallen computing node.
493 * @param _dead The fallen node
494 * @param _remp The replacing node
496 private void propagateReplaceNode( GNode _dead, GNode _remp )
498 if( _dead != null && _remp != null )
500 SuperNodeListe.Instance().propagateReplaceNode( _dead, _remp ) ;
504 /**********************************************************/
505 /**********************************************************/
509 * Add a new node in the list.
510 * @param _g The new node
512 public void addGNode( GNode _g ) throws RemoteException
526 public void removeGNode( GNode _g ) throws RemoteException
530 boolean free = false ;
538 for( int i = 0 ; i < gnodes.size() ; i++ )
540 if( ((Node)gnodes.get(i).getNode()).getId() == ((Node)_g.getNode()).getId() )
543 Register.Instance().removeNodeOfName( _g.getName() ) ;
556 public void setMapping( Algo _al ) throws RemoteException
564 * Allow or deny the use of operations on the gnodes list, in order to
565 * do a mapping operation.
567 * @return The authorization or not to block gnodes
569 public boolean blockForMapping() throws RemoteException
575 } catch (InterruptedException e) {
580 if( operating && ! authorized )