-
Notifications
You must be signed in to change notification settings - Fork 3.6k
feat: Recovery based on only the last event #32629
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a0954dd
feat: Recovery base on only the last event
patriknw af67208
fixme for missing part
patriknw 757601b
impl and test
patriknw a210fff
revert some in classic persistence
patriknw 0f23ab5
some more tck tests
patriknw 50abc88
docs
patriknw 972ff90
warn
patriknw 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
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
101 changes: 101 additions & 0 deletions
101
...s/src/test/scala/akka/persistence/typed/scaladsl/EventSourcedBehaviorReplayLastSpec.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,101 @@ | ||
| /* | ||
| * Copyright (C) 2017-2024 Lightbend Inc. <https://www.lightbend.com> | ||
| */ | ||
|
|
||
| package akka.persistence.typed.scaladsl | ||
|
|
||
| import java.util.concurrent.atomic.AtomicInteger | ||
|
|
||
| import scala.annotation.nowarn | ||
|
|
||
| import org.scalatest.matchers.should.Matchers | ||
| import org.scalatest.wordspec.AnyWordSpecLike | ||
|
|
||
| import akka.actor.testkit.typed.scaladsl._ | ||
| import akka.actor.typed.ActorRef | ||
| import akka.actor.typed.scaladsl.ActorContext | ||
| import akka.actor.typed.scaladsl.Behaviors | ||
| import akka.persistence.testkit.PersistenceTestKitPlugin | ||
| import akka.persistence.testkit.PersistenceTestKitSnapshotPlugin | ||
| import akka.persistence.typed.PersistenceId | ||
| import akka.serialization.jackson.CborSerializable | ||
|
|
||
| object EventSourcedBehaviorReplayLastSpec extends Matchers { | ||
|
|
||
| sealed trait Command extends CborSerializable | ||
| final case class Increment(id: String) extends Command | ||
| final case class GetValue(replyTo: ActorRef[State]) extends Command | ||
|
|
||
| sealed trait Event extends CborSerializable | ||
| final case class Incremented(id: String) extends Event | ||
|
|
||
| final case class State(value: Int, history: Vector[String]) extends CborSerializable | ||
|
|
||
| def counter( | ||
| @nowarn("msg=never used") ctx: ActorContext[Command], | ||
| persistenceId: PersistenceId, | ||
| probe: Option[ActorRef[(State, Event)]] = None): EventSourcedBehavior[Command, Event, State] = { | ||
| EventSourcedBehavior[Command, Event, State]( | ||
| persistenceId, | ||
| emptyState = State(0, Vector.empty), | ||
| commandHandler = (state, cmd) => | ||
| cmd match { | ||
| case Increment(id) => | ||
| Effect.persist(Incremented(id)) | ||
|
|
||
| case GetValue(replyTo) => | ||
| replyTo ! state | ||
| Effect.none | ||
|
|
||
| }, | ||
| eventHandler = (state, evt) => | ||
| evt match { | ||
| case Incremented(id) => | ||
| probe.foreach(_ ! ((state, evt))) | ||
| State(state.value + 1, state.history :+ id) | ||
| }) | ||
| }.withRecovery(Recovery.replayOnlyLast) | ||
|
|
||
| } | ||
|
|
||
| class EventSourcedBehaviorReplayLastSpec | ||
| extends ScalaTestWithActorTestKit( | ||
| PersistenceTestKitPlugin.config.withFallback(PersistenceTestKitSnapshotPlugin.config)) | ||
| with AnyWordSpecLike | ||
| with LogCapturing { | ||
|
|
||
| import EventSourcedBehaviorReplayLastSpec._ | ||
|
|
||
| val pidCounter = new AtomicInteger(0) | ||
| private def nextPid(): PersistenceId = PersistenceId.ofUniqueId(s"c${pidCounter.incrementAndGet()}") | ||
|
|
||
| "EventSourcedBehavior with Recovery.replayOnlyLast" must { | ||
| "recover from last event only" in { | ||
| val pid = nextPid() | ||
| val c = spawn(Behaviors.setup[Command](ctx => counter(ctx, pid))) | ||
| val replyProbe = TestProbe[State]() | ||
|
|
||
| c ! Increment("a") | ||
| c ! Increment("b") | ||
| c ! Increment("c") | ||
| c ! GetValue(replyProbe.ref) | ||
| replyProbe.expectMessage(State(3, Vector("a", "b", "c"))) | ||
| testKit.stop(c) | ||
| replyProbe.expectTerminated(c) | ||
|
|
||
| val probe = TestProbe[(State, Event)]() | ||
| val c2 = spawn(Behaviors.setup[Command](ctx => counter(ctx, pid, Some(probe.ref)))) | ||
| // replayed the last event | ||
| probe.expectMessage((State(0, Vector.empty), Incremented("c"))) | ||
| probe.expectNoMessage() | ||
|
|
||
| c2 ! GetValue(replyProbe.ref) | ||
| replyProbe.expectMessage(State(1, Vector("c"))) | ||
|
|
||
| c2 ! Increment("d") | ||
| c2 ! GetValue(replyProbe.ref) | ||
| replyProbe.expectMessage(State(2, Vector("c", "d"))) | ||
| } | ||
|
|
||
| } | ||
| } |
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.