Persistence: replace initialState with emptyState (#25129)

This commit is contained in:
Richard Imaoka 2018-05-25 21:29:22 +09:00 committed by Arnout Engelen
parent 8eb7b1ea81
commit 84d53d1ad1
19 changed files with 37 additions and 37 deletions

View file

@ -45,7 +45,7 @@ object ClusterShardingPersistenceSpec {
def persistentActor(entityId: String): Behavior[Command] = def persistentActor(entityId: String): Behavior[Command] =
PersistentBehaviors.receive[Command, String, String]( PersistentBehaviors.receive[Command, String, String](
entityId, entityId,
initialState = "", emptyState = "",
commandHandler = (_, state, cmd) cmd match { commandHandler = (_, state, cmd) cmd match {
case Add(s) Effect.persist(s) case Add(s) Effect.persist(s)
case Get(replyTo) case Get(replyTo)

View file

@ -36,7 +36,7 @@ object ClusterSingletonPersistenceSpec {
val persistentActor: Behavior[Command] = val persistentActor: Behavior[Command] =
PersistentBehaviors.receive[Command, String, String]( PersistentBehaviors.receive[Command, String, String](
persistenceId = "TheSingleton", persistenceId = "TheSingleton",
initialState = "", emptyState = "",
commandHandler = (_, state, cmd) cmd match { commandHandler = (_, state, cmd) cmd match {
case Add(s) Effect.persist(s) case Add(s) Effect.persist(s)
case Get(replyTo) case Get(replyTo)

View file

@ -42,7 +42,7 @@ The event and state are only used internally.
The components that make up a PersistentBehavior are: The components that make up a PersistentBehavior are:
* `persistenceId` is the unique identifier for the persistent actor. * `persistenceId` is the unique identifier for the persistent actor.
* `initialState` defines the `State` when the entity is first created e.g. a Counter would start with 0 as state. * `emptyState` defines the `State` when the entity is first created e.g. a Counter would start with 0 as state.
* `commandHandler` defines how to handle command, resulting in Effects e.g. persisting events, stopping the persistent actor. * `commandHandler` defines how to handle command, resulting in Effects e.g. persisting events, stopping the persistent actor.
* `eventHandler` updates the current state when an event has been persisted. * `eventHandler` updates the current state when an event has been persisted.

View file

@ -20,7 +20,7 @@ import akka.persistence.typed.internal.EventsourcedBehavior._
* *
* In this behavior the recovery process is initiated. * In this behavior the recovery process is initiated.
* We try to obtain a snapshot from the configured snapshot store, * We try to obtain a snapshot from the configured snapshot store,
* and if it exists, we use it instead of the `initialState`. * and if it exists, we use it instead of the initial `emptyState`.
* *
* Once snapshot recovery is done (or no snapshot was selected), * Once snapshot recovery is done (or no snapshot was selected),
* recovery of events continues in [[EventsourcedReplayingEvents]]. * recovery of events continues in [[EventsourcedReplayingEvents]].
@ -96,7 +96,7 @@ private[akka] class EventsourcedReplayingSnapshot[C, E, S](override val setup: E
def onSnapshotterResponse(response: SnapshotProtocol.Response): Behavior[InternalProtocol] = { def onSnapshotterResponse(response: SnapshotProtocol.Response): Behavior[InternalProtocol] = {
response match { response match {
case LoadSnapshotResult(sso, toSnr) case LoadSnapshotResult(sso, toSnr)
var state: S = setup.initialState var state: S = setup.emptyState
val seqNr: Long = sso match { val seqNr: Long = sso match {
case Some(SelectedSnapshot(metadata, snapshot)) case Some(SelectedSnapshot(metadata, snapshot))

View file

@ -27,7 +27,7 @@ private[persistence] final class EventsourcedSetup[C, E, S](
val context: ActorContext[InternalProtocol], val context: ActorContext[InternalProtocol],
val timers: TimerScheduler[InternalProtocol], val timers: TimerScheduler[InternalProtocol],
val persistenceId: String, val persistenceId: String,
val initialState: S, val emptyState: S,
val commandHandler: PersistentBehaviors.CommandHandler[C, E, S], val commandHandler: PersistentBehaviors.CommandHandler[C, E, S],
val eventHandler: (S, E) S, val eventHandler: (S, E) S,
val writerIdentity: WriterIdentity, val writerIdentity: WriterIdentity,

View file

@ -33,7 +33,7 @@ private[akka] object PersistentBehaviorImpl {
@InternalApi @InternalApi
private[akka] final case class PersistentBehaviorImpl[Command, Event, State]( private[akka] final case class PersistentBehaviorImpl[Command, Event, State](
persistenceId: String, persistenceId: String,
initialState: State, emptyState: State,
commandHandler: PersistentBehaviors.CommandHandler[Command, Event, State], commandHandler: PersistentBehaviors.CommandHandler[Command, Event, State],
eventHandler: (State, Event) State, eventHandler: (State, Event) State,
journalPluginId: Option[String] = None, journalPluginId: Option[String] = None,
@ -59,7 +59,7 @@ private[akka] final case class PersistentBehaviorImpl[Command, Event, State](
ctx, ctx,
timers, timers,
persistenceId, persistenceId,
initialState, emptyState,
commandHandler, commandHandler,
eventHandler, eventHandler,
WriterIdentity.newIdentity(), WriterIdentity.newIdentity(),

View file

@ -29,12 +29,12 @@ abstract class PersistentBehavior[Command, Event, State >: Null](val persistence
protected final def Effect: EffectFactories[Command, Event, State] = EffectFactory.asInstanceOf[EffectFactories[Command, Event, State]] protected final def Effect: EffectFactories[Command, Event, State] = EffectFactory.asInstanceOf[EffectFactories[Command, Event, State]]
/** /**
* Implement by returning the initial state object. * Implement by returning the initial empty state object.
* This object will be passed into this behaviors handlers, until a new state replaces it. * This object will be passed into this behaviors handlers, until a new state replaces it.
* *
* Also known as "zero state" or "neutral state". * Also known as "zero state" or "neutral state".
*/ */
protected def initialState: State protected def emptyState: State
/** /**
* Implement by handling incoming commands and return an `Effect()` to persist or signal other effects * Implement by handling incoming commands and return an `Effect()` to persist or signal other effects
@ -147,7 +147,7 @@ abstract class PersistentBehavior[Command, Event, State >: Null](val persistence
scaladsl.PersistentBehaviors.receive[Command, Event, State]( scaladsl.PersistentBehaviors.receive[Command, Event, State](
persistenceId, persistenceId,
initialState, emptyState,
(c, state, cmd) commandHandler()(c.asJava, state, cmd).asInstanceOf[EffectImpl[Event, State]], (c, state, cmd) commandHandler()(c.asJava, state, cmd).asInstanceOf[EffectImpl[Event, State]],
eventHandler()(_, _)) eventHandler()(_, _))
.onRecoveryCompleted((ctx, state) onRecoveryCompleted(ctx.asJava, state)) .onRecoveryCompleted((ctx, state) onRecoveryCompleted(ctx.asJava, state))

View file

@ -24,10 +24,10 @@ object PersistentBehaviors {
*/ */
def receive[Command, Event, State]( def receive[Command, Event, State](
persistenceId: String, persistenceId: String,
initialState: State, emptyState: State,
commandHandler: CommandHandler[Command, Event, State], commandHandler: CommandHandler[Command, Event, State],
eventHandler: (State, Event) State): PersistentBehavior[Command, Event, State] = eventHandler: (State, Event) State): PersistentBehavior[Command, Event, State] =
PersistentBehaviorImpl(persistenceId, initialState, commandHandler, eventHandler) PersistentBehaviorImpl(persistenceId, emptyState, commandHandler, eventHandler)
/** /**
* The `CommandHandler` defines how to act on commands. * The `CommandHandler` defines how to act on commands.

View file

@ -89,7 +89,7 @@ public class PersistentActorCompileOnlyTest {
//#behavior //#behavior
public static PersistentBehavior<SimpleCommand, SimpleEvent, SimpleState> pb = new PersistentBehavior<SimpleCommand, SimpleEvent, SimpleState>("p1") { public static PersistentBehavior<SimpleCommand, SimpleEvent, SimpleState> pb = new PersistentBehavior<SimpleCommand, SimpleEvent, SimpleState>("p1") {
@Override @Override
public SimpleState initialState() { public SimpleState emptyState() {
return new SimpleState(); return new SimpleState();
} }
@ -150,7 +150,7 @@ public class PersistentActorCompileOnlyTest {
private PersistentBehavior<MyCommand, MyEvent, ExampleState> pa = new PersistentBehavior<MyCommand, MyEvent, ExampleState>("pa") { private PersistentBehavior<MyCommand, MyEvent, ExampleState> pa = new PersistentBehavior<MyCommand, MyEvent, ExampleState>("pa") {
@Override @Override
public ExampleState initialState() { public ExampleState emptyState() {
return new ExampleState(); return new ExampleState();
} }
@ -259,7 +259,7 @@ public class PersistentActorCompileOnlyTest {
} }
@Override @Override
public EventsInFlight initialState() { public EventsInFlight emptyState() {
return new EventsInFlight(0, Collections.emptyMap()); return new EventsInFlight(0, Collections.emptyMap());
} }

View file

@ -299,7 +299,7 @@ public class PersistentActorJavaDslTest extends JUnitSuite {
} }
@Override @Override
public State initialState() { public State emptyState() {
return emptyState; return emptyState;
} }

View file

@ -29,7 +29,7 @@ public class BasicPersistentBehaviorsTest {
} }
@Override @Override
public State initialState() { public State emptyState() {
return new State(); return new State();
} }

View file

@ -170,7 +170,7 @@ public class InDepthPersistentBehaviorTest {
} }
@Override @Override
public BlogState initialState() { public BlogState emptyState() {
return new BlogState(Optional.empty(), false); return new BlogState(Optional.empty(), false);
} }

View file

@ -29,7 +29,7 @@ object ManyRecoveriesSpec {
latch: Option[TestLatch]): PersistentBehavior[Cmd, Evt, String] = latch: Option[TestLatch]): PersistentBehavior[Cmd, Evt, String] =
PersistentBehaviors.receive[Cmd, Evt, String]( PersistentBehaviors.receive[Cmd, Evt, String](
persistenceId = name, persistenceId = name,
initialState = "", emptyState = "",
commandHandler = CommandHandler.command { commandHandler = CommandHandler.command {
case Cmd(s) Effect.persist(Evt(s)).andThen(_ probe.ref ! s"$name-$s") case Cmd(s) Effect.persist(Evt(s)).andThen(_ probe.ref ! s"$name-$s")
}, },

View file

@ -44,7 +44,7 @@ object RecoveryPermitterSpec {
throwOnRecovery: Boolean = false): Behavior[Command] = throwOnRecovery: Boolean = false): Behavior[Command] =
PersistentBehaviors.receive[Command, Event, State]( PersistentBehaviors.receive[Command, Event, State](
persistenceId = name, persistenceId = name,
initialState = EmptyState, emptyState = EmptyState,
commandHandler = CommandHandler.command { commandHandler = CommandHandler.command {
case StopActor Effect.stop case StopActor Effect.stop
case command commandProbe.ref ! command; Effect.none case command commandProbe.ref ! command; Effect.none

View file

@ -29,7 +29,7 @@ object OptionalSnapshotStoreSpec {
name: String = UUID.randomUUID().toString) = name: String = UUID.randomUUID().toString) =
PersistentBehaviors.receive[Command, Event, State]( PersistentBehaviors.receive[Command, Event, State](
persistenceId = name, persistenceId = name,
initialState = State(), emptyState = State(),
commandHandler = CommandHandler.command { commandHandler = CommandHandler.command {
_ Effect.persist(Event()).andThen(probe.ref ! _) _ Effect.persist(Event()).andThen(probe.ref ! _)
}, },

View file

@ -45,7 +45,7 @@ object PersistentActorCompileOnlyTest {
val simpleBehavior: PersistentBehavior[SimpleCommand, SimpleEvent, ExampleState] = val simpleBehavior: PersistentBehavior[SimpleCommand, SimpleEvent, ExampleState] =
PersistentBehaviors.receive[SimpleCommand, SimpleEvent, ExampleState]( PersistentBehaviors.receive[SimpleCommand, SimpleEvent, ExampleState](
persistenceId = "sample-id-1", persistenceId = "sample-id-1",
initialState = ExampleState(Nil), emptyState = ExampleState(Nil),
commandHandler = commandHandler, commandHandler = commandHandler,
eventHandler = eventHandler) eventHandler = eventHandler)
//#behavior //#behavior
@ -66,7 +66,7 @@ object PersistentActorCompileOnlyTest {
PersistentBehaviors.receive[MyCommand, MyEvent, ExampleState]( PersistentBehaviors.receive[MyCommand, MyEvent, ExampleState](
persistenceId = "sample-id-1", persistenceId = "sample-id-1",
initialState = ExampleState(Nil), emptyState = ExampleState(Nil),
commandHandler = CommandHandler.command { commandHandler = CommandHandler.command {
case Cmd(data, sender) case Cmd(data, sender)
@ -110,7 +110,7 @@ object PersistentActorCompileOnlyTest {
PersistentBehaviors.receive[Command, Event, EventsInFlight]( PersistentBehaviors.receive[Command, Event, EventsInFlight](
persistenceId = "recovery-complete-id", persistenceId = "recovery-complete-id",
initialState = EventsInFlight(0, Map.empty), emptyState = EventsInFlight(0, Map.empty),
commandHandler = (ctx: ActorContext[Command], state, cmd) cmd match { commandHandler = (ctx: ActorContext[Command], state, cmd) cmd match {
case DoSideEffect(data) case DoSideEffect(data)
@ -151,7 +151,7 @@ object PersistentActorCompileOnlyTest {
val b: Behavior[Command] = PersistentBehaviors.receive[Command, Event, Mood]( val b: Behavior[Command] = PersistentBehaviors.receive[Command, Event, Mood](
persistenceId = "myPersistenceId", persistenceId = "myPersistenceId",
initialState = Happy, emptyState = Happy,
commandHandler = CommandHandler.byState { commandHandler = CommandHandler.byState {
case Happy CommandHandler.command { case Happy CommandHandler.command {
case Greet(whom) case Greet(whom)
@ -192,7 +192,7 @@ object PersistentActorCompileOnlyTest {
PersistentBehaviors.receive[Command, Event, State]( PersistentBehaviors.receive[Command, Event, State](
persistenceId = "asdf", persistenceId = "asdf",
initialState = State(Nil), emptyState = State(Nil),
commandHandler = CommandHandler.command { commandHandler = CommandHandler.command {
case RegisterTask(task) Effect.persist(TaskRegistered(task)) case RegisterTask(task) Effect.persist(TaskRegistered(task))
case TaskDone(task) Effect.persist(TaskRemoved(task)) case TaskDone(task) Effect.persist(TaskRemoved(task))
@ -219,7 +219,7 @@ object PersistentActorCompileOnlyTest {
PersistentBehaviors.receive[Command, Event, State]( PersistentBehaviors.receive[Command, Event, State](
persistenceId = "asdf", persistenceId = "asdf",
initialState = State(Nil), emptyState = State(Nil),
commandHandler = (ctx, _, cmd) cmd match { commandHandler = (ctx, _, cmd) cmd match {
case RegisterTask(task) case RegisterTask(task)
Effect.persist(TaskRegistered(task)) Effect.persist(TaskRegistered(task))
@ -282,7 +282,7 @@ object PersistentActorCompileOnlyTest {
PersistentBehaviors.receive[Command, Event, List[Id]]( PersistentBehaviors.receive[Command, Event, List[Id]](
persistenceId = "basket-1", persistenceId = "basket-1",
initialState = Nil, emptyState = Nil,
commandHandler = commandHandler =
CommandHandler.byState(state CommandHandler.byState(state
if (isFullyHydrated(basket, state)) (ctx, state, cmd) if (isFullyHydrated(basket, state)) (ctx, state, cmd)
@ -343,7 +343,7 @@ object PersistentActorCompileOnlyTest {
PersistentBehaviors.receive[Command, Event, Mood]( PersistentBehaviors.receive[Command, Event, Mood](
persistenceId = "myPersistenceId", persistenceId = "myPersistenceId",
initialState = Sad, emptyState = Sad,
commandHandler = (_, state, cmd) commandHandler = (_, state, cmd)
cmd match { cmd match {
case Greet(whom) case Greet(whom)
@ -379,7 +379,7 @@ object PersistentActorCompileOnlyTest {
PersistentBehaviors.receive[Command, Event, State]( PersistentBehaviors.receive[Command, Event, State](
persistenceId = "myPersistenceId", persistenceId = "myPersistenceId",
initialState = new State, emptyState = new State,
commandHandler = CommandHandler.command { commandHandler = CommandHandler.command {
case Enough case Enough
Effect.persist(Done) Effect.persist(Done)

View file

@ -109,7 +109,7 @@ object PersistentBehaviorSpec {
snapshotProbe: ActorRef[Try[Done]]): PersistentBehavior[Command, Event, State] = { snapshotProbe: ActorRef[Try[Done]]): PersistentBehavior[Command, Event, State] = {
PersistentBehaviors.receive[Command, Event, State]( PersistentBehaviors.receive[Command, Event, State](
persistenceId, persistenceId,
initialState = State(0, Vector.empty), emptyState = State(0, Vector.empty),
commandHandler = (ctx, state, cmd) cmd match { commandHandler = (ctx, state, cmd) cmd match {
case Increment case Increment
Effect.persist(Incremented(1)) Effect.persist(Incremented(1))

View file

@ -18,7 +18,7 @@ object BasicPersistentBehaviorsCompileOnly {
val behavior: Behavior[Command] = val behavior: Behavior[Command] =
PersistentBehaviors.receive[Command, Event, State]( PersistentBehaviors.receive[Command, Event, State](
persistenceId = "abc", persistenceId = "abc",
initialState = State(), emptyState = State(),
commandHandler = (ctx, state, cmd) ???, commandHandler = (ctx, state, cmd) ???,
eventHandler = (state, evt) ???) eventHandler = (state, evt) ???)
//#structure //#structure
@ -27,7 +27,7 @@ object BasicPersistentBehaviorsCompileOnly {
val recoveryBehavior: Behavior[Command] = val recoveryBehavior: Behavior[Command] =
PersistentBehaviors.receive[Command, Event, State]( PersistentBehaviors.receive[Command, Event, State](
persistenceId = "abc", persistenceId = "abc",
initialState = State(), emptyState = State(),
commandHandler = (ctx, state, cmd) ???, commandHandler = (ctx, state, cmd) ???,
eventHandler = (state, evt) ???) eventHandler = (state, evt) ???)
.onRecoveryCompleted { (ctx, state) .onRecoveryCompleted { (ctx, state)
@ -39,7 +39,7 @@ object BasicPersistentBehaviorsCompileOnly {
val taggingBehavior: Behavior[Command] = val taggingBehavior: Behavior[Command] =
PersistentBehaviors.receive[Command, Event, State]( PersistentBehaviors.receive[Command, Event, State](
persistenceId = "abc", persistenceId = "abc",
initialState = State(), emptyState = State(),
commandHandler = (ctx, state, cmd) ???, commandHandler = (ctx, state, cmd) ???,
eventHandler = (state, evt) ??? eventHandler = (state, evt) ???
).withTagger(_ Set("tag1", "tag2")) ).withTagger(_ Set("tag1", "tag2"))
@ -49,7 +49,7 @@ object BasicPersistentBehaviorsCompileOnly {
//#wrapPersistentBehavior //#wrapPersistentBehavior
val samplePersistentBehavior = PersistentBehaviors.receive[Command, Event, State]( val samplePersistentBehavior = PersistentBehaviors.receive[Command, Event, State](
persistenceId = "abc", persistenceId = "abc",
initialState = State(), emptyState = State(),
commandHandler = (ctx, state, cmd) ???, commandHandler = (ctx, state, cmd) ???,
eventHandler = (state, evt) ???) eventHandler = (state, evt) ???)
.onRecoveryCompleted { (ctx, state) .onRecoveryCompleted { (ctx, state)

View file

@ -121,7 +121,7 @@ object InDepthPersistentBehaviorSpec {
def behavior(entityId: String): Behavior[BlogCommand] = def behavior(entityId: String): Behavior[BlogCommand] =
PersistentBehaviors.receive[BlogCommand, BlogEvent, BlogState]( PersistentBehaviors.receive[BlogCommand, BlogEvent, BlogState](
persistenceId = "Blog-" + entityId, persistenceId = "Blog-" + entityId,
initialState = BlogState.empty, emptyState = BlogState.empty,
commandHandler, commandHandler,
eventHandler) eventHandler)
//#behavior //#behavior