Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Runtime overhaul and modularization (#17)
- Loading branch information
Showing
132 changed files
with
4,254 additions
and
2,276 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
language: scala | ||
scala: | ||
- 2.11.8 | ||
- 2.12.0 | ||
- 2.11.11-bin-typelevel-4 | ||
- 2.12.2-bin-typelevel-4 | ||
|
||
jdk: | ||
- oraclejdk8 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
aecor-akka-generic/src/main/scala/aecor/runtime/akkageneric/GenericAkkaRuntime.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package aecor.runtime.akkageneric | ||
|
||
import aecor.data.{ Behavior, Correlation } | ||
import aecor.effect.{ Async, Capture, CaptureFuture } | ||
import aecor.runtime.akkageneric.GenericAkkaRuntime.CorrelatedCommand | ||
import akka.actor.ActorSystem | ||
import akka.cluster.sharding.{ ClusterSharding, ShardRegion } | ||
import akka.pattern._ | ||
import akka.util.Timeout | ||
import cats.~> | ||
|
||
import scala.concurrent.Future | ||
|
||
object GenericAkkaRuntime { | ||
def apply[F[_]: Async: CaptureFuture: Capture](system: ActorSystem): GenericAkkaRuntime[F] = | ||
new GenericAkkaRuntime(system) | ||
private final case class CorrelatedCommand[A](entityId: String, command: A) | ||
} | ||
|
||
class GenericAkkaRuntime[F[_]: Async: CaptureFuture: Capture](system: ActorSystem) { | ||
def start[Op[_]](typeName: String, | ||
correlation: Correlation[Op], | ||
behavior: Behavior[F, Op], | ||
settings: GenericAkkaRuntimeSettings = | ||
GenericAkkaRuntimeSettings.default(system)): F[Op ~> F] = | ||
Capture[F] | ||
.capture { | ||
val numberOfShards = settings.numberOfShards | ||
|
||
val extractEntityId: ShardRegion.ExtractEntityId = { | ||
case CorrelatedCommand(entityId, c) => | ||
(entityId, GenericAkkaRuntimeActor.PerformOp(c.asInstanceOf[Op[_]])) | ||
} | ||
|
||
val extractShardId: ShardRegion.ExtractShardId = { | ||
case CorrelatedCommand(entityId, _) => | ||
(scala.math.abs(entityId.hashCode) % numberOfShards).toString | ||
case other => throw new IllegalArgumentException(s"Unexpected message [$other]") | ||
} | ||
|
||
val props = GenericAkkaRuntimeActor.props(behavior, settings.idleTimeout) | ||
|
||
val shardRegionRef = ClusterSharding(system).start( | ||
typeName = typeName, | ||
entityProps = props, | ||
settings = settings.clusterShardingSettings, | ||
extractEntityId = extractEntityId, | ||
extractShardId = extractShardId | ||
) | ||
|
||
implicit val timeout = Timeout(settings.askTimeout) | ||
new (Op ~> F) { | ||
override def apply[A](fa: Op[A]): F[A] = CaptureFuture[F].captureFuture { | ||
(shardRegionRef ? CorrelatedCommand(correlation(fa), fa)).asInstanceOf[Future[A]] | ||
} | ||
} | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
aecor-akka-generic/src/main/scala/aecor/runtime/akkageneric/GenericAkkaRuntimeActor.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package aecor.runtime.akkageneric | ||
|
||
import java.util.UUID | ||
|
||
import aecor.data.Behavior | ||
import aecor.effect.Async | ||
import aecor.effect.Async.ops._ | ||
import aecor.runtime.akkageneric.GenericAkkaRuntimeActor.PerformOp | ||
import akka.actor.{ Actor, ActorLogging, Props, ReceiveTimeout, Stash, Status } | ||
import akka.cluster.sharding.ShardRegion | ||
import akka.pattern.pipe | ||
|
||
import scala.concurrent.duration.FiniteDuration | ||
import scala.util.control.NonFatal | ||
import scala.util.{ Failure, Success, Try } | ||
|
||
object GenericAkkaRuntimeActor { | ||
def props[F[_]: Async, Op[_]](behavior: Behavior[F, Op], idleTimeout: FiniteDuration): Props = | ||
Props(new GenericAkkaRuntimeActor(behavior, idleTimeout)) | ||
|
||
final case class PerformOp[Op[_], A](op: Op[A]) | ||
case object Stop | ||
} | ||
|
||
private[aecor] final class GenericAkkaRuntimeActor[F[_]: Async, Op[_]](behavior: Behavior[F, Op], | ||
idleTimeout: FiniteDuration) | ||
extends Actor | ||
with Stash | ||
with ActorLogging { | ||
|
||
import context._ | ||
|
||
private case class Result(id: UUID, value: Try[(Behavior[F, Op], Any)]) | ||
|
||
setIdleTimeout() | ||
|
||
override def receive: Receive = withBehavior(behavior) | ||
|
||
private def withBehavior(behavior: Behavior[F, Op]): Receive = { | ||
case PerformOp(op) => | ||
val opId = UUID.randomUUID() | ||
behavior | ||
.run(op.asInstanceOf[Op[Any]]) | ||
.unsafeRun | ||
.map(x => Result(opId, Success(x))) | ||
.recover { | ||
case NonFatal(e) => Result(opId, Failure(e)) | ||
} | ||
.pipeTo(self)(sender) | ||
|
||
become { | ||
case Result(`opId`, value) => | ||
value match { | ||
case Success((newBehavior, reply)) => | ||
sender() ! reply | ||
become(withBehavior(newBehavior)) | ||
case Failure(cause) => | ||
sender() ! Status.Failure(cause) | ||
throw cause | ||
} | ||
unstashAll() | ||
case _ => | ||
stash() | ||
} | ||
case ReceiveTimeout => | ||
passivate() | ||
case GenericAkkaRuntimeActor.Stop => | ||
context.stop(self) | ||
case Result(_, _) => | ||
log.debug( | ||
"Ignoring result of another operation. Probably targeted previous instance of actor." | ||
) | ||
} | ||
|
||
private def passivate(): Unit = { | ||
log.debug("Passivating...") | ||
context.parent ! ShardRegion.Passivate(GenericAkkaRuntimeActor.Stop) | ||
} | ||
|
||
private def setIdleTimeout(): Unit = { | ||
log.debug("Setting idle timeout to [{}]", idleTimeout) | ||
context.setReceiveTimeout(idleTimeout) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.