Copilot commented on code in PR #3387:
URL: https://github.com/apache/celeborn/pull/3387#discussion_r2230491592


##########
worker/src/main/java/org/apache/celeborn/service/deploy/worker/WriterHelper.java:
##########
@@ -0,0 +1,119 @@
+/*
+ * 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.celeborn.service.deploy.worker;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.ConcurrentLinkedQueue;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.celeborn.common.identity.UserIdentifier;
+import org.apache.celeborn.common.meta.WorkerPartitionLocationInfo;
+import org.apache.celeborn.common.protocol.PartitionLocation;
+import org.apache.celeborn.common.protocol.PartitionSplitMode;
+import org.apache.celeborn.common.protocol.PartitionType;
+import org.apache.celeborn.service.deploy.worker.storage.PartitionDataWriter;
+import org.apache.celeborn.service.deploy.worker.storage.StorageManager;
+
+public class WriterHelper {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(WriterHelper.class);
+
+  private final StorageManager storageManager;
+  private final WorkerPartitionLocationInfo partitionLocationInfo;
+
+  public WriterHelper(
+      StorageManager storageManager, WorkerPartitionLocationInfo 
partitionLocationInfo) {
+    this.storageManager = storageManager;
+    this.partitionLocationInfo = partitionLocationInfo;
+  }
+
+  public List<PartitionLocation> createWriters(
+      String shuffleKey,
+      String applicationId,
+      int shuffleId,
+      List<PartitionLocation> requestLocs,
+      long splitThreshold,
+      PartitionSplitMode splitMode,
+      PartitionType partitionType,
+      boolean rangeReadFilter,
+      UserIdentifier userIdentifier,
+      boolean partitionSplitEnabled,
+      boolean isSegmentGranularityVisible,
+      boolean isPrimary) {
+    List<PartitionLocation> locs = new ArrayList<>();
+    try {
+      ConcurrentLinkedQueue<PartitionLocation> locQueue = new 
ConcurrentLinkedQueue<>();
+      requestLocs
+          .parallelStream()
+          .forEach(
+              requestLoc -> {
+                PartitionLocation location =
+                    isPrimary
+                        ? partitionLocationInfo.getPrimaryLocation(
+                            shuffleKey, requestLoc.getUniqueId())
+                        : partitionLocationInfo.getReplicaLocation(
+                            shuffleKey, requestLoc.getUniqueId());
+                if (location == null) {
+                  location = requestLoc;
+                  PartitionDataWriter writer;
+                  try {
+                    writer =
+                        storageManager.createPartitionDataWriter(
+                            applicationId,
+                            shuffleId,
+                            location,
+                            splitThreshold,
+                            splitMode,
+                            partitionType,
+                            rangeReadFilter,
+                            userIdentifier,
+                            partitionSplitEnabled,
+                            isSegmentGranularityVisible);
+                  } catch (IOException e) {
+                    throw new RuntimeException(e);

Review Comment:
   Wrapping IOException in RuntimeException loses the specific exception type 
and may make error handling less precise. Consider creating a more specific 
exception or allowing the IOException to propagate appropriately.
   ```suggestion
                       throw new PartitionDataWriterException("Failed to create 
PartitionDataWriter", e);
   ```



##########
worker/src/main/java/org/apache/celeborn/service/deploy/worker/WriterHelper.java:
##########
@@ -0,0 +1,119 @@
+/*
+ * 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.celeborn.service.deploy.worker;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.ConcurrentLinkedQueue;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.celeborn.common.identity.UserIdentifier;
+import org.apache.celeborn.common.meta.WorkerPartitionLocationInfo;
+import org.apache.celeborn.common.protocol.PartitionLocation;
+import org.apache.celeborn.common.protocol.PartitionSplitMode;
+import org.apache.celeborn.common.protocol.PartitionType;
+import org.apache.celeborn.service.deploy.worker.storage.PartitionDataWriter;
+import org.apache.celeborn.service.deploy.worker.storage.StorageManager;
+
+public class WriterHelper {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(WriterHelper.class);
+
+  private final StorageManager storageManager;
+  private final WorkerPartitionLocationInfo partitionLocationInfo;
+
+  public WriterHelper(
+      StorageManager storageManager, WorkerPartitionLocationInfo 
partitionLocationInfo) {
+    this.storageManager = storageManager;
+    this.partitionLocationInfo = partitionLocationInfo;
+  }
+
+  public List<PartitionLocation> createWriters(
+      String shuffleKey,
+      String applicationId,
+      int shuffleId,
+      List<PartitionLocation> requestLocs,
+      long splitThreshold,
+      PartitionSplitMode splitMode,
+      PartitionType partitionType,
+      boolean rangeReadFilter,
+      UserIdentifier userIdentifier,
+      boolean partitionSplitEnabled,
+      boolean isSegmentGranularityVisible,
+      boolean isPrimary) {
+    List<PartitionLocation> locs = new ArrayList<>();
+    try {
+      ConcurrentLinkedQueue<PartitionLocation> locQueue = new 
ConcurrentLinkedQueue<>();
+      requestLocs
+          .parallelStream()
+          .forEach(
+              requestLoc -> {
+                PartitionLocation location =
+                    isPrimary
+                        ? partitionLocationInfo.getPrimaryLocation(
+                            shuffleKey, requestLoc.getUniqueId())
+                        : partitionLocationInfo.getReplicaLocation(
+                            shuffleKey, requestLoc.getUniqueId());
+                if (location == null) {
+                  location = requestLoc;
+                  PartitionDataWriter writer;
+                  try {
+                    writer =
+                        storageManager.createPartitionDataWriter(
+                            applicationId,
+                            shuffleId,
+                            location,
+                            splitThreshold,
+                            splitMode,
+                            partitionType,
+                            rangeReadFilter,
+                            userIdentifier,
+                            partitionSplitEnabled,
+                            isSegmentGranularityVisible);
+                  } catch (IOException e) {
+                    throw new RuntimeException(e);
+                  }
+                  locQueue.add(new WorkingPartition(location, writer));
+                } else {
+                  locQueue.add(location);
+                }
+              });
+      locs.addAll(locQueue);
+    } catch (Exception e) {
+      LOG.error("CreateWriter for {} failed.", shuffleKey, e);
+    }
+    return locs;
+  }
+
+  public void destroyWriters(List<PartitionLocation> locs, String shuffleKey) {
+    locs.parallelStream()
+        .forEach(
+            partitionLocation -> {
+              WorkingPartition workingPartition = (WorkingPartition) 
partitionLocation;
+              PartitionDataWriter fileWriter = 
workingPartition.getFileWriter();
+              fileWriter.destroy(
+                  new IOException(
+                      String.format(
+                          "Destroy FileWriter %s caused by " + "reserving 
slots failed for %s.",
+                          fileWriter, shuffleKey)));

Review Comment:
   This cast is unsafe and will throw a ClassCastException if the 
PartitionLocation is not a WorkingPartition. Add type checking before casting 
or filter to only process WorkingPartition instances.
   ```suggestion
                 if (partitionLocation instanceof WorkingPartition) {
                   WorkingPartition workingPartition = (WorkingPartition) 
partitionLocation;
                   PartitionDataWriter fileWriter = 
workingPartition.getFileWriter();
                   fileWriter.destroy(
                       new IOException(
                           String.format(
                               "Destroy FileWriter %s caused by " + "reserving 
slots failed for %s.",
                               fileWriter, shuffleKey)));
                 } else {
                   LOG.warn("Skipping destruction of non-WorkingPartition 
instance: {}", partitionLocation);
                 }
   ```



##########
worker/src/main/java/org/apache/celeborn/service/deploy/worker/WriterHelper.java:
##########
@@ -0,0 +1,119 @@
+/*
+ * 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.celeborn.service.deploy.worker;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.ConcurrentLinkedQueue;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.celeborn.common.identity.UserIdentifier;
+import org.apache.celeborn.common.meta.WorkerPartitionLocationInfo;
+import org.apache.celeborn.common.protocol.PartitionLocation;
+import org.apache.celeborn.common.protocol.PartitionSplitMode;
+import org.apache.celeborn.common.protocol.PartitionType;
+import org.apache.celeborn.service.deploy.worker.storage.PartitionDataWriter;
+import org.apache.celeborn.service.deploy.worker.storage.StorageManager;
+
+public class WriterHelper {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(WriterHelper.class);
+
+  private final StorageManager storageManager;
+  private final WorkerPartitionLocationInfo partitionLocationInfo;
+
+  public WriterHelper(
+      StorageManager storageManager, WorkerPartitionLocationInfo 
partitionLocationInfo) {
+    this.storageManager = storageManager;
+    this.partitionLocationInfo = partitionLocationInfo;
+  }
+
+  public List<PartitionLocation> createWriters(
+      String shuffleKey,
+      String applicationId,
+      int shuffleId,
+      List<PartitionLocation> requestLocs,
+      long splitThreshold,
+      PartitionSplitMode splitMode,
+      PartitionType partitionType,
+      boolean rangeReadFilter,
+      UserIdentifier userIdentifier,
+      boolean partitionSplitEnabled,
+      boolean isSegmentGranularityVisible,
+      boolean isPrimary) {
+    List<PartitionLocation> locs = new ArrayList<>();
+    try {
+      ConcurrentLinkedQueue<PartitionLocation> locQueue = new 
ConcurrentLinkedQueue<>();
+      requestLocs
+          .parallelStream()
+          .forEach(
+              requestLoc -> {
+                PartitionLocation location =
+                    isPrimary
+                        ? partitionLocationInfo.getPrimaryLocation(
+                            shuffleKey, requestLoc.getUniqueId())
+                        : partitionLocationInfo.getReplicaLocation(
+                            shuffleKey, requestLoc.getUniqueId());
+                if (location == null) {
+                  location = requestLoc;
+                  PartitionDataWriter writer;
+                  try {
+                    writer =
+                        storageManager.createPartitionDataWriter(
+                            applicationId,
+                            shuffleId,
+                            location,
+                            splitThreshold,
+                            splitMode,
+                            partitionType,
+                            rangeReadFilter,
+                            userIdentifier,
+                            partitionSplitEnabled,
+                            isSegmentGranularityVisible);
+                  } catch (IOException e) {
+                    throw new RuntimeException(e);
+                  }
+                  locQueue.add(new WorkingPartition(location, writer));
+                } else {
+                  locQueue.add(location);
+                }
+              });

Review Comment:
   [nitpick] Using parallelStream() with concurrent operations on a 
ConcurrentLinkedQueue may not provide expected performance benefits due to 
coordination overhead. Consider using parallel processing with a thread-safe 
collection strategy or evaluate if the performance gain justifies the 
complexity.
   ```suggestion
         ForkJoinPool customThreadPool = new ForkJoinPool();
         try {
           customThreadPool.submit(() ->
               requestLocs.parallelStream().forEach(
                   requestLoc -> {
                     PartitionLocation location =
                         isPrimary
                             ? partitionLocationInfo.getPrimaryLocation(
                                 shuffleKey, requestLoc.getUniqueId())
                             : partitionLocationInfo.getReplicaLocation(
                                 shuffleKey, requestLoc.getUniqueId());
                     if (location == null) {
                       location = requestLoc;
                       PartitionDataWriter writer;
                       try {
                         writer =
                             storageManager.createPartitionDataWriter(
                                 applicationId,
                                 shuffleId,
                                 location,
                                 splitThreshold,
                                 splitMode,
                                 partitionType,
                                 rangeReadFilter,
                                 userIdentifier,
                                 partitionSplitEnabled,
                                 isSegmentGranularityVisible);
                       } catch (IOException e) {
                         throw new RuntimeException(e);
                       }
                       locQueue.add(new WorkingPartition(location, writer));
                     } else {
                       locQueue.add(location);
                     }
                   })).get();
         } catch (Exception e) {
           throw new RuntimeException("Error during parallel processing", e);
         } finally {
           customThreadPool.shutdown();
         }
   ```



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