<commit_before>package org.hammerlab.guacamole.commands

import org.apache.spark.{SparkConf, SparkContext}

import scala.collection.mutable

abstract class SparkCommand[T <: Args: Manifest] extends Command[T] {
  override def run(args: T): Unit = {
    val sc = createSparkContext()
    try {
      args.validate(sc)
      run(args, sc)
    } finally {
      sc.stop()
    }
  }

  def run(args: T, sc: SparkContext): Unit

  private val defaultConfs = mutable.HashMap[String, String]()
  def setDefaultConf(key: String, value: String): Unit = {
    defaultConfs.update(key, value)
  }

  /**
   * Return a spark context.
   *
   * Typically, most properties are set through config file / cmd-line.
   * @return
   */
  private def createSparkContext(): SparkContext = {
    val config: SparkConf = new SparkConf()

    config.getOption("spark.app.name") match {
      case Some(cmdLineName) => config.setAppName(s"$cmdLineName: $name")
      case _                 => config.setAppName(s"guacamole: $name")
    }

    if (config.getOption("spark.master").isEmpty) {
      val numProcessors = Runtime.getRuntime.availableProcessors()
      config.setMaster(s"local[$numProcessors]")
      info(s"Running in local mode with $numProcessors 'executors'")
    }

    if (config.getOption("spark.serializer").isEmpty) {
      config.set("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
    }

    if (config.getOption("spark.kryo.registrator").isEmpty) {
      config.set("spark.kryo.registrator", "org.hammerlab.guacamole.kryo.Registrar")
    }

    if (config.getOption("spark.kryoserializer.buffer").isEmpty) {
      config.set("spark.kryoserializer.buffer", "4mb")
    }

    if (config.getOption("spark.kryo.referenceTracking").isEmpty) {
      config.set("spark.kryo.referenceTracking", "true")
    }

    for {
      (k, v) <- defaultConfs
    } {
      config.set(k, v)
    }

    new SparkContext(config)
  }
}<commit_msg>require kryo-registration outside of spark-* entry points<commit_after>package org.hammerlab.guacamole.commands

import org.apache.spark.{SparkConf, SparkContext}

import scala.collection.mutable

abstract class SparkCommand[T <: Args: Manifest] extends Command[T] {
  override def run(args: T): Unit = {
    val sc = createSparkContext()
    try {
      args.validate(sc)
      run(args, sc)
    } finally {
      sc.stop()
    }
  }

  def run(args: T, sc: SparkContext): Unit

  private val defaultConfs = mutable.HashMap[String, String]()
  def setDefaultConf(key: String, value: String): Unit = {
    defaultConfs.update(key, value)
  }

  /**
   * Return a spark context.
   *
   * Typically, most properties are set through config file / cmd-line.
   * @return
   */
  private def createSparkContext(): SparkContext = {
    val config: SparkConf = new SparkConf()

    config.getOption("spark.app.name") match {
      case Some(cmdLineName) => config.setAppName(s"$cmdLineName: $name")
      case _                 => config.setAppName(s"guacamole: $name")
    }

    if (config.getOption("spark.master").isEmpty) {
      val numProcessors = Runtime.getRuntime.availableProcessors()
      config.setMaster(s"local[$numProcessors]")
      info(s"Running in local mode with $numProcessors 'executors'")
    }

    if (config.getOption("spark.serializer").isEmpty) {
      config.set("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
    }

    if (config.getOption("spark.kryo.registrator").isEmpty) {
      config.set("spark.kryo.registrator", "org.hammerlab.guacamole.kryo.Registrar")
    }

    if (config.getOption("spark.kryoserializer.buffer").isEmpty) {
      config.set("spark.kryoserializer.buffer", "4mb")
    }

    if (config.getOption("spark.kryo.referenceTracking").isEmpty) {
      config.set("spark.kryo.referenceTracking", "true")
    }

    if (config.getOption("spark.kryo.registrationRequired").isEmpty) {
      config.set("spark.kryo.registrationRequired", "true")
    }

    for {
      (k, v) <- defaultConfs
    } {
      config.set(k, v)
    }

    new SparkContext(config)
  }
}
{'fraction_non_alphanumeric': 0.1374139626352016, 'fraction_numerical': 0.0004916420845624386, 'mean_word_length': 3.2341311134235173, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '258232', 'n_tokens_mistral': 1397, 'n_tokens_neox': 1326, 'n_words': 242}