Skip to content

Commit 688bbae

Browse files
authored
RATIS-2271 Leadership Loss Causes ClosedByInterruptException and NullPointerException in LogAppender Thread (#1245)
1 parent 0fd4b3a commit 688bbae

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,11 @@ ReferenceCountedObject<LogEntryProto> load(TermIndex key) throws IOException {
292292
}
293293
});
294294
loadingTimes.incrementAndGet();
295-
return Objects.requireNonNull(toReturn.get(), () -> "toReturn == null for " + key);
295+
final ReferenceCountedObject<LogEntryProto> proto = toReturn.get();
296+
if (proto == null) {
297+
throw new RaftLogIOException("Failed to load log entry " + key);
298+
}
299+
return proto;
296300
}
297301
}
298302

@@ -502,8 +506,10 @@ synchronized ReferenceCountedObject<LogEntryProto> loadCache(TermIndex ti) throw
502506
}
503507
try {
504508
return cacheLoader.load(ti);
509+
} catch (RaftLogIOException e) {
510+
throw e;
505511
} catch (Exception e) {
506-
throw new RaftLogIOException(e);
512+
throw new RaftLogIOException("Failed to loadCache for log entry " + ti, e);
507513
}
508514
}
509515

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

Lines changed: 6 additions & 1 deletion
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;
@@ -104,7 +105,11 @@ public LogEntryProto nextEntry() throws IOException {
104105
try {
105106
init();
106107
} catch (Exception e) {
107-
LOG.error("caught exception initializing " + this, e);
108+
if (e.getCause() instanceof ClosedByInterruptException) {
109+
LOG.warn("Initialization is interrupted: {}", this, e);
110+
} else {
111+
LOG.error("Failed to initialize {}", 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: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import java.io.FilterInputStream;
4343
import java.io.IOException;
4444
import java.io.InputStream;
45+
import java.nio.channels.ClosedByInterruptException;
4546
import java.util.Optional;
4647
import java.util.zip.Checksum;
4748

@@ -169,7 +170,14 @@ public long skip(long amt) throws IOException {
169170
*/
170171
boolean verifyHeader() throws IOException {
171172
final int headerLength = SegmentedRaftLogFormat.getHeaderLength();
172-
final int readLength = in.read(temp, 0, headerLength);
173+
final int readLength;
174+
try{
175+
readLength = in.read(temp, 0, headerLength);
176+
} catch (ClosedByInterruptException e) {
177+
Thread.currentThread().interrupt();
178+
throw new IOException("Interrupted while reading the header of " + file, e);
179+
}
180+
173181
Preconditions.assertTrue(readLength <= headerLength);
174182
final int matchLength = SegmentedRaftLogFormat.matchHeader(temp, 0, readLength);
175183
Preconditions.assertTrue(matchLength <= readLength);

0 commit comments

Comments
 (0)