abstractdog commented on code in PR #550:
URL: https://github.com/apache/tez/pull/550#discussion_r4665629676


##########
tez-runtime-library/src/test/java/org/apache/tez/runtime/library/common/writers/TestUnorderedPartitionedKVWriter.java:
##########
@@ -1118,6 +1122,59 @@ public void 
testSkippedPartitions_WithFinalMergeDisabled(boolean shouldCompress,
     baseTestWithFinalMergeDisabled(200, 10, Sets.newHashSet(2, 5), 
shouldCompress);
   }
 
+  /**
+   * With final merge off, a spill's callback adds the spill's event to the 
events of close() on the spill thread.
+   * Many small buffers keep many spills in flight, so several callbacks 
finish at once: no spill may be rejected,
+   * and close() must return one event per spill. The writers run 4 at a time 
to make that more frequent.
+   */
+  @Test
+  @Timeout(value = 120, unit = TimeUnit.SECONDS)
+  public void testConcurrentSpillsWithFinalMergeDisabled() throws Exception {
+    setupInit(false, ReportPartitionStats.DISABLED);
+    ExecutorService writers = Executors.newFixedThreadPool(4);
+    try {
+      List<Future<?>> results = new ArrayList<>();
+      for (int writer = 0; writer < 40; writer++) {
+        int id = writer;
+        results.add(writers.submit(() -> {
+          writeWithConcurrentSpills(id);
+          return null;
+        }));
+      }
+      for (Future<?> result : results) {
+        result.get();
+      }
+    } finally {
+      writers.shutdownNow();
+    }
+  }
+
+  private void writeWithConcurrentSpills(int writer) throws IOException, 
InterruptedException {
+    String auxiliaryService = 
defaultConf.get(TezConfiguration.TEZ_AM_SHUFFLE_AUXILIARY_SERVICE_ID,
+        TezConfiguration.TEZ_AM_SHUFFLE_AUXILIARY_SERVICE_ID_DEFAULT);

Review Comment:
   this logic is repeated many times in this class, and always falls back to 
default, can you please refactor to just simply use 
`TezConfiguration.TEZ_AM_SHUFFLE_AUXILIARY_SERVICE_ID_DEFAULT`?



##########
tez-runtime-library/src/test/java/org/apache/tez/runtime/library/common/writers/TestUnorderedPartitionedKVWriter.java:
##########
@@ -1118,6 +1122,59 @@ public void 
testSkippedPartitions_WithFinalMergeDisabled(boolean shouldCompress,
     baseTestWithFinalMergeDisabled(200, 10, Sets.newHashSet(2, 5), 
shouldCompress);
   }
 
+  /**
+   * With final merge off, a spill's callback adds the spill's event to the 
events of close() on the spill thread.
+   * Many small buffers keep many spills in flight, so several callbacks 
finish at once: no spill may be rejected,
+   * and close() must return one event per spill. The writers run 4 at a time 
to make that more frequent.
+   */
+  @Test
+  @Timeout(value = 120, unit = TimeUnit.SECONDS)
+  public void testConcurrentSpillsWithFinalMergeDisabled() throws Exception {
+    setupInit(false, ReportPartitionStats.DISABLED);
+    ExecutorService writers = Executors.newFixedThreadPool(4);
+    try {
+      List<Future<?>> results = new ArrayList<>();
+      for (int writer = 0; writer < 40; writer++) {
+        int id = writer;
+        results.add(writers.submit(() -> {
+          writeWithConcurrentSpills(id);
+          return null;
+        }));
+      }
+      for (Future<?> result : results) {
+        result.get();
+      }
+    } finally {
+      writers.shutdownNow();
+    }

Review Comment:
   maybe simplify:
   ```
    List<Callable<Void>> writers = IntStream.range(0, 40)
           .<Callable<Void>>mapToObj(id -> () -> {
             writeWithConcurrentSpills(id);
             return null;
           })
           .toList();
       try (ExecutorService pool = Executors.newFixedThreadPool(4)) {
         for (Future<Void> result : pool.invokeAll(writers)) {
           result.get();
         }
       }
   ```



##########
tez-runtime-library/src/main/java/org/apache/tez/runtime/library/common/writers/UnorderedPartitionedKVWriter.java:
##########
@@ -272,16 +271,19 @@ public UnorderedPartitionedKVWriter(OutputContext 
outputContext, Configuration c
 
     int maxThreads = Math.max(2, numBuffers/2);
     //TODO: Make use of TezSharedExecutor later
-    ExecutorService executor = new ThreadPoolExecutor(1, maxThreads,
+    // availableSlots bounds the spills in flight; the queue only absorbs the 
moment between a spill callback
+    // releasing its slot and its thread returning to the pool, which a 
SynchronousQueue rejected.

Review Comment:
   `which a SynchronousQueue rejected` mentions the old behavior, try to 
justify current state in code comments if possible



##########
tez-runtime-library/src/test/java/org/apache/tez/runtime/library/common/writers/TestUnorderedPartitionedKVWriter.java:
##########
@@ -1118,6 +1122,59 @@ public void 
testSkippedPartitions_WithFinalMergeDisabled(boolean shouldCompress,
     baseTestWithFinalMergeDisabled(200, 10, Sets.newHashSet(2, 5), 
shouldCompress);
   }
 
+  /**
+   * With final merge off, a spill's callback adds the spill's event to the 
events of close() on the spill thread.
+   * Many small buffers keep many spills in flight, so several callbacks 
finish at once: no spill may be rejected,
+   * and close() must return one event per spill. The writers run 4 at a time 
to make that more frequent.
+   */
+  @Test
+  @Timeout(value = 120, unit = TimeUnit.SECONDS)
+  public void testConcurrentSpillsWithFinalMergeDisabled() throws Exception {

Review Comment:
   the unit test has timed out for me locally without the patch? I guess that's 
not the expected behavior given there are assertions
   
   ```
   [ERROR] 
org.apache.tez.runtime.library.common.writers.TestUnorderedPartitionedKVWriter.testConcurrentSpillsWithFinalMergeDisabled
 -- Time elapsed: 120.6 s <<< ERROR!
   java.util.concurrent.TimeoutException: 
testConcurrentSpillsWithFinalMergeDisabled() timed out after 120 seconds
        at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
        at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
        Suppressed: java.lang.InterruptedException
                at 
java.base/java.util.concurrent.FutureTask.awaitDone(FutureTask.java:471)
                at 
java.base/java.util.concurrent.FutureTask.get(FutureTask.java:190)
                at 
org.apache.tez.runtime.library.common.writers.TestUnorderedPartitionedKVWriter.testConcurrentSpillsWithFinalMergeDisabled(TestUnorderedPartitionedKVWriter.java:1145)
   ```
   



##########
tez-runtime-library/src/main/java/org/apache/tez/runtime/library/common/writers/UnorderedPartitionedKVWriter.java:
##########
@@ -272,16 +271,19 @@ public UnorderedPartitionedKVWriter(OutputContext 
outputContext, Configuration c
 
     int maxThreads = Math.max(2, numBuffers/2);
     //TODO: Make use of TezSharedExecutor later
-    ExecutorService executor = new ThreadPoolExecutor(1, maxThreads,
+    // availableSlots bounds the spills in flight; the queue only absorbs the 
moment between a spill callback
+    // releasing its slot and its thread returning to the pool, which a 
SynchronousQueue rejected.
+    ThreadPoolExecutor executor = new ThreadPoolExecutor(maxThreads, 
maxThreads,
         60L, TimeUnit.SECONDS,
-        new SynchronousQueue<Runnable>(),
+        new LinkedBlockingQueue<Runnable>(),
         new ThreadFactoryBuilder()
             .setDaemon(true)
             .setNameFormat(
                 "UnorderedOutSpiller {" + TezUtilsInternal.cleanVertexName(
                     outputContext.getDestinationVertexName()) + "} #%d")
             .build()
     );
+    executor.allowCoreThreadTimeOut(true);
     // to restrict submission of more tasks than threads (e.g numBuffers > 
numThreads)
     // This is maxThreads - 1, to avoid race between callback thread releasing 
semaphore and the
     // thread calling tryAcquire.

Review Comment:
   The `- 1` and this comment are leftovers from the `SynchronousQueue`: with 
the queue, a callback that has released its slot while its thread is still busy 
no longer causes a rejection. The semaphore itself is still needed, though: 
when no slot is free, `scheduleSpill(false)` leaves the filled buffers in 
`filledBuffers` and they are spilled together later, instead of each queueing 
as its own small spill (one more spill file, and one more event with final 
merge off or pipelined shuffle). Maybe update the comment so nobody removes it:
   
   ```suggestion
       // Bounds the spills in flight. When no slot is free, filled buffers 
wait in filledBuffers and are spilled
       // together later instead of queueing one small spill each. The pool's 
queue absorbs a callback that has
       // released its slot while its thread is still busy, so the slots no 
longer need to stay below maxThreads.
   ```
   



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