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));
}
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));
}
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));
}
}
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);
}
}
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);
}
Aggregations