akka.actor.ActorRefFactory Scala Examples

The following examples show how to use akka.actor.ActorRefFactory. 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: ServiceLocation.scala    From shield   with MIT License 5 votes vote down vote up
package shield.config

import akka.actor.ActorRefFactory
import shield.transports.{HttpTransport, LambdaTransport}
import shield.transports.HttpTransport.SendReceive
import spray.http.Uri
import scala.concurrent.ExecutionContext
import scala.concurrent.duration._


sealed trait ServiceLocation{
  def locationName : String
}

case class HttpServiceLocation(baseUrl: Uri) extends ServiceLocation {
  // nb: spray.http.Uri does not provide default port numbers.  If one is not given, baseUrl.authority.port == 0
  require(baseUrl.isAbsolute, "baseUrl must be absolute")
  require(baseUrl.scheme == "http" || baseUrl.scheme == "https", "baseUrl must use either HTTP or HTTPS")
  require(baseUrl.authority.userinfo == "", "baseUrl cannot have auth information")
  require(baseUrl.path == Uri.Path.Empty, "baseUrl cannot have a path")
  require(baseUrl.query == Uri.Query.Empty, "baseUrl cannot have a query")
  require(baseUrl.fragment.isEmpty, "baseUrl cannot have a fragment")

  override def locationName : String = baseUrl.toString()
}

case class LambdaServiceLocation(arn : String) extends ServiceLocation {
  override def locationName : String = arn
}

object TransportFactory {
  def apply(location: ServiceLocation)(implicit factory: ActorRefFactory, executor: ExecutionContext) : SendReceive = location match {
    case HttpServiceLocation(uri) => HttpTransport.httpTransport(uri, 5.seconds)
    case LambdaServiceLocation(arn) => LambdaTransport.lambdaTransport(arn)
  }
} 
Example 2
Source File: SnsActorSpec.scala    From changestream   with MIT License 5 votes vote down vote up
package changestream.actors

import akka.actor.{ActorRefFactory, Props}
import akka.testkit.{TestActorRef, TestProbe}
import changestream.actors.PositionSaver.EmitterResult
import changestream.helpers.{Config, Emitter}
import com.typesafe.config.ConfigFactory

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

class SnsActorSpec extends Emitter with Config {
  val probe = TestProbe()
  val maker = (_: ActorRefFactory) => probe.ref

  val actorRef = TestActorRef(Props(classOf[SnsActor], maker, awsConfig))

  val configWithInterpolation = ConfigFactory.
    parseString("aws.sns.topic = \"__integration_tests-{database}-{tableName}\"").
    withFallback(awsConfig)
  val snsWithInterpolation = TestActorRef(Props(classOf[SnsActor], maker, configWithInterpolation))


  "When SnsActor receives a single valid message" should {
    "Immediately publish the message to SNS" in {
      actorRef ! message

      val result = probe.expectMsgType[EmitterResult](5000 milliseconds)
      result.position should be(message.nextPosition)
    }
  }

  "When SnsActor receives a message" should {
    "Should correctly publish the message when the topic contains interpolated database and/or tableName" in {
      snsWithInterpolation ! message

      val result = probe.expectMsgType[EmitterResult](5000 milliseconds)
      result.position should be(message.nextPosition)
    }
  }
} 
Example 3
Source File: S3ActorSpec.scala    From changestream   with MIT License 5 votes vote down vote up
package changestream.actors

import akka.actor.{ActorRefFactory, Props}
import akka.testkit.{TestActorRef, TestProbe}
import changestream.actors.PositionSaver.EmitterResult
import changestream.helpers.{Config, Emitter}

import scala.concurrent.duration._
import com.typesafe.config.ConfigFactory

import scala.language.postfixOps

class S3ActorSpec extends Emitter with Config {
  val probe = TestProbe()
  val maker = (_: ActorRefFactory) => probe.ref
  val s3Config = ConfigFactory.
    parseString("aws.s3.batch-size = 2, aws.s3.flush-timeout = 1000").
    withFallback(awsConfig)
  val actorRef = TestActorRef(Props(classOf[S3Actor], maker, s3Config))

  "When S3Actor receives a single valid message" should {
    "Add the message to S3 in a batch of one" in {
      actorRef ! message

      val result = probe.expectMsgType[EmitterResult](5000 milliseconds)
      result.position should be(message.nextPosition)
      result.meta.get.asInstanceOf[String] should endWith ("-1.json")
    }
  }

  "When S3Actor receives multiple valid messages in quick succession" should {
    "Add the messages to S3 in a batch of many" in {
      actorRef ! message
      actorRef ! message.copy(nextPosition = "FOOBAZ")

      val result = probe.expectMsgType[EmitterResult](5000 milliseconds)
      result.position should be("FOOBAZ")
      result.meta.get.asInstanceOf[String] should endWith ("-2.json")
    }
  }

  "When S3Actor receives multiple valid messages in slow succession" should {
    "Add the messages to the S3 queue in multiple batches of one message" in {
      actorRef ! message
      Thread.sleep(2000)
      actorRef ! message.copy(nextPosition = "FOOBAZ")

      val result1 = probe.expectMsgType[EmitterResult](5000 milliseconds)
      val result2 = probe.expectMsgType[EmitterResult](5000 milliseconds)
      result1.position should be(message.nextPosition)
      result1.meta.get.asInstanceOf[String] should endWith ("-1.json")
      result2.position should be("FOOBAZ")
      result2.meta.get.asInstanceOf[String] should endWith ("-1.json")
    }
  }

  "When S3Actor receives multiple valid messages that exceed the flush size" should {
    "Add the messages to the S3 queue in multiple batches" in {
      actorRef ! message
      actorRef ! message.copy(nextPosition = "FOOBAZ")
      actorRef ! message.copy(nextPosition = "BIPBOP")

      val result1 = probe.expectMsgType[EmitterResult](5000 milliseconds)
      val result2 = probe.expectMsgType[EmitterResult](5000 milliseconds)
      result1.position should be("FOOBAZ")
      result1.meta.get.asInstanceOf[String] should endWith ("-2.json")
      result2.position should be("BIPBOP")
      result2.meta.get.asInstanceOf[String] should endWith ("-1.json")
    }
  }
} 
Example 4
Source File: TaskLauncher.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.streaming.executor

import akka.actor.{Actor, ActorRef, ActorRefFactory, Props}

import org.apache.gearpump.cluster.{ExecutorContext, UserConfig}
import org.apache.gearpump.serializer.SerializationFramework
import org.apache.gearpump.streaming.ProcessorDescription
import org.apache.gearpump.streaming.executor.TaskLauncher.TaskArgument
import org.apache.gearpump.streaming.task._
import org.apache.gearpump.streaming.util.ActorPathUtil

trait ITaskLauncher {

  
  def launch(taskIds: List[TaskId], argument: TaskArgument,
      context: ActorRefFactory, serializer: SerializationFramework, dispatcher: String)
    : Map[TaskId, ActorRef]
}

class TaskLauncher(
    appId: Int,
    appName: String,
    executorId: Int,
    appMaster: ActorRef,
    userConf: UserConfig,
    taskActorClass: Class[_ <: Actor])
  extends ITaskLauncher{

  override def launch(
      taskIds: List[TaskId], argument: TaskArgument,
      context: ActorRefFactory, serializer: SerializationFramework, dispatcher: String)
    : Map[TaskId, ActorRef] = {
    import argument.{processorDescription, subscribers}

    val taskConf = userConf.withConfig(processorDescription.taskConf)

    val taskContext = TaskContextData(executorId,
      appId, appName, appMaster,
      processorDescription.parallelism,
      processorDescription.life, subscribers)

    val taskClass = TaskUtil.loadClass(processorDescription.taskClass)

    var tasks = Map.empty[TaskId, ActorRef]
    taskIds.foreach { taskId =>
      val task = new TaskWrapper(taskId, taskClass, taskContext, taskConf)
      val taskActor = context.actorOf(Props(taskActorClass, taskId, taskContext, userConf, task,
        serializer).withDispatcher(dispatcher), ActorPathUtil.taskActorName(taskId))
      tasks += taskId -> taskActor
    }
    tasks
  }
}

object TaskLauncher {

  case class TaskArgument(
      dagVersion: Int, processorDescription: ProcessorDescription,
      subscribers: List[Subscriber])

  def apply(executorContext: ExecutorContext, userConf: UserConfig): TaskLauncher = {
    import executorContext.{appId, appMaster, appName, executorId}
    new TaskLauncher(appId, appName, executorId, appMaster, userConf, classOf[TaskActor])
  }
} 
Example 5
Source File: ActorLoader.scala    From incubator-toree   with Apache License 2.0 5 votes vote down vote up
package org.apache.toree.kernel.protocol.v5.client

import akka.actor.{ActorRefFactory, ActorSelection}


  def load(actorEnum: Enumeration#Value): ActorSelection
}

case class SimpleActorLoader(actorRefFactory: ActorRefFactory)
  extends ActorLoader
{
  private val userActorDirectory: String = "/user/%s"

  override def load(actorEnum: Enumeration#Value): ActorSelection = {
    actorRefFactory.actorSelection(
      userActorDirectory.format(actorEnum.toString)
    )
  }
} 
Example 6
Source File: MongoClient.scala    From tepkin   with Apache License 2.0 5 votes vote down vote up
package net.fehmicansaglam.tepkin


import akka.actor.ActorDSL._
import akka.actor.{ActorRef, ActorRefFactory, ActorSystem, Terminated}
import net.fehmicansaglam.tepkin.TepkinMessage.ShutDown
import net.fehmicansaglam.tepkin.protocol.ReadPreference

import scala.concurrent.ExecutionContext

class MongoClient(_context: ActorRefFactory, uri: MongoClientUri, nConnectionsPerNode: Int) {
  val poolManager = _context.actorOf(
    MongoPoolManager
      .props(uri, nConnectionsPerNode, uri.option("readPreference").map(ReadPreference.apply))
      .withMailbox("tepkin-mailbox"),
    name = "tepkin-pool")


  implicit def context: ActorRefFactory = _context

  implicit def ec: ExecutionContext = _context.dispatcher

  def apply(databaseName: String): MongoDatabase = {
    require(databaseName != null && databaseName.getBytes("UTF-8").size < 123,
      "Database name must be shorter than 123 bytes")
    new MongoDatabase(poolManager, databaseName)
  }

  def db(databaseName: String): MongoDatabase = {
    apply(databaseName)
  }

  def shutdown(): Unit = {
    poolManager ! ShutDown
  }

  
  def shutdown(ref: ActorRef, refs: ActorRef*): Unit = {
    val allRefs = refs :+ ref

    actor(new Act {
      var remaining = allRefs.length

      whenStarting {
        allRefs.foreach(context.watch)
      }

      become {
        case _: Terminated =>
          remaining -= 1
          if (remaining == 0) {
            poolManager ! ShutDown
            context.stop(self)
          }
      }
    })

    ()
  }
}

object MongoClient {

  def apply(uri: String,
            nConnectionsPerNode: Int = 10,
            context: ActorRefFactory = ActorSystem("tepkin-system")): MongoClient = {
    new MongoClient(context, MongoClientUri(uri), nConnectionsPerNode)
  }

} 
Example 7
Source File: DefaultDomainFactory.scala    From ncdbg   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package com.programmaticallyspeaking.ncd.chrome.domains

import akka.actor.{ActorRef, ActorRefFactory, Props}
import com.programmaticallyspeaking.ncd.infra.IdGenerator
import com.programmaticallyspeaking.ncd.ioc.Container

trait DomainFactory {
  def create(domain: String)(implicit factory: ActorRefFactory): ActorRef
}

class DefaultDomainFactory(container: Container) extends DomainFactory {
  private val actorNameIdGenerator = new IdGenerator("domact")

  def create(domain: String)(implicit factory: ActorRefFactory): ActorRef = {
    val clazz = lookupActorClass(domain)
    def creator(clazz: Class[_], args: Seq[Any]) = Props(clazz, args: _*)
    factory.actorOf(container.newInstance(clazz, creator), domain + "-" + actorNameIdGenerator.next)
  }

  private def lookupActorClass(domain: String): Class[_] = {
    val className = getClass.getPackage.getName + "." + domain
    val rejection = new IllegalArgumentException("Not a domain actor: " + className)
    try {
      val clazz = Class.forName(className)
      val baseClass = classOf[DomainActor]
      if (baseClass == clazz || !baseClass.isAssignableFrom(clazz)) throw rejection
      clazz
    } catch {
      case ex: ClassNotFoundException => throw rejection
    }
  }
} 
Example 8
Source File: CapturingDomainFactory.scala    From ncdbg   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package com.programmaticallyspeaking.ncd.chrome.net

import akka.actor.{Actor, ActorRef, ActorRefFactory, ActorSystem, Inbox, Props, Terminated}
import com.programmaticallyspeaking.ncd.chrome.domains.{DefaultDomainFactory, DomainFactory}
import com.programmaticallyspeaking.ncd.ioc.Container

import scala.concurrent.duration._

class CapturingDomainFactory(implicit container: Container, system: ActorSystem) extends DomainFactory {
  import CapturingDomainFactory._
  private val defaultFactory = new DefaultDomainFactory(container)

  private var actorMustNotExist = false
  private val watcher = system.actorOf(Props(new ActorWatcher))
  private val inbox = Inbox.create(system)

  private def sendAndWait(msg: AnyRef): Any = {
    inbox.send(watcher, msg)
    inbox.receive(2.seconds)
  }

  def actorByName(name: String): Option[ActorRef] = {
    sendAndWait(FindActor(name)) match {
      case FindActorResponse(maybeRef) => maybeRef
      case other => throw new UnsupportedOperationException("Unexpected FindActor response: " + other)
    }
  }

  def requireNoOldActor(): Unit = {
    actorMustNotExist = true
  }

  override def create(domain: String)(implicit factory: ActorRefFactory): ActorRef = {
    actorByName(domain) match {
      case Some(ar) if actorMustNotExist =>
        throw new IllegalStateException("Found an old domain actor: " + ar)
      case _ => // noop
    }

    val actor = defaultFactory.create(domain)
    sendAndWait(DomainActor(actor, domain))
    actor
  }
}

object CapturingDomainFactory {

  case class DomainActor(actor: ActorRef, domain: String)
  case class FindActor(domain: String)
  case class FindActorResponse(actor: Option[ActorRef])

  class ActorWatcher extends Actor {

    private var actors = Map[String, ActorRef]()

    override def receive: Receive = {
      case DomainActor(actorRef, domain) =>
        actors += domain -> actorRef
        context.watch(actorRef)
        sender ! "ok"

      case Terminated(actorRef) =>
        actors.find(_._2 == actorRef).map(_._1).foreach(actors -= _)

      case FindActor(domain) =>
        sender ! FindActorResponse(actors.get(domain))
    }
  }
} 
Example 9
Source File: ProxyBalancer.scala    From shield   with MIT License 5 votes vote down vote up
package shield.proxying

import akka.actor.{ActorContext, ActorRef, ActorRefFactory, PoisonPill}
import akka.pattern.ask
import akka.routing.RoundRobinGroup
import akka.util.Timeout
import shield.actors.Middleware
import shield.actors.config.{ProxyState, WeightedProxyState}
import shield.config.ServiceLocation
import shield.routing.{EndpointTemplate, Param}
import spray.http.{HttpRequest, HttpResponse}

import scala.annotation.tailrec
import scala.concurrent.duration._
import scala.concurrent.{ExecutionContext, Future}
import scala.util.Random

case class ProxyRequest(template: EndpointTemplate, request: HttpRequest)
case class ProxiedResponse(upstreamService: ServiceLocation, serviceName: String, template: EndpointTemplate, cacheParams: Set[Param], response: HttpResponse)


trait ProxyBalancer {
  def proxy(template: EndpointTemplate, request: HttpRequest) : Future[ProxiedResponse]
}

object FailBalancer extends ProxyBalancer {
  def proxy(template: EndpointTemplate, request: HttpRequest): Future[ProxiedResponse] =
    Future.failed(new NotImplementedError())
}

class AkkaBalancer(val balancer: ActorRef) extends ProxyBalancer {
  // todo: use global timeout config
  implicit val timeout = Timeout(60.seconds)
  def proxy(template: EndpointTemplate, request: HttpRequest) = (balancer ? ProxyRequest(template, request)).mapTo[ProxiedResponse]
}

trait ProxyBalancerBuilder[T <: ProxyBalancer] {
  val allMiddleware : List[Middleware]
  def build(actors: Set[ActorRef]) : ProxyBalancer
  def teardown() : Unit
}

object EmptyBalancerBuilder extends ProxyBalancerBuilder[ProxyBalancer] {
  val allMiddleware : List[Middleware] = Nil
  def build(actors: Set[ActorRef]) : ProxyBalancer = {
    FailBalancer
  }
  def teardown() : Unit = {}
}

// todo: weighted service instances (and dynamic weighting)
// todo: retry safe gets (config option to enable) via something like http://doc.akka.io/docs/akka/snapshot/scala/routing.html#TailChoppingPool_and_TailChoppingGroup
class RoundRobinBalancerBuilder(val allMiddleware: List[Middleware], factory: ActorRefFactory, hostProxies: Map[ActorRef, WeightedProxyState])(implicit execContext: ExecutionContext) extends ProxyBalancerBuilder[AkkaBalancer] {
  var balancers : List[ActorRef] = Nil

  // https://en.wikipedia.org/wiki/Euclidean_algorithm#Implementations
  // nb: gcd is associative, so we're safe to `reduce` the results
  @tailrec
  private def gcd(a: Int, b: Int) : Int =
    if (b == 0) {
      a
    } else {
      gcd(b, a % b)
    }


  def build(actors: Set[ActorRef]) : AkkaBalancer = {
    // todo: refactor this out to somewhere common when we have other balancer types
    val actorWeight = actors.map(hostProxies(_).weight)
    val totalWeight = actorWeight.sum
    val group = if (totalWeight == 0) {
      actors.toList
    } else {
      val actorGCD = actorWeight.filter(_ != 0).reduceLeftOption(gcd).getOrElse(1)
      Random.shuffle(actors.toList.flatMap(a => List.fill(hostProxies(a).weight / actorGCD)(a)))
    }
    val balancer = factory.actorOf(RoundRobinGroup(group.map(_.path.toString)).props())
    balancers = balancer :: balancers

    new AkkaBalancer(balancer)
  }

  def teardown() = {
    for (balancer <- balancers) {
      // just stop the router, not the host proxy behind them
      balancer ! PoisonPill
    }
    balancers = Nil
  }
} 
Example 10
Source File: SqsActorSpec.scala    From changestream   with MIT License 5 votes vote down vote up
package changestream.actors

import akka.actor.{ActorRefFactory, Props}
import akka.testkit.{TestActorRef, TestProbe}
import changestream.actors.PositionSaver.EmitterResult
import changestream.actors.SqsActor.BatchResult
import changestream.helpers.{Config, Emitter}

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

class SqsActorSpec extends Emitter with Config {
  val probe = TestProbe()
  val maker = (_: ActorRefFactory) => probe.ref
  val actorRef = TestActorRef(Props(classOf[SqsActor], maker, awsConfig))

  "When SqsActor receives a single valid message" should {
    "Add the message to the SQS queue in a batch of one" in {
      actorRef ! message

      val result = probe.expectMsgType[EmitterResult](5000 milliseconds)
      result.position should be(message.nextPosition)
      result.meta.get shouldBe a[BatchResult]
      result.meta.get.asInstanceOf[BatchResult].failed shouldBe empty
      result.meta.get.asInstanceOf[BatchResult].queued should have length 1
    }
  }

  "When SqsActor receives multiple valid messages in quick succession" should {
    "Add the messages to the SQS queue in a batch of multiple" in {
      actorRef ! message
      actorRef ! message.copy(nextPosition = "FOOBAZ")

      val result = probe.expectMsgType[EmitterResult](5000 milliseconds)
      result.position should be("FOOBAZ")
      result.meta.get shouldBe a[BatchResult]
      result.meta.get.asInstanceOf[BatchResult].failed shouldBe empty
      result.meta.get.asInstanceOf[BatchResult].queued should have length 2
    }
  }

  "When SqsActor receives multiple valid messages in slow succession" should {
    "Add the messages to the SQS queue in multiple batches of one message" in {
      actorRef ! message
      Thread.sleep(500)
      actorRef ! message.copy(nextPosition = "FOOBAZ")

      val result1 = probe.expectMsgType[EmitterResult](5000 milliseconds)
      val result2 = probe.expectMsgType[EmitterResult](5000 milliseconds)

      result1.position should be(message.nextPosition)
      result1.meta.get shouldBe a[BatchResult]
      result1.meta.get.asInstanceOf[BatchResult].failed shouldBe empty
      result1.meta.get.asInstanceOf[BatchResult].queued should have length 1

      result2.position should be("FOOBAZ")
      result2.meta.get shouldBe a[BatchResult]
      result2.meta.get.asInstanceOf[BatchResult].failed shouldBe empty
      result2.meta.get.asInstanceOf[BatchResult].queued should have length 1
    }
  }
} 
Example 11
Source File: HttpTransport.scala    From shield   with MIT License 5 votes vote down vote up
package shield.transports

import akka.actor.{ActorRef, ActorRefFactory}
import akka.pattern.ask
import akka.io.IO
import akka.util.Timeout
import spray.can.Http
import spray.can.Http.HostConnectorSetup
import spray.can.client.HostConnectorSettings
import spray.http.Uri.Authority
import spray.http.{Uri, HttpResponsePart, HttpResponse, HttpRequest}
import spray.httpx.{ResponseTransformation, RequestBuilding}
import spray.util._
import scala.concurrent.{ExecutionContext, Future}
import scala.concurrent.duration._

object HttpTransport extends RequestBuilding with ResponseTransformation {
  // Imitating client\pipelining.scala from spray, but taking advantage of the fact that
  // Spray's "Request-level API" lets you send a tuple of (Request, ConnectorSetup) to specify destination
  // without an absolute uri or a Host header
  type SendReceive = spray.client.pipelining.SendReceive

  def httpTransport(host: Uri, futureTimeout: FiniteDuration)(implicit refFactory: ActorRefFactory, executionContext: ExecutionContext) : SendReceive =
    httpTransport(IO(Http)(actorSystem), HostConnectorSetup(host.authority.host.address, host.effectivePort, sslEncryption = host.scheme == "https"))(executionContext, Timeout(futureTimeout))

  def httpTransport(host: Uri, settings: HostConnectorSettings)(implicit refFactory: ActorRefFactory, executionContext: ExecutionContext,
                                                                futureTimeout: Timeout) : SendReceive =
    httpTransport(IO(Http)(actorSystem), HostConnectorSetup(host.authority.host.address, host.effectivePort, sslEncryption = host.scheme == "https", settings=Some(settings)))


  def httpTransport(transport: ActorRef, config: HostConnectorSetup)(implicit ec: ExecutionContext, futureTimeout: Timeout): SendReceive =
    request => transport ? (request, config) map {
      case x: HttpResponse          => x
      case x: HttpResponsePart      => sys.error("sendReceive doesn't support chunked responses, try sendTo instead")
      case x: Http.ConnectionClosed => sys.error("Connection closed before reception of response: " + x)
      case x                        => sys.error("Unexpected response from HTTP transport: " + x)
    }
} 
Example 12
Source File: WorkerConnector.scala    From mist   with Apache License 2.0 5 votes vote down vote up
package io.hydrosphere.mist.master.execution.workers

import akka.actor.{ActorRef, ActorRefFactory}
import io.hydrosphere.mist.master.models.{ContextConfig, RunMode}
import io.hydrosphere.mist.utils.akka.WhenTerminated

import scala.concurrent.{Future, Promise}

trait WorkerConnector {

  def askConnection(): Future[PerJobConnection]

  def warmUp(): Unit

  def shutdown(force: Boolean): Future[Unit]

  def whenTerminated(): Future[Unit]

}

object WorkerConnector {

  sealed trait Event
  object Event {
    final case class AskConnection(resolve: Promise[PerJobConnection]) extends Event
    final case class Released(conn: WorkerConnection) extends Event
    final case class Shutdown(force: Boolean) extends Event
    case object WarmUp extends Event
    final case class ConnTerminated(connId: String) extends Event
    case object GetStatus
  }

  class ActorBasedWorkerConnector(
    underlying: ActorRef,
    termination: Future[Unit]
  ) extends WorkerConnector {

    override def askConnection(): Future[PerJobConnection] = {
      val promise = Promise[PerJobConnection]
      underlying ! WorkerConnector.Event.AskConnection(promise)
      promise.future
    }

    override def shutdown(force: Boolean): Future[Unit] = {
      underlying ! Event.Shutdown(force)
      whenTerminated()
    }

    override def whenTerminated(): Future[Unit] = termination

    override def warmUp(): Unit = underlying ! WorkerConnector.Event.WarmUp


  }

  def actorBased(
    id: String,
    ctx: ContextConfig,
    runner: WorkerRunner,
    af: ActorRefFactory
  ): WorkerConnector = {
    val props = ctx.workerMode match {
      case RunMode.Shared => SharedConnector.props(id, ctx, runner)
      case RunMode.ExclusiveContext => ExclusiveConnector.props(id, ctx, runner)
    }
    val actor = af.actorOf(props)
    new ActorBasedWorkerConnector(actor, WhenTerminated(actor)(af))
  }

} 
Example 13
Source File: WorkerRunner.scala    From mist   with Apache License 2.0 5 votes vote down vote up
package io.hydrosphere.mist.master.execution.workers

import akka.actor.{ActorRef, ActorRefFactory}
import io.hydrosphere.mist.core.CommonData.WorkerInitInfo
import io.hydrosphere.mist.master.execution.SpawnSettings
import io.hydrosphere.mist.master.execution.workers.starter.WorkerProcess
import io.hydrosphere.mist.master.models.ContextConfig
import io.hydrosphere.mist.utils.akka.ActorRegHub

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.{Future, Promise}
import scala.concurrent.duration.FiniteDuration
import scala.util._

trait WorkerRunner extends ((String, ContextConfig) => Future[WorkerConnection])

object WorkerRunner {

  class DefaultRunner(
    spawn: SpawnSettings,
    regHub: ActorRegHub,
    connect: (String, WorkerInitInfo, FiniteDuration, ActorRef, StopAction) => Future[WorkerConnection]
  ) extends WorkerRunner {

    override def apply(id: String, ctx: ContextConfig): Future[WorkerConnection] = {
      import spawn._

      val initInfo = toWorkerInitInfo(ctx)

      def continueSetup(ps: WorkerProcess.StartedProcess): Future[WorkerConnection] ={
        val regFuture = for {
          ref <- regHub.waitRef(id, timeout)
          connection <- connect(id, initInfo, readyTimeout, ref, runnerCmd.stopAction)
        } yield connection

        val promise = Promise[WorkerConnection]
        ps match {
          case WorkerProcess.Local(term) =>
            term.failed.foreach(e => promise.tryComplete(Failure(new RuntimeException(s"Process terminated with error $e"))))
          case WorkerProcess.NonLocal =>
        }

        runnerCmd.stopAction match {
          case StopAction.CustomFn(f) => regFuture.failed.foreach(_ => f(id))
          case StopAction.Remote =>
        }

        regFuture.onComplete(r => promise.tryComplete(r))
        promise.future
      }


      runnerCmd.onStart(id, initInfo) match {
        case ps: WorkerProcess.StartedProcess => continueSetup(ps)
        case WorkerProcess.Failed(e) => Future.failed(new RuntimeException("Starting worker failed", e))
      }
    }

  }

  def default(spawn: SpawnSettings, regHub: ActorRegHub, af: ActorRefFactory): WorkerRunner = {
    val connect = (id: String, info: WorkerInitInfo, ready: FiniteDuration, remote: ActorRef, stopAction: StopAction) => {
      WorkerBridge.connect(id, info, ready, remote, stopAction)(af)
    }
    new DefaultRunner(spawn, regHub, connect)
  }

} 
Example 14
Source File: RestartSupervisor.scala    From mist   with Apache License 2.0 5 votes vote down vote up
package io.hydrosphere.mist.utils.akka

import akka.pattern.pipe
import akka.actor.{Actor, ActorLogging, ActorRef, ActorRefFactory, Props, ReceiveTimeout, SupervisorStrategy, Terminated, Timers}
import io.hydrosphere.mist.utils.Logger

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

class RestartSupervisor(
  name: String,
  start: () => Future[ActorRef],
  timeout: FiniteDuration,
  maxRetry: Int
) extends Actor with ActorLogging with Timers {

  override def receive: Receive = init

  import context._
  import RestartSupervisor._

  private def init: Receive = {
    case Event.Start(req) =>
      start().map(Event.Started) pipeTo self
      context become await(Some(req), 0)
  }

  private def await(req: Option[Promise[ActorRef]], attempts: Int): Receive = {
    case Event.Started(ref) =>
      req.foreach(_.success(self))
      context watch ref
      context become proxy(ref)

    case akka.actor.Status.Failure(e)  if maxRetry == attempts + 1 =>
      req.foreach(_.failure(e))
      log.error(e, "Starting child for {} failed, maxRetry reached", name)
      context stop self

    case akka.actor.Status.Failure(e) =>
      log.error(e, "Starting child for {} failed", name)
      timers.startSingleTimer("timeout", Event.Timeout, timeout)
      context become restartTimeout(req, attempts)
  }

  private def proxy(ref: ActorRef): Receive = {
    case Terminated(_) =>
      log.error(s"Reference for {} was terminated. Restarting", name)
      timers.startSingleTimer("timeout", Event.Timeout, timeout)
      context become restartTimeout(None, 0)

    case x => ref.forward(x)
  }

  private def restartTimeout(req: Option[Promise[ActorRef]], attempts: Int): Receive = {
    case Event.Timeout =>
      start().map(Event.Started) pipeTo self
      context become await(req, attempts + 1)
  }
}

object RestartSupervisor {

  sealed trait Event
  object Event {
    final case class Start(req: Promise[ActorRef]) extends Event
    case object Restart extends Event
    final case class Started(ref: ActorRef) extends Event
    case object Timeout extends Event
  }


  def props(
    name: String,
    start: () => Future[ActorRef],
    timeout: FiniteDuration,
    maxRetry: Int
  ): Props = {
    Props(classOf[RestartSupervisor], name, start, timeout, maxRetry)
  }

  def wrap(
    name: String,
    start: () => Future[ActorRef],
    timeout: FiniteDuration,
    maxRetry: Int
  )(implicit af: ActorRefFactory): Future[ActorRef] = {

    val ref = af.actorOf(props(name, start, timeout, maxRetry))
    val promise = Promise[ActorRef]
    ref ! Event.Start(promise)
    promise.future
  }

  def wrap(
    name: String,
    maxRetry: Int,
    start: () => Future[ActorRef]
  )(implicit af: ActorRefFactory): Future[ActorRef] = wrap(name, start, 5 seconds, maxRetry)(af)

} 
Example 15
Source File: ActorLoader.scala    From incubator-toree   with Apache License 2.0 5 votes vote down vote up
package org.apache.toree.kernel.protocol.v5.client

import akka.actor.{ActorRefFactory, ActorSelection}


  def load(actorEnum: Enumeration#Value): ActorSelection
}

case class SimpleActorLoader(actorRefFactory: ActorRefFactory)
  extends ActorLoader
{
  private val userActorDirectory: String = "/user/%s"

  override def load(actorEnum: Enumeration#Value): ActorSelection = {
    actorRefFactory.actorSelection(
      userActorDirectory.format(actorEnum.toString)
    )
  }
} 
Example 16
Source File: ExplainApi.scala    From iep-apps   with Apache License 2.0 5 votes vote down vote up
package com.netflix.atlas.druid

import akka.actor.ActorRefFactory
import akka.http.scaladsl.model.HttpEntity
import akka.http.scaladsl.model.HttpResponse
import akka.http.scaladsl.model.MediaTypes
import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.Route
import akka.http.scaladsl.server.RouteResult
import akka.pattern.ask
import akka.util.Timeout
import com.netflix.atlas.akka.CustomDirectives._
import com.netflix.atlas.akka.WebApi
import com.netflix.atlas.druid.ExplainApi.ExplainRequest
import com.netflix.atlas.eval.graph.Grapher
import com.netflix.atlas.json.Json
import com.netflix.atlas.webapi.GraphApi.DataRequest
import com.typesafe.config.Config

import scala.concurrent.duration._

class ExplainApi(config: Config, implicit val actorRefFactory: ActorRefFactory) extends WebApi {

  private val grapher: Grapher = Grapher(config)

  private val dbRef = actorRefFactory.actorSelection("/user/db")

  private implicit val ec = actorRefFactory.dispatcher

  override def routes: Route = {
    endpointPath("explain" / "v1" / "graph") {
      get { ctx =>
        val graphCfg = grapher.toGraphConfig(ctx.request)
        dbRef
          .ask(ExplainRequest(DataRequest(graphCfg)))(Timeout(10.seconds))
          .map { response =>
            val json = Json.encode(response)
            val entity = HttpEntity(MediaTypes.`application/json`, json)
            RouteResult.Complete(HttpResponse(StatusCodes.OK, entity = entity))
          }
      }
    }
  }
}

object ExplainApi {
  case class ExplainRequest(dataRequest: DataRequest)
} 
Example 17
Source File: UpdateApi.scala    From iep-apps   with Apache License 2.0 5 votes vote down vote up
package com.netflix.iep.lwc

import akka.actor.ActorRefFactory
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.Route
import akka.http.scaladsl.server.RouteResult
import com.netflix.atlas.akka.CustomDirectives._
import com.netflix.atlas.akka.WebApi
import com.netflix.atlas.webapi.PublishApi

import scala.concurrent.Promise


class UpdateApi(implicit val actorRefFactory: ActorRefFactory) extends WebApi {

  private val publishRef = actorRefFactory.actorSelection("/user/publish")

  override def routes: Route = {
    endpointPathPrefix("database" / "v1" / "update") {
      post {
        extractRequestContext { ctx =>
          parseEntity(customJson(p => PublishApi.decodeList(p))) { datapoints =>
            val promise = Promise[RouteResult]()
            publishRef ! PublishApi.PublishRequest(datapoints, Nil, promise, ctx)
            _ => promise.future
          }
        }
      }
    }
  }
} 
Example 18
Source File: PropertiesApi.scala    From iep-apps   with Apache License 2.0 5 votes vote down vote up
package com.netflix.iep.archaius

import java.io.StringWriter
import java.util.Properties

import akka.actor.ActorRefFactory
import akka.http.scaladsl.model.HttpCharsets
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.model.HttpEntity
import akka.http.scaladsl.model.HttpRequest
import akka.http.scaladsl.model.HttpResponse
import akka.http.scaladsl.model.MediaTypes
import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.server.Route
import com.netflix.atlas.akka.CustomDirectives._
import com.netflix.atlas.akka.WebApi
import com.netflix.atlas.json.Json
import com.netflix.frigga.Names

class PropertiesApi(
  val propContext: PropertiesContext,
  implicit val actorRefFactory: ActorRefFactory
) extends WebApi {

  def routes: Route = {
    endpointPath("api" / "v1" / "property") {
      get {
        parameter("asg") { asg =>
          extractRequest { request =>
            val cluster = Names.parseName(asg).getCluster
            if (propContext.initialized) {
              val props = propContext.getClusterProps(cluster)
              complete(encode(request, props))
            } else {
              complete(HttpResponse(StatusCodes.ServiceUnavailable))
            }
          }
        }
      }
    }
  }

  private def encode(request: HttpRequest, props: List[PropertiesApi.Property]): HttpResponse = {
    val useJson = request.headers.exists(h => h.is("accept") && h.value == "application/json")
    if (useJson) {
      HttpResponse(
        StatusCodes.OK,
        entity = HttpEntity(MediaTypes.`application/json`, Json.encode(props))
      )
    } else {
      val ps = new Properties
      props.foreach { p =>
        ps.setProperty(p.key, p.value)
      }
      val writer = new StringWriter()
      ps.store(writer, s"count: ${ps.size}")
      writer.close()
      val entity =
        HttpEntity(MediaTypes.`text/plain`.toContentType(HttpCharsets.`UTF-8`), writer.toString)
      HttpResponse(StatusCodes.OK, entity = entity)
    }
  }
}

object PropertiesApi {
  case class Property(id: String, cluster: String, key: String, value: String, timestamp: Long)
} 
Example 19
Source File: TransactionsApiRoute.scala    From EncryCore   with GNU General Public License v3.0 5 votes vote down vote up
package encry.api.http.routes

import akka.actor.{ ActorRef, ActorRefFactory }
import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.server.Route
import de.heikoseeberger.akkahttpcirce.FailFastCirceSupport
import encry.settings.RESTApiSettings
import encry.view.mempool.MemoryPool.NewTransaction
import org.encryfoundation.common.modifiers.mempool.transaction.Transaction

case class TransactionsApiRoute(dataHolder: ActorRef, memoryPoolRef: ActorRef, settings: RESTApiSettings)(
  implicit val context: ActorRefFactory
) extends EncryBaseApiRoute
    with FailFastCirceSupport {

  override val route: Route = pathPrefix("transactions") {
    WebRoute.authRoute(defaultTransferTransactionR, settings)
  }

  def defaultTransferTransactionR: Route = path("send") {
    post(entity(as[Transaction]) { tx =>
      complete {
        memoryPoolRef ! NewTransaction(tx)
        StatusCodes.OK
      }
    })
  }
} 
Example 20
Source File: StdOutAppender.scala    From logging   with Apache License 2.0 5 votes vote down vote up
package com.persist.logging

import akka.actor.{ActorContext, ActorRefFactory, ActorSystem}
import com.persist.JsonOps._
import com.persist.logging.LoggingLevels.Level

import scala.concurrent.Future


  def stop(): Future[Unit] = {
    if (summary) {
      val cats = if (categories.size == 0) emptyJsonObject else JsonObject("alts" -> categories)
      val levs = if (levels.size == 0) emptyJsonObject else JsonObject("levels" -> levels)
      val knds = if (kinds.size == 0) emptyJsonObject else JsonObject("kinds" -> kinds)
      val txt = Pretty(levs ++ cats ++ knds, width = width)
      val colorTxt = if (color) {
        s"${Console.BLUE}$txt${Console.RESET}"
      } else {
        txt
      }
      println(colorTxt)
    }
    Future.successful(())
  }
} 
Example 21
Source File: KafkaAppender.scala    From logging   with Apache License 2.0 5 votes vote down vote up
package com.persist.logging.kafka

import akka.actor.{ActorContext, ActorRefFactory, ActorSystem}
import com.persist.logging.{ClassLogging, LogAppender, LogAppenderBuilder, RichMsg}
import com.persist.JsonOps._
import java.util.Properties
import com.persist.logging._
import com.persist.logging.LoggingLevels.Level

import org.apache.kafka.clients.producer._

import scala.concurrent.Future

class CB extends Callback() with ClassLogging {
  var timeoutCnt = 0
  var sawTimeouts = false

  override def onCompletion(metadata: RecordMetadata, exception: Exception): Unit = {
    exception match {
      case ex: org.apache.kafka.common.errors.`TimeoutException` =>
        if (!sawTimeouts) log.error(JsonObject("fail" -> "KAFKA", "error" -> "Kafka timing out"))
        sawTimeouts = true
        timeoutCnt += 1
      case _ =>
        if (sawTimeouts) log.error(JsonObject("fail" -> "KAFKA", "error" -> "messages lost", "lost" -> timeoutCnt))
        sawTimeouts = false
        timeoutCnt = 0
    }
  }

  def finish(blockMs: Int) {
    Thread.sleep(blockMs * 3) // ait for all messages to complete
    if (timeoutCnt > 0) log.error(JsonObject("fail" -> "KAFKA", "error" -> "messages lost", "lost" -> timeoutCnt))
  }
}

object KafkaAppender extends LogAppenderBuilder {
  override def apply(factory: ActorRefFactory, standardHeaders: Map[String, RichMsg]): LogAppender =
    new KafkaAppender(factory, standardHeaders)
}

class KafkaAppender(factory: ActorRefFactory, standardHeaders: Map[String, RichMsg]) extends LogAppender {
  val cb = new CB()
  private[this] val system = factory match {
    case context: ActorContext => context.system
    case s: ActorSystem => s
  }
  private[this] implicit val executionContext = factory.dispatcher
  private[this] val config = system.settings.config.getConfig("com.persist.logging.appenders.kafka")
  private[this] val fullHeaders = config.getBoolean("fullHeaders")
  private[this] val sort = config.getBoolean("sorted")
  private[this] val logLevelLimit = Level(config.getString("logLevelLimit"))

  private[this] val topic = config.getString("topic")
  private[this] val bootstrapServers = config.getString("bootstrapServers")
  private[this] val acks = config.getString("acks")
  private[this] val retries = config.getInt("retries")
  private[this] val batchSize = config.getInt("batchSize")
  private[this] val blockMs = config.getInt("blockMs")
  private[this] val bufferMemory = config.getInt("bufferMemory")


  val props = new Properties()
  props.put("bootstrap.servers", bootstrapServers)
  props.put("acks", acks)
  props.put("retries", new Integer(retries))
  props.put("batch.size", new Integer(batchSize))
  //props.put("request.timeout.ms", new Integer(timeoutMs))
  props.put("max.block.ms", new Integer(blockMs))
  //props.put("linger.ms", new Integer(lingerMs))
  props.put("buffer.memory", new Integer(bufferMemory))
  props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer")
  props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer")

  private[this] val producer: Producer[String, String] = new KafkaProducer(props)

  private def checkLevel(baseMsg: Map[String, RichMsg]): Boolean = {
    val level = jgetString(baseMsg, "@severity")
    Level(level) >= logLevelLimit
  }

  override def append(baseMsg: Map[String, RichMsg], category: String): Unit = {
    if (category != "common" || checkLevel(baseMsg)) {
      val msg = if (fullHeaders) standardHeaders ++ baseMsg else baseMsg
      val txt = Compact(msg, safe = true, sort = sort)
      Future {
        producer.send(new ProducerRecord[String, String](topic, txt), cb)
      }
    }
  }

  override def finish(): Future[Unit] = {
    cb.finish(blockMs)
    Future.successful(())
  }

  override def stop(): Future[Unit] = {
    producer.close()
    Future.successful(())
  }
} 
Example 22
Source File: SidechainNodeViewSynchronizer.scala    From Sidechains-SDK   with MIT License 5 votes vote down vote up
package com.horizen.network

import akka.actor.{ActorRef, ActorRefFactory, Props}
import com.horizen._
import com.horizen.block.SidechainBlock
import com.horizen.validation.{BlockInFutureException, InconsistentDataException}
import scorex.core.network.NodeViewSynchronizer
import scorex.core.network.NodeViewSynchronizer.ReceivableMessages.SyntacticallyFailedModification
import scorex.core.serialization.ScorexSerializer
import scorex.core.settings.NetworkSettings
import scorex.core.utils.NetworkTimeProvider
import scorex.core.{ModifierTypeId, NodeViewModifier}

import scala.concurrent.ExecutionContext

class SidechainNodeViewSynchronizer(networkControllerRef: ActorRef,
                                    viewHolderRef: ActorRef,
                                    syncInfoSpec: SidechainSyncInfoMessageSpec.type,
                                    networkSettings: NetworkSettings,
                                    timeProvider: NetworkTimeProvider,
                                    modifierSerializers: Map[ModifierTypeId, ScorexSerializer[_ <: NodeViewModifier]])(implicit ec: ExecutionContext)
  extends NodeViewSynchronizer[SidechainTypes#SCBT, SidechainSyncInfo, SidechainSyncInfoMessageSpec.type,
    SidechainBlock, SidechainHistory, SidechainMemoryPool](networkControllerRef, viewHolderRef, syncInfoSpec, networkSettings, timeProvider, modifierSerializers){

  override protected val deliveryTracker = new SidechainDeliveryTracker(context.system, deliveryTimeout, maxDeliveryChecks, self)

  private val onSyntacticallyFailedModifier: Receive = {
    case SyntacticallyFailedModification(mod, exception) =>
      exception match {
        case _: BlockInFutureException =>
          // When next time NodeViewSynchronizer.processInv will be emitted for mod.id it will be processed again.
          // So no ban for mod.id
          deliveryTracker.setUnknown(mod.id)
        case _: InconsistentDataException =>
          // Try to ban the sender only (in case of modifier from remote)
          val peerOpt = deliveryTracker.peerInfo(mod.id)
          deliveryTracker.setUnknown(mod.id)
          peerOpt.foreach(penalizeMisbehavingPeer)
        case _ => // InvalidBlockException, InvalidSidechainBlockHeaderException and all other exceptions
          // Ban both mod.id and peer
          deliveryTracker.setInvalid(mod.id).foreach(penalizeMisbehavingPeer)
      }
  }

  override protected def viewHolderEvents: Receive = onSyntacticallyFailedModifier orElse super.viewHolderEvents
}



object SidechainNodeViewSynchronizer {
  def props(networkControllerRef: ActorRef,
            viewHolderRef: ActorRef,
            syncInfoSpec: SidechainSyncInfoMessageSpec.type,
            networkSettings: NetworkSettings,
            timeProvider: NetworkTimeProvider,
            modifierSerializers: Map[ModifierTypeId, ScorexSerializer[_ <: NodeViewModifier]])
           (implicit ex: ExecutionContext): Props =
    Props(new SidechainNodeViewSynchronizer(networkControllerRef, viewHolderRef, syncInfoSpec, networkSettings,
      timeProvider, modifierSerializers))

  def apply(networkControllerRef: ActorRef,
            viewHolderRef: ActorRef,
            syncInfoSpec: SidechainSyncInfoMessageSpec.type,
            networkSettings: NetworkSettings,
            timeProvider: NetworkTimeProvider,
            modifierSerializers: Map[ModifierTypeId, ScorexSerializer[_ <: NodeViewModifier]])
           (implicit context: ActorRefFactory, ex: ExecutionContext): ActorRef =
    context.actorOf(props(networkControllerRef, viewHolderRef, syncInfoSpec, networkSettings, timeProvider, modifierSerializers))

  def apply(networkControllerRef: ActorRef,
            viewHolderRef: ActorRef,
            syncInfoSpec: SidechainSyncInfoMessageSpec.type,
            networkSettings: NetworkSettings,
            timeProvider: NetworkTimeProvider,
            modifierSerializers: Map[ModifierTypeId, ScorexSerializer[_ <: NodeViewModifier]],
            name: String)
           (implicit context: ActorRefFactory, ex: ExecutionContext): ActorRef =
    context.actorOf(props(networkControllerRef, viewHolderRef, syncInfoSpec, networkSettings, timeProvider, modifierSerializers), name)
} 
Example 23
Source File: SidechainRejectionApiRoute.scala    From Sidechains-SDK   with MIT License 5 votes vote down vote up
package com.horizen.api.http

import akka.actor.{ActorRef, ActorRefFactory}
import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.server.Route
import scorex.core.settings.RESTApiSettings

import scala.concurrent.ExecutionContext


case class SidechainRejectionApiRoute(basePath: String, path: String,
                                      override val settings: RESTApiSettings, sidechainNodeViewHolderRef: ActorRef)
                                     (implicit val context: ActorRefFactory, override val ec: ExecutionContext)
  extends SidechainApiRoute {

  override def route: Route =
    if (path.isEmpty)
      (pathPrefix(basePath)) {
        SidechainApiError(StatusCodes.NotFound, "NotFound").complete("The requested resource could not be found.")
      }
    else (pathPrefix(basePath) & path(path)) {
      SidechainApiError(StatusCodes.NotFound, "NotFound").complete("The requested resource could not be found.")
    }

} 
Example 24
Source File: ApplicationApiRoute.scala    From Sidechains-SDK   with MIT License 5 votes vote down vote up
package com.horizen.api.http

import akka.actor.{ActorRef, ActorRefFactory}
import akka.pattern.ask
import akka.http.scaladsl.server.Route
import akka.http.scaladsl.server.directives.RouteDirectives
import com.horizen.SidechainNodeViewHolder.ReceivableMessages.GetDataFromCurrentSidechainNodeView
import com.horizen.node.SidechainNodeView
import scorex.core.settings.RESTApiSettings
import scorex.core.utils.ScorexEncoding

import scala.concurrent.{Await, ExecutionContext}
import scala.collection.JavaConverters._
import scala.util.{Failure, Success, Try}

case class ApplicationApiRoute(override val settings: RESTApiSettings, sidechainNodeViewHolderRef: ActorRef, applicationApiGroup: ApplicationApiGroup)
                              (implicit val context: ActorRefFactory, override val ec: ExecutionContext)
  extends SidechainApiRoute
    with ScorexEncoding {


  override def route: Route = convertRoutes

  private def convertRoutes: Route = {

    applicationApiGroup.setApplicationNodeViewProvider(new ApplicationNodeViewProvider {

      override def getSidechainNodeView: Try[SidechainNodeView] = retrieveSidechainNodeView()

    })

    var listOfAppApis: List[Route] = applicationApiGroup.getRoutes.asScala.toList.map(r => r.asScala)

    pathPrefix(applicationApiGroup.basePath()) {
      listOfAppApis.reduceOption(_ ~ _).getOrElse(RouteDirectives.reject)
    }
  }

  private def retrieveSidechainNodeView(): Try[SidechainNodeView] = {
    def f(v: SidechainNodeView) = v

    def fut = (sidechainNodeViewHolderRef ? GetDataFromCurrentSidechainNodeView(f))
      .mapTo[SidechainNodeView]

    try {
      var result = Await.result(fut, settings.timeout)
      Success(result)
    } catch {
      case e: Throwable => Failure(e)
    }
  }

} 
Example 25
Source File: HistoryApiRoute.scala    From EncryCore   with GNU General Public License v3.0 5 votes vote down vote up
package encry.api.http.routes

import akka.actor.{ ActorRef, ActorRefFactory }
import akka.http.scaladsl.server.Route
import akka.pattern.ask
import encry.api.http.DataHolderForApi.{
  GetDataFromHistory,
  GetFullBlockByIdCommand,
  GetLastHeaderIdAtHeightHelper,
  GetLastHeadersHelper,
  GetMinerStatus
}
import encry.local.miner.Miner.MinerStatus
import encry.settings.RESTApiSettings
import encry.view.history.History
import io.circe.syntax._
import org.encryfoundation.common.modifiers.history.{ Block, Header }
import org.encryfoundation.common.utils.Algos

case class HistoryApiRoute(dataHolder: ActorRef, settings: RESTApiSettings, nodeId: Array[Byte])(
  implicit val context: ActorRefFactory
) extends EncryBaseApiRoute {

  override val route: Route = pathPrefix("history") {
    getHeaderIdsR ~
      getLastHeadersR ~
      getHeadersIdsAtHeightR ~
      getBlockHeaderByHeaderIdR ~
      getBlockTransactionsByHeaderIdR ~
      getFullBlockByHeaderIdR ~
      candidateBlockR
  }

  def getHeaderIdsR: Route = (pathEndOrSingleSlash & get & paging) { (offset, limit) =>
    (dataHolder ? GetDataFromHistory).mapTo[History]
      .map {
      _.getHeaderIds(offset, limit).map(Algos.encode).asJson
    }.okJson()
  }

  def getLastHeadersR: Route = (pathPrefix("lastHeaders" / IntNumber) & get) { qty =>
    (dataHolder ? GetLastHeadersHelper(qty)).mapTo[IndexedSeq[Header]].map(_.asJson).okJson()
  }

  def getHeadersIdsAtHeightR: Route = (pathPrefix("at" / IntNumber) & get) { height =>
    (dataHolder ? GetLastHeaderIdAtHeightHelper(height))
      .mapTo[Seq[String]]
      .map(_.asJson).okJson()
  }

  def getBlockHeaderByHeaderIdR: Route = (modifierId & pathPrefix("header") & get) { id =>
    (dataHolder ? GetFullBlockByIdCommand(Right(id))).mapTo[Option[Block]].map(_.map(x => x.header.asJson)).okJson()
  }

  def getBlockTransactionsByHeaderIdR: Route = (modifierId & pathPrefix("transactions") & get) { id =>
    (dataHolder ? GetFullBlockByIdCommand(Right(id))).mapTo[Option[Block]].map(_.map(_.payload.txs.asJson)).okJson()
  }

  def candidateBlockR: Route = (path("candidateBlock") & pathEndOrSingleSlash & get) {
    (dataHolder ? GetMinerStatus).mapTo[MinerStatus].map(_.json).okJson()
  }

  def getFullBlockByHeaderIdR: Route = (modifierId & get) { id =>
    (dataHolder ? GetFullBlockByIdCommand(Right(id))).mapTo[Option[Block]].map(_.asJson).okJson()
  }
} 
Example 26
Source File: NodeRoute.scala    From EncryCore   with GNU General Public License v3.0 5 votes vote down vote up
package encry.api.http.routes

import akka.actor.{ActorRef, ActorRefFactory}
import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.server.Route
import encry.api.http.DataHolderForApi._
import encry.settings.RESTApiSettings

case class NodeRoute(dataHolder: ActorRef, settings: RESTApiSettings)(implicit val context: ActorRefFactory)
    extends EncryBaseApiRoute {

  override def route: Route = pathPrefix("node") {
    WebRoute.authRoute(nodeStartMiningR ~ nodeStopMiningR ~ nodeShutdownR, settings)
  }

  def nodeStartMiningR: Route = (path("startMining") & get) {
    dataHolder ! StartMinerApiMessage
    withCors(complete(StatusCodes.OK))
  }

  def nodeStopMiningR: Route = (path("stopMining") & get) {
    dataHolder ! StopMinerApiMessage
    withCors(complete(StatusCodes.OK))
  }

  def nodeShutdownR: Route = (path("shutdown") & get) {
    dataHolder ! ShutdownNode
    withCors(complete(StatusCodes.OK))
  }

} 
Example 27
Source File: InfoApiRoute.scala    From EncryCore   with GNU General Public License v3.0 5 votes vote down vote up
package encry.api.http.routes

import java.net.InetSocketAddress
import akka.actor.{ ActorRef, ActorRefFactory }
import akka.http.scaladsl.server.Route
import akka.pattern.ask
import encry.api.http.DataHolderForApi._
import encry.local.miner.Miner.MinerStatus
import encry.settings._
import encry.utils.NetworkTimeProvider
import io.circe.Json
import io.circe.syntax._
import io.circe.generic.auto._
import org.encryfoundation.common.modifiers.history.{ Block, Header }
import org.encryfoundation.common.utils.Algos
import org.encryfoundation.common.utils.constants.Constants

case class InfoApiRoute(dataHolder: ActorRef,
                        settings: RESTApiSettings,
                        nodeId: Array[Byte],
                        timeProvider: NetworkTimeProvider)(implicit val context: ActorRefFactory)
    extends EncryBaseApiRoute {

  override val route: Route = (path("info") & get) {
    (dataHolder ? GetAllInfoHelper)
      .mapTo[Json]
      .okJson()
  }
}

object InfoApiRoute {

  def makeInfoJson(nodeId: Array[Byte],
                   minerInfo: MinerStatus,
                   connectedPeersLength: Int,
                   readers: Readers,
                   stateType: String,
                   nodeName: String,
                   knownPeers: Seq[InetSocketAddress],
                   storage: String,
                   nodeUptime: Long,
                   mempoolSize: Int,
                   connectWithOnlyKnownPeer: Boolean,
                   header: Option[Header],
                   block: Option[Block],
                   constants: Constants
                  ): Json = {
    val stateVersion: Option[String] = readers.s.map(_.version).map(Algos.encode)
    val stateRoot: Option[String] = readers.s.map(_.tree.rootHash).map(Algos.encode)
    val prevFullHeaderId: String = block.map(b => Algos.encode(b.header.parentId)).getOrElse("")
    InfoApi(
      nodeName,
      stateType,
      block.map(_.header.difficulty.toString).getOrElse(constants.InitialDifficulty.toString),
      block.map(_.encodedId).getOrElse(""),
      header.map(_.encodedId).getOrElse(""),
      connectedPeersLength,
      mempoolSize,
      prevFullHeaderId,
      block.map(_.header.height).getOrElse(0),
      header.map(_.height).getOrElse(0),
      stateVersion.getOrElse(""),
      nodeUptime,
      storage,
      connectWithOnlyKnownPeer,
      minerInfo.isMining,
      knownPeers.map { x =>
        x.getHostName + ":" + x.getPort
      },
      stateRoot.getOrElse("")
    ).asJson
  }
} 
Example 28
Source File: Appender.scala    From logging   with Apache License 2.0 5 votes vote down vote up
package demo.test

import java.net.InetAddress
import akka.actor.{ActorRefFactory, ActorSystem}
import com.persist.logging._
import logging_demo.BuildInfo
import scala.concurrent.Await
import scala.concurrent.duration._
import scala.language.postfixOps
import scala.concurrent.Future

case class AppenderClass() extends ClassLogging {

  def demo(): Unit = {
    log.info("Test")
    log.error("Foo failed")
    log.warn(Map("@msg" -> "fail", "value" -> 23))
  }
}

object FlatAppender extends LogAppenderBuilder {
  def apply(factory: ActorRefFactory, stdHeaders: Map[String, RichMsg])
  = new FlatAppender(factory, stdHeaders)
}

class FlatAppender(factory: ActorRefFactory, stdHeaders: Map[String, RichMsg]) extends LogAppender {

  def append(msg: Map[String, RichMsg], category: String) {
    if (category == "common") {
      val level = msg.get("@severity") match {
        case Some(s: String) => s
        case _ => "???"
      }
      val time = msg.get("@timestamp") match {
        case Some(s: String) => s
        case _ => "???"
      }
      val message = richToString(msg.getOrElse("msg","???"))
      println(s"$time\t$level\t$message")
    }
  }

  def finish(): Future[Unit] = Future.successful(())

  def stop(): Future[Unit] = Future.successful(())
}

object Appender {
  def main(args: Array[String]) {
    val system = ActorSystem("test")

    val host = InetAddress.getLocalHost.getHostName
    val loggingSystem = LoggingSystem(system, BuildInfo.name,
      BuildInfo.version, host, appenderBuilders = Seq(FileAppender, FlatAppender))

    val sc = new SimpleClass()
    sc.demo()

    Await.result(loggingSystem.stop, 30 seconds)
    Await.result(system.terminate(), 20 seconds)
  }
} 
Example 29
Source File: PeersApiRoute.scala    From EncryCore   with GNU General Public License v3.0 5 votes vote down vote up
package encry.api.http.routes

import java.net.{InetAddress, InetSocketAddress}
import akka.actor.{ActorRef, ActorRefFactory}
import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.server.Route
import akka.pattern.ask
import encry.api.http.DataHolderForApi._
import encry.api.http.routes.PeersApiRoute.PeerInfoResponse
import encry.network.BlackList.{BanReason, BanTime, BanType}
import encry.network.ConnectedPeersCollection.PeerInfo
import encry.settings.RESTApiSettings
import io.circe.Encoder
import io.circe.generic.semiauto._
import io.circe.syntax._
import scala.util.{Failure, Success, Try}

case class PeersApiRoute(override val settings: RESTApiSettings, dataHolder: ActorRef)(
  implicit val context: ActorRefFactory
) extends EncryBaseApiRoute {

  override lazy val route: Route = pathPrefix("peers") {
    connectedPeers ~ allPeers ~ bannedList ~ WebRoute.authRoute(connectPeer ~ removeFromBan, settings)
  }

  def allPeers: Route = (path("all") & get) {
    (dataHolder ? GetAllPeers)
      .mapTo[Seq[InetSocketAddress]]
      .map(_.map(_.toString).asJson).okJson()
  }

  def connectedPeers: Route = (path("connected") & get) {
    (dataHolder ? GetConnectedPeersHelper)
      .mapTo[Seq[PeerInfoResponse]].map(_.asJson).okJson()
  }

  def bannedList: Route = (path("banned") & get) {
    (dataHolder ? GetBannedPeersHelper).mapTo[Seq[(InetAddress, (BanReason, BanTime, BanType))]]
    .map(_.map(_.toString).asJson).okJson()
  }

  def connectPeer: Route = path("add") {
    post(entity(as[String]) { str =>
      complete {
        Try {
          val split = str.split(':')
          (split(0), split(1).toInt)
        } match {
          case Success((host, port)) =>
            dataHolder ! UserAddPeer(new InetSocketAddress(host, port))
            StatusCodes.OK
          case Failure(_) =>
            StatusCodes.BadRequest
        }
      }
    })
  }

  def removeFromBan: Route = path("remove") {
    post(entity(as[String]) { str =>
      complete {
        Try {
          val split = str.split(':')
          (split(0), split(1).toInt)
        } match {
          case Success((host, port)) =>
            dataHolder ! RemovePeerFromBanList(new InetSocketAddress(host, port))
            StatusCodes.OK
          case Failure(_) =>
            StatusCodes.BadRequest
        }
      }
    })
  }
}

object PeersApiRoute {

  case class PeerInfoResponse(address: String, name: Option[String], connectionType: Option[String])

  object PeerInfoResponse {

    def fromAddressAndInfo(address: InetSocketAddress, peerInfo: PeerInfo): PeerInfoResponse = PeerInfoResponse(
      address.toString,
      Some(peerInfo.connectedPeer.toString),
      Some(peerInfo.connectionType.toString)
    )
  }

  implicit val encodePeerInfoResponse: Encoder[PeerInfoResponse] = deriveEncoder
} 
Example 30
Source File: TransportRegistrar.scala    From hydra   with Apache License 2.0 5 votes vote down vote up
package hydra.ingest.services

import java.lang.reflect.Method

import akka.actor.{Actor, ActorRef, ActorRefFactory, Props}
import com.typesafe.config.Config
import hydra.common.config.ConfigSupport
import hydra.common.logging.LoggingAdapter
import hydra.common.reflect.ReflectionUtils
import hydra.common.util.ActorUtils
import hydra.core.transport.Transport
import hydra.ingest.bootstrap.ClasspathHydraComponentLoader
import hydra.ingest.services.TransportRegistrar.{
  GetTransports,
  GetTransportsResponse
}

import scala.util.Try


  private def companion[T](clazz: Class[T]): Option[(T, Method)] = {
    try {
      val companion = ReflectionUtils.companionOf(clazz)
      companion.getClass.getMethods.toList.filter(m =>
        m.getName == "props"
          && m.getParameterTypes.toList == List(classOf[Config])
      ) match {
        case Nil           => None
        case method :: Nil => Some(companion, method)
        case _             => None
      }
    } catch {
      case _: Throwable => None
    }
  }
} 
Example 31
Source File: IngestSocketFactory.scala    From hydra   with Apache License 2.0 5 votes vote down vote up
package hydra.ingest.services

import akka.NotUsed
import akka.actor.{ActorRef, ActorRefFactory, Props}
import akka.stream.OverflowStrategy
import akka.stream.scaladsl.{Flow, Sink, Source}
import hydra.core.ingest.IngestionReport

trait IngestSocketFactory {
  def ingestFlow(): Flow[String, OutgoingMessage, NotUsed]
}

object IngestSocketFactory {

  def createSocket(fact: ActorRefFactory): IngestSocketFactory = { () =>
    {

      val socketActor = fact.actorOf(Props[IngestionSocketActor])

      def actorSink =
        Sink.actorRefWithBackpressure(
          socketActor,
          onInitMessage = SocketInit,
          ackMessage = SocketAck,
          onCompleteMessage = SocketEnded,
          onFailureMessage = SocketFailed.apply
        )

      val in =
        Flow[String]
          .map(IncomingMessage)
          .to(actorSink)

      val out =
        Source
          .actorRefWithBackpressure[OutgoingMessage](
            SocketAck,
            PartialFunction.empty,
            PartialFunction.empty
          )
          .mapMaterializedValue(socketActor ! SocketStarted(_))

      Flow.fromSinkAndSourceCoupled(in, out)

    }
  }
}

sealed trait SocketEvent

case object SocketInit extends SocketEvent

case class SocketStarted(ref: ActorRef) extends SocketEvent

case object SocketEnded extends SocketEvent

case object SocketAck extends SocketEvent

case class IncomingMessage(message: String) extends SocketEvent

case class SocketFailed(ex: Throwable)

sealed trait OutgoingMessage extends SocketEvent

case class SimpleOutgoingMessage(status: Int, message: String)
    extends OutgoingMessage

case class IngestionOutgoingMessage(report: IngestionReport)
    extends OutgoingMessage 
Example 32
Source File: ConnectionStrategy.scala    From reactive-consul   with MIT License 5 votes vote down vote up
package stormlantern.consul.client.discovery

import akka.actor.{ ActorRef, ActorRefFactory }
import stormlantern.consul.client.loadbalancers.{ LoadBalancer, LoadBalancerActor, RoundRobinLoadBalancer }

case class ServiceDefinition(key: String, serviceName: String, serviceTags: Set[String] = Set.empty, dataCenter: Option[String] = None)
object ServiceDefinition {

  def apply(serviceName: String): ServiceDefinition = {
    ServiceDefinition(serviceName, serviceName)
  }

  def apply(serviceName: String, serviceTags: Set[String]): ServiceDefinition = {
    ServiceDefinition(serviceName, serviceName, serviceTags)
  }

}

case class ConnectionStrategy(
  serviceDefinition: ServiceDefinition,
  connectionProviderFactory: ConnectionProviderFactory,
  loadBalancerFactory: ActorRefFactory ⇒ ActorRef
)

object ConnectionStrategy {

  def apply(serviceDefinition: ServiceDefinition, connectionProviderFactory: ConnectionProviderFactory, loadBalancer: LoadBalancer): ConnectionStrategy =
    ConnectionStrategy(serviceDefinition, connectionProviderFactory, ctx ⇒ ctx.actorOf(LoadBalancerActor.props(loadBalancer, serviceDefinition.key)))

  def apply(serviceDefinition: ServiceDefinition, connectionProviderFactory: (String, Int) ⇒ ConnectionProvider, loadBalancer: LoadBalancer): ConnectionStrategy = {
    val cpf = new ConnectionProviderFactory {
      override def create(host: String, port: Int): ConnectionProvider = connectionProviderFactory(host, port)
    }
    ConnectionStrategy(serviceDefinition, cpf, ctx ⇒ ctx.actorOf(LoadBalancerActor.props(loadBalancer, serviceDefinition.key)))
  }

  def apply(serviceName: String, connectionProviderFactory: (String, Int) ⇒ ConnectionProvider, loadBalancer: LoadBalancer): ConnectionStrategy = {
    ConnectionStrategy(ServiceDefinition(serviceName), connectionProviderFactory, loadBalancer)
  }

  def apply(serviceName: String, connectionProviderFactory: (String, Int) ⇒ ConnectionProvider): ConnectionStrategy = {
    ConnectionStrategy(serviceName, connectionProviderFactory, new RoundRobinLoadBalancer)
  }

} 
Example 33
Source File: TransactionActor.scala    From changestream   with MIT License 5 votes vote down vote up
package changestream.actors

import java.util.UUID

import akka.actor.{ Actor, ActorRef, ActorRefFactory }
import changestream.events.MutationWithInfo

import changestream.events._
import kamon.Kamon
import org.slf4j.LoggerFactory

class TransactionActor(getNextHop: ActorRefFactory => ActorRef) extends Actor {
  protected val log = LoggerFactory.getLogger(getClass)
  protected val batchSizeMetric = Kamon.histogram("changestream.binlog_event.row_count")
  protected val transactionSizeMetric = Kamon.histogram("changestream.transaction.row_count")

  protected val nextHop = getNextHop(context)

  
  protected var mutationCount: Long = 1
  protected var currentGtid: Option[String] = None
  protected var previousMutation: Option[MutationWithInfo] = None

  def receive = {
    case BeginTransaction =>
      log.debug("Received BeginTransacton")
      mutationCount = 1
      currentGtid = Some(UUID.randomUUID.toString)
      previousMutation = None

    case Gtid(guid) =>
      log.debug("Received GTID for transaction: {}", guid)
      currentGtid = Some(guid)

    case event: MutationWithInfo =>
      log.debug("Received Mutation for tableId: {}", event.mutation.tableId)
      batchSizeMetric.record(event.mutation.rows.length)

      currentGtid match {
        case None =>
          nextHop ! event
        case Some(gtid) =>
          previousMutation.foreach { mutation =>
            log.debug("Adding transaction info and forwarding to the {} actor.", nextHop.path.name)
            nextHop ! mutation
          }
          previousMutation = Some(event.copy(
            transaction = Some(TransactionInfo(
              gtid = gtid,
              currentRow = mutationCount
            ))
          ))
          mutationCount += event.mutation.rows.length
      }

    case CommitTransaction(position) =>
      log.debug("Received Commit with position {}", position)
      previousMutation.foreach { mutation =>
        log.debug("Adding transaction info and forwarding to the {} actor.", nextHop.path.name)
        nextHop ! mutation.copy(
          transaction = mutation.transaction.map { txInfo =>
            txInfo.copy(lastMutationInTransaction = true)
          },
          // TODO: this is unfortunate... because we are now essentially saving the "last safe position" we are guaranteed to replay events when we shut down un-gracefully
          nextPosition = mutation.nextPosition.split(":")(0) + ":" + position.toString
        )
      }
      transactionSizeMetric.record(mutationCount)
      mutationCount = 1
      currentGtid = None
      previousMutation = None

    case RollbackTransaction =>
      log.debug("Received Rollback")

      // TODO: this probably doesn't work for mysql configurations that send a rollback event (vs only sending committed events).. consider removing the rollback handling
      previousMutation.foreach { mutation =>
        log.debug("Adding transaction info and forwarding to the {} actor.", nextHop.path.name)
        nextHop ! mutation.copy(
          transaction = mutation.transaction.map { txInfo =>
            txInfo.copy(lastMutationInTransaction = true)
          }
        )
      }
      transactionSizeMetric.record(mutationCount)
      mutationCount = 1
      currentGtid = None
      previousMutation = None
  }
} 
Example 34
Source File: StdoutActor.scala    From changestream   with MIT License 5 votes vote down vote up
package changestream.actors

import akka.actor.{Actor, ActorRef, ActorRefFactory}
import changestream.actors.PositionSaver.EmitterResult
import changestream.events.MutationWithInfo
import com.typesafe.config.{Config, ConfigFactory}
import kamon.Kamon

class StdoutActor(getNextHop: ActorRefFactory => ActorRef,
                  config: Config = ConfigFactory.load().getConfig("changestream")) extends Actor {
  protected val nextHop = getNextHop(context)
  protected val counterMetric = Kamon.counter("changestream.emitter.total").refine("emitter" -> "stdout", "result" -> "success")

  def receive = {
    case MutationWithInfo(_, pos, _, _, Some(message: String)) =>
      println(message)
      counterMetric.increment()
      nextHop ! EmitterResult(pos)
  }
} 
Example 35
Source File: BenchBase.scala    From changestream   with MIT License 5 votes vote down vote up
package changestream.helpers

import akka.actor.{ActorRefFactory, ActorSystem, Props}
import akka.testkit.{TestActorRef, TestProbe}
import com.github.mauricio.async.db.Configuration
import com.github.mauricio.async.db.mysql.MySQLConnection
import com.typesafe.config.ConfigFactory
import org.scalameter.api._
import org.scalameter.picklers.Implicits._

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

class BenchBase extends Bench[Double] {
  lazy val executor = LocalExecutor(
    new Executor.Warmer.Default,
    Aggregator.min[Double],
    measurer)
  lazy val measurer = new Measurer.Default
  lazy val reporter = new LoggingReporter[Double]
  lazy val persistor = Persistor.None

  implicit val system = ActorSystem("changestream", ConfigFactory.load("test.conf"))
  implicit val ec = system.dispatcher
  val probe = TestProbe()
  val maker = (_: ActorRefFactory) => probe.ref

  val testConfig = ConfigFactory.load("test.conf")

  def getProbedActorOf[K](klass: Predef.Class[K], configPath: String = "changestream") =
    TestActorRef(Props(klass, maker, testConfig.getConfig(configPath)))

  protected val config = testConfig.getConfig("changestream.mysql")
  protected val mysqlConfig = new Configuration(
    config.getString("user"),
    config.getString("host"),
    config.getInt("port"),
    Some(config.getString("password"))
  )

  protected val connectionTimeout = config.getLong("timeout")
  protected val connection = new MySQLConnection(mysqlConfig)
  Await.result(connection.connect, connectionTimeout milliseconds)
  val result = connection.sendQuery("drop database if exists changestream_test")
    .flatMap(_ => connection.sendQuery("create database changestream_test"))
    .flatMap(_ => connection.sendQuery(s"""
                                          | CREATE TABLE changestream_test.users (
                                          |   `id` int(11) NOT NULL AUTO_INCREMENT,
                                          |   `username` varchar(32) DEFAULT NULL,
                                          |   `password` varchar(32) DEFAULT NULL,
                                          |   `login_count` int(11) NOT NULL DEFAULT '0',
                                          |   `bio` text DEFAULT NULL,
                                          |   PRIMARY KEY (`id`)
                                          | ) ENGINE=InnoDB
        """.stripMargin))
  Await.result(result, (connectionTimeout * 3) milliseconds)
} 
Example 36
Source File: StdoutActorSpec.scala    From changestream   with MIT License 5 votes vote down vote up
package changestream.actors

import akka.actor.{ActorRefFactory, Props}
import akka.testkit.{TestActorRef, TestProbe}
import changestream.actors.PositionSaver.EmitterResult
import changestream.helpers.{Config, Emitter}

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

class StdoutActorSpec extends Emitter with Config {
  val probe = TestProbe()
  val maker = (_: ActorRefFactory) => probe.ref
  val actorRef = TestActorRef(Props(classOf[StdoutActor], maker, awsConfig))

  "When StdoutActor receives a single valid message" should {
    "Print to stdout and forward result" in {
      actorRef ! message

      val result = probe.expectMsgType[EmitterResult](5000 milliseconds)
      result.position should be(message.nextPosition)
    }
  }
}