List of usage examples for java.util.concurrent CompletableFuture CompletableFuture
public CompletableFuture()
From source file:org.apache.distributedlog.lock.ZKSessionLock.java
private CompletableFuture<String> checkLockOwnerAndWaitIfPossible(final LockWatcher lockWatcher, final boolean wait) { final CompletableFuture<String> promise = new CompletableFuture<String>(); checkLockOwnerAndWaitIfPossible(lockWatcher, wait, promise); return promise; }
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 w ww. ja v a 2 s. co 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; }
From source file:org.apache.bookkeeper.client.BookieWriteLedgerTest.java
@Test @SuppressWarnings("unchecked") public void testLedgerCreateAdvByteBufRefCnt() throws Exception { long ledgerId = rng.nextLong(); ledgerId &= Long.MAX_VALUE; if (!baseConf.getLedgerManagerFactoryClass().equals(LongHierarchicalLedgerManagerFactory.class)) { // since LongHierarchicalLedgerManager supports ledgerIds of // decimal length upto 19 digits but other // LedgerManagers only upto 10 decimals ledgerId %= 9999999999L;//from w w w. ja v a2 s . c o m } final LedgerHandle lh = bkc.createLedgerAdv(ledgerId, 5, 3, 2, digestType, ledgerPassword, null); final List<AbstractByteBufAllocator> allocs = Lists.newArrayList(new PooledByteBufAllocator(true), new PooledByteBufAllocator(false), new UnpooledByteBufAllocator(true), new UnpooledByteBufAllocator(false)); long entryId = 0; for (AbstractByteBufAllocator alloc : allocs) { final ByteBuf data = alloc.buffer(10); data.writeBytes(("fragment0" + entryId).getBytes()); assertEquals("ref count on ByteBuf should be 1", 1, data.refCnt()); CompletableFuture<Integer> cf = new CompletableFuture<>(); lh.asyncAddEntry(entryId, data, (rc, handle, eId, qwcLatency, ctx) -> { CompletableFuture<Integer> future = (CompletableFuture<Integer>) ctx; future.complete(rc); }, cf); int rc = cf.get(); assertEquals("rc code is OK", BKException.Code.OK, rc); for (int i = 0; i < 10; i++) { if (data.refCnt() == 0) { break; } TimeUnit.MILLISECONDS.sleep(250); // recycler runs asynchronously } assertEquals("writing entry with id " + entryId + ", ref count on ByteBuf should be 0 ", 0, data.refCnt()); org.apache.bookkeeper.client.api.LedgerEntry e = lh.read(entryId, entryId).getEntry(entryId); assertEquals("entry data is correct", "fragment0" + entryId, new String(e.getEntryBytes())); entryId++; } bkc.deleteLedger(lh.ledgerId); }
From source file:org.apache.pulsar.broker.service.persistent.PersistentTopic.java
CompletableFuture<Void> removeReplicator(String remoteCluster) { log.info("[{}] Removing replicator to {}", topic, remoteCluster); final CompletableFuture<Void> future = new CompletableFuture<>(); String name = PersistentReplicator.getReplicatorName(replicatorPrefix, remoteCluster); replicators.get(remoteCluster).disconnect().thenRun(() -> { ledger.asyncDeleteCursor(name, new DeleteCursorCallback() { @Override//from w ww . j av a2s. c om public void deleteCursorComplete(Object ctx) { replicators.remove(remoteCluster); future.complete(null); } @Override public void deleteCursorFailed(ManagedLedgerException exception, Object ctx) { log.error("[{}] Failed to delete cursor {} {}", topic, name, exception.getMessage(), exception); future.completeExceptionally(new PersistenceException(exception)); } }, null); }).exceptionally(e -> { log.error("[{}] Failed to close replication producer {} {}", topic, name, e.getMessage(), e); future.completeExceptionally(e); return null; }); return future; }
From source file:org.apache.bookkeeper.client.BookieWriteLedgerTest.java
@Test @SuppressWarnings("unchecked") public void testLedgerCreateByteBufRefCnt() throws Exception { final LedgerHandle lh = bkc.createLedger(5, 3, 2, digestType, ledgerPassword, null); final List<AbstractByteBufAllocator> allocs = Lists.newArrayList(new PooledByteBufAllocator(true), new PooledByteBufAllocator(false), new UnpooledByteBufAllocator(true), new UnpooledByteBufAllocator(false)); int entryId = 0; for (AbstractByteBufAllocator alloc : allocs) { final ByteBuf data = alloc.buffer(10); data.writeBytes(("fragment0" + entryId).getBytes()); assertEquals("ref count on ByteBuf should be 1", 1, data.refCnt()); CompletableFuture<Integer> cf = new CompletableFuture<>(); lh.asyncAddEntry(data, (rc, handle, eId, ctx) -> { CompletableFuture<Integer> future = (CompletableFuture<Integer>) ctx; future.complete(rc);/*from w w w .ja v a 2 s .c o m*/ }, cf); int rc = cf.get(); assertEquals("rc code is OK", BKException.Code.OK, rc); for (int i = 0; i < 10; i++) { if (data.refCnt() == 0) { break; } TimeUnit.MILLISECONDS.sleep(250); // recycler runs asynchronously } assertEquals("writing entry with id " + entryId + ", ref count on ByteBuf should be 0 ", 0, data.refCnt()); org.apache.bookkeeper.client.api.LedgerEntry e = lh.read(entryId, entryId).getEntry(entryId); assertEquals("entry data is correct", "fragment0" + entryId, new String(e.getEntryBytes())); entryId++; } bkc.deleteLedger(lh.ledgerId); }
From source file:org.apache.hadoop.hbase.client.RawAsyncHBaseAdmin.java
@Override public CompletableFuture<Void> flush(TableName tableName) { CompletableFuture<Void> future = new CompletableFuture<>(); addListener(tableExists(tableName), (exists, err) -> { if (err != null) { future.completeExceptionally(err); } else if (!exists) { future.completeExceptionally(new TableNotFoundException(tableName)); } else {/*ww w . j a va 2 s . c o m*/ addListener(isTableEnabled(tableName), (tableEnabled, err2) -> { if (err2 != null) { future.completeExceptionally(err2); } else if (!tableEnabled) { future.completeExceptionally(new TableNotEnabledException(tableName)); } else { addListener(execProcedure(FLUSH_TABLE_PROCEDURE_SIGNATURE, tableName.getNameAsString(), new HashMap<>()), (ret, err3) -> { if (err3 != null) { future.completeExceptionally(err3); } else { future.complete(ret); } }); } }); } }); return future; }
From source file:org.apache.hadoop.hbase.client.RawAsyncHBaseAdmin.java
@Override public CompletableFuture<Void> flushRegion(byte[] regionName) { CompletableFuture<Void> future = new CompletableFuture<>(); addListener(getRegionLocation(regionName), (location, err) -> { if (err != null) { future.completeExceptionally(err); return; }/* ww w .j a v a 2 s .c om*/ ServerName serverName = location.getServerName(); if (serverName == null) { future.completeExceptionally(new NoServerForRegionException(Bytes.toStringBinary(regionName))); return; } addListener(flush(serverName, location.getRegion()), (ret, err2) -> { if (err2 != null) { future.completeExceptionally(err2); } else { future.complete(ret); } }); }); return future; }
From source file:org.apache.hadoop.hbase.client.RawAsyncHBaseAdmin.java
@Override public CompletableFuture<Void> flushRegionServer(ServerName sn) { CompletableFuture<Void> future = new CompletableFuture<>(); addListener(getRegions(sn), (hRegionInfos, err) -> { if (err != null) { future.completeExceptionally(err); return; }//w ww. ja v a 2 s . c o m List<CompletableFuture<Void>> compactFutures = new ArrayList<>(); if (hRegionInfos != null) { hRegionInfos.forEach(region -> compactFutures.add(flush(sn, region))); } addListener( CompletableFuture .allOf(compactFutures.toArray(new CompletableFuture<?>[compactFutures.size()])), (ret, err2) -> { if (err2 != null) { future.completeExceptionally(err2); } else { future.complete(ret); } }); }); return future; }
From source file:org.apache.pulsar.client.impl.ConsumerImpl.java
@Override public CompletableFuture<Void> seekAsync(MessageId messageId) { if (getState() == State.Closing || getState() == State.Closed) { return FutureUtil .failedFuture(new PulsarClientException.AlreadyClosedException("Consumer was already closed")); }/* w w w . j a v a2 s . c o m*/ if (!isConnected()) { return FutureUtil.failedFuture(new PulsarClientException("Not connected to broker")); } final CompletableFuture<Void> seekFuture = new CompletableFuture<>(); long requestId = client.newRequestId(); MessageIdImpl msgId = (MessageIdImpl) messageId; ByteBuf seek = Commands.newSeek(consumerId, requestId, msgId.getLedgerId(), msgId.getEntryId()); ClientCnx cnx = cnx(); log.info("[{}][{}] Seek subscription to message id {}", topic, subscription, messageId); cnx.sendRequestWithId(seek, requestId).thenRun(() -> { log.info("[{}][{}] Successfully reset subscription to message id {}", topic, subscription, messageId); seekFuture.complete(null); }).exceptionally(e -> { log.error("[{}][{}] Failed to reset subscription: {}", topic, subscription, e.getCause().getMessage()); seekFuture.completeExceptionally(e.getCause()); return null; }); return seekFuture; }
From source file:org.apache.hadoop.hbase.client.RawAsyncHBaseAdmin.java
private CompletableFuture<Void> compactRegionServer(ServerName sn, boolean major) { CompletableFuture<Void> future = new CompletableFuture<>(); addListener(getRegions(sn), (hRegionInfos, err) -> { if (err != null) { future.completeExceptionally(err); return; }//from www .j a v a 2 s.co m List<CompletableFuture<Void>> compactFutures = new ArrayList<>(); if (hRegionInfos != null) { hRegionInfos.forEach(region -> compactFutures.add(compact(sn, region, major, null))); } addListener( CompletableFuture .allOf(compactFutures.toArray(new CompletableFuture<?>[compactFutures.size()])), (ret, err2) -> { if (err2 != null) { future.completeExceptionally(err2); } else { future.complete(ret); } }); }); return future; }