java.util.concurrent.TimeoutException Scala Examples

The following examples show how to use java.util.concurrent.TimeoutException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: TimeLimitedFutureSpec.scala    From gfc-concurrent   with Apache License 2.0 9 votes vote down vote up
package com.gilt.gfc.concurrent

import java.util.concurrent.TimeoutException
import scala.concurrent.{ Future, Await }
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration.Duration
import org.scalatest.{WordSpec, Matchers}

class TimeLimitedFutureSpec extends WordSpec with Matchers {
  import TimeLimitedFutureSpec._

  "RichFuture" when {
    import ScalaFutures._

    "waiting for a result to happen" should {
      "return the completed original Future if it completes before the given timeout" in {
        val now = System.currentTimeMillis
        val future: Future[String] = (Future { Thread.sleep(1000); "Here I am" }).withTimeout(Duration(5, "seconds"))
        val msg: String = Await.result(future, Duration(10, "seconds"))
        val elapsed = (System.currentTimeMillis - now)
        msg should equal ("Here I am")
        elapsed should be (2000L +- 1000L)
      }

      "return the failure of the original Future if it fails before the given timeout" in {
        val now = System.currentTimeMillis
        val future = (Future { Thread.sleep(1000); throw new NullPointerException("That hurts!") }).withTimeout(Duration(5, "seconds"))
        a [NullPointerException] should be thrownBy { Await.result(future, Duration(10, "seconds")) }
        val elapsed = (System.currentTimeMillis - now)
        elapsed should be (2000L +- 1000L)
      }

      "return the timeout of the original Future if it had one and it went off and was shorter than the given one" in {
        val now = System.currentTimeMillis
        val timingOutEarlier = Timeouts.timeout(Duration(1, "seconds"))
        val future = timingOutEarlier.withTimeout(Duration(5, "seconds"))
        a [TimeoutException] should be thrownBy { Await.result(future, Duration(10, "seconds")) }
        val elapsed: Long = (System.currentTimeMillis - now)
        elapsed should be >= 500l
        elapsed should be <= 4000l
      }

      "return the timeout if the original Future does not timeout of its own" in {
        val now = System.currentTimeMillis
        val timingOutLater = Timeouts.timeout(Duration(3, "seconds"))
        val future = timingOutLater.withTimeout(Duration(1, "seconds"))
        a [TimeoutException] should be thrownBy  { Await.result(future, Duration(10, "seconds")) }
        val elapsed: Long = (System.currentTimeMillis - now)
        elapsed should be >= 1000l
        elapsed should be <= 2500l
      }
    }

    // an example of how it could be used
    "used in our most common use case" should {
      "fit nicely" in {
        val call: Future[String] = svcCall(1000).withTimeout(Duration(5000, "milliseconds")).recover {
          case _: TimeoutException => "recover.timeout"
          case other => s"recover.${other.getMessage}"
        }
        Await.result(call, Duration(10, "seconds")) should be ("data-1000")

        val call2: Future[String] = svcCall(5000).withTimeout(Duration(1000, "milliseconds")).recover {
          case _: TimeoutException => "recover.timeout"
          case other => s"recover.${other.getMessage}"
        }
        Await.result(call2, Duration(10, "seconds")) should be ("recover.timeout")
      }
    }
  }
}

object TimeLimitedFutureSpec {
  def svcCall(latency: Long): Future[String] = Future { Thread.sleep(latency); s"data-${latency}" }
} 
Example 2
Source File: KillableSingleThread.scala    From shellbase   with Apache License 2.0 5 votes vote down vote up
package com.sumologic.shellbase.interrupts

import java.util.concurrent.TimeoutException

import scala.concurrent.duration.Duration
import scala.concurrent.{Await, Future, Promise}
import scala.util.Try


class KillableSingleThread[T](fn: => T) {

  private val resultPromise = Promise[T]()
  private val thread = new Thread("killable-thread") {
    override def run(): Unit = {
      resultPromise.tryComplete(Try(fn))
    }
  }

  private def interrupt(): Unit = {
    thread.interrupt()
  }

  private def stop(): Unit = {
    //noinspection ScalaDeprecation
    thread.stop()
    resultPromise.tryFailure(new ThreadDeath)
  }

  def future: Future[T] = resultPromise.future

  def start(): Unit = {
    thread.start()
  }

  def waitForCompletion(waitDuration: Duration): Boolean = {
    try {
      Await.ready(resultPromise.future, waitDuration)
      true
    } catch {
      case _: TimeoutException => false
      case _: Throwable => future.isCompleted
    }
  }

  def kill(gracePeriod: Duration): Unit = {
    interrupt()
    if (!waitForCompletion(gracePeriod)) {
      stop()
    }
  }
} 
Example 3
Source File: Dex2JawaConverter.scala    From Argus-SAF   with Apache License 2.0 5 votes vote down vote up
package org.argus.amandroid.core.decompile

import java.io._

import org.argus.jawa.core.util._
import java.net.URI
import java.util.concurrent.TimeoutException

import org.argus.amandroid.core.dedex.JawaDeDex
import org.argus.amandroid.core.util.FixResources
import org.xml.sax.SAXParseException

 
object ConverterUtil {

  def copy(srcUri: FileResourceUri, destUri: FileResourceUri) {
      def copyFile(f: File) {
        try {
          val fin = new FileInputStream(f)
          val dest = new File(new File(new URI(destUri)), f.getName)
          val fout = new FileOutputStream(dest)
          val buffer = new Array[Byte](1024)
          var bytesRead = fin.read(buffer)
          while (bytesRead > 0) {
            fout.write(buffer, 0, bytesRead)
            bytesRead = fin.read(buffer)
          }
          fin.close()
          fout.close()
        } catch {
          case e: Exception =>
            e.printStackTrace()
        }
      }

    val src = new File(new URI(srcUri))
//    val dest = new File(new URI(destUri))

    if (src.exists() && src.isDirectory) {
      src.listFiles().foreach { f =>
        if (f.isFile) {
          copyFile(f)
        }
      }
    }
  }

  def cleanDir(dirUri: FileResourceUri) {
    val dir = new File(new URI(dirUri))
    if (dir.exists)
      dir.listFiles.foreach { f =>
        if (f.isDirectory) {
          cleanDir(f.getAbsoluteFile.toURI.toASCIIString)
        }
        f.delete()
      }
  }
} 
Example 4
Source File: CHA.scala    From Argus-SAF   with Apache License 2.0 5 votes vote down vote up
package org.argus.jawa.flow.cg

import java.util.concurrent.TimeoutException

import hu.ssh.progressbar.ConsoleProgressBar
import org.argus.jawa.core.ast.CallStatement
import org.argus.jawa.core.elements.Signature
import org.argus.jawa.core.util._
import org.argus.jawa.core.{Global, JawaMethod}
import org.argus.jawa.flow.interprocedural.{CallHandler, IndirectCallResolver}
import org.argus.jawa.flow.pta.PTAScopeManager

import scala.concurrent.duration._
import scala.language.postfixOps


object CHA {
  final val TITLE = "CHA"

  def apply(
      global: Global,
      entryPoints: ISet[Signature],
      timer: Option[MyTimeout] = Some(new MyTimeout(1 minutes))): CallGraph = build(global, entryPoints, timer)

  def build(
      global: Global,
      entryPoints: ISet[Signature],
      timer: Option[MyTimeout]): CallGraph = {
    val cg = new CallGraph
    val processed: MSet[String] = msetEmpty

    def handleEntryPoint: Signature => Unit = { ep =>
      if(timer.isDefined) timer.get.refresh()
      try {
        val epmopt = global.getMethodOrResolve(ep)
        epmopt match {
          case Some(epm) =>
            if (!PTAScopeManager.shouldBypass(epm.getDeclaringClass) && epm.isConcrete) {
              sbcg(global, epm, cg, processed, timer)
            }
          case None =>
        }
      } catch {
        case te: TimeoutException =>
          global.reporter.error(TITLE, ep + ": " + te.getMessage)
      }
    }
    val progressBar = ConsoleProgressBar.on(System.out).withFormat("[:bar] :percent% :elapsed Left: :remain")
    ProgressBarUtil.withProgressBar("Building Signature Based Call Graph...", progressBar)(entryPoints, handleEntryPoint)

    global.reporter.println(s"$TITLE done with method size ${processed.size}.")
    cg
  }

  private def sbcg(global: Global, ep: JawaMethod, cg: CallGraph, processed: MSet[String], timer: Option[MyTimeout]): Unit = {
    cg.addCalls(ep.getSignature, isetEmpty)
    val worklistAlgorithm = new WorklistAlgorithm[JawaMethod] {
      override def processElement(m: JawaMethod): Unit = {
        if(timer.isDefined) timer.get.timeoutThrow()
        processed += m.getSignature.signature
        try {
          m.getBody.resolvedBody.locations foreach { l =>
            l.statement match {
              case cs: CallStatement =>
                val callee: MSet[JawaMethod] = msetEmpty
                IndirectCallResolver.getCallResolver(global, cs.signature.classTyp, cs.signature.getSubSignature) match {
                  case Some(res) =>
                    callee ++= res.guessCallTarget(global, cs.signature)
                  case None =>
                    callee ++= CallHandler.resolveSignatureBasedCall(global, cs.signature, cs.kind)
                }
                callee foreach { callee =>
                  cg.addCall(m.getSignature, callee.getSignature)
                  if (!processed.contains(callee.getSignature.signature) && !PTAScopeManager.shouldBypass(callee.getDeclaringClass) && callee.isConcrete) {
                    worklist +:= callee
                  }
                }
              case _ =>
            }
          }
        } catch {
          case e: Throwable => global.reporter.warning(TITLE, e.getMessage)
        }
      }
    }
    worklistAlgorithm.run(worklistAlgorithm.worklist +:= ep)
  }
} 
Example 5
Source File: MyTimeout.scala    From Argus-SAF   with Apache License 2.0 5 votes vote down vote up
package org.argus.jawa.core.util

import scala.concurrent.duration.FiniteDuration
import java.util.concurrent.TimeoutException

class MyTimeout(time: FiniteDuration) {
  private final var startTime: Long = System.currentTimeMillis()
  def refresh(): Unit = this.startTime = System.currentTimeMillis()
  def isTimeout: Boolean = {
    val currentTime = System.currentTimeMillis()
    (currentTime - startTime) >= time.toMillis
  }
  def timeoutThrow(): Unit = {
    if(isTimeout) throw new TimeoutException("Timeout after " + time.toMinutes + " minutes.")
  }
} 
Example 6
Source File: FailingFTSServiceMockup.scala    From CM-Well   with Apache License 2.0 5 votes vote down vote up
package cmwell.bg.test

import java.util.concurrent.TimeoutException

import cmwell.fts._
import cmwell.util.concurrent.SimpleScheduler
import com.typesafe.config.{Config, ConfigFactory}
import com.typesafe.scalalogging.Logger

import scala.concurrent.duration._
import scala.concurrent.{ExecutionContext, Future}


  override def executeBulkIndexRequests(indexRequests: Iterable[ESIndexRequest], numOfRetries: Int,
                                        waitBetweenRetries: Long)
                                       (implicit executionContext: ExecutionContext, logger:Logger = loger) = {

    errorModuloDividend += 1
    logger info s"executeBulkIndexRequests: errorModuloDividend=$errorModuloDividend"
    if(errorModuloDividend % errorModuloDivisor == 2 && errorCount <=2 ) {
      errorCount += 1
      logger info s"delaying response"
      throw new TimeoutException("fake")
    }
    else {
        logger info "forwarding to real ftsservice"
      super.executeBulkIndexRequests(indexRequests, numOfRetries, waitBetweenRetries)
    }
  }
} 
Example 7
Source File: S3ObjectUploader.scala    From CM-Well   with Apache License 2.0 5 votes vote down vote up
package cmwell.tools.neptune.export

import java.io._
import java.util
import java.util.concurrent.{Executors, TimeoutException}
import java.util.stream.Collectors
import java.util.{Collections, Vector}

import com.amazonaws.auth.profile.ProfileCredentialsProvider
import com.amazonaws.services.s3.AmazonS3ClientBuilder
import com.amazonaws.services.s3.model.{ObjectMetadata, PutObjectRequest}
import com.amazonaws.{AmazonServiceException, ClientConfiguration, Protocol, SdkClientException}
import org.apache.commons.io.{FileUtils, IOUtils}
import org.slf4j.LoggerFactory

import scala.concurrent.{Await, ExecutionContext, Future}
import scala.concurrent.duration.{FiniteDuration, _}

object S3ObjectUploader{

  val executor = Executors.newFixedThreadPool(1)
  implicit val ec: ExecutionContext = scala.concurrent.ExecutionContext.fromExecutor(executor)
  protected lazy val logger = LoggerFactory.getLogger("s3_uploader")


  def init(proxyHost:Option[String], proxyPort:Option[Int]) = {
    val clientRegion = "us-east-1"
    val config = new ClientConfiguration
    config.setProtocol(Protocol.HTTPS)
    proxyHost.foreach(host => config.setProxyHost(host))
    proxyPort.foreach(port =>  config.setProxyPort(port))
    val s3Client = AmazonS3ClientBuilder.standard()
      .withRegion(clientRegion)
      .withClientConfiguration(config)
      .withCredentials(new ProfileCredentialsProvider())
      .build()
    s3Client
  }


  def persistChunkToS3Bucket(chunkData:String, fileName:String, proxyHost:Option[String], proxyPort:Option[Int], s3Directory:String) = {
        try{
          init(proxyHost, proxyPort).putObject(s3Directory, fileName, chunkData)
      }
      catch {
        case e: AmazonServiceException =>
          e.printStackTrace()
          throw e
        case e: SdkClientException =>
          e.printStackTrace()
          throw e
      }
  }

  def persistChunkToS3Bucket(tmpFile:File, proxyHost:Option[String], proxyPort:Option[Int], s3Directory:String, retryCount:Int = 3):Unit = {
    try{
      val s3UploadTask = Future{init(proxyHost, proxyPort).putObject(s3Directory, tmpFile.getName, tmpFile)}(ec)
      Await.result(s3UploadTask,  5.minutes)
      tmpFile.delete()
    }
    catch {
      case e:TimeoutException =>
        if(retryCount > 0) {
          logger.error("S3 upload task run more than 5 minutes..Going to retry")
          persistChunkToS3Bucket(tmpFile, proxyHost, proxyPort, s3Directory, retryCount-1)
        }
        else{
          throw new Exception( "S3 upload task duration was more than 5 minutes")
        }
      case e: AmazonServiceException =>
        e.printStackTrace()
        throw e
      case e: SdkClientException =>
        e.printStackTrace()
        throw e
    }
  }

} 
Example 8
Source File: CustomCodeEntryPoint.scala    From seahorse   with Apache License 2.0 5 votes vote down vote up
package ai.deepsense.workflowexecutor.customcode

import java.util.concurrent.TimeoutException
import java.util.concurrent.atomic.AtomicReference

import scala.annotation.tailrec
import scala.concurrent.duration._
import scala.concurrent.{Await, Promise}

import org.apache.spark.api.java.JavaSparkContext
import org.apache.spark.sql.DataFrame
import org.apache.spark.{SparkConf, SparkContext}

import ai.deepsense.commons.utils.Logging
import ai.deepsense.deeplang._
import ai.deepsense.sparkutils.SparkSQLSession


class CustomCodeEntryPoint(
    val sparkContext: SparkContext,
    val sparkSQLSession: SparkSQLSession,
    val dataFrameStorage: DataFrameStorage,
    val operationExecutionDispatcher: OperationExecutionDispatcher)
  extends Logging {
  import ai.deepsense.workflowexecutor.customcode.CustomCodeEntryPoint._
  def getSparkContext: JavaSparkContext = sparkContext

  def getSparkSQLSession: SparkSQLSession = sparkSQLSession

  def getNewSparkSQLSession: SparkSQLSession = sparkSQLSession.newSession()

  def getSparkConf: SparkConf = sparkContext.getConf

  private val codeExecutor: AtomicReference[Promise[CustomCodeExecutor]] =
    new AtomicReference(Promise())

  private val pythonPort: AtomicReference[Promise[Int]] =
    new AtomicReference(Promise())

  def getCodeExecutor(timeout: Duration): CustomCodeExecutor =
    getFromPromise(codeExecutor.get, timeout)

  def getPythonPort(timeout: Duration): Int =
    getFromPromise(pythonPort.get, timeout)

  def registerCodeExecutor(newCodeExecutor: CustomCodeExecutor): Unit =
    replacePromise(codeExecutor, newCodeExecutor)

  def registerCallbackServerPort(newPort: Int): Unit =
    replacePromise(pythonPort, newPort)

  def retrieveInputDataFrame(workflowId: String, nodeId: String, portNumber: Int): DataFrame =
    dataFrameStorage.getInputDataFrame(workflowId, nodeId, portNumber).get

  def retrieveOutputDataFrame(workflowId: String, nodeId: String, portNumber: Int): DataFrame =
    dataFrameStorage.getOutputDataFrame(workflowId, nodeId, portNumber).get

  def registerOutputDataFrame(
      workflowId: String, nodeId: String, portNumber: Int, dataFrame: DataFrame): Unit =
    dataFrameStorage.setOutputDataFrame(workflowId, nodeId, portNumber, dataFrame)

  def executionCompleted(workflowId: String, nodeId: String): Unit =
    operationExecutionDispatcher.executionEnded(workflowId, nodeId, Right(()))

  def executionFailed(workflowId: String, nodeId: String, error: String): Unit =
    operationExecutionDispatcher.executionEnded(workflowId, nodeId, Left(error))
}

object CustomCodeEntryPoint {
  private case class PromiseReplacedException() extends Exception

  @tailrec
  private def getFromPromise[T](promise: => Promise[T], timeout: Duration): T = {
    try {
      Await.result(promise.future, timeout)
    } catch {
      case e: TimeoutException => throw e
      case e: PromiseReplacedException => getFromPromise(promise, timeout)
    }
  }

  private def replacePromise[T](promise: AtomicReference[Promise[T]], newValue: T): Unit = {
    val oldPromise = promise.getAndSet(Promise.successful(newValue))
    try {
      oldPromise.failure(new PromiseReplacedException)
    } catch {
      // The oldPromise will have been completed always, except for the first time.
      // The illegal state is expected, but we have to complete the oldPromise,
      // since someone might be waiting on it.
      case e: IllegalStateException => ()
    }
  }

  case class CustomCodeEntryPointConfig(
    pyExecutorSetupTimeout: Duration = 5.seconds)
} 
Example 9
Source File: PythonCustomCodeEntryPointTest.scala    From seahorse   with Apache License 2.0 5 votes vote down vote up
package ai.deepsense.workflowexecutor.pythongateway

import java.util.concurrent.TimeoutException

import scala.concurrent.duration._

import org.apache.spark.SparkContext
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{Matchers, WordSpec}

import ai.deepsense.deeplang.{CustomCodeExecutor, DataFrameStorage, OperationExecutionDispatcher}
import ai.deepsense.sparkutils.SparkSQLSession
import ai.deepsense.workflowexecutor.customcode.CustomCodeEntryPoint

class PythonCustomCodeEntryPointTest extends WordSpec with MockitoSugar with Matchers {

  "PythonEntryPoint" should {
    "throw on uninitialized code executor" in {
      val entryPoint = createEntryPoint
      a[TimeoutException] shouldBe thrownBy {
        entryPoint.getCodeExecutor(100.millis)
      }
    }

    "throw on uninitialized callback server port" in {
      val entryPoint = createEntryPoint
      a[TimeoutException] shouldBe thrownBy {
        entryPoint.getPythonPort(100.millis)
      }
    }

    "return initialized code executor" in {
      val entryPoint = createEntryPoint
      val mockExecutor = mock[CustomCodeExecutor]
      entryPoint.registerCodeExecutor(mockExecutor)
      entryPoint.getCodeExecutor(100.millis) shouldBe mockExecutor
    }

    "return initialized callback server port" in {
      val entryPoint = createEntryPoint
      entryPoint.registerCallbackServerPort(4412)
      entryPoint.getPythonPort(100.millis) shouldBe 4412
    }

    "return code executor initialized while waiting on it" in {
      val entryPoint = createEntryPoint
      val mockExecutor = mock[CustomCodeExecutor]

      new Thread(new Runnable {
        override def run(): Unit = {
          Thread.sleep(1000)
          entryPoint.registerCodeExecutor(mockExecutor)
        }
      }).start()

      entryPoint.getCodeExecutor(2.seconds) shouldBe mockExecutor
    }
  }

  private def createEntryPoint: CustomCodeEntryPoint =
    new CustomCodeEntryPoint(
      mock[SparkContext],
      mock[SparkSQLSession],
      mock[DataFrameStorage],
      mock[OperationExecutionDispatcher])
} 
Example 10
Source File: MqModule.scala    From seahorse   with Apache License 2.0 5 votes vote down vote up
package ai.deepsense.sessionmanager.mq

import java.util.concurrent.TimeoutException

import akka.actor.{ActorRef, ActorSystem}
import com.google.inject.name.Named
import com.google.inject.{AbstractModule, Provides, Singleton}
import com.rabbitmq.client.ConnectionFactory
import com.thenewmotion.akka.rabbitmq.ConnectionActor

import scala.concurrent.{ExecutionContext, Future}
import scala.util.control.NonFatal

import ai.deepsense.commons.utils.Logging
import ai.deepsense.sessionmanager.service.executor.SessionExecutorClients
import ai.deepsense.sparkutils.AkkaUtils
import ai.deepsense.workflowexecutor.communication.mq.MQCommunication
import ai.deepsense.workflowexecutor.communication.mq.json.Global.{GlobalMQDeserializer, GlobalMQSerializer}
import ai.deepsense.workflowexecutor.rabbitmq.MQCommunicationFactory

class MqModule extends AbstractModule with Logging {
  override def configure(): Unit = {}

  @Provides
  @Singleton
  def communicationFactory(
      actorSystem: ActorSystem,
      @Named("MQConnectionActor") connection: ActorRef): MQCommunicationFactory = {
    MQCommunicationFactory(actorSystem, connection, GlobalMQSerializer, GlobalMQDeserializer)
  }

  @Provides
  @Singleton
  @Named("MQConnectionActor")
  def createConnection(
      system: ActorSystem,
      @Named("queue.host") host: String,
      @Named("queue.port") port: Int,
      @Named("queue.user") user: String,
      @Named("queue.pass") pass: String): ActorRef = {
    val factory = new ConnectionFactory()
    factory.setHost(host)
    factory.setPort(port)
    factory.setUsername(user)
    factory.setPassword(pass)
    system.actorOf(
      ConnectionActor.props(factory),
      MQCommunication.mqActorSystemName)
  }

  @Provides
  @Singleton
  def createSessionExecutorClients(
      communicationFactory: MQCommunicationFactory): SessionExecutorClients = {
    new SessionExecutorClients(communicationFactory)
  }

  @Provides
  @Singleton
  @Named("SessionService.HeartbeatSubscribed")
  def heartbeatSubscriber(
      system: ActorSystem,
      communicationFactory: MQCommunicationFactory,
      @Named("SessionService.Actor") sessionServiceActor: ActorRef,
      @Named("queue.heartbeat.subscription.timeout") timeout: Long): Future[Unit] = {
    import ai.deepsense.sessionmanager.mq.MQCommunicationFactoryEnrichments._
    implicit val ec: ExecutionContext = system.dispatcher
    val subscribed = communicationFactory
      .registerBroadcastSubscriber("seahorse_heartbeats_all", sessionServiceActor)

    val subscribedWithTimeout = Future.firstCompletedOf(List(subscribed, Future {
      Thread.sleep(timeout)
      throw new TimeoutException
    }))

    subscribedWithTimeout.onFailure {
      case NonFatal(e) =>
        logger.error(s"Haven't subscribed to Heartbeats after '$timeout' millis." +
          " Shutting down!")
        AkkaUtils.terminate(system)
    }

    subscribedWithTimeout.map(_.data)
  }
} 
Example 11
Source File: Node.scala    From affinity   with Apache License 2.0 5 votes vote down vote up
package io.amient.affinity.core.cluster


import java.util.concurrent.{CountDownLatch, TimeUnit, TimeoutException}

import akka.actor.{Actor, Props}
import akka.event.Logging
import akka.util.Timeout
import com.typesafe.config.{Config, ConfigFactory}
import io.amient.affinity.core.ack
import io.amient.affinity.core.actor.Controller._
import io.amient.affinity.core.actor.Gateway.{GatewayClusterStatus, GatewayConf}
import io.amient.affinity.core.actor._
import io.amient.affinity.core.config._
import io.amient.affinity.{AffinityActorSystem, Conf}

import scala.concurrent.duration._
import scala.concurrent.{Await, Future, Promise}
import scala.language.{implicitConversions, postfixOps}
import scala.reflect.ClassTag

object Node {

  class NodeConf extends CfgStruct[NodeConf] {
    val Containers: CfgGroup[CfgIntList] = group("container", classOf[CfgIntList], false)
      .doc("Array of partitions assigned to this node, <ID> represents the Keyspace, e.g. assigning first four partitions of MyKeySpace: affinity.node.container.MyKeySpace = [0,1,2,3] ")
    val Gateway: GatewayConf = struct("gateway", new GatewayConf, false)
    val SuspendQueueMaxSize = integer("suspend.queue.max.size", 1000).doc("Size of the queue when the cluster enters suspended mode")
    val StartupTimeoutMs = longint("startup.timeout.ms", Integer.MAX_VALUE).doc("Maximum time a node can take to startup - this number must account for any potential state bootstrap")
    val ShutdownTimeoutMs = longint("shutdown.timeout.ms", 30000).doc("Maximum time a node can take to shutdown gracefully")
    val DataDir = filepath("data.dir", false).doc("Location under which any local state or registers will be kept - this is required if running in a distributed mode or when using persisted kv stores")
    val DataAutoAssign = bool("data.auto.assign", true, false).doc("Determines whether this node auto-balances data its containers; if set tot false the fixed list of container partitions will be used")
    val DataAutoDelete = bool("data.auto.delete", true, false).doc("If set to true, any unassigned partitions will be deleted from the local storage")
  }

}

class Node(config: Config) {

  def this(configResource: String) = this(ConfigFactory.parseResources(configResource).resolve)

  val conf = Conf(config)

  val startupTimeout = conf.Affi.Node.StartupTimeoutMs().toLong milliseconds
  val shutdownTimeout = conf.Affi.Node.ShutdownTimeoutMs().toLong milliseconds

  implicit val system = AffinityActorSystem.create(config)

  private val log = Logging.getLogger(system, this)

  private val controller = system.actorOf(Props(new Controller), name = "controller")

  private val httpGatewayPort = Promise[List[Int]]()

  private val clusterReady = new CountDownLatch(1)

  @volatile private var shuttingDown = false

  @volatile private var fatalError: Option[Throwable] = None

  import scala.concurrent.ExecutionContext.Implicits.global

  val systemEventsWatcher = system.actorOf(Props(new Actor {
    override def receive: Receive = {
      case GatewayClusterStatus(false) => clusterReady.countDown()
      case FatalErrorShutdown(e) =>
        fatalError = Some(e)
        shutdown()
    }
  }))

  system.eventStream.subscribe(systemEventsWatcher, classOf[GatewayClusterStatus])

  system.eventStream.subscribe(systemEventsWatcher, classOf[FatalErrorShutdown])

  sys.addShutdownHook {
    if (!shuttingDown) {
      log.info("process killed - attempting graceful shutdown")
      fatalError = None
      shutdown()
    }
    Await.ready(system.terminate, shutdownTimeout)
  }

  
  def start[T <: Gateway](creator: => T)(implicit tag: ClassTag[T]): Future[List[Int]] = {
    controller ! StartRebalance()
    implicit val timeout = Timeout(startupTimeout)
    val result = controller ?? CreateGateway(Props(creator))
    httpGatewayPort.completeWith(result)
    result
  }

} 
Example 12
Source File: TimeoutsSpec.scala    From futiles   with Apache License 2.0 5 votes vote down vote up
package markatta.futiles

import java.util.concurrent.TimeoutException

import scala.concurrent.{Future, Promise}
import scala.concurrent.duration._
import Timeouts.Implicits._

class TimeoutsSpec extends Spec {

  describe("The timeout utils") {

    it("runs a future after a given timeout") {
      val result = Timeouts.timeout(2.millis)("success")
      result.futureValue should be ("success")
    }

    it("decorates a future with an error timeout") {
      val future = Promise[String]().future
      val result = future.withTimeoutError(2.millis)

      val ex = result.failed.futureValue

      ex.getClass shouldEqual classOf[TimeoutException]
      ex.getMessage shouldEqual "Timed out after 2 milliseconds"
    }

    it("completes a future rathern than fail it if it does not timeout") {
      Future.successful("success").withTimeoutError(10.seconds).futureValue should be ("success")
    }

    it("decorates a future with an default timeout") {
      val future = Promise[String]().future
      val result = future.withTimeoutDefault(2.millis, "banana")

      result.futureValue shouldEqual "banana"
    }

    it("completes a future rathern than default it if it does not timeout") {
      Future.successful("success").withTimeoutDefault(10.seconds, "default").futureValue should be ("success")
    }
  }
} 
Example 13
Source File: HttpHandler.scala    From play-auditing   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.audit.handler

import java.io.IOException
import java.net.URL
import java.util.concurrent.TimeoutException

import akka.stream.Materializer
import org.slf4j.{Logger, LoggerFactory}
import play.api.inject.ApplicationLifecycle
import play.api.libs.json.JsValue

import scala.concurrent.{ExecutionContext, Future}
import scala.concurrent.duration.Duration


sealed trait HttpResult
object HttpResult {
  case class Response(statusCode: Int) extends HttpResult
  case object Malformed extends HttpResult
  case class Failure(msg: String, nested: Option[Throwable] = None) extends Exception(msg, nested.orNull) with HttpResult
}

abstract class HttpHandler(
  endpointUrl      : URL,
  userAgent        : String,
  connectTimeout   : Duration,
  requestTimeout   : Duration,
  materializer     : Materializer,
  lifecycle        : ApplicationLifecycle
) {
  private val logger: Logger = LoggerFactory.getLogger(getClass)

  val HTTP_STATUS_CONTINUE = 100

  val wsClient: WSClient = {
    implicit val m = materializer
    val wsClient = WSClient(connectTimeout, requestTimeout, userAgent)
    lifecycle.addStopHook { () =>
      logger.info("Closing play-auditing http connections...")
      wsClient.close()
      Future.successful(())
    }
    wsClient
  }

  def sendHttpRequest(event: JsValue)(implicit ec: ExecutionContext): Future[HttpResult] =
    try {
      logger.debug(s"Sending audit request to URL ${endpointUrl.toString}")

      wsClient.url(endpointUrl.toString)
        .post(event)
        .map { response =>
          val httpStatusCode = response.status
          logger.debug(s"Got status code : $httpStatusCode")
          response.body
          logger.debug("Response processed and closed")

          if (httpStatusCode >= HTTP_STATUS_CONTINUE) {
            logger.info(s"Got status code $httpStatusCode from HTTP server.")
            HttpResult.Response(httpStatusCode)
          } else {
            logger.warn(s"Malformed response (status $httpStatusCode) returned from server")
            HttpResult.Malformed
          }
        }.recover {
          case e: TimeoutException =>
            HttpResult.Failure("Error opening connection, or request timed out", Some(e))
          case e: IOException =>
            HttpResult.Failure("Error opening connection, or request timed out", Some(e))
        }
    } catch {
      case t: Throwable =>
        Future.successful(HttpResult.Failure("Error sending HTTP request", Some(t)))
    }
} 
Example 14
Source File: DatabaseActorsGuardian.scala    From NSDb   with Apache License 2.0 5 votes vote down vote up
package io.radicalbit.nsdb.cluster.actor

import java.util.concurrent.TimeoutException

import akka.actor.SupervisorStrategy.Resume
import akka.actor._


class DatabaseActorsGuardian extends Actor with ActorLogging {

  override val supervisorStrategy: SupervisorStrategy = OneForOneStrategy() {
    case e: TimeoutException =>
      log.error(e, "Got the following TimeoutException, resuming the processing")
      Resume
    case t =>
      log.error(t, "generic error occurred")
      super.supervisorStrategy.decider.apply(t)
  }

  def receive: Receive = Actor.emptyBehavior
} 
Example 15
Source File: CatsHelpers.scala    From nelson   with Apache License 2.0 5 votes vote down vote up
package nelson

import cats.Eval
import cats.effect.{Effect, IO, Timer}
import cats.free.Cofree
import cats.syntax.functor._
import cats.syntax.monadError._

import fs2.{Pipe, Sink, Stream}

import quiver.{Context, Decomp, Graph}

import java.util.concurrent.TimeoutException

import scala.concurrent.ExecutionContext
import scala.concurrent.duration.FiniteDuration
import scala.collection.immutable.{Stream => SStream}

object CatsHelpers {
  implicit class NelsonEnrichedIO[A](val io: IO[A]) extends AnyVal {
    
  private type Tree[A] = Cofree[SStream, A]

  private def flattenTree[A](tree: Tree[A]): SStream[A] = {
    def go(tree: Tree[A], xs: SStream[A]): SStream[A] =
      SStream.cons(tree.head, tree.tail.value.foldRight(xs)(go(_, _)))
    go(tree, SStream.Empty)
  }

  private def Node[A](root: A, forest: => SStream[Tree[A]]): Tree[A] =
    Cofree[SStream, A](root, Eval.later(forest))

  implicit class NelsonEnrichedGraph[N, A, B](val graph: Graph[N, A, B]) extends AnyVal {
    def reachable(v: N): Vector[N] =
      xdfWith(Seq(v), _.successors, _.vertex)._1.flatMap(flattenTree)

    def xdfWith[C](vs: Seq[N], d: Context[N, A, B] => Seq[N], f: Context[N, A, B] => C): (Vector[Tree[C]], Graph[N, A, B]) =
      if (vs.isEmpty || graph.isEmpty) (Vector(), graph)
      else graph.decomp(vs.head) match {
        case Decomp(None, g) => g.xdfWith(vs.tail, d, f)
        case Decomp(Some(c), g) =>
          val (xs, _) = g.xdfWith(d(c), d, f)
          val (ys, g3) = g.xdfWith(vs.tail, d, f)
          (Node(f(c), xs.toStream) +: ys, g3)
      }
  }
} 
Example 16
Source File: AsyncHttpClientMonixHttpTest.scala    From sttp   with Apache License 2.0 5 votes vote down vote up
package sttp.client.asynchttpclient.monix

import java.util.concurrent.TimeoutException

import monix.eval.Task
import sttp.client._
import sttp.client.impl.monix.convertMonixTaskToFuture
import sttp.client.testing.{CancelTest, ConvertToFuture, HttpTest}
import monix.execution.Scheduler.Implicits.global

import scala.concurrent.duration._

class AsyncHttpClientMonixHttpTest extends HttpTest[Task] with CancelTest[Task, Nothing] {
  override implicit val backend: SttpBackend[Task, Nothing, NothingT] = AsyncHttpClientMonixBackend().runSyncUnsafe()
  override implicit val convertToFuture: ConvertToFuture[Task] = convertMonixTaskToFuture

  override def timeoutToNone[T](t: Task[T], timeoutMillis: Int): Task[Option[T]] =
    t.map(Some(_))
      .timeout(timeoutMillis.milliseconds)
      .onErrorRecover {
        case _: TimeoutException => None
      }

  override def throwsExceptionOnUnsupportedEncoding = false
} 
Example 17
Source File: AsyncHttpClientCatsHttpTest.scala    From sttp   with Apache License 2.0 5 votes vote down vote up
package sttp.client.asynchttpclient.cats

import java.util.concurrent.TimeoutException

import cats.effect.IO
import sttp.client._
import sttp.client.impl.cats.CatsTestBase
import sttp.client.testing.{CancelTest, HttpTest}

import scala.concurrent.duration._

class AsyncHttpClientCatsHttpTest extends HttpTest[IO] with CancelTest[IO, Nothing] with CatsTestBase {
  override implicit val backend: SttpBackend[IO, Nothing, NothingT] = AsyncHttpClientCatsBackend[IO]().unsafeRunSync()

  "illegal url exceptions" - {
    "should be wrapped in the effect wrapper" in {
      basicRequest.get(uri"ps://sth.com").send().toFuture().failed.map { e => e shouldBe a[IllegalArgumentException] }
    }
  }

  override def timeoutToNone[T](t: IO[T], timeoutMillis: Int): IO[Option[T]] =
    t.map(Some(_))
      .timeout(timeoutMillis.milliseconds)
      .handleErrorWith {
        case _: TimeoutException => IO(None)
        case e                   => throw e
      }

  override def throwsExceptionOnUnsupportedEncoding = false
} 
Example 18
Source File: RichFuture.scala    From swave   with Mozilla Public License 2.0 5 votes vote down vote up
package swave.core.util

import java.util.concurrent.TimeoutException
import scala.concurrent.duration._
import scala.concurrent.{Await, Future, Promise}
import swave.core.StreamEnv

final class RichFuture[T](val underlying: Future[T]) extends AnyVal {

  def await(timeout: FiniteDuration = 1.second): T =
    underlying.value match {
      case Some(t)                          ⇒ t.get
      case None if timeout == Duration.Zero ⇒ throw new TimeoutException(s"Future was not completed")
      case _                                ⇒ Await.result(underlying, timeout)
    }

  def delay(duration: FiniteDuration)(implicit env: StreamEnv): Future[T] = {
    import env.defaultDispatcher
    val promise = Promise[T]()
    underlying.onComplete { value ⇒
      env.scheduler.scheduleOnce(duration) { promise.complete(value); () }
    }
    promise.future
  }
} 
Example 19
Source File: StreamEnvImpl.scala    From swave   with Mozilla Public License 2.0 5 votes vote down vote up
package swave.core.impl

import java.util.concurrent.{ConcurrentHashMap, TimeoutException}
import scala.annotation.tailrec
import scala.util.Try
import scala.concurrent.{Future, Promise}
import scala.concurrent.duration._
import com.typesafe.config.Config
import com.typesafe.scalalogging.Logger
import org.slf4j.LoggerFactory
import swave.core.macros._
import swave.core._

private[core] final class StreamEnvImpl(val name: String,
                                        val config: Config,
                                        val settings: StreamEnv.Settings,
                                        val classLoader: ClassLoader)
    extends StreamEnv {

  val startTime = System.currentTimeMillis()

  val log = Logger(LoggerFactory.getLogger(name))

  val dispatchers = DispatchersImpl(settings.dispatcherSettings)

  val scheduler = SchedulerImpl(settings.schedulerSettings)

  if (settings.logConfigOnStart) log.info(settings.toString) // TODO: improve rendering

  def defaultDispatcher = dispatchers.defaultDispatcher

  def shutdown(): StreamEnv.Termination =
    new StreamEnv.Termination {
      val schedulerTermination   = scheduler.shutdown()
      val dispatchersTermination = dispatchers.shutdownAll()

      def isTerminated: Boolean = schedulerTermination.isCompleted && unterminatedDispatchers.isEmpty

      def unterminatedDispatchers: List[String] = dispatchersTermination()

      def awaitTermination(timeout: FiniteDuration): Unit = {
        requireArg(timeout >= Duration.Zero, "`timeout` must be > 0")
        var deadline = System.nanoTime() + timeout.toNanos
        if (deadline < 0) deadline = Long.MaxValue // overflow protection

        @tailrec def await(): Unit =
          if (!isTerminated) {
            if (System.nanoTime() < deadline) {
              Thread.sleep(1L)
              await()
            } else {
              val unterminated =
                if (schedulerTermination.isCompleted) unterminatedDispatchers
                else "scheduler" :: unterminatedDispatchers
              throw new TimeoutException(
                s"StreamEnv did not shut down within specified timeout of $timeout.\n" +
                  s"Unterminated dispatchers: [${unterminated.mkString(", ")}]")
            }
          }

        await()
      }
    }

  private[this] val _extensions = new ConcurrentHashMap[ExtensionId[_], Future[_ <: Extension]]

  @tailrec def getOrLoadExtension[T <: Extension](id: ExtensionId[T]): Future[T] =
    _extensions.get(id) match {
      case null ⇒
        val promise = Promise[T]()
        _extensions.putIfAbsent(id, promise.future) match {
          case null ⇒
            val tryValue = Try(id.createExtension(this))
            promise.complete(tryValue)
            val future = Promise.fromTry(tryValue).future
            _extensions.put(id, future) // speed up future accesses somewhat
            future
          case _ ⇒ getOrLoadExtension(id)
        }
      case x ⇒ x.asInstanceOf[Future[T]]
    }
} 
Example 20
Source File: DataRetrieval.scala    From vamp   with Apache License 2.0 5 votes vote down vote up
package io.vamp.common.akka

import java.util.concurrent.TimeoutException

import akka.actor.Actor
import akka.pattern.after
import akka.util.Timeout

import scala.concurrent.Future
import scala.util.{ Failure, Success }

case class DataRetrieved(data: Map[Class[Actor], Any], succeeded: Boolean)

trait DataRetrieval {
  this: ExecutionContextProvider with ActorSystemProvider ⇒

  def retrieve(actors: List[Class[Actor]], futureOf: (Class[Actor]) ⇒ Future[Any], timeout: Timeout): Future[DataRetrieved] = {
    def noDataError(actor: Class[Actor]) = noData(actor) → false

    val futures: Map[Class[Actor], Future[Any]] = actors.map(actor ⇒ actor → futureOf(actor)).toMap

    Future.firstCompletedOf(List(Future.sequence(futures.values.toList.map(_.recover { case x ⇒ Failure(x) })), after(timeout.duration, using = actorSystem.scheduler) {
      Future.successful(new TimeoutException("Component timeout."))
    })) map { _ ⇒
      futures.map {
        case (actor, future) if future.isCompleted ⇒
          actor → future.value.map {
            case Success(data) ⇒ data → true
            case _             ⇒ noDataError(actor)
          }.getOrElse(noDataError(actor))
        case (actor, future) ⇒ actor → noDataError(actor)
      }.foldLeft[DataRetrieved](DataRetrieved(Map(), succeeded = true)) { (r, e) ⇒ r.copy(r.data + (e._1 → e._2._1), succeeded = r.succeeded && e._2._2) }
    }
  }

  def noData(actor: Class[Actor]) = Map("error" → "No response.")
} 
Example 21
Source File: AkkaUtils.scala    From fusion-data   with Apache License 2.0 5 votes vote down vote up
package akka.mass

import java.util.concurrent.TimeoutException

import akka.actor.{ ActorSystem, ActorSystemImpl }

import scala.concurrent.Await
import scala.concurrent.duration._

object AkkaUtils {
  
  def shutdownActorSystem(
      actorSystem: ActorSystem,
      duration: Duration = 10.seconds,
      verifySystemShutdown: Boolean = false): Unit = {
    actorSystem.terminate()
    try Await.ready(actorSystem.whenTerminated, duration)
    catch {
      case _: TimeoutException =>
        val msg = "Failed to stop [%s] within [%s] \n%s".format(
          actorSystem.name,
          duration,
          actorSystem.asInstanceOf[ActorSystemImpl].printTree)
        if (verifySystemShutdown) throw new RuntimeException(msg)
        else println(msg)
    }
  }
} 
Example 22
Source File: CustomCodeEntryPoint.scala    From seahorse-workflow-executor   with Apache License 2.0 5 votes vote down vote up
package io.deepsense.workflowexecutor.customcode

import java.util.concurrent.TimeoutException
import java.util.concurrent.atomic.AtomicReference

import scala.annotation.tailrec
import scala.concurrent.duration._
import scala.concurrent.{Await, Promise}

import org.apache.spark.api.java.JavaSparkContext
import org.apache.spark.sql.DataFrame
import org.apache.spark.{SparkConf, SparkContext}

import io.deepsense.commons.utils.Logging
import io.deepsense.deeplang._
import io.deepsense.sparkutils.SparkSQLSession


class CustomCodeEntryPoint(
    val sparkContext: SparkContext,
    val sparkSQLSession: SparkSQLSession,
    val dataFrameStorage: DataFrameStorage,
    val operationExecutionDispatcher: OperationExecutionDispatcher)
  extends Logging {
  import io.deepsense.workflowexecutor.customcode.CustomCodeEntryPoint._
  def getSparkContext: JavaSparkContext = sparkContext

  def getSparkSQLSession: SparkSQLSession = sparkSQLSession

  def getNewSparkSQLSession: SparkSQLSession = sparkSQLSession.newSession()

  def getSparkConf: SparkConf = sparkContext.getConf

  private val codeExecutor: AtomicReference[Promise[CustomCodeExecutor]] =
    new AtomicReference(Promise())

  private val pythonPort: AtomicReference[Promise[Int]] =
    new AtomicReference(Promise())

  def getCodeExecutor(timeout: Duration): CustomCodeExecutor =
    getFromPromise(codeExecutor.get, timeout)

  def getPythonPort(timeout: Duration): Int =
    getFromPromise(pythonPort.get, timeout)

  def registerCodeExecutor(newCodeExecutor: CustomCodeExecutor): Unit =
    replacePromise(codeExecutor, newCodeExecutor)

  def registerCallbackServerPort(newPort: Int): Unit =
    replacePromise(pythonPort, newPort)

  def retrieveInputDataFrame(workflowId: String, nodeId: String, portNumber: Int): DataFrame =
    dataFrameStorage.getInputDataFrame(workflowId, nodeId, portNumber).get

  def retrieveOutputDataFrame(workflowId: String, nodeId: String, portNumber: Int): DataFrame =
    dataFrameStorage.getOutputDataFrame(workflowId, nodeId, portNumber).get

  def registerOutputDataFrame(
      workflowId: String, nodeId: String, portNumber: Int, dataFrame: DataFrame): Unit =
    dataFrameStorage.setOutputDataFrame(workflowId, nodeId, portNumber, dataFrame)

  def executionCompleted(workflowId: String, nodeId: String): Unit =
    operationExecutionDispatcher.executionEnded(workflowId, nodeId, Right(()))

  def executionFailed(workflowId: String, nodeId: String, error: String): Unit =
    operationExecutionDispatcher.executionEnded(workflowId, nodeId, Left(error))
}

object CustomCodeEntryPoint {
  private case class PromiseReplacedException() extends Exception

  @tailrec
  private def getFromPromise[T](promise: => Promise[T], timeout: Duration): T = {
    try {
      Await.result(promise.future, timeout)
    } catch {
      case e: TimeoutException => throw e
      case e: PromiseReplacedException => getFromPromise(promise, timeout)
    }
  }

  private def replacePromise[T](promise: AtomicReference[Promise[T]], newValue: T): Unit = {
    val oldPromise = promise.getAndSet(Promise.successful(newValue))
    try {
      oldPromise.failure(new PromiseReplacedException)
    } catch {
      // The oldPromise will have been completed always, except for the first time.
      // The illegal state is expected, but we have to complete the oldPromise,
      // since someone might be waiting on it.
      case e: IllegalStateException => ()
    }
  }

  case class CustomCodeEntryPointConfig(
    pyExecutorSetupTimeout: Duration = 5.seconds)
} 
Example 23
Source File: PythonCustomCodeEntryPointTest.scala    From seahorse-workflow-executor   with Apache License 2.0 5 votes vote down vote up
package io.deepsense.workflowexecutor.pythongateway

import java.util.concurrent.TimeoutException

import scala.concurrent.duration._

import org.apache.spark.SparkContext
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{Matchers, WordSpec}

import io.deepsense.deeplang.{CustomCodeExecutor, DataFrameStorage, OperationExecutionDispatcher}
import io.deepsense.sparkutils.SparkSQLSession
import io.deepsense.workflowexecutor.customcode.CustomCodeEntryPoint

class PythonCustomCodeEntryPointTest extends WordSpec with MockitoSugar with Matchers {

  "PythonEntryPoint" should {
    "throw on uninitialized code executor" in {
      val entryPoint = createEntryPoint
      a[TimeoutException] shouldBe thrownBy {
        entryPoint.getCodeExecutor(100.millis)
      }
    }

    "throw on uninitialized callback server port" in {
      val entryPoint = createEntryPoint
      a[TimeoutException] shouldBe thrownBy {
        entryPoint.getPythonPort(100.millis)
      }
    }

    "return initialized code executor" in {
      val entryPoint = createEntryPoint
      val mockExecutor = mock[CustomCodeExecutor]
      entryPoint.registerCodeExecutor(mockExecutor)
      entryPoint.getCodeExecutor(100.millis) shouldBe mockExecutor
    }

    "return initialized callback server port" in {
      val entryPoint = createEntryPoint
      entryPoint.registerCallbackServerPort(4412)
      entryPoint.getPythonPort(100.millis) shouldBe 4412
    }

    "return code executor initialized while waiting on it" in {
      val entryPoint = createEntryPoint
      val mockExecutor = mock[CustomCodeExecutor]

      new Thread(new Runnable {
        override def run(): Unit = {
          Thread.sleep(1000)
          entryPoint.registerCodeExecutor(mockExecutor)
        }
      }).start()

      entryPoint.getCodeExecutor(2.seconds) shouldBe mockExecutor
    }
  }

  private def createEntryPoint: CustomCodeEntryPoint =
    new CustomCodeEntryPoint(
      mock[SparkContext],
      mock[SparkSQLSession],
      mock[DataFrameStorage],
      mock[OperationExecutionDispatcher])
} 
Example 24
Source File: ConcurrentTransactionsDemo.scala    From redis4cats   with Apache License 2.0 5 votes vote down vote up
package dev.profunktor.redis4cats

import cats.effect._
import cats.implicits._
import dev.profunktor.redis4cats.connection._
import dev.profunktor.redis4cats.data.RedisCodec
import dev.profunktor.redis4cats.effect.Log
import dev.profunktor.redis4cats.hlist._
import dev.profunktor.redis4cats.log4cats._
import dev.profunktor.redis4cats.transactions._
import java.util.concurrent.TimeoutException

object ConcurrentTransactionsDemo extends LoggerIOApp {

  import Demo._

  def program: IO[Unit] = {
    val key1 = "test1"
    val key2 = "test2"

    val showResult: String => Option[String] => IO[Unit] = key =>
      _.fold(Log[IO].info(s"Key not found: $key"))(s => Log[IO].info(s"$key: $s"))

    val mkClient: Resource[IO, RedisClient] =
      Resource.liftF(RedisURI.make[IO](redisURI)).flatMap(RedisClient[IO](_))

    val mkRedis: Resource[IO, RedisCommands[IO, String, String]] =
      mkClient.flatMap(cli => Redis[IO].fromClient(cli, RedisCodec.Utf8))

    def txProgram(v1: String, v2: String) =
      mkRedis
        .use { cmd =>
          val getters =
            cmd.get(key1).flatTap(showResult(key1)) *>
                cmd.get(key2).flatTap(showResult(key2))

          val operations =
            cmd.set(key1, "sad") :: cmd.set(key2, "windows") :: cmd.get(key1) ::
                cmd.set(key1, v1) :: cmd.set(key2, v2) :: cmd.get(key1) :: HNil

          val prog: IO[Unit] =
            RedisTransaction(cmd)
              .filterExec(operations)
              .flatMap {
                case res1 ~: res2 ~: HNil =>
                  Log[IO].info(s"res1: $res1, res2: $res2")
              }
              .onError {
                case TransactionAborted =>
                  Log[IO].error("[Error] - Transaction Aborted")
                case TransactionDiscarded =>
                  Log[IO].error("[Error] - Transaction Discarded")
                case _: TimeoutException =>
                  Log[IO].error("[Error] - Timeout")
              }

          val watching =
            cmd.watch(key1, key2)

          getters >> watching >> prog >> getters >> Log[IO].info("keep doing stuff...")
        }

    // Only the first transaction will be successful. The second one will be discarded.
    //IO.race(txProgram("nix", "linux"), txProgram("foo", "bar")).void

    def retriableTx: IO[Unit] =
      txProgram("foo", "bar").handleErrorWith {
        case TransactionDiscarded => retriableTx
      }.uncancelable

    // The first transaction will be successful but ultimately, the second transaction will retry and win
    IO.race(txProgram("nix", "linux"), retriableTx).void
  }

} 
Example 25
Source File: TimeoutsSpec.scala    From gfc-concurrent   with Apache License 2.0 5 votes vote down vote up
package com.gilt.gfc.concurrent

import java.util.concurrent.{ TimeoutException, TimeUnit }
import scala.concurrent.duration._
import scala.concurrent.{ Future, Await }
import org.scalatest.{WordSpec, Matchers}

class TimeoutsSpec extends WordSpec with Matchers {
  import TimeoutsSpec._

  "Timeouts" when {
    "generating timing out futures" should {
      "create a Future that times out after the given finite duration" in {
        val now = System.currentTimeMillis
        val after = FiniteDuration(1, "second")
        val timingOut = Timeouts.timeout(after)
        an [TimeoutException] should be thrownBy { Await.result(timingOut, Duration(10, "seconds")) }
        val elapsed = (System.currentTimeMillis - now)
        elapsed should be (after.toMillis +- 500L)
      }

      "create timing out Futures that will fail predictably even under load" in {
        import scala.util.Random._

        val MaxTimeout = Duration(10, "seconds").toMillis.toInt
        val MaxDelta = Duration(50, "milliseconds").toMillis
        val Load = 10000

        val timingOuts: List[(Future[Nothing], Duration)] = (1 to Load).map { i =>
          val after = Duration(nextInt(MaxTimeout), "milliseconds")
          val timingOut = Timeouts.timeout(after)
          (timingOut, after)
        }.toList

        val timedOuts: List[(Future[Nothing], Duration, Duration, Duration)] = timingOuts.map { case (timingOut, after) =>
          val thrown = the [TimeoutException] thrownBy { Await.result(timingOut, Duration.Inf) }
          // println(thrown)
          val real = Duration(extractReal(thrown.getCause.getMessage), TimeUnit.MILLISECONDS)
          val delta = Duration(real.toMillis - after.toMillis, TimeUnit.MILLISECONDS)
          (timingOut, after, real, delta)
        }

        timedOuts.filter { case (timedOut, after, real, delta) => delta.toMillis > MaxDelta }.size === 0
      }

      "include the origin of the future" in {
        val here = new Exception()
        val timingOut = Timeouts.timeout(1.millis)
        val thrown = the [TimeoutException] thrownBy { Await.result(timingOut, Duration(10, "seconds")) }
        thrown.getStackTrace.size shouldBe > (50)
        val thrownFrames = thrown.getStackTrace.map(f => f: AnyRef).drop(7)
        val expectedFrames = here.getStackTrace.map(f => f: AnyRef)
        // Scala 2.12 stack frames differ slightly to stack frames in 2.10/2.11
        if (!java.util.Arrays.deepEquals(thrownFrames, expectedFrames.drop(2))) {
          thrownFrames shouldBe expectedFrames.drop(1)
        }
        thrown.getCause should not be null
        thrown.getCause.getStackTrace.size shouldBe <= (10)

      }
    }
  }
}

object TimeoutsSpec {
  private val pattern = """real: (\d+) ms.""".r

  def extractReal(s: String): Int = try {
    pattern.findFirstMatchIn(s).map { _.toString.split("real: ") }.get(1).split(" ms.").head.toInt
  } catch {
    case ex: Exception => throw new RuntimeException(s"Unable to parse real time from '${s}'", ex)
  }
} 
Example 26
Source File: RpcTimeout.scala    From drizzle-spark   with Apache License 2.0 5 votes vote down vote up
package org.apache.spark.rpc

import java.util.concurrent.TimeoutException

import scala.concurrent.{Await, Future}
import scala.concurrent.duration._
import scala.util.control.NonFatal

import org.apache.spark.{SparkConf, SparkException}
import org.apache.spark.util.Utils


  def apply(conf: SparkConf, timeoutPropList: Seq[String], defaultValue: String): RpcTimeout = {
    require(timeoutPropList.nonEmpty)

    // Find the first set property or use the default value with the first property
    val itr = timeoutPropList.iterator
    var foundProp: Option[(String, String)] = None
    while (itr.hasNext && foundProp.isEmpty) {
      val propKey = itr.next()
      conf.getOption(propKey).foreach { prop => foundProp = Some(propKey, prop) }
    }
    val finalProp = foundProp.getOrElse(timeoutPropList.head, defaultValue)
    val timeout = { Utils.timeStringAsSeconds(finalProp._2).seconds }
    new RpcTimeout(timeout, finalProp._1)
  }
} 
Example 27
Source File: ConcurrentConverters.scala    From mango   with Apache License 2.0 5 votes vote down vote up
package com.kakao.mango.concurrent

import java.util.concurrent.{ConcurrentMap, TimeUnit, TimeoutException}

import com.kakao.shaded.netty.util.{HashedWheelTimer, Timeout, TimerTask}

import scala.collection.JavaConversions._
import scala.concurrent.duration._
import scala.concurrent.{Future, Promise}
import scala.language.implicitConversions


  def timeout(duration: Duration): Future[Nothing] = {
    val promise = Promise[Nothing]()
    timer.newTimeout(new TimerTask {
      override def run(timeout: Timeout): Unit = {
        promise.failure(new TimeoutException(s"Operation was timed out after $duration"))
      }
    }, duration.toMillis, TimeUnit.MILLISECONDS)
    promise.future
  }

  implicit def toRichFuture[T](future: Future[T])(implicit timeout: Duration = 5.seconds): RichFuture[T] = new RichFuture[T](future, timeout)
  implicit def toEnsuring[K, V](map: ConcurrentMap[K, V]): EnsuringMap[K, V] = new EnsuringMap(map)
  implicit def toEnsuring[K, V](map: scala.collection.concurrent.Map[K, V]): EnsuringMap[K, V] = new EnsuringMap(map)

} 
Example 28
Source File: BaseNrsConnector.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package v1.connectors

import java.util.concurrent.TimeoutException

import config.AppConfig
import play.api.Logger
import play.api.libs.json.{Json, Writes}
import play.api.libs.ws.WSClient
import uk.gov.hmrc.http.HeaderCarrier
import v1.connectors.httpparsers.WsReads

import scala.concurrent.{ExecutionContext, Future}

trait BaseNrsConnector {
  val ws: WSClient
  val appConfig: AppConfig

  val logger = Logger(this.getClass)

  private[connectors] def nrsHeaderCarrier(implicit hc: HeaderCarrier): HeaderCarrier =
    hc.withExtraHeaders(
      "X-API-Key" -> appConfig.nrsApiKey,
      "User-Agent" -> appConfig.appName
    )

  def nrsPost[Body: Writes, Resp](body: Body, uri: NrsUri[Resp], defaultResult: NrsOutcome[Resp])(implicit ec: ExecutionContext,
                                                                                      hc: HeaderCarrier,
                                                                                      wsReads: WsReads[NrsOutcome[Resp]]): Future[NrsOutcome[Resp]] = {

    def doPost(implicit hc: HeaderCarrier): Future[NrsOutcome[Resp]] = {

      ws.url(s"${appConfig.nrsBaseUrl}/${uri.value}")
        .withHttpHeaders(hc.headers: _*)
        .withRequestTimeout(appConfig.nrsMaxTimeout)
        .post(Json.toJson(body))
        .map(wsReads.wsRead(_, defaultResult)).recover {
        case e: TimeoutException =>
          logger.warn(s"[NrsConnector][nrsPost] - NRS Call timed out - $e")
          defaultResult
      }
    }

    doPost(nrsHeaderCarrier(hc))
  }

} 
Example 29
Source File: NRSConnector.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up

package uk.gov.hmrc.vatapi.connectors

import java.util.concurrent.TimeoutException

import javax.inject.Inject
import play.api.Logger
import play.api.libs.json.{JsValue, Json, Writes}
import play.api.libs.ws.WSClient
import uk.gov.hmrc.domain.Vrn
import uk.gov.hmrc.http._
import uk.gov.hmrc.play.bootstrap.http.DefaultHttpClient
import uk.gov.hmrc.vatapi.config.AppContext
import uk.gov.hmrc.vatapi.httpparsers.EmptyNrsData
import uk.gov.hmrc.vatapi.httpparsers.NrsSubmissionHttpParser.{NrsSubmissionOutcome, NrsSubmissionOutcomeReads}
import uk.gov.hmrc.vatapi.models.NRSSubmission

import scala.concurrent.duration._
import scala.concurrent.{ExecutionContext, Future}
import scala.util.{Success, Try}


class NRSConnector @Inject()(
                              override val http: DefaultHttpClient,
                              override val appContext: AppContext,
                              ws: WSClient
                            ) extends BaseConnector {

  val logger: Logger = Logger(this.getClass)
  val nrsSubmissionUrl: String => String = vrn => s"${appContext.nrsServiceUrl}/submission"
  val nrsMaxTimeout: Duration = appContext.nrsMaxTimeoutMillis.milliseconds

  private val xApiKeyHeader = "X-API-Key"

  def submit(vrn: Vrn, nrsSubmission: NRSSubmission)(implicit hc: HeaderCarrier, ec: ExecutionContext): Future[NrsSubmissionOutcome] = {

    logger.debug(s"[NRSConnector][submit] - Submission to NRS for 9 box vat return for VRN: $vrn")

    val nrsResponse = {
      val submitUrl = nrsSubmissionUrl(vrn.toString)
      val headers = hc.withExtraHeaders(xApiKeyHeader -> s"${appContext.xApiKey}", "User-Agent" -> appContext.appName).headers

      implicit val nrsWrites = implicitly[Writes[NRSSubmission]]

      ws.url(submitUrl)
        .withHttpHeaders(headers: _*)
        .withRequestTimeout(nrsMaxTimeout)
        .post(Json.toJson(nrsSubmission))
    }

    nrsResponse.map { res =>

      val resJson = Try(res.json) match {
        case Success(json: JsValue) => Some(json)
        case _ => None
      }

      val httpResponse = HttpResponse(
        res.status,
        resJson,
        res.headers,
        None
      )

      Logger.debug(s"[NRSConnector][submit] - NRS Call succeeded")

      NrsSubmissionOutcomeReads.read("", "", httpResponse)

    }.recover {
      case e: TimeoutException => {
        logger.warn(s"[NRSConnector][submit] - NRS Call timed out for VRN: $vrn - $e")
        Right(EmptyNrsData)
      }
    }
  }
} 
Example 30
Source File: NRSConnectorSpec.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.vatapi.connectors

import java.util.concurrent.TimeoutException

import org.scalatestplus.play.guice.GuiceOneAppPerSuite
import play.api.libs.json.{JsResultException, Json, Writes}
import play.api.libs.ws.{WSClient, WSRequest, WSResponse}
import uk.gov.hmrc.domain.Vrn
import uk.gov.hmrc.http.HeaderCarrier
import uk.gov.hmrc.vatapi.UnitSpec
import uk.gov.hmrc.vatapi.assets.TestConstants.NRSResponse._
import uk.gov.hmrc.vatapi.httpparsers.NrsSubmissionHttpParser.NrsSubmissionOutcome
import uk.gov.hmrc.vatapi.httpparsers.{EmptyNrsData, NRSData}
import uk.gov.hmrc.vatapi.mocks.MockHttp
import uk.gov.hmrc.vatapi.mocks.config.MockAppContext
import uk.gov.hmrc.vatapi.models.NRSSubmission

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

class NRSConnectorSpec extends UnitSpec with GuiceOneAppPerSuite
  with MockHttp
  with MockAppContext {

  class Setup {
    val wsClient = mock[WSClient]
    val testNrsConnector = new NRSConnector(mockHttp, mockAppContext, wsClient)

    MockAppContext.nrsMaxTimeoutMilliseconds returns 5000

    val testUrl: String = testNrsConnector.nrsSubmissionUrl(testVrn.vrn)
    def result(requestBody: NRSSubmission): Future[NrsSubmissionOutcome] = testNrsConnector.submit(testVrn, requestBody)
  }

  implicit val hc: HeaderCarrier = HeaderCarrier(nsStamp = 2L)

  val testVrn = Vrn("123456789")

  "NRSConnector.submit" should {

    "successful responses are returned from the connector" should {
      "return the correctly formatted NRS Data model" in new Setup {

        val request = mock[WSRequest]
        val response = mock[WSResponse]
        val expectedResponse = NRSData("2dd537bc-4244-4ebf-bac9-96321be13cdc","This has been deprecated - DO NOT USE","")


        implicit val nrsWrites = implicitly[Writes[NRSSubmission]]
        val meJson = Json.toJson(nrsSubmission)

        when(wsClient.url(testUrl)).thenReturn(request)
        when(request.withHttpHeaders(any())).thenReturn(request)
        when(request.withRequestTimeout(testNrsConnector.nrsMaxTimeout)).thenReturn(request)
        when(request.post(eqTo(meJson))(any())).thenReturn(Future.successful(response))
        when(response.json).thenReturn(nrsResponseJson)
        when(response.status).thenReturn(202)

        await(result(nrsSubmission)) shouldBe Right(expectedResponse)

      }

    }

    "return EmptyNrsData" when {
      "the connection times out" in new Setup {

        val request = mock[WSRequest]

        implicit val nrsWrites = implicitly[Writes[NRSSubmission]]

        when(wsClient.url(testUrl)).thenReturn(request)
        when(request.withHttpHeaders(any())).thenReturn(request)
        when(request.withRequestTimeout(testNrsConnector.nrsMaxTimeout)).thenReturn(request)
        when(request.post(eqTo(Json.toJson(nrsSubmission)))(any())).thenReturn(Future.failed(new TimeoutException("Expected Error")))

        await(result(nrsSubmission)) shouldBe Right(EmptyNrsData)

      }

      "the response JSON cannot be parsed" in new Setup {

        val request = mock[WSRequest]
        val response = mock[WSResponse]

        implicit val nrsWrites = implicitly[Writes[NRSSubmission]]

        when(wsClient.url(testUrl)).thenReturn(request)
        when(request.withHttpHeaders(any())).thenReturn(request)
        when(request.withRequestTimeout(testNrsConnector.nrsMaxTimeout)).thenReturn(request)
        when(request.post(eqTo(Json.toJson(nrsSubmission)))(any())).thenReturn(Future.successful(response))
        when(response.json).thenThrow(JsResultException(Seq()))
        await(result(nrsSubmission)) shouldBe Right(EmptyNrsData)

      }
    }
  }
} 
Example 31
Source File: DockerContainerFactory.scala    From openwhisk   with Apache License 2.0 5 votes vote down vote up
package org.apache.openwhisk.core.containerpool.docker

import akka.actor.ActorSystem

import scala.concurrent.Await
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import org.apache.openwhisk.common.Logging
import org.apache.openwhisk.common.TransactionId
import org.apache.openwhisk.core.WhiskConfig
import org.apache.openwhisk.core.containerpool._
import org.apache.openwhisk.core.entity.ByteSize
import org.apache.openwhisk.core.entity.ExecManifest
import org.apache.openwhisk.core.entity.InvokerInstanceId

import scala.concurrent.duration._
import java.util.concurrent.TimeoutException

import pureconfig._
import pureconfig.generic.auto._
import org.apache.openwhisk.core.ConfigKeys

case class DockerContainerFactoryConfig(useRunc: Boolean)

class DockerContainerFactory(instance: InvokerInstanceId,
                             parameters: Map[String, Set[String]],
                             containerArgsConfig: ContainerArgsConfig =
                               loadConfigOrThrow[ContainerArgsConfig](ConfigKeys.containerArgs),
                             protected val runtimesRegistryConfig: RuntimesRegistryConfig =
                               loadConfigOrThrow[RuntimesRegistryConfig](ConfigKeys.runtimesRegistry),
                             protected val userImagesRegistryConfig: RuntimesRegistryConfig =
                               loadConfigOrThrow[RuntimesRegistryConfig](ConfigKeys.userImagesRegistry),
                             dockerContainerFactoryConfig: DockerContainerFactoryConfig =
                               loadConfigOrThrow[DockerContainerFactoryConfig](ConfigKeys.dockerContainerFactory))(
  implicit actorSystem: ActorSystem,
  ec: ExecutionContext,
  logging: Logging,
  docker: DockerApiWithFileAccess,
  runc: RuncApi)
    extends ContainerFactory {

  
  @throws(classOf[TimeoutException])
  @throws(classOf[InterruptedException])
  private def removeAllActionContainers(): Unit = {
    implicit val transid = TransactionId.invoker
    val cleaning =
      docker.ps(filters = Seq("name" -> s"${ContainerFactory.containerNamePrefix(instance)}_"), all = true).flatMap {
        containers =>
          logging.info(this, s"removing ${containers.size} action containers.")
          val removals = containers.map { id =>
            (if (dockerContainerFactoryConfig.useRunc) {
               runc.resume(id)
             } else {
               docker.unpause(id)
             })
              .recoverWith {
                // Ignore resume failures and try to remove anyway
                case _ => Future.successful(())
              }
              .flatMap { _ =>
                docker.rm(id)
              }
          }
          Future.sequence(removals)
      }
    Await.ready(cleaning, 30.seconds)
  }
}

object DockerContainerFactoryProvider extends ContainerFactoryProvider {
  override def instance(actorSystem: ActorSystem,
                        logging: Logging,
                        config: WhiskConfig,
                        instanceId: InvokerInstanceId,
                        parameters: Map[String, Set[String]]): ContainerFactory = {

    new DockerContainerFactory(instanceId, parameters)(
      actorSystem,
      actorSystem.dispatcher,
      logging,
      new DockerClientWithFileAccess()(actorSystem.dispatcher)(logging, actorSystem),
      new RuncClient()(actorSystem.dispatcher)(logging, actorSystem))
  }

} 
Example 32
Source File: Control_03_CustomRetry.scala    From airframe   with Apache License 2.0 5 votes vote down vote up
package wvlet.airframe.examples.control

import java.util.concurrent.TimeoutException

import wvlet.airframe.control.Retry
import wvlet.log.LogSupport


object Control_03_CustomRetry extends App with LogSupport {
  val withRetry =
    Retry
      .withJitter()
      .retryOn {
        case e: IllegalArgumentException =>
          Retry.nonRetryableFailure(e)
        case e: TimeoutException =>
          Retry
            .retryableFailure(e)
            // Add extra wait millis
            .withExtraWaitMillis(50)
      }

  withRetry.run {
    debug("Hello Retry!")
  }

  withRetry.run {
    debug("Retryer can be reused for other runs")
  }
} 
Example 33
Source File: package.scala    From milan   with Apache License 2.0 5 votes vote down vote up
package com.amazon.milan.compiler.flink

import java.time.Duration
import java.util.concurrent.TimeoutException

import com.amazon.milan.testing.Concurrent
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment

import scala.concurrent.{Await, ExecutionContext, Future, blocking}
import scala.language.implicitConversions


package object testing {
  implicit def extendStreamExecutionEnvironment(env: StreamExecutionEnvironment): StreamExecutionEnvironmentExtensions =
    new StreamExecutionEnvironmentExtensions(env)

  implicit def extendFuture[T](future: Future[T]): FutureExtensions[T] =
    new FutureExtensions[T](future)

  implicit class DurationExtensions(d: Duration) {
    def toConcurrent: scala.concurrent.duration.Duration =
      scala.concurrent.duration.Duration(this.d.toMillis, scala.concurrent.duration.MILLISECONDS)
  }

}


class StreamExecutionEnvironmentExtensions(env: StreamExecutionEnvironment) {
  def executeThenWaitFor(predicate: () => Boolean, secondsToWait: Int): Unit = {
    if (!Concurrent.executeAndWait(
      () => env.execute(),
      predicate,
      Duration.ofSeconds(secondsToWait))) {
      throw new TimeoutException("Timed out waiting for stop condition.")
    }
  }

  def executeAsync(maxSeconds: Int): Future[Boolean] = {
    Concurrent.executeAsync(() => env.execute(), () => true, Duration.ofSeconds(maxSeconds))
  }

  def executeUntilAsync(predicate: () => Boolean, secondsToWait: Int): Future[Unit] = {
    val result =
      Concurrent.executeAsync(
        () => env.execute(),
        predicate,
        Duration.ofSeconds(secondsToWait))

    result.transform(
      success =>
        if (!success) {
          throw new TimeoutException("Timed out waiting for stop condition.")
        },
      ex => throw ex)(ExecutionContext.global)
  }

  def executeAtMost(maxSeconds: Int): Unit = {
    if (!Concurrent.executeUntil(
      () => env.execute(),
      () => true,
      Duration.ofSeconds(maxSeconds))) {
      throw new TimeoutException("Timed out waiting for stop condition.")
    }
  }
}


class FutureExtensions[T](future: Future[T]) {
  def thenWaitFor(duration: Duration)(implicit context: ExecutionContext): Future[T] = {
    Future {
      blocking {
        val result = Await.result(this.future, scala.concurrent.duration.Duration.Inf)
        Thread.sleep(duration.toMillis)
        result
      }
    }
  }
} 
Example 34
Source File: ChangeStream.scala    From changestream   with MIT License 5 votes vote down vote up
package changestream

import java.io.IOException
import java.util.concurrent.TimeoutException

import com.github.shyiko.mysql.binlog.BinaryLogClient
import com.typesafe.config.ConfigFactory
import org.slf4j.LoggerFactory

import scala.concurrent.Await
import scala.concurrent.duration._
import scala.language.postfixOps
import scala.concurrent.ExecutionContext.Implicits.global

object ChangeStream extends App {
  protected val log = LoggerFactory.getLogger(getClass)
  protected val config = ConfigFactory.load().getConfig("changestream")
  protected val mysqlHost = config.getString("mysql.host")
  protected val mysqlPort = config.getInt("mysql.port")
  protected val overridePosition = System.getenv("OVERRIDE_POSITION") match {
    case position:String if (position != null && position.length > 0) => Some(position) //scalastyle:ignore
    case _ => None
  }
  protected val client = new BinaryLogClient(
    mysqlHost,
    mysqlPort,
    config.getString("mysql.user"),
    config.getString("mysql.password")
  )

  
  client.setKeepAliveInterval(config.getLong("mysql.keepalive"))

  ChangeStreamEventListener.setConfig(config)
  ChangestreamEventDeserializerConfig.setConfig(config)

  ChangeStreamEventListener.startControlServer(config)

  client.registerEventListener(ChangeStreamEventListener)
  client.setEventDeserializer(ChangeStreamEventDeserializer)
  client.registerLifecycleListener(ChangeStreamLifecycleListener)

  getConnected(overridePosition)

  def serverName = s"${mysqlHost}:${mysqlPort}"
  def clientId = client.getServerId
  def isConnected = client.isConnected

  def getConnectedAndWait(startingPosition: Option[String]) = Await.result(getConnected(startingPosition), 60.seconds)
  def disconnectClient = client.disconnect()

  def getConnected(startingPosition: Option[String]) = {
    log.info("Starting changestream...")

    val getPositionFuture = startingPosition match {
      case Some(_) =>
        log.info("Overriding starting binlog position with OVERRIDE_POSITION={}", overridePosition)
        ChangeStreamEventListener.setPosition(startingPosition)
      case _ =>
        ChangeStreamEventListener.getStoredPosition
    }

    getPositionFuture.map { position =>
      setBinlogClientPosition(position)
      getInternalClientConnected
    }
  }

  protected def setBinlogClientPosition(position: Option[String]) = position match {
    case Some(position) =>
      log.info("Setting starting binlog position at {}.", position)
      val Array(fileName, posLong) = position.split(":")
      client.setBinlogFilename(fileName)
      client.setBinlogPosition(java.lang.Long.valueOf(posLong))
    case None =>
      log.info("Starting binlog position in real time")
      client.setBinlogFilename(null) //scalastyle:ignore
      client.setBinlogPosition(4L)
  }

  protected def getInternalClientConnected = {
    while(!client.isConnected) {
      try {
        client.connect(5000)
      }
      catch {
        case e: IOException =>
          log.error("Failed to connect to MySQL to stream the binlog, retrying in 5 seconds...", e)
          Thread.sleep(5000)
        case e: TimeoutException =>
          log.error("Timed out connecting to MySQL to stream the binlog, retrying in 5 seconds...", e)
          Thread.sleep(5000)
        case e: Exception =>
          log.error("Failed to connect, exiting...", e)
          Await.result(ChangeStreamEventListener.shutdownAndExit(1), 60.seconds)
      }
    }
  }
} 
Example 35
Source File: MultiThreadingTest.scala    From ncdbg   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package com.programmaticallyspeaking.ncd.nashorn
import java.io.{BufferedReader, InputStreamReader}
import java.util.concurrent.TimeoutException
import java.util.concurrent.atomic.AtomicInteger

import com.programmaticallyspeaking.ncd.host._
import com.programmaticallyspeaking.ncd.messaging.Observer
import com.programmaticallyspeaking.ncd.testing.{SharedInstanceActorTesting, UnitTest}
import jdk.nashorn.api.scripting.NashornScriptEngineFactory
import org.scalatest.concurrent.{Eventually, ScalaFutures}
import org.scalatest.exceptions.TestFailedException
import org.slf4s.Logging

import scala.concurrent.duration._
import scala.concurrent.{Await, ExecutionContext, Future, Promise}

trait MultiThreadingTestFixture extends UnitTest with Logging with SharedInstanceActorTesting with VirtualMachineLauncher with ScalaFutures with FairAmountOfPatience with Eventually {
  override val scriptExecutor: ScriptExecutorBase = MultiThreadedScriptExecutor
  override implicit val executionContext: ExecutionContext = ExecutionContext.global
}

class MultiThreadingTest extends MultiThreadingTestFixture {
  def location(ln: Int) = ScriptLocation(ln, None)

  "Breakpoint requests from other threads should be ignore in a paused state" in {
    val scriptAddedPromise = Promise[Script]()
    val hitBreakpointPromise = Promise[String]()
    val breakpointCounter = new AtomicInteger()
    val host = getHost
    observeScriptEvents(new Observer[ScriptEvent] {

      override def onNext(item: ScriptEvent): Unit = item match {
        case ScriptAdded(script) =>
          scriptAddedPromise.success(script)
        case hb: HitBreakpoint =>
          breakpointCounter.incrementAndGet()
          hitBreakpointPromise.trySuccess("")
        case _ =>
      }

      override def onError(error: Throwable): Unit = {}

      override def onComplete(): Unit = {}
    })

    whenReady(scriptAddedPromise.future) { script =>
      val scriptLocation = eventually {
        host.getBreakpointLocations(ScriptIdentity.fromId(script.id), location(1), None).headOption.getOrElse(fail(s"No line numbers for script ${script.id}"))
      }
      host.setBreakpoint(ScriptIdentity.fromURL(script.url), scriptLocation, BreakpointOptions.empty)

      try {
        whenReady(hitBreakpointPromise.future) { _ =>
          // Ugly, but wait for a while to see if the counter increases over 1 (which it shouldn't).
          Thread.sleep(200)
          breakpointCounter.get() should be(1)
        }
      } catch {
        case t: TestFailedException if t.getMessage().contains("timeout") =>
          val progress = summarizeProgress()
          throw new TimeoutException("Timed out: " + progress)
      }
    }
  }
}

object MultiThreadedScriptExecutor extends App with ScriptExecutorBase {
  println("MultiThreadedScriptExecutor starting. Java version: " + System.getProperty("java.version"))
  val scriptEngine = new NashornScriptEngineFactory().getScriptEngine("--no-syntax-extensions")
  val reader = new BufferedReader(new InputStreamReader(System.in))
  println(Signals.ready)
  waitForSignal(Signals.go)

  // Used a compiled script here before, stopped working with JDK 10
  var src =
    """(function () {
      |  return Math.floor(5.5);
      |})();
    """.stripMargin

  implicit val ec = ExecutionContext.global

  val futures = (1 to 5).map { _ =>
    Future {
      while (true) {
        scriptEngine.eval(src)
      }
    }
  }

  Await.result(Future.sequence(futures), 30.seconds)
} 
Example 36
Source File: RedisTransactionsDemo.scala    From redis4cats   with Apache License 2.0 5 votes vote down vote up
package dev.profunktor.redis4cats

import cats.effect._
import cats.implicits._
import dev.profunktor.redis4cats.hlist._
import dev.profunktor.redis4cats.log4cats._
import dev.profunktor.redis4cats.transactions._
import java.util.concurrent.TimeoutException

object RedisTransactionsDemo extends LoggerIOApp {

  import Demo._

  val program: IO[Unit] = {
    val key1 = "test1"
    val key2 = "test2"

    val showResult: String => Option[String] => IO[Unit] = key =>
      _.fold(putStrLn(s"Not found key: $key"))(s => putStrLn(s"$key: $s"))

    val commandsApi: Resource[IO, RedisCommands[IO, String, String]] =
      Redis[IO].utf8(redisURI)

    commandsApi
      .use { cmd =>
        val getters =
          cmd.get(key1).flatTap(showResult(key1)) *>
              cmd.get(key2).flatTap(showResult(key2))

        // the type is fully inferred but you can be explicit if you'd like

        //type Cmd = IO[Unit] :: IO[Unit] :: IO[Option[String]] :: IO[Unit] :: IO[Unit] :: IO[Option[String]] :: HNil
        val operations =
          cmd.set(key1, "sad") :: cmd.set(key2, "windows") :: cmd.get(key1) ::
              cmd.set(key1, "nix") :: cmd.set(key2, "linux") :: cmd.get(key1) :: HNil

        //type Res = Unit :: Unit :: Option[String] :: Unit :: Unit :: Option[String] :: HNil
        val prog =
          RedisTransaction(cmd)
            .exec(operations)
            .flatMap {
              case _ ~: _ ~: res1 ~: _ ~: _ ~: res2 ~: HNil =>
                putStrLn(s"res1: $res1, res2: $res2")
            }
            .onError {
              case TransactionAborted =>
                putStrLn("[Error] - Transaction Aborted")
              case TransactionDiscarded =>
                putStrLn("[Error] - Transaction Discarded")
              case _: TimeoutException =>
                putStrLn("[Error] - Timeout")
            }

        getters >> prog >> getters >> putStrLn("keep doing stuff...")
      }
  }

} 
Example 37
Source File: RedisPipelineDemo.scala    From redis4cats   with Apache License 2.0 5 votes vote down vote up
package dev.profunktor.redis4cats

import cats.effect._
import cats.implicits._
import dev.profunktor.redis4cats.effect.Log.NoOp._
import dev.profunktor.redis4cats.hlist._
import dev.profunktor.redis4cats.pipeline._
import java.util.concurrent.TimeoutException

object RedisPipelineDemo extends LoggerIOApp {

  import Demo._

  val program: IO[Unit] = {
    val key1 = "testp1"
    val key2 = "testp2"

    val showResult: String => Option[String] => IO[Unit] = key =>
      _.fold(putStrLn(s"Not found key: $key"))(s => putStrLn(s"$key: $s"))

    val commandsApi: Resource[IO, RedisCommands[IO, String, String]] =
      Redis[IO].utf8(redisURI)

    commandsApi
      .use { cmd =>
        val getters =
          cmd.get(key1).flatTap(showResult(key1)) *>
              cmd.get(key2).flatTap(showResult(key2))

        val operations =
          cmd.set(key1, "noop") :: cmd.set(key2, "windows") :: cmd.get(key1) ::
              cmd.set(key1, "nix") :: cmd.set(key2, "linux") :: cmd.get(key1) :: HNil

        val prog =
          RedisPipeline(cmd)
            .filterExec(operations)
            .flatMap {
              case res1 ~: res2 ~: HNil =>
                putStrLn(s"res1: $res1, res2: $res2")
            }
            .onError {
              case PipelineError =>
                putStrLn("[Error] - Pipeline failed")
              case _: TimeoutException =>
                putStrLn("[Error] - Timeout")
            }

        getters >> prog >> getters >> putStrLn("keep doing stuff...")
      }
  }

} 
Example 38
Source File: Assertions.scala    From embedded-kafka   with Apache License 2.0 5 votes vote down vote up
package com.tuplejump.embedded.kafka

import java.util.concurrent.TimeoutException

import scala.annotation.tailrec
import scala.util.control.NonFatal


trait Assertions {

  def eventually[T](timeout: Long, interval: Long)(func: => T): T = {
    def makeAttempt(): Either[Throwable, T] = {
      try Right(func) catch {
        case NonFatal(e) => Left(e)
      }
    }

    val startTime = System.currentTimeMillis()
    @tailrec
    def tryAgain(attempt: Int): T = {
      makeAttempt() match {
        case Right(result) => result
        case Left(e) =>
          val duration = System.currentTimeMillis() - startTime
          if (duration < timeout) {
            Thread.sleep(interval)
          } else {
            throw new TimeoutException(e.getMessage)
          }

          tryAgain(attempt + 1)
      }
    }

    tryAgain(1)
  }

} 
Example 39
Source File: HttpVerb.scala    From http-verbs   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.http

import java.net.{ConnectException, URL}
import java.util.concurrent.TimeoutException

import com.typesafe.config.Config

import scala.collection.JavaConverters.iterableAsScalaIterableConverter
import scala.concurrent.{ExecutionContext, Future}
import scala.util.matching.Regex

trait HttpVerb extends Request {

  protected def configuration: Option[Config]

  def mapErrors(httpMethod: String, url: String, f: Future[HttpResponse])(
    implicit ec: ExecutionContext): Future[HttpResponse] =
    f.recoverWith {
      case e: TimeoutException => Future.failed(new GatewayTimeoutException(gatewayTimeoutMessage(httpMethod, url, e)))
      case e: ConnectException => Future.failed(new BadGatewayException(badGatewayMessage(httpMethod, url, e)))
    }

  def badGatewayMessage(verbName: String, url: String, e: Exception): String =
    s"$verbName of '$url' failed. Caused by: '${e.getMessage}'"

  def gatewayTimeoutMessage(verbName: String, url: String, e: Exception): String =
    s"$verbName of '$url' timed out with message '${e.getMessage}'"

  lazy val internalHostPatterns: Seq[Regex] = configuration match {
    case Some(config) if config.hasPathOrNull("internalServiceHostPatterns") =>
      config.getStringList("internalServiceHostPatterns").asScala.map(_.r).toSeq
    case _ =>
      Seq("^.*\\.service$".r, "^.*\\.mdtp$".r)
  }

  lazy val userAgentHeader: Seq[(String, String)] = configuration match {
    case Some(config) if config.hasPathOrNull("appName") =>
      Seq("User-Agent" -> config.getString("appName"))
    case _ =>
      Seq.empty
  }

  override def applicableHeaders(url: String)(implicit hc: HeaderCarrier): Seq[(String, String)] = {
    val headers = if (internalHostPatterns.exists(_.pattern.matcher(new URL(url).getHost).matches())) {
      hc.headers
    } else {
      hc.headers.filterNot(hc.otherHeaders.contains(_))
    }

    headers ++ userAgentHeader
  }
} 
Example 40
Source File: CommonHttpBehaviour.scala    From http-verbs   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.http

import java.net.ConnectException
import java.util.concurrent.TimeoutException

import org.scalatest.concurrent.ScalaFutures
import org.scalatest.wordspec.AnyWordSpecLike
import org.scalatest.matchers.should.Matchers
import play.api.libs.json.Json
import uk.gov.hmrc.http.logging.{ConnectionTracing, LoggingDetails}

import scala.collection.mutable
import scala.concurrent.{ExecutionContext, Future}

trait CommonHttpBehaviour extends ScalaFutures with Matchers with AnyWordSpecLike {

  case class TestClass(foo: String, bar: Int)
  implicit val tcreads = Json.format[TestClass]

  case class TestRequestClass(baz: String, bar: Int)
  implicit val trcreads = Json.format[TestRequestClass]

  implicit val hc     = HeaderCarrier()
  val testBody        = "testBody"
  val testRequestBody = "testRequestBody"
  val url             = "http://some.url"

  def response(returnValue: Option[String] = None, statusCode: Int = 200) =
    Future.successful(HttpResponse(
      status = statusCode,
      body   = returnValue.getOrElse("")
    ))

  val defaultHttpResponse = response()

  def anErrorMappingHttpCall(verb: String, httpCall: (String, Future[HttpResponse]) => Future[_]) = {
    s"throw a GatewayTimeout exception when the HTTP $verb throws a TimeoutException" in {

      implicit val hc = HeaderCarrier()
      val url: String = "http://some.nonexistent.url"

      val e = httpCall(url, Future.failed(new TimeoutException("timeout"))).failed.futureValue

      e            should be(a[GatewayTimeoutException])
      e.getMessage should startWith(verb)
      e.getMessage should include(url)
    }

    s"throw a BadGateway exception when the HTTP $verb throws a ConnectException" in {

      implicit val hc = HeaderCarrier()
      val url: String = "http://some.nonexistent.url"

      val e = httpCall(url, Future.failed(new ConnectException("timeout"))).failed.futureValue

      e            should be(a[BadGatewayException])
      e.getMessage should startWith(verb)
      e.getMessage should include(url)
    }
  }

  def aTracingHttpCall[T <: ConnectionTracingCapturing](verb: String, method: String, httpBuilder: => T)(
    httpAction: (T => Future[_]))(implicit mf: Manifest[T]) =
    s"trace exactly once when the HTTP $verb calls $method" in {
      val http = httpBuilder
      httpAction(http).futureValue
      http.traceCalls         should have size 1
      http.traceCalls.head._1 shouldBe verb
    }

}

trait ConnectionTracingCapturing extends ConnectionTracing {

  val traceCalls = mutable.Buffer[(String, String)]()

  override def withTracing[T](method: String, uri: String)(
    body: => Future[T])(implicit ld: LoggingDetails, ec: ExecutionContext) = {
    traceCalls += ((method, uri))
    body
  }
} 
Example 41
Source File: HttpTimeoutSpec.scala    From http-verbs   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.play.http

import java.net.{ServerSocket, URI}
import java.util.concurrent.TimeoutException

import org.scalatest.concurrent.ScalaFutures
import org.scalatest.BeforeAndAfterAll
import org.scalatest.wordspec.AnyWordSpecLike
import org.scalatest.matchers.should.Matchers
import org.webbitserver.handler.{DelayedHttpHandler, StringHttpHandler}
import org.webbitserver.netty.NettyWebServer
import play.api.Play
import play.api.test.FakeApplication
import uk.gov.hmrc.http.HeaderCarrier
import uk.gov.hmrc.play.http.ws.WSHttp
import uk.gov.hmrc.play.test.TestHttpCore

import scala.concurrent.Await
import scala.concurrent.duration.DurationInt
import scala.concurrent.ExecutionContext.Implicits.global

class HttpTimeoutSpec extends AnyWordSpecLike with Matchers with ScalaFutures with BeforeAndAfterAll {

  lazy val fakeApplication = FakeApplication(additionalConfiguration = Map("ws.timeout.request" -> "1000"))

  override def beforeAll() {
    super.beforeAll()
    Play.start(fakeApplication)
  }

  override def afterAll() {
    super.afterAll()
    Play.stop(fakeApplication)
  }

  "HttpCalls" should {

    "be gracefully timeout when no response is received within the 'timeout' frame" in {
      val http = new WSHttp with TestHttpCore

      // get an unused port
      val ss = new ServerSocket(0)
      ss.close()
      val publicUri = URI.create(s"http://localhost:${ss.getLocalPort}")
      val ws        = new NettyWebServer(global, ss.getLocalSocketAddress, publicUri)
      try {
        //starts web server
        ws.add(
          "/test",
          new DelayedHttpHandler(global, 2000, new StringHttpHandler("application/json", "{name:'pong'}")))
        ws.start().get()

        implicit val hc = HeaderCarrier()

        val start = System.currentTimeMillis()
        intercept[TimeoutException] {
          //make request to web server
          Await.result(http.doPost(s"$publicUri/test", "{name:'ping'}", Seq()), 5.seconds)
        }
        val diff = (System.currentTimeMillis() - start).toInt
        // there is test execution delay around 700ms
        diff should be >= 1000
        diff should be < 2500
      } finally {
        ws.stop()
      }
    }
  }
} 
Example 42
Source File: HttpTimeoutSpec.scala    From http-verbs   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.play.http

import java.net.{ServerSocket, URI}
import java.util.concurrent.TimeoutException

import org.scalatest.concurrent.ScalaFutures
import org.scalatest.BeforeAndAfterAll
import org.scalatest.wordspec.AnyWordSpecLike
import org.scalatest.matchers.should.Matchers
import org.webbitserver.handler.{DelayedHttpHandler, StringHttpHandler}
import org.webbitserver.netty.NettyWebServer
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.libs.ws.WSClient
import play.api.test.WsTestClient
import play.api.{Configuration, Play}
import uk.gov.hmrc.http.HeaderCarrier
import uk.gov.hmrc.play.http.ws.WSHttp
import uk.gov.hmrc.play.test.TestHttpCore

import scala.concurrent.{Await, ExecutionContext}
import scala.concurrent.duration.DurationInt

class HttpTimeoutSpec extends AnyWordSpecLike with Matchers with ScalaFutures with BeforeAndAfterAll {

  import ExecutionContext.Implicits.global

  lazy val fakeApplication =
    GuiceApplicationBuilder(configuration = Configuration("play.ws.timeout.request" -> "1000ms")).build()

  override def beforeAll() {
    super.beforeAll()
    Play.start(fakeApplication)
  }

  override def afterAll() {
    super.afterAll()
    Play.stop(fakeApplication)
  }

  WsTestClient.withClient{ client =>

    "HttpCalls" should {

      "be gracefully timeout when no response is received within the 'timeout' frame" in {
        val http = new WSHttp with TestHttpCore {
          override val wsClient = fakeApplication.injector.instanceOf[WSClient]
        }

        // get an unused port
        val ss = new ServerSocket(0)
        ss.close()
        val executor = ExecutionContext.global // fromExecutorService(ExecutionContext.global)
        val publicUri = URI.create(s"http://localhost:${ss.getLocalPort}")
        val ws        = new NettyWebServer(executor, ss.getLocalSocketAddress, publicUri)
        try {
          //starts web server
          ws.add(
            "/test",
            new DelayedHttpHandler(executor, 2000, new StringHttpHandler("application/json", "{name:'pong'}")))
          ws.start().get()

          implicit val hc = HeaderCarrier()

          val start = System.currentTimeMillis()
          intercept[TimeoutException] {
            //make request to web server
            Await.result(http.doPost(s"$publicUri/test", "{name:'ping'}", Seq()), 5.seconds)
          }
          val diff = (System.currentTimeMillis() - start).toInt
          // there is test execution delay around 700ms
          diff should be >= 1000
          diff should be < 2500
        } finally {
          ws.stop()
        }
      }
    }
  }
} 
Example 43
Source File: RemoraApp.scala    From remora   with MIT License 5 votes vote down vote up
import java.io.IOException
import java.net.ConnectException
import java.util.concurrent.{TimeUnit, TimeoutException}

import akka.actor.ActorSystem
import akka.stream.{ActorMaterializer, ActorMaterializerSettings, Supervision}
import com.amazonaws.services.cloudwatch.{AmazonCloudWatchAsync, AmazonCloudWatchAsyncClientBuilder}
import com.blacklocus.metrics.CloudWatchReporterBuilder
import com.codahale.metrics.jvm.{GarbageCollectorMetricSet, MemoryUsageGaugeSet, ThreadStatesGaugeSet}
import com.typesafe.scalalogging.LazyLogging
import config.{KafkaSettings, MetricsSettings}
import kafka.admin.RemoraKafkaConsumerGroupService
import reporter.RemoraDatadogReporter

import scala.concurrent.duration._
import scala.util.control.NonFatal

object RemoraApp extends App with nl.grons.metrics.scala.DefaultInstrumented with LazyLogging {

  private val actorSystemName: String = "remora"
  implicit val actorSystem = ActorSystem(actorSystemName)

  metricRegistry.registerAll(new GarbageCollectorMetricSet)
  metricRegistry.registerAll(new MemoryUsageGaugeSet)
  metricRegistry.registerAll(new ThreadStatesGaugeSet)

  lazy val decider: Supervision.Decider = {
    case _: IOException | _: ConnectException | _: TimeoutException => Supervision.Restart
    case NonFatal(err: Throwable) =>
      actorSystem.log.error(err, "Unhandled Exception in Stream: {}", err.getMessage)
      Supervision.Stop
  }

  implicit val materializer = ActorMaterializer(
    ActorMaterializerSettings(actorSystem).withSupervisionStrategy(decider))(actorSystem)

  implicit val executionContext = actorSystem.dispatchers.lookup("kafka-consumer-dispatcher")
  val kafkaSettings = KafkaSettings(actorSystem.settings.config)
  val consumer = new RemoraKafkaConsumerGroupService(kafkaSettings)
  val kafkaClientActor = actorSystem.actorOf(KafkaClientActor.props(consumer), name = "kafka-client-actor")

  Api(kafkaClientActor).start()

  val metricsSettings = MetricsSettings(actorSystem.settings.config)

  if (metricsSettings.registryOptions.enabled) {
    val exportConsumerMetricsToRegistryActor =
      actorSystem.actorOf(ExportConsumerMetricsToRegistryActor.props(kafkaClientActor),
        name = "export-consumer-metrics-actor")
    actorSystem.scheduler.schedule(0 second, metricsSettings.registryOptions.intervalSeconds second, exportConsumerMetricsToRegistryActor, "export")
  }

  if (metricsSettings.cloudWatch.enabled) {
    logger.info("Reporting metricsRegistry to Cloudwatch")
    val amazonCloudWatchAsync: AmazonCloudWatchAsync = AmazonCloudWatchAsyncClientBuilder.defaultClient

    new CloudWatchReporterBuilder()
      .withNamespace(metricsSettings.cloudWatch.name)
      .withRegistry(metricRegistry)
      .withClient(amazonCloudWatchAsync)
      .build()
      .start(metricsSettings.cloudWatch.intervalMinutes, TimeUnit.MINUTES)
  }

  if (metricsSettings.dataDog.enabled) {
    logger.info(s"Reporting metricsRegistry to Datadog at ${metricsSettings.dataDog.agentHost}:${metricsSettings.dataDog.agentPort}")
    val datadogReporter = new RemoraDatadogReporter(metricRegistry, metricsSettings.dataDog)
    datadogReporter.startReporter()
  }

} 
Example 44
Source File: LocalStackReadyLogWaitStrategy.scala    From aws-spi-akka-http   with Apache License 2.0 5 votes vote down vote up
package com.github.matsluni.akkahttpspi.testcontainers

import java.util.concurrent.{TimeUnit, TimeoutException}
import java.util.function.Predicate

import org.testcontainers.DockerClientFactory
import org.testcontainers.containers.ContainerLaunchException
import org.testcontainers.containers.output.{OutputFrame, WaitingConsumer}
import org.testcontainers.containers.wait.strategy.AbstractWaitStrategy
import org.testcontainers.utility.LogUtils

import scala.compat.java8.FunctionConverters._

object LocalStackReadyLogWaitStrategy extends AbstractWaitStrategy {
  override def waitUntilReady(): Unit = {
    val waitingConsumer = new WaitingConsumer
    LogUtils.followOutput(DockerClientFactory.instance.client, waitStrategyTarget.getContainerId, waitingConsumer)

    val waitPredicate: Predicate[OutputFrame] = ((outputFrame: OutputFrame) => outputFrame.getUtf8String.contains("Ready.")).asJava

    try
      waitingConsumer.waitUntil(waitPredicate, startupTimeout.getSeconds, TimeUnit.SECONDS, 1)
    catch {
      case e: TimeoutException =>
        throw new ContainerLaunchException("Timed out waiting for localstack")
    }
  }
}