Search in sources :

Example 46 with IgniteKernal

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

the class IgniteTxMultiNodeAbstractTest method nearEntry.

/**
 * @param nodeId Node ID.
 * @param key Key.
 * @return Near entry.
 */
@Nullable
private static GridCacheEntryEx nearEntry(UUID nodeId, Object key) {
    Ignite g = G.ignite(nodeId);
    GridNearCacheAdapter<Object, Integer> near = ((IgniteKernal) g).<Object, Integer>internalCache(DEFAULT_CACHE_NAME).context().near();
    return near.peekEx(key);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteKernal(org.apache.ignite.internal.IgniteKernal) Ignite(org.apache.ignite.Ignite) Nullable(org.jetbrains.annotations.Nullable)

Example 47 with IgniteKernal

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

the class GridCacheNearReadersSelfTest method testTwoNodesTwoKeysOneBackup.

/**
 * @throws Exception If failed.
 */
public void testTwoNodesTwoKeysOneBackup() throws Exception {
    aff.backups(1);
    grids = 2;
    aff.partitions(grids);
    startGrids();
    ClusterNode n1 = F.first(aff.nodes(aff.partition(1), grid(0).cluster().nodes()));
    ClusterNode n2 = F.first(aff.nodes(aff.partition(2), grid(0).cluster().nodes()));
    assertNotNull(n1);
    assertNotNull(n2);
    assertNotSame(n1, n2);
    assertFalse("Nodes cannot be equal: " + n1, n1.equals(n2));
    Ignite g1 = grid(n1.id());
    Ignite g2 = grid(n2.id());
    awaitPartitionMapExchange();
    GridCacheContext ctx = ((IgniteKernal) g1).internalCache(DEFAULT_CACHE_NAME).context();
    List<KeyCacheObject> cacheKeys = F.asList(ctx.toCacheKeyObject(1), ctx.toCacheKeyObject(2));
    IgniteInternalFuture<Object> f1 = ((IgniteKernal) g1).internalCache(DEFAULT_CACHE_NAME).preloader().request(ctx, cacheKeys, new AffinityTopologyVersion(2));
    if (f1 != null)
        f1.get();
    IgniteInternalFuture<Object> f2 = ((IgniteKernal) g2).internalCache(DEFAULT_CACHE_NAME).preloader().request(((IgniteKernal) g2).internalCache(DEFAULT_CACHE_NAME).context(), cacheKeys, new AffinityTopologyVersion(2));
    if (f2 != null)
        f2.get();
    IgniteCache<Integer, String> cache1 = g1.cache(DEFAULT_CACHE_NAME);
    IgniteCache<Integer, String> cache2 = g2.cache(DEFAULT_CACHE_NAME);
    assertEquals(g1.affinity(DEFAULT_CACHE_NAME).mapKeyToNode(1), g1.cluster().localNode());
    assertFalse(g1.affinity(DEFAULT_CACHE_NAME).mapKeyToNode(2).equals(g1.cluster().localNode()));
    assertEquals(g1.affinity(DEFAULT_CACHE_NAME).mapKeyToNode(2), g2.cluster().localNode());
    assertFalse(g2.affinity(DEFAULT_CACHE_NAME).mapKeyToNode(1).equals(g2.cluster().localNode()));
    // Store first value in cache.
    assertNull(cache1.getAndPut(1, "v1"));
    assertTrue(cache1.containsKey(1));
    assertTrue(cache2.containsKey(1));
    assertEquals("v1", nearPeek(cache1, 1));
    assertEquals("v1", nearPeek(cache2, 1));
    assertEquals("v1", dhtPeek(cache1, 1));
    assertEquals("v1", dhtPeek(cache2, 1));
    assertNull(near(cache1).peekEx(1));
    assertNull(near(cache2).peekEx(1));
    GridDhtCacheEntry e1 = (GridDhtCacheEntry) dht(cache1).entryEx(1);
    // Store second value in cache.
    assertNull(cache1.getAndPut(2, "v2"));
    assertTrue(cache1.containsKey(2));
    assertTrue(cache2.containsKey(2));
    assertEquals("v2", nearPeek(cache1, 2));
    assertEquals("v2", nearPeek(cache2, 2));
    assertEquals("v2", dhtPeek(cache1, 2));
    assertEquals("v2", dhtPeek(cache2, 2));
    assertNull(near(cache1).peekEx(2));
    assertNull(near(cache2).peekEx(2));
    GridDhtCacheEntry c2e2 = (GridDhtCacheEntry) dht(cache2).entryEx(2);
    // Nodes are backups of each other, so no readers should be added.
    assertFalse(c2e2.readers().contains(n1.id()));
    assertFalse(e1.readers().contains(n2.id()));
    // Get key1 on node2 (value should come from local DHT cache, as it has a backup).
    assertEquals("v1", cache2.get(1));
    // Since DHT cache2 has the value, Near cache2 should not have it.
    assertNull(near(cache2).peekEx(1));
    e1 = (GridDhtCacheEntry) dht(cache1).entryEx(1);
    // Since v1 was retrieved locally from cache2, cache1 should not know about it.
    assertFalse(e1.readers().contains(n2.id()));
    // Evict locally from cache2.
    // It should not be successful since it's not allowed to evict entry on backup node.
    cache2.localEvict(Collections.singleton(1));
    assertNull(near(cache2).peekEx(1));
    assertEquals("v1", dhtPeek(cache2, 1));
    assertEquals("v1", cache1.getAndPut(1, "z1"));
    e1 = (GridDhtCacheEntry) dht(cache1).entryEx(1);
    // Node 1 should not have node2 in readers map.
    assertFalse(e1.readers().contains(n2.id()));
    assertNull(near(cache2).peekEx(1));
    assertEquals("z1", dhtPeek(cache2, 1));
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteKernal(org.apache.ignite.internal.IgniteKernal) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridDhtCacheEntry(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtCacheEntry) Ignite(org.apache.ignite.Ignite) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 48 with IgniteKernal

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

the class GridCacheNearTxMultiNodeSelfTest method testTxCleanup.

/**
 * @throws Exception If failed.
 */
@SuppressWarnings({ "unchecked" })
public void testTxCleanup() throws Exception {
    backups = 1;
    Ignite ignite = startGrids(GRID_CNT);
    try {
        Integer mainKey = 0;
        ClusterNode priNode = ignite.affinity(DEFAULT_CACHE_NAME).mapKeyToNode(mainKey);
        ClusterNode backupNode = F.first(F.view(ignite.affinity(DEFAULT_CACHE_NAME).mapKeyToPrimaryAndBackups(mainKey), F.notIn(F.asList(priNode))));
        ClusterNode otherNode = F.first(ignite.cluster().forPredicate(F.notIn(F.asList(priNode, backupNode))).nodes());
        assert priNode != backupNode;
        assert backupNode != otherNode;
        assert priNode != otherNode;
        final Ignite priIgnite = grid(priNode);
        Ignite backupIgnite = grid(backupNode);
        Ignite otherIgnite = grid(otherNode);
        List<Ignite> ignites = F.asList(otherIgnite, priIgnite, backupIgnite);
        int cntr = 0;
        // Update main key from all nodes.
        for (Ignite g : ignites) g.cache(DEFAULT_CACHE_NAME).put(mainKey, ++cntr);
        info("Updated mainKey from all nodes.");
        int keyCnt = 200;
        Set<Integer> keys = new TreeSet<>();
        // Populate cache from all nodes.
        for (int i = 1; i <= keyCnt; i++) {
            keys.add(i);
            Ignite g = F.rand(ignites);
            g.cache(DEFAULT_CACHE_NAME).put(new AffinityKey<>(i, mainKey), Integer.toString(cntr++));
        }
        IgniteCache cache = priIgnite.cache(DEFAULT_CACHE_NAME);
        Transaction tx = priIgnite.transactions().txStart(PESSIMISTIC, REPEATABLE_READ);
        try {
            cache.get(mainKey);
            cache.removeAll(keys);
            cache.put(mainKey, ++cntr);
            tx.commit();
        } catch (Error | Exception e) {
            error("Transaction failed: " + tx, e);
            throw e;
        } finally {
            tx.close();
        }
        stopGrid(priIgnite.name(), true);
        stopGrid(backupIgnite.name(), true);
        Ignite newIgnite = startGrid(GRID_CNT);
        ignites = F.asList(otherIgnite, newIgnite);
        for (Ignite g : ignites) {
            GridNearCacheAdapter near = ((IgniteKernal) g).internalCache(DEFAULT_CACHE_NAME).context().near();
            GridDhtCacheAdapter dht = near.dht();
            checkTm(g, near.context().tm());
            checkTm(g, dht.context().tm());
        }
    } finally {
        stopAllGrids();
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteKernal(org.apache.ignite.internal.IgniteKernal) GridDhtCacheAdapter(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtCacheAdapter) IgniteCache(org.apache.ignite.IgniteCache) Transaction(org.apache.ignite.transactions.Transaction) TreeSet(java.util.TreeSet) Ignite(org.apache.ignite.Ignite)

Example 49 with IgniteKernal

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

the class GridCachePartitionedTopologyChangeSelfTest method testExplicitLocks.

/**
 * @throws Exception If failed.
 */
public void testExplicitLocks() throws Exception {
    try {
        startGridsMultiThreaded(2);
        IgniteKernal[] nodes = new IgniteKernal[] { (IgniteKernal) grid(0), (IgniteKernal) grid(1) };
        Collection<IgniteInternalFuture> futs = new ArrayList<>();
        final CountDownLatch startLatch = new CountDownLatch(1);
        for (final IgniteKernal node : nodes) {
            List<Integer> parts = partitions(node, PARTITION_PRIMARY);
            Map<Integer, Integer> keyMap = keysFor(node, parts);
            for (final Integer key : keyMap.values()) {
                futs.add(multithreadedAsync(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            Lock lock = node.cache(DEFAULT_CACHE_NAME).lock(key);
                            lock.lock();
                            try {
                                info(">>> Acquired explicit lock for key: " + key);
                                startLatch.await();
                                info(">>> Acquiring explicit lock for key: " + key * 10);
                                Lock lock10 = node.cache(DEFAULT_CACHE_NAME).lock(key * 10);
                                lock10.lock();
                                try {
                                    info(">>> Releasing locks [key1=" + key + ", key2=" + key * 10 + ']');
                                } finally {
                                    lock10.unlock();
                                }
                            } finally {
                                lock.unlock();
                            }
                        } catch (CacheException e) {
                            info(">>> Failed to perform lock [key=" + key + ", e=" + e + ']');
                        } catch (InterruptedException ignored) {
                            info(">>> Interrupted while waiting for start latch.");
                            Thread.currentThread().interrupt();
                        }
                    }
                }, 1));
            }
        }
        IgniteInternalFuture<?> startFut = multithreadedAsync(new Runnable() {

            @Override
            public void run() {
                try {
                    startGrid(2);
                    info(">>> Started grid2.");
                } catch (Exception e) {
                    info(">>> Failed to start grid: " + e);
                }
            }
        }, 1);
        U.sleep(5000);
        assertFalse(startFut.isDone());
        info(">>> Waiting for all locks to be released.");
        startLatch.countDown();
        for (IgniteInternalFuture fut : futs) fut.get(1000);
        startFut.get();
    } finally {
        stopAllGrids();
    }
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) CacheException(javax.cache.CacheException) ArrayList(java.util.ArrayList) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) CountDownLatch(java.util.concurrent.CountDownLatch) CacheException(javax.cache.CacheException) Lock(java.util.concurrent.locks.Lock)

Example 50 with IgniteKernal

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

the class IgniteCacheNearLockValueSelfTest method testDhtVersion.

/**
 * @throws Exception If failed.
 */
public void testDhtVersion() throws Exception {
    CacheConfiguration<Object, Object> pCfg = new CacheConfiguration<>("partitioned");
    pCfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
    try (IgniteCache<Object, Object> cache = ignite(0).getOrCreateCache(pCfg, new NearCacheConfiguration<>())) {
        cache.put("key1", "val1");
        for (int i = 0; i < 3; i++) {
            try (Transaction tx = ignite(0).transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
                cache.get("key1");
                tx.commit();
            }
            TestRecordingCommunicationSpi comm = (TestRecordingCommunicationSpi) ignite(0).configuration().getCommunicationSpi();
            Collection<GridNearLockRequest> reqs = (Collection) comm.recordedMessages(false);
            assertEquals(1, reqs.size());
            GridCacheAdapter<Object, Object> primary = ((IgniteKernal) grid(1)).internalCache("partitioned");
            GridCacheEntryEx dhtEntry = primary.peekEx(primary.context().toCacheKeyObject("key1"));
            assertNotNull(dhtEntry);
            GridNearLockRequest req = reqs.iterator().next();
            assertEquals(dhtEntry.version(), req.dhtVersion(0));
            // Check entry version in near cache after commit.
            GridCacheAdapter<Object, Object> near = ((IgniteKernal) grid(0)).internalCache("partitioned");
            GridNearCacheEntry nearEntry = (GridNearCacheEntry) near.peekEx(near.context().toCacheKeyObject("key1"));
            assertNotNull(nearEntry);
            assertEquals(dhtEntry.version(), nearEntry.dhtVersion());
        }
    }
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) GridNearLockRequest(org.apache.ignite.internal.processors.cache.distributed.near.GridNearLockRequest) TestRecordingCommunicationSpi(org.apache.ignite.internal.TestRecordingCommunicationSpi) GridNearCacheEntry(org.apache.ignite.internal.processors.cache.distributed.near.GridNearCacheEntry) Transaction(org.apache.ignite.transactions.Transaction) Collection(java.util.Collection) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Aggregations

IgniteKernal (org.apache.ignite.internal.IgniteKernal)181 Ignite (org.apache.ignite.Ignite)78 ClusterNode (org.apache.ignite.cluster.ClusterNode)35 ArrayList (java.util.ArrayList)34 Map (java.util.Map)30 Transaction (org.apache.ignite.transactions.Transaction)30 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)27 IgniteCache (org.apache.ignite.IgniteCache)26 UUID (java.util.UUID)22 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)22 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)22 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)22 IgniteException (org.apache.ignite.IgniteException)21 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)20 GridAbsPredicate (org.apache.ignite.internal.util.lang.GridAbsPredicate)19 IgniteEx (org.apache.ignite.internal.IgniteEx)15 HashMap (java.util.HashMap)14 List (java.util.List)14 NearCacheConfiguration (org.apache.ignite.configuration.NearCacheConfiguration)13 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)13