net.sf.sasl.aop.monitoring.execution.time
Class ThresholdMethodExecutionTimeWarnAspect

java.lang.Object
  extended by net.sf.sasl.aop.AbstractRootAspect
      extended by net.sf.sasl.aop.AbstractPlaceholderScriptUsingAspect
          extended by net.sf.sasl.aop.monitoring.execution.time.ThresholdMethodExecutionTimeWarnAspect
All Implemented Interfaces:
org.aopalliance.aop.Advice, org.aopalliance.intercept.Interceptor, org.aopalliance.intercept.MethodInterceptor, org.springframework.beans.factory.InitializingBean, org.springframework.core.Ordered, org.springframework.core.PriorityOrdered

public class ThresholdMethodExecutionTimeWarnAspect
extends AbstractPlaceholderScriptUsingAspect
implements org.aopalliance.intercept.MethodInterceptor, org.springframework.beans.factory.InitializingBean

The task that this aspect addresses is to send configurable warn and recovery messages over configurable communication channels (mail, irc, instant messenger, ...), if the execution time of a target method exceeds a given threshold value. To reduce flooding of warn messages if a system is temporarily at the limit for some operation, it could be also configured that the aspects sends a warn message only again for a (target class, target method) pair after a given time has gone. This feature is reduced by the maximum number of elements the cache can store.

The aspect could be used to monitor the execution behavior of own services, for example to answer the following questions:

Vice versa it could be also used to monitor services of other domains than the own: This aspect should help to get some fast hints why something is not running as expected. Here focused at the execution time as an indicator. Studying logs can take a high time and effort until something is identified. But fast decisions are sometimes needed.
The worse thing of logs is also that they only get observed if something is totally broken. The aspect maybe notifies accurately timed before a worse state arrives totally or prepares somebody for the arriving damage in the future.
As of deep problems of concurrency (who gets when the time slice of the cpu), the aspect can not 100% guarantee that a warn message will not be send, although the execution time of a target method was lesser than the defined threshold value. But if the system is healthy and threshold values were not too close to the average executions times of the targets methods choosen, the probability should be very small for such a case.
Warn and recovery messages could be formatted as a placeholder script.
The following is an example for a warn message placeholder script:
Subject: [%canonicalHostName]WARN Operation is slow
Message:
Warning, execution time is slow.
Threshold value of %duration{'mm:ss.SSS', %iEnv{'thresholdMilliseconds'}} exceeded.
Check the logs for detail informations what is going on.

***Problem description***
Host: %hostname
Date: %date
Operation: %targetClassName->%targetMethodName(%targetMethodParameterTypes) : %targetMethodReturnType
Thread-Id: %iEnv{'blockerThreadId'}
Thread-Name: %iEnv{'blockerThreadName'}

***VM stats***
Free-Memory: %freeMemory{'MB'} MB
Total-Memory: %totalMemory{'MB'} MB
Max-Memory: %maxMemory{'MB'} MB
Total-Threads: %totalThreadCount

Greetings,
%hostname

After interpreted it will than look like the following for the receivers:
Subject: [node03.example.de]WARN Operation is slow
Message:
Warning, execution time is slow.
Threshold value of 01:00.000 exceeded.
Check the logs for detail informations what is going on.

***Problem description***
Host: node03
Date: 2010-03-24 02:02:02.222
Operation: WeatherService->getWeather(java.util.Date when) : WeatherResponseDTO
Thread-Id: 122
Thread-Name: Worker-Thread-122

***VM stats***
Free-Memory: 82.001 MB
Total-Memory: 4000.0 MB
Max-Memory: 4000.0 MB
Total-Threads: 500

Greetings,
node03


The aspect offers four special environment variables to the placeholder script interpreter, they can be accessed via %iEnv{'variable name'}:
thresholdMilliseconds: Long, holding the defined threshold value before a warn message will be send.
blockerThreadId: Long, holding the thread id of the thread which executes the operation thats execution time is slow.
blockerThreadName: String, holding the thread name of the thread which executes the operation thats execution time is slow.
totalRuntimeMilliseconds: Long, holding the total runtime of the slow operation. If used in warn messages, the value will be 0.

Since:
0.0.1 (sasl-common-aspect-library)
Author:
Philipp Förmer
See Also:
net.sf.sasl.aop.common.grammar.placeholder.resolver

Nested Class Summary
protected  class ThresholdMethodExecutionTimeWarnAspect.MessageSender
          Helper class to interpret and deliver warn and recovery messages concurrently.
protected  class ThresholdMethodExecutionTimeWarnAspect.SendRecoveryMessageMarker
          Small helper class for the concurrent synchronization between the warn and recovery message sending threads.
 
Field Summary
 
Fields inherited from class net.sf.sasl.aop.AbstractPlaceholderScriptUsingAspect
placeholderSymbolTable
 
Fields inherited from class net.sf.sasl.aop.AbstractRootAspect
logger
 
Fields inherited from interface org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
 
Constructor Summary
ThresholdMethodExecutionTimeWarnAspect()
           
 
Method Summary
 void afterPropertiesSet()
           
 net.sf.ehcache.Ehcache getCache()
           
 List<ICommunicationChannel> getCommunicationChannels()
           
 List<MethodExecutionTimeThresholdBean> getMethodExecutionTimeThresholdChain()
           
 IScheduleService getScheduleService()
           
 PlaceholderScriptMessage getScriptedRecoveryMessage()
           
 PlaceholderScriptMessage getScriptedWarnMessage()
           
 Object invoke(org.aopalliance.intercept.MethodInvocation invocation)
          Please see the documentation header of the class for more informations what this method does in whole.
protected  MethodExecutionTimeThresholdBean lookupThresholdConfiguration(org.aopalliance.intercept.MethodInvocation invocation)
          Returns the first threshold configuration of the the threshold configuration chain list that matches for the (targetObject, targetMethod, targetArguments) triple.
protected static String makeCacheKey(Object targetObject, Method targetMethod)
          Creates a cache key for a (targetObject, targetMethod) pair.
 void setCache(net.sf.ehcache.Ehcache cache)
          Sets the cache which will be used to decide if a warn message should be send for a slow operation, if a flooding restriction was set via #setWaitTimeBeforeSendingNewWarnMessage(TimeUnitsBean).
 void setCommunicationChannels(List<ICommunicationChannel> communicationChannels)
          Sets a list of communication channels to which warn and recovery messages will be delivered.
 void setMethodExecutionTimeThresholdChain(List<MethodExecutionTimeThresholdBean> methodExecutionTimeThresholdChain)
          Sets a chain list of MethodExecutionTimeThresholdBeans.
 void setRecoveryMessage(MessageBean recoveryMessage)
          Sets the recovery message which will be send after a target method finished its execution and a warn message was sent for it.
 void setScheduleService(IScheduleService scheduleService)
          Sets a schedule service which will be used for operations that can run concurrent like sending warn and recovery messages, so that the execution of the targetMethod gets not deferred.
 void setWarnMessage(MessageBean warnMessage)
          Sets the warn message which will be send, if the execution time of a target method is over the defined threshold time.
 
Methods inherited from class net.sf.sasl.aop.AbstractPlaceholderScriptUsingAspect
getPlaceholderSymbolTable, runInterpreter, setPlaceholderResolverList
 
Methods inherited from class net.sf.sasl.aop.AbstractRootAspect
getLogger, getOrder, setLogger, setLoggerByName, setOrder
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ThresholdMethodExecutionTimeWarnAspect

public ThresholdMethodExecutionTimeWarnAspect()
Method Detail

invoke

public Object invoke(org.aopalliance.intercept.MethodInvocation invocation)
              throws Throwable
Please see the documentation header of the class for more informations what this method does in whole.

Specified by:
invoke in interface org.aopalliance.intercept.MethodInterceptor
Throws:
Throwable
Since:
0.0.1 (sasl-common-aspect-library)
See Also:
MethodInterceptor.invoke(org.aopalliance.intercept.MethodInvocation)

lookupThresholdConfiguration

protected MethodExecutionTimeThresholdBean lookupThresholdConfiguration(org.aopalliance.intercept.MethodInvocation invocation)
Returns the first threshold configuration of the the threshold configuration chain list that matches for the (targetObject, targetMethod, targetArguments) triple. If no configuration is found, null will be returned.

Parameters:
invocation - non null
Returns:
null or non null
Since:
0.0.1 (sasl-common-aspect-library)

getMethodExecutionTimeThresholdChain

public List<MethodExecutionTimeThresholdBean> getMethodExecutionTimeThresholdChain()
Returns:
non null
Since:
0.0.1 (sasl-common-aspect-library)
See Also:
setMethodExecutionTimeThresholdChain(List)

setMethodExecutionTimeThresholdChain

public void setMethodExecutionTimeThresholdChain(List<MethodExecutionTimeThresholdBean> methodExecutionTimeThresholdChain)
                                          throws IllegalArgumentException
Sets a chain list of MethodExecutionTimeThresholdBeans. The beans define the threshold execution time value of a execution triple (targetObject, targetMethod, targetArguments). If list element N matches for a execution triple than the following configurations will not get considered. So a default rule (for example * match rule) for the threshold execution time should be the last element of the list.

Parameters:
methodExecutionTimeThresholdChain - non null
Throws:
IllegalArgumentException - if methodExecutionTimeThresholdChain is null or there exists a bean in the list that has got a a total milliseconds time value lesser than 0.
Since:
0.0.1 (sasl-common-aspect-library)

getScheduleService

public IScheduleService getScheduleService()
Returns:
null or non null
Since:
0.0.1 (sasl-common-aspect-library)
See Also:
setScheduleService(IScheduleService)

setScheduleService

public void setScheduleService(IScheduleService scheduleService)
                        throws IllegalArgumentException
Sets a schedule service which will be used for operations that can run concurrent like sending warn and recovery messages, so that the execution of the targetMethod gets not deferred. Depending of the used communication channels, the schedule service should have has much worker threads to compensate low round trip times of the channels.

Parameters:
scheduleService - non null
Throws:
IllegalArgumentException - if scheduleService is null.
Since:
0.0.1 (sasl-common-aspect-library)

getCommunicationChannels

public List<ICommunicationChannel> getCommunicationChannels()
Returns:
null or non null
Since:
0.0.1 (sasl-common-aspect-library)
See Also:
setCommunicationChannels(List)

setCommunicationChannels

public void setCommunicationChannels(List<ICommunicationChannel> communicationChannels)
                              throws IllegalArgumentException
Sets a list of communication channels to which warn and recovery messages will be delivered. Such a channel could be for example a mail channel.

Parameters:
communicationChannels - non null
Throws:
IllegalArgumentException - if communicationChannels is null.
Since:
0.0.1 (sasl-common-aspect-library)

getScriptedWarnMessage

public PlaceholderScriptMessage getScriptedWarnMessage()
Returns:
null or non null
Since:
0.0.1 (sasl-common-aspect-library)
See Also:
setWarnMessage(MessageBean)

setWarnMessage

public void setWarnMessage(MessageBean warnMessage)
                    throws IllegalArgumentException,
                           ParseException
Sets the warn message which will be send, if the execution time of a target method is over the defined threshold time. Subject and message text of the message should be valid placeholder scripts.

Parameters:
warnMessage - non null
Throws:
ParseException - if a parse error occurs during parsing the subject and message of the message bean.
IllegalArgumentException - if warnMessage is null.
Since:
0.0.1 (sasl-common-aspect-library)

getScriptedRecoveryMessage

public PlaceholderScriptMessage getScriptedRecoveryMessage()
Returns:
null or non null
Since:
0.0.1 (sasl-common-aspect-library)
See Also:
setRecoveryMessage(MessageBean)

setRecoveryMessage

public void setRecoveryMessage(MessageBean recoveryMessage)
                        throws IllegalArgumentException,
                               ParseException
Sets the recovery message which will be send after a target method finished its execution and a warn message was sent for it. Subject and message text of the message can be placeholder scripts.

Parameters:
recoveryMessage - non null.
Throws:
IllegalArgumentException - if recoveryMessage is null.
ParseException - if a parse error occurs during parsing the subject and message of the message bean.
Since:
0.0.1 (sasl-common-aspect-library)

getCache

public net.sf.ehcache.Ehcache getCache()
Returns:
null or non null
Since:
0.0.1 (sasl-common-aspect-library)
See Also:
#setCache(Cache)

setCache

public void setCache(net.sf.ehcache.Ehcache cache)
Sets the cache which will be used to decide if a warn message should be send for a slow operation, if a flooding restriction was set via #setWaitTimeBeforeSendingNewWarnMessage(TimeUnitsBean).

Parameters:
cache - null or non null
Since:
0.0.1 (sasl-common-aspect-library)

afterPropertiesSet

public void afterPropertiesSet()
                        throws Exception
Specified by:
afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
Throws:
IllegalStateException - if there exists a MethodExecutionTimeThresholdBean in the chain list that have a non zero waitTimeBeforeNewWarnMessageSeconds value but no cache was set.
Exception
Since:
0.0.1 (sasl-common-aspect-library)
See Also:
InitializingBean.afterPropertiesSet()

makeCacheKey

protected static String makeCacheKey(Object targetObject,
                                     Method targetMethod)
Creates a cache key for a (targetObject, targetMethod) pair.

Returns:
non null
Since:
0.0.1 (sasl-common-aspect-library)


Copyright © 2010. All Rights Reserved.