Search in sources :

Example 1 with CreateTopicsRequestData

use of org.apache.kafka.common.message.CreateTopicsRequestData in project kafka by apache.

the class RequestResponseTest method testSerializeWithInconsistentHeaderVersion.

@Test
public void testSerializeWithInconsistentHeaderVersion() {
    CreateTopicsRequest createTopicsRequest = new CreateTopicsRequest.Builder(new CreateTopicsRequestData()).build((short) 2);
    RequestHeader requestHeader = new RequestHeader(CREATE_TOPICS, (short) 1, "client", 2);
    assertThrows(IllegalArgumentException.class, () -> createTopicsRequest.serializeWithHeader(requestHeader));
}
Also used : CreateTopicsRequestData(org.apache.kafka.common.message.CreateTopicsRequestData) Builder(org.apache.kafka.common.requests.CreateTopicsRequest.Builder) Test(org.junit.jupiter.api.Test)

Example 2 with CreateTopicsRequestData

use of org.apache.kafka.common.message.CreateTopicsRequestData in project kafka by apache.

the class RequestResponseTest method testSerializeWithHeader.

@Test
public void testSerializeWithHeader() {
    CreatableTopicCollection topicsToCreate = new CreatableTopicCollection(1);
    topicsToCreate.add(new CreatableTopic().setName("topic").setNumPartitions(3).setReplicationFactor((short) 2));
    CreateTopicsRequest createTopicsRequest = new CreateTopicsRequest.Builder(new CreateTopicsRequestData().setTimeoutMs(10).setTopics(topicsToCreate)).build();
    short requestVersion = CREATE_TOPICS.latestVersion();
    RequestHeader requestHeader = new RequestHeader(CREATE_TOPICS, requestVersion, "client", 2);
    ByteBuffer serializedRequest = createTopicsRequest.serializeWithHeader(requestHeader);
    RequestHeader parsedHeader = RequestHeader.parse(serializedRequest);
    assertEquals(requestHeader, parsedHeader);
    RequestAndSize parsedRequest = AbstractRequest.parseRequest(CREATE_TOPICS, requestVersion, serializedRequest);
    assertEquals(createTopicsRequest.data(), parsedRequest.request.data());
}
Also used : CreatableTopicCollection(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopicCollection) CreatableTopic(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopic) CreateTopicsRequestData(org.apache.kafka.common.message.CreateTopicsRequestData) Builder(org.apache.kafka.common.requests.CreateTopicsRequest.Builder) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 3 with CreateTopicsRequestData

use of org.apache.kafka.common.message.CreateTopicsRequestData in project kafka by apache.

the class QuorumControllerTest method testUnregisterBroker.

@Test
public void testUnregisterBroker() throws Throwable {
    try (LocalLogManagerTestEnv logEnv = new LocalLogManagerTestEnv(1, Optional.empty())) {
        try (QuorumControllerTestEnv controlEnv = new QuorumControllerTestEnv(logEnv, b -> b.setConfigDefs(CONFIGS))) {
            ListenerCollection listeners = new ListenerCollection();
            listeners.add(new Listener().setName("PLAINTEXT").setHost("localhost").setPort(9092));
            QuorumController active = controlEnv.activeController();
            CompletableFuture<BrokerRegistrationReply> reply = active.registerBroker(new BrokerRegistrationRequestData().setBrokerId(0).setClusterId(active.clusterId()).setIncarnationId(Uuid.fromString("kxAT73dKQsitIedpiPtwBA")).setListeners(listeners));
            assertEquals(0L, reply.get().epoch());
            CreateTopicsRequestData createTopicsRequestData = new CreateTopicsRequestData().setTopics(new CreatableTopicCollection(Collections.singleton(new CreatableTopic().setName("foo").setNumPartitions(1).setReplicationFactor((short) 1)).iterator()));
            assertEquals(Errors.INVALID_REPLICATION_FACTOR.code(), active.createTopics(createTopicsRequestData).get().topics().find("foo").errorCode());
            assertEquals("Unable to replicate the partition 1 time(s): All brokers " + "are currently fenced.", active.createTopics(createTopicsRequestData).get().topics().find("foo").errorMessage());
            assertEquals(new BrokerHeartbeatReply(true, false, false, false), active.processBrokerHeartbeat(new BrokerHeartbeatRequestData().setWantFence(false).setBrokerEpoch(0L).setBrokerId(0).setCurrentMetadataOffset(100000L)).get());
            assertEquals(Errors.NONE.code(), active.createTopics(createTopicsRequestData).get().topics().find("foo").errorCode());
            CompletableFuture<TopicIdPartition> topicPartitionFuture = active.appendReadEvent("debugGetPartition", () -> {
                Iterator<TopicIdPartition> iterator = active.replicationControl().brokersToIsrs().iterator(0, true);
                assertTrue(iterator.hasNext());
                return iterator.next();
            });
            assertEquals(0, topicPartitionFuture.get().partitionId());
            active.unregisterBroker(0).get();
            topicPartitionFuture = active.appendReadEvent("debugGetPartition", () -> {
                Iterator<TopicIdPartition> iterator = active.replicationControl().brokersToIsrs().partitionsWithNoLeader();
                assertTrue(iterator.hasNext());
                return iterator.next();
            });
            assertEquals(0, topicPartitionFuture.get().partitionId());
        }
    }
}
Also used : BrokerHeartbeatReply(org.apache.kafka.metadata.BrokerHeartbeatReply) ListenerCollection(org.apache.kafka.common.message.BrokerRegistrationRequestData.ListenerCollection) LocalLogManagerTestEnv(org.apache.kafka.metalog.LocalLogManagerTestEnv) Listener(org.apache.kafka.common.message.BrokerRegistrationRequestData.Listener) BrokerRegistrationRequestData(org.apache.kafka.common.message.BrokerRegistrationRequestData) BrokerRegistrationReply(org.apache.kafka.metadata.BrokerRegistrationReply) TopicIdPartition(org.apache.kafka.controller.BrokersToIsrs.TopicIdPartition) CreatableTopicCollection(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopicCollection) BrokerHeartbeatRequestData(org.apache.kafka.common.message.BrokerHeartbeatRequestData) CreatableTopic(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopic) CreateTopicsRequestData(org.apache.kafka.common.message.CreateTopicsRequestData) Iterator(java.util.Iterator) Test(org.junit.jupiter.api.Test)

Example 4 with CreateTopicsRequestData

use of org.apache.kafka.common.message.CreateTopicsRequestData in project kafka by apache.

the class ReplicationControlManagerTest method testCreatePartitions.

@Test
public void testCreatePartitions() throws Exception {
    ReplicationControlTestContext ctx = new ReplicationControlTestContext();
    ReplicationControlManager replicationControl = ctx.replicationControl;
    CreateTopicsRequestData request = new CreateTopicsRequestData();
    request.topics().add(new CreatableTopic().setName("foo").setNumPartitions(3).setReplicationFactor((short) 2));
    request.topics().add(new CreatableTopic().setName("bar").setNumPartitions(4).setReplicationFactor((short) 2));
    request.topics().add(new CreatableTopic().setName("quux").setNumPartitions(2).setReplicationFactor((short) 2));
    request.topics().add(new CreatableTopic().setName("foo2").setNumPartitions(2).setReplicationFactor((short) 2));
    ctx.registerBrokers(0, 1);
    ctx.unfenceBrokers(0, 1);
    ControllerResult<CreateTopicsResponseData> createTopicResult = replicationControl.createTopics(request);
    ctx.replay(createTopicResult.records());
    List<CreatePartitionsTopic> topics = new ArrayList<>();
    topics.add(new CreatePartitionsTopic().setName("foo").setCount(5).setAssignments(null));
    topics.add(new CreatePartitionsTopic().setName("bar").setCount(3).setAssignments(null));
    topics.add(new CreatePartitionsTopic().setName("baz").setCount(3).setAssignments(null));
    topics.add(new CreatePartitionsTopic().setName("quux").setCount(2).setAssignments(null));
    ControllerResult<List<CreatePartitionsTopicResult>> createPartitionsResult = replicationControl.createPartitions(topics);
    assertEquals(asList(new CreatePartitionsTopicResult().setName("foo").setErrorCode(NONE.code()).setErrorMessage(null), new CreatePartitionsTopicResult().setName("bar").setErrorCode(INVALID_PARTITIONS.code()).setErrorMessage("The topic bar currently has 4 partition(s); 3 would not be an increase."), new CreatePartitionsTopicResult().setName("baz").setErrorCode(UNKNOWN_TOPIC_OR_PARTITION.code()).setErrorMessage(null), new CreatePartitionsTopicResult().setName("quux").setErrorCode(INVALID_PARTITIONS.code()).setErrorMessage("Topic already has 2 partition(s).")), createPartitionsResult.response());
    ctx.replay(createPartitionsResult.records());
    List<CreatePartitionsTopic> topics2 = new ArrayList<>();
    topics2.add(new CreatePartitionsTopic().setName("foo").setCount(6).setAssignments(asList(new CreatePartitionsAssignment().setBrokerIds(asList(1, 0)))));
    topics2.add(new CreatePartitionsTopic().setName("bar").setCount(5).setAssignments(asList(new CreatePartitionsAssignment().setBrokerIds(asList(1)))));
    topics2.add(new CreatePartitionsTopic().setName("quux").setCount(4).setAssignments(asList(new CreatePartitionsAssignment().setBrokerIds(asList(1, 0)))));
    topics2.add(new CreatePartitionsTopic().setName("foo2").setCount(3).setAssignments(asList(new CreatePartitionsAssignment().setBrokerIds(asList(2, 0)))));
    ControllerResult<List<CreatePartitionsTopicResult>> createPartitionsResult2 = replicationControl.createPartitions(topics2);
    assertEquals(asList(new CreatePartitionsTopicResult().setName("foo").setErrorCode(NONE.code()).setErrorMessage(null), new CreatePartitionsTopicResult().setName("bar").setErrorCode(INVALID_REPLICA_ASSIGNMENT.code()).setErrorMessage("The manual partition assignment includes a partition " + "with 1 replica(s), but this is not consistent with previous " + "partitions, which have 2 replica(s)."), new CreatePartitionsTopicResult().setName("quux").setErrorCode(INVALID_REPLICA_ASSIGNMENT.code()).setErrorMessage("Attempted to add 2 additional partition(s), but only 1 assignment(s) were specified."), new CreatePartitionsTopicResult().setName("foo2").setErrorCode(INVALID_REPLICA_ASSIGNMENT.code()).setErrorMessage("The manual partition assignment includes broker 2, but " + "no such broker is registered.")), createPartitionsResult2.response());
    ctx.replay(createPartitionsResult2.records());
}
Also used : ArrayList(java.util.ArrayList) CreateTopicsResponseData(org.apache.kafka.common.message.CreateTopicsResponseData) CreatePartitionsTopicResult(org.apache.kafka.common.message.CreatePartitionsResponseData.CreatePartitionsTopicResult) CreatableTopic(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopic) CreateTopicsRequestData(org.apache.kafka.common.message.CreateTopicsRequestData) CreatePartitionsAssignment(org.apache.kafka.common.message.CreatePartitionsRequestData.CreatePartitionsAssignment) Collections.singletonList(java.util.Collections.singletonList) Arrays.asList(java.util.Arrays.asList) ArrayList(java.util.ArrayList) List(java.util.List) CreatePartitionsTopic(org.apache.kafka.common.message.CreatePartitionsRequestData.CreatePartitionsTopic) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 5 with CreateTopicsRequestData

use of org.apache.kafka.common.message.CreateTopicsRequestData in project kafka by apache.

the class ReplicationControlManagerTest method testCreateTopicsWithValidateOnlyFlag.

@Test
public void testCreateTopicsWithValidateOnlyFlag() throws Exception {
    ReplicationControlTestContext ctx = new ReplicationControlTestContext();
    ctx.registerBrokers(0, 1, 2);
    ctx.unfenceBrokers(0, 1, 2);
    CreateTopicsRequestData request = new CreateTopicsRequestData().setValidateOnly(true);
    request.topics().add(new CreatableTopic().setName("foo").setNumPartitions(1).setReplicationFactor((short) 3));
    ControllerResult<CreateTopicsResponseData> result = ctx.replicationControl.createTopics(request);
    assertEquals(0, result.records().size());
    CreatableTopicResult topicResult = result.response().topics().find("foo");
    assertEquals((short) 0, topicResult.errorCode());
}
Also used : CreatableTopic(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopic) CreateTopicsRequestData(org.apache.kafka.common.message.CreateTopicsRequestData) CreatableTopicResult(org.apache.kafka.common.message.CreateTopicsResponseData.CreatableTopicResult) CreateTopicsResponseData(org.apache.kafka.common.message.CreateTopicsResponseData) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Aggregations

CreateTopicsRequestData (org.apache.kafka.common.message.CreateTopicsRequestData)23 CreatableTopic (org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopic)19 Test (org.junit.jupiter.api.Test)19 CreateTopicsResponseData (org.apache.kafka.common.message.CreateTopicsResponseData)13 CreatableTopicCollection (org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopicCollection)12 HashMap (java.util.HashMap)11 Uuid (org.apache.kafka.common.Uuid)10 LocalLogManagerTestEnv (org.apache.kafka.metalog.LocalLogManagerTestEnv)9 List (java.util.List)6 Map (java.util.Map)6 BrokerHeartbeatRequestData (org.apache.kafka.common.message.BrokerHeartbeatRequestData)6 BrokerRegistrationRequestData (org.apache.kafka.common.message.BrokerRegistrationRequestData)6 Listener (org.apache.kafka.common.message.BrokerRegistrationRequestData.Listener)6 ListenerCollection (org.apache.kafka.common.message.BrokerRegistrationRequestData.ListenerCollection)6 BrokerHeartbeatReply (org.apache.kafka.metadata.BrokerHeartbeatReply)6 BrokerRegistrationReply (org.apache.kafka.metadata.BrokerRegistrationReply)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 CreatableReplicaAssignment (org.apache.kafka.common.message.CreateTopicsRequestData.CreatableReplicaAssignment)5 CreatableTopicResult (org.apache.kafka.common.message.CreateTopicsResponseData.CreatableTopicResult)5 Builder (org.apache.kafka.common.requests.CreateTopicsRequest.Builder)5