Search in sources :

Example 86 with Time

use of org.apache.kafka.common.utils.Time in project kafka by apache.

the class MetadataTest method testMetadataMerge.

@Test
public void testMetadataMerge() {
    Time time = new MockTime();
    Map<String, Uuid> topicIds = new HashMap<>();
    final AtomicReference<Set<String>> retainTopics = new AtomicReference<>(new HashSet<>());
    metadata = new Metadata(refreshBackoffMs, metadataExpireMs, new LogContext(), new ClusterResourceListeners()) {

        @Override
        protected boolean retainTopic(String topic, boolean isInternal, long nowMs) {
            return retainTopics.get().contains(topic);
        }
    };
    // Initialize a metadata instance with two topic variants "old" and "keep". Both will be retained.
    String oldClusterId = "oldClusterId";
    int oldNodes = 2;
    Map<String, Errors> oldTopicErrors = new HashMap<>();
    oldTopicErrors.put("oldInvalidTopic", Errors.INVALID_TOPIC_EXCEPTION);
    oldTopicErrors.put("keepInvalidTopic", Errors.INVALID_TOPIC_EXCEPTION);
    oldTopicErrors.put("oldUnauthorizedTopic", Errors.TOPIC_AUTHORIZATION_FAILED);
    oldTopicErrors.put("keepUnauthorizedTopic", Errors.TOPIC_AUTHORIZATION_FAILED);
    Map<String, Integer> oldTopicPartitionCounts = new HashMap<>();
    oldTopicPartitionCounts.put("oldValidTopic", 2);
    oldTopicPartitionCounts.put("keepValidTopic", 3);
    retainTopics.set(Utils.mkSet("oldInvalidTopic", "keepInvalidTopic", "oldUnauthorizedTopic", "keepUnauthorizedTopic", "oldValidTopic", "keepValidTopic"));
    topicIds.put("oldValidTopic", Uuid.randomUuid());
    topicIds.put("keepValidTopic", Uuid.randomUuid());
    MetadataResponse metadataResponse = RequestTestUtils.metadataUpdateWithIds(oldClusterId, oldNodes, oldTopicErrors, oldTopicPartitionCounts, _tp -> 100, topicIds);
    metadata.updateWithCurrentRequestVersion(metadataResponse, true, time.milliseconds());
    Map<String, Uuid> metadataTopicIds1 = metadata.topicIds();
    retainTopics.get().forEach(topic -> assertEquals(metadataTopicIds1.get(topic), topicIds.get(topic)));
    // Update the metadata to add a new topic variant, "new", which will be retained with "keep". Note this
    // means that all of the "old" topics should be dropped.
    Cluster cluster = metadata.fetch();
    assertEquals(cluster.clusterResource().clusterId(), oldClusterId);
    assertEquals(cluster.nodes().size(), oldNodes);
    assertEquals(cluster.invalidTopics(), new HashSet<>(Arrays.asList("oldInvalidTopic", "keepInvalidTopic")));
    assertEquals(cluster.unauthorizedTopics(), new HashSet<>(Arrays.asList("oldUnauthorizedTopic", "keepUnauthorizedTopic")));
    assertEquals(cluster.topics(), new HashSet<>(Arrays.asList("oldValidTopic", "keepValidTopic")));
    assertEquals(cluster.partitionsForTopic("oldValidTopic").size(), 2);
    assertEquals(cluster.partitionsForTopic("keepValidTopic").size(), 3);
    assertEquals(new HashSet<>(cluster.topicIds()), new HashSet<>(topicIds.values()));
    String newClusterId = "newClusterId";
    int newNodes = oldNodes + 1;
    Map<String, Errors> newTopicErrors = new HashMap<>();
    newTopicErrors.put("newInvalidTopic", Errors.INVALID_TOPIC_EXCEPTION);
    newTopicErrors.put("newUnauthorizedTopic", Errors.TOPIC_AUTHORIZATION_FAILED);
    Map<String, Integer> newTopicPartitionCounts = new HashMap<>();
    newTopicPartitionCounts.put("keepValidTopic", 2);
    newTopicPartitionCounts.put("newValidTopic", 4);
    retainTopics.set(Utils.mkSet("keepInvalidTopic", "newInvalidTopic", "keepUnauthorizedTopic", "newUnauthorizedTopic", "keepValidTopic", "newValidTopic"));
    topicIds.put("newValidTopic", Uuid.randomUuid());
    metadataResponse = RequestTestUtils.metadataUpdateWithIds(newClusterId, newNodes, newTopicErrors, newTopicPartitionCounts, _tp -> 200, topicIds);
    metadata.updateWithCurrentRequestVersion(metadataResponse, true, time.milliseconds());
    topicIds.remove("oldValidTopic");
    Map<String, Uuid> metadataTopicIds2 = metadata.topicIds();
    retainTopics.get().forEach(topic -> assertEquals(metadataTopicIds2.get(topic), topicIds.get(topic)));
    assertNull(metadataTopicIds2.get("oldValidTopic"));
    cluster = metadata.fetch();
    assertEquals(cluster.clusterResource().clusterId(), newClusterId);
    assertEquals(cluster.nodes().size(), newNodes);
    assertEquals(cluster.invalidTopics(), new HashSet<>(Arrays.asList("keepInvalidTopic", "newInvalidTopic")));
    assertEquals(cluster.unauthorizedTopics(), new HashSet<>(Arrays.asList("keepUnauthorizedTopic", "newUnauthorizedTopic")));
    assertEquals(cluster.topics(), new HashSet<>(Arrays.asList("keepValidTopic", "newValidTopic")));
    assertEquals(cluster.partitionsForTopic("keepValidTopic").size(), 2);
    assertEquals(cluster.partitionsForTopic("newValidTopic").size(), 4);
    assertEquals(new HashSet<>(cluster.topicIds()), new HashSet<>(topicIds.values()));
    // Perform another metadata update, but this time all topic metadata should be cleared.
    retainTopics.set(Collections.emptySet());
    metadataResponse = RequestTestUtils.metadataUpdateWithIds(newClusterId, newNodes, newTopicErrors, newTopicPartitionCounts, _tp -> 300, topicIds);
    metadata.updateWithCurrentRequestVersion(metadataResponse, true, time.milliseconds());
    Map<String, Uuid> metadataTopicIds3 = metadata.topicIds();
    topicIds.forEach((topicName, topicId) -> assertNull(metadataTopicIds3.get(topicName)));
    cluster = metadata.fetch();
    assertEquals(cluster.clusterResource().clusterId(), newClusterId);
    assertEquals(cluster.nodes().size(), newNodes);
    assertEquals(cluster.invalidTopics(), Collections.emptySet());
    assertEquals(cluster.unauthorizedTopics(), Collections.emptySet());
    assertEquals(cluster.topics(), Collections.emptySet());
    assertTrue(cluster.topicIds().isEmpty());
}
Also used : Uuid(org.apache.kafka.common.Uuid) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) MessageUtil(org.apache.kafka.common.protocol.MessageUtil) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) MockTime(org.apache.kafka.common.utils.MockTime) Arrays(java.util.Arrays) MockClusterResourceListener(org.apache.kafka.test.MockClusterResourceListener) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) RequestTestUtils(org.apache.kafka.common.requests.RequestTestUtils) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuffer(java.nio.ByteBuffer) HashSet(java.util.HashSet) Cluster(org.apache.kafka.common.Cluster) MetadataResponseBrokerCollection(org.apache.kafka.common.message.MetadataResponseData.MetadataResponseBrokerCollection) MetadataResponseTopic(org.apache.kafka.common.message.MetadataResponseData.MetadataResponseTopic) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) MetadataRequest(org.apache.kafka.common.requests.MetadataRequest) LogContext(org.apache.kafka.common.utils.LogContext) Map(java.util.Map) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) TestUtils.assertOptional(org.apache.kafka.test.TestUtils.assertOptional) Topic(org.apache.kafka.common.internals.Topic) Utils(org.apache.kafka.common.utils.Utils) TopicPartition(org.apache.kafka.common.TopicPartition) MetadataResponseTopicCollection(org.apache.kafka.common.message.MetadataResponseData.MetadataResponseTopicCollection) Time(org.apache.kafka.common.utils.Time) InvalidTopicException(org.apache.kafka.common.errors.InvalidTopicException) Set(java.util.Set) ClusterResourceListeners(org.apache.kafka.common.internals.ClusterResourceListeners) ApiKeys(org.apache.kafka.common.protocol.ApiKeys) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.jupiter.api.Test) Objects(java.util.Objects) List(java.util.List) MetadataResponsePartition(org.apache.kafka.common.message.MetadataResponseData.MetadataResponsePartition) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) TopicAuthorizationException(org.apache.kafka.common.errors.TopicAuthorizationException) Errors(org.apache.kafka.common.protocol.Errors) Optional(java.util.Optional) Node(org.apache.kafka.common.Node) MetadataResponseData(org.apache.kafka.common.message.MetadataResponseData) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) Collections(java.util.Collections) ClusterResourceListeners(org.apache.kafka.common.internals.ClusterResourceListeners) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) LogContext(org.apache.kafka.common.utils.LogContext) Cluster(org.apache.kafka.common.Cluster) MockTime(org.apache.kafka.common.utils.MockTime) Time(org.apache.kafka.common.utils.Time) AtomicReference(java.util.concurrent.atomic.AtomicReference) Errors(org.apache.kafka.common.protocol.Errors) Uuid(org.apache.kafka.common.Uuid) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.jupiter.api.Test)

Example 87 with Time

use of org.apache.kafka.common.utils.Time in project kafka by apache.

the class MetadataTest method testInvalidTopicError.

@Test
public void testInvalidTopicError() {
    Time time = new MockTime();
    String invalidTopic = "topic dfsa";
    MetadataResponse invalidTopicResponse = RequestTestUtils.metadataUpdateWith("clusterId", 1, Collections.singletonMap(invalidTopic, Errors.INVALID_TOPIC_EXCEPTION), Collections.emptyMap());
    metadata.updateWithCurrentRequestVersion(invalidTopicResponse, false, time.milliseconds());
    InvalidTopicException e = assertThrows(InvalidTopicException.class, () -> metadata.maybeThrowAnyException());
    assertEquals(Collections.singleton(invalidTopic), e.invalidTopics());
    // We clear the exception once it has been raised to the user
    metadata.maybeThrowAnyException();
    // Reset the invalid topic error
    metadata.updateWithCurrentRequestVersion(invalidTopicResponse, false, time.milliseconds());
    // If we get a good update, the error should clear even if we haven't had a chance to raise it to the user
    metadata.updateWithCurrentRequestVersion(emptyMetadataResponse(), false, time.milliseconds());
    metadata.maybeThrowAnyException();
}
Also used : MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) InvalidTopicException(org.apache.kafka.common.errors.InvalidTopicException) MockTime(org.apache.kafka.common.utils.MockTime) Time(org.apache.kafka.common.utils.Time) MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.jupiter.api.Test)

Example 88 with Time

use of org.apache.kafka.common.utils.Time in project kafka by apache.

the class MetadataTest method testTopicAuthorizationError.

@Test
public void testTopicAuthorizationError() {
    Time time = new MockTime();
    String invalidTopic = "foo";
    MetadataResponse unauthorizedTopicResponse = RequestTestUtils.metadataUpdateWith("clusterId", 1, Collections.singletonMap(invalidTopic, Errors.TOPIC_AUTHORIZATION_FAILED), Collections.emptyMap());
    metadata.updateWithCurrentRequestVersion(unauthorizedTopicResponse, false, time.milliseconds());
    TopicAuthorizationException e = assertThrows(TopicAuthorizationException.class, () -> metadata.maybeThrowAnyException());
    assertEquals(Collections.singleton(invalidTopic), e.unauthorizedTopics());
    // We clear the exception once it has been raised to the user
    metadata.maybeThrowAnyException();
    // Reset the unauthorized topic error
    metadata.updateWithCurrentRequestVersion(unauthorizedTopicResponse, false, time.milliseconds());
    // If we get a good update, the error should clear even if we haven't had a chance to raise it to the user
    metadata.updateWithCurrentRequestVersion(emptyMetadataResponse(), false, time.milliseconds());
    metadata.maybeThrowAnyException();
}
Also used : MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) MockTime(org.apache.kafka.common.utils.MockTime) Time(org.apache.kafka.common.utils.Time) MockTime(org.apache.kafka.common.utils.MockTime) TopicAuthorizationException(org.apache.kafka.common.errors.TopicAuthorizationException) Test(org.junit.jupiter.api.Test)

Example 89 with Time

use of org.apache.kafka.common.utils.Time in project kafka by apache.

the class MetadataTest method testMetadataMergeOnIdDowngrade.

@Test
public void testMetadataMergeOnIdDowngrade() {
    Time time = new MockTime();
    Map<String, Uuid> topicIds = new HashMap<>();
    final AtomicReference<Set<String>> retainTopics = new AtomicReference<>(new HashSet<>());
    metadata = new Metadata(refreshBackoffMs, metadataExpireMs, new LogContext(), new ClusterResourceListeners()) {

        @Override
        protected boolean retainTopic(String topic, boolean isInternal, long nowMs) {
            return retainTopics.get().contains(topic);
        }
    };
    // Initialize a metadata instance with two topics. Both will be retained.
    String clusterId = "clusterId";
    int nodes = 2;
    Map<String, Integer> topicPartitionCounts = new HashMap<>();
    topicPartitionCounts.put("validTopic1", 2);
    topicPartitionCounts.put("validTopic2", 3);
    retainTopics.set(Utils.mkSet("validTopic1", "validTopic2"));
    topicIds.put("validTopic1", Uuid.randomUuid());
    topicIds.put("validTopic2", Uuid.randomUuid());
    MetadataResponse metadataResponse = RequestTestUtils.metadataUpdateWithIds(clusterId, nodes, Collections.emptyMap(), topicPartitionCounts, _tp -> 100, topicIds);
    metadata.updateWithCurrentRequestVersion(metadataResponse, true, time.milliseconds());
    Map<String, Uuid> metadataTopicIds1 = metadata.topicIds();
    retainTopics.get().forEach(topic -> assertEquals(metadataTopicIds1.get(topic), topicIds.get(topic)));
    // Try removing the topic ID from keepValidTopic (simulating receiving a request from a controller with an older IBP)
    topicIds.remove("validTopic1");
    metadataResponse = RequestTestUtils.metadataUpdateWithIds(clusterId, nodes, Collections.emptyMap(), topicPartitionCounts, _tp -> 200, topicIds);
    metadata.updateWithCurrentRequestVersion(metadataResponse, true, time.milliseconds());
    Map<String, Uuid> metadataTopicIds2 = metadata.topicIds();
    retainTopics.get().forEach(topic -> assertEquals(metadataTopicIds2.get(topic), topicIds.get(topic)));
    Cluster cluster = metadata.fetch();
    // We still have the topic, but it just doesn't have an ID.
    assertEquals(Utils.mkSet("validTopic1", "validTopic2"), cluster.topics());
    assertEquals(2, cluster.partitionsForTopic("validTopic1").size());
    assertEquals(new HashSet<>(topicIds.values()), new HashSet<>(cluster.topicIds()));
    assertEquals(Uuid.ZERO_UUID, cluster.topicId("validTopic1"));
}
Also used : Uuid(org.apache.kafka.common.Uuid) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) MessageUtil(org.apache.kafka.common.protocol.MessageUtil) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) MockTime(org.apache.kafka.common.utils.MockTime) Arrays(java.util.Arrays) MockClusterResourceListener(org.apache.kafka.test.MockClusterResourceListener) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) RequestTestUtils(org.apache.kafka.common.requests.RequestTestUtils) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuffer(java.nio.ByteBuffer) HashSet(java.util.HashSet) Cluster(org.apache.kafka.common.Cluster) MetadataResponseBrokerCollection(org.apache.kafka.common.message.MetadataResponseData.MetadataResponseBrokerCollection) MetadataResponseTopic(org.apache.kafka.common.message.MetadataResponseData.MetadataResponseTopic) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) MetadataRequest(org.apache.kafka.common.requests.MetadataRequest) LogContext(org.apache.kafka.common.utils.LogContext) Map(java.util.Map) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) TestUtils.assertOptional(org.apache.kafka.test.TestUtils.assertOptional) Topic(org.apache.kafka.common.internals.Topic) Utils(org.apache.kafka.common.utils.Utils) TopicPartition(org.apache.kafka.common.TopicPartition) MetadataResponseTopicCollection(org.apache.kafka.common.message.MetadataResponseData.MetadataResponseTopicCollection) Time(org.apache.kafka.common.utils.Time) InvalidTopicException(org.apache.kafka.common.errors.InvalidTopicException) Set(java.util.Set) ClusterResourceListeners(org.apache.kafka.common.internals.ClusterResourceListeners) ApiKeys(org.apache.kafka.common.protocol.ApiKeys) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.jupiter.api.Test) Objects(java.util.Objects) List(java.util.List) MetadataResponsePartition(org.apache.kafka.common.message.MetadataResponseData.MetadataResponsePartition) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) TopicAuthorizationException(org.apache.kafka.common.errors.TopicAuthorizationException) Errors(org.apache.kafka.common.protocol.Errors) Optional(java.util.Optional) Node(org.apache.kafka.common.Node) MetadataResponseData(org.apache.kafka.common.message.MetadataResponseData) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) Collections(java.util.Collections) ClusterResourceListeners(org.apache.kafka.common.internals.ClusterResourceListeners) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) LogContext(org.apache.kafka.common.utils.LogContext) Cluster(org.apache.kafka.common.Cluster) MockTime(org.apache.kafka.common.utils.MockTime) Time(org.apache.kafka.common.utils.Time) AtomicReference(java.util.concurrent.atomic.AtomicReference) Uuid(org.apache.kafka.common.Uuid) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.jupiter.api.Test)

Example 90 with Time

use of org.apache.kafka.common.utils.Time in project kafka by apache.

the class KafkaAdminClientTest method testOffsetCommitNumRetries.

@Test
public void testOffsetCommitNumRetries() throws Exception {
    final Cluster cluster = mockCluster(3, 0);
    final Time time = new MockTime();
    try (AdminClientUnitTestEnv env = new AdminClientUnitTestEnv(time, cluster, AdminClientConfig.RETRIES_CONFIG, "0")) {
        env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
        final TopicPartition tp1 = new TopicPartition("foo", 0);
        env.kafkaClient().prepareResponse(prepareFindCoordinatorResponse(Errors.NONE, env.cluster().controller()));
        env.kafkaClient().prepareResponse(prepareOffsetCommitResponse(tp1, Errors.NOT_COORDINATOR));
        env.kafkaClient().prepareResponse(prepareFindCoordinatorResponse(Errors.NONE, env.cluster().controller()));
        Map<TopicPartition, OffsetAndMetadata> offsets = new HashMap<>();
        offsets.put(tp1, new OffsetAndMetadata(123L));
        final AlterConsumerGroupOffsetsResult result = env.adminClient().alterConsumerGroupOffsets(GROUP_ID, offsets);
        TestUtils.assertFutureError(result.all(), TimeoutException.class);
    }
}
Also used : HashMap(java.util.HashMap) TopicPartition(org.apache.kafka.common.TopicPartition) OffsetAndMetadata(org.apache.kafka.clients.consumer.OffsetAndMetadata) Cluster(org.apache.kafka.common.Cluster) Time(org.apache.kafka.common.utils.Time) MockTime(org.apache.kafka.common.utils.MockTime) MockTime(org.apache.kafka.common.utils.MockTime) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Aggregations

Time (org.apache.kafka.common.utils.Time)125 MockTime (org.apache.kafka.common.utils.MockTime)107 Test (org.junit.jupiter.api.Test)63 MockClient (org.apache.kafka.clients.MockClient)55 HashMap (java.util.HashMap)53 Cluster (org.apache.kafka.common.Cluster)41 Test (org.junit.Test)40 Node (org.apache.kafka.common.Node)39 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)32 MetadataResponse (org.apache.kafka.common.requests.MetadataResponse)31 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)30 Metadata (org.apache.kafka.clients.Metadata)28 ProducerMetadata (org.apache.kafka.clients.producer.internals.ProducerMetadata)25 TopicPartition (org.apache.kafka.common.TopicPartition)22 PartitionAssignor (org.apache.kafka.clients.consumer.internals.PartitionAssignor)21 LogContext (org.apache.kafka.common.utils.LogContext)17 Map (java.util.Map)14 Properties (java.util.Properties)14 MetricName (org.apache.kafka.common.MetricName)14 ExecutionException (java.util.concurrent.ExecutionException)13