<commit_before>package cz.seznam.euphoria.flink;

import cz.seznam.euphoria.core.client.flow.Flow;
import cz.seznam.euphoria.core.client.io.DataSink;
import cz.seznam.euphoria.core.executor.Executor;
import cz.seznam.euphoria.flink.translation.FlowTranslator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.concurrent.Future;

/**
 * Executor implementation using Apache Flink as a runtime
 */
public class FlinkExecutor implements Executor {

  private static final Logger LOG = LoggerFactory.getLogger(FlinkExecutor.class);

  private StreamExecutionEnvironment flinkStreamEnv;

  public FlinkExecutor() {
    this(StreamExecutionEnvironment.getExecutionEnvironment());
  }

  public FlinkExecutor(StreamExecutionEnvironment flinkStreamEnv) {
    this.flinkStreamEnv = flinkStreamEnv;
  }

  @Override
  public Future<Integer> submit(Flow flow) {
    throw new UnsupportedOperationException();
  }

  @Override
  public int waitForCompletion(Flow flow) throws Exception {
    FlowTranslator translator = new FlowTranslator();
    List<DataSink<?>> sinks = translator.translateInto(flow, flinkStreamEnv);

    try {
      flinkStreamEnv.execute(); // blocking operation
    } catch (Exception e) {
      // when exception thrown rollback all sinks
      for (DataSink s : sinks) {
        try {
          s.rollback();
        } catch (Exception ex) {
          LOG.error("Exception during DataSink rollback", ex);
        }
      }
      throw e;
    }

    // when the execution is successful commit all sinks
    Exception ex = null;
    for (DataSink s : sinks) {
      try {
        s.commit();
      } catch (Exception e) {
        // save exception for later and try to commit rest of the sinks
        ex = e;
      }
    }

    // rethrow the exception if any
    if (ex != null) throw ex;

    return 0;
  }
}
<commit_msg>#! [euphoria-flink] Dump execution plan prio to flow execution<commit_after>package cz.seznam.euphoria.flink;

import cz.seznam.euphoria.core.client.flow.Flow;
import cz.seznam.euphoria.core.client.io.DataSink;
import cz.seznam.euphoria.core.executor.Executor;
import cz.seznam.euphoria.flink.translation.FlowTranslator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.concurrent.Future;

/**
 * Executor implementation using Apache Flink as a runtime
 */
public class FlinkExecutor implements Executor {

  private static final Logger LOG = LoggerFactory.getLogger(FlinkExecutor.class);

  private StreamExecutionEnvironment flinkStreamEnv;
  private boolean dumpExecPlan;

  public FlinkExecutor() {
    this(StreamExecutionEnvironment.getExecutionEnvironment());
  }

  public FlinkExecutor(StreamExecutionEnvironment flinkStreamEnv) {
    this.flinkStreamEnv = flinkStreamEnv;
  }

  /**
   * Specify whether to dump the flink execution plan before executing
   * a flow using {@link #waitForCompletion(Flow)}.
   */
  public void setDumpExecutionPlan(boolean dumpExecPlan) {
    this.dumpExecPlan = dumpExecPlan;
  }

  @Override
  public Future<Integer> submit(Flow flow) {
    throw new UnsupportedOperationException();
  }

  @Override
  public int waitForCompletion(Flow flow) throws Exception {
    FlowTranslator translator = new FlowTranslator();
    List<DataSink<?>> sinks = translator.translateInto(flow, flinkStreamEnv);

    if (dumpExecPlan) {
      LOG.info("Flink execution plan for {}: {}",
          flow.getName(), flinkStreamEnv.getExecutionPlan());
    }

    try {
      flinkStreamEnv.execute(); // blocking operation
    } catch (Exception e) {
      // when exception thrown rollback all sinks
      for (DataSink s : sinks) {
        try {
          s.rollback();
        } catch (Exception ex) {
          LOG.error("Exception during DataSink rollback", ex);
        }
      }
      throw e;
    }

    // when the execution is successful commit all sinks
    Exception ex = null;
    for (DataSink s : sinks) {
      try {
        s.commit();
      } catch (Exception e) {
        // save exception for later and try to commit rest of the sinks
        ex = e;
      }
    }

    // rethrow the exception if any
    if (ex != null) throw ex;

    return 0;
  }
}

{'fraction_non_alphanumeric': 0.09486887115165336, 'fraction_numerical': 0.0013683010262257698, 'mean_word_length': 3.213256484149856, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3022095', 'n_tokens_mistral': 1318, 'n_tokens_neox': 1287, 'n_words': 336}