SteNicholas commented on code in PR #2820: URL: https://github.com/apache/celeborn/pull/2820#discussion_r1808162385
########## worker/src/main/java/org/apache/celeborn/service/deploy/worker/storage/segment/SegmentMapPartitionDataReader.java: ########## @@ -0,0 +1,321 @@ +/* + * 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.util.Deque; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.Map; + +import javax.annotation.concurrent.GuardedBy; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.Channel; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.apache.celeborn.common.meta.DiskFileInfo; +import org.apache.celeborn.service.deploy.worker.memory.BufferRecycler; +import org.apache.celeborn.service.deploy.worker.memory.RecyclableBuffer; +import org.apache.celeborn.service.deploy.worker.memory.RecyclableSegmentIdBuffer; +import org.apache.celeborn.service.deploy.worker.storage.MapPartitionDataReader; + +public class SegmentMapPartitionDataReader extends MapPartitionDataReader { + + public static final Logger logger = LoggerFactory.getLogger(SegmentMapPartitionDataReader.class); + + // The Flink EndOfSegment buffer data type, which has been written + // to the buffer header, is in the 16th bit(starting from 0). + private static final int END_OF_SEGMENT_BUFFER_DATA_TYPE = 7; + + private final int startPartitionIndex; + + private final int endPartitionIndex; + + @GuardedBy("lock") + private final Deque<Integer> backlogs = new LinkedList<>(); + + @GuardedBy("lock") + // subpartitionId -> segmentId, current required segmentId by client per subpartition + private Map<Integer, Integer> subPartitionRequiredSegmentIds = new HashMap<>(); + + @GuardedBy("lock") + private boolean hasUpdateSegmentId = false; + + @GuardedBy("lock") + // subpartitionId -> segmentId, segmentId of current reading buffer per subpartition + private Map<Integer, Integer> subPartitionLastSegmentIds = new HashMap<>(); + + @GuardedBy("lock") + // subpartitionId -> buffer index, current reading buffer index per subpartition + private Map<Integer, Integer> subPartitionNextBufferIndex = new HashMap<>(); + + public SegmentMapPartitionDataReader( + int startPartitionIndex, + int endPartitionIndex, + DiskFileInfo fileInfo, + long streamId, + Channel associatedChannel, + Runnable recycleStream, + boolean requireSubpartitionId) { + super( + startPartitionIndex, + endPartitionIndex, + fileInfo, + streamId, + associatedChannel, + recycleStream, + requireSubpartitionId); + this.startPartitionIndex = startPartitionIndex; + this.endPartitionIndex = endPartitionIndex; + + for (int i = startPartitionIndex; i <= endPartitionIndex; i++) { + subPartitionLastSegmentIds.put(i, -1); + subPartitionRequiredSegmentIds.put(i, -1); + subPartitionNextBufferIndex.put(i, 0); + } + } + + @Override + protected void tryNotifyBacklog(int numDataBuffers) { + notifyBacklog(getBacklog()); + } + + @Override + public synchronized void sendData() { + while (true) { + synchronized (lock) { + if (!hasUpdateSegmentId) { + logger.debug( + "The required segment id is not updated for {}, skip sending data.", getStreamId()); + return; + } + + RecyclableBuffer buffer; + + // Verify if the client requires the segmentId of the first buffer; if so, send it to the + // worker; otherwise, wait until the client sends a new segmentId. + boolean breakLoop = false; + while ((buffer = buffersToSend.peek()) instanceof RecyclableSegmentIdBuffer) { + RecyclableSegmentIdBuffer recyclableSegmentIdBuffer = (RecyclableSegmentIdBuffer) buffer; + int subPartitionId = recyclableSegmentIdBuffer.getSubPartitionId(); + int segmentId = recyclableSegmentIdBuffer.getSegmentId(); + if (segmentId != subPartitionRequiredSegmentIds.get(subPartitionId)) { + // If the queued head buffer is not the required segment id, we do not sent it. + logger.info( + "The queued head buffer is not the required segment id, " + + "do not sent it. details: streamId {}, subPartitionId: {}, current segment id: {}, required segment id: {}, reader: {}", + streamId, + subPartitionId, + segmentId, + subPartitionRequiredSegmentIds.get(subPartitionId), + this); + breakLoop = true; + break; + } else { + buffersToSend.poll(); + } + } + if (breakLoop) { + break; + } + + // fetch first buffer and send + buffer = fetchBufferToSend(); + if (buffer instanceof RecyclableSegmentIdBuffer) { Review Comment: checkState(!(buffer instanceof RecyclableSegmentIdBuffer)); -- 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]
