Weird fairness/scheduling issue
See original GitHub issueStill not sure what’s going on here. I’ve been poking at this for a little while:
object InefficientProducerConsumer extends IOApp {
def producer[F[_]: Spawn: Console](queueR: Ref[F, Queue[Int]], counter: Int): F[Unit] =
for {
_ <- if (counter % 10000 == 0)
Console[F].println(s"Produced $counter items")
else
Applicative[F].unit
_ <- queueR.getAndUpdate(_.enqueue(counter + 1))
_ <- producer(queueR, counter + 1)
} yield ()
def consumer[F[_]: Monad: Console](queueR: Ref[F, Queue[Int]]): F[Unit] =
for {
_ <- Console[F].println("attempting to consume")
iO <- queueR modify { queue =>
queue.dequeueOption.fold((queue, Option.empty[Int])) {
case (i, queue) => (queue, Option(i))
}
}
_ <- if (iO.exists(_ % 10000 == 0))
Console[F].println(s"Consumed ${iO.get} items")
else
Applicative[F].unit
_ <- consumer(queueR)
} yield ()
override def run(args: List[String]): IO[ExitCode] =
for {
queueR <- Ref.of[IO, Queue[Int]](Queue.empty[Int])
cf <- consumer(queueR).start
pr <- producer(queueR, 0).start
cfR <- cf.join
_ <- IO.println(s"!!! found cfR = $cfR")
prR <- pr.join
_ <- IO.println(s"!!! found prR = $prR")
} yield ExitCode.Success
}
If you run this, you’ll see a ton of attempting to consume
, followed by infinite Produced
printlns, which honestly doesn’t make any sense. It’s like the first fiber hogs the thread for a while, then the second fiber takes over and never lets it go.
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
Fairness in Scheduling - Computer Science
In this paper we explore the issue in settings where there are long living processes which should be repeatedly scheduled for various tasks...
Read more >Fairness in employee scheduling - Christine Chung
The scheduling problem most similar to our our own is probably the nurse rostering problem, as it is concerned with fairness to employees...
Read more >Scheduling: Proportional Share - cs.wisc.edu
In this chapter, we'll examine a different type of scheduler known as a proportional-share scheduler, also sometimes referred to as a fair-share scheduler....
Read more >example for weakly fair v.s. strongly fair scheduling in ...
Weakly fair scheduling means that only one process would run because the condition gets changed before the second process is able to run....
Read more >Irregular Work Scheduling and Its Consequences
This report will inform recently proposed reforms of the Fair ... the incidence of irregular, unusual work schedules (these results are not ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
lol Our runtime is legitimately so fast that it breaks examples like this. So for anyone following along at home, this wasn’t a problem before because the runtime used to be slow enough that the overhead dominated the race on both sides and things were somewhat fair. Now the
Queue
operations dominate by a significant margin, meaning that it’s just a pure race between two differentQueue
operations thrown on a naive scheduler that doesn’t support fairness (AtomicReference
).Or, if you want a tldr: our runtime is too fast for this
Come on Daniel, say what you really mean, no reason to hide it! 😄😆🥳