Skip to content

Commit

Permalink
[JSON-API] Configurable Hikari connection pool props (#11621)
Browse files Browse the repository at this point in the history
* Changes to make certain hikari cp connection pool properties configurable via jdbc conf string

CHANGELOG_BEGIN
[JSON-API] Make certain Hikari cp connection pool properties configurable via jdbc conf string, the properties are listed below
poolSize -- specifies the max pool size for the database connection pool
minIdle -- specifies the min idle connections for database connection pool
connectionTimeout -- long value, specifies the connection timeout for database connection pool
idleTimeout -- long value, specifies the idle timeout for the database connection pool
CHANGELOG_END

* some missed changes for DbTriggerDao

* remove defaults for poolSize on JdbcConfig

* add constants for test defaults
  • Loading branch information
akshayshirahatti-da authored Nov 11, 2021
1 parent cb7099b commit e69a871
Show file tree
Hide file tree
Showing 16 changed files with 122 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package com.daml.http

import com.daml.dbutils
import OracleIntTest.defaultJdbcConfig
import com.daml.dbutils.ConnectionPool
import dbbackend.{DbStartupMode, JdbcConfig}
import dbbackend.OracleQueries.DisableContractPayloadIndexing
import com.daml.testing.oracle.OracleAroundAll
Expand Down Expand Up @@ -33,6 +34,7 @@ object OracleIntTest {
user = user,
password = pwd,
tablePrefix = "some_nice_prefix_",
poolSize = ConnectionPool.PoolSize.Integration,
),
dbStartupMode = DbStartupMode.CreateOnly,
backendSpecificConf =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import scalaz.syntax.tag._
import scalaz.{-\/, EitherT, \/, \/-}

import Config.QueryStoreIndex
import com.daml.dbutils.ConnectionPool

import scala.concurrent.duration.{Duration, _}
import scala.concurrent.{Await, ExecutionContext, Future, Promise, TimeoutException}
Expand Down Expand Up @@ -220,7 +221,13 @@ object Main extends StrictLogging {
import DbStartupMode._
val startupMode: DbStartupMode = if (retainData) CreateIfNeededAndStart else CreateAndStart
JdbcConfig(
dbutils.JdbcConfig("oracle.jdbc.OracleDriver", oracleJdbcUrl, user.name, user.pwd),
dbutils.JdbcConfig(
"oracle.jdbc.OracleDriver",
oracleJdbcUrl,
user.name,
user.pwd,
ConnectionPool.PoolSize.Production,
),
dbStartupMode = startupMode,
)
}
Expand All @@ -240,7 +247,13 @@ object Main extends StrictLogging {
private def jsonApiJdbcConfig(c: PostgresDatabase): JdbcConfig =
JdbcConfig(
dbutils
.JdbcConfig(driver = "org.postgresql.Driver", url = c.url, user = "test", password = ""),
.JdbcConfig(
driver = "org.postgresql.Driver",
url = c.url,
user = "test",
password = "",
ConnectionPool.PoolSize.Production,
),
dbStartupMode = DbStartupMode.CreateOnly,
)

Expand Down
1 change: 0 additions & 1 deletion ledger-service/http-json-testing/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ hj_scalacopts = lf_scalacopts + [
"//ledger/sandbox-common",
"//ledger/sandbox-common:sandbox-common-scala-tests-lib",
"//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 @@ -17,7 +17,6 @@ import com.daml.bazeltools.BazelRunfiles.rlocation
import com.daml.grpc.adapter.ExecutionSequencerFactory
import com.daml.http.HttpService.doLoad
import com.daml.http.dbbackend.{ContractDao, JdbcConfig}
import com.daml.dbutils.ConnectionPool.PoolSize
import com.daml.http.json.{DomainJsonDecoder, DomainJsonEncoder}
import com.daml.http.util.ClientUtil.boxedRecord
import com.daml.http.util.Logging.{InstanceUUID, instanceUUIDLogCtx}
Expand Down Expand Up @@ -266,7 +265,7 @@ object HttpServiceTestFixture extends LazyLogging with Assertions with Inside {
metrics: Metrics,
): Future[ContractDao] =
for {
dao <- Future(ContractDao(c, poolSize = PoolSize.Integration))
dao <- Future(ContractDao(c))
isSuccess <- DbStartupOps
.fromStartupMode(dao, c.dbStartupMode)
.unsafeToFuture()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ import cats.instances.list._
import com.codahale.metrics.MetricRegistry
import doobie.util.log.LogHandler
import com.daml.dbutils
import com.daml.dbutils.ConnectionPool
import com.daml.doobie.logging.Slf4jLogHandler
import com.daml.http.dbbackend.Queries.{DBContract, SurrogateTpId}
import com.daml.http.domain.TemplateId
import com.daml.http.util.Logging.instanceUUIDLogCtx
import com.daml.metrics.Metrics
import com.daml.testing.oracle, oracle.{OracleAround, User}
import com.daml.testing.oracle
import oracle.{OracleAround, User}
import org.openjdk.jmh.annotations._

import scala.concurrent.ExecutionContext
import scalaz.std.list._
import spray.json._
Expand All @@ -39,7 +42,13 @@ abstract class ContractDaoBenchmark extends OracleAround {
connectToOracle()
user = createNewRandomUser()
val cfg = JdbcConfig(
dbutils.JdbcConfig("oracle.jdbc.OracleDriver", oracleJdbcUrl, user.name, user.pwd)
dbutils.JdbcConfig(
"oracle.jdbc.OracleDriver",
oracleJdbcUrl,
user.name,
user.pwd,
poolSize = ConnectionPool.PoolSize.Integration,
)
)
val oracleDao = ContractDao(cfg)
dao = oracleDao
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import akka.http.scaladsl.model.{StatusCodes, Uri}
import akka.stream.{KillSwitches, UniqueKillSwitch}
import akka.stream.scaladsl.{Keep, Sink}
import com.codahale.metrics.MetricRegistry
import com.daml.dbutils.ConnectionPool

import scala.concurrent.{Future, Promise}
import scala.util.{Failure, Success}
Expand Down Expand Up @@ -381,7 +382,13 @@ final class FailureTests
val dao = dbbackend.ContractDao(
JdbcConfig(
// discarding other settings
dbutils.JdbcConfig(driver = bc.driver, url = bc.url, user = bc.user, password = bc.password)
dbutils.JdbcConfig(
driver = bc.driver,
url = bc.url,
user = bc.user,
password = bc.password,
poolSize = ConnectionPool.PoolSize.Integration,
)
)
)
util.Logging
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package com.daml.http
import akka.http.scaladsl.model.Uri
import com.daml.bazeltools.BazelRunfiles
import com.daml.dbutils
import com.daml.dbutils.ConnectionPool
import dbbackend.{DbStartupMode, JdbcConfig}
import com.daml.http.json.{DomainJsonDecoder, DomainJsonEncoder}
import com.daml.ledger.client.withoutledgerid.{LedgerClient => DamlLedgerClient}
Expand Down Expand Up @@ -41,6 +42,7 @@ trait HttpFailureTestFixture extends ToxicSandboxFixture with PostgresAroundAll
s"jdbc:postgresql://${postgresDatabase.hostName}:$dbProxyPort/${postgresDatabase.databaseName}?user=${postgresDatabase.userName}&password=${postgresDatabase.password}",
user = "test",
password = "",
poolSize = ConnectionPool.PoolSize.Integration,
),
dbStartupMode = DbStartupMode.CreateOnly,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package com.daml.http

import com.codahale.metrics.MetricRegistry
import com.daml.dbutils.ConnectionPool.PoolSize
import com.daml.http.dbbackend.JdbcConfig
import com.daml.metrics.Metrics
import com.daml.testing.postgresql.PostgresAroundAll
Expand All @@ -19,7 +18,7 @@ trait HttpServicePostgresInt extends AbstractHttpServiceIntegrationTestFuns with
protected implicit val metics = new Metrics(new MetricRegistry)

// has to be lazy because jdbcConfig_ is NOT initialized yet
protected lazy val dao = dbbackend.ContractDao(jdbcConfig_, poolSize = PoolSize.Integration)
protected lazy val dao = dbbackend.ContractDao(jdbcConfig_)

// has to be lazy because postgresFixture is NOT initialized yet
protected[this] def jdbcConfig_ = PostgresIntTest.defaultJdbcConfig(postgresDatabase.url)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package com.daml.http

import com.daml.dbutils
import com.daml.dbutils.ConnectionPool
import com.daml.http.PostgresIntTest.defaultJdbcConfig
import com.daml.http.dbbackend.{DbStartupMode, JdbcConfig}
import com.daml.testing.postgresql.PostgresAroundAll
Expand All @@ -26,6 +27,7 @@ object PostgresIntTest {
user = "test",
password = "",
tablePrefix = "some_nice_prefix_",
poolSize = ConnectionPool.PoolSize.Integration,
),
dbStartupMode = DbStartupMode.CreateOnly,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package com.daml.http

import cats.effect.IO
import com.codahale.metrics.MetricRegistry
import com.daml.dbutils.ConnectionPool.PoolSize
import com.daml.http.dbbackend.{ContractDao, JdbcConfig}
import com.daml.http.domain.TemplateId
import com.daml.http.util.Logging.{InstanceUUID, instanceUUIDLogCtx}
Expand All @@ -29,14 +28,12 @@ abstract class AbstractDatabaseIntegrationTest extends AsyncFreeSpecLike with Be

// has to be lazy because jdbcConfig is NOT initialized yet
protected lazy val dao = dbbackend.ContractDao(
jdbcConfig.copy(baseConfig = jdbcConfig.baseConfig.copy(tablePrefix = "some_fancy_prefix_")),
poolSize = PoolSize.Integration,
jdbcConfig.copy(baseConfig = jdbcConfig.baseConfig.copy(tablePrefix = "some_fancy_prefix_"))
)

protected lazy val daoWithoutPrefix =
dbbackend.ContractDao(
jdbcConfig.copy(baseConfig = jdbcConfig.baseConfig.copy(tablePrefix = "")),
poolSize = PoolSize.Integration,
jdbcConfig.copy(baseConfig = jdbcConfig.baseConfig.copy(tablePrefix = ""))
)

override protected def afterAll(): Unit = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class ContractDao private (
}

object ContractDao {
import ConnectionPool.PoolSize, SurrogateTemplateIdCache.MaxEntries
import SurrogateTemplateIdCache.MaxEntries
private[this] val supportedJdbcDrivers = Map[String, SupportedJdbcDriver.Available](
"org.postgresql.Driver" -> SupportedJdbcDriver.Postgres,
"oracle.jdbc.OracleDriver" -> SupportedJdbcDriver.Oracle,
Expand All @@ -92,7 +92,6 @@ object ContractDao {
def apply(
cfg: JdbcConfig,
tpIdCacheMaxEntries: Option[Long] = None,
poolSize: PoolSize = PoolSize.Production,
)(implicit
ec: ExecutionContext,
metrics: Metrics,
Expand All @@ -108,9 +107,9 @@ object ContractDao {
} yield {
implicit val sjd: SupportedJdbcDriver.TC = sjdc
//pool for connections awaiting database access
val es = Executors.newWorkStealingPool(poolSize)
val es = Executors.newWorkStealingPool(cfg.baseConfig.poolSize)
val (ds, conn) =
ConnectionPool.connect(cfg.baseConfig, poolSize)(ExecutionContext.fromExecutor(es), cs)
ConnectionPool.connect(cfg.baseConfig)(ExecutionContext.fromExecutor(es), cs)
new ContractDao(ds, conn, es)
}
setup.fold(msg => throw new IllegalArgumentException(msg), identity)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@ package com.daml.http
import org.scalatest.freespec.AnyFreeSpec
import org.scalatest.matchers.should.Matchers
import com.daml.dbutils
import com.daml.http.dbbackend.{JdbcConfig, DbStartupMode}

import com.daml.http.dbbackend.{DbStartupMode, JdbcConfig}

object CliSpec {
private val poolSize = 10
private val minIdle = 4
private val connectionTimeout = 5000L
private val idleTimeout = 1000L
private val tablePrefix = "foo"
}
final class CliSpec extends AnyFreeSpec with Matchers {
import CliSpec._

private def configParser(
parameters: Seq[String],
Expand All @@ -22,11 +30,17 @@ final class CliSpec extends AnyFreeSpec with Matchers {
"jdbc:postgresql://localhost:5432/test?&ssl=true",
"postgres",
"password",
poolSize,
minIdle,
connectionTimeout,
idleTimeout,
tablePrefix,
),
dbStartupMode = DbStartupMode.StartOnly,
)
val jdbcConfigString =
"driver=org.postgresql.Driver,url=jdbc:postgresql://localhost:5432/test?&ssl=true,user=postgres,password=password,createSchema=false"
"driver=org.postgresql.Driver,url=jdbc:postgresql://localhost:5432/test?&ssl=true,user=postgres,password=password," +
s"poolSize=$poolSize,minIdle=$minIdle,connectionTimeout=$connectionTimeout,idleTimeout=$idleTimeout,tablePrefix=$tablePrefix"

val sharedOptions =
Seq("--ledger-host", "localhost", "--ledger-port", "6865", "--http-port", "7500")
Expand Down Expand Up @@ -106,7 +120,8 @@ final class CliSpec extends AnyFreeSpec with Matchers {

"DbStartupMode" - {
val jdbcConfigShared =
"driver=org.postgresql.Driver,url=jdbc:postgresql://localhost:5432/test?&ssl=true,user=postgres,password=password"
"driver=org.postgresql.Driver,url=jdbc:postgresql://localhost:5432/test?&ssl=true,user=postgres,password=password," +
s"poolSize=$poolSize,minIdle=$minIdle,connectionTimeout=$connectionTimeout,idleTimeout=$idleTimeout,tablePrefix=$tablePrefix"

"should get the CreateOnly startup mode from the string" in {
val jdbcConfigString = s"$jdbcConfigShared,start-mode=create-only"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ object Connection {
)
}

/*
TODO below values are hardcoded for now, refactor to be picked up as cli flags/ props later.
*/
object ConnectionPool {

type PoolSize = Int
Expand All @@ -40,27 +37,25 @@ object ConnectionPool {
type T = Transactor.Aux[IO, _ <: DataSource with Closeable]

def connect(
c: JdbcConfig,
poolSize: PoolSize,
c: JdbcConfig
)(implicit
ec: ExecutionContext,
cs: ContextShift[IO],
): (DataSource with Closeable, T) = {
val ds = dataSource(c, poolSize)
val ds = dataSource(c)
(
ds,
Transactor
.fromDataSource[IO](
ds,
connectEC = ec,
blocker = Blocker liftExecutorService newWorkStealingPool(poolSize),
blocker = Blocker liftExecutorService newWorkStealingPool(c.poolSize),
)(IO.ioConcurrentEffect(cs), cs),
)
}

private[this] def dataSource(
jc: JdbcConfig,
poolSize: PoolSize,
jc: JdbcConfig
) = {
import jc._
val c = new HikariConfig
Expand Down
Loading

0 comments on commit e69a871

Please sign in to comment.