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

Retry connection acquisition in PostgresStorageBackend.createDataSource #10758

Closed
Closed
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 @@ -36,7 +36,8 @@ import com.daml.platform.store.backend.{
import javax.sql.DataSource
import org.postgresql.ds.PGSimpleDataSource

import scala.util.Using
import scala.annotation.tailrec
import scala.util.{Failure, Success, Try, Using}

private[backend] object PostgresStorageBackend
extends StorageBackend[AppendOnlySchema.Batch]
Expand Down Expand Up @@ -257,7 +258,7 @@ private[backend] object PostgresStorageBackend
val pgSimpleDataSource = new PGSimpleDataSource()
pgSimpleDataSource.setUrl(jdbcUrl)

Using.resource(pgSimpleDataSource.getConnection())(checkCompatibility)
Using.resource(tryAcquireConnection(pgSimpleDataSource))(checkCompatibility)

val hookFunctions = List(
dataSourceConfig.postgresConfig.synchronousCommit.toList
Expand Down Expand Up @@ -305,4 +306,27 @@ private[backend] object PostgresStorageBackend
override def lock(id: Int): DBLockStorageBackend.LockId = PGLockId(id.toLong)

override def dbLockSupported: Boolean = true

// TODO Cleanup: Refactor into or re-use an existing utility
private def tryAcquireConnection(dataSource: DataSource, numberOfAttempts: Int = 600)(implicit
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the number of attempts is so high? Is it because of waiting for db availability?

loggingContext: LoggingContext
): Connection = {
@tailrec
def retryAcquiring(attemptsLeft: Int): Connection =
Try(dataSource.getConnection()) match {
case Failure(exception) if attemptsLeft == 0 =>
logger.warn(s"Failed acquiring database connection after $numberOfAttempts", exception)
throw exception
case Failure(_) =>
logger.info(
s"Attempting to acquire database connection (remaining attempts $attemptsLeft/$numberOfAttempts)"
)
retryAcquiring(attemptsLeft - 1)
case Success(connection) =>
logger.info("Acquired database connection")
connection
}

retryAcquiring(numberOfAttempts)
}
}