You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by "jiangxin369 (via GitHub)" <gi...@apache.org> on 2023/09/12 09:05:14 UTC
[GitHub] [flink-ml] jiangxin369 commented on a diff in pull request #248: [FLINK-32704] Supports spilling to disk when feedback channel memory buffer is full
jiangxin369 commented on code in PR #248:
URL: https://github.com/apache/flink-ml/pull/248#discussion_r1322717816
##########
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:
After reading the code in Flink, I found that the `SerializedUpdateBuffer` is just what we need, so I replaced the SpillingBuffer with SerializedUpdateBuffer to avoid allocating memory multiple times.
--
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@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org