Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change behavior of simulateRemotePods to use the Pods API #142

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ val sttpVersion = "3.9.6"
val calibanVersion = "2.8.1"
val redis4catsVersion = "1.5.2"
val redissonVersion = "3.27.1"
val scalaKryoVersion = "1.0.2"
val scalaKryoVersion = "1.2.0"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They fixed an issue with the serialization of Scala 3 enums.

val testContainersVersion = "0.41.3"
val scalaCompatVersion = "2.12.0"

Expand Down Expand Up @@ -212,6 +212,13 @@ lazy val commonSettings = Def.settings(
"com.dimafeng" %% "testcontainers-scala-core" % testContainersVersion % Test
),
Test / fork := true,
Test / javaOptions ++= Seq(
// Kryo requires this with the recent versions of Java
"--add-opens=java.base/java.util=ALL-UNNAMED",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed to serialize Exception which we do in tests.

"--add-opens=java.base/java.lang=ALL-UNNAMED",
"--add-opens=java.base/java.lang.invoke=ALL-UNNAMED",
"--add-opens=java.sql/java.sql=ALL-UNNAMED"
),
scalacOptions ++= Seq(
"-deprecation",
"-encoding",
Expand Down
26 changes: 10 additions & 16 deletions entities/src/main/scala/com/devsisters/shardcake/Sharding.scala
Original file line number Diff line number Diff line change
Expand Up @@ -257,21 +257,15 @@ class Sharding private (
replyId: Option[String],
replyChannel: ReplyChannel[Res]
): Task[Unit] =
if (config.simulateRemotePods) {
serialization
.encode(msg)
.flatMap(bytes => sendToLocalEntity(BinaryMessage(entityId, recipientTypeName, bytes, replyId), replyChannel))
} else {
// if pod = self, shortcut and send directly without serialization
entityStates.get.flatMap(
_.get(recipientTypeName) match {
case Some(state) =>
state.entityManager.asInstanceOf[EntityManager[Msg]].send(entityId, msg, replyId, replyChannel)
case None =>
ZIO.fail(new Exception(s"Entity type $recipientTypeName was not registered."))
}
)
}
// if pod = self, shortcut and send directly without serialization
entityStates.get.flatMap(
_.get(recipientTypeName) match {
case Some(state) =>
state.entityManager.asInstanceOf[EntityManager[Msg]].send(entityId, msg, replyId, replyChannel)
case None =>
ZIO.fail(new Exception(s"Entity type $recipientTypeName was not registered."))
}
)

private def sendToPod[Msg, Res](
recipientTypeName: String,
Expand All @@ -281,7 +275,7 @@ class Sharding private (
replyChannel: ReplyChannel[Res],
replyId: Option[String]
): Task[Unit] =
if (pod == address) {
if (pod == address && !config.simulateRemotePods) {
val run = sendChannel.foreach(sendToSelf(recipientTypeName, entityId, _, replyId, replyChannel))
sendChannel match {
case _: SendChannel.Single[_] => run
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ import scala.util.Success

object BroadcastingSpec extends ZIOSpecDefault {

private val config = ZLayer.succeed(
Config.default.copy(
simulateRemotePods = true
)
)
private val config = ZLayer.succeed(Config.default)

def spec: Spec[TestEnvironment with Scope, Any] =
suite("BroadcastingSpec")(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ object StorageRedis {

def savePods(pods: Map[PodAddress, Pod]): Task[Unit] =
stringClient.del(config.podsKey) *>
stringClient.hSet(config.podsKey, pods.map { case (k, v) => k.toString -> v.version }).unit
stringClient
.hSet(config.podsKey, pods.map { case (k, v) => k.toString -> v.version })
.when(pods.nonEmpty)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found that this was failing when saving an empty list.

.unit
}
}
}
Loading