Copilot commented on code in PR #1630:
URL: https://github.com/apache/ratis/pull/1630#discussion_r4217743712


##########
ratis-server/src/main/java/org/apache/ratis/server/impl/RaftConfigurationImpl.java:
##########
@@ -303,6 +303,13 @@ public String toString() {
   }
 
   boolean hasNoChange(Collection<RaftPeer> newMembers, Collection<RaftPeer> 
newListeners) {
+    return hasNoChange(newMembers, newListeners, false);
+  }
+
+  /**
+   * @param checkAddresses Should a peer with the same id but different 
addresses be considered as a change?
+   */
+  boolean hasNoChange(Collection<RaftPeer> newMembers, Collection<RaftPeer> 
newListeners, boolean checkAddresses) {

Review Comment:
   The new overload introduces a boolean parameter that changes semantic 
behavior, which can be error-prone at call sites. To make misuse less likely, 
prefer an explicit API shape (e.g., two clearly named methods or an 
enum/strategy parameter) so it’s impossible to accidentally invert the meaning.



##########
ratis-server/src/test/java/org/apache/ratis/server/impl/RaftReconfigurationBaseTest.java:
##########
@@ -663,6 +666,51 @@ void runTestNoChangeRequest(CLUSTER cluster) throws 
Exception {
     }
   }
 
+  /**
+   * When a request changes only the addresses of the existing peers,
+   * make sure the new configuration is committed and persisted.
+   */
+  @Test
+  public void testAddressChangeRequest() throws Exception {
+    runWithNewCluster(3, this::runTestAddressChangeRequest);
+  }
+
+  void runTestAddressChangeRequest(CLUSTER cluster) throws Exception {
+    final RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster);
+    final long confIndex = leader.getRaftConf().getLogEntryIndex();
+
+    // the same peers with an equivalent address
+    final List<RaftPeer> newPeers = 
leader.getRaftConf().getCurrentPeers().stream()
+        .map(p -> 
RaftPeer.newBuilder(p).setAddress(p.getAddress().replace(NetUtils.LOCALHOST, 
"127.0.0.1")).build())
+        .collect(Collectors.toList());

Review Comment:
   The production change compares not only `getAddress()` but also 
`getAdminAddress()`, `getClientAddress()`, and `getDataStreamAddress()`. This 
test currently modifies only `setAddress(...)`, so regressions in the other 
address fields would not be caught. Consider extending this test (or adding a 
second case) that changes one of those other address fields to ensure the new 
comparison logic is fully covered.



##########
ratis-server/src/test/java/org/apache/ratis/server/impl/RaftReconfigurationBaseTest.java:
##########
@@ -663,6 +666,51 @@ void runTestNoChangeRequest(CLUSTER cluster) throws 
Exception {
     }
   }
 
+  /**
+   * When a request changes only the addresses of the existing peers,
+   * make sure the new configuration is committed and persisted.
+   */
+  @Test
+  public void testAddressChangeRequest() throws Exception {
+    runWithNewCluster(3, this::runTestAddressChangeRequest);
+  }
+
+  void runTestAddressChangeRequest(CLUSTER cluster) throws Exception {
+    final RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster);
+    final long confIndex = leader.getRaftConf().getLogEntryIndex();
+
+    // the same peers with an equivalent address
+    final List<RaftPeer> newPeers = 
leader.getRaftConf().getCurrentPeers().stream()
+        .map(p -> 
RaftPeer.newBuilder(p).setAddress(p.getAddress().replace(NetUtils.LOCALHOST, 
"127.0.0.1")).build())
+        .collect(Collectors.toList());
+    try (RaftClient client = cluster.createClient(leader.getId())) {
+      assertTrue(client.admin().setConfiguration(newPeers).isSuccess());
+      assertTrue(leader.getRaftConf().getLogEntryIndex() > confIndex);
+      assertAddresses(newPeers, cluster);
+
+      // the same request again has no change
+      final RaftConfiguration conf = leader.getRaftConf();
+      assertTrue(client.admin().setConfiguration(newPeers).isSuccess());
+      Assertions.assertSame(conf, leader.getRaftConf());
+    }
+
+    cluster.restart(false);
+    RaftTestUtil.waitForLeader(cluster);
+    assertAddresses(newPeers, cluster);
+  }
+
+  void assertAddresses(List<RaftPeer> expected, MiniRaftCluster cluster) 
throws Exception {
+    JavaUtils.attempt(() -> {
+      for (RaftServer.Division d : cluster.iterateDivisions()) {
+        final RaftConfiguration persisted = ((RaftStorageImpl) 
d.getRaftStorage()).readRaftConfiguration();

Review Comment:
   This test hard-casts the storage to `RaftStorageImpl`, which tightly couples 
the test to a specific implementation and can break if `MiniRaftCluster` uses a 
different `RaftStorage` implementation in some environments. Prefer using an 
existing test utility (e.g., `RaftStorageTestUtils`) or reading the 
configuration via a public/abstracted API based on the storage directory 
instead of downcasting.



##########
ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java:
##########
@@ -1492,7 +1492,7 @@ public CompletableFuture<RaftClientReply> 
setConfigurationAsync(SetConfiguration
       }
 
       // return success with a null message if the new conf is the same as the 
current
-      if (current.hasNoChange(serversInNewConf, listenersInNewConf)) {
+      if (current.hasNoChange(serversInNewConf, listenersInNewConf, true)) {

Review Comment:
   Passing a literal boolean here makes the intent easy to miss and creates a 
'boolean trap'. Consider replacing the `true` with a named constant (e.g., 
`CHECK_ADDRESSES`) or using a dedicated method name (e.g., 
`hasNoChangeIncludingAddresses(...)`) so call sites are self-describing.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to