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.getFirst;
 import static com.google.common.collect.Iterables.getLast;
 import static com.google.common.collect.Iterables.transform;
 import static io.airlift.units.DataSize.Unit.BYTE;
 import static java.util.concurrent.TimeUnit.MILLISECONDS;
 import static java.util.concurrent.TimeUnit.NANOSECONDS;
 
 public class DriverContext
 {
     private static final ThreadMXBean THREAD_MX_BEAN = ManagementFactory.getThreadMXBean();
 
     private final PipelineContext pipelineContext;
     private final Executor executor;
 
     private final AtomicBoolean finished = new AtomicBoolean();
 
     private final DateTime createdTime = DateTime.now();
     private final long createNanos = System.nanoTime();
 
     private final AtomicLong startNanos = new AtomicLong();
     private final AtomicLong endNanos = new AtomicLong();
 
     private final AtomicLong intervalWallStart = new AtomicLong();
     private final AtomicLong intervalCpuStart = new AtomicLong();
     private final AtomicLong intervalUserStart = new AtomicLong();
 
     private final AtomicLong processCalls = new AtomicLong();
     private final AtomicLong processWallNanos = new AtomicLong();
     private final AtomicLong processCpuNanos = new AtomicLong();
     private final AtomicLong processUserNanos = new AtomicLong();
 
     private final AtomicReference<BlockedMonitorblockedMonitor = new AtomicReference<>();
     private final AtomicLong blockedWallNanos = new AtomicLong();
 
     private final AtomicReference<DateTimeexecutionStartTime = new AtomicReference<>();
     private final AtomicReference<DateTimeexecutionEndTime = new AtomicReference<>();
 
     private final AtomicLong memoryReservation = new AtomicLong();
 
     private final List<OperatorContextoperatorContexts = new CopyOnWriteArrayList<>();
     private final boolean partitioned;
 
     public DriverContext(PipelineContext pipelineContextExecutor executorboolean partitioned)
     {
         this. = checkNotNull(pipelineContext"pipelineContext is null");
         this. = checkNotNull(executor"executor is null");
         this. = partitioned;
     }
 
     public TaskId getTaskId()
     {
         return .getTaskId();
     }
 
     public OperatorContext addOperatorContext(int operatorIdString operatorType)
     {
         return addOperatorContext(operatorIdoperatorType.getMaxMemorySize().toBytes());
     }
 
     public OperatorContext addOperatorContext(int operatorIdString operatorTypelong maxMemoryReservation)
     {
        checkArgument(operatorId >= 0, "operatorId is negative");
        for (OperatorContext operatorContext : ) {
            checkArgument(operatorId != operatorContext.getOperatorId(), "A context already exists for operatorId %s"operatorId);
        }
        OperatorContext operatorContext = new OperatorContext(operatorIdoperatorTypethismaxMemoryReservation);
        .add(operatorContext);
        return operatorContext;
    }
    {
        return ImmutableList.copyOf();
    }
    {
        return ;
    }
    public Session getSession()
    {
        return .getSession();
    }
    public void startProcessTimer()
    {
        if (.compareAndSet(0, System.nanoTime())) {
            .start();
            .set(DateTime.now());
        }
        .set(System.nanoTime());
    }
    public void recordProcessed()
    {
        .incrementAndGet();
    }
    public void recordBlocked(ListenableFuture<?> blocked)
    {
        checkNotNull(blocked"blocked is null");
        BlockedMonitor monitor = new BlockedMonitor();
        BlockedMonitor oldMonitor = .getAndSet(monitor);
        if (oldMonitor != null) {
            oldMonitor.run();
        }
        blocked.addListener(monitor);
    }
    public void finished()
    {
        if (!.compareAndSet(falsetrue)) {
            // already finished
            return;
        }
        .set(DateTime.now());
        .set(System.nanoTime());
        .driverFinished(this);
    }
    public void failed(Throwable cause)
    {
        .failed(cause);
        .set(true);
    }
    public boolean isDone()
    {
        return .get() || .isDone();
    }
    public DataSize getMaxMemorySize()
    {
        return .getMaxMemorySize();
    }
    {
    }
    public boolean reserveMemory(long bytes)
    {
        boolean result = .reserveMemory(bytes);
        if (result) {
            .getAndAdd(bytes);
        }
        return result;
    }
    public void freeMemory(long bytes)
    {
        .freeMemory(bytes);
        .getAndAdd(-bytes);
    }
    public boolean isVerboseStats()
    {
        return .isVerboseStats();
    }
    public boolean isCpuTimerEnabled()
    {
        return .isCpuTimerEnabled();
    }
    public CounterStat getInputDataSize()
    {
        OperatorContext inputOperator = getFirst(null);
        if (inputOperator != null) {
            return inputOperator.getInputDataSize();
        }
        else {
            return new CounterStat();
        }
    }
    {
        OperatorContext inputOperator = getFirst(null);
        if (inputOperator != null) {
            return inputOperator.getInputPositions();
        }
        else {
            return new CounterStat();
        }
    }
    {
        OperatorContext inputOperator = getLast(null);
        if (inputOperator != null) {
            return inputOperator.getOutputDataSize();
        }
        else {
            return new CounterStat();
        }
    }
    {
        OperatorContext inputOperator = getLast(null);
        if (inputOperator != null) {
            return inputOperator.getOutputPositions();
        }
        else {
            return new CounterStat();
        }
    }
    public DriverStats getDriverStats()
    {
        long totalScheduledTime = .get();
        long totalCpuTime = .get();
        long totalUserTime = .get();
        long totalBlockedTime = .get();
        BlockedMonitor blockedMonitor = this..get();
        if (blockedMonitor != null) {
            totalBlockedTime += blockedMonitor.getBlockedTime();
        }
        List<OperatorStatsoperators = ImmutableList.copyOf(transform(, OperatorContext::getOperatorStats));
        OperatorStats inputOperator = getFirst(operatorsnull);
        DataSize rawInputDataSize;
        long rawInputPositions;
        Duration rawInputReadTime;
        DataSize processedInputDataSize;
        long processedInputPositions;
        DataSize outputDataSize;
        long outputPositions;
        if (inputOperator != null) {
            rawInputDataSize = inputOperator.getInputDataSize();
            rawInputPositions = inputOperator.getInputPositions();
            rawInputReadTime = inputOperator.getAddInputWall();
            processedInputDataSize = inputOperator.getOutputDataSize();
            processedInputPositions = inputOperator.getOutputPositions();
            OperatorStats outputOperator = checkNotNull(getLast(operatorsnull));
            outputDataSize = outputOperator.getOutputDataSize();
            outputPositions = outputOperator.getOutputPositions();
        }
        else {
            rawInputDataSize = new DataSize(0, );
            rawInputPositions = 0;
            rawInputReadTime = new Duration(0, );
            processedInputDataSize = new DataSize(0, );
            processedInputPositions = 0;
            outputDataSize = new DataSize(0, );
            outputPositions = 0;
        }
        long startNanos = this..get();
        if (startNanos < ) {
            startNanos = System.nanoTime();
        }
        Duration queuedTime = new Duration(startNanos - );
        long endNanos = this..get();
        Duration elapsedTime;
        if (endNanos >= startNanos) {
            elapsedTime = new Duration(endNanos - );
        }
        else {
            elapsedTime = new Duration(0, );
        }
        return new DriverStats(
                ,
                .get(),
                .get(),
                queuedTime.convertToMostSuccinctTimeUnit(),
                elapsedTime.convertToMostSuccinctTimeUnit(),
                new DataSize(.get(), ).convertToMostSuccinctDataSize(),
                new Duration(totalScheduledTime).convertToMostSuccinctTimeUnit(),
                new Duration(totalCpuTime).convertToMostSuccinctTimeUnit(),
                new Duration(totalUserTime).convertToMostSuccinctTimeUnit(),
                new Duration(totalBlockedTime).convertToMostSuccinctTimeUnit(),
                rawInputDataSize.convertToMostSuccinctDataSize(),
                rawInputPositions,
                rawInputReadTime,
                processedInputDataSize.convertToMostSuccinctDataSize(),
                processedInputPositions,
                outputDataSize.convertToMostSuccinctDataSize(),
                outputPositions,
                ImmutableList.copyOf(transform(, OperatorContext::getOperatorStats)));
    }
    public boolean isPartitioned()
    {
        return ;
    }
    private long currentThreadUserTime()
    {
        if (!isCpuTimerEnabled()) {
            return 0;
        }
        return .getCurrentThreadUserTime();
    }
    private long currentThreadCpuTime()
    {
        if (!isCpuTimerEnabled()) {
            return 0;
        }
        return .getCurrentThreadCpuTime();
    }
    private static long nanosBetween(long startlong end)
    {
        return Math.abs(end - start);
    }
    // hack for index joins
    @Deprecated
    public Executor getExecutor()
    {
        return ;
    }
    private class BlockedMonitor
            implements Runnable
    {
        private final long start = System.nanoTime();
        private boolean finished;
        @Override
        public void run()
        {
            synchronized (this) {
                if () {
                    return;
                }
                 = true;
                .compareAndSet(thisnull);
                .getAndAdd(getBlockedTime());
            }
        }
        public long getBlockedTime()
        {
            return nanosBetween(, System.nanoTime());
        }
    }
New to GrepCode? Check out our FAQ X