-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Detecting timeouts when a service stops #343
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
+ _boot_ | ||
+ _main_ - list of modules to start the system | ||
|
||
+ _shutdown_ | ||
+ _serviceTimeout_ - the time after which the warning will be issued | ||
+ _serviceAsyncShutdownAfterTimeout_ - if the shutdown time exceeded **serviceTimeout**, the system proceeds to shutdown the next service. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
...ation/oap-application-test/src/test/resources/oap/application/KernelTest/application.conf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
package oap.application.supervision; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import oap.application.ApplicationConfiguration; | ||
import oap.application.KernelHelper; | ||
import oap.concurrent.Executors; | ||
import oap.util.BiStream; | ||
|
@@ -34,7 +35,7 @@ | |
import java.io.Closeable; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Future; | ||
|
@@ -49,20 +50,21 @@ public class Supervisor { | |
private boolean stopped = false; | ||
|
||
private static void runAndDetectTimeout( String name, ShutdownConfiguration shutdownConfiguration, Runnable func ) { | ||
if( shutdownConfiguration.timeoutMs > 0 ) { | ||
long serviceTimeout = shutdownConfiguration.shutdown.serviceTimeout; | ||
if( serviceTimeout > 0 ) { | ||
long start = DateTimeUtils.currentTimeMillis(); | ||
Future<?> future = shutdownConfiguration.submit( func ); | ||
try { | ||
future.get( shutdownConfiguration.timeoutMs, TimeUnit.MILLISECONDS ); | ||
future.get( serviceTimeout, TimeUnit.MILLISECONDS ); | ||
} catch( InterruptedException e ) { | ||
log.trace( e.getMessage() ); | ||
} catch( ExecutionException e ) { | ||
throw Throwables.propagate( e ); | ||
} catch( TimeoutException e ) { | ||
log.warn( "APP_TIMEOUT_START service {} after {}", name, Dates.durationToString( shutdownConfiguration.timeoutMs ) ); | ||
log.warn( "APP_TIMEOUT_START service {} after {}", name, Dates.durationToString( serviceTimeout ) ); | ||
|
||
try { | ||
if( !shutdownConfiguration.forceAsyncAfterTimeout ) { | ||
if( !shutdownConfiguration.shutdown.serviceAsyncShutdownAfterTimeout ) { | ||
future.get(); | ||
|
||
log.warn( "APP_TIMEOUT_END service {} done in {}", name, Dates.durationToString( DateTimeUtils.currentTimeMillis() - start ) ); | ||
|
@@ -90,8 +92,8 @@ public synchronized void startSupervised( String name, Object service, | |
// this.wrappers.put( name, new ThreadService( name, ( Runnable ) instance, this ) ); | ||
// } | ||
|
||
public synchronized void startThread( String name, Object instance ) { | ||
this.wrappers.put( name, new ThreadService( name, ( Runnable ) instance, this ) ); | ||
public synchronized void startThread( String name, Object instance, ApplicationConfiguration.ModuleShutdown shutdown ) { | ||
this.wrappers.put( name, new ThreadService( name, ( Runnable ) instance, this, shutdown ) ); | ||
} | ||
|
||
public synchronized void scheduleWithFixedDelay( String name, Runnable service, long delay, TimeUnit unit ) { | ||
|
@@ -159,42 +161,51 @@ public synchronized void start() { | |
} ); | ||
} | ||
|
||
public synchronized void preStop() { | ||
public synchronized void preStop( ApplicationConfiguration.ModuleShutdown shutdown ) { | ||
if( !stopped ) { | ||
log.debug( "pre stopping..." ); | ||
|
||
BiStream.of( this.wrappers ) | ||
.reversed() | ||
.forEach( ( name, service ) -> { | ||
log.debug( "[{}] pre stopping {}...", service.type(), name ); | ||
KernelHelper.setThreadNameSuffix( name ); | ||
try { | ||
service.preStop(); | ||
} finally { | ||
KernelHelper.restoreThreadName(); | ||
} | ||
log.debug( "[{}] pre stopping {}... Done.", service.type(), name ); | ||
} ); | ||
|
||
BiStream.of( this.supervised ) | ||
.reversed() | ||
.forEach( ( name, service ) -> { | ||
log.debug( "pre stopping {}...", name ); | ||
KernelHelper.setThreadNameSuffix( name ); | ||
try { | ||
service.preStop(); | ||
} finally { | ||
KernelHelper.restoreThreadName(); | ||
} | ||
log.debug( "pre stopping {}... Done.", name ); | ||
} ); | ||
try( ShutdownConfiguration shutdownConfiguration = new ShutdownConfiguration( shutdown ) ) { | ||
log.debug( "pre stopping..." ); | ||
|
||
BiStream.of( this.wrappers ) | ||
.reversed() | ||
.forEach( ( name, service ) -> { | ||
Runnable func = () -> { | ||
log.debug( "[{}] pre stopping {}...", service.type(), name ); | ||
KernelHelper.setThreadNameSuffix( name ); | ||
try { | ||
service.preStop(); | ||
} finally { | ||
KernelHelper.restoreThreadName(); | ||
} | ||
log.debug( "[{}] pre stopping {}... Done.", service.type(), name ); | ||
}; | ||
|
||
runAndDetectTimeout( name, shutdownConfiguration, func ); | ||
} ); | ||
|
||
BiStream.of( this.supervised ) | ||
.reversed() | ||
.forEach( ( name, service ) -> { | ||
Runnable func = () -> { | ||
log.debug( "pre stopping {}...", name ); | ||
KernelHelper.setThreadNameSuffix( name ); | ||
try { | ||
service.preStop(); | ||
} finally { | ||
KernelHelper.restoreThreadName(); | ||
} | ||
log.debug( "pre stopping {}... Done.", name ); | ||
}; | ||
|
||
runAndDetectTimeout( name, shutdownConfiguration, func ); | ||
} ); | ||
} | ||
} | ||
} | ||
|
||
public synchronized void stop() { | ||
public synchronized void stop( ApplicationConfiguration.ModuleShutdown shutdown ) { | ||
if( !stopped ) { | ||
|
||
try( ShutdownConfiguration shutdownConfiguration = new ShutdownConfiguration() ) { | ||
try( ShutdownConfiguration shutdownConfiguration = new ShutdownConfiguration( shutdown ) ) { | ||
log.debug( "stopping..." ); | ||
this.stopped = true; | ||
|
||
|
@@ -237,12 +248,12 @@ public synchronized void stop() { | |
} | ||
} | ||
|
||
public synchronized void stop( String serviceName ) { | ||
public synchronized void stop( String serviceName, ApplicationConfiguration.ModuleShutdown shutdown ) { | ||
if( !stopped ) { | ||
log.debug( "stopping..." ); | ||
this.stopped = true; | ||
|
||
try( ShutdownConfiguration shutdownConfiguration = new ShutdownConfiguration() ) { | ||
try( ShutdownConfiguration shutdownConfiguration = new ShutdownConfiguration( shutdown ) ) { | ||
BiStream.of( this.wrappers ) | ||
.filter( ( name, _ ) -> name.equals( serviceName ) ) | ||
.forEach( ( name, service ) -> { | ||
|
@@ -263,7 +274,7 @@ public synchronized void stop( String serviceName ) { | |
this.wrappers.clear(); | ||
|
||
BiStream.of( this.supervised ) | ||
.filter( ( name, service ) -> name.equals( serviceName ) ) | ||
.filter( ( name, _ ) -> name.equals( serviceName ) ) | ||
.forEach( ( name, service ) -> { | ||
Runnable func = () -> { | ||
log.debug( "stopping {}...", name ); | ||
|
@@ -283,27 +294,30 @@ public synchronized void stop( String serviceName ) { | |
} | ||
} | ||
|
||
public static class ShutdownConfiguration implements Closeable { | ||
private static final Set<String> on = Set.of( "on", "1", "true", "ON", "TRUE", "yes", "YES" ); | ||
public final long timeoutMs; | ||
public final boolean forceAsyncAfterTimeout; | ||
public final ExecutorService threadPoolExecutor = Executors.newCachedThreadPool(); | ||
private static class ShutdownConfiguration implements Closeable { | ||
public final ExecutorService threadPoolExecutor; | ||
private final ApplicationConfiguration.ModuleShutdown shutdown; | ||
|
||
public ShutdownConfiguration() { | ||
String timeoutMsStr = System.getenv( "APPLICATION_STOP_DETECT_TIMEOUT" ); | ||
this.timeoutMs = timeoutMsStr != null ? Long.parseLong( timeoutMsStr ) : Dates.s( 5 ); | ||
private ShutdownConfiguration( ApplicationConfiguration.ModuleShutdown shutdown ) { | ||
this.shutdown = shutdown; | ||
|
||
String forceAsyncAfterTimeoutStr = System.getenv( "APPLICATION_FORCE_ASYNC_AFTER_TIMEOUT" ); | ||
this.forceAsyncAfterTimeout = forceAsyncAfterTimeoutStr != null && on.contains( forceAsyncAfterTimeoutStr ); | ||
threadPoolExecutor = shutdown.serviceTimeout > 0 ? Executors.newCachedThreadPool() : null; | ||
} | ||
|
||
@Override | ||
public void close() { | ||
threadPoolExecutor.shutdown(); | ||
if( threadPoolExecutor != null ) { | ||
threadPoolExecutor.shutdown(); | ||
} | ||
} | ||
|
||
public Future<?> submit( Runnable func ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. jdoc. Submit what for what? |
||
return threadPoolExecutor.submit( func ); | ||
if( threadPoolExecutor != null ) { | ||
return threadPoolExecutor.submit( func ); | ||
} else { | ||
func.run(); | ||
return CompletableFuture.completedFuture( null ); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you please explain more in detail each of
Maybe explain the whole flow and how each of the these will be used.