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 {