<commit_before>/*
 * Copyright 2013 <NAME>
 *
 * 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.
 */

//--------------------------------------
//
// StandaloneCluster.scala
// Since: 2012/12/29 22:33
//
//--------------------------------------

package xerial.silk.cluster

import java.io.File
import xerial.core.io.Path._
import xerial.silk.cluster.ZooKeeper.{ZkStandalone, ZkQuorumPeer}
import xerial.silk.util.ThreadUtil
import xerial.core.log.Logger
import xerial.silk.cluster.SilkClient.{SilkClientRef, RegisterClassBox, Terminate, ClientInfo}
import xerial.core.util.Shell
import xerial.silk.cluster._
import com.netflix.curator.test.{InstanceSpec, TestingServer, TestingZooKeeperServer}
import xerial.core.io.IOUtil
import akka.pattern.{AskTimeoutException, ask}
import akka.util.Timeout
import scala.concurrent.duration._
import scala.concurrent.Await
import java.util.concurrent.TimeoutException


object StandaloneCluster {

  val lh = Host("localhost", "127.0.0.1")


  def randomConfig : Config = {
    val tmpDir : File = IOUtil.createTempDir(new File("target"), "silk-tmp").getAbsoluteFile
    val zkClientPort = IOUtil.randomPort
    val zkLeaderElectionPort = IOUtil.randomPort
    val zkQuorumPort = IOUtil.randomPort

    val config = Config(silkHome=tmpDir,
      silkClientPort = IOUtil.randomPort,
      silkMasterPort = IOUtil.randomPort,
      dataServerPort = IOUtil.randomPort,
      zk=ZkConfig(
        zkServers = Some(Seq(new ZkEnsembleHost(lh, clientPort=zkClientPort, leaderElectionPort = zkLeaderElectionPort, quorumPort = zkQuorumPort))),
        clientPort = zkClientPort,
        quorumPort = zkQuorumPort,
        leaderElectionPort = zkLeaderElectionPort
      ))

    Runtime.getRuntime.addShutdownHook(new Thread(new Runnable {
      def run() {
        // delete on exit
        tmpDir.rmdirs
      }
    }))

    config
  }

  def withCluster(f: => Unit) {
    val tmpDir : File = IOUtil.createTempDir(new File("target"), "silk-tmp").getAbsoluteFile
    var cluster : Option[StandaloneCluster] = None
    try {
      withConfig(randomConfig) {
        cluster = Some(new StandaloneCluster)
        cluster map (_.start)
        f
      }
    }
    finally {
      cluster.map(_.stop)
    }
  }

  def withClusterAndClient(f:SilkClientRef => Unit) {
    withCluster {
      SilkClient.startClient(lh, config.zk.zkServersConnectString) { client =>
        f(client)<commit_msg>remove unused code<commit_after>/*
 * Copyright 2013 <NAME>
 *
 * 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.
 */

//--------------------------------------
//
// StandaloneCluster.scala
// Since: 2012/12/29 22:33
//
//--------------------------------------

package xerial.silk.cluster

import java.io.File
import xerial.core.io.Path._
import xerial.silk.cluster.ZooKeeper.{ZkStandalone, ZkQuorumPeer}
import xerial.silk.util.ThreadUtil
import xerial.core.log.Logger
import xerial.silk.cluster.SilkClient.{SilkClientRef, RegisterClassBox, Terminate, ClientInfo}
import xerial.core.util.Shell
import xerial.silk.cluster._
import com.netflix.curator.test.{InstanceSpec, TestingServer, TestingZooKeeperServer}
import xerial.core.io.IOUtil
import akka.pattern.{AskTimeoutException, ask}
import akka.util.Timeout
import scala.concurrent.duration._
import scala.concurrent.Await
import java.util.concurrent.TimeoutException


object StandaloneCluster {

  val lh = Host("localhost", "127.0.0.1")


  def randomConfig : Config = {
    val tmpDir : File = IOUtil.createTempDir(new File("target"), "silk-tmp").getAbsoluteFile
    val zkClientPort = IOUtil.randomPort
    val zkLeaderElectionPort = IOUtil.randomPort
    val zkQuorumPort = IOUtil.randomPort

    val config = Config(silkHome=tmpDir,
      silkClientPort = IOUtil.randomPort,
      silkMasterPort = IOUtil.randomPort,
      dataServerPort = IOUtil.randomPort,
      zk=ZkConfig(
        zkServers = Some(Seq(new ZkEnsembleHost(lh, clientPort=zkClientPort, leaderElectionPort = zkLeaderElectionPort, quorumPort = zkQuorumPort))),
        clientPort = zkClientPort,
        quorumPort = zkQuorumPort,
        leaderElectionPort = zkLeaderElectionPort
      ))

    Runtime.getRuntime.addShutdownHook(new Thread(new Runnable {
      def run() {
        // delete on exit
        tmpDir.rmdirs
      }
    }))

    config
  }

  def withCluster(f: => Unit) {
    var cluster : Option[StandaloneCluster] = None
    try {
      withConfig(randomConfig) {
        cluster = Some(new StandaloneCluster)
        cluster map (_.start)
        f
      }
    }
    finally {
      cluster.map(_.stop)
    }
  }

  def withClusterAndClient(f:SilkClientRef => Unit) {
    withCluster {
      SilkClient.startClient(lh, config.zk.zkServersConnectString) { client =>
        f(client)
{'fraction_non_alphanumeric': 0.11899791231732777, 'fraction_numerical': 0.009046624913013222, 'mean_word_length': 3.112303290414878, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 2, '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': '1465096', 'n_tokens_mistral': 1780, 'n_tokens_neox': 1735, 'n_words': 425}