Skip to content

Commit 5febdaf

Browse files
committed
Introduce try-with-resources
1 parent e430cd4 commit 5febdaf

File tree

1 file changed

+50
-52
lines changed

1 file changed

+50
-52
lines changed

exist-core/src/main/java/org/exist/storage/recovery/RecoveryManager.java

Lines changed: 50 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -92,73 +92,72 @@ public boolean recover() throws LogException {
9292
// load the last log file
9393
final Path last = journalRecovery.getFile.apply(lastNum);
9494
// scan the last log file and record the last checkpoint found
95-
final JournalReader reader = new JournalReader(broker, last, lastNum);
96-
try {
97-
// try to read the last log record to see if it is a checkpoint
98-
boolean checkpointFound = false;
99-
try {
95+
try (JournalReader reader = new JournalReader(broker, last, lastNum)) {
96+
// try to read the last log record to see if it is a checkpoint
97+
boolean checkpointFound = false;
98+
try {
10099
final Loggable lastLog = reader.lastEntry();
101100
if (lastLog != null && lastLog.getLogType() == LogEntryTypes.CHECKPOINT) {
102-
final Checkpoint checkpoint = (Checkpoint) lastLog;
103-
// Found a checkpoint. To be sure it is indeed a valid checkpoint
104-
// record, we compare the LSN stored in it with the current LSN.
105-
if (checkpoint.getStoredLsn().equals(checkpoint.getLsn())) {
106-
checkpointFound = true;
107-
LOG.debug("Database is in clean state. Last checkpoint: " +
108-
checkpoint.getDateString());
109-
}
101+
final Checkpoint checkpoint = (Checkpoint) lastLog;
102+
// Found a checkpoint. To be sure it is indeed a valid checkpoint
103+
// record, we compare the LSN stored in it with the current LSN.
104+
if (checkpoint.getStoredLsn().equals(checkpoint.getLsn())) {
105+
checkpointFound = true;
106+
LOG.debug("Database is in clean state. Last checkpoint: " +
107+
checkpoint.getDateString());
108+
}
110109
}
111110
} catch (final LogException e) {
112111
LOG.info("Reading last journal log entry failed: " + e.getMessage() + ". Will scan the log...");
113112
// if an exception occurs at this point, the journal file is probably incomplete,
114113
// which indicates a db crash
115114
checkpointFound = false;
116115
}
117-
if (!checkpointFound) {
116+
if (!checkpointFound) {
118117
LOG.info("Unclean shutdown detected. Scanning journal...");
119118
broker.getBrokerPool().reportStatus("Unclean shutdown detected. Scanning log...");
120-
reader.positionFirst();
121-
final Long2ObjectMap<Loggable> txnsStarted = new Long2ObjectOpenHashMap<>();
122-
Checkpoint lastCheckpoint = null;
123-
Lsn lastLsn = Lsn.LSN_INVALID;
124-
Loggable next;
125-
try {
126-
final ProgressBar progress = new ProgressBar("Scanning journal ", FileUtils.sizeQuietly(last));
127-
while ((next = reader.nextEntry()) != null) {
119+
reader.positionFirst();
120+
final Long2ObjectMap<Loggable> txnsStarted = new Long2ObjectOpenHashMap<>();
121+
Checkpoint lastCheckpoint = null;
122+
Lsn lastLsn = Lsn.LSN_INVALID;
123+
Loggable next;
124+
try {
125+
final ProgressBar progress = new ProgressBar("Scanning journal ", FileUtils.sizeQuietly(last));
126+
while ((next = reader.nextEntry()) != null) {
128127
// LOG.debug(next.dump());
129-
progress.set(next.getLsn().getOffset());
130-
if (next.getLogType() == LogEntryTypes.TXN_START) {
131-
// new transaction starts: add it to the transactions table
132-
txnsStarted.put(next.getTransactionId(), next);
133-
} else if (next.getLogType() == LogEntryTypes.TXN_ABORT) {
134-
// transaction aborted: remove it from the transactions table
135-
txnsStarted.remove(next.getTransactionId());
136-
} else if (next.getLogType() == LogEntryTypes.CHECKPOINT) {
137-
txnsStarted.clear();
138-
lastCheckpoint = (Checkpoint) next;
139-
}
140-
lastLsn = next.getLsn();
141-
}
142-
} catch (final LogException e) {
143-
if (LOG.isDebugEnabled()) {
128+
progress.set(next.getLsn().getOffset());
129+
if (next.getLogType() == LogEntryTypes.TXN_START) {
130+
// new transaction starts: add it to the transactions table
131+
txnsStarted.put(next.getTransactionId(), next);
132+
} else if (next.getLogType() == LogEntryTypes.TXN_ABORT) {
133+
// transaction aborted: remove it from the transactions table
134+
txnsStarted.remove(next.getTransactionId());
135+
} else if (next.getLogType() == LogEntryTypes.CHECKPOINT) {
136+
txnsStarted.clear();
137+
lastCheckpoint = (Checkpoint) next;
138+
}
139+
lastLsn = next.getLsn();
140+
}
141+
} catch (final LogException e) {
142+
if (LOG.isDebugEnabled()) {
144143
LOG.debug("Caught exception while reading log", e);
145144
}
146145
LOG.warn("Last readable journal log entry lsn: " + lastLsn);
147146
}
148147

149-
// if the last checkpoint record is not the last record in the file
150-
// we need a recovery.
151-
if ((lastCheckpoint == null || !lastCheckpoint.getLsn().equals(lastLsn)) &&
152-
txnsStarted.size() > 0) {
153-
LOG.info("Dirty transactions: " + txnsStarted.size());
154-
// starting recovery: reposition the log reader to the last checkpoint
155-
if (lastCheckpoint == null)
156-
{reader.positionFirst();}
157-
else {
158-
reader.position(lastCheckpoint.getLsn());
159-
next = reader.nextEntry();
160-
}
161-
recoveryRun = true;
148+
// if the last checkpoint record is not the last record in the file
149+
// we need a recovery.
150+
if ((lastCheckpoint == null || !lastCheckpoint.getLsn().equals(lastLsn)) &&
151+
txnsStarted.size() > 0) {
152+
LOG.info("Dirty transactions: " + txnsStarted.size());
153+
// starting recovery: reposition the log reader to the last checkpoint
154+
if (lastCheckpoint == null) {
155+
reader.positionFirst();
156+
} else {
157+
reader.position(lastCheckpoint.getLsn());
158+
next = reader.nextEntry();
159+
}
160+
recoveryRun = true;
162161
try {
163162
LOG.info("Running recovery...");
164163
broker.getBrokerPool().reportStatus("Running recovery...");
@@ -192,9 +191,8 @@ public boolean recover() throws LogException {
192191
} else {
193192
LOG.info("Database is in clean state. Nothing to recover from the journal.");
194193
}
195-
}
194+
}
196195
} finally {
197-
reader.close();
198196
// remove .log files from directory even if recovery failed.
199197
// Re-applying them on a second start up attempt would definitely damage the db, so we better
200198
// delete them before user tries to launch again.

0 commit comments

Comments
 (0)