akka.actor.SupervisorStrategy Scala Examples
The following examples show how to use akka.actor.SupervisorStrategy.
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: RareBooks.scala From reactive-application-development-scala with Apache License 2.0 | 5 votes |
package com.rarebooks.library import akka.actor.{ Actor, ActorLogging, OneForOneStrategy, Props, Stash, SupervisorStrategy } import akka.routing.{ ActorRefRoutee, Router, RoundRobinRoutingLogic } import scala.concurrent.duration.{ MILLISECONDS => Millis, FiniteDuration, Duration } object RareBooks { case object Close case object Open case object Report def props: Props = Props(new RareBooks) } class RareBooks extends Actor with ActorLogging with Stash { import context.dispatcher import RareBooks._ import RareBooksProtocol._ override val supervisorStrategy: SupervisorStrategy = { val decider: SupervisorStrategy.Decider = { case Librarian.ComplainException(complain, customer) => customer ! Credit() log.info(s"RareBooks sent customer $customer a credit") SupervisorStrategy.Restart } OneForOneStrategy()(decider orElse super.supervisorStrategy.decider) } private val openDuration: FiniteDuration = Duration(context.system.settings.config.getDuration("rare-books.open-duration", Millis), Millis) private val closeDuration: FiniteDuration = Duration(context.system.settings.config.getDuration("rare-books.close-duration", Millis), Millis) private val nbrOfLibrarians: Int = context.system.settings.config getInt "rare-books.nbr-of-librarians" private val findBookDuration: FiniteDuration = Duration(context.system.settings.config.getDuration("rare-books.librarian.find-book-duration", Millis), Millis) private val maxComplainCount: Int = context.system.settings.config getInt "rare-books.librarian.max-complain-count" var requestsToday: Int = 0 var totalRequests: Int = 0 var router: Router = createLibrarian() context.system.scheduler.scheduleOnce(openDuration, self, Close) protected def createLibrarian(): Router = { var cnt: Int = 0 val routees: Vector[ActorRefRoutee] = Vector.fill(nbrOfLibrarians) { val r = context.actorOf(Librarian.props(findBookDuration, maxComplainCount), s"librarian-$cnt") cnt += 1 ActorRefRoutee(r) } Router(RoundRobinRoutingLogic(), routees) } }
Example 2
Source File: ConsumerRecovery.scala From scala-kafka-client with MIT License | 5 votes |
package cakesolutions.kafka.examples import akka.actor.{Actor, ActorLogging, ActorRef, ActorSystem, OneForOneStrategy, Props, SupervisorStrategy} import cakesolutions.kafka.KafkaConsumer import cakesolutions.kafka.akka.KafkaConsumerActor.{Confirm, Subscribe} import cakesolutions.kafka.akka.{ConsumerRecords, Extractor, KafkaConsumerActor} import com.typesafe.config.{Config, ConfigFactory} import org.apache.kafka.clients.consumer.OffsetResetStrategy import org.apache.kafka.common.serialization.StringDeserializer import scala.concurrent.duration._ override def supervisorStrategy: SupervisorStrategy = OneForOneStrategy(maxNrOfRetries = 10) { case _: KafkaConsumerActor.ConsumerException => log.info("Consumer exception caught. Restarting consumer.") SupervisorStrategy.Restart case _ => SupervisorStrategy.Escalate } val recordsExt: Extractor[Any, ConsumerRecords[String, String]] = ConsumerRecords.extractor[String, String] val consumer: ActorRef = context.actorOf( KafkaConsumerActor.props(kafkaConfig, actorConfig, self) ) consumer ! Subscribe.AutoPartition(List("topic1")) override def receive: Receive = { // Records from Kafka case recordsExt(records) => processRecords(records.pairs) sender() ! Confirm(records.offsets, commit = true) } private def processRecords(records: Seq[(Option[String], String)]) = records.foreach { case (key, value) => log.info(s"Received [$key,$value]") } }
Example 3
Source File: Constructr.scala From constructr with Apache License 2.0 | 5 votes |
package de.heikoseeberger.constructr import akka.actor.{ Actor, ActorLogging, ActorRef, Props, SupervisorStrategy, Terminated } import akka.cluster.{ Cluster, Member } import akka.cluster.ClusterEvent.{ InitialStateAsEvents, MemberExited, MemberLeft, MemberRemoved } import akka.cluster.MemberStatus.Up import de.heikoseeberger.constructr.coordination.Coordination import scala.concurrent.duration.{ FiniteDuration, NANOSECONDS } object Constructr { final val Name = "constructr" def props: Props = Props(new Constructr) } final class Constructr private extends Actor with ActorLogging { override val supervisorStrategy = SupervisorStrategy.stoppingStrategy private val cluster = Cluster(context.system) if (cluster.settings.SeedNodes.isEmpty) { log.info("Creating constructr-machine, because no seed-nodes defined") cluster.subscribe(self, InitialStateAsEvents, classOf[MemberLeft], classOf[MemberExited], classOf[MemberRemoved]) context.become(active(context.watch(createConstructrMachine()))) } else { log.info("Stopping self, because seed-nodes defined") context.stop(self) } override def receive = Actor.emptyBehavior private def active(machine: ActorRef): Receive = { case Terminated(`machine`) => val selfAddress = cluster.selfAddress def isSelfAndUp(member: Member) = member.address == selfAddress && member.status == Up if (cluster.state.members.exists(isSelfAndUp)) { log.error("Leaving, because constructr-machine terminated!") cluster.leave(selfAddress) } else { log.error("Terminating system, because constructr-machine terminated!") context.system.terminate() } case MemberRemoved(member, _) if member.address == cluster.selfAddress => log.error("Terminating system, because member has been removed!") context.system.terminate() } private def createConstructrMachine() = { val config = context.system.settings.config def getDuration(key: String) = FiniteDuration(config.getDuration(key).toNanos, NANOSECONDS) val coordinationTimeout = getDuration("constructr.coordination-timeout") val nrOfRetries = config.getInt("constructr.nr-of-retries") val retryDelay = getDuration("constructr.retry-delay") val refreshInterval = getDuration("constructr.refresh-interval") val ttlFactor = config.getDouble("constructr.ttl-factor") val maxNrOfSeedNodes = config.getInt("constructr.max-nr-of-seed-nodes") val joinTimeout = getDuration("constructr.join-timeout") val abortOnJoinTimeout = config.getBoolean("constructr.abort-on-join-timeout") val ignoreRefreshFailures = config.getBoolean("constructr.ignore-refresh-failures") context.actorOf( ConstructrMachine.props( cluster.selfAddress, Coordination(context.system.name, context.system), coordinationTimeout, nrOfRetries, retryDelay, refreshInterval, ttlFactor, if (maxNrOfSeedNodes <= 0) Int.MaxValue else maxNrOfSeedNodes, joinTimeout, abortOnJoinTimeout, ignoreRefreshFailures ), ConstructrMachine.Name ) } }
Example 4
Source File: AkkaActorsKafkaConsumer.scala From kafka-scala-api with Apache License 2.0 | 5 votes |
package com.example import akka.actor.{Actor, ActorLogging, ActorRef, ActorSystem, OneForOneStrategy, Props, SupervisorStrategy} import cakesolutions.kafka.KafkaConsumer import cakesolutions.kafka.akka.KafkaConsumerActor.{Confirm, Subscribe} import cakesolutions.kafka.akka.{ConsumerRecords, Extractor, KafkaConsumerActor} import scala.concurrent.duration._ object AkkaActorsKafkaConsumer extends App { ConsumerRecovery() } object ConsumerRecovery { def apply(): ActorRef ={ val actorConf = KafkaConsumerActor.Conf(1.seconds, 3.seconds) val system = ActorSystem() system.actorOf(Props(new ConsumerRecovery(kafkaConsumerConf, actorConf))) } } class ConsumerRecovery(kafkaConfig: KafkaConsumer.Conf[String, String], actorConfig: KafkaConsumerActor.Conf) extends Actor with ActorLogging { override def supervisorStrategy: SupervisorStrategy = OneForOneStrategy(maxNrOfRetries = 10) { case _: KafkaConsumerActor.ConsumerException => log.info("Consumer exception caught. Restarting consumer.") SupervisorStrategy.Restart case _ => SupervisorStrategy.Escalate } val recordsExt: Extractor[Any, ConsumerRecords[String, String]] = ConsumerRecords.extractor[String, String] val consumer: ActorRef = context.actorOf( KafkaConsumerActor.props(kafkaConfig, actorConfig, self) ) consumer ! Subscribe.AutoPartition(List(topic)) override def receive: Receive = { // Consume from Kafka case recordsExt(records) => processRecords(records.pairs) sender() ! Confirm(records.offsets, commit = true) } private def processRecords(records: Seq[(Option[String], String)]) = records.foreach { case (key, value) => log.info(s"Received [$key,$value]") } }
Example 5
Source File: UserApp.scala From gabbler with Apache License 2.0 | 5 votes |
package de.heikoseeberger.gabbler.user import akka.NotUsed import akka.actor.{ Actor, ActorLogging, ActorSystem, Props, SupervisorStrategy, Terminated } import akka.cluster.Cluster import akka.cluster.singleton.{ ClusterSingletonManager, ClusterSingletonManagerSettings, ClusterSingletonProxy, ClusterSingletonProxySettings } import akka.persistence.cassandra.query.scaladsl.CassandraReadJournal import akka.persistence.query.PersistenceQuery object UserApp { final class Root extends Actor with ActorLogging { override val supervisorStrategy = SupervisorStrategy.stoppingStrategy private val userRepository = { val readJournal = PersistenceQuery(context.system) .readJournalFor[CassandraReadJournal](CassandraReadJournal.Identifier) val userRepository = context.actorOf( ClusterSingletonManager.props(UserRepository(readJournal), NotUsed, ClusterSingletonManagerSettings(context.system)), UserRepository.Name ) context.actorOf( ClusterSingletonProxy.props(userRepository.path.elements.mkString("/", "/", ""), ClusterSingletonProxySettings(context.system)), s"${UserRepository.Name}-proxy" ) } private val userApi = { val config = context.system.settings.config val address = config.getString("gabbler-user.user-api.address") val port = config.getInt("gabbler-user.user-api.port") val timeout = config.getDuration("gabbler-user.user-api.user-repository-timeout").asScala context.actorOf(UserApi(address, port, userRepository, timeout), UserApi.Name) } context.watch(userRepository) context.watch(userApi) log.info("gabbler-user up and running") override def receive = { case Terminated(actor) => log.error("Terminating the system because {} terminated!", actor.path) context.system.terminate() } } def main(args: Array[String]): Unit = { val system = ActorSystem("gabbler-user") Cluster(system).registerOnMemberUp(system.actorOf(Props(new Root), "root")) } }
Example 6
Source File: ChatApp.scala From gabbler with Apache License 2.0 | 5 votes |
package de.heikoseeberger.gabbler.chat import akka.NotUsed import akka.actor.{ Actor, ActorLogging, ActorSystem, Props, SupervisorStrategy, Terminated } import akka.cluster.Cluster import akka.cluster.singleton.{ ClusterSingletonManager, ClusterSingletonManagerSettings, ClusterSingletonProxy, ClusterSingletonProxySettings } import akka.http.scaladsl.model.Uri import scala.concurrent.Await import scala.concurrent.duration.Duration object ChatApp { private final class Root extends Actor with ActorLogging { override val supervisorStrategy = SupervisorStrategy.stoppingStrategy private val userRepository = { val userEvents = Uri(context.system.settings.config.getString("gabbler-chat.user-repository.user-events")) val userRepository = context.actorOf( ClusterSingletonManager.props(UserRepository(userEvents), NotUsed, ClusterSingletonManagerSettings(context.system)), UserRepository.Name ) context.actorOf( ClusterSingletonProxy.props(userRepository.path.elements.mkString("/", "/", ""), ClusterSingletonProxySettings(context.system)), s"${UserRepository.Name}-proxy" ) } context.watch(userRepository) log.info("gabbler-chat up and running") override def receive = { case Terminated(actor) => log.error("Terminating the system because {} terminated!", actor.path) context.system.terminate() } } def main(args: Array[String]): Unit = { val system = ActorSystem("gabbler-chat") Cluster(system).registerOnMemberUp(system.actorOf(Props(new Root), "root")) } }
Example 7
Source File: RootActorSystem.scala From Waves with MIT License | 5 votes |
package com.wavesplatform.actor import akka.actor.{ActorSystem, AllForOneStrategy, SupervisorStrategy, SupervisorStrategyConfigurator} import com.typesafe.config.Config import com.wavesplatform.utils.ScorexLogging import scala.concurrent.Await import scala.concurrent.duration.Duration object RootActorSystem extends ScorexLogging { @volatile private var failed = false final class EscalatingStrategy extends SupervisorStrategyConfigurator { override def create(): SupervisorStrategy = AllForOneStrategy(loggingEnabled = false) { case t: Throwable => failed = true log.error("Root actor got exception, escalate", t) SupervisorStrategy.Escalate } } def start(id: String, config: Config)(init: ActorSystem => Unit): Unit = { val system = ActorSystem(id, config) try { init(system) } catch { case t: Throwable => log.error(s"Error while initializing actor system $id", t) sys.exit(1) } Await.result(system.whenTerminated, Duration.Inf) if (failed) { sys.exit(1) } else { sys.exit(0) } } }
Example 8
Source File: ZeroMQStreamSuite.scala From BigDatalog with Apache License 2.0 | 5 votes |
package org.apache.spark.streaming.zeromq import akka.actor.SupervisorStrategy import akka.util.ByteString import akka.zeromq.Subscribe import org.apache.spark.SparkFunSuite import org.apache.spark.storage.StorageLevel import org.apache.spark.streaming.{Seconds, StreamingContext} import org.apache.spark.streaming.dstream.ReceiverInputDStream class ZeroMQStreamSuite extends SparkFunSuite { val batchDuration = Seconds(1) private val master: String = "local[2]" private val framework: String = this.getClass.getSimpleName test("zeromq input stream") { val ssc = new StreamingContext(master, framework, batchDuration) val publishUrl = "abc" val subscribe = new Subscribe(null.asInstanceOf[ByteString]) val bytesToObjects = (bytes: Seq[ByteString]) => null.asInstanceOf[Iterator[String]] // tests the API, does not actually test data receiving val test1: ReceiverInputDStream[String] = ZeroMQUtils.createStream(ssc, publishUrl, subscribe, bytesToObjects) val test2: ReceiverInputDStream[String] = ZeroMQUtils.createStream( ssc, publishUrl, subscribe, bytesToObjects, StorageLevel.MEMORY_AND_DISK_SER_2) val test3: ReceiverInputDStream[String] = ZeroMQUtils.createStream( ssc, publishUrl, subscribe, bytesToObjects, StorageLevel.MEMORY_AND_DISK_SER_2, SupervisorStrategy.defaultStrategy) // TODO: Actually test data receiving ssc.stop() } }
Example 9
Source File: RestartSupervisor.scala From mist with Apache License 2.0 | 5 votes |
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 10
Source File: RareBooks.scala From reactive-application-development-scala with Apache License 2.0 | 5 votes |
package com.rarebooks.library import akka.actor.{Actor, ActorLogging, ActorPath, Address, OneForOneStrategy, Props, RootActorPath, Stash, SupervisorStrategy} import akka.routing.{ActorRefRoutee, RoundRobinRoutingLogic, Router} import scala.concurrent.duration.{Duration, FiniteDuration, MILLISECONDS => Millis} object RareBooks { case object Close case object Open case object Report // val name: String = // "rare-books" // // def pathFor(address: Address): ActorPath = // RootActorPath(address) / "user" / name def props: Props = Props(new RareBooks) } class RareBooks extends Actor with ActorLogging with Stash { import context.dispatcher import RareBooks._ import LibraryProtocol._ override val supervisorStrategy: SupervisorStrategy = { val decider: SupervisorStrategy.Decider = { case Librarian.ComplainException(complain, customer) => customer ! Credit() log.info(s"RareBooks sent customer $customer a credit") SupervisorStrategy.Restart } OneForOneStrategy()(decider orElse super.supervisorStrategy.decider) } private val openDuration: FiniteDuration = Duration(context.system.settings.config.getDuration("rare-books.open-duration", Millis), Millis) private val closeDuration: FiniteDuration = Duration(context.system.settings.config.getDuration("rare-books.close-duration", Millis), Millis) private val nbrOfLibrarians: Int = context.system.settings.config getInt "rare-books.nbr-of-librarians" private val findBookDuration: FiniteDuration = Duration(context.system.settings.config.getDuration("rare-books.librarian.find-book-duration", Millis), Millis) private val maxComplainCount: Int = context.system.settings.config getInt "rare-books.librarian.max-complain-count" var requestsToday: Int = 0 var totalRequests: Int = 0 var router: Router = createLibrarian() context.system.scheduler.scheduleOnce(openDuration, self, Close) protected def createLibrarian(): Router = { var cnt: Int = 0 val routees: Vector[ActorRefRoutee] = Vector.fill(nbrOfLibrarians) { val r = context.actorOf(Librarian.props(findBookDuration, maxComplainCount), s"librarian-$cnt") cnt += 1 ActorRefRoutee(r) } Router(RoundRobinRoutingLogic(), routees) } }
Example 11
Source File: RootActorSystem.scala From matcher with MIT License | 5 votes |
package com.wavesplatform.dex.actors import akka.actor.{ActorSystem, AllForOneStrategy, SupervisorStrategy, SupervisorStrategyConfigurator} import com.typesafe.config.Config import com.wavesplatform.dex.domain.utils.ScorexLogging import scala.concurrent.Await import scala.concurrent.duration.Duration object RootActorSystem extends ScorexLogging { @volatile private var failed = false final class EscalatingStrategy extends SupervisorStrategyConfigurator { override def create(): SupervisorStrategy = AllForOneStrategy(loggingEnabled = false) { case t: Throwable => failed = true log.error("Root actor got exception, escalate", t) SupervisorStrategy.Escalate } } def start(id: String, config: Config)(init: ActorSystem => Unit): Unit = { val system = ActorSystem(id, config) try { init(system) } catch { case t: Throwable => log.error(s"Error while initializing actor system $id", t) sys.exit(1) } Await.result(system.whenTerminated, Duration.Inf) if (failed) { sys.exit(1) } else { sys.exit(0) } } }
Example 12
Source File: ZeroMQStreamSuite.scala From spark1.52 with Apache License 2.0 | 5 votes |
package org.apache.spark.streaming.zeromq import akka.actor.SupervisorStrategy import akka.util.ByteString import akka.zeromq.Subscribe import org.apache.spark.SparkFunSuite import org.apache.spark.storage.StorageLevel import org.apache.spark.streaming.{Seconds, StreamingContext} import org.apache.spark.streaming.dstream.ReceiverInputDStream class ZeroMQStreamSuite extends SparkFunSuite { val batchDuration = Seconds(1) private val master: String = "local[2]" private val framework: String = this.getClass.getSimpleName test("zeromq input stream") { val ssc = new StreamingContext(master, framework, batchDuration) val publishUrl = "abc" val subscribe = new Subscribe(null.asInstanceOf[ByteString]) val bytesToObjects = (bytes: Seq[ByteString]) => null.asInstanceOf[Iterator[String]] // tests the API, does not actually test data receiving val test1: ReceiverInputDStream[String] = ZeroMQUtils.createStream(ssc, publishUrl, subscribe, bytesToObjects) val test2: ReceiverInputDStream[String] = ZeroMQUtils.createStream( ssc, publishUrl, subscribe, bytesToObjects, StorageLevel.MEMORY_AND_DISK_SER_2) val test3: ReceiverInputDStream[String] = ZeroMQUtils.createStream( ssc, publishUrl, subscribe, bytesToObjects, StorageLevel.MEMORY_AND_DISK_SER_2, SupervisorStrategy.defaultStrategy) // TODO: Actually test data receiving ssc.stop() } }
Example 13
Source File: ZeroMQStreamSuite.scala From iolap with Apache License 2.0 | 5 votes |
package org.apache.spark.streaming.zeromq import akka.actor.SupervisorStrategy import akka.util.ByteString import akka.zeromq.Subscribe import org.apache.spark.SparkFunSuite import org.apache.spark.storage.StorageLevel import org.apache.spark.streaming.{Seconds, StreamingContext} import org.apache.spark.streaming.dstream.ReceiverInputDStream class ZeroMQStreamSuite extends SparkFunSuite { val batchDuration = Seconds(1) private val master: String = "local[2]" private val framework: String = this.getClass.getSimpleName test("zeromq input stream") { val ssc = new StreamingContext(master, framework, batchDuration) val publishUrl = "abc" val subscribe = new Subscribe(null.asInstanceOf[ByteString]) val bytesToObjects = (bytes: Seq[ByteString]) => null.asInstanceOf[Iterator[String]] // tests the API, does not actually test data receiving val test1: ReceiverInputDStream[String] = ZeroMQUtils.createStream(ssc, publishUrl, subscribe, bytesToObjects) val test2: ReceiverInputDStream[String] = ZeroMQUtils.createStream( ssc, publishUrl, subscribe, bytesToObjects, StorageLevel.MEMORY_AND_DISK_SER_2) val test3: ReceiverInputDStream[String] = ZeroMQUtils.createStream( ssc, publishUrl, subscribe, bytesToObjects, StorageLevel.MEMORY_AND_DISK_SER_2, SupervisorStrategy.defaultStrategy) // TODO: Actually test data receiving ssc.stop() } }
Example 14
Source File: Warmup.scala From cloudstate with Apache License 2.0 | 5 votes |
package io.cloudstate.proxy import akka.actor.{Actor, ActorLogging, ActorRef, Props, SupervisorStrategy, Terminated} import com.google.protobuf.ByteString import io.cloudstate.proxy.eventsourced.EventSourcedEntity.{Configuration, Stop} import Warmup.Ready import io.cloudstate.protocol.entity.{ClientAction, Reply} import io.cloudstate.protocol.event_sourced.{EventSourcedReply, EventSourcedStreamIn, EventSourcedStreamOut} import io.cloudstate.proxy.entity.{EntityCommand, UserFunctionReply} import io.cloudstate.proxy.eventsourced.EventSourcedEntity import scala.concurrent.duration._ object Warmup { def props(needsWarmup: Boolean): Props = Props(new Warmup(needsWarmup)) case object Ready } class Warmup(needsWarmup: Boolean) extends Actor with ActorLogging { if (needsWarmup) { log.debug("Starting warmup...") val stateManager = context.watch( context.actorOf(EventSourcedEntity.props( Configuration("warmup.Service", "###warmup", 30.seconds, 100), "###warmup-entity", self, self, self ), "entity") ) stateManager ! EntityCommand( entityId = "###warmup-entity", name = "foo", payload = Some(com.google.protobuf.any.Any("url", ByteString.EMPTY)) ) context become warmingUp(stateManager) } // Default will be overriden above if we need to warm up override def receive = warm private def warmingUp(eventSourcedEntityManager: ActorRef): Receive = { case Ready => sender ! false case ConcurrencyEnforcer.Action(_, start) => log.debug("Warmup received action, starting it.") start() case EventSourcedStreamIn(EventSourcedStreamIn.Message.Event(_), _) => // Ignore case EventSourcedStreamIn(EventSourcedStreamIn.Message.Init(_), _) => log.debug("Warmup got init.") // Ignore case EventSourcedStreamIn(EventSourcedStreamIn.Message.Command(cmd), _) => log.debug("Warmup got forwarded command") // It's forwarded us our command, send it a reply eventSourcedEntityManager ! EventSourcedStreamOut( EventSourcedStreamOut.Message.Reply( EventSourcedReply( commandId = cmd.id, clientAction = Some( ClientAction(ClientAction.Action.Reply(Reply(Some(com.google.protobuf.any.Any("url", ByteString.EMPTY))))) ) ) ) ) case _: UserFunctionReply => log.debug("Warmup got forwarded reply") // It's forwarded the reply, now stop it eventSourcedEntityManager ! Stop case Terminated(_) => log.info("Warmup complete") context.become(warm) case other => // There are a few other messages we'll receive that we don't care about log.debug("Warmup received {}", other.getClass) } private def warm: Receive = { case Ready => sender ! true } override def supervisorStrategy: SupervisorStrategy = SupervisorStrategy.stoppingStrategy }
Example 15
Source File: AkkaUtilsSuite.scala From bahir with Apache License 2.0 | 5 votes |
package org.apache.spark.streaming.akka import scala.concurrent.duration._ import akka.actor.{Props, SupervisorStrategy} import org.apache.spark.SparkFunSuite import org.apache.spark.storage.StorageLevel import org.apache.spark.streaming.{Seconds, StreamingContext} import org.apache.spark.streaming.dstream.ReceiverInputDStream class AkkaUtilsSuite extends SparkFunSuite { test("createStream") { val ssc: StreamingContext = new StreamingContext("local[2]", "test", Seconds(1000)) try { // tests the API, does not actually test data receiving val test1: ReceiverInputDStream[String] = AkkaUtils.createStream( ssc, Props[TestActor](), "test") val test2: ReceiverInputDStream[String] = AkkaUtils.createStream( ssc, Props[TestActor](), "test", StorageLevel.MEMORY_AND_DISK_SER_2) val test3: ReceiverInputDStream[String] = AkkaUtils.createStream( ssc, Props[TestActor](), "test", StorageLevel.MEMORY_AND_DISK_SER_2, supervisorStrategy = SupervisorStrategy.defaultStrategy) val test4: ReceiverInputDStream[String] = AkkaUtils.createStream( ssc, Props[TestActor](), "test", StorageLevel.MEMORY_AND_DISK_SER_2, () => null) val test5: ReceiverInputDStream[String] = AkkaUtils.createStream( ssc, Props[TestActor](), "test", StorageLevel.MEMORY_AND_DISK_SER_2, () => null) val test6: ReceiverInputDStream[String] = AkkaUtils.createStream( ssc, Props[TestActor](), "test", StorageLevel.MEMORY_AND_DISK_SER_2, () => null, SupervisorStrategy.defaultStrategy) } finally { ssc.stop() } } } class TestActor extends ActorReceiver { override def receive: Receive = { case m: String => store(m) case m => store(m, 10.seconds) } }
Example 16
Source File: Controller.scala From eclair with Apache License 2.0 | 5 votes |
package fr.acinq.eclair.tor import java.net.InetSocketAddress import akka.actor.{Actor, ActorLogging, OneForOneStrategy, Props, SupervisorStrategy, Terminated} import akka.io.{IO, Tcp} import akka.util.ByteString import scala.concurrent.ExecutionContext class Controller(address: InetSocketAddress, protocolHandlerProps: Props) (implicit ec: ExecutionContext = ExecutionContext.global) extends Actor with ActorLogging { import Controller._ import Tcp._ import context.system IO(Tcp) ! Connect(address) def receive = { case e@CommandFailed(_: Connect) => e.cause match { case Some(ex) => log.error(ex, "Cannot connect") case _ => log.error("Cannot connect") } context stop self case c: Connected => val protocolHandler = context actorOf protocolHandlerProps protocolHandler ! c val connection = sender() connection ! Register(self) context watch connection context become { case data: ByteString => connection ! Write(data) case CommandFailed(w: Write) => // O/S buffer was full protocolHandler ! SendFailed log.error("Tor command failed") case Received(data) => protocolHandler ! data case _: ConnectionClosed => context stop self case Terminated(actor) if actor == connection => context stop self } } // we should not restart a failing tor session override val supervisorStrategy = OneForOneStrategy(loggingEnabled = true) { case _ => SupervisorStrategy.Escalate } } object Controller { def props(address: InetSocketAddress, protocolHandlerProps: Props)(implicit ec: ExecutionContext = ExecutionContext.global) = Props(new Controller(address, protocolHandlerProps)) case object SendFailed }
Example 17
Source File: Ingestor.scala From hydra with Apache License 2.0 | 5 votes |
package hydra.core.ingest import akka.actor.{Actor, OneForOneStrategy, SupervisorStrategy} import akka.pattern.pipe import hydra.core.akka.InitializingActor import hydra.core.monitor.HydraMetrics import hydra.core.protocol._ import hydra.core.transport.{AckStrategy, HydraRecord, RecordFactory, Transport} import org.apache.commons.lang3.ClassUtils import scala.concurrent.Future import scala.util.{Success, Try} def validateRequest(request: HydraRequest): Try[HydraRequest] = Success(request) def doValidate(request: HydraRequest): Future[MessageValidationResult] = { Future .fromTry(validateRequest(request)) .flatMap[MessageValidationResult] { r => recordFactory.build(r).map { r => val destination = r.destination val ackStrategy = r.ackStrategy.toString HydraMetrics.incrementGauge( lookupKey = ReconciliationMetricName + s"_${destination}_$ackStrategy", metricName = ReconciliationMetricName, tags = Seq( "ingestor" -> name, "destination" -> destination, "ackStrategy" -> ackStrategy ) ) HydraMetrics.incrementCounter( lookupKey = IngestCounterMetricName + s"_${destination}_$ackStrategy", metricName = IngestCounterMetricName, tags = Seq( "ingestor" -> name, "destination" -> destination, "ackStrategy" -> ackStrategy ) ) ValidRequest(r) } } .recover { case e => InvalidRequest(e) } } override def initializationError(ex: Throwable): Receive = { case Publish(req) => sender ! IngestorError(ex) context.system.eventStream .publish(IngestorUnavailable(thisActorName, ex, req)) case _ => sender ! IngestorError(ex) } def ingest(next: Actor.Receive) = compose(next) override val supervisorStrategy = OneForOneStrategy() { case _ => SupervisorStrategy.Restart } def decrementGaugeOnReceipt( destination: String, ackStrategy: String ): Future[Unit] = { Future { HydraMetrics.decrementGauge( lookupKey = Ingestor.ReconciliationMetricName + s"_${destination}_$ackStrategy", metricName = Ingestor.ReconciliationMetricName, tags = Seq( "ingestor" -> name, "destination" -> destination, "ackStrategy" -> ackStrategy ) ) } } } object Ingestor { val ReconciliationMetricName = "hydra_ingest_reconciliation" val IngestCounterMetricName = "hydra_ingest_message_counter" }
Example 18
Source File: Replica.scala From Principles-of-Reactive-Programming with GNU General Public License v3.0 | 5 votes |
package kvstore import akka.actor.{ OneForOneStrategy, Props, ActorRef, Actor } import kvstore.Arbiter._ import scala.collection.immutable.Queue import akka.actor.SupervisorStrategy.Restart import scala.annotation.tailrec import akka.pattern.{ ask, pipe } import akka.actor.Terminated import scala.concurrent.duration._ import akka.actor.PoisonPill import akka.actor.OneForOneStrategy import akka.actor.SupervisorStrategy import akka.util.Timeout object Replica { sealed trait Operation { def key: String def id: Long } case class Insert(key: String, value: String, id: Long) extends Operation case class Remove(key: String, id: Long) extends Operation case class Get(key: String, id: Long) extends Operation sealed trait OperationReply case class OperationAck(id: Long) extends OperationReply case class OperationFailed(id: Long) extends OperationReply case class GetResult(key: String, valueOption: Option[String], id: Long) extends OperationReply def props(arbiter: ActorRef, persistenceProps: Props): Props = Props(new Replica(arbiter, persistenceProps)) } class Replica(val arbiter: ActorRef, persistenceProps: Props) extends Actor { import Replica._ import Replicator._ import Persistence._ import context.dispatcher val replica: Receive = { case _ => } }
Example 19
Source File: AddressDirectoryActor.scala From matcher with MIT License | 5 votes |
package com.wavesplatform.dex.actors.address import akka.actor.{Actor, ActorRef, Props, SupervisorStrategy, Terminated} import com.wavesplatform.dex.db.OrderDB import com.wavesplatform.dex.domain.account.Address import com.wavesplatform.dex.domain.utils.{EitherExt2, ScorexLogging} import com.wavesplatform.dex.history.HistoryRouter._ import com.wavesplatform.dex.model.Events import com.wavesplatform.dex.model.Events.OrderCancelFailed import scala.collection.mutable class AddressDirectoryActor(orderDB: OrderDB, addressActorProps: (Address, Boolean) => Props, historyRouter: Option[ActorRef]) extends Actor with ScorexLogging { import AddressDirectoryActor._ import context._ private var startSchedules: Boolean = false private[this] val children = mutable.AnyRefMap.empty[Address, ActorRef] override def supervisorStrategy: SupervisorStrategy = SupervisorStrategy.stoppingStrategy private def createAddressActor(address: Address): ActorRef = { log.debug(s"Creating address actor for $address") watch(actorOf(addressActorProps(address, startSchedules), address.toString)) } private def forward(address: Address, msg: Any): Unit = (children get address, msg) match { case (None, _: AddressActor.Message.BalanceChanged) => case _ => children getOrElseUpdate (address, createAddressActor(address)) forward msg } override def receive: Receive = { case Envelope(address, cmd) => forward(address, cmd) case e @ Events.OrderAdded(lo, timestamp) => forward(lo.order.sender, e) historyRouter foreach { _ ! SaveOrder(lo, timestamp) } case e: Events.OrderExecuted => import e.{counter, submitted} forward(submitted.order.sender, e) if (counter.order.sender != submitted.order.sender) forward(counter.order.sender, e) historyRouter foreach { _ ! SaveEvent(e) } case e: Events.OrderCanceled => forward(e.acceptedOrder.order.sender, e) historyRouter foreach { _ ! SaveEvent(e) } case e: OrderCancelFailed => orderDB.get(e.id) match { case Some(order) => forward(order.sender.toAddress, e) case None => log.warn(s"The order '${e.id}' not found") } case StartSchedules => if (!startSchedules) { startSchedules = true context.children.foreach(_ ! StartSchedules) } case Terminated(child) => val addressString = child.path.name val address = Address.fromString(addressString).explicitGet() children.remove(address) log.warn(s"Address handler for $addressString terminated") } } object AddressDirectoryActor { case class Envelope(address: Address, cmd: AddressActor.Message) case object StartSchedules }