akka.pattern.ask Scala Examples
The following examples show how to use akka.pattern.ask.
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: UpdateLogger.scala From Scalaprof with GNU General Public License v2.0 | 5 votes |
package com.phasmid.hedge_fund.actors import akka.actor.{ ActorRef, Props } import akka.pattern.ask import akka.util.Timeout import scala.concurrent.duration._ import scala.concurrent.Await import scala.language.postfixOps import com.phasmid.hedge_fund.model.Model import com.phasmid.hedge_fund.portfolio._ class UpdateLogger(blackboard: ActorRef) extends BlackboardActor(blackboard) { var portfolio = new Portfolio("", Nil) override def receive = { case Confirmation(id, model, attrs) => log.debug(s"update for identifier: $id") if (model.isOption) processOption(id, model, attrs) else processStock(id, model) case PortfolioUpdate(p) => log.debug(s"portfolio update for: ${p.name}") portfolio = p showPortfolio case m => super.receive(m) } implicit val timeout = Timeout(5 seconds) def processStock(identifier: String, model: Model) = { model.getKey("price") match { case Some(p) => { // sender is the MarketData actor val future = (sender ? SymbolQuery(identifier, List(p))).mapTo[QueryResponse] // TODO why are we waiting for this here? val result = Await.result(future, timeout.duration) result match { case QueryResponseValid(k,a) => a map { case (k, v) => log.info(s"$identifier attribute $k has been updated to: $v") } case _ => } } case None => log.warning(s"'price' not defined in model") } } def processOption(identifier: String, model: Model, attributes: Map[String, Any]) = { val key = "underlying" attributes.get(key) match { case Some(value) => val future = (blackboard ? OptionQuery("id", value)).mapTo[QueryResponse] // TODO why are we waiting for this here? val result = Await.result(future, timeout.duration) result match { case QueryResponseValid(k,a) => println(s"Action Required: re: qualifying option $identifier with underlying symbol: $k and attributes: $a") case _ => } case None => log.warning(s"processOption: value not present for $key") } } def showPortfolio { println(s"Portfolio for ${portfolio.name}") portfolio.positions foreach { showPosition(_) } } def showPosition(position: Position) { println(s"position for ${position.symbol}: quantity=${position.quantity}; options=") position.contracts foreach { showContract(_) } } def showContract(contract: Contract) { println(s"contract: $contract") } }
Example 2
Source File: RestInterface.scala From akka-sharding-example with MIT License | 5 votes |
package com.michalplachta.shoesorter.api import akka.actor.{Actor, ActorLogging, ActorRef} import akka.io.IO import akka.pattern.ask import com.michalplachta.shoesorter.Domain.{Container, Junction} import com.michalplachta.shoesorter.Messages._ import spray.can.Http import spray.httpx.SprayJsonSupport._ import spray.routing._ import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.duration._ class RestInterface(decider: ActorRef, exposedPort: Int) extends Actor with HttpServiceBase with ActorLogging { val route: Route = { path("junctions" / IntNumber / "decisionForContainer" / IntNumber) { (junctionId, containerId) => get { complete { log.info(s"Request for junction $junctionId and container $containerId") val junction = Junction(junctionId) val container = Container(containerId) decider.ask(WhereShouldIGo(junction, container))(5 seconds).mapTo[Go] } } } } def receive = runRoute(route) implicit val system = context.system IO(Http) ! Http.Bind(self, interface = "0.0.0.0", port = exposedPort) }
Example 3
Source File: HttpManagementServer.scala From akka-cluster-manager with MIT License | 5 votes |
package io.orkestra.cluster.management import java.util.concurrent.atomic.AtomicReference import akka.Done import akka.actor.{ActorSystem, ActorRef} import akka.cluster.Cluster import akka.http.scaladsl.Http import akka.http.scaladsl.model.StatusCodes import akka.http.scaladsl.server.Directives._ import akka.stream.{ActorMaterializer, Materializer} import io.orkestra.cluster.protocol.Response.{Failure, Success} import io.orkestra.cluster.routing.ClusterListener._ import akka.pattern.ask import play.api.libs.json.Json import scala.concurrent.{Promise, Future, ExecutionContext} import scala.concurrent.duration._ class HttpManagementServer(clusterListener: ActorRef, hostName: String = "127.0.0.1", port: Int = 33333)( implicit val system: ActorSystem, implicit val materializer: Materializer, implicit val executer: ExecutionContext ) { import PlayJsonSupport._ def handleOrkestraRequest(req: ManagementReguest) = (clusterListener ? req)(3.seconds).map { case res: Success => res.httpStatusCode -> res.asJson case res: Failure => res.httpStatusCode -> res.asJson } def orkestraRoutes = pathPrefix("orkestra" / "routers") { pathEndOrSingleSlash { get { complete(handleOrkestraRequest(GetRouters)) } } ~ path(Segment ~ Slash.?) { role => get { complete(handleOrkestraRequest(GetRouter(role))) } } ~ path(Segment / Remaining ~ Slash.?) { (role, routeePath) => delete { complete(handleOrkestraRequest(DeleteRoutee(role, routeePath))) } } } private val bindingFuture = new AtomicReference[Future[Http.ServerBinding]]() def start() = { val serverBindingPromise = Promise[Http.ServerBinding]() if (bindingFuture.compareAndSet(null, serverBindingPromise.future)) { Http().bindAndHandle(orkestraRoutes, hostName, port) println(Console.CYAN + s"cluster http management server online at http://${hostName}:${port}/" + Console.WHITE) } } def shutdown = if (bindingFuture.get() == null) { Future(Done) } else { val stopFuture = bindingFuture.get().flatMap(_.unbind()).map(_ => Done) bindingFuture.set(null) stopFuture } }
Example 4
Source File: PluginRegistryTest.scala From sumobot with Apache License 2.0 | 5 votes |
package com.sumologic.sumobot.core import akka.actor.{ActorSystem, Props} import akka.pattern.ask import akka.testkit.TestKit import akka.util.Timeout import com.sumologic.sumobot.core.PluginRegistry.{Plugin, PluginList, RequestPluginList} import com.sumologic.sumobot.plugins.BotPlugin.{PluginAdded, PluginRemoved} import com.sumologic.sumobot.plugins.help.Help import com.sumologic.sumobot.test.annotated.SumoBotTestKit import org.scalatest.BeforeAndAfterAll import scala.concurrent.Await import scala.concurrent.duration._ class PluginRegistryTest extends SumoBotTestKit(ActorSystem("PluginRegistryTest")) with BeforeAndAfterAll { "PluginRegistry" should { "maintain a list of all registered plugins" in { implicit val timeout = Timeout(1.second) val reg = system.actorOf(Props[PluginRegistry]) def checkList(func: Seq[Plugin] => Unit) = { Await.result(reg ? RequestPluginList, 1.second) match { case PluginList(list) => func(list) case other => fail(s"Got $other instead.") } } val fakePlugin = system.actorOf(Props[Help]) checkList(_.isEmpty should be(true)) reg ! PluginAdded(fakePlugin, "hah") checkList(_.size should be(1)) reg ! PluginRemoved(fakePlugin) checkList(_.isEmpty should be(true)) } } override protected def afterAll(): Unit = { TestKit.shutdownActorSystem(system) } }
Example 5
Source File: S3BrainTest.scala From sumobot with Apache License 2.0 | 5 votes |
package com.sumologic.sumobot.brain import akka.actor.ActorSystem import akka.pattern.ask import akka.testkit.TestKit import akka.util.Timeout import com.amazonaws.auth.{AWSCredentials, AWSStaticCredentialsProvider} import com.amazonaws.services.s3.AmazonS3ClientBuilder import com.sumologic.sumobot.brain.Brain.ValueRetrieved import com.sumologic.sumobot.core.aws.AWSAccounts import com.sumologic.sumobot.test.annotated.SumoBotTestKit import org.scalatest.{BeforeAndAfterAll, Matchers} import scala.collection.JavaConverters._ import scala.concurrent.Await import scala.concurrent.duration._ import scala.util.Random class S3BrainTest extends SumoBotTestKit(ActorSystem("S3SingleObjectBrainTest")) with BeforeAndAfterAll with Matchers { lazy val credsOption = AWSAccounts.load(system.settings.config).values.headOption val bucketPrefix = "sumobot-s3-brain" // The tests here only run if there are valid AWS credentials in the configuration. Otherwise, // they're skipped. credsOption foreach { creds => cleanupBuckets(creds) val bucket = bucketPrefix + randomString(5) "S3 brain" should { "persist the contents across reloads" in { implicit val timeout = Timeout(5.seconds) val s3Key = randomString(16) val firstBrain = system.actorOf(S3Brain.props(creds, bucket, s3Key)) firstBrain ! Brain.Store("hello", "world") // Just wait for the next message to return. val firstRetrieval = firstBrain ? Brain.Retrieve("hello") val firstResult = Await.result(firstRetrieval, 5.seconds) firstResult match { case ValueRetrieved(k, v) => k should be("hello") v should be("world") case wrongResult => fail(s"Did not get what we expected: $wrongResult") } // Since we wrote to S3, the 2nd brain should now have the value. val secondBrain = system.actorOf(S3Brain.props(creds, bucket, s3Key)) val secondRetrieval = secondBrain ? Brain.Retrieve("hello") val secondResult = Await.result(secondRetrieval, 5.seconds) secondResult match { case ValueRetrieved(k, v) => k should be("hello") v should be("world") case wrongResult => fail(s"Did not get what we expected: $wrongResult") } } } } private def randomString(length: Int): String = { val alphabet = ('a' to 'z').mkString + ('0' to '9').mkString (1 to length). map(_ => Random.nextInt(alphabet.length)). map(alphabet.charAt).mkString } override def afterAll() { TestKit.shutdownActorSystem(system) credsOption.foreach(cleanupBuckets) } def cleanupBuckets(creds: AWSCredentials): Unit = { val s3 = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(creds)).build() s3.listBuckets().asScala.filter(_.getName.startsWith(bucketPrefix)).foreach { bucket => println(s"Deleting S3 bucket ${bucket.getName}") val objects = s3.listObjects(bucket.getName).getObjectSummaries.asScala.map(_.getKey) objects.foreach { obj => s3.deleteObject(bucket.getName, obj) } s3.deleteBucket(bucket.getName) } } }
Example 6
Source File: Help.scala From sumobot with Apache License 2.0 | 5 votes |
package com.sumologic.sumobot.plugins.help import akka.actor.ActorLogging import akka.pattern.ask import akka.util.Timeout import com.sumologic.sumobot.core.PluginRegistry.{PluginList, RequestPluginList} import com.sumologic.sumobot.core.model.IncomingMessage import com.sumologic.sumobot.plugins.BotPlugin import scala.concurrent.duration._ import scala.util.Success object Help { private[help] val ListPlugins = BotPlugin.matchText("(help|\\?)\\W*") private[help] val HelpForPlugin = BotPlugin.matchText("(help|\\?) ([\\-\\w]+).*") } class Help extends BotPlugin with ActorLogging { override protected def help = s"""I can help you understand plugins. | |help - I'll tell you what plugins I've got. |help <plugin>. - I'll tell you how <plugin> works. """.stripMargin import Help._ override protected def receiveIncomingMessage = { case message@IncomingMessage(ListPlugins(_), true, _, _, _, _, _) => val msg = message implicit val timeout = Timeout(5.seconds) pluginRegistry ? RequestPluginList onComplete { case Success(result) => result match { case PluginList(plugins) => msg.say(plugins.map(_.plugin.path.name).sorted.mkString("\n")) } case _ => } case message@IncomingMessage(HelpForPlugin(_, pluginName), addressedToUs, _, _, _, _, _) => val msg = message implicit val timeout = Timeout(5.seconds) pluginRegistry ? RequestPluginList onComplete { case Success(result) => result match { case PluginList(plugins) => plugins.find(_.plugin.path.name.equalsIgnoreCase(pluginName)) match { case Some(plugin) => msg.say(plugin.help) case None => if (addressedToUs) { msg.respond(s"Sorry, I don't know $pluginName") } } } case _ => } } }
Example 7
Source File: BlockingBrain.scala From sumobot with Apache License 2.0 | 5 votes |
package com.sumologic.sumobot.brain import akka.actor.ActorRef import akka.pattern.ask import akka.util.Timeout import com.sumologic.sumobot.brain.Brain._ import scala.concurrent.Await import scala.concurrent.duration._ class BlockingBrain(brain: ActorRef) { def retrieve(key: String): Option[String] = { implicit val timeout = Timeout(2.seconds) Await.result(brain ? Retrieve(key), 2.seconds) match { case ValueRetrieved(_, value) => Some(value) case ValueMissing(_) => None } } def listValues(prefix: String = ""): Map[String, String] = { implicit val timeout = Timeout(2.seconds) Await.result(brain ? ListValues(prefix), 2.seconds) match { case ValueMap(map) => map } } def store(key: String, value: String): Unit = { brain ! Store(key, value) } def remove(key: String): Unit = { brain ! Remove(key) } }
Example 8
Source File: WorkflowDownloadClient.scala From seahorse-workflow-executor with Apache License 2.0 | 5 votes |
package io.deepsense.workflowexecutor import scala.concurrent.Future import scala.concurrent.duration._ import scala.util.Try import akka.actor.ActorSystem import akka.io.IO import akka.pattern.ask import spray.can.Http import spray.client.pipelining._ import spray.http._ import spray.util._ import io.deepsense.commons.utils.Logging import io.deepsense.sparkutils import io.deepsense.workflowexecutor.exception.UnexpectedHttpResponseException class WorkflowDownloadClient( val address: String, val path: String, val timeout: Int) extends Logging { val downloadUrl = (workflowId: String) => s"$address/$path/$workflowId/download" def downloadWorkflow(workflowId: String): Future[String] = { logger.info(s"Downloading workflow $workflowId...") implicit val system = ActorSystem() import system.dispatcher implicit val timeoutSeconds = timeout.seconds val pipeline: HttpRequest => Future[HttpResponse] = sendReceive val futureResponse = pipeline(Get(downloadUrl(workflowId))) futureResponse.onComplete { _ => Try(IO(Http).ask(Http.CloseAll)(1.second).await) sparkutils.AkkaUtils.terminate(system) } futureResponse.map(handleResponse) } private def handleResponse(response: HttpResponse): String = { response.status match { case StatusCodes.OK => response.entity.data.asString case _ => throw UnexpectedHttpResponseException( "Workflow download failed", response.status, response.entity.data.asString) } } }
Example 9
Source File: RestClient.scala From seahorse-workflow-executor with Apache License 2.0 | 5 votes |
package io.deepsense.commons.rest.client import java.net.URL import java.util.UUID import scala.concurrent.{ExecutionContext, Future} import akka.io.IO import akka.pattern.ask import spray.can.Http import spray.can.Http.HostConnectorInfo import spray.client.pipelining._ import spray.http.{HttpCredentials, HttpRequest, HttpResponse} import spray.httpx.unmarshalling.FromResponseUnmarshaller trait RestClient extends RestClientImplicits { import RestClient._ def apiUrl: URL def userId: Option[UUID] def userName: Option[String] def credentials: Option[HttpCredentials] implicit override val ctx: ExecutionContext = as.dispatcher private def hostConnectorFut(): Future[HostConnectorInfo] = { (IO(Http) ? Http.HostConnectorSetup(apiUrl.getHost, port = apiUrl.getPort)).mapTo[HostConnectorInfo] } private def sendReceivePipeline() : Future[HttpRequest => Future[HttpResponse]] = { for { HostConnectorInfo(hostConnector, _) <- hostConnectorFut() } yield { credentials.map(addCredentials).getOrElse[RequestTransformer](identity) ~> userId.map(id => addHeader(UserIdHeader, id.toString)).getOrElse[RequestTransformer](identity) ~> userName.map(addHeader(UserNameHeader, _)).getOrElse[RequestTransformer](identity) ~> sendReceive(hostConnector) } } private def unmarshalPipeline[U: FromResponseUnmarshaller](): Future[HttpRequest => Future[U]] = { for { sr <- sendReceivePipeline() } yield { sr ~> unmarshal[U] } } def fetchResponse[U : FromResponseUnmarshaller]( req: HttpRequest ): Future[U] = { unmarshalPipeline().flatMap(_(req)) } def fetchHttpResponse(req: HttpRequest) : Future[HttpResponse] = { sendReceivePipeline().flatMap(_(req)) } def endpointPath(endpoint: String): String = { new URL(apiUrl, endpoint).getFile } } object RestClient { val UserIdHeader = "X-Seahorse-UserId" val UserNameHeader = "X-Seahorse-UserName" }
Example 10
Source File: Retry.scala From seahorse-workflow-executor with Apache License 2.0 | 5 votes |
package io.deepsense.commons.utils import scala.concurrent.Future import scala.concurrent.duration.FiniteDuration import akka.actor.{ActorSystem, Props} import akka.pattern.ask import akka.util.Timeout trait Retry[T] { def work: Future[T] def retryInterval: FiniteDuration def retryLimit: Int def actorSystem: ActorSystem // the timeout should exceed the retryLimit * retryInterval + (retryLimit + 1) * avgWorkDuration // otherwise the ask in tryWork method may timeout before all the retries have been attempted implicit def timeout: Timeout def workDescription: Option[String] private lazy val retryActor = actorSystem.actorOf(Props(new RetryActor[T]( retryInterval, retryLimit, work, workDescription ))) def tryWork: Future[T] = (retryActor ? RetryActor.Trigger).asInstanceOf[Future[T]] }
Example 11
Source File: CalculatorSpec.scala From Scalaprof with GNU General Public License v2.0 | 5 votes |
package actors import org.scalatest._ import akka.testkit.TestActorRef import scala.concurrent.duration._ import scala.concurrent.Await import akka.pattern.ask import scala.util._ import scala.io.Source import scala.concurrent._ import scala.concurrent.duration._ import com.typesafe.config.{ ConfigFactory, Config } import akka.actor.{ Actor, ActorSystem, Props, ActorRef } import akka.util.Timeout import java.net.URL import org.scalatest.concurrent._ import org.scalatest._ import org.scalatest.time._ import edu.neu.coe.scala.numerics.Rational import models._ class CalculatorSpec extends FlatSpec with Matchers with Futures with ScalaFutures with Inside { implicit val system = ActorSystem("CountWords") import play.api.libs.concurrent.Execution.Implicits.defaultContext implicit val timeout: Timeout = Timeout(10 seconds) "Rational Calculator" should "yield empty list for /" in { val lookup: String=>Option[Rational] = RationalMill.constants.get _ val conv: String=>Try[Rational] = RationalMill.valueOf _ val parser = new ExpressionParser[Rational](conv,lookup) val mill: Mill[Rational] = RationalMill() val props = Props(new Calculator(mill,parser)) val taf = TestActorRef(props) val xsf = (taf ? View).mapTo[Seq[Rational]] val nf = xsf map { case xs => xs.size } whenReady(nf, timeout(Span(6, Seconds))) { case 0 => } } it should "yield 1 for 1" in { val lookup: String=>Option[Rational] = RationalMill.constants.get _ val conv: String=>Try[Rational] = RationalMill.valueOf _ val parser = new ExpressionParser[Rational](conv,lookup) val mill: Mill[Rational] = RationalMill() val props = Props(new Calculator(mill,parser)) val taf = TestActorRef(props) val xtf = (taf ? "1").mapTo[Try[Rational]] whenReady(xtf, timeout(Span(6, Seconds))) { case Success(Rational(1,1)) => } } it should "yield 1 when given floating point problem" in { val lookup: String=>Option[Rational] = RationalMill.constants.get _ val conv: String=>Try[Rational] = RationalMill.valueOf _ val parser = new ExpressionParser[Rational](conv,lookup) val mill: Mill[Rational] = RationalMill() val props = Props(new Calculator(mill,parser)) val taf = TestActorRef(props) val xtf = (taf ? "0.2 0.1 + 10 * 3 /").mapTo[Try[Rational]] whenReady(xtf, timeout(Span(6, Seconds))) { case Success(Rational(1,1)) => } } "Double Calculator" should "yield empty list for /" in { val lookup: String=>Option[Double] = DoubleMill.constants.get _ val conv: String=>Try[Double] = DoubleMill.valueOf _ val parser = new ExpressionParser[Double](conv,lookup) val mill: Mill[Double] = DoubleMill() val props = Props(new Calculator(mill,parser)) val taf = TestActorRef(props) val xsf = (taf ? View).mapTo[Seq[Double]] val nf = xsf map { case xs => xs.size } whenReady(nf, timeout(Span(6, Seconds))) { case 0 => } } // This test suffers from a very peculiar bug which might even be a bug // in the Scala compiler. Kudos to you if you can fix it!! ignore should "yield 1 for 1" in { val lookup: String=>Option[Double] = DoubleMill.constants.get _ val conv: String=>Try[Double] = DoubleMill.valueOf _ val parser = new ExpressionParser[Double](conv,lookup) val mill: Mill[Double] = DoubleMill() val props = Props(new Calculator(mill,parser)) val taf = TestActorRef(props) val xtf = (taf ? "1").mapTo[Try[Double]] whenReady(xtf, timeout(Span(6, Seconds))) { case Success(1.0) => } } }
Example 12
Source File: Application.scala From Scalaprof with GNU General Public License v2.0 | 5 votes |
package controllers import play.api._ import play.api.mvc._ import akka.actor.{ActorSystem, Props} import akka.util.Timeout import akka.pattern.ask import scala.concurrent._ import scala.concurrent.duration._ import scala.util._ import edu.neu.coe.scala.numerics.Rational import akka.actor.ActorRef import com.typesafe.config.{ ConfigFactory, Config } import actors._ import models._ import spire.math.Real class Application extends Controller { val config = ConfigFactory.load() val which = config.getString("calculator") import play.api.libs.concurrent.Execution.Implicits.defaultContext implicit val timeout: Timeout = Timeout(10 seconds) implicit val system = ActorSystem("RPN-Calculator") val setup = which match { case "rational" => Application.getSetupForRational case "double" => Application.getSetupForDouble case "spire" => Application.getSetupForSpire case _ => Console.err.println(s"Unsupported calculator type: $which"); Application.getSetupForRational } val calculator = system.actorOf(setup _1,setup _2) val name: String = setup _3; println(s"$name is ready") def index() = Action.async { val xsf = (calculator ? View).mapTo[Seq[_]] xsf map { case xs => Ok(s"$name: calculator has the following elements (starting with top): $xs") } } def command(s: String) = Action.async { val xtf = (calculator ? s).mapTo[Try[_]] xtf map { case Success(x) => Ok(s"""$name: you have entered "$s" and got back $x""") case Failure(e) => if (s=="clr") Ok("$name: cleared") else Ok(s"""$name: you entered "$s" which caused error: $e""") // case Failure(e) => if (s=="clr") redirect("/") else Ok(s"""$name: you entered "$s" which caused error: $e""") } } } object Application { // TODO move these to model classes def getSetupForDouble(implicit system: ActorSystem) = { implicit val lookup: String=>Option[Double] = DoubleMill.constants.get _ implicit val conv: String=>Try[Double] = DoubleMill.valueOf _ implicit val parser = new ExpressionParser[Double](conv,lookup) val mill = DoubleMill() // Note: the following pattern should NOT be used within an actor val props = Props(new Calculator(mill,parser)) // TODO for these methods, return mill and parser instead of props (props,"doubleCalculator","Double Calculator") } // CONSIDER This assumes that we have Rational in our classpath already. // I'd like to try the possibility of dynamically loading the Rational stuff. // But, that's going to be very tricky, so we'll leave it for now. def getSetupForRational(implicit system: ActorSystem) = { implicit val lookup: String=>Option[Rational] = RationalMill.constants.get _ implicit val conv: String=>Try[Rational] = RationalMill.valueOf _ implicit val parser = new ExpressionParser[Rational](conv,lookup) val mill = RationalMill() // Note: the following pattern should NOT be used within an actor val props = Props(new Calculator(mill,parser)) (props,"rationalCalculator","Rational Calculator") } // CONSIDER This assumes that we have Spire in our classpath already. def getSetupForSpire(implicit system: ActorSystem) = { import spire.implicits._ import spire.math._ implicit val lookup: String=>Option[Real] = SpireMill.constants.get _ implicit val conv: String=>Try[Real] = SpireMill.valueOf _ implicit val parser = new ExpressionParser[Real](conv,lookup) val mill = SpireMill() // Note: the following pattern should NOT be used within an actor val props = Props(new Calculator(mill,parser)) (props,"spireCalculator","Spire Calculator") } }
Example 13
Source File: JsonYQLParserSpec.scala From Scalaprof with GNU General Public License v2.0 | 5 votes |
package com.phasmid.hedge_fund.actors import akka.actor.{ ActorSystem, Actor, Props, ActorRef } import akka.testkit._ import org.scalatest.{ WordSpecLike, Matchers, BeforeAndAfterAll } import scala.io.Source import scala.concurrent.duration._ import spray.http._ import spray.http.MediaTypes._ import org.scalatest.Inside import scala.language.postfixOps import spray.http.ContentType.apply class JsonYQLParserSpec(_system: ActorSystem) extends TestKit(_system) with ImplicitSender with WordSpecLike with Matchers with Inside with BeforeAndAfterAll { def this() = this(ActorSystem("JsonYQLParserSpec")) override def afterAll { TestKit.shutdownActorSystem(system) } import scala.language.postfixOps val json = Source.fromFile("src/test/resources/yqlExample.json") mkString "json conversion" in { val body = HttpEntity(MediaTypes.`application/json`, json.getBytes()) val ok = JsonYQLParser.decode(body) match { case Right(x) => val count = x.query.count count should equal(4) x.query.results.quote.length should equal(count) x.query.results.get(count - 1, "symbol") should matchPattern { case Some("MSFT") => } case Left(x) => fail("decoding error: " + x) } } "send back" in { val blackboard = system.actorOf(Props.create(classOf[MockYQLBlackboard], testActor), "blackboard") val entityParser = _system.actorOf(Props.create(classOf[EntityParser], blackboard), "entityParser") val entity = HttpEntity(MediaTypes.`application/json`, json.getBytes()) entityParser ! EntityMessage("json:YQL", entity) val msg = expectMsgClass(3.seconds, classOf[QueryResponseValid]) println("msg received: " + msg) msg should matchPattern { case QueryResponseValid("MSFT", _) => } inside(msg) { case QueryResponseValid(symbol, attributes) => attributes.get("Ask") should matchPattern { case Some("46.17") => } } } } import akka.pattern.ask import akka.util.Timeout import scala.concurrent.duration._ import scala.concurrent.Await import com.phasmid.hedge_fund.model.Model class MockYQLUpdateLogger(blackboard: ActorRef) extends UpdateLogger(blackboard) { override def processStock(identifier: String, model: Model) = { model.getKey("price") match { case Some(p) => { // sender is the MarketData actor val future = sender ? SymbolQuery(identifier, List(p)) val result = Await.result(future, timeout.duration).asInstanceOf[QueryResponseValid] result.attributes map { case (k, v) => log.info(s"$identifier attribute $k has been updated to: $v") blackboard ! result } } case None => log.warning(s"'price' not defined in model") } } } class MockYQLBlackboard(testActor: ActorRef) extends Blackboard(Map(classOf[KnowledgeUpdate] -> "marketData", classOf[SymbolQuery] -> "marketData", classOf[OptionQuery] -> "marketData", classOf[CandidateOption] -> "optionAnalyzer", classOf[Confirmation] -> "updateLogger"), Map("marketData" -> classOf[MarketData], "optionAnalyzer" -> classOf[OptionAnalyzer], "updateLogger" -> classOf[MockYQLUpdateLogger])) { override def receive = { case msg: Confirmation => msg match { // Cut down on the volume of messages case Confirmation("MSFT", _, _) => super.receive(msg) case _ => } case msg: QueryResponseValid => testActor forward msg case msg => super.receive(msg) } }
Example 14
Source File: IsCompleteHandler.scala From incubator-toree with Apache License 2.0 | 5 votes |
package org.apache.toree.kernel.protocol.v5.handler import akka.pattern.ask import org.apache.toree.kernel.protocol.v5._ import org.apache.toree.kernel.protocol.v5.content._ import org.apache.toree.kernel.protocol.v5.kernel.{ActorLoader, Utilities} import Utilities._ import org.apache.toree.utils.{MessageLogSupport, LogLike} import play.api.data.validation.ValidationError import play.api.libs.json.{JsPath, Json} import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.Future import scala.util.Success class IsCompleteHandler(actorLoader: ActorLoader) extends BaseHandler(actorLoader) with MessageLogSupport { override def process(kernelMessage: KernelMessage): Future[_] = { logKernelMessageAction("Determining if code is complete for", kernelMessage) Utilities.parseAndHandle( kernelMessage.contentString, IsCompleteRequest.isCompleteRequestReads, isCompleteRequest(kernelMessage, _ : IsCompleteRequest) ) } private def isCompleteRequest(km: KernelMessage, cr: IsCompleteRequest): Future[(String, String)] = { val interpreterActor = actorLoader.load(SystemActorType.Interpreter) val codeCompleteFuture = ask(interpreterActor, cr).mapTo[(String, String)] codeCompleteFuture.onComplete { case Success(tuple) => val reply = IsCompleteReply(tuple._1, tuple._2) val isCompleteReplyType = MessageType.Outgoing.IsCompleteReply.toString logKernelMessageAction("Sending is complete reply for", km) actorLoader.load(SystemActorType.KernelMessageRelay) ! km.copy( header = HeaderBuilder.create(isCompleteReplyType), parentHeader = km.header, contentString = Json.toJson(reply).toString ) case _ => new Exception("Parse error in CodeCompleteHandler") } codeCompleteFuture } }
Example 15
Source File: HelloServiceImpl.scala From scala-tutorials with MIT License | 5 votes |
package com.baeldung.hello.impl import akka.NotUsed import akka.actor.ActorSystem import akka.cluster.Cluster import akka.cluster.routing.{ClusterRouterGroup, ClusterRouterGroupSettings} import akka.pattern.ask import akka.routing.ConsistentHashingGroup import akka.stream.scaladsl.Source import akka.util.Timeout import com.baeldung.hello.akka.{Job, JobAccepted, JobStatus, Worker} import com.baeldung.hello.api.HelloService import com.lightbend.lagom.scaladsl.api.ServiceCall import com.lightbend.lagom.scaladsl.pubsub.{PubSubRegistry, TopicId} import scala.concurrent.{ExecutionContext, Future} import scala.concurrent.duration._ class HelloServiceImpl(system: ActorSystem, pubSub: PubSubRegistry)(implicit ec: ExecutionContext) extends HelloService { if (Cluster.get(system).selfRoles("worker-node")) { system.actorOf(Worker.props(pubSub), "worker") } val workerRouter = { val paths = List("/user/worker") val groupConf = ConsistentHashingGroup(paths, hashMapping = { case Job(_, task, _) => task }) val routerProps = ClusterRouterGroup( groupConf, ClusterRouterGroupSettings( totalInstances = 1000, routeesPaths = paths, allowLocalRoutees = true, useRoles = Set("worker-node") ) ).props system.actorOf(routerProps, "workerRouter") } override def submit(): ServiceCall[Job, JobAccepted] = ServiceCall { job => //Future{JobAccepted(job.jobId)} implicit val timeout = Timeout(5.seconds) (workerRouter ? job).mapTo[JobAccepted] } override def status(): ServiceCall[NotUsed, Source[JobStatus, NotUsed]] = ServiceCall { _ => val topic = pubSub.refFor(TopicId[JobStatus]("job-status")) Future.successful(topic.subscriber) } }
Example 16
Source File: ClientSpec.scala From twitter4s with Apache License 2.0 | 5 votes |
package com.danielasfregola.twitter4s.helpers import java.util.UUID import akka.actor.ActorSystem import akka.http.scaladsl.model._ import akka.pattern.ask import akka.stream.{KillSwitches, Materializer, SharedKillSwitch} import akka.util.Timeout import akka.util.Timeout.durationToTimeout import com.danielasfregola.twitter4s.entities.streaming.StreamingMessage import com.danielasfregola.twitter4s.http.clients.authentication.AuthenticationClient import com.danielasfregola.twitter4s.http.clients.rest.RestClient import com.danielasfregola.twitter4s.http.clients.streaming.StreamingClient import scala.concurrent.Future import scala.concurrent.duration.DurationInt trait ClientSpec extends Spec { abstract class AuthenticationClientSpecContext extends RequestDSL with SpecContext { protected val authenticationClient = new AuthenticationClient(consumerToken) { override def sendAndReceive[T](request: HttpRequest, f: HttpResponse => Future[T])( implicit system: ActorSystem, materializer: Materializer): Future[T] = { implicit val ec = materializer.executionContext implicit val timeout: Timeout = DurationInt(20) seconds val requestStartTime = System.currentTimeMillis val responseR: Future[HttpResponse] = (transport.ref ? request).map(_.asInstanceOf[HttpResponse]) for { response <- responseR t <- unmarshal[T](requestStartTime, f)(request, response, materializer) } yield t } } } abstract class RestClientSpecContext extends RequestDSL with SpecContext { protected val restClient = new RestClient(consumerToken, accessToken) { override def sendAndReceive[T](request: HttpRequest, f: HttpResponse => Future[T])( implicit system: ActorSystem, materializer: Materializer): Future[T] = { implicit val ec = materializer.executionContext implicit val timeout: Timeout = DurationInt(20) seconds val requestStartTime = System.currentTimeMillis val responseR: Future[HttpResponse] = (transport.ref ? request).map(_.asInstanceOf[HttpResponse]) for { response <- responseR t <- unmarshal[T](requestStartTime, f)(request, response, materializer) } yield t } } } abstract class StreamingClientSpecContext extends RequestDSL with SpecContext { def dummyProcessing: PartialFunction[StreamingMessage, Unit] = { case _ => } val killSwitch = KillSwitches.shared(s"test-twitter4s-${UUID.randomUUID}") protected val streamingClient = new StreamingClient(consumerToken, accessToken) { override def processStreamRequest[T <: StreamingMessage: Manifest]( request: HttpRequest )( f: PartialFunction[T, Unit], errorHandler: PartialFunction[Throwable, Unit] )( implicit system: ActorSystem, materializer: Materializer ): Future[SharedKillSwitch] = { implicit val ec = materializer.executionContext implicit val timeout: Timeout = DurationInt(20) seconds val responseR: Future[HttpResponse] = (transport.ref ? request).map(_.asInstanceOf[HttpResponse]) for { response <- responseR _ <- Future.successful(processBody(response, killSwitch)(f)(manifest[T], request, materializer)) } yield killSwitch } } } }
Example 17
Source File: Internal.scala From cave with MIT License | 5 votes |
package controllers import worker.Coordinator import akka.actor.Inbox import akka.pattern.ask import akka.util.Timeout import init.Init import play.api.libs.json.Json import play.api.mvc._ import scala.concurrent.duration._ object Internal extends Controller { def actorSystem = Init.system def coordinator = Init.coordinator def healthCheck() = Action { request => // TODO: add real checks Ok("healthy") } def status() = Action.async { request => implicit val inbox = Inbox.create(actorSystem) implicit val timeout = Timeout(3.seconds) import scala.concurrent.ExecutionContext.Implicits.global ask(coordinator, Coordinator.StatusRequest).mapTo[Coordinator.StatusResponse] map { response => Ok(Json.toJson(response)) } } }
Example 18
Source File: Internal.scala From cave with MIT License | 5 votes |
package controllers import actors.Coordinator import akka.actor.Inbox import akka.pattern.ask import akka.util.Timeout import init.Init import play.api.libs.json.Json import play.api.mvc._ import scala.concurrent.duration._ object Internal extends Controller { def actorSystem = Init.system def coordinator = Init.coordinator def healthCheck() = Action { request => // TODO: add real checks Ok("healthy") } def status() = Action.async { request => implicit val inbox = Inbox.create(actorSystem) implicit val timeout = Timeout(3.seconds) import scala.concurrent.ExecutionContext.Implicits.global ask(coordinator, Coordinator.StatusRequest).mapTo[Coordinator.StatusResponse] map { response => Ok(Json.toJson(response)) } } }
Example 19
Source File: Scheduler.scala From cave with MIT License | 5 votes |
package actors import java.util.concurrent.{Executor, TimeUnit} import akka.actor.{Actor, ActorLogging} import akka.pattern.ask import akka.util.Timeout import com.cave.metrics.data.evaluator.AlertParser import com.cave.metrics.data.{Check, Schedule} import init.{AwsWrapper, Init} import org.joda.time.format.ISODateTimeFormat import org.joda.time.{Minutes, LocalTime, DateTime, DateTimeZone} import scala.concurrent.ExecutionContext import scala.concurrent.duration._ object Scheduler { object DoWork object Die case class NotificationUrlChange(newUrl: String) } class Scheduler(schedule: Schedule, awsWrapper: AwsWrapper) extends Actor with ActorLogging with AlertParser { private[actors] def leader = Init.leader var notificationUrl: String = schedule.notificationUrl implicit val timeout = Timeout(2, TimeUnit.SECONDS) val (waitTime, period) = getSchedule(schedule.alert.period) val Formatter = ISODateTimeFormat.dateTimeNoMillis() implicit val executor = context.dispatcher.asInstanceOf[Executor with ExecutionContext] private val queueCheckSchedule = context.system.scheduler.schedule(waitTime, period, self, Scheduler.DoWork) override def receive = { case Scheduler.DoWork => leader ? Leadership.IsLeader onComplete { case scala.util.Success(imLeader: Boolean) => if (imLeader) { awsWrapper.sendMessage(Check(Schedule(schedule.orgName, schedule.teamName, schedule.clusterName, notificationUrl, schedule.alert), now())) } case scala.util.Success(e) => log.error("Unexpected result returned by the leader actor: " + e) case scala.util.Failure(t) => log.error("Failed to query the leader actor, error was " + t) } case Scheduler.NotificationUrlChange(url) => log.debug(s"Updating the notification URL, from $notificationUrl to $url.") notificationUrl = url case Scheduler.Die => context stop self } override def postStop(): Unit = queueCheckSchedule.cancel() private[actors] def getSchedule(alertPeriod: String): (FiniteDuration, FiniteDuration) = parseAll(duration, alertPeriod) match { case Success(p, _) => (0.minutes, p) case NoSuccess(_, message) => parseAll(daily, alertPeriod) match { case Success(time, _) => (getWait(nowLocal(), time), 1.day) case NoSuccess(_, message2) => sys.error(s"Unexpected alert period $alertPeriod. Not a duration ($message) and not a daily scheduler ($message2).") } } private[actors] def getWait(now: LocalTime, until: LocalTime): FiniteDuration = { val wait = Minutes.minutesBetween(now, until).getMinutes val minutes = if (wait < 0) 1440 + wait else wait minutes.minutes } }
Example 20
Source File: CoordinatorTests.scala From sparkplug with MIT License | 5 votes |
package springnz.sparkplug.client import akka.actor.{ ExtendedActorSystem, ActorRef, ActorSystem } import akka.pattern.ask import akka.testkit.{ ImplicitSender, TestKit } import akka.util.Timeout import com.typesafe.config.ConfigFactory import org.scalatest._ import springnz.sparkplug.executor.MessageTypes.{ JobFailure, JobRequest, JobSuccess, ShutDown } import scala.concurrent.Await import scala.concurrent.duration._ import scala.collection.JavaConverters._ class CoordinatorTests(_system: ActorSystem) extends TestKit(_system) with ImplicitSender with WordSpecLike with BeforeAndAfterAll with Matchers { def this() = this(ActorSystem(Constants.actorSystemName, ConfigFactory.parseMap(Map( "akka.remote.netty.tcp.port" -> new Integer(0)).asJava).withFallback(ClientExecutor.defaultClientAkkaConfig))) var coordinator: ActorRef = null "client coordinator" should { "successfuly execute a job request" in { val request = JobRequest("springnz.sparkplug.executor.LetterCountPlugin", None) coordinator ! request expectMsg[JobSuccess](30.seconds, JobSuccess(request, (2, 2))) } "successfuly execute a job request after a failure" in { val invalidRequest = JobRequest("springnz.sparkplug.executor.InvalidClass", None) coordinator ! invalidRequest expectMsgType[JobFailure](30.seconds) val goodRequest = JobRequest("springnz.sparkplug.executor.LetterCountPlugin", None) coordinator ! goodRequest expectMsg[JobSuccess](30.seconds, JobSuccess(goodRequest, (2, 2))) } "work with the ask pattern as well" in { implicit val timeout = Timeout(30.seconds) val request = JobRequest("springnz.sparkplug.executor.LetterCountPlugin", None) val replyFuture = coordinator ? request val result = Await.result(replyFuture, 30.seconds) result shouldBe JobSuccess(request, (2, 2)) } } override def beforeAll { val configSection = s"sparkplug.${springnz.sparkplug.executor.Constants.defaultAkkaRemoteConfigSection}" val port = system.asInstanceOf[ExtendedActorSystem].provider.getDefaultAddress.port.get val akkaClientConfig = ConfigFactory.parseMap(Map( "akka.remote.netty.tcp.port" -> new Integer(port)).asJava).withFallback(ClientExecutor.defaultClientAkkaConfig) coordinator = system.actorOf(Coordinator.props(None, akkaRemoteConfig = Some(ConfigFactory.load.getConfig(configSection)), akkaClientConfig = akkaClientConfig), "TestCoordinator") } override def afterAll { system.actorSelection(s"/user/TestCoordinator") ! ShutDown TestKit.shutdownActorSystem(system, verifySystemShutdown = true) } }
Example 21
Source File: BankAccountTest.scala From reactive-programming with Apache License 2.0 | 5 votes |
package com.test.week5 import akka.actor.Status.Failure import akka.actor.{ Actor, ActorRef, Props } import akka.event.LoggingReceive import akka.pattern.ask import com.test.TestSpec class BankAccountTest extends TestSpec { "Actors" should "know itself" in { val ref: ActorRef = system.actorOf(Props(new Actor { override def receive: Receive = { case _ ⇒ sender() ! self } })) (ref ? "").futureValue shouldBe ref cleanup(ref) } it should "count" in { val ref: ActorRef = system.actorOf(Props(new Actor { def count(num: Int): Receive = LoggingReceive { case _ ⇒ context.become(count(num + 1)) sender() ! num } override def receive: Receive = LoggingReceive(count(0)) })) (ref ? "").futureValue shouldBe 0 (ref ? "").futureValue shouldBe 1 (ref ? "").futureValue shouldBe 2 (ref ? "").futureValue shouldBe 3 cleanup(ref) } it should "be a BankAccount" in { object BankAccount { case class Transfer(from: ActorRef, to: ActorRef, amount: BigInt) case class Deposit(amount: BigInt) case class Withdraw(amount: BigInt) case object Info case class Done(amount: BigInt) case object Failed } class BankAccount extends Actor { import BankAccount._ var balance: BigInt = BigInt(0) override def receive: Receive = LoggingReceive { case Deposit(amount) ⇒ balance += amount sender() ! Done(balance) case Withdraw(amount) ⇒ balance -= amount sender() ! Done(balance) case Info ⇒ sender() ! Done(balance) case _ ⇒ sender() ! Failure } } import BankAccount._ val account1 = system.actorOf(Props(new BankAccount)) (account1 ? Info).futureValue shouldBe Done(0) (account1 ? Deposit(100)).futureValue shouldBe Done(100) (account1 ? Deposit(100)).futureValue shouldBe Done(200) val account2 = system.actorOf(Props(new BankAccount)) val tom = system.actorOf(Props(new Actor { def awaitDeposit(client: ActorRef): Receive = LoggingReceive { case Done(amount) ⇒ client ! Done(amount) context.stop(self) } def awaitWithdraw(to: ActorRef, amount: BigInt, client: ActorRef): Receive = LoggingReceive { case Done(_) ⇒ to ! Deposit(amount) context.become(awaitDeposit(client)) case Failed ⇒ client ! Failed context.stop(self) } override def receive = { case Transfer(from, to, amount) ⇒ from ! Withdraw(amount) context.become(awaitWithdraw(to, amount, sender())) } })) (tom ? Transfer(account1, account2, 50)).futureValue shouldBe Done(50) (account1 ? Info).futureValue shouldBe Done(150) (account2 ? Info).futureValue shouldBe Done(50) cleanup(account1, account2, tom) } }
Example 22
Source File: SupervisionTest.scala From reactive-programming with Apache License 2.0 | 5 votes |
package com.test.week6 import akka.actor._ import akka.event.LoggingReceive import akka.pattern.ask import akka.testkit.TestProbe import com.test.TestSpec import scala.concurrent.duration._ class SupervisionTest extends TestSpec { case class Command(f: () ⇒ Unit) case object Count case object GetState case class CounterState(counter: Long) class Supervisor(tp: TestProbe, svs: SupervisorStrategy) extends Actor { val worker: ActorRef = context.actorOf(Props(new Actor with ActorLogging { var counter = 0L override def receive: Receive = LoggingReceive { case Command(f) ⇒ f() case Count ⇒ counter += 1 case GetState ⇒ sender() ! CounterState(counter) } override def preStart(): Unit = log.debug("Started") override def postStop(): Unit = log.debug("Stopped") }), "worker") tp watch worker override def receive = LoggingReceive { case msg ⇒ worker forward msg } override def supervisorStrategy: SupervisorStrategy = svs } def createSupervisor(tp: TestProbe)(svs: SupervisorStrategy) = system.actorOf(Props(new Supervisor(tp, svs)), s"sup-${randomId.take(3)}") "SupervisorStrategy" should "resume the worker, state should not change, so should be 1" in { val tp = probe val sup = createSupervisor(tp) { OneForOneStrategy() { case t: RuntimeException ⇒ SupervisorStrategy.Resume } } sup ! Count (sup ? GetState).futureValue shouldBe CounterState(1L) sup ! Command(() ⇒ throw new RuntimeException("resume")) (sup ? GetState).futureValue shouldBe CounterState(1L) tp.expectNoMsg(100.millis) // no Terminated message cleanup(sup) } it should "restart the worker, so the worker instance has been replaced, and state should be 0 again" in { val tp = probe val sup = createSupervisor(tp) { OneForOneStrategy() { case t: RuntimeException ⇒ SupervisorStrategy.Restart } } sup ! Count (sup ? GetState).futureValue shouldBe CounterState(1L) sup ! Command(() ⇒ throw new RuntimeException("restart")) (sup ? GetState).futureValue shouldBe CounterState(0L) tp.expectNoMsg(100.millis) // no Terminated message cleanup(sup) } it should "stop the worker, so worker in not there anymore and should not answer" in { val tp = probe val sup = createSupervisor(tp) { OneForOneStrategy() { case t: RuntimeException ⇒ SupervisorStrategy.Stop } } sup ! Command(() ⇒ throw new RuntimeException("stop")) tp.expectMsgPF[Unit](100.millis) { case Terminated(_) ⇒ } cleanup(sup) } }
Example 23
Source File: ExplainApi.scala From iep-apps with Apache License 2.0 | 5 votes |
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 24
Source File: RestServiceActors.scala From kafka-with-akka-streams-kafka-streams-tutorial with Apache License 2.0 | 5 votes |
package com.lightbend.scala.akkastream.queryablestate.actors import akka.http.scaladsl.Http import akka.http.scaladsl.server.Directives.{complete, get, onSuccess, path} import akka.actor.{ActorRef, ActorSystem} import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Route import akka.pattern.ask import akka.stream.ActorMaterializer import akka.util.Timeout import com.lightbend.scala.akkastream.modelserver.actors.{GetModels, GetModelsResult, GetState} import com.lightbend.scala.modelServer.model.ModelToServeStats import de.heikoseeberger.akkahttpjackson.JacksonSupport import scala.concurrent.ExecutionContextExecutor import scala.concurrent.duration._ object RestServiceActors { // See http://localhost:5500/models // Then select a model shown and try http://localhost:5500/state/<model>, e.g., http://localhost:5500/state/wine def startRest(modelserver: ActorRef)(implicit system: ActorSystem, materializer: ActorMaterializer): Unit = { implicit val executionContext: ExecutionContextExecutor = system.dispatcher // Use with HTTP methods that accept an implicit timeout argument // implicit val timeout = Timeout(10.seconds) val host = "127.0.0.1" val port = 5500 val routes: Route = QueriesAkkaHttpResource.storeRoutes(modelserver) Http().bindAndHandle(routes, host, port) map { binding => println(s"Starting models observer on port ${binding.localAddress}") } recover { case ex => println(s"Models observer could not bind to $host:$port - ${ex.getMessage}") } } } object QueriesAkkaHttpResource extends JacksonSupport { implicit val askTimeout: Timeout = Timeout(30.seconds) def storeRoutes(modelserver: ActorRef): Route = get { path("state"/Segment) { datatype => onSuccess(modelserver ? GetState(datatype)) { case info: ModelToServeStats => complete(info) } } ~ path("models") { onSuccess(modelserver ? GetModels()) { case models: GetModelsResult => complete(models) } } } }
Example 25
Source File: BootstrapTestApp.scala From scalajs-bootstrap with MIT License | 5 votes |
package com.karasiq.bootstrap.test.backend import java.util.concurrent.TimeUnit import scala.concurrent.Await import scala.concurrent.duration._ import scala.language.postfixOps import akka.actor._ import akka.io.IO import akka.pattern.ask import akka.util.Timeout import spray.can.Http import spray.http._ import spray.routing.HttpService import com.karasiq.bootstrap.test.frontend.TestHtmlPage object BootstrapTestApp extends App { final class AppHandler extends Actor with HttpService { override def receive: Actor.Receive = runRoute { get { { // Server-rendered page path("serverside.html") { complete(HttpResponse(entity = HttpEntity(ContentType(MediaTypes.`text/html`), TestHtmlPage()))) } ~ // Index page (pathSingleSlash & respondWithMediaType(MediaTypes.`text/html`)) { getFromResource("webapp/index.html") } ~ // Other resources getFromResourceDirectory("webapp") } } } override def actorRefFactory: ActorRefFactory = context } def startup(): Unit = { implicit val timeout = Timeout(20 seconds) implicit val actorSystem = ActorSystem("bootstrap-test") Runtime.getRuntime.addShutdownHook(new Thread(new Runnable { override def run(): Unit = { Await.result(actorSystem.terminate(), FiniteDuration(5, TimeUnit.MINUTES)) } })) val service = actorSystem.actorOf(Props[AppHandler], "webService") IO(Http) ? Http.Bind(service, interface = "localhost", port = 9000) } startup() }
Example 26
Source File: TransformationFrontend.scala From fusion-data with Apache License 2.0 | 5 votes |
package sample.cluster.transformation import java.util.concurrent.TimeUnit import java.util.concurrent.atomic.AtomicInteger import akka.actor.{ Actor, ActorRef, ActorSystem, Props, Terminated } import akka.pattern.ask import akka.util.Timeout import com.typesafe.config.ConfigFactory import scala.concurrent.ExecutionContext.Implicits import scala.concurrent.Future import scala.concurrent.duration._ import scala.language.postfixOps //#frontend class TransformationFrontend extends Actor { var backends = IndexedSeq.empty[ActorRef] var jobCounter = 0 def receive = { case job: TransformationJob if backends.isEmpty => sender() ! JobFailed("Service unavailable, try again later", job) case job: TransformationJob => jobCounter += 1 backends(jobCounter % backends.size) forward job case BackendRegistration if !backends.contains(sender()) => context watch sender() backends = backends :+ sender() case Terminated(a) => backends = backends.filterNot(_ == a) } } //#frontend object TransformationFrontend { def main(args: Array[String]): Unit = { // Override the configuration of the port when specified as program argument val port = if (args.isEmpty) "0" else args(0) val config = ConfigFactory .parseString(s""" akka.remote.netty.tcp.port=$port akka.remote.artery.canonical.port=$port """) .withFallback(ConfigFactory.parseString("akka.cluster.roles = [frontend]")) .withFallback(ConfigFactory.load("simple-cluster")) val system = ActorSystem("ClusterSystem", config) val frontend = system.actorOf(Props[TransformationFrontend], name = "frontend") val counter = new AtomicInteger import system.dispatcher system.scheduler.schedule(2.seconds, 2.seconds) { implicit val timeout = Timeout(5 seconds) (frontend ? TransformationJob("hello-" + counter.incrementAndGet())) foreach { case result => println(result) } } Future { TimeUnit.SECONDS.sleep(80) system.terminate() }(Implicits.global) } }
Example 27
Source File: PauseHandler.scala From subsearch with GNU General Public License v2.0 | 5 votes |
package com.gilazaria.subsearch.core.subdomainscanner import com.gilazaria.subsearch.core.subdomainscanner.DispatcherMessage.{PauseScanning, ResumeScanning} import com.gilazaria.subsearch.output.Logger import com.gilazaria.subsearch.utils.TimeUtils import akka.actor.ActorRef import akka.pattern.ask import scala.concurrent.ExecutionContext.Implicits.global import sun.misc.{Signal, SignalHandler} import scala.concurrent.Await object PauseHandler { def create(dispatcher: ActorRef, logger: Logger): PauseHandler = new PauseHandler(List("INT"), dispatcher, logger) case class InterruptException(msg: String) extends Exception(msg) case class ContinueException(msg: String) extends Exception(msg) } class PauseHandler(signalNames: List[String], dispatcher: ActorRef, logger: Logger) extends SignalHandler { import PauseHandler.{InterruptException, ContinueException} private val signalMap = signalNames.map(name => (name, Signal.handle(new Signal(name), this))).toMap private var pausingCalled: Boolean = false override def handle(signal: Signal) = { if (pausingCalled) forceExit() else pausingCalled = true implicit val timeout = TimeUtils.akkaAskTimeout Await.result(dispatcher ? PauseScanning, TimeUtils.awaitDuration) try { while (true) { logger.logPauseOptions() val option: String = System.console.readLine().toLowerCase if (option == "e") throw new InterruptException("Exited the program.") else if (option == "c") throw new ContinueException("Continuing the scan.") else logger.logInvalidPauseOption() } } catch { case InterruptException(msg) => exit() case ContinueException(msg) => resume() } } private def forceExit() = { logger.logScanForceCancelled() System.exit(0) } private def exit() = { logger.logScanCancelled() logger.completedLoggingFuture.andThen { case _ => System.exit(0) } } private def resume() = { dispatcher ! ResumeScanning pausingCalled = false } }
Example 28
Source File: SubdomainScanner.scala From subsearch with GNU General Public License v2.0 | 5 votes |
package com.gilazaria.subsearch.core.subdomainscanner import com.gilazaria.subsearch.core.subdomainscanner.DispatcherMessage.NotifyOnCompletion import com.gilazaria.subsearch.output.Logger import com.gilazaria.subsearch.utils.{File, TimeUtils} import akka.actor.ActorSystem import akka.pattern.ask import scala.concurrent.{ExecutionContext, Future} case class SubdomainScannerArguments(hostname: String, wordlist: File, omitSubdomains: List[String], prioritySubdomains: List[String], resolvers: List[String], threads: Int, concurrentResolverRequests: Boolean) object SubdomainScanner { def performScan(arguments: SubdomainScannerArguments, logger: Logger)(implicit ec: ExecutionContext): Future[Unit] = { logger.logStartedSubdomainSearch() val scanner: SubdomainScanner = new SubdomainScanner(arguments, logger) scanner.future.map(_ => None) } } class SubdomainScanner(arguments: SubdomainScannerArguments, logger: Logger)(implicit ec: ExecutionContext) { val system = ActorSystem("SubdomainScanner") val listener = system.actorOf(Listener.props(logger), "listener") val dispatcher = system.actorOf(Dispatcher.props(arguments, listener), "dispatcher") implicit val timeout = TimeUtils.akkaAskTimeout val future: Future[Any] = dispatcher ? NotifyOnCompletion private val pauseHandler = PauseHandler.create(dispatcher, logger) }
Example 29
Source File: OffsetLoader.scala From toketi-iothubreact with MIT License | 5 votes |
package com.microsoft.azure.iot.iothubreact.checkpointing import akka.util.Timeout import com.microsoft.azure.iot.iothubreact.{Logger, Retry} import com.microsoft.azure.iot.iothubreact.checkpointing.CheckpointService.GetOffset import com.microsoft.azure.iot.iothubreact.config.IConfiguration import com.microsoft.azure.iot.iothubreact.scaladsl.IoTHubPartition import scala.concurrent.Await trait IOffsetLoader { private[iothubreact] def GetSavedOffset(partition: Int): Option[String] private[iothubreact] def GetSavedOffsets: Map[Int, String] } class OffsetLoader(config: IConfiguration) extends IOffsetLoader with Logger { private[iothubreact] def GetSavedOffset(partition: Int): Option[String] = { import scala.language.postfixOps import scala.concurrent.duration._ import akka.pattern.ask val partitionCp = CheckpointActorSystem(config.checkpointing).getCheckpointService(partition) implicit val rwTimeout = Timeout(config.checkpointing.checkpointRWTimeout) try { Retry(3, 5 seconds) { log.debug("Loading the stream offset for partition {}", partition) val future = (partitionCp ? GetOffset).mapTo[String] val offset = Await.result(future, rwTimeout.duration) if (offset != IoTHubPartition.OffsetCheckpointNotFound) Some(offset) else None } } catch { case e: java.util.concurrent.TimeoutException ⇒ log.error(e, "Timeout while retrieving the offset from the storage") throw e case e: Exception ⇒ log.error(e, e.getMessage) throw e } } private[iothubreact] def GetSavedOffsets: Map[Int, String] = { (0 to config.connect.iotHubPartitions).flatMap { p ⇒ GetSavedOffset(p).map { o ⇒ p → o } }(collection.breakOut) } }
Example 30
Source File: EventSourcedSupportFactory.scala From cloudstate with Apache License 2.0 | 5 votes |
package io.cloudstate.proxy.eventsourced import akka.NotUsed import akka.actor.{ActorRef, ActorSystem} import akka.cluster.sharding.ShardRegion.HashCodeMessageExtractor import akka.cluster.sharding.{ClusterSharding, ClusterShardingSettings} import akka.event.Logging import akka.grpc.GrpcClientSettings import akka.stream.Materializer import akka.stream.scaladsl.{Flow, Source} import akka.util.Timeout import com.google.protobuf.Descriptors.ServiceDescriptor import io.cloudstate.protocol.entity.{Entity, Metadata} import io.cloudstate.protocol.event_sourced.EventSourcedClient import io.cloudstate.proxy._ import io.cloudstate.proxy.entity.{EntityCommand, UserFunctionReply} import scala.concurrent.{ExecutionContext, Future} import scala.collection.JavaConverters._ class EventSourcedSupportFactory(system: ActorSystem, config: EntityDiscoveryManager.Configuration, grpcClientSettings: GrpcClientSettings, concurrencyEnforcer: ActorRef, statsCollector: ActorRef)(implicit ec: ExecutionContext, mat: Materializer) extends EntityTypeSupportFactory { private final val log = Logging.getLogger(system, this.getClass) private val eventSourcedClient = EventSourcedClient(grpcClientSettings)(system) override def buildEntityTypeSupport(entity: Entity, serviceDescriptor: ServiceDescriptor, methodDescriptors: Map[String, EntityMethodDescriptor]): EntityTypeSupport = { validate(serviceDescriptor, methodDescriptors) val stateManagerConfig = EventSourcedEntity.Configuration(entity.serviceName, entity.persistenceId, config.passivationTimeout, config.relayOutputBufferSize) log.debug("Starting EventSourcedEntity for {}", entity.persistenceId) val clusterSharding = ClusterSharding(system) val clusterShardingSettings = ClusterShardingSettings(system) val eventSourcedEntity = clusterSharding.start( typeName = entity.persistenceId, entityProps = EventSourcedEntitySupervisor.props(eventSourcedClient, stateManagerConfig, concurrencyEnforcer, statsCollector), settings = clusterShardingSettings, messageExtractor = new EntityIdExtractor(config.numberOfShards), allocationStrategy = new DynamicLeastShardAllocationStrategy(1, 10, 2, 0.0), handOffStopMessage = EventSourcedEntity.Stop ) new EventSourcedSupport(eventSourcedEntity, config.proxyParallelism, config.relayTimeout) } private def validate(serviceDescriptor: ServiceDescriptor, methodDescriptors: Map[String, EntityMethodDescriptor]): Unit = { val streamedMethods = methodDescriptors.values.filter(m => m.method.toProto.getClientStreaming || m.method.toProto.getServerStreaming) if (streamedMethods.nonEmpty) { val offendingMethods = streamedMethods.map(_.method.getName).mkString(",") throw EntityDiscoveryException( s"Event sourced entities do not support streamed methods, but ${serviceDescriptor.getFullName} has the following streamed methods: ${offendingMethods}" ) } val methodsWithoutKeys = methodDescriptors.values.filter(_.keyFieldsCount < 1) if (methodsWithoutKeys.nonEmpty) { val offendingMethods = methodsWithoutKeys.map(_.method.getName).mkString(",") throw new EntityDiscoveryException( s"Event sourced entities do not support methods whose parameters do not have at least one field marked as entity_key, " + "but ${serviceDescriptor.getFullName} has the following methods without keys: ${offendingMethods}" ) } } } private class EventSourcedSupport(eventSourcedEntity: ActorRef, parallelism: Int, private implicit val relayTimeout: Timeout) extends EntityTypeSupport { import akka.pattern.ask override def handler(method: EntityMethodDescriptor, metadata: Metadata): Flow[EntityCommand, UserFunctionReply, NotUsed] = Flow[EntityCommand].mapAsync(parallelism)( command => (eventSourcedEntity ? EntityTypeSupport.mergeStreamLevelMetadata(metadata, command)) .mapTo[UserFunctionReply] ) override def handleUnary(command: EntityCommand): Future[UserFunctionReply] = (eventSourcedEntity ? command).mapTo[UserFunctionReply] } private final class EntityIdExtractor(shards: Int) extends HashCodeMessageExtractor(shards) { override final def entityId(message: Any): String = message match { case command: EntityCommand => command.entityId } }
Example 31
Source File: Main.scala From quiz-management-service with Apache License 2.0 | 5 votes |
package com.danielasfregola.quiz.management import akka.actor._ import akka.io.IO import akka.pattern.ask import akka.util.Timeout import com.typesafe.config.ConfigFactory import spray.can.Http import scala.concurrent.duration._ object Main extends App { val config = ConfigFactory.load() val host = config.getString("http.host") val port = config.getInt("http.port") implicit val system = ActorSystem("quiz-management-service") val api = system.actorOf(Props(new RestInterface()), "httpInterface") implicit val executionContext = system.dispatcher implicit val timeout = Timeout(10 seconds) IO(Http).ask(Http.Bind(listener = api, interface = host, port = port)) .mapTo[Http.Event] .map { case Http.Bound(address) => println(s"REST interface bound to $address") case Http.CommandFailed(cmd) => println("REST interface could not bind to " + s"$host:$port, ${cmd.failureMessage}") system.shutdown() } }
Example 32
Source File: Main.scala From quiz-management-service with Apache License 2.0 | 5 votes |
package com.danielasfregola.quiz.management import akka.actor._ import akka.io.IO import akka.pattern.ask import akka.util.Timeout import com.typesafe.config.ConfigFactory import spray.can.Http import scala.concurrent.duration._ object Main extends App { val config = ConfigFactory.load() val host = config.getString("http.host") val port = config.getInt("http.port") implicit val system = ActorSystem("quiz-management-service") val api = system.actorOf(Props(new RestInterface()), "httpInterface") implicit val executionContext = system.dispatcher implicit val timeout = Timeout(10 seconds) IO(Http).ask(Http.Bind(listener = api, interface = host, port = port)) .mapTo[Http.Event] .map { case Http.Bound(address) => println(s"REST interface bound to $address") case Http.CommandFailed(cmd) => println("REST interface could not bind to " + s"$host:$port, ${cmd.failureMessage}") system.shutdown() } }
Example 33
Source File: Main.scala From quiz-management-service with Apache License 2.0 | 5 votes |
package com.danielasfregola.quiz.management import akka.actor._ import akka.io.IO import akka.pattern.ask import akka.util.Timeout import com.typesafe.config.ConfigFactory import spray.can.Http import scala.concurrent.duration._ object Main extends App { val config = ConfigFactory.load() val host = config.getString("http.host") val port = config.getInt("http.port") implicit val system = ActorSystem("quiz-management-service") implicit val executionContext = system.dispatcher implicit val timeout = Timeout(10 seconds) val api = system.actorOf(Props(new RestInterface)) IO(Http).ask(Http.Bind(listener = api, interface = host, port = port)) .mapTo[Http.Event] .map { case Http.Bound(address) => println(s"REST interface bound to $address") case Http.CommandFailed(cmd) => println("REST interface could not bind to " + s"$host:$port, ${cmd.failureMessage}") system.shutdown() } }
Example 34
Source File: Main.scala From quiz-management-service with Apache License 2.0 | 5 votes |
package com.danielasfregola.quiz.management import akka.actor._ import akka.io.IO import akka.pattern.ask import akka.util.Timeout import com.typesafe.config.ConfigFactory import spray.can.Http import scala.concurrent.duration._ object Main extends App { val config = ConfigFactory.load() val host = config.getString("http.host") val port = config.getInt("http.port") implicit val system = ActorSystem("quiz-management-service") val api = system.actorOf(Props(new RestInterface()), "httpInterface") implicit val executionContext = system.dispatcher implicit val timeout = Timeout(10 seconds) IO(Http).ask(Http.Bind(listener = api, interface = host, port = port)) .mapTo[Http.Event] .map { case Http.Bound(address) => println(s"REST interface bound to $address") case Http.CommandFailed(cmd) => println("REST interface could not bind to " + s"$host:$port, ${cmd.failureMessage}") system.shutdown() } }
Example 35
Source File: KubeServiceRegistryImpl.scala From lagom-on-kube with Apache License 2.0 | 5 votes |
package me.alexray.lagom.kube.discovery.impl import java.net.URI import java.util.concurrent.TimeUnit import javax.inject.{Inject, Named} import akka.NotUsed import akka.actor.ActorRef import akka.pattern.ask import akka.util.Timeout import com.lightbend.lagom.internal.javadsl.registry.{RegisteredService, ServiceRegistry, ServiceRegistryService} import com.lightbend.lagom.javadsl.api.ServiceCall import com.lightbend.lagom.javadsl.api.transport.NotFound import org.pcollections.PSequence import play.api.Logger import scala.concurrent.Future import scala.concurrent.duration.Duration import me.alexray.lagom.kube.discovery.KubeServiceRegistryActor import scala.language.implicitConversions class KubeServiceRegistryImpl @Inject() (@Named(KubeServiceRegistryModule.KUBE_SERVICE_REGISTRY_ACTOR) registry: ActorRef) extends ServiceRegistry { import me.alexray.lagom.converters.ServiceCallConverter._ private val logger: Logger = Logger(this.getClass) implicit val timeout = Timeout(Duration.create(5, TimeUnit.SECONDS)) import scala.concurrent.ExecutionContext.Implicits.global override def register(name: String): ServiceCall[ServiceRegistryService, NotUsed] = (service: ServiceRegistryService) => { logger.debug("register invoked, name=[" + name + "], request=[" + service + "]") (registry ? KubeServiceRegistryActor.Register(name, service)).map(_ => NotUsed) } override def unregister(name: String): ServiceCall[NotUsed, NotUsed] = (request: NotUsed) => { logger.debug("unregister invoked, name=[" + name + "], request=[" + request + "]") registry ! KubeServiceRegistryActor.Remove(name) Future.successful(NotUsed) } override def lookup(name: String): ServiceCall[NotUsed, URI] = (request: NotUsed) => { logger.debug("locate invoked, name=[" + name + "], request=[" + request + "]") (registry ? KubeServiceRegistryActor.Lookup(name)).mapTo[Option[URI]].map { case Some(uri) => logger.debug("Location of service name=[" + name + "] is " + uri) uri case None => logger.debug("Service name=[" + name + "] has not been registered") throw new NotFound(name) } } override def registeredServices(): ServiceCall[NotUsed, PSequence[RegisteredService]] = (request: NotUsed) => { (registry ? KubeServiceRegistryActor.GetRegisteredServices).mapTo[KubeServiceRegistryActor.RegisteredServices].map(_.services) } }
Example 36
Source File: HTTPServer.scala From ForestFlow with Apache License 2.0 | 5 votes |
package ai.forestflow.serving.restapi import ai.forestflow.serving.config.ApplicationEnvironment import akka.Done import akka.actor.CoordinatedShutdown.{PhaseServiceUnbind, Reason} import akka.actor.SupervisorStrategy._ import akka.actor.{ActorRef, ActorSystem, CoordinatedShutdown} import akka.cluster.Cluster import akka.http.scaladsl.Http import akka.http.scaladsl.server.Route import akka.pattern.ask import akka.stream.ActorMaterializer import akka.util.Timeout import ai.forestflow.akka.Supervisor import ai.forestflow.domain.ServableRoutes.GetProxyRoutes import ai.forestflow.utils.ThrowableImplicits._ import scala.concurrent.Await import scala.concurrent.duration._ import scala.language.postfixOps import scala.util.{Failure, Success} //noinspection TypeAnnotation object HTTPServer { private final case object BindFailure extends Reason } //noinspection TypeAnnotation class HTTPServer(servableProxyRef: ActorRef)(implicit system: ActorSystem, cluster: Cluster, shutdown: CoordinatedShutdown) { import HTTPServer._ import system.log private implicit val materializer = ActorMaterializer() private implicit val executionContext = system.dispatcher implicit lazy val timeout: Timeout = Timeout(ApplicationEnvironment.HTTP_COMMAND_TIMEOUT_SECS seconds) private val address = ApplicationEnvironment.HTTP_BIND_ADDRESS private val port = ApplicationEnvironment.HTTP_PORT val routesSupervisor = system.actorOf(Supervisor.props { case _: ArithmeticException => Resume case _: Exception => Restart }) private val servableRoutesActor = Await.result( routesSupervisor .ask(ServableRoutes.props(servableProxyRef)) .mapTo[ActorRef], ApplicationEnvironment.HTTP_COMMAND_TIMEOUT_SECS second) servableRoutesActor.ask(GetProxyRoutes()).onComplete { case Success(r: Route) => val bindingFuture = Http().bindAndHandle(r, address, port) bindingFuture.onComplete { case Success(bound) => log.info(s"AKKA HTTP Server online at http://${bound.localAddress.getHostString}:${bound.localAddress.getPort}/") shutdown.addTask(PhaseServiceUnbind, "api.unbind") { () => bound.terminate(5 seconds).map(_ => Done) } case Failure(e) => log.error(s"AKKA HTTP Server could not start! Shutting down... ${e.printableStackTrace}") shutdown.run(BindFailure) } case Failure(e) => log.error(s"Couldn't get dynamic HTTP routes from ServableRoutes actor! Shutting down... ${e.printableStackTrace}") shutdown.run(BindFailure) } }
Example 37
Source File: BlogRestApi.scala From akka-blog-example with Apache License 2.0 | 5 votes |
package com.spr.blog import akka.http.scaladsl.model.StatusCodes import akka.http.scaladsl.server._ import akka.pattern.ask import com.spr.akka.{AkkaConfiguration, RestApi} import scala.concurrent.Future trait BlogRestApi extends RestApi with BlogService { override def route: Route = pathPrefix("api" / "blog") { (pathEndOrSingleSlash & post) { // POST /api/blog/ entity(as[PostContent]) { content => onSuccess(addPost(content)) { added => complete((StatusCodes.Created, added)) } } } ~ pathPrefix(JavaUUID.map(PostId(_))) { id => pathEndOrSingleSlash { get { // GET /api/blog/:id onSuccess(getPost(id)) { case Right(content) => complete((StatusCodes.OK, content)) case Left(error) => complete((StatusCodes.NotFound, error)) } } ~ put { // PUT /api/blog/:id entity(as[PostContent]) { content => onSuccess(updatePost(id, content)) { case Right(updated) => complete((StatusCodes.OK, updated)) case Left(error) => complete((StatusCodes.NotFound, error)) } } } } } } }
Example 38
Source File: HatServerProvider.scala From HAT2.0 with GNU Affero General Public License v3.0 | 5 votes |
package org.hatdex.hat.resourceManagement import java.io.StringWriter import java.security.interfaces.RSAPublicKey import javax.inject.{ Inject, Named, Singleton } import akka.actor.ActorRef import akka.pattern.ask import akka.util.Timeout import com.mohiva.play.silhouette.api.services.DynamicEnvironmentProviderService import org.bouncycastle.util.io.pem.{ PemObject, PemWriter } import org.hatdex.hat.api.service.RemoteExecutionContext import org.hatdex.hat.resourceManagement.actors.HatServerProviderActor import org.hatdex.hat.utils.LoggingProvider import play.api.cache.{ AsyncCacheApi, NamedCache } import play.api.Configuration import play.api.mvc.Request import scala.concurrent.Future import scala.concurrent.duration._ trait HatServerProvider extends DynamicEnvironmentProviderService[HatServer] { def retrieve[B](request: Request[B]): Future[Option[HatServer]] def retrieve(hatAddress: String): Future[Option[HatServer]] def toString(publicKey: RSAPublicKey): String = { val pemObject = new PemObject("PUBLIC KEY", publicKey.getEncoded) val stringPemWriter = new StringWriter() val pemWriter: PemWriter = new PemWriter(stringPemWriter) pemWriter.writeObject(pemObject) pemWriter.flush() val pemPublicKey = stringPemWriter.toString pemPublicKey } } @Singleton class HatServerProviderImpl @Inject() ( configuration: Configuration, @NamedCache("hatserver-cache") cache: AsyncCacheApi, loggingProvider: LoggingProvider, @Named("hatServerProviderActor") serverProviderActor: ActorRef)( implicit val ec: RemoteExecutionContext) extends HatServerProvider { private val logger = loggingProvider.logger(this.getClass) def retrieve[B](request: Request[B]): Future[Option[HatServer]] = { val hatAddress = request.host //.split(':').headOption.getOrElse(request.host) retrieve(hatAddress) } implicit val timeout: Timeout = configuration.get[FiniteDuration]("resourceManagement.serverProvisioningTimeout") implicit val serverInfoTimeout: Duration = configuration.get[FiniteDuration]("resourceManagement.serverIdleTimeout") def retrieve(hatAddress: String): Future[Option[HatServer]] = { cache.get[HatServer](s"server:$hatAddress") .flatMap { case Some(server) => Future.successful(Some(server)) case _ => (serverProviderActor ? HatServerProviderActor.HatServerRetrieve(hatAddress)) map { case server: HatServer => logger.debug(s"Got back server $server") cache.set(s"server:$hatAddress", server, serverInfoTimeout) Some(server) case error: HatServerDiscoveryException => logger.warn(s"Got back error $error") throw error case message => logger.warn(s"Unknown message $message from HAT Server provider actor") val error = new HatServerDiscoveryException("Unknown message") throw error } recoverWith { case e => logger.warn(s"Error while retrieving HAT $hatAddress info: ${e.getMessage}") val error = new HatServerDiscoveryException("HAT Server info retrieval failed", e) throw error } } } }
Example 39
Source File: Bench.scala From akka-nbench with Apache License 2.0 | 5 votes |
package bench import akka.actor._ import akka.pattern.ask import akka.util.Timeout import scala.concurrent.duration._ import scala.reflect.runtime.universe._ import scala.concurrent.Await import com.typesafe.config._ import net.ceedubs.ficus.Ficus._ import java.util.Properties import java.nio.file._ import java.util.Date import java.text.SimpleDateFormat import java.util.Date import Tapper._ object Bench extends App { def prepareOutputDirs(): String = { val csvDateTimeDir = FileSystems.getDefault().getPath( "tests/" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date())) Files.createDirectories(csvDateTimeDir) val csvSymlink = FileSystems.getDefault().getPath("tests/current") if(Files.isSymbolicLink(csvSymlink)){ Files.delete(csvSymlink) } else if (Files.exists(csvSymlink)) { throw new NotASymbolicLinkException(s"test/current is not a symbolic link. Path: $csvSymlink") } Files.createSymbolicLink(csvSymlink, csvDateTimeDir.toAbsolutePath) csvDateTimeDir.toAbsolutePath.toString } def parseOptions(): String = { val usage = """ Usage: activator -mem 4096 "run-main bench.Bench scenario_name" """ if (args.length != 1) println(usage) return args(0) } val scenario = parseOptions val config = ConfigFactory.load().getConfig(scenario) val duration = config.getInt("duration") val concurrent = config.getInt("concurrent") val csvDateTimeDir = prepareOutputDirs val system = ActorSystem("bench") val actorProps = Props(classOf[StatsCollector], csvDateTimeDir, config) val statsCollector = system.actorOf(actorProps, name = "statscollector") val operationsWithRatio: Map[String, Int] = config.as[Map[String, Int]]("operations") val numer = operationsWithRatio.values.sum if (concurrent < numer){ val msg = s"concurrent($concurrent) must greater than sum of operations ratio($numer)" System.err.println(msg) throw new ApplicationConfigException(msg) } val operations = for((key, value) <- operationsWithRatio) yield { List.range(0, concurrent * operationsWithRatio(key) / numer).map(_ => key) } implicit val timeout = Timeout(duration * 2, SECONDS) var driverClz = Class.forName(config.getString("driver")) val drivers = operations.flatten.zipWithIndex.map{ case (operation, i) => system.actorOf(Props(driverClz, operation, statsCollector, config).withDispatcher("my-dispatcher"), name = s"driver_$i") } drivers.par.map(actor => actor ? Ready()).foreach{ f => Await.result(f, timeout.duration).asInstanceOf[OK] } val startAt = new Date() val doUntil = new Date(startAt.getTime + duration * 1000) drivers.par.map(actor => actor ? Go(doUntil)).foreach { f => Await.result(f, timeout.duration).asInstanceOf[OK] } (statsCollector ? TearDown()).tap { f => Await.result(f, timeout.duration).asInstanceOf[OK] } drivers.par.map(actor => actor ? TearDown()).foreach { f => Await.result(f, timeout.duration).asInstanceOf[OK] } (drivers.head ? TearDown()).tap { f => Await.result(f, timeout.duration).asInstanceOf[OK] } system.awaitTermination() }
Example 40
Source File: PublishAndWaitAction.scala From gatling-mqtt-protocol with Apache License 2.0 | 5 votes |
package com.github.jeanadrien.gatling.mqtt.actions import akka.actor.ActorRef import akka.pattern.AskTimeoutException import akka.util.Timeout import com.github.jeanadrien.gatling.mqtt.client.MqttCommands import com.github.jeanadrien.gatling.mqtt.client.MqttQoS.MqttQoS import com.github.jeanadrien.gatling.mqtt.protocol.MqttComponents import io.gatling.commons.stats.{KO, OK} import io.gatling.commons.util.ClockSingleton._ import io.gatling.core.CoreComponents import io.gatling.core.action.Action import io.gatling.core.session._ import scala.concurrent.duration.FiniteDuration import scala.util.{Failure, Success} class PublishAndWaitAction( mqttComponents : MqttComponents, coreComponents : CoreComponents, topic : Expression[String], payload : Expression[Array[Byte]], payloadFeedback : Array[Byte] => Array[Byte] => Boolean, qos : MqttQoS, retain : Boolean, timeout : FiniteDuration, val next : Action ) extends MqttAction(mqttComponents, coreComponents) { import akka.pattern.ask import mqttComponents.system.dispatcher override val name = genName("mqttPublishAndWait") override def execute(session : Session) : Unit = recover(session)(for { connection <- session("engine").validate[ActorRef] connectionId <- session("connectionId").validate[String] resolvedTopic <- topic(session) resolvedPayload <- payload(session) } yield { implicit val messageTimeout = Timeout(timeout) val requestStartDate = nowMillis val requestName = "publish and wait" logger.debug(s"${connectionId} : Execute ${requestName} Payload: ${resolvedPayload}") val payloadCheck = payloadFeedback(resolvedPayload) (connection ? MqttCommands.PublishAndWait( topic = resolvedTopic, payload = resolvedPayload, payloadFeedback = payloadCheck, qos = qos, retain = retain )).mapTo[MqttCommands] onComplete { case Success(MqttCommands.FeedbackReceived) => val latencyTimings = timings(requestStartDate) statsEngine.logResponse( session, requestName, latencyTimings, OK, None, None ) next ! session case Failure(th) => val latencyTimings = timings(requestStartDate) statsEngine.logResponse( session, requestName, latencyTimings, KO, None, th match { case t : AskTimeoutException => logger .warn(s"${connectionId}: Wait for PUBLISH back from mqtt timed out on ${resolvedTopic}") Some("Wait for PUBLISH timed out") case t => logger .warn(s"${connectionId}: Failed to receive PUBLISH back from mqtt on ${resolvedTopic}: ${t}") Some(t.getMessage) } ) next ! session } }) }
Example 41
Source File: WaitForMessagesAction.scala From gatling-mqtt-protocol with Apache License 2.0 | 5 votes |
package com.github.jeanadrien.gatling.mqtt.actions import akka.actor.ActorRef import akka.pattern.AskTimeoutException import akka.util.Timeout import com.github.jeanadrien.gatling.mqtt.client.MqttCommands import com.github.jeanadrien.gatling.mqtt.protocol.MqttComponents import io.gatling.core.CoreComponents import io.gatling.core.action.Action import io.gatling.core.session.Session import scala.concurrent.duration.FiniteDuration import scala.util.{Failure, Success} class WaitForMessagesAction( mqttComponents : MqttComponents, coreComponents : CoreComponents, timeout : FiniteDuration, val next : Action ) extends MqttAction(mqttComponents, coreComponents) { import akka.pattern.ask import mqttComponents.system.dispatcher override val name = genName("mqttWaitForMessage") override def execute(session : Session) : Unit = recover(session)(for { connection <- session("engine").validate[ActorRef] connectionId <- session("connectionId").validate[String] } yield { implicit val messageTimeout = Timeout(timeout) logger.debug(s"Wait for message action started... Timeout: ${timeout}") (connection ? MqttCommands.WaitForMessages).mapTo[MqttCommands] onComplete { case Success(MqttCommands.WaitForMessagesDone) => logger.info(s"${connectionId} : Done waitForMessage.") next ! session case Failure(t) if t.isInstanceOf[AskTimeoutException] => logger.warn("Wait for remaining messages timed out") next ! session.markAsFailed case Failure(t) => logger.warn("Wait for remaining messages error:", t) next ! session.markAsFailed } }) }
Example 42
Source File: PublishAction.scala From gatling-mqtt-protocol with Apache License 2.0 | 5 votes |
package com.github.jeanadrien.gatling.mqtt.actions import akka.actor.ActorRef import akka.pattern.ask import akka.util.Timeout import com.github.jeanadrien.gatling.mqtt.client.MqttCommands import com.github.jeanadrien.gatling.mqtt.client.MqttQoS.MqttQoS import com.github.jeanadrien.gatling.mqtt.protocol.MqttComponents import io.gatling.commons.stats.{KO, OK} import io.gatling.commons.util.ClockSingleton._ import io.gatling.core.CoreComponents import io.gatling.core.action.Action import io.gatling.core.session._ import scala.concurrent.duration._ import scala.util.{Failure, Success} class PublishAction( mqttComponents : MqttComponents, coreComponents : CoreComponents, topic : Expression[String], payload : Expression[Array[Byte]], qos : MqttQoS, retain : Boolean, val next : Action ) extends MqttAction(mqttComponents, coreComponents) { import mqttComponents.system.dispatcher override val name = genName("mqttPublish") override def execute(session : Session) : Unit = recover(session)(for { connection <- session("engine").validate[ActorRef] connectionId <- session("connectionId").validate[String] resolvedTopic <- topic(session) resolvedPayload <- payload(session) } yield { implicit val timeout = Timeout(1 minute) // TODO check how to configure this val requestStartDate = nowMillis val requestName = "publish" logger.debug(s"${connectionId}: Execute ${requestName}:${resolvedTopic} Payload: ${resolvedPayload}") (connection ? MqttCommands.Publish( resolvedTopic, resolvedPayload, qos, retain )).mapTo[MqttCommands].onComplete { case Success(MqttCommands.PublishAck) => val publishTimings = timings(requestStartDate) statsEngine.logResponse( session, requestName, publishTimings, OK, None, None ) next ! session case Failure(th) => val publishTimings = timings(requestStartDate) logger.warn(s"${connectionId}: Failed to publish on ${resolvedTopic}: ${th}") statsEngine.logResponse( session, requestName, publishTimings, KO, None, Some(th.getMessage) ) next ! session } }) }
Example 43
Source File: SubscribeAction.scala From gatling-mqtt-protocol with Apache License 2.0 | 5 votes |
package com.github.jeanadrien.gatling.mqtt.actions import akka.actor.ActorRef import akka.pattern.ask import akka.util.Timeout import com.github.jeanadrien.gatling.mqtt.client.MqttCommands import com.github.jeanadrien.gatling.mqtt.client.MqttQoS.MqttQoS import com.github.jeanadrien.gatling.mqtt.protocol.MqttComponents import io.gatling.commons.stats._ import io.gatling.commons.util.ClockSingleton._ import io.gatling.core.CoreComponents import io.gatling.core.action.Action import io.gatling.core.session._ import scala.concurrent.duration._ import scala.util.{Failure, Success} class SubscribeAction( mqttComponents : MqttComponents, coreComponents : CoreComponents, topic : Expression[String], qos : MqttQoS, val next : Action ) extends MqttAction(mqttComponents, coreComponents) { import mqttComponents.system.dispatcher override val name = genName("mqttSubscribe") override def execute(session : Session) : Unit = recover(session)(for { connection <- session("engine").validate[ActorRef] connectionId <- session("connectionId").validate[String] resolvedTopic <- topic(session) } yield { implicit val timeout = Timeout(1 minute) // TODO check how to configure this val requestStartDate = nowMillis val requestName = "subscribe" logger.debug(s"${connectionId}: Execute ${requestName}:${resolvedTopic}") (connection ? MqttCommands.Subscribe((resolvedTopic -> qos) :: Nil)).mapTo[MqttCommands].onComplete { case Success(MqttCommands.SubscribeAck) => val subscribeTimings = timings(requestStartDate) statsEngine.logResponse( session, requestName, subscribeTimings, OK, None, None // Some(new String(value)) // FIXME see equiv in Fuse client ) next ! session case Failure(th) => val subscribeTimings = timings(requestStartDate) logger.warn(s"${connectionId}: Failed to SUBSCRIBE on ${resolvedTopic}: ${th}") statsEngine.logResponse( session, requestName, subscribeTimings, KO, None, Some(th.getMessage) ) next ! session } }) }
Example 44
Source File: ConnectAction.scala From gatling-mqtt-protocol with Apache License 2.0 | 5 votes |
package com.github.jeanadrien.gatling.mqtt.actions import com.github.jeanadrien.gatling.mqtt.client.{FuseSourceConnectionListener, MqttCommands} import com.github.jeanadrien.gatling.mqtt.protocol.{ConnectionSettings, MqttComponents} import io.gatling.commons.stats._ import io.gatling.commons.util.ClockSingleton._ import io.gatling.core.CoreComponents import io.gatling.core.Predef._ import io.gatling.core.action.Action import akka.pattern.ask import akka.util.Timeout import scala.concurrent.duration._ import scala.util.{Failure, Success} class ConnectAction( mqttComponents : MqttComponents, coreComponents : CoreComponents, connectionSettings : ConnectionSettings, val next : Action ) extends MqttAction(mqttComponents, coreComponents) { import mqttComponents.system.dispatcher override val name = genName("mqttConnect") override def execute(session : Session) : Unit = recover(session) { val connectionId = genName("mqttClient") mqttComponents.mqttEngine(session, connectionSettings, connectionId).flatMap { mqtt => val requestName = "connect" logger.debug(s"${connectionId}: Execute ${requestName}") // connect implicit val timeout = Timeout(1 minute) // TODO check how to configure this val requestStartDate = nowMillis (mqtt ? MqttCommands.Connect).mapTo[MqttCommands].onComplete { case Success(MqttCommands.ConnectAck) => val connectTiming = timings(requestStartDate) statsEngine.logResponse( session, requestName, connectTiming, OK, None, None ) next ! session. set("engine", mqtt). set("connectionId", connectionId) case Failure(th) => val connectTiming = timings(requestStartDate) logger.warn(s"${connectionId}: Failed to connect to MQTT: ${th}") statsEngine.logResponse( session, requestName, connectTiming, KO, None, Some(th.getMessage) ) next ! session.markAsFailed } } } }
Example 45
Source File: JavascriptExecutor.scala From graphcool-framework with Apache License 2.0 | 5 votes |
package cool.graph.javascriptEngine import akka.actor.ActorSystem import akka.pattern.ask import akka.util.Timeout import com.typesafe.jse.Engine.JsExecutionResult import cool.graph.cuid.Cuid import cool.graph.javascriptEngine.lib.{Engine, Trireme} import scala.collection.immutable import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.Future import scala.concurrent.duration._ object JavascriptExecutor { implicit val system = ActorSystem("jse-system") implicit val timeout = Timeout(5.seconds) def execute(program: String): Future[Result] = { // note: probably not the way to do this ... val engine = system.actorOf(Trireme.props(), s"engine-${Cuid.createCuid()}") (engine ? Engine.ExecuteJs(program, immutable.Seq(), timeout.duration)) .mapTo[JsExecutionResult] .map(res => Result(result = res.output.utf8String, error = res.error.utf8String)) } def executeFunction(program: String): Future[Map[String, Any]] = { import spray.json._ import DefaultJsonProtocol._ // todo: copied from shared.Utils. Extract to own module implicit object AnyJsonFormat extends JsonFormat[Any] { def write(x: Any) = x match { case m: Map[_, _] => JsObject(m.asInstanceOf[Map[String, Any]].mapValues(write)) case l: List[Any] => JsArray(l.map(write).toVector) case n: Int => JsNumber(n) case n: Long => JsNumber(n) case s: String => JsString(s) case true => JsTrue case false => JsFalse case v: JsValue => v case null => JsNull case r => JsString(r.toString) } def read(x: JsValue): Any = { x match { case l: JsArray => l.elements.map(read).toList case m: JsObject => m.fields.mapValues(write) case s: JsString => s.value case n: JsNumber => n.value case b: JsBoolean => b.value case JsNull => null case _ => sys.error("implement all scalar types!") } } } execute(program).map(res => { if (!res.error.trim.isEmpty) { throw new JsExecutionError(res.error) } res.result.parseJson.asJsObject.convertTo[Map[String, Any]] }) } } case class Result(result: String, error: String) class JsExecutionError(message: String) extends Error
Example 46
Source File: ExportConsumerMetricsToRegistryActor.scala From remora with MIT License | 5 votes |
import KafkaClientActor.{Command, DescribeKafkaConsumerGroup, ListConsumers} import akka.actor._ import akka.pattern.ask import akka.stream.ActorMaterializer import akka.util.Timeout import com.codahale.metrics.Gauge import models.RegistryKafkaMetric._ import models.{GroupInfo, RegistryKafkaMetric} import nl.grons.metrics.scala.{ActorInstrumentedLifeCycle, ReceiveCounterActor, ReceiveExceptionMeterActor, ReceiveTimerActor} import scala.concurrent.duration._ import scala.reflect.ClassTag object ExportConsumerMetricsToRegistryActor { def props(kafkaClientActorRef: ActorRef)(implicit actorSystem: ActorSystem, materializer: ActorMaterializer) = Props(classOf[ExportConsumerMetricsToRegistryActor], kafkaClientActorRef, actorSystem, materializer) } class BaseExportConsumerMetricsToRegistryActor(kafkaClientActorRef: ActorRef) (implicit actorSystem: ActorSystem, materializer: ActorMaterializer) extends Actor with ActorLogging with nl.grons.metrics.scala.DefaultInstrumented with ActorInstrumentedLifeCycle { implicit val timeout = Timeout(60 seconds) implicit val apiExecutionContext = actorSystem.dispatchers.lookup("exporter-dispatcher") private def askFor[RES](command: Command)(implicit tag: ClassTag[RES]) = (kafkaClientActorRef ? command).mapTo[RES] def receive = { case _ => log.info("Exporting lag info to metrics registry!") val consumerList = askFor[List[String]](ListConsumers) consumerList.map(_.foreach(consumerGroup => { val groupInfo = askFor[GroupInfo](DescribeKafkaConsumerGroup(consumerGroup)) groupInfo.map { gi => gi.partitionAssignmentStates.map(pa => { pa.map { p => val offsetKey = encode(RegistryKafkaMetric("gauge", p.topic.get, p.partition.map(_.toString), p.group, "offset")) registerOrUpdateGauge(offsetKey, p.offset) val lagKey = encode(RegistryKafkaMetric("gauge", p.topic.get, p.partition.map(_.toString), p.group, "lag")) registerOrUpdateGauge(lagKey, p.lag) val logEndKey = encode(RegistryKafkaMetric("gauge", p.topic.get, p.partition.map(_.toString), p.group, "logend")) registerOrUpdateGauge(logEndKey, p.logEndOffset) } gi.lagPerTopic.map { lagPerTopic => lagPerTopic.foreach { case (topic, totalLag) => val lagKey = encode(RegistryKafkaMetric("gauge", topic, None, consumerGroup, "lag" )) registerOrUpdateGauge(lagKey, Some(totalLag)) } } } ) } })) } //yea the gauges aren't really meant to be used by this, but i dont want to cache the results. def registerOrUpdateGauge(gaugeName: String, value: Option[Long]) = { value match { case Some(v) => { metricRegistry.remove(gaugeName) metricRegistry.register(gaugeName, new Gauge[Long] { override def getValue: Long = v }) } case None => log.error(s"Gauge $gaugeName has None!") } } } class ExportConsumerMetricsToRegistryActor(kafkaClientActorRef: ActorRef) (implicit actorSystem: ActorSystem, materializer: ActorMaterializer) extends BaseExportConsumerMetricsToRegistryActor(kafkaClientActorRef) with ReceiveCounterActor with ReceiveTimerActor with ReceiveExceptionMeterActor
Example 47
Source File: CassandraHealthCheck.scala From akka-persistence-cassandra with Apache License 2.0 | 5 votes |
package akka.persistence.cassandra.healthcheck import akka.actor.ActorSystem import akka.event.Logging import akka.pattern.{ ask, AskTimeoutException } import akka.persistence.Persistence import akka.persistence.cassandra.PluginSettings import akka.persistence.cassandra.journal.CassandraJournal.HealthCheckQuery import akka.util.Timeout import scala.concurrent.{ ExecutionContextExecutor, Future } import scala.util.control.NonFatal final class CassandraHealthCheck(system: ActorSystem) extends (() => Future[Boolean]) { private val log = Logging.getLogger(system, getClass) private val settings = new PluginSettings(system, system.settings.config.getConfig("akka.persistence.cassandra")) private val healthCheckSettings = settings.healthCheckSettings private val journalPluginId = s"${healthCheckSettings.pluginLocation}.journal" private val journalRef = Persistence(system).journalFor(journalPluginId) private implicit val ec: ExecutionContextExecutor = system.dispatchers.lookup(s"$journalPluginId.plugin-dispatcher") private implicit val timeout: Timeout = healthCheckSettings.timeout override def apply(): Future[Boolean] = { (journalRef ? HealthCheckQuery).map(_ => true).recoverWith { case _: AskTimeoutException => log.warning("Failed to execute health check due to ask timeout") Future.successful(false) case NonFatal(e) => log.warning("Failed to execute health check due to: {}", e) Future.successful(false) } } }
Example 48
Source File: UpdateLogger.scala From CSYE7200_Old with MIT License | 5 votes |
package edu.neu.coe.csye7200.hedge_fund.actors import akka.actor.{ActorRef, Props} import akka.pattern.ask import akka.util.Timeout import scala.concurrent.duration._ import scala.concurrent.Await import scala.language.postfixOps import edu.neu.coe.csye7200.hedge_fund.model.Model import edu.neu.coe.csye7200.hedge_fund.portfolio.{Contract, Portfolio, Position} class UpdateLogger(blackboard: ActorRef) extends BlackboardActor(blackboard) { var portfolio = Portfolio("", Nil) override def receive = { case Confirmation(id, model, attrs) => log.debug(s"update for identifier: $id") if (model.isOption) processOption(id, model, attrs) else processStock(id, model) case PortfolioUpdate(p) => log.debug(s"portfolio update for: ${p.name}") portfolio = p showPortfolio case m => super.receive(m) } implicit val timeout = Timeout(5 seconds) def processStock(identifier: String, model: Model) = { model.getKey("price") match { case Some(p) => // sender is the MarketData actor val future = sender ? SymbolQuery(identifier, List(p)) val result = Await.result(future, timeout.duration).asInstanceOf[QueryResponse] result.attributes foreach { case (k, v) => log.info(s"$identifier attribute $k has been updated to: $v") } case None => log.warning(s"'price' not defined in model") } } def processOption(identifier: String, model: Model, attributes: Map[String, Any]) = { val key = "underlying" attributes.get(key) match { case Some(value) => val future = blackboard ? OptionQuery("id", value) val result = Await.result(future, timeout.duration).asInstanceOf[QueryResponse] println(s"Action Required: re: qualifying option $identifier with underlying symbol: ${result.identifier} and attributes: ${result.attributes}") case None => log.warning(s"processOption: value not present for $key") } } def showPortfolio() { println(s"Portfolio for ${portfolio.name}") portfolio.positions foreach { showPosition } } def showPosition(position: Position) { println(s"position for ${position.symbol}: quantity=${position.quantity}; options=") position.contracts foreach { showContract } } def showContract(contract: Contract) { println(s"contract: $contract") } }
Example 49
Source File: JsonYQLParserSpec.scala From CSYE7200_Old with MIT License | 5 votes |
package edu.neu.coe.csye7200.hedge_fund.actors import akka.actor.{Actor, ActorRef, ActorSystem, Props} import akka.testkit._ import edu.neu.coe.csye7200.hedge_fund.model.Model import org.scalatest.{BeforeAndAfterAll, Matchers, WordSpecLike} import scala.io.Source import scala.concurrent.duration._ import spray.http._ import org.scalatest.Inside import scala.language.postfixOps import spray.http.ContentType.apply class JsonYQLParserSpec(_system: ActorSystem) extends TestKit(_system) with ImplicitSender with WordSpecLike with Matchers with Inside with BeforeAndAfterAll { def this() = this(ActorSystem("JsonYQLParserSpec")) override def afterAll { TestKit.shutdownActorSystem(system) } import scala.language.postfixOps val json = Source.fromFile(getClass.getResource("/yqlExample.json").getPath) mkString "json conversion" in { val body = HttpEntity(MediaTypes.`application/json`, json.getBytes()) val ok = JsonYQLParser.decode(body) match { case Right(x) => val count = x.query.count count should equal(4) x.query.results.quote.length should equal(count) x.query.results.get(count - 1, "symbol") should matchPattern { case Some("MSFT") => } case Left(x) => fail("decoding error: " + x) } } "send back" in { val blackboard = system.actorOf(Props.create(classOf[MockYQLBlackboard], testActor), "blackboard") val entityParser = _system.actorOf(Props.create(classOf[EntityParser], blackboard), "entityParser") val entity = HttpEntity(MediaTypes.`application/json`, json.getBytes()) entityParser ! EntityMessage("json:YQL", entity) val msg = expectMsgClass(3.seconds, classOf[QueryResponse]) println("msg received: " + msg) msg should matchPattern { case QueryResponse("MSFT", _) => } inside(msg) { case QueryResponse(symbol, attributes) => attributes.get("Ask") should matchPattern { case Some("46.17") => } } } } import akka.pattern.ask import scala.concurrent.Await class MockYQLUpdateLogger(blackboard: ActorRef) extends UpdateLogger(blackboard) { override def processStock(identifier: String, model: Model) = { model.getKey("price") match { case Some(p) => { // sender is the MarketData actor val future = sender ? SymbolQuery(identifier, List(p)) val result = Await.result(future, timeout.duration).asInstanceOf[QueryResponse] result.attributes map { case (k, v) => log.info(s"$identifier attribute $k has been updated to: $v") blackboard ! result } } case None => log.warning(s"'price' not defined in model") } } } class MockYQLBlackboard(testActor: ActorRef) extends Blackboard(Map(classOf[KnowledgeUpdate] -> "marketData", classOf[SymbolQuery] -> "marketData", classOf[OptionQuery] -> "marketData", classOf[CandidateOption] -> "optionAnalyzer", classOf[Confirmation] -> "updateLogger"), Map("marketData" -> classOf[MarketData], "optionAnalyzer" -> classOf[OptionAnalyzer], "updateLogger" -> classOf[MockYQLUpdateLogger])) { override def receive = { case msg: Confirmation => msg match { // Cut down on the volume of messages case Confirmation("MSFT", _, _) => super.receive(msg) case _ => } case msg: QueryResponse => testActor forward msg case msg => super.receive(msg) } }
Example 50
Source File: JsonGoogleOptionParserSpec.scala From CSYE7200_Old with MIT License | 5 votes |
package edu.neu.coe.csye7200.hedge_fund.actors import akka.actor.{Actor, ActorRef, ActorSystem, Props} import akka.testkit._ import edu.neu.coe.csye7200.hedge_fund.model.Model import org.scalatest.{BeforeAndAfterAll, Matchers, WordSpecLike} import scala.io.Source import scala.concurrent.duration._ import spray.http._ import spray.http.MediaTypes._ import org.scalatest.Inside import org.scalatest.tagobjects.Slow import scala.language.postfixOps import spray.json.pimpString class JsonGoogleOptionParserSpec(_system: ActorSystem) extends TestKit(_system) with ImplicitSender with WordSpecLike with Matchers with BeforeAndAfterAll { def this() = this(ActorSystem("JsonGoogleParserSpec")) override def afterAll { TestKit.shutdownActorSystem(system) } import scala.language.postfixOps val json = Source.fromFile(getClass.getResource("/googleOptionExample.json").getPath) mkString "json read" in { import spray.json._ val obj = JsonGoogleOptionParser.fix(json).parseJson } "json conversion" in { val contentType = ContentType(MediaTypes.`application/json`, HttpCharsets.`UTF-8`) val entity = HttpEntity(contentType, json.getBytes()) val ok = JsonGoogleOptionParser.decode(entity) match { case Right(x) => x.puts.length should equal(20) val puts = x.puts puts(0).get("s") should matchPattern { case Some("MSFT150731P00042500") => } case Left(x) => fail("decoding error: " + x) } } "send back" taggedAs(Slow) in { val blackboard = system.actorOf(Props.create(classOf[MockGoogleOptionBlackboard], testActor), "blackboard") val entityParser = _system.actorOf(Props.create(classOf[EntityParser], blackboard)) val contentType = ContentType(MediaTypes.`application/json`, HttpCharsets.`UTF-8`) val entity = HttpEntity(contentType, json.getBytes()) entityParser ! EntityMessage("json:GO", entity) val msg = expectMsgClass(3.seconds, classOf[QueryResponse]) println("msg received: " + msg) msg should matchPattern { case QueryResponse(_, _) => } } } import akka.pattern.ask import akka.util.Timeout import scala.concurrent.duration._ import scala.concurrent.Await class MockGoogleOptionUpdateLogger(blackboard: ActorRef) extends UpdateLogger(blackboard) { override def processOption(identifier: String, model: Model, attributes: Map[String, Any]) = { val keys = model mapKeys (List("underlying", "strikePrice", "expiry")) println(s"$keys") val future = blackboard ? OptionQuery(identifier, keys) val result = Await.result(future, timeout.duration).asInstanceOf[QueryResponse] blackboard ! result } } class MockGoogleOptionBlackboard(testActor: ActorRef) extends Blackboard(Map(classOf[KnowledgeUpdate] -> "marketData", classOf[SymbolQuery] -> "marketData", classOf[OptionQuery] -> "marketData", classOf[CandidateOption] -> "optionAnalyzer", classOf[Confirmation] -> "updateLogger"), Map("marketData" -> classOf[MarketData], "optionAnalyzer" -> classOf[OptionAnalyzer], "updateLogger" -> classOf[MockGoogleOptionUpdateLogger])) { override def receive = { case msg: Confirmation => msg match { // Cut down on the volume of messages case Confirmation("MSFT150731P00045000", _, _) => super.receive(msg) case _ => } case msg: QueryResponse => testActor forward msg case msg => super.receive(msg) } }
Example 51
Source File: UpdateLogger.scala From CSYE7200_Old with MIT License | 5 votes |
package edu.neu.coe.csye7200.actors import akka.actor.ActorRef import akka.pattern.ask import akka.util.Timeout import edu.neu.coe.csye7200.model.Model import edu.neu.coe.csye7200.portfolio.{Contract, Portfolio, Position} import scala.concurrent.Await import scala.concurrent.duration._ import scala.language.postfixOps class UpdateLogger(blackboard: ActorRef) extends BlackboardActor(blackboard) { var portfolio = Portfolio("", Nil) override def receive: PartialFunction[Any, Unit] = { case Confirmation(id, model, attrs) => log.debug(s"update for identifier: $id") if (model.isOption) processOption(id, model, attrs) else processStock(id, model) case PortfolioUpdate(p) => log.debug(s"portfolio update for: ${p.name}") portfolio = p showPortfolio() case m => super.receive(m) } implicit val timeout: Timeout = Timeout(5 seconds) def processStock(identifier: String, model: Model): Unit = { model.getKey("price") match { case Some(p) => // sender is the MarketData actor val future = sender ? SymbolQuery(identifier, List(p)) val result = Await.result(future, timeout.duration).asInstanceOf[QueryResponse] result.attributes foreach { case (k, v) => log.info(s"$identifier attribute $k has been updated to: $v") } case None => log.warning(s"'price' not defined in model") } } def processOption(identifier: String, model: Model, attributes: Map[String, Any]): Unit = { val key = "underlying" attributes.get(key) match { case Some(value) => val future = blackboard ? OptionQuery("id", value) val result = Await.result(future, timeout.duration).asInstanceOf[QueryResponse] println(s"Action Required: re: qualifying option $identifier with underlying symbol: ${result.identifier} and attributes: ${result.attributes}") case None => log.warning(s"processOption: value not present for $key") } } def showPortfolio() { println(s"Portfolio for ${portfolio.name}") portfolio.positions foreach showPosition } def showPosition(position: Position) { println(s"position for ${position.symbol}: quantity=${position.quantity}; options=") position.contracts foreach showContract } def showContract(contract: Contract) { println(s"contract: $contract") } }
Example 52
Source File: JsonYQLParserSpec.scala From CSYE7200_Old with MIT License | 5 votes |
package edu.neu.coe.csye7200.actors import akka.actor.{ActorRef, ActorSystem, Props} import akka.testkit._ import edu.neu.coe.csye7200.model.Model import org.scalatest._ import spray.http.ContentType._ import spray.http._ import scala.concurrent.duration._ import scala.io.Source import scala.language.postfixOps class JsonYQLParserSpec(_system: ActorSystem) extends TestKit(_system) with ImplicitSender with WordSpecLike with Matchers with Inside with BeforeAndAfterAll { def this() = this(ActorSystem("JsonYQLParserSpec")) override def afterAll { TestKit.shutdownActorSystem(system) } import scala.language.postfixOps val json: String = Source.fromFile(getClass.getResource("/yqlExample.json").getPath) mkString "json conversion" in { val body = HttpEntity(MediaTypes.`application/json`, json.getBytes()) val ok = JsonYQLParser.decode(body) match { case Right(x) => val count = x.query.count count should equal(4) x.query.results.quote.length should equal(count) x.query.results.get(count - 1, "symbol") should matchPattern { case Some("MSFT") => } case Left(x) => fail("decoding error: " + x) } ok shouldBe Succeeded } "send back" in { val blackboard = system.actorOf(Props.create(classOf[MockYQLBlackboard], testActor), "blackboard") val entityParser = _system.actorOf(Props.create(classOf[EntityParser], blackboard), "entityParser") val entity = HttpEntity(MediaTypes.`application/json`, json.getBytes()) entityParser ! EntityMessage("json:YQL", entity) val msg = expectMsgClass(3.seconds, classOf[QueryResponse]) println("msg received: " + msg) msg should matchPattern { case QueryResponse("MSFT", _) => } inside(msg) { case QueryResponse(_, attributes) => attributes.get("Ask") should matchPattern { case Some("46.17") => } } } } import akka.pattern.ask import scala.concurrent.Await class MockYQLUpdateLogger(blackboard: ActorRef) extends UpdateLogger(blackboard) { override def processStock(identifier: String, model: Model): Unit = { model.getKey("price") match { case Some(p) => // sender is the MarketData actor val future = sender ? SymbolQuery(identifier, List(p)) val result = Await.result(future, timeout.duration).asInstanceOf[QueryResponse] result.attributes foreach { case (k, v) => log.info(s"$identifier attribute $k has been updated to: $v") blackboard ! result } case None => log.warning(s"'price' not defined in model") } } } class MockYQLBlackboard(testActor: ActorRef) extends Blackboard(Map(classOf[KnowledgeUpdate] -> "marketData", classOf[SymbolQuery] -> "marketData", classOf[OptionQuery] -> "marketData", classOf[CandidateOption] -> "optionAnalyzer", classOf[Confirmation] -> "updateLogger"), Map("marketData" -> classOf[MarketData], "optionAnalyzer" -> classOf[OptionAnalyzer], "updateLogger" -> classOf[MockYQLUpdateLogger])) { override def receive: PartialFunction[Any, Unit] = { case msg: Confirmation => msg match { // Cut down on the volume of messages case Confirmation("MSFT", _, _) => super.receive(msg) case _ => } case msg: QueryResponse => testActor forward msg case msg => super.receive(msg) } }
Example 53
Source File: HedgeFundSpec.scala From CSYE7200_Old with MIT License | 5 votes |
package edu.neu.coe.csye7200 import akka.actor.ActorSystem import akka.pattern.ask import akka.util.Timeout import com.typesafe.config.ConfigFactory import edu.neu.coe.csye7200.actors.SymbolQuery import org.scalatest.concurrent.{Futures, ScalaFutures} import org.scalatest.time.{Seconds, Span} import org.scalatest.{FlatSpec, Matchers, _} import scala.concurrent.Future import scala.util.{Failure, Success} class HedgeFundSpec extends FlatSpec with Matchers with Futures with ScalaFutures with TryValues with Inside { behavior of "SymbolQuery" it should "work" in { import scala.concurrent.duration._ implicit val system: ActorSystem = ActorSystem("HedgeFund") implicit val timeout: Timeout = Timeout(30.seconds) val ay = HedgeFund.startup(ConfigFactory.load()) ay should matchPattern { case Success(_) => } val qf = ay match { case Success(q) => q ? SymbolQuery("MSFT", List("name", "symbol", "price", "GF", "t", "l")) case Failure(x) => Future.failed(x) } whenReady(qf, org.scalatest.concurrent.PatienceConfiguration.Timeout(Span(10, Seconds))) { q => println(q) } } }
Example 54
Source File: CircuitBreakerExample.scala From Scala-Reactive-Programming with MIT License | 5 votes |
package com.packt.publishing.reactive.patterns.circuitbreaker import scala.concurrent.duration._ import akka.pattern.CircuitBreaker import akka.pattern.pipe import akka.actor.{Actor, ActorLogging, ActorSystem, Props} import scala.concurrent.Future import akka.util.Timeout import akka.pattern.ask case class MessageOne(msg:String = "is my middle name") case class MessageTwo(msg:String = "block for me") class DangerousActor extends Actor with ActorLogging { import context.dispatcher val breaker = new CircuitBreaker( context.system.scheduler, maxFailures = 5, callTimeout = 10.seconds, resetTimeout = 1.minute).onOpen(notifyMeOnOpen()) def notifyMeOnOpen(): Unit = log.warning("My CircuitBreaker is now open, and will not close for one minute") def dangerousCall: String = "This really isn't that dangerous of a call after all" def receive = { case MessageOne ⇒ breaker.withCircuitBreaker(Future(dangerousCall)) pipeTo sender() case MessageTwo ⇒ sender() ! breaker.withSyncCircuitBreaker(dangerousCall) } } object CircuitBreakerExample extends App { implicit val timeout: Timeout = 2.seconds val system = ActorSystem("WFSystem") val wfTeller = system.actorOf(Props[DangerousActor], "DangerousActor") val future1 = wfTeller ? MessageTwo val future2 = wfTeller ? MessageTwo val future3 = wfTeller ? MessageTwo val future4 = wfTeller ? MessageTwo val future5 = wfTeller ? MessageTwo val future6 = wfTeller ? MessageTwo import scala.concurrent.ExecutionContext.Implicits.global future1.onComplete { value => println("************ value1 = " + value) } future2.onComplete { value => println("************ value2 = " + value) } future3.onComplete { value => println("************ value3 = " + value) } future4.onComplete { value => println("************ value4 = " + value) } future5.onComplete { value => println("************ value5 = " + value) } future6.onComplete { value => println("************ value6 = " + value) } Thread.sleep(10000) system.terminate() }
Example 55
Source File: AskPatternApp.scala From Scala-Reactive-Programming with MIT License | 5 votes |
package com.packt.publishing.reactive.patterns.ask import akka.actor.{Actor, ActorLogging, ActorRef, ActorSystem, Props} import akka.util.Timeout import akka.pattern.ask import scala.concurrent.duration._ import scala.concurrent.ExecutionContext.Implicits.global case object GetWeather case class WeatherForecast(city:String, temperature:String) class WFClient(wfTeller: ActorRef) extends Actor with ActorLogging { implicit val timeout: Timeout = 2.seconds def receive = { case GetWeather => val wf = (wfTeller ? GetWeather) wf.onComplete{ wfValue => log.info("Weather Forecast = " + wfValue) } } } class WFTeller extends Actor { def receive = { case GetWeather => sender() ! WeatherForecast("London", "12") } } object AskPatternApp extends App{ val system = ActorSystem("WFSystem") val wfTeller = system.actorOf(Props[WFTeller], "WFTeller") val clientActor = system.actorOf(Props(new WFClient(wfTeller)), "WFClient") clientActor ! GetWeather Thread.sleep(1000) system.terminate() }
Example 56
Source File: TrafficMonitoring.scala From CM-Well with Apache License 2.0 | 5 votes |
package trafficshaping import actions.{MarkdownTable, MarkdownTuple} import akka.util.Timeout import cmwell.ctrl.config.Jvms import cmwell.domain.{FileContent, FileInfoton, SystemFields, VirtualInfoton} import k.grid.Grid import java.net.InetAddress import scala.concurrent.Future import akka.pattern.ask import org.joda.time.{DateTime, DateTimeZone} import scala.concurrent.duration._ import scala.concurrent.ExecutionContext.Implicits.global import scala.util.Try object TrafficMonitoring { implicit val timeout = Timeout(5.seconds) def mergeCounters(m1: Map[String, RequestCounter], m2: Map[String, RequestCounter]): Map[String, RequestCounter] = { val keyset = m1.keySet ++ m2.keySet keyset.map { key => val v1 = m1.get(key).map(_.counter).getOrElse(0L) val v2 = m2.get(key).map(_.counter).getOrElse(0L) key -> RequestCounter(v1 + v2) }.toMap } def nslookup(ip: String): String = Try(InetAddress.getByName(ip)).map(_.getHostName).getOrElse("NA") def traffic(path: String, dc: String): Future[Option[VirtualInfoton]] = { val setFut = Grid.jvms(Jvms.WS).map { jvm => (Grid.selectActor(CongestionAnalyzer.name, jvm) ? GetTrafficData).mapTo[TrafficData] } val futSet = cmwell.util.concurrent.successes(setFut) futSet.map { set => val trafficData = set.foldLeft(TrafficData(Map.empty[String, RequestorCounter])) { case (r1, r2) => val keyset = r1.requestors.keySet ++ r2.requestors.keySet val newMap = keyset.map { key => val v1 = r1.requestors.getOrElse(key, RequestorCounter(NoPenalty, Map.empty[String, RequestCounter])) val v2 = r2.requestors.getOrElse(key, RequestorCounter(NoPenalty, Map.empty[String, RequestCounter])) key -> RequestorCounter(PenaltyStage.chooseHighest(v1.penalty, v2.penalty), mergeCounters(v1.requestsCounters, v2.requestsCounters)) }.toMap TrafficData(newMap) } val reqMap = trafficData.requestors val reqTypes = reqMap.values.flatMap(_.requestsCounters.keySet).toSet.toSeq.sorted val header = MarkdownTuple("IP", "Host", "Proc Duration", "Plan").add(reqTypes) val reqTuples = reqMap.map { r => val requestorCounters = r._2.requestsCounters val allCounters = reqTypes.map(t => t -> RequestCounter(0L)).toMap ++ requestorCounters val counters = allCounters.toSeq.sortBy(_._1).map(_._2.counter.toString) MarkdownTuple(r._1, nslookup(r._1), r._2.requestsTime.toString, r._2.penalty.toString).add(counters) }.toSeq val table = MarkdownTable(header, reqTuples) val statusText = if (TrafficShaper.isEnabled) "### Traffic shaping is enabled" else "### Traffic shaping is disabled" val content = statusText + "\n\n\n" + table.get Some( VirtualInfoton(FileInfoton(SystemFields(path, new DateTime(DateTimeZone.UTC), "VirtualInfoton", dc, None, "", "http"), content = Some(FileContent(content.getBytes, "text/x-markdown")))) ) } } }
Example 57
Source File: GridMonitor.scala From CM-Well with Apache License 2.0 | 5 votes |
package k.grid.monitoring import akka.util.Timeout import cmwell.util.concurrent._ import akka.actor.Actor import akka.actor.Actor.Receive import akka.pattern.ask import k.grid._ import k.grid.service.LocalServiceManager import k.grid.service.messages.{RegisterServices, ServiceInstantiationRequest} import scala.concurrent.Future import scala.concurrent.duration._ import com.typesafe.scalalogging.LazyLogging import scala.concurrent.ExecutionContext.Implicits.global case object GetMembersInfo case class MembersInfo(m: Map[GridJvm, JvmInfo]) object GridMonitor extends LazyLogging { implicit val timeout = Timeout(15.seconds) lazy private val sel = Grid.selectSingleton(GridMonitor.name, None, Grid.seedMembers.head) def name = "GridMonitor" def init = Grid.createSingleton(classOf[GridMonitor], name, None) def getMembersInfo: Future[MembersInfo] = { logger.info("[GridMonitor] getMembersInfo") (sel ? GetMembersInfo).mapTo[MembersInfo] } } case class MemInfoKey(host: String, name: String) class GridMonitor extends Actor with LazyLogging { private implicit val timeout = Timeout(15.seconds) private[this] var membersInfo = Map.empty[MemInfoKey, (GridJvm, JvmInfo)] private case object SendInfoRequests private case class UpdateMembersInfoMap(m: Map[GridJvm, JvmInfo]) @throws[Exception](classOf[Exception]) override def preStart(): Unit = { context.system.scheduler.schedule(0.seconds, 10.seconds, self, SendInfoRequests) } override def receive: Receive = { case UpdateMembersInfoMap(m) => membersInfo = membersInfo ++ m.map { case (k, v) => MemInfoKey(k.host, k.identity.map(_.name).getOrElse("")) -> (k, v) } case SendInfoRequests => { logger.info("SendInfoRequests") val jvms = Grid.jvmsAll val futures = jvms.map { jvm => ((Grid.selectActor(ClientActor.name, jvm) ? GetClientInfo).mapTo[JvmInfo].map(jvm -> _)).recover { case _ => { val inf = membersInfo.get(MemInfoKey(jvm.host, jvm.identity.map(_.name).getOrElse(""))) match { case Some((gridJvm, jvmInfo)) => jvmInfo.copy(status = Stopped) case None => JvmInfo(ClientMember, Stopped, -1, 0L, Set.empty[MemoryInfo], Set.empty[GcInfo], "NA", "") } jvm -> inf } } } val future = successes(futures).map(_.toMap) future.foreach(m => self ! UpdateMembersInfoMap(m)) } case GetMembersInfo => logger.info("Got GetMembersInfo") sender ! MembersInfo(membersInfo.map { case (k1, (k2, v)) => k2 -> v }) } }
Example 58
Source File: RegistrationCoordinator.scala From CM-Well with Apache License 2.0 | 5 votes |
package k.grid.registration import akka.actor.Actor import akka.actor.Actor.Receive import akka.pattern.ask import scala.concurrent.duration._ import com.typesafe.scalalogging.LazyLogging import k.grid.{Grid, GridJvm} import k.grid.registration.messages.{GridTopology, RegistrationPing} import scala.concurrent.ExecutionContext.Implicits.global object RegistrationCoordinator { val name = "GossipCoordinator" def init = { Grid.createSingleton(classOf[RegistrationCoordinator], RegistrationCoordinator.name, None) Grid.selectSingleton(RegistrationCoordinator.name, None) } } class RegistrationCoordinator extends Actor with LazyLogging { private case class GridJvmContainer(gj: GridJvm, ts: Long) private case object ClearIdles private def currentSeconds = System.currentTimeMillis() / 1000 private[this] var jvmSet = Set.empty[GridJvmContainer] @throws[Exception](classOf[Exception]) override def preStart(): Unit = { context.system.scheduler.schedule(0.seconds, 5.seconds, self, ClearIdles) } override def receive: Receive = { case RegistrationPing(jvm) => jvmSet = jvmSet + GridJvmContainer(jvm, currentSeconds) sender ! GridTopology(jvmSet.map(_.gj)) case ClearIdles => val currentTime = currentSeconds jvmSet = jvmSet.filter(jvm => currentTime - jvm.ts < 30) } }
Example 59
Source File: HealthControl.scala From CM-Well with Apache License 2.0 | 5 votes |
package cmwell.ctrl.hc import akka.util.Timeout import cmwell.ctrl.config.Config._ import cmwell.ctrl.controllers.CmwellController import cmwell.ctrl.server._ import cmwell.ctrl.tasks.TaskExecutorActor import k.grid.dmap.impl.inmem.InMemDMap import k.grid.service.ServiceInitInfo import k.grid.Grid import org.slf4j.LoggerFactory import scala.concurrent.Await import scala.util.{Failure, Success, Try} import scala.concurrent.duration._ import scala.concurrent.ExecutionContext.Implicits.global object HealthControl { private val clusterLogger = LoggerFactory.getLogger("cluster_state") lazy val services = Map( "CassandraMonitorActor" -> ServiceInitInfo(classOf[CassandraMonitorActor], None, Grid.hostName, 0, hcSampleInterval), "DcMonitorActor" -> ServiceInitInfo(classOf[DcMonitorActor], None, Grid.hostName, 0, hcSampleInterval), "HealthActor" -> ServiceInitInfo(classOf[HealthActor], None) ) def init: Unit = { import akka.pattern.ask implicit val timeout = Timeout(3.seconds) def joinCtrlGrid: Unit = { val forceMajor = sys.env.get("FORCE").isDefined clusterLogger.info(s"force majour is $forceMajor") Try( Await.result( cmwell.util.concurrent.retry(10, 5.seconds) { HealthActor.ref ? NodeJoinRequest(host) }.mapTo[JoinResponse], 30.seconds ) ) match { case Success(r) => clusterLogger.info(s"$host got $r from the cluster $clusterName") r match { case response if forceMajor => clusterLogger.info( s"$host received response $response from the cluster $clusterName. Doing force majour will boot cm-well components." ) HealthActor.ref ! RemoveFromDownNodes(host) CmwellController.start case JoinOk => CmwellController.start case JoinShutdown => //CmwellController.clearData sys.exit(0) case JoinBootComponents => CmwellController.start } case Failure(err) => clusterLogger.error(err.getMessage, err) clusterLogger.info( s"$host didn't receive response from the cluster $clusterName, will boot cm-well components." ) CmwellController.start } } Grid.create(classOf[CommandActor], commandActorName) Grid.create(classOf[WebMonitorActor], "WebMonitorActor", Grid.hostName, 0, hcSampleInterval) Grid.create(classOf[BgHealthMonitorActor], "BgHealthMonitorActor", Grid.hostName, 0, bgSampleInterval) Grid.create(classOf[ElasticsearchMonitorActor], "ElasticsearchMonitorActor", Grid.hostName, 0, hcSampleInterval) Grid.create(classOf[SystemMonitorActor], "SystemMonitorActor", Grid.hostName, 0, hcSampleInterval) Grid.create(classOf[ZookeeperMonitorActor], "ZookeeperMonitorActor", Grid.hostName, 0, hcSampleInterval) Grid.create(classOf[KafkaMonitorActor], "KafkaMonitorActor", Grid.hostName, 0, hcSampleInterval) Grid.create(classOf[PingActor], "PingActor") Grid.serviceRef(TaskExecutorActor.name) //Thread.sleep(20000) // Grid.system.scheduler.scheduleOnce(20.seconds) { joinCtrlGrid InMemDMap.aggregate("knownHosts", host) //} } }
Example 60
Source File: AddNode.scala From CM-Well with Apache License 2.0 | 5 votes |
package cmwell.ctrl.tasks import akka.actor.ActorSelection import akka.actor.FSM.Failure import akka.util.Timeout import cmwell.ctrl.checkers.{ComponentState, GreenStatus, YellowStatus} import cmwell.ctrl.commands._ import cmwell.ctrl.hc._ import cmwell.ctrl.server.CommandActor import com.typesafe.scalalogging.LazyLogging import k.grid.Grid import scala.concurrent.{Future, Promise} import scala.concurrent.duration._ import akka.pattern.ask import scala.util.Success import scala.concurrent.ExecutionContext.Implicits.global case class AddNode(node: String) extends Task with LazyLogging { implicit val timeout = Timeout(15.seconds) private def startElasticsearch(cmd: ActorSelection, prom: Promise[Unit]): Unit = { logger.info(s"Starting Elasticsearch on node $node") cmd ! StartElasticsearch Grid.system.scheduler.scheduleOnce(60.seconds) { (HealthActor.ref ? GetElasticsearchDetailedStatus).mapTo[ElasticsearchGridStatus].map { f => f.getStatesMap.get(node) match { case Some(s) => if (s.getColor == GreenStatus || s.getColor == YellowStatus) prom.success(()) else startElasticsearch(cmd, prom) case None => startElasticsearch(cmd, prom) } } } } private def startCassandra(cmd: ActorSelection, prom: Promise[Unit]): Unit = { logger.info(s"Starting Cassandra on node $node") cmd ! StartCassandra Grid.system.scheduler.scheduleOnce(60.seconds) { (HealthActor.ref ? GetCassandraDetailedStatus).mapTo[CassandraGridStatus].map { f => f.getStatesMap.get(node) match { case Some(s) => if (s.getColor == GreenStatus) prom.success(()) else startCassandra(cmd, prom) case None => startCassandra(cmd, prom) } } } } override def exec: Future[TaskResult] = { val cmd = CommandActor.select(node) val esPromise = Promise[Unit] val casPromise = Promise[Unit] startElasticsearch(cmd, esPromise) startCassandra(cmd, casPromise) val esCancelable = cancel(esPromise, 24.hours) val casCancelable = cancel(casPromise, 24.hours) val esFuture = esPromise.future val casFuture = casPromise.future // cancel the cancelables when the future succeeded esFuture.foreach(x => esCancelable.cancel()) casFuture.foreach(x => casCancelable.cancel()) val fut = for { esStarted <- esFuture casStarted <- casFuture } yield { logger.info("Starting CM-WELL components") cmd ! StartKafka cmd ! StartBg cmd ! StartWebserver cmd ! StartCw cmd ! StartDc } fut.map(r => TaskSuccessful).recover { case err: Throwable => TaskFailed } } }
Example 61
Source File: Node.scala From CM-Well with Apache License 2.0 | 5 votes |
package cmwell.ctrl.client import akka.pattern.ask import akka.actor.ActorRef import akka.util.Timeout import cmwell.ctrl.checkers._ import cmwell.ctrl.server.BashCommand import scala.concurrent.Future import scala.concurrent.duration._ import scala.language.postfixOps case class Node(ip: String, ctrlActor: ActorRef) { implicit val timeout = Timeout(5 seconds) def command(com: String): Future[String] = { (ctrlActor ? BashCommand(com)).mapTo[String] } def getWebStatus: Future[WebState] = { (ctrlActor ? CheckWeb).mapTo[WebState] } def getBgStatus: Future[BgState] = { (ctrlActor ? CheckBg).mapTo[BgState] } def getElasticsearchStatus: Future[ElasticsearchState] = { (ctrlActor ? CheckElasticsearch).mapTo[ElasticsearchState] } def getCassandraStatus: Future[CassandraState] = { (ctrlActor ? CheckCassandra).mapTo[CassandraState] } }
Example 62
Source File: HighchartsTestApp.scala From scalajs-highcharts with MIT License | 5 votes |
package com.karasiq.highcharts.test.backend import java.util.concurrent.TimeUnit import akka.actor._ import akka.io.IO import akka.pattern.ask import akka.util.Timeout import spray.can.Http import spray.http.MediaTypes import spray.routing.HttpService import scala.concurrent.Await import scala.concurrent.duration._ import scala.language.postfixOps object HighchartsTestApp extends App { final class AppHandler extends Actor with HttpService { override def receive: Actor.Receive = runRoute { get { compressResponse() { // Index page (pathSingleSlash & respondWithMediaType(MediaTypes.`text/html`)) { getFromResource("webapp/index.html") } ~ // Other resources getFromResourceDirectory("webapp") } } } override def actorRefFactory: ActorRefFactory = context } def startup(): Unit = { implicit val timeout = Timeout(20 seconds) implicit val actorSystem = ActorSystem("highcharts-test") val service = actorSystem.actorOf(Props[AppHandler], "webService") IO(Http) ? Http.Bind(service, interface = "localhost", port = 9000) Runtime.getRuntime.addShutdownHook(new Thread(new Runnable { override def run(): Unit = { Await.result(actorSystem.terminate(), FiniteDuration(5, TimeUnit.MINUTES)) } })) } startup() }
Example 63
Source File: Retry.scala From seahorse with Apache License 2.0 | 5 votes |
package ai.deepsense.commons.utils import scala.concurrent.Future import scala.concurrent.duration.FiniteDuration import akka.actor.{ActorSystem, Props} import akka.pattern.ask import akka.util.Timeout trait Retry[T] { def work: Future[T] def retryInterval: FiniteDuration def retryLimit: Int def actorSystem: ActorSystem // the timeout should exceed the retryLimit * retryInterval + (retryLimit + 1) * avgWorkDuration // otherwise the ask in tryWork method may timeout before all the retries have been attempted implicit def timeout: Timeout def workDescription: Option[String] private lazy val retryActor = actorSystem.actorOf(Props(new RetryActor[T]( retryInterval, retryLimit, work, workDescription ))) def tryWork: Future[T] = (retryActor ? RetryActor.Trigger).asInstanceOf[Future[T]] }
Example 64
Source File: RestClient.scala From seahorse with Apache License 2.0 | 5 votes |
package ai.deepsense.commons.rest.client import java.net.URL import java.util.UUID import scala.concurrent.{ExecutionContext, Future} import akka.io.IO import akka.pattern.ask import spray.can.Http import spray.can.Http.HostConnectorInfo import spray.client.pipelining._ import spray.http.{HttpCredentials, HttpRequest, HttpResponse} import spray.httpx.unmarshalling.FromResponseUnmarshaller trait RestClient extends RestClientImplicits { import RestClient._ def apiUrl: URL def userId: Option[UUID] def userName: Option[String] def credentials: Option[HttpCredentials] implicit override val ctx: ExecutionContext = as.dispatcher private def hostConnectorFut(): Future[HostConnectorInfo] = { (IO(Http) ? Http.HostConnectorSetup(apiUrl.getHost, port = apiUrl.getPort)).mapTo[HostConnectorInfo] } private def sendReceivePipeline() : Future[HttpRequest => Future[HttpResponse]] = { for { HostConnectorInfo(hostConnector, _) <- hostConnectorFut() } yield { credentials.map(addCredentials).getOrElse[RequestTransformer](identity) ~> userId.map(id => addHeader(UserIdHeader, id.toString)).getOrElse[RequestTransformer](identity) ~> userName.map(addHeader(UserNameHeader, _)).getOrElse[RequestTransformer](identity) ~> sendReceive(hostConnector) } } private def unmarshalPipeline[U: FromResponseUnmarshaller](): Future[HttpRequest => Future[U]] = { for { sr <- sendReceivePipeline() } yield { sr ~> unmarshal[U] } } def fetchResponse[U : FromResponseUnmarshaller]( req: HttpRequest ): Future[U] = { unmarshalPipeline().flatMap(_(req)) } def fetchHttpResponse(req: HttpRequest) : Future[HttpResponse] = { sendReceivePipeline().flatMap(_(req)) } def endpointPath(endpoint: String): String = { new URL(apiUrl, endpoint).getFile } } object RestClient { val UserIdHeader = "X-Seahorse-UserId" val UserNameHeader = "X-Seahorse-UserName" }
Example 65
Source File: WorkflowDownloadClient.scala From seahorse with Apache License 2.0 | 5 votes |
package ai.deepsense.workflowexecutor import scala.concurrent.Future import scala.concurrent.duration._ import scala.util.Try import akka.actor.ActorSystem import akka.io.IO import akka.pattern.ask import spray.can.Http import spray.client.pipelining._ import spray.http._ import spray.util._ import ai.deepsense.commons.utils.Logging import ai.deepsense.sparkutils import ai.deepsense.workflowexecutor.exception.UnexpectedHttpResponseException class WorkflowDownloadClient( val address: String, val path: String, val timeout: Int) extends Logging { val downloadUrl = (workflowId: String) => s"$address/$path/$workflowId/download" def downloadWorkflow(workflowId: String): Future[String] = { logger.info(s"Downloading workflow $workflowId...") implicit val system = ActorSystem() import system.dispatcher implicit val timeoutSeconds = timeout.seconds val pipeline: HttpRequest => Future[HttpResponse] = sendReceive val futureResponse = pipeline(Get(downloadUrl(workflowId))) futureResponse.onComplete { _ => Try(IO(Http).ask(Http.CloseAll)(1.second).await) sparkutils.AkkaUtils.terminate(system) } futureResponse.map(handleResponse) } private def handleResponse(response: HttpResponse): String = { response.status match { case StatusCodes.OK => response.entity.data.asString case _ => throw UnexpectedHttpResponseException( "Workflow download failed", response.status, response.entity.data.asString) } } }
Example 66
Source File: SessionService.scala From seahorse with Apache License 2.0 | 5 votes |
package ai.deepsense.sessionmanager.service import java.util.concurrent.TimeUnit import scala.concurrent.{ExecutionContext, Future} import scala.util.Try import scalaz.ListT._ import scalaz.OptionT import scalaz.OptionT._ import scalaz.std.scalaFuture._ import akka.actor.ActorRef import akka.pattern.ask import akka.util.Timeout import com.google.inject.Inject import com.google.inject.name.Named import ai.deepsense.commons.models.{ClusterDetails, Id} import ai.deepsense.commons.utils.Logging import ai.deepsense.sparkutils.ScalaUtils import ai.deepsense.sessionmanager.rest.responses.NodeStatusesResponse import ai.deepsense.sessionmanager.service.SessionService.FutureOpt import ai.deepsense.sessionmanager.service.actors.SessionServiceActor import ai.deepsense.sessionmanager.service.sessionspawner.{ExecutorSession, SessionConfig} class SessionService @Inject() ( @Named("SessionService.Actor") private val serviceActor: ActorRef, @Named("session-service.timeout") private val timeoutMillis: Int, @Named("predefined-users.admin.id") private val adminUserId: String )(implicit ec: ExecutionContext) extends Logging { private implicit val implicitTimeout = Timeout(timeoutMillis, TimeUnit.MILLISECONDS) def getSession(callerId: String, workflowId: Id): FutureOpt[Session] = { logger.info(s"Getting session '$workflowId'") getSessionImpl(callerId, workflowId) .map { _.sessionForApi() } } def createSession( sessionConfig: SessionConfig, clusterConfig: ClusterDetails): Future[Session] = { logger.info(s"Creating session with config $sessionConfig") (serviceActor ? SessionServiceActor.CreateRequest(sessionConfig, clusterConfig)).mapTo[Session] } def listSessions(callerId: String): Future[List[Session]] = { logger.info(s"Listing sessions") listT((serviceActor ? SessionServiceActor.ListRequest()).mapTo[List[ExecutorSession]]) .filter(session => isAuthorized(callerId = callerId, ownerId = session.sessionConfig.userId)) .map { _.sessionForApi() } .run } def killSession(callerId: String, workflowId: Id): FutureOpt[Unit] = getSessionImpl(callerId, workflowId) .map { _ => logger.info(s"Killing session '$workflowId'") (serviceActor ? SessionServiceActor.KillRequest(workflowId)).mapTo[Unit] } def launchSession(callerId: String, workflowId: Id): FutureOpt[Unit] = getSessionImpl(callerId, workflowId) .map { _ => logger.info(s"Launching nodes in session '$workflowId'") (serviceActor ? SessionServiceActor.LaunchRequest(workflowId)) .mapTo[Try[Unit]] .flatMap(ScalaUtils.futureFromTry) } def nodeStatusesQuery(callerId: String, workflowId: Id): FutureOpt[NodeStatusesResponse] = getSessionImpl(callerId, workflowId) .flatMapF { _ => logger.info(s"Asking for node statuses for session $workflowId") (serviceActor ? SessionServiceActor.NodeStatusesRequest(workflowId)) .mapTo[Try[NodeStatusesResponse]] .flatMap(ScalaUtils.futureFromTry) } private def isAuthorized(callerId: String, ownerId: String): Boolean = callerId == ownerId || callerId == adminUserId private def getSessionImpl(callerId: String, workflowId: Id): FutureOpt[ExecutorSession] = optionT((serviceActor ? SessionServiceActor.GetRequest(workflowId)) .mapTo[Option[ExecutorSession]]) .map { es => if (isAuthorized(callerId = callerId, ownerId = es.sessionConfig.userId)) { es } else { throw UnauthorizedOperationException( s"user $callerId accessing session '$workflowId' owned by ${es.sessionConfig.userId}") } } } object SessionService { type FutureOpt[A] = OptionT[Future, A] }
Example 67
Source File: RestModule.scala From seahorse with Apache License 2.0 | 5 votes |
package ai.deepsense.commons.rest import scala.concurrent.Await import scala.concurrent.duration._ import akka.actor.{ActorRef, ActorSystem} import akka.pattern.ask import akka.util.Timeout import com.google.inject.name.Named import com.google.inject.{AbstractModule, Provides, Singleton} import ai.deepsense.commons.akka.GuiceAkkaExtension class RestModule extends AbstractModule { override def configure(): Unit = { bind(classOf[RestServer]) } @Provides @Singleton @Named("ApiRouterActorRef") def provideApiRouterActorRef( @Named("server.startup.timeout") startupTimeout: Long, system: ActorSystem): ActorRef = { val supervisor = system.actorOf(GuiceAkkaExtension(system).props[RestServiceSupervisor], "RestSupervisor") val restServiceActorProps = GuiceAkkaExtension(system).props[RestServiceActor] implicit val timeout: Timeout = startupTimeout.seconds val actorRef = supervisor.ask((restServiceActorProps, "RestServiceActor")).mapTo[ActorRef] Await.result(actorRef, timeout.duration) } }
Example 68
Source File: ProxyBalancer.scala From shield with MIT License | 5 votes |
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 69
Source File: HttpTransport.scala From shield with MIT License | 5 votes |
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 70
Source File: DistributedProcessing.scala From aecor with MIT License | 5 votes |
package aecor.distributedprocessing import java.net.URLEncoder import java.nio.charset.StandardCharsets import aecor.distributedprocessing.DistributedProcessing.{ KillSwitch, Process } import aecor.distributedprocessing.DistributedProcessingWorker.KeepRunning import aecor.util.effect._ import akka.actor.ActorSystem import akka.cluster.sharding.{ ClusterSharding, ClusterShardingSettings } import akka.pattern.{ BackoffOpts, BackoffSupervisor, ask } import akka.util.Timeout import cats.effect.Effect import cats.implicits._ import scala.concurrent.duration.{ FiniteDuration, _ } final class DistributedProcessing private (system: ActorSystem) { def start[F[_]: Effect](name: String, processes: List[Process[F]], settings: DistributedProcessingSettings = DistributedProcessingSettings.default(system)): F[KillSwitch[F]] = Effect[F].delay { val opts = BackoffOpts .onFailure( DistributedProcessingWorker.props(processes, name), "worker", settings.minBackoff, settings.maxBackoff, settings.randomFactor ) val props = BackoffSupervisor.props(opts) val region = ClusterSharding(system).start( typeName = name, entityProps = props, settings = settings.clusterShardingSettings, extractEntityId = { case c @ KeepRunning(workerId) => (workerId.toString, c) }, extractShardId = { case KeepRunning(workerId) => (workerId % settings.numberOfShards).toString case other => throw new IllegalArgumentException(s"Unexpected message [$other]") } ) val regionSupervisor = system.actorOf( DistributedProcessingSupervisor .props(processes.size, region, settings.heartbeatInterval), "DistributedProcessingSupervisor-" + URLEncoder .encode(name, StandardCharsets.UTF_8.name()) ) implicit val timeout = Timeout(settings.shutdownTimeout) KillSwitch { Effect[F].fromFuture { regionSupervisor ? DistributedProcessingSupervisor.GracefulShutdown }.void } } } object DistributedProcessing { def apply(system: ActorSystem): DistributedProcessing = new DistributedProcessing(system) final case class KillSwitch[F[_]](shutdown: F[Unit]) extends AnyVal final case class RunningProcess[F[_]](watchTermination: F[Unit], shutdown: F[Unit]) final case class Process[F[_]](run: F[RunningProcess[F]]) extends AnyVal } final case class DistributedProcessingSettings(minBackoff: FiniteDuration, maxBackoff: FiniteDuration, randomFactor: Double, shutdownTimeout: FiniteDuration, numberOfShards: Int, heartbeatInterval: FiniteDuration, clusterShardingSettings: ClusterShardingSettings) object DistributedProcessingSettings { def default(clusterShardingSettings: ClusterShardingSettings): DistributedProcessingSettings = DistributedProcessingSettings( minBackoff = 3.seconds, maxBackoff = 10.seconds, randomFactor = 0.2, shutdownTimeout = 10.seconds, numberOfShards = 100, heartbeatInterval = 2.seconds, clusterShardingSettings = clusterShardingSettings ) def default(system: ActorSystem): DistributedProcessingSettings = default(ClusterShardingSettings(system)) }
Example 71
Source File: AkkaPersistenceRuntime.scala From aecor with MIT License | 5 votes |
package aecor.runtime.akkapersistence import aecor.data.{ EventsourcedBehavior, Tagging } import aecor.encoding.WireProtocol.Encoded import aecor.encoding.syntax._ import aecor.encoding.{ KeyDecoder, KeyEncoder, WireProtocol } import aecor.runtime.akkapersistence.AkkaPersistenceRuntime._ import aecor.runtime.akkapersistence.AkkaPersistenceRuntimeActor.CommandResult import aecor.runtime.akkapersistence.readside.{ AkkaPersistenceEventJournalQuery, JournalQuery } import aecor.runtime.akkapersistence.serialization.{ Message, PersistentDecoder, PersistentEncoder } import aecor.util.effect._ import akka.actor.ActorSystem import akka.cluster.sharding.{ ClusterSharding, ShardRegion } import akka.pattern.ask import akka.util.Timeout import cats.effect.Effect import cats.implicits._ import cats.tagless.FunctorK import cats.tagless.syntax.functorK._ import cats.~> import scodec.bits.BitVector object AkkaPersistenceRuntime { def apply[O](system: ActorSystem, journalAdapter: JournalAdapter[O]): AkkaPersistenceRuntime[O] = new AkkaPersistenceRuntime(system, journalAdapter) private[akkapersistence] final case class EntityCommand(entityKey: String, commandBytes: BitVector) extends Message } class AkkaPersistenceRuntime[O] private[akkapersistence] (system: ActorSystem, journalAdapter: JournalAdapter[O]) { def deploy[M[_[_]]: FunctorK, F[_], State, Event: PersistentEncoder: PersistentDecoder, K: KeyEncoder: KeyDecoder]( typeName: String, behavior: EventsourcedBehavior[M, F, State, Event], tagging: Tagging[K], snapshotPolicy: SnapshotPolicy[State] = SnapshotPolicy.never, settings: AkkaPersistenceRuntimeSettings = AkkaPersistenceRuntimeSettings.default(system) )(implicit M: WireProtocol[M], F: Effect[F]): F[K => M[F]] = F.delay { val props = AkkaPersistenceRuntimeActor.props( typeName, behavior, snapshotPolicy, tagging, settings.idleTimeout, journalAdapter.writeJournalId, snapshotPolicy.pluginId ) val extractEntityId: ShardRegion.ExtractEntityId = { case EntityCommand(entityId, bytes) => (entityId, AkkaPersistenceRuntimeActor.HandleCommand(bytes)) } val numberOfShards = settings.numberOfShards val extractShardId: ShardRegion.ExtractShardId = { case EntityCommand(entityId, _) => (scala.math.abs(entityId.hashCode) % numberOfShards).toString case other => throw new IllegalArgumentException(s"Unexpected message [$other]") } val shardRegion = ClusterSharding(system).start( typeName = typeName, entityProps = props, settings = settings.clusterShardingSettings, extractEntityId = extractEntityId, extractShardId = extractShardId ) val keyEncoder = KeyEncoder[K] key => M.encoder.mapK(new (Encoded ~> F) { implicit val askTimeout: Timeout = Timeout(settings.askTimeout) override def apply[A](fa: Encoded[A]): F[A] = F.suspend { val (bytes, decoder) = fa F.fromFuture { shardRegion ? EntityCommand(keyEncoder(key), bytes) } .flatMap { case CommandResult(resultBytes) => decoder.decodeValue(resultBytes).lift[F] case other => F.raiseError( new IllegalArgumentException(s"Unexpected response [$other] from shard region") ) } } }) } def journal[K: KeyDecoder, E: PersistentDecoder]: JournalQuery[O, K, E] = AkkaPersistenceEventJournalQuery[O, K, E](journalAdapter) }
Example 72
Source File: Route.scala From distributed-cache-on-k8s-poc with MIT License | 5 votes |
package http import akka.actor.ActorSystem import akka.cluster.sharding.ClusterSharding import akka.http.scaladsl.marshalling.Marshaller.StringMarshaller import akka.http.scaladsl.model._ import akka.http.scaladsl.server.Directives._ import akka.pattern.ask import akka.util.Timeout import cluster.CacheDataActor.Get import cluster.ClusterShardRegion import scala.concurrent.duration._ import scala.util.{ Failure, Success } class Route(system: ActorSystem) { implicit val timeout: Timeout = 3.seconds val shardRegionActor = new ClusterShardRegion(system).clusterShardRegion def routes: akka.http.scaladsl.server.Route = path("health") { get { complete(StatusCodes.OK) } } ~ path("cache-data" / JavaUUID) { id => get { onComplete((shardRegionActor ? Get(id)).mapTo[String]) { case Success(s) => complete(s) case Failure(f) => complete(f) } } } }
Example 73
Source File: writers.scala From mist with Apache License 2.0 | 5 votes |
package io.hydrosphere.mist.master.logging import java.nio.file._ import akka.actor._ import akka.pattern.ask import akka.util.Timeout import io.hydrosphere.mist.core.logging.LogEvent import io.hydrosphere.mist.master.LogStoragePaths import scala.concurrent.Future import scala.concurrent.duration._ case class WriteRequest( id: String, events: Seq[LogEvent] ) case class LogUpdate( jobId: String, events: Seq[LogEvent], bytesOffset: Long ) class WritersGroupActor(paths: LogStoragePaths) extends Actor { var idToRef = Map.empty[String, ActorRef] var refToId = Map.empty[ActorRef, String] override def receive: Receive = { case req: WriteRequest => idToRef.get(req.id) match { case Some(ref) => ref.forward(req) case None => create(req.id).forward(req) } case Terminated(ref) => remove(ref) } def create(id: String): ActorRef = { val path = paths.pathFor(id) val props = WriterActor.props(path).withDispatcher("writers-blocking-dispatcher") val actor = context.actorOf(props, s"writer-$id") context.watch(actor) idToRef += id -> actor refToId += actor -> id actor } def remove(ref: ActorRef): Unit = { refToId.get(ref).foreach(id => { refToId -= ref idToRef -= id }) } } object WritersGroupActor { def props(paths: LogStoragePaths): Props = { Props(classOf[WritersGroupActor], paths) } } trait LogsWriter { def write(from: String, events: Seq[LogEvent]): Future[LogUpdate] } object LogsWriter { def apply(paths: LogStoragePaths, f: ActorRefFactory): LogsWriter = { new LogsWriter { val actor = f.actorOf(WritersGroupActor.props(paths), "writers-group") implicit val timeout = Timeout(10 second) override def write(from: String, events: Seq[LogEvent]): Future[LogUpdate] = { val f = actor ? WriteRequest(from, events) f.mapTo[LogUpdate] } } } }
Example 74
Source File: LogWriterSpec.scala From mist with Apache License 2.0 | 5 votes |
package io.hydrosphere.mist.master.logging import java.nio.file.{Files, Paths} import akka.actor.ActorSystem import akka.pattern.ask import akka.testkit.{TestActorRef, TestKit} import akka.util.Timeout import com.typesafe.config.ConfigFactory import io.hydrosphere.mist.core.logging.LogEvent import io.hydrosphere.mist.master.LogStoragePaths import org.apache.commons.io.FileUtils import org.scalatest.{BeforeAndAfterAll, FunSpecLike, Matchers} import scala.concurrent.Await import scala.concurrent.duration._ class LogWriterSpec extends TestKit(ActorSystem("log-writer-test", ConfigFactory.load("master"))) with FunSpecLike with Matchers with BeforeAndAfterAll { val dirName = "log_writer_test" val dir = Paths.get(".", "target", dirName) override def beforeAll(): Unit = { Files.createDirectories(dir) } override def afterAll(): Unit = { FileUtils.deleteDirectory(dir.toFile) TestKit.shutdownActorSystem(system) } implicit val timeout = Timeout(5 second) describe("writer actor") { it("should write to file") { val path = dir.resolve("test") val f = path.toFile if (f.exists()) f.delete() Files.createFile(path) val actor = TestActorRef(WriterActor.props(path)) val event = LogEvent.mkDebug("id", "message") val future = actor ? WriteRequest("id", Seq(event)) val update = Await.result(future.mapTo[LogUpdate], Duration.Inf) update.jobId shouldBe "id" update.events shouldBe Seq(event) update.bytesOffset shouldBe (event.mkString + "\n").getBytes.length } } describe("writers group") { it("should proxy to writer") { val mappings = new LogStoragePaths(dir) val expectedPath = mappings.pathFor("id") if (Files.exists(expectedPath)) Files.delete(expectedPath) val actor = TestActorRef(WritersGroupActor.props(mappings)) val event = LogEvent.mkDebug("id", "message") val future = actor ? WriteRequest("id", Seq(event)) val update = Await.result(future.mapTo[LogUpdate], Duration.Inf) val expectedSize = (event.mkString + "\n").getBytes.length update.jobId shouldBe "id" update.events shouldBe Seq(event) update.bytesOffset shouldBe expectedSize Files.readAllBytes(expectedPath).length shouldBe expectedSize } } }
Example 75
Source File: GreeterServiceImpl.scala From akka-grpc with Apache License 2.0 | 5 votes |
package example.myapp.statefulhelloworld import example.myapp.statefulhelloworld.grpc.GreeterService import example.myapp.statefulhelloworld.grpc.{ ChangeRequest, ChangeResponse, HelloReply, HelloRequest } import akka.actor.ActorSystem import akka.pattern.ask import akka.util.Timeout import scala.concurrent.duration._ import scala.concurrent.Future // #stateful-service class GreeterServiceImpl(system: ActorSystem) extends GreeterService { val greeterActor = system.actorOf(GreeterActor.props("Hello"), "greeter") def sayHello(in: HelloRequest): Future[HelloReply] = { // timeout and execution context for ask implicit val timeout: Timeout = 3.seconds import system.dispatcher (greeterActor ? GreeterActor.GetGreeting) .mapTo[GreeterActor.Greeting] .map(message => HelloReply(s"${message.greeting}, ${in.name}")) } def changeGreeting(in: ChangeRequest): Future[ChangeResponse] = { greeterActor ! GreeterActor.ChangeGreeting(in.newGreeting) Future.successful(ChangeResponse()) } } // #stateful-service
Example 76
Source File: NodeRoutes.scala From akka-cluster-playground with MIT License | 5 votes |
package com.elleflorio.cluster.playground.api import akka.actor.{ActorRef, ActorSystem} import akka.http.scaladsl.marshallers.sprayjson._ import akka.http.scaladsl.model.StatusCodes import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Route import akka.pattern.ask import akka.util.Timeout import scala.concurrent.ExecutionContext.Implicits.global import com.elleflorio.cluster.playground.node.processor.ProcessorResponseJsonProtocol._ import com.elleflorio.cluster.playground.node.Node.{GetClusterMembers, GetFibonacci} import com.elleflorio.cluster.playground.node.processor.ProcessorResponse import scala.concurrent.Future import scala.concurrent.duration._ trait NodeRoutes extends SprayJsonSupport { implicit def system: ActorSystem def node: ActorRef implicit lazy val timeout = Timeout(5.seconds) lazy val healthRoute: Route = pathPrefix("health") { concat( pathEnd { concat( get { complete(StatusCodes.OK) } ) } ) } lazy val statusRoutes: Route = pathPrefix("status") { concat( pathPrefix("members") { concat( pathEnd { concat( get { val membersFuture: Future[List[String]] = (node ? GetClusterMembers).mapTo[List[String]] onSuccess(membersFuture) { members => complete(StatusCodes.OK, members) } } ) } ) } ) } lazy val processRoutes: Route = pathPrefix("process") { concat( pathPrefix("fibonacci") { concat( path(IntNumber) { n => pathEnd { concat( get { val processFuture: Future[ProcessorResponse] = (node ? GetFibonacci(n)).mapTo[ProcessorResponse] onSuccess(processFuture) { response => complete(StatusCodes.OK, response) } } ) } } ) } ) } }
Example 77
Source File: QueryValidationApi.scala From NSDb with Apache License 2.0 | 5 votes |
package io.radicalbit.nsdb.web.routes import akka.actor.ActorRef import akka.event.LoggingAdapter import akka.http.scaladsl.model.HttpResponse import akka.http.scaladsl.model.StatusCodes._ import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Route import akka.pattern.ask import akka.util.Timeout import io.radicalbit.nsdb.common.statement.SelectSQLStatement import io.radicalbit.nsdb.protocol.MessageProtocol.Commands.ValidateStatement import io.radicalbit.nsdb.protocol.MessageProtocol.Events._ import io.radicalbit.nsdb.security.http.NSDBAuthProvider import io.radicalbit.nsdb.security.model.Metric import io.radicalbit.nsdb.sql.parser.SQLStatementParser import io.radicalbit.nsdb.sql.parser.StatementParserResult._ import io.swagger.annotations._ import javax.ws.rs.Path import org.json4s.Formats import scala.annotation.meta.field import scala.util.{Failure, Success} @ApiModel(description = "Query Validation body") case class QueryValidationBody(@(ApiModelProperty @field)(value = "database name ") db: String, @(ApiModelProperty @field)(value = "namespace name ") namespace: String, @(ApiModelProperty @field)(value = "metric name ") metric: String, @(ApiModelProperty @field)(value = "sql query string") queryString: String) extends Metric @Api(value = "/query/validate", produces = "application/json") @Path("/query/validate") trait QueryValidationApi { import io.radicalbit.nsdb.web.NSDbJson._ def readCoordinator: ActorRef def authenticationProvider: NSDBAuthProvider implicit val timeout: Timeout implicit val formats: Formats @ApiOperation(value = "Perform query", nickname = "query", httpMethod = "POST", response = classOf[String]) @ApiImplicitParams( Array( new ApiImplicitParam(name = "body", value = "query definition", required = true, dataTypeClass = classOf[QueryValidationBody], paramType = "body") )) @ApiResponses( Array( new ApiResponse(code = 200, message = "Query is valid"), new ApiResponse(code = 404, message = "Not found item reason"), new ApiResponse(code = 400, message = "statement is invalid") )) def queryValidationApi(implicit logger: LoggingAdapter): Route = { path("query" / "validate") { post { entity(as[QueryValidationBody]) { qb => optionalHeaderValueByName(authenticationProvider.headerName) { header => authenticationProvider.authorizeMetric(ent = qb, header = header, writePermission = false) { new SQLStatementParser().parse(qb.db, qb.namespace, qb.queryString) match { case SqlStatementParserSuccess(_, statement: SelectSQLStatement) => onComplete(readCoordinator ? ValidateStatement(statement)) { case Success(SelectStatementValidated(_)) => complete(HttpResponse(OK)) case Success(SelectStatementValidationFailed(_, reason, MetricNotFound(_))) => complete(HttpResponse(NotFound, entity = reason)) case Success(SelectStatementValidationFailed(_, reason, _)) => complete(HttpResponse(BadRequest, entity = reason)) case Success(r) => logger.error("unknown response received {}", r) complete(HttpResponse(InternalServerError, entity = "unknown response")) case Failure(ex) => logger.error("", ex) complete(HttpResponse(InternalServerError, entity = ex.getMessage)) } case SqlStatementParserSuccess(queryString, _) => complete(HttpResponse(BadRequest, entity = s"statement ${queryString} is not a select statement")) case SqlStatementParserFailure(queryString, _) => complete(HttpResponse(BadRequest, entity = s"statement ${queryString} is invalid")) } } } } } } } }
Example 78
Source File: DataApi.scala From NSDb with Apache License 2.0 | 5 votes |
package io.radicalbit.nsdb.web.routes import javax.ws.rs.Path import akka.actor.ActorRef import akka.pattern.ask import akka.http.scaladsl.model.HttpResponse import akka.http.scaladsl.model.StatusCodes.{BadRequest, InternalServerError} import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Route import akka.util.Timeout import io.radicalbit.nsdb.common.protocol.Bit import io.radicalbit.nsdb.protocol.MessageProtocol.Commands.MapInput import io.radicalbit.nsdb.protocol.MessageProtocol.Events.{InputMapped, RecordRejected} import io.radicalbit.nsdb.security.http.NSDBAuthProvider import io.radicalbit.nsdb.security.model.Metric import io.swagger.annotations._ import org.json4s.Formats import scala.annotation.meta.field import scala.util.{Failure, Success} @ApiModel(description = "Data insertion body") case class InsertBody(@(ApiModelProperty @field)(value = "database name") db: String, @(ApiModelProperty @field)(value = "namespace name") namespace: String, @(ApiModelProperty @field)(value = "metric name") metric: String, @(ApiModelProperty @field)( value = "bit representing a single row" ) bit: Bit) extends Metric @Api(value = "/data", produces = "application/json") @Path("/data") trait DataApi { import io.radicalbit.nsdb.web.NSDbJson._ import io.radicalbit.nsdb.web.validation.ValidationDirective._ import io.radicalbit.nsdb.web.validation.Validators._ def writeCoordinator: ActorRef def authenticationProvider: NSDBAuthProvider implicit val timeout: Timeout implicit val formats: Formats @ApiOperation(value = "Insert Bit", nickname = "insert", httpMethod = "POST", response = classOf[String]) @ApiImplicitParams( Array( new ApiImplicitParam(name = "body", value = "bit definition", required = true, dataTypeClass = classOf[InsertBody], paramType = "body") )) @ApiResponses( Array( new ApiResponse(code = 500, message = "Internal server error"), new ApiResponse(code = 400, message = "insert statement is invalid") )) def dataApi: Route = pathPrefix("data") { post { entity(as[InsertBody]) { insertBody => optionalHeaderValueByName(authenticationProvider.headerName) { header => validateModel(insertBody).apply { validatedInsertBody => authenticationProvider.authorizeMetric(ent = validatedInsertBody, header = header, writePermission = true) { onComplete( writeCoordinator ? MapInput(validatedInsertBody.bit.timestamp, validatedInsertBody.db, validatedInsertBody.namespace, validatedInsertBody.metric, validatedInsertBody.bit)) { case Success(_: InputMapped) => complete("OK") case Success(RecordRejected(_, _, _, _, _, reasons, _)) => complete(HttpResponse(BadRequest, entity = reasons.mkString(","))) case Success(_) => complete(HttpResponse(InternalServerError, entity = "unknown response")) case Failure(ex) => complete(HttpResponse(InternalServerError, entity = ex.getMessage)) } } } } } } } }
Example 79
Source File: CommitLogCoordinator.scala From NSDb with Apache License 2.0 | 5 votes |
package io.radicalbit.nsdb.cluster.coordinator import java.util.concurrent.TimeUnit import akka.actor.ActorRef import akka.pattern.{ask, pipe} import akka.util.Timeout import io.radicalbit.nsdb.actors.MetricPerformerActor import io.radicalbit.nsdb.actors.MetricPerformerActor.PersistedBits import io.radicalbit.nsdb.commit_log.CommitLogWriterActor._ import io.radicalbit.nsdb.commit_log.RollingCommitLogFileWriter import io.radicalbit.nsdb.util.ActorPathLogging import scala.concurrent.Future class CommitLogCoordinator extends ActorPathLogging { private def getWriter(db: String, namespace: String, metric: String): ActorRef = context .child(s"commit-log-writer-$db-$namespace-$metric") .getOrElse( context.actorOf(RollingCommitLogFileWriter.props(db, namespace, metric), s"commit-log-writer-$db-$namespace-$metric") ) implicit val timeout: Timeout = Timeout( context.system.settings.config.getDuration("nsdb.write-coordinator.timeout", TimeUnit.SECONDS), TimeUnit.SECONDS) def receive: Receive = { case msg @ WriteToCommitLog(db, namespace, metric, _, _, _) => getWriter(db, namespace, metric).forward(msg) case persistedBits: PersistedBits => import context.dispatcher // Handle successful events of Bit Persistence val successfullyPersistedBits = persistedBits.persistedBits val successfulCommitLogResponses: Future[Seq[WriteToCommitLogSucceeded]] = Future.sequence { successfullyPersistedBits.map { persistedBit => (getWriter(persistedBit.db, persistedBit.namespace, persistedBit.metric) ? WriteToCommitLog(persistedBit.db, persistedBit.namespace, persistedBit.metric, persistedBit.timestamp, PersistedEntryAction(persistedBit.bit), persistedBit.location)).collect { case s: WriteToCommitLogSucceeded => s } } } val response = successfulCommitLogResponses.map { responses => if (responses.size == successfullyPersistedBits.size) MetricPerformerActor.PersistedBitsAck else context.system.terminate() } response.pipeTo(sender()) case _ => log.error("UnexpectedMessage") } }
Example 80
Source File: WriteCoordinatorSpec.scala From NSDb with Apache License 2.0 | 5 votes |
package io.radicalbit.nsdb.cluster.coordinator import akka.actor.ActorSystem import akka.pattern.ask import akka.testkit.{ImplicitSender, TestKit} import akka.util.Timeout import com.typesafe.config.{ConfigFactory, ConfigValueFactory} import io.radicalbit.nsdb.protocol.MessageProtocol.Commands._ import org.scalatest._ import scala.concurrent.Await import scala.concurrent.duration._ class WriteCoordinatorSpec extends TestKit( ActorSystem( "WriteCoordinatorSpec", ConfigFactory .load() .withValue("nsdb.sharding.interval", ConfigValueFactory.fromAnyRef("5s")) )) with ImplicitSender with WordSpecLike with Matchers with BeforeAndAfter with BeforeAndAfterAll with WriteCoordinatorBehaviour { lazy val basePath = "target/test_index/WriteCoordinatorSpec" val db = "writeCoordinatorSpecDB" val namespace = "namespace" implicit val timeout = Timeout(10 seconds) override def beforeAll: Unit = { Await.result(writeCoordinatorActor ? SubscribeCommitLogCoordinator(commitLogCoordinator, "localhost"), 10 seconds) Await.result(writeCoordinatorActor ? SubscribeMetricsDataActor(metricsDataActor, "localhost"), 10 seconds) Await.result(writeCoordinatorActor ? SubscribePublisher(publisherActor, "localhost"), 10 seconds) Await.result(writeCoordinatorActor ? DeleteNamespace(db, namespace), 10 seconds) Await.result(schemaCoordinator ? UpdateSchemaFromRecord(db, namespace, "testMetric", record1), 10 seconds) } "WriteCoordinator" should behave.like(defaultBehaviour) }
Example 81
Source File: ClusterApi.scala From asura with MIT License | 5 votes |
package asura.app.api import akka.actor.ActorSystem import akka.pattern.ask import akka.util.Timeout import asura.app.AppErrorMessages import asura.cluster.ClusterManager import asura.cluster.actor.MemberListenerActor.GetAllMembers import asura.common.model.ApiResError import asura.core.CoreConfig import asura.play.api.BaseApi.OkApiRes import javax.inject.{Inject, Singleton} import org.pac4j.play.scala.SecurityComponents import play.api.Configuration import play.api.mvc.{RequestHeader, Result} import scala.concurrent.{ExecutionContext, Future} @Singleton class ClusterApi @Inject()( implicit val system: ActorSystem, val exec: ExecutionContext, val configuration: Configuration, val controllerComponents: SecurityComponents ) extends BaseApi { implicit val timeout: Timeout = CoreConfig.DEFAULT_ACTOR_ASK_TIMEOUT def getMembers() = Action(parse.byteString).async { implicit req => checkClusterEnabled { (ClusterManager.clusterManagerActor ? GetAllMembers).toOkResult } } private def checkClusterEnabled(func: => Future[Result])(implicit request: RequestHeader): Future[Result] = { if (ClusterManager.enabled) { func } else { Future.successful(OkApiRes(ApiResError(getI18nMessage(AppErrorMessages.error_ClusterNotEnabled)))) } } }
Example 82
Source File: GenericServiceInvokerActor.scala From asura with MIT License | 5 votes |
package asura.dubbo.actor import akka.actor.{Props, Status} import akka.pattern.{ask, pipe} import akka.util.Timeout import asura.common.actor.BaseActor import asura.common.util.LogUtils import asura.dubbo.actor.GenericServiceInvokerActor.{GetInterfaceMethodParams, GetInterfacesMessage, GetProvidersMessage} import asura.dubbo.{DubboConfig, GenericRequest} import scala.concurrent.{ExecutionContext, Future} class GenericServiceInvokerActor extends BaseActor { implicit val ec: ExecutionContext = context.dispatcher implicit val timeout: Timeout = DubboConfig.DEFAULT_ACTOR_ASK_TIMEOUT val curatorClientCacheActor = context.actorOf(CuratorClientCacheActor.props()) val referenceActor = context.actorOf(DubboReferenceCacheActor.props()) override def receive: Receive = { case msg: GetInterfacesMessage => curatorClientCacheActor ? msg pipeTo sender() case msg: GetProvidersMessage => curatorClientCacheActor ? msg pipeTo sender() case msg: GenericRequest => referenceActor ? msg pipeTo sender() case msg: GetInterfaceMethodParams => context.actorOf(InterfaceMethodParamsActor.props(sender(), msg)) case Status.Failure(t) => log.warning(LogUtils.stackTraceToString(t)) Future.failed(t) pipeTo sender() case _ => Future.failed(new RuntimeException("Unknown message type")) pipeTo sender() } override def postStop(): Unit = { DubboConfig.DUBBO_EC.shutdown() } } object GenericServiceInvokerActor { def props() = Props(new GenericServiceInvokerActor()) case class GetInterfacesMessage( zkConnectString: String, path: String, zkUsername: String = null, zkPassword: String = null, ) case class GetProvidersMessage( zkConnectString: String, path: String, ref: String, zkUsername: String = null, zkPassword: String = null, ) case class GetInterfaceMethodParams(address: String, port: Int, ref: String) }
Example 83
Source File: SqlRequestInvokerActor.scala From asura with MIT License | 5 votes |
package asura.core.sql.actor import java.sql.Connection import akka.actor.Props import akka.pattern.{ask, pipe} import akka.util.Timeout import asura.common.actor.BaseActor import asura.common.util.FutureUtils import asura.core.CoreConfig import asura.core.es.model.SqlRequest.SqlRequestBody import asura.core.sql.actor.MySqlConnectionCacheActor.GetConnectionMessage import asura.core.sql.{MySqlConnector, SqlConfig, SqlParserUtils} import scala.concurrent.{ExecutionContext, Future} class SqlRequestInvokerActor extends BaseActor { implicit val ec: ExecutionContext = context.dispatcher implicit val timeout: Timeout = CoreConfig.DEFAULT_ACTOR_ASK_TIMEOUT val connectionCacheActor = context.actorOf(MySqlConnectionCacheActor.props()) override def receive: Receive = { case requestBody: SqlRequestBody => getResponse(requestBody) pipeTo sender() case _ => Future.failed(new RuntimeException("Unknown message type")) pipeTo sender() } def getResponse(requestBody: SqlRequestBody): Future[Object] = { implicit val sqlEc = SqlConfig.SQL_EC val futConn = (connectionCacheActor ? GetConnectionMessage(requestBody)).asInstanceOf[Future[Connection]] val (isOk, errMsg) = SqlParserUtils.isSelectStatement(requestBody.sql) if (null == errMsg) { futConn.flatMap(conn => { if (isOk) { Future { MySqlConnector.executeQuery(conn, requestBody.sql) } } else { Future { MySqlConnector.executeUpdate(conn, requestBody.sql) } } }) } else { FutureUtils.requestFail(errMsg) } } } object SqlRequestInvokerActor { def props() = Props(new SqlRequestInvokerActor()) }
Example 84
Source File: SqlRunner.scala From asura with MIT License | 5 votes |
package asura.core.sql import akka.pattern.ask import akka.util.Timeout import asura.common.exceptions.WithDataException import asura.core.concurrent.ExecutionContextManager.sysGlobal import asura.core.es.model.SqlRequest import asura.core.es.model.SqlRequest.SqlRequestBody import asura.core.runtime.{ContextOptions, RuntimeContext, RuntimeMetrics} import asura.core.sql.SqlReportModel.{SqlRequestReportModel, SqlResponseReportModel} import asura.core.{CoreConfig, RunnerActors} import com.typesafe.scalalogging.Logger import scala.concurrent.Future object SqlRunner { val logger = Logger("SqlRunner") implicit val timeout: Timeout = CoreConfig.DEFAULT_ACTOR_ASK_TIMEOUT lazy val sqlInvoker = RunnerActors.sqlInvoker def test(docId: String, request: SqlRequest, context: RuntimeContext = RuntimeContext()): Future[SqlResult] = { implicit val metrics = RuntimeMetrics() metrics.start() context.eraseCurrentData() var options = context.options if (null != options) { options.caseEnv = request.env } else { options = ContextOptions(caseEnv = request.env) context.options = options } metrics.renderRequestStart() context.evaluateOptions().flatMap(_ => { renderRequest(request.request, context) .flatMap(tuple => { metrics.performRequestStart() (sqlInvoker ? tuple._1).flatMap(responseObj => { context.setCurrentEntity(responseObj.asInstanceOf[Object]) metrics.evalAssertionBegin() context.setCurrentMetrics(metrics) SqlResult.evaluate( docId, request.assert, context, tuple._2, SqlResponseReportModel(responseObj.asInstanceOf[Object]) ) }).recover { case t: Throwable => throw WithDataException(t, tuple._2) } }) .map(result => { metrics.evalAssertionEnd() metrics.theEnd() result.metrics = metrics.toReportStepItemMetrics() result }) }) } def renderRequest(request: SqlRequestBody, context: RuntimeContext) (implicit metrics: RuntimeMetrics): Future[(SqlRequestBody, SqlRequestReportModel)] = { val host = request.host val port = request.port val database = request.database val sql = context.renderBodyAsString(request.sql) metrics.renderRequestEnd() metrics.renderAuthBegin() metrics.renderAuthEnd() val renderedRequest = request.copyFrom(host, port, database, sql) val reportModel = SqlRequestReportModel( host = request.host, port = request.port, username = request.username, database = request.database, table = request.table, sql = sql ) Future.successful((renderedRequest, reportModel)) } }
Example 85
package com.ebay.neutrino.www import java.util.concurrent.TimeUnit import akka.actor.{ActorRef, Props, ActorSystem, ScalaActorRef} import akka.pattern.ask import akka.util.Timeout import com.ebay.neutrino.{SLB, NeutrinoPoolId} import com.ebay.neutrino.api.ApiData import com.ebay.neutrino.cluster.{SLBTopology, SystemConfiguration} import com.ebay.neutrino.www.ui.SideMenu import com.ebay.neutrino.www.ui.PageFormatting import com.ebay.neutrino.cluster.SLBLoader import com.typesafe.config.ConfigRenderOptions import com.typesafe.scalalogging.slf4j.StrictLogging import spray.http.StatusCodes import scala.concurrent.Await import scala.concurrent.duration.FiniteDuration import scala.concurrent.ExecutionContext.Implicits.global import scala.util.{Failure, Success} trait WebService extends spray.routing.HttpService with ApiData with PageFormatting with StrictLogging { def system: ActorSystem def topology = SystemConfiguration(system).topology val poolPage = new SideMenu("Pools") with PoolsPage val serverPage = new SideMenu("Servers") with ServersPage def webRoutes = path ("activity") { complete { import PageFormatting.ScalaTagsPrettyMarshaller ActivityPage.page() } } ~ path("pools") { complete { import PageFormatting.ScalaTagsPrettyMarshaller poolPage.summary(topology.toSeq) } } ~ path("pools" / Segment) { id => complete { import PageFormatting.ScalaTagsPrettyMarshaller val pool = topology.getPool(NeutrinoPoolId(id)) poolPage.detail(pool) } } ~ path("servers") { complete { import PageFormatting.ScalaTagsPrettyMarshaller val pools = topology.toSeq val services = topology.asInstanceOf[SLBTopology].core.services val nodes = services flatMap (_.pools()) flatMap (_.nodes()) serverPage.summary(pools, nodes.toSeq) } } ~ path("refresh") { complete { import PageFormatting.ScalaTagsPrettyMarshaller implicit val timeout = Timeout(FiniteDuration(3, TimeUnit.SECONDS)) // Wait for the result, since refresh api has to be synchronous val reloader = Await.result(system.actorSelection("user/loader").resolveOne(), timeout.duration) val future = reloader ? "reload" val result = Await.result(future, timeout.duration) if (result == "complete") { logger.warn("Config reloaded, Successfully completed") } else { logger.warn("Unable to load the configuration") } poolPage.summary(topology.toSeq) } } ~ path("config") { complete { val sysconfig = SystemConfiguration(system) sysconfig.config.root.render(ConfigRenderOptions.defaults) } } ~ pathEndOrSingleSlash { complete { import PageFormatting.ScalaTagsPrettyMarshaller Overview.generate(generateStatus()) } } ~ get { redirect("/", StatusCodes.PermanentRedirect) } }
Example 86
Source File: KeepAliveTest.scala From Neutrino with Apache License 2.0 | 5 votes |
package com.ebay.neutrino.channel import akka.actor.ActorSystem import akka.io.IO import akka.pattern.ask import akka.util.Timeout import com.ebay.neutrino.{EchoIntegrationTest, ProxyIntegrationTest} import org.scalatest._ import spray.can.Http import spray.http._ import scala.concurrent.Await import scala.concurrent.duration._ class KeepAliveTest extends FlatSpec with ProxyIntegrationTest with EchoIntegrationTest with Matchers { behavior of "KeepAlive integration support" // Default requests override val configurationFile = "proxy-echo-test.conf" implicit val system = ActorSystem() implicit val timeout: Timeout = 15 seconds val Get = HttpRequest(HttpMethods.GET, Uri("http://localhost:8080/someurl")) def send(request: HttpRequest) = (IO(Http) ? request).mapTo[HttpResponse] it should "successfully send a single HTTP 1.1 GET" in { val request = Get val response = Await.result(send(request), 2 seconds) //response.status should be (StatusCodes.OK) //response.protocol should be (HttpProtocols.`HTTP/1.1`) //response.header[HttpHeaders.Connection] shouldBe a [Some[HttpHeader]] //response.headers find (_ == HttpHeaders.Connection) .hasKeepAlive should be (false) } it should "successfully send a single HTTP 1.1 GET with connection-close" in { val start = System.currentTimeMillis val request = Get.copy(headers = List(HttpHeaders.Connection("close"))) val response = Await.result(send(request), 2 seconds) response.status should be (StatusCodes.OK) response.protocol should be (HttpProtocols.`HTTP/1.1`) response.header[HttpHeaders.Connection] should be (Some(HttpHeaders.Connection("close"))) } it should "successfully send a single HTTP 1.0 GET" in { val request = Get.copy(protocol=HttpProtocols.`HTTP/1.0`) val response = Await.result(send(request), 2 seconds) // Regardless of Client:1.0, server should support 1.1 response.status should be (StatusCodes.OK) response.protocol should be (HttpProtocols.`HTTP/1.1`) // response.headers should not contain keep-alive } it should "successfully send a single HTTP 1.0 GET w. keepalive" in { val request = Get.copy(protocol=HttpProtocols.`HTTP/1.0`, headers = List(HttpHeaders.Connection("Keep-Alive"))) val response = Await.result(send(request), 2 seconds) // Regardless of Client:1.0, server should support 1.1 response.status should be (StatusCodes.OK) response.protocol should be (HttpProtocols.`HTTP/1.1`) response.header[HttpHeaders.Connection] shouldBe a [Some[_]] response.header[HttpHeaders.Connection].get.hasKeepAlive should be (true) } }
Example 87
Source File: Main.scala From qamr with MIT License | 5 votes |
package qamr.analysis import qamr.example._ import qamr.util._ import qamr._ import spacro._ import spacro.tasks._ import spacro.util._ import akka.pattern.ask import scala.concurrent.duration._ import cats.implicits._ import com.amazonaws.services.mturk._ import com.amazonaws.services.mturk.model._ import nlpdata.util.Text import nlpdata.util.HasTokens.ops._ import nlpdata.datasets.wiktionary.Inflections import nlpdata.datasets.wiktionary.WiktionaryFileSystemService import java.nio.file.Paths object Main extends App { val domain = "localhost" val isProduction = false val interface = "0.0.0.0" val httpPort = 8888 val httpsPort = 8080 val rootPath = java.nio.file.Paths.get(".") val dataPath = rootPath.resolve("data/example") val annotationPath = dataPath.resolve("annotations") val resourcePath = rootPath.resolve("datasets") implicit val timeout = akka.util.Timeout(5.seconds) implicit val config: TaskConfig = { if(isProduction) { val hitDataService = new FileSystemHITDataService(annotationPath.resolve("production")) ProductionTaskConfig("qamr-example", domain, interface, httpPort, httpsPort, hitDataService) } else { val hitDataService = new FileSystemHITDataService(annotationPath.resolve("sandbox")) SandboxTaskConfig("qamr-example", domain, interface, httpPort, httpsPort, hitDataService) } } try { val annotationConfig = new AnnotationConfig(dataPath, resourcePath) import annotationConfig.SentenceIdHasTokens val datasets = new Datasets(Paths.get("../data")) val trainDev = datasets.train |+| datasets.dev val allUnfiltered = datasets.trainFull |+| datasets.devFull |+| datasets.testFull val Wiktionary = new WiktionaryFileSystemService(Paths.get("datasets/wiktionary")) implicit val inflections = Wiktionary.getInflectionsForTokens( trainDev.sentenceToQAs.keys.iterator.flatMap(_.tokens) ) val extPhrases = new ExternalPhrases(annotationConfig, trainDev) println(extPhrases.fullReport) println val validation = new Validation(allUnfiltered) println(validation.report) println val srlComparison = new SRLComparison(annotationConfig, datasets.ptb) println(srlComparison.pbRecallReport) println println(srlComparison.nbRecallReport) println println(srlComparison.qasrlRecallReport) val manualAnalysis = new ManualAnalysis("manual-analysis/data-analysis.txt") println(manualAnalysis.report) } finally { config.actorSystem.terminate import org.slf4j.LoggerFactory import ch.qos.logback.classic.LoggerContext LoggerFactory.getILoggerFactory.asInstanceOf[LoggerContext].stop } }
Example 88
Source File: WriteSquadData.scala From qamr with MIT License | 5 votes |
package qamr.analysis import qamr.example._ import qamr.util._ import qamr._ import spacro._ import spacro.tasks._ import spacro.util._ import akka.pattern.ask import scala.concurrent.duration._ import cats.implicits._ import com.amazonaws.services.mturk._ import com.amazonaws.services.mturk.model._ import nlpdata.util.Text import nlpdata.util.HasTokens.ops._ import nlpdata.datasets.wiktionary.Inflections import nlpdata.datasets.wiktionary.WiktionaryFileSystemService import java.nio.file.Paths object WriteSquadData extends App { val domain = "localhost" val isProduction = false val interface = "0.0.0.0" val httpPort = 8888 val httpsPort = 8080 val rootPath = java.nio.file.Paths.get(".") val dataPath = rootPath.resolve("data/example") val annotationPath = dataPath.resolve("annotations") val resourcePath = rootPath.resolve("datasets") implicit val timeout = akka.util.Timeout(5.seconds) implicit val config: TaskConfig = { if(isProduction) { val hitDataService = new FileSystemHITDataService(annotationPath.resolve("production")) ProductionTaskConfig("qamr-example", domain, interface, httpPort, httpsPort, hitDataService) } else { val hitDataService = new FileSystemHITDataService(annotationPath.resolve("sandbox")) SandboxTaskConfig("qamr-example", domain, interface, httpPort, httpsPort, hitDataService) } } try { val annotationConfig = new AnnotationConfig(dataPath, resourcePath) import annotationConfig.SentenceIdHasTokens val datasets = new Datasets(Paths.get("../data")) val squadFormatting = new SquadFormatting val trainSquad = squadFormatting.squadFormattedString( datasets.train, Set("Nikola Tesla", "Oxygen", "Geology", "Genghis Khan", "Imperialism") ) annotationConfig.saveOutputFile("squad-train.json", trainSquad) val devSquad = squadFormatting.squadFormattedString( datasets.dev, Set("Brain", "Emotion") ) annotationConfig.saveOutputFile("squad-dev.json", devSquad) val testSquad = squadFormatting.squadFormattedString( datasets.test, Set("Architecture", "Middle Ages", "Avicenna", "Capacitor", "Martin Luther", "Steam engine") ) annotationConfig.saveOutputFile("squad-test.json", testSquad) } finally { config.actorSystem.terminate import org.slf4j.LoggerFactory import ch.qos.logback.classic.LoggerContext LoggerFactory.getILoggerFactory.asInstanceOf[LoggerContext].stop } }
Example 89
Source File: Bootstrap.scala From squbs with Apache License 2.0 | 5 votes |
package org.squbs.unicomplex import akka.actor.ActorSystem import akka.pattern.ask import akka.util.Timeout import org.squbs.lifecycle.GracefulStop import org.squbs.util.ConfigUtil._ import scala.concurrent.Await import scala.concurrent.duration._ import scala.util.{Failure, Success, Try} import scala.language.postfixOps object Bootstrap extends App { println("Booting unicomplex") // Note, the config directories may change during extension init. It is important to re-read the full config // for the actor system start. UnicomplexBoot { (name, config) => ActorSystem(name, config) } .scanResources() .initExtensions .stopJVMOnExit .start() sys.addShutdownHook { Shutdown.shutdown() } } object Shutdown extends App { shutdown(actorSystemName = args.headOption) def shutdown(delayParameter: Option[FiniteDuration] = None, actorSystemName: Option[String] = None): Unit = { val name = actorSystemName getOrElse { val preConfig = UnicomplexBoot.getFullConfig(None) preConfig.getString("squbs.actorsystem-name") } UnicomplexBoot.actorSystems.get(name) map { actorSystem => val delay = delayParameter orElse actorSystem.settings.config.getOption[FiniteDuration]("squbs.shutdown-delay") getOrElse Duration.Zero implicit val squbsStopTimeout = Timeout(actorSystem.settings.config.get[FiniteDuration]("squbs.default-stop-timeout", 3.seconds)) val systemState = (Unicomplex(actorSystem).uniActor ? SystemState).mapTo[LifecycleState] import actorSystem.dispatcher systemState.onComplete { case Success(Stopping | Stopped) | Failure(_) => // Termination already started/happened. Do nothing! case _ => actorSystem.scheduler.scheduleOnce(delay, Unicomplex(name), GracefulStop) } Try { Await.ready(actorSystem.whenTerminated, delay + squbsStopTimeout.duration + (1 second)) } } } }
Example 90
Source File: PerpetualStreamMergeHubJSpec.scala From squbs with Apache License 2.0 | 5 votes |
package org.squbs.stream import akka.actor.{ActorRef, ActorSystem} import akka.http.scaladsl.Http import akka.http.scaladsl.model.{HttpRequest, Uri} import akka.pattern.ask import akka.stream.ActorMaterializer import akka.testkit.TestKit import com.typesafe.config.ConfigFactory import org.scalatest.{FlatSpecLike, Matchers} import org.squbs.unicomplex.Timeouts.{awaitMax, _} import org.squbs.unicomplex._ import scala.collection.mutable import scala.concurrent.Await object PerpetualStreamMergeHubJSpec { val dummyJarsDir = getClass.getClassLoader.getResource("classpaths").getPath val classPaths = Array("JavaPerpetualStreamMergeHubSpec") map (dummyJarsDir + "/" + _) val config = ConfigFactory.parseString( s""" |squbs { | actorsystem-name = JavaPerpetualStreamMergeHubSpec | ${JMX.prefixConfig} = true |} |default-listener.bind-port = 0 """.stripMargin ) val boot = UnicomplexBoot(config) .createUsing { (name, config) => ActorSystem(name, config) } .scanComponents(classPaths) .start() } class PerpetualStreamMergeHubJSpec extends TestKit(PerpetualStreamMergeHubJSpec.boot.actorSystem) with FlatSpecLike with Matchers { val portBindings = Await.result((Unicomplex(system).uniActor ? PortBindings).mapTo[Map[String, Int]], awaitMax) val psActorName = "/user/JavaPerpetualStreamMergeHubSpec/perpetualStreamWithMergeHub" val actorRef = Await.result((system.actorSelection(psActorName) ? RetrieveMyMessageStorageActorRef).mapTo[ActorRef], awaitMax) val port = portBindings("default-listener") it should "connect streams with mergehub" in { implicit val ac = ActorMaterializer() Http().singleRequest(HttpRequest(uri = Uri(s"http://127.0.0.1:$port/mergehub"), entity = "10")) Http().singleRequest(HttpRequest(uri = Uri(s"http://127.0.0.1:$port/mergehub"), entity = "11")) awaitAssert { val messages = Await.result((actorRef ? RetrieveMyMessages).mapTo[mutable.Set[MyMessage]], awaitMax) messages should have size 2 messages should contain(MyMessage(10)) messages should contain(MyMessage(11)) } } }
Example 91
Source File: PingPongSvc.scala From squbs with Apache License 2.0 | 5 votes |
package org.squbs.unicomplex.dummycubesvc import akka.actor.{Actor, ActorLogging, ActorRef, Props} import akka.http.scaladsl.server.Route import akka.pattern.ask import org.squbs.lifecycle.{GracefulStop, GracefulStopHelper} import org.squbs.unicomplex.Timeouts._ import org.squbs.unicomplex.{Ping, Pong, RouteDefinition} class PingPongSvc extends RouteDefinition{ def route: Route = path("ping") { get { onSuccess((context.actorOf(Props(classOf[PingPongClient])) ? "ping").mapTo[String]) { case value => complete(value) } } } ~ path("pong") { get { onSuccess((context.actorOf(Props(classOf[PingPongClient])) ? "pong").mapTo[String]) { case value => complete(value) } } } } private class PingPongClient extends Actor with ActorLogging { private val pingPongActor = context.actorSelection("/user/DummyCubeSvc/PingPongPlayer") def ping(responder: ActorRef): Receive = { case Pong => responder ! Pong.toString } def pong(responder: ActorRef): Receive = { case Ping => responder ! Ping.toString } def receive: Receive = { case "ping" => pingPongActor ! Ping context.become(ping(sender())) case "pong" => pingPongActor ! Pong context.become(pong(sender())) } } class PingPongActor extends Actor with ActorLogging with GracefulStopHelper{ def receive = { case GracefulStop => defaultLeafActorStop case Ping => sender ! Pong case Pong => sender ! Ping } }
Example 92
Source File: UnicomplexTimeoutSpec.scala From squbs with Apache License 2.0 | 5 votes |
package org.squbs.unicomplex import akka.actor.ActorSystem import akka.http.scaladsl.model.StatusCodes import akka.stream.ActorMaterializer import akka.testkit.{ImplicitSender, TestKit} import com.typesafe.config.ConfigFactory import org.scalatest._ import org.scalatest.concurrent.Waiters import org.squbs.lifecycle.GracefulStop import org.squbs.unicomplex.Timeouts._ import scala.concurrent.Await object UnicomplexTimeoutSpec { val dummyJarsDir = getClass.getClassLoader.getResource("classpaths").getPath val classPaths = Array( "DummySvcActor" ) map (dummyJarsDir + "/" + _) val aConfig = ConfigFactory.parseString( s""" |squbs { | actorsystem-name = UnicomplexTimeoutSpec | ${JMX.prefixConfig} = true |} |default-listener { | bind-port = 0 |} |akka.http.server { | request-timeout = 3s |} """.stripMargin) val boot = UnicomplexBoot(aConfig) .createUsing {(name, config) => ActorSystem(name, config)} .scanComponents(classPaths) .initExtensions.start() } class UnicomplexTimeoutSpec extends TestKit(UnicomplexTimeoutSpec.boot.actorSystem) with ImplicitSender with WordSpecLike with Matchers with BeforeAndAfterAll with Waiters { implicit val am = ActorMaterializer() import akka.pattern.ask val port = Await.result((Unicomplex(system).uniActor ? PortBindings).mapTo[Map[String, Int]], awaitMax)("default-listener") override def afterAll(): Unit = { Unicomplex(system).uniActor ! GracefulStop } "Unicomplex" must { "Cause a timeout event" in { system.settings.config getString "akka.http.server.request-timeout" should be ("3s") val response = Await.result(get(s"http://127.0.0.1:$port/dummysvcactor/timeout"), awaitMax) // TODO This test is useless to me.. Need to explore how we can intervene with timeouts.. Do we need to ? // There may be scenarios, where we may want to do some work when a timeout happens.. So, having a hook // would be useful.. response.status should be (StatusCodes.ServiceUnavailable) } } }
Example 93
Source File: UnicomplexPortAutoSelectSpec.scala From squbs with Apache License 2.0 | 5 votes |
package org.squbs.unicomplex import akka.actor.ActorSystem import akka.testkit.{ImplicitSender, TestKit} import com.typesafe.config.ConfigFactory import org.scalatest._ import org.squbs.lifecycle.GracefulStop import org.squbs.unicomplex.Timeouts._ import scala.concurrent.Await import scala.language.postfixOps object UnicomplexPortAutoSelectSpec { val dummyJarsDir = getClass.getClassLoader.getResource("classpaths").getPath val classPaths = Array( "DummySvc" ) map (dummyJarsDir + "/" + _) val config = ConfigFactory.parseString( s""" |squbs { | actorsystem-name = UnicomplexPortAutoSelectSpec | ${JMX.prefixConfig} = true |} | |default-listener.bind-port = 0 """.stripMargin ) val boot = UnicomplexBoot(config) .createUsing {(name, config) => ActorSystem(name, config)} .scanComponents(classPaths) .initExtensions.start() } class UnicomplexPortAutoSelectSpec extends TestKit(UnicomplexPortAutoSelectSpec.boot.actorSystem) with ImplicitSender with FlatSpecLike with Matchers with BeforeAndAfterAll { import akka.pattern.ask val portBindings = Await.result((Unicomplex(system).uniActor ? PortBindings).mapTo[Map[String, Int]], awaitMax) override def afterAll(): Unit = { Unicomplex(system).uniActor ! GracefulStop } "Unicomplex" should "let the system pick the port" in { portBindings("default-listener") should not be(8080) portBindings("default-listener") should not be(13000) // bind-port specified in src/test/resources/reference.conf } }
Example 94
Source File: UnicomplexTestModeOnSpec.scala From squbs with Apache License 2.0 | 5 votes |
package org.squbs.unicomplex import akka.actor.ActorSystem import akka.testkit.{ImplicitSender, TestKit} import com.typesafe.config.ConfigFactory import org.scalatest._ import org.squbs.lifecycle.GracefulStop import org.squbs.unicomplex.Timeouts._ import scala.concurrent.Await import scala.language.postfixOps object UnicomplexTestModeOnSpec { val dummyJarsDir = getClass.getClassLoader.getResource("classpaths").getPath val classPaths = Array( "DummySvc" ) map (dummyJarsDir + "/" + _) val config = ConfigFactory.parseString( s""" |squbs { | actorsystem-name = unicomplexTestModeSpec | ${JMX.prefixConfig} = true |} | |default-listener.bind-port = 0 """.stripMargin ) val boot = UnicomplexBoot(config) .createUsing {(name, config) => ActorSystem(name, config)} .scanComponents(classPaths) .initExtensions.start() } class UnicomplexTestModeOnSpec extends TestKit(UnicomplexTestModeOnSpec.boot.actorSystem) with ImplicitSender with FlatSpecLike with Matchers with BeforeAndAfterAll { import akka.pattern.ask val portBindings = Await.result((Unicomplex(system).uniActor ? PortBindings).mapTo[Map[String, Int]], awaitMax) override def afterAll(): Unit = { Unicomplex(system).uniActor ! GracefulStop } "Unicomplex" should "let the system pick the port" in { portBindings("default-listener") should not be 8080 portBindings("default-listener") should not be 13000 // bind-port specified in src/test/resources/reference.conf } }
Example 95
Source File: DummySvc.scala From squbs with Apache License 2.0 | 5 votes |
package org.squbs.unicomplex.dummysvc import akka.actor.{Actor, ActorLogging, ActorRef, Props} import akka.http.scaladsl.server.Route import akka.pattern.ask import org.squbs.unicomplex.Timeouts._ import org.squbs.unicomplex._ class DummySvc extends RouteDefinition with WebContext { def route: Route = get { path("msg" / Segment) { param => onSuccess((context.actorOf(Props(classOf[DummyClient])) ? EchoMsg(param)).mapTo[String]) { value => complete(value) } } ~ path("who") { extractClientIP { ip => complete(ip.toString) } } } } class Dummy2VersionedSvc extends RouteDefinition with WebContext { def route: Route = path("msg" / Segment) {param => get { onSuccess((context.actorOf(Props(classOf[DummyClient])) ? EchoMsg(param)).mapTo[String]) { case value => complete(value) } } } } class Dummy2Svc extends RouteDefinition with WebContext { def route: Route = path("msg" / Segment) {param => get { onSuccess((context.actorOf(Props(classOf[DummyClient])) ? EchoMsg(param.reverse)).mapTo[String]) { case value => complete(value) } } } } private class DummyClient extends Actor with ActorLogging { private def receiveMsg(responder: ActorRef): Receive = { case AppendedMsg(appendedMsg) => context.actorSelection("/user/DummyCube/Prepender") ! EchoMsg(appendedMsg) case PrependedMsg(prependedMsg) => responder ! prependedMsg context.stop(self) } def receive: Receive = { case msg: EchoMsg => context.actorSelection("/user/DummyCube/Appender") ! msg context.become(receiveMsg(sender())) } }
Example 96
Source File: JobHistoryServiceDelegate.scala From metronome with Apache License 2.0 | 5 votes |
package dcos.metronome package history.impl import akka.actor.ActorRef import akka.pattern.ask import akka.util.Timeout import dcos.metronome.history.{JobHistoryConfig, JobHistoryService} import dcos.metronome.model.{JobId, JobHistory} import scala.concurrent.Future class JobHistoryServiceDelegate(actorRef: ActorRef, config: JobHistoryConfig) extends JobHistoryService { import JobHistoryServiceActor._ implicit val timeout: Timeout = config.askTimeout override def statusFor(jobSpecId: JobId): Future[Option[JobHistory]] = { actorRef.ask(GetJobHistory(jobSpecId)).mapTo[Option[JobHistory]] } override def list(filter: JobHistory => Boolean): Future[Iterable[JobHistory]] = { actorRef.ask(ListJobHistories(filter)).mapTo[Iterable[JobHistory]] } }
Example 97
Source File: JobSpecServiceDelegate.scala From metronome with Apache License 2.0 | 5 votes |
package dcos.metronome package jobspec.impl import akka.actor.ActorRef import akka.pattern.ask import akka.util.Timeout import dcos.metronome.jobspec.impl.JobSpecServiceActor._ import dcos.metronome.jobspec.{JobSpecConfig, JobSpecService} import dcos.metronome.model.{JobId, JobSpec} import mesosphere.marathon.metrics.Metrics import scala.concurrent.Future class JobSpecServiceDelegate(config: JobSpecConfig, actorRef: ActorRef, metrics: Metrics) extends JobSpecService { private val createJobSpecTimeMetric = metrics.timer("debug.job-spec.operations.create.duration") private val getJobSpecTimeMetric = metrics.timer("debug.job-spec.operations.get.duration") private val updateJobSpecTimeMetric = metrics.timer("debug.job-spec.operations.update.duration") private val listJobSpecTimeMetric = metrics.timer("debug.job-spec.operations.list.duration") private val deleteJobSpecTimeMetric = metrics.timer("debug.job-spec.operations.delete.duration") implicit val timeout: Timeout = config.askTimeout override def createJobSpec(jobSpec: JobSpec): Future[JobSpec] = createJobSpecTimeMetric { actorRef.ask(CreateJobSpec(jobSpec)).mapTo[JobSpec] } override def getJobSpec(id: JobId): Future[Option[JobSpec]] = getJobSpecTimeMetric { actorRef.ask(GetJobSpec(id)).mapTo[Option[JobSpec]] } override def updateJobSpec(id: JobId, update: JobSpec => JobSpec): Future[JobSpec] = updateJobSpecTimeMetric { actorRef.ask(UpdateJobSpec(id, update)).mapTo[JobSpec] } override def listJobSpecs(filter: JobSpec => Boolean): Future[Iterable[JobSpec]] = listJobSpecTimeMetric { actorRef.ask(ListJobSpecs(filter)).mapTo[Iterable[JobSpec]] } override def deleteJobSpec(id: JobId): Future[JobSpec] = deleteJobSpecTimeMetric { actorRef.ask(DeleteJobSpec(id)).mapTo[JobSpec] } }
Example 98
Source File: JobRunServiceDelegate.scala From metronome with Apache License 2.0 | 5 votes |
package dcos.metronome package jobrun.impl import akka.actor.ActorRef import akka.pattern.ask import akka.util.Timeout import dcos.metronome.jobrun.{JobRunConfig, JobRunService, StartedJobRun} import dcos.metronome.model._ import mesosphere.marathon.metrics.Metrics import scala.concurrent.Future private[jobrun] class JobRunServiceDelegate(config: JobRunConfig, actorRef: ActorRef, metrics: Metrics) extends JobRunService { implicit val timeout: Timeout = config.askTimeout import JobRunServiceActor._ private val listRunsTimeMetric = metrics.timer("debug.job-run.operations.list.duration") private val getRunTimeMetric = metrics.timer("debug.job-run.operations.get.duration") private val killRunTimeMetric = metrics.timer("debug.job-run.operations.kill.duration") private val listActiveRunsTimeMetric = metrics.timer("debug.job-run.operations.list-active.duration") private val startRunTimeMetric = metrics.timer("debug.job-run.operations.start.duration") override def listRuns(filter: JobRun => Boolean): Future[Iterable[StartedJobRun]] = listRunsTimeMetric { actorRef.ask(ListRuns(filter)).mapTo[Iterable[StartedJobRun]] } override def getJobRun(jobRunId: JobRunId): Future[Option[StartedJobRun]] = getRunTimeMetric { actorRef.ask(GetJobRun(jobRunId)).mapTo[Option[StartedJobRun]] } override def killJobRun(jobRunId: JobRunId): Future[StartedJobRun] = killRunTimeMetric { actorRef.ask(KillJobRun(jobRunId)).mapTo[StartedJobRun] } override def activeRuns(jobId: JobId): Future[Iterable[StartedJobRun]] = listActiveRunsTimeMetric { actorRef.ask(GetActiveJobRuns(jobId)).mapTo[Iterable[StartedJobRun]] } override def startJobRun(jobSpec: JobSpec, schedule: Option[ScheduleSpec] = None): Future[StartedJobRun] = startRunTimeMetric { actorRef.ask(TriggerJobRun(jobSpec, schedule)).mapTo[StartedJobRun] } }
Example 99
Source File: ConnectedCarCluster.scala From cloudflow with Apache License 2.0 | 5 votes |
package connectedcar.streamlets import akka.actor.{ ActorRef, Props } import akka.cluster.sharding.{ ClusterSharding, ClusterShardingSettings } import akka.util.Timeout import cloudflow.akkastream.scaladsl.{ FlowWithCommittableContext, RunnableGraphStreamletLogic } import cloudflow.streamlets.StreamletShape import cloudflow.streamlets.avro.{ AvroInlet, AvroOutlet } import connectedcar.actors.ConnectedCarActor import scala.concurrent.duration._ import akka.pattern.ask import cloudflow.akkastream.{ AkkaStreamlet, Clustering } import connectedcar.data.{ ConnectedCarAgg, ConnectedCarERecord } object ConnectedCarCluster extends AkkaStreamlet with Clustering { val in = AvroInlet[ConnectedCarERecord]("in") val out = AvroOutlet[ConnectedCarAgg]("out", m ⇒ m.driver.toString) val shape = StreamletShape(in).withOutlets(out) override def createLogic = new RunnableGraphStreamletLogic() { def runnableGraph = sourceWithCommittableContext(in).via(flow).to(committableSink(out)) val carRegion: ActorRef = ClusterSharding(context.system).start( typeName = "Counter", entityProps = Props[ConnectedCarActor], settings = ClusterShardingSettings(context.system), extractEntityId = ConnectedCarActor.extractEntityId, extractShardId = ConnectedCarActor.extractShardId ) implicit val timeout: Timeout = 3.seconds def flow = FlowWithCommittableContext[ConnectedCarERecord] .mapAsync(5)(msg ⇒ (carRegion ? msg).mapTo[ConnectedCarAgg]) } }
Example 100
Source File: OnlineAction.scala From marvin-engine-executor with Apache License 2.0 | 5 votes |
package org.marvin.executor.actions import akka.Done import akka.actor.SupervisorStrategy._ import akka.actor.{Actor, ActorLogging, ActorRef, OneForOneStrategy, Props, Status} import akka.pattern.{ask, pipe} import akka.util.Timeout import io.grpc.StatusRuntimeException import org.marvin.artifact.manager.ArtifactSaver import org.marvin.executor.actions.OnlineAction.{OnlineExecute, OnlineHealthCheck, OnlineReload} import org.marvin.executor.proxies.EngineProxy.{ExecuteOnline, HealthCheck, Reload} import org.marvin.executor.proxies.OnlineActionProxy import org.marvin.artifact.manager.ArtifactSaver.SaveToLocal import org.marvin.model.{EngineActionMetadata, EngineMetadata} import org.marvin.util.ProtocolUtil import scala.collection.mutable.ListBuffer import scala.concurrent.Future import scala.concurrent.duration._ import scala.util.{Failure, Success} object OnlineAction { case class OnlineExecute(message: String, params: String) case class OnlineReload(protocol: String) case class OnlineHealthCheck() } class OnlineAction(actionName: String, metadata: EngineMetadata) extends Actor with ActorLogging { var onlineActionProxy: ActorRef = _ var artifactSaver: ActorRef = _ var engineActionMetadata: EngineActionMetadata = _ var artifactsToLoad: String = _ implicit val ec = context.dispatcher override def preStart() = { engineActionMetadata = metadata.actionsMap(actionName) artifactsToLoad = engineActionMetadata.artifactsToLoad.mkString(",") onlineActionProxy = context.actorOf(Props(new OnlineActionProxy(engineActionMetadata)), name = "onlineActionProxy") artifactSaver = context.actorOf(ArtifactSaver.build(metadata), name = "artifactSaver") } override val supervisorStrategy = OneForOneStrategy(maxNrOfRetries = 10, withinTimeRange = metadata.onlineActionTimeout milliseconds) { case _: StatusRuntimeException => Restart case _: Exception => Escalate } override def receive = { case OnlineExecute(message, params) => implicit val futureTimeout = Timeout(metadata.onlineActionTimeout milliseconds) log.info(s"Starting to process execute to $actionName. Message: [$message] and params: [$params].") val originalSender = sender ask(onlineActionProxy, ExecuteOnline(message, params)) pipeTo originalSender case OnlineReload(protocol) => implicit val futureTimeout = Timeout(metadata.reloadTimeout milliseconds) log.info(s"Starting to process reload to $actionName. Protocol: [$protocol].") if(protocol == null || protocol.isEmpty){ onlineActionProxy forward Reload() }else{ val splitedProtocols = ProtocolUtil.splitProtocol(protocol, metadata) val futures:ListBuffer[Future[Any]] = ListBuffer[Future[Any]]() for(artifactName <- engineActionMetadata.artifactsToLoad) { futures += (artifactSaver ? SaveToLocal(artifactName, splitedProtocols(artifactName))) } val origSender = sender() Future.sequence(futures).onComplete{ case Success(_) => onlineActionProxy.ask(Reload(protocol)) pipeTo origSender case Failure(e) => { log.error(s"Failure to reload artifacts using protocol $protocol.") origSender ! Status.Failure(e) } } } case OnlineHealthCheck => implicit val futureTimeout = Timeout(metadata.healthCheckTimeout milliseconds) log.info(s"Starting to process health to $actionName.") val originalSender = sender ask(onlineActionProxy, HealthCheck) pipeTo originalSender case Done => log.info("Work Done!") case _ => log.warning(s"Not valid message !!") } }
Example 101
Source File: PipelineAction.scala From marvin-engine-executor with Apache License 2.0 | 5 votes |
package org.marvin.executor.actions import java.time.LocalDateTime import java.util.NoSuchElementException import akka.Done import akka.actor.{Actor, ActorLogging, ActorRef, Props} import akka.pattern.ask import akka.util.Timeout import org.marvin.artifact.manager.ArtifactSaver import org.marvin.artifact.manager.ArtifactSaver.SaveToRemote import org.marvin.exception.MarvinEExecutorException import org.marvin.executor.actions.PipelineAction.{PipelineExecute, PipelineExecutionStatus} import org.marvin.executor.proxies.BatchActionProxy import org.marvin.executor.proxies.EngineProxy.{ExecuteBatch, Reload} import org.marvin.model._ import org.marvin.util.{JsonUtil, LocalCache} import scala.collection.mutable.ListBuffer import scala.concurrent.duration._ import scala.concurrent.{Await, Future} import scala.util.Success object PipelineAction { case class PipelineExecute(protocol:String, params:String) case class PipelineExecutionStatus(protocol:String) } class PipelineAction(metadata: EngineMetadata) extends Actor with ActorLogging{ implicit val ec = context.dispatcher var artifactSaver: ActorRef = _ var cache: LocalCache[BatchExecution] = _ override def preStart() = { artifactSaver = context.actorOf(ArtifactSaver.build(metadata), name = "artifactSaver") cache = new LocalCache[BatchExecution](maximumSize = 10000L, defaultTTL = 30.days) } override def receive = { case PipelineExecute(protocol, params) => implicit val futureTimeout = Timeout(metadata.pipelineTimeout milliseconds) log.info(s"Starting to process pipeline process with. Protocol: [$protocol] and Params: [$params].") cache.save(protocol, new BatchExecution("pipeline", protocol, LocalDateTime.now, Working)) try{ for(actionName <- metadata.pipelineActions){ val engineActionMetadata = metadata.actionsMap(actionName) val _actor: ActorRef = context.actorOf(Props(new BatchActionProxy(engineActionMetadata)), name = actionName.concat("Actor")) Await.result((_actor ? Reload(protocol)), futureTimeout.duration) Await.result((_actor ? ExecuteBatch(protocol, params)), futureTimeout.duration) context stop _actor val futures:ListBuffer[Future[Done]] = ListBuffer[Future[Done]]() for(artifactName <- engineActionMetadata.artifactsToPersist) { futures += (artifactSaver ? SaveToRemote(artifactName, protocol)).mapTo[Done] } if (!futures.isEmpty) Future.sequence(futures).onComplete{ case Success(response) => log.info(s"All artifacts from [$actionName] were saved with success!! [$response]") } } }catch { case e: Exception => cache.save(protocol, new BatchExecution("pipeline", protocol, LocalDateTime.now, Failed)) throw e } cache.save(protocol, new BatchExecution("pipeline", protocol, LocalDateTime.now, Finished)) case PipelineExecutionStatus(protocol) => log.info(s"Getting pipeline execution status to protocol $protocol.") try { sender ! JsonUtil.toJson(cache.load(protocol).get) }catch { case _: NoSuchElementException => sender ! akka.actor.Status.Failure(new MarvinEExecutorException(s"Protocol $protocol not found!")) } case Done => log.info("Work Done!") case _ => log.warning(s"Not valid message !!") } }
Example 102
Source File: TalkToATeamMemberRoute.scala From akka-kubernetes-tests with Apache License 2.0 | 5 votes |
package akka.kubernetes.sample import akka.actor.ActorRef import akka.http.scaladsl.model._ import akka.http.scaladsl.server.Directives._ import akka.kubernetes.sample.AkkaMember.Hello import akka.pattern.ask import akka.util.Timeout import scala.concurrent.duration._ import scala.util.{Failure, Success} class TalkToATeamMemberRoute(sharding: ActorRef) { implicit val timeout = Timeout(10.second) def route() = extractActorSystem { as => path("team-member" / Segment) { name => get { val teamMemberF = (sharding ? Hello(name)).mapTo[String] onComplete(teamMemberF) { case Success(response) => complete(response) case Failure(ex) => complete((StatusCodes.InternalServerError, s"An error occurred: ${ex.getMessage}")) } } } } }
Example 103
Source File: TalkToTheBossRouteRoute.scala From akka-kubernetes-tests with Apache License 2.0 | 5 votes |
package akka.kubernetes.sample import akka.actor.ActorRef import akka.http.scaladsl.model._ import akka.http.scaladsl.server.Directives._ import akka.pattern.ask import akka.util.Timeout import scala.concurrent.duration._ import scala.util.{Failure, Success} class TalkToTheBossRouteRoute(boss: ActorRef) { implicit val timeout = Timeout(10.second) def route() = extractActorSystem { as => path("boss") { get { val bossF = (boss ? "hello").mapTo[String] onComplete(bossF) { case Success(response) => complete(response) case Failure(ex) => complete((StatusCodes.InternalServerError, s"An error occurred: ${ex.getMessage}")) } } } } }
Example 104
Source File: StatsEndpoint.scala From akka-kubernetes-tests with Apache License 2.0 | 5 votes |
package akka.kubernetes.soak import akka.actor.{ActorRef, ActorSystem} import akka.event.Logging import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport import akka.http.scaladsl.model.StatusCodes import akka.http.scaladsl.server.{Directives, Route} import spray.json.DefaultJsonProtocol import akka.pattern.ask import akka.util.Timeout import scala.concurrent.duration._ import scala.util.{Failure, Success} // collect your json format instances into a support trait: trait StatsJsonSupport extends SprayJsonSupport with DefaultJsonProtocol { implicit val testResultFormat = jsonFormat2(TestResult) implicit val testResultsFormat = jsonFormat7(TestResults) } class StatsEndpoint(system: ActorSystem, client: ActorRef) extends Directives with StatsJsonSupport { private implicit val askTimeout = Timeout(5.seconds) private val log = Logging(system, getClass) val route: Route = path("stats") { get { onComplete(client.ask(GetTestResults()).mapTo[TestResults]) { case Failure(t) => log.error(t, "Failed to get test results") complete(StatusCodes.InternalServerError) case Success(value) => complete(value) } } } }
Example 105
Source File: BankAccountAggregateFlowsImpl.scala From akka-ddd-cqrs-es-example with MIT License | 5 votes |
package com.github.j5ik2o.bank.adaptor.aggregate import akka.NotUsed import akka.actor.{ ActorRef, ActorSystem } import akka.pattern.ask import akka.stream.scaladsl.Flow import akka.util.Timeout import com.github.j5ik2o.bank.useCase.BankAccountAggregateUseCase.Protocol import com.github.j5ik2o.bank.useCase.port.BankAccountAggregateFlows import pureconfig._ class BankAccountAggregateFlowsImpl(aggregateRef: ActorRef)( implicit val system: ActorSystem ) extends BankAccountAggregateFlows { import Protocol._ private val config = loadConfigOrThrow[BankAccountAggregateFlowsConfig]( system.settings.config.getConfig("bank.interface.bank-account-aggregate-flows") ) private implicit val to: Timeout = Timeout(config.callTimeout) override def openBankAccountFlow: Flow[OpenBankAccountRequest, OpenBankAccountResponse, NotUsed] = Flow[OpenBankAccountRequest] .map { request => BankAccountAggregate.Protocol.OpenBankAccountRequest(request.bankAccountId, request.name) } .mapAsync(1)(aggregateRef ? _) .map { case response: BankAccountAggregate.Protocol.OpenBankAccountSucceeded => OpenBankAccountSucceeded(response.bankAccountId) case response: BankAccountAggregate.Protocol.OpenBankAccountFailed => OpenBankAccountFailed(response.bankAccountId, response.error) } override def updateBankAccountFlow: Flow[UpdateBankAccountRequest, UpdateBankAccountResponse, NotUsed] = Flow[UpdateBankAccountRequest] .map { request => BankAccountAggregate.Protocol.UpdateBankAccountRequest(request.bankAccountId, request.name) } .mapAsync(1)(aggregateRef ? _) .map { case response: BankAccountAggregate.Protocol.UpdateBankAccountSucceeded => UpdateBankAccountSucceeded(response.bankAccountId) case response: BankAccountAggregate.Protocol.UpdateBankAccountFailed => UpdateBankAccountFailed(response.bankAccountId, response.error) } override def addBankAccountEventFlow: Flow[AddBankAccountEventRequest, AddBankAccountEventResponse, NotUsed] = Flow[AddBankAccountEventRequest] .map { case request: DepositRequest => BankAccountAggregate.Protocol.DepositRequest(request.bankAccountId, request.deposit) case request: WithdrawRequest => BankAccountAggregate.Protocol.WithdrawRequest(request.bankAccountId, request.withdraw) } .mapAsync(1)(aggregateRef ? _) .map { case response: BankAccountAggregate.Protocol.DepositSucceeded => DepositSucceeded(response.bankAccountId) case response: BankAccountAggregate.Protocol.DepositFailed => DepositFailed(response.bankAccountId, response.error) case response: BankAccountAggregate.Protocol.WithdrawSucceeded => WithdrawSucceeded(response.bankAccountId) case response: BankAccountAggregate.Protocol.WithdrawFailed => WithdrawFailed(response.bankAccountId, response.error) } override def closeBankAccountFlow: Flow[CloseBankAccountRequest, CloseBankAccountResponse, NotUsed] = Flow[CloseBankAccountRequest] .map { request => BankAccountAggregate.Protocol.CloseBankAccountRequest(request.bankAccountId) } .mapAsync(1)(aggregateRef ? _) .map { case response: BankAccountAggregate.Protocol.CloseBankAccountSucceeded => CloseBankAccountSucceeded(response.bankAccountId) case response: BankAccountAggregate.Protocol.CloseBankAccountFailed => CloseBankAccountFailed(response.bankAccountId, response.error) } }
Example 106
Source File: Guestbook.scala From skuber with Apache License 2.0 | 5 votes |
package skuber.examples.guestbook import akka.actor._ import akka.pattern.ask import akka.util.Timeout import scala.concurrent.duration._ import scala.concurrent.Await import scala.concurrent.ExecutionContext.Implicits.global object Guestbook extends App { val sys = ActorSystem("SkuberExamples") val guestbook = sys.actorOf(Props[GuestbookActor], "guestbook") implicit val timeout = Timeout(40.seconds) val deploymentResult = ask(guestbook, GuestbookActor.Deploy) deploymentResult map { result => result match { case GuestbookActor.DeployedSuccessfully => { System.out.println("\n*** Deployment of Guestbook application to Kubernetes completed successfully!") sys.terminate().foreach { f => System.exit(0) } } case GuestbookActor.DeploymentFailed(ex) => { System.err.println("\n!!! Deployment of Guestbook application failed: " + ex) sys.terminate().foreach { f => System.exit(0) } } } } deploymentResult.failed.foreach { case ex => System.err.println("Unexpected error deploying Guestbook: " + ex) sys.terminate().foreach { f => System.exit(1) } } }
Example 107
Source File: Boot.scala From unicorn with Apache License 2.0 | 5 votes |
package unicorn.rhino import scala.concurrent.duration._ import akka.actor.{ActorSystem, Props} import akka.routing.FromConfig import akka.io.IO import spray.can.Http import akka.pattern.ask import akka.util.Timeout import com.typesafe.config.ConfigFactory object Boot extends App { // we need an ActorSystem to host our application in implicit val actorSystem = ActorSystem("unicorn-rhino") // create a pool of RhinoActors val service = actorSystem.actorOf(FromConfig.props(Props[RhinoActor]), "rhino-router") val conf = ConfigFactory.load() val serverPort = conf.getInt("spray.can.server.port") val ip = if (System.getProperty("loopback.only") != null) "127.0.0.1" else "0.0.0.0" IO(Http) ! Http.Bind(service, interface = ip, port = serverPort) }
Example 108
Source File: AddService.scala From swagger-akka-http-sample with Apache License 2.0 | 5 votes |
package com.example.akka.add import javax.ws.rs.{Consumes, POST, Path, Produces} import akka.actor.ActorRef import akka.http.scaladsl.server.{Directives, Route} import akka.pattern.ask import akka.util.Timeout import com.example.akka.DefaultJsonFormats import com.example.akka.add.AddActor._ import io.swagger.v3.oas.annotations.Operation import io.swagger.v3.oas.annotations.media.{Content, Schema} import io.swagger.v3.oas.annotations.parameters.RequestBody import io.swagger.v3.oas.annotations.responses.ApiResponse import javax.ws.rs.core.MediaType import spray.json.RootJsonFormat import scala.concurrent.ExecutionContext import scala.concurrent.duration.DurationInt @Path("/add") class AddService(addActor: ActorRef)(implicit executionContext: ExecutionContext) extends Directives with DefaultJsonFormats { implicit val timeout: Timeout = Timeout(2.seconds) implicit val requestFormat: RootJsonFormat[AddRequest] = jsonFormat1(AddRequest) implicit val responseFormat: RootJsonFormat[AddResponse] = jsonFormat1(AddResponse) val route: Route = add @POST @Consumes(Array(MediaType.APPLICATION_JSON)) @Produces(Array(MediaType.APPLICATION_JSON)) @Operation(summary = "Add integers", description = "Add integers", requestBody = new RequestBody(content = Array(new Content(schema = new Schema(implementation = classOf[AddRequest])))), responses = Array( new ApiResponse(responseCode = "200", description = "Add response", content = Array(new Content(schema = new Schema(implementation = classOf[AddResponse])))), new ApiResponse(responseCode = "500", description = "Internal server error")) ) def add: Route = path("add") { post { entity(as[AddRequest]) { request => complete { (addActor ? request).mapTo[AddResponse] } } } } }
Example 109
Source File: AddOptionService.scala From swagger-akka-http-sample with Apache License 2.0 | 5 votes |
package com.example.akka.addoption import javax.ws.rs.{Consumes, POST, Path, Produces} import akka.actor.ActorRef import akka.http.scaladsl.server.{Directives, Route} import akka.pattern.ask import akka.util.Timeout import com.example.akka.DefaultJsonFormats import com.example.akka.addoption.AddOptionActor._ import io.swagger.v3.oas.annotations.Operation import io.swagger.v3.oas.annotations.media.{Content, Schema} import io.swagger.v3.oas.annotations.parameters.RequestBody import io.swagger.v3.oas.annotations.responses.ApiResponse import javax.ws.rs.core.MediaType import spray.json.RootJsonFormat import scala.concurrent.ExecutionContext import scala.concurrent.duration.DurationInt @Path("/addOption") class AddOptionService(addActor: ActorRef)(implicit executionContext: ExecutionContext) extends Directives with DefaultJsonFormats { implicit val timeout: Timeout = Timeout(2.seconds) implicit val requestFormat: RootJsonFormat[AddOptionRequest] = jsonFormat2(AddOptionRequest) implicit val responseFormat: RootJsonFormat[AddOptionResponse] = jsonFormat1(AddOptionResponse) val route: Route = addOption @POST @Consumes(Array(MediaType.APPLICATION_JSON)) @Produces(Array(MediaType.APPLICATION_JSON)) @Operation(summary = "Add integers", description = "Add integers", requestBody = new RequestBody(content = Array(new Content(schema = new Schema(implementation = classOf[AddOptionRequest])))), responses = Array( new ApiResponse(responseCode = "200", description = "Add response", content = Array(new Content(schema = new Schema(implementation = classOf[AddOptionResponse])))), new ApiResponse(responseCode = "500", description = "Internal server error")) ) def addOption: Route = path("addOption") { post { entity(as[AddOptionRequest]) { request => complete { (addActor ? request).mapTo[AddOptionResponse] } } } } }
Example 110
Source File: HelloService.scala From swagger-akka-http-sample with Apache License 2.0 | 5 votes |
package com.example.akka.hello import javax.ws.rs.{GET, Path, Produces} import akka.actor.ActorRef import akka.http.scaladsl.server.{Directives, Route} import akka.pattern.ask import akka.util.Timeout import com.example.akka.DefaultJsonFormats import com.example.akka.hello.HelloActor._ import io.swagger.v3.oas.annotations.enums.ParameterIn import io.swagger.v3.oas.annotations.media.{Content, Schema} import io.swagger.v3.oas.annotations.responses.ApiResponse import io.swagger.v3.oas.annotations.{Operation, Parameter} import javax.ws.rs.core.MediaType import spray.json.RootJsonFormat import scala.concurrent.ExecutionContext import scala.concurrent.duration.DurationInt @Path("/hello") class HelloService(hello: ActorRef)(implicit executionContext: ExecutionContext) extends Directives with DefaultJsonFormats { implicit val timeout: Timeout = Timeout(2.seconds) implicit val greetingFormat: RootJsonFormat[Greeting] = jsonFormat1(Greeting) val route: Route = getHello ~ getHelloSegment @GET @Produces(Array(MediaType.APPLICATION_JSON)) @Operation(summary = "Return Hello greeting (anonymous)", description = "Return Hello greeting for anonymous request", responses = Array( new ApiResponse(responseCode = "200", description = "Hello response", content = Array(new Content(schema = new Schema(implementation = classOf[Greeting])))), new ApiResponse(responseCode = "500", description = "Internal server error")) ) def getHello: Route = path("hello") { get { complete { (hello ? AnonymousHello).mapTo[Greeting] } } } @GET @Produces(Array(MediaType.APPLICATION_JSON)) @Operation(summary = "Return Hello greeting", description = "Return Hello greeting for named user", parameters = Array(new Parameter(name = "name", in = ParameterIn.PATH, description = "user name")), responses = Array( new ApiResponse(responseCode = "200", description = "Hello response", content = Array(new Content(schema = new Schema(implementation = classOf[Greeting])))), new ApiResponse(responseCode = "500", description = "Internal server error")) ) def getHelloSegment: Route = path("hello" / Segment) { name => get { complete { (hello ? Hello(name)).mapTo[Greeting] } } } }
Example 111
Source File: UserServiceImpl.scala From scalafiddle-editor with Apache License 2.0 | 5 votes |
package scalafiddle.server.models.services import java.util.UUID import javax.inject.{Inject, Named} import akka.actor._ import akka.pattern.ask import akka.util.Timeout import com.mohiva.play.silhouette.api.LoginInfo import com.mohiva.play.silhouette.impl.providers.CommonSocialProfile import play.api.Logger import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.Future import scala.concurrent.duration._ import scala.util.{Success, Try} import scalafiddle.server._ import scalafiddle.server.models.User class UserServiceImpl @Inject() (@Named("persistence") persistence: ActorRef) extends UserService { implicit val timeout = Timeout(15.seconds) val log = Logger(getClass) override def save(profile: CommonSocialProfile): Future[User] = { log.debug(s"User $profile logged in") val user = User( UUID.randomUUID().toString, profile.loginInfo, profile.firstName, profile.lastName, profile.fullName, profile.email, profile.avatarURL, true ) ask(persistence, AddUser(user)).mapTo[Try[User]].map { case Success(u) => u case _ => throw new Exception("Unable to save user") } } override def retrieve(loginInfo: LoginInfo): Future[Option[User]] = { ask(persistence, FindUserLogin(loginInfo)).mapTo[Try[User]].map { case Success(user) => Some(user) case _ => None } } }
Example 112
Source File: ApiService.scala From scalafiddle-editor with Apache License 2.0 | 5 votes |
package scalafiddle.server import akka.actor._ import akka.pattern.ask import akka.util.Timeout import kamon.Kamon import scala.concurrent.duration._ import scala.concurrent.{ExecutionContext, Future} import scala.util.{Failure, Success, Try} import scalafiddle.server.dao.Fiddle import scalafiddle.server.models.User import scalafiddle.shared.{FiddleVersions, _} class ApiService(persistence: ActorRef, user: Option[User], _loginProviders: Seq[LoginProvider])( implicit ec: ExecutionContext ) extends Api { implicit val timeout = Timeout(15.seconds) val saveCount = Kamon.metrics.counter("fiddle-save") val updateCount = Kamon.metrics.counter("fiddle-update") val forkCount = Kamon.metrics.counter("fiddle-fork") override def save(fiddle: FiddleData): Future[Either[String, FiddleId]] = { saveCount.increment() ask(persistence, AddFiddle(fiddle, user.fold("anonymous")(_.userID))).mapTo[Try[FiddleId]].map { case Success(fid) => Right(fid) case Failure(ex) => Left(ex.toString) } } override def update(fiddle: FiddleData, id: String): Future[Either[String, FiddleId]] = { // allow update only when user is the same as fiddle author (or fiddle is anonymous) val updateOk = (fiddle.author.map(_.id), user.map(_.userID)) match { case (Some(authorId), Some(userId)) if authorId == userId => true case (None, _) => true case _ => false } if (updateOk) { updateCount.increment() ask(persistence, UpdateFiddle(fiddle, id)).mapTo[Try[FiddleId]].map { case Success(fid) => Right(fid) case Failure(ex) => Left(ex.toString) } } else { Future.successful(Left("Not allowed to update fiddle")) } } override def fork(fiddle: FiddleData, id: String, version: Int): Future[Either[String, FiddleId]] = { forkCount.increment() ask(persistence, ForkFiddle(fiddle, id, version, user.fold("anonymous")(_.userID))).mapTo[Try[FiddleId]].map { case Success(fid) => Right(fid) case Failure(ex) => Left(ex.toString) } } override def loginProviders(): Seq[LoginProvider] = _loginProviders override def userInfo(): UserInfo = user.fold(UserInfo("", "", None, loggedIn = false))(u => UserInfo(u.userID, u.name.getOrElse("Anonymous"), u.avatarURL, loggedIn = true) ) override def listFiddles(): Future[Seq[FiddleVersions]] = { user match { case Some(currentUser) => ask(persistence, FindUserFiddles(currentUser.userID)).mapTo[Try[Map[String, Seq[Fiddle]]]].map { case Success(fiddles) => fiddles.toList.map { case (id, versions) => val latest = versions.last FiddleVersions(id, latest.name, latest.libraries, latest.version, latest.created) } case Failure(e) => Seq.empty } case None => Future.successful(Seq.empty) } } }
Example 113
Source File: AkkaJobClient.scala From lemon-schedule with GNU General Public License v2.0 | 5 votes |
package com.gabry.job.client.akkaclient import java.util.concurrent.TimeUnit import akka.actor.{ActorRef, ActorSystem, Props} import akka.pattern.{AskTimeoutException, ask} import akka.util.Timeout import com.gabry.job.client.AbstractJobClient import com.gabry.job.core.command.JobClientCommand import com.gabry.job.core.domain.{Dependency, Job} import com.gabry.job.core.event.{FailedEvent, JobTrackerEvent} import com.gabry.job.core.registry.{Registry, RegistryFactory} import com.typesafe.config.Config import scala.concurrent.ExecutionContextExecutor import scala.util.{Failure, Success} override def cancelJob(jobId: Long,force:Boolean): Unit = { clientActor ? JobClientCommand.CancelJob(jobId,force) onComplete{ case Success(_) => println("作业取消成功") case Failure(reason) => reason.printStackTrace() println(s"作业取消失败 ${reason.getMessage}") } } }
Example 114
Source File: DaemonWorkflowDriver.scala From vamp with Apache License 2.0 | 5 votes |
package io.vamp.workflow_driver import akka.actor.{ Actor, ActorRef } import akka.pattern.ask import com.typesafe.scalalogging.LazyLogging import io.vamp.common.akka.IoC.actorFor import io.vamp.container_driver.ContainerDriverActor.{ DeployWorkflow, GetWorkflow, UndeployWorkflow } import io.vamp.container_driver.ContainerWorkflow import io.vamp.model.artifact.{ DaemonSchedule, DefaultBreed, Instance, Workflow } import io.vamp.persistence.PersistenceActor import scala.concurrent.Future trait DaemonWorkflowDriver extends WorkflowDriver with LazyLogging { protected def driverActor: ActorRef override def receive: Actor.Receive = super.receive orElse { case ContainerWorkflow(workflow, containers, health) ⇒ logger.info("DaemonWorkflowDriver - received ContainerWorkflow {}", workflow.name) if (workflow.health != health) actorFor[PersistenceActor] ! PersistenceActor.UpdateWorkflowHealth(workflow, health) val instances = containers.map(_.instances.map { instance ⇒ val ports: Map[String, Int] = { workflow.breed match { case breed: DefaultBreed ⇒ breed.ports.map(_.name) zip instance.ports case _ ⇒ Map[String, Int]() } }.toMap logger.info("DaemonWorkflowDriver - Ports for ContainerInstance {} are {}", instance.toString, ports.toString) Instance(instance.name, instance.host, ports, instance.deployed) }).getOrElse(Nil) if (workflow.instances != instances) actorFor[PersistenceActor] ! PersistenceActor.UpdateWorkflowInstances(workflow, instances) case _ ⇒ logger.info("DaemonWorkflowDriver - received an unrecognised message") } protected override def request(workflows: List[Workflow]): Unit = workflows.foreach(request) protected def request: PartialFunction[Workflow, Unit] = { case workflow if workflow.schedule == DaemonSchedule ⇒ driverActor ! GetWorkflow(workflow, self) case workflow ⇒ logger.info("DaemonWorkflowDriver - Workflow schedule is {} instead of DaemonSchedule", workflow.schedule) } protected override def schedule(data: Any): PartialFunction[Workflow, Future[Any]] = { case workflow if workflow.schedule == DaemonSchedule ⇒ { logger.info("DaemonWorkflowDriver - Workflow number of instances is {}", workflow.instances.size) enrich(workflow, data).flatMap { enriched ⇒ driverActor ? DeployWorkflow(enriched, update = workflow.instances.nonEmpty) } } } protected override def unschedule(): PartialFunction[Workflow, Future[Any]] = { case workflow if workflow.schedule == DaemonSchedule && workflow.instances.nonEmpty ⇒ driverActor ? UndeployWorkflow(workflow) } }
Example 115
Source File: InfoController.scala From vamp with Apache License 2.0 | 5 votes |
package io.vamp.operation.controller import akka.actor.Actor import akka.pattern.ask import akka.util.Timeout import io.vamp.common.{ Config, ConfigMagnet, Namespace } import io.vamp.common.akka.DataRetrieval import io.vamp.common.akka.IoC._ import io.vamp.common.vitals.{ InfoRequest, JmxVitalsProvider, JvmInfoMessage, JvmVitals } import io.vamp.container_driver.ContainerDriverActor import io.vamp.gateway_driver.GatewayDriverActor import io.vamp.model.Model import io.vamp.persistence.{ KeyValueStoreActor, PersistenceActor } import io.vamp.pulse.PulseActor import io.vamp.workflow_driver.WorkflowDriverActor import scala.concurrent.Future import scala.language.postfixOps trait AbstractInfoMessage extends JvmInfoMessage { def message: String def version: String def uuid: String def runningSince: String } case class InfoMessage( message: String, version: String, uuid: String, runningSince: String, jvm: Option[JvmVitals], persistence: Option[Any], keyValue: Option[Any], pulse: Option[Any], gatewayDriver: Option[Any], containerDriver: Option[Any], workflowDriver: Option[Any] ) extends AbstractInfoMessage trait InfoController extends AbstractController with DataRetrieval with JmxVitalsProvider { val infoMessage: ConfigMagnet[String] = Config.string("vamp.operation.info.message") protected val dataRetrievalTimeout: ConfigMagnet[Timeout] = Config.timeout("vamp.operation.info.timeout") def infoMessage(on: Set[String])(implicit namespace: Namespace, timeout: Timeout): Future[(AbstractInfoMessage, Boolean)] = { retrieve(infoActors(on), actor ⇒ actorFor(actor) ? InfoRequest, dataRetrievalTimeout()) map { result ⇒ InfoMessage( infoMessage(), Model.version, Model.uuid, Model.runningSince, if (on.isEmpty || on.contains("jvm")) Option(jvmVitals()) else None, result.data.get(classOf[PersistenceActor].asInstanceOf[Class[Actor]]), result.data.get(classOf[KeyValueStoreActor].asInstanceOf[Class[Actor]]), result.data.get(classOf[PulseActor].asInstanceOf[Class[Actor]]), result.data.get(classOf[GatewayDriverActor].asInstanceOf[Class[Actor]]), result.data.get(classOf[ContainerDriverActor].asInstanceOf[Class[Actor]]), result.data.get(classOf[WorkflowDriverActor].asInstanceOf[Class[Actor]]) ) → result.succeeded } } protected def infoActors(on: Set[String]): List[Class[Actor]] = { val list = if (on.isEmpty) { List( classOf[PersistenceActor], classOf[KeyValueStoreActor], classOf[PulseActor], classOf[GatewayDriverActor], classOf[ContainerDriverActor], classOf[WorkflowDriverActor] ) } else on.map(_.toLowerCase).collect { case "persistence" ⇒ classOf[PersistenceActor] case "key_value" ⇒ classOf[KeyValueStoreActor] case "pulse" ⇒ classOf[PulseActor] case "gateway_driver" ⇒ classOf[GatewayDriverActor] case "container_driver" ⇒ classOf[ContainerDriverActor] case "workflow_driver" ⇒ classOf[WorkflowDriverActor] } toList list.map(_.asInstanceOf[Class[Actor]]) } }
Example 116
Source File: StatsController.scala From vamp with Apache License 2.0 | 5 votes |
package io.vamp.operation.controller import akka.actor.Actor import akka.pattern.ask import akka.util.Timeout import io.vamp.common.{ Config, Namespace } import io.vamp.common.akka.DataRetrieval import io.vamp.common.akka.IoC._ import io.vamp.common.vitals.{ JmxVitalsProvider, JvmVitals, StatsRequest } import io.vamp.operation.metrics.KamonMetricsActor import io.vamp.persistence.PersistenceActor import io.vamp.pulse.PulseActor import scala.concurrent.Future case class StatsMessage(jvm: JvmVitals, system: Any, persistence: Any, pulse: Any) trait StatsController extends AbstractController with DataRetrieval with JmxVitalsProvider { private val dataRetrievalTimeout = Config.timeout("vamp.operation.stats.timeout") def stats()(implicit namespace: Namespace, timeout: Timeout): Future[StatsMessage] = { val actors = List(classOf[KamonMetricsActor], classOf[PersistenceActor], classOf[PulseActor]) map { _.asInstanceOf[Class[Actor]] } retrieve(actors, actor ⇒ actorFor(actor) ? StatsRequest, dataRetrievalTimeout()) map { result ⇒ StatsMessage( jvmVitals(), result.data.get(classOf[KamonMetricsActor].asInstanceOf[Class[Actor]]), result.data.get(classOf[PersistenceActor].asInstanceOf[Class[Actor]]), result.data.get(classOf[PulseActor].asInstanceOf[Class[Actor]]) ) } } }
Example 117
Source File: SchedulerController.scala From vamp with Apache License 2.0 | 5 votes |
package io.vamp.operation.controller import akka.pattern.ask import akka.util.Timeout import io.vamp.common.Namespace import io.vamp.common.akka.{ IoC, ReplyCheck } import io.vamp.common.http.{ OffsetEnvelope, OffsetResponseEnvelope } import io.vamp.container_driver.ContainerDriverActor.{ GetNodes, GetRoutingGroups } import io.vamp.container_driver.{ ContainerDriverActor, RoutingGroup, SchedulerNode } import io.vamp.model.artifact.RouteSelector import io.vamp.model.notification.InvalidSelectorError import io.vamp.operation.gateway.{ GatewaySelectorResolver, RouteSelectionProcessor } import io.vamp.persistence.ArtifactResponseEnvelope import scala.concurrent.Future import scala.util.Try trait SchedulerController extends ReplyCheck with GatewaySelectorResolver with AbstractController { def nodes(page: Int, perPage: Int)(implicit namespace: Namespace, timeout: Timeout): Future[OffsetResponseEnvelope[SchedulerNode]] = { checked[List[SchedulerNode]](IoC.actorFor[ContainerDriverActor] ? GetNodes) map paginate(page, perPage) } def routing(selector: Option[String])(page: Int, perPage: Int)(implicit namespace: Namespace, timeout: Timeout): Future[OffsetResponseEnvelope[RoutingGroup]] = { val filter = selector.map(s ⇒ Try(RouteSelector(s).verified).getOrElse(throwException(InvalidSelectorError(selector.get)))) checked[List[RoutingGroup]](IoC.actorFor[ContainerDriverActor] ? GetRoutingGroups).map { routingGroups ⇒ val targets = RouteSelectionProcessor.targets( Try(RouteSelector(defaultSelector()).verified).getOrElse(RouteSelector("false")), routingGroups, filter ).map(_.url).toSet val all = routingGroups.flatMap { group ⇒ val instances = group.instances.flatMap { instance ⇒ val ports = instance.ports.flatMap { port ⇒ if (targets.contains(s"${instance.ip}:${port.container}")) port :: Nil else Nil } if (ports.nonEmpty) instance.copy(ports = ports) :: Nil else Nil } if (instances.nonEmpty) group.copy(instances = instances) :: Nil else Nil } paginate(page, perPage)(all) } } private def paginate[T](page: Int, perPage: Int)(items: List[T]): OffsetResponseEnvelope[T] = { val (p, pp) = OffsetEnvelope.normalize(page, perPage, ArtifactResponseEnvelope.maxPerPage) val (rp, rpp) = OffsetEnvelope.normalize(items.size, p, pp, ArtifactResponseEnvelope.maxPerPage) new OffsetResponseEnvelope[T] { val page: Int = rp val perPage: Int = rpp val total: Long = items.size val response: List[T] = items.slice((p - 1) * pp, p * pp) } } }
Example 118
Source File: GatewayWorkflowDeploymentResolver.scala From vamp with Apache License 2.0 | 5 votes |
package io.vamp.operation.controller import akka.pattern.ask import akka.util.Timeout import io.vamp.common.Namespace import io.vamp.common.akka.{ IoC, ReplyCheck } import io.vamp.model.artifact.{ Deployment, Gateway, Workflow } import io.vamp.persistence.PersistenceActor import scala.concurrent.Future trait GatewayWorkflowDeploymentResolver extends ReplyCheck { this: AbstractController ⇒ protected def gatewayFor(name: String)(implicit namespace: Namespace, timeout: Timeout): Future[Option[Gateway]] = { checked[Option[Gateway]](IoC.actorFor[PersistenceActor] ? PersistenceActor.Read(name, classOf[Gateway])) } protected def workflowFor(name: String)(implicit namespace: Namespace, timeout: Timeout): Future[Option[Workflow]] = { checked[Option[Workflow]](IoC.actorFor[PersistenceActor] ? PersistenceActor.Read(name, classOf[Workflow])) } protected def deploymentFor(name: String)(implicit namespace: Namespace, timeout: Timeout): Future[Option[Deployment]] = { checked[Option[Deployment]](IoC.actorFor[PersistenceActor] ? PersistenceActor.Read(name, classOf[Deployment])) } }
Example 119
Source File: GatewayApiController.scala From vamp with Apache License 2.0 | 5 votes |
package io.vamp.operation.controller import akka.pattern.ask import akka.util.Timeout import io.vamp.common.{ Artifact, Namespace } import io.vamp.common.akka.IoC.actorFor import io.vamp.model.artifact.Gateway import io.vamp.model.notification.InconsistentArtifactName import io.vamp.operation.gateway.GatewayActor import io.vamp.persistence.ArtifactExpansionSupport import scala.concurrent.Future trait GatewayApiController extends AbstractController { this: ArtifactExpansionSupport ⇒ protected def createGateway(artifact: Artifact, source: String, validateOnly: Boolean)(implicit namespace: Namespace, timeout: Timeout): Future[Any] = { expandGateway(artifact.asInstanceOf[Gateway]) flatMap { gateway ⇒ actorFor[GatewayActor] ? GatewayActor.Create(gateway, Option(source), validateOnly) } } protected def updateGateway(artifact: Artifact, name: String, source: String, validateOnly: Boolean)(implicit namespace: Namespace, timeout: Timeout): Future[Any] = { expandGateway(artifact.asInstanceOf[Gateway]) flatMap { gateway ⇒ if (name != gateway.name) throwException(InconsistentArtifactName(name, gateway.name)) actorFor[GatewayActor] ? GatewayActor.Update(gateway, Option(source), validateOnly) } } protected def deleteGateway(name: String, validateOnly: Boolean)(implicit namespace: Namespace, timeout: Timeout): Future[Any] = { actorFor[GatewayActor] ? GatewayActor.Delete(name, validateOnly) } }
Example 120
Source File: WorkflowApiController.scala From vamp with Apache License 2.0 | 5 votes |
package io.vamp.operation.controller import akka.pattern.ask import akka.util.Timeout import io.vamp.common.{ Artifact, Namespace } import io.vamp.common.akka.IoC.actorFor import io.vamp.model.artifact._ import io.vamp.model.notification.InconsistentArtifactName import io.vamp.model.reader.WorkflowStatusReader import io.vamp.operation.notification.{ DeploymentWorkflowNameCollision, WorkflowUpdateError } import io.vamp.persistence.{ ArtifactExpansionSupport, PersistenceActor } import scala.concurrent.Future trait WorkflowApiController extends AbstractController { this: ArtifactExpansionSupport ⇒ import PersistenceActor._ protected def createWorkflow(artifact: Artifact, source: String, validateOnly: Boolean)(implicit namespace: Namespace, timeout: Timeout): Future[Any] = { updateWorkflow(artifact, source, validateOnly, create = true) } protected def updateWorkflow(artifact: Artifact, name: String, source: String, validateOnly: Boolean)(implicit namespace: Namespace, timeout: Timeout): Future[Any] = { if (name != artifact.name) throwException(InconsistentArtifactName(name, artifact.name)) updateWorkflow(artifact, source, validateOnly, create = false) } private def updateWorkflow(artifact: Artifact, source: String, validateOnly: Boolean, create: Boolean)(implicit namespace: Namespace, timeout: Timeout): Future[Any] = { artifactForIfExists[Deployment](artifact.name).flatMap { case Some(_) ⇒ throwException(DeploymentWorkflowNameCollision(artifact.name)) case _ ⇒ artifactForIfExists[Workflow](artifact.name).flatMap { case Some(workflow) if workflow.status != Workflow.Status.Suspended ⇒ throwException(WorkflowUpdateError(workflow)) case _ ⇒ if (validateOnly) Future.successful(artifact) else actorFor[PersistenceActor] ? (if (create) PersistenceActor.Create(artifact, Option(source)) else PersistenceActor.Update(artifact, Some(source))) } } map { case list: List[_] ⇒ if (!validateOnly) list.foreach { case workflow: Workflow ⇒ actorFor[PersistenceActor] ? ResetWorkflow(workflow) } list case any ⇒ any } } protected def deleteWorkflow(read: Future[Any], source: String, validateOnly: Boolean)(implicit namespace: Namespace, timeout: Timeout): Future[Any] = { read.flatMap { case Some(workflow: Workflow) ⇒ if (validateOnly) Future.successful(true) else actorFor[PersistenceActor] ? UpdateWorkflowStatus(workflow, Workflow.Status.Stopping) case _ ⇒ Future.successful(false) } } protected def workflowStatus(name: String)(implicit namespace: Namespace, timeout: Timeout): Future[Any] = { (actorFor[PersistenceActor] ? PersistenceActor.Read(name, classOf[Workflow])).map { case Some(workflow: Workflow) ⇒ workflow.status.toString case _ ⇒ None } } def workflowStatusUpdate(name: String, request: String, validateOnly: Boolean)(implicit namespace: Namespace, timeout: Timeout): Future[Any] = { val status = WorkflowStatusReader.status(request) if (validateOnly) Future.successful(status.toString) else actorFor[PersistenceActor] ? PersistenceActor.Read(name, classOf[Workflow]) flatMap { case Some(workflow: Workflow) ⇒ actorFor[PersistenceActor] ? UpdateWorkflowStatus(workflow, status) case _ ⇒ Future(None) } } }
Example 121
Source File: ConfigurationLoaderActor.scala From vamp with Apache License 2.0 | 5 votes |
package io.vamp.operation.config import akka.pattern.ask import akka.util.Timeout import io.vamp.common.{ Config, ConfigFilter, ConfigMagnet } import io.vamp.common.akka._ import io.vamp.operation.notification._ import io.vamp.persistence.KeyValueStoreActor import scala.concurrent.Future import scala.util.Try object ConfigurationLoaderActor { val path: List[String] = "configuration" :: "applied" :: Nil val timeout: ConfigMagnet[Timeout] = KeyValueStoreActor.timeout case class Reload(force: Boolean) case class Get(`type`: String, flatten: Boolean, filter: ConfigFilter) case class Set(input: String, filter: ConfigFilter, validateOnly: Boolean) } class ConfigurationLoaderActor extends CommonSupportForActors with OperationNotificationProvider { import ConfigurationLoaderActor._ protected implicit val timeout: Timeout = ConfigurationLoaderActor.timeout() def receive: Receive = { case Reload(force) ⇒ reload(force) case request: Get ⇒ reply(get(request.`type`, request.flatten, request.filter)) case request: Set ⇒ reply(set(request.input, request.filter, request.validateOnly)) case _ ⇒ } override def preStart(): Unit = { if (Config.boolean("vamp.operation.reload-configuration")()) { val delay = Config.duration("vamp.operation.reload-configuration-delay")() log.info(s"Getting configuration update from key-value store in ${delay.toSeconds} seconds.") context.system.scheduler.scheduleOnce(delay, self, Reload(force = false)) } } protected def get(`type`: String, flatten: Boolean, filter: ConfigFilter): Future[Map[String, Any]] = Future.successful { Config.export( Try(Config.Type.withName(`type`)).getOrElse(Config.Type.applied), flatten, filter ) } protected def set(input: String, filter: ConfigFilter, validateOnly: Boolean): Future[_] = try { val config = if (input.trim.isEmpty) Map[String, Any]() else Config.unmarshall(input.trim, filter) if (validateOnly) Future.successful(config) else IoC.actorFor[KeyValueStoreActor] ? KeyValueStoreActor.Set(path, if (config.isEmpty) None else Option(Config.marshall(config))) map { _ ⇒ reload(force = false) } } catch { case _: Exception ⇒ throwException(InvalidConfigurationError) } protected def reload(force: Boolean): Unit = { IoC.actorFor[KeyValueStoreActor] ? KeyValueStoreActor.Get(path) map { case Some(content: String) if force || ( content != Config.marshall(Config.export(Config.Type.dynamic, flatten = false)) && content != Config.marshall(Config.export(Config.Type.dynamic, flatten = true)) ) ⇒ reload(Config.unmarshall(content)) case None if force || Config.export(Config.Type.dynamic).nonEmpty ⇒ reload(Map[String, Any]()) case _ ⇒ } } protected def reload(config: Map[String, Any]): Unit = { log.info("Reloading due to configuration change") Config.load(config) actorSystem.actorSelection(s"/user/${namespace.name}-config") ! "reload" } }
Example 122
Source File: KubernetesWorkflowActor.scala From vamp with Apache License 2.0 | 5 votes |
package io.vamp.workflow_driver import akka.actor.ActorRef import akka.pattern.ask import io.vamp.common.ClassMapper import io.vamp.common.akka.IoC import io.vamp.container_driver.kubernetes.KubernetesDriverActor.{ CreateJob, DeleteJob } import io.vamp.container_driver.kubernetes.{ Job, K8sClientConfig, KubernetesDriverActor } import io.vamp.container_driver.{ ContainerDriverMapping, ContainerDriverValidation, DeployableType, DockerDeployableType } import io.vamp.model.artifact._ import io.vamp.model.event.Event import io.vamp.model.resolver.WorkflowValueResolver import io.vamp.persistence.PersistenceActor import io.vamp.pulse.Percolator.GetPercolator import io.vamp.pulse.PulseActor import scala.concurrent.Future class KubernetesWorkflowActorMapper extends ClassMapper { val name = "kubernetes" val clazz: Class[_] = classOf[KubernetesWorkflowActor] } class KubernetesWorkflowActor extends DaemonWorkflowDriver with WorkflowValueResolver with ContainerDriverMapping with ContainerDriverValidation { override protected lazy val supportedDeployableTypes: List[DeployableType] = DockerDeployableType :: Nil override protected lazy val info: Future[Map[_, _]] = Future.successful(Map("kubernetes" → Map("url" → K8sClientConfig(customNamespace).url))) override protected lazy val driverActor: ActorRef = IoC.actorFor[KubernetesDriverActor] protected override def request: PartialFunction[Workflow, Unit] = ({ case workflow if workflow.schedule.isInstanceOf[EventSchedule] ⇒ logger.debug("KubernetesWorkflowActor - Workflow schedule is an instance of EventSchedule") IoC.actorFor[PulseActor] ? GetPercolator(WorkflowDriverActor.percolator(workflow)) map { case Some(_) if runnable(workflow) ⇒ if (workflow.instances.isEmpty) { logger.debug(s"KubernetesWorkflowActor - workflow.instances.isEmpty : ${workflow.instances.isEmpty}") IoC.actorFor[PersistenceActor] ! PersistenceActor.UpdateWorkflowInstances(workflow, Instance(workflow.name, "", Map(), deployed = true) :: Nil) } case _ ⇒ if (workflow.instances.nonEmpty) { logger.debug(s"KubernetesWorkflowActor - workflow.instances.nonEmpty : ${workflow.instances.nonEmpty}") IoC.actorFor[PersistenceActor] ! PersistenceActor.UpdateWorkflowInstances(workflow, Nil) } } // case workflow ⇒ // logger.debug("KubernetesWorkflowActor - workflow schedule is not an instance of EventSchedule - {}", workflow.toString) // super.request }: PartialFunction[Workflow, Unit]) orElse { logger.debug("KubernetesWorkflowActor - workflow schedule is not an instance of EventSchedule") super.request } protected override def schedule(data: Any): PartialFunction[Workflow, Future[Any]] = super.schedule(data) orElse { case w if data.isInstanceOf[Event] && w.schedule.isInstanceOf[EventSchedule] ⇒ enrich(w, data).flatMap { workflow ⇒ validateDeployable(workflow.breed.asInstanceOf[DefaultBreed].deployable) val name = s"workflow-${workflow.lookupName}-${data.asInstanceOf[Event].timestamp.toInstant.toEpochMilli}" val scale = workflow.scale.get.asInstanceOf[DefaultScale] driverActor ? CreateJob(Job( name = name, group = group(workflow), docker = docker(workflow), cpu = scale.cpu.value, mem = Math.round(scale.memory.value).toInt, environmentVariables = environment(workflow) )) } } protected override def unschedule(): PartialFunction[Workflow, Future[Any]] = super.unschedule() orElse { case w if w.schedule.isInstanceOf[EventSchedule] ⇒ driverActor ? DeleteJob(group(w)) } override def resolverClasses: List[String] = super[WorkflowValueResolver].resolverClasses private def group(workflow: Workflow) = s"workflow-${workflow.lookupName}" }
Example 123
Source File: ArtifactSupport.scala From vamp with Apache License 2.0 | 5 votes |
package io.vamp.persistence import akka.pattern.ask import io.vamp.common.akka.{ ActorSystemProvider, ExecutionContextProvider, IoC } import io.vamp.common.notification.NotificationProvider import io.vamp.common.{ Artifact, Namespace } import io.vamp.persistence.notification.{ ArtifactNotFound, PersistenceOperationFailure } import scala.concurrent.Future import scala.reflect._ trait ArtifactSupport { this: ActorSystemProvider with ExecutionContextProvider with NotificationProvider ⇒ def artifactFor[T <: Artifact: ClassTag](artifact: Option[Artifact])(implicit namespace: Namespace): Future[Option[T]] = artifact match { case None ⇒ Future.successful(None) case Some(a) ⇒ artifactFor[T](a).map(Some(_)) } def artifactFor[T <: Artifact: ClassTag](artifact: Artifact, force: Boolean = false)(implicit namespace: Namespace): Future[T] = artifact match { case a: T if !force ⇒ Future.successful(a) case _ ⇒ artifactFor[T](artifact.name) } def artifactFor[T <: Artifact: ClassTag](name: String)(implicit namespace: Namespace): Future[T] = { artifactForIfExists[T](name) map { case Some(artifact: T) ⇒ artifact case _ ⇒ throwException(ArtifactNotFound(name, classTag[T].runtimeClass)) } } def artifactForIfExists[T <: Artifact: ClassTag](name: String)(implicit namespace: Namespace): Future[Option[T]] = { implicit val timeout = PersistenceActor.timeout() IoC.actorFor[PersistenceActor] ? PersistenceActor.Read(name, classTag[T].runtimeClass.asInstanceOf[Class[Artifact]]) map { case Some(artifact: T) ⇒ Some(artifact) case Some(artifact) ⇒ throwException(PersistenceOperationFailure(new RuntimeException(s"Expected: [${classTag[T].runtimeClass}], actual: [${artifact.getClass}]"))) case None ⇒ None } } }
Example 124
Source File: PersistenceStats.scala From vamp with Apache License 2.0 | 5 votes |
package io.vamp.persistence import akka.pattern.ask import akka.util.Timeout import io.vamp.common.Artifact import io.vamp.common.akka.CommonProvider import io.vamp.common.akka.IoC._ import io.vamp.model.artifact._ import io.vamp.model.event.{ Aggregator, EventQuery, LongValueAggregationResult } import io.vamp.pulse.{ EventRequestEnvelope, PulseActor } import scala.concurrent.Future private case class DeploymentsStatistics(count: Int, clusters: Int, services: Int) trait PersistenceStats extends ArtifactPaginationSupport with PersistenceTag { this: PersistenceApi with CommonProvider ⇒ protected implicit def timeout: Timeout protected def stats(): Future[Map[String, Any]] = { val types = List( classOf[Gateway], classOf[Breed], classOf[Blueprint], classOf[Sla], classOf[Scale], classOf[Escalation], classOf[Route], classOf[Condition], classOf[Rewrite], classOf[Workflow] ) for { map ← Future.sequence(types.flatMap(artifact)).map(list ⇒ list.reduce((m1, m2) ⇒ m1 ++ m2)) deployments ← deployments() } yield { map + ("deployments" → Map("count" → deployments.count, "clusters" → deployments.clusters, "services" → deployments.services)) } } protected def artifact(`type`: Class[_ <: Artifact]): Option[Future[Map[String, _]]] = { tagFor(`type`).map { tag ⇒ def count(archiveTag: String): Future[Long] = { (actorFor[PulseActor] ? PulseActor.Query(EventRequestEnvelope(EventQuery(Set(tag, archiveTag), None, None, Some(Aggregator(Aggregator.count))), 1, 1))) map { case LongValueAggregationResult(count) ⇒ count case _ ⇒ 0 } } for { created ← count(PersistenceArchive.archiveCreateTag) updated ← count(PersistenceArchive.archiveUpdateTag) deleted ← count(PersistenceArchive.archiveDeleteTag) } yield Map( tag → Map("count" → all(`type`, 1, 1).total, "created" → created, "updated" → updated, "deleted" → deleted) ) } } private def deployments(): Future[DeploymentsStatistics] = { def stats: Deployment ⇒ DeploymentsStatistics = { deployment ⇒ DeploymentsStatistics(1, deployment.clusters.size, deployment.clusters.flatMap(_.services).size) } def reduce: (DeploymentsStatistics, DeploymentsStatistics) ⇒ DeploymentsStatistics = { (s1, s2) ⇒ DeploymentsStatistics(s1.count + s2.count, s1.clusters + s2.clusters, s1.services + s2.services) } collectAll[Deployment, DeploymentsStatistics](allArtifacts[Deployment], { case deployments ⇒ deployments.map(stats).reduce(reduce) }).flatMap(stream ⇒ Future.sequence(stream.toList).map(_.reduce(reduce))) } }
Example 125
Source File: PaginationSupport.scala From vamp with Apache License 2.0 | 5 votes |
package io.vamp.persistence import akka.pattern.ask import akka.util.Timeout import io.vamp.common.{ Artifact, Namespace } import io.vamp.common.akka.{ ActorSystemProvider, ExecutionContextProvider, IoC } import io.vamp.common.http.OffsetResponseEnvelope import io.vamp.common.notification.NotificationProvider import io.vamp.model.event.{ Event, EventQuery } import io.vamp.persistence.notification.PersistenceOperationFailure import io.vamp.pulse.{ EventRequestEnvelope, PulseActor } import scala.concurrent.Future import scala.reflect._ trait PaginationSupport { this: ExecutionContextProvider ⇒ def allPages[T](onePage: (Int, Int) ⇒ Future[_ <: OffsetResponseEnvelope[T]], perPage: Int = 10): Future[Stream[Future[List[T]]]] = { def stream(current: Int, until: Long, envelope: Future[_ <: OffsetResponseEnvelope[T]]): Stream[Future[List[T]]] = { if (current > until) Stream.empty else envelope.map(_.response) #:: stream(current + 1, until, onePage(current + 1, perPage)) } val head = onePage(1, perPage) head.map { envelope ⇒ stream(1, (envelope.total + perPage - 1) / perPage, head) } } def forAll[T](all: Future[Stream[Future[List[T]]]], process: (List[T]) ⇒ Unit) = { all.foreach(_.foreach(_.foreach(process))) } def forEach[T](all: Future[Stream[Future[List[T]]]], process: (T) ⇒ Unit) = { all.foreach(_.foreach(_.foreach(_.foreach(process)))) } def collectAll[A, B](all: Future[Stream[Future[List[A]]]], process: PartialFunction[List[A], B]): Future[Stream[Future[B]]] = { all.map(_.map(_.collect(process))) } def collectEach[A, B](all: Future[Stream[Future[List[A]]]], process: PartialFunction[A, B]): Future[Stream[Future[List[B]]]] = { all.map(_.map(_.map(_.collect(process)))) } def consume[T](all: Future[Stream[Future[List[T]]]]): Future[List[T]] = { all.flatMap { stream ⇒ Future.sequence(stream.toList).map(_.flatten) } } } trait ArtifactPaginationSupport extends PaginationSupport { this: ActorSystemProvider with ExecutionContextProvider with NotificationProvider ⇒ def allArtifacts[T <: Artifact: ClassTag](implicit namespace: Namespace, timeout: Timeout): Future[Stream[Future[List[T]]]] = { allPages[T]((page: Int, perPage: Int) ⇒ { IoC.actorFor[PersistenceActor] ? PersistenceActor.All(classTag[T].runtimeClass.asInstanceOf[Class[_ <: Artifact]], page, perPage) map { case envelope: OffsetResponseEnvelope[_] ⇒ envelope.asInstanceOf[OffsetResponseEnvelope[T]] case other ⇒ throwException(PersistenceOperationFailure(other)) } }, ArtifactResponseEnvelope.maxPerPage) } } trait EventPaginationSupport extends PaginationSupport { this: ActorSystemProvider with ExecutionContextProvider with NotificationProvider ⇒ def allEvents(eventQuery: EventQuery)(implicit namespace: Namespace, timeout: Timeout): Future[Stream[Future[List[Event]]]] = { allPages[Event]((page: Int, perPage: Int) ⇒ { IoC.actorFor[PulseActor] ? PulseActor.Query(EventRequestEnvelope(eventQuery, page, perPage)) map { case envelope: OffsetResponseEnvelope[_] ⇒ envelope.asInstanceOf[OffsetResponseEnvelope[Event]] case other ⇒ throwException(PersistenceOperationFailure(other)) } }, EventRequestEnvelope.maxPerPage) } }
Example 126
Source File: JavascriptBreedRoute.scala From vamp with Apache License 2.0 | 5 votes |
package io.vamp.http_api import akka.http.scaladsl.model.HttpMethods._ import akka.http.scaladsl.model.MediaTypes._ import akka.http.scaladsl.model.StatusCodes._ import akka.pattern.ask import akka.util.Timeout import io.vamp.common.Namespace import io.vamp.common.akka.IoC._ import io.vamp.common.http.HttpApiDirectives import io.vamp.model.artifact.{ DefaultBreed, Deployable } import io.vamp.persistence.PersistenceActor import scala.concurrent.Future trait JavascriptBreedRoute extends AbstractRoute { this: HttpApiDirectives ⇒ def javascriptBreedRoute(implicit namespace: Namespace, timeout: Timeout) = path("breeds" / Remaining) { name ⇒ pathEndOrSingleSlash { (method(PUT) & contentTypeOnly(`application/javascript`)) { entity(as[String]) { request ⇒ validateOnly { validateOnly ⇒ onSuccess(create(name, request, validateOnly)) { result ⇒ respondWith(OK, result) } } } } } } private def create(name: String, source: String, validateOnly: Boolean)(implicit namespace: Namespace, timeout: Timeout): Future[Any] = { val breed = DefaultBreed( name = name, metadata = Map(), deployable = Deployable("application/javascript", source), ports = Nil, environmentVariables = Nil, constants = Nil, arguments = Nil, dependencies = Map(), healthChecks = None ) if (validateOnly) Future.successful(breed) else actorFor[PersistenceActor] ? PersistenceActor.Update(breed, Some(source)) } }
Example 127
Source File: SignatureManagerActor.scala From incubator-toree with Apache License 2.0 | 5 votes |
package org.apache.toree.communication.security import akka.actor.{Props, ActorRef, Actor} import akka.util.Timeout import org.apache.toree.communication.utils.OrderedSupport import org.apache.toree.kernel.protocol.v5.KernelMessage import org.apache.toree.utils.LogLike import scala.concurrent.duration._ import akka.pattern.ask import akka.pattern.pipe class SignatureManagerActor( key: String, scheme: String ) extends Actor with LogLike with OrderedSupport { private val hmac = Hmac(key, HmacAlgorithm(scheme)) def this(key: String) = this(key, HmacAlgorithm.SHA256.toString) // NOTE: Required to provide the execution context for futures with akka import context._ // NOTE: Required for ask (?) to function... maybe can define elsewhere? implicit val timeout = Timeout(5.seconds) // // List of child actors that the signature manager contains // private var signatureChecker: ActorRef = _ private var signatureProducer: ActorRef = _ override def orderedTypes(): Seq[Class[_]] = Seq( classOf[(String, Seq[_])], classOf[KernelMessage] ) }
Example 128
Source File: HeartbeatClient.scala From incubator-toree with Apache License 2.0 | 5 votes |
package org.apache.toree.kernel.protocol.v5.client.socket import akka.actor.{ActorRef, Actor} import akka.util.{ByteString, Timeout} import org.apache.toree.communication.ZMQMessage import akka.pattern.ask import org.apache.toree.kernel.protocol.v5.client.ActorLoader import org.apache.toree.utils.LogLike import org.apache.toree.kernel.protocol.v5.UUID import scala.collection.concurrent.{Map, TrieMap} import scala.concurrent.duration._ object HeartbeatMessage {} class HeartbeatClient( socketFactory : SocketFactory, actorLoader: ActorLoader, signatureEnabled: Boolean ) extends Actor with LogLike { logger.debug("Created new Heartbeat Client actor") implicit val timeout = Timeout(1.minute) val futureMap: Map[UUID, ActorRef] = TrieMap[UUID, ActorRef]() val socket = socketFactory.HeartbeatClient(context.system, self) override def receive: Receive = { // from Heartbeat case message: ZMQMessage => val id = message.frames.map((byteString: ByteString) => new String(byteString.toArray)).mkString("\n") logger.info(s"Heartbeat client receive:$id") futureMap(id) ! true futureMap.remove(id) // from SparkKernelClient case HeartbeatMessage => import scala.concurrent.ExecutionContext.Implicits.global val id = java.util.UUID.randomUUID().toString futureMap += (id -> sender) logger.info(s"Heartbeat client send: $id") val future = socket ? ZMQMessage(ByteString(id.getBytes)) future.onComplete { // future always times out because server "tells" response { case(_) => futureMap.remove(id) } } }
Example 129
Source File: ShellClient.scala From incubator-toree with Apache License 2.0 | 5 votes |
package org.apache.toree.kernel.protocol.v5.client.socket import akka.actor.Actor import akka.util.Timeout import org.apache.toree.communication.ZMQMessage import org.apache.toree.communication.security.SecurityActorType import org.apache.toree.kernel.protocol.v5.client.{ActorLoader, Utilities} import org.apache.toree.kernel.protocol.v5.{KernelMessage, UUID} import Utilities._ import org.apache.toree.kernel.protocol.v5.client.execution.{DeferredExecution, DeferredExecutionManager} import org.apache.toree.kernel.protocol.v5.content.ExecuteReply import org.apache.toree.utils.LogLike import scala.concurrent.Await import scala.concurrent.duration._ import akka.pattern.ask class ShellClient( socketFactory: SocketFactory, actorLoader: ActorLoader, signatureEnabled: Boolean ) extends Actor with LogLike { logger.debug("Created shell client actor") implicit val timeout = Timeout(21474835.seconds) val socket = socketFactory.ShellClient(context.system, self) def receiveExecuteReply(parentId:String, kernelMessage: KernelMessage): Unit = { val deOption: Option[DeferredExecution] = DeferredExecutionManager.get(parentId) deOption match { case None => logger.warn(s"No deferred execution for parent id ${parentId}") case Some(de) => Utilities.parseAndHandle(kernelMessage.contentString, ExecuteReply.executeReplyReads, (er: ExecuteReply) => de.resolveReply(er)) } } override def receive: Receive = { // from shell case message: ZMQMessage => logger.debug("Received shell kernel message.") val kernelMessage: KernelMessage = message // TODO: Validate incoming message signature logger.trace(s"Kernel message is ${kernelMessage}") receiveExecuteReply(message.parentHeader.msg_id,kernelMessage) // from handler case message: KernelMessage => logger.trace(s"Sending kernel message ${message}") val signatureManager = actorLoader.load(SecurityActorType.SignatureManager) import scala.concurrent.ExecutionContext.Implicits.global val messageWithSignature = if (signatureEnabled) { val signatureMessage = signatureManager ? message Await.result(signatureMessage, 100.milliseconds) .asInstanceOf[KernelMessage] } else message val zMQMessage: ZMQMessage = messageWithSignature socket ! zMQMessage } }
Example 130
Source File: StdinClient.scala From incubator-toree with Apache License 2.0 | 5 votes |
package org.apache.toree.kernel.protocol.v5.client.socket import akka.actor.Actor import org.apache.toree.communication.ZMQMessage import org.apache.toree.communication.security.SecurityActorType import org.apache.toree.kernel.protocol.v5.client.ActorLoader import org.apache.toree.kernel.protocol.v5.{HeaderBuilder, KMBuilder, KernelMessage} import org.apache.toree.kernel.protocol.v5.content.{InputReply, InputRequest} import org.apache.toree.utils.LogLike import org.apache.toree.kernel.protocol.v5.client.Utilities._ import play.api.libs.json.Json import StdinClient._ import akka.pattern.ask import scala.concurrent.duration._ import scala.concurrent.Await object StdinClient { case class ResponseFunctionMessage(responseFunction: ResponseFunction) type ResponseFunction = (String, Boolean) => String val EmptyResponseFunction: ResponseFunction = (_, _) => "" } private var responseFunc: ResponseFunction = EmptyResponseFunction override def receive: Receive = { case responseFunctionMessage: ResponseFunctionMessage => logger.debug("Updating response function") this.responseFunc = responseFunctionMessage.responseFunction case message: ZMQMessage => logger.debug("Received stdin kernel message") val kernelMessage: KernelMessage = message val messageType = kernelMessage.header.msg_type if (messageType == InputRequest.toTypeString) { logger.debug("Message is an input request") val inputRequest = Json.parse(kernelMessage.contentString).as[InputRequest] val value = responseFunc(inputRequest.prompt, inputRequest.password) val inputReply = InputReply(value) val newKernelMessage = KMBuilder() .withParent(kernelMessage) .withHeader(HeaderBuilder.empty.copy( msg_type = InputReply.toTypeString, session = getSessionId )) .withContentString(inputReply) .build import scala.concurrent.ExecutionContext.Implicits.global val messageWithSignature = if (signatureEnabled) { val signatureManager = actorLoader.load(SecurityActorType.SignatureManager) val signatureMessage = signatureManager ? newKernelMessage Await.result(signatureMessage, 100.milliseconds) .asInstanceOf[KernelMessage] } else newKernelMessage val zmqMessage: ZMQMessage = messageWithSignature socket ! zmqMessage } else { logger.debug(s"Unknown message of type $messageType") } } }
Example 131
Source File: SparkKernelClient.scala From incubator-toree with Apache License 2.0 | 5 votes |
package org.apache.toree.kernel.protocol.v5.client import akka.actor.ActorSystem import akka.pattern.ask import akka.util.Timeout import org.apache.toree.comm._ import org.apache.toree.kernel.protocol.v5._ import org.apache.toree.kernel.protocol.v5.client.execution.{DeferredExecution, ExecuteRequestTuple} import org.apache.toree.kernel.protocol.v5.client.socket.HeartbeatMessage import org.apache.toree.kernel.protocol.v5.client.socket.StdinClient.{ResponseFunctionMessage, ResponseFunction} import org.apache.toree.kernel.protocol.v5.content.ExecuteRequest import org.apache.toree.utils.LogLike import scala.concurrent.duration._ import scala.concurrent.ExecutionContext.Implicits.global import scala.util.{Failure, Success} val comm = new ClientCommManager( actorLoader = actorLoader, kmBuilder = KMBuilder(), commRegistrar = commRegistrar ) // TODO: hide this? just heartbeat to see if kernel is reachable? def heartbeat(failure: () => Unit): Unit = { val future = actorLoader.load(SocketType.Heartbeat) ? HeartbeatMessage future.onComplete { case Success(_) => logger.info("Client received heartbeat.") case Failure(_) => failure() logger.info("There was an error receiving heartbeat from kernel.") } } def shutdown() = { logger.info("Shutting down client") actorSystem.terminate() } }
Example 132
Source File: KernelInputStream.scala From incubator-toree with Apache License 2.0 | 5 votes |
package org.apache.toree.kernel.protocol.v5.stream import java.io.InputStream import java.nio.charset.Charset import akka.pattern.ask import org.apache.toree.kernel.protocol.v5.content.InputRequest import org.apache.toree.kernel.protocol.v5.kernel.ActorLoader import org.apache.toree.kernel.protocol.v5.kernel.Utilities.timeout import org.apache.toree.kernel.protocol.v5.{KMBuilder, MessageType} import scala.collection.mutable.ListBuffer import scala.concurrent.{Await, Future} import KernelInputStream._ object KernelInputStream { val DefaultPrompt = "" val DefaultPassword = false } override def read(): Int = { if (!this.hasByte) this.requestBytes() this.nextByte() } private def hasByte: Boolean = internalBytes.nonEmpty private def nextByte(): Int = { val byte = internalBytes.head internalBytes = internalBytes.tail byte } private def requestBytes(): Unit = { val inputRequest = InputRequest(prompt, password) // NOTE: Assuming already provided parent header and correct ids val kernelMessage = kmBuilder .withHeader(MessageType.Outgoing.InputRequest) .withContentString(inputRequest) .build // NOTE: The same handler is being used in both request and reply val responseFuture: Future[String] = (actorLoader.load(MessageType.Incoming.InputReply) ? kernelMessage) .mapTo[String] // Block until we get a response import scala.concurrent.duration._ internalBytes ++= Await.result(responseFuture, Duration.Inf).getBytes(EncodingType) } }
Example 133
Source File: CodeCompleteHandler.scala From incubator-toree with Apache License 2.0 | 5 votes |
package org.apache.toree.kernel.protocol.v5.handler import akka.pattern.ask import org.apache.toree.kernel.protocol.v5._ import org.apache.toree.kernel.protocol.v5.content._ import org.apache.toree.kernel.protocol.v5.kernel.{ActorLoader, Utilities} import Utilities._ import org.apache.toree.utils.{MessageLogSupport, LogLike} import play.api.data.validation.ValidationError import play.api.libs.json.{JsPath, Json} import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.Future import scala.util.Success class CodeCompleteHandler(actorLoader: ActorLoader) extends BaseHandler(actorLoader) with MessageLogSupport { override def process(kernelMessage: KernelMessage): Future[_] = { logKernelMessageAction("Generating code completion for", kernelMessage) Utilities.parseAndHandle( kernelMessage.contentString, CompleteRequest.completeRequestReads, completeRequest(kernelMessage, _ : CompleteRequest) ) } private def completeRequest(km: KernelMessage, cr: CompleteRequest): Future[(Int, List[String])] = { val interpreterActor = actorLoader.load(SystemActorType.Interpreter) val codeCompleteFuture = ask(interpreterActor, cr).mapTo[(Int, List[String])] codeCompleteFuture.onComplete { case Success(tuple) => val reply = CompleteReplyOk(tuple._2, tuple._1, cr.cursor_pos, Metadata()) val completeReplyType = MessageType.Outgoing.CompleteReply.toString logKernelMessageAction("Sending code complete reply for", km) actorLoader.load(SystemActorType.KernelMessageRelay) ! km.copy( header = HeaderBuilder.create(completeReplyType), parentHeader = km.header, contentString = Json.toJson(reply).toString ) case _ => new Exception("Parse error in CodeCompleteHandler") } codeCompleteFuture } }
Example 134
Source File: AkkaExecutionSequencer.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.grpc.adapter import akka.Done import akka.actor.{Actor, ActorLogging, ActorRef, ActorSystem, ExtendedActorSystem, Props} import akka.pattern.{AskTimeoutException, ask} import akka.util.Timeout import com.daml.grpc.adapter.RunnableSequencingActor.ShutdownRequest import scala.concurrent.duration.FiniteDuration import scala.concurrent.{ExecutionContext, Future} import scala.util.control.NonFatal import com.daml.dec.DirectExecutionContext def closeAsync(implicit ec: ExecutionContext): Future[Done] = (actorRef ? ShutdownRequest).mapTo[Done].recover { case askTimeoutException: AskTimeoutException if actorIsTerminated(askTimeoutException) => Done } private def actorIsTerminated(askTimeoutException: AskTimeoutException) = { AkkaExecutionSequencer.actorTerminatedRegex.findFirstIn(askTimeoutException.getMessage).nonEmpty } } object AkkaExecutionSequencer { def apply(name: String, terminationTimeout: FiniteDuration)( implicit system: ActorSystem): AkkaExecutionSequencer = { system match { case extendedSystem: ExtendedActorSystem => new AkkaExecutionSequencer( extendedSystem.systemActorOf(Props[RunnableSequencingActor], name))( Timeout.durationToTimeout(terminationTimeout)) case _ => new AkkaExecutionSequencer(system.actorOf(Props[RunnableSequencingActor], name))( Timeout.durationToTimeout(terminationTimeout)) } } private val actorTerminatedRegex = """Recipient\[.*]\] had already been terminated.""".r } private[grpc] class RunnableSequencingActor extends Actor with ActorLogging { @SuppressWarnings(Array("org.wartremover.warts.Any")) override val receive: Receive = { case runnable: Runnable => try { runnable.run() } catch { case NonFatal(t) => log.error("Unexpected exception while executing Runnable", t) } case ShutdownRequest => context.stop(self) // processing of the current message will continue sender() ! Done } } private[grpc] object RunnableSequencingActor { case object ShutdownRequest }
Example 135
Source File: Create.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.navigator.console.commands import java.util.concurrent.TimeUnit import com.daml.ledger.api.refinements.ApiTypes import com.daml.navigator.console._ import com.daml.lf.value.json.ApiCodecCompressed import com.daml.navigator.model import com.daml.navigator.store.Store.CreateContract import akka.pattern.ask import akka.util.Timeout import scala.concurrent.{Await, ExecutionContext, Future} import scala.concurrent.duration._ import scala.util.Try @SuppressWarnings(Array("org.wartremover.warts.Product", "org.wartremover.warts.Serializable")) case object Create extends SimpleCommand { def name: String = "create" def description: String = "Create a contract" def params: List[Parameter] = List( ParameterTemplateId("template", "Template ID"), ParameterLiteral("with"), ParameterDamlValue("argument", "Contract argument") ) def sendCommand( state: State, ps: model.PartyState, template: String, arg: model.ApiRecord): Future[ApiTypes.CommandId] = { implicit val actorTimeout: Timeout = Timeout(20, TimeUnit.SECONDS) implicit val executionContext: ExecutionContext = state.ec val command = CreateContract( ps, model.TemplateStringId(template), arg ) (state.store ? command) .mapTo[Try[ApiTypes.CommandId]] .map(c => c.get) } def eval( state: State, args: List[String], set: CommandSet): Either[CommandError, (State, String)] = { args match { case templateName :: w :: damlA if w.equalsIgnoreCase("with") => for { ps <- state.getPartyState ~> s"Unknown party ${state.party}" templateId <- model.parseOpaqueIdentifier(templateName) ~> s"Unknown template $templateName" apiValue <- Try( ApiCodecCompressed.stringToApiType( damlA.mkString(" "), templateId, ps.packageRegistry.damlLfDefDataType _)) ~> "Failed to parse DAML value" apiRecord <- Try(apiValue.asInstanceOf[model.ApiRecord]) ~> "Record argument required" future <- Try(sendCommand(state, ps, templateName, apiRecord)) ~> "Failed to create contract" commandId <- Try(Await.result(future, 30.seconds)) ~> "Failed to create contract" } yield { (state, Pretty.yaml(Pretty.commandResult(ps, commandId))) } case _ => Left(CommandError("Invalid syntax", None)) } } }
Example 136
Source File: SetTime.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.navigator.console.commands import java.time.Instant import java.time.format.DateTimeFormatter import java.util.concurrent.TimeUnit import com.daml.navigator.console._ import com.daml.navigator.store.Store.AdvanceTime import com.daml.navigator.time.TimeProviderWithType import akka.pattern.ask import akka.util.Timeout import scala.concurrent.{Await, ExecutionContext, Future} import scala.concurrent.duration._ import scala.util.Try case object SetTime extends SimpleCommand { def name: String = "set_time" def description: String = "Set the (static) ledger effective time" def params: List[Parameter] = List(ParameterString("time", "New (static) ledger effective time")) private def advanceTime(state: State, newTime: Instant): Future[TimeProviderWithType] = { implicit val actorTimeout: Timeout = Timeout(20, TimeUnit.SECONDS) implicit val executionContext: ExecutionContext = state.ec (state.store ? AdvanceTime(newTime)) .mapTo[Try[TimeProviderWithType]] .map(t => t.get) } private def formatTime(t: Instant): String = DateTimeFormatter.ISO_INSTANT.format(t) def eval( state: State, args: List[String], set: CommandSet): Either[CommandError, (State, String)] = { for { arg1 <- args.headOption ~> "Missing <time> argument" newTime <- Try(Instant.parse(arg1)) ~> "Failed to parse time" future <- Try(advanceTime(state, newTime)) ~> "Failed to advance time" confirmedTime <- Try(Await.result(future, 30.seconds)) ~> "Failed to advance time" result <- Try(formatTime(confirmedTime.time.getCurrentTime)) ~> "Failed to format time" } yield { (state, s"New ledger effective time: $result") } } }
Example 137
Source File: Exercise.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.navigator.console.commands import java.util.concurrent.TimeUnit import com.daml.ledger.api.refinements.ApiTypes import com.daml.navigator.console._ import com.daml.lf.value.Value.ValueUnit import com.daml.lf.value.json.ApiCodecCompressed import com.daml.navigator.model import com.daml.navigator.store.Store.ExerciseChoice import akka.pattern.ask import akka.util.Timeout import com.daml.navigator.model.ApiValue import scala.concurrent.{Await, ExecutionContext, Future} import scala.concurrent.duration._ import scala.util.Try @SuppressWarnings(Array("org.wartremover.warts.Product", "org.wartremover.warts.Serializable")) case object Exercise extends SimpleCommand { def name: String = "exercise" def description: String = "Exercises a choice" def params: List[Parameter] = List( ParameterContractId("contract", "Contract ID"), ParameterChoiceId("choice", "Name of the choice"), ParameterLiteral("with"), ParameterDamlValue("argument", "Choice argument") ) def sendCommand( state: State, ps: model.PartyState, contract: String, choice: String, arg: model.ApiValue): Future[ApiTypes.CommandId] = { implicit val actorTimeout: Timeout = Timeout(20, TimeUnit.SECONDS) implicit val executionContext: ExecutionContext = state.ec val command = ExerciseChoice( ps, ApiTypes.ContractId(contract), ApiTypes.Choice(choice), arg ) (state.store ? command) .mapTo[Try[ApiTypes.CommandId]] .map(c => c.get) } def exerciseChoice( state: State, cid: String, choice: String, damlA: Option[List[String]]): Either[CommandError, (State, String)] = { for { ps <- state.getPartyState ~> s"Unknown party ${state.party}" types = ps.packageRegistry contract <- ps.ledger.contract(ApiTypes.ContractId(cid), types) ~> s"Unknown contract $cid" choiceType <- contract.template.choices .find(c => ApiTypes.Choice.unwrap(c.name) == choice) ~> s"Unknown choice $choice" apiValue <- Try( // Use unit value if no argument is given damlA.fold[ApiValue](ValueUnit)( arg => ApiCodecCompressed.stringToApiType( arg.mkString(" "), choiceType.parameter, ps.packageRegistry.damlLfDefDataType _))) ~> "Failed to parse choice argument" future <- Try(sendCommand(state, ps, cid, choice, apiValue)) ~> "Failed to exercise choice" commandId <- Try(Await.result(future, 30.seconds)) ~> "Failed to exercise choice" } yield { (state, Pretty.yaml(Pretty.commandResult(ps, commandId))) } } def eval( state: State, args: List[String], set: CommandSet): Either[CommandError, (State, String)] = { args match { case cid :: choice :: Nil => exerciseChoice(state, cid, choice, None) case cid :: choice :: w :: damlA if w.equalsIgnoreCase("with") => exerciseChoice(state, cid, choice, Some(damlA)) case _ => Left(CommandError("Invalid syntax", None)) } } }
Example 138
Source File: Time.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.navigator.console.commands import java.time.Instant import java.time.format.DateTimeFormatter import java.util.concurrent.TimeUnit import com.daml.navigator.console._ import com.daml.navigator.store.Store.ReportCurrentTime import com.daml.navigator.time.TimeProviderWithType import akka.pattern.ask import akka.util.Timeout import scala.concurrent.{Await, ExecutionContext, Future} import scala.concurrent.duration._ import scala.util.Try case object Time extends SimpleCommand { def name: String = "time" def description: String = "Print the ledger effective time" def params: List[Parameter] = List.empty def getTime(state: State): Future[TimeProviderWithType] = { implicit val actorTimeout: Timeout = Timeout(20, TimeUnit.SECONDS) implicit val executionContext: ExecutionContext = state.ec (state.store ? ReportCurrentTime) .mapTo[Try[TimeProviderWithType]] .map(t => t.get) } def formatTime(t: Instant): String = DateTimeFormatter.ISO_INSTANT.format(t) def eval( state: State, args: List[String], set: CommandSet): Either[CommandError, (State, String)] = { for { future <- Try(getTime(state)) ~> "Failed to get time" time <- Try(Await.result(future, 30.seconds)) ~> "Failed to get time" result <- Try(formatTime(time.time.getCurrentTime)) ~> "Failed to format time" } yield { (state, result) } } }
Example 139
Source File: ChaosActorInterface.scala From eventuate-chaos with Apache License 2.0 | 5 votes |
package com.rbmhtechnology.eventuate.chaos import akka.actor.ActorRef import akka.io.Tcp import akka.util.ByteString import akka.pattern.ask import akka.util.Timeout import com.rbmhtechnology.eventuate.chaos.ChaosActorInterface.HealthCheckResult import com.rbmhtechnology.eventuate.chaos.ChaosActorInterface.HealthCheck import scala.concurrent.duration._ import scala.util.Failure import scala.util.Success object ChaosActorInterface { case class HealthCheck(requester: ActorRef) case class HealthCheckResult(state: Int, requester: ActorRef) } class ChaosActorInterface(chaosActor: ActorRef) extends ChaosInterface { implicit val timeout = Timeout(1.seconds) def handleCommand = { case ("persist", None, recv) => val check = HealthCheck(recv) (chaosActor ? check).mapTo[HealthCheckResult] onComplete { case Success(result) => result.requester ! Tcp.Write(ByteString(result.state.toString)) result.requester ! Tcp.Close case Failure(e) => recv ! Tcp.Close } } }
Example 140
Source File: NetService.scala From mantis with Apache License 2.0 | 5 votes |
package io.iohk.ethereum.jsonrpc import akka.actor.ActorRef import akka.agent.Agent import akka.util.Timeout import io.iohk.ethereum.jsonrpc.NetService.NetServiceConfig import io.iohk.ethereum.network.PeerManagerActor import io.iohk.ethereum.utils.ServerStatus.{Listening, NotListening} import io.iohk.ethereum.utils.{Config, NodeStatus} import scala.concurrent.Future import scala.concurrent.duration._ import scala.concurrent.ExecutionContext.Implicits.global object NetService { case class VersionRequest() case class VersionResponse(value: String) case class ListeningRequest() case class ListeningResponse(value: Boolean) case class PeerCountRequest() case class PeerCountResponse(value: Int) case class NetServiceConfig(peerManagerTimeout: FiniteDuration) object NetServiceConfig { def apply(etcClientConfig: com.typesafe.config.Config): NetServiceConfig = { val netServiceConfig = etcClientConfig.getConfig("network.rpc.net") NetServiceConfig( peerManagerTimeout = netServiceConfig.getDuration("peer-manager-timeout").toMillis.millis) } } } class NetService(nodeStatusHolder: Agent[NodeStatus], peerManager: ActorRef, config: NetServiceConfig) { import NetService._ def version(req: VersionRequest): ServiceResponse[VersionResponse] = Future.successful(Right(VersionResponse(Config.Network.peer.networkId.toString))) def listening(req: ListeningRequest): ServiceResponse[ListeningResponse] = { Future.successful { Right( nodeStatusHolder().serverStatus match { case _: Listening => ListeningResponse(true) case NotListening => ListeningResponse(false) } ) } } def peerCount(req: PeerCountRequest): ServiceResponse[PeerCountResponse] = { import akka.pattern.ask implicit val timeout = Timeout(config.peerManagerTimeout) (peerManager ? PeerManagerActor.GetPeers) .mapTo[PeerManagerActor.Peers] .map { peers => Right(PeerCountResponse(peers.handshaked.size)) } } }
Example 141
Source File: PersonTest.scala From akka-serialization-test with Apache License 2.0 | 5 votes |
package com.github.dnvriend.domain import akka.actor.{ ActorRef, Props } import akka.pattern.ask import akka.persistence.query.EventEnvelope import akka.stream.scaladsl.{ Sink, Source } import akka.testkit.TestProbe import com.github.dnvriend.TestSpec import com.github.dnvriend.domain.Person._ import com.github.dnvriend.persistence.ProtobufReader import proto.person.Command._ class PersonTest extends TestSpec { import com.github.dnvriend.persistence.ProtobufFormats._ def withPerson(id: String)(f: ActorRef ⇒ TestProbe ⇒ Unit): Unit = { val tp = TestProbe() val ref = system.actorOf(Props(new Person(id))) try f(ref)(tp) finally killActors(ref) } "Person" should "register a name" in { withPerson("p1") { ref ⇒ tp ⇒ Source(List(RegisterNameCommand("dennis", "vriend"))) .mapAsync(1)(ref ? _).runWith(Sink.ignore).futureValue } withPerson("p1") { ref ⇒ tp ⇒ Source(List(RegisterNameCommand("dennis", "vriend"))) .mapAsync(1)(ref ? _).runWith(Sink.ignore).futureValue } // note that the persistence-query does not use the deserializer // so the protobuf must be deserialized inline eventsForPersistenceIdSource("p1").collect { case EventEnvelope(_, _, _, proto: NameRegisteredMessage) ⇒ implicitly[ProtobufReader[NameRegisteredEvent]].read(proto) }.testProbe { tp ⇒ tp.request(Int.MaxValue) tp.expectNext(NameRegisteredEvent("dennis", "vriend")) tp.expectNext(NameRegisteredEvent("dennis", "vriend")) tp.expectComplete() } } it should "update its name and surname" in { withPerson("p2") { ref ⇒ tp ⇒ Source(List(RegisterNameCommand("dennis", "vriend"), ChangeNameCommand("jimi"), ChangeSurnameCommand("hendrix"))) .mapAsync(1)(ref ? _).runWith(Sink.ignore).futureValue } eventsForPersistenceIdSource("p2").collect { case EventEnvelope(_, _, _, proto: NameRegisteredMessage) ⇒ implicitly[ProtobufReader[NameRegisteredEvent]].read(proto) case EventEnvelope(_, _, _, proto: NameChangedMessage) ⇒ implicitly[ProtobufReader[NameChangedEvent]].read(proto) case EventEnvelope(_, _, _, proto: SurnameChangedMessage) ⇒ implicitly[ProtobufReader[SurnameChangedEvent]].read(proto) }.testProbe { tp ⇒ tp.request(Int.MaxValue) tp.expectNext(NameRegisteredEvent("dennis", "vriend")) tp.expectNext(NameChangedEvent("jimi")) tp.expectNext(SurnameChangedEvent("hendrix")) tp.expectComplete() } } }
Example 142
Source File: AlbumTest.scala From akka-serialization-test with Apache License 2.0 | 5 votes |
// package com.github.dnvriend.domain import java.time.Duration import akka.pattern.ask import akka.stream.scaladsl.{ Sink, Source } import com.github.dnvriend.TestSpec import com.github.dnvriend.domain.Music._ import com.github.dnvriend.repository.AlbumRepository class AlbumTest extends TestSpec { "Album" should "register a title" in { val album = AlbumRepository.forId("album-1") val xs = List(ChangeAlbumTitle("Dark side of the Moon")) Source(xs).mapAsync(1)(album ? _).runWith(Sink.ignore).futureValue eventsForPersistenceIdSource("album-1").map(_.event).testProbe { tp ⇒ tp.request(Int.MaxValue) tp.expectNextN(xs.map(cmd ⇒ TitleChanged(cmd.title))) tp.expectComplete() } killActors(album) } it should "update its title and year and songs" in { val album = AlbumRepository.forId("album-2") val xs = List( ChangeAlbumTitle("Dark side of the Moon"), ChangeAlbumYear(1973), AddSong(Song("Money", Duration.ofSeconds(390))), AddSong(Song("Redemption Song", Duration.ofSeconds(227))), RemoveSong(Song("Redemption Song", Duration.ofSeconds(227))) ) val expectedEvents = xs.map { case ChangeAlbumTitle(title) ⇒ TitleChanged(title) case ChangeAlbumYear(year) ⇒ YearChanged(year) case AddSong(song) ⇒ SongAdded(song) case RemoveSong(song) ⇒ SongRemoved(song) } Source(xs).mapAsync(1)(album ? _).runWith(Sink.ignore).futureValue eventsForPersistenceIdSource("album-2").map(_.event).testProbe { tp ⇒ tp.request(Int.MaxValue) tp.expectNextN(expectedEvents) tp.expectComplete() } } }
Example 143
Source File: AggregateStateGetter.scala From akka-tools with MIT License | 5 votes |
package no.nextgentel.oss.akkatools.testing import java.util.concurrent.TimeUnit import akka.actor.{ActorRef, ActorSystem} import akka.pattern.ask import akka.util.Timeout import no.nextgentel.oss.akkatools.persistence.GetState import scala.concurrent.Await import scala.concurrent.duration.Duration import scala.reflect.ClassTag object AggregateStateGetter { val defaultTimeout = Duration("60s") def apply[S:ClassTag](aggregateActorRef:ActorRef, timeout:Duration = defaultTimeout)(implicit system:ActorSystem):AggregateStateGetter[S] = new AggregateStateGetter[S](system, aggregateActorRef, timeout) } import AggregateStateGetter._ class AggregateStateGetter[S:ClassTag](system:ActorSystem, aggregateActorRef:ActorRef, timeout:Duration) { def getState():S = getState(None) def getState(aggregateId:Option[String]):S = { implicit val ec = system.dispatcher implicit val t = Timeout(timeout.toMillis, TimeUnit.MILLISECONDS) val getStateMsg = aggregateId match { case Some(id) => GetState(id) case None => GetState() } val f = ask(aggregateActorRef, getStateMsg).mapTo[S] Await.result(f, timeout) } } class AggregateStateGetterJava(system:ActorSystem, aggregateActorRef:ActorRef, timeout:Duration) extends AggregateStateGetter[Any](system, aggregateActorRef, timeout) { def this(system:ActorSystem, aggregateActorRef:ActorRef) = this(system, aggregateActorRef, defaultTimeout) }
Example 144
Source File: CurrentPersistenceIds1Test.scala From akka-persistence-dynamodb with Apache License 2.0 | 5 votes |
package com.github.j5ik2o.akka.persistence.dynamodb.query.query import java.net.URI import akka.pattern.ask import akka.persistence.query.{ EventEnvelope, Sequence } import com.github.j5ik2o.akka.persistence.dynamodb.query.QueryJournalSpec import com.github.j5ik2o.akka.persistence.dynamodb.utils.{ DynamoDBSpecSupport, RandomPortUtil } import com.github.j5ik2o.reactive.aws.dynamodb.DynamoDbAsyncClient import com.typesafe.config.{ Config, ConfigFactory } import software.amazon.awssdk.auth.credentials.{ AwsBasicCredentials, StaticCredentialsProvider } import software.amazon.awssdk.services.dynamodb.{ DynamoDbAsyncClient => JavaDynamoDbAsyncClient } import scala.concurrent.Future import scala.concurrent.duration._ abstract class CurrentPersistenceIds1Test(config: Config) extends QueryJournalSpec(config) { it should "not find any events for unknown pid" in withCurrentEventsByPersistenceId()("unkown-pid", 0L, Long.MaxValue) { tp => tp.request(Int.MaxValue) tp.expectComplete() } it should "find events from an offset" in { withTestActors() { (actor1, actor2, actor3) => Future.sequence(Range.inclusive(1, 4).map(_ => actor1 ? "a")).toTry should be a Symbol("success") withCurrentEventsByPersistenceId()("my-1", 2, 3) { tp => tp.request(Int.MaxValue) tp.expectNext(new EventEnvelope(Sequence(2), "my-1", 2, "a-2", 0L)) tp.expectNext(new EventEnvelope(Sequence(3), "my-1", 3, "a-3", 0L)) tp.expectComplete() } } } } object DynamoDBCurrentPersistenceIds1Test { val dynamoDBPort = RandomPortUtil.temporaryServerPort() } class DynamoDBCurrentPersistenceIds1Test extends CurrentPersistenceIds1Test( ConfigFactory .parseString( s""" |j5ik2o.dynamo-db-journal { | query-batch-size = 1 | dynamo-db-client { | endpoint = "http://127.0.0.1:${DynamoDBCurrentPersistenceIds1Test.dynamoDBPort}/" | } |} | |j5ik2o.dynamo-db-snapshot.dynamo-db-client { | endpoint = "http://127.0.0.1:${DynamoDBCurrentPersistenceIds1Test.dynamoDBPort}/" |} | |j5ik2o.dynamo-db-read-journal { | query-batch-size = 1 | dynamo-db-client { | endpoint = "http://127.0.0.1:${DynamoDBCurrentPersistenceIds1Test.dynamoDBPort}/" | } |} """.stripMargin ).withFallback(ConfigFactory.load("query-reference")) ) with DynamoDBSpecSupport { override implicit val pc: PatienceConfig = PatienceConfig(30 seconds, 1 seconds) override protected lazy val dynamoDBPort: Int = DynamoDBCurrentPersistenceIds1Test.dynamoDBPort val underlying: JavaDynamoDbAsyncClient = JavaDynamoDbAsyncClient .builder() .credentialsProvider( StaticCredentialsProvider.create(AwsBasicCredentials.create(accessKeyId, secretAccessKey)) ) .endpointOverride(URI.create(dynamoDBEndpoint)) .build() override def dynamoDbAsyncClient: DynamoDbAsyncClient = DynamoDbAsyncClient(underlying) override def afterAll(): Unit = { underlying.close() super.afterAll() } before { createTable } after { deleteTable } }
Example 145
Source File: CurrentEventsByTagTest4.scala From akka-persistence-dynamodb with Apache License 2.0 | 5 votes |
package com.github.j5ik2o.akka.persistence.dynamodb.query.query import java.net.URI import akka.pattern.ask import akka.persistence.query.{ EventEnvelope, Sequence } import com.github.j5ik2o.akka.persistence.dynamodb.query.QueryJournalSpec import com.github.j5ik2o.akka.persistence.dynamodb.utils.{ DynamoDBSpecSupport, RandomPortUtil } import com.github.j5ik2o.reactive.aws.dynamodb.DynamoDbAsyncClient import com.typesafe.config.{ Config, ConfigFactory } import software.amazon.awssdk.auth.credentials.{ AwsBasicCredentials, StaticCredentialsProvider } import software.amazon.awssdk.services.dynamodb.{ DynamoDbAsyncClient => JavaDynamoDbAsyncClient } import scala.concurrent.duration._ abstract class CurrentEventsByTagTest4(config: Config) extends QueryJournalSpec(config) { it should "persist and find a tagged event with one tag" in withTestActors() { (actor1, actor2, actor3) => (actor1 ? withTags(1, "one2")).toTry should be a Symbol("success") withClue("query should find the event by tag") { withCurrentEventsByTag()("one2", 0) { tp => tp.request(Int.MaxValue) tp.expectNextPF { case EventEnvelope(Sequence(1), _, _, _) => } tp.expectComplete() } } withClue("query should find the event by persistenceId") { withCurrentEventsByPersistenceId()("my-1", 1, 1) { tp => tp.request(Int.MaxValue) tp.expectNextPF { case EventEnvelope(Sequence(1), _, _, _) => } tp.expectComplete() } } } } object DynamoDBCurrentEventsByTagTest4 { val dynamoDBPort = RandomPortUtil.temporaryServerPort() } class DynamoDBCurrentEventsByTagTest4 extends CurrentEventsByTagTest4( ConfigFactory .parseString( s""" |j5ik2o.dynamo-db-journal{ | query-batch-size = 1 | dynamo-db-client { | endpoint = "http://127.0.0.1:${DynamoDBCurrentEventsByTagTest4.dynamoDBPort}/" | } |} | |j5ik2o.dynamo-db-snapshot.dynamo-db-client { | endpoint = "http://127.0.0.1:${DynamoDBCurrentEventsByTagTest4.dynamoDBPort}/" |} | |j5ik2o.dynamo-db-read-journal { | query-batch-size = 1 | dynamo-db-client { | endpoint = "http://127.0.0.1:${DynamoDBCurrentEventsByTagTest4.dynamoDBPort}/" | } |} """.stripMargin ).withFallback(ConfigFactory.load("query-reference")) ) with DynamoDBSpecSupport { override implicit val pc: PatienceConfig = PatienceConfig(30 seconds, 1 seconds) override protected lazy val dynamoDBPort: Int = DynamoDBCurrentEventsByTagTest4.dynamoDBPort val underlying: JavaDynamoDbAsyncClient = JavaDynamoDbAsyncClient .builder() .credentialsProvider( StaticCredentialsProvider.create(AwsBasicCredentials.create(accessKeyId, secretAccessKey)) ) .endpointOverride(URI.create(dynamoDBEndpoint)) .build() override def dynamoDbAsyncClient: DynamoDbAsyncClient = DynamoDbAsyncClient(underlying) override def afterAll(): Unit = { underlying.close() super.afterAll() } before { createTable } after { deleteTable } }
Example 146
Source File: RandomDataProducer.scala From parquet4s with MIT License | 5 votes |
package com.github.mjakubowski84.parquet4s.indefinite import akka.actor.{Actor, ActorRef, Cancellable, Props, Scheduler} import akka.pattern.ask import akka.util.Timeout import scala.concurrent.duration._ import scala.concurrent.{Await, ExecutionContext} import scala.util.Random object RandomDataProducer { private val words = Seq("Example", "how", "to", "setup", "indefinite", "stream", "with", "Parquet", "writer") } trait RandomDataProducer { this: Akka with Logger with Kafka => import RandomDataProducer._ private def nextWord: String = words(Random.nextInt(words.size - 1)) private def action(): Unit = sendKafkaMessage(nextWord) private lazy val scheduler: ActorRef = system.actorOf(FluctuatingSchedulerActor.props(action)) implicit private val stopTimeout: Timeout = new Timeout(FluctuatingSchedulerActor.MaxDelay) def startDataProducer(): Unit = { logger.info("Starting scheduler that sends messages to Kafka...") scheduler ! FluctuatingSchedulerActor.Start } def stopDataProducer(): Unit = { logger.info("Stopping scheduler...") Await.ready(scheduler.ask(FluctuatingSchedulerActor.Stop), Duration.Inf) } } private object FluctuatingSchedulerActor { case object Start case object ScheduleNext case object Stop val MinDelay: FiniteDuration = 1.milli val MaxDelay: FiniteDuration = 500.millis val StartDelay: FiniteDuration = 100.millis trait Direction case object Up extends Direction case object Down extends Direction def props(action: () => Unit): Props = Props(new FluctuatingSchedulerActor(action)) } private class FluctuatingSchedulerActor(action: () => Unit) extends Actor { import FluctuatingSchedulerActor._ implicit def executionContext: ExecutionContext = context.system.dispatcher def scheduler: Scheduler = context.system.scheduler var scheduled: Option[Cancellable] = None override def receive: Receive = { case Start => self ! ScheduleNext context.become(scheduling(StartDelay, direction = Down), discardOld = true) } def scheduling(delay: FiniteDuration, direction: Direction): Receive = { case ScheduleNext => action() val rate = Random.nextFloat / 10.0f val step = (delay.toMillis * rate).millis val (newDirection, newDelay) = direction match { case Up if delay + step < MaxDelay => (Up, delay + step) case Up => (Down, delay - step) case Down if delay - step > MinDelay => (Down, delay - step) case Down => (Up, delay + step) } scheduled = Some(scheduler.scheduleOnce(delay, self, ScheduleNext)) context.become(scheduling(newDelay, newDirection), discardOld = true) case Stop => scheduled.foreach(_.cancel()) context.stop(self) } }
Example 147
Source File: Actors.scala From scala-concurrency-playground with MIT License | 5 votes |
package org.zalando.benchmarks import akka.actor._ import akka.pattern.{ask, pipe} import akka.routing.BalancingPool import akka.util.Timeout import org.zalando.benchmarks.ComputationFollowedByAsyncPublishing._ import scala.concurrent.{Await, Future} import scala.concurrent.duration._ class Actors(system: ActorSystem) { def benchmark(coreFactor: Int): Unit = { import system.dispatcher implicit val timeout = Timeout(1 hour) // Route computations through a balanced pool of (cpu bound) computation workers. val router = system actorOf BalancingPool(numWorkers(coreFactor)).props(Props[ComputeActor]) try { // Collect the results, sum them up and print the sum. printResult(Await.result(Future.traverse(1 to numTasks map Job) { job => (router ? job).mapTo[PublishResult] }, 1 hour)) } finally { // Shut down the actors. router ! PoisonPill } } } // Actor responsible for the computation, and for delegating to the publishing actor(s). class ComputeActor extends Actor { val publisher = context actorOf Props[PublishActor] def receive = { case job: Job => // tell the publisher about who sent us the job, and the job results val s = sender() publisher ! (s, Computer compute job) } } // Actor responsible for publishing, and for sending the response back. class PublishActor extends Actor { import context.dispatcher def receive = { case (s: ActorRef, r: JobResult) => // just pipe the result back to the original sender Publisher.publish(r, context.system) pipeTo s } }
Example 148
Source File: Server.scala From glint with MIT License | 5 votes |
package glint import java.util.concurrent.TimeUnit import akka.actor._ import akka.pattern.ask import akka.util.Timeout import com.typesafe.config.Config import com.typesafe.scalalogging.slf4j.StrictLogging import glint.messages.master.RegisterServer import scala.concurrent.duration._ import scala.concurrent.{ExecutionContext, Future} def run(config: Config): Future[(ActorSystem, ActorRef)] = { logger.debug(s"Starting actor system ${config.getString("glint.server.system")}") val system = ActorSystem(config.getString("glint.server.system"), config.getConfig("glint.server")) logger.debug("Starting server actor") val server = system.actorOf(Props[Server], config.getString("glint.server.name")) logger.debug("Reading master information from config") val masterHost = config.getString("glint.master.host") val masterPort = config.getInt("glint.master.port") val masterName = config.getString("glint.master.name") val masterSystem = config.getString("glint.master.system") logger.info(s"Registering with master ${masterSystem}@${masterHost}:${masterPort}/user/${masterName}") implicit val ec = ExecutionContext.Implicits.global implicit val timeout = Timeout(config.getDuration("glint.server.registration-timeout", TimeUnit.MILLISECONDS) milliseconds) val master = system.actorSelection(s"akka.tcp://${masterSystem}@${masterHost}:${masterPort}/user/${masterName}") val registration = master ? RegisterServer(server) registration.map { case a => logger.info("Server successfully registered with master") (system, server) } } }
Example 149
Source File: TokenizerWrapper.scala From dbpedia-spotlight-model with Apache License 2.0 | 5 votes |
package org.dbpedia.spotlight.db.concurrent import java.io.IOException import java.util.concurrent.TimeUnit import akka.actor.SupervisorStrategy.Restart import akka.actor.{Actor, ActorSystem, OneForOneStrategy, Props} import akka.pattern.ask import akka.routing.SmallestMailboxRouter import akka.util import org.apache.commons.lang.NotImplementedException import org.dbpedia.spotlight.db.model.{StringTokenizer, TextTokenizer} import org.dbpedia.spotlight.model.{Text, Token} import scala.concurrent.Await class TokenizerWrapper(val tokenizers: Seq[TextTokenizer]) extends TextTokenizer { var requestTimeout = 60 val system = ActorSystem() val workers = tokenizers.map { case tokenizer: TextTokenizer => system.actorOf(Props(new TokenizerActor(tokenizer))) }.seq def size: Int = tokenizers.size val router = system.actorOf(Props[TokenizerActor].withRouter( // This might be a hack SmallestMailboxRouter(scala.collection.immutable.Iterable(workers:_*)).withSupervisorStrategy( OneForOneStrategy(maxNrOfRetries = 10) { case _: IOException => Restart }) ) ) implicit val timeout = util.Timeout(requestTimeout, TimeUnit.SECONDS) override def tokenizeMaybe(text: Text) { val futureResult = router ? TokenizerRequest(text) Await.result(futureResult, timeout.duration) } override def tokenize(text: Text): List[Token] = { tokenizeMaybe(text) text.featureValue[List[Token]]("tokens").get } def tokenizeRaw(text: String): Seq[String] = { throw new NotImplementedException() } def close() { system.shutdown() } def getStringTokenizer: StringTokenizer = tokenizers.head.getStringTokenizer } class TokenizerActor(val tokenizer: TextTokenizer) extends Actor { def receive = { case TokenizerRequest(text) => { try { sender ! tokenizer.tokenizeMaybe(text) } catch { case e: NullPointerException => throw new IOException("Could not tokenize.") } } } } case class TokenizerRequest(text: Text)
Example 150
Source File: Mutation.scala From sangria-subscriptions-example with Apache License 2.0 | 5 votes |
import java.util.UUID import schema.MutationError import generic.View.Get import sangria.macros.derive.GraphQLField import akka.pattern.ask trait Mutation { this: Ctx ⇒ @GraphQLField def createAuthor(firstName: String, lastName: String) = addEvent[Author](authors, AuthorCreated(UUID.randomUUID.toString, 1, firstName, lastName)) @GraphQLField def changeAuthorName(id: String, version: Long, firstName: String, lastName: String) = loadLatestVersion(id, version) flatMap (version ⇒ addEvent[Author](authors, AuthorNameChanged(id, version, firstName, lastName))) @GraphQLField def deleteAuthor(id: String, version: Long) = for { version ← loadLatestVersion(id, version) author ← (authors ? Get(id)).mapTo[Option[Author]] _ ← addDeleteEvent(AuthorDeleted(id, version)) } yield author @GraphQLField def createArticle(title: String, authorId: String, text: Option[String]) = (authors ? Get(authorId)) flatMap { case Some(author: Author) ⇒ addEvent[Article](articles, ArticleCreated(UUID.randomUUID.toString, 1, title, author.id, text)) case _ ⇒ throw MutationError(s"Author with ID '$authorId' does not exist.") } @GraphQLField def changeArticleText(id: String, version: Long, text: Option[String]) = loadLatestVersion(id, version) flatMap (version ⇒ addEvent[Article](articles, ArticleTextChanged(id, version, text))) @GraphQLField def deleteArticle(id: String, version: Long) = for { version ← loadLatestVersion(id, version) author ← (articles ? Get(id)).mapTo[Option[Article]] _ ← addDeleteEvent(ArticleDeleted(id, version)) } yield author }
Example 151
Source File: schema.scala From sangria-subscriptions-example with Apache License 2.0 | 5 votes |
import akka.actor.ActorRef import akka.util.Timeout import generic.{Event, Versioned, View} import generic.View.Get import sangria.execution.UserFacingError import sangria.schema._ import sangria.macros.derive._ import akka.pattern.ask import akka.stream.Materializer import sangria.execution.deferred.{Fetcher, HasId} import scala.concurrent.ExecutionContext import sangria.streaming.akkaStreams._ object schema { case class MutationError(message: String) extends Exception(message) with UserFacingError val authors = Fetcher.caching((c: Ctx, ids: Seq[String]) ⇒ c.loadAuthors(ids))(HasId(_.id)) def createSchema(implicit timeout: Timeout, ec: ExecutionContext, mat: Materializer) = { val VersionedType = InterfaceType("Versioned", fields[Ctx, Versioned]( Field("id", StringType, resolve = _.value.id), Field("version", LongType, resolve = _.value.version))) implicit val AuthorType = deriveObjectType[Unit, Author](Interfaces(VersionedType)) val EventType = InterfaceType("Event", fields[Ctx, Event]( Field("id", StringType, resolve = _.value.id), Field("version", LongType, resolve = _.value.version))) val AuthorCreatedType = deriveObjectType[Unit, AuthorCreated](Interfaces(EventType)) val AuthorNameChangedType = deriveObjectType[Unit, AuthorNameChanged](Interfaces(EventType)) val AuthorDeletedType = deriveObjectType[Unit, AuthorDeleted](Interfaces(EventType)) val ArticleCreatedType = deriveObjectType[Unit, ArticleCreated]( Interfaces(EventType), ReplaceField("authorId", Field("author", OptionType(AuthorType), resolve = c ⇒ authors.deferOpt(c.value.authorId)))) val ArticleTextChangedType = deriveObjectType[Unit, ArticleTextChanged](Interfaces(EventType)) val ArticleDeletedType = deriveObjectType[Unit, ArticleDeleted](Interfaces(EventType)) implicit val ArticleType = deriveObjectType[Ctx, Article]( Interfaces(VersionedType), ReplaceField("authorId", Field("author", OptionType(AuthorType), resolve = c ⇒ authors.deferOpt(c.value.authorId)))) val IdArg = Argument("id", StringType) val OffsetArg = Argument("offset", OptionInputType(IntType), 0) val LimitArg = Argument("limit", OptionInputType(IntType), 100) def entityFields[T](name: String, tpe: ObjectType[Ctx, T], actor: Ctx ⇒ ActorRef) = fields[Ctx, Unit]( Field(name, OptionType(tpe), arguments = IdArg :: Nil, resolve = c ⇒ (actor(c.ctx) ? Get(c.arg(IdArg))).mapTo[Option[T]]), Field(name + "s", ListType(tpe), arguments = OffsetArg :: LimitArg :: Nil, resolve = c ⇒ (actor(c.ctx) ? View.List(c.arg(OffsetArg), c.arg(LimitArg))).mapTo[Seq[T]])) val QueryType = ObjectType("Query", entityFields[Author]("author", AuthorType, _.authors) ++ entityFields[Article]("article", ArticleType, _.articles)) val MutationType = deriveContextObjectType[Ctx, Mutation, Unit](identity) def subscriptionField[T <: Event](tpe: ObjectType[Ctx, T]) = { val fieldName = tpe.name.head.toLower + tpe.name.tail Field.subs(fieldName, tpe, resolve = (c: Context[Ctx, Unit]) ⇒ c.ctx.eventStream .filter(event ⇒ tpe.valClass.isAssignableFrom(event.getClass)) .map(event ⇒ Action(event.asInstanceOf[T]))) } val SubscriptionType = ObjectType("Subscription", fields[Ctx, Unit]( subscriptionField(AuthorCreatedType), subscriptionField(AuthorNameChangedType), subscriptionField(AuthorDeletedType), subscriptionField(ArticleCreatedType), subscriptionField(ArticleTextChangedType), subscriptionField(ArticleDeletedType), Field.subs("allEvents", EventType, resolve = _.ctx.eventStream.map(Action(_))) )) Schema(QueryType, Some(MutationType), Some(SubscriptionType)) } }
Example 152
Source File: Ctx.scala From sangria-subscriptions-example with Apache License 2.0 | 5 votes |
import akka.NotUsed import akka.util.Timeout import schema.MutationError import akka.actor.ActorRef import generic.Event import generic.MemoryEventStore._ import generic.View.{Get, GetMany} import akka.pattern.ask import akka.stream.OverflowStrategy import akka.stream.scaladsl.Source import org.reactivestreams.Publisher import scala.concurrent.{ExecutionContext, Future} case class Ctx( authors: ActorRef, articles: ActorRef, eventStore: ActorRef, eventStorePublisher: Publisher[Event], ec: ExecutionContext, to: Timeout ) extends Mutation { implicit def executionContext = ec implicit def timeout = to lazy val eventStream: Source[Event, NotUsed] = Source.fromPublisher(eventStorePublisher).buffer(100, OverflowStrategy.fail) def addEvent[T](view: ActorRef, event: Event) = (eventStore ? AddEvent(event)).flatMap { case EventAdded(_) ⇒ (view ? Get(event.id, Some(event.version))).mapTo[Option[T]] case OverCapacity(_) ⇒ throw MutationError("Service is overloaded.") case ConcurrentModification(_, latestVersion) ⇒ throw MutationError(s"Concurrent Modification error for entity '${event.id}'. Latest entity version is '$latestVersion'.") } def addDeleteEvent(event: Event) = (eventStore ? AddEvent(event)).map { case EventAdded(e) ⇒ e case OverCapacity(_) ⇒ throw MutationError("Service is overloaded.") case ConcurrentModification(_, latestVersion) ⇒ throw MutationError(s"Concurrent Modification error for entity '${event.id}'. Latest entity version is '$latestVersion'.") } def loadLatestVersion(id: String, version: Long): Future[Long] = (eventStore ? LatestEventVersion(id)) map { case Some(latestVersion: Long) if version != latestVersion ⇒ throw MutationError(s"Concurrent Modification error for entity '$id'. Latest entity version is '$latestVersion'.") case Some(version: Long) ⇒ version + 1 case _ ⇒ throw MutationError(s"Entity with ID '$id' does not exist.") } def loadAuthors(ids: Seq[String]) = (authors ? GetMany(ids)).mapTo[Seq[Author]] }
Example 153
Source File: WordCountConsumer.scala From akka_streams_tutorial with MIT License | 5 votes |
package alpakka.kafka import akka.Done import akka.actor.{ActorSystem, Props} import akka.kafka.scaladsl.Consumer.DrainingControl import akka.kafka.scaladsl.{Committer, Consumer} import akka.kafka.{CommitterSettings, ConsumerSettings, Subscriptions} import akka.stream.scaladsl.Sink import akka.util.Timeout import alpakka.kafka.TotalFake.{IncrementMessage, IncrementWord} import org.apache.kafka.clients.consumer.ConsumerConfig import org.apache.kafka.common.serialization.{LongDeserializer, StringDeserializer} import scala.concurrent.Future import scala.concurrent.duration._ object WordCountConsumer extends App { implicit val system = ActorSystem("WordCountConsumer") implicit val ec = system.dispatcher val total = system.actorOf(Props[TotalFake], "totalFake") val committerSettings = CommitterSettings(system).withMaxBatch(1) def createConsumerSettings(group: String): ConsumerSettings[String, java.lang.Long] = { ConsumerSettings(system, new StringDeserializer , new LongDeserializer) .withBootstrapServers("localhost:9092") .withGroupId(group) //Define consumer behavior upon starting to read a partition for which it does not have a committed offset or if the committed offset it has is invalid .withProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest") } def createAndRunConsumerWordCount(id: String) = { Consumer.committableSource(createConsumerSettings("wordcount consumer group"), Subscriptions.topics("wordcount-output")) .mapAsync(1) { msg => //println(s"$id - Offset: ${msg.record.offset()} - Partition: ${msg.record.partition()} Consume msg with key: ${msg.record.key()} and value: ${msg.record.value()}") if (msg.record.key().equalsIgnoreCase("fakeNews")) { //hardcoded because WordCountProducer.fakeNewsKeyword does not work import akka.pattern.ask implicit val askTimeout: Timeout = Timeout(3.seconds) (total ? IncrementWord(msg.record.value.toInt, id)) .mapTo[Done] .map(_ => msg.committableOffset) } else { Future(msg).map(_ => msg.committableOffset) } } .via(Committer.flow(committerSettings)) .toMat(Sink.seq)(DrainingControl.apply) .run() } def createAndRunConsumerMessageCount(id: String) = { Consumer.committableSource(createConsumerSettings("messagecount consumer group"), Subscriptions.topics("messagecount-output")) .mapAsync(1) { msg => //println(s"$id - Offset: ${msg.record.offset()} - Partition: ${msg.record.partition()} Consume msg with key: ${msg.record.key()} and value: ${msg.record.value()}") import akka.pattern.ask implicit val askTimeout: Timeout = Timeout(3.seconds) (total ? IncrementMessage(msg.record.value.toInt, id)) .mapTo[Done] .map(_ => msg.committableOffset) } .via(Committer.flow(committerSettings)) .toMat(Sink.seq)(DrainingControl.apply) .run() } val drainingControlW1 = createAndRunConsumerWordCount("W.1") val drainingControlW2 = createAndRunConsumerWordCount("W.2") val drainingControlM = createAndRunConsumerMessageCount("M") sys.addShutdownHook{ println("Got control-c cmd from shell, about to shutdown...") drainingControlW1.drainAndShutdown() drainingControlW2.drainAndShutdown() drainingControlM.drainAndShutdown() } }
Example 154
Source File: TotalTweetsScheduler.scala From redrock with Apache License 2.0 | 5 votes |
package com.restapi import java.io.{File, FileInputStream} import akka.actor.{ActorRef, Actor, ActorSystem, Props} import akka.io.IO import org.slf4j.LoggerFactory import play.api.libs.json.Json import spray.can.Http import akka.pattern.ask import spray.http.DateTime import scala.concurrent.duration._ import akka.util.Timeout import scala.concurrent.ExecutionContext.Implicits.global import org.apache.commons.codec.digest.DigestUtils import scala.io.Source case object GetTotalTweetsScheduler object CurrentTotalTweets { @volatile var totalTweets: Long = 0 } class ExecuterTotalTweetsES(delay: FiniteDuration, interval: FiniteDuration) extends Actor { context.system.scheduler.schedule(delay, interval) { getTotalTweetsES } val logger = LoggerFactory.getLogger(this.getClass) override def receive: Actor.Receive = { case GetTotalTweetsScheduler => { logger.info(s"Getting Total of Tweets. Begin: ${CurrentTotalTweets.totalTweets}") } case _ => // just ignore any messages } def getTotalTweetsES: Unit = { val elasticsearchRequests = new GetElasticsearchResponse(0, Array[String](), Array[String](), LoadConf.restConf.getString("searchParam.defaulStartDatetime"), LoadConf.restConf.getString("searchParam.defaultEndDatetime"), LoadConf.esConf.getString("decahoseIndexName")) val totalTweetsResponse = Json.parse(elasticsearchRequests.getTotalTweetsESResponse()) logger.info(s"Getting Total of Tweets. Current: ${CurrentTotalTweets.totalTweets}") CurrentTotalTweets.totalTweets = (totalTweetsResponse \ "hits" \ "total").as[Long] logger.info(s"Total users updated. New: ${CurrentTotalTweets.totalTweets}") } }
Example 155
Source File: Application.scala From redrock with Apache License 2.0 | 5 votes |
package com.restapi import akka.actor.{ActorSystem, Props} import akka.io.IO import spray.can.Http import akka.pattern.ask import scala.concurrent.duration._ import akka.util.Timeout import org.slf4j.LoggerFactory; object Application extends App { val logger = LoggerFactory.getLogger(this.getClass) // we need an ActorSystem to host our application in implicit val system = ActorSystem(LoadConf.restConf.getString("actor")) // create and start our service actor val service = system.actorOf(Props[MyServiceActor], LoadConf.restConf.getString("name")) val sessionTimeout = system.actorOf(Props[SessionTimeoutActor]) val sessionTable = system.actorOf(Props(classOf[SimpleSession], sessionTimeout, LoadConf.accessConf.getInt("delay") seconds, LoadConf.accessConf.getInt("timeout-interval") seconds)) sessionTable ! InitSessionTable val sessionLoader = system.actorOf(Props(classOf[LoadSessionActor], sessionTable, LoadConf.accessConf.getInt("delay") seconds, LoadConf.accessConf.getInt("check-interval") seconds)) sessionLoader ! InitFileMd5Sum val schedTotalTweets = system.actorOf(Props(classOf[ExecuterTotalTweetsES], LoadConf.restConf.getInt("totalTweetsScheduler.delay") seconds, LoadConf.restConf.getInt("totalTweetsScheduler.reapeatEvery") seconds)) schedTotalTweets ! GetTotalTweetsScheduler implicit val timeout = Timeout(800.seconds) IO(Http) ? Http.Bind(service, interface = "0.0.0.0", port = LoadConf.restConf.getInt("port")) logger.info( s"""Application: ${LoadConf.globalConf.getString("appName")} running version: ${LoadConf.globalConf.getString("appVersion")}""".stripMargin) }
Example 156
Source File: AmqpTermination.scala From gatling-amqp with MIT License | 5 votes |
package io.gatling.amqp.config import akka.pattern.ask import akka.util.Timeout import io.gatling.amqp.data._ import io.gatling.core.session.Session import pl.project13.scala.rainbow._ import scala.concurrent.Await import scala.concurrent.duration._ import scala.util._ trait AmqpTermination { this: AmqpProtocol => private val publishTimeout: Timeout = Timeout(1 hour) private val consumeTimeout: Timeout = Timeout(1 hour) protected def awaitTerminationFor(session: Session): Unit = { // wait nacker to ensure all confirms has been fired Await.result((nacker ask WaitTermination(session))(publishTimeout), Duration.Inf) match { case Success(m) => logger.debug(s"amqp: $m".green) case Failure(e) => throw e } // wait consumers Await.result((router ask WaitTermination(session))(consumeTimeout), Duration.Inf) match { case Success(m) => logger.debug(s"amqp: $m".green) case Failure(e) => throw e } } }
Example 157
Source File: AmqpPreparation.scala From gatling-amqp with MIT License | 5 votes |
package io.gatling.amqp.config import akka.pattern.ask import akka.util.Timeout import pl.project13.scala.rainbow._ import scala.concurrent.Await import scala.concurrent.duration._ import scala.util._ trait AmqpPreparation { this: AmqpProtocol => private val prepareTimeout: Timeout = Timeout(3 seconds) protected def awaitPreparation(): Unit = { for (msg <- preparings) { Await.result((manage ask msg)(prepareTimeout), Duration.Inf) match { case Success(m) => logger.info(s"amqp: $m".green) case Failure(e) => throw e } } } }
Example 158
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 159
Source File: ApplicationApiRoute.scala From Sidechains-SDK with MIT License | 5 votes |
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 160
Source File: SidechainNodeViewHolderTest.scala From Sidechains-SDK with MIT License | 5 votes |
package com.horizen.actors import java.util.concurrent.TimeUnit import akka.actor.{ActorRef, ActorSystem} import akka.pattern.ask import akka.testkit.TestKit import akka.util.Timeout import com.horizen.SidechainNodeViewHolder.ReceivableMessages.GetDataFromCurrentSidechainNodeView import com.horizen.fixtures.SidechainNodeViewHolderFixture import com.horizen.node.SidechainNodeView import org.scalatest.{BeforeAndAfterAll, FunSuiteLike} import scala.concurrent._ import scala.concurrent.duration._ import org.scalatest._ import org.junit.runner.RunWith import org.scalatest.junit.JUnitRunner @RunWith(classOf[JUnitRunner]) class SidechainNodeViewHolderTest extends Suites( new SidechainNodeViewHolderTest1, new SidechainNodeViewHolderTest2 ) @RunWith(classOf[JUnitRunner]) class SidechainNodeViewHolderTest1 extends TestKit(ActorSystem("testsystem")) with FunSuiteLike with BeforeAndAfterAll with SidechainNodeViewHolderFixture { implicit val timeout = Timeout(5, TimeUnit.SECONDS) override def afterAll: Unit = { //info("Actor system is shutting down...") TestKit.shutdownActorSystem(system) } test ("Test1") { def f(v: SidechainNodeView) = v val sidechainNodeViewHolderRef: ActorRef = getSidechainNodeViewHolderRef val nodeView = (sidechainNodeViewHolderRef ? GetDataFromCurrentSidechainNodeView(f)) .mapTo[SidechainNodeView] assert(Await.result(nodeView, 5 seconds) != null) } test("Test2") { } } @RunWith(classOf[JUnitRunner]) class SidechainNodeViewHolderTest2 extends TestKit(ActorSystem("testSystem")) with FeatureSpecLike with BeforeAndAfterAll with Matchers with SidechainNodeViewHolderFixture { implicit val timeout = Timeout(5, TimeUnit.SECONDS) override def afterAll: Unit = { //info("Actor system is shutting down...") TestKit.shutdownActorSystem(system) } feature("Actor1") { scenario("Scenario 1"){ system should not be(null) def f(v: SidechainNodeView) = v val sidechainNodeViewHolderRef: ActorRef = getSidechainNodeViewHolderRef val nodeView = (sidechainNodeViewHolderRef ? GetDataFromCurrentSidechainNodeView(f)) .mapTo[SidechainNodeView] Await.result(nodeView, 5 seconds) should not be(null) } } }
Example 161
Source File: HistoryApiRoute.scala From EncryCore with GNU General Public License v3.0 | 5 votes |
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 162
Source File: InfoApiRoute.scala From EncryCore with GNU General Public License v3.0 | 5 votes |
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 163
Source File: PeersApiRoute.scala From EncryCore with GNU General Public License v3.0 | 5 votes |
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 164
Source File: ProcessStep.scala From process with Apache License 2.0 | 5 votes |
package processframework import scala.concurrent.duration.Duration import scala.concurrent.{ ExecutionContext, Future, Promise } import scala.reflect.ClassTag import akka.actor.{ Actor, ActorContext, ActorRef, Props } import akka.util.Timeout trait ProcessStep[S] { implicit def context: ActorContext private[processframework] val promise: Promise[Unit] = Promise[Unit]() type Execution = S ⇒ Unit type UpdateFunction = PartialFunction[Process.Event, S ⇒ S] type CommandToEvent = PartialFunction[Any, Process.Event] def execute()(implicit process: ActorRef): Execution def receiveCommand: CommandToEvent def updateState: UpdateFunction def retryInterval: Duration = Duration.Inf final def isCompleted = promise.isCompleted final def markDone(): Unit = promise.trySuccess(()) final def markDone(newState: S): S = { markDone() newState } private[processframework] def abort(): Unit = promise.tryFailure(new RuntimeException("Process aborted")) final def onComplete(completeFn: ((ActorContext, S)) ⇒ Unit)(implicit executionContext: ExecutionContext, process: ActorRef): Unit = promise.future.foreach { _ ⇒ process ! PersistentProcess.Perform(completeFn) } final def onCompleteAsync(completeFn: ⇒ Unit)(implicit executionContext: ExecutionContext): Unit = promise.future.foreach(_ ⇒ completeFn) final def ~>(next: ProcessStep[S]*)(implicit context: ActorContext): ProcessStep[S] = new Chain(this, next: _*) private[processframework] def run()(implicit process: ActorRef, executionContext: ExecutionContext, classTag: ClassTag[S]): Future[Unit] = runImpl private val innerActor = context.actorOf(Props(new Actor { def receive = { case msg if receiveCommand.isDefinedAt(msg) ⇒ val event = receiveCommand(msg) context.parent ! event } })) private[processframework] def handleUpdateState: UpdateFunction = if (isCompleted) PartialFunction.empty[Process.Event, S ⇒ S] else updateState private[processframework] def handleReceiveCommand: CommandToEvent = if (isCompleted) PartialFunction.empty[Any, Process.Event] else receiveCommand private[processframework] def executeWithPossibleRetry()(implicit process: ActorRef): Execution = { state ⇒ implicit val _ = context.dispatcher if (retryInterval.isFinite()) context.system.scheduler.scheduleOnce(Duration.fromNanos(retryInterval.toNanos)) { if (!isCompleted) executeWithPossibleRetry()(process)(state) } execute()(process)(state) } private[processframework] def runImpl()(implicit process: ActorRef, executionContext: ExecutionContext, classTag: ClassTag[S]): Future[Unit] = { import akka.pattern.ask import scala.concurrent.duration._ implicit val timeout: Timeout = 5 seconds if (!isCompleted) (process ? Process.GetState).mapTo[S].foreach(executeWithPossibleRetry()(innerActor)) promise.future } }
Example 165
Source File: ProcessStepTestSupport.scala From process with Apache License 2.0 | 5 votes |
package processframework import akka.pattern.ask import akka.actor.{ ActorRef, ActorContext, Actor, Props } import akka.util.Timeout import scala.concurrent.duration._ import scala.concurrent.Await import scala.reflect.ClassTag import akka.testkit.{ TestProbe, TestKit } import org.scalatest.BeforeAndAfterEach object ProcessStepTestSupport { case object GetStep case object ACommand case object AnEvent extends Process.Event } trait ProcessStepTestSupport[S, PS <: ProcessStep[S]] { this: TestKit with BeforeAndAfterEach ⇒ implicit val timeout: Timeout = 1 second var testProbe: TestProbe = null var processActor: ActorRef = null override protected def beforeEach(): Unit = { testProbe = createTestProbe() processActor = createProcessActor() } def createTestProbe(): TestProbe def createProcessStep(executeProbe: TestProbe)(implicit context: ActorContext): PS def createProcessActor() = system.actorOf(Props(new Actor { val step = createProcessStep(testProbe) def receive = { case msg if sender() == step ⇒ testActor forward msg case ProcessStepTestSupport.GetStep ⇒ sender() ! step case e: Process.Event ⇒ testActor ! e } })) def processStep()(implicit classTag: ClassTag[PS]): PS = Await.result[PS]((processActor ? ProcessStepTestSupport.GetStep).mapTo[PS], 2 seconds) }
Example 166
Source File: AkkaThriftTaskHandler.scala From DataXServer with Apache License 2.0 | 5 votes |
package org.tianlangstudio.data.hamal.yarn.thrift import java.util import java.util.UUID import akka.pattern.ask import akka.actor.ActorRef import akka.util.Timeout import org.apache.commons.codec.digest.DigestUtils import org.tianlangstudio.data.hamal.common.{TaskCost, TaskResult} import scala.concurrent.Await import scala.concurrent.duration._ import org.tianlangstudio.data.hamal.core.{ConfigUtil, Constants} import org.tianlangstudio.data.hamal.server.thrift.ThriftServer import org.tianlangstudio.data.hamal.yarn.{CancelTask, SubmitTask, TaskInfo} import org.tianlangstudio.data.hamal.yarn.server.handler.AkkaTaskHandler import org.tianlangstudio.data.hamal.yarn.util.Utils import org.tianlangstudio.data.hamal.common.TaskCost /** * Created by zhuhq on 2016/4/27. */ class AkkaThriftTaskHandler(taskSchedulerActor:ActorRef) extends AkkaTaskHandler(taskSchedulerActor = taskSchedulerActor) with ThriftServer.Iface{ override def submitTask(taskConfPath: String): String = { submitTaskWithParams(taskConfPath,null) } override def getTaskStatus(taskId: String): String = { if(TaskInfo.taskId2ExecutorId.contains(taskId) || TaskInfo.acceptedTaskIds.contains(taskId) || TaskInfo.rerunTaskIds.contains(taskId)) { Constants.TASK_STATUS_RUNNING }else if(TaskInfo.taskId2Result.contains(taskId)) { Constants.TASK_STATUS_DONE }else { "" } } override def getTaskCost(taskId: String): TaskCost = { null } override def submitTaskWithParams(taskConfPath: String, params: util.Map[String, String]): String = { //val taskId = UUID.randomUUID().toString val taskDesc = ConfigUtil.readTaskDescIfInFileAndReplaceHolder(taskConfPath,params) //val taskId = DigestUtils.md5Hex(taskDesc); val taskId = Utils.genTaskId() taskSchedulerActor ! SubmitTask(taskId,taskDesc) taskId } override def cancelTask(taskId: String): Boolean = { taskSchedulerActor ! CancelTask(taskId) true } override def getTaskResult(taskId: String): TaskResult = { if(Constants.TASK_STATUS_DONE.equals(getTaskStatus(taskId))) { TaskInfo.taskId2Result.get(taskId) match { case Some(taskResult) => taskResult case _ => null } }else { null } } }
Example 167
Source File: PolicyManager.scala From wookiee with Apache License 2.0 | 5 votes |
package com.webtrends.harness.policy import akka.pattern.{ask, pipe} import akka.actor.{ActorRef, Props} import akka.routing.{RoundRobinPool, FromConfig} import com.webtrends.harness.HarnessConstants import com.webtrends.harness.app.{PrepareForShutdown, HActor} import com.webtrends.harness.app.HarnessActor.SystemReady import org.slf4j.LoggerFactory import scala.collection.mutable import scala.concurrent.{Future, Promise} import scala.util.{Success, Failure} case class GetPolicies() class PolicyManager extends PrepareForShutdown { import context.dispatcher override def receive = super.receive orElse { case GetPolicies => pipe(getPolicies) to sender case SystemReady => // ignore } protected def getPolicies : Future[Map[String, Policy]] = { Future { PolicyManager.getPolicies.get } } } object PolicyManager { private val externalLogger = LoggerFactory.getLogger(this.getClass) // map that stores the name of the command with the actor it references val policyMap = mutable.Map[String, Policy]() def props = Props[PolicyManager] def addPolicy[T<:Policy](name:String, ref:T) = { ref.addCommands externalLogger.debug(s"Policy $name inserted into Policy Manager map.") policyMap += (name -> ref) } protected def removePolicy(name:String) : Boolean = { policyMap.get(name) match { case Some(n) => externalLogger.debug(s"Policy $name removed from Policy Manager map.") policyMap -= name true case None => false } } def getPolicy(name:String) : Option[Policy] = policyMap.get(name) def getPolicies : Option[Map[String, Policy]] = Some(policyMap.toMap) }
Example 168
Source File: Component.scala From wookiee with Apache License 2.0 | 5 votes |
package com.webtrends.harness.component import akka.actor.{ActorRef, Status} import akka.pattern.ask import akka.util.Timeout import com.webtrends.harness.HarnessConstants import com.webtrends.harness.app.HActor import com.webtrends.harness.app.HarnessActor.{ConfigChange, PrepareForShutdown, SystemReady} import scala.concurrent.duration._ import scala.util.{Failure, Success} sealed class ComponentMessages() case class StartComponent() extends ComponentMessages case class StopComponent() extends ComponentMessages case class ComponentRequest[T](msg:T, name:Option[String]=None, timeout:Timeout=5 seconds) extends ComponentMessages case class ComponentMessage[T](msg:T, name:Option[String]=None) extends ComponentMessages case class ComponentResponse[T](resp:T) def prepareForShutdown() = {} } object Component { def getActorPath() : String = { s"${HarnessConstants.ComponentName}/" } }
Example 169
Source File: CommandHelper.scala From wookiee with Apache License 2.0 | 5 votes |
package com.webtrends.harness.command import akka.actor.{Props, ActorRef, Actor} import akka.pattern.ask import akka.util.Timeout import com.webtrends.harness.app.Harness import scala.concurrent.duration._ import com.webtrends.harness.HarnessConstants import com.webtrends.harness.logging.ActorLoggingAdapter import scala.concurrent.{Promise, Future} import scala.util.{Failure, Success} trait CommandHelper extends ActorLoggingAdapter with BaseCommandHelper { this: Actor => override lazy implicit val actorSystem = context.system } def executeCommand[T:Manifest](name:String, bean:Option[CommandBean]=None, server:Option[String]=None, port:Int=2552)(implicit timeout:Timeout) : Future[BaseCommandResponse[T]] = { val p = Promise[BaseCommandResponse[T]] initCommandManager onComplete { case Success(_) => commandManager match { case Some(cm) => val msg = server match { case Some(srv) => ExecuteRemoteCommand(name, srv, port, bean, timeout) case None => ExecuteCommand(name, bean, timeout) } (cm ? msg)(timeout).mapTo[BaseCommandResponse[T]] onComplete { case Success(s) => p success s case Failure(f) => p failure CommandException("CommandManager", f) } case None => p failure CommandException("CommandManager", "CommandManager not found!") } case Failure(f) => p failure f } p.future } }
Example 170
Source File: InternalHttpSpec.scala From wookiee with Apache License 2.0 | 5 votes |
package com.webtrends.harness.http import java.net.{HttpURLConnection, URL} import java.util.concurrent.TimeUnit import akka.actor.{Props, ActorSystem} import akka.testkit.TestKit import akka.util.Timeout import com.webtrends.harness.TestKitSpecificationWithJUnit import com.webtrends.harness.service.messages.CheckHealth import scala.concurrent.Await import akka.pattern.ask import scala.concurrent.duration.FiniteDuration class InternalHttpSpec extends TestKitSpecificationWithJUnit(ActorSystem("test")) with InternalHttpClient { val port = 8123 val path = "http://127.0.0.1:" + port + "/" val httpActor = system.actorOf(Props(classOf[SimpleHttpServer], port)) // We need to make sure the httpActor has started up before trying to connect. implicit val timeout = Timeout(FiniteDuration(5, TimeUnit.SECONDS)) Await.result(httpActor ? CheckHealth, timeout.duration) "Test handlers" should { "handle the get path /ping" in { val url = new URL(path + "ping") val conn = url.openConnection().asInstanceOf[HttpURLConnection] val resp = getResponseContent(conn) resp.status mustEqual "200" resp.content.length must be > 0 resp.content.substring(0, 5) mustEqual "pong:" } } step { TestKit.shutdownActorSystem(system) } }
Example 171
Source File: ActorWaitSpec.scala From wookiee with Apache License 2.0 | 5 votes |
package com.webtrends.harness import java.util.concurrent.TimeUnit import akka.actor.{Actor, ActorSystem, PoisonPill, Props} import akka.pattern.ask import akka.testkit.TestKit import akka.util.Timeout import com.webtrends.harness.utils.ActorWaitHelper import org.specs2.mutable.SpecificationLike import scala.concurrent.Await import scala.concurrent.duration.Duration class WaitedOnActor extends Actor with ActorWaitHelper { def receive: Receive = { case "message" => sender ! "waitedResponse" } } class WaitActor extends Actor with ActorWaitHelper { implicit val timeout = Timeout(5000, TimeUnit.MILLISECONDS) val waited = awaitActor(Props[WaitedOnActor]) def receive: Receive = { case "message" => sender ! "response" case "waited" => sender ! Await.result((waited ? "message").mapTo[String], Duration(5, "seconds")) } } class ActorWaitSpec extends TestKit(ActorSystem("wait-spec")) with SpecificationLike { implicit val timeout = Timeout(5000, TimeUnit.MILLISECONDS) val waitActor = ActorWaitHelper.awaitActor(Props[WaitActor], system) sequential "ActorWaitSpec" should { "await the WaitActor successfully " in { Await.result((waitActor ? "message").mapTo[String], Duration(5, "seconds")) must beEqualTo("response") } "the WaitActor's awaited actor must have come up " in { Await.result((waitActor ? "waited").mapTo[String], Duration(5, "seconds")) must beEqualTo("waitedResponse") } } step { waitActor ! PoisonPill } }
Example 172
Source File: IngestorRegistryEndpoint.scala From hydra with Apache License 2.0 | 5 votes |
package hydra.ingest.http import akka.actor.ActorSystem import akka.http.scaladsl.server.Route import akka.pattern.ask import akka.util.Timeout import hydra.common.config.ConfigSupport import ConfigSupport._ import hydra.core.http.RouteSupport import hydra.ingest.bootstrap.HydraIngestorRegistryClient import hydra.ingest.services.IngestorRegistry.{FindAll, LookupResult} import scala.concurrent.duration.{FiniteDuration, _} class IngestorRegistryEndpoint()(implicit system:ActorSystem) extends RouteSupport with HydraIngestJsonSupport with ConfigSupport { private val registryLookupTimeout = applicationConfig .getDurationOpt("ingest.service-lookup.timeout") .getOrElse(5.seconds) lazy val registry = HydraIngestorRegistryClient(applicationConfig).registry private implicit val timeout = Timeout(registryLookupTimeout) override val route: Route = path("ingestors" ~ Slash.?) { get { onSuccess(registry ? FindAll) { case response: LookupResult => complete(response.ingestors) } } } }
Example 173
Source File: HydraIngestorRegistryClient.scala From hydra with Apache License 2.0 | 5 votes |
package hydra.ingest.bootstrap import akka.actor.{ActorSelection, ActorSystem} import akka.pattern.ask import akka.util.Timeout import com.typesafe.config.Config import hydra.common.util.ActorUtils import hydra.ingest.services.IngestorRegistry import hydra.ingest.services.IngestorRegistry.{FindByName, LookupResult} import scala.concurrent.Future class HydraIngestorRegistryClient(registryPath: String)( implicit val system: ActorSystem ) { lazy val registry: ActorSelection = system.actorSelection(registryPath) def lookupIngestor( name: String )(implicit timeout: Timeout): Future[LookupResult] = { (registry ? FindByName(name)).mapTo[LookupResult] } } object HydraIngestorRegistryClient { import hydra.common.config.ConfigSupport._ def registryPath(config: Config) = config .getStringOpt("ingest.ingestor-registry.path") .getOrElse( s"/user/service/${ActorUtils.actorName(classOf[IngestorRegistry])}" ) def apply( config: Config )(implicit system: ActorSystem): HydraIngestorRegistryClient = { new HydraIngestorRegistryClient(registryPath(config))(system) } }
Example 174
Source File: IngestorRegistrarSpec.scala From hydra with Apache License 2.0 | 5 votes |
package hydra.ingest.services import java.util.concurrent.TimeUnit import akka.actor.{ActorSystem, Props} import akka.pattern.ask import akka.testkit.{ImplicitSender, TestKit} import akka.util.Timeout import hydra.common.util.ActorUtils import hydra.ingest.services.IngestorRegistrar.UnregisterAll import hydra.ingest.services.IngestorRegistry.{ FindAll, FindByName, LookupResult } import hydra.ingest.test.TestIngestor import org.scalatest.concurrent.{Eventually, ScalaFutures} import org.scalatest.time.{Seconds, Span} import org.scalatest.matchers.should.Matchers import org.scalatest.funspec.AnyFunSpecLike import org.scalatest.BeforeAndAfterAll import scala.concurrent.duration._ class IngestorRegistrarSpec extends TestKit(ActorSystem("IngestorRegistrarSpec")) with Matchers with AnyFunSpecLike with ImplicitSender with ScalaFutures with BeforeAndAfterAll with Eventually { override def afterAll = TestKit.shutdownActorSystem(system, verifySystemShutdown = true) implicit override val patienceConfig = PatienceConfig(timeout = Span(10, Seconds), interval = Span(1, Seconds)) val registry = system.actorOf(Props[IngestorRegistry], "ingestor_registry") val act = system.actorOf(Props[IngestorRegistrar]) implicit val timeout = Timeout(3, TimeUnit.SECONDS) describe("The ingestor registrar actor") { it("registers from classpath on bootstrap") { eventually { whenReady( (registry ? FindByName(ActorUtils.actorName(classOf[TestIngestor]))) .mapTo[LookupResult] ) { i => i.ingestors.size shouldBe 1 i.ingestors(0).name shouldBe ActorUtils.actorName( classOf[TestIngestor] ) } } } it("unregisters") { act ! UnregisterAll eventually { whenReady((registry ? FindAll).mapTo[LookupResult]) { i => i.ingestors.size shouldBe 0 } } } } }
Example 175
Source File: RabbitTransport.scala From hydra with Apache License 2.0 | 5 votes |
package hydra.rabbit import akka.actor.Props import akka.pattern.ask import akka.util.Timeout import com.spingo.op_rabbit.Message.{Ack, ConfirmResponse, Fail, Nack} import com.spingo.op_rabbit._ import com.typesafe.config.Config import hydra.core.transport.Transport import hydra.core.transport.Transport.Deliver import scala.concurrent.duration._ class RabbitTransport(rabbitControlProps: Props) extends Transport { implicit val ec = context.dispatcher val rabbitControl = context.actorOf(rabbitControlProps) private def sendMessage(r: RabbitRecord) = { implicit val timeout = Timeout(3 seconds) val message = r.destinationType match { case RabbitRecord.DESTINATION_TYPE_EXCHANGE => val pub = Publisher.exchange(r.destination) Message(r.payload.getBytes(), pub) case RabbitRecord.DESTINATION_TYPE_QUEUE => val pub = Publisher.queue(r.destination) Message(r.payload.getBytes(), pub) } (rabbitControl ? message).mapTo[ConfirmResponse] } override def transport = { case Deliver(r: RabbitRecord, deliveryId, callback) => sendMessage(r).foreach { result => result match { case x: Ack => callback.onCompletion( deliveryId, Some( RabbitRecordMetadata( System.currentTimeMillis(), x.id, r.destination, r.destinationType, r.ackStrategy ) ), None ) case _: Nack => callback.onCompletion( deliveryId, None, Some( RabbitProducerException( "Rabbit returned Nack, record not produced" ) ) ) case x: Fail => callback.onCompletion(deliveryId, None, Some(x.exception)) } } } } object RabbitTransport { // will be used in testing def props(p: Props): Props = Props(classOf[RabbitTransport], p) // $COVERAGE-OFF$ def props(c: Config): Props = Props( classOf[RabbitTransport], Props( classOf[RabbitControl], Left(ConnectionParams.fromConfig(c.getConfig("op-rabbit.connection"))) ) ) // $COVERAGE-ON$ } case class RabbitProducerException(msg: String) extends Exception(msg)
Example 176
Source File: AvroRecordFactory.scala From hydra with Apache License 2.0 | 5 votes |
package hydra.kafka.producer import akka.actor.ActorRef import akka.pattern.ask import akka.util import com.pluralsight.hydra.avro.JsonConverter import hydra.avro.registry.ConfluentSchemaRegistry import hydra.avro.resource.SchemaResource import hydra.avro.util.AvroUtils import hydra.common.config.ConfigSupport import hydra.common.logging.LoggingAdapter import hydra.core.akka.SchemaRegistryActor.{FetchSchemaRequest, FetchSchemaResponse} import hydra.core.ingest.HydraRequest import hydra.core.transport.ValidationStrategy.Strict import org.apache.avro.generic.GenericRecord import scala.concurrent.duration._ import scala.concurrent.{ExecutionContext, Future} class AvroRecordFactory(schemaResourceLoader: ActorRef) extends KafkaRecordFactory[String, GenericRecord] with ConfigSupport with LoggingAdapter { private implicit val timeout = util.Timeout(3.seconds) override def build( request: HydraRequest )(implicit ec: ExecutionContext): Future[AvroRecord] = { for { (topic, subject) <- Future.fromTry(getTopicAndSchemaSubject(request)) schemaResource <- (schemaResourceLoader ? FetchSchemaRequest(subject)) .mapTo[FetchSchemaResponse] .map(_.schemaResource) record <- convert(schemaResource, request) } yield AvroRecord( topic, schemaResource.schema, getKey(request, record), record, request.ackStrategy ) } private def convert(schemaResource: SchemaResource, request: HydraRequest)( implicit ec: ExecutionContext ): Future[GenericRecord] = { val converter = new JsonConverter[GenericRecord]( schemaResource.schema, request.validationStrategy == Strict ) Future({ val converted = converter.convert(request.payload) converted }).recover { case ex => throw AvroUtils.improveException(ex, schemaResource, ConfluentSchemaRegistry.registryUrl(applicationConfig)) } } }
Example 177
Source File: BootstrapEndpoint.scala From hydra with Apache License 2.0 | 5 votes |
package hydra.kafka.endpoints import akka.actor._ import akka.http.scaladsl.model.StatusCodes import akka.http.scaladsl.server.Route import akka.pattern.ask import akka.util.Timeout import ch.megard.akka.http.cors.scaladsl.CorsDirectives._ import hydra.common.logging.LoggingAdapter import hydra.core.http.{CorsSupport, HydraDirectives, RouteSupport} import hydra.core.marshallers.TopicMetadataRequest import hydra.kafka.model.TopicMetadataAdapter import hydra.kafka.services.TopicBootstrapActor._ import scala.concurrent.duration._ import scala.util.{Failure, Success} class BootstrapEndpoint(override val system:ActorSystem) extends RouteSupport with LoggingAdapter with TopicMetadataAdapter with HydraDirectives with CorsSupport with BootstrapEndpointActors { private implicit val timeout = Timeout(10.seconds) override val route: Route = cors(settings) { pathPrefix("streams") { pathEndOrSingleSlash { post { requestEntityPresent { entity(as[TopicMetadataRequest]) { topicMetadataRequest => onComplete( bootstrapActor ? InitiateTopicBootstrap(topicMetadataRequest) ) { case Success(message) => message match { case BootstrapSuccess(metadata) => complete(StatusCodes.OK, toResource(metadata)) case BootstrapFailure(reasons) => complete(StatusCodes.BadRequest, reasons) case e: Exception => log.error("Unexpected error in TopicBootstrapActor", e) complete(StatusCodes.InternalServerError, e.getMessage) } case Failure(ex) => log.error("Unexpected error in BootstrapEndpoint", ex) complete(StatusCodes.InternalServerError, ex.getMessage) } } } } } ~ get { pathEndOrSingleSlash(getAllStreams(None)) ~ path(Segment)(subject => getAllStreams(Some(subject))) } } } private def getAllStreams(subject: Option[String]): Route = { onSuccess(bootstrapActor ? GetStreams(subject)) { case GetStreamsResponse(metadata) => complete(StatusCodes.OK, metadata.map(toResource)) case Failure(ex) => throw ex case x => log.error("Unexpected error in BootstrapEndpoint", x) complete(StatusCodes.InternalServerError, "Unknown error") } } }
Example 178
Source File: TopicsEndpoint.scala From hydra with Apache License 2.0 | 5 votes |
package hydra.kafka.endpoints import akka.actor.ActorSelection import akka.http.scaladsl.common.EntityStreamingSupport import akka.kafka.Subscriptions import akka.kafka.scaladsl.Consumer import akka.pattern.ask import akka.util.Timeout import hydra.core.http.RouteSupport import hydra.kafka.consumer.KafkaConsumerProxy.{GetLatestOffsets, LatestOffsetsResponse} import org.apache.kafka.clients.consumer.ConsumerRecord import org.apache.kafka.common.TopicPartition import scala.collection.immutable.Map import scala.concurrent.duration._ import scala.concurrent.{Await, ExecutionContext, Future} class TopicsEndpoint(consumerProxy:ActorSelection)(implicit ec:ExecutionContext) extends RouteSupport { import hydra.kafka.util.KafkaUtils._ implicit val jsonStreamingSupport = EntityStreamingSupport.json() override val route = path("transports" / "kafka" / "consumer" / "topics" / Segment) { topicName => get { extractRequestContext { ctx => parameters('format.?, 'group.?, 'n ? 10, 'start ? "earliest") { (format, groupId, n, startOffset) => val settings = loadConsumerSettings[Any, Any]( format.getOrElse("avro"), groupId.getOrElse("hydra"), startOffset ) val offsets = latestOffsets(topicName) val source = Consumer .plainSource(settings, Subscriptions.topics(topicName)) .initialTimeout(5.seconds) .zipWithIndex .takeWhile(rec => rec._2 <= n && !shouldCancel(offsets, rec._1) ) .map(rec => rec._1.value().toString) .watchTermination()((_, termination) => termination.failed.foreach { case cause => ctx.fail(cause) } ) complete(source) } } } } def shouldCancel( fpartitions: Future[Map[TopicPartition, Long]], record: ConsumerRecord[Any, Any] ): Boolean = { if (fpartitions.isCompleted) { val partitions = Await.result(fpartitions, 1.millis) val tp = new TopicPartition(record.topic(), record.partition()) partitions.get(tp) match { case Some(offset) => record.offset() >= offset case None => false } } else { false } } private def latestOffsets( topic: String ): Future[Map[TopicPartition, Long]] = { implicit val timeout = Timeout(5 seconds) (consumerProxy ? GetLatestOffsets(topic)) .mapTo[LatestOffsetsResponse] .map(_.offsets) } }
Example 179
Source File: KafkaIngestor.scala From hydra with Apache License 2.0 | 5 votes |
package hydra.kafka.ingestors import akka.pattern.ask import hydra.common.config.ConfigSupport._ import akka.util.Timeout import hydra.core.ingest.RequestParams._ import hydra.core.ingest.{HydraRequest, Ingestor, RequestParams} import hydra.core.protocol._ import hydra.kafka.config.KafkaConfigSupport import hydra.kafka.ingestors.KafkaTopicsActor.{ GetTopicRequest, GetTopicResponse } import hydra.kafka.producer.{KafkaProducerSupport, KafkaRecordFactories} import scala.concurrent.Future import scala.concurrent.duration._ class KafkaIngestor extends Ingestor with KafkaProducerSupport { override val recordFactory = new KafkaRecordFactories(schemaRegistryActor) private val timeoutDuration = applicationConfig .getDurationOpt("kafka-ingestor-timeout") .getOrElse(2.seconds) private implicit val timeout = Timeout(timeoutDuration) private val topicActor = context.actorOf( KafkaTopicsActor .props(KafkaConfigSupport.kafkaConfig.getConfig("kafka.admin")) ) ingest { case Publish(request) => val hasTopic = request.metadataValue(HYDRA_KAFKA_TOPIC_PARAM).isDefined sender ! (if (hasTopic) Join else Ignore) case Ingest(record, ackStrategy) => transport(record, ackStrategy) } }
Example 180
Source File: ProgressSource.scala From eventuate with Apache License 2.0 | 5 votes |
package com.rbmhtechnology.eventuate.adapter.stream import java.util.concurrent.TimeUnit import akka.NotUsed import akka.actor._ import akka.pattern.ask import akka.stream._ import akka.stream.scaladsl.Source import akka.util.Timeout import com.rbmhtechnology.eventuate.ReplicationProtocol._ import com.typesafe.config.Config import scala.concurrent.Future import scala.concurrent.duration._ private class ProgressSourceSettings(config: Config) { val readTimeout = config.getDuration("eventuate.log.read-timeout", TimeUnit.MILLISECONDS).millis } object ProgressSource { def apply(sourceLogId: String, targetLog: ActorRef)(implicit system: ActorSystem): Graph[SourceShape[Long], NotUsed] = { implicit val timeout = Timeout(new ProgressSourceSettings(system.settings.config).readTimeout) import system.dispatcher Source.fromFuture(targetLog.ask(GetReplicationProgress(sourceLogId)).flatMap { case GetReplicationProgressSuccess(_, progress, _) => Future.successful(progress) case GetReplicationProgressFailure(cause) => Future.failed(cause) }) } }
Example 181
Source File: OrderManager.scala From eventuate with Apache License 2.0 | 5 votes |
package com.rbmhtechnology.example.ordermgnt import akka.actor._ import akka.pattern.ask import akka.util.Timeout import com.rbmhtechnology.eventuate.EventsourcedView import com.rbmhtechnology.eventuate.VersionedAggregate.Resolve import scala.concurrent.Future import scala.concurrent.duration._ import scala.util._ class OrderManager(replicaId: String, val eventLog: ActorRef) extends EventsourcedView { import OrderActor._ import context.dispatcher private implicit val timeout = Timeout(10.seconds) private var orderActors: Map[String, ActorRef] = Map.empty override val id = s"s-om-$replicaId" override def onCommand = { case c: OrderCommand => orderActor(c.orderId) forward c case c: SaveSnapshot => orderActor(c.orderId) forward c case r: Resolve => orderActor(r.id) forward r case GetState if orderActors.isEmpty => sender() ! GetStateSuccess(Map.empty) case GetState => val sdr = sender() val statesF = orderActors.values.map(_.ask(GetState).mapTo[GetStateSuccess].map(_.state)) Future.sequence(statesF).map(_.reduce(_ ++ _)) onComplete { case Success(states) => sdr ! GetStateSuccess(states) case Failure(cause) => sdr ! GetStateFailure(cause) } } override def onEvent = { // eagerly create order actor so that their console output is immediately visible case OrderCreated(orderId, _) if !orderActors.contains(orderId) => orderActor(orderId) } private def orderActor(orderId: String): ActorRef = orderActors.get(orderId) match { case Some(orderActor) => orderActor case None => orderActors = orderActors + (orderId -> context.actorOf(Props(new OrderActor(orderId, replicaId, eventLog)))) orderActors(orderId) } }
Example 182
Source File: WriterApp.scala From eventuate with Apache License 2.0 | 5 votes |
package com.rbmhtechnology.example.querydb import akka.actor._ import akka.pattern.ask import akka.util.Timeout import com.datastax.driver.core._ import com.rbmhtechnology.eventuate.log.leveldb.LeveldbEventLog import com.typesafe.config.ConfigFactory import scala.concurrent.duration._ import scala.util._ object WriterApp extends App { // --------------------------------------------------------------- // Assumption: Cassandra 2.1 or higher running on localhost:9042 // --------------------------------------------------------------- withQueryDB(drop = false) { session => val system = ActorSystem("example-querydb", ConfigFactory.load(args(0))) val log = system.actorOf(LeveldbEventLog.props("example")) val emitter = system.actorOf(Props(new Emitter("emitter", log))) val writer = system.actorOf(Props(new Writer("writer", log, session))) import system.dispatcher implicit val timeout = Timeout(5.seconds) emitter ! CreateCustomer("Martin", "Krasser", "Somewhere 1") emitter ? CreateCustomer("Volker", "Stampa", "Somewhere 2") onComplete { case Success(CustomerCreated(cid, _, _, _)) => emitter ! UpdateAddress(cid, s"Somewhere ${Random.nextInt(10)}") case Failure(e) => e.printStackTrace() } Thread.sleep(3000) system.terminate() } def createQueryDB(drop: Boolean): Session = { val cluster = Cluster.builder().addContactPoint("localhost").build() val session = cluster.connect() if (drop) { session.execute("DROP KEYSPACE IF EXISTS QUERYDB") } session.execute("CREATE KEYSPACE IF NOT EXISTS QUERYDB WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 }") session.execute("USE QUERYDB") session.execute("CREATE TABLE IF NOT EXISTS CUSTOMER (id bigint, first text, last text, address text, PRIMARY KEY (id))") session.execute("CREATE TABLE IF NOT EXISTS PROGRESS (id bigint, sequence_nr bigint, PRIMARY KEY (id))") session.execute("INSERT INTO PROGRESS (id, sequence_nr) VALUES(0, 0) IF NOT EXISTS") session } def dropQueryDB(session: Session): Unit = { session.close() session.getCluster.close() } def withQueryDB[A](drop: Boolean = true)(f: Session => A): A = { val session = createQueryDB(drop) try f(session) finally dropQueryDB(session) } }
Example 183
Source File: EventLogWriter.scala From eventuate with Apache License 2.0 | 5 votes |
package com.rbmhtechnology.eventuate.log import akka.actor._ import com.rbmhtechnology.eventuate._ import scala.collection.immutable.Seq import scala.concurrent.Future import scala.concurrent.duration._ import scala.util._ private object EventLogWriter { class EventLogWriterActor(val id: String, val eventLog: ActorRef, override val aggregateId: Option[String]) extends EventsourcedActor { override def onCommand: Receive = { case event => persist(event) { case Success(r) => sender() ! lastHandledEvent case Failure(e) => sender() ! Status.Failure(e) } } override def onEvent: Receive = { case event => } } } def stop(): Unit = system.stop(actor) }
Example 184
Source File: RestarterActor.scala From eventuate with Apache License 2.0 | 5 votes |
package com.rbmhtechnology.eventuate.utilities import akka.actor._ import akka.pattern.ask import akka.util.Timeout class RestarterActor(props: Props, name: Option[String]) extends Actor { import RestarterActor._ var child: ActorRef = newActor var requester: ActorRef = _ override def receive = { case Restart => requester = sender() context.watch(child) context.stop(child) case Terminated(_) => child = newActor requester ! child case msg => child forward msg } private def newActor: ActorRef = name.map(context.actorOf(props, _)).getOrElse(context.actorOf(props)) } object RestarterActor { case object Restart implicit val timeout = Timeout(timeoutDuration) def restartActor(restarterRef: ActorRef): ActorRef = (restarterRef ? Restart).mapTo[ActorRef].await def props(props: Props, name: Option[String] = None) = Props(new RestarterActor(props, name)) }
Example 185
Source File: package.scala From eventuate with Apache License 2.0 | 5 votes |
package com.rbmhtechnology.eventuate import akka.pattern.ask import akka.testkit.TestProbe import akka.util.Timeout import com.rbmhtechnology.eventuate.EventsourcingProtocol._ import com.rbmhtechnology.eventuate.ReplicationFilter.NoFilter import com.rbmhtechnology.eventuate.ReplicationProtocol._ import scala.collection.immutable.Seq import scala.concurrent._ import scala.concurrent.duration._ package object utilities { val timeoutDuration = 20.seconds implicit class AwaitHelper[T](awaitable: Awaitable[T]) { def await: T = Await.result(awaitable, timeoutDuration) } def write(target: ReplicationTarget, events: Seq[String], aggregateId: Option[String] = None): Unit = { val system = target.endpoint.system val probe = TestProbe()(system) target.log ! Write(events.map(DurableEvent(_, target.logId, emitterAggregateId = aggregateId)), system.deadLetters, probe.ref, 0, 0) probe.expectMsgClass(classOf[WriteSuccess]) } def read(target: ReplicationTarget): Seq[String] = { import target.endpoint.system.dispatcher implicit val timeout = Timeout(3.seconds) def readEvents: Future[ReplicationReadSuccess] = target.log.ask(ReplicationRead(1L, Int.MaxValue, Int.MaxValue, NoFilter, DurableEvent.UndefinedLogId, target.endpoint.system.deadLetters, VectorTime())).mapTo[ReplicationReadSuccess] val reading = for { res <- readEvents } yield res.events.map(_.payload.asInstanceOf[String]) reading.await } def replicate(from: ReplicationTarget, to: ReplicationTarget, num: Int = Int.MaxValue): Int = { import to.endpoint.system.dispatcher implicit val timeout = Timeout(3.seconds) def readProgress: Future[GetReplicationProgressSuccess] = to.log.ask(GetReplicationProgress(from.logId)).mapTo[GetReplicationProgressSuccess] def readEvents(reply: GetReplicationProgressSuccess): Future[ReplicationReadSuccess] = from.log.ask(ReplicationRead(reply.storedReplicationProgress + 1, num, Int.MaxValue, NoFilter, to.logId, to.endpoint.system.deadLetters, reply.currentTargetVersionVector)).mapTo[ReplicationReadSuccess] def writeEvents(reply: ReplicationReadSuccess): Future[ReplicationWriteSuccess] = to.log.ask(ReplicationWrite(reply.events, Map(from.logId -> ReplicationMetadata(reply.replicationProgress, VectorTime.Zero)))).mapTo[ReplicationWriteSuccess] val replication = for { rps <- readProgress res <- readEvents(rps) wes <- writeEvents(res) } yield wes.events.size replication.await } }
Example 186
Source File: ActorStorageProvider.scala From eventuate with Apache License 2.0 | 5 votes |
package com.rbmhtechnology.eventuate.adapter.vertx import akka.actor.{ ActorSystem, Status } import akka.pattern.ask import akka.testkit.TestProbe import akka.util.Timeout import com.rbmhtechnology.eventuate.adapter.vertx.api.StorageProvider import scala.concurrent.duration._ import scala.concurrent.{ ExecutionContext, Future } class ActorStorageProvider(defaultId: String)(implicit system: ActorSystem) extends StorageProvider { implicit val timeout = Timeout(20.seconds) val probe = TestProbe() override def readProgress(id: String)(implicit executionContext: ExecutionContext): Future[Long] = probe.ref.ask(read(id)).mapTo[Long] override def writeProgress(id: String, sequenceNr: Long)(implicit executionContext: ExecutionContext): Future[Long] = probe.ref.ask(write(id, sequenceNr)).mapTo[Long] def expectRead(replySequenceNr: Long, id: String = defaultId): Unit = { probe.expectMsg(read(id)) probe.reply(replySequenceNr) } def expectWrite(sequenceNr: Long, id: String = defaultId): Unit = { probe.expectMsg(write(id, sequenceNr)) probe.reply(sequenceNr) } def expectWriteAndFail(sequenceNr: Long, failure: Throwable, id: String = defaultId): Unit = { probe.expectMsg(write(id, sequenceNr)) probe.reply(Status.Failure(failure)) } def expectWriteAnyOf(sequenceNrs: Seq[Long], id: String = defaultId): Unit = { probe.expectMsgAnyOf(sequenceNrs.map(write(id, _)): _*) probe.reply(sequenceNrs.max) } def expectNoMsg(duration: FiniteDuration): Unit = { probe.expectNoMsg(duration) } private def read(id: String): String = s"read[$id]" private def write(id: String, sequenceNr: Long): String = s"write[$id]-$sequenceNr" }
Example 187
Source File: ClusterAwareHostBalancer.scala From clickhouse-scala-client with GNU Lesser General Public License v3.0 | 5 votes |
package com.crobox.clickhouse.balancing import akka.actor.{ActorRef, ActorSystem} import akka.http.scaladsl.model.Uri import akka.pattern.ask import akka.stream.scaladsl.Sink import akka.stream.{ActorAttributes, Materializer, Supervision} import akka.util.Timeout import com.crobox.clickhouse.balancing.discovery.ConnectionManagerActor.{GetConnection, LogDeadConnections} import com.crobox.clickhouse.balancing.discovery.cluster.ClusterConnectionFlow import scala.concurrent.duration._ import scala.concurrent.{ExecutionContext, Future} case class ClusterAwareHostBalancer(host: Uri, cluster: String = "cluster", manager: ActorRef, scanningInterval: FiniteDuration)( implicit system: ActorSystem, connectionRetrievalTimeout: Timeout, ec: ExecutionContext, materializer: Materializer ) extends HostBalancer { ClusterConnectionFlow .clusterConnectionsFlow(Future.successful(host), scanningInterval, cluster) .withAttributes( ActorAttributes.supervisionStrategy({ case ex: IllegalArgumentException => logger.error("Failed resolving hosts for cluster, stopping the flow.", ex) Supervision.stop case ex => logger.error("Failed resolving hosts for cluster, resuming.", ex) Supervision.Resume }) ) .runWith(Sink.actorRef(manager, LogDeadConnections)) override def nextHost: Future[Uri] = (manager ? GetConnection()).mapTo[Uri] }
Example 188
Source File: ClickhouseClientAsyncSpec.scala From clickhouse-scala-client with GNU Lesser General Public License v3.0 | 5 votes |
package com.crobox.clickhouse import akka.actor.{ActorRef, ActorSystem} import akka.http.scaladsl.model.Uri import akka.pattern.ask import akka.stream.{ActorMaterializer, Materializer} import akka.testkit.TestKit import akka.util.Timeout import akka.util.Timeout.durationToTimeout import com.crobox.clickhouse.balancing.HostBalancer import com.crobox.clickhouse.balancing.discovery.ConnectionManagerActor.GetConnection import com.typesafe.config.{Config, ConfigFactory} import org.scalatest._ import scala.concurrent.{Await, Future} import scala.concurrent.duration._ import org.scalatest.flatspec.AsyncFlatSpecLike import org.scalatest.matchers.should.Matchers abstract class ClickhouseClientAsyncSpec(val config: Config = ConfigFactory.load()) extends TestKit(ActorSystem("clickhouseClientAsyncTestSystem", config.getConfig("crobox.clickhouse.client"))) with AsyncFlatSpecLike with Matchers with BeforeAndAfterAll with BeforeAndAfterEach { implicit val timeout: Timeout = 5.second implicit val materializer: Materializer = ActorMaterializer() override protected def afterAll(): Unit = { try super.afterAll() finally Await.result(system.terminate(), 10.seconds) } def requestParallelHosts(balancer: HostBalancer, connections: Int = 10): Future[Seq[Uri]] = Future.sequence( (1 to connections) .map(_ => { balancer.nextHost }) ) def getConnections(manager: ActorRef, connections: Int = 10): Future[Seq[Uri]] = Future.sequence( (1 to connections) .map(_ => { (manager ? GetConnection()).mapTo[Uri] }) ) // TODO change this methods to custom matchers def returnsConnectionsInRoundRobinFashion(manager: ActorRef, expectedConnections: Set[Uri]): Future[Assertion] = { val RequestConnectionsPerHost = 100 getConnections(manager, RequestConnectionsPerHost * expectedConnections.size) .map(connections => { expectedConnections.foreach( uri => connections .count(_ == uri) shouldBe (RequestConnectionsPerHost +- RequestConnectionsPerHost / 10) //10% delta for warm-up phase ) succeed }) } }
Example 189
Source File: DemoApp.scala From constructr-consul with Apache License 2.0 | 5 votes |
package com.tecsisa.constructr.coordination package demo import akka.actor.{ ActorRef, ActorSystem, Address } import akka.http.scaladsl.Http import akka.http.scaladsl.server.Directives import akka.pattern.ask import akka.stream.ActorMaterializer import akka.util.Timeout import com.typesafe.config.ConfigFactory import scala.concurrent.duration.{ Duration, MILLISECONDS } object DemoApp { val conf = ConfigFactory.load() val hostname = conf.getString("demo.hostname") val httpPort = conf.getInt("demo.port") def main(args: Array[String]): Unit = { // Create an Akka system implicit val system = ActorSystem("ConstructR-Consul") import system.dispatcher implicit val mat = ActorMaterializer() // Create an actor that handles cluster domain events val cluster = system.actorOf(SimpleClusterListener.props, SimpleClusterListener.Name) Http().bindAndHandle(route(cluster), hostname, httpPort) } private def route(cluster: ActorRef) = { import Directives._ implicit val timeout = Timeout( Duration( conf.getDuration("demo.cluster-view-timeout").toMillis, MILLISECONDS ) ) path("member-nodes") { // List cluster nodes get { onSuccess( (cluster ? SimpleClusterListener.GetMemberNodes).mapTo[Set[Address]] )(addresses => complete(addresses.mkString("\n"))) } } } }
Example 190
Source File: ReplicaRemoteWriter.scala From JustinDB with Apache License 2.0 | 5 votes |
package justin.db.replica.write import akka.pattern.ask import akka.util.Timeout import justin.db.Data import justin.db.actors.StorageNodeActorRef import justin.db.actors.protocol.{StorageNodeFailedWrite, StorageNodeWriteDataLocal, StorageNodeWriteResponse} import scala.concurrent.duration._ import scala.concurrent.{ExecutionContext, Future} class ReplicaRemoteWriter(implicit ec: ExecutionContext) { private implicit val timeout = Timeout(3.seconds) // TODO: tune this value def apply(storageNodeRefs: List[StorageNodeActorRef], data: Data): Future[List[StorageNodeWriteResponse]] = { Future.sequence(storageNodeRefs.map(putLocalValue(_, data))) } private def putLocalValue(node: StorageNodeActorRef, data: Data): Future[StorageNodeWriteResponse] = { (node.ref ? StorageNodeWriteDataLocal(data)) .mapTo[StorageNodeWriteResponse] .recover { case _ => StorageNodeFailedWrite(data.id) } } }
Example 191
Source File: ReplicaRemoteReader.scala From JustinDB with Apache License 2.0 | 5 votes |
package justin.db.replica.read import java.util.UUID import akka.pattern.ask import akka.util.Timeout import justin.db.actors.StorageNodeActorRef import justin.db.actors.protocol.{StorageNodeFailedRead, StorageNodeLocalRead, StorageNodeReadResponse} import scala.concurrent.duration._ import scala.concurrent.{ExecutionContext, Future} class ReplicaRemoteReader(implicit ec: ExecutionContext) { private implicit val timeout = Timeout(3.seconds) // TODO: tune this value def apply(storageNodeRefs: List[StorageNodeActorRef], id: UUID): Future[List[StorageNodeReadResponse]] = { Future.sequence(storageNodeRefs.map(getValue(_, id))) } private def getValue(node: StorageNodeActorRef, id: UUID): Future[StorageNodeReadResponse] = { (node.ref ? StorageNodeLocalRead(id)) .mapTo[StorageNodeReadResponse] .recover { case _ => StorageNodeFailedRead(id) } } }
Example 192
Source File: ActorRefStorageNodeClient.scala From JustinDB with Apache License 2.0 | 5 votes |
package justin.db.client import java.util.UUID import akka.pattern.ask import akka.util.Timeout import justin.db.Data import justin.db.actors.StorageNodeActorRef import justin.db.actors.protocol._ import justin.db.replica.{R, W} import scala.concurrent.duration._ import scala.concurrent.{ExecutionContext, Future} class ActorRefStorageNodeClient(storageNodeActor: StorageNodeActorRef)(implicit ex: ExecutionContext) extends StorageNodeClient { implicit val timeout = Timeout(5.seconds) // TODO: tune this value override def get(id: UUID, r: R): Future[GetValueResponse] = { (storageNodeActor.ref ? Internal.ReadReplica(r, id)).mapTo[StorageNodeReadResponse].map { case StorageNodeFoundRead(data) => GetValueResponse.Found(data) case StorageNodeConflictedRead(data) => GetValueResponse.Conflicts(data) case StorageNodeNotFoundRead(id) => GetValueResponse.NotFound(id) case StorageNodeFailedRead(_) => GetValueResponse.Failure(s"Couldn't read value with id ${id.toString}") } recover { case ex: Throwable => GetValueResponse.Failure(s"Unsuccessful read of value with id ${id.toString}") } } override def write(data: Data, w: W): Future[WriteValueResponse] = { (storageNodeActor.ref ? Internal.WriteReplica(w, data)).mapTo[StorageNodeWriteResponse].map { case StorageNodeSuccessfulWrite(id) => WriteValueResponse.Success(id) case StorageNodeConflictedWrite(_, _) => WriteValueResponse.Conflict case StorageNodeFailedWrite(id) => WriteValueResponse.Failure(s"Couldn't write value with id ${id.toString}") } recover { case ex: Throwable => WriteValueResponse.Failure(s"Unsuccessful write of value with id ${data.id.toString}") } } }
Example 193
Source File: ServiceBroker.scala From reactive-consul with MIT License | 5 votes |
package stormlantern.consul.client import java.net.URL import scala.concurrent.duration._ import scala.concurrent._ import akka.actor._ import akka.util.Timeout import akka.pattern.ask import stormlantern.consul.client.dao._ import stormlantern.consul.client.dao.akka.AkkaHttpConsulClient import stormlantern.consul.client.discovery._ import stormlantern.consul.client.election.LeaderInfo import stormlantern.consul.client.loadbalancers.LoadBalancerActor import stormlantern.consul.client.util._ class ServiceBroker(serviceBrokerActor: ActorRef, consulClient: ConsulHttpClient)(implicit ec: ExecutionContext) extends RetryPolicy with Logging { private[this] implicit val timeout = Timeout(10.seconds) def withService[A, B](name: String)(f: A ⇒ Future[B]): Future[B] = { logger.info(s"Trying to get connection for service $name") serviceBrokerActor.ask(ServiceBrokerActor.GetServiceConnection(name)).mapTo[ConnectionHolder].flatMap { connectionHolder ⇒ logger.info(s"Received connectionholder $connectionHolder") try { connectionHolder.connection.flatMap(c ⇒ f(c.asInstanceOf[A])) } finally { connectionHolder.loadBalancer ! LoadBalancerActor.ReturnConnection(connectionHolder) } } } def registerService(registration: ServiceRegistration): Future[Unit] = { consulClient.putService(registration).map { serviceId ⇒ // Add shutdown hook val deregisterService = new Runnable { override def run(): Unit = consulClient.deleteService(serviceId) } Runtime.getRuntime.addShutdownHook(new Thread(deregisterService)) } } def withLeader[A](key: String)(f: Option[LeaderInfo] ⇒ Future[A]): Future[A] = { ??? } def joinElection(key: String): Future[Unit] = { ??? } } object ServiceBroker { def apply(rootActor: ActorSystem, httpClient: ConsulHttpClient, services: Set[ConnectionStrategy]): ServiceBroker = { implicit val ec = ExecutionContext.Implicits.global val serviceAvailabilityActorFactory = (factory: ActorRefFactory, service: ServiceDefinition, listener: ActorRef) ⇒ factory.actorOf(ServiceAvailabilityActor.props(httpClient, service, listener)) val actorRef = rootActor.actorOf(ServiceBrokerActor.props(services, serviceAvailabilityActorFactory), "ServiceBroker") new ServiceBroker(actorRef, httpClient) } def apply(consulAddress: URL, services: Set[ConnectionStrategy]): ServiceBroker = { implicit val rootActor = ActorSystem("reactive-consul") val httpClient = new AkkaHttpConsulClient(consulAddress) ServiceBroker(rootActor, httpClient, services) } } case class ServiceUnavailableException(service: String) extends RuntimeException(s"$service service unavailable")
Example 194
Source File: ConsumerCommands.scala From reactive-kafka-microservice-template with Apache License 2.0 | 5 votes |
package com.omearac.http.routes import akka.actor.ActorRef import akka.event.LoggingAdapter import akka.http.scaladsl.model.StatusCodes import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server._ import akka.pattern.ask import akka.util.Timeout import com.omearac.consumers.DataConsumer.{ConsumerActorReply, ManuallyInitializeStream, ManuallyTerminateStream} import scala.concurrent.duration._ trait ConsumerCommands { def dataConsumer: ActorRef def eventConsumer: ActorRef def log: LoggingAdapter val dataConsumerHttpCommands: Route = pathPrefix("data_consumer") { implicit val timeout = Timeout(10 seconds) path("stop") { get { onSuccess(dataConsumer ? ManuallyTerminateStream) { case m: ConsumerActorReply => log.info(m.message); complete(StatusCodes.OK, m.message); case _ => complete(StatusCodes.InternalServerError) } } } ~ path("start") { get { onSuccess(dataConsumer ? ManuallyInitializeStream) { case m: ConsumerActorReply => log.info(m.message); complete(StatusCodes.OK, m.message) case _ => complete(StatusCodes.InternalServerError) } } } } val eventConsumerHttpCommands: Route = pathPrefix("event_consumer") { implicit val timeout = Timeout(10 seconds) path("stop") { get { onSuccess(eventConsumer ? ManuallyTerminateStream) { case m: ConsumerActorReply => log.info(m.message); complete(StatusCodes.OK, m.message); case _ => complete(StatusCodes.InternalServerError) } } } ~ path("start") { get { onSuccess(eventConsumer ? ManuallyInitializeStream) { case m: ConsumerActorReply => log.info(m.message); complete(StatusCodes.OK, m.message) case _ => complete(StatusCodes.InternalServerError) } } } } }
Example 195
Source File: ProducerCommands.scala From reactive-kafka-microservice-template with Apache License 2.0 | 5 votes |
package com.omearac.http.routes import akka.actor.ActorRef import akka.event.LoggingAdapter import akka.http.scaladsl.model.StatusCodes import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Route import akka.pattern.ask import akka.util.Timeout import com.omearac.producers.DataProducer.PublishMessages import com.omearac.shared.EventMessages.MessagesPublished import scala.concurrent.duration._ trait ProducerCommands { def log: LoggingAdapter def dataProducer: ActorRef val producerHttpCommands: Route = pathPrefix("data_producer"){ implicit val timeout = Timeout(10 seconds) path("produce" / IntNumber) { {numOfMessagesToProduce => get { onSuccess(dataProducer ? PublishMessages(numOfMessagesToProduce)) { case MessagesPublished(numberOfMessages) => complete(StatusCodes.OK, numberOfMessages + " messages Produced as Ordered, Boss!") case _ => complete(StatusCodes.InternalServerError) } } } } } }
Example 196
Source File: SampleFramework.scala From mesos-actor with Apache License 2.0 | 5 votes |
package com.adobe.api.platform.runtime.mesos.sample import akka.actor.ActorSystem import akka.pattern.ask import akka.stream.ActorMaterializer import akka.util.Timeout import com.adobe.api.platform.runtime.mesos._ import java.time.Instant import java.util.UUID import scala.concurrent.Await import scala.concurrent.Future import scala.concurrent.duration._ object SampleFramework { def main(args: Array[String]): Unit = { implicit val system = ActorSystem("sample-framework-system") implicit val mat = ActorMaterializer() implicit val log = system.log implicit val ec = system.dispatcher val taskLaunchTimeout = Timeout(15 seconds) val taskDeleteTimeout = Timeout(10 seconds) val subscribeTimeout = Timeout(5 seconds) val teardownTimeout = Timeout(5 seconds) val mesosClientActor = system.actorOf( MesosClient.props( () => "sample-" + UUID.randomUUID(), "sample-framework", "http://192.168.99.100:5050", "*", 30.seconds, taskStore = new LocalTaskStore)) mesosClientActor .ask(Subscribe)(subscribeTimeout) .mapTo[SubscribeComplete] .onComplete(complete => { log.info("subscribe completed successfully...") }) var taskCount = 0 def nextName() = { taskCount += 1 s"sample-task-${Instant.now.getEpochSecond}-${taskCount}" } def nextId() = "sample-task-" + UUID.randomUUID() (1 to 3).foreach(_ => { val task = TaskDef( nextId(), nextName(), "trinitronx/python-simplehttpserver", 0.1, 24, List(8080, 8081), Some(HealthCheckConfig(0)), commandDef = Some(CommandDef())) val launched: Future[TaskState] = mesosClientActor.ask(SubmitTask(task))(taskLaunchTimeout).mapTo[TaskState] launched map { case taskDetails: Running => { val taskHost = taskDetails.hostname val taskPorts = taskDetails.hostports log.info( s"launched task id ${taskDetails.taskId} with state ${taskDetails.taskStatus.getState} on agent ${taskHost} listening on ports ${taskPorts}") //schedule delete in 10 seconds system.scheduler.scheduleOnce(10.seconds) { log.info(s"removing previously created task ${taskDetails.taskId}") mesosClientActor .ask(DeleteTask(taskDetails.taskId))(taskDeleteTimeout) .mapTo[Deleted] .map(deleted => { log.info(s"task killed ended with state ${deleted.taskStatus.getState}") }) } } case s => log.error(s"failed to launch task; state is ${s}") } recover { case t => log.error(s"task launch failed ${t.getMessage}", t) } }) system.scheduler.scheduleOnce(30.seconds) { val complete: Future[Any] = mesosClientActor.ask(Teardown)(teardownTimeout) Await.result(complete, 10.seconds) println("teardown completed!") system.terminate().map(_ => System.exit(0)) } } }
Example 197
Source File: ActiveMqTestSpec.scala From reactive-activemq with Apache License 2.0 | 5 votes |
package akka.stream.integration package activemq import akka.NotUsed import akka.actor.ActorRef import akka.stream.integration.PersonDomain.Person import akka.stream.scaladsl.{ Flow, Keep } import akka.stream.testkit.scaladsl.{ TestSink, TestSource } import akka.stream.testkit.{ TestPublisher, TestSubscriber } import akka.testkit.TestActor.AutoPilot import akka.testkit.TestProbe import JsonCamelMessageExtractor._ import JsonCamelMessageBuilder._ import scala.util.{ Failure, Success, Try } implicit def function1ToAutoPilot[S, T](f: S => T): AutoPilot = new AutoPilot { override def run(sender: ActorRef, msg: Any): AutoPilot = msg match { case s: S => val tryT: Try[T] = Try(f(s)) tryT match { case Success(t) => sender ! t function1ToAutoPilot(f) case Failure(f) => fail(s"Failed to apply supplied function to received message: $s", f) } case _ => fail(s"Received message is not of the required type: $msg") } } }
Example 198
Source File: TFuturesTest.scala From Scala-for-Machine-Learning-Second-Edition with MIT License | 5 votes |
package org.scalaml.scalability.akka import akka.actor.{ActorSystem, Props} import akka.pattern.ask import akka.util.Timeout import org.scalaml.Logging import org.scalaml.Predef.DblVec import org.scalaml.filtering.dft.DFT import org.scalaml.scalability.akka.message._ import org.scalaml.util.FormatUtils._ import org.scalatest.{FlatSpec, Matchers} protected[this] val name: String = "Scala futures" private val NUM_WORKERS = 8 private val NUM_DATA_POINTS = 1000000 private val h = (x: Double) => 2.0 * Math.cos(Math.PI * 0.005 * x) + // simulated first harmonic Math.cos(Math.PI * 0.05 * x) + // simulated second harmonic 0.5 * Math.cos(Math.PI * 0.2 * x) + // simulated third harmonic 0.2 * Random.nextDouble private val TimeOut = 5000L private val duration = Duration(TimeOut, "millis") implicit val timeout = new Timeout(duration) it should s"$name Data transformation futures using Akka actors" in { show("$name Data transformation futures using Akka actors") val actorSystem = ActorSystem("System") val xt = Vector.tabulate(NUM_DATA_POINTS)(h(_)) val master = actorSystem.actorOf( Props(new DFTFutures(xt, NUM_WORKERS)), "DFTTransform" ) val future = master ? Start() Thread.sleep(TimeOut) actorSystem.shutdown() } } // ----------------------------------------------- EOF ---------------------------
Example 199
Source File: Routes.scala From Learn-Scala-Programming with MIT License | 5 votes |
package ch14 import akka.actor.{ActorRef, ActorSystem} import akka.http.scaladsl.model.{HttpResponse, StatusCodes} import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Route import akka.http.scaladsl.server.directives.MethodDirectives.{delete, get, post} import akka.http.scaladsl.server.directives.PathDirectives.path import akka.http.scaladsl.server.directives.RouteDirectives.complete import akka.pattern.ask import akka.util.Timeout import ch14.Commands._ import ch14.Events.{ ArticleCreated, ArticleDeleted, ArticlesPurchased, ArticlesRestocked } import scala.concurrent.{ExecutionContext, Future} trait Routes extends JsonSupport { implicit def system: ActorSystem def inventory: ActorRef def config: Config implicit lazy val timeout: Timeout = config.timeout implicit lazy val ec: ExecutionContext = system.dispatcher lazy val articlesRoutes: Route = pathPrefix("articles") { concat( path(Segment) { name => concat( post { val changedInventory: Future[Option[ArticleCreated]] = (inventory ? CreateArticle(name, 0)) .mapTo[Option[ArticleCreated]] onSuccess(changedInventory) { case None => complete(StatusCodes.Conflict) case Some(event) => complete(StatusCodes.Created, event) } }, delete { val changedInventory: Future[Option[ArticleDeleted]] = (inventory ? DeleteArticle(name)).mapTo[Option[ArticleDeleted]] rejectEmptyResponse { complete(changedInventory) } }, get { complete((inventory ? GetArticle(name)).mapTo[Inventory]) } ) } ) } lazy val inventoryRoutes: Route = path("inventory") { get { complete((inventory ? GetInventory).mapTo[Inventory]) } } ~ path("purchase") { post { entity(as[PurchaseArticles]) { order => val response: Future[Option[ArticlesPurchased]] = (inventory ? order).mapTo[Option[ArticlesPurchased]] onSuccess(response) { case None => complete(StatusCodes.Conflict) case Some(event) => complete(event) } } } } ~ path("restock") { post { entity(as[RestockArticles]) { stock => val response: Future[Option[ArticlesRestocked]] = (inventory ? stock).mapTo[Option[ArticlesRestocked]] complete(response) } } } lazy val routes: Route = articlesRoutes ~ inventoryRoutes }
Example 200
Source File: LowLevelServer.scala From akka-http-test with Apache License 2.0 | 5 votes |
package com.github.dnvriend.component.lowlevelserver import akka.NotUsed import akka.actor.{ ActorSystem, Props } import akka.event.{ Logging, LoggingAdapter } import akka.http.scaladsl.Http import akka.http.scaladsl.model._ import akka.pattern.ask import akka.stream.scaladsl.{ Flow, Sink, Source } import akka.stream.{ ActorMaterializer, Materializer } import akka.util.Timeout import com.github.dnvriend.component.lowlevelserver.dto.{ Person, PersonWithId } import com.github.dnvriend.component.lowlevelserver.marshaller.Marshaller import com.github.dnvriend.component.lowlevelserver.repository.PersonRepository import spray.json.{ DefaultJsonProtocol, _ } import scala.concurrent.duration._ import scala.concurrent.{ ExecutionContext, Future } class LowLevelServer(implicit val system: ActorSystem, mat: Materializer, ec: ExecutionContext, log: LoggingAdapter, timeout: Timeout) extends DefaultJsonProtocol with Marshaller { val personDb = system.actorOf(Props[PersonRepository]) def debug(t: Any)(implicit log: LoggingAdapter = null): Unit = if (Option(log).isEmpty) println(t) else log.debug(t.toString) def http200Okay(req: HttpRequest): HttpResponse = HttpResponse(StatusCodes.OK) def http200AsyncOkay(req: HttpRequest): Future[HttpResponse] = Future(http200Okay(req)) val http200OkayFlow: Flow[HttpRequest, HttpResponse, NotUsed] = Flow[HttpRequest].map { req => HttpResponse(StatusCodes.OK) } val serverSource: Source[Http.IncomingConnection, Future[Http.ServerBinding]] = Http().bind(interface = "localhost", port = 8080) val binding: Future[Http.ServerBinding] = serverSource.to(Sink.foreach { conn => // conn.handleWith(http200OkayFlow) // conn.handleWithSyncHandler(http200Okay) // conn.handleWithAsyncHandler(http200AsyncOkay, 8) conn.handleWithAsyncHandler(personRequestHandler) }).run() def personRequestHandler(req: HttpRequest): Future[HttpResponse] = req match { case HttpRequest(HttpMethods.GET, Uri.Path("/api/person"), _, _, _) => for { xs <- (personDb ? "findAll").mapTo[List[PersonWithId]] entity = HttpEntity(ContentTypes.`application/json`, xs.toJson.compactPrint) } yield HttpResponse(StatusCodes.OK, entity = entity) case HttpRequest(HttpMethods.POST, Uri.Path("/api/person"), _, ent, _) => for { strictEntity <- ent.toStrict(1.second) person <- (personDb ? strictEntity.data.utf8String.parseJson.convertTo[Person]).mapTo[PersonWithId] } yield HttpResponse(StatusCodes.OK, entity = person.toJson.compactPrint) case req => req.discardEntityBytes() Future.successful(HttpResponse(StatusCodes.NotFound)) } } object LowLevelServerLauncher extends App with DefaultJsonProtocol { // setting up some machinery implicit val system: ActorSystem = ActorSystem() implicit val mat: Materializer = ActorMaterializer() implicit val ec: ExecutionContext = system.dispatcher implicit val log: LoggingAdapter = Logging(system, this.getClass) implicit val timeout: Timeout = Timeout(10.seconds) new LowLevelServer() }