From c362df101d648a8c9131d411c60eb50e3951a6a0 Mon Sep 17 00:00:00 2001 From: vate <806019582@qq.com> Date: Wed, 17 Jan 2024 11:54:19 +0800 Subject: [PATCH] style: optmize or simplify some code. --- .../storage/dledger/DLedgerConfig.java | 2 +- .../storage/dledger/AppendAndGetTest.java | 1 - .../storage/dledger/AppendAndPushTest.java | 18 ++++++++---------- .../storage/dledger/BatchPushTest.java | 15 +++++++-------- .../storage/dledger/CommitIndexTest.java | 16 ++++------------ .../storage/dledger/LeaderElectorTest.java | 11 ++--------- .../storage/dledger/ServerTestHarness.java | 8 ++++---- .../storage/dledger/WriteTest.java | 8 ++++---- .../dledger/entry/DLedgerEntryCoderTest.java | 1 - .../dledger/statemachine/MockSnapshotFile.java | 6 +++--- .../statemachine/RegisterSnapshotFile.java | 2 +- 11 files changed, 34 insertions(+), 54 deletions(-) diff --git a/dledger/src/main/java/io/openmessaging/storage/dledger/DLedgerConfig.java b/dledger/src/main/java/io/openmessaging/storage/dledger/DLedgerConfig.java index e53e11fb..0bb539b6 100644 --- a/dledger/src/main/java/io/openmessaging/storage/dledger/DLedgerConfig.java +++ b/dledger/src/main/java/io/openmessaging/storage/dledger/DLedgerConfig.java @@ -463,7 +463,7 @@ public void setReadOnlyDataStoreDirs(String readOnlyDataStoreDirs) { private String selfAddress; - // groupId#selfIf -> address + // groupId#selfId -> address private Map peerAddressMap; private final AtomicBoolean inited = new AtomicBoolean(false); diff --git a/dledger/src/test/java/io/openmessaging/storage/dledger/AppendAndGetTest.java b/dledger/src/test/java/io/openmessaging/storage/dledger/AppendAndGetTest.java index 76345ea4..d43d7480 100644 --- a/dledger/src/test/java/io/openmessaging/storage/dledger/AppendAndGetTest.java +++ b/dledger/src/test/java/io/openmessaging/storage/dledger/AppendAndGetTest.java @@ -23,7 +23,6 @@ import io.openmessaging.storage.dledger.protocol.DLedgerResponseCode; import io.openmessaging.storage.dledger.protocol.GetEntriesResponse; import io.openmessaging.storage.dledger.util.FileTestUtil; -import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.UUID; diff --git a/dledger/src/test/java/io/openmessaging/storage/dledger/AppendAndPushTest.java b/dledger/src/test/java/io/openmessaging/storage/dledger/AppendAndPushTest.java index f35c44c6..f0ccd532 100644 --- a/dledger/src/test/java/io/openmessaging/storage/dledger/AppendAndPushTest.java +++ b/dledger/src/test/java/io/openmessaging/storage/dledger/AppendAndPushTest.java @@ -34,7 +34,6 @@ import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; -import org.awaitility.core.AssertionCondition; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.mockito.Mockito; @@ -59,7 +58,7 @@ public void testPushCommittedIndex() throws Exception { appendEntryRequest.setRemoteId(dLedgerServer0.getMemberState().getSelfId()); appendEntryRequest.setBody(new byte[256]); CompletableFuture future = dLedgerServer0.handleAppend(appendEntryRequest); - Assertions.assertTrue(future instanceof AppendFuture); + Assertions.assertInstanceOf(AppendFuture.class, future); futures.add(future); } Assertions.assertEquals(9, dLedgerServer0.getDLedgerStore().getLedgerEndIndex()); @@ -86,13 +85,12 @@ public void testPushNetworkOffline() throws Exception { appendEntryRequest.setRemoteId(dLedgerServer0.getMemberState().getSelfId()); appendEntryRequest.setBody(new byte[128]); CompletableFuture future = dLedgerServer0.handleAppend(appendEntryRequest); - Assertions.assertTrue(future instanceof AppendFuture); + Assertions.assertInstanceOf(AppendFuture.class, future); futures.add(future); } Assertions.assertEquals(9, dLedgerServer0.getDLedgerStore().getLedgerEndIndex()); Thread.sleep(dLedgerServer0.getDLedgerConfig().getMaxWaitAckTimeMs() + 1000); - for (int i = 0; i < futures.size(); i++) { - CompletableFuture future = futures.get(i); + for (CompletableFuture future : futures) { Assertions.assertTrue(future.isDone()); Assertions.assertEquals(DLedgerResponseCode.WAIT_QUORUM_ACK_TIMEOUT.getCode(), future.get().getCode()); } @@ -104,7 +102,7 @@ public void testPushNetworkOffline() throws Exception { appendEntryRequest.setRemoteId(dLedgerServer0.getMemberState().getSelfId()); appendEntryRequest.setBody(new byte[128]); CompletableFuture future = dLedgerServer0.handleAppend(appendEntryRequest); - Assertions.assertTrue(future instanceof AppendFuture); + Assertions.assertInstanceOf(AppendFuture.class, future); if (future.isDone()) { Assertions.assertEquals(DLedgerResponseCode.LEADER_PENDING_FULL.getCode(), future.get().getCode()); hasWait = true; @@ -126,12 +124,12 @@ public void testPushNetworkNotStable() throws Exception { appendEntryRequest.setRemoteId(dLedgerServer0.getMemberState().getSelfId()); appendEntryRequest.setBody(new byte[128]); CompletableFuture future = dLedgerServer0.handleAppend(appendEntryRequest); - Assertions.assertTrue(future instanceof AppendFuture); + Assertions.assertInstanceOf(AppendFuture.class, future); future.whenComplete((x, ex) -> { sendSuccess.set(true); }); Thread.sleep(500); - Assertions.assertTrue(!sendSuccess.get()); + Assertions.assertFalse(sendSuccess.get()); //start server1 DLedgerServer dLedgerServer1 = launchServer(group, peers, "n1", "n0", DLedgerConfig.FILE); Thread.sleep(1500); @@ -140,12 +138,12 @@ public void testPushNetworkNotStable() throws Exception { dLedgerServer1.shutdown(); sendSuccess.set(false); future = dLedgerServer0.handleAppend(appendEntryRequest); - Assertions.assertTrue(future instanceof AppendFuture); + Assertions.assertInstanceOf(AppendFuture.class, future); future.whenComplete((x, ex) -> { sendSuccess.set(true); }); Thread.sleep(500); - Assertions.assertTrue(!sendSuccess.get()); + Assertions.assertFalse(sendSuccess.get()); //restart server1 dLedgerServer1 = launchServer(group, peers, "n1", "n0", DLedgerConfig.FILE); Thread.sleep(1500); diff --git a/dledger/src/test/java/io/openmessaging/storage/dledger/BatchPushTest.java b/dledger/src/test/java/io/openmessaging/storage/dledger/BatchPushTest.java index 07f7a55e..695f762b 100644 --- a/dledger/src/test/java/io/openmessaging/storage/dledger/BatchPushTest.java +++ b/dledger/src/test/java/io/openmessaging/storage/dledger/BatchPushTest.java @@ -122,13 +122,12 @@ public void testBatchPushNetworkOffline() throws Exception { appendEntryRequest.setRemoteId(dLedgerServer0.getMemberState().getSelfId()); appendEntryRequest.setBody(new byte[128]); CompletableFuture future = dLedgerServer0.handleAppend(appendEntryRequest); - Assertions.assertTrue(future instanceof AppendFuture); + Assertions.assertInstanceOf(AppendFuture.class, future); futures.add(future); } Assertions.assertEquals(9, dLedgerServer0.getDLedgerStore().getLedgerEndIndex()); Thread.sleep(dLedgerServer0.getDLedgerConfig().getMaxWaitAckTimeMs() + 1000); - for (int i = 0; i < futures.size(); i++) { - CompletableFuture future = futures.get(i); + for (CompletableFuture future : futures) { Assertions.assertTrue(future.isDone()); Assertions.assertEquals(DLedgerResponseCode.WAIT_QUORUM_ACK_TIMEOUT.getCode(), future.get().getCode()); } @@ -140,7 +139,7 @@ public void testBatchPushNetworkOffline() throws Exception { appendEntryRequest.setRemoteId(dLedgerServer0.getMemberState().getSelfId()); appendEntryRequest.setBody(new byte[128]); CompletableFuture future = dLedgerServer0.handleAppend(appendEntryRequest); - Assertions.assertTrue(future instanceof AppendFuture); + Assertions.assertInstanceOf(AppendFuture.class, future); if (future.isDone()) { Assertions.assertEquals(DLedgerResponseCode.LEADER_PENDING_FULL.getCode(), future.get().getCode()); hasWait = true; @@ -162,12 +161,12 @@ public void testBatchPushNetworkNotStable() throws Exception { appendEntryRequest.setRemoteId(dLedgerServer0.getMemberState().getSelfId()); appendEntryRequest.setBody(new byte[128]); CompletableFuture future = dLedgerServer0.handleAppend(appendEntryRequest); - Assertions.assertTrue(future instanceof AppendFuture); + Assertions.assertInstanceOf(AppendFuture.class, future); future.whenComplete((x, ex) -> { sendSuccess.set(true); }); Thread.sleep(500); - Assertions.assertTrue(!sendSuccess.get()); + Assertions.assertFalse(sendSuccess.get()); //start server1 DLedgerServer dLedgerServer1 = launchServerEnableBatchPush(group, peers, "n1", "n0", DLedgerConfig.FILE); Thread.sleep(1500); @@ -176,12 +175,12 @@ public void testBatchPushNetworkNotStable() throws Exception { dLedgerServer1.shutdown(); sendSuccess.set(false); future = dLedgerServer0.handleAppend(appendEntryRequest); - Assertions.assertTrue(future instanceof AppendFuture); + Assertions.assertInstanceOf(AppendFuture.class, future); future.whenComplete((x, ex) -> { sendSuccess.set(true); }); Thread.sleep(500); - Assertions.assertTrue(!sendSuccess.get()); + Assertions.assertFalse(sendSuccess.get()); //restart server1 dLedgerServer1 = launchServerEnableBatchPush(group, peers, "n1", "n0", DLedgerConfig.FILE); Thread.sleep(1500); diff --git a/dledger/src/test/java/io/openmessaging/storage/dledger/CommitIndexTest.java b/dledger/src/test/java/io/openmessaging/storage/dledger/CommitIndexTest.java index 1ac09eb9..4198f2ae 100644 --- a/dledger/src/test/java/io/openmessaging/storage/dledger/CommitIndexTest.java +++ b/dledger/src/test/java/io/openmessaging/storage/dledger/CommitIndexTest.java @@ -49,9 +49,7 @@ public void testDisableFastAdvanceCommitIndex() throws Exception { DLedgerServer server0 = launchServer(group, peers, "n0", "n0"); DLedgerServer server1 = launchServer(group, peers, "n1", "n0"); DLedgerServer server2 = launchServer(group, peers, "n2", "n0"); - await().atMost(6, TimeUnit.SECONDS).pollInterval(300, TimeUnit.MILLISECONDS).until(() -> { - return server0.isLeader(); - }); + await().atMost(6, TimeUnit.SECONDS).pollInterval(300, TimeUnit.MILLISECONDS).until(server0::isLeader); // at beginning, the commit index is -1 Assertions.assertEquals(-1, server0.getMemberState().getCommittedIndex()); Assertions.assertEquals(-1, server1.getMemberState().getCommittedIndex()); @@ -133,9 +131,7 @@ public void testDisableFastAdvanceCommitIndex() throws Exception { // now restart n0 DLedgerServer newServer0 = launchServer(group, peers, "n0", "n0"); - await().atMost(6, TimeUnit.SECONDS).pollInterval(300, TimeUnit.MILLISECONDS).until(() -> { - return newServer0.isLeader(); - }); + await().atMost(6, TimeUnit.SECONDS).pollInterval(300, TimeUnit.MILLISECONDS).until(newServer0::isLeader); Assertions.assertEquals(1, newServer0.getMemberState().getCommittedIndex()); } @@ -146,9 +142,7 @@ public void testEnableFastAdvanceCommitIndex() throws Exception { DLedgerServer server0 = launchServer(group, peers, "n0", "n0", true); DLedgerServer server1 = launchServer(group, peers, "n1", "n0", true); DLedgerServer server2 = launchServer(group, peers, "n2", "n0", true); - await().atMost(6, TimeUnit.SECONDS).pollInterval(300, TimeUnit.MILLISECONDS).until(() -> { - return server0.isLeader(); - }); + await().atMost(6, TimeUnit.SECONDS).pollInterval(300, TimeUnit.MILLISECONDS).until(server0::isLeader); // at beginning, the commit index is -1 Assertions.assertEquals(-1, server0.getMemberState().getCommittedIndex()); Assertions.assertEquals(-1, server1.getMemberState().getCommittedIndex()); @@ -232,9 +226,7 @@ public void testEnableFastAdvanceCommitIndex() throws Exception { // why not 3? because when leader change, n0 hava already advanced the commit index to 2 (commit index is not stale at that time), // so it is meaningless to append one more no-op entry to advance the commit index DLedgerServer newServer0 = launchServer(group, peers, "n0", "n0", true); - await().atMost(6, TimeUnit.SECONDS).pollInterval(300, TimeUnit.MILLISECONDS).until(() -> { - return newServer0.isLeader(); - }); + await().atMost(6, TimeUnit.SECONDS).pollInterval(300, TimeUnit.MILLISECONDS).until(newServer0::isLeader); Assertions.assertEquals(2, newServer0.getMemberState().getCommittedIndex()); } } diff --git a/dledger/src/test/java/io/openmessaging/storage/dledger/LeaderElectorTest.java b/dledger/src/test/java/io/openmessaging/storage/dledger/LeaderElectorTest.java index 0b9d8a41..c4c32489 100644 --- a/dledger/src/test/java/io/openmessaging/storage/dledger/LeaderElectorTest.java +++ b/dledger/src/test/java/io/openmessaging/storage/dledger/LeaderElectorTest.java @@ -25,6 +25,7 @@ import org.junit.jupiter.api.Test; import java.util.ArrayList; +import java.util.Comparator; import java.util.List; import java.util.UUID; import java.util.concurrent.atomic.AtomicInteger; @@ -80,15 +81,7 @@ public void testThreeServer() throws Exception { Assertions.assertNotNull(leaderServer); for (int i = 0; i < 10; i++) { - long maxTerm = servers.stream().max((o1, o2) -> { - if (o1.getMemberState().currTerm() < o2.getMemberState().currTerm()) { - return -1; - } else if (o1.getMemberState().currTerm() > o2.getMemberState().currTerm()) { - return 1; - } else { - return 0; - } - }).get().getMemberState().currTerm(); + long maxTerm = servers.stream().max(Comparator.comparingLong(o -> o.getMemberState().currTerm())).get().getMemberState().currTerm(); DLedgerServer candidate = servers.get(i % servers.size()); candidate.getDLedgerLeaderElector().testRevote(maxTerm + 1); Thread.sleep(2000); diff --git a/dledger/src/test/java/io/openmessaging/storage/dledger/ServerTestHarness.java b/dledger/src/test/java/io/openmessaging/storage/dledger/ServerTestHarness.java index c5a6879a..3d39659c 100644 --- a/dledger/src/test/java/io/openmessaging/storage/dledger/ServerTestHarness.java +++ b/dledger/src/test/java/io/openmessaging/storage/dledger/ServerTestHarness.java @@ -44,13 +44,13 @@ protected synchronized DLedgerServer launchServer(String group, String peers, St } protected synchronized DLedgerServer launchServer(String group, String peers, String selfId, - String preferredLeaderId, boolean enbaleFastAdvanceCommitIndex) { + String preferredLeaderId, boolean enableFastAdvanceCommitIndex) { DLedgerConfig config = new DLedgerConfig(); config.setStoreBaseDir(getBaseDir() + File.separator + group); config.group(group).selfId(selfId).peers(peers); config.setStoreType(DLedgerConfig.FILE); config.setPreferredLeaderId(preferredLeaderId); - config.setEnableFastAdvanceCommitIndex(enbaleFastAdvanceCommitIndex); + config.setEnableFastAdvanceCommitIndex(enableFastAdvanceCommitIndex); DLedgerServer dLedgerServer = new DLedgerServer(config); dLedgerServer.startup(); bases.add(config.getDefaultPath()); @@ -85,8 +85,8 @@ protected synchronized DLedgerServer launchServer(String group, String peers, St protected DLedgerServer launchServerWithStateMachineDisableSnapshot(String group, String peers, - String selfIf, String leaderId, String storeType, int mappedFileSizeForEntryData, StateMachine stateMachine) { - return this.launchServerWithStateMachine(group, peers, selfIf, leaderId, storeType, false, 0, + String selfId, String leaderId, String storeType, int mappedFileSizeForEntryData, StateMachine stateMachine) { + return this.launchServerWithStateMachine(group, peers, selfId, leaderId, storeType, false, 0, mappedFileSizeForEntryData, stateMachine); } diff --git a/dledger/src/test/java/io/openmessaging/storage/dledger/WriteTest.java b/dledger/src/test/java/io/openmessaging/storage/dledger/WriteTest.java index 0506ca6d..37d89da3 100644 --- a/dledger/src/test/java/io/openmessaging/storage/dledger/WriteTest.java +++ b/dledger/src/test/java/io/openmessaging/storage/dledger/WriteTest.java @@ -77,7 +77,7 @@ public void done(Status status) { try { latch.await(); } catch (InterruptedException e) { - Assertions.assertTrue(false); + Assertions.fail(); } } @@ -110,7 +110,7 @@ public void done(Status status) { try { latch.await(); } catch (InterruptedException e) { - Assertions.assertTrue(false); + Assertions.fail(); } } @@ -138,7 +138,7 @@ public void done(Status status) { try { latch.await(); } catch (InterruptedException e) { - Assertions.assertTrue(false); + Assertions.fail(); } } @@ -171,7 +171,7 @@ public void done(Status status) { try { latch.await(); } catch (InterruptedException e) { - Assertions.assertTrue(false); + Assertions.fail(); } } diff --git a/dledger/src/test/java/io/openmessaging/storage/dledger/entry/DLedgerEntryCoderTest.java b/dledger/src/test/java/io/openmessaging/storage/dledger/entry/DLedgerEntryCoderTest.java index a72315f8..6ded650c 100644 --- a/dledger/src/test/java/io/openmessaging/storage/dledger/entry/DLedgerEntryCoderTest.java +++ b/dledger/src/test/java/io/openmessaging/storage/dledger/entry/DLedgerEntryCoderTest.java @@ -69,7 +69,6 @@ public void testEncodeDecode() { public void testCompareEntry() { DLedgerEntry entry = new DLedgerEntry(); DLedgerEntry other = new DLedgerEntry(); - Assertions.assertTrue(!entry.equals(null)); Assertions.assertEquals(entry, other); Assertions.assertEquals(other, entry); Assertions.assertEquals(other.hashCode(), entry.hashCode()); diff --git a/dledger/src/test/java/io/openmessaging/storage/dledger/statemachine/MockSnapshotFile.java b/dledger/src/test/java/io/openmessaging/storage/dledger/statemachine/MockSnapshotFile.java index fbf3c894..3c2769fa 100644 --- a/dledger/src/test/java/io/openmessaging/storage/dledger/statemachine/MockSnapshotFile.java +++ b/dledger/src/test/java/io/openmessaging/storage/dledger/statemachine/MockSnapshotFile.java @@ -9,7 +9,7 @@ public class MockSnapshotFile { - private static Logger logger = LoggerFactory.getLogger(MockSnapshotFile.class); + private static final Logger logger = LoggerFactory.getLogger(MockSnapshotFile.class); private final String snapshotStorePath; @@ -29,10 +29,10 @@ public boolean save(final long value) { public long load() throws IOException { String str = IOUtils.file2String(new File(snapshotStorePath)); - if (str != null && str.length() != 0) { + if (str != null && !str.isEmpty()) { return Long.parseLong(str); } else { throw new IOException("Unable to load snapshot data from " + snapshotStorePath); } } -} \ No newline at end of file +} diff --git a/dledger/src/test/java/io/openmessaging/storage/dledger/statemachine/RegisterSnapshotFile.java b/dledger/src/test/java/io/openmessaging/storage/dledger/statemachine/RegisterSnapshotFile.java index 638e7a17..0bb241f7 100644 --- a/dledger/src/test/java/io/openmessaging/storage/dledger/statemachine/RegisterSnapshotFile.java +++ b/dledger/src/test/java/io/openmessaging/storage/dledger/statemachine/RegisterSnapshotFile.java @@ -25,7 +25,7 @@ public class RegisterSnapshotFile { - private static Logger logger = LoggerFactory.getLogger(RegisterSnapshotFile.class); + private static final Logger logger = LoggerFactory.getLogger(RegisterSnapshotFile.class); private final String snapshotStorePath;