akka.actor.Actor.Receive Scala Examples

The following examples show how to use akka.actor.Actor.Receive. 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: TestAkka.scala    From DataXServer   with Apache License 2.0 6 votes vote down vote up
package org.tianlangstudio.data.hamal.akka

import akka.actor.{Props, ActorSystem, Actor}
import akka.actor.Actor.Receive

/**
 * Created by zhuhq on 2016/5/5.
 */
object TestAkka extends App{
  val system = ActorSystem("test")
  val actor = system.actorOf(Props(classOf[TestAkka]))
  for(i <- 0 to 10) {
    actor ! Remove()
    actor ! Add()
  }
}
class TestAkka extends Actor{
  override def receive: Receive = {
    case Remove() =>
      println("remove begin")
      Thread.sleep((1000 * math.ceil(math.random) * 10).toLong)
      println("remove end")
    case Add() =>
      println("add begin")
      Thread.sleep((1000 * math.ceil(math.random) * 10).toLong)
      println("add end")

  }
}
case class Remove()
case class Add() 
Example 2
Source File: TaskExecutorActor.scala    From CM-Well   with Apache License 2.0 5 votes vote down vote up
package cmwell.ctrl.tasks

import akka.actor.{Actor, ActorRef}
import akka.actor.Actor.Receive
import akka.util.Timeout
import com.typesafe.scalalogging.LazyLogging
import scala.concurrent.duration._
import scala.util.{Failure, Success}
import scala.concurrent.ExecutionContext.Implicits.global


trait TaskStatus
case object InProgress extends TaskStatus
case object Complete extends TaskStatus
case object Failed extends TaskStatus

object TaskExecutorActor {
  val name = "TaskExecutorActor"
}

class TaskExecutorActor extends Actor with LazyLogging {
  implicit val timeout = Timeout(15.seconds)
  private var status: TaskStatus = _
  private var s: ActorRef = _
  private case class TaskFinished(status: TaskStatus)
  @throws[Exception](classOf[Exception])
  override def preStart(): Unit = {}

  override def receive: Receive = {
    case t: Task => {
      logger.info(s"Starting task: $t")
      s = sender()
      status = InProgress
      t.exec.onComplete {
        case Success(tr) => {
          tr match {
            case TaskSuccessful =>
              self ! TaskFinished(Complete)
              s ! TaskSuccessful
            case TaskFailed =>
              self ! TaskFinished(Failed)
              s ! TaskFailed
          }
        }
        case Failure(err) =>
          self ! TaskFinished(Failed)
          s ! TaskFailed
      }
    }
    case TaskFinished(stat) => {
      status = stat
    }
  }
} 
Example 3
Source File: ActorSubscriberTest.scala    From intro-to-akka-streams   with Apache License 2.0 5 votes vote down vote up
package com.github.dnvriend.streams.sink

import akka.Done
import akka.actor.Actor.Receive
import akka.actor.{ ActorRef, Props }
import akka.event.LoggingReceive
import akka.stream.actor.ActorSubscriberMessage.{ OnComplete, OnError, OnNext }
import akka.stream.actor.{ ActorSubscriber, OneByOneRequestStrategy, RequestStrategy }
import akka.stream.scaladsl.{ Sink, Source }
import akka.stream.testkit.TestPublisher
import akka.stream.testkit.scaladsl.TestSource
import akka.testkit.TestProbe
import com.github.dnvriend.streams.TestSpec
import com.github.dnvriend.streams.sink.ActorSubscriberTest.TestActorSubscriber

import scala.concurrent.Future
import scala.reflect.ClassTag

object ActorSubscriberTest {
  final val OnNextMessage = "onNext"
  final val OnCompleteMessage = "onComplete"
  final val OnErrorMessage = "onError"

  class TestActorSubscriber(ref: ActorRef) extends ActorSubscriber {
    override protected val requestStrategy: RequestStrategy = OneByOneRequestStrategy
    override def receive: Receive = LoggingReceive {
      case OnNext(msg)    ⇒ ref ! OnNextMessage
      case OnComplete     ⇒ ref ! OnCompleteMessage
      case OnError(cause) ⇒ ref ! OnErrorMessage
    }
  }
}

//class ActorSubscriberTest extends TestSpec {
//  def withForwarder(xs: Int*)(f: TestProbe ⇒ Unit): Unit = {
//    val tp = TestProbe()
//    val ref = new TestActorSubscriber(tp.ref)
//    Source(xs.toList).to(Sink.actorSubscriber(Props())).mapMaterializedValue(_ ⇒ Future.successful[Done]).run()
//    try f(tp) finally killActors(ref)
//  }
//
//} 
Example 4
Source File: DummyService.scala    From CM-Well   with Apache License 2.0 5 votes vote down vote up
package k.grid.testgrid

import akka.actor.Actor
import akka.actor.Actor.Receive
import com.typesafe.scalalogging.LazyLogging
import k.grid.Grid
import k.grid.dmap.api.MapData
import k.grid.dmap.impl.persistent.PersistentDMap
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global


case class DummyMessage(str: String)
case class WriteToPersistentDMap(md: MapData, delay: FiniteDuration = 1.seconds)

class DummyService extends Actor with LazyLogging {

  @throws[Exception](classOf[Exception])
  override def preStart(): Unit = {
    logger.info(" *** Starting DummyService")
  }

  override def receive: Receive = {
    case msg @ DummyMessage(str) => {
      logger.info(s" *** DummyService Received $msg")
      sender ! str
    }

    case msg @ WriteToPersistentDMap(md, delay) => {
      logger.info(s" *** DummyService Received $msg")
      Grid.system.scheduler.scheduleOnce(delay) {
        md.m.foreach { tuple =>
          PersistentDMap.set(tuple._1, tuple._2)
        }
      }
    }
  }
} 
Example 5
Source File: LocalRegistrationManager.scala    From CM-Well   with Apache License 2.0 5 votes vote down vote up
package k.grid.registration

import scala.concurrent.duration._
import akka.actor.Actor
import akka.actor.Actor.Receive
import com.typesafe.scalalogging.LazyLogging
import k.grid._
import k.grid.registration.messages.{GridTopology, RegistrationPing}
import scala.concurrent.ExecutionContext.Implicits.global


object LocalRegistrationManager {
  val name = "LocalGossipManager"
  // todo: change to Map[(Host, IdentityName),GridJvm]
  private[LocalRegistrationManager] var _jvms = Set.empty[GridJvm]
  private[LocalRegistrationManager] var _regFails = 0

  def registrationFailure = _regFails == Config.possibleRegFails

  def jvms = _jvms
}
class LocalRegistrationManager extends Actor with LazyLogging {
  private case object SendGossipPing
  private case object IncreaseRegFails

  val isController = Grid.isController
  def registrationCoordinator = Grid.selectSingleton(RegistrationCoordinator.name, None, Grid.clusterProxy)
  @throws[Exception](classOf[Exception])
  override def preStart(): Unit = {
    context.system.scheduler.schedule(5.seconds, 5.seconds, self, SendGossipPing)
    context.system.scheduler.schedule(30.seconds, 30.seconds, self, IncreaseRegFails)

  }

  override def receive: Receive = {
    case SendGossipPing => registrationCoordinator ! RegistrationPing(Grid.thisMember)
    case GridTopology(jvms) =>
      LocalRegistrationManager._regFails = 0

      val jvmsJoined = jvms -- LocalRegistrationManager._jvms
      val jvmsLeft = LocalRegistrationManager._jvms -- jvms

      LocalRegistrationManager._jvms = jvms

      // send the data to the client actor so it can forward it to its subscribers.
      Grid.selectActor(ClientActor.name, Grid.thisMember) ! JvmMembershipReport(jvmsJoined, jvmsLeft)

      logger.debug(s"Current jvms: $jvms")
    case IncreaseRegFails =>
      LocalRegistrationManager._regFails = Math.min(LocalRegistrationManager._regFails + 1, Config.possibleRegFails)
  }
} 
Example 6
Source File: RegistrationCoordinator.scala    From CM-Well   with Apache License 2.0 5 votes vote down vote up
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 7
Source File: TaskActor.scala    From CM-Well   with Apache License 2.0 5 votes vote down vote up
package k.grid

import akka.actor.Actor.Receive
import akka.actor.{Actor, ActorLogging, Cancellable}
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.language.postfixOps


case object PauseTask
case object ResumeTask
class TaskActor(start: Long, interval: Long, block: () => Unit) extends Actor with ActorLogging {

  var s: Cancellable = _

  def startTask: Cancellable = {
    Grid.system.scheduler.schedule(start milli, interval milli) {
      block()
    }
  }

  s = startTask

  override def receive: Receive = {
    case PauseTask  => if (!s.isCancelled) s.cancel()
    case ResumeTask => if (s.isCancelled) s = startTask
  }
} 
Example 8
Source File: GridMonitor.scala    From CM-Well   with Apache License 2.0 5 votes vote down vote up
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 9
Source File: CongestionAnalyzer.scala    From CM-Well   with Apache License 2.0 5 votes vote down vote up
package trafficshaping

import akka.actor.Actor
import akka.actor.Actor.Receive
import cmwell.ws.Settings
import cmwell.ws.Settings._
import com.typesafe.scalalogging.LazyLogging
import k.grid.dmap.api.SettingsLong
import k.grid.dmap.impl.persistent.PersistentDMap

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._


case object GetTrafficData
case class TrafficData(requestors: Map[String, RequestorCounter])

object CongestionAnalyzer {
  val name = "CongestionAnalyzer"
  var penalizeTopUsers = 3
}

class CongestionAnalyzer extends Actor with LazyLogging {
  import Settings._
  import DMapKeys._

  val numOfCpus = Runtime.getRuntime.availableProcessors()
  def getThresholdFactor: Long = {
    PersistentDMap
      .get(THRESHOLD_FACTOR)
      .map {
        case SettingsLong(l) => l
        case _               => 0L
      }
      .getOrElse(0L)
  }

  case object AnalyzeCongestion

  @throws[Exception](classOf[Exception])
  override def preStart(): Unit = {
    context.system.scheduler.schedule(0.seconds, checkFrequency.seconds, self, AnalyzeCongestion)
  }

  override def receive: Receive = {
    case AnalyzeCongestion =>
      val thresholdFactor = getThresholdFactor
      val threshold = checkFrequency.seconds.toMillis * thresholdFactor

      TrafficShaper.lastRequests.toVector
        .sortBy(_._2.requestsTime)
        .takeRight(CongestionAnalyzer.penalizeTopUsers)
        .foreach {
          case (k, v) =>
            if (v.requestsTime > threshold) {
              v.penalty = v.penalty.next
              logger.info(s"The user $k is getting ${v.penalty}.")
            } else v.penalty = v.penalty.prev
            v.reset
        }

      TrafficShaper.lastRequests = TrafficShaper.lastRequests.filter {
        case (k, v) => v.penalty != NoPenalty || v.requestsTime > 0L
      }

    case GetTrafficData =>
      sender ! TrafficData(TrafficShaper.getRequestors)
  }
} 
Example 10
Source File: StatsSender.scala    From CM-Well   with Apache License 2.0 5 votes vote down vote up
package cmwell.stats

import java.net.{DatagramPacket, DatagramSocket, InetAddress}
import java.util.Calendar
import java.text.SimpleDateFormat
import akka.actor.{Actor, ActorSystem, Props}
import akka.actor.Actor.Receive


case class Message(msg: String, host: String, port: Int)

class SenderActor extends Actor {
  private val dsocket = new DatagramSocket()

  sys.addShutdownHook {
    dsocket.close()
  }

  override def receive: Receive = {
    case Message(msg, host, port) =>
      val address = InetAddress.getByName(host)
      val packet = new DatagramPacket(msg.getBytes(), msg.length, address, port)
      dsocket.send(packet)
  }
}

class StatsSender(path: String, host: String = "localhost", port: Int = 8125) {

  object Sender {
    val system = ActorSystem("mySystem")
    val actor = system.actorOf(Props[SenderActor], "SenderActor")
    def send(message: String) {
      actor ! Message(message, host, port)
    }

  }

  private def getCurrentTimeStr: String = {
    val now = Calendar.getInstance().getTime()
    val dateFormat = new SimpleDateFormat("ddMMyyyy_hhmm")
    dateFormat.format(now)
  }

  private def getMachineName: String = {
    java.net.InetAddress.getLocalHost().getHostName().split('.')(0)
  }

  private def getName(p: String, action: String): String = {
    p.replace("{MachineName}", getMachineName).replace("{DateTime}", getCurrentTimeStr) + "." + action
      .replace(".", "-")
      .replace(" ", "_")
  }

  def sendCounts(action: String, num: Int) {
    val message = getName(path, action) + ":" + num + "|c"
    Sender.send(message)
  }

  def sendTimings(action: String, num: Int) {
    val message = getName(path, action) + ":" + num + "|ms"
    Sender.send(message)
  }

  def sendGauges(action: String, num: Int) {
    val message = getName(path, action) + ":" + num + "|g"
    Sender.send(message)
  }

  def sendSets(action: String) {
    val message = getName(path, action) + "|s"
    Sender.send(message)
  }
} 
Example 11
Source File: LogUnhandled.scala    From graphcool-framework   with Apache License 2.0 5 votes vote down vote up
package cool.graph.akkautil

import akka.actor.Actor.Receive

trait LogUnhandled { self =>
  private val className = self.getClass.getSimpleName

  def logUnhandled(receive: Receive): Receive = receive orElse {
    case x =>
      println(Console.RED + s"[$className] Received unknown message: $x" + Console.RESET)
  }

  def logAll(receive: Receive): Receive = {
    case x =>
      if (receive.isDefinedAt(x)) {
        receive(x)
        println(Console.GREEN + s"[$className] Handled message: $x" + Console.RESET)
      } else {
        println(Console.RED + s"[$className] Unhandled message: $x" + Console.RESET)
      }
  }
} 
Example 12
Source File: PaymentHandlerSpec.scala    From eclair   with Apache License 2.0 5 votes vote down vote up
package fr.acinq.eclair.payment

import akka.actor.Actor.Receive
import akka.actor.{ActorContext, ActorSystem}
import akka.event.DiagnosticLoggingAdapter
import akka.testkit.{TestKit, TestProbe}
import fr.acinq.eclair.TestConstants.Alice
import fr.acinq.eclair.TestKitBaseClass
import fr.acinq.eclair.payment.receive.{PaymentHandler, ReceiveHandler}
import org.scalatest.funsuite.AnyFunSuiteLike

import scala.concurrent.duration._

class PaymentHandlerSpec extends TestKitBaseClass with AnyFunSuiteLike {

  test("compose payment handlers") {
    val handler = system.actorOf(PaymentHandler.props(Alice.nodeParams, TestProbe().ref))

    val intHandler = new ReceiveHandler {
      override def handle(implicit ctx: ActorContext, log: DiagnosticLoggingAdapter): Receive = {
        case i: Int => ctx.sender ! -i
      }
    }

    val stringHandler = new ReceiveHandler {
      override def handle(implicit ctx: ActorContext, log: DiagnosticLoggingAdapter): Receive = {
        case s: String => ctx.sender ! s.reverse
      }
    }

    val probe = TestProbe()

    probe.send(handler, 42)
    probe.expectNoMsg(300 millis)

    probe.send(handler, intHandler)

    probe.send(handler, 42)
    probe.expectMsg(-42)

    probe.send(handler, "abcdef")
    probe.expectNoMsg(300 millis)

    probe.send(handler, stringHandler)

    probe.send(handler, 123)
    probe.expectMsg(-123)

    probe.send(handler, "abcdef")
    probe.expectMsg("fedcba")
  }

} 
Example 13
Source File: PaymentHandler.scala    From eclair   with Apache License 2.0 5 votes vote down vote up
package fr.acinq.eclair.payment.receive

import akka.actor.Actor.Receive
import akka.actor.{Actor, ActorContext, ActorRef, DiagnosticActorLogging, Props}
import akka.event.DiagnosticLoggingAdapter
import akka.event.Logging.MDC
import fr.acinq.eclair.{Logs, NodeParams}

trait ReceiveHandler {
  def handle(implicit ctx: ActorContext, log: DiagnosticLoggingAdapter): Receive
}


class PaymentHandler(nodeParams: NodeParams, commandBuffer: ActorRef) extends Actor with DiagnosticActorLogging {

  // we do this instead of sending it to ourselves, otherwise there is no guarantee that this would be the first processed message
  private val defaultHandler = new MultiPartHandler(nodeParams, nodeParams.db.payments, commandBuffer)

  override def receive: Receive = normal(defaultHandler.handle(context, log))

  def normal(handle: Receive): Receive = handle orElse {
    case handler: ReceiveHandler =>
      log.info(s"registering handler of type=${handler.getClass.getSimpleName}")
      // NB: the last handler that was added will be the first called
      context become normal(handler.handle(context, log) orElse handle)
  }

  override def mdc(currentMessage: Any): MDC = Logs.mdc(category_opt = Some(Logs.LogCategory.PAYMENT))
}

object PaymentHandler {
  def props(nodeParams: NodeParams, commandBuffer: ActorRef): Props = Props(new PaymentHandler(nodeParams, commandBuffer))
} 
Example 14
Source File: StormProducer.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.experiments.storm.producer

import java.time.Instant
import java.util.concurrent.TimeUnit

import akka.actor.Actor.Receive
import org.apache.gearpump.Message
import org.apache.gearpump.cluster.UserConfig
import org.apache.gearpump.experiments.storm.topology.GearpumpStormComponent.GearpumpSpout
import org.apache.gearpump.experiments.storm.util._
import org.apache.gearpump.streaming.source.Watermark
import org.apache.gearpump.streaming.task._

import scala.concurrent.duration.Duration

object StormProducer {
  private[storm] val TIMEOUT = Message("timeout")
}


private[storm] class StormProducer(gearpumpSpout: GearpumpSpout,
    taskContext: TaskContext, conf: UserConfig)
  extends Task(taskContext, conf) {
  import org.apache.gearpump.experiments.storm.producer.StormProducer._

  def this(taskContext: TaskContext, conf: UserConfig) = {
    this(StormUtil.getGearpumpStormComponent(taskContext, conf)(taskContext.system)
      .asInstanceOf[GearpumpSpout], taskContext, conf)
  }

  private val timeoutMillis = gearpumpSpout.getMessageTimeout

  override def onStart(startTime: Instant): Unit = {
    gearpumpSpout.start(startTime)
    if (gearpumpSpout.ackEnabled) {
      getCheckpointClock
    }
    timeoutMillis.foreach(scheduleTimeout)
    self ! Watermark(Instant.now)
  }

  override def onNext(msg: Message): Unit = {
    msg match {
      case TIMEOUT =>
        timeoutMillis.foreach { timeout =>
          gearpumpSpout.timeout(timeout)
          scheduleTimeout(timeout)
        }
      case _ =>
        gearpumpSpout.next(msg)
    }
    self ! Watermark(Instant.now)
  }

  override def receiveUnManagedMessage: Receive = {
    case CheckpointClock(optClock) =>
      optClock.foreach { clock =>
        gearpumpSpout.checkpoint(clock)
      }
      getCheckpointClock()
  }

  def getCheckpointClock(): Unit = {
    taskContext.scheduleOnce(Duration(StormConstants.CHECKPOINT_INTERVAL_MILLIS,
      TimeUnit.MILLISECONDS))(taskContext.appMaster ! GetCheckpointClock)
  }

  private def scheduleTimeout(timeout: Long): Unit = {
    taskContext.scheduleOnce(Duration(timeout, TimeUnit.MILLISECONDS)) {
      self ! TIMEOUT
    }
  }
} 
Example 15
Source File: SourceBridgeTask.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.akkastream.task

import java.time.Instant

import akka.actor.Actor.Receive
import org.apache.gearpump.Message
import org.apache.gearpump.akkastream.task.SourceBridgeTask.{AkkaStreamMessage, Complete, Error}
import org.apache.gearpump.cluster.UserConfig
import org.apache.gearpump.cluster.client.ClientContext
import org.apache.gearpump.streaming.ProcessorId
import org.apache.gearpump.streaming.appmaster.AppMaster.{LookupTaskActorRef, TaskActorRef}
import org.apache.gearpump.streaming.task.{Task, TaskContext, TaskId}
import org.reactivestreams.{Subscriber, Subscription}

import scala.concurrent.ExecutionContext


class SourceBridgeTask(taskContext : TaskContext, userConf : UserConfig)
  extends Task(taskContext, userConf) {
  import taskContext.taskId

  override def onStart(startTime : Instant) : Unit = {}

  override def onNext(msg: Message) : Unit = {
    LOG.info("AkkaStreamSource receiving message " + msg)
  }

  override def onStop() : Unit = {}

  override def receiveUnManagedMessage: Receive = {
    case Error(ex) =>
      LOG.error("the stream has error", ex)
    case AkkaStreamMessage(msg) =>
      LOG.info("we have received message from akka stream source: " + msg)
      taskContext.output(Message(msg, Instant.now()))
    case Complete(description) =>
      LOG.info("the stream is completed: " + description)
    case msg =>
      LOG.error("Failed! Received unknown message " + "taskId: " + taskId + ", " + msg.toString)
  }
}


object SourceBridgeTask {
  case class Error(ex: java.lang.Throwable)

  case class Complete(description: String)

  case class AkkaStreamMessage[T >: AnyRef](msg: T)

  class SourceBridgeTaskClient[T >: AnyRef](ec: ExecutionContext,
      context: ClientContext, appId: Int, processorId: ProcessorId) extends Subscriber[T] {
    val taskId = TaskId(processorId, 0)
    var subscription: Subscription = _
    implicit val dispatcher = ec

    val task = context.askAppMaster[TaskActorRef](appId,
      LookupTaskActorRef(taskId)).map{container =>
      // println("Successfully resolved taskRef for taskId " + taskId + ", " + container.task)
      container.task
    }

    override def onError(throwable: Throwable): Unit = {
      task.map(task => task ! Error(throwable))
    }

    override def onSubscribe(subscription: Subscription): Unit = {
      // when taskActorRef is resolved, request message from upstream
      this.subscription = subscription
      task.map(task => subscription.request(1))
    }

    override def onComplete(): Unit = {
      task.map(task => task ! Complete("the upstream is completed"))
    }

    override def onNext(t: T): Unit = {
      task.map {task =>
        task ! AkkaStreamMessage(t)
      }
      subscription.request(1)
    }
  }
} 
Example 16
Source File: SinkBridgeTask.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.akkastream.task

import java.time.Instant
import java.util
import java.util.concurrent.TimeUnit

import akka.actor.Actor.Receive
import akka.actor.{Actor, ActorRef, ActorSystem, Props}
import akka.util.Timeout
import org.apache.gearpump.Message
import org.apache.gearpump.akkastream.task.SinkBridgeTask.RequestMessage
import org.apache.gearpump.cluster.UserConfig
import org.apache.gearpump.cluster.client.ClientContext
import org.apache.gearpump.streaming.ProcessorId
import org.apache.gearpump.streaming.appmaster.AppMaster.{LookupTaskActorRef, TaskActorRef}
import org.apache.gearpump.streaming.task.{Task, TaskContext, TaskId}
import org.apache.gearpump.util.LogUtil
import org.reactivestreams.{Publisher, Subscriber, Subscription}


class SinkBridgeTask(taskContext : TaskContext, userConf : UserConfig)
  extends Task(taskContext, userConf) {
  import taskContext.taskId

  val queue = new util.LinkedList[Message]()
  var subscriber: ActorRef = _

  var request: Int = 0

  override def onStart(startTime : Instant) : Unit = {}

  override def onNext(msg: Message) : Unit = {
    queue.add(msg)
    trySendingData()
  }

  override def onStop() : Unit = {}

  private def trySendingData(): Unit = {
    if (subscriber != null) {
      (0 to request).map(_ => queue.poll()).filter(_ != null).foreach { msg =>
        subscriber ! msg.value
        request -= 1
      }
    }
  }

  override def receiveUnManagedMessage: Receive = {
    case RequestMessage(n) =>
      this.subscriber = sender
      LOG.info("the downstream has requested " + n + " messages from " + subscriber)
      request += n.toInt
      trySendingData()
    case msg =>
      LOG.error("Failed! Received unknown message " + "taskId: " + taskId + ", " + msg.toString)
  }
}

object SinkBridgeTask {

  case class RequestMessage(number: Int)

  class SinkBridgeTaskClient(system: ActorSystem, context: ClientContext, appId: Int,
      processorId: ProcessorId) extends Publisher[AnyRef] with Subscription {
    private val taskId = TaskId(processorId, index = 0)
    private val LOG = LogUtil.getLogger(getClass)

    private var actor: ActorRef = _
    import system.dispatcher

    private val task =
      context.askAppMaster[TaskActorRef](appId, LookupTaskActorRef(taskId)).map{container =>
      // println("Successfully resolved taskRef for taskId " + taskId + ", " + container.task)
      container.task
    }

    override def subscribe(subscriber: Subscriber[_ >: AnyRef]): Unit = {
      this.actor = system.actorOf(Props(new ClientActor(subscriber)))
      subscriber.onSubscribe(this)
    }

    override def cancel(): Unit = Unit

    private implicit val timeout = Timeout(5, TimeUnit.SECONDS)

    override def request(l: Long): Unit = {
      task.foreach{ task =>
        task.tell(RequestMessage(l.toInt), actor)
      }
    }
  }

  class ClientActor(subscriber: Subscriber[_ >: AnyRef]) extends Actor {
    def receive: Receive = {
      case result: AnyRef =>
        subscriber.onNext(result)
    }
  }
} 
Example 17
Source File: PageRankController.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.experiments.pagerank

import java.time.Instant

import akka.actor.Actor.Receive

import org.apache.gearpump.cluster.UserConfig
import org.apache.gearpump.experiments.pagerank.PageRankController.Tick
import org.apache.gearpump.experiments.pagerank.PageRankWorker.LatestWeight
import org.apache.gearpump.streaming.task._

class PageRankController(taskContext: TaskContext, conf: UserConfig)
  extends Task(taskContext, conf) {

  val taskCount = conf.getInt(PageRankApplication.COUNT).get
  val iterationMax = conf.getInt(PageRankApplication.ITERATION).get
  val delta = conf.getDouble(PageRankApplication.DELTA).get

  val tasks = (0 until taskCount).toList.map(TaskId(1, _))

  var tick: Int = 0
  var receivedWeightForCurrentTick = 0

  var weights = Map.empty[TaskId, Double]
  var deltas = Map.empty[TaskId, Double]

  override def onStart(startTime: Instant): Unit = {
    output(Tick(tick), tasks: _*)
  }

  private def output(msg: AnyRef, tasks: TaskId*): Unit = {
    taskContext.asInstanceOf[TaskWrapper].outputUnManaged(msg, tasks: _*)
  }

  override def receiveUnManagedMessage: Receive = {
    case LatestWeight(taskId, weight, replyTick) =>
      if (this.tick == replyTick) {

        deltas += taskId -> Math.abs(weight - weights.getOrElse(taskId, 0.0))
        weights += taskId -> weight
        receivedWeightForCurrentTick += 1
        if (receivedWeightForCurrentTick == taskCount) {
          this.tick += 1
          receivedWeightForCurrentTick = 0
          if (continueIteration) {
            LOG.debug(s"next iteration: $tick, weight: $weights, delta: $deltas")
            output(Tick(tick), tasks: _*)
          } else {
            LOG.info(s"iterations: $tick, weight: $weights, delta: $deltas")
          }
        }
      }
  }

  private def continueIteration: Boolean = {
    (tick < iterationMax) && deltas.values.foldLeft(false) { (deltaExceed, value) =>
      deltaExceed || value > delta
    }
  }
}

object PageRankController {
  case class Tick(iteration: Int)
} 
Example 18
Source File: PageRankWorker.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.experiments.pagerank

import akka.actor.Actor.Receive

import org.apache.gearpump.cluster.UserConfig
import org.apache.gearpump.experiments.pagerank.PageRankApplication.NodeWithTaskId
import org.apache.gearpump.experiments.pagerank.PageRankController.Tick
import org.apache.gearpump.experiments.pagerank.PageRankWorker.{LatestWeight, UpdateWeight}
import org.apache.gearpump.streaming.task.{Task, TaskContext, TaskId, TaskWrapper}
import org.apache.gearpump.util.Graph

class PageRankWorker(taskContext: TaskContext, conf: UserConfig) extends Task(taskContext, conf) {
  import taskContext.taskId

  private var weight: Double = 1.0
  private var upstreamWeights = Map.empty[TaskId, Double]

  val taskCount = conf.getInt(PageRankApplication.COUNT).get
  lazy val allTasks = (0 until taskCount).toList.map(TaskId(processorId = 1, _))

  private val graph = conf.getValue[Graph[NodeWithTaskId[_], AnyRef]](PageRankApplication.DAG).get

  private val node = graph.getVertices.find { node =>
    node.taskId == taskContext.taskId.index
  }.get

  private val downstream = graph.outgoingEdgesOf(node).map(_._3)
    .map(id => taskId.copy(index = id.taskId)).toSeq
  private val upstreamCount = graph.incomingEdgesOf(node).map(_._1).size

  LOG.info(s"downstream nodes: $downstream")

  private var tick = 0

  private def output(msg: AnyRef, tasks: TaskId*): Unit = {
    taskContext.asInstanceOf[TaskWrapper].outputUnManaged(msg, tasks: _*)
  }

  override def receiveUnManagedMessage: Receive = {
    case Tick(tick) =>
      this.tick = tick

      if (downstream.length == 0) {
        // If there is no downstream, we will evenly distribute our page rank to
        // every node in the graph
        val update = UpdateWeight(taskId, weight / taskCount)
        output(update, allTasks: _*)
      } else {
        val update = UpdateWeight(taskId, weight / downstream.length)
        output(update, downstream: _*)
      }
    case update@UpdateWeight(upstreamTaskId, weight) =>
      upstreamWeights += upstreamTaskId -> weight
      if (upstreamWeights.size == upstreamCount) {
        val nextWeight = upstreamWeights.foldLeft(0.0) { (sum, item) => sum + item._2 }
        this.upstreamWeights = Map.empty[TaskId, Double]
        this.weight = nextWeight
        output(LatestWeight(taskId, weight, tick), TaskId(0, 0))
      }
  }
}

object PageRankWorker {
  case class UpdateWeight(taskId: TaskId, weight: Double)
  case class LatestWeight(taskId: TaskId, weight: Double, tick: Int)
} 
Example 19
Source File: LoggingReceive.scala    From perf_tester   with Apache License 2.0 5 votes vote down vote up
package akka.event

import language.existentials
import akka.actor.Actor.Receive
import akka.actor.ActorContext
import akka.actor.ActorCell
import akka.actor.DiagnosticActorLogging
import akka.event.Logging.{ LogEvent, LogLevel }
import akka.actor.AbstractActor
import scala.runtime.BoxedUnit

object LoggingReceive {

  
class LoggingReceive(source: Option[AnyRef], r: Receive, label: Option[String], logLevel: LogLevel)(implicit context: ActorContext) extends Receive {
  def this(source: Option[AnyRef], r: Receive, label: Option[String])(implicit context: ActorContext) = this(source, r, label, Logging.DebugLevel)
  def this(source: Option[AnyRef], r: Receive)(implicit context: ActorContext) = this(source, r, None, Logging.DebugLevel)
  def isDefinedAt(o: Any): Boolean = {
    val handled = r.isDefinedAt(o)
    if (context.system.eventStream.logLevel >= logLevel) {
      val src = source getOrElse context.asInstanceOf[ActorCell].actor
      val (str, clazz) = LogSource.fromAnyRef(src)
      val message = "received " + (if (handled) "handled" else "unhandled") + " message " + o + " from " + context.sender() +
        (label match {
          case Some(l) ⇒ " in state " + l
          case _ ⇒ ""
        })
      val event = src match {
        case a: DiagnosticActorLogging ⇒ LogEvent(logLevel, str, clazz, message, a.log.mdc)
        case _ ⇒ LogEvent(logLevel, str, clazz, message)
      }
      context.system.eventStream.publish(event)
    }
    handled
  }
  def apply(o: Any): Unit = r(o)
} 
Example 20
Source File: LoggingReceive.scala    From typed-actors   with Apache License 2.0 5 votes vote down vote up
package akka

import akka.actor.Actor.Receive
import akka.actor.ActorContext


object LoggingReceive {

  def apply(r: Receive)(implicit context: ActorContext): Receive = r match {
    case _: LoggingReceive ⇒ r
    case _                 ⇒ if (context.system.settings.AddLoggingReceive) new LoggingReceive(r) else r
  }
}

class LoggingReceive(r: Receive)(implicit context: ActorContext) extends Receive {
  def isDefinedAt(o: Any): Boolean = {
    val handled = r.isDefinedAt(o)
    val msg = s"received ${if (handled) "handled" else "unhandled"} message $o"
    println(s"[DEBUG] $msg")
    handled
  }
  def apply(o: Any): Unit = r(o)
} 
Example 21
Source File: BehaviorContextSpec.scala    From eventuate   with Apache License 2.0 5 votes vote down vote up
package com.rbmhtechnology.eventuate

import akka.actor.Actor.Receive

import org.scalatest._

class BehaviorContextSpec extends WordSpec with Matchers with BeforeAndAfterEach {
  var context: BehaviorContext = _
  var state: Int = 0

  val b1: Receive = {
    case _ => state = 1
  }

  val b2: Receive = {
    case _ => state = 2
  }

  val b3: Receive = {
    case _ => state = 3
  }

  implicit class RunnableBehavior(behavior: Receive) {
    def run: Int = {
      behavior.apply(())
      state
    }
  }

  override protected def beforeEach(): Unit = {
    context = new DefaultBehaviorContext(b1)
  }

  "A DefaultBehaviorContext" must {
    "have an initial behavior" in {
      context.current.run should be(1)
    }
    "add and remove behavior" in {
      context.become(b2, replace = false)
      context.current.run should be(2)
      context.become(b3, replace = false)
      context.current.run should be(3)
      context.unbecome()
      context.current.run should be(2)
      context.unbecome()
      context.current.run should be(1)
    }
    "replace behavior and revert to initial behavior" in {
      context.become(b2)
      context.current.run should be(2)
      context.become(b3)
      context.current.run should be(3)
      context.unbecome() // revert to initial behavior
      context.current.run should be(1)
    }
  }
} 
Example 22
Source File: BehaviorContext.scala    From eventuate   with Apache License 2.0 5 votes vote down vote up
package com.rbmhtechnology.eventuate

import akka.actor.Actor.Receive


  def unbecome(): Unit
}

private class DefaultBehaviorContext(val initial: Receive) extends BehaviorContext {
  private var behaviorStack: List[Receive] = List(initial)

  override def current: Receive =
    behaviorStack.head

  override def become(behavior: Receive, discardOld: Boolean): Unit = {
    behaviorStack = behaviorStack match {
      case b :: bs if discardOld => behavior :: bs
      case bs                    => behavior :: bs
    }
  }

  override def unbecome(): Unit = {
    behaviorStack = behaviorStack match {
      case b :: Nil => initial :: Nil
      case bs       => bs.tail
    }
  }
}

private class StaticBehaviorContext(val initial: Receive) extends BehaviorContext {
  override def current: Receive =
    initial

  override def become(behavior: Receive, discardOld: Boolean): Unit =
    throw new UnsupportedOperationException("Behavior change not supported")

  override def unbecome(): Unit =
    ()
}