Package org.apache.hadoop.service
Class AbstractService
java.lang.Object
org.apache.hadoop.service.AbstractService
- All Implemented Interfaces:
Closeable,AutoCloseable,Service
- Direct Known Subclasses:
AbstractLaunchableService,AbstractLivelinessMonitor,AHSClient,AMRMClient,AMRMClientAsync,AsyncDispatcher,CompositeService,HistoryFileManager,NMClient,NMClientAsync,SharedCacheClient,YarnClient
This is the base implementation class for services.
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.apache.hadoop.service.Service
Service.STATE -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal voidclose()Relay tostop()Get the blockers on a service -remote dependencies that are stopping the service from being live.Get the configuration of this service.final ThrowableGet the first exception raised during the service failure.Get the state in which the failure inService.getFailureCause()occurred.Get a snapshot of the lifecycle history; it is a static listgetName()Get the name of this service.final Service.STATEGet the current service statelongGet the service start timevoidinit(Configuration conf) Initialize the service.final booleanisInState(Service.STATE expected) Query to see if the service is in a specific state.protected final voidnoteFailure(Exception exception) Failure handling: record the exception that triggered it -if there was not one already.protected voidputBlocker(String name, String details) Put a blocker to the blocker map -replacing any with the same name.static voidRegister a global listener, which receives notifications from the state change events of all services in the JVMvoidRegister a listener to the service state change events.voidremoveBlocker(String name) Remove a blocker from the blocker map - this is a no-op if the blocker is not presentprotected voidserviceInit(Configuration conf) All initialization code needed by a service.protected voidActions called during the INITED to STARTED transition.protected voidActions called during the transition to the STOPPED state.protected voidsetConfig(Configuration conf) Set the configuration for this service.voidstart()Start the service.voidstop()Stop the service.toString()static booleanunregister a global listener.voidUnregister a previously registered listener of the service state change events.final booleanwaitForServiceToStop(long timeout) Block waiting for the service to stop; uses the termination notification object to do so.
-
Constructor Details
-
AbstractService
Construct the service.- Parameters:
name- service name
-
-
Method Details
-
getServiceState
Description copied from interface:ServiceGet the current service state- Specified by:
getServiceStatein interfaceService- Returns:
- the state of the service
-
getFailureCause
Description copied from interface:ServiceGet the first exception raised during the service failure. If null, no exception was logged- Specified by:
getFailureCausein interfaceService- Returns:
- the failure logged during a transition to the stopped state
-
getFailureState
Description copied from interface:ServiceGet the state in which the failure inService.getFailureCause()occurred.- Specified by:
getFailureStatein interfaceService- Returns:
- the state or null if there was no failure
-
setConfig
Set the configuration for this service. This method is called duringinit(Configuration)and should only be needed if for some reason a service implementation needs to override that initial setting -for example replacing it with a new subclass ofConfiguration- Parameters:
conf- new configuration.
-
init
Initialize the service. The transition MUST be fromService.STATE.NOTINITEDtoService.STATE.INITEDunless the operation failed and an exception was raised, in which caseService.stop()MUST be invoked and the service enter the stateService.STATE.STOPPED. This invokesserviceInit(org.apache.hadoop.conf.Configuration)- Specified by:
initin interfaceService- Parameters:
conf- the configuration of the service. This must not be null- Throws:
ServiceStateException- if the configuration was null, the state change not permitted, or something else went wrong
-
start
public void start()Start the service. The transition MUST be fromService.STATE.INITEDtoService.STATE.STARTEDunless the operation failed and an exception was raised, in which caseService.stop()MUST be invoked and the service enter the stateService.STATE.STOPPED.- Specified by:
startin interfaceService- Throws:
ServiceStateException- if the current service state does not permit this action
-
stop
public void stop()Stop the service. This MUST be a no-op if the service is already in theService.STATE.STOPPEDstate. It SHOULD be a best-effort attempt to stop all parts of the service. The implementation must be designed to complete regardless of the service state, including the initialized/uninitialized state of all its internal fields. -
close
Relay tostop()- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceService- Throws:
IOException- raised on errors performing I/O.
-
noteFailure
Failure handling: record the exception that triggered it -if there was not one already. Services are free to call this themselves.- Parameters:
exception- the exception
-
waitForServiceToStop
public final boolean waitForServiceToStop(long timeout) Description copied from interface:ServiceBlock waiting for the service to stop; uses the termination notification object to do so. This method will only return after all the service stop actions have been executed (to success or failure), or the timeout elapsed This method can be called before the service is inited or started; this is to eliminate any race condition with the service stopping before this event occurs.- Specified by:
waitForServiceToStopin interfaceService- Parameters:
timeout- timeout in milliseconds. A value of zero means "forever"- Returns:
- true iff the service stopped in the time period
-
serviceInit
All initialization code needed by a service. This method will only ever be called once during the lifecycle of a specific service instance. Implementations do not need to be synchronized as the logic ininit(Configuration)prevents re-entrancy. The base implementation checks to see if the subclass has created a new configuration instance, and if so, updates the base class value- Parameters:
conf- configuration- Throws:
Exception- on a failure -these will be caught, possibly wrapped, and will trigger a service stop
-
serviceStart
Actions called during the INITED to STARTED transition. This method will only ever be called once during the lifecycle of a specific service instance. Implementations do not need to be synchronized as the logic instart()prevents re-entrancy.- Throws:
Exception- if needed -these will be caught, wrapped, and trigger a service stop
-
serviceStop
Actions called during the transition to the STOPPED state. This method will only ever be called once during the lifecycle of a specific service instance. Implementations do not need to be synchronized as the logic instop()prevents re-entrancy. Implementations MUST write this to be robust against failures, including checks for null references -and for the first failure to not stop other attempts to shut down parts of the service.- Throws:
Exception- if needed -these will be caught and logged.
-
registerServiceListener
Description copied from interface:ServiceRegister a listener to the service state change events. If the supplied listener is already listening to this service, this method is a no-op.- Specified by:
registerServiceListenerin interfaceService- Parameters:
l- a new listener
-
unregisterServiceListener
Description copied from interface:ServiceUnregister a previously registered listener of the service state change events. No-op if the listener is already unregistered.- Specified by:
unregisterServiceListenerin interfaceService- Parameters:
l- the listener to unregister.
-
registerGlobalListener
Register a global listener, which receives notifications from the state change events of all services in the JVM- Parameters:
l- listener
-
unregisterGlobalListener
unregister a global listener.- Parameters:
l- listener to unregister- Returns:
- true if the listener was found (and then deleted)
-
getName
Description copied from interface:ServiceGet the name of this service. -
getConfig
Description copied from interface:ServiceGet the configuration of this service. This is normally not a clone and may be manipulated, though there are no guarantees as to what the consequences of such actions may be -
getStartTime
public long getStartTime()Description copied from interface:ServiceGet the service start time- Specified by:
getStartTimein interfaceService- Returns:
- the start time of the service. This will be zero if the service has not yet been started.
-
getLifecycleHistory
Description copied from interface:ServiceGet a snapshot of the lifecycle history; it is a static list- Specified by:
getLifecycleHistoryin interfaceService- Returns:
- a possibly empty but never null list of lifecycle events.
-
isInState
Description copied from interface:ServiceQuery to see if the service is in a specific state. In a multi-threaded system, the state may not hold for very long. -
toString
-
putBlocker
Put a blocker to the blocker map -replacing any with the same name.- Parameters:
name- blocker namedetails- any specifics on the block. This must be non-null.
-
removeBlocker
Remove a blocker from the blocker map - this is a no-op if the blocker is not present- Parameters:
name- the name of the blocker
-
getBlockers
Description copied from interface:ServiceGet the blockers on a service -remote dependencies that are stopping the service from being live.- Specified by:
getBlockersin interfaceService- Returns:
- a (snapshotted) map of blocker name->description values
-