Skip to content

HDDS-14370. RandomAccessFileChannel to implement Closeable#9905

Open
rich7420 wants to merge 9 commits intoapache:masterfrom
rich7420:HDDS-14370
Open

HDDS-14370. RandomAccessFileChannel to implement Closeable#9905
rich7420 wants to merge 9 commits intoapache:masterfrom
rich7420:HDDS-14370

Conversation

@rich7420
Copy link
Contributor

What changes were proposed in this pull request?

The class already had a close() method but didn't implement Closeable, which could lead to resource leaks if developers miss calling close().

What is the link to the Apache JIRA

HDDS-14370

How was this patch tested?

https://github.com/rich7420/ozone/actions/runs/22942351747

reopen

#9628

@adoroszlai adoroszlai requested a review from jojochuang March 11, 2026 14:48
@adoroszlai
Copy link
Contributor

@Gargi-jais11 please take a look

Copy link

@yandrey321 yandrey321 left a comment

Choose a reason for hiding this comment

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

lgtm

Copy link
Contributor

@Gargi-jais11 Gargi-jais11 left a comment

Choose a reason for hiding this comment

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

Thanks @rich7420 for updating the patch. LGTM!

@rich7420
Copy link
Contributor Author

Thanks so much @adoroszlai , @yandrey321 and @Gargi-jais11

* {@link FileChannel#close()} is final (inherited), so we implement a minimal {@link FileChannel}
* and throw from {@link #implCloseChannel()} to simulate close failure.
*/
private static final class FailingCloseFileChannel extends FileChannel {
Copy link
Member

Choose a reason for hiding this comment

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

could we find other to verify the close behaviour? there are so much code that are unused

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch! I tried to replace FailingCloseFileChannel and TrackingRandomAccessFile with Mockito mock to eliminate the boilerplate.

Copy link
Contributor

@sreejasahithi sreejasahithi left a comment

Choose a reason for hiding this comment

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

Thanks @rich7420 left few comments

Comment on lines +88 to +89
assertDoesNotThrow(c::close);
verify(spyRaf).close();
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
assertDoesNotThrow(c::close);
verify(spyRaf).close();
assertDoesNotThrow(c::close);
verify(failingChannel).close();
verify(spyRaf).close();

Optional: We can add verify(failingChannel).close(); to explicitly verify the channel close was attempted.

Copy link
Contributor

@sreejasahithi sreejasahithi Mar 16, 2026

Choose a reason for hiding this comment

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

nit : Not related to your changes, but there is a typo in javadoc - ture should be true

Comment on lines +101 to +109
@Test
void readWithZeroSizedBuffer() throws Exception {
final RandomAccessFileChannel c = new RandomAccessFileChannel();
final File f = tempDir.resolve("test-file").toFile();
try (RandomAccessFile raf = new RandomAccessFile(f, "rw")) {
raf.write(new byte[]{1, 2, 3, 4, 5});
}

c.open(f);
Copy link
Contributor

Choose a reason for hiding this comment

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

we should also have c.close()

@rich7420
Copy link
Contributor Author

@sreejasahithi thanks for the review!

Copy link
Contributor

@sreejasahithi sreejasahithi left a comment

Choose a reason for hiding this comment

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

LGTM

@adoroszlai adoroszlai requested a review from peterxcli March 18, 2026 10:22
@adoroszlai
Copy link
Contributor

@peterxcli would you like to take another look?

final File f = Objects.requireNonNull(file, "blockFile == null");
final RandomAccessFile newRaf = new RandomAccessFile(f, "r");
try {
final FileChannel newChannel = newRaf.getChannel();
Copy link
Member

Choose a reason for hiding this comment

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

I think this wont throw, so we can remove the exception handling block

// Closeable contract: close via try-with-resources helper
closeAndVerify(c);
assertFalse(c.isOpen(), "should be closed after try-with-resources");
assertDoesNotThrow(c::close, "double close should be safe");
Copy link
Member

@peterxcli peterxcli Mar 20, 2026

Choose a reason for hiding this comment

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

if double close happened, then the programming logic must be wrong, right?

@Override
public synchronized void close() {
if (blockFile == null) {
final File fileToClose = blockFile;
Copy link
Member

Choose a reason for hiding this comment

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

why add a new variable? doesn't the original logic work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants