hbase icon indicating copy to clipboard operation
hbase copied to clipboard

HBASE-29348 Fixed NPE issue due to HStoreFile getting closed

Open SwaraliJoshi opened this issue 11 months ago • 16 comments

Root cause

The NPE is a race between asynchronous archival of an already-compacted file and the compaction bookkeeping that re-sorts the compacted-files list

The file whose reader gets closed is a previously compacted file (already compactedAway) that the background CompactedHFilesDischarger is archiving. So there is no overlap between two compactions; the overlap is between (a) the discharger closing/archiving an old compacted file and (b) a concurrent compaction/selection thread sorting the compactedfiles list that still contains that old file.

Concretely, two threads on the same store:

  1. Discharger thread (CompactedHFilesDischarger → HStore.closeAndArchiveCompactedFiles → removeCompactedfiles): while archiving a compacted file it calls storeFileTracker.removeStoreFiles(...) (HStore#removeCompactedfiles) → HFileArchiver → FileableStoreFile.moveAndClose → HStoreFile.closeStoreFile, which sets initialReader = null. This runs under archiveLock only (not under storeEngine.writeLock), and it happens before clearCompactedfiles removes the file from the compactedfiles list. So there is a window where the file is still present in compactedfiles but its reader is already null.
  2. Compaction/selection thread (HStore.compact / HStore.removeUnneededFiles → replaceStoreFiles → StoreEngine.replaceStoreFiles → DefaultStoreFileManager.addCompactionResults): runs under storeEngine.writeLock and sorts ImmutableList.sortedCopyOf(storeFileComparator, Iterables.concat(compactedfiles, newCompactedfiles)).

The two list-mutating operations (addCompactionResults and clearCompactedfiles/removeCompactedFiles) are both under writeLock, so the list itself is never corrupted. The unprotected state is the reader field, which the discharger mutates to null outside writeLock while the file is still list-visible. The sorting thread then observes that null reader.

Why it throws an NPE, and only for date-tiered compaction

The store file comparator for date-tiered (and custom-tiered) stores is StoreFileComparators.SEQ_ID_MAX_TIMESTAMP, whose GetMaxTimestamp extractor calls HStoreFile.getMaximumTimestamp(), which dereferences the reader (getReader().timeRange). When the comparator hits the file whose reader was just nulled, getReader() returns null → NullPointerException during the sort in addCompactionResults.

Other policies use StoreFileComparators.SEQ_ID, which never reads the timestamp; its only reader access (GetFileSize) is already null-guarded. That is why this is specific to date-tiered/custom-tiered stores.

Why this causes store files to grow unbounded (the production impact)

The NPE most often fires from the TTL cleanup step that runs at the start of every compaction request (HStore.removeUnneededFiles): it first adds the expired files to filesCompacting (addToCompactingFiles), then calls replaceStoreFiles. The code that removes those files from filesCompacting is the actionUnderLock that runs after addCompactionResults in StoreEngine.replaceStoreFiles. Because addCompactionResults throws, that cleanup never runs and there is no finally to undo it, so the expired files are permanently leaked into filesCompacting.

From then on, every compaction selection trips Preconditions.checkArgument(idx != -1) in SortedCompactionPolicy.getCurrentEligibleFiles (the orphaned file is no longer a candidate) and throws IllegalArgumentException. Compaction selection is wedged for that store until the region is reopened, so ongoing flushes keep adding store files with nothing to merge them — the store-file count grows without bound. This is the durable, observable symptom (the one-time NPE is easy to miss; the growing store-file count and the recurring IllegalArgumentException are the fingerprint).

The fix

Make the date-tiered comparator tolerate a transiently-null reader, mirroring the existing null-safe convention in GetFileSize:

  • StoreFileComparators.GetMaxTimestamp now returns a default timestamp when sf.getReader() == null instead of dereferencing it. This removes the NPE from the sort regardless of timing, which in turn prevents the filesCompacting leak and the resulting unbounded growth. HStoreFile.getMaximumTimestamp() is intentionally not changed, so its fail-fast behavior is preserved for the compaction-policy callers that operate on live, active files.
  • HFileArchiver.moveAndClose is reordered to rename first and close the reader only on a successful rename, so an archive failure no longer strands a compacted file with a closed reader (root-cause hardening for the related failure mode).

Testing

  • A regression test drives the real DefaultStoreFileManager.addCompactionResults path with the date-tiered comparator and a compacted file whose reader has been closed; it fails with the original NPE without the fix and passes with it.
  • Tests for HFileArchiver.moveAndClose verify the reader is closed on a successful archive and, critically, stays open when the rename fails.

SwaraliJoshi avatar Dec 16 '25 05:12 SwaraliJoshi

:broken_heart: -1 overall

Vote Subsystem Runtime Logfile Comment
+0 :ok: reexec 0m 30s Docker mode activated.
-0 :warning: yetus 0m 3s Unprocessed flag(s): --brief-report-file --spotbugs-strict-precheck --author-ignore-list --blanks-eol-ignore-file --blanks-tabs-ignore-file --quick-hadoopcheck
_ Prechecks _
_ master Compile Tests _
+1 :green_heart: mvninstall 3m 55s master passed
+1 :green_heart: compile 1m 4s master passed
+1 :green_heart: javadoc 0m 32s master passed
+1 :green_heart: shadedjars 6m 31s branch has no errors when building our shaded downstream artifacts.
_ Patch Compile Tests _
-1 :x: mvninstall 1m 52s /patch-mvninstall-root.txt root in the patch failed.
-1 :x: compile 1m 4s /patch-compile-hbase-server.txt hbase-server in the patch failed.
-0 :warning: javac 1m 4s /patch-compile-hbase-server.txt hbase-server in the patch failed.
+1 :green_heart: javadoc 0m 31s the patch passed
-1 :x: shadedjars 4m 56s patch has 20 errors when building our shaded downstream artifacts.
_ Other Tests _
-1 :x: unit 1m 5s /patch-unit-hbase-server.txt hbase-server in the patch failed.
22m 57s
Subsystem Report/Notes
Docker ClientAPI=1.43 ServerAPI=1.43 base: https://ci-hbase.apache.org/job/HBase-PreCommit-GitHub-PR/job/PR-7552/1/artifact/yetus-jdk17-hadoop3-check/output/Dockerfile
GITHUB PR https://github.com/apache/hbase/pull/7552
Optional Tests javac javadoc unit compile shadedjars
uname Linux f4749785981f 5.4.0-1103-aws #111~18.04.1-Ubuntu SMP Tue May 23 20:04:10 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
Build tool maven
Personality dev-support/hbase-personality.sh
git revision master / 5121ec19172b03a1330176921dd97fa24c33a254
Default Java Eclipse Adoptium-17.0.11+9
shadedjars https://ci-hbase.apache.org/job/HBase-PreCommit-GitHub-PR/job/PR-7552/1/artifact/yetus-jdk17-hadoop3-check/output/patch-shadedjars.txt
Test Results https://ci-hbase.apache.org/job/HBase-PreCommit-GitHub-PR/job/PR-7552/1/testReport/
Max. process+thread count 83 (vs. ulimit of 30000)
modules C: hbase-server U: hbase-server
Console output https://ci-hbase.apache.org/job/HBase-PreCommit-GitHub-PR/job/PR-7552/1/console
versions git=2.34.1 maven=3.9.8
Powered by Apache Yetus 0.15.0 https://yetus.apache.org

This message was automatically generated.

Apache-HBase avatar Dec 16 '25 06:12 Apache-HBase

:broken_heart: -1 overall

Vote Subsystem Runtime Logfile Comment
+0 :ok: reexec 0m 38s Docker mode activated.
_ Prechecks _
+1 :green_heart: dupname 0m 0s No case conflicting files found.
+0 :ok: codespell 0m 0s codespell was not available.
+0 :ok: detsecrets 0m 0s detect-secrets was not available.
+1 :green_heart: @author 0m 0s The patch does not contain any @author tags.
+1 :green_heart: hbaseanti 0m 0s Patch does not have any anti-patterns.
_ master Compile Tests _
+1 :green_heart: mvninstall 4m 9s master passed
+1 :green_heart: compile 4m 2s master passed
+1 :green_heart: checkstyle 1m 13s master passed
+1 :green_heart: spotbugs 2m 8s master passed
+1 :green_heart: spotless 1m 1s branch has no errors when running spotless:check.
_ Patch Compile Tests _
-1 :x: mvninstall 1m 58s /patch-mvninstall-root.txt root in the patch failed.
-1 :x: compile 2m 8s /patch-compile-hbase-server.txt hbase-server in the patch failed.
-0 :warning: javac 2m 8s /patch-compile-hbase-server.txt hbase-server in the patch failed.
+1 :green_heart: blanks 0m 0s The patch has no blanks issues.
+1 :green_heart: checkstyle 1m 12s the patch passed
-1 :x: spotbugs 1m 7s /patch-spotbugs-hbase-server.txt hbase-server in the patch failed.
-1 :x: hadoopcheck 2m 37s The patch causes 20 errors with Hadoop v3.3.6.
-1 :x: hadoopcheck 5m 1s The patch causes 20 errors with Hadoop v3.4.1.
-1 :x: spotless 0m 50s patch has 63 errors when running spotless:check, run spotless:apply to fix.
_ Other Tests _
+1 :green_heart: asflicense 0m 12s The patch does not generate ASF License warnings.
27m 20s
Subsystem Report/Notes
Docker ClientAPI=1.43 ServerAPI=1.43 base: https://ci-hbase.apache.org/job/HBase-PreCommit-GitHub-PR/job/PR-7552/1/artifact/yetus-general-check/output/Dockerfile
GITHUB PR https://github.com/apache/hbase/pull/7552
Optional Tests dupname asflicense javac spotbugs checkstyle codespell detsecrets compile hadoopcheck hbaseanti spotless
uname Linux f8095fa05512 5.4.0-1103-aws #111~18.04.1-Ubuntu SMP Tue May 23 20:04:10 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
Build tool maven
Personality dev-support/hbase-personality.sh
git revision master / 5121ec19172b03a1330176921dd97fa24c33a254
Default Java Eclipse Adoptium-17.0.11+9
hadoopcheck https://ci-hbase.apache.org/job/HBase-PreCommit-GitHub-PR/job/PR-7552/1/artifact/yetus-general-check/output/patch-javac-3.3.6.txt
hadoopcheck https://ci-hbase.apache.org/job/HBase-PreCommit-GitHub-PR/job/PR-7552/1/artifact/yetus-general-check/output/patch-javac-3.4.1.txt
spotless https://ci-hbase.apache.org/job/HBase-PreCommit-GitHub-PR/job/PR-7552/1/artifact/yetus-general-check/output/patch-spotless.txt
Max. process+thread count 85 (vs. ulimit of 30000)
modules C: hbase-server U: hbase-server
Console output https://ci-hbase.apache.org/job/HBase-PreCommit-GitHub-PR/job/PR-7552/1/console
versions git=2.34.1 maven=3.9.8 spotbugs=4.7.3
Powered by Apache Yetus 0.15.0 https://yetus.apache.org

This message was automatically generated.

Apache-HBase avatar Dec 16 '25 06:12 Apache-HBase

Seems to be a import issue in the patch. @SwaraliJoshi

mnpoonia avatar Dec 16 '25 10:12 mnpoonia

Mind explaining more about the fix? What is the root cause of the NPE and why it only affect DateTieredCompaction?

Thanks.

Apache9 avatar Dec 20 '25 15:12 Apache9

:broken_heart: -1 overall

Vote Subsystem Runtime Logfile Comment
+0 :ok: reexec 0m 29s Docker mode activated.
_ Prechecks _
+1 :green_heart: dupname 0m 0s No case conflicting files found.
+0 :ok: codespell 0m 0s codespell was not available.
+0 :ok: detsecrets 0m 0s detect-secrets was not available.
+1 :green_heart: @author 0m 0s The patch does not contain any @author tags.
+1 :green_heart: hbaseanti 0m 0s Patch does not have any anti-patterns.
_ master Compile Tests _
+1 :green_heart: mvninstall 3m 21s master passed
+1 :green_heart: compile 3m 16s master passed
+1 :green_heart: checkstyle 0m 58s master passed
+1 :green_heart: spotbugs 1m 34s master passed
+1 :green_heart: spotless 0m 49s branch has no errors when running spotless:check.
_ Patch Compile Tests _
+1 :green_heart: mvninstall 2m 54s the patch passed
+1 :green_heart: compile 3m 15s the patch passed
+1 :green_heart: javac 3m 15s the patch passed
+1 :green_heart: blanks 0m 0s The patch has no blanks issues.
+1 :green_heart: checkstyle 0m 57s the patch passed
+1 :green_heart: spotbugs 1m 39s the patch passed
+1 :green_heart: hadoopcheck 11m 17s Patch does not cause any errors with Hadoop 3.3.6 3.4.1.
-1 :x: spotless 0m 36s patch has 63 errors when running spotless:check, run spotless:apply to fix.
_ Other Tests _
+1 :green_heart: asflicense 0m 12s The patch does not generate ASF License warnings.
38m 33s
Subsystem Report/Notes
Docker ClientAPI=1.43 ServerAPI=1.43 base: https://ci-hbase.apache.org/job/HBase-PreCommit-GitHub-PR/job/PR-7552/2/artifact/yetus-general-check/output/Dockerfile
GITHUB PR https://github.com/apache/hbase/pull/7552
Optional Tests dupname asflicense javac spotbugs checkstyle codespell detsecrets compile hadoopcheck hbaseanti spotless
uname Linux 6432dc50d6bc 5.4.0-1103-aws #111~18.04.1-Ubuntu SMP Tue May 23 20:04:10 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
Build tool maven
Personality dev-support/hbase-personality.sh
git revision master / 171ba9928ce97b85dae6749ea179c2daf2491900
Default Java Eclipse Adoptium-17.0.11+9
spotless https://ci-hbase.apache.org/job/HBase-PreCommit-GitHub-PR/job/PR-7552/2/artifact/yetus-general-check/output/patch-spotless.txt
Max. process+thread count 84 (vs. ulimit of 30000)
modules C: hbase-server U: hbase-server
Console output https://ci-hbase.apache.org/job/HBase-PreCommit-GitHub-PR/job/PR-7552/2/console
versions git=2.34.1 maven=3.9.8 spotbugs=4.7.3
Powered by Apache Yetus 0.15.0 https://yetus.apache.org

This message was automatically generated.

Apache-HBase avatar Dec 30 '25 20:12 Apache-HBase

:broken_heart: -1 overall

Vote Subsystem Runtime Logfile Comment
+0 :ok: reexec 0m 29s Docker mode activated.
-0 :warning: yetus 0m 3s Unprocessed flag(s): --brief-report-file --spotbugs-strict-precheck --author-ignore-list --blanks-eol-ignore-file --blanks-tabs-ignore-file --quick-hadoopcheck
_ Prechecks _
_ master Compile Tests _
+1 :green_heart: mvninstall 3m 37s master passed
+1 :green_heart: compile 1m 0s master passed
+1 :green_heart: javadoc 0m 30s master passed
+1 :green_heart: shadedjars 6m 16s branch has no errors when building our shaded downstream artifacts.
_ Patch Compile Tests _
+1 :green_heart: mvninstall 3m 6s the patch passed
+1 :green_heart: compile 1m 0s the patch passed
+1 :green_heart: javac 1m 0s the patch passed
+1 :green_heart: javadoc 0m 27s the patch passed
+1 :green_heart: shadedjars 6m 10s patch has no errors when building our shaded downstream artifacts.
_ Other Tests _
-1 :x: unit 228m 57s /patch-unit-hbase-server.txt hbase-server in the patch failed.
256m 26s
Subsystem Report/Notes
Docker ClientAPI=1.43 ServerAPI=1.43 base: https://ci-hbase.apache.org/job/HBase-PreCommit-GitHub-PR/job/PR-7552/2/artifact/yetus-jdk17-hadoop3-check/output/Dockerfile
GITHUB PR https://github.com/apache/hbase/pull/7552
Optional Tests javac javadoc unit compile shadedjars
uname Linux ae869da82352 5.4.0-1103-aws #111~18.04.1-Ubuntu SMP Tue May 23 20:04:10 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
Build tool maven
Personality dev-support/hbase-personality.sh
git revision master / 171ba9928ce97b85dae6749ea179c2daf2491900
Default Java Eclipse Adoptium-17.0.11+9
Test Results https://ci-hbase.apache.org/job/HBase-PreCommit-GitHub-PR/job/PR-7552/2/testReport/
Max. process+thread count 4846 (vs. ulimit of 30000)
modules C: hbase-server U: hbase-server
Console output https://ci-hbase.apache.org/job/HBase-PreCommit-GitHub-PR/job/PR-7552/2/console
versions git=2.34.1 maven=3.9.8
Powered by Apache Yetus 0.15.0 https://yetus.apache.org

This message was automatically generated.

Apache-HBase avatar Dec 30 '25 23:12 Apache-HBase

@Apache9 I have updated the description with all the details now. Kindly take a look please. In short, NPE was caused by a race where a store file reader could be closed before DateTieredCompaction finished using it and we are trying to fix the same here.

SwaraliJoshi avatar Dec 31 '25 04:12 SwaraliJoshi

Thanks for providing the details, but it is still a bit strange to me that why we close a store file reader and then we still try to select for compaction?

And seems the code changes are mostly in addCompactionResult, in general, we will only close a store file reader after it is compacted away, and there should be no overlap on store files between different compactions, then why do we want to close a store file while it is stil referenced in a compaction?

Thanks.

Apache9 avatar Jan 01 '26 15:01 Apache9

@Apache9 My sincere apologies for delayed response. Here are the details :

but it is still a bit strange to me that why we close a store file reader and then we still try to select for compaction?

So here we are actually seeing that the file is sent for compaction (DateTieredCompaction) and simultaneously when the file is marked as compacted, the reader is closed

newCompactedfiles.forEach(HStoreFile::markCompactedAway);

Here we are marking them compacted while a parallel thread is marking all the marked files closed. Whereas in the next step we have :

compactedfiles = ImmutableList.sortedCopyOf(storeFileComparator, Iterables.concat(compactedfiles, newCompactedfiles));

Here the sorting happens based on timestamp (reader.getMaximumTimestamp()) where we are seeing the NPE.

The root cause of the NPE is a race condition between store file reader closure and compaction bookkeeping. During compaction, the system performs two logically separate steps:

  • Compaction bookkeeping The StoreFileManager (e.g. DefaultStoreFileManager) removes compacted store files from its internal active file set via addCompactionResults(...).

  • Reader cleanup The compacted HStoreFile’s reader is eventually closed (via closeStoreFile(...)), either synchronously or asynchronously.

Before this fix, these two steps were not strictly ordered. As a result, it was possible for a store file’s reader to be closed while the file was still visible to compaction logic.

SwaraliJoshi avatar Apr 15 '26 05:04 SwaraliJoshi

@Apache9 Kindly requesting you to please review this PR. I would be more than happy to connect to discuss about the fix further 😃

SwaraliJoshi avatar Apr 27 '26 12:04 SwaraliJoshi

@Apache9 Gentle reminder for review on this PR

SwaraliJoshi avatar May 14 '26 07:05 SwaraliJoshi

@Apache9 Kind reminder for review. Please help here.

SwaraliJoshi avatar May 24 '26 23:05 SwaraliJoshi

@Apache9 My sincere apologies for delayed response. Here are the details :

but it is still a bit strange to me that why we close a store file reader and then we still try to select for compaction?

So here we are actually seeing that the file is sent for compaction (DateTieredCompaction) and simultaneously when the file is marked as compacted, the reader is closed

newCompactedfiles.forEach(HStoreFile::markCompactedAway);

Here we are marking them compacted while a parallel thread is marking all the marked files closed. Whereas in the next step we have :

compactedfiles = ImmutableList.sortedCopyOf(storeFileComparator, Iterables.concat(compactedfiles, newCompactedfiles));

Here the sorting happens based on timestamp (reader.getMaximumTimestamp()) where we are seeing the NPE.

The root cause of the NPE is a race condition between store file reader closure and compaction bookkeeping. During compaction, the system performs two logically separate steps:

  • Compaction bookkeeping The StoreFileManager (e.g. DefaultStoreFileManager) removes compacted store files from its internal active file set via addCompactionResults(...).
  • Reader cleanup The compacted HStoreFile’s reader is eventually closed (via closeStoreFile(...)), either synchronously or asynchronously.

Before this fix, these two steps were not strictly ordered. As a result, it was possible for a store file’s reader to be closed while the file was still visible to compaction logic.

This only affects DateTieredCompaction or is a general problem for all compaction algorithm?

Apache9 avatar May 25 '26 01:05 Apache9

This only affects DateTieredCompaction or is a general problem for all compaction algorithm?

@Apache9 This only happens for DateTieredCompaction as it uses getMaximumTimestamp(). Other compactions are not affected and would not face this issue.

SwaraliJoshi avatar Jun 04 '26 06:06 SwaraliJoshi

Screenshot 2026-06-06 at 5 09 21 AM

@Apache9 This is the flow of how this issue is coming into being. I have also updated the description to have more detailed explanation.

I thought about what you said and went back to the code to keep the fix as precise to our JIRA while minimum change to the design. Instead of now opening reader and closing it just to get the timestamp, I have instead added a safety check in the comparator itself. This will be much faster and fail-safe as well, without having any impact on other flows (I have confirmed the use cases of this method). I request you to kindly re-review this PR.

SwaraliJoshi avatar Jun 06 '26 00:06 SwaraliJoshi

@Apache9 Kind reminder to please review the PR

SwaraliJoshi avatar Jun 22 '26 00:06 SwaraliJoshi

@Apache9 Kind reminder to please review the updated PR

SwaraliJoshi avatar Jul 16 '26 06:07 SwaraliJoshi

@Apache9 Gentle reminder to please review the latest changes in this PR

SwaraliJoshi avatar Aug 03 '26 10:08 SwaraliJoshi