Search in sources :

Example 16 with Disk

use of com.linkedin.kafka.cruisecontrol.model.Disk 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);
                    }
                }
            }
        }
    }
}
Also used : Replica(com.linkedin.kafka.cruisecontrol.model.Replica) Arrays(java.util.Arrays) ReplicaPlacementInfo(com.linkedin.kafka.cruisecontrol.model.ReplicaPlacementInfo) ClusterModel(com.linkedin.kafka.cruisecontrol.model.ClusterModel) HashMap(java.util.HashMap) PreferredLeaderElectionGoal(com.linkedin.kafka.cruisecontrol.analyzer.goals.PreferredLeaderElectionGoal) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) TOPIC0(com.linkedin.kafka.cruisecontrol.common.TestConstants.TOPIC0) Disk(com.linkedin.kafka.cruisecontrol.model.Disk) TOPIC1(com.linkedin.kafka.cruisecontrol.common.TestConstants.TOPIC1) Cluster(org.apache.kafka.common.Cluster) TOPIC2(com.linkedin.kafka.cruisecontrol.common.TestConstants.TOPIC2) KafkaMetricDef(com.linkedin.kafka.cruisecontrol.monitor.metricdefinition.KafkaMetricDef) Map(java.util.Map) AggregatedMetricValues(com.linkedin.cruisecontrol.monitor.sampling.aggregator.AggregatedMetricValues) ModelGeneration(com.linkedin.kafka.cruisecontrol.monitor.ModelGeneration) TopicPartition(org.apache.kafka.common.TopicPartition) BrokerCapacityInfo(com.linkedin.kafka.cruisecontrol.config.BrokerCapacityInfo) Set(java.util.Set) Test(org.junit.Test) PartitionInfo(org.apache.kafka.common.PartitionInfo) MetricValues(com.linkedin.cruisecontrol.monitor.sampling.aggregator.MetricValues) LOGDIR0(com.linkedin.kafka.cruisecontrol.common.TestConstants.LOGDIR0) Collectors(java.util.stream.Collectors) LOGDIR1(com.linkedin.kafka.cruisecontrol.common.TestConstants.LOGDIR1) TestConstants(com.linkedin.kafka.cruisecontrol.common.TestConstants) Broker(com.linkedin.kafka.cruisecontrol.model.Broker) List(java.util.List) Resource(com.linkedin.kafka.cruisecontrol.common.Resource) Node(org.apache.kafka.common.Node) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) HashMap(java.util.HashMap) PreferredLeaderElectionGoal(com.linkedin.kafka.cruisecontrol.analyzer.goals.PreferredLeaderElectionGoal) Replica(com.linkedin.kafka.cruisecontrol.model.Replica) ClusterModel(com.linkedin.kafka.cruisecontrol.model.ClusterModel) TopicPartition(org.apache.kafka.common.TopicPartition) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

Disk (com.linkedin.kafka.cruisecontrol.model.Disk)16 Broker (com.linkedin.kafka.cruisecontrol.model.Broker)10 Replica (com.linkedin.kafka.cruisecontrol.model.Replica)10 ArrayList (java.util.ArrayList)4 PriorityQueue (java.util.PriorityQueue)4 HashSet (java.util.HashSet)3 TopicPartition (org.apache.kafka.common.TopicPartition)3 AggregatedMetricValues (com.linkedin.cruisecontrol.monitor.sampling.aggregator.AggregatedMetricValues)2 MetricValues (com.linkedin.cruisecontrol.monitor.sampling.aggregator.MetricValues)2 PreferredLeaderElectionGoal (com.linkedin.kafka.cruisecontrol.analyzer.goals.PreferredLeaderElectionGoal)2 Resource (com.linkedin.kafka.cruisecontrol.common.Resource)2 TestConstants (com.linkedin.kafka.cruisecontrol.common.TestConstants)2 LOGDIR0 (com.linkedin.kafka.cruisecontrol.common.TestConstants.LOGDIR0)2 LOGDIR1 (com.linkedin.kafka.cruisecontrol.common.TestConstants.LOGDIR1)2 TOPIC0 (com.linkedin.kafka.cruisecontrol.common.TestConstants.TOPIC0)2 TOPIC1 (com.linkedin.kafka.cruisecontrol.common.TestConstants.TOPIC1)2 TOPIC2 (com.linkedin.kafka.cruisecontrol.common.TestConstants.TOPIC2)2 BrokerCapacityInfo (com.linkedin.kafka.cruisecontrol.config.BrokerCapacityInfo)2 ClusterModel (com.linkedin.kafka.cruisecontrol.model.ClusterModel)2 ReplicaPlacementInfo (com.linkedin.kafka.cruisecontrol.model.ReplicaPlacementInfo)2