Search in sources :

Example 1 with CreatableTopic

use of org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopic 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 2 with CreatableTopic

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

the class CreateTopicsRequest method getErrorResponse.

@Override
public AbstractResponse getErrorResponse(int throttleTimeMs, Throwable e) {
    CreateTopicsResponseData response = new CreateTopicsResponseData();
    if (version() >= 2) {
        response.setThrottleTimeMs(throttleTimeMs);
    }
    ApiError apiError = ApiError.fromThrowable(e);
    for (CreatableTopic topic : data.topics()) {
        response.topics().add(new CreatableTopicResult().setName(topic.name()).setErrorCode(apiError.error().code()).setErrorMessage(apiError.message()));
    }
    return new CreateTopicsResponse(response);
}
Also used : CreatableTopic(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopic) CreatableTopicResult(org.apache.kafka.common.message.CreateTopicsResponseData.CreatableTopicResult) CreateTopicsResponseData(org.apache.kafka.common.message.CreateTopicsResponseData)

Example 3 with CreatableTopic

use of org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopic 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 CreatableTopic

use of org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopic 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 CreatableTopic

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

the class ReplicationControlManagerTest method testValidateNewTopicNames.

@Test
public void testValidateNewTopicNames() {
    Map<String, ApiError> topicErrors = new HashMap<>();
    CreatableTopicCollection topics = new CreatableTopicCollection();
    topics.add(new CreatableTopic().setName(""));
    topics.add(new CreatableTopic().setName("woo"));
    topics.add(new CreatableTopic().setName("."));
    ReplicationControlManager.validateNewTopicNames(topicErrors, topics);
    Map<String, ApiError> expectedTopicErrors = new HashMap<>();
    expectedTopicErrors.put("", new ApiError(INVALID_TOPIC_EXCEPTION, "Topic name is illegal, it can't be empty"));
    expectedTopicErrors.put(".", new ApiError(INVALID_TOPIC_EXCEPTION, "Topic name cannot be \".\" or \"..\""));
    assertEquals(expectedTopicErrors, topicErrors);
}
Also used : CreatableTopicCollection(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopicCollection) HashMap(java.util.HashMap) CreatableTopic(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopic) ApiError(org.apache.kafka.common.requests.ApiError) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Aggregations

CreatableTopic (org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopic)22 CreateTopicsRequestData (org.apache.kafka.common.message.CreateTopicsRequestData)19 Test (org.junit.jupiter.api.Test)17 CreateTopicsResponseData (org.apache.kafka.common.message.CreateTopicsResponseData)14 CreatableTopicCollection (org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopicCollection)12 HashMap (java.util.HashMap)10 Uuid (org.apache.kafka.common.Uuid)10 LocalLogManagerTestEnv (org.apache.kafka.metalog.LocalLogManagerTestEnv)8 CreatableTopicResult (org.apache.kafka.common.message.CreateTopicsResponseData.CreatableTopicResult)7 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)7 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 Map (java.util.Map)5 CreatableReplicaAssignment (org.apache.kafka.common.message.CreateTopicsRequestData.CreatableReplicaAssignment)5 List (java.util.List)4 CreatableReplicaAssignmentCollection (org.apache.kafka.common.message.CreateTopicsRequestData.CreatableReplicaAssignmentCollection)4