-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Configuring persistence plugins at runtime for EventSourcedBehavior #32601
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
ptrdom
wants to merge
1
commit into
akka:main
Choose a base branch
from
ptrdom:typed-persistence-runtime-journal-config
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
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
122 changes: 122 additions & 0 deletions
122
...stence-testkit/src/test/scala/akka/persistence/testkit/scaladsl/RuntimeJournalsSpec.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,122 @@ | ||
| /* | ||
| * Copyright (C) 2020-2024 Lightbend Inc. <https://www.lightbend.com> | ||
| */ | ||
|
|
||
| package akka.persistence.testkit.scaladsl | ||
|
|
||
| import akka.Done | ||
| import akka.actor.testkit.typed.scaladsl.LogCapturing | ||
| import akka.actor.testkit.typed.scaladsl.ScalaTestWithActorTestKit | ||
| import akka.actor.typed.ActorRef | ||
| import akka.actor.typed.Behavior | ||
| import akka.actor.typed.scaladsl.adapter._ | ||
| import akka.persistence.JournalProtocol.RecoverySuccess | ||
| import akka.persistence.JournalProtocol.ReplayMessages | ||
| import akka.persistence.JournalProtocol.ReplayedMessage | ||
| import akka.persistence.Persistence | ||
| import akka.persistence.SelectedSnapshot | ||
| import akka.persistence.SnapshotProtocol.LoadSnapshot | ||
| import akka.persistence.SnapshotProtocol.LoadSnapshotResult | ||
| import akka.persistence.SnapshotSelectionCriteria | ||
| import akka.persistence.testkit.PersistenceTestKitPlugin | ||
| import akka.persistence.testkit.PersistenceTestKitSnapshotPlugin | ||
| import akka.persistence.typed.PersistenceId | ||
| import akka.persistence.typed.scaladsl.Effect | ||
| import akka.persistence.typed.scaladsl.EventSourcedBehavior | ||
| import akka.persistence.typed.scaladsl.RetentionCriteria | ||
| import com.typesafe.config.ConfigFactory | ||
| import org.scalatest.Inside | ||
| import org.scalatest.wordspec.AnyWordSpecLike | ||
|
|
||
| object RuntimeJournalsSpec { | ||
|
|
||
| private object Actor { | ||
| sealed trait Command | ||
| case class Save(text: String, replyTo: ActorRef[Done]) extends Command | ||
| case class ShowMeWhatYouGot(replyTo: ActorRef[String]) extends Command | ||
| case object Stop extends Command | ||
|
|
||
| def apply(persistenceId: String, journal: String): Behavior[Command] = | ||
| EventSourcedBehavior[Command, String, String]( | ||
| PersistenceId.ofUniqueId(persistenceId), | ||
| "", | ||
| (state, cmd) => | ||
| cmd match { | ||
| case Save(text, replyTo) => | ||
| Effect.persist(text).thenRun(_ => replyTo ! Done) | ||
| case ShowMeWhatYouGot(replyTo) => | ||
| replyTo ! state | ||
| Effect.none | ||
| case Stop => | ||
| Effect.stop() | ||
| }, | ||
| (state, evt) => Seq(state, evt).filter(_.nonEmpty).mkString("|")) | ||
| .withRetention(RetentionCriteria.snapshotEvery(1, Int.MaxValue)) | ||
| .withJournalPluginId(s"$journal.journal") | ||
| .withJournalPluginConfig(Some(config(journal))) | ||
| .withSnapshotPluginId(s"$journal.snapshot") | ||
| .withSnapshotPluginConfig(Some(config(journal))) | ||
|
|
||
| } | ||
|
|
||
| private def config(journal: String) = { | ||
| ConfigFactory.parseString(s""" | ||
| $journal { | ||
| journal.class = "${classOf[PersistenceTestKitPlugin].getName}" | ||
| snapshot.class = "${classOf[PersistenceTestKitSnapshotPlugin].getName}" | ||
| } | ||
| """) | ||
| } | ||
| } | ||
|
|
||
| class RuntimeJournalsSpec extends ScalaTestWithActorTestKit with AnyWordSpecLike with LogCapturing with Inside { | ||
|
|
||
| import RuntimeJournalsSpec._ | ||
|
|
||
| "The testkit journal and snapshot store plugins" must { | ||
|
|
||
| "be possible to configure at runtime and use in multiple isolated instances" in { | ||
| val probe = createTestProbe[Any]() | ||
|
|
||
| { | ||
| // one actor in each journal with same id | ||
| val j1 = spawn(Actor("id1", "journal1")) | ||
| val j2 = spawn(Actor("id1", "journal2")) | ||
| j1 ! Actor.Save("j1m1", probe.ref) | ||
| probe.receiveMessage() | ||
| j2 ! Actor.Save("j2m1", probe.ref) | ||
| probe.receiveMessage() | ||
| } | ||
|
|
||
| { | ||
| def assertJournal(journal: String, expectedEvent: String) = { | ||
| val ref = Persistence(system).journalFor(s"$journal.journal", config(journal)) | ||
| ref.tell(ReplayMessages(0, Long.MaxValue, Long.MaxValue, "id1", probe.ref.toClassic), probe.ref.toClassic) | ||
| inside(probe.receiveMessage()) { | ||
| case ReplayedMessage(persistentRepr) => | ||
| persistentRepr.persistenceId shouldBe "id1" | ||
| persistentRepr.payload shouldBe expectedEvent | ||
| } | ||
| probe.expectMessage(RecoverySuccess(1)) | ||
| } | ||
|
|
||
| assertJournal("journal1", "j1m1") | ||
| assertJournal("journal2", "j2m1") | ||
| } | ||
|
|
||
| { | ||
| def assertSnapshot(journal: String, expectedShapshot: String) = { | ||
| val ref = Persistence(system).snapshotStoreFor(s"$journal.snapshot", config(journal)) | ||
| ref.tell(LoadSnapshot("id1", SnapshotSelectionCriteria.Latest, Long.MaxValue), probe.ref.toClassic) | ||
| inside(probe.receiveMessage()) { | ||
| case LoadSnapshotResult(Some(SelectedSnapshot(_, snapshot)), _) => | ||
| snapshot shouldBe expectedShapshot | ||
| } | ||
| } | ||
|
|
||
| assertSnapshot("journal1", "j1m1") | ||
| assertSnapshot("journal2", "j2m1") | ||
| } | ||
| } | ||
| } | ||
| } |
2 changes: 2 additions & 0 deletions
2
...tence-typed/src/main/mima-filters/2.10.x.backwards.excludes/eventsourcedbehavior.excludes
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,2 @@ | ||
| ProblemFilters.exclude[ReversedMissingMethodProblem]("akka.persistence.typed.scaladsl.EventSourcedBehavior.withJournalPluginConfig") | ||
| ProblemFilters.exclude[ReversedMissingMethodProblem]("akka.persistence.typed.scaladsl.EventSourcedBehavior.withSnapshotPluginConfig") |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this PR, I agree that it would be a nice feature. Before merging I would like to see that we can support it all the way in the r2dbc plugin, which currently isn't using that config. I recall we had problems and gave up in the Cassandra plugin akka/akka-persistence-cassandra#694
It's not only about the journal and snapshot. It should work with the queries too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is the PR for Pekko I was planning to port to Akka too - apache/pekko-projection#225. The r2dbc plugin would just piggyback off that, IIUC. Is this fine?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aha, I think I finally understood what you had in mind with potential issues - my comment about projections was not relevant for that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My initial thinking is that journal plugin should simply not use the query plugin - the code doing the replay could be shared between journal and query plugins without the need for the former to depend on latter. @patriknw Does that make sense?