S
- service class to cast the generated service to.public class ServiceLauncher<S extends Service> extends Object implements LauncherExitCodes, LauncherArguments, Thread.UncaughtExceptionHandler
Service
LaunchableService.bindArgs(Configuration, List)
,
it is given the binding args off the CLI after all general configuration
arguments have been stripped.Service.init(Configuration)
and Service.start()
methods are called.LaunchableService.execute()
is called and its return code used as the exit code.Service.start()
method spawns one or more thread
to perform workExitCodeProvider
,
the return value of ExitCodeProvider.getExitCode()
,
becomes the exit code of the command.stderr
.Modifier and Type | Class and Description |
---|---|
protected static class |
ServiceLauncher.MinimalGenericOptionsParser
A generic options parser which does not parse any of the traditional
Hadoop options.
|
Modifier and Type | Field and Description |
---|---|
protected static String[] |
DEFAULT_CONFIGS
List of the standard configurations to create (and so load in properties).
|
static String |
NAME
The name of this class.
|
protected static int |
SHUTDOWN_PRIORITY
Priority for the shutdown hook: 30.
|
static String |
USAGE_MESSAGE
Usage message.
|
protected static String |
USAGE_NAME |
protected static String |
USAGE_SERVICE_ARGUMENTS |
EXIT_BAD_CONFIGURATION, EXIT_CLIENT_INITIATED_SHUTDOWN, EXIT_COMMAND_ARGUMENT_ERROR, EXIT_CONNECTIVITY_PROBLEM, EXIT_EXCEPTION_THROWN, EXIT_FAIL, EXIT_FORBIDDEN, EXIT_INTERRUPTED, EXIT_NOT_ACCEPTABLE, EXIT_NOT_FOUND, EXIT_OPERATION_NOT_ALLOWED, EXIT_OTHER_FAILURE, EXIT_SERVICE_CREATION_FAILURE, EXIT_SERVICE_LIFECYCLE_EXCEPTION, EXIT_SERVICE_UNAVAILABLE, EXIT_SUCCESS, EXIT_TASK_LAUNCH_FAILURE, EXIT_UNAUTHORIZED, EXIT_UNIMPLEMENTED, EXIT_UNSUPPORTED_VERSION, EXIT_USAGE
ARG_CONF, ARG_CONF_PREFIXED, ARG_CONF_SHORT, ARG_CONFCLASS, ARG_CONFCLASS_PREFIXED, ARG_CONFCLASS_SHORT, E_PARSE_FAILED
Constructor and Description |
---|
ServiceLauncher(String serviceClassName)
Create an instance of the launcher.
|
ServiceLauncher(String serviceName,
String serviceClassName)
Create an instance of the launcher.
|
Modifier and Type | Method and Description |
---|---|
protected void |
bindCommandOptions()
Set the
commandOptions field to the result of
createOptions() ; protected for subclasses and test access. |
protected static ExitUtil.ExitException |
convertToExitException(Throwable thrown)
Convert an exception to an
ExitException . |
protected int |
coreServiceLaunch(Configuration conf,
List<String> processedArgs,
boolean addShutdownHook,
boolean execute)
Launch the service.
|
protected Configuration |
createConfiguration()
Override point: create the base configuration for the service.
|
protected org.apache.hadoop.util.GenericOptionsParser |
createGenericOptionsParser(Configuration conf,
String[] argArray)
Override point: create a generic options parser or subclass thereof.
|
protected org.apache.commons.cli.Options |
createOptions()
Override point: create an options instance to combine with the
standard options set.
|
protected void |
error(String message,
Throwable thrown)
Report an error.
|
protected void |
exit(ExitUtil.ExitException ee)
Exit the JVM using an exception for the exit code and message,
invoking
ExitUtil.terminate(ExitUtil.ExitException) . |
protected void |
exit(int exitCode,
String message)
Exit the JVM.
|
protected static void |
exitWithMessage(int status,
String message)
Exit with a printed message.
|
protected static void |
exitWithUsageMessage()
Exit with the usage exit code
LauncherExitCodes.EXIT_USAGE
and message USAGE_MESSAGE . |
List<String> |
extractCommandOptions(Configuration conf,
List<String> args)
Extract the command options and apply them to the configuration,
building an array of processed arguments to hand down to the service.
|
protected ClassLoader |
getClassLoader()
Override point: get the classloader to use.
|
Configuration |
getConfiguration()
Get the configuration constructed from the command line arguments.
|
protected List<String> |
getConfigurationsToCreate()
Override point: Get a list of configuration classes to create.
|
S |
getService()
Get the service.
|
ExitUtil.ExitException |
getServiceException()
Get the exit exception used to end this service.
|
int |
getServiceExitCode()
The exit code from a successful service execution.
|
String |
getServiceName()
Get the service name via
Service.getName() . |
protected String |
getUsageMessage()
Get the usage message, ideally dynamically.
|
Service |
instantiateService(Configuration conf)
Instantiate the service defined in
serviceClassName . |
ExitUtil.ExitException |
launchService(Configuration conf,
List<String> processedArgs,
boolean addShutdownHook,
boolean execute)
Launch a service catching all exceptions and downgrading them to exit codes
after logging.
|
void |
launchServiceAndExit(List<String> args)
Launch the service and exit.
|
int |
loadConfigurationClasses()
This creates all the configurations defined by
getConfigurationsToCreate() , ensuring that
the resources have been pushed in. |
static void |
main(String[] args)
This is the JVM entry point for the service launcher.
|
protected List<String> |
parseCommandArgs(Configuration conf,
List<String> args)
Parse the command arguments, extracting the service class as the last
element of the list (after extracting all the rest).
|
protected void |
registerFailureHandling()
Override point: register this class as the handler for the control-C
and SIGINT interrupts.
|
protected ServiceLaunchException |
serviceCreationFailure(Exception exception)
Generate an exception announcing a failure to create the service.
|
static void |
serviceMain(List<String> argsList)
The real main function, which takes the arguments as a list.
|
static void |
serviceMain(String... args)
Varargs version of the entry point for testing and other in-JVM use.
|
protected void |
setService(S s)
Setter is to give subclasses the ability to manipulate the service.
|
protected static String |
startupShutdownMessage(String classname,
List<String> args)
Build a log message for starting up and shutting down.
|
String |
toString() |
void |
uncaughtException(Thread thread,
Throwable exception)
Handler for uncaught exceptions: terminate the service.
|
protected void |
verifyConfigurationFilesExist(String[] filenames)
Verify that all the specified filenames exist.
|
protected void |
warn(String text)
Print a warning message.
|
protected static final int SHUTDOWN_PRIORITY
public static final String NAME
protected static final String USAGE_NAME
protected static final String USAGE_SERVICE_ARGUMENTS
public static final String USAGE_MESSAGE
protected static final String[] DEFAULT_CONFIGS
public ServiceLauncher(String serviceClassName)
serviceClassName
- classname of the servicepublic final S getService()
coreServiceLaunch(Configuration, List, boolean, boolean)
has completed.protected void setService(S s)
s
- the new servicepublic final Configuration getConfiguration()
public final int getServiceExitCode()
public final ExitUtil.ExitException getServiceException()
System.exit
has not been called)public void launchServiceAndExit(List<String> args)
LaunchableService
: execute itexit(int, String)
method.args
- arguments to the service. arg[0]
is
assumed to be the service classname.protected void bindCommandOptions()
commandOptions
field to the result of
createOptions()
; protected for subclasses and test access.protected String getUsageMessage()
protected org.apache.commons.cli.Options createOptions()
OptionBuilder
with OptionBuilder.class
protected Configuration createConfiguration()
protected List<String> getConfigurationsToCreate()
public int loadConfigurationClasses()
getConfigurationsToCreate()
, ensuring that
the resources have been pushed in.
If one cannot be loaded it is logged and the operation continues
except in the case that the class does load but it isn't actually
a subclass of Configuration
.ExitUtil.ExitException
- if a loaded class is of the wrong typepublic ExitUtil.ExitException launchService(Configuration conf, List<String> processedArgs, boolean addShutdownHook, boolean execute)
serviceException
to this value.conf
- configuration to useprocessedArgs
- command line after the launcher-specific arguments
have been stripped out.addShutdownHook
- should a shutdown hook be added to terminate
this service on shutdown. Tests should set this to false.execute
- execute/wait for the service to stop.protected int coreServiceLaunch(Configuration conf, List<String> processedArgs, boolean addShutdownHook, boolean execute) throws Exception
LaunchableService
, that the
method LaunchableService.execute()
has completed.
After this method returns, the service can be retrieved returned by
getService()
.conf
- configurationprocessedArgs
- arguments after the configuration parameters
have been stripped out.addShutdownHook
- should a shutdown hook be added to terminate
this service on shutdown. Tests should set this to false.execute
- execute/wait for the service to stopClassNotFoundException
- classname not on the classpathIllegalAccessException
- not allowed at the classInstantiationException
- not allowed to instantiate itInterruptedException
- thread interruptedExitUtil.ExitException
- any exception defining the status code.Exception
- any other failure -if it implements
ExitCodeProvider
then it defines the exit code for any
containing exceptionpublic Service instantiateService(Configuration conf)
serviceClassName
.
Sets the configuration
field
to the the value of conf
,
and the service
field to the service created.conf
- configuration to useprotected static ExitUtil.ExitException convertToExitException(Throwable thrown)
ExitException
.
This process may just be a simple pass through, otherwise a new
exception is created with an exit code, the text of the supplied
exception, and the supplied exception as an inner cause.
ExitCodeProvider.getExitCode()
,
the exit code is extracted and used in the new exception.LauncherExitCodes.EXIT_EXCEPTION_THROWN
is used.thrown
- the exception thrownExitException
with a status codeprotected ServiceLaunchException serviceCreationFailure(Exception exception)
exception
- inner exception.LauncherExitCodes.EXIT_SERVICE_CREATION_FAILURE
protected void registerFailureHandling()
Thread.setDefaultUncaughtExceptionHandler( new YarnUncaughtExceptionHandler());
public void uncaughtException(Thread thread, Throwable exception)
uncaughtException
in interface Thread.UncaughtExceptionHandler
thread
- threadexception
- exceptionpublic String getServiceName()
Service.getName()
.
If the service is not instantiated, the classname is returned instead.protected void warn(String text)
This tries to log to the log's warn() operation. If the log at that level is disabled it logs to system error
text
- warning textprotected void error(String message, Throwable thrown)
This tries to log to LOG.error()
.
If that log level is disabled disabled the message
is logged to system error along with thrown.toString()
message
- message for the userthrown
- the exception thrownprotected void exit(int exitCode, String message)
ExitException
instance/subclass.
The service launcher code assumes that after this method is invoked,
no other code in the same method is called.exitCode
- code to exitprotected void exit(ExitUtil.ExitException ee)
ExitUtil.terminate(ExitUtil.ExitException)
.
This is the standard way a launched service exits.
An error code of 0 means success -nothing is printed.
If ExitUtil.disableSystemExit()
has been called, this
method will throw the exception.
The method may be subclassed for testingee
- exit exceptionExitUtil.ExitException
- if ExitUtil exceptions are disabledprotected ClassLoader getClassLoader()
public List<String> extractCommandOptions(Configuration conf, List<String> args)
conf
- configuration to update.args
- main arguments. args[0]
is assumed to be
the service classname and is skipped.ExitUtil.ExitException
- if JVM exiting is disabled.protected List<String> parseCommandArgs(Configuration conf, List<String> args)
commandOptions
field must already have been set.conf
- configuration to useargs
- command line argument listServiceLaunchException
- if processing of arguments failedprotected org.apache.hadoop.util.GenericOptionsParser createGenericOptionsParser(Configuration conf, String[] argArray) throws IOException
conf
- Hadoop configurationargArray
- array of argumentsIOException
- on any failureprotected void verifyConfigurationFilesExist(String[] filenames)
filenames
- a list of filesServiceLaunchException
- if a file is not foundprotected static String startupShutdownMessage(String classname, List<String> args)
classname
- the class of the serverargs
- argumentsprotected static void exitWithMessage(int status, String message)
status
- status codemessage
- message message to print before exitingExitUtil.ExitException
- if exceptions are disabledprotected static void exitWithUsageMessage()
LauncherExitCodes.EXIT_USAGE
and message USAGE_MESSAGE
.ExitUtil.ExitException
- if exceptions are disabledpublic static void main(String[] args)
serviceMain(List)
args
- command line arguments.public static void serviceMain(String... args)
serviceMain(List)
args
- command line arguments.Copyright © 2019 Apache Software Foundation. All Rights Reserved.