You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by fa...@apache.org on 2023/07/12 10:41:46 UTC

[flink] 01/02: [FLINK-32420][refactor] Add the AbstractHeapPriorityQueueElement

This is an automated email from the ASF dual-hosted git repository.

fanrui pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git

commit 224aa2e9ca20ed7ce009a2c2f485c1a232e4c807
Author: 1996fanrui <19...@gmail.com>
AuthorDate: Thu Jun 29 00:36:58 2023 +0800

    [FLINK-32420][refactor] Add the AbstractHeapPriorityQueueElement
---
 .../heap/AbstractHeapPriorityQueueElement.java     | 39 ++++++++++++++++++++++
 .../state/InternalPriorityQueueTestBase.java       | 18 ++--------
 .../runtime/testutils/statemigration/TestType.java | 18 ++--------
 .../state/RocksDBCachingPriorityQueueSet.java      | 17 ++--------
 4 files changed, 47 insertions(+), 45 deletions(-)

diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/state/heap/AbstractHeapPriorityQueueElement.java b/flink-runtime/src/main/java/org/apache/flink/runtime/state/heap/AbstractHeapPriorityQueueElement.java
new file mode 100644
index 00000000000..bee5c0eb3ab
--- /dev/null
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/state/heap/AbstractHeapPriorityQueueElement.java
@@ -0,0 +1,39 @@
+/*
+ * 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.state.heap;
+
+/** Abstract base class for {@link HeapPriorityQueueElement}. */
+public abstract class AbstractHeapPriorityQueueElement implements HeapPriorityQueueElement {
+
+    private int internalIndex;
+
+    public AbstractHeapPriorityQueueElement() {
+        this.internalIndex = HeapPriorityQueueElement.NOT_CONTAINED;
+    }
+
+    @Override
+    public int getInternalIndex() {
+        return internalIndex;
+    }
+
+    @Override
+    public void setInternalIndex(int newIndex) {
+        this.internalIndex = newIndex;
+    }
+}
diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/state/InternalPriorityQueueTestBase.java b/flink-runtime/src/test/java/org/apache/flink/runtime/state/InternalPriorityQueueTestBase.java
index 4a3b243fca4..0d1387dc4d0 100644
--- a/flink-runtime/src/test/java/org/apache/flink/runtime/state/InternalPriorityQueueTestBase.java
+++ b/flink-runtime/src/test/java/org/apache/flink/runtime/state/InternalPriorityQueueTestBase.java
@@ -25,7 +25,7 @@ import org.apache.flink.core.memory.ByteArrayOutputStreamWithPos;
 import org.apache.flink.core.memory.DataInputView;
 import org.apache.flink.core.memory.DataOutputView;
 import org.apache.flink.core.memory.DataOutputViewStreamWrapper;
-import org.apache.flink.runtime.state.heap.HeapPriorityQueueElement;
+import org.apache.flink.runtime.state.heap.AbstractHeapPriorityQueueElement;
 import org.apache.flink.util.CloseableIterator;
 import org.apache.flink.util.MathUtils;
 
@@ -343,17 +343,15 @@ public abstract class InternalPriorityQueueTestBase {
     protected abstract boolean testSetSemanticsAgainstDuplicateElements();
 
     /** Payload for usage in the test. */
-    protected static class TestElement
-            implements HeapPriorityQueueElement, Keyed<Long>, PriorityComparable<TestElement> {
+    protected static class TestElement extends AbstractHeapPriorityQueueElement
+            implements Keyed<Long>, PriorityComparable<TestElement> {
 
         private final long key;
         private final long priority;
-        private int internalIndex;
 
         public TestElement(long key, long priority) {
             this.key = key;
             this.priority = priority;
-            this.internalIndex = NOT_CONTAINED;
         }
 
         @Override
@@ -369,16 +367,6 @@ public abstract class InternalPriorityQueueTestBase {
             return priority;
         }
 
-        @Override
-        public int getInternalIndex() {
-            return internalIndex;
-        }
-
-        @Override
-        public void setInternalIndex(int newIndex) {
-            internalIndex = newIndex;
-        }
-
         @Override
         public boolean equals(Object o) {
             if (this == o) {
diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/testutils/statemigration/TestType.java b/flink-runtime/src/test/java/org/apache/flink/runtime/testutils/statemigration/TestType.java
index b024f76cd41..8db517f68b3 100644
--- a/flink-runtime/src/test/java/org/apache/flink/runtime/testutils/statemigration/TestType.java
+++ b/flink-runtime/src/test/java/org/apache/flink/runtime/testutils/statemigration/TestType.java
@@ -24,7 +24,7 @@ import org.apache.flink.core.memory.DataInputView;
 import org.apache.flink.core.memory.DataOutputView;
 import org.apache.flink.runtime.state.Keyed;
 import org.apache.flink.runtime.state.PriorityComparable;
-import org.apache.flink.runtime.state.heap.HeapPriorityQueueElement;
+import org.apache.flink.runtime.state.heap.AbstractHeapPriorityQueueElement;
 
 import org.junit.Assert;
 
@@ -38,10 +38,8 @@ import java.util.Objects;
  *
  * <p>This is implemented so that the type can also be used as keyed priority queue state.
  */
-public class TestType
-        implements HeapPriorityQueueElement, PriorityComparable<TestType>, Keyed<String> {
-
-    private int index;
+public class TestType extends AbstractHeapPriorityQueueElement
+        implements PriorityComparable<TestType>, Keyed<String> {
 
     private final int value;
     private final String key;
@@ -65,16 +63,6 @@ public class TestType
         return Integer.compare(value, other.value);
     }
 
-    @Override
-    public int getInternalIndex() {
-        return index;
-    }
-
-    @Override
-    public void setInternalIndex(int newIndex) {
-        this.index = newIndex;
-    }
-
     @Override
     public boolean equals(Object obj) {
         if (obj == null || !(obj instanceof TestType)) {
diff --git a/flink-state-backends/flink-statebackend-rocksdb/src/main/java/org/apache/flink/contrib/streaming/state/RocksDBCachingPriorityQueueSet.java b/flink-state-backends/flink-statebackend-rocksdb/src/main/java/org/apache/flink/contrib/streaming/state/RocksDBCachingPriorityQueueSet.java
index 8dbaca93f5e..b3d0112dc60 100644
--- a/flink-state-backends/flink-statebackend-rocksdb/src/main/java/org/apache/flink/contrib/streaming/state/RocksDBCachingPriorityQueueSet.java
+++ b/flink-state-backends/flink-statebackend-rocksdb/src/main/java/org/apache/flink/contrib/streaming/state/RocksDBCachingPriorityQueueSet.java
@@ -23,6 +23,7 @@ import org.apache.flink.core.memory.DataInputDeserializer;
 import org.apache.flink.core.memory.DataOutputSerializer;
 import org.apache.flink.runtime.state.CompositeKeySerializationUtils;
 import org.apache.flink.runtime.state.InternalPriorityQueue;
+import org.apache.flink.runtime.state.heap.AbstractHeapPriorityQueueElement;
 import org.apache.flink.runtime.state.heap.HeapPriorityQueueElement;
 import org.apache.flink.util.CloseableIterator;
 import org.apache.flink.util.FlinkRuntimeException;
@@ -56,7 +57,7 @@ import static org.apache.flink.contrib.streaming.state.RocksDBCachingPriorityQue
  * @param <E> the type of the contained elements in the queue.
  */
 public class RocksDBCachingPriorityQueueSet<E extends HeapPriorityQueueElement>
-        implements InternalPriorityQueue<E>, HeapPriorityQueueElement {
+        extends AbstractHeapPriorityQueueElement implements InternalPriorityQueue<E> {
 
     /** Serialized empty value to insert into RocksDB. */
     private static final byte[] DUMMY_BYTES = new byte[] {};
@@ -102,9 +103,6 @@ public class RocksDBCachingPriorityQueueSet<E extends HeapPriorityQueueElement>
     /** This flag is true iff all elements in RocksDB are also contained in the cache. */
     private boolean allElementsInCache;
 
-    /** Index for management as a {@link HeapPriorityQueueElement}. */
-    private int internalIndex;
-
     RocksDBCachingPriorityQueueSet(
             @Nonnegative int keyGroupId,
             @Nonnegative int keyGroupPrefixBytes,
@@ -127,7 +125,6 @@ public class RocksDBCachingPriorityQueueSet<E extends HeapPriorityQueueElement>
         this.allElementsInCache = false;
         this.groupPrefixBytes = createKeyGroupBytes(keyGroupId, keyGroupPrefixBytes);
         this.seekHint = groupPrefixBytes;
-        this.internalIndex = HeapPriorityQueueElement.NOT_CONTAINED;
     }
 
     @Nullable
@@ -289,16 +286,6 @@ public class RocksDBCachingPriorityQueueSet<E extends HeapPriorityQueueElement>
         }
     }
 
-    @Override
-    public int getInternalIndex() {
-        return internalIndex;
-    }
-
-    @Override
-    public void setInternalIndex(int newIndex) {
-        this.internalIndex = newIndex;
-    }
-
     @Nonnull
     private RocksBytesIterator orderedBytesIterator() {
         flushWriteBatch();