You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by "xuyangzhong (via GitHub)" <gi...@apache.org> on 2024/12/03 05:37:01 UTC

[PR] [FLINK-36831][table] Introduce AppendOnlyTopNFunction in Rank with Async State API [flink]

xuyangzhong opened a new pull request, #25723:
URL: https://github.com/apache/flink/pull/25723

   ## What is the purpose of the change
   
   Introduce AppendOnlyTopNFunction in Rank with Async State API.
   
   ## Brief change log
   
     - *Support variable rank end for async state top-n function*
     - *Add AsyncStateAppendOnlyTopNFunction and AppendOnlyTopNHelper*
     - *Add ITs and HarnessTests*
   
   ## Verifying this change
   
   Existent tests and new added tests can verify this change.
   
   ## Does this pull request potentially affect one of the following parts:
   
     - Dependencies (does it add or upgrade a dependency): no
     - The public API, i.e., is any changed class annotated with `@Public(Evolving)`: no
     - The serializers: no
     - The runtime per-record code paths (performance sensitive): no
     - Anything that affects deployment or recovery: JobManager (and its components), Checkpointing, Kubernetes/Yarn, ZooKeeper: no
     - The S3 file system connector: no
   
   ## Documentation
   
     - Does this pull request introduce a new feature? yes
     - If yes, how is the feature documented?  A independent pr will be introduced later.
   


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


Re: [PR] [FLINK-36831][table] Introduce AppendOnlyTopNFunction in Rank with Async State API [flink]

Posted by "xuyangzhong (via GitHub)" <gi...@apache.org>.
xuyangzhong commented on code in PR #25723:
URL: https://github.com/apache/flink/pull/25723#discussion_r1914191986


##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/operators/rank/UpdatableTopNFunction.java:
##########
@@ -541,8 +541,10 @@ private void processElementWithoutRowNumber(RowData inputRow, Collector<RowData>
         }
     }
 
-    private void flushBufferToState(Tuple2<TopNBuffer, Map<RowData, RankRow>> bufferEntry)
+    private void flushBufferToState(

Review Comment:
   As it stands, there is no issue with the append top N. I have placed it under the Jira ticket for introducing the updatable top N with the async state API https://issues.apache.org/jira/browse/FLINK-36936



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


Re: [PR] [FLINK-36831][table] Introduce AppendOnlyTopNFunction in Rank with Async State API [flink]

Posted by "lincoln-lil (via GitHub)" <gi...@apache.org>.
lincoln-lil commented on code in PR #25723:
URL: https://github.com/apache/flink/pull/25723#discussion_r1914390827


##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecRank.java:
##########
@@ -283,17 +284,31 @@ protected Transformation<RowData> translateToPlanInternal(
                                     cacheSize);
                 }
             } else {
-                processFunction =
-                        new AppendOnlyTopNFunction(
-                                ttlConfig,
-                                inputRowTypeInfo,
-                                sortKeyComparator,
-                                sortKeySelector,
-                                rankType,
-                                rankRange,
-                                generateUpdateBefore,
-                                outputRankNumber,
-                                cacheSize);
+                if (isAsyncStateEnabled) {

Review Comment:
   nit: we can remove the TODO in L362 directly (there'll be no coming  `UnaryUpdateTopNFunction`)



##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/operators/rank/async/AsyncStateAppendOnlyTopNFunction.java:
##########
@@ -0,0 +1,199 @@
+/*
+ * 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.table.runtime.operators.rank.async;
+
+import org.apache.flink.api.common.functions.OpenContext;
+import org.apache.flink.api.common.serialization.SerializerConfigImpl;
+import org.apache.flink.api.common.state.StateTtlConfig;
+import org.apache.flink.api.common.state.v2.MapState;
+import org.apache.flink.api.common.state.v2.StateFuture;
+import org.apache.flink.api.common.typeutils.TypeSerializer;
+import org.apache.flink.api.java.typeutils.ListTypeInfo;
+import org.apache.flink.core.state.StateFutureUtils;
+import org.apache.flink.runtime.state.v2.MapStateDescriptor;
+import org.apache.flink.streaming.api.operators.StreamingRuntimeContext;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.runtime.generated.GeneratedRecordComparator;
+import org.apache.flink.table.runtime.keyselector.RowDataKeySelector;
+import org.apache.flink.table.runtime.operators.rank.AppendOnlyTopNFunction;
+import org.apache.flink.table.runtime.operators.rank.RankRange;
+import org.apache.flink.table.runtime.operators.rank.RankType;
+import org.apache.flink.table.runtime.operators.rank.TopNBuffer;
+import org.apache.flink.table.runtime.operators.rank.utils.AppendOnlyTopNHelper;
+import org.apache.flink.table.runtime.typeutils.InternalTypeInfo;
+import org.apache.flink.util.Collector;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+/**
+ * A TopN function could handle insert-only stream.
+ *
+ * <p>The input stream should only contain INSERT messages.
+ *
+ * <p>Different with {@link AppendOnlyTopNFunction}, this function is used with async state api.
+ */
+public class AsyncStateAppendOnlyTopNFunction extends AbstractAsyncStateTopNFunction {
+
+    private static final long serialVersionUID = 1L;
+
+    private final InternalTypeInfo<RowData> sortKeyType;
+    private final TypeSerializer<RowData> inputRowSer;
+    private final long cacheSize;
+
+    // a map state stores mapping from sort key to records list which is in topN
+    private transient MapState<RowData, List<RowData>> dataState;
+
+    private transient AsyncStateAppendOnlyTopNHelper helper;
+
+    public AsyncStateAppendOnlyTopNFunction(
+            StateTtlConfig ttlConfig,
+            InternalTypeInfo<RowData> inputRowType,
+            GeneratedRecordComparator sortKeyGeneratedRecordComparator,
+            RowDataKeySelector sortKeySelector,
+            RankType rankType,
+            RankRange rankRange,
+            boolean generateUpdateBefore,
+            boolean outputRankNumber,
+            long cacheSize) {
+        super(
+                ttlConfig,
+                inputRowType,
+                sortKeyGeneratedRecordComparator,
+                sortKeySelector,
+                rankType,
+                rankRange,
+                generateUpdateBefore,
+                outputRankNumber);
+        this.sortKeyType = sortKeySelector.getProducedType();
+        this.inputRowSer = inputRowType.createSerializer(new SerializerConfigImpl());
+        this.cacheSize = cacheSize;
+    }
+
+    @Override
+    public void open(OpenContext openContext) throws Exception {
+        super.open(openContext);
+
+        ListTypeInfo<RowData> valueTypeInfo = new ListTypeInfo<>(inputRowType);
+        MapStateDescriptor<RowData, List<RowData>> mapStateDescriptor =

Review Comment:
   Curious about using this deprecated `MapStateDescriptor` since we're implementing a totally new operator base the the new api.



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


Re: [PR] [FLINK-36831][table] Introduce AppendOnlyTopNFunction in Rank with Async State API [flink]

Posted by "Au-Miner (via GitHub)" <gi...@apache.org>.
Au-Miner commented on PR #25723:
URL: https://github.com/apache/flink/pull/25723#issuecomment-2516533987

   Your code is really great. I really want to be responsible for the implementation of AppendOnlyFirstNFunction. Can you check your email? Thank you~


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


Re: [PR] [FLINK-36831][table] Introduce AppendOnlyTopNFunction in Rank with Async State API [flink]

Posted by "lincoln-lil (via GitHub)" <gi...@apache.org>.
lincoln-lil merged PR #25723:
URL: https://github.com/apache/flink/pull/25723


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


Re: [PR] [FLINK-36831][table] Introduce AppendOnlyTopNFunction in Rank with Async State API [flink]

Posted by "Zakelly (via GitHub)" <gi...@apache.org>.
Zakelly commented on code in PR #25723:
URL: https://github.com/apache/flink/pull/25723#discussion_r1909824008


##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/operators/rank/UpdatableTopNFunction.java:
##########
@@ -541,8 +541,10 @@ private void processElementWithoutRowNumber(RowData inputRow, Collector<RowData>
         }
     }
 
-    private void flushBufferToState(Tuple2<TopNBuffer, Map<RowData, RankRow>> bufferEntry)
+    private void flushBufferToState(

Review Comment:
   I'd suggest providing a more complete cache also tracking those keys that are already removed from the cache map but still flushing to the state, to maintain cache coherence for accessing during flushing



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


Re: [PR] [FLINK-36831][table] Introduce AppendOnlyTopNFunction in Rank with Async State API [flink]

Posted by "xuyangzhong (via GitHub)" <gi...@apache.org>.
xuyangzhong commented on code in PR #25723:
URL: https://github.com/apache/flink/pull/25723#discussion_r1914432615


##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/operators/rank/async/AsyncStateAppendOnlyTopNFunction.java:
##########
@@ -0,0 +1,199 @@
+/*
+ * 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.table.runtime.operators.rank.async;
+
+import org.apache.flink.api.common.functions.OpenContext;
+import org.apache.flink.api.common.serialization.SerializerConfigImpl;
+import org.apache.flink.api.common.state.StateTtlConfig;
+import org.apache.flink.api.common.state.v2.MapState;
+import org.apache.flink.api.common.state.v2.StateFuture;
+import org.apache.flink.api.common.typeutils.TypeSerializer;
+import org.apache.flink.api.java.typeutils.ListTypeInfo;
+import org.apache.flink.core.state.StateFutureUtils;
+import org.apache.flink.runtime.state.v2.MapStateDescriptor;
+import org.apache.flink.streaming.api.operators.StreamingRuntimeContext;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.runtime.generated.GeneratedRecordComparator;
+import org.apache.flink.table.runtime.keyselector.RowDataKeySelector;
+import org.apache.flink.table.runtime.operators.rank.AppendOnlyTopNFunction;
+import org.apache.flink.table.runtime.operators.rank.RankRange;
+import org.apache.flink.table.runtime.operators.rank.RankType;
+import org.apache.flink.table.runtime.operators.rank.TopNBuffer;
+import org.apache.flink.table.runtime.operators.rank.utils.AppendOnlyTopNHelper;
+import org.apache.flink.table.runtime.typeutils.InternalTypeInfo;
+import org.apache.flink.util.Collector;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+/**
+ * A TopN function could handle insert-only stream.
+ *
+ * <p>The input stream should only contain INSERT messages.
+ *
+ * <p>Different with {@link AppendOnlyTopNFunction}, this function is used with async state api.
+ */
+public class AsyncStateAppendOnlyTopNFunction extends AbstractAsyncStateTopNFunction {
+
+    private static final long serialVersionUID = 1L;
+
+    private final InternalTypeInfo<RowData> sortKeyType;
+    private final TypeSerializer<RowData> inputRowSer;
+    private final long cacheSize;
+
+    // a map state stores mapping from sort key to records list which is in topN
+    private transient MapState<RowData, List<RowData>> dataState;
+
+    private transient AsyncStateAppendOnlyTopNHelper helper;
+
+    public AsyncStateAppendOnlyTopNFunction(
+            StateTtlConfig ttlConfig,
+            InternalTypeInfo<RowData> inputRowType,
+            GeneratedRecordComparator sortKeyGeneratedRecordComparator,
+            RowDataKeySelector sortKeySelector,
+            RankType rankType,
+            RankRange rankRange,
+            boolean generateUpdateBefore,
+            boolean outputRankNumber,
+            long cacheSize) {
+        super(
+                ttlConfig,
+                inputRowType,
+                sortKeyGeneratedRecordComparator,
+                sortKeySelector,
+                rankType,
+                rankRange,
+                generateUpdateBefore,
+                outputRankNumber);
+        this.sortKeyType = sortKeySelector.getProducedType();
+        this.inputRowSer = inputRowType.createSerializer(new SerializerConfigImpl());
+        this.cacheSize = cacheSize;
+    }
+
+    @Override
+    public void open(OpenContext openContext) throws Exception {
+        super.open(openContext);
+
+        ListTypeInfo<RowData> valueTypeInfo = new ListTypeInfo<>(inputRowType);
+        MapStateDescriptor<RowData, List<RowData>> mapStateDescriptor =

Review Comment:
   Oh... The deprecation tag for this API was added after this pull request was submitted... Let me update them.



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


Re: [PR] [FLINK-36831][table] Introduce AppendOnlyTopNFunction in Rank with Async State API [flink]

Posted by "xuyangzhong (via GitHub)" <gi...@apache.org>.
xuyangzhong commented on code in PR #25723:
URL: https://github.com/apache/flink/pull/25723#discussion_r1914136210


##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/operators/rank/AbstractTopNFunction.java:
##########
@@ -76,9 +79,15 @@ public abstract class AbstractTopNFunction extends KeyedProcessFunction<RowData,
 
     protected KeyContext keyContext;

Review Comment:
   done



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


Re: [PR] [FLINK-36831][table] Introduce AppendOnlyTopNFunction in Rank with Async State API [flink]

Posted by "xishuaidelin (via GitHub)" <gi...@apache.org>.
xishuaidelin commented on code in PR #25723:
URL: https://github.com/apache/flink/pull/25723#discussion_r1914029626


##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/operators/rank/AbstractTopNFunction.java:
##########
@@ -76,9 +79,15 @@ public abstract class AbstractTopNFunction extends KeyedProcessFunction<RowData,
 
     protected KeyContext keyContext;

Review Comment:
   Nit: plz move this field down with non final field.



##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/operators/rank/AbstractSyncStateTopNFunction.java:
##########
@@ -72,10 +77,10 @@ public void open(OpenContext openContext) throws Exception {
      *
      * @param row input record
      * @return rank end
-     * @throws Exception

Review Comment:
   Nit: why delete this?



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


Re: [PR] [FLINK-36831][table] Introduce AppendOnlyTopNFunction in Rank with Async State API [flink]

Posted by "xuyangzhong (via GitHub)" <gi...@apache.org>.
xuyangzhong commented on code in PR #25723:
URL: https://github.com/apache/flink/pull/25723#discussion_r1914130378


##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/operators/rank/AbstractSyncStateTopNFunction.java:
##########
@@ -72,10 +77,10 @@ public void open(OpenContext openContext) throws Exception {
      *
      * @param row input record
      * @return rank end
-     * @throws Exception

Review Comment:
   reverted



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


Re: [PR] [FLINK-36831][table] Introduce AppendOnlyTopNFunction in Rank with Async State API [flink]

Posted by "flinkbot (via GitHub)" <gi...@apache.org>.
flinkbot commented on PR #25723:
URL: https://github.com/apache/flink/pull/25723#issuecomment-2513609853

   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "77455753e15710830941c62a23eb36596ec2e854",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "77455753e15710830941c62a23eb36596ec2e854",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * 77455753e15710830941c62a23eb36596ec2e854 UNKNOWN
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run azure` re-run the last Azure build
   </details>


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