You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by StefanRRichter <gi...@git.apache.org> on 2016/12/14 11:17:15 UTC
[GitHub] flink pull request #3005: [FLINK-5335] Allow ListCheckpointed user functions...
GitHub user StefanRRichter opened a pull request:
https://github.com/apache/flink/pull/3005
[FLINK-5335] Allow ListCheckpointed user functions to return null
Currently, it is not allowed to return ``null`` as result for the methods in ``ListCheckpointed``. From a usability perspective, I think it is nicer to allow this. This PR introduces proper handling for ``null`` plus additional unit tests for ``ListCheckpointed``
You can merge this pull request into a Git repository by running:
$ git pull https://github.com/StefanRRichter/flink allow-list-state-null
Alternatively you can review and apply these changes as the patch at:
https://github.com/apache/flink/pull/3005.patch
To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:
This closes #3005
----
commit 6d11885d5ddc4ba53b248b7683f24ee87e1360f7
Author: Stefan Richter <s....@data-artisans.com>
Date: 2016-12-14T11:13:15Z
[FLINK-5335] Allow ListCheckpointed user functions to return null
----
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request #3005: [FLINK-5335] Allow ListCheckpointed user functions...
Posted by StefanRRichter <gi...@git.apache.org>.
Github user StefanRRichter commented on a diff in the pull request:
https://github.com/apache/flink/pull/3005#discussion_r92402545
--- Diff: flink-streaming-java/src/main/java/org/apache/flink/streaming/api/checkpoint/ListCheckpointed.java ---
@@ -41,6 +41,7 @@
* @param checkpointId The ID of the checkpoint.
* @param timestamp Timestamp of the checkpoint.
* @return The operator state in a list of redistributable, atomic sub-states.
+ * Should not return null, but empty list instead.
--- End diff --
That is why I wrote "should". It is possible, but not encouraged because i) `null` is evil ;-) and ii) user will also receive empty list on restore.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request #3005: [FLINK-5335] Allow ListCheckpointed user functions...
Posted by aljoscha <gi...@git.apache.org>.
Github user aljoscha commented on a diff in the pull request:
https://github.com/apache/flink/pull/3005#discussion_r92403016
--- Diff: flink-streaming-java/src/test/java/org/apache/flink/streaming/api/checkpoint/ListCheckpointedTest.java ---
@@ -0,0 +1,120 @@
+/*
+ * 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.streaming.api.checkpoint;
+
+import org.apache.flink.api.common.functions.AbstractRichFunction;
+import org.apache.flink.core.fs.CloseableRegistry;
+import org.apache.flink.streaming.api.operators.AbstractUdfStreamOperator;
+import org.apache.flink.streaming.runtime.tasks.OperatorStateHandles;
+import org.apache.flink.streaming.runtime.tasks.StreamTask;
+import org.apache.flink.streaming.util.AbstractStreamOperatorTestHarness;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class ListCheckpointedTest {
+
+ @Test
+ public void testUDFReturningNull() throws Exception {
+ TestUserFunction userFunction = new TestUserFunction(null);
+ AbstractStreamOperatorTestHarness<Integer> testHarness =
+ new AbstractStreamOperatorTestHarness<>(new TestOperator(userFunction), 1, 1, 0);
+ testHarness.open();
+ OperatorStateHandles snapshot = testHarness.snapshot(0L, 0L);
+ testHarness.initializeState(snapshot);
+ Assert.assertTrue(userFunction.isRestored());
+ }
+
+ @Test
+ public void testUDFReturningEmpty() throws Exception {
+ TestUserFunction userFunction = new TestUserFunction(Collections.<Integer>emptyList());
+ AbstractStreamOperatorTestHarness<Integer> testHarness =
+ new AbstractStreamOperatorTestHarness<>(new TestOperator(userFunction), 1, 1, 0);
+ testHarness.open();
+ OperatorStateHandles snapshot = testHarness.snapshot(0L, 0L);
+ testHarness.initializeState(snapshot);
+ Assert.assertTrue(userFunction.isRestored());
+ }
+
+ @Test
+ public void testUDFReturningData() throws Exception {
+ TestUserFunction userFunction = new TestUserFunction(Arrays.asList(1, 2, 3));
+ AbstractStreamOperatorTestHarness<Integer> testHarness =
+ new AbstractStreamOperatorTestHarness<>(new TestOperator(userFunction), 1, 1, 0);
+ testHarness.open();
+ OperatorStateHandles snapshot = testHarness.snapshot(0L, 0L);
+ testHarness.initializeState(snapshot);
+ Assert.assertTrue(userFunction.isRestored());
+ }
+
+
+ private static class TestUserFunction extends AbstractRichFunction implements ListCheckpointed<Integer> {
+
+ private final List<Integer> expected;
+ private boolean restored;
+
+ public TestUserFunction(List<Integer> expected) {
+ this.expected = expected;
+ this.restored = false;
+ }
+
+ @Override
+ public List<Integer> snapshotState(long checkpointId, long timestamp) throws Exception {
+ return expected;
+ }
+
+ @Override
+ public void restoreState(List<Integer> state) throws Exception {
+ if (null != expected) {
+ Assert.assertEquals(expected, state);
+ } else {
+ Assert.assertTrue(state.isEmpty());
+ }
+ restored = true;
+ }
+
+ public boolean isRestored() {
+ return restored;
+ }
+ }
+
+ private static class TestOperator extends AbstractUdfStreamOperator<Integer, TestUserFunction> {
--- End diff --
I think you can make the test UDF a `RichMapFunction` and then you don't need the `TestOperator`.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request #3005: [FLINK-5335] Allow ListCheckpointed user functions...
Posted by aljoscha <gi...@git.apache.org>.
Github user aljoscha commented on a diff in the pull request:
https://github.com/apache/flink/pull/3005#discussion_r92403232
--- Diff: flink-streaming-java/src/main/java/org/apache/flink/streaming/api/checkpoint/ListCheckpointed.java ---
@@ -41,6 +41,7 @@
* @param checkpointId The ID of the checkpoint.
* @param timestamp Timestamp of the checkpoint.
* @return The operator state in a list of redistributable, atomic sub-states.
+ * Should not return null, but empty list instead.
--- End diff --
kk \U0001f603
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request #3005: [FLINK-5335] Allow ListCheckpointed user functions...
Posted by StefanRRichter <gi...@git.apache.org>.
Github user StefanRRichter commented on a diff in the pull request:
https://github.com/apache/flink/pull/3005#discussion_r92402942
--- Diff: flink-streaming-java/src/main/java/org/apache/flink/streaming/api/checkpoint/ListCheckpointed.java ---
@@ -41,6 +41,7 @@
* @param checkpointId The ID of the checkpoint.
* @param timestamp Timestamp of the checkpoint.
* @return The operator state in a list of redistributable, atomic sub-states.
+ * Should not return null, but empty list instead.
--- End diff --
Oh, plus `null` is semantically indifferent from empy list in this case. So for the sake of consistency, I give this advice in the doc.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request #3005: [FLINK-5335] Allow ListCheckpointed user functions...
Posted by aljoscha <gi...@git.apache.org>.
Github user aljoscha commented on a diff in the pull request:
https://github.com/apache/flink/pull/3005#discussion_r92402081
--- Diff: flink-streaming-java/src/main/java/org/apache/flink/streaming/api/checkpoint/ListCheckpointed.java ---
@@ -41,6 +41,7 @@
* @param checkpointId The ID of the checkpoint.
* @param timestamp Timestamp of the checkpoint.
* @return The operator state in a list of redistributable, atomic sub-states.
+ * Should not return null, but empty list instead.
--- End diff --
Isn't the point of this to allow returning `null`? \U0001f609
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink issue #3005: [FLINK-5335] Allow ListCheckpointed user functions to ret...
Posted by aljoscha <gi...@git.apache.org>.
Github user aljoscha commented on the issue:
https://github.com/apache/flink/pull/3005
Thanks for your work! \U0001f603
I merged. Could you please close this PR and the Jira issue?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request #3005: [FLINK-5335] Allow ListCheckpointed user functions...
Posted by StefanRRichter <gi...@git.apache.org>.
Github user StefanRRichter commented on a diff in the pull request:
https://github.com/apache/flink/pull/3005#discussion_r92403863
--- Diff: flink-streaming-java/src/test/java/org/apache/flink/streaming/util/AbstractStreamOperatorTestHarness.java ---
@@ -143,6 +144,15 @@ public AbstractStreamOperatorTestHarness(
when(mockTask.getExecutionConfig()).thenReturn(executionConfig);
when(mockTask.getUserCodeClassLoader()).thenReturn(this.getClass().getClassLoader());
when(mockTask.getCancelables()).thenReturn(this.closableRegistry);
+ when(mockTask.createOperatorStateBackend(any(StreamOperator.class), any(Collection.class))).
--- End diff --
Oh yes, that is true :D
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request #3005: [FLINK-5335] Allow ListCheckpointed user functions...
Posted by StefanRRichter <gi...@git.apache.org>.
Github user StefanRRichter closed the pull request at:
https://github.com/apache/flink/pull/3005
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink issue #3005: [FLINK-5335] Allow ListCheckpointed user functions to ret...
Posted by StefanRRichter <gi...@git.apache.org>.
Github user StefanRRichter commented on the issue:
https://github.com/apache/flink/pull/3005
CC @aljoscha
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---