You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "LuciferYang (via GitHub)" <gi...@apache.org> on 2023/10/11 15:24:18 UTC

[PR] Use `java.lang.ref.Cleaner` instead of `finalize` for `NioBufferedFileInputStream` [spark]

LuciferYang opened a new pull request, #43333:
URL: https://github.com/apache/spark/pull/43333

   <!--
   Thanks for sending a pull request!  Here are some tips for you:
     1. If this is your first time, please read our contributor guidelines: https://spark.apache.org/contributing.html
     2. Ensure you have added or run the appropriate tests for your PR: https://spark.apache.org/developer-tools.html
     3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][SPARK-XXXX] Your PR title ...'.
     4. Be sure to keep the PR description updated to reflect all changes.
     5. Please write your PR title to summarize what this PR proposes.
     6. If possible, provide a concise example to reproduce the issue for a faster review.
     7. If you want to add a new configuration, please read the guideline first for naming configurations in
        'core/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
     8. If you want to add or modify an error type or message, please read the guideline first in
        'core/src/main/resources/error/README.md'.
   -->
   
   ### What changes were proposed in this pull request?
   <!--
   Please clarify what changes you are proposing. The purpose of this section is to outline the changes and how this PR fixes the issue. 
   If possible, please consider writing useful notes for better and faster reviews in your PR. See the examples below.
     1. If you refactor some codes with changing classes, showing the class hierarchy will help reviewers.
     2. If you fix some SQL features, you can provide some references of other DBMSes.
     3. If there is design documentation, please add the link.
     4. If there is a discussion in the mailing list, please add the link.
   -->
   
   
   ### Why are the changes needed?
   <!--
   Please clarify why the changes are needed. For instance,
     1. If you propose a new API, clarify the use case for a new API.
     2. If you fix a bug, you can clarify why it is a bug.
   -->
   
   
   ### Does this PR introduce _any_ user-facing change?
   <!--
   Note that it means *any* user-facing change including all aspects such as the documentation fix.
   If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible.
   If possible, please also clarify if this is a user-facing change compared to the released Spark versions or within the unreleased branches such as master.
   If no, write 'No'.
   -->
   
   
   ### How was this patch tested?
   <!--
   If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible.
   If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future.
   If tests were not added, please describe why they were not added and/or why it was difficult to add.
   If benchmark tests were added, please run the benchmarks in GitHub Actions for the consistent environment, and the instructions could accord to: https://spark.apache.org/developer-tools.html#github-workflow-benchmarks.
   -->
   
   
   ### Was this patch authored or co-authored using generative AI tooling?
   <!--
   If generative AI tooling has been used in the process of authoring this patch, please include the
   phrase: 'Generated-by: ' followed by the name of the tool and its version.
   If no, write 'No'.
   Please refer to the [ASF Generative Tooling Guidance](https://www.apache.org/legal/generative-tooling.html) for details.
   -->
   


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] Use `java.lang.ref.Cleaner` instead of `finalize` for `NioBufferedFileInputStream` [spark]

Posted by "mridulm (via GitHub)" <gi...@apache.org>.
mridulm commented on code in PR #43333:
URL: https://github.com/apache/spark/pull/43333#discussion_r1357160347


##########
core/src/main/java/org/apache/spark/io/NioBufferedFileInputStream.java:
##########
@@ -125,13 +132,28 @@ private long skipFromFileChannel(long n) throws IOException {
 
   @Override
   public synchronized void close() throws IOException {
-    fileChannel.close();
-    StorageUtils.dispose(byteBuffer);
+    try {
+      this.cleanable.clean();
+    } catch (UncheckedIOException re) {
+      if (re.getCause() != null) {
+        throw re.getCause();
+      } else {
+        throw re;
+      }
+    }
   }
 
-  @SuppressWarnings("deprecation")
-  @Override
-  protected void finalize() throws IOException {
-    close();
+  private record ResourceCleaner(
+      FileChannel fileChannel,
+      ByteBuffer byteBuffer) implements Runnable {
+    @Override
+    public void run() {
+      try {
+        fileChannel.close();
+        StorageUtils.dispose(byteBuffer);

Review Comment:
   This was not done earlier - but do we want to ensure `dispose` is called even if `close` threw an exception ?



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] Use `java.lang.ref.Cleaner` instead of `finalize` for `NioBufferedFileInputStream` [spark]

Posted by "LuciferYang (via GitHub)" <gi...@apache.org>.
LuciferYang commented on code in PR #43333:
URL: https://github.com/apache/spark/pull/43333#discussion_r1357171350


##########
core/src/main/java/org/apache/spark/io/NioBufferedFileInputStream.java:
##########
@@ -125,13 +132,28 @@ private long skipFromFileChannel(long n) throws IOException {
 
   @Override
   public synchronized void close() throws IOException {
-    fileChannel.close();
-    StorageUtils.dispose(byteBuffer);
+    try {
+      this.cleanable.clean();
+    } catch (UncheckedIOException re) {
+      if (re.getCause() != null) {
+        throw re.getCause();
+      } else {
+        throw re;
+      }
+    }
   }
 
-  @SuppressWarnings("deprecation")
-  @Override
-  protected void finalize() throws IOException {
-    close();
+  private record ResourceCleaner(
+      FileChannel fileChannel,
+      ByteBuffer byteBuffer) implements Runnable {
+    @Override
+    public void run() {
+      try {
+        fileChannel.close();
+        StorageUtils.dispose(byteBuffer);

Review Comment:
   This is a good question, I think even if `fileChannel.close()` throws an exception, we should try to dispose of the byteBuffer, let's put it in the finally block.
   
   



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-45530][CORE] Use `java.lang.ref.Cleaner` instead of `finalize` for `NioBufferedFileInputStream` [spark]

Posted by "LuciferYang (via GitHub)" <gi...@apache.org>.
LuciferYang commented on PR #43333:
URL: https://github.com/apache/spark/pull/43333#issuecomment-1762516078

   ```
   [info] *** 1 TEST FAILED ***
   [error] Failed tests:
   [error] 	org.apache.spark.sql.kafka010.KafkaSourceStressSuite
   [warn] In the last 2048 seconds, 6.951 (0.3%) were spent in GC. [Heap: 3.23GB free of 4.00GB, max 4.00GB] Consider increasing the JVM heap using `-Xmx` or try a different collector, e.g. `-XX:+UseG1GC`, for better performance.
   [error] (sql-kafka-0-10 / Test / test) sbt.TestsFailedException: Tests unsuccessful
   [error] Total time: 2049 s (34:09), completed Oct 13, 2023, 4:05:45 PM
   ```
   This is a case that started failing after upgrading to Kafka 3.6, the author is fixing it, it's unrelated to this PR. Could you take another look at this pr If you have time, 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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-45530][CORE] Use `java.lang.ref.Cleaner` instead of `finalize` for `NioBufferedFileInputStream` [spark]

Posted by "mridulm (via GitHub)" <gi...@apache.org>.
mridulm commented on PR #43333:
URL: https://github.com/apache/spark/pull/43333#issuecomment-1762592047

   Looks good to me, thanks for fixing this @LuciferYang !


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-45530][CORE] Use `java.lang.ref.Cleaner` instead of `finalize` for `NioBufferedFileInputStream` [spark]

Posted by "LuciferYang (via GitHub)" <gi...@apache.org>.
LuciferYang closed pull request #43333: [SPARK-45530][CORE] Use `java.lang.ref.Cleaner` instead of `finalize` for `NioBufferedFileInputStream`
URL: https://github.com/apache/spark/pull/43333


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-45530][CORE] Use `java.lang.ref.Cleaner` instead of `finalize` for `NioBufferedFileInputStream` [spark]

Posted by "LuciferYang (via GitHub)" <gi...@apache.org>.
LuciferYang commented on PR #43333:
URL: https://github.com/apache/spark/pull/43333#issuecomment-1760793180

   There are a few more places to do similar work after this pr:
   
   1. RocksDBIterator/LevelDBIterator(SPARK-45533)
   2. RemoteBlockPushResolver (SPARK-45534)
   3. ThreadWithGarbageCleanup
   
   I have already filed new Jira for Item 1 and 2 for tracking. As for Item 3, since it's in the `hive-thriftserver` module, I have no plans to fix it at the moment.
   
   


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] Use `java.lang.ref.Cleaner` instead of `finalize` for `NioBufferedFileInputStream` [spark]

Posted by "mridulm (via GitHub)" <gi...@apache.org>.
mridulm commented on code in PR #43333:
URL: https://github.com/apache/spark/pull/43333#discussion_r1357436429


##########
core/src/main/java/org/apache/spark/io/NioBufferedFileInputStream.java:
##########
@@ -32,6 +34,10 @@
  */
 public final class NioBufferedFileInputStream extends InputStream {
 
+  private static final Cleaner cleaner = Cleaner.create();
+
+  private final Cleaner.Cleanable cleanable;
+
   private static final int DEFAULT_BUFFER_SIZE_BYTES = 8192;

Review Comment:
   ```suggestion
     private static final Cleaner cleaner = Cleaner.create();
     private static final int DEFAULT_BUFFER_SIZE_BYTES = 8192;
   
     private final Cleaner.Cleanable cleanable;
   ```



##########
core/src/main/java/org/apache/spark/io/NioBufferedFileInputStream.java:
##########
@@ -32,6 +34,10 @@
  */
 public final class NioBufferedFileInputStream extends InputStream {
 
+  private static final Cleaner cleaner = Cleaner.create();
+
+  private final Cleaner.Cleanable cleanable;
+
   private static final int DEFAULT_BUFFER_SIZE_BYTES = 8192;

Review Comment:
   nit:
   ```suggestion
     private static final Cleaner cleaner = Cleaner.create();
     private static final int DEFAULT_BUFFER_SIZE_BYTES = 8192;
   
     private final Cleaner.Cleanable cleanable;
   ```



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] Use `java.lang.ref.Cleaner` instead of `finalize` for `NioBufferedFileInputStream` [spark]

Posted by "LuciferYang (via GitHub)" <gi...@apache.org>.
LuciferYang commented on PR #43333:
URL: https://github.com/apache/spark/pull/43333#issuecomment-1757950663

   Test first


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] Use `java.lang.ref.Cleaner` instead of `finalize` for `NioBufferedFileInputStream` [spark]

Posted by "LuciferYang (via GitHub)" <gi...@apache.org>.
LuciferYang commented on PR #43333:
URL: https://github.com/apache/spark/pull/43333#issuecomment-1760052816

   Yes, for the other `finalize()`, I am also trying to fix them
   
   


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-45530][CORE] Use `java.lang.ref.Cleaner` instead of `finalize` for `NioBufferedFileInputStream` [spark]

Posted by "LuciferYang (via GitHub)" <gi...@apache.org>.
LuciferYang commented on code in PR #43333:
URL: https://github.com/apache/spark/pull/43333#discussion_r1359102252


##########
core/src/main/java/org/apache/spark/io/NioBufferedFileInputStream.java:
##########
@@ -32,8 +34,11 @@
  */
 public final class NioBufferedFileInputStream extends InputStream {
 
+  private static final Cleaner CLEANER = Cleaner.create();

Review Comment:
   [a9f3025](https://github.com/apache/spark/pull/43333/commits/a9f3025afb2381f573ee0c3e146da9d0b704593b) rename this from `cleaner` to `CLEANER` due to it's static



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-45530][CORE] Use `java.lang.ref.Cleaner` instead of `finalize` for `NioBufferedFileInputStream` [spark]

Posted by "LuciferYang (via GitHub)" <gi...@apache.org>.
LuciferYang commented on PR #43333:
URL: https://github.com/apache/spark/pull/43333#issuecomment-1762853580

   Merged into master for Spark 4.0. Thanks @mridulm ~


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org