-
Notifications
You must be signed in to change notification settings - Fork 199
Runtime plugin configuration for DurableState #3058
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.pekko.persistence.typed.state.scaladsl | ||
|
|
||
| import org.apache.pekko | ||
| import pekko.Done | ||
| import pekko.actor.testkit.typed.scaladsl.LogCapturing | ||
| import pekko.actor.testkit.typed.scaladsl.ScalaTestWithActorTestKit | ||
| import pekko.actor.typed.ActorRef | ||
| import pekko.actor.typed.Behavior | ||
| import pekko.persistence.state.DurableStateStoreRegistry | ||
| import pekko.persistence.state.scaladsl.DurableStateUpdateStore | ||
| import pekko.persistence.testkit.state.PersistenceTestKitDurableStateStoreProvider | ||
| import pekko.persistence.typed.PersistenceId | ||
|
|
||
| import com.typesafe.config.Config | ||
| import com.typesafe.config.ConfigFactory | ||
| import org.scalatest.wordspec.AnyWordSpecLike | ||
|
|
||
| object RuntimeDurableStateStoreSpec { | ||
|
|
||
| private object Actor { | ||
| sealed trait Command | ||
| final case class Save(text: String, replyTo: ActorRef[Done]) extends Command | ||
| final case class ShowMeWhatYouGot(replyTo: ActorRef[String]) extends Command | ||
| case object Stop extends Command | ||
|
|
||
| def apply(persistenceId: String, store: String): Behavior[Command] = | ||
| DurableStateBehavior[Command, String]( | ||
| PersistenceId.ofUniqueId(persistenceId), | ||
| "", | ||
| (state, cmd) => | ||
| cmd match { | ||
| case Save(text, replyTo) => | ||
| Effect.persist(Seq(state, text).filter(_.nonEmpty).mkString("|")).thenRun(_ => replyTo ! Done) | ||
| case ShowMeWhatYouGot(replyTo) => | ||
| replyTo ! state | ||
| Effect.none | ||
| case Stop => | ||
| Effect.stop() | ||
| }) | ||
| .withDurableStateStorePluginId(s"$store.state") | ||
| .withDurableStateStorePluginConfig(Some(config(store))) | ||
| } | ||
|
|
||
| private def config(store: String): Config = | ||
| ConfigFactory.parseString(s""" | ||
| $store { | ||
| state.class = "${classOf[PersistenceTestKitDurableStateStoreProvider].getName}" | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Test only covers Scala API This spec exercises The Java DSL counterpart ( Per Suggested fix — add a Java DSL spec (e.g., |
||
| """) | ||
| } | ||
|
|
||
| class RuntimeDurableStateStoreSpec extends ScalaTestWithActorTestKit with AnyWordSpecLike with LogCapturing { | ||
|
|
||
| import RuntimeDurableStateStoreSpec._ | ||
|
|
||
| "The durable state store plugin" must { | ||
|
|
||
| "be possible to configure at runtime and use in multiple isolated instances" in { | ||
| val probe = createTestProbe[Any]() | ||
|
|
||
| { | ||
| // one actor in each store with same id | ||
| val s1 = spawn(Actor("id1", "store1")) | ||
| val s2 = spawn(Actor("id1", "store2")) | ||
| s1 ! Actor.Save("s1m1", probe.ref) | ||
| probe.receiveMessage() | ||
| s2 ! Actor.Save("s2m1", probe.ref) | ||
| probe.receiveMessage() | ||
| } | ||
|
|
||
| { | ||
| def assertStore(store: String, expectedState: String) = { | ||
| val durableStateStore = DurableStateStoreRegistry(system) | ||
| .durableStateStoreFor[DurableStateUpdateStore[String]](s"$store.state", config(store)) | ||
| durableStateStore.getObject("id1").futureValue.value shouldBe Some(expectedState) | ||
| } | ||
|
|
||
| assertStore("store1", "s1m1") | ||
| assertStore("store2", "s2m1") | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| # Add DurableStateBehavior.withDurableStateStorePluginConfig | ||
| ProblemFilters.exclude[ReversedMissingMethodProblem]("org.apache.pekko.persistence.typed.state.scaladsl.DurableStateBehavior.withDurableStateStorePluginConfig") |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,7 @@ import pekko.persistence.PluginProvider | |
| import pekko.persistence.state.scaladsl.DurableStateStore | ||
|
|
||
| import com.typesafe.config.Config | ||
| import com.typesafe.config.ConfigFactory | ||
|
|
||
| /** | ||
| * Persistence extension for queries. | ||
|
|
@@ -67,9 +68,12 @@ class DurableStateStoreRegistry(system: ExtendedActorSystem) | |
| configPath | ||
| } | ||
|
|
||
| private def pluginIdOrDefault(pluginId: String): String = { | ||
| private def pluginIdOrDefault(pluginId: String): String = | ||
| pluginIdOrDefault(pluginId, ConfigFactory.empty) | ||
|
|
||
| private def pluginIdOrDefault(pluginId: String, pluginConfig: Config): String = { | ||
| val configPath = if (isEmpty(pluginId)) defaultPluginId else pluginId | ||
| Persistence.verifyPluginConfigExists(systemConfig, configPath, "DurableStateStore") | ||
| Persistence.verifyPluginConfigExists(pluginConfig.withFallback(systemConfig), configPath, "DurableStateStore") | ||
| configPath | ||
| } | ||
|
|
||
|
|
@@ -91,6 +95,17 @@ class DurableStateStoreRegistry(system: ExtendedActorSystem) | |
| pluginFor(pluginIdOrDefault(pluginId), pluginConfig(pluginId)).scaladslPlugin.asInstanceOf[T] | ||
| } | ||
|
|
||
| /** | ||
| * Scala API: Returns the [[pekko.persistence.state.scaladsl.DurableStateStore]] specified by the given | ||
| * configuration entry. The provided `pluginConfig` is used to configure the plugin at runtime, taking | ||
| * precedence over the plugin configuration defined in the actor system configuration. | ||
| * | ||
| * @since 2.0.0 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Parameter name shadows private method The parameter Same concern applies to Suggested fix — rename the parameter to avoid the collision: final def durableStateStoreFor[T <: scaladsl.DurableStateStore[?]](pluginId: String, runtimeConfig: Config): T = {
pluginFor(pluginIdOrDefault(pluginId, runtimeConfig), runtimeConfig).scaladslPlugin.asInstanceOf[T]
} |
||
| */ | ||
| final def durableStateStoreFor[T <: scaladsl.DurableStateStore[?]](pluginId: String, pluginConfig: Config): T = { | ||
| pluginFor(pluginIdOrDefault(pluginId, pluginConfig), pluginConfig).scaladslPlugin.asInstanceOf[T] | ||
| } | ||
|
|
||
| /** | ||
| * Java API: Returns the [[pekko.persistence.state.javadsl.DurableStateStore]] specified by the given | ||
| * configuration entry. | ||
|
|
@@ -101,4 +116,18 @@ class DurableStateStoreRegistry(system: ExtendedActorSystem) | |
| pluginFor(pluginIdOrDefault(pluginId), pluginConfig(pluginId)).javadslPlugin.asInstanceOf[T] | ||
| } | ||
|
|
||
| /** | ||
| * Java API: Returns the [[pekko.persistence.state.javadsl.DurableStateStore]] specified by the given | ||
| * configuration entry. The provided `pluginConfig` is used to configure the plugin at runtime, taking | ||
| * precedence over the plugin configuration defined in the actor system configuration. | ||
| * | ||
| * @since 2.0.0 | ||
| */ | ||
| final def getDurableStateStoreFor[T <: javadsl.DurableStateStore[?]]( | ||
| @nowarn("msg=never used") clazz: Class[T], // FIXME generic Class could be problematic in Java | ||
| pluginId: String, | ||
| pluginConfig: Config): T = { | ||
| pluginFor(pluginIdOrDefault(pluginId, pluginConfig), pluginConfig).javadslPlugin.asInstanceOf[T] | ||
| } | ||
|
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.