Search in sources :

Example 1 with DisconnectException

use of org.apache.kafka.common.errors.DisconnectException in project ksql by confluentinc.

the class KafkaTopicClientImplTest method shouldHandleRetriableGetTopicConfigError.

@Test
public void shouldHandleRetriableGetTopicConfigError() {
    expect(adminClient.describeConfigs(anyObject())).andReturn(topicConfigResponse(new DisconnectException())).andReturn(topicConfigResponse("fred", overriddenConfigEntry(TopicConfig.RETENTION_MS_CONFIG, "12345"), defaultConfigEntry(TopicConfig.COMPRESSION_TYPE_CONFIG, "producer")));
    replay(adminClient);
    KafkaTopicClient kafkaTopicClient = new KafkaTopicClientImpl(adminClient);
    final Map<String, String> config = kafkaTopicClient.getTopicConfig("fred");
    assertThat(config.get(TopicConfig.RETENTION_MS_CONFIG), is("12345"));
    assertThat(config.get(TopicConfig.COMPRESSION_TYPE_CONFIG), is("producer"));
}
Also used : DisconnectException(org.apache.kafka.common.errors.DisconnectException) Test(org.junit.Test)

Example 2 with DisconnectException

use of org.apache.kafka.common.errors.DisconnectException in project apache-kafka-on-k8s by banzaicloud.

the class ConsumerCoordinatorTest method testManyInFlightAsyncCommitsWithCoordinatorDisconnect.

@Test
public void testManyInFlightAsyncCommitsWithCoordinatorDisconnect() throws Exception {
    client.prepareResponse(groupCoordinatorResponse(node, Errors.NONE));
    coordinator.ensureCoordinatorReady();
    int numRequests = 1000;
    TopicPartition tp = new TopicPartition("foo", 0);
    final AtomicInteger responses = new AtomicInteger(0);
    for (int i = 0; i < numRequests; i++) {
        Map<TopicPartition, OffsetAndMetadata> offsets = singletonMap(tp, new OffsetAndMetadata(i));
        coordinator.commitOffsetsAsync(offsets, new OffsetCommitCallback() {

            @Override
            public void onComplete(Map<TopicPartition, OffsetAndMetadata> offsets, Exception exception) {
                responses.incrementAndGet();
                Throwable cause = exception.getCause();
                assertTrue("Unexpected exception cause type: " + (cause == null ? null : cause.getClass()), cause instanceof DisconnectException);
            }
        });
    }
    coordinator.markCoordinatorUnknown();
    consumerClient.pollNoWakeup();
    coordinator.invokeCompletedOffsetCommitCallbacks();
    assertEquals(numRequests, responses.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TopicPartition(org.apache.kafka.common.TopicPartition) OffsetAndMetadata(org.apache.kafka.clients.consumer.OffsetAndMetadata) OffsetCommitCallback(org.apache.kafka.clients.consumer.OffsetCommitCallback) GroupAuthorizationException(org.apache.kafka.common.errors.GroupAuthorizationException) KafkaException(org.apache.kafka.common.KafkaException) TimeoutException(java.util.concurrent.TimeoutException) WakeupException(org.apache.kafka.common.errors.WakeupException) AuthenticationException(org.apache.kafka.common.errors.AuthenticationException) RetriableCommitFailedException(org.apache.kafka.clients.consumer.RetriableCommitFailedException) DisconnectException(org.apache.kafka.common.errors.DisconnectException) CommitFailedException(org.apache.kafka.clients.consumer.CommitFailedException) ApiException(org.apache.kafka.common.errors.ApiException) DisconnectException(org.apache.kafka.common.errors.DisconnectException) Test(org.junit.Test)

Example 3 with DisconnectException

use of org.apache.kafka.common.errors.DisconnectException in project apache-kafka-on-k8s by banzaicloud.

the class ConsumerCoordinatorTest method testCoordinatorUnknownInUnsentCallbacksAfterCoordinatorDead.

@Test
public void testCoordinatorUnknownInUnsentCallbacksAfterCoordinatorDead() throws Exception {
    // When the coordinator is marked dead, all unsent or in-flight requests are cancelled
    // with a disconnect error. This test case ensures that the corresponding callbacks see
    // the coordinator as unknown which prevents additional retries to the same coordinator.
    client.prepareResponse(groupCoordinatorResponse(node, Errors.NONE));
    coordinator.ensureCoordinatorReady();
    final AtomicBoolean asyncCallbackInvoked = new AtomicBoolean(false);
    Map<TopicPartition, OffsetCommitRequest.PartitionData> offsets = singletonMap(new TopicPartition("foo", 0), new OffsetCommitRequest.PartitionData(13L, ""));
    consumerClient.send(coordinator.checkAndGetCoordinator(), new OffsetCommitRequest.Builder(groupId, offsets)).compose(new RequestFutureAdapter<ClientResponse, Object>() {

        @Override
        public void onSuccess(ClientResponse value, RequestFuture<Object> future) {
        }

        @Override
        public void onFailure(RuntimeException e, RequestFuture<Object> future) {
            assertTrue("Unexpected exception type: " + e.getClass(), e instanceof DisconnectException);
            assertTrue(coordinator.coordinatorUnknown());
            asyncCallbackInvoked.set(true);
        }
    });
    coordinator.markCoordinatorUnknown();
    consumerClient.pollNoWakeup();
    assertTrue(asyncCallbackInvoked.get());
}
Also used : ClientResponse(org.apache.kafka.clients.ClientResponse) DisconnectException(org.apache.kafka.common.errors.DisconnectException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) OffsetCommitRequest(org.apache.kafka.common.requests.OffsetCommitRequest) TopicPartition(org.apache.kafka.common.TopicPartition) Test(org.junit.Test)

Example 4 with DisconnectException

use of org.apache.kafka.common.errors.DisconnectException in project kafka by apache.

the class AdminApiDriverTest method testCoalescedStaticAndDynamicFulfillment.

@Test
public void testCoalescedStaticAndDynamicFulfillment() {
    Map<String, String> dynamicMapping = map("foo", "c1");
    Map<String, Integer> staticMapping = map("bar", 1);
    TestContext ctx = new TestContext(staticMapping, dynamicMapping);
    // Initially we expect a lookup for the dynamic key and a
    // fulfillment request for the static key
    LookupResult<String> lookupResult = mapped("foo", 1);
    ctx.lookupStrategy().expectLookup(mkSet("foo"), lookupResult);
    ctx.handler.expectRequest(mkSet("bar"), completed("bar", 10L));
    List<RequestSpec<String>> requestSpecs = ctx.driver.poll();
    assertEquals(2, requestSpecs.size());
    RequestSpec<String> lookupSpec = requestSpecs.get(0);
    assertEquals(mkSet("foo"), lookupSpec.keys);
    ctx.assertLookupResponse(lookupSpec, lookupResult);
    // Receive a disconnect from the fulfillment request so that
    // we have an opportunity to coalesce the keys.
    RequestSpec<String> fulfillmentSpec = requestSpecs.get(1);
    assertEquals(mkSet("bar"), fulfillmentSpec.keys);
    ctx.driver.onFailure(ctx.time.milliseconds(), fulfillmentSpec, new DisconnectException());
    // Now we should get two fulfillment requests. One of them will
    // the coalesced dynamic and static keys for broker 1. The other
    // should contain the single dynamic key for broker 0.
    ctx.handler.reset();
    ctx.handler.expectRequest(mkSet("foo", "bar"), completed("foo", 15L, "bar", 30L));
    List<RequestSpec<String>> coalescedSpecs = ctx.driver.poll();
    assertEquals(1, coalescedSpecs.size());
    RequestSpec<String> coalescedSpec = coalescedSpecs.get(0);
    assertEquals(mkSet("foo", "bar"), coalescedSpec.keys);
    // Disconnect in order to ensure that only the dynamic key is unmapped.
    // Then complete the remaining requests.
    ctx.driver.onFailure(ctx.time.milliseconds(), coalescedSpec, new DisconnectException());
    Map<Set<String>, LookupResult<String>> fooLookupRetry = map(mkSet("foo"), mapped("foo", 3));
    Map<Set<String>, ApiResult<String, Long>> barFulfillmentRetry = map(mkSet("bar"), completed("bar", 30L));
    ctx.poll(fooLookupRetry, barFulfillmentRetry);
    Map<Set<String>, ApiResult<String, Long>> fooFulfillmentRetry = map(mkSet("foo"), completed("foo", 15L));
    ctx.poll(emptyMap(), fooFulfillmentRetry);
    ctx.poll(emptyMap(), emptyMap());
}
Also used : HashSet(java.util.HashSet) Utils.mkSet(org.apache.kafka.common.utils.Utils.mkSet) Set(java.util.Set) DisconnectException(org.apache.kafka.common.errors.DisconnectException) ApiResult(org.apache.kafka.clients.admin.internals.AdminApiHandler.ApiResult) LookupResult(org.apache.kafka.clients.admin.internals.AdminApiLookupStrategy.LookupResult) RequestSpec(org.apache.kafka.clients.admin.internals.AdminApiDriver.RequestSpec) Test(org.junit.jupiter.api.Test)

Example 5 with DisconnectException

use of org.apache.kafka.common.errors.DisconnectException in project kafka by apache.

the class AllBrokersStrategyIntegrationTest method testRetryFulfillmentAfterDisconnect.

@Test
public void testRetryFulfillmentAfterDisconnect() throws Exception {
    AllBrokersStrategy.AllBrokersFuture<Integer> result = new AllBrokersStrategy.AllBrokersFuture<>();
    AdminApiDriver<AllBrokersStrategy.BrokerKey, Integer> driver = buildDriver(result);
    List<AdminApiDriver.RequestSpec<AllBrokersStrategy.BrokerKey>> lookupSpecs = driver.poll();
    assertEquals(1, lookupSpecs.size());
    AdminApiDriver.RequestSpec<AllBrokersStrategy.BrokerKey> lookupSpec = lookupSpecs.get(0);
    int brokerId = 1;
    driver.onResponse(time.milliseconds(), lookupSpec, responseWithBrokers(Collections.singleton(brokerId)), Node.noNode());
    assertTrue(result.all().isDone());
    Map<Integer, KafkaFutureImpl<Integer>> brokerFutures = result.all().get();
    KafkaFutureImpl<Integer> future = brokerFutures.get(brokerId);
    assertFalse(future.isDone());
    List<AdminApiDriver.RequestSpec<AllBrokersStrategy.BrokerKey>> requestSpecs = driver.poll();
    assertEquals(1, requestSpecs.size());
    AdminApiDriver.RequestSpec<AllBrokersStrategy.BrokerKey> requestSpec = requestSpecs.get(0);
    driver.onFailure(time.milliseconds(), requestSpec, new DisconnectException());
    assertFalse(future.isDone());
    List<AdminApiDriver.RequestSpec<AllBrokersStrategy.BrokerKey>> retrySpecs = driver.poll();
    assertEquals(1, retrySpecs.size());
    AdminApiDriver.RequestSpec<AllBrokersStrategy.BrokerKey> retrySpec = retrySpecs.get(0);
    assertEquals(time.milliseconds() + RETRY_BACKOFF_MS, retrySpec.nextAllowedTryMs);
    assertEquals(OptionalInt.of(brokerId), retrySpec.scope.destinationBrokerId());
    driver.onResponse(time.milliseconds(), retrySpec, null, new Node(brokerId, "host", 1234));
    assertTrue(future.isDone());
    assertEquals(brokerId, future.get());
    assertEquals(Collections.emptyList(), driver.poll());
}
Also used : Node(org.apache.kafka.common.Node) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) DisconnectException(org.apache.kafka.common.errors.DisconnectException) Test(org.junit.jupiter.api.Test)

Aggregations

DisconnectException (org.apache.kafka.common.errors.DisconnectException)10 Test (org.junit.jupiter.api.Test)6 Test (org.junit.Test)4 TopicPartition (org.apache.kafka.common.TopicPartition)3 HashSet (java.util.HashSet)2 Set (java.util.Set)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ClientResponse (org.apache.kafka.clients.ClientResponse)2 RequestSpec (org.apache.kafka.clients.admin.internals.AdminApiDriver.RequestSpec)2 LookupResult (org.apache.kafka.clients.admin.internals.AdminApiLookupStrategy.LookupResult)2 OffsetAndMetadata (org.apache.kafka.clients.consumer.OffsetAndMetadata)2 Utils.mkSet (org.apache.kafka.common.utils.Utils.mkSet)2 TimeoutException (java.util.concurrent.TimeoutException)1 ApiResult (org.apache.kafka.clients.admin.internals.AdminApiHandler.ApiResult)1 CommitFailedException (org.apache.kafka.clients.consumer.CommitFailedException)1 OffsetCommitCallback (org.apache.kafka.clients.consumer.OffsetCommitCallback)1 RetriableCommitFailedException (org.apache.kafka.clients.consumer.RetriableCommitFailedException)1 KafkaException (org.apache.kafka.common.KafkaException)1 Node (org.apache.kafka.common.Node)1