Search in sources :

Example 81 with KafkaFuture

use of org.apache.kafka.common.KafkaFuture in project cruise-control by linkedin.

the class TopicMinIsrCacheTest method testPutTopicMinIsr.

@Test
public void testPutTopicMinIsr() throws ExecutionException, InterruptedException {
    EasyMock.expect(_mockTime.milliseconds()).andReturn(MOCK_TIME_MS).anyTimes();
    EasyMock.replay(_mockTime);
    TopicMinIsrCache topicMinIsrCache = new TopicMinIsrCache(MOCK_CONCURRENCY_ADJUSTER_MIN_ISR_RETENTION_MS, MOCK_CONCURRENCY_ADJUSTER_MIN_ISR_CACHE_SIZE, MOCK_CACHE_CLEANER_PERIOD, MOCK_CACHE_CLEANER_INITIAL_DELAY, _mockTime);
    // 0. Test null describeConfigsResult input
    DescribeConfigsResult describeConfigsResult = null;
    topicMinIsrCache.putTopicMinIsr(describeConfigsResult);
    assertTrue(topicMinIsrCache.minIsrWithTimeByTopic().isEmpty());
    // 1. Test (1.1) Successful put, (1.2) Put with TimeoutException
    describeConfigsResult = EasyMock.mock(DescribeConfigsResult.class);
    KafkaFuture<Config> describedConfigsFuture = EasyMock.createMock(KafkaFuture.class);
    Config topicConfig = EasyMock.createMock(Config.class);
    Map<ConfigResource, KafkaFuture<Config>> describeConfigsValues = Collections.singletonMap(MOCK_TOPIC_RESOURCE, describedConfigsFuture);
    EasyMock.expect(describeConfigsResult.values()).andReturn(describeConfigsValues).times(3);
    org.apache.kafka.common.errors.TimeoutException kafkaTimeoutException = new org.apache.kafka.common.errors.TimeoutException();
    EasyMock.expect(describedConfigsFuture.get()).andReturn(topicConfig).andThrow(new ExecutionException(kafkaTimeoutException));
    EasyMock.expect(topicConfig.get(EasyMock.eq(TopicConfig.MIN_IN_SYNC_REPLICAS_CONFIG))).andReturn(new ConfigEntry(TopicConfig.MIN_IN_SYNC_REPLICAS_CONFIG, MIN_IN_SYNC_REPLICAS_VALUE));
    EasyMock.replay(describeConfigsResult, describedConfigsFuture, topicConfig);
    // 1.2. TimeoutException -- config for MOCK_TOPIC is still cached.
    for (int i = 0; i < 2; i++) {
        topicMinIsrCache.putTopicMinIsr(describeConfigsResult);
        Map<String, TopicMinIsrCache.MinIsrWithTime> minIsrWithTimeByTopic = topicMinIsrCache.minIsrWithTimeByTopic();
        assertEquals(1, minIsrWithTimeByTopic.size());
        assertEquals(Integer.parseInt(MIN_IN_SYNC_REPLICAS_VALUE), minIsrWithTimeByTopic.get(MOCK_TOPIC).minISR());
        assertEquals(MOCK_TIME_MS, minIsrWithTimeByTopic.get(MOCK_TOPIC).timeMs());
    }
    EasyMock.verify(_mockTime, describeConfigsResult, describedConfigsFuture, topicConfig);
    // 2. Test put for 2 described topic configs and one described config having ExecutionException that is not due to TimeoutException.
    EasyMock.reset(describeConfigsResult, describedConfigsFuture);
    KafkaFuture<Config> describedConfigsFuture2 = EasyMock.createMock(KafkaFuture.class);
    Config topicConfig2 = EasyMock.createMock(Config.class);
    Map<ConfigResource, KafkaFuture<Config>> describeConfigsWithMultipleValues = new LinkedHashMap<>(2);
    describeConfigsWithMultipleValues.put(MOCK_TOPIC_RESOURCE_2, describedConfigsFuture2);
    describeConfigsWithMultipleValues.put(MOCK_TOPIC_RESOURCE_3, describedConfigsFuture);
    EasyMock.expect(describeConfigsResult.values()).andReturn(describeConfigsWithMultipleValues);
    EasyMock.expect(describedConfigsFuture.get()).andThrow(new ExecutionException(Errors.UNKNOWN_TOPIC_OR_PARTITION.exception()));
    EasyMock.expect(describedConfigsFuture2.get()).andReturn(topicConfig2);
    EasyMock.expect(topicConfig2.get(EasyMock.eq(TopicConfig.MIN_IN_SYNC_REPLICAS_CONFIG))).andReturn(new ConfigEntry(TopicConfig.MIN_IN_SYNC_REPLICAS_CONFIG, MIN_IN_SYNC_REPLICAS_VALUE_2));
    EasyMock.replay(describeConfigsResult, describedConfigsFuture, describedConfigsFuture2, topicConfig2);
    topicMinIsrCache.putTopicMinIsr(describeConfigsResult);
    Map<String, TopicMinIsrCache.MinIsrWithTime> minIsrWithTimeByTopic = topicMinIsrCache.minIsrWithTimeByTopic();
    assertEquals(2, minIsrWithTimeByTopic.size());
    assertEquals(Integer.parseInt(MIN_IN_SYNC_REPLICAS_VALUE), minIsrWithTimeByTopic.get(MOCK_TOPIC).minISR());
    assertEquals(Integer.parseInt(MIN_IN_SYNC_REPLICAS_VALUE_2), minIsrWithTimeByTopic.get(MOCK_TOPIC_2).minISR());
    assertEquals(MOCK_TIME_MS, minIsrWithTimeByTopic.get(MOCK_TOPIC).timeMs());
    assertEquals(MOCK_TIME_MS, minIsrWithTimeByTopic.get(MOCK_TOPIC_2).timeMs());
    EasyMock.verify(_mockTime, describeConfigsResult, describedConfigsFuture, describedConfigsFuture2, topicConfig2);
    // 3. Test put for 2 described topic configs and one described config having InterruptedException.
    EasyMock.reset(describeConfigsResult, describedConfigsFuture);
    KafkaFuture<Config> describedConfigsFuture3 = EasyMock.createMock(KafkaFuture.class);
    Config topicConfig3 = EasyMock.createMock(Config.class);
    describeConfigsWithMultipleValues = new LinkedHashMap<>(2);
    describeConfigsWithMultipleValues.put(MOCK_TOPIC_RESOURCE_2, describedConfigsFuture);
    describeConfigsWithMultipleValues.put(MOCK_TOPIC_RESOURCE_3, describedConfigsFuture3);
    EasyMock.expect(describeConfigsResult.values()).andReturn(describeConfigsWithMultipleValues);
    EasyMock.expect(describedConfigsFuture.get()).andThrow(new InterruptedException());
    EasyMock.expect(describedConfigsFuture3.get()).andReturn(topicConfig3);
    EasyMock.expect(topicConfig3.get(EasyMock.eq(TopicConfig.MIN_IN_SYNC_REPLICAS_CONFIG))).andReturn(new ConfigEntry(TopicConfig.MIN_IN_SYNC_REPLICAS_CONFIG, MIN_IN_SYNC_REPLICAS_VALUE_3));
    EasyMock.replay(describeConfigsResult, describedConfigsFuture, describedConfigsFuture3, topicConfig3);
    topicMinIsrCache.putTopicMinIsr(describeConfigsResult);
    minIsrWithTimeByTopic = topicMinIsrCache.minIsrWithTimeByTopic();
    assertEquals(3, minIsrWithTimeByTopic.size());
    assertEquals(Integer.parseInt(MIN_IN_SYNC_REPLICAS_VALUE), minIsrWithTimeByTopic.get(MOCK_TOPIC).minISR());
    assertEquals(Integer.parseInt(MIN_IN_SYNC_REPLICAS_VALUE_2), minIsrWithTimeByTopic.get(MOCK_TOPIC_2).minISR());
    assertEquals(Integer.parseInt(MIN_IN_SYNC_REPLICAS_VALUE_3), minIsrWithTimeByTopic.get(MOCK_TOPIC_3).minISR());
    assertEquals(MOCK_TIME_MS, minIsrWithTimeByTopic.get(MOCK_TOPIC).timeMs());
    assertEquals(MOCK_TIME_MS, minIsrWithTimeByTopic.get(MOCK_TOPIC_2).timeMs());
    assertEquals(MOCK_TIME_MS, minIsrWithTimeByTopic.get(MOCK_TOPIC_3).timeMs());
    EasyMock.verify(_mockTime, describeConfigsResult, describedConfigsFuture, describedConfigsFuture3, topicConfig3);
    // 4. Test putting one more new entry when the cache is full.
    EasyMock.reset(describeConfigsResult);
    KafkaFuture<Config> describedConfigsFuture4 = EasyMock.createMock(KafkaFuture.class);
    Config topicConfig4 = EasyMock.createMock(Config.class);
    describeConfigsValues = Collections.singletonMap(MOCK_TOPIC_RESOURCE_4, describedConfigsFuture4);
    EasyMock.expect(describeConfigsResult.values()).andReturn(describeConfigsValues);
    EasyMock.expect(describedConfigsFuture4.get()).andReturn(topicConfig4);
    EasyMock.expect(topicConfig4.get(EasyMock.eq(TopicConfig.MIN_IN_SYNC_REPLICAS_CONFIG))).andReturn(new ConfigEntry(TopicConfig.MIN_IN_SYNC_REPLICAS_CONFIG, MIN_IN_SYNC_REPLICAS_VALUE_4));
    EasyMock.replay(describeConfigsResult, describedConfigsFuture4, topicConfig4);
    topicMinIsrCache.putTopicMinIsr(describeConfigsResult);
    minIsrWithTimeByTopic = topicMinIsrCache.minIsrWithTimeByTopic();
    assertEquals(3, minIsrWithTimeByTopic.size());
    assertNull(minIsrWithTimeByTopic.get(MOCK_TOPIC));
    assertEquals(Integer.parseInt(MIN_IN_SYNC_REPLICAS_VALUE_2), minIsrWithTimeByTopic.get(MOCK_TOPIC_2).minISR());
    assertEquals(Integer.parseInt(MIN_IN_SYNC_REPLICAS_VALUE_3), minIsrWithTimeByTopic.get(MOCK_TOPIC_3).minISR());
    assertEquals(Integer.parseInt(MIN_IN_SYNC_REPLICAS_VALUE_4), minIsrWithTimeByTopic.get(MOCK_TOPIC_4).minISR());
    assertEquals(MOCK_TIME_MS, minIsrWithTimeByTopic.get(MOCK_TOPIC_2).timeMs());
    assertEquals(MOCK_TIME_MS, minIsrWithTimeByTopic.get(MOCK_TOPIC_3).timeMs());
    assertEquals(MOCK_TIME_MS, minIsrWithTimeByTopic.get(MOCK_TOPIC_4).timeMs());
    EasyMock.verify(_mockTime, describeConfigsResult, describedConfigsFuture4, topicConfig4);
}
Also used : KafkaFuture(org.apache.kafka.common.KafkaFuture) Config(org.apache.kafka.clients.admin.Config) TopicConfig(org.apache.kafka.common.config.TopicConfig) DescribeConfigsResult(org.apache.kafka.clients.admin.DescribeConfigsResult) ConfigResource(org.apache.kafka.common.config.ConfigResource) LinkedHashMap(java.util.LinkedHashMap) ConfigEntry(org.apache.kafka.clients.admin.ConfigEntry) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 82 with KafkaFuture

use of org.apache.kafka.common.KafkaFuture in project cruise-control by linkedin.

the class SamplingUtilsTest method testMaybeIncreasePartitionCount.

@Test
public void testMaybeIncreasePartitionCount() throws InterruptedException, ExecutionException, TimeoutException {
    AdminClient adminClient = EasyMock.createMock(AdminClient.class);
    NewTopic topicToAddPartitions = wrapTopic(MOCK_TOPIC, MOCK_DESIRED_PARTITION_COUNT, MOCK_REPLICATION_FACTOR, MOCK_DESIRED_RETENTION_MS);
    DescribeTopicsResult describeTopicsResult = EasyMock.createMock(DescribeTopicsResult.class);
    KafkaFuture<TopicDescription> topicDescriptionFuture = EasyMock.createMock(KafkaFuture.class);
    TopicDescription topicDescription = EasyMock.createMock(TopicDescription.class);
    Map<String, KafkaFuture<TopicDescription>> describeTopicsValues = Collections.singletonMap(MOCK_TOPIC, topicDescriptionFuture);
    Map<String, KafkaFuture<Void>> createPartitionsValues = Collections.singletonMap(MOCK_TOPIC, EasyMock.createMock(KafkaFuture.class));
    CreatePartitionsResult createPartitionsResult = EasyMock.createMock(CreatePartitionsResult.class);
    EasyMock.expect(adminClient.describeTopics(Collections.singletonList(MOCK_TOPIC))).andReturn(describeTopicsResult);
    EasyMock.expect(describeTopicsResult.values()).andReturn(describeTopicsValues);
    EasyMock.expect(topicDescriptionFuture.get(CLIENT_REQUEST_TIMEOUT_MS, TimeUnit.MILLISECONDS)).andReturn(topicDescription);
    EasyMock.expect(topicDescription.partitions()).andReturn(MOCK_PARTITIONS);
    EasyMock.expect(adminClient.createPartitions(Collections.singletonMap(MOCK_TOPIC, EasyMock.anyObject()))).andReturn(createPartitionsResult);
    EasyMock.expect(createPartitionsResult.values()).andReturn(createPartitionsValues);
    EasyMock.replay(adminClient, describeTopicsResult, topicDescriptionFuture, topicDescription, createPartitionsResult);
    KafkaCruiseControlUtils.CompletionType increasePartitionCount = maybeIncreasePartitionCount(adminClient, topicToAddPartitions);
    EasyMock.verify(adminClient, describeTopicsResult, topicDescriptionFuture, topicDescription, createPartitionsResult);
    assertEquals(KafkaCruiseControlUtils.CompletionType.COMPLETED, increasePartitionCount);
}
Also used : KafkaFuture(org.apache.kafka.common.KafkaFuture) KafkaCruiseControlUtils(com.linkedin.kafka.cruisecontrol.KafkaCruiseControlUtils) CreatePartitionsResult(org.apache.kafka.clients.admin.CreatePartitionsResult) DescribeTopicsResult(org.apache.kafka.clients.admin.DescribeTopicsResult) NewTopic(org.apache.kafka.clients.admin.NewTopic) TopicDescription(org.apache.kafka.clients.admin.TopicDescription) AdminClient(org.apache.kafka.clients.admin.AdminClient) Test(org.junit.Test)

Example 83 with KafkaFuture

use of org.apache.kafka.common.KafkaFuture in project cruise-control by linkedin.

the class SamplingUtilsTest method testMaybeUpdateTopicConfig.

@Test
public void testMaybeUpdateTopicConfig() throws InterruptedException, ExecutionException, TimeoutException {
    AdminClient adminClient = EasyMock.createMock(AdminClient.class);
    DescribeConfigsResult describeConfigsResult = EasyMock.createMock(DescribeConfigsResult.class);
    KafkaFuture<Config> describedConfigsFuture = EasyMock.createMock(KafkaFuture.class);
    Config topicConfig = EasyMock.createMock(Config.class);
    AlterConfigsResult alterConfigsResult = EasyMock.createMock(AlterConfigsResult.class);
    Set<AlterConfigOp> alterConfigOps = Collections.singleton(new AlterConfigOp(new ConfigEntry(RetentionMsProp(), Long.toString(MOCK_DESIRED_RETENTION_MS)), AlterConfigOp.OpType.SET));
    Map<ConfigResource, KafkaFuture<Config>> describeConfigsValues = Collections.singletonMap(MOCK_TOPIC_RESOURCE, describedConfigsFuture);
    Map<ConfigResource, KafkaFuture<Void>> alterConfigsValues = Collections.singletonMap(MOCK_TOPIC_RESOURCE, EasyMock.createMock(KafkaFuture.class));
    NewTopic topicToUpdateConfigs = wrapTopic(MOCK_TOPIC, MOCK_PARTITION_COUNT, MOCK_REPLICATION_FACTOR, MOCK_DESIRED_RETENTION_MS);
    EasyMock.expect(adminClient.describeConfigs(EasyMock.eq(Collections.singleton(MOCK_TOPIC_RESOURCE)))).andReturn(describeConfigsResult);
    EasyMock.expect(describeConfigsResult.values()).andReturn(describeConfigsValues);
    EasyMock.expect(describedConfigsFuture.get(CLIENT_REQUEST_TIMEOUT_MS, TimeUnit.MILLISECONDS)).andReturn(topicConfig);
    EasyMock.expect(topicConfig.get(EasyMock.eq(CleanupPolicyProp()))).andReturn(new ConfigEntry(CleanupPolicyProp(), DEFAULT_CLEANUP_POLICY));
    EasyMock.expect(topicConfig.get(EasyMock.eq(RetentionMsProp()))).andReturn(new ConfigEntry(RetentionMsProp(), MOCK_CURRENT_RETENTION_MS));
    EasyMock.expect(adminClient.incrementalAlterConfigs(EasyMock.eq(Collections.singletonMap(MOCK_TOPIC_RESOURCE, alterConfigOps)))).andReturn(alterConfigsResult);
    EasyMock.expect(alterConfigsResult.values()).andReturn(alterConfigsValues);
    EasyMock.replay(adminClient, describeConfigsResult, describedConfigsFuture, topicConfig, alterConfigsResult);
    boolean updateTopicConfig = maybeUpdateTopicConfig(adminClient, topicToUpdateConfigs);
    EasyMock.verify(adminClient, describeConfigsResult, describedConfigsFuture, topicConfig, alterConfigsResult);
    assertTrue(updateTopicConfig);
}
Also used : KafkaFuture(org.apache.kafka.common.KafkaFuture) Config(org.apache.kafka.clients.admin.Config) KafkaCruiseControlUtils.maybeUpdateTopicConfig(com.linkedin.kafka.cruisecontrol.KafkaCruiseControlUtils.maybeUpdateTopicConfig) DescribeConfigsResult(org.apache.kafka.clients.admin.DescribeConfigsResult) AlterConfigOp(org.apache.kafka.clients.admin.AlterConfigOp) ConfigResource(org.apache.kafka.common.config.ConfigResource) ConfigEntry(org.apache.kafka.clients.admin.ConfigEntry) NewTopic(org.apache.kafka.clients.admin.NewTopic) AlterConfigsResult(org.apache.kafka.clients.admin.AlterConfigsResult) AdminClient(org.apache.kafka.clients.admin.AdminClient) Test(org.junit.Test)

Example 84 with KafkaFuture

use of org.apache.kafka.common.KafkaFuture in project cruise-control by linkedin.

the class KafkaCruiseControlUtilsTest method testCreateTopic.

@Test
public void testCreateTopic() throws ExecutionException, InterruptedException, TimeoutException {
    AdminClient adminClient = EasyMock.createMock(AdminClient.class);
    CreateTopicsResult createTopicsResult = EasyMock.createMock(CreateTopicsResult.class);
    KafkaFuture<Void> createTopicsResultFuture = EasyMock.createMock(KafkaFuture.class);
    Map<String, KafkaFuture<Void>> createTopicsResultValues = Collections.singletonMap(TEST_TOPIC.name(), createTopicsResultFuture);
    // 1. Existing topic
    EasyMock.expect(adminClient.createTopics(Collections.singletonList(TEST_TOPIC))).andReturn(createTopicsResult).once();
    EasyMock.expect(createTopicsResult.values()).andReturn(createTopicsResultValues).once();
    EasyMock.expect(createTopicsResultFuture.get(CLIENT_REQUEST_TIMEOUT_MS, TimeUnit.MILLISECONDS)).andThrow(new ExecutionException(Errors.TOPIC_ALREADY_EXISTS.exception()));
    EasyMock.replay(adminClient, createTopicsResult, createTopicsResultFuture);
    assertFalse(createTopic(adminClient, TEST_TOPIC));
    EasyMock.verify(adminClient, createTopicsResult, createTopicsResultFuture);
    EasyMock.reset(adminClient, createTopicsResult, createTopicsResultFuture);
    // 2. New topic
    EasyMock.expect(adminClient.createTopics(Collections.singletonList(TEST_TOPIC))).andReturn(createTopicsResult).once();
    EasyMock.expect(createTopicsResult.values()).andReturn(createTopicsResultValues).once();
    EasyMock.expect(createTopicsResultFuture.get(CLIENT_REQUEST_TIMEOUT_MS, TimeUnit.MILLISECONDS)).andReturn(null).once();
    EasyMock.replay(adminClient, createTopicsResult, createTopicsResultFuture);
    assertTrue(createTopic(adminClient, TEST_TOPIC));
    EasyMock.verify(adminClient, createTopicsResult, createTopicsResultFuture);
}
Also used : CreateTopicsResult(org.apache.kafka.clients.admin.CreateTopicsResult) KafkaFuture(org.apache.kafka.common.KafkaFuture) ExecutionException(java.util.concurrent.ExecutionException) AdminClient(org.apache.kafka.clients.admin.AdminClient) Test(org.junit.Test)

Aggregations

KafkaFuture (org.apache.kafka.common.KafkaFuture)84 HashMap (java.util.HashMap)59 Map (java.util.Map)43 KafkaFutureImpl (org.apache.kafka.common.internals.KafkaFutureImpl)31 ExecutionException (java.util.concurrent.ExecutionException)30 TimeoutException (org.apache.kafka.common.errors.TimeoutException)21 ArrayList (java.util.ArrayList)16 TopicPartition (org.apache.kafka.common.TopicPartition)16 ConfigResource (org.apache.kafka.common.config.ConfigResource)16 UnknownTopicOrPartitionException (org.apache.kafka.common.errors.UnknownTopicOrPartitionException)15 Test (org.junit.jupiter.api.Test)15 HashSet (java.util.HashSet)14 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)14 Test (org.junit.Test)12 TopicPartitionReplica (org.apache.kafka.common.TopicPartitionReplica)10 TopicExistsException (org.apache.kafka.common.errors.TopicExistsException)10 NewTopic (org.apache.kafka.clients.admin.NewTopic)8 AbstractResponse (org.apache.kafka.common.requests.AbstractResponse)8 AdminClient (org.apache.kafka.clients.admin.AdminClient)7 ReplicaLogDirInfo (org.apache.kafka.clients.admin.DescribeReplicaLogDirsResult.ReplicaLogDirInfo)7