Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package com.facebook.presto.event.query;
 
 
 
 import java.util.List;
 import java.util.Map;
 
 import static com.google.common.base.Preconditions.checkNotNull;
 import static java.util.concurrent.TimeUnit.MILLISECONDS;
 
 public class QueryMonitor
 {
     private static final Logger log = Logger.get(QueryMonitor.class);
 
     private final ObjectMapper objectMapper;
     private final EventClient eventClient;
     private final String environment;
     private final String serverVersion;
 
     @Inject
     public QueryMonitor(ObjectMapper objectMapperEventClient eventClientNodeInfo nodeInfoNodeVersion nodeVersion)
     {
         this. = checkNotNull(objectMapper"objectMapper is null");
         this. = checkNotNull(eventClient"eventClient is null");
         this. = checkNotNull(nodeInfo"nodeInfo is null").getEnvironment();
         this. = checkNotNull(nodeVersion"nodeVersion is null").toString();
     }
 
     public void createdEvent(QueryInfo queryInfo)
     {
         .post(
                 new QueryCreatedEvent(
                         queryInfo.getQueryId(),
                         queryInfo.getSession().getUser(),
                         queryInfo.getSession().getSource(),
                         ,
                         ,
                         queryInfo.getSession().getCatalog(),
                         queryInfo.getSession().getSchema(),
                         queryInfo.getSession().getRemoteUserAddress(),
                         queryInfo.getSession().getUserAgent(),
                         queryInfo.getSelf(),
                         queryInfo.getQuery(),
                         queryInfo.getQueryStats().getCreateTime()
                 )
         );
     }
 
     public void completionEvent(QueryInfo queryInfo)
     {
         try {
             QueryStats queryStats = queryInfo.getQueryStats();
             FailureInfo failureInfo = queryInfo.getFailureInfo();
 
             String failureType = failureInfo == null ? null : failureInfo.getType();
             String failureMessage = failureInfo == null ? null : failureInfo.getMessage();
 
             ImmutableMap.Builder<StringStringmergedProperties = ImmutableMap.builder();
             mergedProperties.putAll(queryInfo.getSession().getSystemProperties());
             for (Map.Entry<StringMap<StringString>> catalogEntry : queryInfo.getSession().getCatalogProperties().entrySet()) {
                 for (Map.Entry<StringStringentry : catalogEntry.getValue().entrySet()) {
                     mergedProperties.put(catalogEntry.getKey() + "." + entry.getKey(), entry.getValue());
                 }
             }
 
             .post(
                    new QueryCompletionEvent(
                            queryInfo.getQueryId(),
                            queryInfo.getSession().getUser(),
                            queryInfo.getSession().getSource(),
                            ,
                            ,
                            queryInfo.getSession().getCatalog(),
                            queryInfo.getSession().getSchema(),
                            queryInfo.getSession().getRemoteUserAddress(),
                            queryInfo.getSession().getUserAgent(),
                            queryInfo.getState(),
                            queryInfo.getSelf(),
                            queryInfo.getFieldNames(),
                            queryInfo.getQuery(),
                            queryStats.getCreateTime(),
                            queryStats.getExecutionStartTime(),
                            queryStats.getEndTime(),
                            queryStats.getQueuedTime(),
                            queryStats.getAnalysisTime(),
                            queryStats.getDistributedPlanningTime(),
                            queryStats.getTotalScheduledTime(),
                            queryStats.getTotalCpuTime(),
                            queryStats.getRawInputDataSize(),
                            queryStats.getRawInputPositions(),
                            queryStats.getTotalDrivers(),
                            queryInfo.getErrorCode(),
                            failureType,
                            failureMessage,
                            .writeValueAsString(queryInfo.getOutputStage()),
                            .writeValueAsString(queryInfo.getFailureInfo()),
                            .writeValueAsString(queryInfo.getInputs()),
                            .writeValueAsString(mergedProperties.build())
                    )
            );
            logQueryTimeline(queryInfo);
        }
        catch (JsonProcessingException e) {
            throw Throwables.propagate(e);
        }
    }
    private void logQueryTimeline(QueryInfo queryInfo)
    {
        try {
            QueryStats queryStats = queryInfo.getQueryStats();
            DateTime queryStartTime = queryStats.getCreateTime();
            DateTime queryEndTime = queryStats.getEndTime();
            // query didn't finish cleanly
            if (queryStartTime == null || queryEndTime == null) {
                return;
            }
            // planning duration -- start to end of planning
            Duration planning = queryStats.getTotalPlanningTime();
            if (planning == null) {
                planning = new Duration(0, );
            }
            List<StageInfostages = StageInfo.getAllStages(queryInfo.getOutputStage());
            // long lastSchedulingCompletion = 0;
            long firstTaskStartTime = queryEndTime.getMillis();
            long lastTaskStartTime = queryStartTime.getMillis() + planning.toMillis();
            long lastTaskEndTime = queryStartTime.getMillis() + planning.toMillis();
            for (StageInfo stage : stages) {
                // only consider leaf stages
                if (!stage.getSubStages().isEmpty()) {
                    continue;
                }
                for (TaskInfo taskInfo : stage.getTasks()) {
                    TaskStats taskStats = taskInfo.getStats();
                    DateTime firstStartTime = taskStats.getFirstStartTime();
                    if (firstStartTime != null) {
                        firstTaskStartTime = Math.min(firstStartTime.getMillis(), firstTaskStartTime);
                    }
                    DateTime lastStartTime = taskStats.getLastStartTime();
                    if (lastStartTime != null) {
                        lastTaskStartTime = Math.max(lastStartTime.getMillis(), lastTaskStartTime);
                    }
                    DateTime endTime = taskStats.getEndTime();
                    if (endTime != null) {
                        lastTaskEndTime = Math.max(endTime.getMillis(), lastTaskEndTime);
                    }
                }
            }
            Duration elapsed = millis(queryEndTime.getMillis() - queryStartTime.getMillis());
            Duration scheduling = millis(firstTaskStartTime - queryStartTime.getMillis() - planning.toMillis());
            Duration running = millis(lastTaskEndTime - firstTaskStartTime);
            Duration finishing = millis(queryEndTime.getMillis() - lastTaskEndTime);
            .info("TIMELINE: Query %s :: elapsed %s :: planning %s :: scheduling %s :: running %s :: finishing %s :: begin %s :: end %s",
                    queryInfo.getQueryId(),
                    elapsed,
                    planning,
                    scheduling,
                    running,
                    finishing,
                    queryStartTime,
                    queryEndTime
            );
        }
        catch (Exception e) {
            .error(e"Error logging query timeline");
        }
    }
    public void splitCompletionEvent(TaskId taskIdDriverStats driverStats)
    {
        splitCompletionEvent(taskIddriverStatsnullnull);
    }
    public void splitFailedEvent(TaskId taskIdDriverStats driverStatsThrowable cause)
    {
        splitCompletionEvent(taskIddriverStatscause.getClass().getName(), cause.getMessage());
    }
    private void splitCompletionEvent(TaskId taskIdDriverStats driverStats, @Nullable String failureType, @Nullable String failureMessage)
    {
        Duration timeToStart = null;
        if (driverStats.getStartTime() != null) {
            timeToStart = millis(driverStats.getStartTime().getMillis() - driverStats.getCreateTime().getMillis());
        }
        Duration timeToEnd = null;
        if (driverStats.getEndTime() != null) {
            timeToEnd = millis(driverStats.getEndTime().getMillis() - driverStats.getCreateTime().getMillis());
        }
        try {
            .post(
                    new SplitCompletionEvent(
                            taskId.getQueryId(),
                            taskId.getStageId(),
                            taskId,
                            ,
                            driverStats.getQueuedTime(),
                            driverStats.getStartTime(),
                            timeToStart,
                            timeToEnd,
                            driverStats.getRawInputDataSize(),
                            driverStats.getRawInputPositions(),
                            driverStats.getRawInputReadTime(),
                            driverStats.getElapsedTime(),
                            driverStats.getTotalCpuTime(),
                            driverStats.getTotalUserTime(),
                            failureType,
                            failureMessage,
                            .writeValueAsString(driverStats)
                    )
            );
        }
        catch (JsonProcessingException e) {
            .error(e"Error posting split completion event for task %s"taskId);
        }
    }
    private static Duration millis(long millis)
    {
        if (millis < 0) {
            millis = 0;
        }
        return new Duration(millis);
    }
New to GrepCode? Check out our FAQ X