forked from canton-network/splice
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: sync operator app skeleton #16
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
Open
sadiq1971
wants to merge
5
commits into
feat/dedicated-sync
Choose a base branch
from
feat/sync-operator-node
base: feat/dedicated-sync
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cdf23be
feat: sync operator app skeleton (Scala) [ci]
sadiq1971 75cbcb9
fix: sync operator review fixes [ci]
sadiq1971 8f80966
fix: review follow-ups on the sync operator store and build [ci]
sadiq1971 bca0d2f
fix: review wording on the sync operator app [ci]
sadiq1971 6021ca7
fix: pin the sync operator store's migration id to 0 [ci]
sadiq1971 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
...n/resources/db/migration/canton-network/postgres/stable/V073__sync_operator_acs_store.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| -- ACS store of the sync operator app, which only ingests MemberTraffic. | ||
| create table sync_operator_acs_store( | ||
| like acs_store_template including all, | ||
|
|
||
| -- reestablish foreign key constraint as that one is not copied by the LIKE statement above | ||
| foreign key (store_id) references store_descriptors(id), | ||
|
|
||
| -- index columns | ||
| ---------------- | ||
|
|
||
| -- the member id in a MemberTraffic | ||
| member_traffic_member text, | ||
|
|
||
| -- the synchronizer id in a MemberTraffic. Constant for this store, which is scoped to a single | ||
| -- synchronizer, but kept so the query and index match the SV and scan stores. | ||
| member_traffic_domain text, | ||
|
|
||
| -- the purchased traffic in a MemberTraffic | ||
| total_traffic_purchased bigint | ||
| ); | ||
|
|
||
| create index sync_operator_acs_store_sid_mid_pn_tid_mtm_mtd | ||
| on sync_operator_acs_store (store_id, migration_id, package_name, template_id_qualified_name, | ||
| member_traffic_member, member_traffic_domain) | ||
| where member_traffic_member is not null; | ||
51 changes: 51 additions & 0 deletions
51
...common/src/main/scala/org/lfdecentralizedtrust/splice/store/db/MemberTrafficQueries.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // Copyright (c) 2024 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package org.lfdecentralizedtrust.splice.store.db | ||
|
|
||
| import com.digitalasset.canton.lifecycle.CloseContext | ||
| import com.digitalasset.canton.logging.NamedLogging | ||
| import com.digitalasset.canton.resource.DbStorage | ||
| import com.digitalasset.canton.topology.{Member, SynchronizerId} | ||
| import com.digitalasset.canton.tracing.TraceContext | ||
| import org.lfdecentralizedtrust.splice.codegen.java.splice.decentralizedsynchronizer.MemberTraffic | ||
| import org.lfdecentralizedtrust.splice.store.LimitHelpers | ||
| import org.lfdecentralizedtrust.splice.store.db.AcsQueries.AcsStoreId | ||
| import org.lfdecentralizedtrust.splice.util.QualifiedName | ||
| import org.lfdecentralizedtrust.splice.util.FutureUnlessShutdownUtil.futureUnlessShutdownToFuture | ||
| import slick.jdbc.canton.ActionBasedSQLInterpolation.Implicits.actionBasedSQLInterpolationCanton | ||
|
|
||
| import scala.concurrent.{ExecutionContext, Future} | ||
|
|
||
| /** Shared `MemberTraffic` queries for stores that ingest it with the standard index columns. */ | ||
| trait MemberTrafficQueries extends AcsJdbcTypes with LimitHelpers { this: NamedLogging => | ||
|
|
||
| protected def sumPurchasedMemberTraffic( | ||
| storage: DbStorage, | ||
| acsTableName: String, | ||
| acsStoreId: AcsStoreId, | ||
| migrationId: Long, | ||
| memberId: Member, | ||
| synchronizerId: SynchronizerId, | ||
| )(implicit ec: ExecutionContext, tc: TraceContext, closeContext: CloseContext): Future[Long] = | ||
| for { | ||
| sum <- storage | ||
| .querySingle( | ||
| sql""" | ||
| select sum(total_traffic_purchased) | ||
| from #$acsTableName | ||
| where store_id = $acsStoreId | ||
| and migration_id = $migrationId | ||
| and package_name = ${MemberTraffic.PACKAGE_NAME} | ||
| and template_id_qualified_name = ${QualifiedName( | ||
| MemberTraffic.TEMPLATE_ID_WITH_PACKAGE_ID | ||
| )} | ||
| and member_traffic_member = ${lengthLimited(memberId.toProtoPrimitive)} | ||
| and member_traffic_domain = $synchronizerId | ||
| """.as[Long].headOption, | ||
| // the callers' method name, which is what the DB retry logs report | ||
| "getTotalPurchasedMemberTraffic", | ||
| ) | ||
| .value | ||
| } yield sum.getOrElse(0L) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
217 changes: 217 additions & 0 deletions
217
...perator/src/main/scala/org/lfdecentralizedtrust/splice/syncoperator/SyncOperatorApp.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,217 @@ | ||
| // Copyright (c) 2024 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package org.lfdecentralizedtrust.splice.syncoperator | ||
|
|
||
| import com.daml.grpc.adapter.ExecutionSequencerFactory | ||
| import com.digitalasset.canton.concurrent.FutureSupervisor | ||
| import com.digitalasset.canton.config.CantonRequireTypes.InstanceName | ||
| import com.digitalasset.canton.config.ProcessingTimeout | ||
| import com.digitalasset.canton.lifecycle.LifeCycle | ||
| import com.digitalasset.canton.logging.{NamedLoggerFactory, TracedLogger} | ||
| import com.digitalasset.canton.resource.DbStorage | ||
| import com.digitalasset.canton.time.Clock | ||
| import com.digitalasset.canton.topology.{PartyId, SynchronizerId} | ||
| import com.digitalasset.canton.tracing.{TraceContext, TracerProvider} | ||
| import io.grpc.Status | ||
| import io.opentelemetry.api.trace.Tracer | ||
| import org.apache.pekko.actor.ActorSystem | ||
| import org.lfdecentralizedtrust.splice.config.SharedSpliceAppParameters | ||
| import org.lfdecentralizedtrust.splice.environment.{ | ||
| BaseLedgerConnection, | ||
| Node, | ||
| PackageVersionSupport, | ||
| ParticipantAdminConnection, | ||
| RetryFor, | ||
| SequencerAdminConnection, | ||
| SpliceLedgerClient, | ||
| } | ||
| import org.lfdecentralizedtrust.splice.scan.admin.api.client.ScanConnection | ||
| import org.lfdecentralizedtrust.splice.syncoperator.automation.SyncOperatorAutomationService | ||
| import org.lfdecentralizedtrust.splice.syncoperator.config.SyncOperatorAppBackendConfig | ||
| import org.lfdecentralizedtrust.splice.syncoperator.metrics.SyncOperatorAppMetrics | ||
| import org.lfdecentralizedtrust.splice.syncoperator.store.SyncOperatorStore | ||
| import org.lfdecentralizedtrust.splice.util.HasHealth | ||
|
|
||
| import scala.concurrent.{ExecutionContextExecutor, Future} | ||
|
|
||
| /** Class representing a sync operator app instance. | ||
| * | ||
| * Ingests the traffic purchases made for the synchronizer it operates and grants them on that | ||
| * synchronizer's sequencer. | ||
| * | ||
| * Modelled after Canton's ParticipantNode class. | ||
| */ | ||
| class SyncOperatorApp( | ||
| override val name: InstanceName, | ||
| val config: SyncOperatorAppBackendConfig, | ||
| val appParameters: SharedSpliceAppParameters, | ||
| storage: DbStorage, | ||
| override protected val clock: Clock, | ||
| val loggerFactory: NamedLoggerFactory, | ||
| tracerProvider: TracerProvider, | ||
| futureSupervisor: FutureSupervisor, | ||
| metrics: SyncOperatorAppMetrics, | ||
| )(implicit | ||
| ac: ActorSystem, | ||
| ec: ExecutionContextExecutor, | ||
| esf: ExecutionSequencerFactory, | ||
| tracer: Tracer, | ||
| ) extends Node[SyncOperatorApp.State, Unit]( | ||
| config.operatorUser, | ||
| config.participantClient, | ||
| appParameters, | ||
| loggerFactory, | ||
| tracerProvider, | ||
| futureSupervisor, | ||
| metrics, | ||
| ) { | ||
|
|
||
| override lazy val ports = Map("admin" -> config.adminApi.port) | ||
|
|
||
| override def preInitializeAfterLedgerConnection( | ||
| connection: BaseLedgerConnection, | ||
| ledgerClient: SpliceLedgerClient, | ||
| )(implicit traceContext: TraceContext): Future[Unit] = Future.unit | ||
|
|
||
| override def initialize( | ||
| ledgerClient: SpliceLedgerClient, | ||
| partyId: PartyId, | ||
| preInitializeState: Unit, | ||
| )(implicit traceContext: TraceContext): Future[SyncOperatorApp.State] = { | ||
| for { | ||
| scanConnection <- appInitStep(s"Get scan connection") { | ||
| ScanConnection.singleCached( | ||
| ledgerClient, | ||
| config.scanClient, | ||
| appParameters.upgradesConfig, | ||
| clock, | ||
| retryProvider, | ||
| loggerFactory, | ||
| ) | ||
| } | ||
| participantAdminConnection = new ParticipantAdminConnection( | ||
| config.participantClient.adminApi, | ||
| appParameters.loggingConfig.api, | ||
| loggerFactory, | ||
| metrics.grpcClientMetrics, | ||
| retryProvider, | ||
| ) | ||
| participantId <- appInitStep("Get participant id") { | ||
| participantAdminConnection.getParticipantId() | ||
| } | ||
| dsoParty <- appInitStep("Get DSO party id") { scanConnection.getDsoPartyId() } | ||
| sequencerAdminConnection = new SequencerAdminConnection( | ||
| config.sequencer.adminApi, | ||
| appParameters.loggingConfig.api, | ||
| loggerFactory, | ||
| metrics.grpcClientMetrics, | ||
| retryProvider, | ||
| ) | ||
| synchronizerId <- appInitStep("Get the synchronizer id from the sequencer") { | ||
|
sadiq1971 marked this conversation as resolved.
|
||
| servedSynchronizerId(sequencerAdminConnection) | ||
| } | ||
| storeKey = SyncOperatorStore.Key( | ||
| operatorParty = partyId, | ||
| dsoParty = dsoParty, | ||
| synchronizerId = synchronizerId, | ||
| ) | ||
| store = SyncOperatorStore( | ||
| storeKey, | ||
| storage, | ||
| loggerFactory, | ||
| retryProvider, | ||
| // MIGRATION_ID is frozen network-wide and logical synchronizer upgrades carry a serial id | ||
| // instead, so the store's partition never has to move. | ||
| 0L, | ||
| participantId, | ||
| config.automation.ingestion, | ||
| config.parameters.defaultLimit, | ||
| ) | ||
| globalSynchronizerId <- appInitStep("Get the global synchronizer id") { | ||
| scanConnection.getAmuletRulesDomain()(traceContext) | ||
| } | ||
| readOnlyLedgerConnection = ledgerClient | ||
| .readOnlyConnection( | ||
| this.getClass.getSimpleName, | ||
| loggerFactory, | ||
| ) | ||
| packageVersionSupport = PackageVersionSupport.createPackageVersionSupport( | ||
| globalSynchronizerId, | ||
| readOnlyLedgerConnection, | ||
| loggerFactory, | ||
| ) | ||
| automation = new SyncOperatorAutomationService( | ||
| config.automation, | ||
| clock, | ||
| store, | ||
| storage, | ||
| ledgerClient, | ||
| retryProvider, | ||
| config.parameters, | ||
| loggerFactory, | ||
| packageVersionSupport, | ||
| ) | ||
| } yield { | ||
| SyncOperatorApp.State( | ||
| automation, | ||
| storage, | ||
| store, | ||
| scanConnection, | ||
| participantAdminConnection, | ||
| sequencerAdminConnection, | ||
| loggerFactory.getTracedLogger(SyncOperatorApp.State.getClass), | ||
| timeouts, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| /** The synchronizer the configured sequencer serves. Waits while it is still initializing. */ | ||
| private def servedSynchronizerId( | ||
| sequencerAdminConnection: SequencerAdminConnection | ||
| )(implicit traceContext: TraceContext): Future[SynchronizerId] = | ||
| retryProvider.getValueWithRetries( | ||
| RetryFor.WaitingOnInitDependency, | ||
| "sync_operator_served_synchronizer_id", | ||
| "the sequencer reports the synchronizer it serves", | ||
| sequencerAdminConnection.getStatus.map( | ||
| _.successOption | ||
| .map(_.synchronizerId.logical) | ||
| .getOrElse( | ||
| throw Status.UNAVAILABLE | ||
| .withDescription("Sequencer is not yet initialized") | ||
| .asRuntimeException() | ||
| ) | ||
| ), | ||
| logger, | ||
| ) | ||
|
|
||
| protected[this] override def automationServices(st: SyncOperatorApp.State) = | ||
| Seq(st.automation) | ||
| } | ||
|
|
||
| object SyncOperatorApp { | ||
| case class State( | ||
| automation: SyncOperatorAutomationService, | ||
| storage: DbStorage, | ||
| store: SyncOperatorStore, | ||
| scanConnection: ScanConnection, | ||
| participantAdminConnection: ParticipantAdminConnection, | ||
| sequencerAdminConnection: SequencerAdminConnection, | ||
| logger: TracedLogger, | ||
| timeouts: ProcessingTimeout, | ||
| ) extends AutoCloseable | ||
| with HasHealth { | ||
| override def isHealthy: Boolean = storage.isActive | ||
|
|
||
| override def close(): Unit = | ||
| LifeCycle.close( | ||
| automation, | ||
| storage, | ||
| store, | ||
| scanConnection, | ||
| participantAdminConnection, | ||
| sequencerAdminConnection, | ||
| )(logger) | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.