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());
}
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);
}
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());
}
}
}
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());
}
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);
}
Aggregations