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.facebook.presto.util.Threads.checkNotSameThreadExecutor;
 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 io.airlift.units.DataSize.Unit.MEGABYTE;
 import static java.util.concurrent.TimeUnit.NANOSECONDS;
 
 public class TaskContext
 {
     private final TaskStateMachine taskStateMachine;
     private final Executor executor;
     private final Session session;
 
     private final long maxMemory;
     private final DataSize operatorPreAllocatedMemory;
 
     private final AtomicLong memoryReservation = new AtomicLong();
 
     private final long createNanos = System.nanoTime();
 
     private final AtomicLong startNanos = new AtomicLong();
     private final AtomicLong endNanos = new AtomicLong();
 
     private final AtomicReference<DateTimeexecutionStartTime = new AtomicReference<>();
     private final AtomicReference<DateTimelastExecutionStartTime = new AtomicReference<>();
     private final AtomicReference<DateTimeexecutionEndTime = new AtomicReference<>();
 
     private final List<PipelineContextpipelineContexts = new CopyOnWriteArrayList<>();
 
     private final boolean verboseStats;
     private final boolean cpuTimerEnabled;
 
     public TaskContext(TaskId taskIdExecutor executorSession session)
     {
         this(
                 checkNotNull(taskId"taskId is null"),
                 checkNotSameThreadExecutor(executor"executor is null"),
                 session,
                 new DataSize(256, ));
     }
 
     public TaskContext(TaskId taskIdExecutor executorSession sessionDataSize maxMemory)
     {
         this(
                 taskId,
                 executor,
                 session,
                 checkNotNull(maxMemory"maxMemory is null"),
                 true);
     }
 
     public TaskContext(TaskId taskIdExecutor executorSession sessionDataSize maxMemoryboolean cpuTimerEnabled)
     {
         this(
                 new TaskStateMachine(checkNotNull(taskId"taskId is null"), checkNotSameThreadExecutor(executor"executor is null")),
                 executor,
                 session,
                 checkNotNull(maxMemory"maxMemory is null"),
                 new DataSize(1, ),
                 true,
                 cpuTimerEnabled);
     }
 
    public TaskContext(TaskStateMachine taskStateMachine,
            Executor executor,
            Session session,
            DataSize maxMemory,
            DataSize operatorPreAllocatedMemory,
            boolean verboseStats,
            boolean cpuTimerEnabled)
    {
        this. = checkNotNull(taskStateMachine"taskStateMachine is null");
        this. = checkNotNull(executor"executor is null");
        this. = session;
        this. = checkNotNull(maxMemory"maxMemory is null").toBytes();
        this. = checkNotNull(operatorPreAllocatedMemory"operatorPreAllocatedMemory is null");
        taskStateMachine.addStateChangeListener(new StateChangeListener<TaskState>()
        {
            @Override
            public void stateChanged(TaskState newValue)
            {
                if (newValue.isDone()) {
                    .set(DateTime.now());
                    .set(System.nanoTime());
                }
            }
        });
        this. = verboseStats;
        this. = cpuTimerEnabled;
    }
    public TaskId getTaskId()
    {
        return .getTaskId();
    }
    public PipelineContext addPipelineContext(boolean inputPipelineboolean outputPipeline)
    {
        PipelineContext pipelineContext = new PipelineContext(thisinputPipelineoutputPipeline);
        .add(pipelineContext);
        return pipelineContext;
    }
    public Session getSession()
    {
        return ;
    }
    public void start()
    {
        if (!.compareAndSet(0, System.nanoTime())) {
            // already started
            return;
        }
        DateTime now = DateTime.now();
        .compareAndSet(nullnow);
        .set(now);
    }
    public void failed(Throwable cause)
    {
        .failed(cause);
    }
    public boolean isDone()
    {
        return .getState().isDone();
    }
    public TaskState getState()
    {
        return .getState();
    }
    public DataSize getMaxMemorySize()
    {
        return new DataSize().convertToMostSuccinctDataSize();
    }
    {
        return ;
    }
    public synchronized boolean reserveMemory(long bytes)
    {
        checkArgument(bytes >= 0, "bytes is negative");
        if (.get() + bytes > ) {
            return false;
        }
        .getAndAdd(bytes);
        return true;
    }
    public synchronized void freeMemory(long bytes)
    {
        checkArgument(bytes <= .get(), "tried to free more memory than is reserved");
        .getAndAdd(-bytes);
    }
    public boolean isVerboseStats()
    {
        return ;
    }
    public boolean isCpuTimerEnabled()
    {
        return ;
    }
    public CounterStat getInputDataSize()
    {
        CounterStat stat = new CounterStat();
        for (PipelineContext pipelineContext : ) {
            if (pipelineContext.isInputPipeline()) {
                stat.merge(pipelineContext.getInputDataSize());
            }
        }
        return stat;
    }
    {
        CounterStat stat = new CounterStat();
        for (PipelineContext pipelineContext : ) {
            if (pipelineContext.isInputPipeline()) {
                stat.merge(pipelineContext.getInputPositions());
            }
        }
        return stat;
    }
    {
        CounterStat stat = new CounterStat();
        for (PipelineContext pipelineContext : ) {
            if (pipelineContext.isOutputPipeline()) {
                stat.merge(pipelineContext.getOutputDataSize());
            }
        }
        return stat;
    }
    {
        CounterStat stat = new CounterStat();
        for (PipelineContext pipelineContext : ) {
            if (pipelineContext.isOutputPipeline()) {
                stat.merge(pipelineContext.getOutputPositions());
            }
        }
        return stat;
    }
    public TaskStats getTaskStats()
    {
        // check for end state to avoid callback ordering problems
        if (.getState().isDone()) {
            DateTime now = DateTime.now();
            if (.compareAndSet(nullnow)) {
                .compareAndSet(nullnow);
                .set(System.nanoTime());
            }
        }
        List<PipelineStatspipelineStats = ImmutableList.copyOf(transform(, PipelineContext::getPipelineStats));
        int totalDrivers = 0;
        int queuedDrivers = 0;
        int queuedPartitionedDrivers = 0;
        int runningDrivers = 0;
        int runningPartitionedDrivers = 0;
        int completedDrivers = 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;
        for (PipelineStats pipeline : pipelineStats) {
            totalDrivers += pipeline.getTotalDrivers();
            queuedDrivers += pipeline.getQueuedDrivers();
            queuedPartitionedDrivers += pipeline.getQueuedPartitionedDrivers();
            runningDrivers += pipeline.getRunningDrivers();
            runningPartitionedDrivers += pipeline.getRunningPartitionedDrivers();
            completedDrivers += pipeline.getCompletedDrivers();
            totalScheduledTime += pipeline.getTotalScheduledTime().roundTo();
            totalCpuTime += pipeline.getTotalCpuTime().roundTo();
            totalUserTime += pipeline.getTotalUserTime().roundTo();
            totalBlockedTime += pipeline.getTotalBlockedTime().roundTo();
            if (pipeline.isInputPipeline()) {
                rawInputDataSize += pipeline.getRawInputDataSize().toBytes();
                rawInputPositions += pipeline.getRawInputPositions();
                processedInputDataSize += pipeline.getProcessedInputDataSize().toBytes();
                processedInputPositions += pipeline.getProcessedInputPositions();
            }
            if (pipeline.isOutputPipeline()) {
                outputDataSize += pipeline.getOutputDataSize().toBytes();
                outputPositions += pipeline.getOutputPositions();
            }
        }
        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 TaskStats(
                .getCreatedTime(),
                .get(),
                .get(),
                .get(),
                elapsedTime.convertToMostSuccinctTimeUnit(),
                queuedTime.convertToMostSuccinctTimeUnit(),
                totalDrivers,
                queuedDrivers,
                queuedPartitionedDrivers,
                runningDrivers,
                runningPartitionedDrivers,
                completedDrivers,
                new DataSize(.get(), ).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,
                pipelineStats);
    }
New to GrepCode? Check out our FAQ X