use of com.linkedin.kafka.cruisecontrol.model.Replica in project cruise-control by linkedin.
the class PreferredLeaderElectionGoalTest method testOptimize.
@Test
public void testOptimize() throws KafkaCruiseControlException {
ClusterModel clusterModel = createClusterModel();
PreferredLeaderElectionGoal goal = new PreferredLeaderElectionGoal();
goal.optimize(clusterModel, Collections.emptySet(), Collections.emptySet());
for (String t : Arrays.asList(TOPIC0, TOPIC1, TOPIC2)) {
for (int p = 0; p < 3; p++) {
List<Replica> replicas = clusterModel.partition(new TopicPartition(t, p)).replicas();
for (int i = 0; i < 3; i++) {
// only the first replica should be leader.
assertEquals(i == 0, replicas.get(i).isLeader());
}
}
}
}
use of com.linkedin.kafka.cruisecontrol.model.Replica in project cruise-control by linkedin.
the class RandomClusterTest method testNewBrokers.
/**
* This test first creates a random cluster, balance it. Then add two new brokers, balance the cluster again.
*/
public void testNewBrokers() throws Exception {
ClusterModel clusterModel = rebalance();
ClusterModel clusterWithNewBroker = new ClusterModel(new ModelGeneration(0, 0L), 1.0);
for (Broker b : clusterModel.brokers()) {
clusterWithNewBroker.createRack(b.rack().id());
Map<Resource, Double> brokerCapacity = new HashMap<>();
for (Resource r : Resource.cachedValues()) {
brokerCapacity.put(r, b.capacityFor(r));
}
clusterWithNewBroker.createBroker(b.rack().id(), Integer.toString(b.id()), b.id(), brokerCapacity);
}
for (Map.Entry<String, List<Partition>> entry : clusterModel.getPartitionsByTopic().entrySet()) {
for (Partition p : entry.getValue()) {
int index = 0;
for (Replica r : p.replicas()) {
clusterWithNewBroker.createReplica(r.broker().rack().id(), r.broker().id(), p.topicPartition(), index++, r.isLeader());
}
}
}
for (Broker b : clusterModel.brokers()) {
for (Replica replica : b.replicas()) {
AggregatedMetricValues aggregatedMetricValues = clusterModel.broker(b.id()).replica(replica.topicPartition()).load().loadByWindows();
clusterWithNewBroker.setReplicaLoad(b.rack().id(), b.id(), replica.topicPartition(), aggregatedMetricValues, clusterModel.load().windows());
}
}
for (int i = 1; i < 3; i++) {
clusterWithNewBroker.createBroker(Integer.toString(i), Integer.toString(i + clusterModel.brokers().size() - 1), i + clusterModel.brokers().size() - 1, TestConstants.BROKER_CAPACITY);
clusterWithNewBroker.setBrokerState(i + clusterModel.brokers().size() - 1, Broker.State.NEW);
}
assertTrue("Random Cluster Test failed to improve the existing state with new brokers.", OptimizationVerifier.executeGoalsFor(_balancingConstraint, clusterWithNewBroker, _goalNameByPriority, _verifications));
}
use of com.linkedin.kafka.cruisecontrol.model.Replica in project cruise-control by linkedin.
the class KafkaAssignerDiskUsageDistributionGoalTest method findReplicaToSwapWithAndVerify.
private void findReplicaToSwapWithAndVerify(List<Double> targetSizes, List<TopicPartition> expectedResults, double minSize, double maxSize, Replica replica, int brokerIdToSwapWith, ClusterModel clusterModel, KafkaAssignerDiskUsageDistributionGoal goal) {
for (int i = 0; i < targetSizes.size(); i++) {
Replica toSwapWith = goal.findReplicaToSwapWith(replica, sortedReplicaAscend(clusterModel.broker(brokerIdToSwapWith)), targetSizes.get(i), minSize, maxSize, clusterModel);
assertEquals(String.format("Wrong answer for targetSize = %f. Expected %s, but the result was %s", targetSizes.get(i), expectedResults.get(i), toSwapWith), expectedResults.get(i), toSwapWith == null ? null : toSwapWith.topicPartition());
}
}
use of com.linkedin.kafka.cruisecontrol.model.Replica in project cruise-control by linkedin.
the class KafkaAssignerDiskUsageDistributionGoalTest method testFindReplicaToSwapWith.
@Test
public void testFindReplicaToSwapWith() {
Properties props = KafkaCruiseControlUnitTestUtils.getKafkaCruiseControlProperties();
props.setProperty(KafkaCruiseControlConfig.MAX_REPLICAS_PER_BROKER_CONFIG, Long.toString(10L));
props.setProperty(KafkaCruiseControlConfig.DISK_BALANCE_THRESHOLD_CONFIG, "1.05");
BalancingConstraint balancingConstraint = new BalancingConstraint(new KafkaCruiseControlConfig(props));
KafkaAssignerDiskUsageDistributionGoal goal = new KafkaAssignerDiskUsageDistributionGoal(balancingConstraint);
ClusterModel clusterModel = createClusterModel();
Broker b2 = clusterModel.broker(2);
Replica r = b2.replica(T0P1);
assertNull(goal.findReplicaToSwapWith(r, sortedReplicaAscend(clusterModel.broker(1)), 30, 10, 90, clusterModel));
// The replicas on broker 3 are of the following sizes
// T0P0: 10
// T0P2: 20
// T1P1: 30
// T2P2: 50
// T2P1: 60
// T1P0: 80
// Only T0P0 and T1P1 are eligible to swap with r.
findReplicaToSwapWithAndVerify(Arrays.asList(-1.0, 5.0, 10.0, 20.0, 21.0, 60.0, 100.0), Arrays.asList(T0P0, T0P0, T0P0, T0P0, T1P1, T1P1, T1P1), 9, 90, r, 3, clusterModel, goal);
findReplicaToSwapWithAndVerify(Arrays.asList(-1.0, 5.0, 10.0, 20.0, 21.0, 60.0, 100.0), Arrays.asList(T1P1, T1P1, T1P1, T1P1, T1P1, T1P1, T1P1), 10, 31, r, 3, clusterModel, goal);
findReplicaToSwapWithAndVerify(Arrays.asList(-1.0, 5.0, 10.0, 20.0, 21.0, 60.0, 100.0), Arrays.asList(T0P0, T0P0, T0P0, T0P0, T0P0, T0P0, T0P0), 9, 30, r, 3, clusterModel, goal);
findReplicaToSwapWithAndVerify(Arrays.asList(-1.0, 5.0, 10.0, 20.0, 21.0, 60.0, 100.0), Arrays.asList(null, null, null, null, null, null, null), 10, 30, r, 3, clusterModel, goal);
}
use of com.linkedin.kafka.cruisecontrol.model.Replica in project cruise-control by linkedin.
the class KafkaAssignerDiskUsageDistributionGoalTest method sortedReplicaAscend.
private List<KafkaAssignerDiskUsageDistributionGoal.ReplicaWrapper> sortedReplicaAscend(Broker broker) {
List<KafkaAssignerDiskUsageDistributionGoal.ReplicaWrapper> sortedReplicas = new ArrayList<>();
List<Replica> replicasInDesc = broker.sortedReplicas(DISK);
for (int i = replicasInDesc.size() - 1; i >= 0; i--) {
Replica r = replicasInDesc.get(i);
sortedReplicas.add(new KafkaAssignerDiskUsageDistributionGoal.ReplicaWrapper(r, r.load().expectedUtilizationFor(DISK)));
}
return sortedReplicas;
}
Aggregations