HBASE-29348 Fixed NPE issue due to HStoreFile getting closed
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:
- Discharger thread (
CompactedHFilesDischarger→HStore.closeAndArchiveCompactedFiles→removeCompactedfiles): while archiving a compacted file it callsstoreFileTracker.removeStoreFiles(...)(HStore#removeCompactedfiles) →HFileArchiver→FileableStoreFile.moveAndClose→HStoreFile.closeStoreFile, which setsinitialReader = null. This runs underarchiveLockonly (not understoreEngine.writeLock), and it happens beforeclearCompactedfilesremoves the file from thecompactedfileslist. So there is a window where the file is still present incompactedfilesbut its reader is alreadynull. - Compaction/selection thread (
HStore.compact/HStore.removeUnneededFiles→replaceStoreFiles→StoreEngine.replaceStoreFiles→DefaultStoreFileManager.addCompactionResults): runs understoreEngine.writeLockand sortsImmutableList.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.GetMaxTimestampnow returns a default timestamp whensf.getReader() == nullinstead of dereferencing it. This removes the NPE from the sort regardless of timing, which in turn prevents thefilesCompactingleak 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.moveAndCloseis 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.addCompactionResultspath 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.moveAndCloseverify the reader is closed on a successful archive and, critically, stays open when the rename fails.
: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.
: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.
Seems to be a import issue in the patch. @SwaraliJoshi
Mind explaining more about the fix? What is the root cause of the NPE and why it only affect DateTieredCompaction?
Thanks.
: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.
: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.
@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.
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 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.
@Apache9 Kindly requesting you to please review this PR. I would be more than happy to connect to discuss about the fix further 😃
@Apache9 Gentle reminder for review on this PR
@Apache9 Kind reminder for review. Please help here.
@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?
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.
@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.
@Apache9 Kind reminder to please review the PR
@Apache9 Kind reminder to please review the updated PR
@Apache9 Gentle reminder to please review the latest changes in this PR