Abacn commented on code in PR #41156:
URL: https://github.com/apache/beam/pull/41156#discussion_r4776022873


##########
runners/flink/src/main/java/org/apache/beam/runners/flink/translation/functions/DeterministicRebalance.java:
##########
@@ -105,9 +115,51 @@ public Tuple2<Long, T> map(T value) {
     }
   }
 
+  /** {@code (sqrt(5) - 1) / 2}. Multiples of it are spread almost evenly 
modulo one. */
+  private static final double GOLDEN_RATIO_CONJUGATE = 0.6180339887498949;
+
+  /**
+   * Returns the multiplier {@code g} of the permutation {@code i -> i * g mod 
numSenders} that
+   * assigns start slots to senders: the integer closest to {@code numSenders} 
times the golden
+   * ratio conjugate that is coprime to {@code numSenders}, so that the map is 
a permutation.
+   */
+  static int scatterMultiplier(int numSenders) {
+    if (numSenders <= 2) {
+      return 1;
+    }
+    long target = Math.round(numSenders * GOLDEN_RATIO_CONJUGATE);
+    for (long distance = 0; distance < numSenders; distance++) {

Review Comment:
   One can simplify this two-sided for loop to a one-side do loop:
   
   Starting from int `step = (int) Math.round(numSenders * 
GOLDEN_RATIO_CONJUGATE)` and decrementing while `(step > 1 && IntMath.gcd(step, 
numSenders) != 1) { step--; }`
   - this works uniformly for numSenders = `1, 2, ..., Integer.MAX_VALUE` (no 
if (`numSenders <= 2`) special case, no long overflow/bounds checks since step 
only decreases toward 1).
   - Gives identical or tighter spread on the PR's own benchmarks (1760/48: 
step=1087, min=2, max=4 — identical; 2000/200: step=1233, min=9, max=11 vs PR's 
min=8, max=11).



##########
runners/flink/src/main/java/org/apache/beam/runners/flink/translation/functions/DeterministicRebalance.java:
##########
@@ -121,10 +173,8 @@ static class DeterministicRoundRobinPartitioner implements 
Partitioner<Long> {
     @Override
     public int partition(Long key, int numPartitions) {
       if (!started) {

Review Comment:
   After simplification one can inline the logic altogether
   
   ```
   if (!started) {
           int senderIndex = SenderKey.senderIndex(key);
           int numSenders = Math.max(1, SenderKey.numSenders(key));
           int step = (int) Math.round(numSenders * GOLDEN_RATIO_CONJUGATE);
           while (step > 1 && IntMath.gcd(step, numSenders) != 1) {
             step--;
           }
           long slot = (long) senderIndex * step % numSenders;
           nextChannel = (int) (slot * numPartitions / numSenders);
           started = true;
         }
   ```
   
   (Note: % numPartitions is also redundant because 0 <= slot < numSenders 
guarantees slot * numPartitions / numSenders < numPartitions.)



##########
runners/flink/src/test/java/org/apache/beam/runners/flink/translation/functions/DeterministicRebalanceTest.java:
##########
@@ -53,6 +53,35 @@ private static int[] receiverCounts(int numElements, int 
numSenders, int numRece
     return counts;
   }
 
+  /**

Review Comment:
   consider extend existing receiverCounts instead of adding two new helpers



##########
runners/flink/src/main/java/org/apache/beam/runners/flink/translation/functions/DeterministicRebalance.java:
##########
@@ -105,9 +115,51 @@ public Tuple2<Long, T> map(T value) {
     }
   }
 
+  /** {@code (sqrt(5) - 1) / 2}. Multiples of it are spread almost evenly 
modulo one. */
+  private static final double GOLDEN_RATIO_CONJUGATE = 0.6180339887498949;
+
+  /**
+   * Returns the multiplier {@code g} of the permutation {@code i -> i * g mod 
numSenders} that
+   * assigns start slots to senders: the integer closest to {@code numSenders} 
times the golden
+   * ratio conjugate that is coprime to {@code numSenders}, so that the map is 
a permutation.
+   */
+  static int scatterMultiplier(int numSenders) {
+    if (numSenders <= 2) {
+      return 1;
+    }
+    long target = Math.round(numSenders * GOLDEN_RATIO_CONJUGATE);
+    for (long distance = 0; distance < numSenders; distance++) {
+      long above = target + distance;
+      if (above < numSenders && gcd(above, numSenders) == 1) {
+        return (int) above;
+      }
+      long below = target - distance;
+      if (below > 0 && gcd(below, numSenders) == 1) {
+        return (int) below;
+      }
+    }
+    return 1;
+  }
+
+  /** Returns the channel at which sender {@code senderIndex} of {@code 
numSenders} starts. */
+  static int startChannel(int senderIndex, int numSenders, int numPartitions) {
+    int senders = Math.max(1, numSenders);
+    long slot = (long) senderIndex * scatterMultiplier(senders) % senders;
+    return (int) (slot * numPartitions / senders % numPartitions);
+  }
+
+  private static long gcd(long a, long b) {

Review Comment:
   This is standard math, consider use a library (Guava IntMath.gcd)



##########
runners/flink/src/main/java/org/apache/beam/runners/flink/translation/functions/DeterministicRebalance.java:
##########
@@ -37,10 +37,20 @@
  * elements per receiver follows a Poisson distribution of mean {@code k}, so 
some receivers get
  * none and others several times the mean.
  *
- * <p>Here sender {@code i} of {@code M} sends its {@code j}-th element to 
channel {@code (floor(i *
- * N / M) + j) mod N} for {@code N} receivers. The senders' start channels are 
spread evenly, so an
- * input that is already balanced stays balanced, and a single sender's 
elements still go
- * round-robin over all receivers.
+ * <p>Here sender {@code i} of {@code M} sends its {@code j}-th element to 
channel {@code

Review Comment:
   Please consider consolidate these explanations



##########
runners/flink/src/test/java/org/apache/beam/runners/flink/translation/functions/DeterministicRebalanceTest.java:
##########
@@ -98,6 +127,66 @@ public void 
testMoreSendersThanReceiversStayCloseToBalanced() {
     assertTrue("got min " + min + " and max " + max, min > 0 && max - min <= 
bound);
   }
 
+  @Test
+  public void testConsecutiveBusySendersSpreadOverAllReceivers() {

Review Comment:
   There are many tests and helpers added. Since this is a single logic change, 
it appears to be only 
   `testConsecutiveBusySendersSpreadOverAllReceivers` is essential



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to