From ad3c39cc0aa24008fb3db45f2e5d22b5c36b28c5 Mon Sep 17 00:00:00 2001 From: Matthew Ball Date: Fri, 20 Nov 2026 13:52:54 -0800 Subject: [PATCH] perf(amber): round-robin partitioner even distribution RoundRobinPartitioner.getBucketIndex incremented roundRobinIndex before emitting it, so starting from 0 the first tuple went to receiver 1 and receiver 0 was skipped until the second cycle; it also threw ArithmeticException on an empty receiver set. getBucketIndex now emits the current index and then advances, cycling from receiver 0, and returns an empty iterator when there are no receivers. PartitionersSpec flips the cycle expectation to start at 0 and adds a case asserting an empty receiver set yields no bucket index. --- .../partitioners/RoundRobinPartitioner.scala | 8 ++++++-- .../partitioners/PartitionersSpec.scala | 12 +++++++++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/amber/src/main/scala/org/apache/texera/amber/engine/architecture/sendsemantics/partitioners/RoundRobinPartitioner.scala b/amber/src/main/scala/org/apache/texera/amber/engine/architecture/sendsemantics/partitioners/RoundRobinPartitioner.scala index 185f2e5e3ac..63f247c4386 100644 --- a/amber/src/main/scala/org/apache/texera/amber/engine/architecture/sendsemantics/partitioners/RoundRobinPartitioner.scala +++ b/amber/src/main/scala/org/apache/texera/amber/engine/architecture/sendsemantics/partitioners/RoundRobinPartitioner.scala @@ -28,8 +28,12 @@ case class RoundRobinPartitioner(partitioning: RoundRobinPartitioning) extends P private val receivers = partitioning.channels.map(_.toWorkerId).distinct override def getBucketIndex(tuple: Tuple): Iterator[Int] = { - roundRobinIndex = (roundRobinIndex + 1) % receivers.length - Iterator(roundRobinIndex) + if (receivers.isEmpty) Iterator.empty + else { + val index = roundRobinIndex + roundRobinIndex = (roundRobinIndex + 1) % receivers.length + Iterator(index) + } } override def allReceivers: Seq[ActorVirtualIdentity] = receivers diff --git a/amber/src/test/scala/org/apache/texera/amber/engine/architecture/sendsemantics/partitioners/PartitionersSpec.scala b/amber/src/test/scala/org/apache/texera/amber/engine/architecture/sendsemantics/partitioners/PartitionersSpec.scala index 10d244b7441..b343dbd81f2 100644 --- a/amber/src/test/scala/org/apache/texera/amber/engine/architecture/sendsemantics/partitioners/PartitionersSpec.scala +++ b/amber/src/test/scala/org/apache/texera/amber/engine/architecture/sendsemantics/partitioners/PartitionersSpec.scala @@ -111,9 +111,15 @@ class PartitionersSpec extends AnyFlatSpec { val partitioner = RoundRobinPartitioner(partitioning) val indices = (1 to 7).map(_ => partitioner.getBucketIndex(intTuple(0)).next()).toList - // Implementation increments first, then emits. Starting from 0, the first - // emitted index is therefore 1, then 2, then 0, repeating. - assert(indices == List(1, 2, 0, 1, 2, 0, 1)) + // Emits the current index then advances, so the cycle starts at receiver 0. + assert(indices == List(0, 1, 2, 0, 1, 2, 0)) + } + + it should "emit nothing when there are no receivers" in { + val partitioner = + RoundRobinPartitioner(RoundRobinPartitioning(batchSize = 100, channels = Seq())) + assert(partitioner.getBucketIndex(intTuple(0)).toList == List()) + assert(partitioner.allReceivers == Seq()) } "RoundRobinPartitioner.allReceivers" should "preserve channel order while deduplicating" in {