Rebalance doesn't work reliably
See original GitHub issueI faced an issue with a rebalance on production: sometimes it just doesn’t happen. Looks like old partition streams are not always closing after rebalance.
I managed to minimize this issue into the test case:
withKafka { (config, topic) =>
val numPartitions = 3
createCustomTopic(topic, partitions = numPartitions)
val produced = (0 until 10000).map(n => s"key-$n" -> s"value->$n")
publishToKafka(topic, produced)
def run(instance: Int, allAssignments: SignallingRef[IO, Map[Int, Set[Int]]]): IO[Unit] = {
consumerStream[IO]
.using(consumerSettings[IO](config).withGroupId("test"))
.evalTap(_.subscribeTo(topic))
.flatMap(_.partitionsMapStream)
.flatMap { assignment =>
Stream.eval(allAssignments.update { current =>
current.updated(instance, assignment.keySet.map(_.partition()))
}) >> Stream.emits(assignment.map { case (_, partitionStream) =>
partitionStream.evalMap(_ => IO.sleep(10.millis))
}.toList).parJoinUnbounded
}.compile.drain
}
def checkAssignments(allAssignments: SignallingRef[IO, Map[Int, Set[Int]]])(instances: Set[Int]) = {
allAssignments.discrete.filter { state =>
instances.forall { instance =>
state.get(instance).exists(_.nonEmpty)
} && state.values.toList.flatMap(_.toList).sorted == List(0, 1, 2)
}.take(1).compile.drain
}
(for {
allAssignments <- SignallingRef[IO, Map[Int, Set[Int]]](Map.empty)
fiber0 <- run(0, allAssignments).start
_ <- checkAssignments(allAssignments)(Set(0))
fiber1 <- run(1, allAssignments).start
_ <- checkAssignments(allAssignments)(Set(0, 1))
fiber2 <- run(2, allAssignments).start
_ <- checkAssignments(allAssignments)(Set(0, 1, 2))
_ <- fiber2.cancel
_ <- checkAssignments(allAssignments)(Set(0, 1))
_ <- fiber1.cancel
_ <- checkAssignments(allAssignments)(Set(0))
_ <- fiber0.cancel
} yield succeed).unsafeRunSync()
}
I started 3 instances one by one and after that shut down them one by one.
From logs, I’m 100% sure, that on the java side all works fine because the java kafka consumer receives correct rebalance events.
I will try to fix this issue because it’s a blocker for me now. Also, It could be a blocker for any users, who try to rely on a rebalance in fs2-kafka.
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (6 by maintainers)
Top Results From Across the Web
To Rebalance or Not to Rebalance - CFA Institute Blogs
Rebalancing is a topic that most professional money managers are familiar with and yet it is hardly clear to many whether this is...
Read more >Rebalancing: A simple, if unappreciated, way to enhance ...
Rebalancing doesn't work as well with individual stocks or bonds, which conceivably could lose all their value. You want to use broadly ...
Read more >Is it really necessary to rebalance your investment portfolio?
Just to be clear: rebalancing doesn't boost your long-term returns. If anything, to the extent rebalancing forces you to cut back on your...
Read more >The Case Against Rebalancing Your Portfolio - The Balance
Common wisdom tells us that we should periodically rebalance our investment portfolio. But here's a reason why this might not always work.
Read more >Portfolio rebalancing in a down market
“You should rebalance your portfolio anytime it has deviated or 'drifted' enough from your original allocations,” says Claire Mork, director ...
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 FreeTop 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
Top GitHub Comments
@bplommer I think I found a solution. I will prepare it and update the pull request. Don’t spend your time on the investigation.
Fixed by #533