Skip to content

Commit

Permalink
Changed config setup for default values. Also turned SQLConnectionPoo…
Browse files Browse the repository at this point in the history
…l to a RawService (#390)
  • Loading branch information
bgaidioz authored Mar 26, 2024
1 parent 65c951b commit 88444a4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 23 deletions.
8 changes: 7 additions & 1 deletion snapi-frontend/src/main/resources/reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ raw {
}
}
}
raw.creds.client {
fdw-db-cache {
size = 1000
expiry = 1h
}
}
raw.rest.client {
async-request-retries = 10

Expand Down Expand Up @@ -117,4 +123,4 @@ raw.sources.s3 {
tmp-dir = ${java.io.tmpdir}/s3

default-region = eu-west-1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@ package raw.creds.client

import com.google.common.cache.{CacheBuilder, CacheLoader, LoadingCache}
import raw.creds.api._
import raw.creds.client.ClientCredentialsService.{
CACHE_FDW_EXPIRY_IN_HOURS,
CACHE_FDW_SIZE,
DEFAULT_CACHE_FDW_EXPIRY_IN_HOURS,
DEFAULT_CACHE_FDW_SIZE,
SERVER_ADDRESS
}
import raw.creds.client.ClientCredentialsService.{CACHE_FDW_EXPIRY, CACHE_FDW_SIZE, SERVER_ADDRESS}
import raw.rest.client.APIException
import raw.utils.{AuthenticatedUser, RawSettings}

Expand All @@ -30,9 +24,7 @@ import java.util.concurrent.TimeUnit
object ClientCredentialsService {
private val SERVER_ADDRESS = "raw.creds.client.server-address"
private val CACHE_FDW_SIZE = "raw.creds.client.fdw-db-cache.size"
private val DEFAULT_CACHE_FDW_SIZE = 1000
private val CACHE_FDW_EXPIRY_IN_HOURS = "raw.creds.client.fdw-db-cache.expiry-in-hours"
private val DEFAULT_CACHE_FDW_EXPIRY_IN_HOURS = 1
private val CACHE_FDW_EXPIRY = "raw.creds.client.fdw-db-cache.expiry"
}

class ClientCredentialsService(implicit settings: RawSettings) extends CredentialsService {
Expand All @@ -51,11 +43,8 @@ class ClientCredentialsService(implicit settings: RawSettings) extends Credentia

private val dbCache: LoadingCache[AuthenticatedUser, String] = CacheBuilder
.newBuilder()
.maximumSize(settings.getIntOpt(CACHE_FDW_SIZE).getOrElse(DEFAULT_CACHE_FDW_SIZE).toLong)
.expireAfterAccess(
settings.getIntOpt(CACHE_FDW_EXPIRY_IN_HOURS).getOrElse(DEFAULT_CACHE_FDW_EXPIRY_IN_HOURS).toLong,
TimeUnit.HOURS
)
.maximumSize(settings.getInt(CACHE_FDW_SIZE).toLong)
.expireAfterAccess(settings.getDuration(CACHE_FDW_EXPIRY))
.build(dbCacheLoader)

/** S3 buckets */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ class SqlCompilerService(maybeClassLoader: Option[ClassLoader] = None)(implicit
}

override def doStop(): Unit = {
connectionPool.stop()
credentials.stop()
}

Expand Down
24 changes: 17 additions & 7 deletions sql-client/src/main/scala/raw/client/sql/SqlConnectionPool.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ package raw.client.sql
import com.typesafe.scalalogging.StrictLogging
import com.zaxxer.hikari.{HikariConfig, HikariDataSource}
import raw.creds.api.CredentialsService
import raw.utils.{AuthenticatedUser, RawSettings}
import raw.utils.{AuthenticatedUser, RawService, RawSettings, RawUtils}

import java.sql.SQLException
import java.util.concurrent.TimeUnit
import scala.collection.mutable

class SqlConnectionPool(credentialsService: CredentialsService)(implicit settings: RawSettings) extends StrictLogging {
class SqlConnectionPool(credentialsService: CredentialsService)(implicit settings: RawSettings)
extends RawService
with StrictLogging {

// One pool of connections per DB (which means per user).
private val pools = mutable.Map.empty[String, HikariDataSource]
Expand All @@ -33,14 +35,16 @@ class SqlConnectionPool(credentialsService: CredentialsService)(implicit setting
def getConnection(user: AuthenticatedUser): java.sql.Connection = {
val db = credentialsService.getUserDb(user)
logger.debug(s"Got database $db for user $user")
getConnection(user, db)
getConnection(db)
}

@throws[SQLException]
def getConnection(user: AuthenticatedUser, db: String): java.sql.Connection = {
val userPool = pools.get(db) match {
case Some(pool) => pool
private def getConnection(db: String): java.sql.Connection = {
val pool = pools.get(db) match {
case Some(existingPool) => existingPool
case None =>
// Create a pool and store it in `pools`.
logger.info(s"Creating a SQL connection pool for database $db")
val config = new HikariConfig()
config.setJdbcUrl(s"jdbc:postgresql://$dbHost:$dbPort/$db")
config.setMaximumPoolSize(settings.getInt("raw.client.sql.pool.max-connections"))
Expand All @@ -56,7 +60,13 @@ class SqlConnectionPool(credentialsService: CredentialsService)(implicit setting
pools.put(db, pool)
pool
}
userPool.getConnection
pool.getConnection
}

override def doStop(): Unit = {
for ((db, pool) <- pools) {
logger.info(s"Shutting down SQL connection pool for database $db")
RawUtils.withSuppressNonFatalException(pool.close())
}
}
}

0 comments on commit 88444a4

Please sign in to comment.