Skip to content

Commit

Permalink
libs-scala/resources: Move Resource and friends to their own packag…
Browse files Browse the repository at this point in the history
…e. (digital-asset#4066)

* libs-scala/resources: Move `Resource` and friends to their own package.

CHANGELOG_BEGIN
- [Ledger API Server] Publish the resource management code as a library
  under ``com.digitalasset:resources``.
CHANGELOG_END

* libs-scala/resources: Add to the artifacts list.

* libs-scala/resources: Factor out the test helpers into test/lib.

* libs-scala/resources-akka: Make the Akka resources an optional dep.
  • Loading branch information
SamirTalwar authored and mergify[bot] committed Jan 16, 2020
1 parent a549065 commit b96e54f
Show file tree
Hide file tree
Showing 46 changed files with 339 additions and 195 deletions.
2 changes: 2 additions & 0 deletions ledger/api-server-damlonx/reference-v2/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ da_scala_binary(
"//ledger/participant-state",
"//ledger/participant-state/kvutils",
"//ledger/sandbox",
"//libs-scala/resources",
"//libs-scala/resources-akka",
"@maven//:com_github_scopt_scopt_2_12",
"@maven//:com_typesafe_akka_akka_actor_2_12",
"@maven//:com_typesafe_akka_akka_stream_2_12",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import com.digitalasset.ledger.api.auth.{AuthService, AuthServiceWildcard}
import com.digitalasset.platform.apiserver.{ApiServerConfig, StandaloneApiServer}
import com.digitalasset.platform.common.logging.NamedLoggerFactory
import com.digitalasset.platform.indexer.{IndexerConfig, StandaloneIndexerServer}
import com.digitalasset.platform.resources.{Resource, ResourceOwner}
import com.digitalasset.resources.akka.AkkaResourceOwner
import com.digitalasset.resources.{Resource, ResourceOwner}
import org.slf4j.LoggerFactory

import scala.concurrent.duration.DurationInt
Expand All @@ -42,8 +43,8 @@ object ReferenceServer extends App {
val resource = for {
// Take ownership of the actor system and materializer so they're cleaned up properly.
// This is necessary because we can't declare them as implicits within a `for` comprehension.
_ <- ResourceOwner.forActorSystem(() => system).acquire()
_ <- ResourceOwner.forMaterializer(() => materializer).acquire()
_ <- AkkaResourceOwner.forActorSystem(() => system).acquire()
_ <- AkkaResourceOwner.forMaterializer(() => materializer).acquire()
ledger <- ResourceOwner
.forCloseable(() => new InMemoryKVParticipantState(config.participantId))
.acquire()
Expand Down
2 changes: 2 additions & 0 deletions ledger/participant-state/kvutils/app/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ da_scala_library(
"//ledger/participant-state",
"//ledger/participant-state/kvutils",
"//ledger/sandbox",
"//libs-scala/resources",
"//libs-scala/resources-akka",
"@maven//:com_github_scopt_scopt_2_12",
"@maven//:com_google_protobuf_protobuf_java",
"@maven//:com_typesafe_akka_akka_actor_2_12",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import com.digitalasset.platform.indexer.{
IndexerStartupMode,
StandaloneIndexerServer
}
import com.digitalasset.platform.resources.{Resource, ResourceOwner}
import com.digitalasset.resources.akka.AkkaResourceOwner
import com.digitalasset.resources.{Resource, ResourceOwner}
import org.slf4j.LoggerFactory

import scala.concurrent.duration.DurationInt
Expand All @@ -44,8 +45,8 @@ class Runner[Extra](name: String, constructor: LedgerFactory[Extra]) {
val resource = for {
// Take ownership of the actor system and materializer so they're cleaned up properly.
// This is necessary because we can't declare them as implicits within a `for` comprehension.
_ <- ResourceOwner.forActorSystem(() => system).acquire()
_ <- ResourceOwner.forMaterializer(() => materializer).acquire()
_ <- AkkaResourceOwner.forActorSystem(() => system).acquire()
_ <- AkkaResourceOwner.forMaterializer(() => materializer).acquire()
readerWriter <- ResourceOwner
.forCloseable(() => constructor(config.participantId, config.extra))
.acquire()
Expand Down
6 changes: 6 additions & 0 deletions ledger/sandbox/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ da_scala_library(
"//ledger/participant-state",
"//ledger/participant-state-index",
"//libs-scala/direct-execution-context",
"//libs-scala/resources",
"//libs-scala/resources-akka",
"@maven//:ch_qos_logback_logback_classic",
"@maven//:ch_qos_logback_logback_core",
"@maven//:com_auth0_java_jwt",
Expand Down Expand Up @@ -133,6 +135,8 @@ da_scala_library(
"//ledger/participant-state",
"//ledger/participant-state-index",
"//libs-scala/direct-execution-context",
"//libs-scala/resources",
"//libs-scala/resources-akka",
"@maven//:ch_qos_logback_logback_classic",
"@maven//:ch_qos_logback_logback_core",
"@maven//:com_auth0_java_jwt",
Expand Down Expand Up @@ -260,6 +264,7 @@ da_scala_library(
"//ledger/test-common",
"//libs-scala/direct-execution-context",
"//libs-scala/grpc-utils",
"//libs-scala/resources",
"//libs-scala/timer-utils",
"@maven//:ch_qos_logback_logback_classic",
"@maven//:ch_qos_logback_logback_core",
Expand Down Expand Up @@ -315,6 +320,7 @@ da_scala_test_suite(
"//ledger/test-common",
"//libs-scala/direct-execution-context",
"//libs-scala/grpc-utils",
"//libs-scala/resources",
"//libs-scala/timer-utils",
"@maven//:ch_qos_logback_logback_classic",
"@maven//:ch_qos_logback_logback_core",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import com.daml.ledger.participant.state.v1.{ParticipantId, ReadService}
import com.digitalasset.dec.DirectExecutionContext
import com.digitalasset.ledger.api.domain.{ParticipantId => _, _}
import com.digitalasset.platform.common.logging.NamedLoggerFactory
import com.digitalasset.platform.resources.Resource
import com.digitalasset.platform.sandbox.stores.LedgerBackedIndexService
import com.digitalasset.platform.sandbox.stores.ledger.{
Ledger,
MeteredReadOnlyLedger,
SandboxContractStore
}
import com.digitalasset.resources.Resource

object JdbcIndex {
def apply(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package com.digitalasset.platform.apiserver

import java.io.IOException
import java.net.{BindException, InetSocketAddress, InetAddress}
import java.net.{BindException, InetAddress, InetSocketAddress}
import java.util.UUID
import java.util.concurrent.TimeUnit.{MILLISECONDS, SECONDS}

Expand All @@ -13,7 +13,7 @@ import akka.stream.Materializer
import com.codahale.metrics.MetricRegistry
import com.digitalasset.grpc.adapter.{AkkaExecutionSequencerPool, ExecutionSequencerFactory}
import com.digitalasset.platform.common.logging.NamedLoggerFactory
import com.digitalasset.platform.resources.{Resource, ResourceOwner}
import com.digitalasset.resources.{Resource, ResourceOwner}
import io.grpc.netty.NettyServerBuilder
import io.grpc.{Server, ServerInterceptor}
import io.netty.channel.EventLoopGroup
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ import com.digitalasset.ledger.api.domain
import com.digitalasset.ledger.api.health.HealthChecks
import com.digitalasset.platform.apiserver.StandaloneApiServer._
import com.digitalasset.platform.common.logging.NamedLoggerFactory
import com.digitalasset.platform.resources.{Resource, ResourceOwner}
import com.digitalasset.platform.sandbox.BuildInfo
import com.digitalasset.platform.sandbox.config.SandboxConfig
import com.digitalasset.platform.sandbox.stores.InMemoryPackageStore
import com.digitalasset.resources.akka.AkkaResourceOwner
import com.digitalasset.resources.{Resource, ResourceOwner}

import scala.collection.JavaConverters._
import scala.concurrent.{ExecutionContext, Future}
Expand Down Expand Up @@ -89,10 +90,8 @@ final class StandaloneApiServer(
preloadPackages(packageStore)

for {
actorSystem <- ResourceOwner.forActorSystem(() => ActorSystem(actorSystemName)).acquire()
materializer <- ResourceOwner
.forMaterializer(() => Materializer(actorSystem))
.acquire()
actorSystem <- AkkaResourceOwner.forActorSystem(() => ActorSystem(actorSystemName)).acquire()
materializer <- AkkaResourceOwner.forMaterializer(() => Materializer(actorSystem)).acquire()
initialConditions <- ResourceOwner
.forFuture(() => readService.getLedgerInitialConditions().runWith(Sink.head)(materializer))
.acquire()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package com.digitalasset.platform.indexer

import com.daml.ledger.participant.state.v1.ReadService
import com.digitalasset.platform.resources.ResourceOwner
import com.digitalasset.resources.ResourceOwner

import scala.concurrent.Future

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import com.digitalasset.dec.{DirectExecutionContext => DEC}
import com.digitalasset.ledger.api.domain
import com.digitalasset.ledger.api.domain.{LedgerId, PartyDetails}
import com.digitalasset.platform.common.logging.NamedLoggerFactory
import com.digitalasset.platform.resources.{Resource, ResourceOwner}
import com.digitalasset.platform.sandbox.EventIdFormatter
import com.digitalasset.platform.sandbox.metrics.timedFuture
import com.digitalasset.platform.sandbox.stores.ledger.sql.SqlLedger.defaultNumberOfShortLivedConnections
Expand All @@ -39,6 +38,7 @@ import com.digitalasset.platform.sandbox.stores.ledger.{
PackageLedgerEntry,
PartyLedgerEntry
}
import com.digitalasset.resources.{Resource, ResourceOwner}
import scalaz.syntax.tag._

import scala.concurrent.duration._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import akka.actor.Scheduler
import akka.pattern.after
import com.digitalasset.dec.DirectExecutionContext
import com.digitalasset.platform.common.logging.NamedLoggerFactory
import com.digitalasset.platform.resources.{Resource, ResourceOwner}
import com.digitalasset.resources.{Resource, ResourceOwner}

import scala.concurrent.duration.FiniteDuration
import scala.concurrent.{Await, ExecutionContext, Future, Promise}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import com.codahale.metrics.MetricRegistry
import com.daml.ledger.participant.state.v1.ReadService
import com.digitalasset.platform.common.logging.NamedLoggerFactory
import com.digitalasset.platform.indexer.StandaloneIndexerServer._
import com.digitalasset.platform.resources.{Resource, ResourceOwner}
import com.digitalasset.resources.akka.AkkaResourceOwner
import com.digitalasset.resources.{Resource, ResourceOwner}

import scala.concurrent.duration.{DurationInt, FiniteDuration}
import scala.concurrent.{ExecutionContext, Future}
Expand All @@ -24,7 +25,7 @@ class StandaloneIndexerServer(
override def acquire()(implicit executionContext: ExecutionContext): Resource[Unit] =
for {
// ActorSystem name not allowed to contain daml-lf LedgerString characters ".:#/ "
actorSystem <- ResourceOwner
actorSystem <- AkkaResourceOwner
.forActorSystem(() =>
ActorSystem("StandaloneIndexerServer-" + config.participantId.filterNot(".:#/ ".toSet)))
.acquire()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import com.digitalasset.platform.apiserver.{
}
import com.digitalasset.platform.common.LedgerIdMode
import com.digitalasset.platform.common.logging.NamedLoggerFactory
import com.digitalasset.platform.resources.{Resource, ResourceOwner}
import com.digitalasset.platform.sandbox.SandboxServer._
import com.digitalasset.platform.sandbox.banner.Banner
import com.digitalasset.platform.sandbox.config.SandboxConfig
Expand All @@ -44,6 +43,8 @@ import com.digitalasset.platform.sandbox.stores.{
SandboxIndexAndWriteService
}
import com.digitalasset.platform.services.time.TimeProviderType
import com.digitalasset.resources.akka.AkkaResourceOwner
import com.digitalasset.resources.{Resource, ResourceOwner}
import org.slf4j.LoggerFactory

import scala.collection.JavaConverters._
Expand Down Expand Up @@ -285,10 +286,8 @@ final class SandboxServer(config: => SandboxConfig) extends AutoCloseable {
private def start()(implicit executionContext: ExecutionContext): Resource[SandboxState] = {
val packageStore = loadDamlPackages()
for {
actorSystem <- ResourceOwner.forActorSystem(() => ActorSystem(ActorSystemName)).acquire()
materializer <- ResourceOwner
.forMaterializer(() => Materializer(actorSystem))
.acquire()
actorSystem <- AkkaResourceOwner.forActorSystem(() => ActorSystem(ActorSystemName)).acquire()
materializer <- AkkaResourceOwner.forMaterializer(() => Materializer(actorSystem)).acquire()
} yield {
val apiServerResource = buildAndStartApiServer(
materializer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ import com.digitalasset.ledger.api.domain.{ParticipantId => _, _}
import com.digitalasset.ledger.api.health.HealthStatus
import com.digitalasset.platform.common.logging.NamedLoggerFactory
import com.digitalasset.platform.participant.util.EventFilter
import com.digitalasset.platform.resources.{Resource, ResourceOwner}
import com.digitalasset.platform.sandbox.stores.ledger.ScenarioLoader.LedgerEntryOrBump
import com.digitalasset.platform.sandbox.stores.ledger._
import com.digitalasset.platform.sandbox.stores.ledger.sql.SqlStartMode
import com.digitalasset.platform.server.api.validation.ErrorFactories
import com.digitalasset.resources.{Resource, ResourceOwner}
import org.slf4j.LoggerFactory
import scalaz.Tag
import scalaz.syntax.tag._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import com.digitalasset.ledger.api.domain.{LedgerId, PartyDetails}
import com.digitalasset.ledger.api.health.ReportsHealth
import com.digitalasset.platform.common.logging.NamedLoggerFactory
import com.digitalasset.platform.participant.util.EventFilter.TemplateAwareFilter
import com.digitalasset.platform.resources.{Resource, ResourceOwner}
import com.digitalasset.platform.sandbox.stores.ActiveLedgerState.Contract
import com.digitalasset.platform.sandbox.stores.ledger.ScenarioLoader.LedgerEntryOrBump
import com.digitalasset.platform.sandbox.stores.ledger.inmemory.InMemoryLedger
Expand All @@ -34,6 +33,7 @@ import com.digitalasset.platform.sandbox.stores.ledger.sql.{
SqlStartMode
}
import com.digitalasset.platform.sandbox.stores.{InMemoryActiveLedgerState, InMemoryPackageStore}
import com.digitalasset.resources.{Resource, ResourceOwner}

import scala.concurrent.Future

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import com.digitalasset.dec.{DirectExecutionContext => DEC}
import com.digitalasset.ledger.api.domain.LedgerId
import com.digitalasset.ledger.api.health.HealthStatus
import com.digitalasset.platform.common.logging.NamedLoggerFactory
import com.digitalasset.platform.resources.{Resource, ResourceOwner}
import com.digitalasset.platform.sandbox.stores.ledger.ReadOnlyLedger
import com.digitalasset.platform.sandbox.stores.ledger.sql.dao.{
DbType,
Expand All @@ -20,6 +19,7 @@ import com.digitalasset.platform.sandbox.stores.ledger.sql.dao.{
LedgerReadDao
}
import com.digitalasset.platform.sandbox.stores.ledger.sql.util.DbDispatcher
import com.digitalasset.resources.{Resource, ResourceOwner}
import scalaz.syntax.tag._

import scala.concurrent.duration._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import com.digitalasset.dec.{DirectExecutionContext => DEC}
import com.digitalasset.ledger.api.domain.{LedgerId, PartyDetails, RejectionReason}
import com.digitalasset.ledger.api.health.HealthStatus
import com.digitalasset.platform.common.logging.NamedLoggerFactory
import com.digitalasset.platform.resources.ResourceOwner
import com.digitalasset.platform.sandbox.stores.ledger.ScenarioLoader.LedgerEntryOrBump
import com.digitalasset.platform.sandbox.stores.ledger.sql.SqlStartMode.{
AlwaysReset,
Expand All @@ -41,6 +40,7 @@ import com.digitalasset.platform.sandbox.stores.ledger.{
}
import com.digitalasset.platform.sandbox.stores.{InMemoryActiveLedgerState, InMemoryPackageStore}
import com.digitalasset.platform.sandbox.{EventIdFormatter, LedgerIdGenerator}
import com.digitalasset.resources.ResourceOwner
import scalaz.syntax.tag._

import scala.collection.immutable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import java.util.{Timer, TimerTask}

import com.codahale.metrics.MetricRegistry
import com.digitalasset.ledger.api.health.{HealthStatus, Healthy, ReportsHealth, Unhealthy}
import com.digitalasset.platform.resources.ResourceOwner
import com.digitalasset.platform.sandbox.stores.ledger.sql.dao.HikariJdbcConnectionProvider._
import com.digitalasset.resources.ResourceOwner
import com.zaxxer.hikari.{HikariConfig, HikariDataSource}

import scala.concurrent.duration.{DurationInt, FiniteDuration}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ package com.digitalasset.platform.sandbox.stores.ledger.sql.migration

import com.digitalasset.dec.DirectExecutionContext
import com.digitalasset.platform.common.logging.NamedLoggerFactory
import com.digitalasset.platform.resources.Resource
import com.digitalasset.platform.sandbox.stores.ledger.sql.dao.{DbType, HikariConnection}
import com.digitalasset.resources.Resource
import com.zaxxer.hikari.HikariDataSource
import org.flywaydb.core.Flyway
import org.flywaydb.core.api.configuration.FluentConfiguration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import java.util.concurrent.{ExecutorService, Executors, TimeUnit}
import com.codahale.metrics.{MetricRegistry, Timer}
import com.digitalasset.ledger.api.health.{HealthStatus, ReportsHealth}
import com.digitalasset.platform.common.logging.NamedLoggerFactory
import com.digitalasset.platform.resources.ResourceOwner
import com.digitalasset.platform.sandbox.stores.ledger.sql.dao.HikariJdbcConnectionProvider
import com.digitalasset.resources.ResourceOwner
import com.google.common.util.concurrent.ThreadFactoryBuilder
import org.slf4j.Logger

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import com.digitalasset.daml.lf.data.ImmArray
import com.digitalasset.ledger.api.domain.LedgerId
import com.digitalasset.ledger.api.testing.utils.Resource
import com.digitalasset.platform.common.logging.NamedLoggerFactory
import com.digitalasset.platform.resources
import com.digitalasset.platform.sandbox.persistence.{PostgresFixture, PostgresResource}
import com.digitalasset.platform.sandbox.stores.ledger.Ledger
import com.digitalasset.platform.sandbox.stores.ledger.ScenarioLoader.LedgerEntryOrBump
import com.digitalasset.platform.sandbox.stores.ledger.sql.SqlStartMode
import com.digitalasset.platform.sandbox.stores.{InMemoryActiveLedgerState, InMemoryPackageStore}
import com.digitalasset.resources
import scalaz.Tag

import scala.concurrent.duration.DurationInt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import ch.qos.logback.classic.Level
import com.digitalasset.dec.DirectExecutionContext
import com.digitalasset.platform.indexer.RecoveringIndexerSpec._
import com.digitalasset.platform.indexer.TestIndexer._
import com.digitalasset.platform.resources.{Resource, ResourceOwner}
import com.digitalasset.platform.sandbox.logging.TestNamedLoggerFactory
import com.digitalasset.resources.{Resource, ResourceOwner}
import org.scalatest.{AsyncWordSpec, BeforeAndAfterEach, Matchers}

import scala.collection.mutable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ package com.digitalasset.platform.sandbox.persistence
import com.codahale.metrics.MetricRegistry
import com.digitalasset.dec.DirectExecutionContext
import com.digitalasset.platform.common.logging.NamedLoggerFactory
import com.digitalasset.platform.resources.Resource
import com.digitalasset.platform.sandbox.stores.ledger.sql.dao.{
HikariJdbcConnectionProvider,
JdbcConnectionProvider
}
import com.digitalasset.platform.sandbox.stores.ledger.sql.migration.FlywayMigrations
import com.digitalasset.resources.Resource
import org.scalatest._

import scala.concurrent.Await
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ import com.digitalasset.ledger.api.domain.{
import com.digitalasset.ledger.api.testing.utils.AkkaBeforeAndAfterAll
import com.digitalasset.platform.common.logging.NamedLoggerFactory
import com.digitalasset.platform.participant.util.EventFilter
import com.digitalasset.platform.resources.Resource
import com.digitalasset.platform.sandbox.persistence.PostgresAroundAll
import com.digitalasset.platform.sandbox.stores.ActiveLedgerState.ActiveContract
import com.digitalasset.platform.sandbox.stores.ledger.sql.dao._
import com.digitalasset.platform.sandbox.stores.ledger.sql.migration.FlywayMigrations
import com.digitalasset.platform.sandbox.stores.ledger.sql.util.DbDispatcher
import com.digitalasset.platform.sandbox.stores.ledger.{ConfigurationEntry, LedgerEntry}
import com.digitalasset.resources.Resource
import org.scalatest.{AsyncWordSpec, Matchers, OptionValues}

import scala.collection.immutable.TreeMap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import com.digitalasset.ledger.api.domain.LedgerId
import com.digitalasset.ledger.api.health.{Healthy, Unhealthy}
import com.digitalasset.ledger.api.testing.utils.AkkaBeforeAndAfterAll
import com.digitalasset.platform.common.logging.NamedLoggerFactory
import com.digitalasset.platform.resources.Resource
import com.digitalasset.platform.sandbox.MetricsAround
import com.digitalasset.platform.sandbox.persistence.PostgresAroundEach
import com.digitalasset.platform.sandbox.stores.ledger.Ledger
import com.digitalasset.platform.sandbox.stores.{InMemoryActiveLedgerState, InMemoryPackageStore}
import com.digitalasset.resources.Resource
import org.scalatest.concurrent.{AsyncTimeLimitedTests, Eventually, ScaledTimeSpans}
import org.scalatest.time.{Minute, Seconds, Span}
import org.scalatest.{AsyncWordSpec, Matchers}
Expand Down
Loading

0 comments on commit b96e54f

Please sign in to comment.