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(IIIFService): zio-fying iiif service (DEV-801) #2044

Merged
merged 50 commits into from May 5, 2022
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
29ce54b
refactor(IIIFService): IIIFService and config
subotic Apr 13, 2022
e35cbb1
Update ManagersWithMockedSipi.scala
subotic Apr 13, 2022
5cfa753
Merge branch 'main' into wip/DEV-801-zio-fying-iiif-service
subotic Apr 13, 2022
1f1d6bc
Merge branch 'main' into wip/DEV-801-zio-fying-iiif-service
subotic Apr 14, 2022
755d62a
refactor(IIIFService): use TestManagersWithMockedSipi for tests needi…
subotic Apr 14, 2022
ea9a4dd
build(CI): change Java to Temurin JDK 17
subotic Apr 14, 2022
2a18748
refactor(IIIFService): use TestManagersWithAllTestContainers (ongoing)
subotic Apr 14, 2022
bed2eff
refactor(IIIFService): use TestManagersWithAllTestContainers
subotic Apr 15, 2022
76b92cb
refactor(IIIFService): use sipi test client (ongoing)
subotic Apr 16, 2022
5d4c3ce
refactor(IIIFService): use test client (ongoing)
subotic Apr 18, 2022
09a07b9
refactor(IIIFService): use test client
subotic Apr 18, 2022
8a90c9c
refactor(IIIFService): fix tests
subotic Apr 18, 2022
e9cf31e
refactor(IIIFService): fix tests
subotic Apr 19, 2022
867e4f3
refactor(IIIFService): fix shacl tests
subotic Apr 19, 2022
bdc03b9
refactor(IIIFService): fix integration specs (reverting to original i…
subotic Apr 19, 2022
d2fa994
refactor(IIIFService): fix tests on CI (trying)
subotic Apr 19, 2022
60bf122
Update TestClientService.scala
subotic Apr 19, 2022
e292672
remove unused tests
subotic Apr 19, 2022
c54c77d
refactor(IIIFService): optimize logging
subotic Apr 19, 2022
180c95e
refactor(IIIFService): raise timeout in TestClient
subotic Apr 19, 2022
f842570
refactor(IIIFService): fix error handling
subotic Apr 20, 2022
86b658a
refactor(IIIFService): tune down logging
subotic Apr 20, 2022
7aea3de
add scalafmt plugin
subotic Apr 23, 2022
63461fa
add better error handling at the edge
subotic Apr 23, 2022
76b3434
add automatic format checking
subotic Apr 23, 2022
f390494
add automatic format checking
subotic Apr 23, 2022
6053b17
add automatic format checking
subotic Apr 23, 2022
499c3f2
Merge branch 'main' into wip/DEV-801-zio-fying-iiif-service
subotic Apr 26, 2022
6a5d77e
format more files
subotic Apr 26, 2022
a16293f
add ADR
subotic Apr 28, 2022
c44718c
Merge branch 'main' into wip/DEV-801-zio-fying-iiif-service
subotic Apr 28, 2022
f0e740f
Update webapi/src/main/scala/org/knora/webapi/config/AppConfig.scala
subotic May 3, 2022
f8d2f88
Update webapi/src/test/scala/org/knora/webapi/testcontainers/SipiTest…
subotic May 3, 2022
937df12
Update webapi/src/test/scala/org/knora/webapi/testservices/TestClient…
subotic May 3, 2022
245b02a
Update webapi/src/test/scala/org/knora/webapi/testservices/TestClient…
subotic May 3, 2022
5de2b21
Update webapi/src/test/scala/org/knora/webapi/testservices/TestClient…
subotic May 3, 2022
5d064cc
Update webapi/src/test/scala/org/knora/webapi/testservices/TestClient…
subotic May 3, 2022
ca5bd78
add review fixes
subotic May 3, 2022
9a2a8d4
Merge branch 'wip/DEV-801-zio-fying-iiif-service' of github.com:dasch…
subotic May 3, 2022
024eec0
add review fixes
subotic May 3, 2022
8243aa1
fix formatting
subotic May 3, 2022
ce5929b
Update .gitignore
subotic May 3, 2022
8114547
Update webapi/src/main/scala/org/knora/webapi/store/iiif/IIIFServiceM…
subotic May 4, 2022
2c76c60
organize imports
subotic May 4, 2022
54bede5
Merge branch 'wip/DEV-801-zio-fying-iiif-service' of github.com:dasch…
subotic May 4, 2022
aff409c
fix formatting
subotic May 4, 2022
3663a3d
rename class
subotic May 4, 2022
d74a6a2
fix typos
subotic May 4, 2022
6ffb1e4
add missing type annotations
subotic May 4, 2022
33f37ad
Merge branch 'main' into wip/DEV-801-zio-fying-iiif-service
subotic May 5, 2022
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
76 changes: 55 additions & 21 deletions .github/workflows/main.yml
Expand Up @@ -17,22 +17,17 @@ jobs:
uses: actions/checkout@v1
with:
fetch-depth: 15
- name: Setup JDK
uses: actions/setup-java@v2
with:
distribution: temurin
java-version: 11
- name: get current time
uses: josStorer/get-current-time@v2
id: current-time
with:
format: YYYY-MM-DD
utcOffset: "+02:00"
- name: setup java version
uses: joschi/setup-jdk@v2
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '11' # The OpenJDK version to make available on the path
architecture: 'x64' # defaults to 'x64'
distribution: 'temurin'
java-version: '17'
- name: add docker compose v2
run: |
mkdir -p ~/.docker/cli-plugins/
Expand Down Expand Up @@ -69,11 +64,11 @@ jobs:
with:
format: YYYY-MM-DD
utcOffset: "+02:00"
- name: setup java version
uses: joschi/setup-jdk@v2
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '11' # The OpenJDK version to make available on the path
architecture: 'x64' # defaults to 'x64'
distribution: 'temurin'
java-version: '17'
- name: add docker compose v2
run: |
mkdir -p ~/.docker/cli-plugins/
Expand Down Expand Up @@ -116,11 +111,11 @@ jobs:
with:
format: YYYY-MM-DD
utcOffset: "+02:00"
- name: setup java version
uses: joschi/setup-jdk@v2
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '11' # The OpenJDK version to make available on the path
architecture: 'x64' # defaults to 'x64'
distribution: 'temurin'
java-version: '17'
- name: add docker compose v2
run: |
mkdir -p ~/.docker/cli-plugins/
Expand Down Expand Up @@ -211,11 +206,11 @@ jobs:
with:
format: YYYY-MM-DD
utcOffset: "+02:00"
- name: setup java version
uses: joschi/setup-jdk@v2
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '11' # The OpenJDK version to make available on the path
architecture: 'x64' # defaults to 'x64'
distribution: 'temurin'
java-version: '17'
- name: add docker compose v2
run: |
mkdir -p ~/.docker/cli-plugins/
Expand Down Expand Up @@ -287,3 +282,42 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
CUSTOM_DOMAIN: docs-api.dasch.swiss
REQUIREMENTS: docs/requirements.txt

fmtcheck:
name: Check Formating
runs-on: ubuntu-latest
steps:
- name: checkout source
uses: actions/checkout@v1
with:
fetch-depth: 15
- name: get current time
uses: josStorer/get-current-time@v2
id: current-time
with:
format: YYYY-MM-DD
utcOffset: "+02:00"
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '17'
- name: add docker compose v2
run: |
mkdir -p ~/.docker/cli-plugins/
curl -SL https://github.com/docker/compose/releases/download/v2.2.3/docker-compose-linux-x86_64 -o ~/.docker/cli-plugins/docker-compose
chmod +x ~/.docker/cli-plugins/docker-compose
- name: install requirements
run: sudo apt-get install ca-certificates-java expect
- name: Coursier cache
uses: coursier/cache-action@v6
- name: Run fmtcheck
run: make check
- name: Cleanup before cache
shell: bash
run: |
rm -rf "$HOME/.ivy2/local" || true
find $HOME/Library/Caches/Coursier/v1 -name "ivydata-*.properties" -delete || true
find $HOME/.ivy2/cache -name "ivydata-*.properties" -delete || true
find $HOME/.cache/coursier/v1 -name "ivydata-*.properties" -delete || true
find $HOME/.sbt -name "*.lock" -delete || true
4 changes: 4 additions & 0 deletions Makefile
Expand Up @@ -300,6 +300,10 @@ info: ## print out all variables
@echo "BUILD_TAG: \t\t $(BUILD_TAG)"
@echo "GIT_EMAIL: \t\t $(GIT_EMAIL)"

.PHONY: check
check: # Run code formating check
@sbt "check"

.PHONY: help
help: ## this help
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST) | sort
Expand Down
9 changes: 4 additions & 5 deletions build.sbt
Expand Up @@ -49,6 +49,9 @@ lazy val root: Project = Project(id = "root", file("."))
publish / skip := true
)

addCommandAlias("fmt", "all root/scalafmtSbt root/scalafmtAll")
addCommandAlias("check", "all root/scalafmtSbtCheck root/scalafmtCheckAll")

//////////////////////////////////////
// DSP's custom SIPI
//////////////////////////////////////
Expand Down Expand Up @@ -98,7 +101,7 @@ lazy val webApiCommonSettings = Seq(

lazy val webapi: Project = Project(id = "webapi", base = file("webapi"))
.settings(buildSettings)
.enablePlugins(SbtTwirl, JavaAppPackaging, DockerPlugin, GatlingPlugin, JavaAgent, RevolverPlugin, BuildInfoPlugin)
.enablePlugins(SbtTwirl, JavaAppPackaging, DockerPlugin, GatlingPlugin, JavaAgent, BuildInfoPlugin)
.settings(
name := "webapi",
resolvers ++= Seq(
Expand Down Expand Up @@ -133,10 +136,6 @@ lazy val webapi: Project = Project(id = "webapi", base = file("webapi"))
scalacOptions ++= Seq("-feature", "-unchecked", "-deprecation", "-Yresolve-term-conflict:package"),
logLevel := Level.Info,
run / javaOptions := webapiJavaRunOptions,
reStart / javaOptions ++= resolvedJavaAgents.value map { resolved =>
"-javaagent:" + resolved.artifact.absolutePath + resolved.agent.arguments
}, // allows sbt-javaagent to work with sbt-revolver
reStart / javaOptions ++= webapiJavaRunOptions,
javaAgents += Dependencies.aspectjweaver,
fork := true, // run tests in a forked JVM
Test / testForkedParallel := false, // run forked tests in parallel
Expand Down
@@ -0,0 +1,20 @@
# 3. Change IIIF Service Manager and Sipi implementation to zlayer

Date: 2022-04-29

## Status

Accepted

## Context

Both `org.knora.webapi.store.iiif.IIIFServiceManager` and `org.knora.webapi.store.iiif.impl.IIIFServiceSipiImpl`
where implemented as Akka-Actors

## Decision

As part of the move from `Akka` to `ZIO`, it was decided that the `IIIFServiceManager` and the `IIIFServiceSipiImpl` is refactored using ZIO.

## Consequences

The usage from other actors stays the same. The actor messages and responses don't change.
4 changes: 2 additions & 2 deletions project/Dependencies.scala
Expand Up @@ -33,7 +33,7 @@ object Dependencies {
val zioHttp = "io.d11" %% "zhttp" % ZioHttpVersion
val zioJson = "dev.zio" %% "zio-json" % ZioJsonVersion
val zioPrelude = "dev.zio" %% "zio-prelude" % ZioPreludeVersion
val zioLoggingSlf4j = "dev.zio" %% "zio-logging-slf4j" % ZioLoggingVersion
val zioLogging = "dev.zio" %% "zio-logging" % ZioLoggingVersion
val zioConfig = "dev.zio" %% "zio-config" % ZioConfigVersion
val zioConfigMagnolia = "dev.zio" %% "zio-config-magnolia" % ZioConfigVersion
val zioConfigTypesafe = "dev.zio" %% "zio-config-typesafe" % ZioConfigVersion
Expand Down Expand Up @@ -143,7 +143,7 @@ object Dependencies {
zioConfigTypesafe,
zioHttp,
zioJson,
zioLoggingSlf4j,
zioLogging,
zioPrelude,
zioTest % Test,
zioTestSbt % Test
Expand Down
12 changes: 5 additions & 7 deletions project/plugins.sbt
Expand Up @@ -5,13 +5,9 @@ resolvers ++= Seq(
// please don't remove or merge uncommented to main
addDependencyTreePlugin

// Knora
addSbtPlugin("com.typesafe.sbt" % "sbt-git" % "1.0.2")
addSbtPlugin("com.github.sbt" % "sbt-native-packager" % "1.9.9")
addSbtPlugin("io.kamon" % "sbt-aspectj-runner" % "1.1.1")

// webapi
addSbtPlugin("io.spray" % "sbt-revolver" % "0.9.1")
addSbtPlugin("com.typesafe.sbt" % "sbt-git" % "1.0.2")
addSbtPlugin("com.github.sbt" % "sbt-native-packager" % "1.9.9")
addSbtPlugin("io.kamon" % "sbt-aspectj-runner" % "1.1.1")
addSbtPlugin("com.typesafe.sbt" % "sbt-twirl" % "1.5.1")
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.10")
addSbtPlugin("com.lightbend.sbt" % "sbt-javaagent" % "0.1.5")
Expand All @@ -20,6 +16,8 @@ addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.6.1")
addSbtPlugin("com.codacy" % "sbt-codacy-coverage" % "1.3.15")
addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.11.0")

addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.3")
subotic marked this conversation as resolved.
Show resolved Hide resolved

// ad-hoc plugins - uncomment on demenad and keep it commented out in main branch

// https://github.com/rtimush/sbt-updates
Expand Down
4 changes: 2 additions & 2 deletions sipi/scripts/jwt.lua
Expand Up @@ -26,9 +26,9 @@ function get_knora_token()
end

token_issuer = webapi_hostname .. ':' .. webapi_port
server.log("token_issuer:" .. token_issuer, server.loglevel.LOG_DEBUG)
server.log("token_issuer: " .. token_issuer, server.loglevel.LOG_DEBUG)
if token["iss"] ~= token_issuer then
server.log(token_issuer, server.loglevel.LOG_DEBUG)
server.log("Invalid token issuer: " .. token_issuer .. " . Expected: " .. token["iss"], server.loglevel.LOG_DEBUG)
send_error(401, "Invalid token. The token was not issued by the same server that sent the request.")
return nil
end
Expand Down
2 changes: 1 addition & 1 deletion webapi/src/main/resources/application.conf
Expand Up @@ -296,7 +296,7 @@ app {

shacl {
# The directory that SHACL shapes are loaded from.
shapes-dir = "shacl"
shapes-dir = "../test_data/shacl"
subotic marked this conversation as resolved.
Show resolved Hide resolved
subotic marked this conversation as resolved.
Show resolved Hide resolved
shapes-dir = ${?KNORA_WEBAPI_SHACLE_SHAPES_DIR}
}

Expand Down
94 changes: 42 additions & 52 deletions webapi/src/main/scala/org/knora/webapi/app/ApplicationActor.scala
Expand Up @@ -23,7 +23,6 @@ import ch.megard.akka.http.cors.scaladsl.settings.CorsSettings
import com.typesafe.config.ConfigFactory
import com.typesafe.scalalogging.LazyLogging
import kamon.Kamon
import org.knora.webapi.config.AppConfig
import org.knora.webapi.core.LiveActorMaker
import org.knora.webapi.exceptions.InconsistentRepositoryDataException
import org.knora.webapi.exceptions.MissingLastModificationDateOntologyException
Expand Down Expand Up @@ -65,6 +64,7 @@ import org.knora.webapi.store.StoreManager
import org.knora.webapi.store.cacheservice.CacheServiceManager
import org.knora.webapi.store.cacheservice.impl.CacheServiceInMemImpl
import org.knora.webapi.store.cacheservice.settings.CacheServiceSettings
import org.knora.webapi.store.iiif.IIIFServiceManager
import org.knora.webapi.util.cache.CacheUtil
import redis.clients.jedis.exceptions.JedisConnectionException
import zio.Runtime
Expand All @@ -78,58 +78,13 @@ import scala.concurrent.duration._
import scala.util.Failure
import scala.util.Success
import org.knora.webapi.store.cacheservice.config.RedisConfig
import org.knora.webapi.store.iiif.impl.IIIFServiceSipiImpl
import zio.ZEnvironment
import zio.RuntimeConfig
import org.knora.webapi.core.Logging

trait Managers {
implicit val system: ActorSystem
val responderManager: ActorRef
val storeManager: ActorRef
}

trait LiveManagers extends Managers {
this: Actor =>

/**
* Initializing the cache service manager, which is a ZLayer,
* by unsafe running it.
*/
lazy val cacheServiceManager: CacheServiceManager =
Runtime(ZEnvironment.empty, RuntimeConfig.default @@ Logging.live)
.unsafeRun(
(for (manager <- ZIO.service[CacheServiceManager])
yield manager).provide(CacheServiceInMemImpl.layer, CacheServiceManager.layer)
)

/**
* The actor that forwards messages to actors that deal with persistent storage.
*/
lazy val storeManager: ActorRef = context.actorOf(
Props(new StoreManager(appActor = self, csm = cacheServiceManager) with LiveActorMaker)
.withDispatcher(KnoraDispatchers.KnoraActorDispatcher),
name = StoreManagerActorName
)

/**
* The actor that forwards messages to responder actors to handle API requests.
*/
lazy val responderManager: ActorRef = context.actorOf(
Props(
new ResponderManager(
appActor = self,
responderData = ResponderData(
system = context.system,
appActor = self,
knoraSettings = KnoraSettings(system),
cacheServiceSettings = new CacheServiceSettings(system.settings.config)
)
) with LiveActorMaker
)
.withDispatcher(KnoraDispatchers.KnoraActorDispatcher),
name = RESPONDER_MANAGER_ACTOR_NAME
)
}
import org.knora.webapi.auth.JWTService
import zio.ZLayer
import org.knora.webapi.config.AppConfig

/**
* This is the first actor in the application. All other actors are children
Expand All @@ -139,8 +94,15 @@ trait LiveManagers extends Managers {
* the startup and shutdown sequence. Further, it forwards any messages meant
* for responders or the store to the respective actor.
*/
class ApplicationActor extends Actor with Stash with LazyLogging with AroundDirectives with Timers {
this: Managers =>
class ApplicationActor(
cacheServiceManager: CacheServiceManager,
iiifServiceManager: IIIFServiceManager,
appConfig: AppConfig
) extends Actor
with Stash
with LazyLogging
with AroundDirectives
with Timers {

logger.debug("entered the ApplicationManager constructor")

Expand Down Expand Up @@ -184,6 +146,34 @@ class ApplicationActor extends Actor with Stash with LazyLogging with AroundDire
appActor = self
)

/**
* The actor that forwards messages to responder actors to handle API requests.
*/
lazy val responderManager: ActorRef = context.actorOf(
Props(
new ResponderManager(
appActor = self,
responderData = ResponderData(
system = context.system,
appActor = self,
knoraSettings = KnoraSettings(system),
cacheServiceSettings = new CacheServiceSettings(system.settings.config)
)
) with LiveActorMaker
)
.withDispatcher(KnoraDispatchers.KnoraActorDispatcher),
name = RESPONDER_MANAGER_ACTOR_NAME
)

/**
* The actor that forwards messages to actors that deal with persistent storage.
*/
lazy val storeManager: ActorRef = context.actorOf(
Props(new StoreManager(self, cacheServiceManager, iiifServiceManager, appConfig) with LiveActorMaker)
.withDispatcher(KnoraDispatchers.KnoraActorDispatcher),
name = StoreManagerActorName
)

/**
* This actor acts as the supervisor for its child actors.
* Here we can override the default supervisor strategy.
Expand Down
4 changes: 3 additions & 1 deletion webapi/src/main/scala/org/knora/webapi/app/LiveCore.scala
Expand Up @@ -15,6 +15,8 @@ import org.knora.webapi.settings.{KnoraDispatchers, KnoraSettings, KnoraSettings
import scala.concurrent.ExecutionContext
import scala.language.postfixOps
import scala.languageFeature.postfixOps
import org.knora.webapi.store.iiif.IIIFServiceManager
import org.knora.webapi.store.cacheservice.CacheServiceManager

/**
* The applications actor system.
Expand Down Expand Up @@ -53,7 +55,7 @@ trait LiveCore extends Core {
* sequences.
*/
lazy val appActor: ActorRef = system.actorOf(
Props(new ApplicationActor with LiveManagers)
Props(new ApplicationActor(cacheServiceManager, iiifServiceManager, appConfig))
.withDispatcher(KnoraDispatchers.KnoraActorDispatcher),
name = APPLICATION_MANAGER_ACTOR_NAME
)
Expand Down