Search in sources :

Example 1 with AclCreation

use of org.apache.kafka.common.message.CreateAclsRequestData.AclCreation 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)

Aggregations

ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 AclBinding (org.apache.kafka.common.acl.AclBinding)1 InvalidRequestException (org.apache.kafka.common.errors.InvalidRequestException)1 UnknownServerException (org.apache.kafka.common.errors.UnknownServerException)1 KafkaFutureImpl (org.apache.kafka.common.internals.KafkaFutureImpl)1 CreateAclsRequestData (org.apache.kafka.common.message.CreateAclsRequestData)1 AclCreation (org.apache.kafka.common.message.CreateAclsRequestData.AclCreation)1 AclCreationResult (org.apache.kafka.common.message.CreateAclsResponseData.AclCreationResult)1 ChannelBuilder (org.apache.kafka.common.network.ChannelBuilder)1 Errors (org.apache.kafka.common.protocol.Errors)1 AbstractResponse (org.apache.kafka.common.requests.AbstractResponse)1 ApiError (org.apache.kafka.common.requests.ApiError)1 CreateAclsRequest (org.apache.kafka.common.requests.CreateAclsRequest)1 CreateAclsResponse (org.apache.kafka.common.requests.CreateAclsResponse)1