Skip to content

Commit

Permalink
[COMMON] rewrite Configuration by java 17 record (#1758)
Browse files Browse the repository at this point in the history
  • Loading branch information
chia7712 authored May 18, 2023
1 parent 63e5ee4 commit a43d47a
Show file tree
Hide file tree
Showing 41 changed files with 116 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ static DataGenerator of(
Performance.Argument argument) {
if (queues.size() == 0) return terminatedGenerator();

var keyDistConfig = Configuration.of(argument.keyDistributionConfig);
var keySizeDistConfig = Configuration.of(argument.keySizeDistributionConfig);
var valueDistConfig = Configuration.of(argument.valueDistributionConfig);
var keyDistConfig = new Configuration(argument.keyDistributionConfig);
var keySizeDistConfig = new Configuration(argument.keySizeDistributionConfig);
var valueDistConfig = new Configuration(argument.valueDistributionConfig);
var dataSupplier =
RecordGenerator.builder()
.batchSize(argument.transactionSize)
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/org/astraea/app/web/BalancerHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public CompletionStage<Response> put(Channel channel) {
final var request = channel.request(TypeRef.of(BalancerPutRequest.class));
final var taskId = request.id;
final var taskPhase = balancerConsole.taskPhase(taskId);
final var executorConfig = Configuration.of(request.executorConfig);
final var executorConfig = new Configuration(request.executorConfig);
final var executor =
Utils.construct(request.executor, RebalancePlanExecutor.class, executorConfig);

Expand Down Expand Up @@ -216,7 +216,7 @@ static PostRequestWrapper parsePostRequestWrapper(

return new PostRequestWrapper(
balancerPostRequest.balancer,
Configuration.of(balancerPostRequest.balancerConfig),
new Configuration(balancerPostRequest.balancerConfig),
balancerPostRequest.parse(),
currentClusterInfo);
}
Expand Down
53 changes: 18 additions & 35 deletions common/src/main/java/org/astraea/common/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,90 +18,71 @@

import java.time.Duration;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Optional;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public interface Configuration {
public record Configuration(Map<String, String> raw) {

Configuration EMPTY = Configuration.of(Map.of());

static Configuration of(Map<String, String> configs) {
return new Configuration() {
@Override
public Map<String, String> raw() {
return Collections.unmodifiableMap(configs);
}

@Override
public Optional<String> string(String key) {
return Optional.ofNullable(configs.get(key)).map(Object::toString);
}

@Override
public List<String> list(String key, String separator) {
return string(key).map(s -> Arrays.asList(s.split(separator))).orElseGet(List::of);
}
};
}

Map<String, String> raw();
public static final Configuration EMPTY = new Configuration(Map.of());

/**
* @param key the key whose associated value is to be returned
* @return string value. never null
*/
Optional<String> string(String key);
public Optional<String> string(String key) {
return Optional.ofNullable(raw.get(Objects.requireNonNull(key)));
}

/**
* @param key the key whose associated value is to be returned
* @return optional {@link Pattern} compiled from the string associated with the key. never null
*/
default Optional<Pattern> regexString(String key) {
public Optional<Pattern> regexString(String key) {
return string(key).map(Pattern::compile);
}

/**
* @param key the key whose associated value is to be returned
* @return integer value. never null
*/
default Optional<Integer> integer(String key) {
public Optional<Integer> integer(String key) {
return string(key).map(Integer::parseInt);
}

default Optional<Long> longInteger(String key) {
public Optional<Long> longInteger(String key) {
return string(key).map(Long::parseLong);
}

/**
* @param key the key whose associated value is to be returned
* @return duration value. If there is no key, return Optional.Empty
*/
default Optional<Duration> duration(String key) {
public Optional<Duration> duration(String key) {
return string(key).map(Utils::toDuration);
}

/**
* @param key the key whose associated value is to be returned
* @return DataSize value. If there is no key, return Optional.Empty
*/
default Optional<DataSize> dataSize(String key) {
public Optional<DataSize> dataSize(String key) {
return string(key).map(DataSize::of);
}

default int requireInteger(String key) {
public int requireInteger(String key) {
return integer(key).orElseThrow(() -> new NoSuchElementException(key + " is nonexistent"));
}

/**
* @param key the key whose associated value is to be returned
* @return string value. never null
*/
default String requireString(String key) {
public String requireString(String key) {
return string(key).orElseThrow(() -> new NoSuchElementException(key + " is nonexistent"));
}

Expand All @@ -110,8 +91,8 @@ default String requireString(String key) {
* @return new Configuration only contains which the key value starts with the prefix, and the
* prefix string and the following dot will be removed from the key
*/
default Configuration filteredPrefixConfigs(String prefix) {
return of(
public Configuration filteredPrefixConfigs(String prefix) {
return new Configuration(
raw().entrySet().stream()
.filter(k -> k.getKey().startsWith(prefix))
.collect(
Expand All @@ -124,5 +105,7 @@ default Configuration filteredPrefixConfigs(String prefix) {
* @param separator to split string to multiple strings
* @return string list. never null
*/
List<String> list(String key, String separator);
public List<String> list(String key, String separator) {
return string(key).map(s -> Arrays.asList(s.split(separator))).orElseGet(List::of);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public final GroupAssignment assign(Cluster metadata, GroupSubscription groupSub
@Override
public final void configure(Map<String, ?> configs) {
this.config =
Configuration.of(
new Configuration(
configs.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().toString())));
var costFunctions =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public Builder timeout(Duration timeout) {
}

public AlgorithmConfig build() {
var config = Configuration.of(balancerConfig);
var config = new Configuration(balancerConfig);

return new AlgorithmConfig() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public AlgorithmConfig parse() {

private HasClusterCost clusterCost() {
if (clusterCosts.isEmpty()) throw new IllegalArgumentException("clusterCosts is not specified");
var config = Configuration.of(costConfig);
var config = new Configuration(costConfig);
return HasClusterCost.of(
Utils.costFunctions(
clusterCosts.stream()
Expand All @@ -65,7 +65,7 @@ private HasClusterCost clusterCost() {
}

private HasMoveCost moveCost() {
var config = Configuration.of(costConfig);
var config = new Configuration(costConfig);
var cf = Utils.costFunctions(moveCosts, HasMoveCost.class, config);
return HasMoveCost.of(cf);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class RecordSizeCost
public static final String MAX_MIGRATE_SIZE_KEY = "max.migrated.size";

public RecordSizeCost() {
this.config = Configuration.of(Map.of());
this.config = new Configuration(Map.of());
}

public RecordSizeCost(Configuration config) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class ReplicaLeaderCost implements HasBrokerCost, HasClusterCost, HasMove
public static final String MAX_MIGRATE_LEADER_KEY = "max.migrated.leader.number";

public ReplicaLeaderCost() {
this.config = Configuration.of(Map.of());
this.config = new Configuration(Map.of());
}

public ReplicaLeaderCost(Configuration config) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class ReplicaLeaderSizeCost
public static final String MOVED_LEADER_SIZE = "moved leader size (bytes)";

public ReplicaLeaderSizeCost() {
this.config = Configuration.of(Map.of());
this.config = new Configuration(Map.of());
}

public ReplicaLeaderSizeCost(Configuration config) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class ReplicaNumberCost implements HasClusterCost, HasMoveCost {
private final Configuration config;

public ReplicaNumberCost() {
this.config = Configuration.of(Map.of());
this.config = new Configuration(Map.of());
}

public ReplicaNumberCost(Configuration config) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private static class Interdependent {
@Override
public final void configure(Map<String, ?> configs) {
var config =
Configuration.of(
new Configuration(
configs.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().toString())));
config.string(ProducerConfigs.BOOTSTRAP_SERVERS_CONFIG).ifPresent(s -> admin = Admin.of(s));
Expand Down
12 changes: 6 additions & 6 deletions common/src/test/java/org/astraea/common/ConfigurationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,27 @@ public class ConfigurationTest {

@Test
void testString() {
var config = Configuration.of(Map.of("key", "value"));
var config = new Configuration(Map.of("key", "value"));
Assertions.assertEquals(Optional.of("value"), config.string("key"));
Assertions.assertEquals("value", config.requireString("key"));
}

@Test
void testList() {
var config = Configuration.of(Map.of("key", "v0,v1"));
var config = new Configuration(Map.of("key", "v0,v1"));
Assertions.assertEquals(List.of("v0", "v1"), config.list("key", ","));
Assertions.assertEquals(List.of(), config.list("nonExistKey", ","));
}

@Test
void testFilteredConfigs() {
var config = Configuration.of(Map.of("key", "v1", "filtered.key", "v2", "key.filtered", "v3"));
var config = new Configuration(Map.of("key", "v1", "filtered.key", "v2", "key.filtered", "v3"));
Assertions.assertEquals(Map.of("key", "v2"), config.filteredPrefixConfigs("filtered").raw());
}

@Test
void testDuration() {
var config = Configuration.of(Map.of("wait.time", "15ms", "response", "3s"));
var config = new Configuration(Map.of("wait.time", "15ms", "response", "3s"));
var waitTime = config.duration("wait.time");
var response = config.duration("response");
var empty = config.duration("walala");
Expand All @@ -57,13 +57,13 @@ void testDuration() {

@Test
void testLong() {
var config = Configuration.of(Map.of("long.value", "2147483648"));
var config = new Configuration(Map.of("long.value", "2147483648"));
Assertions.assertEquals(2147483648L, config.longInteger("long.value").orElse(0L));
}

@Test
void testDataSize() {
var config = Configuration.of(Map.of("upper.bound", "30MiB", "traffic.interval", "5MB"));
var config = new Configuration(Map.of("upper.bound", "30MiB", "traffic.interval", "5MB"));
var upper = config.dataSize("upper.bound");
var interval = config.dataSize("traffic.interval");
var empty = config.dataSize("kekw");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void testZipfianConfig() {
// fixed seed
var zip100 =
DistributionType.ZIPFIAN.create(
10000, Configuration.of(Map.of(DistributionType.ZIPFIAN_SEED, "100")));
10000, new Configuration(Map.of(DistributionType.ZIPFIAN_SEED, "100")));
Assertions.assertEquals(
List.of(11, 18, 0, 1126, 12),
IntStream.range(0, 5)
Expand All @@ -101,21 +101,25 @@ void testZipfianConfig() {
var seed = ThreadLocalRandom.current().nextInt();
var zipA =
DistributionType.ZIPFIAN.create(
10000, Configuration.of(Map.of(DistributionType.ZIPFIAN_SEED, Integer.toString(seed))));
10000,
new Configuration(Map.of(DistributionType.ZIPFIAN_SEED, Integer.toString(seed))));
var zipB =
DistributionType.ZIPFIAN.create(
10000, Configuration.of(Map.of(DistributionType.ZIPFIAN_SEED, Integer.toString(seed))));
10000,
new Configuration(Map.of(DistributionType.ZIPFIAN_SEED, Integer.toString(seed))));
var sequenceA = IntStream.range(0, 1000).map(i -> zipA.get().intValue()).toArray();
var sequenceB = IntStream.range(0, 1000).map(i -> zipB.get().intValue()).toArray();
Assertions.assertArrayEquals(sequenceA, sequenceB);

// high exponent come with high skewness
var zip1 =
DistributionType.ZIPFIAN.create(
100, Configuration.of(Map.of(DistributionType.ZIPFIAN_EXPONENT, Double.toString(1.0))));
100,
new Configuration(Map.of(DistributionType.ZIPFIAN_EXPONENT, Double.toString(1.0))));
var zip2 =
DistributionType.ZIPFIAN.create(
100, Configuration.of(Map.of(DistributionType.ZIPFIAN_EXPONENT, Double.toString(2.0))));
100,
new Configuration(Map.of(DistributionType.ZIPFIAN_EXPONENT, Double.toString(2.0))));
var counting1 =
IntStream.range(0, 10000)
.map(x -> zip1.get().intValue())
Expand Down
10 changes: 5 additions & 5 deletions common/src/test/java/org/astraea/common/UtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public int value() {
@ParameterizedTest
@ValueSource(classes = {TestCostFunction.class, TestConfigCostFunction.class})
void testConstruct(Class<? extends CostFunction> aClass) {
var config = Configuration.of(Map.of());
var config = new Configuration(Map.of());

var costFunction = Utils.construct(aClass, config);
Assertions.assertInstanceOf(CostFunction.class, costFunction);
Expand All @@ -226,7 +226,7 @@ void testConstruct(Class<? extends CostFunction> aClass) {
void testConstructException() {
// arrange
var aClass = TestBadCostFunction.class;
var config = Configuration.of(Map.of());
var config = new Configuration(Map.of());

// act, assert
Assertions.assertThrows(RuntimeException.class, () -> Utils.construct(aClass, config));
Expand Down Expand Up @@ -283,7 +283,7 @@ public TestBadCostFunction(int value) {}
@Test
void testCostFunctions() {
var config =
Configuration.of(
new Configuration(
Map.of(
"org.astraea.common.cost.BrokerInputCost",
"20",
Expand All @@ -307,7 +307,7 @@ void testCostFunctions() {

// test negative weight
var config2 =
Configuration.of(
new Configuration(
Map.of(
"org.astraea.common.cost.BrokerInputCost",
"-20",
Expand All @@ -321,7 +321,7 @@ void testCostFunctions() {
var cf =
Set.of(
"org.astraea.common.cost.RecordSizeCost", "org.astraea.common.cost.ReplicaLeaderCost");
var mConfig = Configuration.of(Map.of("maxMigratedSize", "50MB", "maxMigratedLeader", "5"));
var mConfig = new Configuration(Map.of("maxMigratedSize", "50MB", "maxMigratedLeader", "5"));
var mAns = Utils.costFunctions(cf, HasMoveCost.class, mConfig);
Assertions.assertEquals(2, mAns.size());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public ClusterCost clusterCost(ClusterInfo clusterInfo, ClusterBean clusterBean)
.toCompletableFuture()
.join();
var newAllocation =
Utils.construct(theClass, Configuration.of(Map.of("iteration", "500")))
Utils.construct(theClass, new Configuration(Map.of("iteration", "500")))
.offer(
AlgorithmConfig.builder()
.clusterInfo(clusterInfo)
Expand Down Expand Up @@ -285,7 +285,7 @@ public ClusterCost clusterCost(ClusterInfo clusterInfo, ClusterBean clusterBean)
return () -> 0;
}
};
Utils.construct(theClass, Configuration.of(Map.of("iteration", "500")))
Utils.construct(theClass, new Configuration(Map.of("iteration", "500")))
.offer(
AlgorithmConfig.builder()
.clusterInfo(ClusterInfo.empty())
Expand Down
Loading

0 comments on commit a43d47a

Please sign in to comment.