Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BALANCER] rewrite AlgorithmConfig by java 17 record #1761

Merged
merged 1 commit into from
May 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 33 additions & 81 deletions common/src/main/java/org/astraea/common/balancer/AlgorithmConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,59 +27,41 @@
import org.astraea.common.cost.HasMoveCost;
import org.astraea.common.metrics.ClusterBean;

/** The generic algorithm parameter for resolving the Kafka rebalance problem. */
public interface AlgorithmConfig {

static Builder builder() {
/**
* The generic algorithm parameter for resolving the Kafka rebalance problem.
*
* @param executionId a String indicate the name of this execution. This information is used for
* debug and logging usage.
* @param clusterCostFunction the cluster cost function for this problem.
* @param moveCostFunction the movement cost functions for this problem
* @param balancerConfig the configuration of this balancer run
* @param clusterInfo the initial cluster state of this optimization problem
* @param clusterBean the metrics of the associated cluster and optimization problem
* @param timeout the execution limit of this optimization problem
*/
public record AlgorithmConfig(
String executionId,
HasClusterCost clusterCostFunction,
HasMoveCost moveCostFunction,
Configuration balancerConfig,
ClusterInfo clusterInfo,
ClusterBean clusterBean,
Duration timeout) {

public static Builder builder() {
return new Builder(null);
}

static Builder builder(AlgorithmConfig config) {
public static Builder builder(AlgorithmConfig config) {
return new Builder(config);
}

/**
* @return a String indicate the name of this execution. This information is used for debug and
* logging usage.
*/
String executionId();

/**
* @return the cluster cost function for this problem.
*/
HasClusterCost clusterCostFunction();

/**
* @return the movement cost functions for this problem
*/
HasMoveCost moveCostFunction();

/**
* @return the configuration of this balancer run
*/
Configuration balancerConfig();

/**
* @return the initial cluster state of this optimization problem
*/
ClusterInfo clusterInfo();

/**
* @return the metrics of the associated cluster and optimization problem
*/
ClusterBean clusterBean();

/**
* @return the execution limit of this optimization problem
*/
Duration timeout();

class Builder {
public static class Builder {

private String executionId = "noname-" + UUID.randomUUID();
private HasClusterCost clusterCostFunction;
private HasMoveCost moveCostFunction = HasMoveCost.EMPTY;
private Map<String, String> balancerConfig = new HashMap<>();
private final Map<String, String> balancerConfig = new HashMap<>();

private ClusterInfo clusterInfo;
private ClusterBean clusterBean = ClusterBean.EMPTY;
Expand Down Expand Up @@ -184,44 +166,14 @@ public Builder timeout(Duration timeout) {
}

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

return new AlgorithmConfig() {
@Override
public String executionId() {
return executionId;
}

@Override
public HasClusterCost clusterCostFunction() {
return clusterCostFunction;
}

@Override
public HasMoveCost moveCostFunction() {
return moveCostFunction;
}

@Override
public Configuration balancerConfig() {
return config;
}

@Override
public ClusterInfo clusterInfo() {
return clusterInfo;
}

@Override
public ClusterBean clusterBean() {
return clusterBean;
}

@Override
public Duration timeout() {
return timeout;
}
};
return new AlgorithmConfig(
executionId,
clusterCostFunction,
moveCostFunction,
new Configuration(balancerConfig),
clusterInfo,
clusterBean,
timeout);
}
}
}