Search in sources :

Example 6 with MetadataResponse

use of org.apache.kafka.common.requests.MetadataResponse in project kafka by apache.

the class MetadataTest method testNodeIfOffline.

@Test
public void testNodeIfOffline() {
    Map<String, Integer> partitionCounts = new HashMap<>();
    partitionCounts.put("topic-1", 1);
    Node node0 = new Node(0, "localhost", 9092);
    Node node1 = new Node(1, "localhost", 9093);
    MetadataResponse metadataResponse = RequestTestUtils.metadataUpdateWith("dummy", 2, Collections.emptyMap(), partitionCounts, _tp -> 99, (error, partition, leader, leaderEpoch, replicas, isr, offlineReplicas) -> new MetadataResponse.PartitionMetadata(error, partition, Optional.of(node0.id()), leaderEpoch, Collections.singletonList(node0.id()), Collections.emptyList(), Collections.singletonList(node1.id())), ApiKeys.METADATA.latestVersion(), Collections.emptyMap());
    metadata.updateWithCurrentRequestVersion(emptyMetadataResponse(), false, 0L);
    metadata.updateWithCurrentRequestVersion(metadataResponse, false, 10L);
    TopicPartition tp = new TopicPartition("topic-1", 0);
    assertOptional(metadata.fetch().nodeIfOnline(tp, 0), node -> assertEquals(node.id(), 0));
    assertFalse(metadata.fetch().nodeIfOnline(tp, 1).isPresent());
    assertEquals(metadata.fetch().nodeById(0).id(), 0);
    assertEquals(metadata.fetch().nodeById(1).id(), 1);
}
Also used : HashMap(java.util.HashMap) TopicPartition(org.apache.kafka.common.TopicPartition) Node(org.apache.kafka.common.Node) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) Test(org.junit.jupiter.api.Test)

Example 7 with MetadataResponse

use of org.apache.kafka.common.requests.MetadataResponse in project kafka by apache.

the class MetadataTest method testEpochUpdateAfterTopicDeletion.

@Test
public void testEpochUpdateAfterTopicDeletion() {
    TopicPartition tp = new TopicPartition("topic-1", 0);
    MetadataResponse metadataResponse = emptyMetadataResponse();
    metadata.updateWithCurrentRequestVersion(metadataResponse, false, 0L);
    // Start with a Topic topic-1 with a random topic ID
    Map<String, Uuid> topicIds = Collections.singletonMap("topic-1", Uuid.randomUuid());
    metadataResponse = RequestTestUtils.metadataUpdateWithIds("dummy", 1, Collections.emptyMap(), Collections.singletonMap("topic-1", 1), _tp -> 10, topicIds);
    metadata.updateWithCurrentRequestVersion(metadataResponse, false, 1L);
    assertEquals(Optional.of(10), metadata.lastSeenLeaderEpoch(tp));
    // Topic topic-1 is now deleted so Response contains an Error. LeaderEpoch should still maintain Old value
    metadataResponse = RequestTestUtils.metadataUpdateWith("dummy", 1, Collections.singletonMap("topic-1", Errors.UNKNOWN_TOPIC_OR_PARTITION), Collections.emptyMap());
    metadata.updateWithCurrentRequestVersion(metadataResponse, false, 1L);
    assertEquals(Optional.of(10), metadata.lastSeenLeaderEpoch(tp));
    // Create topic-1 again but this time with a different topic ID. LeaderEpoch should be updated to new even if lower.
    Map<String, Uuid> newTopicIds = Collections.singletonMap("topic-1", Uuid.randomUuid());
    metadataResponse = RequestTestUtils.metadataUpdateWithIds("dummy", 1, Collections.emptyMap(), Collections.singletonMap("topic-1", 1), _tp -> 5, newTopicIds);
    metadata.updateWithCurrentRequestVersion(metadataResponse, false, 1L);
    assertEquals(Optional.of(5), metadata.lastSeenLeaderEpoch(tp));
}
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) Uuid(org.apache.kafka.common.Uuid) TopicPartition(org.apache.kafka.common.TopicPartition) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) Test(org.junit.jupiter.api.Test)

Example 8 with MetadataResponse

use of org.apache.kafka.common.requests.MetadataResponse in project kafka by apache.

the class MetadataTest method testRejectOldMetadata.

@Test
public void testRejectOldMetadata() {
    Map<String, Integer> partitionCounts = new HashMap<>();
    partitionCounts.put("topic-1", 1);
    TopicPartition tp = new TopicPartition("topic-1", 0);
    metadata.updateWithCurrentRequestVersion(emptyMetadataResponse(), false, 0L);
    // First epoch seen, accept it
    {
        MetadataResponse metadataResponse = RequestTestUtils.metadataUpdateWith("dummy", 1, Collections.emptyMap(), partitionCounts, _tp -> 100);
        metadata.updateWithCurrentRequestVersion(metadataResponse, false, 10L);
        assertNotNull(metadata.fetch().partition(tp));
        assertTrue(metadata.lastSeenLeaderEpoch(tp).isPresent());
        assertEquals(metadata.lastSeenLeaderEpoch(tp).get().longValue(), 100);
    }
    // Fake an empty ISR, but with an older epoch, should reject it
    {
        MetadataResponse metadataResponse = RequestTestUtils.metadataUpdateWith("dummy", 1, Collections.emptyMap(), partitionCounts, _tp -> 99, (error, partition, leader, leaderEpoch, replicas, isr, offlineReplicas) -> new MetadataResponse.PartitionMetadata(error, partition, leader, leaderEpoch, replicas, Collections.emptyList(), offlineReplicas), ApiKeys.METADATA.latestVersion(), Collections.emptyMap());
        metadata.updateWithCurrentRequestVersion(metadataResponse, false, 20L);
        assertEquals(metadata.fetch().partition(tp).inSyncReplicas().length, 1);
        assertEquals(metadata.lastSeenLeaderEpoch(tp).get().longValue(), 100);
    }
    // Fake an empty ISR, with same epoch, accept it
    {
        MetadataResponse metadataResponse = RequestTestUtils.metadataUpdateWith("dummy", 1, Collections.emptyMap(), partitionCounts, _tp -> 100, (error, partition, leader, leaderEpoch, replicas, isr, offlineReplicas) -> new MetadataResponse.PartitionMetadata(error, partition, leader, leaderEpoch, replicas, Collections.emptyList(), offlineReplicas), ApiKeys.METADATA.latestVersion(), Collections.emptyMap());
        metadata.updateWithCurrentRequestVersion(metadataResponse, false, 20L);
        assertEquals(metadata.fetch().partition(tp).inSyncReplicas().length, 0);
        assertEquals(metadata.lastSeenLeaderEpoch(tp).get().longValue(), 100);
    }
    // Empty metadata response, should not keep old partition but should keep the last-seen epoch
    {
        MetadataResponse metadataResponse = RequestTestUtils.metadataUpdateWith("dummy", 1, Collections.emptyMap(), Collections.emptyMap());
        metadata.updateWithCurrentRequestVersion(metadataResponse, false, 20L);
        assertNull(metadata.fetch().partition(tp));
        assertEquals(metadata.lastSeenLeaderEpoch(tp).get().longValue(), 100);
    }
    // Back in the metadata, with old epoch, should not get added
    {
        MetadataResponse metadataResponse = RequestTestUtils.metadataUpdateWith("dummy", 1, Collections.emptyMap(), partitionCounts, _tp -> 99);
        metadata.updateWithCurrentRequestVersion(metadataResponse, false, 10L);
        assertNull(metadata.fetch().partition(tp));
        assertEquals(metadata.lastSeenLeaderEpoch(tp).get().longValue(), 100);
    }
}
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) HashMap(java.util.HashMap) TopicPartition(org.apache.kafka.common.TopicPartition) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) Test(org.junit.jupiter.api.Test)

Example 9 with MetadataResponse

use of org.apache.kafka.common.requests.MetadataResponse in project kafka by apache.

the class MetadataTest method testMetadataTopicErrors.

@Test
public void testMetadataTopicErrors() {
    Time time = new MockTime();
    Map<String, Errors> topicErrors = new HashMap<>(3);
    topicErrors.put("invalidTopic", Errors.INVALID_TOPIC_EXCEPTION);
    topicErrors.put("sensitiveTopic1", Errors.TOPIC_AUTHORIZATION_FAILED);
    topicErrors.put("sensitiveTopic2", Errors.TOPIC_AUTHORIZATION_FAILED);
    MetadataResponse metadataResponse = RequestTestUtils.metadataUpdateWith("clusterId", 1, topicErrors, Collections.emptyMap());
    metadata.updateWithCurrentRequestVersion(metadataResponse, false, time.milliseconds());
    TopicAuthorizationException e1 = assertThrows(TopicAuthorizationException.class, () -> metadata.maybeThrowExceptionForTopic("sensitiveTopic1"));
    assertEquals(Collections.singleton("sensitiveTopic1"), e1.unauthorizedTopics());
    // We clear the exception once it has been raised to the user
    metadata.maybeThrowAnyException();
    metadata.updateWithCurrentRequestVersion(metadataResponse, false, time.milliseconds());
    TopicAuthorizationException e2 = assertThrows(TopicAuthorizationException.class, () -> metadata.maybeThrowExceptionForTopic("sensitiveTopic2"));
    assertEquals(Collections.singleton("sensitiveTopic2"), e2.unauthorizedTopics());
    metadata.maybeThrowAnyException();
    metadata.updateWithCurrentRequestVersion(metadataResponse, false, time.milliseconds());
    InvalidTopicException e3 = assertThrows(InvalidTopicException.class, () -> metadata.maybeThrowExceptionForTopic("invalidTopic"));
    assertEquals(Collections.singleton("invalidTopic"), e3.invalidTopics());
    metadata.maybeThrowAnyException();
    // Other topics should not throw exception, but they should clear existing exception
    metadata.updateWithCurrentRequestVersion(metadataResponse, false, time.milliseconds());
    metadata.maybeThrowExceptionForTopic("anotherTopic");
    metadata.maybeThrowAnyException();
}
Also used : Errors(org.apache.kafka.common.protocol.Errors) HashMap(java.util.HashMap) 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) TopicAuthorizationException(org.apache.kafka.common.errors.TopicAuthorizationException) Test(org.junit.jupiter.api.Test)

Example 10 with MetadataResponse

use of org.apache.kafka.common.requests.MetadataResponse in project kafka by apache.

the class MetadataTest method testOutOfBandEpochUpdate.

@Test
public void testOutOfBandEpochUpdate() {
    Map<String, Integer> partitionCounts = new HashMap<>();
    partitionCounts.put("topic-1", 5);
    TopicPartition tp = new TopicPartition("topic-1", 0);
    metadata.updateWithCurrentRequestVersion(emptyMetadataResponse(), false, 0L);
    assertFalse(metadata.updateLastSeenEpochIfNewer(tp, 99));
    // Update epoch to 100
    MetadataResponse metadataResponse = RequestTestUtils.metadataUpdateWith("dummy", 1, Collections.emptyMap(), partitionCounts, _tp -> 100);
    metadata.updateWithCurrentRequestVersion(metadataResponse, false, 10L);
    assertNotNull(metadata.fetch().partition(tp));
    assertTrue(metadata.lastSeenLeaderEpoch(tp).isPresent());
    assertEquals(metadata.lastSeenLeaderEpoch(tp).get().longValue(), 100);
    // Simulate a leader epoch from another response, like a fetch response or list offsets
    assertTrue(metadata.updateLastSeenEpochIfNewer(tp, 101));
    // Cache of partition stays, but current partition info is not available since it's stale
    assertNotNull(metadata.fetch().partition(tp));
    assertEquals(Objects.requireNonNull(metadata.fetch().partitionCountForTopic("topic-1")).longValue(), 5);
    assertFalse(metadata.partitionMetadataIfCurrent(tp).isPresent());
    assertEquals(metadata.lastSeenLeaderEpoch(tp).get().longValue(), 101);
    // Metadata with older epoch is rejected, metadata state is unchanged
    metadata.updateWithCurrentRequestVersion(metadataResponse, false, 20L);
    assertNotNull(metadata.fetch().partition(tp));
    assertEquals(Objects.requireNonNull(metadata.fetch().partitionCountForTopic("topic-1")).longValue(), 5);
    assertFalse(metadata.partitionMetadataIfCurrent(tp).isPresent());
    assertEquals(metadata.lastSeenLeaderEpoch(tp).get().longValue(), 101);
    // Metadata with equal or newer epoch is accepted
    metadataResponse = RequestTestUtils.metadataUpdateWith("dummy", 1, Collections.emptyMap(), partitionCounts, _tp -> 101);
    metadata.updateWithCurrentRequestVersion(metadataResponse, false, 30L);
    assertNotNull(metadata.fetch().partition(tp));
    assertEquals(Objects.requireNonNull(metadata.fetch().partitionCountForTopic("topic-1")).longValue(), 5);
    assertTrue(metadata.partitionMetadataIfCurrent(tp).isPresent());
    assertEquals(metadata.lastSeenLeaderEpoch(tp).get().longValue(), 101);
}
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) HashMap(java.util.HashMap) TopicPartition(org.apache.kafka.common.TopicPartition) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) Test(org.junit.jupiter.api.Test)

Aggregations

MetadataResponse (org.apache.kafka.common.requests.MetadataResponse)107 Test (org.junit.jupiter.api.Test)71 HashMap (java.util.HashMap)68 TopicPartition (org.apache.kafka.common.TopicPartition)43 MockTime (org.apache.kafka.common.utils.MockTime)38 Time (org.apache.kafka.common.utils.Time)37 Node (org.apache.kafka.common.Node)33 ArrayList (java.util.ArrayList)30 MockClient (org.apache.kafka.clients.MockClient)29 Cluster (org.apache.kafka.common.Cluster)29 MetadataRequest (org.apache.kafka.common.requests.MetadataRequest)28 Errors (org.apache.kafka.common.protocol.Errors)27 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)27 Map (java.util.Map)26 ProducerMetadata (org.apache.kafka.clients.producer.internals.ProducerMetadata)26 InvalidTopicException (org.apache.kafka.common.errors.InvalidTopicException)26 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)25 List (java.util.List)24 LogContext (org.apache.kafka.common.utils.LogContext)22 HashSet (java.util.HashSet)21