Skip to content

Commit

Permalink
RATIS-2229. Do not print the same conf values multiple times.
Browse files Browse the repository at this point in the history
  • Loading branch information
szetszwo committed Dec 25, 2024
1 parent accb612 commit fae59fb
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
18 changes: 17 additions & 1 deletion ratis-common/src/main/java/org/apache/ratis/conf/ConfUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
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;
Expand All @@ -41,8 +43,22 @@
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"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@

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();
Expand Down
32 changes: 32 additions & 0 deletions ratis-test/src/test/java/org/apache/ratis/conf/TestConfUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,42 @@
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);
Expand Down

0 comments on commit fae59fb

Please sign in to comment.