Skip to content

Commit 6f34fb1

Browse files
authored
RATIS-2229. Do not print the same conf values multiple times. (#1200)
1 parent c454d78 commit 6f34fb1

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

ratis-common/src/main/java/org/apache/ratis/conf/ConfUtils.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import java.net.InetSocketAddress;
3434
import java.util.Arrays;
3535
import java.util.List;
36+
import java.util.concurrent.ConcurrentHashMap;
37+
import java.util.concurrent.ConcurrentMap;
3638
import java.util.function.BiConsumer;
3739
import java.util.function.BiFunction;
3840
import java.util.function.Consumer;
@@ -41,8 +43,22 @@
4143
public interface ConfUtils {
4244
Logger LOG = LoggerFactory.getLogger(ConfUtils.class);
4345

46+
class Utils {
47+
private static final ConcurrentMap<String, Object> CACHE = new ConcurrentHashMap<>();
48+
49+
private static <T> boolean isNew(String key, T value) {
50+
if (value == null) {
51+
final Object previous = CACHE.remove(key);
52+
return previous != null;
53+
} else {
54+
final Object previous = CACHE.put(key, value);
55+
return !value.equals(previous);
56+
}
57+
}
58+
}
59+
4460
static <T> void logGet(String key, T value, T defaultValue, Consumer<String> logger) {
45-
if (logger != null) {
61+
if (logger != null && Utils.isNew(key, value)) {
4662
logger.accept(String.format("%s = %s (%s)", key, value,
4763
Objects.equal(value, defaultValue)? "default": "custom"));
4864
}

ratis-server/src/test/java/org/apache/ratis/server/simulation/SimulatedRpc.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,13 @@
2828

2929
import java.util.Objects;
3030

31-
class SimulatedRpc implements RpcType {
31+
public class SimulatedRpc implements RpcType {
3232
static final SimulatedRpc INSTANCE = new SimulatedRpc();
3333

34+
public static SimulatedRpc get() {
35+
return INSTANCE;
36+
}
37+
3438
@Override
3539
public String name() {
3640
return getClass().getName();

ratis-test/src/test/java/org/apache/ratis/conf/TestConfUtils.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,42 @@
2222
import org.apache.ratis.client.RaftClientConfigKeys;
2323
import org.apache.ratis.grpc.GrpcConfigKeys;
2424
import org.apache.ratis.netty.NettyConfigKeys;
25+
import org.apache.ratis.rpc.RpcType;
2526
import org.apache.ratis.server.RaftServerConfigKeys;
27+
import org.apache.ratis.server.simulation.SimulatedRpc;
28+
import org.junit.jupiter.api.Assertions;
2629
import org.junit.jupiter.api.Test;
2730

31+
import java.util.concurrent.atomic.AtomicInteger;
32+
import java.util.function.Consumer;
33+
2834
public class TestConfUtils extends BaseTest {
35+
@Test
36+
public void testLogging() {
37+
final AtomicInteger count = new AtomicInteger();
38+
final Consumer<String> logger = s -> {
39+
System.out.println("log: " + s);
40+
count.incrementAndGet();
41+
};
42+
43+
final RaftProperties properties = new RaftProperties();
44+
final RpcType simulated = SimulatedRpc.get();
45+
46+
// get a value the first time
47+
final RpcType defaultType = RaftConfigKeys.Rpc.type(properties, logger);
48+
Assertions.assertEquals(1, count.get());
49+
Assertions.assertNotEquals(defaultType, simulated);
50+
51+
// get the same value the second time
52+
RaftConfigKeys.Rpc.type(properties, logger);
53+
Assertions.assertEquals(1, count.get());
54+
55+
// get a different value
56+
RaftConfigKeys.Rpc.setType(properties, SimulatedRpc.get());
57+
RaftConfigKeys.Rpc.type(properties, logger);
58+
Assertions.assertEquals(2, count.get());
59+
}
60+
2961
@Test
3062
public void testRaftConfigKeys() {
3163
ConfUtils.printAll(RaftConfigKeys.class);

0 commit comments

Comments
 (0)