-
Notifications
You must be signed in to change notification settings - Fork 59
[ci] Run sv rewards trigger during a random time between the round open and one tick #2669
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5845da5
[ci] Run sv rewards trigger during a random time between the round st…
nicu-da 29c6e7b
[ci] add feature flag
nicu-da b471618
[ci] .
nicu-da 9f129dc
[ci] .
nicu-da c642a47
[ci] .
nicu-da c36835e
[ci] .
nicu-da 7b84c77
[ci] .
nicu-da df70d7e
[ci] .
nicu-da eaa1c0b
[ci] why am i dumb
nicu-da f910d90
[ci] .
nicu-da 8669da1
[ci] .
nicu-da 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
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
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
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
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
103 changes: 103 additions & 0 deletions
103
...n/src/main/scala/org/lfdecentralizedtrust/splice/automation/RoundBasedRewardTrigger.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,103 @@ | ||
| // Copyright (c) 2024 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package org.lfdecentralizedtrust.splice.automation | ||
|
|
||
| import com.digitalasset.canton.logging.pretty.Pretty | ||
| import com.digitalasset.canton.tracing.TraceContext | ||
| import io.opentelemetry.api.trace.Tracer | ||
| import org.apache.pekko.stream.Materializer | ||
| import org.lfdecentralizedtrust.splice.automation.RoundBasedRewardTrigger.RoundBasedTask | ||
|
|
||
| import java.time.{Duration, Instant} | ||
| import java.util.concurrent.atomic.AtomicReference | ||
| import scala.concurrent.{ExecutionContext, Future} | ||
| import scala.util.Random | ||
|
|
||
| abstract class RoundBasedRewardTrigger[T <: RoundBasedTask: Pretty]()(implicit | ||
| ec: ExecutionContext, | ||
| mat: Materializer, | ||
| tracer: Tracer, | ||
| ) extends PollingParallelTaskExecutionTrigger[T] { | ||
| private val nextRunTime = new AtomicReference[Option[(Long, Instant)]](None) | ||
|
|
||
| private val isNewSchedulingLogicEnabled: Boolean = context.config.enableNewRewardTriggerScheduling | ||
|
|
||
| // if the new logic is disable then use the old behaviour that uses increased polling intervals | ||
| override protected def isRewardOperationTrigger: Boolean = !isNewSchedulingLogicEnabled | ||
|
|
||
| override protected def retrieveTasks()(implicit tc: TraceContext): Future[Seq[T]] = { | ||
| if (isNewSchedulingLogicEnabled) { | ||
| if (shouldRun) { | ||
| retrieveAvailableTasksForRound().map(tasks => { | ||
| tasks.minByOption(_.roundDetails._1) match { | ||
| case Some(firstRound) => | ||
| val (roundNumber, roundOpening) = firstRound.roundDetails | ||
| val lastRunWasForAnOlderRound = nextRunTime.get().forall(_._1 < roundNumber) | ||
| if (lastRunWasForAnOlderRound) { | ||
| @SuppressWarnings(Array("org.wartremover.warts.IterableOps")) | ||
| val minRunTime = Seq(roundOpening, context.clock.now.toInstant).max | ||
| val maxRunTime = roundOpening.plus(firstRound.tickDuration) | ||
| val minRunAt = randomInstantBetween(minRunTime, maxRunTime) | ||
| nextRunTime.set( | ||
| Some( | ||
| roundNumber -> minRunAt | ||
| ) | ||
| ) | ||
| if (shouldRun) { | ||
| logger.info( | ||
| s"Running for $tasks because the calculated run time $minRunAt is now (between $minRunTime and $maxRunTime)." | ||
| ) | ||
| tasks | ||
| } else { | ||
| logger.info( | ||
| s"Will run for $tasks at min time $minRunAt (computed for interval between $minRunTime and $maxRunTime)." | ||
| ) | ||
| Seq.empty | ||
| } | ||
| } else { | ||
| logger.info( | ||
| s"Running for $tasks as the round still matches the next run ${nextRunTime.get()}." | ||
| ) | ||
| tasks | ||
| } | ||
| case None => | ||
| // no tasks available | ||
| tasks | ||
| } | ||
| }) | ||
| } else Future.successful(Seq.empty) | ||
| } else { | ||
| retrieveAvailableTasksForRound() | ||
| } | ||
| } | ||
|
|
||
| protected def retrieveAvailableTasksForRound()(implicit tc: TraceContext): Future[Seq[T]] | ||
|
|
||
| private def shouldRun = { | ||
| nextRunTime | ||
| .get() | ||
| .fold(true) { case (_, runAt) => | ||
| runAt.isBefore(context.clock.now.toInstant) || runAt.equals(context.clock.now.toInstant) | ||
| } | ||
| } | ||
|
|
||
| private def randomInstantBetween(start: Instant, end: Instant): Instant = { | ||
| val range = Duration.between(start, end) | ||
| if (start.isBefore(end) || range.toMillis <= 0) | ||
| start | ||
| else { | ||
| val randomMillisInRange = Random.nextLong(range.toMillis) | ||
| start.plusMillis(randomMillisInRange) | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| object RoundBasedRewardTrigger { | ||
|
|
||
| trait RoundBasedTask { | ||
| def roundDetails: (Long, Instant) | ||
| def tickDuration: Duration | ||
| } | ||
| } |
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
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
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.