This is an automated email from the ASF dual-hosted git repository.

szetszwo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ratis.git


The following commit(s) were added to refs/heads/master by this push:
     new 6f34fb10b RATIS-2229. Do not print the same conf values multiple 
times. (#1200)
6f34fb10b is described below

commit 6f34fb10beeb5a633650f17e30e1a321fef43c5b
Author: Tsz-Wo Nicholas Sze <[email protected]>
AuthorDate: Fri Jan 3 09:55:48 2025 -0800

    RATIS-2229. Do not print the same conf values multiple times. (#1200)
---
 .../main/java/org/apache/ratis/conf/ConfUtils.java | 18 +++++++++++-
 .../ratis/server/simulation/SimulatedRpc.java      |  6 +++-
 .../java/org/apache/ratis/conf/TestConfUtils.java  | 32 ++++++++++++++++++++++
 3 files changed, 54 insertions(+), 2 deletions(-)

diff --git a/ratis-common/src/main/java/org/apache/ratis/conf/ConfUtils.java 
b/ratis-common/src/main/java/org/apache/ratis/conf/ConfUtils.java
index c1fb9268c..3f7678a0b 100644
--- a/ratis-common/src/main/java/org/apache/ratis/conf/ConfUtils.java
+++ b/ratis-common/src/main/java/org/apache/ratis/conf/ConfUtils.java
@@ -33,6 +33,8 @@ import java.lang.reflect.Modifier;
 import java.net.InetSocketAddress;
 import java.util.Arrays;
 import java.util.List;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
 import java.util.function.BiConsumer;
 import java.util.function.BiFunction;
 import java.util.function.Consumer;
@@ -41,8 +43,22 @@ import java.util.function.Function;
 public interface ConfUtils {
   Logger LOG = LoggerFactory.getLogger(ConfUtils.class);
 
+  class Utils {
+    private static final ConcurrentMap<String, Object> CACHE = new 
ConcurrentHashMap<>();
+
+    private static <T> boolean isNew(String key, T value) {
+      if (value == null) {
+        final Object previous = CACHE.remove(key);
+        return previous != null;
+      } else {
+        final Object previous = CACHE.put(key, value);
+        return !value.equals(previous);
+      }
+    }
+  }
+
   static <T> void logGet(String key, T value, T defaultValue, Consumer<String> 
logger) {
-    if (logger != null) {
+    if (logger != null && Utils.isNew(key, value)) {
       logger.accept(String.format("%s = %s (%s)", key, value,
           Objects.equal(value, defaultValue)? "default": "custom"));
     }
diff --git 
a/ratis-server/src/test/java/org/apache/ratis/server/simulation/SimulatedRpc.java
 
b/ratis-server/src/test/java/org/apache/ratis/server/simulation/SimulatedRpc.java
index 0399b414b..e570c35af 100644
--- 
a/ratis-server/src/test/java/org/apache/ratis/server/simulation/SimulatedRpc.java
+++ 
b/ratis-server/src/test/java/org/apache/ratis/server/simulation/SimulatedRpc.java
@@ -28,9 +28,13 @@ import org.apache.ratis.util.JavaUtils;
 
 import java.util.Objects;
 
-class SimulatedRpc implements RpcType {
+public class SimulatedRpc implements RpcType {
   static final SimulatedRpc INSTANCE = new SimulatedRpc();
 
+  public static SimulatedRpc get() {
+    return INSTANCE;
+  }
+
   @Override
   public String name() {
     return getClass().getName();
diff --git a/ratis-test/src/test/java/org/apache/ratis/conf/TestConfUtils.java 
b/ratis-test/src/test/java/org/apache/ratis/conf/TestConfUtils.java
index 67c02cd5f..859c597b5 100644
--- a/ratis-test/src/test/java/org/apache/ratis/conf/TestConfUtils.java
+++ b/ratis-test/src/test/java/org/apache/ratis/conf/TestConfUtils.java
@@ -22,10 +22,42 @@ import org.apache.ratis.RaftConfigKeys;
 import org.apache.ratis.client.RaftClientConfigKeys;
 import org.apache.ratis.grpc.GrpcConfigKeys;
 import org.apache.ratis.netty.NettyConfigKeys;
+import org.apache.ratis.rpc.RpcType;
 import org.apache.ratis.server.RaftServerConfigKeys;
+import org.apache.ratis.server.simulation.SimulatedRpc;
+import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.Consumer;
+
 public class TestConfUtils  extends BaseTest {
+  @Test
+  public void testLogging() {
+    final AtomicInteger count = new AtomicInteger();
+    final Consumer<String> logger = s -> {
+      System.out.println("log: " + s);
+      count.incrementAndGet();
+    };
+
+    final RaftProperties properties = new RaftProperties();
+    final RpcType simulated = SimulatedRpc.get();
+
+    // get a value the first time
+    final RpcType defaultType = RaftConfigKeys.Rpc.type(properties, logger);
+    Assertions.assertEquals(1, count.get());
+    Assertions.assertNotEquals(defaultType, simulated);
+
+    // get the same value the second time
+    RaftConfigKeys.Rpc.type(properties, logger);
+    Assertions.assertEquals(1, count.get());
+
+    // get a different value
+    RaftConfigKeys.Rpc.setType(properties, SimulatedRpc.get());
+    RaftConfigKeys.Rpc.type(properties, logger);
+    Assertions.assertEquals(2, count.get());
+  }
+
   @Test
   public void testRaftConfigKeys() {
     ConfUtils.printAll(RaftConfigKeys.class);

Reply via email to