Skip to content

Commit

Permalink
feat: Native-image support (#529)
Browse files Browse the repository at this point in the history
Also bumps Akka version to 2.9.2 for native-image metadata
  • Loading branch information
johanandren authored Feb 28, 2024
1 parent 1532507 commit c6586b4
Show file tree
Hide file tree
Showing 17 changed files with 422 additions and 7 deletions.
30 changes: 24 additions & 6 deletions .github/workflows/native-image-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,30 @@ jobs:
run: |-
sbt "publishLocal"
#- name: Akka Persistence R2DBC native image test app build
# run: |-
# cd native-image-tests/
# sbt nativeImage -Dakka.http.version=`cat ~/.version`
# run the binary
# target/native-image/native-image-tests
- name: Akka Persistence R2DBC native image test app build
run: |-
cd native-image-tests/
sbt nativeImage -Dakka.r2dbc.version=`cat ~/.version`
- name: Akka Persistence native image H2 inmem
run: |-
cd native-image-tests/
target/native-image/native-image-tests
- name: Akka Persistence native image H2 file
run: |-
cd native-image-tests/
target/native-image/native-image-tests -Dconfig.resource=application-h2-file.conf
- name: Start Postgres DB
run: |-
docker compose -f docker/docker-compose-postgres.yml up --wait
docker exec -i postgres-db psql -U postgres -t < ddl-scripts/create_tables_postgres.sql
- name: Akka Persistence native image H2
run: |-
cd native-image-tests/
target/native-image/native-image-tests -Dconfig.resource=application-postgres.conf
- name: Email on failure
if: ${{ failure() }}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
[
{
"name": "akka.persistence.r2dbc.journal.R2dbcJournal",
"methods": [
{
"name": "<init>",
"parameterTypes": [
"com.typesafe.config.Config",
"java.lang.String"
]
}
]
},
{
"name": "akka.persistence.r2dbc.query.R2dbcReadJournalProvider",
"methods": [
{
"name": "<init>",
"parameterTypes": [
"akka.actor.ExtendedActorSystem",
"com.typesafe.config.Config",
"java.lang.String"
]
}
]
},
{
"name": "akka.persistence.r2dbc.snapshot.R2dbcSnapshotStore",
"methods": [
{
"name": "<init>",
"parameterTypes": [
"com.typesafe.config.Config",
"java.lang.String"
]
}
]
},
{
"name": "akka.persistence.r2dbc.state.R2dbcDurableStateStoreProvider",
"methods": [
{
"name": "<init>",
"parameterTypes": [
"akka.actor.ExtendedActorSystem",
"com.typesafe.config.Config",
"java.lang.String"
]
}
]
}

]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Args = --initialize-at-run-time=io.netty.handler.ssl.BouncyCastleAlpnSslUtils
1 change: 1 addition & 0 deletions docs/src/main/paradox/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ The Akka Persistence R2DBC plugin allows for using SQL database with R2DBC as a
* [Cleanup tool](cleanup.md)
* [Migration tool](migration.md)
* [Migration Guide](migration-guide.md)
* [Native Image](native-image.md)
* [Contributing](contributing.md)

@@@
Expand Down
11 changes: 11 additions & 0 deletions docs/src/main/paradox/native-image.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Building Native Images

Building native images with Akka Persistence R2DBC is supported out of the box for the event sourced journal, snapshot store and
durable state store with the following databases:

* H2 (inmem and file)
* Postgres

Other databases can likely be used but will require figuring out and adding additional native-image metadata.

For details about building native images with Akka in general, see the @extref[Akka Documentation](akka:additional/native-image.html).
12 changes: 12 additions & 0 deletions native-image-tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
target/

.settings
.project
.classpath

.idea
*.iml

.metals
.bloop
.bsp
34 changes: 34 additions & 0 deletions native-image-tests/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name := "native-image-tests"

version := "1.0"

scalaVersion := "2.13.12"

resolvers += "Akka library repository".at("https://repo.akka.io/maven")

lazy val akkaVersion = sys.props.getOrElse("akka.version", "2.9.2")
lazy val akkaR2dbcVersion = sys.props.getOrElse("akka.r2dbc.version", "1.2.3")

fork := true

// GraalVM native image build
enablePlugins(NativeImagePlugin)
nativeImageJvm := "graalvm-community"
nativeImageVersion := "21.0.2"
nativeImageOptions := Seq(
"--no-fallback",
"--verbose",
"--initialize-at-build-time=ch.qos.logback",
"-Dakka.native-image.debug=true")

libraryDependencies ++= Seq(
"com.typesafe.akka" %% "akka-actor-typed" % akkaVersion,
"com.typesafe.akka" %% "akka-persistence-typed" % akkaVersion,
"com.typesafe.akka" %% "akka-persistence-query" % akkaVersion,
"com.typesafe.akka" %% "akka-slf4j" % akkaVersion,
"com.typesafe.akka" %% "akka-serialization-jackson" % akkaVersion,
"com.lightbend.akka" %% "akka-persistence-r2dbc" % akkaR2dbcVersion,
"ch.qos.logback" % "logback-classic" % "1.2.13",
// H2
"com.h2database" % "h2" % "2.2.224",
"io.r2dbc" % "r2dbc-h2" % "1.0.0.RELEASE")
1 change: 1 addition & 0 deletions native-image-tests/project/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version=1.9.8
1 change: 1 addition & 0 deletions native-image-tests/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("org.scalameta" % "sbt-native-image" % "0.3.4")
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
akka.persistence.journal.plugin = "akka.persistence.r2dbc.journal"
akka.persistence.snapshot-store.plugin = "akka.persistence.r2dbc.snapshot"
akka.persistence.state.plugin = "akka.persistence.r2dbc.state"

akka.persistence.r2dbc.connection-factory = ${akka.persistence.r2dbc.h2}
akka.persistence.r2dbc.connection-factory = {
protocol = "file"
database = "/tmp/test-h2-database"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
akka.persistence.journal.plugin = "akka.persistence.r2dbc.journal"
akka.persistence.snapshot-store.plugin = "akka.persistence.r2dbc.snapshot"
akka.persistence.state.plugin = "akka.persistence.r2dbc.state"

akka.persistence.r2dbc.connection-factory = ${akka.persistence.r2dbc.postgres}
10 changes: 10 additions & 0 deletions native-image-tests/src/main/resources/application.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
akka.persistence.journal.plugin = "akka.persistence.r2dbc.journal"
akka.persistence.snapshot-store.plugin = "akka.persistence.r2dbc.snapshot"
akka.persistence.state.plugin = "akka.persistence.r2dbc.state"

akka.persistence.r2dbc.connection-factory = ${akka.persistence.r2dbc.h2}
akka.persistence.r2dbc.connection-factory = {
# overrides for default values from the 'akka.persistence.r2dbc.h2' config block
protocol = "mem"
database = "mydb"
}
13 changes: 13 additions & 0 deletions native-image-tests/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appender name="STDOUT" target="System.out" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>[%date{ISO8601}] [%level] [%logger] [%X{akkaAddress}] [%marker] [%thread] - %msg%n</pattern>
</encoder>
</appender>

<root level="INFO">
<appender-ref ref="STDOUT"/>
</root>

</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (C) 2009-2024 Lightbend Inc. <https://www.lightbend.com>
*/
package com.lightbend

import akka.actor.typed.ActorRef
import akka.actor.typed.Behavior
import akka.actor.typed.scaladsl.Behaviors
import akka.persistence.typed.PersistenceId
import akka.persistence.typed.state.scaladsl.DurableStateBehavior
import akka.persistence.typed.state.scaladsl.Effect
import akka.serialization.jackson.JsonSerializable

import scala.concurrent.duration.DurationInt

object DurableStateCounter {
sealed trait Command extends JsonSerializable
final case class Increase(amount: Int, replyTo: ActorRef[Increased]) extends Command

final case class GetState(replyTo: ActorRef[State]) extends Command

final case class Increased(newValue: Int) extends JsonSerializable

final case class State(value: Int) extends JsonSerializable
def apply(id: String): Behavior[Command] =
DurableStateBehavior[Command, State](
PersistenceId("DSCounter", id),
State(0),
{
case (state, Increase(amount, replyTo)) =>
Effect.persist(State(state.value + amount)).thenReply(replyTo)(newState => Increased(newState.value))
case (state, GetState(replyTo)) =>
Effect.reply(replyTo)(state)
})
}

object DurableStateTester {

def apply(whenDone: ActorRef[String]): Behavior[AnyRef] = Behaviors.setup { context =>
Behaviors.withTimers { timers =>
timers.startSingleTimer("Timeout", 10.seconds)

var durableActor = context.spawn(DurableStateCounter("one"), "DurableOne")
context.watchWith(durableActor, "DurableOneStopped")

def messageOrTimeout(step: String)(partial: PartialFunction[AnyRef, Behavior[AnyRef]]): Behavior[AnyRef] = {
context.log.info("On {}", step)
Behaviors.receiveMessage(message =>
partial.orElse[AnyRef, Behavior[AnyRef]] {
case "Timeout" =>
context.log.error(s"Durable state checks timed out in {}", step)
System.exit(1)
Behaviors.same

case other =>
context.log.warn("Unexpected message in {}: {}", step, other)
Behaviors.same
}(message))
}

durableActor ! DurableStateCounter.Increase(1, context.self)

def step1() = messageOrTimeout("step1") { case DurableStateCounter.Increased(1) =>
// write works
context.stop(durableActor)
step2()
}

def step2() = messageOrTimeout("step2") { case "DurableOneStopped" =>
durableActor = context.spawn(DurableStateCounter("one"), "DurableOneIncarnation2")
durableActor ! DurableStateCounter.GetState(context.self)
step3()
}

def step3() = messageOrTimeout("step3") { case DurableStateCounter.State(1) =>
whenDone ! "Durable State works"
Behaviors.stopped
}

step1()
}
}

}
Loading

0 comments on commit c6586b4

Please sign in to comment.