Search in sources :

Example 71 with IgniteInternalFuture

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

the class DataStreamerImpl method waitAffinityAndRun.

/**
 * @param c Closure to run.
 * @param topVer Topology version to wait for.
 * @param async Async flag.
 */
private void waitAffinityAndRun(final Runnable c, long topVer, boolean async) {
    AffinityTopologyVersion topVer0 = new AffinityTopologyVersion(topVer, 0);
    IgniteInternalFuture<?> fut = ctx.cache().context().exchange().affinityReadyFuture(topVer0);
    if (fut != null && !fut.isDone()) {
        fut.listen(new CI1<IgniteInternalFuture<?>>() {

            @Override
            public void apply(IgniteInternalFuture<?> fut) {
                ctx.closure().runLocalSafe(c, true);
            }
        });
    } else {
        if (async)
            ctx.closure().runLocalSafe(c, true);
        else
            c.run();
    }
}
Also used : AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture)

Example 72 with IgniteInternalFuture

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

the class GridNearOptimisticTxPrepareFuture method proceedPrepare.

/**
 * Continues prepare after previous mapping successfully finished.
 *
 * @param m Mapping.
 * @param mappings Queue of mappings.
 */
private void proceedPrepare(GridDistributedTxMapping m, @Nullable final Queue<GridDistributedTxMapping> mappings) {
    if (isDone())
        return;
    boolean set = cctx.tm().setTxTopologyHint(tx.topologyVersionSnapshot());
    try {
        assert !m.empty();
        final ClusterNode n = m.primary();
        long timeout = tx.remainingTime();
        if (timeout != -1) {
            GridNearTxPrepareRequest req = new GridNearTxPrepareRequest(futId, tx.topologyVersion(), tx, timeout, null, m.writes(), m.hasNearCacheEntries(), txMapping.transactionNodes(), m.last(), tx.onePhaseCommit(), tx.needReturnValue() && tx.implicit(), tx.implicitSingle(), m.explicitLock(), tx.subjectId(), tx.taskNameHash(), m.clientFirst(), true, tx.activeCachesDeploymentEnabled());
            for (IgniteTxEntry txEntry : m.entries()) {
                if (txEntry.op() == TRANSFORM)
                    req.addDhtVersion(txEntry.txKey(), null);
            }
            // Must lock near entries separately.
            if (m.hasNearCacheEntries()) {
                try {
                    cctx.tm().prepareTx(tx, m.nearCacheEntries());
                } catch (IgniteCheckedException e) {
                    onError(e, false);
                    return;
                }
            }
            final MiniFuture fut = new MiniFuture(this, m, ++miniId, mappings);
            req.miniId(fut.futureId());
            // Append new future.
            add(fut);
            if (n.isLocal()) {
                assert !(m.hasColocatedCacheEntries() && m.hasNearCacheEntries()) : m;
                IgniteInternalFuture<GridNearTxPrepareResponse> prepFut = m.hasNearCacheEntries() ? cctx.tm().txHandler().prepareNearTxLocal(req) : cctx.tm().txHandler().prepareColocatedTx(tx, req);
                prepFut.listen(new CI1<IgniteInternalFuture<GridNearTxPrepareResponse>>() {

                    @Override
                    public void apply(IgniteInternalFuture<GridNearTxPrepareResponse> prepFut) {
                        try {
                            fut.onResult(prepFut.get());
                        } catch (IgniteCheckedException e) {
                            fut.onResult(e);
                        }
                    }
                });
            } else {
                try {
                    cctx.io().send(n, req, tx.ioPolicy());
                    if (msgLog.isDebugEnabled()) {
                        msgLog.debug("Near optimistic prepare fut, sent request [txId=" + tx.nearXidVersion() + ", node=" + n.id() + ']');
                    }
                } catch (ClusterTopologyCheckedException e) {
                    e.retryReadyFuture(cctx.nextAffinityReadyFuture(tx.topologyVersion()));
                    fut.onNodeLeft(e, false);
                } catch (IgniteCheckedException e) {
                    if (msgLog.isDebugEnabled()) {
                        msgLog.debug("Near optimistic prepare fut, failed to sent request [txId=" + tx.nearXidVersion() + ", node=" + n.id() + ", err=" + e + ']');
                    }
                    fut.onResult(e);
                }
            }
        } else
            onTimeout();
    } finally {
        if (set)
            cctx.tm().setTxTopologyHint(null);
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteTxEntry(org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Example 73 with IgniteInternalFuture

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

the class CacheExchangeMergeTest method mergeServersAndClientsFail.

/**
 * @param waitRebalance Wait for rebalance end before start tested topology change.
 * @throws Exception If failed.
 */
private void mergeServersAndClientsFail(boolean waitRebalance) throws Exception {
    clientC = new IgniteClosure<String, Boolean>() {

        @Override
        public Boolean apply(String nodeName) {
            return nodeName.equals(getTestIgniteInstanceName(2)) || nodeName.equals(getTestIgniteInstanceName(3));
        }
    };
    final Ignite srv0 = startGrids(6);
    if (waitRebalance)
        awaitPartitionMapExchange();
    mergeExchangeWaitVersion(srv0, 10);
    stopGrid(getTestIgniteInstanceName(1), true, false);
    stopGrid(getTestIgniteInstanceName(2), true, false);
    stopGrid(getTestIgniteInstanceName(3), true, false);
    stopGrid(getTestIgniteInstanceName(4), true, false);
    checkAffinity();
    mergeExchangeWaitVersion(srv0, 12);
    IgniteInternalFuture fut = startGridsAsync(srv0, 6, 2);
    fut.get();
    checkCaches();
}
Also used : Ignite(org.apache.ignite.Ignite) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture)

Example 74 with IgniteInternalFuture

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

the class CacheGetFutureHangsSelfTest method doTestFailover.

/**
 * Executes one test iteration.
 *
 * @throws Exception If failed.
 */
private void doTestFailover() throws Exception {
    try {
        done = false;
        nodes = new AtomicReferenceArray<>(GRID_CNT);
        startGridsMultiThreaded(GRID_CNT, false);
        for (int i = 0; i < GRID_CNT; i++) assertTrue(nodes.compareAndSet(i, null, ignite(i)));
        List<IgniteInternalFuture> futs = new ArrayList<>();
        for (int i = 0; i < GRID_CNT + 1; i++) {
            futs.add(multithreadedAsync(new Runnable() {

                @Override
                public void run() {
                    T2<Ignite, Integer> ignite;
                    Set<Integer> keys = F.asSet(1, 2, 3, 4, 5);
                    while ((ignite = randomNode()) != null) {
                        IgniteCache<Object, Object> cache = ignite.get1().cache(DEFAULT_CACHE_NAME);
                        for (int i = 0; i < 100; i++) cache.containsKey(ThreadLocalRandom.current().nextInt(100_000));
                        cache.containsKeys(keys);
                        assertTrue(nodes.compareAndSet(ignite.get2(), null, ignite.get1()));
                        try {
                            Thread.sleep(ThreadLocalRandom.current().nextLong(50));
                        } catch (InterruptedException ignored) {
                            Thread.currentThread().interrupt();
                        }
                    }
                }
            }, 1, "containsKey-thread-" + i));
            futs.add(multithreadedAsync(new Runnable() {

                @Override
                public void run() {
                    T2<Ignite, Integer> ignite;
                    while ((ignite = randomNode()) != null) {
                        IgniteCache<Object, Object> cache = ignite.get1().cache(DEFAULT_CACHE_NAME);
                        for (int i = 0; i < 100; i++) cache.put(ThreadLocalRandom.current().nextInt(100_000), UUID.randomUUID());
                        assertTrue(nodes.compareAndSet(ignite.get2(), null, ignite.get1()));
                        try {
                            Thread.sleep(ThreadLocalRandom.current().nextLong(50));
                        } catch (InterruptedException ignored) {
                            Thread.currentThread().interrupt();
                        }
                    }
                }
            }, 1, "put-thread-" + i));
        }
        try {
            int aliveGrids = GRID_CNT;
            while (aliveGrids > 0) {
                T2<Ignite, Integer> ignite = randomNode();
                assert ignite != null;
                Ignite ignite0 = ignite.get1();
                log.info("Stop node: " + ignite0.name());
                ignite0.close();
                log.info("Node stop finished: " + ignite0.name());
                aliveGrids--;
            }
        } finally {
            done = true;
        }
        for (IgniteInternalFuture fut : futs) fut.get();
    } finally {
        done = true;
    }
}
Also used : ArrayList(java.util.ArrayList) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) Ignite(org.apache.ignite.Ignite)

Example 75 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;
    pool.shutdownNow();
    pool.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
}
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) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter) ExecutorService(java.util.concurrent.ExecutorService)

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