-
Notifications
You must be signed in to change notification settings - Fork 4
feat: Add gRPC projections support #24
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
Open
johanandren
wants to merge
4
commits into
main
Choose a base branch
from
wip-add-grpc-projections
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,17 @@ | ||
| include "r2dbc" | ||
| include "local-shared" | ||
|
|
||
| akka.http.server.enable-http2 = on | ||
|
|
||
| akka.projection.grpc.consumer { | ||
| client { | ||
| host = "127.0.0.1" | ||
| host = ${?SERVICE_GRPC_HOST} | ||
| port = ${test.http.port} | ||
| port = ${?SERVICE_GRPC_PORT} | ||
| use-tls = false | ||
| } | ||
| stream-id = "events" | ||
| } | ||
|
|
||
| akka.projection.grpc.producer.query-plugin-id = "akka.persistence.r2dbc.query" |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -2,3 +2,4 @@ | |
| include "local-r2dbc" | ||
| #include "local-cassandra" | ||
| #include "local-jdbc" | ||
| #include "local-grpc" | ||
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
46 changes: 46 additions & 0 deletions
46
src/main/scala/akka/projection/testing/grpc/GrpcProjectionHandler.scala
This file contains hidden or 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,46 @@ | ||
| /* | ||
| * Copyright (C) 2020 - 2024 Lightbend Inc. <https://www.lightbend.com> | ||
| */ | ||
|
|
||
| package akka.projection.testing.grpc | ||
|
|
||
| import akka.Done | ||
| import akka.actor.typed.ActorSystem | ||
| import akka.persistence.query.typed.EventEnvelope | ||
| import akka.persistence.r2dbc.internal.R2dbcExecutor | ||
| import akka.persistence.r2dbc.internal.Sql.Interpolation | ||
| import akka.projection.ProjectionId | ||
| import akka.projection.r2dbc.scaladsl.R2dbcSession | ||
| import akka.projection.scaladsl.Handler | ||
| import akka.projection.testing.ConfigurablePersistentActor | ||
|
|
||
| import scala.concurrent.ExecutionContext | ||
| import scala.concurrent.Future | ||
|
|
||
| class GrpcProjectionHandler( | ||
| val projectionId: ProjectionId, | ||
| projectionIndex: Int, | ||
| readOnly: Boolean, | ||
| val failEvery: Int, | ||
| executor: R2dbcExecutor) | ||
| extends Handler[EventEnvelope[ConfigurablePersistentActor.Event]] { | ||
|
|
||
| override def process(envelope: EventEnvelope[ConfigurablePersistentActor.Event]): Future[Done] = | ||
| if (readOnly) | ||
| Future.successful(Done) | ||
| else { | ||
| executor | ||
| .updateOne("insert") { connection => | ||
| connection | ||
| .createStatement(sql""" | ||
| INSERT INTO events(name, projection_id, event) | ||
| VALUES (?, ?, ?) | ||
| ON CONFLICT (name, projection_id, event) | ||
| DO UPDATE SET updates = events.updates + 1; | ||
| """).bind(0, envelope.event.testName) | ||
| .bind(1, projectionIndex) | ||
| .bind(2, envelope.event.payload) | ||
| } | ||
| .map(_ => Done)(ExecutionContext.parasitic) | ||
| } | ||
| } |
154 changes: 154 additions & 0 deletions
154
src/main/scala/akka/projection/testing/grpc/GrpcTestSetup.scala
This file contains hidden or 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,154 @@ | ||
| /* | ||
| * Copyright (C) 2020 - 2024 Lightbend Inc. <https://www.lightbend.com> | ||
| */ | ||
|
|
||
| package akka.projection.testing.grpc | ||
|
|
||
| import akka.Done | ||
| import akka.actor.typed.ActorSystem | ||
| import akka.actor.typed.Behavior | ||
| import akka.actor.typed.scaladsl.Behaviors | ||
| import akka.cluster.MemberStatus.Up | ||
| import akka.cluster.typed.Cluster | ||
| import akka.grpc.GrpcClientSettings | ||
| import akka.persistence.query.Offset | ||
| import akka.persistence.query.typed.EventEnvelope | ||
| import akka.persistence.r2dbc.ConnectionFactoryProvider | ||
| import akka.persistence.r2dbc.internal.R2dbcExecutor | ||
| import akka.persistence.r2dbc.internal.Sql.Interpolation | ||
| import akka.projection.ProjectionBehavior | ||
| import akka.projection.ProjectionId | ||
| import akka.projection.eventsourced.scaladsl.EventSourcedProvider | ||
| import akka.projection.grpc.consumer.GrpcQuerySettings | ||
| import akka.projection.grpc.consumer.scaladsl.GrpcReadJournal | ||
| import akka.projection.r2dbc.scaladsl.R2dbcProjection | ||
| import akka.projection.scaladsl.SourceProvider | ||
| import akka.projection.testing.ConfigurablePersistentActor | ||
| import akka.projection.testing.EventProcessorSettings | ||
| import akka.projection.testing.TestSetup | ||
| import org.slf4j.LoggerFactory | ||
|
|
||
| import scala.concurrent.ExecutionContext | ||
| import scala.concurrent.Future | ||
| import scala.concurrent.duration.DurationInt | ||
| import scala.util.Random | ||
|
|
||
| /** | ||
| * Uses R2DBC for actual DB but gRPC for the projection | ||
| */ | ||
| object GrpcTestSetup { | ||
| def isGrpcRun(system: ActorSystem[_]): Boolean = | ||
| system.settings.config.getString("akka.projection.grpc.consumer.stream-id") != "" | ||
| } | ||
|
|
||
| class GrpcTestSetup(settings: EventProcessorSettings)(implicit system: ActorSystem[_]) extends TestSetup { | ||
| override val journal: TestSetup.Journal = TestSetup.GRPC | ||
| private val log = LoggerFactory.getLogger(this.getClass) | ||
|
|
||
| private val executor = new R2dbcExecutor( | ||
| ConnectionFactoryProvider(system).connectionFactoryFor("akka.persistence.r2dbc.connection-factory"), | ||
| log, | ||
| logDbCallsExceeding = 5.seconds, | ||
| closeCallsExceeding = None)(system.executionContext, system) | ||
|
|
||
| override def init(): Future[Done] = | ||
| Future.successful(Done) | ||
|
|
||
| override def reset(): Future[Done] = { | ||
| executor | ||
| .executeDdls(s"reset") { connection => | ||
| Vector(connection.createStatement("TRUNCATE events")) | ||
| } | ||
| .map(_ => Done)(ExecutionContext.parasitic) | ||
| } | ||
|
|
||
| override def createProjection(projectionIndex: Int, sliceIndex: Int): Behavior[ProjectionBehavior.Command] = { | ||
|
|
||
| val grpcQuerySettings = GrpcQuerySettings(PublishEvents.streamId) | ||
|
|
||
| val portFromConfig = system.settings.config.getInt("test.http.port") | ||
| val (grpcHost: String, grpcPort: Int) = | ||
| if (Cluster(system).state.members.size == 1) (Cluster(system).selfMember.address.host.get, portFromConfig) | ||
| else if (!Cluster(system).selfMember.address.host.contains("127.0.0.1")) { | ||
| // poor man's way to detect if we are non-local/k8 - canonical is not localhost | ||
|
|
||
| val dnsName = system.settings.config.getString("test.service-dns-name") | ||
| if (dnsName != "") { | ||
| log.info("Using configured service dns entry for gRPC projection [{}:{}]", dnsName, portFromConfig) | ||
| // configured dns name to go via | ||
| (dnsName, portFromConfig) | ||
| } else { | ||
| // random other cluster node - directly to canonical ip | ||
| val hostsAndPorts = Cluster(system).state.members | ||
| .filter(_.status == Up) | ||
| .filterNot(_ == Cluster(system).selfMember) | ||
| .map(member => member.address.host.get -> portFromConfig) | ||
| val hostAndPort @ (host, port) = Random.shuffle(hostsAndPorts).head | ||
| log.info("Using random node ip for gRPC projection [{}:{}]", host, port) | ||
| hostAndPort | ||
| } | ||
| } else { | ||
| // random other cluster node | ||
| val hostsAndPorts = Cluster(system).state.members | ||
| .filter(_.status == Up) | ||
| .filterNot(_.address.port.contains(portFromConfig)) | ||
| .map(member => member.address.host.get -> ("80" + member.address.port.get.toString.takeRight(2)).toInt) | ||
| val hostAndPort @ (host, port) = Random.shuffle(hostsAndPorts).head | ||
| log.info("Using random local port for gRPC projection [{}:{}]", host, port) | ||
| hostAndPort | ||
| } | ||
|
|
||
| val grpcClientSettings = | ||
| GrpcClientSettings.connectToServiceAt(grpcHost, grpcPort).withTls(false) | ||
|
|
||
| val eventsBySlicesQuery = GrpcReadJournal(grpcQuerySettings, grpcClientSettings, Nil) | ||
|
|
||
| val ranges = EventSourcedProvider.sliceRanges(system, journal.readJournal, settings.parallelism) | ||
| val sliceRange = ranges(sliceIndex) | ||
|
|
||
| val projectionId = ProjectionId(s"test-projection-id-$projectionIndex", s"${sliceRange.min}-${sliceRange.max}") | ||
|
|
||
| val sourceProvider: SourceProvider[Offset, EventEnvelope[ConfigurablePersistentActor.Event]] = EventSourcedProvider | ||
| .eventsBySlices(system, eventsBySlicesQuery, eventsBySlicesQuery.streamId, sliceRange.min, sliceRange.max) | ||
|
|
||
| ProjectionBehavior( | ||
| R2dbcProjection.atLeastOnceAsync( | ||
| projectionId, | ||
| None, | ||
| sourceProvider, | ||
| () => | ||
| new GrpcProjectionHandler(projectionId, projectionIndex, settings.readOnly, settings.failEvery, executor))) | ||
| } | ||
|
|
||
| override def countEvents(testName: String, projectionId: Int): Future[Int] = { | ||
| executor | ||
| .selectOne("count events")( | ||
| _.createStatement(sql"SELECT count(*) FROM events WHERE name = ? AND projection_id = ?") | ||
| .bind(0, testName) | ||
| .bind(1, projectionId), | ||
| { row => | ||
| row.get(0, classOf[Integer]).intValue | ||
| }) | ||
| .map(_.getOrElse(0))(ExecutionContext.parasitic) | ||
| } | ||
|
|
||
| override def writeResult(testName: String, result: String): Future[Done] = { | ||
| executor | ||
| .updateOne("write result")( | ||
| _.createStatement(sql"INSERT INTO results(name, result) VALUES (?, ?)").bind(0, testName).bind(1, result)) | ||
| .map(_ => Done)(ExecutionContext.parasitic) | ||
| } | ||
|
|
||
| override def readResult(testName: String): Future[String] = { | ||
| executor | ||
| .selectOne("read result")( | ||
| _.createStatement(sql"SELECT result FROM results WHERE name = ?") | ||
| .bind(0, testName), | ||
| { row => | ||
| row.get(0, classOf[String]) | ||
| }) | ||
| .map(_.getOrElse("not finished"))(ExecutionContext.parasitic) | ||
| } | ||
|
|
||
| override def cleanUp(): Unit = () | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.