List of usage examples for java.util.concurrent CompletableFuture complete
public boolean complete(T value)
From source file:org.apache.pulsar.client.impl.ConsumerImpl.java
@Override public CompletableFuture<Void> unsubscribeAsync() { if (getState() == State.Closing || getState() == State.Closed) { return FutureUtil .failedFuture(new PulsarClientException.AlreadyClosedException("Consumer was already closed")); }/* w w w . j a v a2s .c o m*/ final CompletableFuture<Void> unsubscribeFuture = new CompletableFuture<>(); if (isConnected()) { setState(State.Closing); long requestId = client.newRequestId(); ByteBuf unsubscribe = Commands.newUnsubscribe(consumerId, requestId); ClientCnx cnx = cnx(); cnx.sendRequestWithId(unsubscribe, requestId).thenRun(() -> { cnx.removeConsumer(consumerId); unAckedMessageTracker.close(); if (possibleSendToDeadLetterTopicMessages != null) { possibleSendToDeadLetterTopicMessages.clear(); } client.cleanupConsumer(ConsumerImpl.this); log.info("[{}][{}] Successfully unsubscribed from topic", topic, subscription); setState(State.Closed); unsubscribeFuture.complete(null); }).exceptionally(e -> { log.error("[{}][{}] Failed to unsubscribe: {}", topic, subscription, e.getCause().getMessage()); setState(State.Ready); unsubscribeFuture.completeExceptionally(e.getCause()); return null; }); } else { unsubscribeFuture.completeExceptionally(new PulsarClientException("Not connected to broker")); } return unsubscribeFuture; }
From source file:io.atomix.cluster.messaging.impl.NettyMessagingService.java
private CompletableFuture<Void> startAcceptingConnections() { CompletableFuture<Void> future = new CompletableFuture<>(); ServerBootstrap b = new ServerBootstrap(); b.option(ChannelOption.SO_REUSEADDR, true); b.option(ChannelOption.SO_BACKLOG, 128); b.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(8 * 1024, 32 * 1024)); b.childOption(ChannelOption.SO_RCVBUF, 1024 * 1024); b.childOption(ChannelOption.SO_SNDBUF, 1024 * 1024); b.childOption(ChannelOption.SO_KEEPALIVE, true); b.childOption(ChannelOption.TCP_NODELAY, true); b.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); b.group(serverGroup, clientGroup);//from ww w . j a va2 s . c om b.channel(serverChannelClass); if (enableNettyTls) { b.childHandler(new SslServerCommunicationChannelInitializer()); } else { b.childHandler(new BasicChannelInitializer()); } // Bind and start to accept incoming connections. b.bind(localAddress.port()).addListener((ChannelFutureListener) f -> { if (f.isSuccess()) { log.info("{} accepting incoming connections on port {}", localAddress.address(true), localAddress.port()); serverChannel = f.channel(); future.complete(null); } else { log.warn("{} failed to bind to port {} due to {}", localAddress.address(true), localAddress.port(), f.cause()); future.completeExceptionally(f.cause()); } }); return future; }
From source file:org.apache.bookkeeper.mledger.impl.OffloadPrefixTest.java
@Test public void multipleAutoTriggers() throws Exception { CompletableFuture<Void> slowOffload = new CompletableFuture<>(); CountDownLatch offloadRunning = new CountDownLatch(1); MockLedgerOffloader offloader = new MockLedgerOffloader() { @Override/*from ww w . ja v a2 s .c o m*/ public CompletableFuture<Void> offload(ReadHandle ledger, UUID uuid, Map<String, String> extraMetadata) { offloadRunning.countDown(); return slowOffload.thenCompose((res) -> super.offload(ledger, uuid, extraMetadata)); } }; ManagedLedgerConfig config = new ManagedLedgerConfig(); config.setMaxEntriesPerLedger(10); config.setOffloadAutoTriggerSizeThresholdBytes(100); config.setRetentionTime(10, TimeUnit.MINUTES); config.setLedgerOffloader(offloader); ManagedLedgerImpl ledger = (ManagedLedgerImpl) factory.open("my_test_ledger", config); // Ledger will roll twice, offload will run on first ledger after second closed for (int i = 0; i < 25; i++) { ledger.addEntry(buildEntry(10, "entry-" + i)); } offloadRunning.await(); // trigger a bunch more rolls. Eventually there will be 5 ledgers. // first 3 should be offloaded, 4th is 100bytes, 5th is 0 bytes. // 4th and 5th sum to 100 bytes so they're just at edge of threshold for (int i = 0; i < 20; i++) { ledger.addEntry(buildEntry(10, "entry-" + i)); } // allow the first offload to continue slowOffload.complete(null); assertEventuallyTrue(() -> offloader.offloadedLedgers().size() == 3); Assert.assertEquals(offloader.offloadedLedgers(), ImmutableSet.of(ledger.getLedgersInfoAsList().get(0).getLedgerId(), ledger.getLedgersInfoAsList().get(1).getLedgerId(), ledger.getLedgersInfoAsList().get(2).getLedgerId())); }
From source file:io.atomix.cluster.messaging.impl.NettyMessagingService.java
private CompletableFuture<Channel> getChannel(Address address, String messageType) { List<CompletableFuture<Channel>> channelPool = getChannelPool(address); int offset = getChannelOffset(messageType); CompletableFuture<Channel> channelFuture = channelPool.get(offset); if (channelFuture == null || channelFuture.isCompletedExceptionally()) { synchronized (channelPool) { channelFuture = channelPool.get(offset); if (channelFuture == null || channelFuture.isCompletedExceptionally()) { channelFuture = openChannel(address); channelPool.set(offset, channelFuture); }/*w ww .j a v a 2s . c o m*/ } } final CompletableFuture<Channel> future = new CompletableFuture<>(); final CompletableFuture<Channel> finalFuture = channelFuture; finalFuture.whenComplete((channel, error) -> { if (error == null) { if (!channel.isActive()) { CompletableFuture<Channel> currentFuture; synchronized (channelPool) { currentFuture = channelPool.get(offset); if (currentFuture == finalFuture) { channelPool.set(offset, null); } else if (currentFuture == null) { currentFuture = openChannel(address); channelPool.set(offset, currentFuture); } } final ClientConnection connection = clientConnections.remove(channel); if (connection != null) { connection.close(); } if (currentFuture == finalFuture) { getChannel(address, messageType).whenComplete((recursiveResult, recursiveError) -> { if (recursiveError == null) { future.complete(recursiveResult); } else { future.completeExceptionally(recursiveError); } }); } else { currentFuture.whenComplete((recursiveResult, recursiveError) -> { if (recursiveError == null) { future.complete(recursiveResult); } else { future.completeExceptionally(recursiveError); } }); } } else { future.complete(channel); } } else { future.completeExceptionally(error); } }); return future; }
From source file:org.apache.bookkeeper.mledger.impl.OffloadPrefixTest.java
@Test public void manualTriggerWhileAutoInProgress() throws Exception { CompletableFuture<Void> slowOffload = new CompletableFuture<>(); CountDownLatch offloadRunning = new CountDownLatch(1); MockLedgerOffloader offloader = new MockLedgerOffloader() { @Override//from www . j av a 2s .co m public CompletableFuture<Void> offload(ReadHandle ledger, UUID uuid, Map<String, String> extraMetadata) { offloadRunning.countDown(); return slowOffload.thenCompose((res) -> super.offload(ledger, uuid, extraMetadata)); } }; ManagedLedgerConfig config = new ManagedLedgerConfig(); config.setMaxEntriesPerLedger(10); config.setOffloadAutoTriggerSizeThresholdBytes(100); config.setRetentionTime(10, TimeUnit.MINUTES); config.setLedgerOffloader(offloader); ManagedLedgerImpl ledger = (ManagedLedgerImpl) factory.open("my_test_ledger", config); // Ledger will roll twice, offload will run on first ledger after second closed for (int i = 0; i < 25; i++) { ledger.addEntry(buildEntry(10, "entry-" + i)); } offloadRunning.await(); for (int i = 0; i < 20; i++) { ledger.addEntry(buildEntry(10, "entry-" + i)); } Position p = ledger.addEntry(buildEntry(10, "last-entry")); try { ledger.offloadPrefix(p); Assert.fail("Shouldn't have succeeded"); } catch (ManagedLedgerException.OffloadInProgressException e) { // expected } slowOffload.complete(null); // eventually all over threshold will be offloaded assertEventuallyTrue(() -> offloader.offloadedLedgers().size() == 3); Assert.assertEquals(offloader.offloadedLedgers(), ImmutableSet.of(ledger.getLedgersInfoAsList().get(0).getLedgerId(), ledger.getLedgersInfoAsList().get(1).getLedgerId(), ledger.getLedgersInfoAsList().get(2).getLedgerId())); // then a manual offload can run and offload the one ledger under the threshold ledger.offloadPrefix(p); Assert.assertEquals(offloader.offloadedLedgers().size(), 4); Assert.assertEquals(offloader.offloadedLedgers(), ImmutableSet.of(ledger.getLedgersInfoAsList().get(0).getLedgerId(), ledger.getLedgersInfoAsList().get(1).getLedgerId(), ledger.getLedgersInfoAsList().get(2).getLedgerId(), ledger.getLedgersInfoAsList().get(3).getLedgerId())); }
From source file:org.apache.pulsar.broker.service.persistent.PersistentTopic.java
/** * Delete the managed ledger associated with this topic * * @param failIfHasSubscriptions//w w w. jav a 2 s . c o m * Flag indicating whether delete should succeed if topic still has unconnected subscriptions. Set to * false when called from admin API (it will delete the subs too), and set to true when called from GC * thread * @param closeIfClientsConnected * Flag indicate whether explicitly close connected producers/consumers/replicators before trying to delete topic. If * any client is connected to a topic and if this flag is disable then this operation fails. * * @return Completable future indicating completion of delete operation Completed exceptionally with: * IllegalStateException if topic is still active ManagedLedgerException if ledger delete operation fails */ private CompletableFuture<Void> delete(boolean failIfHasSubscriptions, boolean closeIfClientsConnected) { CompletableFuture<Void> deleteFuture = new CompletableFuture<>(); lock.writeLock().lock(); try { if (isFenced) { log.warn("[{}] Topic is already being closed or deleted", topic); deleteFuture.completeExceptionally(new TopicFencedException("Topic is already fenced")); return deleteFuture; } CompletableFuture<Void> closeClientFuture = new CompletableFuture<>(); if (closeIfClientsConnected) { List<CompletableFuture<Void>> futures = Lists.newArrayList(); replicators.forEach((cluster, replicator) -> futures.add(replicator.disconnect())); producers.forEach(producer -> futures.add(producer.disconnect())); subscriptions.forEach((s, sub) -> futures.add(sub.disconnect())); FutureUtil.waitForAll(futures).thenRun(() -> { closeClientFuture.complete(null); }).exceptionally(ex -> { log.error("[{}] Error closing clients", topic, ex); isFenced = false; closeClientFuture.completeExceptionally(ex); return null; }); } else { closeClientFuture.complete(null); } closeClientFuture.thenAccept(delete -> { if (USAGE_COUNT_UPDATER.get(this) == 0) { isFenced = true; List<CompletableFuture<Void>> futures = Lists.newArrayList(); if (failIfHasSubscriptions) { if (!subscriptions.isEmpty()) { isFenced = false; deleteFuture.completeExceptionally(new TopicBusyException("Topic has subscriptions")); return; } } else { subscriptions.forEach((s, sub) -> futures.add(sub.delete())); } FutureUtil.waitForAll(futures).whenComplete((v, ex) -> { if (ex != null) { log.error("[{}] Error deleting topic", topic, ex); isFenced = false; deleteFuture.completeExceptionally(ex); } else { ledger.asyncDelete(new AsyncCallbacks.DeleteLedgerCallback() { @Override public void deleteLedgerComplete(Object ctx) { brokerService.removeTopicFromCache(topic); log.info("[{}] Topic deleted", topic); deleteFuture.complete(null); } @Override public void deleteLedgerFailed(ManagedLedgerException exception, Object ctx) { isFenced = false; log.error("[{}] Error deleting topic", topic, exception); deleteFuture.completeExceptionally(new PersistenceException(exception)); } }, null); } }); } else { deleteFuture.completeExceptionally(new TopicBusyException( "Topic has " + USAGE_COUNT_UPDATER.get(this) + " connected producers/consumers")); } }).exceptionally(ex -> { deleteFuture.completeExceptionally( new TopicBusyException("Failed to close clients before deleting topic.")); return null; }); } finally { lock.writeLock().unlock(); } return deleteFuture; }
From source file:org.apache.pulsar.client.impl.ConsumerImpl.java
private void internalGetLastMessageIdAsync(final Backoff backoff, final AtomicLong remainingTime, CompletableFuture<MessageId> future) { ClientCnx cnx = cnx();//from www . ja v a 2s . c om if (isConnected() && cnx != null) { if (!Commands.peerSupportsGetLastMessageId(cnx.getRemoteEndpointProtocolVersion())) { future.completeExceptionally(new PulsarClientException.NotSupportedException( "GetLastMessageId Not supported for ProtocolVersion: " + cnx.getRemoteEndpointProtocolVersion())); } long requestId = client.newRequestId(); ByteBuf getLastIdCmd = Commands.newGetLastMessageId(consumerId, requestId); log.info("[{}][{}] Get topic last message Id", topic, subscription); cnx.sendGetLastMessageId(getLastIdCmd, requestId).thenAccept((result) -> { log.info("[{}][{}] Successfully getLastMessageId {}:{}", topic, subscription, result.getLedgerId(), result.getEntryId()); future.complete( new MessageIdImpl(result.getLedgerId(), result.getEntryId(), result.getPartition())); }).exceptionally(e -> { log.error("[{}][{}] Failed getLastMessageId command", topic, subscription); future.completeExceptionally(e.getCause()); return null; }); } else { long nextDelay = Math.min(backoff.next(), remainingTime.get()); if (nextDelay <= 0) { future.completeExceptionally(new PulsarClientException.TimeoutException( "Could not getLastMessageId within configured timeout.")); return; } ((ScheduledExecutorService) listenerExecutor).schedule(() -> { log.warn("[{}] [{}] Could not get connection while getLastMessageId -- Will try again in {} ms", topic, getHandlerName(), nextDelay); remainingTime.addAndGet(-nextDelay); internalGetLastMessageIdAsync(backoff, remainingTime, future); }, nextDelay, TimeUnit.MILLISECONDS); } }
From source file:org.apache.bookkeeper.mledger.impl.OffloadPrefixTest.java
@Test public void autoTriggerWhileManualInProgress() throws Exception { CompletableFuture<Void> slowOffload = new CompletableFuture<>(); CountDownLatch offloadRunning = new CountDownLatch(1); MockLedgerOffloader offloader = new MockLedgerOffloader() { @Override/*from w w w. j a va2s . c o m*/ public CompletableFuture<Void> offload(ReadHandle ledger, UUID uuid, Map<String, String> extraMetadata) { offloadRunning.countDown(); return slowOffload.thenCompose((res) -> super.offload(ledger, uuid, extraMetadata)); } }; ManagedLedgerConfig config = new ManagedLedgerConfig(); config.setMaxEntriesPerLedger(10); config.setOffloadAutoTriggerSizeThresholdBytes(100); config.setRetentionTime(10, TimeUnit.MINUTES); config.setLedgerOffloader(offloader); ManagedLedgerImpl ledger = (ManagedLedgerImpl) factory.open("my_test_ledger", config); // Ledger rolls once, threshold not hit so auto shouldn't run for (int i = 0; i < 14; i++) { ledger.addEntry(buildEntry(10, "entry-" + i)); } Position p = ledger.addEntry(buildEntry(10, "trigger-entry")); OffloadCallbackPromise cbPromise = new OffloadCallbackPromise(); ledger.asyncOffloadPrefix(p, cbPromise, null); offloadRunning.await(); // add enough entries to roll the ledger a couple of times and trigger some offloads for (int i = 0; i < 20; i++) { ledger.addEntry(buildEntry(10, "entry-" + i)); } // allow the manual offload to complete slowOffload.complete(null); Assert.assertEquals(cbPromise.join(), PositionImpl.get(ledger.getLedgersInfoAsList().get(1).getLedgerId(), 0)); // auto trigger should eventually offload everything else over threshold assertEventuallyTrue(() -> offloader.offloadedLedgers().size() == 2); Assert.assertEquals(offloader.offloadedLedgers(), ImmutableSet.of(ledger.getLedgersInfoAsList().get(0).getLedgerId(), ledger.getLedgersInfoAsList().get(1).getLedgerId())); }
From source file:com.yahoo.pulsar.broker.namespace.NamespaceService.java
/** * 1. split the given bundle into two bundles 2. assign ownership of both the bundles to current broker 3. update * policies with newly created bundles into LocalZK 4. disable original bundle and refresh the cache * * @param bundle// www. j a v a 2 s . c o m * @return * @throws Exception */ public CompletableFuture<Void> splitAndOwnBundle(NamespaceBundle bundle) throws Exception { final CompletableFuture<Void> future = new CompletableFuture<>(); Pair<NamespaceBundles, List<NamespaceBundle>> splittedBundles = bundleFactory.splitBundles(bundle, 2 /* by default split into 2 */); if (splittedBundles != null) { checkNotNull(splittedBundles.getLeft()); checkNotNull(splittedBundles.getRight()); checkArgument(splittedBundles.getRight().size() == 2, "bundle has to be split in two bundles"); NamespaceName nsname = bundle.getNamespaceObject(); try { // take ownership of newly split bundles for (NamespaceBundle sBundle : splittedBundles.getRight()) { checkNotNull(ownershipCache.tryAcquiringOwnership(sBundle)); } updateNamespaceBundles(nsname, splittedBundles.getLeft(), (rc, path, zkCtx, stat) -> pulsar.getOrderedExecutor().submit(safeRun(() -> { if (rc == KeeperException.Code.OK.intValue()) { // disable old bundle try { ownershipCache.disableOwnership(bundle); // invalidate cache as zookeeper has new split // namespace bundle bundleFactory.invalidateBundleCache(nsname); // update bundled_topic cache for load-report-generation pulsar.getBrokerService().refreshTopicToStatsMaps(bundle); loadManager.setLoadReportForceUpdateFlag(); future.complete(null); } catch (Exception e) { String msg1 = format( "failed to disable bundle %s under namespace [%s] with error %s", nsname.toString(), bundle.toString(), e.getMessage()); LOG.warn(msg1, e); future.completeExceptionally(new ServiceUnitNotReadyException(msg1)); } } else { String msg2 = format("failed to update namespace [%s] policies due to %s", nsname.toString(), KeeperException.create(KeeperException.Code.get(rc)).getMessage()); LOG.warn(msg2); future.completeExceptionally(new ServiceUnitNotReadyException(msg2)); } }))); } catch (Exception e) { String msg = format("failed to aquire ownership of split bundle for namespace [%s], %s", nsname.toString(), e.getMessage()); LOG.warn(msg, e); future.completeExceptionally(new ServiceUnitNotReadyException(msg)); } } else { String msg = format("bundle %s not found under namespace", bundle.toString()); future.completeExceptionally(new ServiceUnitNotReadyException(msg)); } return future; }
From source file:org.apache.pulsar.broker.service.persistent.PersistentTopic.java
CompletableFuture<Void> startReplicator(String remoteCluster) { log.info("[{}] Starting replicator to remote: {}", topic, remoteCluster); final CompletableFuture<Void> future = new CompletableFuture<>(); String name = PersistentReplicator.getReplicatorName(replicatorPrefix, remoteCluster); ledger.asyncOpenCursor(name, new OpenCursorCallback() { @Override//from www . jav a2 s . c o m public void openCursorComplete(ManagedCursor cursor, Object ctx) { String localCluster = brokerService.pulsar().getConfiguration().getClusterName(); boolean isReplicatorStarted = addReplicationCluster(remoteCluster, PersistentTopic.this, cursor, localCluster); if (isReplicatorStarted) { future.complete(null); } else { future.completeExceptionally(new NamingException( PersistentTopic.this.getName() + " Failed to start replicator " + remoteCluster)); } } @Override public void openCursorFailed(ManagedLedgerException exception, Object ctx) { future.completeExceptionally(new PersistenceException(exception)); } }, null); return future; }