Skip to content

MINOR; Remove cast for Records' slice #19661

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: trunk
Choose a base branch
from

Conversation

jsancio
Copy link
Member

@jsancio jsancio commented May 8, 2025

In Java return types are covariant. This means that method override can
override the return type with a subclass.

@github-actions github-actions bot added core Kafka Broker clients small Small PRs labels May 8, 2025
@chia7712
Copy link
Member

chia7712 commented May 8, 2025

@jsancio there is a discussion before - see #19581 (comment)

Copy link
Contributor

@junrao junrao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jsancio : Thanks for the PR. Since this usage of covariant is idiomatic in java, the change looks good to me. Could you revert some of the other unnecessary casting introduced in 7eb661b?

@jsancio
Copy link
Member Author

jsancio commented May 8, 2025

@jsancio : Thanks for the PR. Since this usage of covariant is idiomatic in java, the change looks good to me. Could you revert some of the other unnecessary casting introduced in 7eb661b?

Thanks. I removed the casts added in that commit. Let me know if you think I missed some.

@jsancio
Copy link
Member Author

jsancio commented May 8, 2025

@jsancio there is a discussion before - see #19581 (comment)

Thanks. I looked at that discussion. It is always safe for an implementation to override the return type with a subtype. This is because return types are covariant and Java supports it.

Copy link
Contributor

@junrao junrao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jsancio : Thanks for the updated PR. Just one comment.

try {
return new FileRecords(file, channel, startPosition, startPosition + availableBytes, true);
} catch (IOException e) {
throw new UncheckedIOException(e);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In RecordBatchIterator, we convert an IOException to a KafkaException. Should we be consistent here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

It seems the IOException is meaningless when the isSlice=true. Maybe we can have a new constructor of FileRecords which does not have IOException in the signature?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In RecordBatchIterator, we convert an IOException to a KafkaException. Should we be consistent here?

Are you suggesting I change it to throw an UncheckedIOException? If so, that is what I did. I looked at the method declaration and we don't seem to document that KafkaException is thrown. It should be okay to change the type. What do you think?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems the IOException is meaningless when the isSlice=true. Maybe we can have a new constructor of FileRecords which does not have IOException in the signature?

Good catch. Fixed it. Let me know what you think of my solution.

Copy link
Contributor

@apoorvmittal10 apoorvmittal10 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, one comment.

@@ -1170,7 +1169,7 @@ public void testSliceEmptyRecords() {
*/
@ParameterizedTest
@ArgumentsSource(MemoryRecordsArgumentsProvider.class)
public void testSliceForAlreadySlicedMemoryRecords(Args args) throws IOException {
public void testSliceForAlreadySlicedMemoryRecords(Args args) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also remove explicit casting in this class i.e. at line 1092, 1093, etc. The similar we removed in FileRecordsTest.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I searched the entire repo for casts to MemoryRecord and FileRecords and I think we got all of them.

@github-actions github-actions bot removed the small Small PRs label May 9, 2025
Copy link
Contributor

@junrao junrao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jsancio : Thanks for the updated PR. A few more comments.

@@ -41,7 +41,7 @@ protected T makeNext() {
} catch (EOFException e) {
throw new CorruptRecordException("Unexpected EOF while attempting to read the next batch", e);
} catch (IOException e) {
throw new KafkaException(e);
throw new UncheckedIOException(e);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need this change? There are quite a few other places where we convert to KafkaException.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed this change.

FileRecords(
File file,
FileChannel channel,
int start,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we remove start since it's always 0? Also, we should direct open() to use this constructor, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. Good catch. Should make the constructor much cleaner.

FileChannel channel,
int start,
int end,
boolean isSlice
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we just remove isSlice and add a comment that this is intended for slicing only?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I removed this since open doesn't specify the start.

Copy link
Contributor

@junrao junrao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jsancio : Thanks for the updated PR. Just a minor comment.

// set the file position to the last byte in the file
channel.position(limit);
}
// if this is not a slice, update the file pointer to the end of the file
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we change this comment since this constructor is not for slicing?

Copy link
Member

@chia7712 chia7712 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jsancio thanks for this patch. one comment is left. PTAL

this.start = start;
this.end = end;
this.isSlice = true;
this.size = new AtomicInteger();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

        // don't check the file size if this is just a slice view
        this.size = new AtomicInteger(end - start);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clients core Kafka Broker
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants