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.QueryState.PLANNING;
 import static com.facebook.presto.execution.QueryState.QUEUED;
 import static com.facebook.presto.execution.QueryState.RUNNING;
 import static com.facebook.presto.execution.QueryState.STARTING;
 import static com.facebook.presto.execution.QueryState.TERMINAL_QUERY_STATES;
 import static com.facebook.presto.execution.StageInfo.getAllStages;
 import static com.facebook.presto.memory.LocalMemoryManager.GENERAL_POOL;
 import static com.facebook.presto.util.Failures.toFailure;
 import static io.airlift.units.DataSize.Unit.BYTE;
 import static io.airlift.units.Duration.nanosSince;
 import static java.util.Objects.requireNonNull;
 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;
 
 
     private final AtomicReference<DateTimelastHeartbeat = new AtomicReference<>(DateTime.now());
     private final AtomicReference<DateTimeexecutionStartTime = new AtomicReference<>();
     private final AtomicReference<DateTimeendTime = new AtomicReference<>();
 
     private final AtomicReference<DurationqueuedTime = new AtomicReference<>();
     private final AtomicReference<DurationanalysisTime = new AtomicReference<>();
     private final AtomicReference<DurationdistributedPlanningTime = new AtomicReference<>();
 
     private final AtomicReference<DurationtotalPlanningTime = new AtomicReference<>();
 
     private final StateMachine<QueryStatequeryState;
 
     private final Map<StringStringsetSessionProperties = new ConcurrentHashMap<>();
     private final Set<StringresetSessionProperties = Sets.newConcurrentHashSet();
 
     private final AtomicReference<StringupdateType = new AtomicReference<>();
 
     private final AtomicReference<ExecutionFailureInfofailureCause = new AtomicReference<>();
 
     private final AtomicReference<List<String>> outputFieldNames = new AtomicReference<>(ImmutableList.of());
 
     private final AtomicReference<Set<Input>> inputs = new AtomicReference<>(ImmutableSet.of());
 
     public QueryStateMachine(QueryId queryIdString querySession sessionURI selfExecutor executor)
     {
         this. = requireNonNull(queryId"queryId is null");
        this. = requireNonNull(query"query is null");
        this. = requireNonNull(session"session is null");
        this. = requireNonNull(self"self is null");
        this. = new StateMachine<>("query " + queryexecutor);
        .addStateChangeListener(currentState -> .debug("Query %s is %s"QueryStateMachine.this.currentState));
    }
    public QueryId getQueryId()
    {
        return ;
    }
    public Session getSession()
    {
        return ;
    }
    {
        return getQueryInfo(null);
    }
    public QueryInfo getQueryInfo(StageInfo rootStage)
    {
        // Query state must be captured first in order to provide a
        // correct view of the query.  For example, building this
        // information, the query could finish, and the task states would
        // never be visible.
        QueryState state = .get();
        Duration elapsedTime;
        if (.get() != null) {
            elapsedTime = new Duration(.get().getMillis() - .getMillis(), );
        }
        else {
            elapsedTime = nanosSince();
        }
        // don't report failure info is query is marked as success
        FailureInfo failureInfo = null;
        ErrorCode errorCode = null;
        if (state != ) {
            ExecutionFailureInfo failureCause = this..get();
            if (failureCause != null) {
                failureInfo = failureCause.toFailureInfo();
                errorCode = failureCause.getErrorCode();
            }
        }
        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;
        boolean fullyBlocked = rootStage != null;
        Set<BlockedReasonblockedReasons = new HashSet<>();
        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 (!stageInfo.getState().isDone()) {
                    fullyBlocked &= stageStats.isFullyBlocked();
                    blockedReasons.addAll(stageStats.getBlockedReasons());
                }
                PlanFragment plan = stageInfo.getPlan();
                if (plan != null && plan.getPartitionedSourceNode() instanceof TableScanNode) {
                    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(
                ,
                .get(),
                .get(),
                .get(),
                elapsedTime.convertToMostSuccinctTimeUnit(),
                .get(),
                .get(),
                .get(),
                .get(),
                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(),
                fullyBlocked,
                blockedReasons,
                new DataSize(rawInputDataSize).convertToMostSuccinctDataSize(),
                rawInputPositions,
                new DataSize(processedInputDataSize).convertToMostSuccinctDataSize(),
                processedInputPositions,
                new DataSize(outputDataSize).convertToMostSuccinctDataSize(),
                outputPositions);
        return new QueryInfo(,
                ,
                state,
                .get().getId(),
                isScheduled(rootStage),
                ,
                .get(),
                ,
                queryStats,
                ,
                ,
                .get(),
                rootStage,
                failureInfo,
                errorCode,
                .get());
    }
    {
        return .get();
    }
    public void setMemoryPool(VersionedMemoryPoolId memoryPool)
    {
        this..set(requireNonNull(memoryPool"memoryPool is null"));
    }
    public void setOutputFieldNames(List<StringoutputFieldNames)
    {
        requireNonNull(outputFieldNames"outputFieldNames is null");
        this..set(ImmutableList.copyOf(outputFieldNames));
    }
    public void setInputs(List<Inputinputs)
    {
        requireNonNull(inputs"inputs is null");
        this..set(ImmutableSet.copyOf(inputs));
    }
    {
        return ;
    }
    public void addSetSessionProperties(String keyString value)
    {
        .put(requireNonNull(key"key is null"), requireNonNull(value"value is null"));
    }
    {
        return ;
    }
    public void addResetSessionProperties(String name)
    {
        .add(requireNonNull(name"name is null"));
    }
    public void setUpdateType(String updateType)
    {
        this..set(updateType);
    }
    public QueryState getQueryState()
    {
        return .get();
    }
    public boolean isDone()
    {
        return .get().isDone();
    }
    public boolean transitionToPlanning()
    {
        return .compareAndSet();
    }
    public boolean transitionToStarting()
    {
        Duration durationSinceCreation = nanosSince().convertToMostSuccinctTimeUnit();
        .compareAndSet(nulldurationSinceCreation);
        .compareAndSet(nulldurationSinceCreation);
        return .setIf(currentState -> currentState ==  || currentState == );
    }
    public boolean transitionToRunning()
    {
        Duration durationSinceCreation = nanosSince().convertToMostSuccinctTimeUnit();
        .compareAndSet(nulldurationSinceCreation);
        .compareAndSet(nulldurationSinceCreation);
        .compareAndSet(null, DateTime.now());
        return .setIf(currentState -> currentState !=  && !currentState.isDone());
    }
    public boolean transitionToFinished()
    {
        Duration durationSinceCreation = nanosSince().convertToMostSuccinctTimeUnit();
        .compareAndSet(nulldurationSinceCreation);
        .compareAndSet(nulldurationSinceCreation);
        DateTime now = DateTime.now();
        .compareAndSet(nullnow);
        .compareAndSet(nullnow);
        return .setIf(currentState ->!currentState.isDone());
    }
    public boolean transitionToFailed(Throwable throwable)
    {
        requireNonNull(throwable"throwable is null");
        Duration durationSinceCreation = nanosSince().convertToMostSuccinctTimeUnit();
        .compareAndSet(nulldurationSinceCreation);
        .compareAndSet(nulldurationSinceCreation);
        DateTime now = DateTime.now();
        .compareAndSet(nullnow);
        .compareAndSet(nullnow);
        .compareAndSet(nulltoFailure(throwable));
        boolean failed = .setIf(currentState -> !currentState.isDone());
        if (failed) {
            .error(throwable"Query %s failed");
        }
        else {
            .debug(throwable"Failure after query %s finished");
        }
        return failed;
    }
    public void addStateChangeListener(StateChangeListener<QueryStatestateChangeListener)
    {
        .addStateChangeListener(stateChangeListener);
    }
    public Duration waitForStateChange(QueryState currentStateDuration maxWait)
            throws InterruptedException
    {
        return .waitForStateChange(currentStatemaxWait);
    }
    public void recordHeartbeat()
    {
        this..set(DateTime.now());
    }
    public void recordAnalysisTime(long analysisStart)
    {
        .compareAndSet(nullnanosSince(analysisStart).convertToMostSuccinctTimeUnit());
    }
    public void recordDistributedPlanningTime(long distributedPlanningStart)
    {
        .compareAndSet(nullnanosSince(distributedPlanningStart).convertToMostSuccinctTimeUnit());
    }
    private static boolean isScheduled(StageInfo rootStage)
    {
        if (rootStage == null) {
            return false;
        }
        return getAllStages(rootStage).stream()
                .map(StageInfo::getState)
                .allMatch(state -> (state == .) || state.isDone());
    }
New to GrepCode? Check out our FAQ X