You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@celeborn.apache.org by "waitinfuture (via GitHub)" <gi...@apache.org> on 2023/08/23 15:55:32 UTC

[GitHub] [incubator-celeborn] waitinfuture commented on a diff in pull request #1812: [CELEBORN-752] Support read local shuffle file for spark

waitinfuture commented on code in PR #1812:
URL: https://github.com/apache/incubator-celeborn/pull/1812#discussion_r1303214598


##########
client/src/main/java/org/apache/celeborn/client/read/LocalPartitionReader.java:
##########
@@ -0,0 +1,205 @@
+/*
+ * 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.client.read;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import io.netty.util.ReferenceCounted;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.celeborn.client.ShuffleClient;
+import org.apache.celeborn.common.CelebornConf;
+import org.apache.celeborn.common.exception.CelebornIOException;
+import org.apache.celeborn.common.network.client.TransportClient;
+import org.apache.celeborn.common.network.client.TransportClientFactory;
+import org.apache.celeborn.common.network.protocol.TransportMessage;
+import org.apache.celeborn.common.protocol.MessageType;
+import org.apache.celeborn.common.protocol.PartitionLocation;
+import org.apache.celeborn.common.protocol.PbOpenStream;
+import org.apache.celeborn.common.protocol.PbStreamHandler;
+import org.apache.celeborn.common.util.FileChannelUtils;
+
+public class LocalPartitionReader implements PartitionReader {
+
+  private static final Logger logger = LoggerFactory.getLogger(LocalPartitionReader.class);
+  private final LinkedBlockingQueue<ByteBuf> results;
+  private final AtomicReference<IOException> exception = new AtomicReference<>();
+  private final int fetchMaxReqsInFlight;
+  private final PartitionLocation location;
+  private volatile boolean closed = false;
+  private final Thread readThread;
+
+  private final int numChunks;
+  private int returnedChunks = 0;
+  private int currentChunkIndex = 0;
+  private final FileChannel shuffleChannel;
+
+  public LocalPartitionReader(
+      CelebornConf conf,
+      String shuffleKey,
+      PartitionLocation location,
+      TransportClientFactory clientFactory,
+      int startMapIndex,
+      int endMapIndex)
+      throws IOException {
+    fetchMaxReqsInFlight = conf.clientFetchMaxReqsInFlight();
+    results = new LinkedBlockingQueue<>();
+    this.location = location;
+    PbStreamHandler streamHandle;
+    long fetchTimeoutMs = conf.clientFetchTimeoutMs();
+    try {
+      TransportClient client =
+          clientFactory.createClient(location.getHost(), location.getFetchPort(), 0);
+      TransportMessage openStreamMsg =
+          new TransportMessage(
+              MessageType.OPEN_STREAM,
+              PbOpenStream.newBuilder()
+                  .setShuffleKey(shuffleKey)
+                  .setFileName(location.getFileName())
+                  .setStartIndex(startMapIndex)
+                  .setEndIndex(endMapIndex)
+                  .setReadLocalShuffle(true)
+                  .build()
+                  .toByteArray());
+      ByteBuffer response = client.sendRpcSync(openStreamMsg.toByteBuffer(), fetchTimeoutMs);
+      streamHandle = TransportMessage.fromByteBuffer(response).getParsedPayload();
+    } catch (IOException | InterruptedException e) {
+      throw new IOException(
+          "Read shuffle file from local file failed, partition location: "
+              + location
+              + " filePath: "
+              + location.getStorageInfo().getFilePath(),
+          e);
+    }
+
+    final List<Long> chunkOffsets = new ArrayList<>(streamHandle.getChunkOffsetsList());
+    numChunks = streamHandle.getNumChunks();
+    shuffleChannel = FileChannelUtils.openReadableFileChannel(streamHandle.getFullPath());
+
+    logger.debug(
+        "Local partition reader {} index count:{} offsets:{}",
+        location.getStorageInfo().getFilePath(),
+        chunkOffsets.size(),
+        chunkOffsets);
+
+    readThread =

Review Comment:
   Each partition has one thread may cause too many threads. I think it's better to pass a shared ExecutorService object to each LocalPartitionReader, and submit a reading task when `results` is polled out a chunk.



##########
client/src/main/java/org/apache/celeborn/client/read/LocalPartitionReader.java:
##########
@@ -0,0 +1,205 @@
+/*
+ * 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.client.read;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import io.netty.util.ReferenceCounted;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.celeborn.client.ShuffleClient;
+import org.apache.celeborn.common.CelebornConf;
+import org.apache.celeborn.common.exception.CelebornIOException;
+import org.apache.celeborn.common.network.client.TransportClient;
+import org.apache.celeborn.common.network.client.TransportClientFactory;
+import org.apache.celeborn.common.network.protocol.TransportMessage;
+import org.apache.celeborn.common.protocol.MessageType;
+import org.apache.celeborn.common.protocol.PartitionLocation;
+import org.apache.celeborn.common.protocol.PbOpenStream;
+import org.apache.celeborn.common.protocol.PbStreamHandler;
+import org.apache.celeborn.common.util.FileChannelUtils;
+
+public class LocalPartitionReader implements PartitionReader {
+
+  private static final Logger logger = LoggerFactory.getLogger(LocalPartitionReader.class);
+  private final LinkedBlockingQueue<ByteBuf> results;
+  private final AtomicReference<IOException> exception = new AtomicReference<>();
+  private final int fetchMaxReqsInFlight;
+  private final PartitionLocation location;
+  private volatile boolean closed = false;
+  private final Thread readThread;
+
+  private final int numChunks;
+  private int returnedChunks = 0;
+  private int currentChunkIndex = 0;
+  private final FileChannel shuffleChannel;
+
+  public LocalPartitionReader(
+      CelebornConf conf,
+      String shuffleKey,
+      PartitionLocation location,
+      TransportClientFactory clientFactory,
+      int startMapIndex,
+      int endMapIndex)
+      throws IOException {
+    fetchMaxReqsInFlight = conf.clientFetchMaxReqsInFlight();
+    results = new LinkedBlockingQueue<>();
+    this.location = location;
+    PbStreamHandler streamHandle;
+    long fetchTimeoutMs = conf.clientFetchTimeoutMs();
+    try {
+      TransportClient client =
+          clientFactory.createClient(location.getHost(), location.getFetchPort(), 0);
+      TransportMessage openStreamMsg =
+          new TransportMessage(
+              MessageType.OPEN_STREAM,
+              PbOpenStream.newBuilder()
+                  .setShuffleKey(shuffleKey)
+                  .setFileName(location.getFileName())
+                  .setStartIndex(startMapIndex)
+                  .setEndIndex(endMapIndex)
+                  .setReadLocalShuffle(true)
+                  .build()
+                  .toByteArray());
+      ByteBuffer response = client.sendRpcSync(openStreamMsg.toByteBuffer(), fetchTimeoutMs);
+      streamHandle = TransportMessage.fromByteBuffer(response).getParsedPayload();
+    } catch (IOException | InterruptedException e) {
+      throw new IOException(
+          "Read shuffle file from local file failed, partition location: "
+              + location
+              + " filePath: "
+              + location.getStorageInfo().getFilePath(),
+          e);
+    }
+
+    final List<Long> chunkOffsets = new ArrayList<>(streamHandle.getChunkOffsetsList());
+    numChunks = streamHandle.getNumChunks();
+    shuffleChannel = FileChannelUtils.openReadableFileChannel(streamHandle.getFullPath());
+
+    logger.debug(
+        "Local partition reader {} index count:{} offsets:{}",

Review Comment:
   I think it's no need to print `index count` since offsets are printed.



##########
client/src/main/java/org/apache/celeborn/client/read/LocalPartitionReader.java:
##########
@@ -0,0 +1,205 @@
+/*
+ * 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.client.read;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import io.netty.util.ReferenceCounted;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.celeborn.client.ShuffleClient;
+import org.apache.celeborn.common.CelebornConf;
+import org.apache.celeborn.common.exception.CelebornIOException;
+import org.apache.celeborn.common.network.client.TransportClient;
+import org.apache.celeborn.common.network.client.TransportClientFactory;
+import org.apache.celeborn.common.network.protocol.TransportMessage;
+import org.apache.celeborn.common.protocol.MessageType;
+import org.apache.celeborn.common.protocol.PartitionLocation;
+import org.apache.celeborn.common.protocol.PbOpenStream;
+import org.apache.celeborn.common.protocol.PbStreamHandler;
+import org.apache.celeborn.common.util.FileChannelUtils;
+
+public class LocalPartitionReader implements PartitionReader {
+
+  private static final Logger logger = LoggerFactory.getLogger(LocalPartitionReader.class);
+  private final LinkedBlockingQueue<ByteBuf> results;
+  private final AtomicReference<IOException> exception = new AtomicReference<>();
+  private final int fetchMaxReqsInFlight;
+  private final PartitionLocation location;
+  private volatile boolean closed = false;
+  private final Thread readThread;
+
+  private final int numChunks;
+  private int returnedChunks = 0;
+  private int currentChunkIndex = 0;
+  private final FileChannel shuffleChannel;
+
+  public LocalPartitionReader(
+      CelebornConf conf,
+      String shuffleKey,
+      PartitionLocation location,
+      TransportClientFactory clientFactory,
+      int startMapIndex,
+      int endMapIndex)
+      throws IOException {
+    fetchMaxReqsInFlight = conf.clientFetchMaxReqsInFlight();
+    results = new LinkedBlockingQueue<>();
+    this.location = location;
+    PbStreamHandler streamHandle;
+    long fetchTimeoutMs = conf.clientFetchTimeoutMs();
+    try {
+      TransportClient client =
+          clientFactory.createClient(location.getHost(), location.getFetchPort(), 0);
+      TransportMessage openStreamMsg =
+          new TransportMessage(
+              MessageType.OPEN_STREAM,
+              PbOpenStream.newBuilder()
+                  .setShuffleKey(shuffleKey)
+                  .setFileName(location.getFileName())
+                  .setStartIndex(startMapIndex)
+                  .setEndIndex(endMapIndex)
+                  .setReadLocalShuffle(true)
+                  .build()
+                  .toByteArray());
+      ByteBuffer response = client.sendRpcSync(openStreamMsg.toByteBuffer(), fetchTimeoutMs);
+      streamHandle = TransportMessage.fromByteBuffer(response).getParsedPayload();
+    } catch (IOException | InterruptedException e) {
+      throw new IOException(
+          "Read shuffle file from local file failed, partition location: "
+              + location
+              + " filePath: "
+              + location.getStorageInfo().getFilePath(),
+          e);
+    }
+
+    final List<Long> chunkOffsets = new ArrayList<>(streamHandle.getChunkOffsetsList());
+    numChunks = streamHandle.getNumChunks();
+    shuffleChannel = FileChannelUtils.openReadableFileChannel(streamHandle.getFullPath());
+
+    logger.debug(
+        "Local partition reader {} index count:{} offsets:{}",
+        location.getStorageInfo().getFilePath(),
+        chunkOffsets.size(),
+        chunkOffsets);
+
+    readThread =
+        new Thread(
+            () -> {
+              try {
+                while (!closed && currentChunkIndex < numChunks) {
+                  while (results.size() >= fetchMaxReqsInFlight) {
+                    Thread.sleep(10);
+                  }
+                  long offset = chunkOffsets.get(currentChunkIndex);
+                  long length = chunkOffsets.get(currentChunkIndex + 1) - offset;
+                  logger.debug("Read {} offset {} length {}", currentChunkIndex, offset, length);
+                  // A chunk must be lesser than INT.MAX_VALUE

Review Comment:
   smaller than



##########
client/src/main/java/org/apache/celeborn/client/read/CelebornInputStream.java:
##########
@@ -389,20 +394,30 @@ private PartitionReader createReader(
         logger.debug("Read peer {} for attempt {}.", location, attemptNumber);
       }
 
-      logger.debug("create reader for location {}", location);
+      logger.debug("Create reader for location {}", location);
 
       StorageInfo storageInfo = location.getStorageInfo();
       if (storageInfo.getType() == StorageInfo.Type.HDD
           || storageInfo.getType() == StorageInfo.Type.SSD) {
-        return new WorkerPartitionReader(
-            conf,
-            shuffleKey,
-            location,
-            clientFactory,
-            startMapIndex,
-            endMapIndex,
-            fetchChunkRetryCnt,
-            fetchChunkMaxRetry);
+        logger.debug(
+            "Read local shuffle file {} , {}, {}",

Review Comment:
   Read local shuffle file enabled



##########
worker/src/test/scala/org/apache/celeborn/service/deploy/cluster/ClusterReadWriteTestWithLZ4.scala:
##########
@@ -25,4 +25,8 @@ class ClusterReadWriteTestWithLZ4 extends ReadWriteTestBase {
     testReadWriteByCode(CompressionCodec.LZ4)
   }
 
+  test(s"test MiniCluster With LZ4 and local read for spark") {
+    testReadWriteByCode(CompressionCodec.ZSTD, true)

Review Comment:
   LZ4



-- 
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: issues-unsubscribe@celeborn.apache.org

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