Search in sources :

Example 51 with GridFutureAdapter

use of org.apache.ignite.internal.util.future.GridFutureAdapter in project ignite by apache.

the class IgniteRoundRobinErrorAfterClientReconnectTest method testClientReconnect.

/**
 * @throws Exception If failed.
 */
@Test
public void testClientReconnect() throws Exception {
    final Ignite cli = grid(CLI_IDX);
    final GridFutureAdapter<Boolean> fut = new GridFutureAdapter<>();
    cli.events().localListen(new IgnitePredicate<Event>() {

        @Override
        public boolean apply(Event event) {
            try {
                cli.compute().apply(new IgniteClosure<String, Void>() {

                    @Override
                    public Void apply(String arg) {
                        return null;
                    }
                }, "Hello!");
                fut.onDone(true);
                return true;
            } catch (Exception e) {
                fut.onDone(e);
                return false;
            }
        }
    }, EventType.EVT_CLIENT_NODE_RECONNECTED);
    stopGrid(SRV_IDX);
    startGrid(SRV_IDX);
    assert fut.get();
}
Also used : IgniteClosure(org.apache.ignite.lang.IgniteClosure) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter) Event(org.apache.ignite.events.Event) Ignite(org.apache.ignite.Ignite) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 52 with GridFutureAdapter

use of org.apache.ignite.internal.util.future.GridFutureAdapter in project ignite by apache.

the class TxRollbackAsyncTest method testSynchronousRollback0.

/**
 * @param holdLockNode Node holding the write lock.
 * @param tryLockNode Node trying to acquire lock.
 * @param useTimeout {@code True} if need to start tx with timeout.
 * @throws Exception If failed.
 */
private void testSynchronousRollback0(Ignite holdLockNode, final Ignite tryLockNode, final boolean useTimeout) throws Exception {
    final GridFutureAdapter<Void> keyLocked = new GridFutureAdapter<>();
    CountDownLatch waitCommit = new CountDownLatch(1);
    // Used for passing tx instance to rollback thread.
    IgniteInternalFuture<?> lockFut = lockInTx(holdLockNode, keyLocked, waitCommit, 0);
    keyLocked.get();
    final int txCnt = SF.applyLB(250, 25);
    final IgniteKernal k = (IgniteKernal) tryLockNode;
    final GridCacheSharedContext<Object, Object> ctx = k.context().cache().context();
    final GridCacheContext<Object, Object> cctx = ctx.cacheContext(CU.cacheId(CACHE_NAME));
    GridFutureAdapter<Transaction> txReadyFut = new GridFutureAdapter<>();
    long seed = System.currentTimeMillis();
    Random r = new Random(seed);
    log.info("Running: node0=" + holdLockNode.cluster().localNode().consistentId() + ", node1=" + tryLockNode.cluster().localNode().consistentId() + ", useTimeout=" + useTimeout + ", seed=" + seed);
    IgniteInternalFuture<?> txFut = multithreadedAsync(new Runnable() {

        @Override
        public void run() {
            for (int i = 0; i < txCnt; i++) {
                GridNearTxLocal tx0 = ctx.tm().threadLocalTx(cctx);
                assertTrue(tx0 == null || tx0.state() == ROLLED_BACK);
                try (Transaction tx = tryLockNode.transactions().txStart(PESSIMISTIC, REPEATABLE_READ, useTimeout ? 50 : 0, 1)) {
                    txReadyFut.onDone(tx);
                    // Will block on lock request until rolled back asynchronously.
                    Object o = tryLockNode.cache(CACHE_NAME).getAndPut(0, 0);
                    // If rolled back by close, previous get will return null.
                    assertNull(o);
                } catch (Exception ignore) {
                // If rolled back by rollback, previous get will throw an exception.
                }
            }
            txReadyFut.onDone((Transaction) null);
        }
    }, 1, "tx-get-thread");
    IgniteInternalFuture<?> rollbackFut = multithreadedAsync(new Runnable() {

        @Override
        public void run() {
            Set<IgniteUuid> rolledBackVers = new HashSet<>();
            int proc = 1;
            while (true) {
                try {
                    Transaction tx = txReadyFut.get();
                    txReadyFut.reset();
                    if (tx == null)
                        break;
                    // Wait a bit to reduce chance of rolling back empty transactions.
                    doSleep(r.nextInt(15));
                    if (rolledBackVers.contains(tx.xid()))
                        fail("Rollback version is expected");
                    try {
                        if (proc % 2 == 0)
                            tx.rollback();
                        else
                            tx.close();
                    } catch (IgniteException e) {
                        log.warning("Got exception while rolling back a transaction", e);
                    }
                    rolledBackVers.add(tx.xid());
                    if (proc % 100 == 0)
                        log.info("Rolled back: " + proc);
                    proc++;
                } catch (IgniteCheckedException e) {
                    fail(e.getMessage());
                }
            }
        }
    }, 1, "tx-rollback-thread");
    rollbackFut.get();
    txFut.get();
    log.info("All transactions are rolled back: holdLockNode=" + holdLockNode + ", tryLockNode=" + tryLockNode);
    waitCommit.countDown();
    lockFut.get();
    assertEquals(0, holdLockNode.cache(CACHE_NAME).get(0));
    checkFutures();
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) Set(java.util.Set) HashSet(java.util.HashSet) GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteFutureCancelledCheckedException(org.apache.ignite.internal.IgniteFutureCancelledCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) CacheException(javax.cache.CacheException) TransactionRollbackException(org.apache.ignite.transactions.TransactionRollbackException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Transaction(org.apache.ignite.transactions.Transaction) Random(java.util.Random) IgniteException(org.apache.ignite.IgniteException) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter)

Example 53 with GridFutureAdapter

use of org.apache.ignite.internal.util.future.GridFutureAdapter in project ignite by apache.

the class TxPartitionCounterStateOnePrimaryTwoBackupsTest method testMissingUpdateBetweenMultipleCheckpoints.

/**
 * Test scenario:
 * <p>
 * 1. Assign counters in order tx0, tx1
 * <p>
 * 2. Commit tx1.
 * <p>
 * 3. Delay tx0 commit on backup1.
 * <p>
 * 4. Put more keys in partition, trigger checkpoint, put more keys.
 * <p>
 * 5. Commit delayed tx closing gap.
 * <p>
 * 5. Restart backup1 without triggering checkpoint on stop.
 * <p>
 *
 * Pass condition: backup1 after restart has sequential update counter. No rebalance is expected.
 * The test states necessity of storing gaps between checkpoints.
 *
 * @throws Exception If failed.
 */
@Test
public void testMissingUpdateBetweenMultipleCheckpoints() throws Exception {
    int[] sizes = new int[] { 3, 7 };
    int[] assignOrder = new int[] { 0, 1 };
    int delayBackupIdx = 0;
    GridFutureAdapter<T2<Ignite, GridFutureAdapter>> fut = new GridFutureAdapter<>();
    GridTestUtils.runAsync(new Runnable() {

        @Override
        public void run() {
            try {
                T2<Ignite, GridFutureAdapter> pair = fut.get(30, TimeUnit.SECONDS);
                IgniteEx client = grid(CLIENT_GRID_NAME);
                // Allow txs to work as usual.
                for (Ignite node : G.allGrids()) TestRecordingCommunicationSpi.spi(node).stopBlock(false, null, true, false);
                List<Integer> keys = partitionKeys(client.cache(DEFAULT_CACHE_NAME), PARTITION_ID, 10, sizes[0] + sizes[1] + PRELOAD_KEYS_CNT);
                for (Integer key : keys) client.cache(DEFAULT_CACHE_NAME).put(key, key);
                Ignite backup1 = pair.get1();
                forceCheckpoint(backup1);
                // Commit delayed tx.
                pair.get2().onDone();
            } catch (IgniteCheckedException e) {
                fail(X.getFullStackTrace(e));
            }
        }
    });
    Map<Integer, T2<Ignite, List<Ignite>>> txTops = runOnPartition(PARTITION_ID, null, BACKUPS, SERVERS_CNT, map -> {
        Ignite primary = map.get(PARTITION_ID).get1();
        Ignite backup1 = map.get(PARTITION_ID).get2().get(delayBackupIdx);
        return new TwoPhaseCommitTxCallbackAdapter(U.map((IgniteEx) primary, assignOrder), new HashMap<>(), new HashMap<>(), sizes.length) {

            @Override
            public boolean beforeBackupFinish(IgniteEx primary, IgniteEx backup, @Nullable IgniteInternalTx primaryTx, IgniteInternalTx backupTx, IgniteUuid nearXidVer, GridFutureAdapter<?> proceedFut) {
                if (order(nearXidVer) == assignOrder[0] && backup == backup1) {
                    fut.onDone(new T2<>(backup1, proceedFut));
                    // Delay commit on backup.
                    return true;
                }
                return super.beforeBackupFinish(primary, backup, primaryTx, backupTx, nearXidVer, proceedFut);
            }
        };
    }, sizes);
    // At this point all txs are committed and no gaps are expected.
    Ignite backup1 = txTops.get(PARTITION_ID).get2().get(delayBackupIdx);
    PartitionUpdateCounter cntr;
    assertNotNull(cntr = counter(PARTITION_ID, backup1.name()));
    assertTrue(cntr.sequential());
    stopGrid(true, backup1.name());
    startGrid(backup1.name());
    awaitPartitionMapExchange();
    assertNotNull(cntr = counter(PARTITION_ID, backup1.name()));
    assertTrue(cntr.sequential());
    assertPartitionsSame(idleVerify(grid(CLIENT_GRID_NAME), DEFAULT_CACHE_NAME));
    assertCountersSame(PARTITION_ID, true, DEFAULT_CACHE_NAME);
}
Also used : PartitionUpdateCounter(org.apache.ignite.internal.processors.cache.PartitionUpdateCounter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteUuid(org.apache.ignite.lang.IgniteUuid) IgniteEx(org.apache.ignite.internal.IgniteEx) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter) Ignite(org.apache.ignite.Ignite) List(java.util.List) T2(org.apache.ignite.internal.util.typedef.T2) Nullable(org.jetbrains.annotations.Nullable) Test(org.junit.Test)

Example 54 with GridFutureAdapter

use of org.apache.ignite.internal.util.future.GridFutureAdapter in project ignite by apache.

the class TxPartitionCounterStateOnePrimaryTwoBackupsTest method doTestPartialCommit_3tx_2.

/**
 * Test scenario:
 * <p>
 * 1. Assign counters in specified order.
 * <p>
 * 2. Prepare all three txs on backups.
 * <p>
 * 3. Fail primary to trigger recovery. Some tx will be committed, some will be rolled back.
 * <p>
 * Pass condition: after primary joined partitions are consistent, all transactions are committed.
 *
 * @param skipCheckpointOnStop Skip checkpoint on node stop.
 * @throws Exception If failed.
 */
private void doTestPartialCommit_3tx_2(boolean skipCheckpointOnStop) throws Exception {
    Map<Integer, T2<Ignite, List<Ignite>>> txTops = runOnPartition(PARTITION_ID, null, BACKUPS, SERVERS_CNT, map -> {
        Ignite primary = map.get(PARTITION_ID).get1();
        final Ignite backup1 = map.get(PARTITION_ID).get2().get(0);
        final Ignite backup2 = map.get(PARTITION_ID).get2().get(1);
        return new TwoPhaseCommitTxCallbackAdapter(U.map((IgniteEx) primary, new int[] { 0, 1, 2 }), U.map((IgniteEx) primary, new int[] { 0, 1, 2 }, (IgniteEx) backup1, new int[] { 0, 1, 2 }, (IgniteEx) backup2, new int[] { 0, 1, 2 }), new HashMap<>(), SIZES.length) {

            @Override
            public boolean beforePrimaryFinish(IgniteEx primary, IgniteInternalTx tx, GridFutureAdapter<?> proceedFut) {
                runAsync(() -> {
                    stopGrid(skipCheckpointOnStop, primary.name());
                    TestRecordingCommunicationSpi.stopBlockAll();
                });
                // Stop primary before any tx committed.
                return true;
            }
        };
    }, SIZES);
    waitForTopology(SERVERS_CNT);
    awaitPartitionMapExchange();
    IgniteEx client = grid(CLIENT_GRID_NAME);
    assertPartitionsSame(idleVerify(client, DEFAULT_CACHE_NAME));
    assertCountersSame(PARTITION_ID, true, DEFAULT_CACHE_NAME);
    startGrid(txTops.get(PARTITION_ID).get1().name());
    awaitPartitionMapExchange();
    // TODO assert with expected lwm value.
    assertCountersSame(PARTITION_ID, true, DEFAULT_CACHE_NAME);
    assertPartitionsSame(idleVerify(client, DEFAULT_CACHE_NAME));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteEx(org.apache.ignite.internal.IgniteEx) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter) Ignite(org.apache.ignite.Ignite) T2(org.apache.ignite.internal.util.typedef.T2)

Example 55 with GridFutureAdapter

use of org.apache.ignite.internal.util.future.GridFutureAdapter 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

GridFutureAdapter (org.apache.ignite.internal.util.future.GridFutureAdapter)110 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)59 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)34 IgniteException (org.apache.ignite.IgniteException)23 ArrayList (java.util.ArrayList)21 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)21 List (java.util.List)20 UUID (java.util.UUID)20 ClusterNode (org.apache.ignite.cluster.ClusterNode)20 IgniteEx (org.apache.ignite.internal.IgniteEx)20 HashMap (java.util.HashMap)19 Map (java.util.Map)19 IgniteInterruptedCheckedException (org.apache.ignite.internal.IgniteInterruptedCheckedException)18 Test (org.junit.Test)18 Nullable (org.jetbrains.annotations.Nullable)17 Ignite (org.apache.ignite.Ignite)15 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)15 HashSet (java.util.HashSet)14 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)14 T2 (org.apache.ignite.internal.util.typedef.T2)13