You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by wenlong88 <gi...@git.apache.org> on 2017/03/22 13:18:23 UTC
[GitHub] flink pull request #3595: [FLINK-6162]Fix bug in ByteArrayOutputStreamWithPo...
GitHub user wenlong88 opened a pull request:
https://github.com/apache/flink/pull/3595
[FLINK-6162]Fix bug in ByteArrayOutputStreamWithPos#setPosition
Currently the precondition check in setPosition will fail when the buffer is full:
{{Preconditions.checkArgument(position < getEndPosition(), "Position out of bounds.");{{
We should allow the expected position to be the end position
I have added a test on the fix.
You can merge this pull request into a Git repository by running:
$ git pull https://github.com/wenlong88/flink bugfix-1
Alternatively you can review and apply these changes as the patch at:
https://github.com/apache/flink/pull/3595.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 #3595
----
commit 905e513612142a934bde78e54bf8a311c25a3107
Author: wenlong.lwl <we...@alibaba-inc.com>
Date: 2017-03-22T01:48:42Z
Fix bug in ByteArrayOutputStreamWithPos
commit 09cc96adf52f78052bb3555f2ccda0d860da4895
Author: wenlong.lwl <we...@alibaba-inc.com>
Date: 2017-03-22T06:30:59Z
fix typo
----
---
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 #3595: [FLINK-6162]Fix bug in ByteArrayOutputStreamWithPo...
Posted by greghogan <gi...@git.apache.org>.
Github user greghogan commented on a diff in the pull request:
https://github.com/apache/flink/pull/3595#discussion_r107423467
--- Diff: flink-core/src/test/java/org/apache/flink/core/memory/ByteArrayOutputStreamWithPosTest.java ---
@@ -0,0 +1,31 @@
+/*
+ * 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.core.memory;
+
+import org.junit.Test;
+
+public class ByteArrayOutputStreamWithPosTest {
+ @Test
+ public void setPositionWhenBufferIsFull() throws Exception {
+ ByteArrayOutputStreamWithPos stream = new ByteArrayOutputStreamWithPos();
--- End diff --
Initialize with a constant size matching the next line.
---
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 #3595: [FLINK-6162]Fix bug in ByteArrayOutputStreamWithPos#setPo...
Posted by wenlong88 <gi...@git.apache.org>.
Github user wenlong88 commented on the issue:
https://github.com/apache/flink/pull/3595
hi, @StefanRRichter , I have add check in setPosition in ByteArrayInputSteamWithPos, there is no dependency on this method too, so I rename the method from setPos to setPosition for unifying.
---
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 #3595: [FLINK-6162]Fix bug in ByteArrayOutputStreamWithPos#setPo...
Posted by wenlong88 <gi...@git.apache.org>.
Github user wenlong88 commented on the issue:
https://github.com/apache/flink/pull/3595
hi, @greghogan , do you have any new comment about the changes?
---
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 #3595: [FLINK-6162]Fix bug in ByteArrayOutputStreamWithPo...
Posted by greghogan <gi...@git.apache.org>.
Github user greghogan commented on a diff in the pull request:
https://github.com/apache/flink/pull/3595#discussion_r107697744
--- Diff: flink-core/src/test/java/org/apache/flink/core/memory/ByteArrayOutputStreamWithPosTest.java ---
@@ -0,0 +1,31 @@
+/*
+ * 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.core.memory;
+
+import org.junit.Test;
+
+public class ByteArrayOutputStreamWithPosTest {
+ @Test
+ public void setPositionWhenBufferIsFull() throws Exception {
+ ByteArrayOutputStreamWithPos stream = new ByteArrayOutputStreamWithPos(32);
+ stream.write(new byte[32]);
+ stream.setPosition(stream.getPosition());
--- End diff --
Assert that `getPosition` is equal to the expected value. Can validate that `setPosition` now works per the change above and does not change the position, and also check that `toString` can be executed.
---
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 #3595: [FLINK-6162]Fix bug in ByteArrayOutputStreamWithPos#setPo...
Posted by wenlong88 <gi...@git.apache.org>.
Github user wenlong88 commented on the issue:
https://github.com/apache/flink/pull/3595
@greghogan I come across this bug when we are trying to write some objects to a byte array: at first, I wrote a 0 as initial size and then the serialized object, after that, I seek back to the start position to correct the size and then seek to the end of the written data to continue to write other object. When the array can auto expanding, I can just reserve some hole at first and then seek back and fill the hole.
---
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 #3595: [FLINK-6162]Fix bug in ByteArrayOutputStreamWithPos#setPo...
Posted by greghogan <gi...@git.apache.org>.
Github user greghogan commented on the issue:
https://github.com/apache/flink/pull/3595
@wenlong88 thanks for the contribution (and the ping)! Merging ...
---
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 #3595: [FLINK-6162]Fix bug in ByteArrayOutputStreamWithPos#setPo...
Posted by wenlong88 <gi...@git.apache.org>.
Github user wenlong88 commented on the issue:
https://github.com/apache/flink/pull/3595
@greghogan Currently we check the position with the EndPosition which is the size of buffer instead of a max written size , so it is still possible to leave holes when keeping unmodified. Enabling auto extending can give the user the flexibility to reserved some space for later filling and does not change current functions logics.
---
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 #3595: [FLINK-6162]Fix bug in ByteArrayOutputStreamWithPo...
Posted by greghogan <gi...@git.apache.org>.
Github user greghogan commented on a diff in the pull request:
https://github.com/apache/flink/pull/3595#discussion_r108437565
--- Diff: flink-core/src/main/java/org/apache/flink/core/memory/ByteArrayInputStreamWithPos.java ---
@@ -118,7 +118,8 @@ public int getPosition() {
return position;
}
- public void setPos(int pos) {
+ public void setPosition(int pos) {
+ Preconditions.checkArgument(pos < count, "Position out of bounds.");
--- End diff --
Allow for equality as with `ByteArrayOutputStreamWithPos`? And check for non-negative. Should we create a matching `getEndPosition()` as in `ByteArrayOutputStreamWithPos`?
---
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 #3595: [FLINK-6162]Fix bug in ByteArrayOutputStreamWithPo...
Posted by greghogan <gi...@git.apache.org>.
Github user greghogan commented on a diff in the pull request:
https://github.com/apache/flink/pull/3595#discussion_r108438856
--- Diff: flink-core/src/test/java/org/apache/flink/core/memory/ByteArrayInputStreamWithPosTest.java ---
@@ -0,0 +1,50 @@
+/*
+ * 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.core.memory;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class ByteArrayInputStreamWithPosTest {
+
+ /**
+ * This tests setting position on a {@link ByteArrayInputStreamWithPos}
+ */
+ @Test
+ public void testSetPosition() throws Exception {
+ byte[] data = new byte[] {'0','1','2','3','4','5','6','7','8','9'};
+ ByteArrayInputStreamWithPos inputStreamWithPos = new ByteArrayInputStreamWithPos(data);
+ inputStreamWithPos.setPosition(1);
+ Assert.assertEquals('1', inputStreamWithPos.read());
+ inputStreamWithPos.setPosition(3);
+ Assert.assertEquals('3', inputStreamWithPos.read());
+ }
+
+ /**
+ * This tests that the expected position exceeds the capacity of the byte array.
+ */
+ @Test(expected = IllegalArgumentException.class)
--- End diff --
I think the preference is to instead use `ExpectedException` which can also take an expected message.
---
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 #3595: [FLINK-6162]Fix bug in ByteArrayOutputStreamWithPos#setPo...
Posted by wenlong88 <gi...@git.apache.org>.
Github user wenlong88 commented on the issue:
https://github.com/apache/flink/pull/3595
@greghogan comments addressed
---
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 #3595: [FLINK-6162]Fix bug in ByteArrayOutputStreamWithPos#setPo...
Posted by wenlong88 <gi...@git.apache.org>.
Github user wenlong88 commented on the issue:
https://github.com/apache/flink/pull/3595
@greghogan thanks for the review, I have changed the change test to use explicit buffer size.
you are right that currently there is no dependency on this method. We used the ByteArrayOutputStreamWithPOs in our own scenario and caught the bug.
---
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 #3595: [FLINK-6162]Fix bug in ByteArrayOutputStreamWithPo...
Posted by greghogan <gi...@git.apache.org>.
Github user greghogan commented on a diff in the pull request:
https://github.com/apache/flink/pull/3595#discussion_r108437640
--- Diff: flink-core/src/main/java/org/apache/flink/core/memory/ByteArrayOutputStreamWithPos.java ---
@@ -110,7 +110,7 @@ public int getPosition() {
}
public void setPosition(int position) {
- Preconditions.checkArgument(position < getEndPosition(), "Position out of bounds.");
+ Preconditions.checkArgument(position <= getEndPosition(), "Position out of bounds.");
--- End diff --
Check for non-negative.
---
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 #3595: [FLINK-6162]Fix bug in ByteArrayOutputStreamWithPo...
Posted by greghogan <gi...@git.apache.org>.
Github user greghogan commented on a diff in the pull request:
https://github.com/apache/flink/pull/3595#discussion_r107696959
--- Diff: flink-core/src/test/java/org/apache/flink/core/memory/ByteArrayOutputStreamWithPosTest.java ---
@@ -0,0 +1,31 @@
+/*
+ * 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.core.memory;
+
+import org.junit.Test;
+
+public class ByteArrayOutputStreamWithPosTest {
+ @Test
+ public void setPositionWhenBufferIsFull() throws Exception {
+ ByteArrayOutputStreamWithPos stream = new ByteArrayOutputStreamWithPos(32);
--- End diff --
I think it is stronger to replace `32` on these lines with a descriptive local variable. After initialization could add an assertion that `getBuf().length` equals that value.
---
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 #3595: [FLINK-6162]Fix bug in ByteArrayOutputStreamWithPos#setPo...
Posted by wenlong88 <gi...@git.apache.org>.
Github user wenlong88 commented on the issue:
https://github.com/apache/flink/pull/3595
@greghogan Thanks for the review. I have add non-negative check and update the test with ExpectedException. But I don't think we need to call toString in the test since we have check the position instead I add a test to verify that toString returns the substring with range(0, position) to meet your concern. One update about setPosition in outputStream, I think it is better to auto extend the buffer when setting a position larger than the buffer size, just like the auto extending in write.
---
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 #3595: [FLINK-6162]Fix bug in ByteArrayOutputStreamWithPos#setPo...
Posted by greghogan <gi...@git.apache.org>.
Github user greghogan commented on the issue:
https://github.com/apache/flink/pull/3595
@wenlong88 you are right that the position can currently be moved beyond written data. This still feels a bit like feature creep as we've added to the PR. Do you need to expand the array is this manner? How did you run across this bug? I'm not disagreeing with the change but would like to be conservative and understand the why.
---
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 #3595: [FLINK-6162]Fix bug in ByteArrayOutputStreamWithPos#setPo...
Posted by StefanRRichter <gi...@git.apache.org>.
Github user StefanRRichter commented on the issue:
https://github.com/apache/flink/pull/3595
I would suggest to introduce the analogue check also in `ByteArrayInputStreamWithPos`.
---
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 #3595: [FLINK-6162]Fix bug in ByteArrayOutputStreamWithPo...
Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:
https://github.com/apache/flink/pull/3595
---
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 #3595: [FLINK-6162]Fix bug in ByteArrayOutputStreamWithPos#setPo...
Posted by greghogan <gi...@git.apache.org>.
Github user greghogan commented on the issue:
https://github.com/apache/flink/pull/3595
When expanding with `write` the new capacity (up to `position` of course) is filled with data. Expanding with `setPosition` can leave holes in the data and may mask a bug. Since these classes are undocumented it would be safer to leave this behavior unmodified.
---
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.
---