reswqa commented on code in PR #20100:
URL: https://github.com/apache/flink/pull/20100#discussion_r918596608


##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/HsSelectiveSpillingStrategy.java:
##########
@@ -0,0 +1,122 @@
+/*
+ * 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;
+
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Deque;
+import java.util.List;
+import java.util.PriorityQueue;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+
+/**
+ * A special implementation of {@link HsSpillingStrategy} that reduce disk 
writes as much as
+ * possible.
+ */
+public class HsSelectiveSpillingStrategy implements HsSpillingStrategy {
+    public final float spillBufferRatio;
+
+    public final float spillThreshold;
+
+    public HsSelectiveSpillingStrategy(HybridShuffleConfiguration 
hybridShuffleConfiguration) {
+        spillThreshold = 
hybridShuffleConfiguration.getSelectiveSpillThreshold();
+        spillBufferRatio = 
hybridShuffleConfiguration.getSelectiveSpillBufferRatio();
+    }
+
+    /**
+     * For the case of buffer finished, there is no need to make a decision 
for {@link
+     * HsSelectiveSpillingStrategy}.
+     */
+    @Override
+    public Action onBufferFinished(
+            HsSpillingInfoProvider spillingInfoProvider, BufferWithIdentity 
finishedBuffer) {
+        return Action.EMPTY;
+    }
+
+    /**
+     * For the case of buffer consumed, this buffer need release. The control 
of the buffer is taken
+     * over by the downstream task.
+     */
+    @Override
+    public Action onBufferConsumed(
+            HsSpillingInfoProvider spillingInfoProvider, BufferWithIdentity 
consumedBuffer) {
+        return Action.builder().addBufferToRelease(consumedBuffer).build();
+    }
+
+    /**
+     * When the amount of memory used exceeds the threshold, trigger the 
spilling operation and
+     * score the buffer of each subpartition. The lower the score, the more 
likely the buffer will
+     * be consumed in the next time, and should be kept in memory as much as 
possible. Select all
+     * buffers that need to be spilled according to the score from high to low.
+     */
+    @Override
+    public Action checkForAction(HsSpillingInfoProvider spillInfoProvider) {
+        if (spillInfoProvider.getNumTotalUnSpilledBuffers()
+                < spillInfoProvider.getPoolSize() * spillThreshold) {
+            return Action.EMPTY;
+        }
+
+        List<Integer> consumptionProgress = 
spillInfoProvider.getConsumptionProgress();
+        PriorityQueue<BufferScoreQueue> heap = new PriorityQueue<>();
+        for (int channel = 0; channel < 
spillInfoProvider.getNumSubpartitions(); channel++) {
+            Deque<BufferWithIdentity> unSpilledBuffers =
+                    spillInfoProvider.getUnSpilledBuffers(channel);
+            // In order to prevent the impact on the original queue, copy it 
and then pass to heap.
+            Deque<BufferWithIdentity> copiedQueue = new 
ArrayDeque<>(unSpilledBuffers);

Review Comment:
   Rewritten here using PeekingIterator



-- 
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-unsubscr...@flink.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to