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
}
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<ServicesClient> save_neighbor ;
- private String lastSaveName ;
-
- ComputingClient( ConnectedClient cl )
- {
- client = cl ;
- save_status = false ;
- save_neighbor = new ArrayList<ServicesClient>() ;
- 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
IPAssociation()
{
- vmIP = null ;
- hostIP = null ;
+ vmIP = "" ;
+ hostIP = "" ;
}
protected void setVmIP( String _vmIP )
private int port ;
private ArrayList<ConnectedClient> clients ;
private ArrayList<ComputingClient> computingClients ;
+ private ArrayList<RunningApplication> applications ;
private int max_timeout ;
private ConnectedMonitor monitor ;
private DiscCount counter ;
private ArrayList<IPAssociation> vmIPs ;
private String working_directory ;
+ private long save_interleave ;
+ private Semaphore semaSave ;
protected Server() throws RemoteException
{
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 ) ;
port = _port ;
max_timeout = 4 ;
- clients = new ArrayList<Server.ConnectedClient>() ;
- computingClients = new ArrayList<Server.ComputingClient>() ;
+ clients = new ArrayList<ConnectedClient>() ;
+ computingClients = new ArrayList<ComputingClient>() ;
+ applications = new ArrayList<RunningApplication>() ;
monitor = null ;
working_directory = "/localhome/vmware" ;
+ save_interleave = 30 * 60 * 1000 ;
+
+ semaSave = new Semaphore( 1 ) ;
+
exportObject() ;
vmIPs = new ArrayList<IPAssociation>() ;
vmIPs.get( vmIPs.size() - 1 ).setVmIP( "10.11.10." + i ) ;
}
- clients = new ArrayList<Server.ConnectedClient>() ;
+ clients = new ArrayList<ConnectedClient>() ;
counter = new DiscCount() ;
monitor = new ConnectedMonitor() ;
monitor.start() ;
+
+ // TODO
+ // Check if there are running applications ... and restart them :)
}
for( int i = 0 ; i < clients.size() ; i++ )
{
try {
+ clients.get( i ).getStub().emergencyStop() ;
clients.get( i ).getStub().stop() ;
} catch (RemoteException e) {
e.printStackTrace();
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 ;
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 ) ;
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 {
{
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!" ) ;
}
}
- 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() {
}
computingClients.get( i ).setSaveStatus( false ) ;
}
+
+ applications.get( 0 ).setLastSaveDate( System.currentTimeMillis() ) ;
}
return 0 ;
if( nb > _nb )
{
ArrayList<ServicesClient> ac = new ArrayList<ServicesClient>() ;
- ArrayList<ComputingClient> tmp = new ArrayList<Server.ComputingClient>() ;
+ ArrayList<ComputingClient> tmp = new ArrayList<ComputingClient>() ;
+
+ RunningApplication app = new RunningApplication( "Test" ) ;
int i = 0 ;
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++ )
{
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 ) ) ;
{
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()
ComputingClient cl = it.next() ;
try {
- cl.getClient().getStub().stopVM() ;
+ cl.getClient().getStub().emergencyStop() ;
} catch (RemoteException e) {
e.printStackTrace();
}
cl = null ;
}
+ applications.get( 0 ).setEndTime( System.currentTimeMillis() ) ;
+ applications.get( 0 ).setRunning( false ) ;
+ applications.get( 0 ).clear() ;
+
}
{
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++ ;
+ }
}
}
}
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 ;