question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rebalance doesn't work reliably

See original GitHub issue

I 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:closed
  • Created 3 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
LMnetcommented, Feb 16, 2021

@bplommer I think I found a solution. I will prepare it and update the pull request. Don’t spend your time on the investigation.

0reactions
bplommercommented, Feb 19, 2021

Fixed by #533

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found