Skip to content

Commit

Permalink
[DPP-646][Self-service error codes] Adopt ApiPackageManagementService…
Browse files Browse the repository at this point in the history
… [WIP]

CHANGELOG_BEGIN
CHANGELOG_END
  • Loading branch information
pbatko-da committed Oct 20, 2021
1 parent 5f5af30 commit d0c09d8
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ private[daml] object ApiServices {
writeService,
managementServiceTimeout,
engine,
errorsVersionsSwitcher,
)

val apiConfigManagementService = ApiConfigManagementService.createApiService(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import java.util.zip.ZipInputStream
import akka.stream.Materializer
import akka.stream.scaladsl.Source
import com.daml.daml_lf_dev.DamlLf.Archive
import com.daml.error.{DamlContextualizedErrorLogger, ContextualizedErrorLogger}
import com.daml.error.{
ContextualizedErrorLogger,
DamlContextualizedErrorLogger,
ErrorCodesVersionSwitcher,
}
import com.daml.ledger.api.domain.{LedgerOffset, PackageEntry}
import com.daml.ledger.api.v1.admin.package_management_service.PackageManagementServiceGrpc.PackageManagementService
import com.daml.ledger.api.v1.admin.package_management_service._
Expand Down Expand Up @@ -47,6 +51,7 @@ private[apiserver] final class ApiPackageManagementService private (
engine: Engine,
darReader: GenDarReader[Archive],
submissionIdGenerator: String => Ref.SubmissionId,
errorCodesVersionSwitcher: ErrorCodesVersionSwitcher,
)(implicit
materializer: Materializer,
executionContext: ExecutionContext,
Expand All @@ -58,11 +63,14 @@ private[apiserver] final class ApiPackageManagementService private (
private implicit val contextualizedErrorLogger: ContextualizedErrorLogger =
new DamlContextualizedErrorLogger(logger, loggingContext, None)

private val errorFactories = ErrorFactories(errorCodesVersionSwitcher)

private val synchronousResponse = new SynchronousResponse(
new SynchronousResponseStrategy(
transactionsService,
packagesIndex,
packagesWrite,
errorFactories,
),
timeToLive = managementServiceTimeout,
)
Expand Down Expand Up @@ -120,7 +128,7 @@ private[apiserver] final class ApiPackageManagementService private (
ValidationLogger
.logFailureWithContext(
request,
ErrorFactories.invalidArgument(None)(err.getMessage),
errorFactories.invalidArgument(None)(err.getMessage),
)
),
Future.successful,
Expand All @@ -146,6 +154,7 @@ private[apiserver] object ApiPackageManagementService {
writeBackend: state.WritePackagesService,
managementServiceTimeout: Duration,
engine: Engine,
errorCodesVersionSwitcher: ErrorCodesVersionSwitcher,
darReader: GenDarReader[Archive] = DarParser,
submissionIdGenerator: String => Ref.SubmissionId = augmentSubmissionId,
)(implicit
Expand All @@ -161,12 +170,14 @@ private[apiserver] object ApiPackageManagementService {
engine,
darReader,
submissionIdGenerator,
errorCodesVersionSwitcher,
)

private final class SynchronousResponseStrategy(
ledgerEndService: LedgerEndService,
packagesIndex: IndexPackagesService,
packagesWrite: state.WritePackagesService,
errorFactories: ErrorFactories,
)(implicit executionContext: ExecutionContext, loggingContext: LoggingContext)
extends SynchronousResponse.Strategy[
Dar[Archive],
Expand Down Expand Up @@ -198,7 +209,9 @@ private[apiserver] object ApiPackageManagementService {
submissionId: Ref.SubmissionId
): PartialFunction[PackageEntry, StatusRuntimeException] = {
case PackageEntry.PackageUploadRejected(`submissionId`, _, reason) =>
ErrorFactories.invalidArgument(None)(reason)
// TODO error-codes: Different status than actual: ALREADY_EXISTS or FAILED_PRECONDITION
// TODO PBATKO
errorFactories.invalidArgument(None)(reason)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ package com.daml.platform.apiserver.services.admin
import java.time.{Duration, Instant}
import java.util.concurrent.{CompletableFuture, CompletionStage}
import java.util.zip.ZipInputStream

import akka.stream.scaladsl.Source
import com.daml.daml_lf_dev.DamlLf
import com.daml.daml_lf_dev.DamlLf.Archive
import com.daml.error.ErrorCodesVersionSwitcher
import com.daml.ledger.api.domain.LedgerOffset.Absolute
import com.daml.ledger.api.domain.PackageEntry
import com.daml.ledger.api.testing.utils.AkkaBeforeAndAfterAll
Expand Down Expand Up @@ -49,55 +49,64 @@ class ApiPackageManagementServiceSpec

private implicit val loggingContext: LoggingContext = LoggingContext.ForTesting

"ApiPackageManagementService" should {
"propagate trace context" in {
val apiService = createApiService()

val span = anEmptySpan()
val scope = span.makeCurrent()
apiService
.uploadDarFile(UploadDarFileRequest(ByteString.EMPTY, aSubmissionId))
.andThen { case _ =>
scope.close()
span.end()
}
.map { _ =>
spanExporter.finishedSpanAttributes should contain(anApplicationIdSpanAttribute)
}
addTests(true)
addTests(false)

def addTests(useSelfServiceErrorCodes: Boolean): Unit = {
val suffix = s"(enableSelfServiceErrorCodes=${useSelfServiceErrorCodes})"
val errorCodesVersionSwitcher = new ErrorCodesVersionSwitcher(useSelfServiceErrorCodes)

s"ApiPackageManagementService $suffix" should {
"propagate trace context" in {
val apiService = createApiService()

val span = anEmptySpan()
val scope = span.makeCurrent()
apiService
.uploadDarFile(UploadDarFileRequest(ByteString.EMPTY, aSubmissionId))
.andThen { case _ =>
scope.close()
span.end()
}
.map { _ =>
spanExporter.finishedSpanAttributes should contain(anApplicationIdSpanAttribute)
}
}
}
}

private def createApiService(): PackageManagementServiceGrpc.PackageManagementService = {
val mockDarReader = mock[GenDarReader[Archive]]
when(mockDarReader.readArchive(any[String], any[ZipInputStream], any[Int]))
.thenReturn(Right(new Dar[Archive](anArchive, List.empty)))

val mockEngine = mock[Engine]
when(
mockEngine.validatePackages(any[Map[PackageId, Ast.Package]])
).thenReturn(Right(()))

val mockIndexTransactionsService = mock[IndexTransactionsService]
when(mockIndexTransactionsService.currentLedgerEnd())
.thenReturn(Future.successful(Absolute(Ref.LedgerString.assertFromString("0"))))

val mockIndexPackagesService = mock[IndexPackagesService]
when(mockIndexPackagesService.packageEntries(any[Option[Absolute]])(any[LoggingContext]))
.thenReturn(
Source.single(
PackageEntry.PackageUploadAccepted(aSubmissionId, Instant.EPOCH)
def createApiService(): PackageManagementServiceGrpc.PackageManagementService = {
val mockDarReader = mock[GenDarReader[Archive]]
when(mockDarReader.readArchive(any[String], any[ZipInputStream], any[Int]))
.thenReturn(Right(new Dar[Archive](anArchive, List.empty)))

val mockEngine = mock[Engine]
when(
mockEngine.validatePackages(any[Map[PackageId, Ast.Package]])
).thenReturn(Right(()))

val mockIndexTransactionsService = mock[IndexTransactionsService]
when(mockIndexTransactionsService.currentLedgerEnd())
.thenReturn(Future.successful(Absolute(Ref.LedgerString.assertFromString("0"))))

val mockIndexPackagesService = mock[IndexPackagesService]
when(mockIndexPackagesService.packageEntries(any[Option[Absolute]])(any[LoggingContext]))
.thenReturn(
Source.single(
PackageEntry.PackageUploadAccepted(aSubmissionId, Instant.EPOCH)
)
)
)

ApiPackageManagementService.createApiService(
mockIndexPackagesService,
mockIndexTransactionsService,
TestWritePackagesService,
Duration.ZERO,
mockEngine,
mockDarReader,
_ => Ref.SubmissionId.assertFromString("aSubmission"),
)
ApiPackageManagementService.createApiService(
mockIndexPackagesService,
mockIndexTransactionsService,
TestWritePackagesService,
Duration.ZERO,
mockEngine,
errorCodesVersionSwitcher,
mockDarReader,
_ => Ref.SubmissionId.assertFromString("aSubmission"),
)
}
}
}

Expand Down

0 comments on commit d0c09d8

Please sign in to comment.