Jackie-Jiang commented on a change in pull request #7893:
URL: https://github.com/apache/pinot/pull/7893#discussion_r774225405



##########
File path: 
pinot-server/src/main/java/org/apache/pinot/server/starter/helix/HelixInstanceDataManager.java
##########
@@ -212,36 +218,56 @@ public void reloadSegment(String tableNameWithType, 
String segmentName, boolean
   }
 
   @Override
-  public void reloadAllSegments(String tableNameWithType, boolean 
forceDownload) {
+  public void reloadAllSegments(String tableNameWithType, boolean 
forceDownload,
+      @Nullable Semaphore refreshThreadSemaphore)
+      throws Exception {
     LOGGER.info("Reloading all segments in table: {}", tableNameWithType);
     TableConfig tableConfig = 
ZKMetadataProvider.getTableConfig(_propertyStore, tableNameWithType);
     Preconditions.checkNotNull(tableConfig);
-
     Schema schema = ZKMetadataProvider.getTableSchema(_propertyStore, 
tableNameWithType);
-
     List<String> failedSegments = new ArrayList<>();
-    Exception sampleException = null;
     List<SegmentMetadata> segmentsMetadata = 
getAllSegmentsMetadata(tableNameWithType);
-    for (SegmentMetadata segmentMetadata : segmentsMetadata) {
+    ExecutorService workers = Executors.newCachedThreadPool();
+    final AtomicReference<Exception> sampleException = new AtomicReference<>();
+    //calling thread hasn't acquired any permit so we don't reload any 
segments using it.
+    CompletableFuture.allOf(segmentsMetadata.stream().map(segmentMetadata -> 
CompletableFuture.runAsync(() -> {
       try {
+        acquireSema("ALL", refreshThreadSemaphore);

Review comment:
       Put the segment name
   ```suggestion
           acquireSema(segmentsMetadata.getName(), refreshThreadSemaphore);
   ```

##########
File path: 
pinot-server/src/main/java/org/apache/pinot/server/starter/helix/HelixInstanceDataManager.java
##########
@@ -212,36 +218,56 @@ public void reloadSegment(String tableNameWithType, 
String segmentName, boolean
   }
 
   @Override
-  public void reloadAllSegments(String tableNameWithType, boolean 
forceDownload) {
+  public void reloadAllSegments(String tableNameWithType, boolean 
forceDownload,
+      @Nullable Semaphore refreshThreadSemaphore)
+      throws Exception {
     LOGGER.info("Reloading all segments in table: {}", tableNameWithType);
     TableConfig tableConfig = 
ZKMetadataProvider.getTableConfig(_propertyStore, tableNameWithType);
     Preconditions.checkNotNull(tableConfig);
-
     Schema schema = ZKMetadataProvider.getTableSchema(_propertyStore, 
tableNameWithType);
-
     List<String> failedSegments = new ArrayList<>();
-    Exception sampleException = null;
     List<SegmentMetadata> segmentsMetadata = 
getAllSegmentsMetadata(tableNameWithType);
-    for (SegmentMetadata segmentMetadata : segmentsMetadata) {
+    ExecutorService workers = Executors.newCachedThreadPool();
+    final AtomicReference<Exception> sampleException = new AtomicReference<>();
+    //calling thread hasn't acquired any permit so we don't reload any 
segments using it.
+    CompletableFuture.allOf(segmentsMetadata.stream().map(segmentMetadata -> 
CompletableFuture.runAsync(() -> {
       try {
+        acquireSema("ALL", refreshThreadSemaphore);

Review comment:
       `acquireSema()` should be outside of the `try` block

##########
File path: 
pinot-server/src/main/java/org/apache/pinot/server/starter/helix/HelixInstanceDataManager.java
##########
@@ -212,36 +218,56 @@ public void reloadSegment(String tableNameWithType, 
String segmentName, boolean
   }
 
   @Override
-  public void reloadAllSegments(String tableNameWithType, boolean 
forceDownload) {
+  public void reloadAllSegments(String tableNameWithType, boolean 
forceDownload,
+      @Nullable Semaphore refreshThreadSemaphore)
+      throws Exception {
     LOGGER.info("Reloading all segments in table: {}", tableNameWithType);
     TableConfig tableConfig = 
ZKMetadataProvider.getTableConfig(_propertyStore, tableNameWithType);
     Preconditions.checkNotNull(tableConfig);
-
     Schema schema = ZKMetadataProvider.getTableSchema(_propertyStore, 
tableNameWithType);
-
     List<String> failedSegments = new ArrayList<>();
-    Exception sampleException = null;
     List<SegmentMetadata> segmentsMetadata = 
getAllSegmentsMetadata(tableNameWithType);
-    for (SegmentMetadata segmentMetadata : segmentsMetadata) {
+    ExecutorService workers = Executors.newCachedThreadPool();
+    final AtomicReference<Exception> sampleException = new AtomicReference<>();
+    //calling thread hasn't acquired any permit so we don't reload any 
segments using it.
+    CompletableFuture.allOf(segmentsMetadata.stream().map(segmentMetadata -> 
CompletableFuture.runAsync(() -> {
       try {
+        acquireSema("ALL", refreshThreadSemaphore);
         reloadSegment(tableNameWithType, segmentMetadata, tableConfig, schema, 
forceDownload);
       } catch (Exception e) {
         String segmentName = segmentMetadata.getName();
         LOGGER.error("Caught exception while reloading segment: {} in table: 
{}", segmentName, tableNameWithType, e);
         failedSegments.add(segmentName);
-        sampleException = e;
+        sampleException.set(e);
+      } finally {
+        if (refreshThreadSemaphore != null) {
+          refreshThreadSemaphore.release();
+        }
       }
-    }
+    }, workers)).toArray(CompletableFuture[]::new)).get();

Review comment:
       Do you need to shut down the executor service after it is done?

##########
File path: 
pinot-server/src/main/java/org/apache/pinot/server/starter/helix/HelixInstanceDataManager.java
##########
@@ -212,36 +218,56 @@ public void reloadSegment(String tableNameWithType, 
String segmentName, boolean
   }
 
   @Override
-  public void reloadAllSegments(String tableNameWithType, boolean 
forceDownload) {
+  public void reloadAllSegments(String tableNameWithType, boolean 
forceDownload,
+      @Nullable Semaphore refreshThreadSemaphore)
+      throws Exception {
     LOGGER.info("Reloading all segments in table: {}", tableNameWithType);
     TableConfig tableConfig = 
ZKMetadataProvider.getTableConfig(_propertyStore, tableNameWithType);
     Preconditions.checkNotNull(tableConfig);
-
     Schema schema = ZKMetadataProvider.getTableSchema(_propertyStore, 
tableNameWithType);
-
     List<String> failedSegments = new ArrayList<>();
-    Exception sampleException = null;
     List<SegmentMetadata> segmentsMetadata = 
getAllSegmentsMetadata(tableNameWithType);
-    for (SegmentMetadata segmentMetadata : segmentsMetadata) {
+    ExecutorService workers = Executors.newCachedThreadPool();
+    final AtomicReference<Exception> sampleException = new AtomicReference<>();
+    //calling thread hasn't acquired any permit so we don't reload any 
segments using it.
+    CompletableFuture.allOf(segmentsMetadata.stream().map(segmentMetadata -> 
CompletableFuture.runAsync(() -> {
       try {
+        acquireSema("ALL", refreshThreadSemaphore);
         reloadSegment(tableNameWithType, segmentMetadata, tableConfig, schema, 
forceDownload);
       } catch (Exception e) {
         String segmentName = segmentMetadata.getName();
         LOGGER.error("Caught exception while reloading segment: {} in table: 
{}", segmentName, tableNameWithType, e);
         failedSegments.add(segmentName);
-        sampleException = e;
+        sampleException.set(e);
+      } finally {
+        if (refreshThreadSemaphore != null) {
+          refreshThreadSemaphore.release();
+        }
       }
-    }
+    }, workers)).toArray(CompletableFuture[]::new)).get();
 
-    if (sampleException != null) {
+    if (sampleException.get() != null) {
       throw new RuntimeException(
           String.format("Failed to reload %d/%d segments: %s in table: %s", 
failedSegments.size(),
-              segmentsMetadata.size(), failedSegments, tableNameWithType), 
sampleException);
+              segmentsMetadata.size(), failedSegments, tableNameWithType), 
sampleException.get());
     }
-
     LOGGER.info("Reloaded all segments in table: {}", tableNameWithType);
   }
 
+  private void acquireSema(String context, Semaphore refreshThreadSemaphore)

Review comment:
       The first argument is `segmentName`
   
   (optional) This part is duplicated. One way to avoid duplicate code is to 
add a `SegmentRefreshSemaphore` class to wrap this logic.




-- 
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: commits-unsubscr...@pinot.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org
For additional commands, e-mail: commits-h...@pinot.apache.org

Reply via email to