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



##########
File path: 
pinot-server/src/main/java/org/apache/pinot/server/starter/helix/HelixInstanceDataManager.java
##########
@@ -212,33 +218,45 @@ public void reloadSegment(String tableNameWithType, 
String segmentName, boolean
   }
 
   @Override
-  public void reloadAllSegments(String tableNameWithType, boolean 
forceDownload) {
+  public void reloadAllSegments(String tableNameWithType, boolean 
forceDownload,
+      SegmentRefreshSemaphore segmentRefreshSemaphore)
+      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(() -> {
+      String segmentName = segmentMetadata.getName();
       try {
-        reloadSegment(tableNameWithType, segmentMetadata, tableConfig, schema, 
forceDownload);
+        segmentRefreshSemaphore.acquireSema(segmentMetadata.getName(), LOGGER);
+        try {
+          reloadSegment(tableNameWithType, segmentMetadata, tableConfig, 
schema, forceDownload);
+        } catch (Exception e) {
+          LOGGER.error("Caught exception while reloading segment: {} in table: 
{}", segmentName, tableNameWithType, e);
+          failedSegments.add(segmentName);
+          sampleException.set(e);

Review comment:
       This catch is duplicated. We only need one `try/catch/finally` here

##########
File path: 
pinot-core/src/main/java/org/apache/pinot/core/util/SegmentRefreshSemaphore.java
##########
@@ -0,0 +1,60 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.pinot.core.util;
+
+import java.util.concurrent.Semaphore;
+import org.slf4j.Logger;
+
+
+/**
+ * Wrapper class for semaphore used to control concurrent segment 
reload/refresh
+ */
+public class SegmentRefreshSemaphore {
+
+  private final Semaphore _semaphore;
+
+  public SegmentRefreshSemaphore(int permits, boolean fair) {
+    if (permits > 0) {
+      _semaphore = new Semaphore(permits, fair);
+    } else {
+      _semaphore = null;
+    }
+  }
+
+  public void acquireSema(String context, Logger logger)

Review comment:
       (minor)
   ```suggestion
     public void acquireSema(String segmentName, Logger logger)
   ```




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