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.out.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("CONNEXION 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 /*********************************/
203 public Register getRegisterSpawner(String spawnerIP, int nbTasks, Task t,
204 int nbNoeuds, int algo, double paramAlgo) throws RemoteException {
206 // Have we the correct application ?
208 System.err.println( "Problem of class transmission !" ) ;
212 if( t.getDependencies( 0 ) == null )
214 System.err.println( "No redifinition of getDependencies() functions !" ) ;
219 /** Creation of an empty new Register **/
220 Register reg = new Register() ;
224 /** Initialization of Grid architecture (G5K for now) **/
225 Grid grid = Utils.createGridG5k(gnodes);
229 /** Creation of tasks GTask **/
230 ArrayList<GTask> ts = new ArrayList<GTask>();
231 for (int i = 0; i < nbTasks; i++) {
232 ts.add(new GTask( i ) ) ;
235 /** Research of dependencies **/
236 for (int i = 0; i < nbTasks; i++) {
238 dep = t.getDependencies(i);
240 /** Adding dependencies to tasks **/
241 for (int j = 0; j < dep.length; j++) {
243 ts.get(i).addDependance(ts.get(dep[j]));
251 Graph graph = new Graph();
253 for( int i = 0 ; i < ts.size() ; i++)
255 graph.addGTask(ts.get(i));
262 // Thread.sleep(10000) ;
263 // } catch( Exception e ) {}
268 // Thread.sleep( 10000 ) ;
269 // } catch( Exception e ) {}
271 /** Selection of the mapping algorithm **/
276 al = new Simple(graph, grid);
279 al = new QM(graph, grid, paramAlgo);
282 al = new LSM(graph, grid, paramAlgo);
285 al = new DefaultMapping( graph, grid, gnodes ) ;
289 /** Launching the Mapping **/
293 /** Transforming mapping in register **/
294 Mapping mp = al.getMapping();
297 /** Creating the register **/
298 ArrayList<GNode> ag = mp.getMappedGNodes();
302 for (int i = 0; i < ag.size(); i++) {
303 reg.addNode((Node) ag.get(i).getNode());
304 delGNodeFromList( (Node) ag.get(i).getNode(), 0 ) ;
305 // gnodes.remove(ag.get(i));
306 // Register.Instance().removeNode((Node) ag.get(i).getNode());
310 /** Searching extra nodes if any **/
311 if( nbNoeuds > nbTasks )
313 int nb = nbNoeuds - nbTasks ;
316 for( int i = 0 ; i < nb ; i ++ )
318 sup = al.getOtherGNode( gnodes ) ;
319 reg.addNodeBeg( (Node) sup.getNode() ) ;
320 delGNodeFromList( (Node) sup.getNode(), 0 ) ;
326 if (ag.size() != 0) {
327 SuperNodeListe.Instance().forwardCountNode();
332 // daemonListChange = false ;
334 System.out.println( "Spawner returned reg: " + reg ) ;
336 /** Mapping distribution over other Super Nodes */
337 SuperNodeListe.Instance().setMapping( al ) ;
341 /* Returning result */
345 /*****************************************/
346 /**** Sébastien Miquée ****/
348 /** Recherche nouveau noeud **/
349 /*****************************************/
351 protected GNode delGNodeFromList( Node _n, int _mode )
353 GNode removedGNode = null ;
357 boolean free = false ;
365 /** The dead node is not in the mapping **/
368 for( int i = 0 ; i < gnodes.size() ; i++ )
370 if( ((Node)gnodes.get(i).getNode()).getId() == _n.getId() )
372 removedGNode = gnodes.remove( i ) ;
373 Register.Instance().removeNodeOfName( removedGNode.getName() ) ;
379 /** The dead node is in the mapping **/
382 ArrayList<GNode> temp = al.getMapping().getMappedGNodes() ;
384 for( int i = 0 ; i < temp.size() ; i++ )
386 if( ((Node)temp.get(i).getNode()).getId() == _n.getId() )
388 removedGNode = temp.get( i ) ;
389 Register.Instance().removeNodeOfName( removedGNode.getName() ) ;
396 // daemonListChange = true ;
398 SuperNodeListe.Instance().removeGNode( removedGNode ) ;
407 return removedGNode ;
411 private void workingOnGnodes()
423 tmp = tmp && SuperNodeListe.Instance().workingOnGnodes() ;
433 } catch (InterruptedException e) {
443 public Node getNewNode( String _spawnerIP, Node _deadNode ) throws RemoteException
446 GNode remp = null, gnode = null ;
448 /** Can we use gnodes ?**/
451 if( _deadNode != null )
455 gnode = delGNodeFromList( _deadNode, 1 ) ;
457 remp = al.replaceNode( gnode, gnodes ) ;
461 System.out.println( "Replacing node found." ) ;
462 node = (Node) remp.getNode() ;
464 delGNodeFromList( node, 0 ) ;
466 /** Updating all mapping **/
467 updateMappedNode( gnode, remp ) ;
468 propagateReplaceNode( gnode, remp ) ;
472 SuperNodeListe.Instance().forwardCountNode();
474 System.err.println( "Replacing node not found !!" ) ;
477 remp = al.getOtherGNode( gnodes ) ;
481 System.out.println( "Other new node found." ) ;
482 node = (Node) remp.getNode() ;
486 delGNodeFromList( node, 0 ) ;
490 SuperNodeListe.Instance().forwardCountNode();
492 System.err.println( "Other new node not found !!" ) ;
496 /** Free the gnodes use **/
503 public void updateMappedNode( GNode _dead, GNode _remp )
506 pos = al.getMapping().getIdOfAssociation( _dead ) ;
510 System.err.println( "GNode "+_dead+" does not exist in the mapping!" ) ;
514 al.getMapping().getMapping().get( pos ).setGNode( _remp ) ;
516 System.out.println( "Succesfully replacing the fallen node in the mapping." ) ;
521 * Inform all SuperNodes of the replacement of a fallen computing node.
522 * @param _dead The fallen node
523 * @param _remp The replacing node
525 private void propagateReplaceNode( GNode _dead, GNode _remp ) throws RemoteException
527 if( _dead != null && _remp != null )
529 SuperNodeListe.Instance().propagateReplaceNode( _dead, _remp ) ;
533 /**********************************************************/
534 /**********************************************************/
538 * Add a new node in the list.
539 * @param _g The new node
541 public void addGNode( GNode _g ) throws RemoteException
545 boolean free = false ;
562 public void removeGNode( GNode _g ) throws RemoteException
566 boolean free = false ;
574 for( int i = 0 ; i < gnodes.size() ; i++ )
576 if( ((Node)gnodes.get(i).getNode()).getId() == ((Node)_g.getNode()).getId() )
579 Register.Instance().removeNodeOfName( _g.getName() ) ;
592 public void setMapping( Algo _al ) throws RemoteException
600 * Allow or deny the use of operations on the gnodes list, in order to
601 * do a mapping operation.
603 * @return The authorization or not to block gnodes
605 public boolean blockForMapping() throws RemoteException
611 } catch (InterruptedException e) {
616 if( operating && ! authorized )
625 public ArrayList<GNode> getGNodes() throws RemoteException