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.execution;
 
 
 
 import java.net.URI;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 import static com.facebook.presto.execution.QueryState.FAILED;
 import static com.facebook.presto.execution.QueryState.FINISHED;
 import static com.facebook.presto.execution.StageInfo.getAllStages;
 import static com.facebook.presto.util.Failures.toFailure;
 import static com.google.common.base.Preconditions.checkNotNull;
 import static io.airlift.units.DataSize.Unit.BYTE;
 import static java.util.concurrent.TimeUnit.MILLISECONDS;
 import static java.util.concurrent.TimeUnit.NANOSECONDS;
 
 public class QueryStateMachine
 {
     private static final Logger log = Logger.get(QueryStateMachine.class);
 
     private final DateTime createTime = DateTime.now();
     private final long createNanos = System.nanoTime();
 
     private final QueryId queryId;
     private final String query;
     private final Session session;
     private final URI self;
 
     @GuardedBy("this")
     private DateTime lastHeartbeat = DateTime.now();
     @GuardedBy("this")
     private DateTime executionStartTime;
     @GuardedBy("this")
     private DateTime endTime;
 
     @GuardedBy("this")
     private Duration queuedTime;
     @GuardedBy("this")
     private Duration analysisTime;
     @GuardedBy("this")
     private Duration distributedPlanningTime;
 
     @GuardedBy("this")
     private Duration totalPlanningTime;
 
     private final StateMachine<QueryStatequeryState;
 
     @GuardedBy("this")
     private final Map<StringStringsetSessionProperties = new LinkedHashMap<>();
 
     @GuardedBy("this")
     private final Set<StringresetSessionProperties = new LinkedHashSet<>();
 
     @GuardedBy("this")
     private String updateType;
 
     @GuardedBy("this")
     private Throwable failureCause;
 
     @GuardedBy("this")
     private List<StringoutputFieldNames = ImmutableList.of();
    @GuardedBy("this")
    private Set<Inputinputs = ImmutableSet.of();
    public QueryStateMachine(QueryId queryIdString querySession sessionURI selfExecutor executor)
    {
        this. = checkNotNull(queryId"queryId is null");
        this. = checkNotNull(query"query is null");
        this. = checkNotNull(session"session is null");
        this. = checkNotNull(self"self is null");
        this. = new StateMachine<>("query " + queryexecutor.);
        {
            @Override
            public void stateChanged(QueryState newValue)
            {
                .debug("Query %s is %s"QueryStateMachine.this.newValue);
            }
        });
    }
    public QueryId getQueryId()
    {
        return ;
    }
    public Session getSession()
    {
        return ;
    }
    {
        return getQueryInfo(null);
    }
    public synchronized QueryInfo getQueryInfo(StageInfo rootStage)
    {
        QueryState state = .get();
        Duration elapsedTime;
        if ( != null) {
            elapsedTime = new Duration(.getMillis() - .getMillis(), );
        }
        else {
            elapsedTime = Duration.nanosSince();
        }
        // don't report failure info is query is marked as success
        FailureInfo failureInfo = null;
        ErrorCode errorCode = null;
        if (state != ) {
            failureInfo =  == null ? null : toFailure().toFailureInfo();
            errorCode = ErrorCodes.toErrorCode();
        }
        int totalTasks = 0;
        int runningTasks = 0;
        int completedTasks = 0;
        int totalDrivers = 0;
        int queuedDrivers = 0;
        int runningDrivers = 0;
        int completedDrivers = 0;
        long totalMemoryReservation = 0;
        long totalScheduledTime = 0;
        long totalCpuTime = 0;
        long totalUserTime = 0;
        long totalBlockedTime = 0;
        long rawInputDataSize = 0;
        long rawInputPositions = 0;
        long processedInputDataSize = 0;
        long processedInputPositions = 0;
        long outputDataSize = 0;
        long outputPositions = 0;
        if (rootStage != null) {
            for (StageInfo stageInfo : getAllStages(rootStage)) {
                StageStats stageStats = stageInfo.getStageStats();
                totalTasks += stageStats.getTotalTasks();
                runningTasks += stageStats.getRunningTasks();
                completedTasks += stageStats.getCompletedTasks();
                totalDrivers += stageStats.getTotalDrivers();
                queuedDrivers += stageStats.getQueuedDrivers();
                runningDrivers += stageStats.getRunningDrivers();
                completedDrivers += stageStats.getCompletedDrivers();
                totalMemoryReservation += stageStats.getTotalMemoryReservation().toBytes();
                totalScheduledTime += stageStats.getTotalScheduledTime().roundTo();
                totalCpuTime += stageStats.getTotalCpuTime().roundTo();
                totalUserTime += stageStats.getTotalUserTime().roundTo();
                totalBlockedTime += stageStats.getTotalBlockedTime().roundTo();
                if (Iterables.any(stageInfo.getPlan().getSources(), Predicates.instanceOf(TableScanNode.class))) {
                    rawInputDataSize += stageStats.getRawInputDataSize().toBytes();
                    rawInputPositions += stageStats.getRawInputPositions();
                    processedInputDataSize += stageStats.getProcessedInputDataSize().toBytes();
                    processedInputPositions += stageStats.getProcessedInputPositions();
                }
            }
            StageStats outputStageStats = rootStage.getStageStats();
            outputDataSize += outputStageStats.getOutputDataSize().toBytes();
            outputPositions += outputStageStats.getOutputPositions();
        }
        QueryStats queryStats = new QueryStats(
                ,
                ,
                ,
                ,
                elapsedTime.convertToMostSuccinctTimeUnit(),
                ,
                ,
                ,
                ,
                totalTasks,
                runningTasks,
                completedTasks,
                totalDrivers,
                queuedDrivers,
                runningDrivers,
                completedDrivers,
                new DataSize(totalMemoryReservation).convertToMostSuccinctDataSize(),
                new Duration(totalScheduledTime).convertToMostSuccinctTimeUnit(),
                new Duration(totalCpuTime).convertToMostSuccinctTimeUnit(),
                new Duration(totalUserTime).convertToMostSuccinctTimeUnit(),
                new Duration(totalBlockedTime).convertToMostSuccinctTimeUnit(),
                new DataSize(rawInputDataSize).convertToMostSuccinctDataSize(),
                rawInputPositions,
                new DataSize(processedInputDataSize).convertToMostSuccinctDataSize(),
                processedInputPositions,
                new DataSize(outputDataSize).convertToMostSuccinctDataSize(),
                outputPositions);
        return new QueryInfo(,
                ,
                state,
                isScheduled(rootStage),
                ,
                ,
                ,
                queryStats,
                ,
                ,
                ,
                rootStage,
                failureInfo,
                errorCode,
                );
    }
    public synchronized void setOutputFieldNames(List<StringoutputFieldNames)
    {
        checkNotNull(outputFieldNames"outputFieldNames is null");
        this. = ImmutableList.copyOf(outputFieldNames);
    }
    public synchronized void setInputs(List<Inputinputs)
    {
        checkNotNull(inputs"inputs is null");
        this. = ImmutableSet.copyOf(inputs);
    }
    public synchronized Map<StringStringgetSetSessionProperties()
    {
        return ;
    }
    public synchronized void addSetSessionProperties(String keyString value)
    {
        .put(checkNotNull(key"key is null"), checkNotNull(value"value is null"));
    }
    public synchronized Set<StringgetResetSessionProperties()
    {
        return ;
    }
    public synchronized void addResetSessionProperties(String name)
    {
        .add(checkNotNull(name"name is null"));
    }
    public synchronized void setUpdateType(String updateType)
    {
        this. = updateType;
    }
    public synchronized QueryState getQueryState()
    {
        return .get();
    }
    public synchronized boolean isDone()
    {
        return .get().isDone();
    }
    public boolean beginPlanning()
    {
        // transition from queued to planning
            return false;
        }
        // planning has begun
        synchronized (this) {
            Preconditions.checkState( > 0, "Can not record analysis start");
             = Duration.nanosSince().convertToMostSuccinctTimeUnit();
        }
        return true;
    }
    public synchronized boolean starting()
    {
        // transition from queued or planning to starting
        boolean changed = .setIf(., Predicates.in(ImmutableSet.of(..)));
        if (changed) {
             = Duration.nanosSince();
        }
        return changed;
    }
    public synchronized boolean running()
    {
        // transition to running if not already done
        return .setIf(., Predicates.not(QueryState::isDone));
    }
    public boolean finished()
    {
        synchronized (this) {
            if ( == null) {
                 = DateTime.now();
            }
        }
        return .setIf(, Predicates.not(QueryState::isDone));
    }
    public boolean fail(@Nullable Throwable cause)
    {
        synchronized (this) {
            if ( == null) {
                 = DateTime.now();
            }
        }
        synchronized (this) {
            if (cause != null) {
                if ( == null) {
                     = cause;
                }
                else {
                    .addSuppressed(cause);
                }
            }
        }
        return .setIf(, Predicates.not(QueryState::isDone));
    }
    public void addStateChangeListener(StateChangeListener<QueryStatestateChangeListener)
    {
        .addStateChangeListener(stateChangeListener);
    }
    public Duration waitForStateChange(QueryState currentStateDuration maxWait)
            throws InterruptedException
    {
        return .waitForStateChange(currentStatemaxWait);
    }
    public synchronized void recordHeartbeat()
    {
        this. = DateTime.now();
    }
    public synchronized void recordExecutionStart()
    {
        if ( == null) {
            this. = DateTime.now();
        }
    }
    public synchronized void recordAnalysisTime(long analysisStart)
    {
         = Duration.nanosSince(analysisStart).convertToMostSuccinctTimeUnit();
    }
    public synchronized void recordDistributedPlanningTime(long distributedPlanningStart)
    {
         = Duration.nanosSince(distributedPlanningStart).convertToMostSuccinctTimeUnit();
    }
    private static boolean isScheduled(StageInfo rootStage)
    {
        if (rootStage == null) {
            return false;
        }
        return FluentIterable.from(getAllStages(rootStage))
                .transform(StageInfo::getState)
                .allMatch(state -> (state == .) || state.isDone());
    }
New to GrepCode? Check out our FAQ X