jiangxin369 commented on code in PR #248:
URL: https://github.com/apache/flink-ml/pull/248#discussion_r1297236349


##########
flink-ml-iteration/flink-ml-iteration-common/src/main/java/org/apache/flink/iteration/operator/feedback/SpillableFeedbackQueue.java:
##########
@@ -0,0 +1,115 @@
+/*
+ * 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.iteration.operator.feedback;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.typeutils.TypeSerializer;
+import org.apache.flink.core.memory.DataInputView;
+import org.apache.flink.core.memory.DataOutputSerializer;
+import org.apache.flink.core.memory.MemorySegment;
+import org.apache.flink.runtime.io.disk.InputViewIterator;
+import org.apache.flink.runtime.io.disk.SpillingBuffer;
+import org.apache.flink.runtime.io.disk.iomanager.IOManager;
+import org.apache.flink.runtime.memory.MemoryAllocationException;
+import org.apache.flink.runtime.memory.MemoryManager;
+import org.apache.flink.table.runtime.operators.sort.ListMemorySegmentPool;
+import org.apache.flink.util.MutableObjectIterator;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * * A queue that can spill the items to disks automatically when the memory 
buffer is full.
+ *
+ * @param <T> The element type.
+ */
+@Internal
+final class SpillableFeedbackQueue<T> {
+    private final DataOutputSerializer output = new DataOutputSerializer(256);
+    private final TypeSerializer<T> serializer;
+    private final IOManager ioManager;
+    private final MemoryManager memoryManager;
+    private final int numPages;
+
+    private List<MemorySegment> segments;
+    private ListMemorySegmentPool segmentPool;
+
+    private SpillingBuffer target;
+    private long size = 0L;
+
+    SpillableFeedbackQueue(
+            IOManager ioManager,
+            MemoryManager memoryManager,
+            TypeSerializer<T> serializer,
+            long inMemoryBufferSize,
+            long pageSize)
+            throws MemoryAllocationException {
+        this.serializer = Objects.requireNonNull(serializer);
+        this.ioManager = Objects.requireNonNull(ioManager);
+        this.memoryManager = Objects.requireNonNull(memoryManager);
+
+        this.numPages = (int) (inMemoryBufferSize / pageSize);
+        resetSpillingBuffer();
+    }
+
+    void add(T item) {
+        try {
+            output.clear();
+            serializer.serialize(item, output);
+            target.write(output.getSharedBuffer(), 0, output.length());
+            size++;
+        } catch (IOException e) {
+            throw new IllegalStateException(e);
+        }
+    }
+
+    MutableObjectIterator<T> iterate() {
+        try {
+            DataInputView input = target.flip();
+            return new InputViewIterator<>(input, this.serializer);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    long size() {
+        return size;
+    }
+
+    void reset() throws Exception {
+        size = 0;
+        close();
+        resetSpillingBuffer();

Review Comment:
   `SpillingResettableIterator` is not similar to our requirement because it 
just iterates an existing collection and doesn't need to re-allocate more 
memories, however, the `SpillableFeedbackQueue` needs to receive, iterate and 
free items again and again. With the given API of `SpillingBuffer`, I think we 
need to release and re-allocate the buffers. 
   Btw, I think the allocation is just taking out a piece of memories from the 
reserved ones which doesn't cost much.



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