Skip to content

Commit

Permalink
Changes to migrate http-perf-test to sandbox on x
Browse files Browse the repository at this point in the history
CHANGELOG_BEGIN
CHANGELOG_END
  • Loading branch information
akshayshirahatti-da committed Nov 3, 2021
1 parent 7391a3c commit a9aaf19
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ object Main extends StrictLogging {
ec: ExecutionContext,
elg: EventLoopGroup,
): Future[ExitCode] =
withLedger(config.dars, ledgerId.unwrap) { (ledgerPort, _, _) =>
withLedger(config.dars, ledgerId.unwrap) { (ledgerPort, _) =>
logger.info(s"Started ledger $ledgerId with port $ledgerPort")
withJsonApiJdbcConfig(config.queryStoreIndex) { jsonApiJdbcConfig =>
withHttpService(
ledgerId.unwrap,
Expand Down
6 changes: 6 additions & 0 deletions ledger-service/http-json-testing/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,24 @@ hj_scalacopts = lf_scalacopts + [
"//ledger/caching",
"//ledger/ledger-api-auth",
"//ledger/ledger-api-common",
"//ledger/ledger-api-health",
"//ledger/ledger-configuration",
"//ledger/ledger-resources",
"//ledger/metrics",
"//ledger/participant-integration-api",
"//ledger/participant-state",
"//ledger/participant-state/kvutils",
"//ledger/participant-state/kvutils/app",
"//ledger/sandbox",
"//ledger/sandbox:sandbox-scala-tests-lib",
"//ledger/sandbox-classic",
"//ledger/sandbox-common",
"//ledger/sandbox-common:sandbox-common-scala-tests-lib",
"//ledger/sandbox-on-x",
"//libs-scala/contextualized-logging",
"//libs-scala/db-utils",
"//libs-scala/ports",
"//libs-scala/resources",
"@maven//:io_dropwizard_metrics_metrics_core",
],
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import com.daml.http.util.TestUtil.getResponseDataBytes
import com.daml.http.util.{FutureUtil, NewBoolean}
import com.daml.jwt.JwtSigner
import com.daml.jwt.domain.{DecodedJwt, Jwt}
import com.daml.ledger.api.auth.{AuthService, AuthServiceJWTCodec, AuthServiceJWTPayload}
import com.daml.ledger.api.auth.{AuthServiceJWTCodec, AuthServiceJWTPayload}
import com.daml.ledger.api.domain.LedgerId
import com.daml.ledger.api.refinements.ApiTypes.ApplicationId
import com.daml.ledger.api.refinements.{ApiTypes => lar}
Expand All @@ -37,14 +37,16 @@ import com.daml.ledger.client.configuration.{
LedgerIdRequirement,
}
import com.daml.ledger.client.withoutledgerid.{LedgerClient => DamlLedgerClient}
import com.daml.ledger.participant.state.kvutils.app.{
ParticipantIndexerConfig,
ParticipantRunMode,
Runner,
}
import com.daml.ledger.resources.ResourceContext
import com.daml.ledger.sandbox.{BridgeConfig, BridgeLedgerFactory}
import com.daml.lf.data.Ref
import com.daml.logging.LoggingContextOf
import com.daml.metrics.Metrics
import com.daml.platform.apiserver.SeedService.Seeding
import com.daml.platform.common.LedgerIdMode
import com.daml.platform.sandbox
import com.daml.platform.sandbox.SandboxServer
import com.daml.platform.sandbox.config.SandboxConfig
import com.daml.platform.services.time.TimeProviderType
import com.daml.ports.Port
import com.typesafe.scalalogging.LazyLogging
import org.scalatest.{Assertions, Inside}
Expand Down Expand Up @@ -146,62 +148,73 @@ object HttpServiceTestFixture extends LazyLogging with Assertions with Inside {
def withLedger[A](
dars: List[File],
testName: String,
token: Option[String] = None,
useTls: UseTls = UseTls.NoTls,
authService: Option[AuthService] = None,
)(testFn: (Port, DamlLedgerClient, LedgerId) => Future[A])(implicit
mat: Materializer,
aesf: ExecutionSequencerFactory,
ec: ExecutionContext,
)(testFn: (Port, LedgerId) => Future[A])(implicit
ec: ExecutionContext
): Future[A] = {

implicit val rc: ResourceContext = ResourceContext(ec)
import com.daml.ledger.participant.state.kvutils.app.{Config, ParticipantConfig}
val ledgerId = LedgerId(testName)
val applicationId = ApplicationId(testName)

val ledgerF = for {
ledger <- Future(
new SandboxServer(ledgerConfig(Port.Dynamic, dars, ledgerId, authService, useTls), mat)
val participantId = Ref.ParticipantId.assertFromString(testName)
val participantConfig = ParticipantConfig(
mode = ParticipantRunMode.Combined,
participantId = participantId,
shardName = None,
address = Some("localhost"),
port = Port.Dynamic,
portFile = None,
indexerConfig = ParticipantIndexerConfig(
allowExistingSchema = false
),
serverJdbcUrl = ParticipantConfig.defaultIndexJdbcUrl(participantId),
)
val config = Config
.createDefault(BridgeLedgerFactory.defaultExtraConfig)
.copy(
ledgerId = ledgerId.unwrap,
participants = Seq(participantConfig),
archiveFiles = dars.map(_.toPath),
)
port <- ledger.portF
} yield (ledger, port)
logger.info(s"Starting ledger with config ${config.participants}")

val clientF: Future[DamlLedgerClient] = for {
(_, ledgerPort) <- ledgerF
} yield DamlLedgerClient.singleHost(
"localhost",
ledgerPort.value,
clientConfig(applicationId, token, useTls),
)
val ledgerF = for {
server <- {
val r = new Runner("sandbox-on-x", BridgeLedgerFactory)
r.owner(config).acquire().asFuture.flatMap(_ => r.apiServer)
}
} yield (server, server.port)

val fa: Future[A] = for {
(_, ledgerPort) <- ledgerF
client <- clientF
a <- testFn(ledgerPort, client, ledgerId)
a <- testFn(ledgerPort, ledgerId)
} yield a

fa.onComplete { _ =>
ledgerF.foreach(_._1.close())
for {
(ledger, _) <- ledgerF
_ <- ledger.servicesClosed()
} ledger
}

fa
}

private def ledgerConfig(
ledgerPort: Port,
dars: List[File],
ledgerId: LedgerId,
authService: Option[AuthService],
useTls: UseTls,
): SandboxConfig =
sandbox.DefaultConfig.copy(
port = ledgerPort,
damlPackages = dars,
timeProviderType = Some(TimeProviderType.WallClock),
tlsConfig = if (useTls) Some(serverTlsConfig) else None,
ledgerIdMode = LedgerIdMode.Static(ledgerId),
authService = authService,
seeding = Some(Seeding.Weak),
)
// private def ledgerConfig(
// ledgerPort: Port,
// dars: List[File],
// ledgerId: LedgerId,
// authService: Option[AuthService],
// useTls: UseTls,
// ): SandboxConfig =
// sandbox.DefaultConfig.copy(
// port = ledgerPort,
// damlPackages = dars,
// timeProviderType = Some(TimeProviderType.WallClock),
// tlsConfig = if (useTls) Some(serverTlsConfig) else None,
// ledgerIdMode = LedgerIdMode.Static(ledgerId),
// authService = authService,
// seeding = Some(Seeding.Weak),
// )

private def clientConfig[A](
applicationId: ApplicationId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import com.daml.lf.engine.{Engine, EngineConfig}
import com.daml.logging.LoggingContext.newLoggingContext
import com.daml.logging.{ContextualizedLogger, LoggingContext}
import com.daml.metrics.JvmMetricSet
import com.daml.platform.apiserver.StandaloneApiServer
import com.daml.platform.apiserver.{ApiServer, StandaloneApiServer}
import com.daml.platform.indexer.StandaloneIndexerServer
import com.daml.platform.store.{IndexMetadata, LfValueTranslationCache}
import com.daml.telemetry.{DefaultTelemetry, SpanKind, SpanName}
Expand Down Expand Up @@ -52,6 +52,9 @@ final class Runner[T <: ReadWriteService, Extra](
}
}

private var _apiServer: Resource[ApiServer] = _
def apiServer = _apiServer.asFuture

private def dumpIndexMetadata(
jdbcUrls: Seq[String]
)(implicit resourceContext: ResourceContext): Resource[Unit] = {
Expand Down Expand Up @@ -149,7 +152,7 @@ final class Runner[T <: ReadWriteService, Extra](
}
_ <- participantConfig.mode match {
case ParticipantRunMode.Combined | ParticipantRunMode.LedgerApiServer =>
new StandaloneApiServer(
val server = new StandaloneApiServer(
ledgerId = config.ledgerId,
config = factory.apiServerConfig(participantConfig, config),
commandConfig = config.commandConfig,
Expand All @@ -165,6 +168,8 @@ final class Runner[T <: ReadWriteService, Extra](
servicesExecutionContext = servicesExecutionContext,
lfValueTranslationCache = lfValueTranslationCache,
).acquire()
_apiServer = server
server
case ParticipantRunMode.Indexer =>
Resource.unit
}
Expand Down

0 comments on commit a9aaf19

Please sign in to comment.