SteNicholas commented on code in PR #2741:
URL: https://github.com/apache/celeborn/pull/2741#discussion_r1770738444


##########
worker/src/main/java/org/apache/celeborn/service/deploy/worker/storage/segment/SegmentMapPartitionFileWriter.java:
##########
@@ -0,0 +1,166 @@
+/*
+ * 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.storage.segment;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+
+import io.netty.buffer.ByteBuf;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.celeborn.common.CelebornConf;
+import org.apache.celeborn.common.meta.MapFileMeta;
+import org.apache.celeborn.common.metrics.source.AbstractSource;
+import org.apache.celeborn.service.deploy.worker.storage.DeviceMonitor;
+import 
org.apache.celeborn.service.deploy.worker.storage.MapPartitionDataWriter;
+import 
org.apache.celeborn.service.deploy.worker.storage.PartitionDataWriterContext;
+import org.apache.celeborn.service.deploy.worker.storage.StorageManager;
+
+/**
+ * Write the shuffle file in map partition format with segment granularity 
visibility. This means
+ * that the shuffle file should be written intermediate, allowing it to be 
read in segments rather
+ * than waiting for the entire shuffle file to be completed.
+ */
+public class SegmentMapPartitionFileWriter extends MapPartitionDataWriter {
+
+  public static final Logger logger = 
LoggerFactory.getLogger(SegmentMapPartitionFileWriter.class);
+
+  /**
+   * subPartitionId -> started (boolean). There are 3 cases: 1. If the 
subPartition key not exist,
+   * it indicates that the subPartition has not sent the {@link
+   * org.apache.celeborn.common.protocol.PbSegmentStart}. Therefore, shuffle 
data should not be
+   * written in this case. 2. If the subPartition key exists and the started 
value is true, it means
+   * that the subPartition has initiated the segment. In this situation, the 
next buffer for this
+   * subPartition will be the first buffer of the current segment. The 
information about the first
+   * buffer will be recorded in {@code 
MapFileMeta#subPartitionSegmentIndexes}. 3. If the
+   * subPartition key exists and the started value is false, it means that the 
subPartition has
+   * initiated the segment, but the next buffer for this subPartition is not 
the first buffer of the
+   * current segment.
+   */
+  private final Map<Integer, Boolean> subPartitionHasStartSegment;
+
+  // current buffer index per subPartition
+  private int[] subPartitionBufferIndex;
+
+  public SegmentMapPartitionFileWriter(
+      StorageManager storageManager,
+      AbstractSource workerSource,
+      CelebornConf conf,
+      DeviceMonitor deviceMonitor,
+      PartitionDataWriterContext writerContext)
+      throws IOException {
+    super(storageManager, workerSource, conf, deviceMonitor, writerContext);
+    this.subPartitionHasStartSegment = new HashMap<>();
+  }
+
+  @Override
+  public void pushDataHandShake(int numSubpartitions, int bufferSize) {
+    super.pushDataHandShake(numSubpartitions, bufferSize);
+    subPartitionBufferIndex = new int[numSubpartitions];
+    Arrays.fill(subPartitionBufferIndex, 0);
+    getFileMeta().setIsWriterClosed(false);
+    getFileMeta().setSegmentGranularityVisible(true);
+  }
+
+  @Override
+  public void write(ByteBuf data) throws IOException {
+    data.markReaderIndex();
+    int subPartitionId = data.readInt();
+    int attemptId = data.readInt();
+    int batchId = data.readInt();
+    int size = data.readInt();
+
+    if (!subPartitionHasStartSegment.containsKey(subPartitionId)) {
+      throw new IllegalStateException("This partition may not start a segment: 
" + subPartitionId);
+    }
+    int currentSubpartition = getCurrentSubpartition();
+    // the subPartitionId must be ordered in a region
+    if (subPartitionId < currentSubpartition) {
+      throw new IOException(
+          "Must writing data in reduce partition index order, but now 
supPartitionId is "

Review Comment:
   Ditto. Please add `attemptId`, `batchId` into message.



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