diff --git a/artemis-journal/src/main/java/org/apache/activemq/artemis/core/journal/impl/Reclaimer.java b/artemis-journal/src/main/java/org/apache/activemq/artemis/core/journal/impl/Reclaimer.java index 2b028ab964..c83e2afa7f 100644 --- a/artemis-journal/src/main/java/org/apache/activemq/artemis/core/journal/impl/Reclaimer.java +++ b/artemis-journal/src/main/java/org/apache/activemq/artemis/core/journal/impl/Reclaimer.java @@ -25,7 +25,7 @@ import org.jboss.logging.Logger; * *
(Positives correspond either to adds or updates, and negatives correspond to deletes).
* - *A file Fn can be deleted if, and only if the following criteria are satisified
+ *A file Fn can be deleted if, and only if the following criteria are satisfied
* *1) All pos in a file Fn, must have corresponding neg in any file Fm where {@code m >= n}.
* @@ -36,67 +36,44 @@ public class Reclaimer { private static final Logger logger = Logger.getLogger(Reclaimer.class); - private static void trace(final String message) { - logger.trace(message); - } - public void scan(final JournalFile[] files) { for (int i = 0; i < files.length; i++) { - // First we evaluate criterion 1) - JournalFile currentFile = files[i]; - - int posCount = currentFile.getPosCount(); - - int totNeg = 0; - - if (logger.isTraceEnabled()) { - logger.trace("posCount on " + currentFile + " = " + posCount); - } - - for (int j = i; j < files.length; j++) { - if (logger.isTraceEnabled()) { - if (files[j].getNegCount(currentFile) != 0) { - logger.trace("Negative from " + files[j] + - " into " + - currentFile + - " = " + - files[j].getNegCount(currentFile)); - } - } - - totNeg += files[j].getNegCount(currentFile); - } - currentFile.setCanReclaim(true); - if (posCount <= totNeg) { - // Now we evaluate criterion 2) - - for (int j = 0; j <= i; j++) { - JournalFile file = files[j]; - - int negCount = currentFile.getNegCount(file); - - if (negCount != 0) { - if (file.isCanReclaim()) { - // Ok - } - else { - if (logger.isTraceEnabled()) { - logger.trace(currentFile + " Can't be reclaimed because " + file + " has negative values"); - } - - currentFile.setCanReclaim(false); - - break; - } - } + // First we evaluate criterion 2) + for (int j = i - 1; j >= 0; j--) { + JournalFile file = files[j]; + if (currentFile.getNegCount(file) != 0 && !file.isCanReclaim()) { + logger.tracef("%s can't be reclaimed because %s has negative values", currentFile, file); + currentFile.setCanReclaim(false); + break; } } - else { + if (!currentFile.isCanReclaim()) { + continue; // Move to next file as we already know that this file can't be reclaimed because criterion 2) + } + + // Now we evaluate criterion 1) + int negCount = 0, posCount = currentFile.getPosCount(); + logger.tracef("posCount on %s = %d", currentFile, posCount); + + for (int j = i; j < files.length && negCount < posCount; j++) { + int toNeg = files[j].getNegCount(currentFile); + negCount += toNeg; + + if (logger.isTraceEnabled() && toNeg != 0) { + logger.tracef("Negative from %s into %s = %d", files[j], currentFile, toNeg); + } + } + + if (negCount < posCount ) { + logger.tracef("%s can't be reclaimed because there are not enough negatives %d", currentFile, negCount); currentFile.setCanReclaim(false); } + else { + logger.tracef("%s can be reclaimed", currentFile); + } } } }