Skip to content
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

refactor: Simplify ActorSystem layer #3123

Merged
merged 4 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@

package org.knora.webapi.core

import org.apache.pekko
import org.apache.pekko.actor.ActorSystem
import zio.*

object ActorSystemTest {

def layer(sys: pekko.actor.ActorSystem): ZLayer[Any, Nothing, ActorSystem] =
ZLayer.scoped(ZIO.succeed(new ActorSystem { override val system: pekko.actor.ActorSystem = sys }))
def layer(sys: ActorSystem): ZLayer[Any, Nothing, ActorSystem] = ZLayer.succeed(sys)
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ object LayersTest {
with DspIngestTestContainer
with SharedVolumes.Images

type CommonR0 = ActorSystem with AppConfigurationsTest with JwtService with SipiService with StringFormatter
type CommonR0 = pekko.actor.ActorSystem
with AppConfigurationsTest
with JwtService
with SipiService
with StringFormatter
type CommonR =
ApiRoutes
with AdminApiEndpoints
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import org.apache.http.impl.client.HttpClients
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager
import org.apache.http.util.EntityUtils
import org.apache.pekko
import org.apache.pekko.actor.ActorSystem
import spray.json.JsObject
import spray.json.*
import zio.*
Expand All @@ -33,7 +34,6 @@ import dsp.errors.AssertionException
import dsp.errors.BadRequestException
import dsp.errors.NotFoundException
import org.knora.webapi.config.AppConfig
import org.knora.webapi.core.ActorSystem
import org.knora.webapi.messages.store.sipimessages.SipiUploadResponse
import org.knora.webapi.messages.store.sipimessages.SipiUploadResponseJsonProtocol.*
import org.knora.webapi.messages.store.sipimessages.SipiUploadWithoutProcessingResponse
Expand Down Expand Up @@ -65,11 +65,10 @@ final case class FileToUpload(path: Path, mimeType: ContentType)
*/
final case class InputFile(fileToUpload: FileToUpload, width: Int, height: Int)

final case class TestClientService(config: AppConfig, httpClient: CloseableHttpClient, sys: pekko.actor.ActorSystem)
final case class TestClientService(config: AppConfig, httpClient: CloseableHttpClient)(implicit system: ActorSystem)
extends TriplestoreJsonProtocol
with RequestBuilding {

implicit val system: pekko.actor.ActorSystem = sys
implicit val executionContext: ExecutionContext = system.dispatchers.lookup(KnoraDispatchers.KnoraBlockingDispatcher)

case class TestClientTimeoutException(msg: String) extends Exception
Expand Down Expand Up @@ -356,7 +355,7 @@ object TestClientService {
/**
* Releases the httpClient, freeing all resources.
*/
private def release(httpClient: CloseableHttpClient)(implicit system: pekko.actor.ActorSystem) = ZIO.attemptBlocking {
private def release(httpClient: CloseableHttpClient)(implicit system: ActorSystem) = ZIO.attemptBlocking {
pekko.http.scaladsl.Http().shutdownAllConnectionPools()
httpClient.close()
}.tap(_ => ZIO.logDebug(">>> Release Test Client Service <<<")).orDie
Expand All @@ -366,8 +365,8 @@ object TestClientService {
for {
sys <- ZIO.service[ActorSystem]
config <- ZIO.service[AppConfig]
httpClient <- ZIO.acquireRelease(acquire)(release(_)(sys.system))
} yield TestClientService(config, httpClient, sys.system)
httpClient <- ZIO.acquireRelease(acquire)(release(_)(sys))
} yield TestClientService(config, httpClient)(sys)
}

}
40 changes: 10 additions & 30 deletions webapi/src/main/scala/org/knora/webapi/core/ActorSystem.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,27 @@

package org.knora.webapi.core

import org.apache.pekko
import org.apache.pekko.actor
import zio.*
import zio.macros.accessible

import scala.concurrent.ExecutionContext

import pekko.actor

@accessible
trait ActorSystem {
val system: pekko.actor.ActorSystem
}

object ActorSystem {

private def acquire(executionContext: ExecutionContext): URIO[Any, actor.ActorSystem] =
ZIO
.attempt(
pekko.actor.ActorSystem(
name = "webapi",
config = None,
classLoader = None,
defaultExecutionContext = Some(executionContext),
),
)
.attempt(actor.ActorSystem("webapi", None, None, Some(executionContext)))

Check warning on line 17 in webapi/src/main/scala/org/knora/webapi/core/ActorSystem.scala

View check run for this annotation

Codecov / codecov/patch

webapi/src/main/scala/org/knora/webapi/core/ActorSystem.scala#L17

Added line #L17 was not covered by tests
.zipLeft(ZIO.logInfo(">>> Acquire Actor System <<<"))
.orDie

private def release(system: pekko.actor.ActorSystem): URIO[Any, actor.Terminated] =
ZIO
.fromFuture(_ => system.terminate())
.zipLeft(ZIO.logInfo(">>> Release Actor System <<<"))
.orDie
private def release(system: actor.ActorSystem): URIO[Any, actor.Terminated] =
ZIO.fromFuture(_ => system.terminate()).zipLeft(ZIO.logInfo(">>> Release Actor System <<<")).orDie

Check warning on line 22 in webapi/src/main/scala/org/knora/webapi/core/ActorSystem.scala

View check run for this annotation

Codecov / codecov/patch

webapi/src/main/scala/org/knora/webapi/core/ActorSystem.scala#L22

Added line #L22 was not covered by tests

val layer: ZLayer[Any, Nothing, ActorSystem] =
ZLayer.scoped {
val layer: ZLayer[Any, Nothing, actor.ActorSystem] =
ZLayer.scoped(

Check warning on line 25 in webapi/src/main/scala/org/knora/webapi/core/ActorSystem.scala

View check run for this annotation

Codecov / codecov/patch

webapi/src/main/scala/org/knora/webapi/core/ActorSystem.scala#L25

Added line #L25 was not covered by tests
for {
context <- ZIO.executor.map(_.asExecutionContext)
actorSystem <- ZIO.acquireRelease(acquire(context))(release)
} yield new ActorSystem {
override val system: pekko.actor.ActorSystem = actorSystem
}
}
context <- ZIO.executor.map(_.asExecutionContext)
system <- ZIO.acquireRelease(acquire(context))(release)

Check warning on line 28 in webapi/src/main/scala/org/knora/webapi/core/ActorSystem.scala

View check run for this annotation

Codecov / codecov/patch

webapi/src/main/scala/org/knora/webapi/core/ActorSystem.scala#L27-L28

Added lines #L27 - L28 were not covered by tests
} yield system,
)
}
6 changes: 3 additions & 3 deletions webapi/src/main/scala/org/knora/webapi/core/AppRouter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@

object AppRouter {
val layer: ZLayer[
core.ActorSystem & CardinalityHandler & CardinalityService & ConstructResponseUtilV2 & MessageRelay &
pekko.actor.ActorSystem & CardinalityHandler & CardinalityService & ConstructResponseUtilV2 & MessageRelay &
OntologyCache & OntologyHelpers & OntologyRepo & PermissionUtilADM & ResourceUtilV2 & StandoffTagUtilV2,
Nothing,
AppRouter,
] =
ZLayer {
for {
as <- ZIO.service[core.ActorSystem]
as <- ZIO.service[pekko.actor.ActorSystem]

Check warning on line 42 in webapi/src/main/scala/org/knora/webapi/core/AppRouter.scala

View check run for this annotation

Codecov / codecov/patch

webapi/src/main/scala/org/knora/webapi/core/AppRouter.scala#L42

Added line #L42 was not covered by tests
messageRelay <- ZIO.service[MessageRelay]
runtime <-
ZIO.runtime[
CardinalityHandler & CardinalityService & ConstructResponseUtilV2 & OntologyCache & OntologyHelpers &
OntologyRepo & PermissionUtilADM & ResourceUtilV2 & StandoffTagUtilV2,
]
} yield new AppRouter {
implicit val system: org.apache.pekko.actor.ActorSystem = as.system
implicit val system: org.apache.pekko.actor.ActorSystem = as

val ref: ActorRef = system.actorOf(
Props(
Expand Down
7 changes: 4 additions & 3 deletions webapi/src/main/scala/org/knora/webapi/core/AppServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package org.knora.webapi.core

import org.apache.pekko.actor
import zio.*

import org.knora.webapi.config.AppConfig
Expand All @@ -26,7 +27,7 @@
state: State,
ts: TriplestoreService,
ru: RepositoryUpdater,
as: ActorSystem,
as: actor.ActorSystem,
ontologyCache: OntologyCache,
sipiService: SipiService,
hs: HttpServer,
Expand Down Expand Up @@ -136,7 +137,7 @@
object AppServer {

private type AppServerEnvironment =
State & TriplestoreService & RepositoryUpdater & ActorSystem & OntologyCache & SipiService & HttpServer & AppConfig
State & TriplestoreService & RepositoryUpdater & actor.ActorSystem & OntologyCache & SipiService & HttpServer & AppConfig

/**
* Initializes the AppServer instance with the required services
Expand All @@ -146,7 +147,7 @@
state <- ZIO.service[State]
ts <- ZIO.service[TriplestoreService]
ru <- ZIO.service[RepositoryUpdater]
as <- ZIO.service[ActorSystem]
as <- ZIO.service[actor.ActorSystem]

Check warning on line 150 in webapi/src/main/scala/org/knora/webapi/core/AppServer.scala

View check run for this annotation

Codecov / codecov/patch

webapi/src/main/scala/org/knora/webapi/core/AppServer.scala#L150

Added line #L150 was not covered by tests
oc <- ZIO.service[OntologyCache]
iiifs <- ZIO.service[SipiService]
hs <- ZIO.service[HttpServer]
Expand Down
10 changes: 4 additions & 6 deletions webapi/src/main/scala/org/knora/webapi/core/HttpServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@

package org.knora.webapi.core

import org.apache.pekko
import org.apache.pekko.actor.ActorSystem
import org.apache.pekko.http.scaladsl.Http
import zio.*

import org.knora.webapi.config.AppConfig
import org.knora.webapi.core
import org.knora.webapi.routing.ApiRoutes

import pekko.http.scaladsl.Http

/**
* The Akka based HTTP server
*/
Expand All @@ -25,11 +23,11 @@
val layer: ZLayer[ActorSystem & AppConfig & ApiRoutes, Nothing, HttpServer] =
ZLayer.scoped {
for {
as <- ZIO.service[core.ActorSystem]
as <- ZIO.service[ActorSystem]

Check warning on line 26 in webapi/src/main/scala/org/knora/webapi/core/HttpServer.scala

View check run for this annotation

Codecov / codecov/patch

webapi/src/main/scala/org/knora/webapi/core/HttpServer.scala#L26

Added line #L26 was not covered by tests
config <- ZIO.service[AppConfig]
apiRoutes <- ZIO.service[ApiRoutes]
binding <- {
implicit val system: pekko.actor.ActorSystem = as.system
implicit val system: ActorSystem = as

ZIO.acquireRelease {
ZIO
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package org.knora.webapi.core

import org.apache.pekko.actor.ActorSystem
import zio.ULayer
import zio.ZLayer

Expand Down Expand Up @@ -93,7 +94,7 @@ object LayersLive {
*/
val dspLayersLive: ULayer[DspEnvironmentLive] =
ZLayer.make[DspEnvironmentLive](
ActorSystem.layer,
org.knora.webapi.core.ActorSystem.layer,
AdminApiEndpoints.layer,
AdminApiRoutes.layer,
ApiRoutes.layer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

package org.knora.webapi.routing

import org.apache.pekko.actor
import org.apache.pekko.actor.ActorSystem
import org.apache.pekko.http.cors.scaladsl.CorsDirectives
import org.apache.pekko.http.cors.scaladsl.settings.CorsSettings
import org.apache.pekko.http.scaladsl.model.HttpMethods.*
Expand All @@ -15,7 +15,6 @@

import org.knora.webapi.config.AppConfig
import org.knora.webapi.core
import org.knora.webapi.core.ActorSystem
import org.knora.webapi.core.AppRouter
import org.knora.webapi.core.MessageRelay
import org.knora.webapi.http.directives.DSPApiDirectives
Expand Down Expand Up @@ -61,7 +60,7 @@
resourceInfoRoutes <- ZIO.service[ResourceInfoRoutes]
searchApiRoutes <- ZIO.service[SearchApiRoutes]
managementRoutes <- ZIO.service[ManagementRoutes]
routeData <- ZIO.succeed(KnoraRouteData(sys.system, router.ref, appConfig))
routeData <- ZIO.succeed(KnoraRouteData(sys, router.ref, appConfig))

Check warning on line 63 in webapi/src/main/scala/org/knora/webapi/routing/ApiRoutes.scala

View check run for this annotation

Codecov / codecov/patch

webapi/src/main/scala/org/knora/webapi/routing/ApiRoutes.scala#L63

Added line #L63 was not covered by tests
runtime <-
ZIO.runtime[
AppConfig & AuthorizationRestService & core.State & IriConverter & KnoraProjectRepo & MessageRelay & ProjectADMRestService & RestCardinalityService & RestResourceInfoService & routing.Authenticator & SearchApiRoutes & SearchResponderV2 & SipiService & StringFormatter & UserService & ValuesResponderV2,
Expand Down Expand Up @@ -98,7 +97,7 @@
) extends ApiRoutes
with AroundDirectives {

private implicit val system: actor.ActorSystem = routeData.system
private implicit val system: ActorSystem = routeData.system

Check warning on line 100 in webapi/src/main/scala/org/knora/webapi/routing/ApiRoutes.scala

View check run for this annotation

Codecov / codecov/patch

webapi/src/main/scala/org/knora/webapi/routing/ApiRoutes.scala#L100

Added line #L100 was not covered by tests

val routes: Route =
logDuration {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package org.knora.webapi.slice.common.api

import org.apache.pekko.actor.ActorSystem
import org.apache.pekko.http.scaladsl.server.Route
import sttp.capabilities.WebSockets
import sttp.capabilities.pekko.PekkoStreams
Expand All @@ -22,10 +23,8 @@
import scala.concurrent.ExecutionContext
import scala.concurrent.Future

import org.knora.webapi.core.ActorSystem

final case class TapirToPekkoInterpreter()(actorSystem: ActorSystem) {
implicit val executionContext: ExecutionContext = actorSystem.system.dispatcher
final case class TapirToPekkoInterpreter()(system: ActorSystem) {
implicit val executionContext: ExecutionContext = system.dispatcher

Check warning on line 27 in webapi/src/main/scala/org/knora/webapi/slice/common/api/TapirToPekkoInterpreter.scala

View check run for this annotation

Codecov / codecov/patch

webapi/src/main/scala/org/knora/webapi/slice/common/api/TapirToPekkoInterpreter.scala#L27

Added line #L27 was not covered by tests
private case class GenericErrorResponse(error: String)
private object GenericErrorResponse {
implicit val codec: JsonCodec[GenericErrorResponse] = DeriveJsonCodec.gen[GenericErrorResponse]
Expand Down