use of org.apache.kafka.common.errors.InvalidTopicException in project kafka by apache.
the class PartitionLeaderStrategyTest method testInvalidTopicError.
@Test
public void testInvalidTopicError() {
TopicPartition topicPartition = new TopicPartition("foo", 0);
Throwable exception = assertFatalTopicError(topicPartition, Errors.INVALID_TOPIC_EXCEPTION);
assertTrue(exception instanceof InvalidTopicException);
InvalidTopicException invalidTopicException = (InvalidTopicException) exception;
assertEquals(mkSet("foo"), invalidTopicException.invalidTopics());
}
use of org.apache.kafka.common.errors.InvalidTopicException in project kafka by apache.
the class Fetcher method getTopicMetadata.
/**
* Get metadata for all topics present in Kafka cluster
*
* @param request The MetadataRequest to send
* @param timeout time for which getting topic metadata is attempted
* @return The map of topics with their partition information
*/
public Map<String, List<PartitionInfo>> getTopicMetadata(MetadataRequest.Builder request, long timeout) {
// Save the round trip if no topics are requested.
if (!request.isAllTopics() && request.topics().isEmpty())
return Collections.emptyMap();
long start = time.milliseconds();
long remaining = timeout;
do {
RequestFuture<ClientResponse> future = sendMetadataRequest(request);
client.poll(future, remaining);
if (future.failed() && !future.isRetriable())
throw future.exception();
if (future.succeeded()) {
MetadataResponse response = (MetadataResponse) future.value().responseBody();
Cluster cluster = response.cluster();
Set<String> unauthorizedTopics = cluster.unauthorizedTopics();
if (!unauthorizedTopics.isEmpty())
throw new TopicAuthorizationException(unauthorizedTopics);
boolean shouldRetry = false;
Map<String, Errors> errors = response.errors();
if (!errors.isEmpty()) {
// if there were errors, we need to check whether they were fatal or whether
// we should just retry
log.debug("Topic metadata fetch included errors: {}", errors);
for (Map.Entry<String, Errors> errorEntry : errors.entrySet()) {
String topic = errorEntry.getKey();
Errors error = errorEntry.getValue();
if (error == Errors.INVALID_TOPIC_EXCEPTION)
throw new InvalidTopicException("Topic '" + topic + "' is invalid");
else if (error == Errors.UNKNOWN_TOPIC_OR_PARTITION)
// in the returned map
continue;
else if (error.exception() instanceof RetriableException)
shouldRetry = true;
else
throw new KafkaException("Unexpected error fetching metadata for topic " + topic, error.exception());
}
}
if (!shouldRetry) {
HashMap<String, List<PartitionInfo>> topicsPartitionInfos = new HashMap<>();
for (String topic : cluster.topics()) topicsPartitionInfos.put(topic, cluster.availablePartitionsForTopic(topic));
return topicsPartitionInfos;
}
}
long elapsed = time.milliseconds() - start;
remaining = timeout - elapsed;
if (remaining > 0) {
long backoff = Math.min(remaining, retryBackoffMs);
time.sleep(backoff);
remaining -= backoff;
}
} while (remaining > 0);
throw new TimeoutException("Timeout expired while fetching topic metadata");
}
use of org.apache.kafka.common.errors.InvalidTopicException in project apache-kafka-on-k8s by banzaicloud.
the class KafkaAdminClientTest method testCreatePartitions.
@Test
public void testCreatePartitions() throws Exception {
try (AdminClientUnitTestEnv env = mockClientEnv()) {
env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
env.kafkaClient().prepareMetadataUpdate(env.cluster(), Collections.<String>emptySet());
env.kafkaClient().setNode(env.cluster().controller());
Map<String, ApiError> m = new HashMap<>();
m.put("my_topic", ApiError.NONE);
m.put("other_topic", ApiError.fromThrowable(new InvalidTopicException("some detailed reason")));
// Test a call where one filter has an error.
env.kafkaClient().prepareResponse(new CreatePartitionsResponse(0, m));
Map<String, NewPartitions> counts = new HashMap<>();
counts.put("my_topic", NewPartitions.increaseTo(3));
counts.put("other_topic", NewPartitions.increaseTo(3, asList(asList(2), asList(3))));
CreatePartitionsResult results = env.adminClient().createPartitions(counts);
Map<String, KafkaFuture<Void>> values = results.values();
KafkaFuture<Void> myTopicResult = values.get("my_topic");
myTopicResult.get();
KafkaFuture<Void> otherTopicResult = values.get("other_topic");
try {
otherTopicResult.get();
fail("get() should throw ExecutionException");
} catch (ExecutionException e0) {
assertTrue(e0.getCause() instanceof InvalidTopicException);
InvalidTopicException e = (InvalidTopicException) e0.getCause();
assertEquals("some detailed reason", e.getMessage());
}
}
}
use of org.apache.kafka.common.errors.InvalidTopicException in project apache-kafka-on-k8s by banzaicloud.
the class RequestResponseTest method testSerialization.
@Test
public void testSerialization() throws Exception {
checkRequest(createFindCoordinatorRequest(0));
checkRequest(createFindCoordinatorRequest(1));
checkErrorResponse(createFindCoordinatorRequest(0), new UnknownServerException());
checkErrorResponse(createFindCoordinatorRequest(1), new UnknownServerException());
checkResponse(createFindCoordinatorResponse(), 0);
checkResponse(createFindCoordinatorResponse(), 1);
checkRequest(createControlledShutdownRequest());
checkResponse(createControlledShutdownResponse(), 1);
checkErrorResponse(createControlledShutdownRequest(), new UnknownServerException());
checkErrorResponse(createControlledShutdownRequest(0), new UnknownServerException());
checkRequest(createFetchRequest(4));
checkResponse(createFetchResponse(), 4);
List<TopicPartition> toForgetTopics = new ArrayList<>();
toForgetTopics.add(new TopicPartition("foo", 0));
toForgetTopics.add(new TopicPartition("foo", 2));
toForgetTopics.add(new TopicPartition("bar", 0));
checkRequest(createFetchRequest(7, new FetchMetadata(123, 456), toForgetTopics));
checkResponse(createFetchResponse(123), 7);
checkResponse(createFetchResponse(Errors.FETCH_SESSION_ID_NOT_FOUND, 123), 7);
checkErrorResponse(createFetchRequest(4), new UnknownServerException());
checkRequest(createHeartBeatRequest());
checkErrorResponse(createHeartBeatRequest(), new UnknownServerException());
checkResponse(createHeartBeatResponse(), 0);
checkRequest(createJoinGroupRequest(1));
checkErrorResponse(createJoinGroupRequest(0), new UnknownServerException());
checkErrorResponse(createJoinGroupRequest(1), new UnknownServerException());
checkResponse(createJoinGroupResponse(), 0);
checkRequest(createLeaveGroupRequest());
checkErrorResponse(createLeaveGroupRequest(), new UnknownServerException());
checkResponse(createLeaveGroupResponse(), 0);
checkRequest(createListGroupsRequest());
checkErrorResponse(createListGroupsRequest(), new UnknownServerException());
checkResponse(createListGroupsResponse(), 0);
checkRequest(createDescribeGroupRequest());
checkErrorResponse(createDescribeGroupRequest(), new UnknownServerException());
checkResponse(createDescribeGroupResponse(), 0);
checkRequest(createDeleteGroupsRequest());
checkErrorResponse(createDeleteGroupsRequest(), new UnknownServerException());
checkResponse(createDeleteGroupsResponse(), 0);
checkRequest(createListOffsetRequest(1));
checkErrorResponse(createListOffsetRequest(1), new UnknownServerException());
checkResponse(createListOffsetResponse(1), 1);
checkRequest(createListOffsetRequest(2));
checkErrorResponse(createListOffsetRequest(2), new UnknownServerException());
checkResponse(createListOffsetResponse(2), 2);
checkRequest(MetadataRequest.Builder.allTopics().build((short) 2));
checkRequest(createMetadataRequest(1, singletonList("topic1")));
checkErrorResponse(createMetadataRequest(1, singletonList("topic1")), new UnknownServerException());
checkResponse(createMetadataResponse(), 2);
checkErrorResponse(createMetadataRequest(2, singletonList("topic1")), new UnknownServerException());
checkResponse(createMetadataResponse(), 3);
checkErrorResponse(createMetadataRequest(3, singletonList("topic1")), new UnknownServerException());
checkResponse(createMetadataResponse(), 4);
checkErrorResponse(createMetadataRequest(4, singletonList("topic1")), new UnknownServerException());
checkRequest(createOffsetCommitRequest(2));
checkErrorResponse(createOffsetCommitRequest(2), new UnknownServerException());
checkResponse(createOffsetCommitResponse(), 0);
checkRequest(OffsetFetchRequest.forAllPartitions("group1"));
checkErrorResponse(OffsetFetchRequest.forAllPartitions("group1"), new NotCoordinatorException("Not Coordinator"));
checkRequest(createOffsetFetchRequest(0));
checkRequest(createOffsetFetchRequest(1));
checkRequest(createOffsetFetchRequest(2));
checkRequest(OffsetFetchRequest.forAllPartitions("group1"));
checkErrorResponse(createOffsetFetchRequest(0), new UnknownServerException());
checkErrorResponse(createOffsetFetchRequest(1), new UnknownServerException());
checkErrorResponse(createOffsetFetchRequest(2), new UnknownServerException());
checkResponse(createOffsetFetchResponse(), 0);
checkRequest(createProduceRequest(2));
checkErrorResponse(createProduceRequest(2), new UnknownServerException());
checkRequest(createProduceRequest(3));
checkErrorResponse(createProduceRequest(3), new UnknownServerException());
checkResponse(createProduceResponse(), 2);
checkRequest(createStopReplicaRequest(true));
checkRequest(createStopReplicaRequest(false));
checkErrorResponse(createStopReplicaRequest(true), new UnknownServerException());
checkResponse(createStopReplicaResponse(), 0);
checkRequest(createLeaderAndIsrRequest());
checkErrorResponse(createLeaderAndIsrRequest(), new UnknownServerException());
checkResponse(createLeaderAndIsrResponse(), 0);
checkRequest(createSaslHandshakeRequest());
checkErrorResponse(createSaslHandshakeRequest(), new UnknownServerException());
checkResponse(createSaslHandshakeResponse(), 0);
checkRequest(createApiVersionRequest());
checkErrorResponse(createApiVersionRequest(), new UnknownServerException());
checkResponse(createApiVersionResponse(), 0);
checkRequest(createCreateTopicRequest(0));
checkErrorResponse(createCreateTopicRequest(0), new UnknownServerException());
checkResponse(createCreateTopicResponse(), 0);
checkRequest(createCreateTopicRequest(1));
checkErrorResponse(createCreateTopicRequest(1), new UnknownServerException());
checkResponse(createCreateTopicResponse(), 1);
checkRequest(createDeleteTopicsRequest());
checkErrorResponse(createDeleteTopicsRequest(), new UnknownServerException());
checkResponse(createDeleteTopicsResponse(), 0);
checkRequest(createInitPidRequest());
checkErrorResponse(createInitPidRequest(), new UnknownServerException());
checkResponse(createInitPidResponse(), 0);
checkRequest(createAddPartitionsToTxnRequest());
checkResponse(createAddPartitionsToTxnResponse(), 0);
checkErrorResponse(createAddPartitionsToTxnRequest(), new UnknownServerException());
checkRequest(createAddOffsetsToTxnRequest());
checkResponse(createAddOffsetsToTxnResponse(), 0);
checkErrorResponse(createAddOffsetsToTxnRequest(), new UnknownServerException());
checkRequest(createEndTxnRequest());
checkResponse(createEndTxnResponse(), 0);
checkErrorResponse(createEndTxnRequest(), new UnknownServerException());
checkRequest(createWriteTxnMarkersRequest());
checkResponse(createWriteTxnMarkersResponse(), 0);
checkErrorResponse(createWriteTxnMarkersRequest(), new UnknownServerException());
checkRequest(createTxnOffsetCommitRequest());
checkResponse(createTxnOffsetCommitResponse(), 0);
checkErrorResponse(createTxnOffsetCommitRequest(), new UnknownServerException());
checkOlderFetchVersions();
checkResponse(createMetadataResponse(), 0);
checkResponse(createMetadataResponse(), 1);
checkErrorResponse(createMetadataRequest(1, singletonList("topic1")), new UnknownServerException());
checkRequest(createOffsetCommitRequest(0));
checkErrorResponse(createOffsetCommitRequest(0), new UnknownServerException());
checkRequest(createOffsetCommitRequest(1));
checkErrorResponse(createOffsetCommitRequest(1), new UnknownServerException());
checkRequest(createJoinGroupRequest(0));
checkRequest(createUpdateMetadataRequest(0, null));
checkErrorResponse(createUpdateMetadataRequest(0, null), new UnknownServerException());
checkRequest(createUpdateMetadataRequest(1, null));
checkRequest(createUpdateMetadataRequest(1, "rack1"));
checkErrorResponse(createUpdateMetadataRequest(1, null), new UnknownServerException());
checkRequest(createUpdateMetadataRequest(2, "rack1"));
checkRequest(createUpdateMetadataRequest(2, null));
checkErrorResponse(createUpdateMetadataRequest(2, "rack1"), new UnknownServerException());
checkRequest(createUpdateMetadataRequest(3, "rack1"));
checkRequest(createUpdateMetadataRequest(3, null));
checkErrorResponse(createUpdateMetadataRequest(3, "rack1"), new UnknownServerException());
checkResponse(createUpdateMetadataResponse(), 0);
checkRequest(createListOffsetRequest(0));
checkErrorResponse(createListOffsetRequest(0), new UnknownServerException());
checkResponse(createListOffsetResponse(0), 0);
checkRequest(createLeaderEpochRequest());
checkResponse(createLeaderEpochResponse(), 0);
checkErrorResponse(createLeaderEpochRequest(), new UnknownServerException());
checkRequest(createAddPartitionsToTxnRequest());
checkErrorResponse(createAddPartitionsToTxnRequest(), new UnknownServerException());
checkResponse(createAddPartitionsToTxnResponse(), 0);
checkRequest(createAddOffsetsToTxnRequest());
checkErrorResponse(createAddOffsetsToTxnRequest(), new UnknownServerException());
checkResponse(createAddOffsetsToTxnResponse(), 0);
checkRequest(createEndTxnRequest());
checkErrorResponse(createEndTxnRequest(), new UnknownServerException());
checkResponse(createEndTxnResponse(), 0);
checkRequest(createWriteTxnMarkersRequest());
checkErrorResponse(createWriteTxnMarkersRequest(), new UnknownServerException());
checkResponse(createWriteTxnMarkersResponse(), 0);
checkRequest(createTxnOffsetCommitRequest());
checkErrorResponse(createTxnOffsetCommitRequest(), new UnknownServerException());
checkResponse(createTxnOffsetCommitResponse(), 0);
checkRequest(createListAclsRequest());
checkErrorResponse(createListAclsRequest(), new SecurityDisabledException("Security is not enabled."));
checkResponse(createDescribeAclsResponse(), ApiKeys.DESCRIBE_ACLS.latestVersion());
checkRequest(createCreateAclsRequest());
checkErrorResponse(createCreateAclsRequest(), new SecurityDisabledException("Security is not enabled."));
checkResponse(createCreateAclsResponse(), ApiKeys.CREATE_ACLS.latestVersion());
checkRequest(createDeleteAclsRequest());
checkErrorResponse(createDeleteAclsRequest(), new SecurityDisabledException("Security is not enabled."));
checkResponse(createDeleteAclsResponse(), ApiKeys.DELETE_ACLS.latestVersion());
checkRequest(createAlterConfigsRequest());
checkErrorResponse(createAlterConfigsRequest(), new UnknownServerException());
checkResponse(createAlterConfigsResponse(), 0);
checkRequest(createDescribeConfigsRequest(0));
checkRequest(createDescribeConfigsRequestWithConfigEntries(0));
checkErrorResponse(createDescribeConfigsRequest(0), new UnknownServerException());
checkResponse(createDescribeConfigsResponse(), 0);
checkRequest(createDescribeConfigsRequest(1));
checkRequest(createDescribeConfigsRequestWithConfigEntries(1));
checkErrorResponse(createDescribeConfigsRequest(1), new UnknownServerException());
checkResponse(createDescribeConfigsResponse(), 1);
checkDescribeConfigsResponseVersions();
checkRequest(createCreatePartitionsRequest());
checkRequest(createCreatePartitionsRequestWithAssignments());
checkErrorResponse(createCreatePartitionsRequest(), new InvalidTopicException());
checkResponse(createCreatePartitionsResponse(), 0);
checkRequest(createCreateTokenRequest());
checkErrorResponse(createCreateTokenRequest(), new UnknownServerException());
checkResponse(createCreateTokenResponse(), 0);
checkRequest(createDescribeTokenRequest());
checkErrorResponse(createDescribeTokenRequest(), new UnknownServerException());
checkResponse(createDescribeTokenResponse(), 0);
checkRequest(createExpireTokenRequest());
checkErrorResponse(createExpireTokenRequest(), new UnknownServerException());
checkResponse(createExpireTokenResponse(), 0);
checkRequest(createRenewTokenRequest());
checkErrorResponse(createRenewTokenRequest(), new UnknownServerException());
checkResponse(createRenewTokenResponse(), 0);
}
use of org.apache.kafka.common.errors.InvalidTopicException in project apache-kafka-on-k8s by banzaicloud.
the class Fetcher method getTopicMetadata.
/**
* Get metadata for all topics present in Kafka cluster
*
* @param request The MetadataRequest to send
* @param timeout time for which getting topic metadata is attempted
* @return The map of topics with their partition information
*/
public Map<String, List<PartitionInfo>> getTopicMetadata(MetadataRequest.Builder request, long timeout) {
// Save the round trip if no topics are requested.
if (!request.isAllTopics() && request.topics().isEmpty())
return Collections.emptyMap();
long start = time.milliseconds();
long remaining = timeout;
do {
RequestFuture<ClientResponse> future = sendMetadataRequest(request);
client.poll(future, remaining);
if (future.failed() && !future.isRetriable())
throw future.exception();
if (future.succeeded()) {
MetadataResponse response = (MetadataResponse) future.value().responseBody();
Cluster cluster = response.cluster();
Set<String> unauthorizedTopics = cluster.unauthorizedTopics();
if (!unauthorizedTopics.isEmpty())
throw new TopicAuthorizationException(unauthorizedTopics);
boolean shouldRetry = false;
Map<String, Errors> errors = response.errors();
if (!errors.isEmpty()) {
// if there were errors, we need to check whether they were fatal or whether
// we should just retry
log.debug("Topic metadata fetch included errors: {}", errors);
for (Map.Entry<String, Errors> errorEntry : errors.entrySet()) {
String topic = errorEntry.getKey();
Errors error = errorEntry.getValue();
if (error == Errors.INVALID_TOPIC_EXCEPTION)
throw new InvalidTopicException("Topic '" + topic + "' is invalid");
else if (error == Errors.UNKNOWN_TOPIC_OR_PARTITION)
// in the returned map
continue;
else if (error.exception() instanceof RetriableException)
shouldRetry = true;
else
throw new KafkaException("Unexpected error fetching metadata for topic " + topic, error.exception());
}
}
if (!shouldRetry) {
HashMap<String, List<PartitionInfo>> topicsPartitionInfos = new HashMap<>();
for (String topic : cluster.topics()) topicsPartitionInfos.put(topic, cluster.availablePartitionsForTopic(topic));
return topicsPartitionInfos;
}
}
long elapsed = time.milliseconds() - start;
remaining = timeout - elapsed;
if (remaining > 0) {
long backoff = Math.min(remaining, retryBackoffMs);
time.sleep(backoff);
remaining -= backoff;
}
} while (remaining > 0);
throw new TimeoutException("Timeout expired while fetching topic metadata");
}
Aggregations