You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2023/01/09 06:32:47 UTC
[GitHub] [flink] wanglijie95 opened a new pull request, #21621: [FLINK-30601][runtime] Omit "setKeyContextElement" call for non-keyed stream/operators to improve performance
wanglijie95 opened a new pull request, #21621:
URL: https://github.com/apache/flink/pull/21621
## What is the purpose of the change
Currently, flink will set the correct key context(by call [setKeyContextElement](https://github.com/apache/flink/blob/master/flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/tasks/ChainingOutput.java#:~:text=input.setKeyContextElement(castRecord)%3B)) before processing each record, which is typically used to extract key from record and pass that key to the state backends.
However, the "setKeyContextElement" is obviously not need for non-keyed stream/operator, in which case we can omit the "setKeyContextElement" calls to improve performance.
## Verifying this change
Add test `RecordProcessorUtilsTest`
## 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? (**no**)
- If yes, how is the feature documented? (**not applicable**)
--
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
[GitHub] [flink] lindong28 commented on a diff in pull request #21621: [FLINK-30601][runtime] Omit "setKeyContextElement" call for non-keyed stream/operators to improve performance
Posted by GitBox <gi...@apache.org>.
lindong28 commented on code in PR #21621:
URL: https://github.com/apache/flink/pull/21621#discussion_r1066582414
##########
flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/BootstrapStreamTask.java:
##########
@@ -82,16 +86,14 @@ protected void init() throws Exception {
mainOperator = mainOperatorAndTimeService.f0;
mainOperator.initializeState(createStreamTaskStateInitializer());
mainOperator.open();
+ recordProcessor = RecordProcessorUtils.getRecordProcessor(mainOperator);
}
@Override
protected void processInput(MailboxDefaultAction.Controller controller) throws Exception {
StreamElement element = input.take();
if (element.isRecord()) {
- StreamRecord<IN> streamRecord = element.asRecord();
-
- mainOperator.setKeyContextElement1(streamRecord);
- mainOperator.processElement(streamRecord);
+ recordProcessor.accept(element.asRecord());
Review Comment:
Sounds good. Thanks for the explanation.
--
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
[GitHub] [flink] flinkbot commented on pull request #21621: [FLINK-30601][runtime] Omit "setKeyContextElement" call for non-keyed stream/operators to improve performance
Posted by GitBox <gi...@apache.org>.
flinkbot commented on PR #21621:
URL: https://github.com/apache/flink/pull/21621#issuecomment-1375167857
<!--
Meta data
{
"version" : 1,
"metaDataEntries" : [ {
"hash" : "3e738de71ae44af8940e8e170f71a82c473d012b",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "3e738de71ae44af8940e8e170f71a82c473d012b",
"triggerType" : "PUSH"
} ]
}-->
## CI report:
* 3e738de71ae44af8940e8e170f71a82c473d012b 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
[GitHub] [flink] wanglijie95 closed pull request #21621: [FLINK-30601][runtime] Omit "setKeyContextElement" call for non-keyed stream/operators to improve performance
Posted by GitBox <gi...@apache.org>.
wanglijie95 closed pull request #21621: [FLINK-30601][runtime] Omit "setKeyContextElement" call for non-keyed stream/operators to improve performance
URL: https://github.com/apache/flink/pull/21621
--
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
[GitHub] [flink] lindong28 commented on a diff in pull request #21621: [FLINK-30601][runtime] Omit "setKeyContextElement" call for non-keyed stream/operators to improve performance
Posted by GitBox <gi...@apache.org>.
lindong28 commented on code in PR #21621:
URL: https://github.com/apache/flink/pull/21621#discussion_r1066536879
##########
flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/BootstrapStreamTask.java:
##########
@@ -82,16 +86,14 @@ protected void init() throws Exception {
mainOperator = mainOperatorAndTimeService.f0;
mainOperator.initializeState(createStreamTaskStateInitializer());
mainOperator.open();
+ recordProcessor = RecordProcessorUtils.getRecordProcessor(mainOperator);
}
@Override
protected void processInput(MailboxDefaultAction.Controller controller) throws Exception {
StreamElement element = input.take();
if (element.isRecord()) {
- StreamRecord<IN> streamRecord = element.asRecord();
-
- mainOperator.setKeyContextElement1(streamRecord);
- mainOperator.processElement(streamRecord);
+ recordProcessor.accept(element.asRecord());
Review Comment:
Do you think we need to add `hasKeyContext1()` and `hasKeyContext2()` to `StateBootstrapWrapperOperator`?
--
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
[GitHub] [flink] zhuzhurk commented on pull request #21621: [FLINK-30601][runtime] Omit "setKeyContextElement" call for non-keyed stream/operators to improve performance
Posted by GitBox <gi...@apache.org>.
zhuzhurk commented on PR #21621:
URL: https://github.com/apache/flink/pull/21621#issuecomment-1379880157
Sorry that we missed something, that it changes the `@PublicEvolving` `AbstractStreamOperator` to implement an `@Internal` `KeyContextHandler`. This is not correct.
I had an offline discussion with @wanglijie95, a potential problem is that when a user implemented an operator which overrides `AbstractStreamOperator#setKeyContextElement` and maintains different key selectors on its own, the change would result in incompatibility. This should rarely happen, but is possible because Flink offers this ability via public interface.
Therefore, we decide to open a FLIP to the community and have a discussion on it, to avoid making the change silently.
--
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
[GitHub] [flink] lsyldliu commented on pull request #21621: [FLINK-30601][runtime] Omit "setKeyContextElement" call for non-keyed stream/operators to improve performance
Posted by GitBox <gi...@apache.org>.
lsyldliu commented on PR #21621:
URL: https://github.com/apache/flink/pull/21621#issuecomment-1378997835
Thanks for the PR. LGTM
--
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
[GitHub] [flink] zhuzhurk commented on a diff in pull request #21621: [FLINK-30601][runtime] Omit "setKeyContextElement" call for non-keyed stream/operators to improve performance
Posted by GitBox <gi...@apache.org>.
zhuzhurk commented on code in PR #21621:
URL: https://github.com/apache/flink/pull/21621#discussion_r1066555577
##########
flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/output/BootstrapStreamTask.java:
##########
@@ -82,16 +86,14 @@ protected void init() throws Exception {
mainOperator = mainOperatorAndTimeService.f0;
mainOperator.initializeState(createStreamTaskStateInitializer());
mainOperator.open();
+ recordProcessor = RecordProcessorUtils.getRecordProcessor(mainOperator);
}
@Override
protected void processInput(MailboxDefaultAction.Controller controller) throws Exception {
StreamElement element = input.take();
if (element.isRecord()) {
- StreamRecord<IN> streamRecord = element.asRecord();
-
- mainOperator.setKeyContextElement1(streamRecord);
- mainOperator.processElement(streamRecord);
+ recordProcessor.accept(element.asRecord());
Review Comment:
I think it's not needed because all invocations to operators are intercepted. Operators' responsibility is to provide correct information of `hasKeyContext`.
--
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
[GitHub] [flink] lindong28 commented on pull request #21621: [FLINK-30601][runtime] Omit "setKeyContextElement" call for non-keyed stream/operators to improve performance
Posted by GitBox <gi...@apache.org>.
lindong28 commented on PR #21621:
URL: https://github.com/apache/flink/pull/21621#issuecomment-1378140166
Thanks for the PR! LGTM.
--
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
[GitHub] [flink] wanglijie95 commented on a diff in pull request #21621: [FLINK-30601][runtime] Omit "setKeyContextElement" call for non-keyed stream/operators to improve performance
Posted by GitBox <gi...@apache.org>.
wanglijie95 commented on code in PR #21621:
URL: https://github.com/apache/flink/pull/21621#discussion_r1070990867
##########
flink-streaming-java/src/test/java/org/apache/flink/streaming/runtime/io/RecordProcessorUtilsTest.java:
##########
@@ -95,6 +100,163 @@ void testGetRecordProcessor2() throws Exception {
assertThat(operator3.processElement2Called).isTrue();
}
+ @Test
+ void testOverrideSetKeyContextElementForOneInputStreamOperator() throws Exception {
+ // test no override
+ NoOverrideOneInputStreamOperator noOverride = new NoOverrideOneInputStreamOperator();
+ RecordProcessorUtils.getRecordProcessor(noOverride).accept(new StreamRecord<>("test"));
+ assertThat(noOverride.setCurrentKeyCalled).isFalse();
+
+ // test override "SetKeyContextElement"
+ OverrideSetKeyContextOneInputStreamOperator overrideSetKeyContext =
+ new OverrideSetKeyContextOneInputStreamOperator();
+ RecordProcessorUtils.getRecordProcessor(overrideSetKeyContext)
+ .accept(new StreamRecord<>("test"));
+ assertThat(overrideSetKeyContext.setKeyContextElementCalled).isTrue();
+
+ // test override "SetKeyContextElement1"
+ OverrideSetKeyContext1OneInputStreamOperator overrideSetKeyContext1 =
+ new OverrideSetKeyContext1OneInputStreamOperator();
+ RecordProcessorUtils.getRecordProcessor(overrideSetKeyContext1)
+ .accept(new StreamRecord<>("test"));
+ assertThat(overrideSetKeyContext1.setKeyContextElement1Called).isTrue();
+ }
+
+ @Test
+ void testOverrideSetKeyContextElementForTwoInputStreamOperator() throws Exception {
+ // test no override
+ NoOverrideTwoInputStreamOperator noOverride = new NoOverrideTwoInputStreamOperator();
+ RecordProcessorUtils.getRecordProcessor1(noOverride).accept(new StreamRecord<>("test"));
+ RecordProcessorUtils.getRecordProcessor2(noOverride).accept(new StreamRecord<>("test"));
+ assertThat(noOverride.setCurrentKeyCalled).isFalse();
+
+ // test override "SetKeyContextElement1" and "SetKeyContextElement2"
+ OverrideSetKeyContext1AndSetKeyContext2TwoInputStreamOperator override =
+ new OverrideSetKeyContext1AndSetKeyContext2TwoInputStreamOperator();
+ RecordProcessorUtils.getRecordProcessor1(override).accept(new StreamRecord<>("test"));
+ RecordProcessorUtils.getRecordProcessor2(override).accept(new StreamRecord<>("test"));
+ assertThat(override.setKeyContextElement1Called).isTrue();
+ assertThat(override.setKeyContextElement2Called).isTrue();
+ }
+
+ private static class NoOverrideOperator extends AbstractStreamOperator<String> {
+
+ boolean setCurrentKeyCalled = false;
+
+ NoOverrideOperator() throws NoSuchFieldException, IllegalAccessException {
+ super();
+ // For case that "SetKeyContextElement" has not been overridden,
+ // we can determine whether the "SetKeyContextElement" is called through
+ // "setCurrentKey". According to the implementation, we need to make the
+ // "stateKeySelector1/stateKeySelector2" not null. Besides, we override the
+ // "hasKeyContext1" and "hasKeyContext2" to avoid "stateKeySelector1/stateKeySelector2"
+ // from affecting the return value
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
[GitHub] [flink] zhuzhurk commented on a diff in pull request #21621: [FLINK-30601][runtime] Omit "setKeyContextElement" call for non-keyed stream/operators to improve performance
Posted by GitBox <gi...@apache.org>.
zhuzhurk commented on code in PR #21621:
URL: https://github.com/apache/flink/pull/21621#discussion_r1070900001
##########
flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/io/RecordProcessorUtils.java:
##########
@@ -86,6 +118,71 @@ public static <T> ThrowingConsumer<StreamRecord<T>, Exception> getRecordProcesso
}
}
+ private static boolean canOmitSetKeyContext(
+ AbstractStreamOperator<?> streamOperator, int input) {
+ // Since AbstractStreamOperator is @PublicEvolving, we need to check whether the
+ // "SetKeyContextElement" is overridden by the (user-implemented) subclass. If it is
+ // overridden, we cannot omit it due to the subclass may maintain different key selectors on
+ // its own.
+ return !hasKeyContext(streamOperator, input)
+ && !methodSetKeyContextIsOverride(streamOperator, input);
+ }
+
+ private static boolean hasKeyContext(AbstractStreamOperator<?> operator, int input) {
+ if (input == 0) {
+ return operator.hasKeyContext1();
+ } else {
+ return operator.hasKeyContext2();
+ }
+ }
+
+ private static boolean methodSetKeyContextIsOverride(
+ AbstractStreamOperator<?> operator, int input) {
+ if (input == 0) {
+ if (operator instanceof OneInputStreamOperator) {
+ return methodIsOverride(
+ operator,
+ OneInputStreamOperator.class,
+ METHOD_SET_KEY_CONTEXT_ELEMENT,
+ StreamRecord.class)
+ || methodIsOverride(
+ operator,
+ AbstractStreamOperator.class,
+ METHOD_SET_KEY_CONTEXT_ELEMENT1,
+ StreamRecord.class);
+ } else {
+ return methodIsOverride(
+ operator,
+ AbstractStreamOperator.class,
+ METHOD_SET_KEY_CONTEXT_ELEMENT1,
+ StreamRecord.class);
+ }
+ } else {
+ return methodIsOverride(
+ operator,
+ AbstractStreamOperator.class,
+ METHOD_SET_KEY_CONTEXT_ELEMENT2,
+ StreamRecord.class);
+ }
+ }
+
+ private static boolean methodIsOverride(
Review Comment:
methodIsOverride -> methodIsOverridden
##########
flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/io/RecordProcessorUtils.java:
##########
@@ -86,6 +118,71 @@ public static <T> ThrowingConsumer<StreamRecord<T>, Exception> getRecordProcesso
}
}
+ private static boolean canOmitSetKeyContext(
+ AbstractStreamOperator<?> streamOperator, int input) {
+ // Since AbstractStreamOperator is @PublicEvolving, we need to check whether the
+ // "SetKeyContextElement" is overridden by the (user-implemented) subclass. If it is
+ // overridden, we cannot omit it due to the subclass may maintain different key selectors on
+ // its own.
+ return !hasKeyContext(streamOperator, input)
+ && !methodSetKeyContextIsOverride(streamOperator, input);
+ }
+
+ private static boolean hasKeyContext(AbstractStreamOperator<?> operator, int input) {
+ if (input == 0) {
+ return operator.hasKeyContext1();
+ } else {
+ return operator.hasKeyContext2();
+ }
+ }
+
+ private static boolean methodSetKeyContextIsOverride(
Review Comment:
methodSetKeyContextIsOverride -> methodSetKeyContextElementIsOverridden
##########
flink-streaming-java/src/test/java/org/apache/flink/streaming/runtime/io/RecordProcessorUtilsTest.java:
##########
@@ -95,6 +100,163 @@ void testGetRecordProcessor2() throws Exception {
assertThat(operator3.processElement2Called).isTrue();
}
+ @Test
+ void testOverrideSetKeyContextElementForOneInputStreamOperator() throws Exception {
+ // test no override
+ NoOverrideOneInputStreamOperator noOverride = new NoOverrideOneInputStreamOperator();
+ RecordProcessorUtils.getRecordProcessor(noOverride).accept(new StreamRecord<>("test"));
+ assertThat(noOverride.setCurrentKeyCalled).isFalse();
+
+ // test override "SetKeyContextElement"
+ OverrideSetKeyContextOneInputStreamOperator overrideSetKeyContext =
+ new OverrideSetKeyContextOneInputStreamOperator();
+ RecordProcessorUtils.getRecordProcessor(overrideSetKeyContext)
+ .accept(new StreamRecord<>("test"));
+ assertThat(overrideSetKeyContext.setKeyContextElementCalled).isTrue();
+
+ // test override "SetKeyContextElement1"
+ OverrideSetKeyContext1OneInputStreamOperator overrideSetKeyContext1 =
+ new OverrideSetKeyContext1OneInputStreamOperator();
+ RecordProcessorUtils.getRecordProcessor(overrideSetKeyContext1)
+ .accept(new StreamRecord<>("test"));
+ assertThat(overrideSetKeyContext1.setKeyContextElement1Called).isTrue();
+ }
+
+ @Test
+ void testOverrideSetKeyContextElementForTwoInputStreamOperator() throws Exception {
+ // test no override
+ NoOverrideTwoInputStreamOperator noOverride = new NoOverrideTwoInputStreamOperator();
+ RecordProcessorUtils.getRecordProcessor1(noOverride).accept(new StreamRecord<>("test"));
+ RecordProcessorUtils.getRecordProcessor2(noOverride).accept(new StreamRecord<>("test"));
+ assertThat(noOverride.setCurrentKeyCalled).isFalse();
+
+ // test override "SetKeyContextElement1" and "SetKeyContextElement2"
+ OverrideSetKeyContext1AndSetKeyContext2TwoInputStreamOperator override =
+ new OverrideSetKeyContext1AndSetKeyContext2TwoInputStreamOperator();
+ RecordProcessorUtils.getRecordProcessor1(override).accept(new StreamRecord<>("test"));
+ RecordProcessorUtils.getRecordProcessor2(override).accept(new StreamRecord<>("test"));
+ assertThat(override.setKeyContextElement1Called).isTrue();
+ assertThat(override.setKeyContextElement2Called).isTrue();
+ }
+
+ private static class NoOverrideOperator extends AbstractStreamOperator<String> {
+
+ boolean setCurrentKeyCalled = false;
+
+ NoOverrideOperator() throws NoSuchFieldException, IllegalAccessException {
+ super();
+ // For case that "SetKeyContextElement" has not been overridden,
+ // we can determine whether the "SetKeyContextElement" is called through
+ // "setCurrentKey". According to the implementation, we need to make the
+ // "stateKeySelector1/stateKeySelector2" not null. Besides, we override the
+ // "hasKeyContext1" and "hasKeyContext2" to avoid "stateKeySelector1/stateKeySelector2"
+ // from affecting the return value
Review Comment:
Is is possible to invoke `setup(...)` to initialize the key selectors in a valid way?
--
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