<commit_before>/*
 * Licensed to Crate under one or more contributor license agreements.
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.  Crate licenses this file
 * to you 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.
 *
 * However, if you have executed another commercial license agreement
 * with Crate these terms will supersede the license and you may use the
 * software solely pursuant to the terms of the relevant commercial
 * agreement.
 */

package io.crate.executor.transport.executionphases;

import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import io.crate.core.collections.Row;
import io.crate.exceptions.Exceptions;
import io.crate.executor.transport.kill.KillJobsRequest;
import io.crate.executor.transport.kill.KillResponse;
import io.crate.executor.transport.kill.TransportKillJobsNodeAction;
import io.crate.operation.projectors.*;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.Loggers;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

class InterceptingRowReceiver implements RowReceiver, FutureCallback<Void> {

    private final static ESLogger LOGGER = Loggers.getLogger(InterceptingRowReceiver.class);

    private final AtomicInteger upstreams = new AtomicInteger(2);
    private final UUID jobId;
    private final RowReceiver rowReceiver;
    private final TransportKillJobsNodeAction transportKillJobsNodeAction;
    private final AtomicBoolean rowReceiverDone = new AtomicBoolean(false);
    private Throwable failure;

    InterceptingRowReceiver(UUID jobId,
                            RowReceiver rowReceiver,
                            InitializationTracker jobsInitialized,
                            TransportKillJobsNodeAction transportKillJobsNodeAction) {
        this.jobId = jobId;
        this.rowReceiver = rowReceiver;
        this.transportKillJobsNodeAction = transportKillJobsNodeAction;
        Futures.addCallback(jobsInitialized.future, this);
    }

    @Override
    public void fail(Throwable throwable) {
        if (rowReceiverDone.compareAndSet(false, true)) {
            tryForwardResult(throwable);
        }
    }

    @Override
    public void kill(Throwable throwable) {
        fail(throwable);
    }

    @Override
    public void prepare() {
    }

    @Override
    public Set<Requirement> requirements() {
        return Requirements.NO_REQUIREMENTS;
    }

    @Override
    public Result setNextRow(Row row) {
        return rowReceiver.setNextRow(row);
    }

    @Override
    public void pauseProcessed(ResumeHandle resumeable) {
        rowReceiver.pauseProcessed(resumeable);
    }

    @Override
    public void finish(RepeatHandle repeatHandle) {
        fail(null);
    }

    @Override
    public void onSuccess(@Nullable Void result) {
        tryForwardResult(null);
    }

    @Override
    public void onFailure(@Nonnull Throwable t) {
        tryForwardResult(t);
    }

    private void tryForwardResult(Throwable throwable) {
        if (throwable != null && (failure == null || failure instanceof InterruptedException)) {
            failure = Exceptions.unwrap(throwable);
        }
        if (upstreams.decrementAndGet() > 0) {
            return;
        }
        if (failure == null) {
            rowReceiver.finish(RepeatHandle.UNSUPPORTED);
        } else {
            transportKillJobsNodeAction.executeKillOnAllNodes(
                new KillJobsRequest(Collections.singletonList(jobId)), new ActionListener<KillResponse>() {
                    @Override
                    public void onResponse(KillResponse killResponse) {
                        LOGGER.debug("Killed {} jobs before forwarding the failure", killResponse.numKilled());
                        rowReceiver.fail(failure);
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        LOGGER.warn("Failed to kill job, forwarding failure anyway..", e);
                        rowReceiver.fail(failure);
                    }
                });
        }
    }<commit_msg>changes kill-on-failure loggers to TRACE level<commit_after>/*
 * Licensed to Crate under one or more contributor license agreements.
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.  Crate licenses this file
 * to you 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.
 *
 * However, if you have executed another commercial license agreement
 * with Crate these terms will supersede the license and you may use the
 * software solely pursuant to the terms of the relevant commercial
 * agreement.
 */

package io.crate.executor.transport.executionphases;

import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import io.crate.core.collections.Row;
import io.crate.exceptions.Exceptions;
import io.crate.executor.transport.kill.KillJobsRequest;
import io.crate.executor.transport.kill.KillResponse;
import io.crate.executor.transport.kill.TransportKillJobsNodeAction;
import io.crate.operation.projectors.*;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.Loggers;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

class InterceptingRowReceiver implements RowReceiver, FutureCallback<Void> {

    private final static ESLogger LOGGER = Loggers.getLogger(InterceptingRowReceiver.class);

    private final AtomicInteger upstreams = new AtomicInteger(2);
    private final UUID jobId;
    private final RowReceiver rowReceiver;
    private final TransportKillJobsNodeAction transportKillJobsNodeAction;
    private final AtomicBoolean rowReceiverDone = new AtomicBoolean(false);
    private Throwable failure;

    InterceptingRowReceiver(UUID jobId,
                            RowReceiver rowReceiver,
                            InitializationTracker jobsInitialized,
                            TransportKillJobsNodeAction transportKillJobsNodeAction) {
        this.jobId = jobId;
        this.rowReceiver = rowReceiver;
        this.transportKillJobsNodeAction = transportKillJobsNodeAction;
        Futures.addCallback(jobsInitialized.future, this);
    }

    @Override
    public void fail(Throwable throwable) {
        if (rowReceiverDone.compareAndSet(false, true)) {
            tryForwardResult(throwable);
        }
    }

    @Override
    public void kill(Throwable throwable) {
        fail(throwable);
    }

    @Override
    public void prepare() {
    }

    @Override
    public Set<Requirement> requirements() {
        return Requirements.NO_REQUIREMENTS;
    }

    @Override
    public Result setNextRow(Row row) {
        return rowReceiver.setNextRow(row);
    }

    @Override
    public void pauseProcessed(ResumeHandle resumeable) {
        rowReceiver.pauseProcessed(resumeable);
    }

    @Override
    public void finish(RepeatHandle repeatHandle) {
        fail(null);
    }

    @Override
    public void onSuccess(@Nullable Void result) {
        tryForwardResult(null);
    }

    @Override
    public void onFailure(@Nonnull Throwable t) {
        tryForwardResult(t);
    }

    private void tryForwardResult(Throwable throwable) {
        if (throwable != null && (failure == null || failure instanceof InterruptedException)) {
            failure = Exceptions.unwrap(throwable);
        }
        if (upstreams.decrementAndGet() > 0) {
            return;
        }
        if (failure == null) {
            rowReceiver.finish(RepeatHandle.UNSUPPORTED);
        } else {
            transportKillJobsNodeAction.executeKillOnAllNodes(
                new KillJobsRequest(Collections.singletonList(jobId)), new ActionListener<KillResponse>() {
                    @Override
                    public void onResponse(KillResponse killResponse) {
                        LOGGER.trace("Killed {} jobs before forwarding the failure", killResponse.numKilled());
                        rowReceiver.fail(failure);
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        LOGGER.trace("Failed to kill job, forwarding failure anyway...", e);
                        rowReceiver.fail(failure);
                    }
                });
        }
    }
{'fraction_non_alphanumeric': 0.07525574799959485, 'fraction_numerical': 0.001215436037678517, 'mean_word_length': 2.852516582130316, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1468882', 'n_tokens_mistral': 2536, 'n_tokens_neox': 2470, 'n_words': 697}