<commit_before>/*
 * Copyright 2004-present Facebook. All Rights Reserved.
 */
package com.facebook.presto.server;

import com.facebook.presto.operator.Page;
import com.facebook.presto.tuple.TupleInfo;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.primitives.Ints;
import io.airlift.units.DataSize;
import io.airlift.units.DataSize.Unit;
import io.airlift.units.Duration;

import javax.annotation.concurrent.GuardedBy;
import javax.annotation.concurrent.ThreadSafe;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

/**
 * QueryState contains the current state of the query and output buffer.
 */
@ThreadSafe
public class QueryState
{
    public static enum State
    {
        PREPARING,
        RUNNING,
        FINISHED,
        CANCELED,
        FAILED
    }

    private final List<TupleInfo> tupleInfos;

    @GuardedBy("pageBuffer")
    private final ArrayDeque<Page> pageBuffer;

    @GuardedBy("pageBuffer")
    private State state = State.PREPARING;

    @GuardedBy("pageBuffer")
    private final List<Throwable> causes = new ArrayList<>();

    @GuardedBy("pageBuffer")
    private int sourceCount;

    private final int splits;
    private final AtomicInteger startedSplits = new AtomicInteger();
    private final AtomicInteger completedSplits = new AtomicInteger();

    private final AtomicLong splitCpuMillis = new AtomicLong();

    private final AtomicLong inputDataSize = new AtomicLong();
    private final AtomicLong inputPositions = new AtomicLong();

    private final AtomicLong completedDataSize = new AtomicLong();
    private final AtomicLong completedPositions = new AtomicLong();

    private final Semaphore notFull;
    private final Semaphore notEmpty;

    public QueryState(List<TupleInfo> tupleInfos, int sourceCount, int pageBufferMax)
    {
        this(tupleInfos, sourceCount, pageBufferMax, 0);
    }

    public QueryState(List<TupleInfo> tupleInfos, int sourceCount, int pageBufferMax, int splits)
    {
        Preconditions.checkNotNull(tupleInfos, "tupleInfos is null");
        Preconditions.checkArgument(sourceCount > 0, "sourceCount must be at least 1");
        Preconditions.checkArgument(pageBufferMax > 0, "pageBufferMax must be at least 1");

        this.tupleInfos = tupleInfos;
        this.sourceCount = sourceCount;
        this.pageBuffer = new ArrayDeque<>(pageBufferMax);
        this.splits = splits;
        this.notFull = new Semaphore(pageBufferMax);
        this.notEmpty = new Semaphore(0);
    }

    public List<TupleInfo> getTupleInfos()
    {
        return tupleInfos;
    }

    public State getState()
    {
        synchronized (pageBuffer) {
            return state;
        }
    }

    public boolean isDone()
    {
        synchronized (pageBuffer) {
            return state == State.FINISHED || state == State.CANCELED || state == State.FAILED;
        }
    }

    public boolean isFailed()
    {
        synchronized (pageBuffer) {
            return state == State.FAILED;
        }
    }

    public boolean isCanceled()
    {
        synchronized (pageBuffer) {
            return state == State.CANCELED;
        }
    }

    public int getSplits()
    {
        return splits;
    }

    public int getStartedSplits()
    {
        return Ints.min(startedSplits.get(), completedSplits.get(), splits);
    }

    public int getCompletedSplits()
    {
        return Ints.min(completedSplits.get(), splits);
    }
<commit_msg>Fix bug in reporting of running tasks<commit_after>/*
 * Copyright 2004-present Facebook. All Rights Reserved.
 */
package com.facebook.presto.server;

import com.facebook.presto.operator.Page;
import com.facebook.presto.tuple.TupleInfo;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.primitives.Ints;
import io.airlift.units.DataSize;
import io.airlift.units.DataSize.Unit;
import io.airlift.units.Duration;

import javax.annotation.concurrent.GuardedBy;
import javax.annotation.concurrent.ThreadSafe;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

/**
 * QueryState contains the current state of the query and output buffer.
 */
@ThreadSafe
public class QueryState
{
    public static enum State
    {
        PREPARING,
        RUNNING,
        FINISHED,
        CANCELED,
        FAILED
    }

    private final List<TupleInfo> tupleInfos;

    @GuardedBy("pageBuffer")
    private final ArrayDeque<Page> pageBuffer;

    @GuardedBy("pageBuffer")
    private State state = State.PREPARING;

    @GuardedBy("pageBuffer")
    private final List<Throwable> causes = new ArrayList<>();

    @GuardedBy("pageBuffer")
    private int sourceCount;

    private final int splits;
    private final AtomicInteger startedSplits = new AtomicInteger();
    private final AtomicInteger completedSplits = new AtomicInteger();

    private final AtomicLong splitCpuMillis = new AtomicLong();

    private final AtomicLong inputDataSize = new AtomicLong();
    private final AtomicLong inputPositions = new AtomicLong();

    private final AtomicLong completedDataSize = new AtomicLong();
    private final AtomicLong completedPositions = new AtomicLong();

    private final Semaphore notFull;
    private final Semaphore notEmpty;

    public QueryState(List<TupleInfo> tupleInfos, int sourceCount, int pageBufferMax)
    {
        this(tupleInfos, sourceCount, pageBufferMax, 0);
    }

    public QueryState(List<TupleInfo> tupleInfos, int sourceCount, int pageBufferMax, int splits)
    {
        Preconditions.checkNotNull(tupleInfos, "tupleInfos is null");
        Preconditions.checkArgument(sourceCount > 0, "sourceCount must be at least 1");
        Preconditions.checkArgument(pageBufferMax > 0, "pageBufferMax must be at least 1");

        this.tupleInfos = tupleInfos;
        this.sourceCount = sourceCount;
        this.pageBuffer = new ArrayDeque<>(pageBufferMax);
        this.splits = splits;
        this.notFull = new Semaphore(pageBufferMax);
        this.notEmpty = new Semaphore(0);
    }

    public List<TupleInfo> getTupleInfos()
    {
        return tupleInfos;
    }

    public State getState()
    {
        synchronized (pageBuffer) {
            return state;
        }
    }

    public boolean isDone()
    {
        synchronized (pageBuffer) {
            return state == State.FINISHED || state == State.CANCELED || state == State.FAILED;
        }
    }

    public boolean isFailed()
    {
        synchronized (pageBuffer) {
            return state == State.FAILED;
        }
    }

    public boolean isCanceled()
    {
        synchronized (pageBuffer) {
            return state == State.CANCELED;
        }
    }

    public int getSplits()
    {
        return splits;
    }

    public int getStartedSplits()
    {
        return Ints.min(startedSplits.get(), splits);
    }

    public int getCompletedSplits()
    {
        return Ints.min(completedSplits.get(), startedSplits.get(), splits);
    }

{'fraction_non_alphanumeric': 0.09196925523456136, 'fraction_numerical': 0.0026504108136761197, 'mean_word_length': 3.1038607939097336, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1373661', 'n_tokens_mistral': 2231, 'n_tokens_neox': 2126, 'n_words': 532}