Search in sources :

Example 76 with CompletableFuture

use of java.util.concurrent.CompletableFuture in project jodd by oblac.

the class Futures method failAfter.

/**
	 * Returns {@code CompletableFuture} that fails after certain number of milliseconds.
	 */
public static <T> CompletableFuture<T> failAfter(long duration) {
    final CompletableFuture<T> promise = new CompletableFuture<>();
    SCHEDULER.schedule(() -> {
        final TimeoutException ex = new TimeoutException("Timeout after " + duration);
        return promise.completeExceptionally(ex);
    }, duration, MILLISECONDS);
    return promise;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) TimeoutException(java.util.concurrent.TimeoutException)

Example 77 with CompletableFuture

use of java.util.concurrent.CompletableFuture in project pulsar by yahoo.

the class ManagedCursorConcurrencyTest method testCloseAndRead.

@Test
public void testCloseAndRead() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger_test_close_and_read", new ManagedLedgerConfig().setMaxEntriesPerLedger(2));
    final ManagedCursor cursor = ledger.openCursor("c1");
    final CompletableFuture<String> closeFuture = new CompletableFuture<>();
    final String CLOSED = "closed";
    final List<Position> addedEntries = Lists.newArrayList();
    for (int i = 0; i < 1000; i++) {
        Position pos = ledger.addEntry("entry".getBytes());
        addedEntries.add(pos);
    }
    final CyclicBarrier barrier = new CyclicBarrier(2);
    final CountDownLatch counter = new CountDownLatch(2);
    final AtomicBoolean gotException = new AtomicBoolean(false);
    Thread deleter = new Thread() {

        public void run() {
            try {
                barrier.await();
                for (Position position : addedEntries) {
                    cursor.markDelete(position);
                    Thread.sleep(1);
                }
            } catch (ManagedLedgerException e) {
                if (!e.getMessage().equals("Cursor was already closed")) {
                    gotException.set(true);
                }
            } catch (Exception e) {
                e.printStackTrace();
                gotException.set(true);
            } finally {
                counter.countDown();
            }
        }
    };
    Thread reader = new Thread() {

        public void run() {
            try {
                barrier.await();
                for (int i = 0; i < 1000; i++) {
                    cursor.readEntries(1).forEach(e -> e.release());
                    // Thread.sleep(2,200);
                    Thread.sleep(2, 195);
                }
                cursor.asyncClose(new AsyncCallbacks.CloseCallback() {

                    @Override
                    public void closeComplete(Object ctx) {
                        log.info("Successfully closed cursor ledger");
                        closeFuture.complete(CLOSED);
                    }

                    @Override
                    public void closeFailed(ManagedLedgerException exception, Object ctx) {
                        log.error("Error closing cursor: ", exception);
                        closeFuture.completeExceptionally(new Exception(exception));
                    }
                }, null);
            } catch (Exception e) {
                e.printStackTrace();
                gotException.set(true);
            } finally {
                counter.countDown();
            }
        }
    };
    deleter.start();
    reader.start();
    counter.await();
    assertEquals(gotException.get(), false);
    assertEquals(closeFuture.get(), CLOSED);
}
Also used : Position(org.apache.bookkeeper.mledger.Position) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) CountDownLatch(java.util.concurrent.CountDownLatch) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) AsyncCallbacks(org.apache.bookkeeper.mledger.AsyncCallbacks) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) Test(org.testng.annotations.Test)

Example 78 with CompletableFuture

use of java.util.concurrent.CompletableFuture in project pulsar by yahoo.

the class ManagedLedgerFactoryImpl method asyncOpen.

@Override
public void asyncOpen(final String name, final ManagedLedgerConfig config, final OpenLedgerCallback callback, final Object ctx) {
    // If the ledger state is bad, remove it from the map.
    CompletableFuture<ManagedLedgerImpl> existingFuture = ledgers.get(name);
    if (existingFuture != null && existingFuture.isDone()) {
        try {
            ManagedLedgerImpl l = existingFuture.get();
            if (l.getState().equals(State.Fenced.toString()) || l.getState().equals(State.Closed.toString())) {
                // Managed ledger is in unusable state. Recreate it.
                log.warn("[{}] Attempted to open ledger in {} state. Removing from the map to recreate it", name, l.getState());
                ledgers.remove(name, existingFuture);
            }
        } catch (Exception e) {
            // Unable to get the future
            log.warn("[{}] Got exception while trying to retrieve ledger", name, e);
        }
    }
    // Ensure only one managed ledger is created and initialized
    ledgers.computeIfAbsent(name, (mlName) -> {
        // Create the managed ledger
        CompletableFuture<ManagedLedgerImpl> future = new CompletableFuture<>();
        final ManagedLedgerImpl newledger = new ManagedLedgerImpl(this, bookKeeper, store, config, executor, orderedExecutor, name);
        newledger.initialize(new ManagedLedgerInitializeLedgerCallback() {

            @Override
            public void initializeComplete() {
                future.complete(newledger);
            }

            @Override
            public void initializeFailed(ManagedLedgerException e) {
                // Clean the map if initialization fails
                ledgers.remove(name, future);
                future.completeExceptionally(e);
            }
        }, null);
        return future;
    }).thenAccept(ml -> {
        callback.openLedgerComplete(ml, ctx);
    }).exceptionally(exception -> {
        callback.openLedgerFailed((ManagedLedgerException) exception.getCause(), ctx);
        return null;
    });
}
Also used : ScheduledFuture(java.util.concurrent.ScheduledFuture) DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) ManagedLedgerFactory(org.apache.bookkeeper.mledger.ManagedLedgerFactory) ManagedLedgerFactoryMXBean(org.apache.bookkeeper.mledger.ManagedLedgerFactoryMXBean) State(org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl.State) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) States(org.apache.zookeeper.ZooKeeper.States) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) Map(java.util.Map) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ManagedLedgerFactoryConfig(org.apache.bookkeeper.mledger.ManagedLedgerFactoryConfig) Predicates(com.google.common.base.Predicates) ZooKeeper(org.apache.zookeeper.ZooKeeper) ManagedLedgerInitializeLedgerCallback(org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl.ManagedLedgerInitializeLedgerCallback) Logger(org.slf4j.Logger) OrderedSafeExecutor(org.apache.bookkeeper.util.OrderedSafeExecutor) Watcher(org.apache.zookeeper.Watcher) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) BookKeeper(org.apache.bookkeeper.client.BookKeeper) Maps(com.google.common.collect.Maps) Executors(java.util.concurrent.Executors) BKException(org.apache.bookkeeper.client.BKException) TimeUnit(java.util.concurrent.TimeUnit) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) CountDownLatch(java.util.concurrent.CountDownLatch) OpenLedgerCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.OpenLedgerCallback) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration) AsyncCallbacks(org.apache.bookkeeper.mledger.AsyncCallbacks) CompletableFuture(java.util.concurrent.CompletableFuture) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedLedgerInitializeLedgerCallback(org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl.ManagedLedgerInitializeLedgerCallback) BKException(org.apache.bookkeeper.client.BKException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException)

Example 79 with CompletableFuture

use of java.util.concurrent.CompletableFuture in project pulsar by yahoo.

the class Futures method waitForAll.

public static CompletableFuture<Void> waitForAll(List<CompletableFuture<Void>> futures) {
    final CompletableFuture<Void> compositeFuture = new CompletableFuture<>();
    final AtomicInteger count = new AtomicInteger(futures.size());
    for (CompletableFuture<Void> future : futures) {
        future.whenComplete((r, ex) -> {
            if (ex != null) {
                compositeFuture.completeExceptionally(ex);
            } else if (count.decrementAndGet() == 0) {
                // All the pending futures did complete
                compositeFuture.complete(null);
            }
        });
    }
    if (futures.isEmpty()) {
        compositeFuture.complete(null);
    }
    return compositeFuture;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 80 with CompletableFuture

use of java.util.concurrent.CompletableFuture in project pulsar by yahoo.

the class PersistentTopic method unsubscribe.

/**
     * Delete the cursor ledger for a given subscription
     *
     * @param subscriptionName
     *            Subscription for which the cursor ledger is to be deleted
     * @return Completable future indicating completion of unsubscribe operation Completed exceptionally with:
     *         ManagedLedgerException if cursor ledger delete fails
     */
@Override
public CompletableFuture<Void> unsubscribe(String subscriptionName) {
    CompletableFuture<Void> unsubscribeFuture = new CompletableFuture<>();
    ledger.asyncDeleteCursor(Codec.encode(subscriptionName), new DeleteCursorCallback() {

        @Override
        public void deleteCursorComplete(Object ctx) {
            if (log.isDebugEnabled()) {
                log.debug("[{}][{}] Cursor deleted successfully", topic, subscriptionName);
            }
            subscriptions.remove(subscriptionName);
            unsubscribeFuture.complete(null);
            lastActive = System.nanoTime();
        }

        @Override
        public void deleteCursorFailed(ManagedLedgerException exception, Object ctx) {
            if (log.isDebugEnabled()) {
                log.debug("[{}][{}] Error deleting cursor for subscription", topic, subscriptionName, exception);
            }
            unsubscribeFuture.completeExceptionally(new PersistenceException(exception));
        }
    }, null);
    return unsubscribeFuture;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) PersistenceException(com.yahoo.pulsar.broker.service.BrokerServiceException.PersistenceException) DeleteCursorCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.DeleteCursorCallback)

Aggregations

CompletableFuture (java.util.concurrent.CompletableFuture)490 Test (org.junit.Test)152 ArrayList (java.util.ArrayList)88 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)77 List (java.util.List)75 UUID (java.util.UUID)62 Futures (io.pravega.common.concurrent.Futures)60 Map (java.util.Map)59 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)57 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)53 HashMap (java.util.HashMap)52 TimeUnit (java.util.concurrent.TimeUnit)52 Cleanup (lombok.Cleanup)49 Exceptions (io.pravega.common.Exceptions)48 Collectors (java.util.stream.Collectors)48 lombok.val (lombok.val)47 IOException (java.io.IOException)46 Duration (java.time.Duration)46 Slf4j (lombok.extern.slf4j.Slf4j)46 AtomicReference (java.util.concurrent.atomic.AtomicReference)45