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

Move maxConnections into RDSClientConfig, move creation of RDSClientConfig #2817

Merged
merged 2 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import weco.pipeline.id_minter.config.builders.{
IdentifiersTableBuilder,
RDSBuilder
}
import weco.pipeline.id_minter.config.models.RDSClientConfig
import weco.pipeline.id_minter.database.IdentifiersDao
import weco.pipeline.id_minter.models.IdentifiersTable
import weco.pipeline.id_minter.services.IdMinterWorkerService
Expand All @@ -29,9 +30,9 @@ object Main extends WellcomeTypesafeApp {
config: Config =>
implicit val executionContext: ExecutionContext =
ActorSystem("main-actor-system").dispatcher

val rdsConfig = RDSClientConfig(config)
val identifiersTableConfig = IdentifiersTableBuilder.buildConfig(config)
RDSBuilder.buildDB(config)
RDSBuilder.buildDB(rdsConfig)

val identifierGenerator = new IdentifierGenerator(
identifiersDao = new IdentifiersDao(
Expand Down Expand Up @@ -66,7 +67,7 @@ object Main extends WellcomeTypesafeApp {
identifierGenerator = identifierGenerator,
jsonRetriever = jsonRetriever,
pipelineStream = pipelineStream,
rdsClientConfig = RDSBuilder.buildRDSClientConfig(config),
rdsClientConfig = rdsConfig,
identifiersTableConfig = identifiersTableConfig
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
package weco.pipeline.id_minter.config.builders

import com.typesafe.config.Config
import scalikejdbc.{ConnectionPool, ConnectionPoolSettings}
import weco.pipeline.id_minter.config.models.RDSClientConfig
import weco.typesafe.config.builders.EnrichConfig._

object RDSBuilder {
def buildDB(config: Config): Unit = {
val maxConnections = config.requireInt("aws.rds.maxConnections")

val rdsClientConfig = buildRDSClientConfig(config)

buildDB(
maxConnections = maxConnections,
rdsClientConfig = rdsClientConfig
)
}

def buildDB(maxConnections: Int, rdsClientConfig: RDSClientConfig): Unit = {
def buildDB(rdsClientConfig: RDSClientConfig): Unit = {
val connectionPoolSettings = ConnectionPoolSettings(
maxSize = maxConnections,
maxSize = rdsClientConfig.maxConnections,
connectionTimeoutMillis = 120000L
)

Expand All @@ -41,23 +29,4 @@ object RDSBuilder {
)
}

def buildRDSClientConfig(config: Config): RDSClientConfig = {
val primaryHost = config.requireString("aws.rds.primary_host")
val replicaHost = config.requireString("aws.rds.replica_host")

val port = config
.getIntOption("aws.rds.port")
.getOrElse(3306)

val username = config.requireString("aws.rds.username")
val password = config.requireString("aws.rds.password")

RDSClientConfig(
primaryHost = primaryHost,
replicaHost = replicaHost,
port = port,
username = username,
password = password
)
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,36 @@
package weco.pipeline.id_minter.config.models

import com.typesafe.config.Config
import weco.typesafe.config.builders.EnrichConfig._

case class RDSClientConfig(
primaryHost: String,
replicaHost: String,
port: Int,
username: String,
password: String
password: String,
maxConnections: Int
)

object RDSClientConfig {
def apply(config: Config): RDSClientConfig = {
val primaryHost = config.requireString("aws.rds.primary_host")
val replicaHost = config.requireString("aws.rds.replica_host")

val port = config
.getIntOption("aws.rds.port")
.getOrElse(3306)

val username = config.requireString("aws.rds.username")
val password = config.requireString("aws.rds.password")

RDSClientConfig(
primaryHost = primaryHost,
replicaHost = replicaHost,
port = port,
username = username,
password = password,
maxConnections = config.requireInt("aws.rds.maxConnections")
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package weco.pipeline.id_minter.config.builders

import com.typesafe.config.ConfigFactory
import org.scalatest.funspec.AnyFunSpec
import org.scalatest.matchers.should.Matchers
import scalikejdbc.ConnectionPool
import weco.pipeline.id_minter.config.models.RDSClientConfig

import scala.jdk.CollectionConverters._

class RDSBuilderTest extends AnyFunSpec with Matchers {
describe("Building the database client connection pools") {
it("sets up a connection pool for the primary host") {
RDSBuilder.buildDB(
RDSClientConfig(
primaryHost = "realdeal.example.com",
replicaHost = "doppelganger.example.com",
port = 999,
username = "slartibartfast",
password = "ssh_its_a_secret",
maxConnections = 5
)
)
val p = ConnectionPool.get(name = 'primary)
p.url shouldBe "jdbc:mysql://realdeal.example.com:999"
p.user shouldBe "slartibartfast"
p.settings.maxSize shouldBe 5
}

it("sets up a connection pool for the replica host") {
RDSBuilder.buildDB(
RDSClientConfig(
primaryHost = "primary.example.com",
replicaHost = "doppelganger.example.com",
port = 1234,
username = "slartibartfast",
password = "ssh_its_a_secret",
maxConnections = 12
)
)
val p = ConnectionPool.get(name = 'replica)
p.url shouldBe "jdbc:mysql://doppelganger.example.com:1234"
p.user shouldBe "slartibartfast"
p.settings.maxSize shouldBe 12
}
}
describe("Extracting configuration values") {
val rawConfig = Map(
"aws.rds.primary_host" -> "numberone",
"aws.rds.replica_host" -> "numbertwo",
"aws.rds.username" -> "rincewind",
"aws.rds.password" -> "opensesame",
"aws.rds.maxConnections" -> "678"
)
val config = RDSClientConfig(
ConfigFactory.parseMap(rawConfig.asJava)
)
it("extracts the primary host") {
config.primaryHost shouldBe "numberone"
}
it("extracts the replica host") {
config.replicaHost shouldBe "numbertwo"
}
it("extracts the username") {
config.username shouldBe "rincewind"
}
it("extracts the password") {
config.password shouldBe "opensesame"
}
it("extracts the maxConnections") {
config.maxConnections shouldBe 678
}
it("defaults to port 3306") {
config.port shouldBe 3306
}
it("extracts the port value if specified") {
val configWithPort = RDSClientConfig(
ConfigFactory.parseMap((rawConfig + ("aws.rds.port" -> 1234)).asJava)
)
configWithPort.port shouldBe 1234

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ trait IdentifiersDatabase
replicaHost = rdsHost,
port = rdsPort,
username = rdsUsername,
password = rdsPassword
password = rdsPassword,
maxConnections = rdsMaxPoolSize
)

def withIdentifiersTable[R](
Expand Down
Loading