Search in sources :

Example 81 with IgniteInternalFuture

use of org.apache.ignite.internal.IgniteInternalFuture in project ignite by apache.

the class GridFutureAdapterSelfTest method testListenNotify.

/**
 * @throws Exception If failed.
 */
public void testListenNotify() throws Exception {
    GridTestKernalContext ctx = new GridTestKernalContext(log);
    ctx.setExecutorService(Executors.newFixedThreadPool(1));
    ctx.setSystemExecutorService(Executors.newFixedThreadPool(1));
    ctx.add(new PoolProcessor(ctx));
    ctx.add(new GridClosureProcessor(ctx));
    ctx.start();
    try {
        GridFutureAdapter<String> fut = new GridFutureAdapter<>();
        int lsnrCnt = 10;
        final CountDownLatch latch = new CountDownLatch(lsnrCnt);
        final Thread runThread = Thread.currentThread();
        for (int i = 0; i < lsnrCnt; i++) {
            fut.listen(new CI1<IgniteInternalFuture<String>>() {

                @Override
                public void apply(IgniteInternalFuture<String> t) {
                    assert Thread.currentThread() == runThread;
                    latch.countDown();
                }
            });
        }
        fut.onDone();
        latch.await();
        final CountDownLatch doneLatch = new CountDownLatch(1);
        fut.listen(new CI1<IgniteInternalFuture<String>>() {

            @Override
            public void apply(IgniteInternalFuture<String> t) {
                assert Thread.currentThread() == runThread;
                doneLatch.countDown();
            }
        });
        assert doneLatch.getCount() == 0;
        doneLatch.await();
    } finally {
        ctx.stop(false);
    }
}
Also used : GridTestKernalContext(org.apache.ignite.testframework.junits.GridTestKernalContext) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) GridClosureProcessor(org.apache.ignite.internal.processors.closure.GridClosureProcessor) PoolProcessor(org.apache.ignite.internal.processors.pool.PoolProcessor)

Example 82 with IgniteInternalFuture

use of org.apache.ignite.internal.IgniteInternalFuture in project ignite by apache.

the class GridFutureAdapterSelfTest method testChaining.

/**
 * Test futures chaining.
 *
 * @throws Exception In case of any exception.
 */
public void testChaining() throws Exception {
    checkChaining(null);
    ExecutorService exec = Executors.newFixedThreadPool(1);
    try {
        checkChaining(exec);
        GridFinishedFuture<Integer> fut = new GridFinishedFuture<>(1);
        IgniteInternalFuture<Object> chain = fut.chain(new CX1<IgniteInternalFuture<Integer>, Object>() {

            @Override
            public Object applyx(IgniteInternalFuture<Integer> fut) throws IgniteCheckedException {
                return fut.get() + 1;
            }
        }, exec);
        assertEquals(2, chain.get());
    } finally {
        exec.shutdown();
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) ExecutorService(java.util.concurrent.ExecutorService) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture)

Example 83 with IgniteInternalFuture

use of org.apache.ignite.internal.IgniteInternalFuture in project ignite by apache.

the class GridFutureListenPerformanceTest method main.

/**
 * @param args Args.
 * @throws InterruptedException If failed.
 */
public static void main(String[] args) throws InterruptedException {
    final LongAdder cnt = new LongAdder();
    final ConcurrentLinkedDeque<GridFutureAdapter<Object>> futs = new ConcurrentLinkedDeque<>();
    ExecutorService pool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
    Thread statThread = new Thread() {

        @SuppressWarnings("BusyWait")
        @Override
        public void run() {
            while (!done) {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException ignored) {
                    return;
                }
                System.out.println(new Date() + " Notifications per sec: " + (cnt.sumThenReset() / 5));
            }
        }
    };
    statThread.setDaemon(true);
    statThread.start();
    for (int i = 0; i < Runtime.getRuntime().availableProcessors(); i++) {
        pool.submit(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                Random rnd = new Random();
                while (!done) {
                    for (int j = 0; j < rnd.nextInt(10); j++) {
                        GridFutureAdapter<Object> fut = new GridFutureAdapter<>();
                        futs.add(fut);
                        for (int k = 1; k < rnd.nextInt(3); k++) {
                            fut.listen(new IgniteInClosure<IgniteInternalFuture<Object>>() {

                                @Override
                                public void apply(IgniteInternalFuture<Object> t) {
                                    try {
                                        t.get();
                                    } catch (IgniteCheckedException e) {
                                        e.printStackTrace();
                                    }
                                    cnt.increment();
                                }
                            });
                        }
                    }
                    GridFutureAdapter<Object> fut;
                    while ((fut = futs.poll()) != null) fut.onDone();
                }
                return null;
            }
        });
    }
    Thread.sleep(5 * 60 * 1000);
    done = true;
}
Also used : IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) Date(java.util.Date) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) ConcurrentLinkedDeque(java.util.concurrent.ConcurrentLinkedDeque) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) LongAdder(java.util.concurrent.atomic.LongAdder) Random(java.util.Random) ExecutorService(java.util.concurrent.ExecutorService) IgniteInClosure(org.apache.ignite.lang.IgniteInClosure)

Example 84 with IgniteInternalFuture

use of org.apache.ignite.internal.IgniteInternalFuture in project ignite by apache.

the class GridNioFutureSelfTest method testListenSyncNotify.

/**
 * @throws Exception If failed.
 */
public void testListenSyncNotify() throws Exception {
    GridNioFutureImpl<String> fut = new GridNioFutureImpl<>(null);
    int lsnrCnt = 10;
    final CountDownLatch latch = new CountDownLatch(lsnrCnt);
    final Thread runThread = Thread.currentThread();
    final AtomicReference<Exception> err = new AtomicReference<>();
    for (int i = 0; i < lsnrCnt; i++) {
        fut.listen(new CI1<IgniteInternalFuture<String>>() {

            @Override
            public void apply(IgniteInternalFuture<String> t) {
                if (Thread.currentThread() != runThread)
                    err.compareAndSet(null, new Exception("Wrong notification thread: " + Thread.currentThread()));
                latch.countDown();
            }
        });
    }
    fut.onDone();
    assertEquals(0, latch.getCount());
    if (err.get() != null)
        throw err.get();
    final AtomicBoolean called = new AtomicBoolean();
    err.set(null);
    fut.listen(new CI1<IgniteInternalFuture<String>>() {

        @Override
        public void apply(IgniteInternalFuture<String> t) {
            if (Thread.currentThread() != runThread)
                err.compareAndSet(null, new Exception("Wrong notification thread: " + Thread.currentThread()));
            called.set(true);
        }
    });
    assertTrue(called.get());
    if (err.get() != null)
        throw err.get();
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) GridNioFutureImpl(org.apache.ignite.internal.util.nio.GridNioFutureImpl) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteFutureCancelledCheckedException(org.apache.ignite.internal.IgniteFutureCancelledCheckedException) IgniteFutureTimeoutCheckedException(org.apache.ignite.internal.IgniteFutureTimeoutCheckedException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Example 85 with IgniteInternalFuture

use of org.apache.ignite.internal.IgniteInternalFuture in project ignite by apache.

the class ReadWriteLockMultiThreadedTest method testTryWriteLock.

/**
 * @throws Exception If failed.
 */
@SuppressWarnings({ "LockAcquiredButNotSafelyReleased" })
public void testTryWriteLock() throws Exception {
    final ReadWriteLock lock = new ReentrantReadWriteLock();
    lock.readLock().lock();
    X.println("Read lock acquired.");
    IgniteInternalFuture fut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

        @Nullable
        @Override
        public Object call() throws Exception {
            boolean res = lock.writeLock().tryLock();
            X.println("Attempting to try write lock: " + res);
            try {
                return null;
            } finally {
                if (res)
                    lock.writeLock().unlock();
            }
        }
    }, 1, "write-lock");
    Thread.sleep(2000);
    X.println("Read lock released: " + lock);
    lock.readLock().unlock();
    fut.get();
}
Also used : ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)245 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)114 Ignite (org.apache.ignite.Ignite)71 ArrayList (java.util.ArrayList)52 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)46 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)46 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)43 IgniteException (org.apache.ignite.IgniteException)33 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)29 UUID (java.util.UUID)28 IgniteCache (org.apache.ignite.IgniteCache)28 ClusterNode (org.apache.ignite.cluster.ClusterNode)28 Callable (java.util.concurrent.Callable)27 HashMap (java.util.HashMap)25 Map (java.util.Map)25 CountDownLatch (java.util.concurrent.CountDownLatch)24 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)19 CacheException (javax.cache.CacheException)16 GridFinishedFuture (org.apache.ignite.internal.util.future.GridFinishedFuture)16 GridFutureAdapter (org.apache.ignite.internal.util.future.GridFutureAdapter)16