@@ -92,73 +92,72 @@ public boolean recover() throws LogException {
92
92
// load the last log file
93
93
final Path last = journalRecovery .getFile .apply (lastNum );
94
94
// 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 {
100
99
final Loggable lastLog = reader .lastEntry ();
101
100
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
+ }
110
109
}
111
110
} catch (final LogException e ) {
112
111
LOG .info ("Reading last journal log entry failed: " + e .getMessage () + ". Will scan the log..." );
113
112
// if an exception occurs at this point, the journal file is probably incomplete,
114
113
// which indicates a db crash
115
114
checkpointFound = false ;
116
115
}
117
- if (!checkpointFound ) {
116
+ if (!checkpointFound ) {
118
117
LOG .info ("Unclean shutdown detected. Scanning journal..." );
119
118
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 ) {
128
127
// 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 ()) {
144
143
LOG .debug ("Caught exception while reading log" , e );
145
144
}
146
145
LOG .warn ("Last readable journal log entry lsn: " + lastLsn );
147
146
}
148
147
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 ;
162
161
try {
163
162
LOG .info ("Running recovery..." );
164
163
broker .getBrokerPool ().reportStatus ("Running recovery..." );
@@ -192,9 +191,8 @@ public boolean recover() throws LogException {
192
191
} else {
193
192
LOG .info ("Database is in clean state. Nothing to recover from the journal." );
194
193
}
195
- }
194
+ }
196
195
} finally {
197
- reader .close ();
198
196
// remove .log files from directory even if recovery failed.
199
197
// Re-applying them on a second start up attempt would definitely damage the db, so we better
200
198
// delete them before user tries to launch again.
0 commit comments