Search in sources :

Example 21 with PlanFragment

use of com.facebook.presto.sql.planner.PlanFragment in project presto by prestodb.

the class AllAtOnceExecutionSchedule method getPreferredScheduleOrder.

@VisibleForTesting
static List<PlanFragmentId> getPreferredScheduleOrder(Collection<PlanFragment> fragments) {
    // determine output fragment
    Set<PlanFragmentId> remoteSources = fragments.stream().map(PlanFragment::getRemoteSourceNodes).flatMap(Collection::stream).map(RemoteSourceNode::getSourceFragmentIds).flatMap(Collection::stream).collect(toImmutableSet());
    Set<PlanFragment> rootFragments = fragments.stream().filter(fragment -> !remoteSources.contains(fragment.getId())).collect(toImmutableSet());
    Visitor visitor = new Visitor(fragments);
    rootFragments.forEach(fragment -> visitor.processFragment(fragment.getId()));
    return visitor.getSchedulerOrder();
}
Also used : SpatialJoinNode(com.facebook.presto.sql.planner.plan.SpatialJoinNode) UnionNode(com.facebook.presto.spi.plan.UnionNode) RemoteSourceNode(com.facebook.presto.sql.planner.plan.RemoteSourceNode) SCHEDULED(com.facebook.presto.execution.StageExecutionState.SCHEDULED) SqlStageExecution(com.facebook.presto.execution.SqlStageExecution) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) PlanFragment(com.facebook.presto.sql.planner.PlanFragment) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) IndexJoinNode(com.facebook.presto.sql.planner.plan.IndexJoinNode) InternalPlanVisitor(com.facebook.presto.sql.planner.plan.InternalPlanVisitor) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) PlanFragmentId(com.facebook.presto.sql.planner.plan.PlanFragmentId) StageExecutionState(com.facebook.presto.execution.StageExecutionState) LinkedHashSet(java.util.LinkedHashSet) JoinNode(com.facebook.presto.sql.planner.plan.JoinNode) ImmutableSet(com.google.common.collect.ImmutableSet) Iterator(java.util.Iterator) RUNNING(com.facebook.presto.execution.StageExecutionState.RUNNING) Collection(java.util.Collection) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Set(java.util.Set) PlanNode(com.facebook.presto.spi.plan.PlanNode) List(java.util.List) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) Ordering(com.google.common.collect.Ordering) SemiJoinNode(com.facebook.presto.sql.planner.plan.SemiJoinNode) Function.identity(java.util.function.Function.identity) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ExchangeNode(com.facebook.presto.sql.planner.plan.ExchangeNode) RemoteSourceNode(com.facebook.presto.sql.planner.plan.RemoteSourceNode) InternalPlanVisitor(com.facebook.presto.sql.planner.plan.InternalPlanVisitor) PlanFragmentId(com.facebook.presto.sql.planner.plan.PlanFragmentId) PlanFragment(com.facebook.presto.sql.planner.PlanFragment) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 22 with PlanFragment

use of com.facebook.presto.sql.planner.PlanFragment in project presto by prestodb.

the class QueryStats method create.

public static QueryStats create(QueryStateTimer queryStateTimer, Optional<StageInfo> rootStage, int peakRunningTasks, DataSize peakUserMemoryReservation, DataSize peakTotalMemoryReservation, DataSize peakTaskUserMemory, DataSize peakTaskTotalMemory, DataSize peakNodeTotalMemory, RuntimeStats runtimeStats) {
    int totalTasks = 0;
    int runningTasks = 0;
    int completedTasks = 0;
    int totalDrivers = 0;
    int queuedDrivers = 0;
    int runningDrivers = 0;
    int blockedDrivers = 0;
    int completedDrivers = 0;
    double cumulativeUserMemory = 0;
    double cumulativeTotalMemory = 0;
    long userMemoryReservation = 0;
    long totalMemoryReservation = 0;
    long totalScheduledTime = 0;
    long totalCpuTime = 0;
    long retriedCpuTime = 0;
    long totalBlockedTime = 0;
    long totalAllocation = 0;
    long rawInputDataSize = 0;
    long rawInputPositions = 0;
    long processedInputDataSize = 0;
    long processedInputPositions = 0;
    long outputDataSize = 0;
    long outputPositions = 0;
    long writtenOutputPositions = 0;
    long writtenOutputLogicalDataSize = 0;
    long writtenOutputPhysicalDataSize = 0;
    long writtenIntermediatePhysicalDataSize = 0;
    ImmutableList.Builder<StageGcStatistics> stageGcStatistics = ImmutableList.builder();
    boolean fullyBlocked = rootStage.isPresent();
    Set<BlockedReason> blockedReasons = new HashSet<>();
    ImmutableList.Builder<OperatorStats> operatorStatsSummary = ImmutableList.builder();
    boolean completeInfo = true;
    RuntimeStats mergedRuntimeStats = RuntimeStats.copyOf(runtimeStats);
    for (StageInfo stageInfo : getAllStages(rootStage)) {
        StageExecutionStats stageExecutionStats = stageInfo.getLatestAttemptExecutionInfo().getStats();
        totalTasks += stageExecutionStats.getTotalTasks();
        runningTasks += stageExecutionStats.getRunningTasks();
        completedTasks += stageExecutionStats.getCompletedTasks();
        totalDrivers += stageExecutionStats.getTotalDrivers();
        queuedDrivers += stageExecutionStats.getQueuedDrivers();
        runningDrivers += stageExecutionStats.getRunningDrivers();
        blockedDrivers += stageExecutionStats.getBlockedDrivers();
        completedDrivers += stageExecutionStats.getCompletedDrivers();
        cumulativeUserMemory += stageExecutionStats.getCumulativeUserMemory();
        cumulativeTotalMemory += stageExecutionStats.getCumulativeTotalMemory();
        userMemoryReservation += stageExecutionStats.getUserMemoryReservation().toBytes();
        totalMemoryReservation += stageExecutionStats.getTotalMemoryReservation().toBytes();
        totalScheduledTime += stageExecutionStats.getTotalScheduledTime().roundTo(MILLISECONDS);
        totalCpuTime += stageExecutionStats.getTotalCpuTime().roundTo(MILLISECONDS);
        retriedCpuTime += computeRetriedCpuTime(stageInfo);
        totalBlockedTime += stageExecutionStats.getTotalBlockedTime().roundTo(MILLISECONDS);
        if (!stageInfo.getLatestAttemptExecutionInfo().getState().isDone()) {
            fullyBlocked &= stageExecutionStats.isFullyBlocked();
            blockedReasons.addAll(stageExecutionStats.getBlockedReasons());
        }
        totalAllocation += stageExecutionStats.getTotalAllocation().toBytes();
        if (stageInfo.getPlan().isPresent()) {
            PlanFragment plan = stageInfo.getPlan().get();
            if (!plan.getTableScanSchedulingOrder().isEmpty()) {
                rawInputDataSize += stageExecutionStats.getRawInputDataSize().toBytes();
                rawInputPositions += stageExecutionStats.getRawInputPositions();
                processedInputDataSize += stageExecutionStats.getProcessedInputDataSize().toBytes();
                processedInputPositions += stageExecutionStats.getProcessedInputPositions();
            }
            if (plan.isOutputTableWriterFragment()) {
                writtenOutputPositions += stageExecutionStats.getOperatorSummaries().stream().filter(stats -> stats.getOperatorType().equals(TableWriterOperator.class.getSimpleName())).mapToLong(OperatorStats::getInputPositions).sum();
                writtenOutputLogicalDataSize += stageExecutionStats.getOperatorSummaries().stream().filter(stats -> stats.getOperatorType().equals(TableWriterOperator.class.getSimpleName())).mapToLong(stats -> stats.getInputDataSize().toBytes()).sum();
                writtenOutputPhysicalDataSize += stageExecutionStats.getPhysicalWrittenDataSize().toBytes();
            } else {
                writtenIntermediatePhysicalDataSize += stageExecutionStats.getPhysicalWrittenDataSize().toBytes();
            }
        }
        stageGcStatistics.add(stageExecutionStats.getGcInfo());
        completeInfo = completeInfo && stageInfo.isFinalStageInfo();
        operatorStatsSummary.addAll(stageExecutionStats.getOperatorSummaries());
        // We prepend each metric name with the stage id to avoid merging metrics across stages.
        int stageId = stageInfo.getStageId().getId();
        stageExecutionStats.getRuntimeStats().getMetrics().forEach((name, metric) -> {
            String metricName = String.format("S%d-%s", stageId, name);
            mergedRuntimeStats.mergeMetric(metricName, metric);
        });
    }
    if (rootStage.isPresent()) {
        StageExecutionStats outputStageStats = rootStage.get().getLatestAttemptExecutionInfo().getStats();
        outputDataSize += outputStageStats.getOutputDataSize().toBytes();
        outputPositions += outputStageStats.getOutputPositions();
    }
    return new QueryStats(queryStateTimer.getCreateTime(), queryStateTimer.getExecutionStartTime().orElse(null), queryStateTimer.getLastHeartbeat(), queryStateTimer.getEndTime().orElse(null), queryStateTimer.getElapsedTime(), queryStateTimer.getWaitingForPrerequisitesTime(), queryStateTimer.getQueuedTime(), queryStateTimer.getResourceWaitingTime(), queryStateTimer.getSemanticAnalyzingTime(), queryStateTimer.getColumnAccessPermissionCheckingTime(), queryStateTimer.getDispatchingTime(), queryStateTimer.getExecutionTime(), queryStateTimer.getAnalysisTime(), queryStateTimer.getPlanningTime(), queryStateTimer.getFinishingTime(), totalTasks, runningTasks, peakRunningTasks, completedTasks, totalDrivers, queuedDrivers, runningDrivers, blockedDrivers, completedDrivers, cumulativeUserMemory, cumulativeTotalMemory, succinctBytes(userMemoryReservation), succinctBytes(totalMemoryReservation), peakUserMemoryReservation, peakTotalMemoryReservation, peakTaskUserMemory, peakTaskTotalMemory, peakNodeTotalMemory, isScheduled(rootStage), succinctDuration(totalScheduledTime, MILLISECONDS), succinctDuration(totalCpuTime, MILLISECONDS), succinctDuration(retriedCpuTime, MILLISECONDS), succinctDuration(totalBlockedTime, MILLISECONDS), fullyBlocked, blockedReasons, succinctBytes(totalAllocation), succinctBytes(rawInputDataSize), rawInputPositions, succinctBytes(processedInputDataSize), processedInputPositions, succinctBytes(outputDataSize), outputPositions, writtenOutputPositions, succinctBytes(writtenOutputLogicalDataSize), succinctBytes(writtenOutputPhysicalDataSize), succinctBytes(writtenIntermediatePhysicalDataSize), stageGcStatistics.build(), operatorStatsSummary.build(), mergedRuntimeStats);
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) OptionalDouble(java.util.OptionalDouble) DataSize.succinctBytes(io.airlift.units.DataSize.succinctBytes) StageGcStatistics(com.facebook.presto.spi.eventlistener.StageGcStatistics) Duration(io.airlift.units.Duration) OperatorStats(com.facebook.presto.operator.OperatorStats) HashSet(java.util.HashSet) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) PlanFragment(com.facebook.presto.sql.planner.PlanFragment) ImmutableList(com.google.common.collect.ImmutableList) Objects.requireNonNull(java.util.Objects.requireNonNull) RuntimeStats(com.facebook.presto.common.RuntimeStats) Duration.succinctDuration(io.airlift.units.Duration.succinctDuration) Nullable(javax.annotation.Nullable) StageInfo.getAllStages(com.facebook.presto.execution.StageInfo.getAllStages) BlockedReason(com.facebook.presto.operator.BlockedReason) ImmutableSet(com.google.common.collect.ImmutableSet) DateTime(org.joda.time.DateTime) Set(java.util.Set) Math.min(java.lang.Math.min) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) TableWriterOperator(com.facebook.presto.operator.TableWriterOperator) DataSize(io.airlift.units.DataSize) List(java.util.List) JsonCreator(com.fasterxml.jackson.annotation.JsonCreator) Optional(java.util.Optional) BYTE(io.airlift.units.DataSize.Unit.BYTE) BlockedReason(com.facebook.presto.operator.BlockedReason) ImmutableList(com.google.common.collect.ImmutableList) RuntimeStats(com.facebook.presto.common.RuntimeStats) OperatorStats(com.facebook.presto.operator.OperatorStats) PlanFragment(com.facebook.presto.sql.planner.PlanFragment) TableWriterOperator(com.facebook.presto.operator.TableWriterOperator) StageGcStatistics(com.facebook.presto.spi.eventlistener.StageGcStatistics) HashSet(java.util.HashSet)

Example 23 with PlanFragment

use of com.facebook.presto.sql.planner.PlanFragment in project presto by prestodb.

the class MockRemoteTaskFactory method createTableScanTask.

public MockRemoteTask createTableScanTask(TaskId taskId, InternalNode newNode, List<Split> splits, NodeTaskMap.NodeStatsTracker nodeStatsTracker) {
    VariableReferenceExpression variable = new VariableReferenceExpression(Optional.empty(), "column", VARCHAR);
    PlanNodeId sourceId = new PlanNodeId("sourceId");
    PlanFragment testFragment = new PlanFragment(new PlanFragmentId(0), new TableScanNode(Optional.empty(), sourceId, new TableHandle(new ConnectorId("test"), new TestingTableHandle(), TestingTransactionHandle.create(), Optional.of(TestingHandle.INSTANCE)), ImmutableList.of(variable), ImmutableMap.of(variable, new TestingColumnHandle("column")), TupleDomain.all(), TupleDomain.all()), ImmutableSet.of(variable), SOURCE_DISTRIBUTION, ImmutableList.of(sourceId), new PartitioningScheme(Partitioning.create(SINGLE_DISTRIBUTION, ImmutableList.of()), ImmutableList.of(variable)), StageExecutionDescriptor.ungroupedExecution(), false, StatsAndCosts.empty(), Optional.empty());
    ImmutableMultimap.Builder<PlanNodeId, Split> initialSplits = ImmutableMultimap.builder();
    for (Split sourceSplit : splits) {
        initialSplits.put(sourceId, sourceSplit);
    }
    return createRemoteTask(TEST_SESSION, taskId, newNode, testFragment, initialSplits.build(), createInitialEmptyOutputBuffers(BROADCAST), nodeStatsTracker, true, new TableWriteInfo(Optional.empty(), Optional.empty(), Optional.empty()));
}
Also used : TableWriteInfo(com.facebook.presto.execution.scheduler.TableWriteInfo) TestingTableHandle(com.facebook.presto.testing.TestingMetadata.TestingTableHandle) PartitioningScheme(com.facebook.presto.sql.planner.PartitioningScheme) PlanFragment(com.facebook.presto.sql.planner.PlanFragment) PlanNodeId(com.facebook.presto.spi.plan.PlanNodeId) TestingColumnHandle(com.facebook.presto.testing.TestingMetadata.TestingColumnHandle) TableScanNode(com.facebook.presto.spi.plan.TableScanNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) TableHandle(com.facebook.presto.spi.TableHandle) TestingTableHandle(com.facebook.presto.testing.TestingMetadata.TestingTableHandle) PlanFragmentId(com.facebook.presto.sql.planner.plan.PlanFragmentId) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) Split(com.facebook.presto.metadata.Split) ConnectorId(com.facebook.presto.spi.ConnectorId)

Example 24 with PlanFragment

use of com.facebook.presto.sql.planner.PlanFragment in project presto by prestodb.

the class PhasedExecutionSchedule method extractPhases.

@VisibleForTesting
static List<Set<PlanFragmentId>> extractPhases(Collection<PlanFragment> fragments) {
    // Build a graph where the plan fragments are vertexes and the edges represent
    // a before -> after relationship.  For example, a join hash build has an edge
    // to the join probe.
    DirectedGraph<PlanFragmentId, DefaultEdge> graph = new DefaultDirectedGraph<>(DefaultEdge.class);
    fragments.forEach(fragment -> graph.addVertex(fragment.getId()));
    Visitor visitor = new Visitor(fragments, graph);
    for (PlanFragment fragment : fragments) {
        visitor.processFragment(fragment.getId());
    }
    // Computes all the strongly connected components of the directed graph.
    // These are the "phases" which hold the set of fragments that must be started
    // at the same time to avoid deadlock.
    List<Set<PlanFragmentId>> components = new StrongConnectivityInspector<>(graph).stronglyConnectedSets();
    Map<PlanFragmentId, Set<PlanFragmentId>> componentMembership = new HashMap<>();
    for (Set<PlanFragmentId> component : components) {
        for (PlanFragmentId planFragmentId : component) {
            componentMembership.put(planFragmentId, component);
        }
    }
    // build graph of components (phases)
    DirectedGraph<Set<PlanFragmentId>, DefaultEdge> componentGraph = new DefaultDirectedGraph<>(DefaultEdge.class);
    components.forEach(componentGraph::addVertex);
    for (DefaultEdge edge : graph.edgeSet()) {
        PlanFragmentId source = graph.getEdgeSource(edge);
        PlanFragmentId target = graph.getEdgeTarget(edge);
        Set<PlanFragmentId> from = componentMembership.get(source);
        Set<PlanFragmentId> to = componentMembership.get(target);
        if (!from.equals(to)) {
            // the topological order iterator below doesn't include vertices that have self-edges, so don't add them
            componentGraph.addEdge(from, to);
        }
    }
    List<Set<PlanFragmentId>> schedulePhases = ImmutableList.copyOf(new TopologicalOrderIterator<>(componentGraph));
    return schedulePhases;
}
Also used : HashSet(java.util.HashSet) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) InternalPlanVisitor(com.facebook.presto.sql.planner.plan.InternalPlanVisitor) DefaultDirectedGraph(org.jgrapht.graph.DefaultDirectedGraph) HashMap(java.util.HashMap) DefaultEdge(org.jgrapht.graph.DefaultEdge) PlanFragment(com.facebook.presto.sql.planner.PlanFragment) PlanFragmentId(com.facebook.presto.sql.planner.plan.PlanFragmentId) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 25 with PlanFragment

use of com.facebook.presto.sql.planner.PlanFragment in project presto by prestodb.

the class SqlQueryScheduler method createStageExecutions.

private List<SectionExecution> createStageExecutions(List<StreamingPlanSection> sections) {
    ImmutableList.Builder<SectionExecution> result = ImmutableList.builder();
    for (StreamingPlanSection section : sections) {
        StageId sectionId = getStageId(section.getPlan().getFragment().getId());
        List<SectionExecution> attempts = sectionExecutions.computeIfAbsent(sectionId, (ignored) -> new CopyOnWriteArrayList<>());
        // sectionExecutions only get created when they are about to be scheduled, so there should
        // never be a non-failed SectionExecution for a section that's ready for execution
        verify(attempts.isEmpty() || getLast(attempts).isFailed(), "Non-failed sectionExecutions already exists");
        PlanFragment sectionRootFragment = section.getPlan().getFragment();
        Optional<int[]> bucketToPartition;
        OutputBuffers outputBuffers;
        ExchangeLocationsConsumer locationsConsumer;
        if (isRootFragment(sectionRootFragment)) {
            bucketToPartition = Optional.of(new int[1]);
            outputBuffers = createInitialEmptyOutputBuffers(sectionRootFragment.getPartitioningScheme().getPartitioning().getHandle()).withBuffer(new OutputBufferId(0), BROADCAST_PARTITION_ID).withNoMoreBufferIds();
            OutputBufferId rootBufferId = getOnlyElement(outputBuffers.getBuffers().keySet());
            locationsConsumer = (fragmentId, tasks, noMoreExchangeLocations) -> updateQueryOutputLocations(queryStateMachine, rootBufferId, tasks, noMoreExchangeLocations);
        } else {
            bucketToPartition = Optional.empty();
            outputBuffers = createDiscardingOutputBuffers();
            locationsConsumer = (fragmentId, tasks, noMoreExchangeLocations) -> {
            };
        }
        int attemptId = attempts.size();
        SectionExecution sectionExecution = sectionExecutionFactory.createSectionExecutions(session, section, locationsConsumer, bucketToPartition, outputBuffers, summarizeTaskInfo, remoteTaskFactory, splitSourceFactory, attemptId);
        addStateChangeListeners(sectionExecution);
        attempts.add(sectionExecution);
        result.add(sectionExecution);
    }
    return result.build();
}
Also used : ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) StageId(com.facebook.presto.execution.StageId) OutputBufferId(com.facebook.presto.execution.buffer.OutputBuffers.OutputBufferId) PlanFragment(com.facebook.presto.sql.planner.PlanFragment) OutputBuffers.createInitialEmptyOutputBuffers(com.facebook.presto.execution.buffer.OutputBuffers.createInitialEmptyOutputBuffers) OutputBuffers.createDiscardingOutputBuffers(com.facebook.presto.execution.buffer.OutputBuffers.createDiscardingOutputBuffers) OutputBuffers(com.facebook.presto.execution.buffer.OutputBuffers)

Aggregations

PlanFragment (com.facebook.presto.sql.planner.PlanFragment)30 PlanFragmentId (com.facebook.presto.sql.planner.plan.PlanFragmentId)15 ImmutableSet (com.google.common.collect.ImmutableSet)12 Set (java.util.Set)12 PartitioningScheme (com.facebook.presto.sql.planner.PartitioningScheme)7 RemoteSourceNode (com.facebook.presto.sql.planner.plan.RemoteSourceNode)6 ImmutableList (com.google.common.collect.ImmutableList)6 List (java.util.List)6 PlanNode (com.facebook.presto.spi.plan.PlanNode)5 PlanNodeId (com.facebook.presto.spi.plan.PlanNodeId)5 SubPlan (com.facebook.presto.sql.planner.SubPlan)5 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)5 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)5 Objects.requireNonNull (java.util.Objects.requireNonNull)5 Test (org.testng.annotations.Test)5 Session (com.facebook.presto.Session)4 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 TableScanNode (com.facebook.presto.spi.plan.TableScanNode)3 JoinNode (com.facebook.presto.sql.planner.plan.JoinNode)3