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.operator;
 
 
 
 import java.util.List;
 
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.collect.Iterables.transform;
 import static io.airlift.units.DataSize.Unit.BYTE;
 import static java.util.concurrent.TimeUnit.NANOSECONDS;
 
 public class PipelineContext
 {
     private final TaskContext taskContext;
     private final Executor executor;
 
     private final boolean inputPipeline;
     private final boolean outputPipeline;
 
     private final List<DriverContextdrivers = new CopyOnWriteArrayList<>();
 
     private final AtomicInteger completedDrivers = new AtomicInteger();
 
     private final AtomicLong memoryReservation = new AtomicLong();
 
     private final Distribution queuedTime = new Distribution();
     private final Distribution elapsedTime = new Distribution();
 
     private final AtomicLong totalScheduledTime = new AtomicLong();
     private final AtomicLong totalCpuTime = new AtomicLong();
     private final AtomicLong totalUserTime = new AtomicLong();
     private final AtomicLong totalBlockedTime = new AtomicLong();
 
     private final CounterStat rawInputDataSize = new CounterStat();
     private final CounterStat rawInputPositions = new CounterStat();
 
     private final CounterStat processedInputDataSize = new CounterStat();
     private final CounterStat processedInputPositions = new CounterStat();
 
     private final CounterStat outputDataSize = new CounterStat();
     private final CounterStat outputPositions = new CounterStat();
 
     private final ConcurrentMap<IntegerOperatorStatsoperatorSummaries = new ConcurrentHashMap<>();
 
     public PipelineContext(TaskContext taskContextExecutor executorboolean inputPipelineboolean outputPipeline)
     {
         this. = inputPipeline;
         this. = outputPipeline;
         this. = checkNotNull(taskContext"taskContext is null");
         this. = checkNotNull(executor"executor is null");
     }
 
     public TaskContext getTaskContext()
     {
         return ;
     }
 
     public TaskId getTaskId()
     {
         return .getTaskId();
     }
 
     public boolean isInputPipeline()
     {
         return ;
    }
    public boolean isOutputPipeline()
    {
        return ;
    }
    {
        return addDriverContext(false);
    }
    public DriverContext addDriverContext(boolean partitioned)
    {
        DriverContext driverContext = new DriverContext(thispartitioned);
        .add(driverContext);
        return driverContext;
    }
    public Session getSession()
    {
        return .getSession();
    }
    public void driverFinished(DriverContext driverContext)
    {
        checkNotNull(driverContext"driverContext is null");
        if (!.remove(driverContext)) {
            throw new IllegalArgumentException("Unknown driver " + driverContext);
        }
        DriverStats driverStats = driverContext.getDriverStats();
        // remove the memory reservation
        freeMemory(driverStats.getMemoryReservation().toBytes());
        .add(driverStats.getQueuedTime().roundTo());
        .add(driverStats.getElapsedTime().roundTo());
        // merge the operator stats into the operator summary
        List<OperatorStatsoperators = driverStats.getOperatorStats();
        for (OperatorStats operator : operators) {
            // TODO: replace with ConcurrentMap.compute() when we migrate to java 8
            OperatorStats updated;
            OperatorStats current;
            do {
                current = .get(operator.getOperatorId());
                if (current != null) {
                    updated = current.add(operator);
                }
                else {
                    updated = operator;
                }
            }
            while (!compareAndSet(operator.getOperatorId(), currentupdated));
        }
        .update(driverStats.getRawInputDataSize().toBytes());
        .update(driverStats.getRawInputPositions());
        .update(driverStats.getOutputDataSize().toBytes());
        .update(driverStats.getOutputPositions());
    }
    public void start()
    {
        .start();
    }
    public void failed(Throwable cause)
    {
        .failed(cause);
    }
    public boolean isDone()
    {
        return .isDone();
    }
    public DataSize getMaxMemorySize()
    {
        return .getMaxMemorySize();
    }
    {
    }
    public synchronized boolean reserveMemory(long bytes)
    {
        boolean result = .reserveMemory(bytes);
        if (result) {
            .getAndAdd(bytes);
        }
        return result;
    }
    public synchronized void freeMemory(long bytes)
    {
        checkArgument(bytes <= .get(), "tried to free more memory than is reserved");
        .freeMemory(bytes);
        .getAndAdd(-bytes);
    }
    public boolean isVerboseStats()
    {
        return .isVerboseStats();
    }
    public boolean isCpuTimerEnabled()
    {
        return .isCpuTimerEnabled();
    }
    public CounterStat getInputDataSize()
    {
        CounterStat stat = new CounterStat();
        stat.merge();
        for (DriverContext driver : ) {
            stat.merge(driver.getInputDataSize());
        }
        return stat;
    }
    {
        CounterStat stat = new CounterStat();
        stat.merge();
        for (DriverContext driver : ) {
            stat.merge(driver.getInputPositions());
        }
        return stat;
    }
    {
        CounterStat stat = new CounterStat();
        stat.merge();
        for (DriverContext driver : ) {
            stat.merge(driver.getOutputDataSize());
        }
        return stat;
    }
    {
        CounterStat stat = new CounterStat();
        stat.merge();
        for (DriverContext driver : ) {
            stat.merge(driver.getOutputPositions());
        }
        return stat;
    }
    {
        List<DriverContextdriverContexts = ImmutableList.copyOf(this.);
        int totalDriers = .get() + driverContexts.size();
        int queuedDrivers = 0;
        int queuedPartitionedDrivers = 0;
        int runningDrivers = 0;
        int runningPartitionedDrivers = 0;
        int completedDrivers = this..get();
        Distribution queuedTime = new Distribution(this.);
        Distribution elapsedTime = new Distribution(this.);
        long totalScheduledTime = this..get();
        long totalCpuTime = this..get();
        long totalUserTime = this..get();
        long totalBlockedTime = this..get();
        long rawInputDataSize = this..getTotalCount();
        long rawInputPositions = this..getTotalCount();
        long processedInputDataSize = this..getTotalCount();
        long processedInputPositions = this..getTotalCount();
        long outputDataSize = this..getTotalCount();
        long outputPositions = this..getTotalCount();
        List<DriverStatsdrivers = new ArrayList<>();
        Multimap<IntegerOperatorStatsrunningOperators = ArrayListMultimap.create();
        for (DriverContext driverContext : driverContexts) {
            DriverStats driverStats = driverContext.getDriverStats();
            drivers.add(driverStats);
            if (driverStats.getStartTime() == null) {
                queuedDrivers++;
                if (driverContext.isPartitioned()) {
                    queuedPartitionedDrivers++;
                }
            }
            else {
                runningDrivers++;
                if (driverContext.isPartitioned()) {
                    runningPartitionedDrivers++;
                }
            }
            queuedTime.add(driverStats.getQueuedTime().roundTo());
            elapsedTime.add(driverStats.getElapsedTime().roundTo());
            totalScheduledTime += driverStats.getTotalScheduledTime().roundTo();
            totalCpuTime += driverStats.getTotalCpuTime().roundTo();
            totalUserTime += driverStats.getTotalUserTime().roundTo();
            totalBlockedTime += driverStats.getTotalBlockedTime().roundTo();
            List<OperatorStatsoperators = ImmutableList.copyOf(transform(driverContext.getOperatorContexts(), OperatorContext::getOperatorStats));
            for (OperatorStats operator : operators) {
                runningOperators.put(operator.getOperatorId(), operator);
            }
            rawInputDataSize += driverStats.getRawInputDataSize().toBytes();
            rawInputPositions += driverStats.getRawInputPositions();
            processedInputDataSize += driverStats.getProcessedInputDataSize().toBytes();
            processedInputPositions += driverStats.getProcessedInputPositions();
            outputDataSize += driverStats.getOutputDataSize().toBytes();
            outputPositions += driverStats.getOutputPositions();
        }
        // merge the running operator stats into the operator summary
        TreeMap<IntegerOperatorStatsoperatorSummaries = new TreeMap<>(this.);
        for (Entry<IntegerOperatorStatsentry : runningOperators.entries()) {
            OperatorStats current = operatorSummaries.get(entry.getKey());
            if (current == null) {
                current = entry.getValue();
            }
            else {
                current = current.add(entry.getValue());
            }
            operatorSummaries.put(entry.getKey(), current);
        }
        return new PipelineStats(
                ,
                ,
                totalDriers,
                queuedDrivers,
                queuedPartitionedDrivers,
                runningDrivers,
                runningPartitionedDrivers,
                completedDrivers,
                new DataSize(.get(), ).convertToMostSuccinctDataSize(),
                queuedTime.snapshot(),
                elapsedTime.snapshot(),
                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,
                ImmutableList.copyOf(operatorSummaries.values()),
                drivers);
    }
    private static <K, V> boolean compareAndSet(ConcurrentMap<K, V> map, K key, V oldValue, V newValue)
    {
        if (oldValue == null) {
            return map.putIfAbsent(keynewValue) == null;
        }
        return map.replace(keyoldValuenewValue);
    }
New to GrepCode? Check out our FAQ X