Class SlowQueryReport
- All Implemented Interfaces:
InvocationHandler
- Direct Known Subclasses:
SlowQueryReportJmx
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 ClassesModifier and TypeClassDescriptionprotected static classThe query statistics of one pool: the map its connections book into and the name the map is published under.static classHolds timing and invocation statistics for a single SQL query.static classCompares QueryStats by their lastInvocation value.Nested classes/interfaces inherited from class AbstractQueryReport
AbstractQueryReport.StatementProxy -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final StringThe name of the property that configuressetLogFailed(boolean).protected static final StringThe name of the property that configuressetLogSlow(boolean).protected booleanFlag to enable or disable failed query loggingprotected booleanFlag to enable or disable slow query loggingprotected static final StringThe name of the property that configuressetMaxQueries(int).protected intMaximum number of queries we will be storing statistics for.protected static final ConcurrentHashMap<String, SlowQueryReport.PoolStatistics> The statistics store published under each pool name.protected SlowQueryReport.PoolStatisticsThe statistics store this instance books to, resolved from the pool it serves;nullwhen that pool has no store.protected ConcurrentHashMap<String, SlowQueryReport.QueryStats> 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.protected final Comparator<SlowQueryReport.QueryStats> Sort QueryStats by last invocation timeprotected static final StringThe name of the property that configuresAbstractQueryReport.setThreshold(long).Fields inherited from class AbstractQueryReport
thresholdFields inherited from class 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_TYPESFields inherited from class 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 -
Method Summary
Modifier and TypeMethodDescriptionprotected booleanWhether this instance books the shared statistics for its pool.voidInvoked when the connection receives the close request.protected static BooleanThe flag a configured text states, accepting the word in any case as the parser this reading replaces did.getPoolStats(String poolname) Returns the query statistics published under the given pool name.protected SlowQueryReport.QueryStatsgetQueryStats(String sql) Retrieves or creates the statistics entry for the given SQL string in the store this instance books to.protected booleanisKnownProperty(String name) Whether this interceptor configuration reads the named property.booleanReturns whether failed query logging is enabled.booleanReturns 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(String sql, long time) Tracks the preparation time for a callable statement.voidprepareStatement(String sql, long time) Tracks the preparation time for a prepared statement.protected static SlowQueryReport.PoolStatisticsGives up the statistics store registered for the given pool: the store stops publishing its statistics and is taken out of the registry.protected voidEvicts the least recently invoked query statistics until the map is back withinmaxQueriesentries.protected StringreportFailedQuery(String query, Object[] args, String name, long start, long delta, Throwable t) Invoked when a query execution, a call to an execute method as recognised byAbstractCreateStatementInterceptor.isExecute(Method, boolean), failed.protected StringreportQuery(String query, Object[] args, 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 StringreportSlowQuery(String query, Object[] args, 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(Map<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 AbstractQueryReport
createStatement, getThreshold, setThresholdMethods inherited from class AbstractCreateStatementInterceptor
elapsedMillis, getConstructor, invoke, isExecute, isStatement, processMethods inherited from class JdbcInterceptor
compare, compare, disconnected, getNext, getProperties, isUseEquals, setNext, setUseEquals, unregisterMBean
-
Field Details
-
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:
-
MAX_QUERIES
The name of the property that configuressetMaxQueries(int).- See Also:
-
LOG_SLOW
The name of the property that configuressetLogSlow(boolean).- See Also:
-
LOG_FAILED
The name of the property that configuressetLogFailed(boolean).- See Also:
-
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
The statistics store this instance books to, resolved from the pool it serves;nullwhen that pool has no store. Seereset(ConnectionPool, PooledConnection). -
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 maxQueriesMaximum 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 logSlowFlag to enable or disable slow query logging -
logFailed
protected boolean logFailedFlag to enable or disable failed query logging -
queryStatsComparator
Sort QueryStats by last invocation time
-
-
Constructor Details
-
SlowQueryReport
public SlowQueryReport()Creates a slow query report interceptor
-
-
Method Details
-
getPoolStats
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
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 String reportFailedQuery(String query, Object[] args, String name, long start, long delta, 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
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
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
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
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
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:
IllegalStateException- if the pool carries no name to publish under
-
poolClosed
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
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
-
getQueryStats
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
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
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
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
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
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
-