Search in sources :

Example 16 with UnknownServerException

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];
}
Also used : OffsetResponse(kafka.javaapi.OffsetResponse) PartitionOffsetRequestInfo(kafka.api.PartitionOffsetRequestInfo) TopicAndPartition(kafka.common.TopicAndPartition) UnknownServerException(org.apache.kafka.common.errors.UnknownServerException) OffsetRequest(kafka.javaapi.OffsetRequest)

Example 17 with UnknownServerException

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);
}
Also used : NotCoordinatorException(org.apache.kafka.common.errors.NotCoordinatorException) TopicPartition(org.apache.kafka.common.TopicPartition) ArrayList(java.util.ArrayList) InvalidTopicException(org.apache.kafka.common.errors.InvalidTopicException) UnknownServerException(org.apache.kafka.common.errors.UnknownServerException) SecurityDisabledException(org.apache.kafka.common.errors.SecurityDisabledException) Test(org.junit.Test)

Example 18 with UnknownServerException

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;
}
Also used : NotCoordinatorException(org.apache.kafka.common.errors.NotCoordinatorException) UnknownTopicOrPartitionException(org.apache.kafka.common.errors.UnknownTopicOrPartitionException) Arguments(org.junit.jupiter.params.provider.Arguments) ArrayList(java.util.ArrayList) IOException(java.io.IOException) UnknownServerException(org.apache.kafka.common.errors.UnknownServerException) CompletionException(java.util.concurrent.CompletionException) NotEnoughReplicasException(org.apache.kafka.common.errors.NotEnoughReplicasException) ExecutionException(java.util.concurrent.ExecutionException) NotControllerException(org.apache.kafka.common.errors.NotControllerException) TimeoutException(org.apache.kafka.common.errors.TimeoutException)

Example 19 with UnknownServerException

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));
}
Also used : Callback(org.apache.kafka.clients.producer.Callback) ConnectorTaskId(org.apache.kafka.connect.util.ConnectorTaskId) TopicStatus(org.apache.kafka.connect.runtime.TopicStatus) UnknownServerException(org.apache.kafka.common.errors.UnknownServerException) Test(org.junit.Test)

Example 20 with UnknownServerException

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());
}
Also used : UnknownServerException(org.apache.kafka.common.errors.UnknownServerException) Test(org.junit.jupiter.api.Test)

Aggregations

UnknownServerException (org.apache.kafka.common.errors.UnknownServerException)28 ArrayList (java.util.ArrayList)12 HashMap (java.util.HashMap)7 List (java.util.List)7 KafkaFutureImpl (org.apache.kafka.common.internals.KafkaFutureImpl)7 Test (org.junit.jupiter.api.Test)7 LinkedList (java.util.LinkedList)6 TopicPartition (org.apache.kafka.common.TopicPartition)6 ChannelBuilder (org.apache.kafka.common.network.ChannelBuilder)5 AbstractResponse (org.apache.kafka.common.requests.AbstractResponse)5 Iterator (java.util.Iterator)4 KafkaFuture (org.apache.kafka.common.KafkaFuture)4 AclBinding (org.apache.kafka.common.acl.AclBinding)4 ApiException (org.apache.kafka.common.errors.ApiException)4 Test (org.junit.Test)4 Map (java.util.Map)3 Callback (org.apache.kafka.clients.producer.Callback)3 ApiMessageAndVersion (org.apache.kafka.server.common.ApiMessageAndVersion)3 CompletableFuture (java.util.concurrent.CompletableFuture)2 ExecutionException (java.util.concurrent.ExecutionException)2