Skip to content

Commit

Permalink
Supply jdbc string through an env variable (#7660)
Browse files Browse the repository at this point in the history
* Supply jdbc string through an env variable

* address review comments
  • Loading branch information
mziolekda authored Oct 13, 2020
1 parent 60efe06 commit 22fb8d4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
1 change: 1 addition & 0 deletions ledger/participant-state/kvutils/app/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ da_scala_test_suite(
srcs = glob(["src/test/**/*.scala"]),
deps = [
":app",
"//daml-lf/data",
"//ledger/ledger-api-health",
"//ledger/participant-state",
"//ledger/participant-state/kvutils",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ object Config {
extraOptions: OptionParser[Config[Extra]] => Unit,
defaultExtra: Extra,
args: Seq[String],
getEnvVar: String => Option[String] = sys.env.get(_),
): Option[Config[Extra]] =
parser(name, extraOptions).parse(args, createDefault(defaultExtra)).flatMap {
parser(name, extraOptions, getEnvVar).parse(args, createDefault(defaultExtra)).flatMap {
case config if config.mode == Mode.Run && config.participants.isEmpty =>
System.err.println("No --participant provided to run")
None
Expand All @@ -87,6 +88,7 @@ object Config {
private def parser[Extra](
name: String,
extraOptions: OptionParser[Config[Extra]] => Unit,
getEnvVar: String => Option[String],
): OptionParser[Config[Extra]] = {
val parser: OptionParser[Config[Extra]] = new OptionParser[Config[Extra]](name) {
head(name)
Expand All @@ -108,8 +110,12 @@ object Config {
throw new RuntimeException(
s"$unknownMode is not a valid run mode. Valid modes are: combined, indexer, ledger-api-server. Default mode is combined.")
}
val jdbcUrlFromEnv =
kv.get("server-jdbc-url-env").flatMap(getEnvVar(_))
val jdbcUrl =
kv.getOrElse("server-jdbc-url", ParticipantConfig.defaultIndexJdbcUrl(participantId))
kv.getOrElse(
"server-jdbc-url",
jdbcUrlFromEnv.getOrElse(ParticipantConfig.defaultIndexJdbcUrl(participantId)))
val maxCommandsInFlight = kv.get("max-commands-in-flight").map(_.toInt)
val managementServiceTimeout = kv
.get("management-service-timeout")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,66 @@

package com.daml.ledger.participant.state.kvutils.app

import com.daml.ledger.participant.state.v1.ParticipantId
import org.scalatest.{FlatSpec, Matchers, OptionValues}
import scopt.OptionParser

final class ConfigSpec extends FlatSpec with Matchers with OptionValues {

private val DumpIndexMetadataCommand = "dump-index-metadata"
private val ConfigParser: Seq[String] => Option[Config[Unit]] =
Config.parse("Test", (_: OptionParser[Config[Unit]]) => (), (), _)
private val dumpIndexMetadataCommand = "dump-index-metadata"
private val participantOption = "--participant"
private val participantId: ParticipantId = ParticipantId.assertFromString("dummy-participant")
private val fixedParticipantSubOptions = s"participant-id=$participantId,port=123"
private val jdbcUrlSubOption = "server-jdbc-url"
private val jdbcUrlEnvSubOption = "server-jdbc-url-env"
private def configParser(
parameters: Seq[String],
getEnvVar: String => Option[String] = (_ => None)): Option[Config[Unit]] =
Config.parse("Test", (_: OptionParser[Config[Unit]]) => (), (), parameters, getEnvVar)

behavior of "Runner"

it should "fail if a participant is not provided in run mode" in {
ConfigParser(Seq.empty) shouldEqual None
configParser(Seq.empty) shouldEqual None
}

it should "fail if a participant is not provided when dumping the index metadata" in {
ConfigParser(Seq(DumpIndexMetadataCommand)) shouldEqual None
configParser(Seq(dumpIndexMetadataCommand)) shouldEqual None
}

it should "succeed if a participant is provided when dumping the index metadata" in {
ConfigParser(Seq(DumpIndexMetadataCommand, "some-jdbc-url"))
configParser(Seq(dumpIndexMetadataCommand, "some-jdbc-url"))
}

it should "succeed if more than one participant is provided when dumping the index metadata" in {
ConfigParser(Seq(DumpIndexMetadataCommand, "some-jdbc-url", "some-other-jdbc-url"))
configParser(Seq(dumpIndexMetadataCommand, "some-jdbc-url", "some-other-jdbc-url"))
}

it should "get the jdbc string from the command line argument when provided" in {
val jdbcFromCli = "command-line-jdbc"
val config = configParser(
Seq(participantOption, s"$fixedParticipantSubOptions,$jdbcUrlSubOption=$jdbcFromCli"))
.getOrElse(fail())
config.participants.head.serverJdbcUrl should be(jdbcFromCli)
}

it should "get the jdbc string from the environment when provided" in {
val jdbcEnvVar = "JDBC_ENV_VAR"
val jdbcFromEnv = "env-jdbc"
val config = configParser(
Seq(participantOption, s"$fixedParticipantSubOptions,$jdbcUrlEnvSubOption=$jdbcEnvVar"),
{ case `jdbcEnvVar` => Some(jdbcFromEnv) }
).getOrElse(fail())
config.participants.head.serverJdbcUrl should be(jdbcFromEnv)
}

it should "return the default when env variable not provided" in {
val jdbcEnvVar = "JDBC_ENV_VAR"
val defaultJdbc = ParticipantConfig.defaultIndexJdbcUrl(participantId)
val config = configParser(
Seq(participantOption, s"$fixedParticipantSubOptions,$jdbcUrlEnvSubOption=$jdbcEnvVar")
).getOrElse(fail())
config.participants.head.serverJdbcUrl should be(defaultJdbc)
}

}

0 comments on commit 22fb8d4

Please sign in to comment.