TanYuxin-tyx commented on code in PR #22804:
URL: https://github.com/apache/flink/pull/22804#discussion_r1247346559


##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/disk/SubpartitionDiskCacheManager.java:
##########
@@ -0,0 +1,130 @@
+/*
+ * 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.flink.runtime.io.network.partition.hybrid.tiered.tier.disk;
+
+import org.apache.flink.api.java.tuple.Tuple2;
+import org.apache.flink.core.memory.MemorySegment;
+import org.apache.flink.core.memory.MemorySegmentFactory;
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import org.apache.flink.runtime.io.network.buffer.Buffer.DataType;
+import org.apache.flink.runtime.io.network.buffer.FreeingBufferRecycler;
+import org.apache.flink.runtime.io.network.buffer.NetworkBuffer;
+
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Deque;
+import java.util.LinkedList;
+import java.util.List;
+
+import static org.apache.flink.util.Preconditions.checkArgument;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * The {@link SubpartitionDiskCacheManager} is responsible to manage the 
cached buffers in a single
+ * subpartition.
+ */
+class SubpartitionDiskCacheManager {
+
+    /**
+     * All the buffers. The first field of the tuple is the buffer, while the 
second field of the
+     * buffer is the buffer index.
+     *
+     * <p>Note that this field can be accessed by the task thread or the write 
IO thread, so the
+     * thread safety should be ensured.
+     */
+    private final Deque<Tuple2<Buffer, Integer>> allBuffers = new 
LinkedList<>();
+
+    /**
+     * Record the buffer index in the {@link SubpartitionDiskCacheManager}. 
Each time a new buffer
+     * is added to the {@code allBuffers}, this field is increased by one.
+     */
+    private int bufferIndex;
+
+    /**
+     * Record the segment id that is writing to. Each time when the segment is 
finished, this filed
+     * is increased by one.
+     */
+    private int segmentIndex;
+
+    // ------------------------------------------------------------------------
+    //  Called by DiskCacheManager
+    // ------------------------------------------------------------------------
+
+    void append(Buffer buffer) {
+        addBuffer(buffer);
+    }
+
+    void appendEndOfSegmentEvent(ByteBuffer record, DataType dataType) {
+        writeEvent(record, dataType);
+        segmentIndex++;
+    }
+
+    /** Note that allBuffers can be touched by multiple threads. */
+    List<Tuple2<Buffer, Integer>> removeAllBuffers() {
+        synchronized (allBuffers) {
+            List<Tuple2<Buffer, Integer>> targetBuffers = new 
ArrayList<>(allBuffers);
+            allBuffers.clear();
+            return targetBuffers;
+        }
+    }
+
+    int getBufferIndex() {
+        return bufferIndex;
+    }
+
+    int getSegmentIndex() {
+        return segmentIndex;
+    }
+
+    void release() {
+        recycleBuffers();
+        checkState(allBuffers.isEmpty(), "Leaking buffers.");
+    }
+
+    // ------------------------------------------------------------------------
+    //  Internal Methods
+    // ------------------------------------------------------------------------
+
+    private void writeEvent(ByteBuffer event, DataType dataType) {
+        checkArgument(dataType.isEvent());
+
+        MemorySegment data = MemorySegmentFactory.wrap(event.array());
+        addBuffer(new NetworkBuffer(data, FreeingBufferRecycler.INSTANCE, 
dataType, data.size()));
+    }
+
+    /** Note that allBuffers can be touched by multiple threads. */

Review Comment:
   Fixed.



##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/tier/disk/DiskCacheManager.java:
##########
@@ -0,0 +1,174 @@
+/*
+ * 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.flink.runtime.io.network.partition.hybrid.tiered.tier.disk;
+
+import org.apache.flink.api.java.tuple.Tuple2;
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import 
org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStoragePartitionId;
+import 
org.apache.flink.runtime.io.network.partition.hybrid.tiered.file.PartitionFileWriter;
+import 
org.apache.flink.runtime.io.network.partition.hybrid.tiered.storage.TieredStorageMemoryManager;
+import org.apache.flink.util.concurrent.FutureUtils;
+
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+
+/**
+ * The {@link DiskCacheManager} is responsible for managing cached buffers 
before flushing to files.
+ */
+class DiskCacheManager {
+
+    private final TieredStoragePartitionId partitionId;
+
+    private final int numSubpartitions;
+
+    private final PartitionFileWriter partitionFileWriter;
+
+    private final SubpartitionDiskCacheManager[] subpartitionCacheManagers;
+
+    /** Whether the current flush process has completed. */
+    private CompletableFuture<Void> hasFlushCompleted;
+
+    DiskCacheManager(
+            TieredStoragePartitionId partitionId,
+            int numSubpartitions,
+            TieredStorageMemoryManager storageMemoryManager,
+            PartitionFileWriter partitionFileWriter) {
+        this.partitionId = partitionId;
+        this.numSubpartitions = numSubpartitions;
+        this.partitionFileWriter = partitionFileWriter;
+        this.subpartitionCacheManagers = new 
SubpartitionDiskCacheManager[numSubpartitions];
+        this.hasFlushCompleted = FutureUtils.completedVoidFuture();
+
+        for (int subpartitionId = 0; subpartitionId < numSubpartitions; 
++subpartitionId) {
+            subpartitionCacheManagers[subpartitionId] = new 
SubpartitionDiskCacheManager();
+        }
+        
storageMemoryManager.listenBufferReclaimRequest(this::notifyFlushCachedBuffers);
+    }
+
+    // ------------------------------------------------------------------------
+    //  Called by DiskTierProducerAgent
+    // ------------------------------------------------------------------------
+
+    /**
+     * Append buffer to {@link DiskCacheManager}.
+     *
+     * @param buffer to be managed by this class.
+     * @param subpartitionId the subpartition of this record.
+     */
+    void append(Buffer buffer, int subpartitionId) {
+        subpartitionCacheManagers[subpartitionId].append(buffer);
+    }
+
+    /**
+     * Append the end-of-segment event to {@link DiskCacheManager}, which 
indicates the segment has
+     * finished.
+     *
+     * @param record the end-of-segment event
+     * @param subpartitionId target subpartition of this record.
+     * @param dataType the type of this record. In other words, is it data or 
event.
+     */
+    void appendEndOfSegmentEvent(ByteBuffer record, int subpartitionId, 
Buffer.DataType dataType) {

Review Comment:
   Fixed.



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