Skip to content

Commit

Permalink
Retry efficiency update (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
gvonness committed Jul 12, 2023
1 parent 5e7c1d3 commit 911216f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 7 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ThisBuild / baseVersion := "0.9.1"
ThisBuild / baseVersion := "0.9.2"

ThisBuild / organization := "ai.entrolution"
ThisBuild / organizationName := "Greg von Nessi"
Expand Down
5 changes: 4 additions & 1 deletion src/main/scala/bengal/stm/STM.scala
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,17 @@ object STM {
idGenVar <- Ref.of[F, Long](0)
idGenTxn <- Ref.of[F, Long](0)
graphBuilderSemaphore <- Semaphore[F](1)
retrySemaphore <- Semaphore[F](1)
stm <- Async[F].delay {
new STM[F] {
override val txnVarIdGen: Ref[F, TxnVarId] = idGenVar
override val txnIdGen: Ref[F, TxnId] = idGenTxn

val txnRuntime: TxnRuntime = new TxnRuntime {
override val scheduler: TxnScheduler =
TxnScheduler(graphBuilderSemaphore)
TxnScheduler(graphBuilderSemaphore = graphBuilderSemaphore,
retrySemaphore = retrySemaphore
)
}

override def allocateTxnVar[V](value: V): F[TxnVar[F, V]] =
Expand Down
62 changes: 57 additions & 5 deletions src/main/scala/bengal/stm/runtime/TxnRuntimeContext.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,50 @@ private[stm] trait TxnRuntimeContext[F[_]] {

private[stm] case class TxnScheduler(
graphBuilderSemaphore: Semaphore[F],
activeTransactions: MutableMap[TxnId, AnalysedTxn[_]]
activeTransactions: MutableMap[TxnId, AnalysedTxn[_]],
retrySemaphore: Semaphore[F],
retryMap: MutableMap[IdFootprint, F[Unit]]
) {
override val toString: String = "TxnScheduler"

def checkRetryQueue(idFootprint: IdFootprint): F[Unit] =
for {
_ <- retrySemaphore.acquire
triggeredFootprints <-
retryMap.keys.toList.parTraverse { waitingFootprint =>
Async[F].ifM(
Async[F].delay(
!idFootprint.isCompatibleWith(waitingFootprint)
)
)(
retryMap(waitingFootprint) >> Async[F].pure(
Option(waitingFootprint)
),
Async[F].pure(None.asInstanceOf[Option[IdFootprint]])
)
}.map(_.flatten)
_ <- triggeredFootprints.traverse(footprint =>
Async[F].delay(retryMap.remove(footprint))
)
_ <- retrySemaphore.release
} yield ()

def submitTxnForRetry(analysedTxn: AnalysedTxn[_]): F[Unit] =
for {
_ <- retrySemaphore.acquire
footprint <- Async[F].delay(analysedTxn.idFootprint)
execSpec <- Async[F].delay(retryMap.get(footprint))
_ <- Async[F].delay(execSpec match {
case Some(spec) =>
retryMap.update(footprint,
spec >> submitTxn(analysedTxn).start.void
)
case None =>
retryMap.addOne(footprint -> submitTxn(analysedTxn).start.void)
})
_ <- retrySemaphore.release
} yield ()

def submitTxnForImmediateRetry(analysedTxn: AnalysedTxn[_]): F[Unit] =
for {
_ <- analysedTxn.resetDependencyTally
Expand Down Expand Up @@ -108,6 +148,7 @@ private[stm] trait TxnRuntimeContext[F[_]] {
_ <- testAndLink.parTraverse(_.joinWithNever)
_ <- analysedTxn.checkExecutionReadiness
_ <- graphBuilderSemaphore.release
_ <- checkRetryQueue(analysedTxn.idFootprint).start
} yield ()

def submitTxn(analysedTxn: AnalysedTxn[_]): F[Unit] =
Expand Down Expand Up @@ -135,6 +176,7 @@ private[stm] trait TxnRuntimeContext[F[_]] {
_ <- testAndLink.parTraverse(_.joinWithNever)
_ <- analysedTxn.checkExecutionReadiness
_ <- graphBuilderSemaphore.release
_ <- checkRetryQueue(analysedTxn.idFootprint).start
} yield ()

def registerCompletion(analysedTxn: AnalysedTxn[_]): F[Unit] =
Expand All @@ -156,11 +198,14 @@ private[stm] trait TxnRuntimeContext[F[_]] {
private[stm] object TxnScheduler {

private[stm] def apply(
graphBuilderSemaphore: Semaphore[F]
graphBuilderSemaphore: Semaphore[F],
retrySemaphore: Semaphore[F]
): TxnScheduler =
TxnScheduler(
activeTransactions = MutableMap(),
graphBuilderSemaphore = graphBuilderSemaphore
graphBuilderSemaphore = graphBuilderSemaphore,
retrySemaphore = retrySemaphore,
retryMap = MutableMap()
)

}
Expand All @@ -173,11 +218,12 @@ private[stm] trait TxnRuntimeContext[F[_]] {
dependencyTally: Ref[F, Int],
unsubSpecs: MutableMap[TxnId, F[Unit]],
executionStatus: Ref[F, ExecutionStatus],
hasDownstream: Ref[F, Boolean],
scheduler: TxnScheduler
) {

private[stm] val resetDependencyTally: F[Unit] =
dependencyTally.set(0)
dependencyTally.set(0) >> hasDownstream.set(false)

private[stm] val checkExecutionReadiness: F[Unit] =
Async[F].ifM(dependencyTally.get.map(_ == 0))(
Expand Down Expand Up @@ -208,6 +254,7 @@ private[stm] trait TxnRuntimeContext[F[_]] {
txn.id -> txn.unsubscribeUpstreamDependency
)
)
_ <- hasDownstream.set(true)
} yield ()
)

Expand Down Expand Up @@ -293,7 +340,10 @@ private[stm] trait TxnRuntimeContext[F[_]] {
Right[Throwable, V](result.asInstanceOf[V])
)
case TxnResultRetry =>
ex.submitTxn(this)
Async[F].ifM(hasDownstream.get)(
ex.submitTxn(this),
ex.submitTxnForRetry(this)
)
case TxnResultLogDirty(idFootprintRefinement) =>
ex.submitTxnForImmediateRetry(
this.copy(idFootprint =
Expand Down Expand Up @@ -335,6 +385,7 @@ private[stm] trait TxnRuntimeContext[F[_]] {
}
completionSignal <- Deferred[F, Either[Throwable, V]]
dependencyTally <- Ref[F].of(0)
hasDownstream <- Ref[F].of(false)
executionStatus <- Ref[F].of(NotScheduled.asInstanceOf[ExecutionStatus])
id <- txnIdGen.getAndUpdate(_ + 1)
analysedTxn <-
Expand All @@ -347,6 +398,7 @@ private[stm] trait TxnRuntimeContext[F[_]] {
dependencyTally = dependencyTally,
unsubSpecs = MutableMap(),
executionStatus = executionStatus,
hasDownstream = hasDownstream,
scheduler = scheduler
)
)
Expand Down

0 comments on commit 911216f

Please sign in to comment.