use of org.apache.kafka.common.errors.UnknownServerException in project cdap by caskdata.
the class KafkaUtil method getOffsetByTimestamp.
/**
* Fetch the starting offset of the last segment whose latest message is published before the given timestamp.
* The timestamp can also be special value {@link OffsetRequest$#EarliestTime()}
* or {@link OffsetRequest$#LatestTime()}.
*
* @param consumer the consumer to send request to and receive response from
* @param topic the topic for fetching the offset from
* @param partition the partition for fetching the offset from
* @param timestamp the timestamp to use for fetching last offset before it
* @return the latest offset
*
* @throws NotLeaderForPartitionException if the broker that the consumer is talking to is not the leader
* for the given topic and partition.
* @throws UnknownTopicOrPartitionException if the topic or partition is not known by the Kafka server
* @throws UnknownServerException if the Kafka server responded with error.
*/
public static long getOffsetByTimestamp(SimpleConsumer consumer, String topic, int partition, long timestamp) throws KafkaException {
// Fire offset request
OffsetRequest request = new OffsetRequest(ImmutableMap.of(new TopicAndPartition(topic, partition), new PartitionOffsetRequestInfo(timestamp, 1)), kafka.api.OffsetRequest.CurrentVersion(), consumer.clientId());
OffsetResponse response = consumer.getOffsetsBefore(request);
if (response.hasError()) {
throw Errors.forCode(response.errorCode(topic, partition)).exception();
}
// Retrieve offsets from response
long[] offsets = response.offsets(topic, partition);
if (offsets.length == 0) {
if (timestamp != kafka.api.OffsetRequest.EarliestTime()) {
// Hence, use the earliest time to find out the offset
return getOffsetByTimestamp(consumer, topic, partition, kafka.api.OffsetRequest.EarliestTime());
}
// This shouldn't happen. The find earliest offset response should return at least one offset.
throw new UnknownServerException("Empty offsets received from offsets request on " + topic + ":" + partition + " from broker " + consumer.host() + ":" + consumer.port());
}
LOG.debug("Offset {} fetched for {}:{} with timestamp {}.", offsets[0], topic, partition, timestamp);
return offsets[0];
}
use of org.apache.kafka.common.errors.UnknownServerException 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.UnknownServerException in project kafka by apache.
the class ApiErrorTest method parameters.
private static Collection<Arguments> parameters() {
List<Arguments> arguments = new ArrayList<>();
arguments.add(Arguments.of(new UnknownServerException("Don't leak sensitive information "), Errors.UNKNOWN_SERVER_ERROR, null));
arguments.add(Arguments.of(new NotEnoughReplicasException(), Errors.NOT_ENOUGH_REPLICAS, null));
// avoid populating the error message if it's a generic one
arguments.add(Arguments.of(new UnknownTopicOrPartitionException(Errors.UNKNOWN_TOPIC_OR_PARTITION.message()), Errors.UNKNOWN_TOPIC_OR_PARTITION, null));
String notCoordinatorErrorMsg = "Not coordinator";
arguments.add(Arguments.of(new NotCoordinatorException(notCoordinatorErrorMsg), Errors.NOT_COORDINATOR, notCoordinatorErrorMsg));
String notControllerErrorMsg = "Not controller";
// test the NotControllerException is wrapped in the CompletionException, should return correct error
arguments.add(Arguments.of(new CompletionException(new NotControllerException(notControllerErrorMsg)), Errors.NOT_CONTROLLER, notControllerErrorMsg));
String requestTimeoutErrorMsg = "request time out";
// test the TimeoutException is wrapped in the ExecutionException, should return correct error
arguments.add(Arguments.of(new ExecutionException(new TimeoutException(requestTimeoutErrorMsg)), Errors.REQUEST_TIMED_OUT, requestTimeoutErrorMsg));
// test the exception not in the Errors list, should return UNKNOWN_SERVER_ERROR
arguments.add(Arguments.of(new IOException(), Errors.UNKNOWN_SERVER_ERROR, null));
return arguments;
}
use of org.apache.kafka.common.errors.UnknownServerException in project kafka by apache.
the class KafkaStatusBackingStoreFormatTest method putTopicStateNonRetriableFailure.
@Test
public void putTopicStateNonRetriableFailure() {
TopicStatus topicStatus = new TopicStatus(FOO_TOPIC, new ConnectorTaskId(FOO_CONNECTOR, 0), time.milliseconds());
String key = TOPIC_STATUS_PREFIX + FOO_TOPIC + TOPIC_STATUS_SEPARATOR + FOO_CONNECTOR;
ArgumentCaptor<byte[]> valueCaptor = ArgumentCaptor.forClass(byte[].class);
doAnswer(invocation -> {
((Callback) invocation.getArgument(2)).onCompletion(null, new UnknownServerException());
return null;
}).when(kafkaBasedLog).send(eq(key), valueCaptor.capture(), any(Callback.class));
// the error is logged and ignored
store.put(topicStatus);
// check capture state
assertEquals(topicStatus, store.parseTopicStatus(valueCaptor.getValue()));
// state is not visible until read back from the log
assertNull(store.getTopic(FOO_CONNECTOR, FOO_TOPIC));
}
use of org.apache.kafka.common.errors.UnknownServerException in project kafka by apache.
the class AllBrokersStrategyIntegrationTest method testFatalLookupError.
@Test
public void testFatalLookupError() {
AllBrokersStrategy.AllBrokersFuture<Integer> result = new AllBrokersStrategy.AllBrokersFuture<>();
AdminApiDriver<AllBrokersStrategy.BrokerKey, Integer> driver = buildDriver(result);
List<AdminApiDriver.RequestSpec<AllBrokersStrategy.BrokerKey>> requestSpecs = driver.poll();
assertEquals(1, requestSpecs.size());
AdminApiDriver.RequestSpec<AllBrokersStrategy.BrokerKey> spec = requestSpecs.get(0);
assertEquals(AllBrokersStrategy.LOOKUP_KEYS, spec.keys);
driver.onFailure(time.milliseconds(), spec, new UnknownServerException());
assertTrue(result.all().isDone());
TestUtils.assertFutureThrows(result.all(), UnknownServerException.class);
assertEquals(Collections.emptyList(), driver.poll());
}
Aggregations