ledger-api-test-tool: Retry the participant connection for a minute. (#7475)

This means we can stop writing loops to wait for the ledger to come up
wherever this is used.

CHANGELOG_BEGIN
CHANGELOG_END
This commit is contained in:
Samir Talwar 2020-09-24 10:29:29 +02:00 committed by GitHub
parent 20732edc32
commit 40b893e9bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 11 deletions

View File

@ -9,6 +9,7 @@ import java.nio.file.{Files, Paths, StandardCopyOption}
import com.daml.ledger.api.testtool.infrastructure.Reporter.ColorizedPrintStreamReporter
import com.daml.ledger.api.testtool.infrastructure.{
Dars,
Errors,
LedgerSessionConfiguration,
LedgerTestCase,
LedgerTestCasesRunner,
@ -161,8 +162,12 @@ object LedgerApiTestTool {
config.verbose,
).report(summaries)
sys.exit(exitCode(summaries, config.mustFail))
case Failure(e) =>
logger.error(e.getMessage, e)
case Failure(exception: Errors.FrameworkException) =>
logger.error(exception.getMessage)
logger.debug(exception.getMessage, exception)
sys.exit(1)
case Failure(exception) =>
logger.error(exception.getMessage, exception)
sys.exit(1)
}
}

View File

@ -0,0 +1,14 @@
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package com.daml.ledger.api.testtool.infrastructure
object Errors {
sealed abstract class FrameworkException(message: String, cause: Throwable)
extends RuntimeException(message, cause)
final class ParticipantConnectionException(address: String, cause: Throwable)
extends FrameworkException(s"Could not connect to the participant at $address.", cause)
}

View File

@ -3,7 +3,7 @@
package com.daml.ledger.api.testtool.infrastructure.participant
import com.daml.ledger.api.testtool.infrastructure.LedgerServices
import com.daml.ledger.api.testtool.infrastructure.{Errors, LedgerServices}
import com.daml.ledger.api.v1.ledger_identity_service.GetLedgerIdentityRequest
import com.daml.ledger.api.v1.transaction_service.GetLedgerEndRequest
import com.daml.timer.RetryStrategy
@ -12,6 +12,8 @@ import org.slf4j.LoggerFactory
import scala.concurrent.duration.DurationInt
import scala.concurrent.{ExecutionContext, Future}
import scala.util.Failure
import scala.util.control.NonFatal
private[infrastructure] final class ParticipantSession private (
config: ParticipantSessionConfiguration,
@ -56,11 +58,22 @@ object ParticipantSession {
)(implicit executionContext: ExecutionContext): Future[ParticipantSession] = {
val services = new LedgerServices(channel)
for {
ledgerId <- RetryStrategy.exponentialBackoff(10, 10.millis) { (attempt, wait) =>
logger.debug(
s"Fetching ledgerId to create context (attempt #$attempt, next one in $wait)...")
services.identity.getLedgerIdentity(new GetLedgerIdentityRequest).map(_.ledgerId)
}
// Keep retrying for about a minute.
ledgerId <- RetryStrategy
.exponentialBackoff(10, 100.millis) { (attempt, wait) =>
services.identity
.getLedgerIdentity(new GetLedgerIdentityRequest)
.map(_.ledgerId)
.andThen {
case Failure(_) =>
logger.info(
s"Could not connect to the participant (attempt #$attempt). Trying again in $wait...")
}
}
.recoverWith {
case NonFatal(exception) =>
Future.failed(new Errors.ParticipantConnectionException(config.address, exception))
}
} yield new ParticipantSession(config, services, ledgerId)
}
}

View File

@ -69,9 +69,10 @@ object ParticipantSessionManager {
}
channelBuilder.maxInboundMessageSize(10000000)
val channel = channelBuilder.build()
logger.info(s"Connected to participant at ${config.address}.")
ParticipantSession(config, channel).map(session =>
new Session(config, session, channel, eventLoopGroup))
ParticipantSession(config, channel).map { session =>
logger.info(s"Connected to participant at ${config.address}.")
new Session(config, session, channel, eventLoopGroup)
}
}
private final class Session(