Skip to content

Commit 9b8236e

Browse files
committed
avoid NullPointerException and change the log level of ClosedByInterruptException from ERROR to WARN
1 parent 0fd4b3a commit 9b8236e

3 files changed

Lines changed: 31 additions & 27 deletions

File tree

ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/LogSegment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ ReferenceCountedObject<LogEntryProto> load(TermIndex key) throws IOException {
292292
}
293293
});
294294
loadingTimes.incrementAndGet();
295-
return Objects.requireNonNull(toReturn.get(), () -> "toReturn == null for " + key);
295+
return toReturn.get();
296296
}
297297
}
298298

ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/SegmentedRaftLogInputStream.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.EOFException;
2222
import java.io.File;
2323
import java.io.IOException;
24+
import java.nio.channels.ClosedByInterruptException;
2425
import java.util.Optional;
2526

2627
import org.apache.ratis.proto.RaftProtos.LogEntryProto;
@@ -103,8 +104,12 @@ public LogEntryProto nextEntry() throws IOException {
103104
if (state.isUnopened()) {
104105
try {
105106
init();
106-
} catch (Exception e) {
107-
LOG.error("caught exception initializing " + this, e);
107+
} catch (IOException e) {
108+
if (e.getCause() instanceof ClosedByInterruptException) {
109+
LOG.warn("Initialization is interrupted: {}", this, e);
110+
} else {
111+
LOG.error("caught exception initializing " + this, e);
112+
}
108113
throw IOUtils.asIOException(e);
109114
}
110115
}

ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/SegmentedRaftLogReader.java

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,8 @@
3434
import org.slf4j.Logger;
3535
import org.slf4j.LoggerFactory;
3636

37-
import java.io.BufferedInputStream;
38-
import java.io.Closeable;
39-
import java.io.DataInputStream;
40-
import java.io.EOFException;
41-
import java.io.File;
42-
import java.io.FilterInputStream;
43-
import java.io.IOException;
44-
import java.io.InputStream;
37+
import java.io.*;
38+
import java.nio.channels.ClosedByInterruptException;
4539
import java.util.Optional;
4640
import java.util.zip.Checksum;
4741

@@ -169,23 +163,28 @@ public long skip(long amt) throws IOException {
169163
*/
170164
boolean verifyHeader() throws IOException {
171165
final int headerLength = SegmentedRaftLogFormat.getHeaderLength();
172-
final int readLength = in.read(temp, 0, headerLength);
173-
Preconditions.assertTrue(readLength <= headerLength);
174-
final int matchLength = SegmentedRaftLogFormat.matchHeader(temp, 0, readLength);
175-
Preconditions.assertTrue(matchLength <= readLength);
176-
177-
if (readLength == headerLength && matchLength == readLength) {
178-
// The header is matched successfully
179-
return true;
180-
} else if (SegmentedRaftLogFormat.isTerminator(temp, matchLength, readLength - matchLength)) {
181-
// The header is partially written
182-
return false;
166+
try{
167+
final int readLength = in.read(temp, 0, headerLength);
168+
Preconditions.assertTrue(readLength <= headerLength);
169+
final int matchLength = SegmentedRaftLogFormat.matchHeader(temp, 0, readLength);
170+
Preconditions.assertTrue(matchLength <= readLength);
171+
172+
if (readLength == headerLength && matchLength == readLength) {
173+
// The header is matched successfully
174+
return true;
175+
} else if (SegmentedRaftLogFormat.isTerminator(temp, matchLength, readLength - matchLength)) {
176+
// The header is partially written
177+
return false;
178+
}
179+
// The header is corrupted
180+
throw new CorruptedFileException(file, "Log header mismatched: expected header length="
181+
+ SegmentedRaftLogFormat.getHeaderLength() + ", read length=" + readLength + ", match length=" + matchLength
182+
+ ", header in file=" + StringUtils.bytes2HexString(temp, 0, readLength)
183+
+ ", expected header=" + StringUtils.bytes2HexString(SegmentedRaftLogFormat.getHeaderBytebuffer()));
184+
} catch (ClosedByInterruptException e) {
185+
Thread.currentThread().interrupt();
186+
throw new IOException("Interrupted while reading the header of " + file, e);
183187
}
184-
// The header is corrupted
185-
throw new CorruptedFileException(file, "Log header mismatched: expected header length="
186-
+ SegmentedRaftLogFormat.getHeaderLength() + ", read length=" + readLength + ", match length=" + matchLength
187-
+ ", header in file=" + StringUtils.bytes2HexString(temp, 0, readLength)
188-
+ ", expected header=" + StringUtils.bytes2HexString(SegmentedRaftLogFormat.getHeaderBytebuffer()));
189188
}
190189

191190
/**

0 commit comments

Comments
 (0)