Search in sources :

Example 6 with UnknownServerException

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

the class KafkaAdminClient method deleteAcls.

@Override
public DeleteAclsResult deleteAcls(Collection<AclBindingFilter> filters, DeleteAclsOptions options) {
    final long now = time.milliseconds();
    final Map<AclBindingFilter, KafkaFutureImpl<FilterResults>> futures = new HashMap<>();
    final List<AclBindingFilter> aclBindingFiltersSent = new ArrayList<>();
    final List<DeleteAclsFilter> deleteAclsFilters = new ArrayList<>();
    for (AclBindingFilter filter : filters) {
        if (futures.get(filter) == null) {
            aclBindingFiltersSent.add(filter);
            deleteAclsFilters.add(DeleteAclsRequest.deleteAclsFilter(filter));
            futures.put(filter, new KafkaFutureImpl<>());
        }
    }
    final DeleteAclsRequestData data = new DeleteAclsRequestData().setFilters(deleteAclsFilters);
    runnable.call(new Call("deleteAcls", calcDeadlineMs(now, options.timeoutMs()), new LeastLoadedNodeProvider()) {

        @Override
        DeleteAclsRequest.Builder createRequest(int timeoutMs) {
            return new DeleteAclsRequest.Builder(data);
        }

        @Override
        void handleResponse(AbstractResponse abstractResponse) {
            DeleteAclsResponse response = (DeleteAclsResponse) abstractResponse;
            List<DeleteAclsResponseData.DeleteAclsFilterResult> results = response.filterResults();
            Iterator<DeleteAclsResponseData.DeleteAclsFilterResult> iter = results.iterator();
            for (AclBindingFilter bindingFilter : aclBindingFiltersSent) {
                KafkaFutureImpl<FilterResults> future = futures.get(bindingFilter);
                if (!iter.hasNext()) {
                    future.completeExceptionally(new UnknownServerException("The broker reported no deletion result for the given filter."));
                } else {
                    DeleteAclsFilterResult filterResult = iter.next();
                    ApiError error = new ApiError(Errors.forCode(filterResult.errorCode()), filterResult.errorMessage());
                    if (error.isFailure()) {
                        future.completeExceptionally(error.exception());
                    } else {
                        List<FilterResult> filterResults = new ArrayList<>();
                        for (DeleteAclsMatchingAcl matchingAcl : filterResult.matchingAcls()) {
                            ApiError aclError = new ApiError(Errors.forCode(matchingAcl.errorCode()), matchingAcl.errorMessage());
                            AclBinding aclBinding = DeleteAclsResponse.aclBinding(matchingAcl);
                            filterResults.add(new FilterResult(aclBinding, aclError.exception()));
                        }
                        future.complete(new FilterResults(filterResults));
                    }
                }
            }
        }

        @Override
        void handleFailure(Throwable throwable) {
            completeAllExceptionally(futures.values(), throwable);
        }
    }, now);
    return new DeleteAclsResult(new HashMap<>(futures));
}
Also used : HashMap(java.util.HashMap) ChannelBuilder(org.apache.kafka.common.network.ChannelBuilder) ArrayList(java.util.ArrayList) DeleteAclsRequestData(org.apache.kafka.common.message.DeleteAclsRequestData) DeleteAclsResponse(org.apache.kafka.common.requests.DeleteAclsResponse) DeleteAclsFilterResult(org.apache.kafka.common.message.DeleteAclsResponseData.DeleteAclsFilterResult) Iterator(java.util.Iterator) LinkedList(java.util.LinkedList) ArrayList(java.util.ArrayList) List(java.util.List) AclBinding(org.apache.kafka.common.acl.AclBinding) FilterResults(org.apache.kafka.clients.admin.DeleteAclsResult.FilterResults) AclBindingFilter(org.apache.kafka.common.acl.AclBindingFilter) AbstractResponse(org.apache.kafka.common.requests.AbstractResponse) DeleteAclsResponseData(org.apache.kafka.common.message.DeleteAclsResponseData) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) DeleteAclsRequest(org.apache.kafka.common.requests.DeleteAclsRequest) DeleteAclsFilter(org.apache.kafka.common.message.DeleteAclsRequestData.DeleteAclsFilter) UnknownServerException(org.apache.kafka.common.errors.UnknownServerException) ApiError(org.apache.kafka.common.requests.ApiError) DeleteAclsFilterResult(org.apache.kafka.common.message.DeleteAclsResponseData.DeleteAclsFilterResult) FilterResult(org.apache.kafka.clients.admin.DeleteAclsResult.FilterResult) DeleteAclsMatchingAcl(org.apache.kafka.common.message.DeleteAclsResponseData.DeleteAclsMatchingAcl)

Example 7 with UnknownServerException

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

the class KafkaAdminClient method createAcls.

@Override
public CreateAclsResult createAcls(Collection<AclBinding> acls, CreateAclsOptions options) {
    final long now = time.milliseconds();
    final Map<AclBinding, KafkaFutureImpl<Void>> futures = new HashMap<>();
    final List<AclCreation> aclCreations = new ArrayList<>();
    final List<AclBinding> aclBindingsSent = new ArrayList<>();
    for (AclBinding acl : acls) {
        if (futures.get(acl) == null) {
            KafkaFutureImpl<Void> future = new KafkaFutureImpl<>();
            futures.put(acl, future);
            String indefinite = acl.toFilter().findIndefiniteField();
            if (indefinite == null) {
                aclCreations.add(CreateAclsRequest.aclCreation(acl));
                aclBindingsSent.add(acl);
            } else {
                future.completeExceptionally(new InvalidRequestException("Invalid ACL creation: " + indefinite));
            }
        }
    }
    final CreateAclsRequestData data = new CreateAclsRequestData().setCreations(aclCreations);
    runnable.call(new Call("createAcls", calcDeadlineMs(now, options.timeoutMs()), new LeastLoadedNodeProvider()) {

        @Override
        CreateAclsRequest.Builder createRequest(int timeoutMs) {
            return new CreateAclsRequest.Builder(data);
        }

        @Override
        void handleResponse(AbstractResponse abstractResponse) {
            CreateAclsResponse response = (CreateAclsResponse) abstractResponse;
            List<AclCreationResult> responses = response.results();
            Iterator<AclCreationResult> iter = responses.iterator();
            for (AclBinding aclBinding : aclBindingsSent) {
                KafkaFutureImpl<Void> future = futures.get(aclBinding);
                if (!iter.hasNext()) {
                    future.completeExceptionally(new UnknownServerException("The broker reported no creation result for the given ACL: " + aclBinding));
                } else {
                    AclCreationResult creation = iter.next();
                    Errors error = Errors.forCode(creation.errorCode());
                    ApiError apiError = new ApiError(error, creation.errorMessage());
                    if (apiError.isFailure())
                        future.completeExceptionally(apiError.exception());
                    else
                        future.complete(null);
                }
            }
        }

        @Override
        void handleFailure(Throwable throwable) {
            completeAllExceptionally(futures.values(), throwable);
        }
    }, now);
    return new CreateAclsResult(new HashMap<>(futures));
}
Also used : HashMap(java.util.HashMap) ChannelBuilder(org.apache.kafka.common.network.ChannelBuilder) ArrayList(java.util.ArrayList) CreateAclsResponse(org.apache.kafka.common.requests.CreateAclsResponse) CreateAclsRequest(org.apache.kafka.common.requests.CreateAclsRequest) Iterator(java.util.Iterator) InvalidRequestException(org.apache.kafka.common.errors.InvalidRequestException) LinkedList(java.util.LinkedList) ArrayList(java.util.ArrayList) List(java.util.List) AclBinding(org.apache.kafka.common.acl.AclBinding) AclCreation(org.apache.kafka.common.message.CreateAclsRequestData.AclCreation) AbstractResponse(org.apache.kafka.common.requests.AbstractResponse) CreateAclsRequestData(org.apache.kafka.common.message.CreateAclsRequestData) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) UnknownServerException(org.apache.kafka.common.errors.UnknownServerException) Errors(org.apache.kafka.common.protocol.Errors) ApiError(org.apache.kafka.common.requests.ApiError) AclCreationResult(org.apache.kafka.common.message.CreateAclsResponseData.AclCreationResult)

Example 8 with UnknownServerException

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

the class RequestResponseTest method checkErrorResponse.

private void checkErrorResponse(AbstractRequest req, Throwable e) {
    AbstractResponse response = req.getErrorResponse(e);
    checkResponse(response, req.version());
    Errors error = Errors.forException(e);
    Map<Errors, Integer> errorCounts = response.errorCounts();
    assertEquals(Collections.singleton(error), errorCounts.keySet(), "API Key " + req.apiKey().name + " v" + req.version() + " failed errorCounts test");
    assertTrue(errorCounts.get(error) > 0);
    if (e instanceof UnknownServerException) {
        String responseStr = response.toString();
        assertFalse(responseStr.contains(e.getMessage()), String.format("Unknown message included in response for %s: %s ", req.apiKey(), responseStr));
    }
}
Also used : Errors(org.apache.kafka.common.protocol.Errors) UnknownServerException(org.apache.kafka.common.errors.UnknownServerException)

Example 9 with UnknownServerException

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

the class KafkaAdminClientTest method testDescribeLogDirs.

@Test
public void testDescribeLogDirs() throws ExecutionException, InterruptedException {
    Set<Integer> brokers = singleton(0);
    String logDir = "/var/data/kafka";
    TopicPartition tp = new TopicPartition("topic", 12);
    long partitionSize = 1234567890;
    long offsetLag = 24;
    try (AdminClientUnitTestEnv env = mockClientEnv()) {
        env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
        env.kafkaClient().prepareResponseFrom(prepareDescribeLogDirsResponse(Errors.NONE, logDir, tp, partitionSize, offsetLag), env.cluster().nodeById(0));
        DescribeLogDirsResult result = env.adminClient().describeLogDirs(brokers);
        Map<Integer, KafkaFuture<Map<String, LogDirDescription>>> descriptions = result.descriptions();
        assertEquals(brokers, descriptions.keySet());
        assertNotNull(descriptions.get(0));
        assertDescriptionContains(descriptions.get(0).get(), logDir, tp, partitionSize, offsetLag);
        Map<Integer, Map<String, LogDirDescription>> allDescriptions = result.allDescriptions().get();
        assertEquals(brokers, allDescriptions.keySet());
        assertDescriptionContains(allDescriptions.get(0), logDir, tp, partitionSize, offsetLag);
        // Empty results when not authorized with version < 3
        env.kafkaClient().prepareResponseFrom(prepareEmptyDescribeLogDirsResponse(Optional.empty()), env.cluster().nodeById(0));
        final DescribeLogDirsResult errorResult = env.adminClient().describeLogDirs(brokers);
        ExecutionException exception = assertThrows(ExecutionException.class, () -> errorResult.allDescriptions().get());
        assertTrue(exception.getCause() instanceof ClusterAuthorizationException);
        // Empty results with an error with version >= 3
        env.kafkaClient().prepareResponseFrom(prepareEmptyDescribeLogDirsResponse(Optional.of(Errors.UNKNOWN_SERVER_ERROR)), env.cluster().nodeById(0));
        final DescribeLogDirsResult errorResult2 = env.adminClient().describeLogDirs(brokers);
        exception = assertThrows(ExecutionException.class, () -> errorResult2.allDescriptions().get());
        assertTrue(exception.getCause() instanceof UnknownServerException);
    }
}
Also used : KafkaFuture(org.apache.kafka.common.KafkaFuture) UnknownServerException(org.apache.kafka.common.errors.UnknownServerException) TopicPartition(org.apache.kafka.common.TopicPartition) ExecutionException(java.util.concurrent.ExecutionException) Map(java.util.Map) HashMap(java.util.HashMap) ClusterAuthorizationException(org.apache.kafka.common.errors.ClusterAuthorizationException) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 10 with UnknownServerException

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

the class PartitionLeaderStrategyTest method testUnexpectedPartitionError.

@Test
public void testUnexpectedPartitionError() {
    TopicPartition topicPartition = new TopicPartition("foo", 0);
    Throwable exception = assertFatalPartitionError(topicPartition, Errors.UNKNOWN_SERVER_ERROR);
    assertTrue(exception instanceof UnknownServerException);
}
Also used : TopicPartition(org.apache.kafka.common.TopicPartition) 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