-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
42f07a7
commit 0b590d1
Showing
6 changed files
with
71 additions
and
56 deletions.
There are no files selected for viewing
This file contains 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 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 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
41 changes: 19 additions & 22 deletions
41
...-core/src/main/kotlin/com/himadieiev/redpulsar/core/locks/excecutors/ExecutorAdditions.kt
This file contains 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 |
---|---|---|
@@ -1,29 +1,26 @@ | ||
package com.himadieiev.redpulsar.core.locks.excecutors | ||
|
||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.joinAll | ||
import kotlinx.coroutines.yield | ||
import java.util.concurrent.atomic.AtomicInteger | ||
|
||
suspend inline fun waitAllJobs(jobs: List<Job>) { | ||
jobs.joinAll() | ||
enum class WaitStrategy { | ||
ALL, | ||
MAJORITY, | ||
} | ||
|
||
suspend inline fun waitMajorityJobs(jobs: List<Job>) { | ||
val quorum: Int = jobs.size / 2 + 1 | ||
val succeed = AtomicInteger(0) | ||
val failed = AtomicInteger(0) | ||
jobs.forEach { job -> | ||
job.invokeOnCompletion { cause -> | ||
if (cause == null) { | ||
succeed.incrementAndGet() | ||
} else { | ||
failed.incrementAndGet() | ||
} | ||
} | ||
fun requiredToSuccessCount( | ||
waitStrategy: WaitStrategy, | ||
backendsSize: Int, | ||
): Int { | ||
return when (waitStrategy) { | ||
WaitStrategy.ALL -> backendsSize | ||
WaitStrategy.MAJORITY -> backendsSize / 2 + 1 | ||
} | ||
while (succeed.get() < quorum && failed.get() < quorum) { | ||
yield() | ||
} | ||
|
||
fun enoughToFailCount( | ||
waitStrategy: WaitStrategy, | ||
backendsSize: Int, | ||
): Int { | ||
return when (waitStrategy) { | ||
WaitStrategy.ALL -> 1 | ||
WaitStrategy.MAJORITY -> backendsSize - requiredToSuccessCount(waitStrategy, backendsSize) + 1 | ||
} | ||
return | ||
} |
This file contains 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 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