X-Git-Url: http://bilbo.iut-bm.univ-fcomte.fr/pub/gitweb/hpcvm.git/blobdiff_plain/d83793da6e881ebab006a50a47fbba914e65f9cf..f74ee61df3fbc8cf8371347069a715270a3e8a72:/src/and/hpcvm/Server.java diff --git a/src/and/hpcvm/Server.java b/src/and/hpcvm/Server.java index 0c696a4..2bc7ab8 100644 --- a/src/and/hpcvm/Server.java +++ b/src/and/hpcvm/Server.java @@ -11,6 +11,7 @@ import java.rmi.registry.Registry; import java.rmi.server.UnicastRemoteObject; import java.util.ArrayList; import java.util.Iterator; +import java.util.concurrent.Semaphore; public class Server extends UnicastRemoteObject implements ServicesServer @@ -31,119 +32,7 @@ public class Server extends UnicastRemoteObject implements ServicesServer } protected int getNb() { return nb ; } - } - - - private class ConnectedClient - { - private ServicesClient stub ; - private int timeout ; - private Status state ; - private String ip ; - private String name ; - private ComputingClient cl ; - - ConnectedClient( ServicesClient _stub ) - { - stub = _stub ; - timeout = 0 ; - state = new Status() ; - state.setStatus( "connected" ) ; - try { - ip = stub.getIPHost() ; - name = stub.getName() ; - } catch (RemoteException e) { - e.printStackTrace(); - } - cl = null ; - } - - protected ServicesClient getStub() { return stub ; } - - protected void setStub( ServicesClient _stub ) { stub = _stub ; } - - protected int getTimeout() { return timeout ; } - - protected void incTimeout() { timeout++ ; } - - protected void resetTimeout() { timeout = 0 ; } - - protected String getStatus() { return state.getStatus() ; } - - protected void setStatus( String _state ) { state.setStatus( _state ) ; } - - protected String getIP() { return ip ; } - - protected String getName() { return name ; } ; - - protected void setComputingClient( ComputingClient _cl ) { cl = _cl ; } - - protected ComputingClient getComputingClient() { return cl ; } - } - - - private class ComputingClient - { - private ConnectedClient client ; - private boolean save_status ; - private ArrayList save_neighbor ; - private String lastSaveName ; - - ComputingClient( ConnectedClient cl ) - { - client = cl ; - save_status = false ; - save_neighbor = new ArrayList() ; - lastSaveName = "none" ; - } - - protected ConnectedClient getClient() { return client ; } - - protected boolean getSaveStatus(){ return save_status ; } - - protected void setSaveStatus( boolean _status ) { save_status = _status ; } - - protected void setSaveNeighbor( ServicesClient _sn ) - { - if( _sn != null ) - { - if( save_neighbor.size() == 0 ) - { - save_neighbor.add( _sn ) ; - } else { - save_neighbor.set( 0, _sn ) ; - } - -// System.out.println( "My save neighbor is " + _sn ) ; - - try { - client.getStub().setSavingNeighbor( _sn ) ; - } catch( RemoteException e ) { - System.err.println( "Error while setting save neighbor on " + - client.getName() + "(" + client.getIP() + ")!" ) ; - e.printStackTrace() ; - } - } - } - - protected ServicesClient getSaveNeighbor() - { - if( save_neighbor.isEmpty() ) - { - return null ; - } else { - return save_neighbor.get( 0 ) ; - } - } - - public void setLastSave( String _saveName ) - { - lastSaveName = _saveName ; - } - - public String getLastSave() { return lastSaveName ; } - - } + } private class IPAssociation @@ -153,8 +42,8 @@ public class Server extends UnicastRemoteObject implements ServicesServer IPAssociation() { - vmIP = null ; - hostIP = null ; + vmIP = "" ; + hostIP = "" ; } protected void setVmIP( String _vmIP ) @@ -184,11 +73,14 @@ public class Server extends UnicastRemoteObject implements ServicesServer private int port ; private ArrayList clients ; private ArrayList computingClients ; + private ArrayList applications ; private int max_timeout ; private ConnectedMonitor monitor ; private DiscCount counter ; private ArrayList vmIPs ; private String working_directory ; + private long save_interleave ; + private Semaphore semaSave ; protected Server() throws RemoteException @@ -259,7 +151,7 @@ public class Server extends UnicastRemoteObject implements ServicesServer { for( int i = 0 ; i < vmIPs.size() ; i++ ) { - if( vmIPs.get( i ).getHostIP() == null ) + if( vmIPs.get( i ).getHostIP().equalsIgnoreCase( "" ) ) { vmIPs.get( i ).setHostIP( _ip ) ; @@ -310,12 +202,17 @@ public class Server extends UnicastRemoteObject implements ServicesServer port = _port ; max_timeout = 4 ; - clients = new ArrayList() ; - computingClients = new ArrayList() ; + clients = new ArrayList() ; + computingClients = new ArrayList() ; + applications = new ArrayList() ; monitor = null ; working_directory = "/localhome/vmware" ; + save_interleave = 30 * 60 * 1000 ; + + semaSave = new Semaphore( 1 ) ; + exportObject() ; vmIPs = new ArrayList() ; @@ -326,12 +223,15 @@ public class Server extends UnicastRemoteObject implements ServicesServer vmIPs.get( vmIPs.size() - 1 ).setVmIP( "10.11.10." + i ) ; } - clients = new ArrayList() ; + clients = new ArrayList() ; counter = new DiscCount() ; monitor = new ConnectedMonitor() ; monitor.start() ; + + // TODO + // Check if there are running applications ... and restart them :) } @@ -342,6 +242,7 @@ public class Server extends UnicastRemoteObject implements ServicesServer for( int i = 0 ; i < clients.size() ; i++ ) { try { + clients.get( i ).getStub().emergencyStop() ; clients.get( i ).getStub().stop() ; } catch (RemoteException e) { e.printStackTrace(); @@ -428,6 +329,15 @@ public class Server extends UnicastRemoteObject implements ServicesServer ComputingClient cc = cl.getComputingClient() ; // ServicesClient dead = cc.getClient().getStub() ; String ipDead = cc.getClient().getIP() ; + SaveNeighbor snDead = null ; + for( int i = 0 ; i < computingClients.size() ; i++ ) + { + if( computingClients.get( i ).getSaveNeighbor().getIPHost().equalsIgnoreCase( ipDead ) ) + { + snDead = computingClients.get( i ).getSaveNeighbor() ; + break ; + } + } boolean ok = false ; @@ -458,7 +368,7 @@ public class Server extends UnicastRemoteObject implements ServicesServer ComputingClient ccl = new ComputingClient( clients.get(i) ) ; clients.get( i ).setComputingClient( ccl ) ; - ServicesClient sn = computingClients.get( pos ).getSaveNeighbor() ; + SaveNeighbor sn = computingClients.get( pos ).getSaveNeighbor() ; ccl.setSaveNeighbor( sn ) ; computingClients.set( pos, ccl ) ; @@ -475,24 +385,45 @@ public class Server extends UnicastRemoteObject implements ServicesServer if( res == 0 ) { ok = true ; + + boolean ok_new = false, ok_old = false ; - // replace dead client in vmIPs + // replace dead client in vmIPs for( int j = 0 ; j < vmIPs.size() ; j++ ) { + if( vmIPs.get( j ).getHostIP().equalsIgnoreCase( computingClients.get( pos ).getClient().getIP() ) ) + { + vmIPs.get( j ).setHostIP( "" ) ; + ok_new = true ; + } if( vmIPs.get( j ).getHostIP().equalsIgnoreCase( ipDead ) ) { String vmIP = vmIPs.get( j ).getVmIP() ; vmIPs.get( j ).setHostIP( computingClients.get( pos ).getClient().getIP() ) ; - + ok_old = true ; + try { computingClients.get( pos ).getClient().getStub().setIPVM( vmIP ) ; } catch( RemoteException e ) { System.err.println( "Unable to set the new VM IP on the replacing client!" ) ; e.printStackTrace() ; } - break ; + + if( ok_new && ok_old ) + { + break ; + } } } + + // Replacing in RunningApplication + applications.get( 0 ).replaceComputingClient( cc, ccl ) ; + + for( int l = 0 ; l < applications.get(0).getComputingClients().size() ; l++ ) + { + applications.get(0).getComputingClients().get( l ).setSaveRequest( false ) ; + } + System.out.println( "Successful redeployment of the VM." ) ; } else { @@ -510,7 +441,7 @@ public class Server extends UnicastRemoteObject implements ServicesServer { try { computingClients.get( k ).getClient().getStub(). - replaceSavingNeighbor( ipDead, clients.get( i ).getStub() ) ; + replaceSaveNeighbor( snDead, new SaveNeighbor( clients.get( i ).getStub() ) ) ; } catch( RemoteException e ) { System.err.println( "Unable to inform " + computingClients.get( k ).getClient().getName() + " of the replacement of a save neighbor!" ) ; @@ -649,9 +580,11 @@ public class Server extends UnicastRemoteObject implements ServicesServer } } - for( int i = 0 ; i < computingClients.size() ; i++ ) + for( int i = 0 ; i < applications.get(0).getComputingClients().size() ; i++ ) { - final ServicesClient sc = computingClients.get( i ).getClient().getStub() ; + final ServicesClient sc = applications.get(0).getComputingClients().get( i ).getClient().getStub() ; + + applications.get( 0 ).getComputingClients().get( i ).setRestartOk( false ) ; new Thread( new Runnable() { @@ -713,6 +646,8 @@ public class Server extends UnicastRemoteObject implements ServicesServer } computingClients.get( i ).setSaveStatus( false ) ; } + + applications.get( 0 ).setLastSaveDate( System.currentTimeMillis() ) ; } return 0 ; @@ -745,7 +680,9 @@ public class Server extends UnicastRemoteObject implements ServicesServer if( nb > _nb ) { ArrayList ac = new ArrayList() ; - ArrayList tmp = new ArrayList() ; + ArrayList tmp = new ArrayList() ; + + RunningApplication app = new RunningApplication( "Test" ) ; int i = 0 ; @@ -779,6 +716,10 @@ public class Server extends UnicastRemoteObject implements ServicesServer if( ac.size() == _nb ) { + app.setComputingClients( tmp ) ; + app.setRunning( true ) ; + app.setStartTime( System.currentTimeMillis() ) ; + int index, index2 ; /* Choosing save neighbors */ for( i = 0 ; i < tmp.size() ; i++ ) @@ -792,7 +733,12 @@ public class Server extends UnicastRemoteObject implements ServicesServer { System.err.println( "Problem in ComputingClients list!" ) ; } else { - computingClients.get( index ).setSaveNeighbor( computingClients.get( index2 ).getClient().getStub() ) ; + try { + computingClients.get( index ).setSaveNeighbor( new SaveNeighbor( computingClients.get( index2 ).getClient().getStub() )) ; + } catch( RemoteException e ) { + System.err.println( "Unable to create the save neighbor!" ) ; + e.printStackTrace() ; + } } } else { index = computingClients.indexOf( tmp.get( i ) ) ; @@ -802,23 +748,183 @@ public class Server extends UnicastRemoteObject implements ServicesServer { System.err.println( "Problem in ComputingClients list!" ) ; } else { - computingClients.get( index ).setSaveNeighbor( computingClients.get( index2 ).getClient().getStub() ) ; + try { + computingClients.get( index ).setSaveNeighbor( new SaveNeighbor( computingClients.get( index2 ).getClient().getStub() ) ) ; + } catch( RemoteException e ) { + System.err.println( "Unable to create the save neighbor!" ) ; + e.printStackTrace() ; + } } } } } - /* Cleaning */ - tmp.clear() ; - tmp = null ; + applications.add( app ) ; return ac ; } return null ; } + + + @Override + public void requestSave( String _ip ) + { + try { + semaSave.acquire() ; + } catch( InterruptedException e ) { + System.err.println( "Unable to obtain the semaphore for semaSave!" ) ; + e.printStackTrace() ; + } + + final String ip = _ip ; + + new Thread( new Runnable() { + + @Override + public void run() + { + treatRequestSave( ip ) ; + } + } ).start() ; + } - + + public void treatRequestSave( String _ip ) + { + if( applications.size() > 0 && _ip != null && _ip.length() > 0 ) + { + if( (System.currentTimeMillis() - applications.get( 0 ).getLastSaveDate()) > save_interleave ) + { + // Mark it as a requester + for( int i = 0 ; i < applications.get( 0 ).getComputingClients().size() ; i++ ) + { + if( applications.get( 0 ).getComputingClients().get( i ).getClient().getIP().equalsIgnoreCase( _ip ) ) + { + applications.get( 0 ).getComputingClients().get( i ).setSaveRequest( true ) ; + + break ; + } + } + + semaSave.release() ; + + // Is every body ok? + boolean ok = false ; + for( int i = 0 ; i < applications.get( 0 ).getComputingClients().size() ; i++ ) + { + if( i == 0 ) + { + ok = applications.get( 0 ).getComputingClients().get( i ).getSaveRequest() ; + } else { + ok = ok & applications.get( 0 ).getComputingClients().get( i ).getSaveRequest() ; + } + + if( ! ok ) + { + break ; + } + } + + if( ok ) + { +// try { +// Thread.sleep( 5000 ) ; +// } catch( InterruptedException e1 ) { +// e1.printStackTrace() ; +// } + + for( int i = 0 ; i < applications.get( 0 ).getComputingClients().size() ; i++ ) + { + try { + applications.get( 0 ).getComputingClients().get( i ).getClient().getStub().responseSave( true ) ; + applications.get( 0 ).getComputingClients().get( i ).setSaveRequest( false ) ; + } catch( RemoteException e ) { + System.err.println( "Unable to send the save request response to " + + applications.get( 0 ).getComputingClients().get( i ).getClient().getName() + "!" ) ; + e.printStackTrace() ; + } + } + + applications.get( 0 ).setLastSaveDate( System.currentTimeMillis() ) ; + } + + } else { + semaSave.release() ; + + for( int i = 0 ; i < applications.get( 0 ).getComputingClients().size() ; i++ ) + { + if( applications.get( 0 ).getComputingClients().get( i ).getClient().getIP().equalsIgnoreCase( _ip ) ) + { + try { + applications.get( 0 ).getComputingClients().get( i ).getClient().getStub().responseSave( false ) ; + } catch( RemoteException e ) { + System.err.println( "Unable to send the save request response to " + + applications.get( 0 ).getComputingClients().get( i ).getClient().getName() + "!" ) ; + e.printStackTrace() ; + } + break ; + } + } + } + } else { + semaSave.release() ; + System.err.println( "!! Serious problem in treatRequestSave method!!" ) ; + } + } + + + @Override + public void restartOk( String _ip ) + { + if( applications.size() > 0 && _ip != null && _ip.length() > 0 ) + { + System.out.println( "Client " + _ip + " has finished to restart ("+applications.get(0).getComputingClients().size()+") ... " ) ; + if( (System.currentTimeMillis() - applications.get( 0 ).getLastSaveDate()) > save_interleave ) + { + // Has it already finished? + for( int i = 0 ; i < applications.get( 0 ).getComputingClients().size() ; i++ ) + { + if( applications.get( 0 ).getComputingClients().get( i ).getClient().getIP().equalsIgnoreCase( _ip ) ) + { + applications.get( 0 ).getComputingClients().get( i ).setRestartOk( true ) ; + + break ; + } + } + + // Is every body ok? + boolean ok = false ; + for( int i = 0 ; i < applications.get( 0 ).getComputingClients().size() ; i++ ) + { + if( i == 0 ) + { + ok = applications.get( 0 ).getComputingClients().get( i ).getRestartOk() ; + } else { + ok = ok & applications.get( 0 ).getComputingClients().get( i ).getRestartOk() ; + } + + if( ! ok ) + { + break ; + } + } + + if( ok ) + { + applications.get( 0 ).setLastSaveDate( System.currentTimeMillis() ) ; + + for( int i = 0 ; i < applications.get( 0 ).getComputingClients().size() ; i++ ) + { + applications.get( 0 ).getComputingClients().get( i ).setRestartOk( false ) ; + } + } + + } + } + } + @Override public void endApplication() @@ -830,7 +936,7 @@ public class Server extends UnicastRemoteObject implements ServicesServer ComputingClient cl = it.next() ; try { - cl.getClient().getStub().stopVM() ; + cl.getClient().getStub().emergencyStop() ; } catch (RemoteException e) { e.printStackTrace(); } @@ -841,6 +947,10 @@ public class Server extends UnicastRemoteObject implements ServicesServer cl = null ; } + applications.get( 0 ).setEndTime( System.currentTimeMillis() ) ; + applications.get( 0 ).setRunning( false ) ; + applications.get( 0 ).clear() ; + } @@ -897,94 +1007,98 @@ public class Server extends UnicastRemoteObject implements ServicesServer { ret = 1 ; error = false ; - try { - ret = clients.get( i ).getStub().deployVM( _name, _archive, _directory ) ; - } catch( RemoteException e ) { - System.err.println( "Unable to deploy the VM on " + clients.get( i ).getName() + "!" ) ; - e.printStackTrace() ; - } - - // The client does not have the archive, we have to send it. - if( ret == 2 ) + if( clients.get( i ).getStatus().equalsIgnoreCase( "connected" ) ) { - System.out.print( "Sending VM archive to " + clients.get( i ).getName() + " ... " ) ; - - String wd = "" ; - String snIP = "" ; - error = false ; - try { - wd = clients.get( i ).getStub().getWorkingDirectory() ; - snIP = clients.get( i ).getStub().getIPHost() ; - } catch (RemoteException e2) { - System.err.println( "Unable to retrieve information on " + clients.get( i ).getName() + "!" ) ; - e2.printStackTrace() ; - error = true ; - pb++ ; + ret = clients.get( i ).getStub().deployVM( _name, _archive, _directory ) ; + } catch( RemoteException e ) { + System.err.println( "Unable to deploy the VM on " + clients.get( i ).getName() + "!" ) ; + e.printStackTrace() ; } - String[] command = new String[]{ "/usr/bin/scp", working_directory + "/" + _archive, + // The client does not have the archive, we have to send it. + if( ret == 2 ) + { + System.out.print( "Sending VM archive to " + clients.get( i ).getName() + " ... " ) ; + + String wd = "" ; + String snIP = "" ; + error = false ; + + try { + wd = clients.get( i ).getStub().getWorkingDirectory() ; + snIP = clients.get( i ).getStub().getIPHost() ; + } catch (RemoteException e2) { + System.err.println( "Unable to retrieve information on " + clients.get( i ).getName() + "!" ) ; + e2.printStackTrace() ; + error = true ; + pb++ ; + } + + String[] command = new String[]{ "/usr/bin/scp", working_directory + "/" + _archive, snIP + ":" + wd } ; - if( ! error ) - try { - Process proc = Runtime.getRuntime().exec( command ) ; - proc.waitFor() ; - - if( proc.exitValue() == 0 ) + if( ! error ) { - System.out.println( "Initial VM archive successfully sent." ) ; - } else { - System.err.println( "Initial VM archive not sent!" ) ; -// printProcessError( p.getErrorStream() ) ; - System.err.println( "Error: " + proc.exitValue() ) ; - BufferedReader b = new BufferedReader( new InputStreamReader( proc.getErrorStream() ) ) ; - - String l ; try { - while( (l = b.readLine()) != null ) + Process proc = Runtime.getRuntime().exec( command ) ; + proc.waitFor() ; + + if( proc.exitValue() == 0 ) { - System.err.println( l ) ; + System.out.println( "Initial VM archive successfully sent." ) ; + } else { + System.err.println( "Initial VM archive not sent!" ) ; + System.err.println( "Error: " + proc.exitValue() ) ; + BufferedReader b = new BufferedReader( new InputStreamReader( proc.getErrorStream() ) ) ; + + String l ; + try { + while( (l = b.readLine()) != null ) + { + System.err.println( l ) ; + } + } catch( IOException e ) { + e.printStackTrace() ; + } + + error = true ; + pb++ ; } } catch( IOException e ) { - e.printStackTrace() ; + System.err.println( "Error during initial VM archive send command: " ) ; + e.printStackTrace() ; + error = true ; + pb++ ; + } catch( InterruptedException e ) { + e.printStackTrace() ; + error = true ; + pb++ ; } - - error = true ; - pb++ ; } - } catch( IOException e ) { - System.err.println( "Error during initial VM archive send command: " ) ; - e.printStackTrace() ; - error = true ; - pb++ ; - } catch( InterruptedException e ) { - e.printStackTrace() ; - error = true ; - pb++ ; - } - if( error ) - { - continue ; - } + if( error ) + { + continue ; + } - // Second try ... - ret = 1 ; - try { - ret = clients.get( i ).getStub().deployVM( _name, _archive, _directory ) ; - } catch( RemoteException e ) { - System.err.println( "Unable to deploy the VM on " + clients.get( i ).getName() + "!" ) ; - e.printStackTrace() ; - pb++ ; + // Second try ... + ret = 1 ; + try { + ret = clients.get( i ).getStub().deployVM( _name, _archive, _directory ) ; + } catch( RemoteException e ) { + System.err.println( "Unable to deploy the VM on " + clients.get( i ).getName() + "!" ) ; + e.printStackTrace() ; + pb++ ; + } } - } - if( ret == 0 ) - { - System.out.println( "Initial VM archive successfully deployed on " + clients.get( i ).getName() + "." ) ; - nb++ ; + if( ret == 0 ) + { + System.out.println( "Initial VM archive successfully deployed on " + clients.get( i ).getName() + "." ) ; + nb++ ; + } } } } @@ -994,7 +1108,7 @@ public class Server extends UnicastRemoteObject implements ServicesServer if( pb == 1 ) System.err.println( "** " + pb + " machine is not deployed!" ) ; if( pb > 1 ) - System.err.println( "** " + pb + " machine(s) are not deployed!" ) ; + System.err.println( "** " + pb + " machines are not deployed!" ) ; } return nb ;