Class SlowQueryReport
- java.lang.Object
-
- org.apache.tomcat.jdbc.pool.JdbcInterceptor
-
- org.apache.tomcat.jdbc.pool.interceptor.AbstractCreateStatementInterceptor
-
- org.apache.tomcat.jdbc.pool.interceptor.AbstractQueryReport
-
- org.apache.tomcat.jdbc.pool.interceptor.SlowQueryReport
-
- All Implemented Interfaces:
java.lang.reflect.InvocationHandler
- Direct Known Subclasses:
SlowQueryReportJmx
public class SlowQueryReport extends AbstractQueryReport
Slow query report interceptor. Tracks timing of query executions.Statistics belong to the pool the interceptor serves: the pool registers a store when it starts, every connection books into it, and the close releases it. The store is read by the name the pool started with. Names must be unique among live pools; a pool starting under a live pool's name is a configuration error and displaces the other pool's store. Registrations hold the pool only weakly, so an abandoned pool is not pinned by its statistics.
A reporting definition added after the pool started registers its store on first borrow. Several reporting definitions may be configured on one pool; the deepest definition that stores statistics books each execution once, while each definition's own slow and failed query logging still fires.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classSlowQueryReport.PoolStatisticsThe query statistics of one pool: the map its connections book into and the name the map is published under.static classSlowQueryReport.QueryStatsHolds timing and invocation statistics for a single SQL query.static classSlowQueryReport.QueryStatsComparatorCompares QueryStats by their lastInvocation value.-
Nested classes/interfaces inherited from class org.apache.tomcat.jdbc.pool.interceptor.AbstractQueryReport
AbstractQueryReport.StatementProxy
-
-
Field Summary
Fields Modifier and Type Field Description protected static java.lang.StringLOG_FAILEDThe name of the property that configuressetLogFailed(boolean).protected static java.lang.StringLOG_SLOWThe name of the property that configuressetLogSlow(boolean).protected booleanlogFailedFlag to enable or disable failed query loggingprotected booleanlogSlowFlag to enable or disable slow query loggingprotected static java.lang.StringMAX_QUERIESThe name of the property that configuressetMaxQueries(int).protected intmaxQueriesMaximum number of queries we will be storing statistics for.protected static java.util.concurrent.ConcurrentHashMap<java.lang.String,SlowQueryReport.PoolStatistics>perPoolStatsThe statistics store published under each pool name.protected SlowQueryReport.PoolStatisticspoolStatisticsThe statistics store this instance books to, resolved from the pool it serves;nullwhen that pool has no store.protected java.util.concurrent.ConcurrentHashMap<java.lang.String,SlowQueryReport.QueryStats>queriesThe statistics map this instance books to: the store of the pool it serves, handed to the chain byreset(ConnectionPool, PooledConnection)at a borrow and taken away when that chain is released.protected java.util.Comparator<SlowQueryReport.QueryStats>queryStatsComparatorSort QueryStats by last invocation timeprotected static java.lang.StringTHRESHOLDThe name of the property that configuresAbstractQueryReport.setThreshold(long).-
Fields inherited from class org.apache.tomcat.jdbc.pool.interceptor.AbstractQueryReport
threshold
-
Fields inherited from class org.apache.tomcat.jdbc.pool.interceptor.AbstractCreateStatementInterceptor
CREATE_STATEMENT, CREATE_STATEMENT_IDX, EXECUTE, EXECUTE_BATCH, EXECUTE_LARGE_BATCH, EXECUTE_LARGE_UPDATE, EXECUTE_QUERY, EXECUTE_TYPES, EXECUTE_UPDATE, GET_GENERATED_KEYS, GET_RESULTSET, GET_STATEMENT, PREPARE_CALL, PREPARE_CALL_IDX, PREPARE_STATEMENT, PREPARE_STATEMENT_IDX, RESULTSET_IDX, STATEMENT_TYPE_COUNT, STATEMENT_TYPES
-
Fields inherited from class org.apache.tomcat.jdbc.pool.JdbcInterceptor
ABORT_VAL, CLOSE_VAL, EQUALS_VAL, GETCONNECTION_VAL, HASHCODE_VAL, ISCLOSED_VAL, ISVALID_VAL, ISWRAPPERFOR_VAL, properties, TOSTRING_VAL, UNWRAP_VAL
-
-
Constructor Summary
Constructors Constructor Description SlowQueryReport()Creates a slow query report interceptor
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected booleanbooksSharedStats()Whether this instance books the shared statistics for its pool.voidcloseInvoked()Invoked when the connection receives the close request.protected static java.lang.BooleanflagValue(java.lang.String value)The flag a configured text states, accepting the word in any case as the parser this reading replaces did.static java.util.concurrent.ConcurrentHashMap<java.lang.String,SlowQueryReport.QueryStats>getPoolStats(java.lang.String poolname)Returns the query statistics published under the given pool name.protected SlowQueryReport.QueryStatsgetQueryStats(java.lang.String sql)Retrieves or creates the statistics entry for the given SQL string in the store this instance books to.protected booleanisKnownProperty(java.lang.String name)Whether this interceptor configuration reads the named property.booleanisLogFailed()Returns whether failed query logging is enabled.booleanisLogSlow()Returns whether slow query logging is enabled.voidpoolClosed(ConnectionPool pool)Releases the statistics store of the given connection pool.voidpoolStarted(ConnectionPool pool)Registers the statistics store of a pool that has started, under the name the pool started with.voidprepareCall(java.lang.String sql, long time)Tracks the preparation time for a callable statement.voidprepareStatement(java.lang.String sql, long time)Tracks the preparation time for a prepared statement.protected static SlowQueryReport.PoolStatisticsreleaseStatistics(ConnectionPool pool)Gives up the statistics store registered for the given pool: the store stops publishing its statistics and is taken out of the registry.protected voidremoveOldest(java.util.concurrent.ConcurrentHashMap<java.lang.String,SlowQueryReport.QueryStats> queries)Evicts the least recently invoked query statistics until the map is back withinmaxQueriesentries.protected java.lang.StringreportFailedQuery(java.lang.String query, java.lang.Object[] args, java.lang.String name, long start, long delta, java.lang.Throwable t)Invoked when a query execution, a call to an execute method as recognised byAbstractCreateStatementInterceptor.isExecute(Method, boolean), failed.protected java.lang.StringreportQuery(java.lang.String query, java.lang.Object[] args, java.lang.String name, long start, long delta)Invoked when a query execution, a call to an execute method as recognised byAbstractCreateStatementInterceptor.isExecute(Method, boolean), succeeded and was within the timing thresholdprotected java.lang.StringreportSlowQuery(java.lang.String query, java.lang.Object[] args, java.lang.String name, long start, long delta)Invoked when a query execution, a call to an execute method as recognised byAbstractCreateStatementInterceptor.isExecute(Method, boolean), succeeded and exceeded the timing thresholdvoidreset(ConnectionPool parent, PooledConnection con)Resolves the statistics store this instance serves, registering its pool's store when this borrow is the first component of that pool to need one.voidsetLogFailed(boolean logFailed)Enables or disables failed query logging.voidsetLogSlow(boolean logSlow)Enables or disables slow query logging.voidsetMaxQueries(int maxQueries)Sets the maximum number of queries to track statistics for.voidsetProperties(java.util.Map<java.lang.String,PoolProperties.InterceptorProperty> properties)Configures this interceptor from the properties of its definition: the threshold, the capacity of the statistics store and the two log flags.protected static SlowQueryReport.PoolStatisticsstatisticsFor(ConnectionPool pool)Returns the statistics store registered for the given pool.-
Methods inherited from class org.apache.tomcat.jdbc.pool.interceptor.AbstractQueryReport
createStatement, getThreshold, setThreshold
-
Methods inherited from class org.apache.tomcat.jdbc.pool.interceptor.AbstractCreateStatementInterceptor
elapsedMillis, getConstructor, invoke, isExecute, isStatement, process
-
Methods inherited from class org.apache.tomcat.jdbc.pool.JdbcInterceptor
compare, compare, disconnected, getNext, getProperties, isUseEquals, setNext, setUseEquals, unregisterMBean
-
-
-
-
Field Detail
-
THRESHOLD
protected static final java.lang.String THRESHOLD
The name of the property that configuresAbstractQueryReport.setThreshold(long). The property names are spelled once each, here:setProperties(java.util.Map)applies them andisKnownProperty(String)reports the names it does not read, and the two must not disagree about the spelling.- See Also:
- Constant Field Values
-
MAX_QUERIES
protected static final java.lang.String MAX_QUERIES
The name of the property that configuressetMaxQueries(int).- See Also:
- Constant Field Values
-
LOG_SLOW
protected static final java.lang.String LOG_SLOW
The name of the property that configuressetLogSlow(boolean).- See Also:
- Constant Field Values
-
LOG_FAILED
protected static final java.lang.String LOG_FAILED
The name of the property that configuressetLogFailed(boolean).- See Also:
- Constant Field Values
-
perPoolStats
protected static final java.util.concurrent.ConcurrentHashMap<java.lang.String,SlowQueryReport.PoolStatistics> perPoolStats
The statistics store published under each pool name. The name identifies the pool - a container names the pools it manages uniquely, and the default name of a programmatically built pool is unique as well - so this one registry resolves both the store a pool's connections book into and the storegetPoolStats(String)andSlowQueryReportJmxMBean.getPoolNames()read.
-
poolStatistics
protected volatile SlowQueryReport.PoolStatistics poolStatistics
The statistics store this instance books to, resolved from the pool it serves;nullwhen that pool has no store. Seereset(ConnectionPool, PooledConnection).
-
queries
protected volatile java.util.concurrent.ConcurrentHashMap<java.lang.String,SlowQueryReport.QueryStats> queries
The statistics map this instance books to: the store of the pool it serves, handed to the chain byreset(ConnectionPool, PooledConnection)at a borrow and taken away when that chain is released.
-
maxQueries
protected int maxQueries
Maximum number of queries we will be storing statistics for. Eviction keeps the number of stored queries near this amount; while queries are being executed concurrently it can briefly be exceeded. This sizes the statistics store only: the slow and failed query logging is configured bysetLogSlow(boolean)andsetLogFailed(boolean)independently of this value.
-
logSlow
protected boolean logSlow
Flag to enable or disable slow query logging
-
logFailed
protected boolean logFailed
Flag to enable or disable failed query logging
-
queryStatsComparator
protected final java.util.Comparator<SlowQueryReport.QueryStats> queryStatsComparator
Sort QueryStats by last invocation time
-
-
Method Detail
-
getPoolStats
public static java.util.concurrent.ConcurrentHashMap<java.lang.String,SlowQueryReport.QueryStats> getPoolStats(java.lang.String poolname)
Returns the query statistics published under the given pool name.The store is published under the name the pool started with and is given up with that pool's close. The one store that outlives the rule is a pool that was abandoned without closing: its bounded statistics stay published under its name until a pool starting under the name replaces it, because the close that would give them up never ran.
- Parameters:
poolname- - the name of the pool we want to retrieve stats for- Returns:
- a hash map containing the statistics of the monitored queries, kept near
maxQueriesentries by eviction, ornullwhen no store is published under that name. Anullname is answered the same way as a name that holds nothing, since nothing can be registered under it. The map is the live store, not a copy: callers must not modify it
-
statisticsFor
protected static SlowQueryReport.PoolStatistics statisticsFor(ConnectionPool pool)
Returns the statistics store registered for the given pool.- Parameters:
pool- the pool whose store is wanted, possiblynull- Returns:
- the store registered for that pool, or
nullwhen the pool has none - because it never registered one, because it has closed, or because it carries no name
-
setMaxQueries
public void setMaxQueries(int maxQueries)
Sets the maximum number of queries to track statistics for.- Parameters:
maxQueries- the maximum number of queries
-
reportFailedQuery
protected java.lang.String reportFailedQuery(java.lang.String query, java.lang.Object[] args, java.lang.String name, long start, long delta, java.lang.Throwable t)Description copied from class:AbstractQueryReportInvoked when a query execution, a call to an execute method as recognised byAbstractCreateStatementInterceptor.isExecute(Method, boolean), failed.- Overrides:
reportFailedQueryin classAbstractQueryReport- Parameters:
query- the query that was executed and failedargs- the arguments to the executionname- the name of the method used to execute, as recognised byAbstractCreateStatementInterceptor.isExecute(Method, boolean)start- the wall clock time the query execution starteddelta- the time the execution took before it failed, measured with the monotonic clockt- the exception that happened- Returns:
- - the SQL that was executed or the string "batch" if it was a batch execution, or
nullif the invocation carries no SQL string
-
reportQuery
protected java.lang.String reportQuery(java.lang.String query, java.lang.Object[] args, java.lang.String name, long start, long delta)Description copied from class:AbstractQueryReportInvoked when a query execution, a call to an execute method as recognised byAbstractCreateStatementInterceptor.isExecute(Method, boolean), succeeded and was within the timing threshold- Overrides:
reportQueryin classAbstractQueryReport- Parameters:
query- the query that was executedargs- the arguments to the executionname- the name of the method used to execute, as recognised byAbstractCreateStatementInterceptor.isExecute(Method, boolean)start- the wall clock time the query execution starteddelta- the time the execution took, measured with the monotonic clock- Returns:
- - the SQL that was executed or the string "batch" if it was a batch execution, or
nullif the invocation carries no SQL string
-
reportSlowQuery
protected java.lang.String reportSlowQuery(java.lang.String query, java.lang.Object[] args, java.lang.String name, long start, long delta)Description copied from class:AbstractQueryReportInvoked when a query execution, a call to an execute method as recognised byAbstractCreateStatementInterceptor.isExecute(Method, boolean), succeeded and exceeded the timing threshold- Overrides:
reportSlowQueryin classAbstractQueryReport- Parameters:
query- the query that was executedargs- the arguments to the executionname- the name of the method used to execute, as recognised byAbstractCreateStatementInterceptor.isExecute(Method, boolean)start- the wall clock time the query execution starteddelta- the time the execution took, measured with the monotonic clock- Returns:
- - the SQL that was executed or the string "batch" if it was a batch execution, or
nullif the invocation carries no SQL string
-
closeInvoked
public void closeInvoked()
Invoked when the connection receives the close request. Not used for now.- Specified by:
closeInvokedin classAbstractCreateStatementInterceptor
-
prepareStatement
public void prepareStatement(java.lang.String sql, long time)Tracks the preparation time for a prepared statement.- Specified by:
prepareStatementin classAbstractQueryReport- Parameters:
sql- the SQL statementtime- the time taken to prepare the statement
-
prepareCall
public void prepareCall(java.lang.String sql, long time)Tracks the preparation time for a callable statement.- Specified by:
prepareCallin classAbstractQueryReport- Parameters:
sql- the SQL calltime- the time taken to prepare the call
-
poolStarted
public void poolStarted(ConnectionPool pool)
Registers the statistics store of a pool that has started, under the name the pool started with. A start always begins a fresh store rather than inheriting numbers from an abandoned pool's leftover.- Overrides:
poolStartedin classJdbcInterceptor- Parameters:
pool- the connection pool that has started- Throws:
java.lang.IllegalStateException- if the pool carries no name to publish under
-
poolClosed
public void poolClosed(ConnectionPool pool)
Releases the statistics store of the given connection pool.The callback is delivered by
ConnectionPoolto a freshly instantiated interceptor (seeConnectionPool#close(boolean)), which has never held a reference to the statistics, so the store to release can only be identified through the closing pool passed here, not through this instance's own state. The release is idempotent, and every definition of the closing pool's chain can run it.- Overrides:
poolClosedin classJdbcInterceptor- Parameters:
pool- the connection pool that has been closed
-
releaseStatistics
protected static SlowQueryReport.PoolStatistics releaseStatistics(ConnectionPool pool)
Gives up the statistics store registered for the given pool: the store stops publishing its statistics and is taken out of the registry.- Parameters:
pool- the connection pool that has been closed- Returns:
- the store that was registered for the pool, or
nullif it had registered none
-
booksSharedStats
protected boolean booksSharedStats()
Whether this instance books the shared statistics for its pool. Every reporting definition sees every execution, so only the deepest definition that stores statistics books; a definition with no capacity never claims the booking. The decision reads the fixed chain below this instance and nothing is remembered across executions.- Returns:
trueif this instance must book the statistics of its pool for the executions it reports
-
getQueryStats
protected SlowQueryReport.QueryStats getQueryStats(java.lang.String sql)
Retrieves or creates the statistics entry for the given SQL string in the store this instance books to.- Parameters:
sql- the SQL query string- Returns:
- the query statistics, or
nullwhen this instance has no store to book to - the session it serves outlived the pool it was borrowed from, the chain it belongs to has been released, or its pool carries no name to publish statistics under
-
removeOldest
protected void removeOldest(java.util.concurrent.ConcurrentHashMap<java.lang.String,SlowQueryReport.QueryStats> queries)
Evicts the least recently invoked query statistics until the map is back withinmaxQueriesentries. The map is shared by all the connections of the pool, so the eviction runs concurrently with the inserts and removals of the other borrower threads and can only do as much work as its own snapshot allows; it then leaves the map above capacity for the next new query to evict further.- Parameters:
queries- The queries map
-
reset
public void reset(ConnectionPool parent, PooledConnection con)
Resolves the statistics store this instance serves, registering its pool's store when this borrow is the first component of that pool to need one.- Overrides:
resetin classAbstractCreateStatementInterceptor- Parameters:
parent- the connection poolcon- the pooled connection being reset
-
isLogSlow
public boolean isLogSlow()
Returns whether slow query logging is enabled.- Returns:
trueif slow query logging is enabled
-
setLogSlow
public void setLogSlow(boolean logSlow)
Enables or disables slow query logging.- Parameters:
logSlow-trueto enable slow query logging
-
isLogFailed
public boolean isLogFailed()
Returns whether failed query logging is enabled.- Returns:
trueif failed query logging is enabled
-
setLogFailed
public void setLogFailed(boolean logFailed)
Enables or disables failed query logging.- Parameters:
logFailed-trueto enable failed query logging
-
setProperties
public void setProperties(java.util.Map<java.lang.String,PoolProperties.InterceptorProperty> properties)
Configures this interceptor from the properties of its definition: the threshold, the capacity of the statistics store and the two log flags.The values arrive as text, and text is where typos live, so a value this interceptor cannot read is reported and left out rather than read as something else: the setting keeps the value it already carries, which for the log flags means the channel the operator meant to configure is not switched off by the typo, and for the numbers means the default threshold or capacity stays in force. A name this configuration does not read is reported the same way. The pool configures an interceptor chain per physical connection, so one misconfiguration is reported once per configuration of the bad definition rather than once per JVM.
- Overrides:
setPropertiesin classJdbcInterceptor- Parameters:
properties- the interceptor properties
-
isKnownProperty
protected boolean isKnownProperty(java.lang.String name)
Whether this interceptor configuration reads the named property.useEqualsbelongs to theJdbcInterceptorbase and is reported as known beside this class's own names, since the point of the report is a mistyped name rather than a configuration that sets more than one thing. A subclass that reads properties of its own - asSlowQueryReportJmxdoes - recognises them in its own override, so a correct configuration of the subclass is never reported as a typo.- Parameters:
name- the property name to test- Returns:
trueif this configuration applies that name
-
flagValue
protected static java.lang.Boolean flagValue(java.lang.String value)
The flag a configured text states, accepting the word in any case as the parser this reading replaces did.- Parameters:
value- the configured text- Returns:
- the stated flag, or
nullwhen the text states neither true nor false and is therefore a value to report rather than to read as false
-
-