Skip to content

Commit eb465d5

Browse files
RATIS-2371. Fix LeaderElection/SegmentedRaftLogReader/FileChunkReader CT_CONSTRUCTOR_THROW spotbugs (#1327)
1 parent 0443685 commit eb465d5

File tree

9 files changed

+53
-39
lines changed

9 files changed

+53
-39
lines changed

ratis-server/dev-support/findbugsExcludeFile.xml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515
limitations under the License.
1616
-->
1717
<FindBugsFilter>
18-
<Match>
19-
<Class name="org.apache.ratis.server.impl.LeaderElection"/>
20-
<Bug pattern="CT_CONSTRUCTOR_THROW"/>
21-
</Match>
2218
<Match>
2319
<Class name="org.apache.ratis.server.impl.LeaderStateImpl"/>
2420
<Bug pattern="CT_CONSTRUCTOR_THROW"/>
@@ -51,10 +47,6 @@
5147
<Class name="org.apache.ratis.server.raftlog.segmented.SegmentedRaftLogOutputStream"/>
5248
<Bug pattern="CT_CONSTRUCTOR_THROW"/>
5349
</Match>
54-
<Match>
55-
<Class name="org.apache.ratis.server.raftlog.segmented.SegmentedRaftLogReader"/>
56-
<Bug pattern="CT_CONSTRUCTOR_THROW"/>
57-
</Match>
5850
<Match>
5951
<Class name="org.apache.ratis.server.raftlog.segmented.SegmentedRaftLog$Builder"/>
6052
<Bug pattern="EI_EXPOSE_REP2"/>
@@ -67,10 +59,6 @@
6759
<Class name="org.apache.ratis.server.raftlog.segmented.SegmentedRaftLogWorker$WriteLog"/>
6860
<Bug pattern="CT_CONSTRUCTOR_THROW"/>
6961
</Match>
70-
<Match>
71-
<Class name="org.apache.ratis.server.storage.FileChunkReader"/>
72-
<Bug pattern="CT_CONSTRUCTOR_THROW"/>
73-
</Match>
7462
<Match>
7563
<Class name="org.apache.ratis.server.storage.RaftStorageImpl"/>
7664
<Bug pattern="EI_EXPOSE_REP"/>

ratis-server/src/main/java/org/apache/ratis/server/impl/LeaderElection.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
* Ongaro, D. Consensus: Bridging Theory and Practice. PhD thesis, Stanford University, 2014.
7878
* Available at https://github.com/ongardie/dissertation
7979
*/
80-
class LeaderElection implements Runnable {
80+
final class LeaderElection implements Runnable {
8181
public static final Logger LOG = LoggerFactory.getLogger(LeaderElection.class);
8282

8383
interface ServerInterface {
@@ -306,25 +306,33 @@ public String toString() {
306306
private final boolean skipPreVote;
307307
private final ConfAndTerm round0;
308308

309-
LeaderElection(RaftServerImpl server, boolean force) {
310-
this(ServerInterface.get(server), force);
309+
static LeaderElection newInstance(RaftServerImpl server, boolean force) {
310+
return newInstance(ServerInterface.get(server), force);
311311
}
312312

313-
LeaderElection(ServerInterface server, boolean force) {
314-
this.name = ServerStringUtils.generateUnifiedName(server.getMemberId(), getClass()) + COUNT.incrementAndGet();
315-
this.lifeCycle = new LifeCycle(this);
316-
this.daemon = Daemon.newBuilder().setName(name).setRunnable(this)
317-
.setThreadGroup(server.getThreadGroup()).build();
318-
this.server = server;
319-
this.skipPreVote = force || !server.isPreVoteEnabled();
313+
static LeaderElection newInstance(ServerInterface server, boolean force) {
314+
String name = ServerStringUtils.generateUnifiedName(server.getMemberId(), LeaderElection.class)
315+
+ COUNT.incrementAndGet();
320316
try {
321317
// increase term of the candidate in advance if it's forced to election
322-
this.round0 = force ? server.initElection(Phase.ELECTION) : null;
318+
final ConfAndTerm round0 = force ? server.initElection(Phase.ELECTION) : null;
319+
return new LeaderElection(name, server, force, round0);
323320
} catch (IOException e) {
324321
throw new IllegalStateException(name + ": Failed to initialize election", e);
325322
}
326323
}
327324

325+
326+
private LeaderElection(String name, ServerInterface server, boolean force, ConfAndTerm round0) {
327+
this.name = name;
328+
this.lifeCycle = new LifeCycle(this);
329+
this.daemon = Daemon.newBuilder().setName(name).setRunnable(this)
330+
.setThreadGroup(server.getThreadGroup()).build();
331+
this.server = server;
332+
this.skipPreVote = force || !server.isPreVoteEnabled();
333+
this.round0 = round0;
334+
}
335+
328336
void start() {
329337
startIfNew(daemon::start);
330338
}

ratis-server/src/main/java/org/apache/ratis/server/impl/RoleInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ void startLeaderElection(RaftServerImpl server, boolean force) {
126126
if (pauseLeaderElection.get()) {
127127
return;
128128
}
129-
updateAndGet(leaderElection, new LeaderElection(server, force)).start();
129+
updateAndGet(leaderElection, LeaderElection.newInstance(server, force)).start();
130130
}
131131

132132
void setLeaderElectionPause(boolean pause) {

ratis-server/src/main/java/org/apache/ratis/server/leader/InstallSnapshotRequests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public InstallSnapshotRequestProto next() {
119119
final FileInfo info = snapshot.getFiles().get(fileIndex);
120120
try {
121121
if (current == null) {
122-
current = new FileChunkReader(info, getRelativePath.apply(info));
122+
current = FileChunkReader.newInstance(info, getRelativePath.apply(info));
123123
}
124124
final FileChunkProto chunk = current.readFileChunk(snapshotChunkMaxSize);
125125
if (chunk.getDone()) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ private void init() throws IOException {
8383
state.open();
8484
boolean initSuccess = false;
8585
try {
86-
reader = new SegmentedRaftLogReader(logFile, maxOpSize, raftLogMetrics);
86+
reader = SegmentedRaftLogReader.newInstance(logFile, maxOpSize, raftLogMetrics);
8787
initSuccess = reader.verifyHeader();
8888
} finally {
8989
if (!initSuccess) {

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
import java.util.Optional;
4747
import java.util.zip.Checksum;
4848

49-
class SegmentedRaftLogReader implements Closeable {
49+
final class SegmentedRaftLogReader implements Closeable {
5050
static final Logger LOG = LoggerFactory.getLogger(SegmentedRaftLogReader.class);
5151
/**
5252
* InputStream wrapper that keeps track of the current stream position.
@@ -150,10 +150,18 @@ public long skip(long amt) throws IOException {
150150
private final SegmentedRaftLogMetrics raftLogMetrics;
151151
private final SizeInBytes maxOpSize;
152152

153-
SegmentedRaftLogReader(File file, SizeInBytes maxOpSize, SegmentedRaftLogMetrics raftLogMetrics) throws IOException {
153+
static SegmentedRaftLogReader newInstance(File file, SizeInBytes maxOpSize, SegmentedRaftLogMetrics raftLogMetrics)
154+
throws IOException {
155+
final LimitedInputStream limiter = new LimitedInputStream(new BufferedInputStream(FileUtils.newInputStream(file)));
156+
final DataInputStream in = new DataInputStream(limiter);
157+
return new SegmentedRaftLogReader(file, maxOpSize, raftLogMetrics, limiter, in);
158+
}
159+
160+
private SegmentedRaftLogReader(File file, SizeInBytes maxOpSize, SegmentedRaftLogMetrics raftLogMetrics,
161+
LimitedInputStream limiter, DataInputStream in) {
154162
this.file = file;
155-
this.limiter = new LimitedInputStream(new BufferedInputStream(FileUtils.newInputStream(file)));
156-
in = new DataInputStream(limiter);
163+
this.limiter = limiter;
164+
this.in = in;
157165
checksum = new PureJavaCrc32C();
158166
this.maxOpSize = maxOpSize;
159167
this.raftLogMetrics = raftLogMetrics;

ratis-server/src/main/java/org/apache/ratis/server/storage/FileChunkReader.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import java.security.MessageDigest;
3535

3636
/** Read {@link FileChunkProto}s from a file. */
37-
public class FileChunkReader implements Closeable {
37+
public final class FileChunkReader implements Closeable {
3838
private final FileInfo info;
3939
private final Path relativePath;
4040
private final InputStream in;
@@ -51,17 +51,27 @@ public class FileChunkReader implements Closeable {
5151
* @param relativePath the relative path of the file.
5252
* @throws IOException if it failed to open the file.
5353
*/
54-
public FileChunkReader(FileInfo info, Path relativePath) throws IOException {
55-
this.info = info;
56-
this.relativePath = relativePath;
54+
public static FileChunkReader newInstance(FileInfo info, Path relativePath) throws IOException {
5755
final File f = info.getPath().toFile();
56+
final InputStream in;
57+
final MessageDigest digester;
58+
5859
if (info.getFileDigest() == null) {
5960
digester = MD5FileUtil.newMD5();
60-
this.in = new DigestInputStream(FileUtils.newInputStream(f), digester);
61+
in = new DigestInputStream(FileUtils.newInputStream(f), digester);
6162
} else {
6263
digester = null;
63-
this.in = FileUtils.newInputStream(f);
64+
in = FileUtils.newInputStream(f);
6465
}
66+
67+
return new FileChunkReader(info, relativePath, in, digester);
68+
}
69+
70+
private FileChunkReader(FileInfo info, Path relativePath, InputStream in, MessageDigest digester) {
71+
this.info = info;
72+
this.relativePath = relativePath;
73+
this.in = in;
74+
this.digester = digester;
6575
}
6676

6777
static ByteString readFileChunk(int chunkLength, InputStream in) throws IOException {

ratis-server/src/test/java/org/apache/ratis/server/impl/LeaderElectionTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ public void testLeaderElectionMetrics() throws IOException, InterruptedException
592592
@Test
593593
public void testImmediatelyRevertedToFollower() {
594594
RaftServerImpl server = createMockServer(true);
595-
LeaderElection subject = new LeaderElection(server, false);
595+
LeaderElection subject = LeaderElection.newInstance(server, false);
596596

597597
try {
598598
subject.startInForeground();
@@ -606,7 +606,7 @@ public void testImmediatelyRevertedToFollower() {
606606
@Test
607607
public void testShutdownBeforeStart() {
608608
RaftServerImpl server = createMockServer(false);
609-
LeaderElection subject = new LeaderElection(server, false);
609+
LeaderElection subject = LeaderElection.newInstance(server, false);
610610

611611
try {
612612
subject.shutdown();

ratis-test/src/test/java/org/apache/ratis/server/impl/TestLeaderElectionServerInterface.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ void runTestVoterWithEmptyLog(boolean expectToPass, TermIndex... lastEntries) {
186186
for(int i = 0; i < lastEntries.length; i++) {
187187
map.put(peers.get(i).getId(), lastEntries[i]);
188188
}
189-
final LeaderElection election = new LeaderElection(newServerInterface(expectToPass, map), false);
189+
final LeaderElection election = LeaderElection.newInstance(newServerInterface(expectToPass, map), false);
190190
election.startInForeground();
191191
}
192192

0 commit comments

Comments
 (0)