List of usage examples for java.util.concurrent CompletableFuture completeExceptionally
public boolean completeExceptionally(Throwable ex)
From source file:org.apache.pulsar.functions.runtime.ProcessRuntime.java
@Override public CompletableFuture<Void> resetMetrics() { CompletableFuture<Void> retval = new CompletableFuture<>(); if (stub == null) { retval.completeExceptionally(new RuntimeException("Not alive")); return retval; }// w w w. j a v a2 s .c o m ListenableFuture<Empty> response = stub.withDeadlineAfter(GRPC_TIMEOUT_SECS, TimeUnit.SECONDS) .resetMetrics(Empty.newBuilder().build()); Futures.addCallback(response, new FutureCallback<Empty>() { @Override public void onFailure(Throwable throwable) { retval.completeExceptionally(throwable); } @Override public void onSuccess(Empty t) { retval.complete(null); } }); return retval; }
From source file:org.apache.pulsar.functions.runtime.ProcessRuntime.java
@Override public CompletableFuture<InstanceCommunication.MetricsData> getMetrics(int instanceId) { CompletableFuture<InstanceCommunication.MetricsData> retval = new CompletableFuture<>(); if (stub == null) { retval.completeExceptionally(new RuntimeException("Not alive")); return retval; }/*from www .j a v a 2 s.co m*/ ListenableFuture<InstanceCommunication.MetricsData> response = stub .withDeadlineAfter(GRPC_TIMEOUT_SECS, TimeUnit.SECONDS).getMetrics(Empty.newBuilder().build()); Futures.addCallback(response, new FutureCallback<InstanceCommunication.MetricsData>() { @Override public void onFailure(Throwable throwable) { retval.completeExceptionally(throwable); } @Override public void onSuccess(InstanceCommunication.MetricsData t) { retval.complete(t); } }); return retval; }
From source file:org.apache.pulsar.functions.runtime.ProcessRuntime.java
public CompletableFuture<InstanceCommunication.HealthCheckResult> healthCheck() { CompletableFuture<InstanceCommunication.HealthCheckResult> retval = new CompletableFuture<>(); if (stub == null) { retval.completeExceptionally(new RuntimeException("Not alive")); return retval; }/*from w w w .j a v a 2s. c om*/ ListenableFuture<InstanceCommunication.HealthCheckResult> response = stub .withDeadlineAfter(GRPC_TIMEOUT_SECS, TimeUnit.SECONDS).healthCheck(Empty.newBuilder().build()); Futures.addCallback(response, new FutureCallback<InstanceCommunication.HealthCheckResult>() { @Override public void onFailure(Throwable throwable) { retval.completeExceptionally(throwable); } @Override public void onSuccess(InstanceCommunication.HealthCheckResult t) { retval.complete(t); } }); return retval; }
From source file:org.apache.pulsar.io.kafka.connect.PulsarOffsetBackingStore.java
void readToEnd(CompletableFuture<Void> future) { synchronized (this) { if (outstandingReadToEnd != null) { outstandingReadToEnd.whenComplete((result, cause) -> { if (null != cause) { future.completeExceptionally(cause); } else { future.complete(result); }/*from w ww.j a v a 2s .c o m*/ }); // return if the outstanding read has been issued return; } else { outstandingReadToEnd = future; future.whenComplete((result, cause) -> { synchronized (PulsarOffsetBackingStore.this) { outstandingReadToEnd = null; } }); } } producer.flushAsync().whenComplete((ignored, cause) -> { if (null != cause) { future.completeExceptionally(cause); } else { checkAndReadNext(future); } }); }
From source file:org.apache.pulsar.io.kafka.connect.PulsarOffsetBackingStore.java
private void checkAndReadNext(CompletableFuture<Void> endFuture) { reader.hasMessageAvailableAsync().whenComplete((hasMessageAvailable, cause) -> { if (null != cause) { endFuture.completeExceptionally(cause); } else {/*ww w .ja v a 2 s .co m*/ if (hasMessageAvailable) { readNext(endFuture); } else { endFuture.complete(null); } } }); }
From source file:org.apache.pulsar.io.kafka.connect.PulsarOffsetBackingStore.java
private void readNext(CompletableFuture<Void> endFuture) { reader.readNextAsync().whenComplete((message, cause) -> { if (null != cause) { endFuture.completeExceptionally(cause); } else {//from w w w. j av a2 s. c om processMessage(message); checkAndReadNext(endFuture); } }); }
From source file:org.apache.pulsar.testclient.ManagedLedgerWriter.java
public static void main(String[] args) throws Exception { final Arguments arguments = new Arguments(); JCommander jc = new JCommander(arguments); jc.setProgramName("pulsar-perf-producer"); try {//from www . j av a 2s. co m jc.parse(args); } catch (ParameterException e) { System.out.println(e.getMessage()); jc.usage(); System.exit(-1); } if (arguments.help) { jc.usage(); System.exit(-1); } arguments.testTime = TimeUnit.SECONDS.toMillis(arguments.testTime); // Dump config variables ObjectMapper m = new ObjectMapper(); ObjectWriter w = m.writerWithDefaultPrettyPrinter(); log.info("Starting Pulsar managed-ledger perf writer with config: {}", w.writeValueAsString(arguments)); byte[] payloadData = new byte[arguments.msgSize]; ByteBuf payloadBuffer = PooledByteBufAllocator.DEFAULT.directBuffer(arguments.msgSize); payloadBuffer.writerIndex(arguments.msgSize); // Now processing command line arguments String managedLedgerPrefix = "test-" + DigestUtils.sha1Hex(UUID.randomUUID().toString()).substring(0, 5); ClientConfiguration bkConf = new ClientConfiguration(); bkConf.setUseV2WireProtocol(true); bkConf.setAddEntryTimeout(30); bkConf.setReadEntryTimeout(30); bkConf.setThrottleValue(0); bkConf.setNumChannelsPerBookie(arguments.maxConnections); bkConf.setZkServers(arguments.zookeeperServers); ManagedLedgerFactoryConfig mlFactoryConf = new ManagedLedgerFactoryConfig(); mlFactoryConf.setMaxCacheSize(0); ManagedLedgerFactory factory = new ManagedLedgerFactoryImpl(bkConf, mlFactoryConf); ManagedLedgerConfig mlConf = new ManagedLedgerConfig(); mlConf.setEnsembleSize(arguments.ensembleSize); mlConf.setWriteQuorumSize(arguments.writeQuorum); mlConf.setAckQuorumSize(arguments.ackQuorum); mlConf.setMinimumRolloverTime(10, TimeUnit.MINUTES); mlConf.setMetadataEnsembleSize(arguments.ensembleSize); mlConf.setMetadataWriteQuorumSize(arguments.writeQuorum); mlConf.setMetadataAckQuorumSize(arguments.ackQuorum); mlConf.setDigestType(arguments.digestType); mlConf.setMaxSizePerLedgerMb(2048); List<CompletableFuture<ManagedLedger>> futures = new ArrayList<>(); for (int i = 0; i < arguments.numManagedLedgers; i++) { String name = String.format("%s-%03d", managedLedgerPrefix, i); CompletableFuture<ManagedLedger> future = new CompletableFuture<>(); futures.add(future); factory.asyncOpen(name, mlConf, new OpenLedgerCallback() { @Override public void openLedgerComplete(ManagedLedger ledger, Object ctx) { future.complete(ledger); } @Override public void openLedgerFailed(ManagedLedgerException exception, Object ctx) { future.completeExceptionally(exception); } }, null); } List<ManagedLedger> managedLedgers = futures.stream().map(CompletableFuture::join) .collect(Collectors.toList()); log.info("Created {} managed ledgers", managedLedgers.size()); Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { printAggregatedStats(); } }); Collections.shuffle(managedLedgers); AtomicBoolean isDone = new AtomicBoolean(); List<List<ManagedLedger>> managedLedgersPerThread = Lists.partition(managedLedgers, Math.max(1, managedLedgers.size() / arguments.numThreads)); for (int i = 0; i < arguments.numThreads; i++) { List<ManagedLedger> managedLedgersForThisThread = managedLedgersPerThread.get(i); int nunManagedLedgersForThisThread = managedLedgersForThisThread.size(); long numMessagesForThisThread = arguments.numMessages / arguments.numThreads; int maxOutstandingForThisThread = arguments.maxOutstanding; executor.submit(() -> { try { final double msgRate = arguments.msgRate / (double) arguments.numThreads; final RateLimiter rateLimiter = RateLimiter.create(msgRate); // Acquire 1 sec worth of messages to have a slower ramp-up rateLimiter.acquire((int) msgRate); final long startTime = System.currentTimeMillis(); final Semaphore semaphore = new Semaphore(maxOutstandingForThisThread); final AddEntryCallback addEntryCallback = new AddEntryCallback() { @Override public void addComplete(Position position, Object ctx) { long sendTime = (Long) (ctx); messagesSent.increment(); bytesSent.add(payloadData.length); long latencyMicros = NANOSECONDS.toMicros(System.nanoTime() - sendTime); recorder.recordValue(latencyMicros); cumulativeRecorder.recordValue(latencyMicros); semaphore.release(); } @Override public void addFailed(ManagedLedgerException exception, Object ctx) { log.warn("Write error on message", exception); System.exit(-1); } }; // Send messages on all topics/producers long totalSent = 0; while (true) { for (int j = 0; j < nunManagedLedgersForThisThread; j++) { if (arguments.testTime > 0) { if (System.currentTimeMillis() - startTime > arguments.testTime) { log.info("------------------- DONE -----------------------"); printAggregatedStats(); isDone.set(true); Thread.sleep(5000); System.exit(0); } } if (numMessagesForThisThread > 0) { if (totalSent++ >= numMessagesForThisThread) { log.info("------------------- DONE -----------------------"); printAggregatedStats(); isDone.set(true); Thread.sleep(5000); System.exit(0); } } semaphore.acquire(); rateLimiter.acquire(); final long sendTime = System.nanoTime(); managedLedgersForThisThread.get(j).asyncAddEntry(payloadBuffer, addEntryCallback, sendTime); } } } catch (Throwable t) { log.error("Got error", t); } }); } // Print report stats long oldTime = System.nanoTime(); Histogram reportHistogram = null; while (true) { try { Thread.sleep(10000); } catch (InterruptedException e) { break; } if (isDone.get()) { break; } long now = System.nanoTime(); double elapsed = (now - oldTime) / 1e9; double rate = messagesSent.sumThenReset() / elapsed; double throughput = bytesSent.sumThenReset() / elapsed / 1024 / 1024 * 8; reportHistogram = recorder.getIntervalHistogram(reportHistogram); log.info( "Throughput produced: {} msg/s --- {} Mbit/s --- Latency: mean: {} ms - med: {} - 95pct: {} - 99pct: {} - 99.9pct: {} - 99.99pct: {} - Max: {}", throughputFormat.format(rate), throughputFormat.format(throughput), dec.format(reportHistogram.getMean() / 1000.0), dec.format(reportHistogram.getValueAtPercentile(50) / 1000.0), dec.format(reportHistogram.getValueAtPercentile(95) / 1000.0), dec.format(reportHistogram.getValueAtPercentile(99) / 1000.0), dec.format(reportHistogram.getValueAtPercentile(99.9) / 1000.0), dec.format(reportHistogram.getValueAtPercentile(99.99) / 1000.0), dec.format(reportHistogram.getMaxValue() / 1000.0)); reportHistogram.reset(); oldTime = now; } factory.shutdown(); }
From source file:org.apache.pulsar.tests.DockerUtils.java
public static String runCommand(DockerClient docker, String containerId, String... cmd) { CompletableFuture<Boolean> future = new CompletableFuture<>(); String execid = docker.execCreateCmd(containerId).withCmd(cmd).withAttachStderr(true).withAttachStdout(true) .exec().getId();/* www. j av a 2s . c om*/ String cmdString = Arrays.stream(cmd).collect(Collectors.joining(" ")); StringBuffer output = new StringBuffer(); docker.execStartCmd(execid).withDetach(false).exec(new ResultCallback<Frame>() { @Override public void close() { } @Override public void onStart(Closeable closeable) { LOG.info("DOCKER.exec({}:{}): Executing...", containerId, cmdString); } @Override public void onNext(Frame object) { LOG.info("DOCKER.exec({}:{}): {}", containerId, cmdString, object); output.append(new String(object.getPayload())); } @Override public void onError(Throwable throwable) { future.completeExceptionally(throwable); } @Override public void onComplete() { LOG.info("DOCKER.exec({}:{}): Done", containerId, cmdString); future.complete(true); } }); future.join(); InspectExecResponse resp = docker.inspectExecCmd(execid).exec(); while (resp.isRunning()) { try { Thread.sleep(200); } catch (InterruptedException ie) { Thread.currentThread().interrupt(); throw new RuntimeException(ie); } resp = docker.inspectExecCmd(execid).exec(); } int retCode = resp.getExitCode(); if (retCode != 0) { throw new RuntimeException( String.format("cmd(%s) failed on %s with exitcode %d", cmdString, containerId, retCode)); } else { LOG.info("DOCKER.exec({}:{}): completed with {}", containerId, cmdString, retCode); } return output.toString(); }
From source file:org.apache.pulsar.tests.integration.utils.DockerUtils.java
public static void dumpContainerLogToTarget(DockerClient dockerClient, String containerId) { InspectContainerResponse inspectContainerResponse = dockerClient.inspectContainerCmd(containerId).exec(); // docker api returns names prefixed with "/", it's part of it's legacy design, // this removes it to be consistent with what docker ps shows. final String containerName = inspectContainerResponse.getName().replace("/", ""); File output = new File(getTargetDirectory(containerName), "docker.log"); int i = 0;/*from ww w .java 2 s.c om*/ while (output.exists()) { LOG.info("{} exists, incrementing", output); output = new File(getTargetDirectory(containerName), "docker." + i++ + ".log"); } try (FileOutputStream os = new FileOutputStream(output)) { CompletableFuture<Boolean> future = new CompletableFuture<>(); dockerClient.logContainerCmd(containerName).withStdOut(true).withStdErr(true).withTimestamps(true) .exec(new ResultCallback<Frame>() { @Override public void close() { } @Override public void onStart(Closeable closeable) { } @Override public void onNext(Frame object) { try { os.write(object.getPayload()); } catch (IOException e) { onError(e); } } @Override public void onError(Throwable throwable) { future.completeExceptionally(throwable); } @Override public void onComplete() { future.complete(true); } }); future.get(); } catch (RuntimeException | ExecutionException | IOException e) { LOG.error("Error dumping log for {}", containerName, e); } catch (InterruptedException ie) { Thread.currentThread().interrupt(); LOG.info("Interrupted dumping log from container {}", containerName, ie); } }
From source file:org.apache.pulsar.tests.integration.utils.DockerUtils.java
public static ContainerExecResult runCommand(DockerClient docker, String containerId, String... cmd) throws ContainerExecException { CompletableFuture<Boolean> future = new CompletableFuture<>(); String execid = docker.execCreateCmd(containerId).withCmd(cmd).withAttachStderr(true).withAttachStdout(true) .exec().getId();// w ww. j a v a 2 s . c o m String cmdString = Arrays.stream(cmd).collect(Collectors.joining(" ")); StringBuilder stdout = new StringBuilder(); StringBuilder stderr = new StringBuilder(); docker.execStartCmd(execid).withDetach(false).exec(new ResultCallback<Frame>() { @Override public void close() { } @Override public void onStart(Closeable closeable) { LOG.info("DOCKER.exec({}:{}): Executing...", containerId, cmdString); } @Override public void onNext(Frame object) { LOG.info("DOCKER.exec({}:{}): {}", containerId, cmdString, object); if (StreamType.STDOUT == object.getStreamType()) { stdout.append(new String(object.getPayload(), UTF_8)); } else if (StreamType.STDERR == object.getStreamType()) { stderr.append(new String(object.getPayload(), UTF_8)); } } @Override public void onError(Throwable throwable) { future.completeExceptionally(throwable); } @Override public void onComplete() { LOG.info("DOCKER.exec({}:{}): Done", containerId, cmdString); future.complete(true); } }); future.join(); InspectExecResponse resp = docker.inspectExecCmd(execid).exec(); while (resp.isRunning()) { try { Thread.sleep(200); } catch (InterruptedException ie) { Thread.currentThread().interrupt(); throw new RuntimeException(ie); } resp = docker.inspectExecCmd(execid).exec(); } int retCode = resp.getExitCode(); ContainerExecResult result = ContainerExecResult.of(retCode, stdout.toString(), stderr.toString()); LOG.info("DOCKER.exec({}:{}): completed with {}", containerId, cmdString, retCode); if (retCode != 0) { throw new ContainerExecException(cmdString, containerId, result); } return result; }