Search in sources :

Example 1 with CreateTopicsResponseData

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

the class RequestContextTest method testEnvelopeResponseSerde.

@Test
public void testEnvelopeResponseSerde() throws Exception {
    CreateTopicsResponseData.CreatableTopicResultCollection collection = new CreateTopicsResponseData.CreatableTopicResultCollection();
    collection.add(new CreateTopicsResponseData.CreatableTopicResult().setTopicConfigErrorCode(Errors.CLUSTER_AUTHORIZATION_FAILED.code()).setNumPartitions(5));
    CreateTopicsResponseData expectedResponse = new CreateTopicsResponseData().setThrottleTimeMs(10).setTopics(collection);
    int correlationId = 15;
    String clientId = "clientId";
    RequestHeader header = new RequestHeader(ApiKeys.CREATE_TOPICS, ApiKeys.CREATE_TOPICS.latestVersion(), clientId, correlationId);
    RequestContext context = new RequestContext(header, "0", InetAddress.getLocalHost(), KafkaPrincipal.ANONYMOUS, new ListenerName("ssl"), SecurityProtocol.SASL_SSL, ClientInformation.EMPTY, true);
    ByteBuffer buffer = context.buildResponseEnvelopePayload(new CreateTopicsResponse(expectedResponse));
    assertEquals(buffer.capacity(), buffer.limit(), "Buffer limit and capacity should be the same");
    CreateTopicsResponse parsedResponse = (CreateTopicsResponse) AbstractResponse.parseResponse(buffer, header);
    assertEquals(expectedResponse, parsedResponse.data());
}
Also used : ListenerName(org.apache.kafka.common.network.ListenerName) CreateTopicsResponseData(org.apache.kafka.common.message.CreateTopicsResponseData) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 2 with CreateTopicsResponseData

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

the class RequestResponseTest method createCreateTopicResponse.

private CreateTopicsResponse createCreateTopicResponse() {
    CreateTopicsResponseData data = new CreateTopicsResponseData();
    data.topics().add(new CreatableTopicResult().setName("t1").setErrorCode(Errors.INVALID_TOPIC_EXCEPTION.code()).setErrorMessage(null));
    data.topics().add(new CreatableTopicResult().setName("t2").setErrorCode(Errors.LEADER_NOT_AVAILABLE.code()).setErrorMessage("Leader with id 5 is not available."));
    data.topics().add(new CreatableTopicResult().setName("t3").setErrorCode(Errors.NONE.code()).setNumPartitions(1).setReplicationFactor((short) 2).setConfigs(singletonList(new CreatableTopicConfigs().setName("min.insync.replicas").setValue("2"))));
    return new CreateTopicsResponse(data);
}
Also used : CreatableTopicResult(org.apache.kafka.common.message.CreateTopicsResponseData.CreatableTopicResult) CreatableTopicConfigs(org.apache.kafka.common.message.CreateTopicsResponseData.CreatableTopicConfigs) CreateTopicsResponseData(org.apache.kafka.common.message.CreateTopicsResponseData)

Example 3 with CreateTopicsResponseData

use of org.apache.kafka.common.message.CreateTopicsResponseData 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 4 with CreateTopicsResponseData

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

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

CreateTopicsResponseData (org.apache.kafka.common.message.CreateTopicsResponseData)17 CreatableTopic (org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopic)13 CreateTopicsRequestData (org.apache.kafka.common.message.CreateTopicsRequestData)12 Test (org.junit.jupiter.api.Test)12 Uuid (org.apache.kafka.common.Uuid)10 CreatableTopicResult (org.apache.kafka.common.message.CreateTopicsResponseData.CreatableTopicResult)9 HashMap (java.util.HashMap)8 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 Map (java.util.Map)5 CreatableTopicCollection (org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopicCollection)5 LocalLogManagerTestEnv (org.apache.kafka.metalog.LocalLogManagerTestEnv)5 List (java.util.List)4 ApiMessageAndVersion (org.apache.kafka.server.common.ApiMessageAndVersion)4 ArrayList (java.util.ArrayList)3 BrokerHeartbeatRequestData (org.apache.kafka.common.message.BrokerHeartbeatRequestData)3 BrokerRegistrationRequestData (org.apache.kafka.common.message.BrokerRegistrationRequestData)3 Listener (org.apache.kafka.common.message.BrokerRegistrationRequestData.Listener)3 ListenerCollection (org.apache.kafka.common.message.BrokerRegistrationRequestData.ListenerCollection)3 CreatableReplicaAssignment (org.apache.kafka.common.message.CreateTopicsRequestData.CreatableReplicaAssignment)3 BrokerEndpoint (org.apache.kafka.common.metadata.RegisterBrokerRecord.BrokerEndpoint)3