@Test public void testCreatePartitions() throws Exception { try (AdminClientUnitTestEnv env = mockClientEnv()) { env.kafkaClient().setNodeApiVersions(NodeApiVersions.create()); Map<String, ApiError> m = new HashMap<>(); m.put("my_topic", ApiError.NONE); m.put("other_topic", ApiError.fromThrowable(new InvalidTopicException("some detailed reason"))); // Test a call where one filter has an error. env.kafkaClient().prepareResponse(new CreatePartitionsResponse(0, m)); Map<String, NewPartitions> counts = new HashMap<>(); counts.put("my_topic", NewPartitions.increaseTo(3)); counts.put("other_topic", NewPartitions.increaseTo(3, asList(asList(2), asList(3)))); CreatePartitionsResult results = env.adminClient().createPartitions(counts); Map<String, KafkaFuture<Void>> values = results.values(); KafkaFuture<Void> myTopicResult = values.get("my_topic"); myTopicResult.get(); KafkaFuture<Void> otherTopicResult = values.get("other_topic"); try { otherTopicResult.get(); fail("get() should throw ExecutionException"); } catch (ExecutionException e0) { assertTrue(e0.getCause() instanceof InvalidTopicException); InvalidTopicException e = (InvalidTopicException) e0.getCause(); assertEquals("some detailed reason", e.getMessage()); } } }
return new CreatePartitionsResult(new HashMap<>(futures));
counts.put("my_topic", NewPartitions.increaseTo(3)); counts.put("other_topic", NewPartitions.increaseTo(3, asList(asList(2), asList(3)))); env.adminClient().createPartitions(counts).all().get(); fail("Expected an authentication error."); } catch (ExecutionException e) {
private void modifyTopics(AdminClient adminClient, Map<String, NewPartitions> topicsToModify) { CreatePartitionsResult partitionsResult = adminClient.createPartitions(topicsToModify); try { partitionsResult.all().get(this.operationTimeout, TimeUnit.SECONDS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); logger.error("Interrupted while waiting for partition creation results", e); } catch (TimeoutException e) { throw new KafkaException("Timed out waiting for create partitions results", e); } catch (ExecutionException e) { if (e.getCause() instanceof InvalidPartitionsException) { // Possible race with another app instance logger.debug("Failed to create partitions", e.getCause()); } else { logger.error("Failed to create partitions", e.getCause()); if (!(e.getCause() instanceof UnsupportedVersionException)) { throw new KafkaException("Failed to create partitions", e.getCause()); // NOSONAR } } } }
CreatePartitionsResult partitions = adminClient.createPartitions( Collections.singletonMap(topicName, NewPartitions.increaseTo(effectivePartitionCount))); partitions.all().get(this.operationTimeout, TimeUnit.SECONDS);
@Override public void increasePartitions(Topic topic, Handler<AsyncResult<Void>> handler) { final NewPartitions newPartitions = NewPartitions.increaseTo(topic.getNumPartitions()); final Map<String, NewPartitions> request = Collections.singletonMap(topic.getTopicName().toString(), newPartitions); KafkaFuture<Void> future = adminClient.createPartitions(request).values().get(topic.getTopicName().toString()); queueWork(new UniWork<>("increasePartitions", future, handler)); }
CreatePartitionsResult partitions = adminClient.createPartitions( Collections.singletonMap(topicName, NewPartitions.increaseTo(effectivePartitionCount))); partitions.all().get(this.operationTimeout, TimeUnit.SECONDS);