-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
134 additions
and
1 deletion.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
...persistence-r2dbc-lagom-api/src/main/scala/ClusterComponentsR2dbcPersistenceService.scala
This file contains 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,21 @@ | ||
package net.sc8s.akka.components.persistence.r2dbc.lagom.api | ||
|
||
import akka.{Done, NotUsed} | ||
import com.lightbend.lagom.scaladsl.api.transport.Method | ||
import com.lightbend.lagom.scaladsl.api.{Service, ServiceCall} | ||
|
||
trait ClusterComponentsR2dbcPersistenceService extends Service { | ||
val apiPrefix: String | ||
|
||
def deleteSingletonEntity(name: String): ServiceCall[NotUsed, Done] | ||
|
||
def deleteShardedEntities(name: String): ServiceCall[NotUsed, Done] | ||
|
||
abstract override def descriptor = { | ||
import Service._ | ||
super.descriptor.addCalls( | ||
restCall(Method.DELETE, s"$apiPrefix/entity/singleton/:name", deleteSingletonEntity _), | ||
restCall(Method.DELETE, s"$apiPrefix/entity/sharded/:name", deleteShardedEntities _), | ||
) | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
...persistence-r2dbc-lagom/src/main/scala/ClusterComponentsR2dbcPersistenceServiceImpl.scala
This file contains 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,83 @@ | ||
package net.sc8s.akka.components.persistence.cassandra.lagom | ||
|
||
import akka.Done | ||
import akka.actor.typed.ActorSystem | ||
import akka.persistence.query.PersistenceQuery | ||
import akka.persistence.query.scaladsl.CurrentPersistenceIdsQuery | ||
import akka.persistence.r2dbc.cleanup.scaladsl.EventSourcedCleanup | ||
import akka.persistence.r2dbc.query.scaladsl.R2dbcReadJournal | ||
import akka.stream.Materializer.matFromSystem | ||
import akka.stream.scaladsl.Sink | ||
import com.lightbend.lagom.scaladsl.api.ServiceCall | ||
import net.sc8s.akka.circe.CirceSerializer | ||
import net.sc8s.akka.components.ClusterComponent | ||
import net.sc8s.akka.components.persistence.r2dbc.lagom.api.ClusterComponentsR2dbcPersistenceService | ||
import net.sc8s.logstage.elastic.Logging | ||
|
||
import scala.util.Success | ||
|
||
trait ClusterComponentsR2dbcPersistenceServiceImpl extends ClusterComponentsR2dbcPersistenceService with Logging { | ||
val clusterComponents: Set[ClusterComponent.Component[_]] | ||
|
||
implicit val actorSystem: ActorSystem[_] | ||
|
||
import actorSystem.executionContext | ||
|
||
val queries = PersistenceQuery(actorSystem).readJournalFor[CurrentPersistenceIdsQuery](R2dbcReadJournal.Identifier) | ||
val cleanup = new EventSourcedCleanup(actorSystem) | ||
|
||
override def deleteSingletonEntity(name: String) = ServiceCall { _ => | ||
val maybeSingletonPersistenceId = clusterComponents | ||
.map(wiredComponent => wiredComponent.component -> wiredComponent.innerComponent) | ||
.collectFirst { case (outerComponent: ClusterComponent.Singleton.EventSourced, innerComponent) if outerComponent.name == name => | ||
innerComponent.asInstanceOf[outerComponent.BaseComponent].persistenceId | ||
} | ||
|
||
lazy val singletonEntities = clusterComponents.map(_.component).collect { | ||
case outerComponent: ClusterComponent.Singleton.EventSourced => outerComponent.name | ||
} | ||
|
||
maybeSingletonPersistenceId.fold( | ||
throw new Exception(s"singleton with name=$name not found, existing singletonEntities=$singletonEntities") | ||
) { singletonPersistenceId => | ||
log.infoT("deleteSingletonEntity", s"$name") | ||
cleanup | ||
.deleteAll(singletonPersistenceId.id, resetSequenceNumber = false) | ||
.andThen { | ||
case Success(_) => | ||
log.infoT("singletonEntityDeleted", s"$name") | ||
} | ||
} | ||
} | ||
|
||
override def deleteShardedEntities(name: String) = ServiceCall { _ => | ||
val maybeTypeKey = clusterComponents | ||
.map(wiredComponent => wiredComponent.component -> wiredComponent.innerComponent) | ||
.collectFirst { case (outerComponent: ClusterComponent.Sharded.EventSourced, innerComponent) if outerComponent.name == name => | ||
innerComponent.asInstanceOf[outerComponent.BaseComponent].typeKey | ||
} | ||
|
||
lazy val shardedEntities = clusterComponents.map(_.component).collect { | ||
case outerComponent: ClusterComponent.Sharded.EventSourced => outerComponent.name | ||
} | ||
|
||
maybeTypeKey.fold( | ||
throw new Exception(s"shardedEntity with name=$name not found, existing shardedEntities=$shardedEntities") | ||
) { typeKey => | ||
log.infoT("deleteShardedEntities", s"$name") | ||
|
||
queries | ||
.currentPersistenceIds() | ||
.filter(_.startsWith(s"${typeKey.name}|")) | ||
.mapAsync(10) { id => | ||
log.infoT("deleteShardedEntity", s"$id") | ||
cleanup.deleteAll(id, resetSequenceNumber = false) | ||
} | ||
.runWith(Sink.fold(0)((i, _) => i + 1)) | ||
.map { deletedEntities => | ||
log.infoT("shardedEntitiesDeleted", s"$name with $deletedEntities") | ||
Done | ||
} | ||
} | ||
} | ||
} |
This file contains 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 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