com.sun.faban.harness.engine
Class CmdService

java.lang.Object
  extended by com.sun.faban.harness.engine.CmdService

public final class CmdService
extends Object

This file contains the class that implements the Command service API. The Command Service object is created by the Engine at the start of a run and it starts up the CmdAgent applications on all the machines and connects to them via RMI. In the API implementation, it identifies the particular CmdAgent and passes the call along. The CmdAgents take care of any error messages generated by the command and automatically log them to the run's error log. The CmdAgent's path will include the default faban bin directories (in addition to /usr/bin, /usr/sbin, /usr/ucb), so any faban executables will be found. Commands in any other path should be invoked with the full pathname of the command. The CmdAgent's environment will also include CLASSPATH set to the faban lib directory to find any Java classes. Shell commands or any commands whose output must be re-directed or piped (basically using shell) should be executed using syntax such as : "sh -c [] [> out]". IMPORTANT: There should be a single CmdService object in the entire framework or else multiple copies of the CmdAgent application will be spawned on the target machines. Therefore, this class is implemented as a Singleton. No public constructors are defined and the object cannot be cloned.

Author:
Ramesh Ramachandran
See Also:
CmdAgent, GenericBenchmark

Field Summary
static int PARALLEL
          Parallel flag in FG mode.
static int SEQUENTIAL
          Sequential flag in FG mode.
 
Method Summary
 boolean copy(String srcmachine, String destmachine, String srcfile, String destfile, boolean append)
          Deprecated.  
 boolean copyToStream(String srcmachine, String srcfile, OutputStream stream)
          Copy a file from one remote machine to a stream on the master.
 boolean delete(String srcmachine, String srcfile)
          Deletes the file from the machine.
 boolean delete(String srcmachine, String dir, FileFilter filter)
          Deletes the file from the machine based on the filter provided.
 boolean doesFileExist(String hostName, String fileName)
          Checks whether the given remote file exists.
 void downloadServices(String machine, List<String> pathList)
          Downloads files used by deploy images, especially services and tools.
 CommandHandle execute(Command c, String svcPath)
          Executes a command from the master's command agent.
 CommandHandle[] execute(String[] machines, Command c, String svcPath)
          Executes a command from the remote command agent.
<V extends Serializable>
List<V>
execute(String[] machines, RemoteCallable<V> callable, String svcPath)
          Executes a job on remote command agents on a list of systems.
 CommandHandle execute(String machine, Command c, String svcPath)
          Executes a command from the remote command agent.
<V extends Serializable>
V
execute(String machine, RemoteCallable<V> callable, String svcPath)
          Executes a job in a remote command agent.
 boolean get(String srcmachine, String srcfile, String destfile)
          Gets a remote file to the Faban master.
static CmdService getHandle()
          This method is the only way that an external object can get a reference to the singleton CmdService.
 String getHostName(String machineName)
          Returns the hostname of this machine as known to the machine itself.
 HostRoles getHostRoles()
          Obtains the cached HostType object.
 String getMaster()
          Obtains the name of the master machine.
 String getMasterIP()
          Returns the ip address of the master.
 String getMasterIP(String agentHost)
          Returns the ip address of the master's interface best used for communicating with the target host.
 String getProperty(String machine, String propFile, String propName)
          Gets a property from a given file.
 Registry getRegistry()
          Obtains the registry.
 String getTmpDir(String machine)
          Obtains the temporary dircteroy for the given machine.
 void init()
          This method is called after every run to re-initialize the data structures that need to change from one run to another.
 boolean isDirectory(String hostName, String fileName)
          Checks whether the given remote file exists and is a directory.
 boolean isFile(String hostName, String fileName)
          Checks whether the given remote file exists and is a normal file.
 CommandHandle java(Command c, String svcPath)
          Executes a java command from the master's command agent.
 CommandHandle[] java(String[] machines, Command c, String svcPath)
          Executes a java command from the remote command agent.
 CommandHandle java(String machine, Command c, String svcPath)
          Executes a java command from the remote command agent.
 void kill()
          Kill all commands currently running and cleanup.
 boolean push(String srcfile, String destmachine, String destfile)
          Pushes a local file on the Faban master to the remote host.
 void setLogLevel(String name, Level level)
          Set the Log level for Agents.
 boolean setup(String benchName, ParamRepository par)
          This method initializes the CmdAgent RMI server processes on the specified set of machines.
 boolean startAgent(String[] machines, Class agentClass, String identifier)
          Start Agent in the specified machines.
 boolean startAgent(String machine, Class agentClass, String identifier)
          Start the agent on a single machine.
 boolean truncate(String srcmachine, String srcfile)
          Truncates the file from the machine.
 void updatePaths(List<String> pathList)
          Updates the paths in the local command agent.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SEQUENTIAL

public static final int SEQUENTIAL
Sequential flag in FG mode.

See Also:
Constant Field Values

PARALLEL

public static final int PARALLEL
Parallel flag in FG mode.

See Also:
Constant Field Values
Method Detail

getHandle

public static CmdService getHandle()
This method is the only way that an external object can get a reference to the singleton CmdService. This method should not be used outside engine.

Returns:
reference to the single CmdService

getMaster

public String getMaster()
Obtains the name of the master machine.

Returns:
The master machine name

getMasterIP

public String getMasterIP()
Returns the ip address of the master.

Returns:
The ip address of the master

getMasterIP

public String getMasterIP(String agentHost)
Returns the ip address of the master's interface best used for communicating with the target host.

Parameters:
agentHost - The target host
Returns:
The ip address of the master

init

public void init()
This method is called after every run to re-initialize the data structures that need to change from one run to another.


setup

public boolean setup(String benchName,
                     ParamRepository par)
This method initializes the CmdAgent RMI server processes on the specified set of machines. This method can be called multiple times to initialize multiple classes of machines.

Parameters:
benchName - The name of the benchmark
par - The parameter repository
Returns:
true if successful, false if setup failed

getHostRoles

public HostRoles getHostRoles()
Obtains the cached HostType object. Note that this is not a public API.

Returns:
The cached HostType object;

getHostName

public String getHostName(String machineName)
Returns the hostname of this machine as known to the machine itself. This method is included in order to solve a Naming problem related to the names of the tpcw result files to be transferred to the the master machine.

Parameters:
machineName - The target machine to check the host name
Returns:
The host name of the remote machine

updatePaths

public void updatePaths(List<String> pathList)
                 throws RemoteException
Updates the paths in the local command agent.

Parameters:
pathList - The list of paths to download
Throws:
RemoteException - If there is a communication error to the remote agent

downloadServices

public void downloadServices(String machine,
                             List<String> pathList)
                      throws RemoteException
Downloads files used by deploy images, especially services and tools. The pathList contains a list of resources in the form type/resource.

Parameters:
machine - The host name to initiate the download
pathList - The list of paths to download
Throws:
RemoteException - If there is a communication error to the remote agent

execute

public CommandHandle execute(Command c,
                             String svcPath)
                      throws IOException,
                             InterruptedException
Executes a command from the master's command agent.

Parameters:
c - The command to be executed
svcPath -
Returns:
A handle to the command
Throws:
IOException - Error communicating with resulting process
InterruptedException - Thread got interrupted waiting
RemoteException - If there is a communication error to the remote agent

execute

public CommandHandle execute(String machine,
                             Command c,
                             String svcPath)
                      throws IOException,
                             InterruptedException
Executes a command from the remote command agent.

Parameters:
machine - The target machine to execute the command
c - The command to be executed
svcPath - The location of the invoking service, if any
Returns:
A handle to the command
Throws:
IOException - Error communicating with resulting process
InterruptedException - Thread got interrupted waiting
RemoteException - If there is a communication error to the remote agent

execute

public CommandHandle[] execute(String[] machines,
                               Command c,
                               String svcPath)
                        throws IOException,
                               InterruptedException
Executes a command from the remote command agent.

Parameters:
machines - The target machines to execute the command
c - The command to be executed
svcPath - The location of the invoking service, if any
Returns:
Handles to the command on each of the target machines
Throws:
IOException - Error communicating with resulting process
InterruptedException - Thread got interrupted waiting
RemoteException - If there is a communication error to the remote agent

java

public CommandHandle java(Command c,
                          String svcPath)
                   throws IOException,
                          InterruptedException
Executes a java command from the master's command agent.

Parameters:
c - The command to be executed
svcPath - The location of the invoking service, if any
Returns:
A handle to the command
Throws:
IOException - Error communicating with resulting process
InterruptedException - Thread got interrupted waiting
RemoteException - If there is a communication error to the remote agent

java

public CommandHandle java(String machine,
                          Command c,
                          String svcPath)
                   throws IOException,
                          InterruptedException
Executes a java command from the remote command agent.

Parameters:
machine - The target machine to execute the command
c - The command to be executed
svcPath - The location of the invoking service, if any
Returns:
A handle to the command
Throws:
IOException - Error communicating with resulting process
InterruptedException - Thread got interrupted waiting
RemoteException - If there is a communication error to the remote agent

java

public CommandHandle[] java(String[] machines,
                            Command c,
                            String svcPath)
                     throws IOException,
                            InterruptedException
Executes a java command from the remote command agent.

Parameters:
machines - The target machines to execute the command
c - The command to be executed
svcPath - The location of the invoking service, if any
Returns:
Handles to the command on each of the target machines
Throws:
IOException - Error communicating with resulting process
InterruptedException - Thread got interrupted waiting
RemoteException - If there is a communication error to the remote agent

execute

public <V extends Serializable> V execute(String machine,
                                          RemoteCallable<V> callable,
                                          String svcPath)
                               throws Exception
Executes a job in a remote command agent.

Parameters:
machine - The host to execute the command
callable - The job
svcPath - The location of the invoking service, if any
Returns:
The return value of the job
Throws:
Exception - An error occured executing the remote job

execute

public <V extends Serializable> List<V> execute(String[] machines,
                                                RemoteCallable<V> callable,
                                                String svcPath)
                                     throws Exception
Executes a job on remote command agents on a list of systems.

Parameters:
machines - The host names to execute the job
callable - The job
svcPath - The location of the invoking service, if any
Returns:
The return values of the job, in sequence
Throws:
Exception - An error occurred executing the job

startAgent

public boolean startAgent(String machine,
                          Class agentClass,
                          String identifier)
                   throws Exception
Start the agent on a single machine.

Parameters:
machine - on which command should be started
agentClass - Impl Class of the agent to be started
identifier - to identify this agent later
Returns:
true if the command completed successfully, else false
Throws:
Exception - An error occurred starting the command

startAgent

public boolean startAgent(String[] machines,
                          Class agentClass,
                          String identifier)
                   throws Exception
Start Agent in the specified machines.

Parameters:
machines - on which command should be started
agentClass - Impl Class of the agent to be started
identifier - to identify this agent later
Returns:
true if all commands completed successfully, else false
Throws:
Exception - An error occurred starting the commands

getProperty

public String getProperty(String machine,
                          String propFile,
                          String propName)
                   throws IOException
Gets a property from a given file.

Parameters:
machine - The machine name
propFile - The property file name
propName - The property key name
Returns:
The property value
Throws:
IOException - If there is an error accessing the config file

kill

public void kill()
Kill all commands currently running and cleanup. This method is called when a run must be aborted or at the end of a benchmark run.


push

public boolean push(String srcfile,
                    String destmachine,
                    String destfile)
Pushes a local file on the Faban master to the remote host.

Parameters:
srcfile - The source file name, relative to the out dir
destmachine - The destination machine
destfile - The destination file name
Returns:
true if successful, false otherwise

get

public boolean get(String srcmachine,
                   String srcfile,
                   String destfile)
Gets a remote file to the Faban master.

Parameters:
srcmachine - The source machine
srcfile - The source file name
destfile - The destination file name, always full path
Returns:
true if successful, false otherwise

copy

@Deprecated
public boolean copy(String srcmachine,
                               String destmachine,
                               String srcfile,
                               String destfile,
                               boolean append)
Deprecated. 

Copy a file from one remote machine to another This method essentially does the work of 'rcp' using the FileAgents on the machines.

Parameters:
srcmachine - - Name of source machine
destmachine - - Name of destination machine
srcfile - - Name of source file
destfile - - Name of destination file
append - to dest file or overwrite it
Returns:
true/false if copy was successful/failed

getTmpDir

public String getTmpDir(String machine)
Obtains the temporary dircteroy for the given machine.

Parameters:
machine - The machine name
Returns:
The temporary directory to use on the machine

delete

public boolean delete(String srcmachine,
                      String srcfile)
Deletes the file from the machine.

Parameters:
srcmachine - The machine name
srcfile - The file name
Returns:
true if the file has been deleted, false otherwise

delete

public boolean delete(String srcmachine,
                      String dir,
                      FileFilter filter)
Deletes the file from the machine based on the filter provided.

Parameters:
srcmachine - The machine name
dir - The directory name
filter - The file filter to use
Returns:
true if akk files selected by the filter has been removed

truncate

public boolean truncate(String srcmachine,
                        String srcfile)
Truncates the file from the machine.

Parameters:
srcmachine - The machine name
srcfile - The file name
Returns:
true if the file has been deleted, false otherwise

copyToStream

public boolean copyToStream(String srcmachine,
                            String srcfile,
                            OutputStream stream)
Copy a file from one remote machine to a stream on the master. This method essentially does the work of 'rcp' using the FileAgents on the machines.

Parameters:
srcmachine - - Name of source machine
srcfile - - Name of source file
stream - The stream to copy the content to
Returns:
true/false if copy was successful/failed

setLogLevel

public void setLogLevel(String name,
                        Level level)
Set the Log level for Agents.

Parameters:
name - Logger name
level - Log level

getRegistry

public Registry getRegistry()
Obtains the registry.

Returns:
The registry

doesFileExist

public boolean doesFileExist(String hostName,
                             String fileName)
Checks whether the given remote file exists.

Parameters:
hostName - The host name to check.
fileName - The file name to test.
Returns:
true if exists, false otherwise.

isFile

public boolean isFile(String hostName,
                      String fileName)
Checks whether the given remote file exists and is a normal file.

Parameters:
hostName - The host name to check.
fileName - The file name to test.
Returns:
true if file is a normal file, false otherwise.

isDirectory

public boolean isDirectory(String hostName,
                           String fileName)
Checks whether the given remote file exists and is a directory.

Parameters:
hostName - The host name to check.
fileName - The file name to test.
Returns:
true if file is a directory, false otherwise.