-
Notifications
You must be signed in to change notification settings - Fork 976
[bk-gc] Prevent OOM: Restrict number of entry-loggers extraction #1938
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
rdhabalia
wants to merge
1
commit into
apache:master
Choose a base branch
from
rdhabalia:gc_scan_o
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -63,6 +63,13 @@ public class GarbageCollectorThread extends SafeRunnable { | |
|
|
||
| // This is how often we want to run the Garbage Collector Thread (in milliseconds). | ||
| final long gcWaitTime; | ||
| // Max number of entry-logger files should be extracted concurrently while | ||
| // performing GC | ||
| final int maxEntryLoggersPerScan; | ||
| // flag to iterate over chunk of of entry-logger files | ||
| boolean moreEntryLoggers = true; | ||
| long lastIterationLogId = 0; | ||
| private final boolean verifyMetadataOnGc; | ||
|
|
||
| // Compaction parameters | ||
| boolean enableMinorCompaction = false; | ||
|
|
@@ -146,6 +153,9 @@ public GarbageCollectorThread(ServerConfiguration conf, | |
| this.entryLogger = ledgerStorage.getEntryLogger(); | ||
| this.ledgerStorage = ledgerStorage; | ||
| this.gcWaitTime = conf.getGcWaitTime(); | ||
|
|
||
| this.maxEntryLoggersPerScan = conf.getMaxEntryLoggersScanOnGc(); | ||
| this.verifyMetadataOnGc = conf.getVerifyMetadataOnGC(); | ||
|
|
||
| this.numActiveEntryLogs = 0; | ||
| this.totalEntryLogSize = 0L; | ||
|
|
@@ -322,55 +332,64 @@ public void runWithFlags(boolean force, boolean suspendMajor, boolean suspendMin | |
| } | ||
| // Recover and clean up previous state if using transactional compaction | ||
| compactor.cleanUpAndRecover(); | ||
|
|
||
| long startTime = System.currentTimeMillis(); | ||
| boolean isIteration = false; | ||
| do { | ||
| // Extract all of the ledger ID's that comprise all of the entry | ||
| // logs | ||
| // (except for the current new one which is still being written to). | ||
| entryLogMetaMap = extractMetaFromEntryLogs(entryLogMetaMap, isIteration); | ||
|
|
||
| // gc inactive/deleted ledgers | ||
| doGcLedgers(); | ||
|
|
||
| // gc entry logs | ||
| doGcEntryLogs(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this method iterates over all the entries in entryLogMetaMap, again calling this multiple times is not sensible. |
||
|
|
||
| if (suspendMajor) { | ||
| LOG.info("Disk almost full, suspend major compaction to slow down filling disk."); | ||
| } | ||
| if (suspendMinor) { | ||
| LOG.info("Disk full, suspend minor compaction to slow down filling disk."); | ||
| } | ||
|
|
||
| // Extract all of the ledger ID's that comprise all of the entry logs | ||
| // (except for the current new one which is still being written to). | ||
| entryLogMetaMap = extractMetaFromEntryLogs(entryLogMetaMap); | ||
|
|
||
| // gc inactive/deleted ledgers | ||
| doGcLedgers(); | ||
|
|
||
| // gc entry logs | ||
| doGcEntryLogs(); | ||
|
|
||
| if (suspendMajor) { | ||
| LOG.info("Disk almost full, suspend major compaction to slow down filling disk."); | ||
| } | ||
| if (suspendMinor) { | ||
| LOG.info("Disk full, suspend minor compaction to slow down filling disk."); | ||
| } | ||
|
|
||
| long curTime = System.currentTimeMillis(); | ||
| if (enableMajorCompaction && (!suspendMajor) | ||
| && (force || curTime - lastMajorCompactionTime > majorCompactionInterval)) { | ||
| // enter major compaction | ||
| LOG.info("Enter major compaction, suspendMajor {}", suspendMajor); | ||
| majorCompacting.set(true); | ||
| doCompactEntryLogs(majorCompactionThreshold); | ||
| lastMajorCompactionTime = System.currentTimeMillis(); | ||
| // and also move minor compaction time | ||
| lastMinorCompactionTime = lastMajorCompactionTime; | ||
| gcStats.getMajorCompactionCounter().inc(); | ||
| majorCompacting.set(false); | ||
| } else if (enableMinorCompaction && (!suspendMinor) | ||
| && (force || curTime - lastMinorCompactionTime > minorCompactionInterval)) { | ||
| // enter minor compaction | ||
| LOG.info("Enter minor compaction, suspendMinor {}", suspendMinor); | ||
| minorCompacting.set(true); | ||
| doCompactEntryLogs(minorCompactionThreshold); | ||
| lastMinorCompactionTime = System.currentTimeMillis(); | ||
| gcStats.getMinorCompactionCounter().inc(); | ||
| minorCompacting.set(false); | ||
| } | ||
| long curTime = System.currentTimeMillis(); | ||
| if (enableMajorCompaction && (!suspendMajor) | ||
| && (force || curTime - lastMajorCompactionTime > majorCompactionInterval)) { | ||
| // enter major compaction | ||
| LOG.info("Enter major compaction, suspendMajor {}", suspendMajor); | ||
| majorCompacting.set(true); | ||
| doCompactEntryLogs(majorCompactionThreshold); | ||
| lastMajorCompactionTime = System.currentTimeMillis(); | ||
| // and also move minor compaction time | ||
| lastMinorCompactionTime = lastMajorCompactionTime; | ||
| gcStats.getMajorCompactionCounter().inc(); | ||
| majorCompacting.set(false); | ||
| } else if (enableMinorCompaction && (!suspendMinor) | ||
| && (force || curTime - lastMinorCompactionTime > minorCompactionInterval)) { | ||
| // enter minor compaction | ||
| LOG.info("Enter minor compaction, suspendMinor {}", suspendMinor); | ||
| minorCompacting.set(true); | ||
| doCompactEntryLogs(minorCompactionThreshold); | ||
| lastMinorCompactionTime = System.currentTimeMillis(); | ||
| gcStats.getMinorCompactionCounter().inc(); | ||
| minorCompacting.set(false); | ||
| } | ||
|
|
||
| if (force) { | ||
| if (forceGarbageCollection.compareAndSet(true, false)) { | ||
| LOG.info("{} Set forceGarbageCollection to false after force GC to make it forceGC-able again.", Thread | ||
| .currentThread().getName()); | ||
| if (force) { | ||
| if (forceGarbageCollection.compareAndSet(true, false)) { | ||
| LOG.info("{} Set forceGarbageCollection to false after force GC to make it forceGC-able again.", | ||
| Thread.currentThread().getName()); | ||
| } | ||
| } | ||
| } | ||
| gcStats.getGcThreadRuntime().registerSuccessfulEvent( | ||
| MathUtils.nowInNano() - threadStart, TimeUnit.NANOSECONDS); | ||
| gcStats.getGcThreadRuntime().registerSuccessfulEvent(MathUtils.nowInNano() - threadStart, | ||
| TimeUnit.NANOSECONDS); | ||
| isIteration = true; | ||
| } while (moreEntryLoggers); | ||
|
|
||
| long endTime = System.currentTimeMillis(); | ||
| LOG.info("Garbage collector completed in {}", TimeUnit.MILLISECONDS.toSeconds(endTime - startTime)); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -526,21 +545,35 @@ protected void compactEntryLog(EntryLogMetadata entryLogMeta) { | |
| * Existing EntryLogs to Meta | ||
| * @throws IOException | ||
| */ | ||
| protected Map<Long, EntryLogMetadata> extractMetaFromEntryLogs(Map<Long, EntryLogMetadata> entryLogMetaMap) { | ||
| protected Map<Long, EntryLogMetadata> extractMetaFromEntryLogs(Map<Long, EntryLogMetadata> entryLogMetaMap, boolean isIteration) { | ||
| moreEntryLoggers = false; | ||
| // Extract it for every entry log except for the current one. | ||
| // Entry Log ID's are just a long value that starts at 0 and increments | ||
| // by 1 when the log fills up and we roll to a new one. | ||
| long curLogId = entryLogger.getLeastUnflushedLogId(); | ||
| boolean hasExceptionWhenScan = false; | ||
| for (long entryLogId = scannedLogId; entryLogId < curLogId; entryLogId++) { | ||
| // Comb the current entry log file if it has not already been extracted. | ||
| int entryLogFileCount = 0; | ||
| long entryLogId = isIteration ? lastIterationLogId : scannedLogId; | ||
| for (; entryLogId < curLogId; entryLogId++, entryLogFileCount++) { | ||
| if (entryLogFileCount > maxEntryLoggersPerScan && verifyMetadataOnGc) { | ||
| lastIterationLogId = entryLogId; | ||
| moreEntryLoggers = true; | ||
| LOG.debug("extraction max-entry-logger {}, next iteration starts from {}", entryLogFileCount, | ||
| maxEntryLoggersPerScan, entryLogId); | ||
| break; | ||
| } | ||
| // Comb the current entry log file if it has not already been | ||
| // extracted. | ||
| if (entryLogMetaMap.containsKey(entryLogId)) { | ||
| entryLogFileCount--; | ||
| continue; | ||
| } | ||
|
|
||
| // check whether log file exists or not | ||
| // if it doesn't exist, this log file might have been garbage collected. | ||
| // if it doesn't exist, this log file might have been garbage | ||
| // collected. | ||
| if (!entryLogger.logExists(entryLogId)) { | ||
| entryLogFileCount--; | ||
| continue; | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is very expensive operation. It iterates over all the ledger ranges in the metadata server/all the ledgers in the bookie, this shouldn't be called multiple times
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, that's correct. therefore, have provided option
maxEntryLoggersPerScan=2000. In normal case, bookie will have < 500 entryLog files so, it will not require multiple iteration but it will target issue where bookie is not able to recover with large number of entrylog files. and therefore, @merlimat suggested to persist entryLogmetadataMap into rocksDB so, I have created PR: #1949