use of com.linkedin.kafka.cruisecontrol.common.TestConstants.TOPIC1 in project cruise-control by linkedin.
the class PreferredLeaderElectionGoalTest method testOptimizeWithDemotedBrokers.
@Test
public void testOptimizeWithDemotedBrokers() {
ClusterModel clusterModel = createClusterModel(true, false).clusterModel();
clusterModel.setBrokerState(0, Broker.State.DEMOTED);
Set<TopicPartition> leaderPartitionsOnDemotedBroker = new HashSet<>();
clusterModel.broker(0).leaderReplicas().forEach(r -> leaderPartitionsOnDemotedBroker.add(r.topicPartition()));
Map<TopicPartition, Integer> leaderDistributionBeforeBrokerDemotion = new HashMap<>();
clusterModel.brokers().forEach(b -> b.leaderReplicas().forEach(r -> leaderDistributionBeforeBrokerDemotion.put(r.topicPartition(), b.id())));
PreferredLeaderElectionGoal goal = new PreferredLeaderElectionGoal(false, false, null);
// Before the optimization, goals are expected to be undecided wrt their provision status.
assertEquals(ProvisionStatus.UNDECIDED, goal.provisionResponse().status());
goal.optimize(clusterModel, Collections.emptySet(), new OptimizationOptions(Collections.emptySet(), Collections.emptySet(), Collections.emptySet()));
// After the optimization, PreferredLeaderElectionGoal is expected to be undecided wrt its provision status.
assertEquals(ProvisionStatus.UNDECIDED, goal.provisionResponse().status());
for (String t : Arrays.asList(TOPIC0, TOPIC1, TOPIC2)) {
for (int p = 0; p < 3; p++) {
TopicPartition tp = new TopicPartition(t, p);
if (!leaderPartitionsOnDemotedBroker.contains(tp)) {
int oldLeaderBroker = leaderDistributionBeforeBrokerDemotion.get(tp);
assertEquals("Tp " + tp, oldLeaderBroker, clusterModel.partition(tp).leader().broker().id());
} else {
List<Replica> replicas = clusterModel.partition(tp).replicas();
for (int i = 0; i < 3; i++) {
Replica replica = replicas.get(i);
// only the first replica should be leader.
assertEquals(i == 0, replica.isLeader());
if (clusterModel.broker(0).replicas().contains(replica)) {
// The demoted replica should be in the last position.
assertEquals(replicas.size() - 1, i);
}
}
}
}
}
}
use of com.linkedin.kafka.cruisecontrol.common.TestConstants.TOPIC1 in project cruise-control by linkedin.
the class PreferredLeaderElectionGoalTest method testOptimizeWithDemotedDisks.
@Test
public void testOptimizeWithDemotedDisks() {
ClusterModel clusterModel = createClusterModel(true, true).clusterModel();
clusterModel.broker(0).disk(LOGDIR0).setState(Disk.State.DEMOTED);
clusterModel.broker(1).disk(LOGDIR1).setState(Disk.State.DEMOTED);
Set<TopicPartition> leaderPartitionsOnDemotedDisk = new HashSet<>();
clusterModel.broker(0).disk(LOGDIR0).leaderReplicas().forEach(r -> leaderPartitionsOnDemotedDisk.add(r.topicPartition()));
clusterModel.broker(1).disk(LOGDIR1).leaderReplicas().forEach(r -> leaderPartitionsOnDemotedDisk.add(r.topicPartition()));
Map<TopicPartition, Integer> leaderDistributionBeforeBrokerDemotion = new HashMap<>();
clusterModel.brokers().forEach(b -> b.leaderReplicas().forEach(r -> leaderDistributionBeforeBrokerDemotion.put(r.topicPartition(), b.id())));
PreferredLeaderElectionGoal goal = new PreferredLeaderElectionGoal(false, false, null);
// Before the optimization, goals are expected to be undecided wrt their provision status.
assertEquals(ProvisionStatus.UNDECIDED, goal.provisionResponse().status());
goal.optimize(clusterModel, Collections.emptySet(), new OptimizationOptions(Collections.emptySet(), Collections.emptySet(), Collections.emptySet()));
// After the optimization, PreferredLeaderElectionGoal is expected to be undecided wrt its provision status.
assertEquals(ProvisionStatus.UNDECIDED, goal.provisionResponse().status());
for (String t : Arrays.asList(TOPIC0, TOPIC1, TOPIC2)) {
for (int p = 0; p < 3; p++) {
TopicPartition tp = new TopicPartition(t, p);
if (!leaderPartitionsOnDemotedDisk.contains(tp)) {
int oldLeaderBroker = leaderDistributionBeforeBrokerDemotion.get(tp);
assertEquals("Tp " + tp, oldLeaderBroker, clusterModel.partition(tp).leader().broker().id());
} else {
List<Replica> replicas = clusterModel.partition(tp).replicas();
for (int i = 0; i < 3; i++) {
Replica replica = replicas.get(i);
// only the first replica should be leader.
assertEquals(i == 0, replica.isLeader());
if (clusterModel.broker(0).disk(LOGDIR0).replicas().contains(replica) || clusterModel.broker(1).disk(LOGDIR1).replicas().contains(replica)) {
// The demoted replica should be in the last position.
assertEquals(replica.topicPartition() + " broker " + replica.broker().id(), replicas.size() - 1, i);
}
}
}
}
}
}
use of com.linkedin.kafka.cruisecontrol.common.TestConstants.TOPIC1 in project cruise-control by linkedin.
the class PreferredLeaderElectionGoalTest method testOptimizeWithDemotedBrokersAndDisks.
@Test
public void testOptimizeWithDemotedBrokersAndDisks() {
ClusterModel clusterModel = createClusterModel(true, true).clusterModel();
clusterModel.setBrokerState(0, Broker.State.DEMOTED);
clusterModel.broker(1).disk(LOGDIR0).setState(Disk.State.DEMOTED);
Set<TopicPartition> leaderPartitionsToBeDemoted = new HashSet<>();
clusterModel.broker(0).leaderReplicas().forEach(r -> leaderPartitionsToBeDemoted.add(r.topicPartition()));
clusterModel.broker(1).disk(LOGDIR0).leaderReplicas().forEach(r -> leaderPartitionsToBeDemoted.add(r.topicPartition()));
Map<TopicPartition, Integer> leaderDistributionBeforeBrokerDemotion = new HashMap<>();
clusterModel.brokers().forEach(b -> b.leaderReplicas().forEach(r -> leaderDistributionBeforeBrokerDemotion.put(r.topicPartition(), b.id())));
PreferredLeaderElectionGoal goal = new PreferredLeaderElectionGoal(false, false, null);
// Before the optimization, goals are expected to be undecided wrt their provision status.
assertEquals(ProvisionStatus.UNDECIDED, goal.provisionResponse().status());
goal.optimize(clusterModel, Collections.emptySet(), new OptimizationOptions(Collections.emptySet(), Collections.emptySet(), Collections.emptySet()));
// After the optimization, PreferredLeaderElectionGoal is expected to be undecided wrt its provision status.
assertEquals(ProvisionStatus.UNDECIDED, goal.provisionResponse().status());
for (String t : Arrays.asList(TOPIC0, TOPIC1, TOPIC2)) {
for (int p = 0; p < 3; p++) {
TopicPartition tp = new TopicPartition(t, p);
if (!leaderPartitionsToBeDemoted.contains(tp)) {
int oldLeaderBroker = leaderDistributionBeforeBrokerDemotion.get(tp);
assertEquals("Tp " + tp, oldLeaderBroker, clusterModel.partition(tp).leader().broker().id());
} else {
List<Replica> replicas = clusterModel.partition(tp).replicas();
for (int i = 0; i < 3; i++) {
Replica replica = replicas.get(i);
// only the first replica should be leader.
assertEquals(i == 0, replica.isLeader());
if (clusterModel.broker(0).replicas().contains(replica) || clusterModel.broker(1).disk(LOGDIR0).replicas().contains(replica)) {
// The demoted replica should be in the last position.
assertEquals(replicas.size() - 1, i);
}
}
}
}
}
}
Aggregations