You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by GitBox <gi...@apache.org> on 2022/12/21 06:07:59 UTC
[GitHub] [kafka] michaeljmarshall opened a new pull request, #13032: KAFKA-14540: Fix DataOutputStreamWritable#writeByteBuffer
michaeljmarshall opened a new pull request, #13032:
URL: https://github.com/apache/kafka/pull/13032
When writing a `ByteBuffer` backed by a `HeapBuffer` to a `DataOutputStream`, it is necessary to pass in the offset, not the position. The current code results in writing the wrong data to `DataOutputStream`. While the `DataOutputStreamWritable` is used in the project, I do not see any references that would utilize this code path, so this bug fix is relatively minor.
I added a new test to cover the exact bug. The test fails without this change.
Additionally, it's relevant to note that there are other locations in the code base with the correct usage of `arrayOffset()`. I don't see any similar bugs based on some quick searches through the code base.
### Committer Checklist (excluded from commit message)
- [ ] Verify design and implementation
- [ ] Verify test coverage and CI build status
- [ ] Verify documentation (including upgrade notes)
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [kafka] ijuma commented on a diff in pull request #13032: KAFKA-14540: Fix DataOutputStreamWritable#writeByteBuffer
Posted by GitBox <gi...@apache.org>.
ijuma commented on code in PR #13032:
URL: https://github.com/apache/kafka/pull/13032#discussion_r1054369362
##########
clients/src/main/java/org/apache/kafka/common/protocol/DataOutputStreamWritable.java:
##########
@@ -99,7 +99,7 @@ public void writeUnsignedVarint(int i) {
public void writeByteBuffer(ByteBuffer buf) {
try {
if (buf.hasArray()) {
- out.write(buf.array(), buf.position(), buf.limit());
+ out.write(buf.array(), buf.arrayOffset(), buf.limit());
Review Comment:
Nice catch. Can we extend the tests to cover both of the cases discussed here too?
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [kafka] lhotari commented on a diff in pull request #13032: KAFKA-14540: Fix DataOutputStreamWritable#writeByteBuffer
Posted by GitBox <gi...@apache.org>.
lhotari commented on code in PR #13032:
URL: https://github.com/apache/kafka/pull/13032#discussion_r1054114528
##########
clients/src/main/java/org/apache/kafka/common/protocol/DataOutputStreamWritable.java:
##########
@@ -99,7 +99,7 @@ public void writeUnsignedVarint(int i) {
public void writeByteBuffer(ByteBuffer buf) {
try {
if (buf.hasArray()) {
- out.write(buf.array(), buf.position(), buf.limit());
+ out.write(buf.array(), buf.arrayOffset(), buf.limit());
Review Comment:
Yes, it looks like it should be `buf.position() + buf.arrayOffset()`. In addition, `buf.limit()` should be replaced with `buf.remaining()`.
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [kafka] viktorsomogyi commented on a diff in pull request #13032: KAFKA-14540: Fix DataOutputStreamWritable#writeByteBuffer
Posted by GitBox <gi...@apache.org>.
viktorsomogyi commented on code in PR #13032:
URL: https://github.com/apache/kafka/pull/13032#discussion_r1060471853
##########
clients/src/test/java/org/apache/kafka/common/protocol/DataOutputStreamWritableTest.java:
##########
@@ -0,0 +1,66 @@
+/*
+ * 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.kafka.common.protocol;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.io.DataOutputStream;
+import java.nio.ByteBuffer;
+import org.apache.kafka.common.utils.ByteBufferOutputStream;
+import org.junit.jupiter.api.Test;
+
+public class DataOutputStreamWritableTest {
+ @Test
+ public void testWritingSlicedByteBuffer() {
+ byte[] expectedArray = new byte[]{2, 3, 0, 0};
+ ByteBuffer sourceBuffer = ByteBuffer.wrap(new byte[]{0, 1, 2, 3});
+ ByteBuffer resultBuffer = ByteBuffer.allocate(4);
+
+ // Move position forward to ensure slice is not whole buffer
+ sourceBuffer.position(2);
+ ByteBuffer slicedBuffer = sourceBuffer.slice();
+
+ Writable writable = new DataOutputStreamWritable(
+ new DataOutputStream(new ByteBufferOutputStream(resultBuffer)));
+
+ writable.writeByteBuffer(slicedBuffer);
+
+ assertEquals(2, resultBuffer.position(), "Writing to the buffer moves the position forward");
+ assertEquals(expectedArray, resultBuffer.array(), "Result buffer should have expected elements");
Review Comment:
Please use assertArrayEquals
##########
clients/src/test/java/org/apache/kafka/common/protocol/DataOutputStreamWritableTest.java:
##########
@@ -0,0 +1,66 @@
+/*
+ * 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.kafka.common.protocol;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.io.DataOutputStream;
+import java.nio.ByteBuffer;
+import org.apache.kafka.common.utils.ByteBufferOutputStream;
+import org.junit.jupiter.api.Test;
+
+public class DataOutputStreamWritableTest {
+ @Test
+ public void testWritingSlicedByteBuffer() {
+ byte[] expectedArray = new byte[]{2, 3, 0, 0};
+ ByteBuffer sourceBuffer = ByteBuffer.wrap(new byte[]{0, 1, 2, 3});
+ ByteBuffer resultBuffer = ByteBuffer.allocate(4);
+
+ // Move position forward to ensure slice is not whole buffer
+ sourceBuffer.position(2);
+ ByteBuffer slicedBuffer = sourceBuffer.slice();
+
+ Writable writable = new DataOutputStreamWritable(
+ new DataOutputStream(new ByteBufferOutputStream(resultBuffer)));
+
+ writable.writeByteBuffer(slicedBuffer);
+
+ assertEquals(2, resultBuffer.position(), "Writing to the buffer moves the position forward");
+ assertEquals(expectedArray, resultBuffer.array(), "Result buffer should have expected elements");
+ }
+
+ @Test
+ public void testWritingSlicedByteBufferWithNonZeroPosition() {
+ byte[] expectedArray = new byte[]{3, 0, 0, 0};
+ ByteBuffer originalBuffer = ByteBuffer.wrap(new byte[]{0, 1, 2, 3});
+ ByteBuffer resultBuffer = ByteBuffer.allocate(4);
+
+ // Move position forward to ensure slice is backed by heap buffer with non-zero offset
+ originalBuffer.position(2);
+ ByteBuffer slicedBuffer = originalBuffer.slice();
+ // Move the slice's position forward to ensure the writer starts reading at that position
+ slicedBuffer.position(1);
+
+ Writable writable = new DataOutputStreamWritable(
+ new DataOutputStream(new ByteBufferOutputStream(resultBuffer)));
+
+ writable.writeByteBuffer(slicedBuffer);
+
+ assertEquals(1, resultBuffer.position(), "Writing to the buffer moves the position forward");
+ assertEquals(expectedArray, resultBuffer.array(), "Result buffer should have expected elements");
Review Comment:
Please use assertArrayEquals
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [kafka] michaeljmarshall commented on a diff in pull request #13032: KAFKA-14540: Fix DataOutputStreamWritable#writeByteBuffer
Posted by GitBox <gi...@apache.org>.
michaeljmarshall commented on code in PR #13032:
URL: https://github.com/apache/kafka/pull/13032#discussion_r1054547725
##########
clients/src/main/java/org/apache/kafka/common/protocol/DataOutputStreamWritable.java:
##########
@@ -99,7 +99,7 @@ public void writeUnsignedVarint(int i) {
public void writeByteBuffer(ByteBuffer buf) {
try {
if (buf.hasArray()) {
- out.write(buf.array(), buf.position(), buf.limit());
+ out.write(buf.array(), buf.arrayOffset(), buf.limit());
Review Comment:
Great catch, thank you for your reviews. I'll fix the implementation and make sure the missed cases are covered by tests.
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [kafka] michaeljmarshall commented on pull request #13032: KAFKA-14540: Fix DataOutputStreamWritable#writeByteBuffer
Posted by GitBox <gi...@apache.org>.
michaeljmarshall commented on PR #13032:
URL: https://github.com/apache/kafka/pull/13032#issuecomment-1374113532
Thanks for catching my mistake @viktorsomogyi. My mistake was trusting IntelliJ to run the test.
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [kafka] showuon commented on a diff in pull request #13032: KAFKA-14540: Fix DataOutputStreamWritable#writeByteBuffer
Posted by GitBox <gi...@apache.org>.
showuon commented on code in PR #13032:
URL: https://github.com/apache/kafka/pull/13032#discussion_r1054067080
##########
clients/src/main/java/org/apache/kafka/common/protocol/DataOutputStreamWritable.java:
##########
@@ -99,7 +99,7 @@ public void writeUnsignedVarint(int i) {
public void writeByteBuffer(ByteBuffer buf) {
try {
if (buf.hasArray()) {
- out.write(buf.array(), buf.position(), buf.limit());
+ out.write(buf.array(), buf.arrayOffset(), buf.limit());
Review Comment:
Should it be `buf.position() + buf.arrayOffset()`?
> If this buffer is backed by an array then buffer position p corresponds to array index p + arrayOffset().
https://docs.oracle.com/javase/8/docs/api/java/nio/Buffer.html#arrayOffset--
##########
clients/src/test/java/org/apache/kafka/common/protocol/DataOutputStreamWritableTest.java:
##########
@@ -0,0 +1,47 @@
+/*
+ * 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.kafka.common.protocol;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.io.DataOutputStream;
+import java.nio.ByteBuffer;
+import org.apache.kafka.common.utils.ByteBufferOutputStream;
+import org.junit.jupiter.api.Test;
+
+public class DataOutputStreamWritableTest {
+ @Test
+ public void testWritingSlicedByteBuffer() {
+ ByteBuffer expectedBuffer = ByteBuffer.wrap(new byte[]{2, 3});
+ ByteBuffer originalBuffer = ByteBuffer.allocate(4);
+ for (int i = 0; i < originalBuffer.limit(); i++) {
+ originalBuffer.put(i, (byte) i);
+ }
+ // Move position forward to ensure slice is not whole buffer
+ originalBuffer.position(2);
+ ByteBuffer slicedBuffer = originalBuffer.slice();
+
Review Comment:
if this slicedBuffer position is not 0, the test will fail. ex:
```
...
ByteBuffer slicedBuffer = originalBuffer.slice();
slicedBuffer.position(1);
...
writable.writeByteBuffer(slicedBuffer);
// we expect the actualBuf should have only 1 data, but got 2
```
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [kafka] michaeljmarshall commented on a diff in pull request #13032: KAFKA-14540: Fix DataOutputStreamWritable#writeByteBuffer
Posted by GitBox <gi...@apache.org>.
michaeljmarshall commented on code in PR #13032:
URL: https://github.com/apache/kafka/pull/13032#discussion_r1054618672
##########
clients/src/test/java/org/apache/kafka/common/protocol/DataOutputStreamWritableTest.java:
##########
@@ -0,0 +1,47 @@
+/*
+ * 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.kafka.common.protocol;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.io.DataOutputStream;
+import java.nio.ByteBuffer;
+import org.apache.kafka.common.utils.ByteBufferOutputStream;
+import org.junit.jupiter.api.Test;
+
+public class DataOutputStreamWritableTest {
+ @Test
+ public void testWritingSlicedByteBuffer() {
+ ByteBuffer expectedBuffer = ByteBuffer.wrap(new byte[]{2, 3});
+ ByteBuffer originalBuffer = ByteBuffer.allocate(4);
+ for (int i = 0; i < originalBuffer.limit(); i++) {
+ originalBuffer.put(i, (byte) i);
+ }
+ // Move position forward to ensure slice is not whole buffer
+ originalBuffer.position(2);
+ ByteBuffer slicedBuffer = originalBuffer.slice();
+
Review Comment:
Fixed.
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [kafka] viktorsomogyi commented on pull request #13032: KAFKA-14540: Fix DataOutputStreamWritable#writeByteBuffer
Posted by GitBox <gi...@apache.org>.
viktorsomogyi commented on PR #13032:
URL: https://github.com/apache/kafka/pull/13032#issuecomment-1377067030
Looks like there are 3 flaky test failures, I'll merge this one.
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [kafka] showuon commented on pull request #13032: KAFKA-14540: Fix DataOutputStreamWritable#writeByteBuffer
Posted by GitBox <gi...@apache.org>.
showuon commented on PR #13032:
URL: https://github.com/apache/kafka/pull/13032#issuecomment-1376939462
Nice catch for `assertArrayEquals` issue. LGTM! Thanks.
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [kafka] michaeljmarshall commented on a diff in pull request #13032: KAFKA-14540: Fix DataOutputStreamWritable#writeByteBuffer
Posted by GitBox <gi...@apache.org>.
michaeljmarshall commented on code in PR #13032:
URL: https://github.com/apache/kafka/pull/13032#discussion_r1054597086
##########
clients/src/test/java/org/apache/kafka/common/protocol/DataOutputStreamWritableTest.java:
##########
@@ -0,0 +1,47 @@
+/*
+ * 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.kafka.common.protocol;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.io.DataOutputStream;
+import java.nio.ByteBuffer;
+import org.apache.kafka.common.utils.ByteBufferOutputStream;
+import org.junit.jupiter.api.Test;
+
+public class DataOutputStreamWritableTest {
+ @Test
+ public void testWritingSlicedByteBuffer() {
+ ByteBuffer expectedBuffer = ByteBuffer.wrap(new byte[]{2, 3});
+ ByteBuffer originalBuffer = ByteBuffer.allocate(4);
+ for (int i = 0; i < originalBuffer.limit(); i++) {
+ originalBuffer.put(i, (byte) i);
+ }
+ // Move position forward to ensure slice is not whole buffer
+ originalBuffer.position(2);
+ ByteBuffer slicedBuffer = originalBuffer.slice();
+
Review Comment:
You are right. You provided the solution in the other comment.
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [kafka] viktorsomogyi merged pull request #13032: KAFKA-14540: Fix DataOutputStreamWritable#writeByteBuffer
Posted by GitBox <gi...@apache.org>.
viktorsomogyi merged PR #13032:
URL: https://github.com/apache/kafka/pull/13032
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [kafka] michaeljmarshall commented on a diff in pull request #13032: KAFKA-14540: Fix DataOutputStreamWritable#writeByteBuffer
Posted by GitBox <gi...@apache.org>.
michaeljmarshall commented on code in PR #13032:
URL: https://github.com/apache/kafka/pull/13032#discussion_r1063786426
##########
clients/src/test/java/org/apache/kafka/common/protocol/DataOutputStreamWritableTest.java:
##########
@@ -0,0 +1,66 @@
+/*
+ * 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.kafka.common.protocol;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.io.DataOutputStream;
+import java.nio.ByteBuffer;
+import org.apache.kafka.common.utils.ByteBufferOutputStream;
+import org.junit.jupiter.api.Test;
+
+public class DataOutputStreamWritableTest {
+ @Test
+ public void testWritingSlicedByteBuffer() {
+ byte[] expectedArray = new byte[]{2, 3, 0, 0};
+ ByteBuffer sourceBuffer = ByteBuffer.wrap(new byte[]{0, 1, 2, 3});
+ ByteBuffer resultBuffer = ByteBuffer.allocate(4);
+
+ // Move position forward to ensure slice is not whole buffer
+ sourceBuffer.position(2);
+ ByteBuffer slicedBuffer = sourceBuffer.slice();
+
+ Writable writable = new DataOutputStreamWritable(
+ new DataOutputStream(new ByteBufferOutputStream(resultBuffer)));
+
+ writable.writeByteBuffer(slicedBuffer);
+
+ assertEquals(2, resultBuffer.position(), "Writing to the buffer moves the position forward");
+ assertEquals(expectedArray, resultBuffer.array(), "Result buffer should have expected elements");
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org