Search in sources :

Example 1 with ProfilerEventHandler

use of com.mysql.cj.log.ProfilerEventHandler in project JavaSegundasQuintas by ecteruel.

the class NativeProtocol method sendQueryPacket.

/**
 * Send a query stored in a packet to the server.
 *
 * @param <T>
 *            extends {@link Resultset}
 * @param callingQuery
 *            {@link Query}
 * @param queryPacket
 *            {@link NativePacketPayload} containing query
 * @param maxRows
 *            rows limit
 * @param streamResults
 *            whether a stream result should be created
 * @param cachedMetadata
 *            use this metadata instead of the one provided on wire
 * @param resultSetFactory
 *            {@link ProtocolEntityFactory}
 * @return T instance
 * @throws IOException
 *             if an i/o error occurs
 */
public final <T extends Resultset> T sendQueryPacket(Query callingQuery, NativePacketPayload queryPacket, int maxRows, boolean streamResults, ColumnDefinition cachedMetadata, ProtocolEntityFactory<T, NativePacketPayload> resultSetFactory) throws IOException {
    final long queryStartTime = getCurrentTimeNanosOrMillis();
    this.statementExecutionDepth++;
    byte[] queryBuf = queryPacket.getByteBuffer();
    // save the packet position
    int oldPacketPosition = queryPacket.getPosition();
    int queryPosition = queryPacket.getTag("QUERY");
    LazyString query = new LazyString(queryBuf, queryPosition, (oldPacketPosition - queryPosition));
    try {
        if (this.queryInterceptors != null) {
            T interceptedResults = invokeQueryInterceptorsPre(query, callingQuery, false);
            if (interceptedResults != null) {
                return interceptedResults;
            }
        }
        if (this.autoGenerateTestcaseScript) {
            StringBuilder debugBuf = new StringBuilder(query.length() + 32);
            generateQueryCommentBlock(debugBuf);
            debugBuf.append(query);
            debugBuf.append(';');
            TestUtils.dumpTestcaseQuery(debugBuf.toString());
        }
        // Send query command and sql query string
        NativePacketPayload resultPacket = sendCommand(queryPacket, false, 0);
        final long queryEndTime = getCurrentTimeNanosOrMillis();
        final long queryDuration = queryEndTime - queryStartTime;
        if (callingQuery != null) {
            callingQuery.setExecuteTime(queryDuration);
        }
        boolean queryWasSlow = this.logSlowQueries && (this.useAutoSlowLog ? this.metricsHolder.checkAbonormallyLongQuery(queryDuration) : queryDuration > this.propertySet.getIntegerProperty(PropertyKey.slowQueryThresholdMillis).getValue());
        long fetchBeginTime = this.profileSQL ? getCurrentTimeNanosOrMillis() : 0L;
        T rs = readAllResults(maxRows, streamResults, resultPacket, false, cachedMetadata, resultSetFactory);
        if (this.profileSQL || queryWasSlow) {
            long fetchEndTime = this.profileSQL ? getCurrentTimeNanosOrMillis() : 0L;
            // Extract the actual query from the network packet
            boolean truncated = oldPacketPosition - queryPosition > this.maxQuerySizeToLog.getValue();
            int extractPosition = truncated ? this.maxQuerySizeToLog.getValue() + queryPosition : oldPacketPosition;
            String extractedQuery = StringUtils.toString(queryBuf, queryPosition, (extractPosition - queryPosition));
            if (truncated) {
                extractedQuery += Messages.getString("Protocol.2");
            }
            ProfilerEventHandler eventSink = this.session.getProfilerEventHandler();
            if (this.logSlowQueries) {
                if (queryWasSlow) {
                    eventSink.processEvent(ProfilerEvent.TYPE_SLOW_QUERY, this.session, callingQuery, rs, queryDuration, new Throwable(), Messages.getString("Protocol.SlowQuery", new Object[] { this.useAutoSlowLog ? " 95% of all queries " : String.valueOf(this.slowQueryThreshold), this.queryTimingUnits, Long.valueOf(queryDuration), extractedQuery }));
                    if (this.propertySet.getBooleanProperty(PropertyKey.explainSlowQueries).getValue()) {
                        if (oldPacketPosition - queryPosition < MAX_QUERY_SIZE_TO_EXPLAIN) {
                            // skip until the query is located in the packet
                            queryPacket.setPosition(queryPosition);
                            explainSlowQuery(query.toString(), extractedQuery);
                        } else {
                            this.log.logWarn(Messages.getString("Protocol.3", new Object[] { MAX_QUERY_SIZE_TO_EXPLAIN }));
                        }
                    }
                }
                if (this.serverSession.noGoodIndexUsed()) {
                    eventSink.processEvent(ProfilerEvent.TYPE_SLOW_QUERY, this.session, callingQuery, rs, queryDuration, new Throwable(), Messages.getString("Protocol.4") + extractedQuery);
                }
                if (this.serverSession.noIndexUsed()) {
                    eventSink.processEvent(ProfilerEvent.TYPE_SLOW_QUERY, this.session, callingQuery, rs, queryDuration, new Throwable(), Messages.getString("Protocol.5") + extractedQuery);
                }
                if (this.serverSession.queryWasSlow()) {
                    eventSink.processEvent(ProfilerEvent.TYPE_SLOW_QUERY, this.session, callingQuery, rs, queryDuration, new Throwable(), Messages.getString("Protocol.ServerSlowQuery") + extractedQuery);
                }
            }
            if (this.profileSQL) {
                eventSink.processEvent(ProfilerEvent.TYPE_QUERY, this.session, callingQuery, rs, queryDuration, new Throwable(), extractedQuery);
                eventSink.processEvent(ProfilerEvent.TYPE_FETCH, this.session, callingQuery, rs, (fetchEndTime - fetchBeginTime), new Throwable(), null);
            }
        }
        if (this.hadWarnings) {
            scanForAndThrowDataTruncation();
        }
        if (this.queryInterceptors != null) {
            rs = invokeQueryInterceptorsPost(query, callingQuery, rs, false);
        }
        return rs;
    } catch (CJException sqlEx) {
        if (this.queryInterceptors != null) {
            // TODO why doing this?
            // we don't do anything with the result set in this case
            invokeQueryInterceptorsPost(query, callingQuery, null, false);
        }
        if (callingQuery != null) {
            callingQuery.checkCancelTimeout();
        }
        throw sqlEx;
    } finally {
        this.statementExecutionDepth--;
    }
}
Also used : ProfilerEventHandler(com.mysql.cj.log.ProfilerEventHandler) LazyString(com.mysql.cj.util.LazyString) LazyString(com.mysql.cj.util.LazyString) CJException(com.mysql.cj.exceptions.CJException)

Aggregations

CJException (com.mysql.cj.exceptions.CJException)1 ProfilerEventHandler (com.mysql.cj.log.ProfilerEventHandler)1 LazyString (com.mysql.cj.util.LazyString)1