Search in sources :

Example 41 with IgniteKernal

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

the class CacheExchangeMergeTest method checkExchanges.

/**
 * @param node Node.
 * @param vers Expected exchange versions.
 */
private void checkExchanges(Ignite node, long... vers) {
    IgniteKernal node0 = (IgniteKernal) node;
    List<AffinityTopologyVersion> expVers = new ArrayList<>();
    for (long ver : vers) expVers.add(new AffinityTopologyVersion(ver));
    List<AffinityTopologyVersion> doneVers = new ArrayList<>();
    List<GridDhtPartitionsExchangeFuture> futs = node0.context().cache().context().exchange().exchangeFutures();
    for (int i = futs.size() - 1; i >= 0; i--) {
        GridDhtPartitionsExchangeFuture fut = futs.get(i);
        if (!fut.isMerged() && fut.exchangeDone() && fut.firstEvent().type() != EVT_DISCOVERY_CUSTOM_EVT) {
            AffinityTopologyVersion resVer = fut.topologyVersion();
            Assert.assertNotNull(resVer);
            doneVers.add(resVer);
        }
    }
    assertEquals(expVers, doneVers);
    for (CacheGroupContext grpCtx : node0.context().cache().cacheGroups()) {
        for (AffinityTopologyVersion ver : grpCtx.affinity().cachedVersions()) {
            if (ver.minorTopologyVersion() > 0)
                continue;
            assertTrue("Unexpected version [ver=" + ver + ", exp=" + expVers + ']', expVers.contains(ver));
        }
    }
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) GridDhtPartitionsExchangeFuture(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsExchangeFuture) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) ArrayList(java.util.ArrayList) CacheGroupContext(org.apache.ignite.internal.processors.cache.CacheGroupContext)

Example 42 with IgniteKernal

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

the class CacheLateAffinityAssignmentTest method testRandomOperations.

/**
 * @throws Exception If failed.
 */
@Test
public void testRandomOperations() throws Exception {
    forceSrvMode = true;
    final int MAX_SRVS = 10;
    final int MAX_CLIENTS = 10;
    final int MAX_CACHES = 15;
    List<String> srvs = new ArrayList<>();
    List<String> clients = new ArrayList<>();
    int srvIdx = 0;
    int clientIdx = 0;
    int cacheIdx = 0;
    List<String> caches = new ArrayList<>();
    long seed = System.currentTimeMillis();
    Random rnd = new Random(seed);
    log.info("Random seed: " + seed);
    long topVer = 0;
    for (int i = 0; i < 100; i++) {
        int op = i == 0 ? 0 : rnd.nextInt(7);
        log.info("Iteration [iter=" + i + ", op=" + op + ']');
        switch(op) {
            case 0:
                {
                    if (srvs.size() < MAX_SRVS) {
                        srvIdx++;
                        String srvName = "server-" + srvIdx;
                        log.info("Start server: " + srvName);
                        if (rnd.nextBoolean()) {
                            cacheIdx++;
                            String cacheName = "join-cache-" + cacheIdx;
                            log.info("Cache for joining node: " + cacheName);
                            cacheClosure(rnd, caches, cacheName, srvs, srvIdx);
                        } else
                            cacheClosure(rnd, caches, DEFAULT_CACHE_NAME, srvs, srvIdx);
                        startNode(srvName, ++topVer, false);
                        srvs.add(srvName);
                    } else
                        log.info("Skip start server.");
                    break;
                }
            case 1:
                {
                    if (srvs.size() > 1) {
                        String srvName = srvs.get(rnd.nextInt(srvs.size()));
                        log.info("Stop server: " + srvName);
                        stopNode(srvName, ++topVer);
                        srvs.remove(srvName);
                    } else
                        log.info("Skip stop server.");
                    break;
                }
            case 2:
                {
                    if (clients.size() < MAX_CLIENTS) {
                        clientIdx++;
                        String clientName = "client-" + clientIdx;
                        log.info("Start client: " + clientName);
                        if (rnd.nextBoolean()) {
                            cacheIdx++;
                            String cacheName = "join-cache-" + cacheIdx;
                            log.info("Cache for joining node: " + cacheName);
                            cacheClosure(rnd, caches, cacheName, srvs, srvIdx);
                        } else
                            cacheClosure(rnd, caches, DEFAULT_CACHE_NAME, srvs, srvIdx);
                        startNode(clientName, ++topVer, true);
                        clients.add(clientName);
                    } else
                        log.info("Skip start client.");
                    break;
                }
            case 3:
                {
                    if (clients.size() > 1) {
                        String clientName = clients.get(rnd.nextInt(clients.size()));
                        log.info("Stop client: " + clientName);
                        stopNode(clientName, ++topVer);
                        clients.remove(clientName);
                    } else
                        log.info("Skip stop client.");
                    break;
                }
            case 4:
                {
                    if (!caches.isEmpty()) {
                        String cacheName = caches.get(rnd.nextInt(caches.size()));
                        Ignite node = randomNode(rnd, srvs, clients);
                        log.info("Destroy cache [cache=" + cacheName + ", node=" + node.name() + ']');
                        node.destroyCache(cacheName);
                        caches.remove(cacheName);
                    } else
                        log.info("Skip destroy cache.");
                    break;
                }
            case 5:
                {
                    if (caches.size() < MAX_CACHES) {
                        cacheIdx++;
                        String cacheName = "cache-" + cacheIdx;
                        Ignite node = randomNode(rnd, srvs, clients);
                        log.info("Create cache [cache=" + cacheName + ", node=" + node.name() + ']');
                        node.createCache(randomCacheConfiguration(rnd, cacheName, srvs, srvIdx));
                        calculateAffinity(topVer);
                        caches.add(cacheName);
                    } else
                        log.info("Skip create cache.");
                    break;
                }
            case 6:
                {
                    if (!caches.isEmpty()) {
                        for (int j = 0; j < 3; j++) {
                            String cacheName = caches.get(rnd.nextInt(caches.size()));
                            for (int k = 0; k < 3; k++) {
                                Ignite node = randomNode(rnd, srvs, clients);
                                log.info("Get/closes cache [cache=" + cacheName + ", node=" + node.name() + ']');
                                node.cache(cacheName).close();
                            }
                        }
                    } else
                        log.info("Skip get/close cache.");
                    break;
                }
            default:
                fail();
        }
        IgniteKernal node = (IgniteKernal) grid(srvs.get(0));
        checkAffinity(srvs.size() + clients.size(), node.context().cache().context().exchange().readyAffinityVersion(), false);
    }
    srvIdx++;
    String srvName = "server-" + srvIdx;
    log.info("Start server: " + srvName);
    cacheClosure(rnd, caches, DEFAULT_CACHE_NAME, srvs, srvIdx);
    startNode(srvName, ++topVer, false);
    srvs.add(srvName);
    checkAffinity(srvs.size() + clients.size(), topVer(topVer, 1), true);
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) Random(java.util.Random) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) ArrayList(java.util.ArrayList) Ignite(org.apache.ignite.Ignite) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 43 with IgniteKernal

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

the class GridCacheRebalancingSyncSelfTest method testSimpleRebalancing.

/**
 * @throws Exception If failed.
 */
@Test
public void testSimpleRebalancing() throws Exception {
    IgniteKernal ignite = (IgniteKernal) startGrid(0);
    generateData(ignite, 0, 0);
    log.info("Preloading started.");
    long start = System.currentTimeMillis();
    startGrid(1);
    awaitPartitionMapExchange(true, true, null, true);
    checkPartitionMapExchangeFinished();
    awaitPartitionMessagesAbsent();
    stopGrid(0);
    awaitPartitionMapExchange(true, true, null, true);
    checkPartitionMapExchangeFinished();
    awaitPartitionMessagesAbsent();
    startGrid(2);
    awaitPartitionMapExchange(true, true, null, true);
    checkPartitionMapExchangeFinished();
    awaitPartitionMessagesAbsent();
    stopGrid(2);
    awaitPartitionMapExchange(true, true, null, true);
    checkPartitionMapExchangeFinished();
    awaitPartitionMessagesAbsent();
    long spend = (System.currentTimeMillis() - start) / 1000;
    checkData(grid(1), 0, 0);
    log.info("Spend " + spend + " seconds to rebalance entries.");
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 44 with IgniteKernal

use of org.apache.ignite.internal.IgniteKernal 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 45 with IgniteKernal

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

the class TxOptimisticPrepareOnUnstableTopologyTest method doPrepareOnUnstableTopology.

/**
 * @param keys Keys.
 * @param testClient Test client.
 * @param isolation Isolation.
 * @param timeout Timeout.
 */
private void doPrepareOnUnstableTopology(int keys, boolean testClient, TransactionIsolation isolation, long timeout) throws Exception {
    GridCompoundFuture<Void, Object> compFut = new GridCompoundFuture<>();
    AtomicBoolean stopFlag = new AtomicBoolean();
    try {
        int clientIdx = testClient ? 1 : -1;
        try {
            for (int i = 0; i < GRID_CNT; i++) {
                IgniteEx grid;
                if (clientIdx == i)
                    grid = startClientGrid(i);
                else
                    grid = startGrid(i);
                assertEquals(clientIdx == i, grid.configuration().isClientMode().booleanValue());
                IgniteInternalFuture<Void> fut = runCacheOperationsAsync(grid, stopFlag, isolation, timeout, keys);
                compFut.add(fut);
                U.sleep(STARTUP_DELAY);
            }
        } finally {
            stopFlag.set(true);
        }
        compFut.markInitialized();
        compFut.get();
        for (int i = 0; i < GRID_CNT; i++) {
            IgniteTxManager tm = ((IgniteKernal) grid(i)).internalCache(CACHE_NAME).context().tm();
            assertEquals("txMap is not empty: " + i, 0, tm.idMapSize());
        }
    } finally {
        stopAllGrids();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IgniteKernal(org.apache.ignite.internal.IgniteKernal) IgniteEx(org.apache.ignite.internal.IgniteEx) GridCompoundFuture(org.apache.ignite.internal.util.future.GridCompoundFuture)

Aggregations

IgniteKernal (org.apache.ignite.internal.IgniteKernal)203 Ignite (org.apache.ignite.Ignite)88 Test (org.junit.Test)70 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)63 ClusterNode (org.apache.ignite.cluster.ClusterNode)38 ArrayList (java.util.ArrayList)36 Transaction (org.apache.ignite.transactions.Transaction)36 Map (java.util.Map)35 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)29 IgniteCache (org.apache.ignite.IgniteCache)29 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)28 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)28 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)27 IgniteEx (org.apache.ignite.internal.IgniteEx)27 UUID (java.util.UUID)25 IgniteException (org.apache.ignite.IgniteException)25 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)23 GridAbsPredicate (org.apache.ignite.internal.util.lang.GridAbsPredicate)20 CacheException (javax.cache.CacheException)17 HashMap (java.util.HashMap)16