Java Code Examples for org.apache.ignite.IgniteCache#unwrap()

The following examples show how to use org.apache.ignite.IgniteCache#unwrap() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GridCommonAbstractTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @param cache Cache.
 * @param nodes Nodes.
 */
private List<List<ClusterNode>> calcAffinity(IgniteCache<?, ?> cache, List<ClusterNode> nodes) {
    IgniteCacheProxyImpl proxy = cache.unwrap(IgniteCacheProxyImpl.class);

    GridCacheContext<?, ?> cctx = proxy.context();

    AffinityFunction func = cctx.config().getAffinity();

    AffinityFunctionContext ctx = new GridAffinityFunctionContextImpl(
        nodes,
        null,
        null,
        AffinityTopologyVersion.NONE,
        cctx.config().getBackups());

    return func.assignPartitions(ctx);
}
 
Example 2
Source File: GridCacheDhtPreloadStartStopSelfTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @param c Cache.
 * @param cnt Key count.
 */
private void checkKeys(IgniteCache<Integer, String> c, int cnt) {
    Affinity<Integer> aff = affinity(c);

    boolean sync = isSync(c);

    Ignite ignite = c.unwrap(Ignite.class);

    for (int i = 0; i < cnt; i++) {
        if (aff.mapPartitionToPrimaryAndBackups(aff.partition(i)).contains(ignite.cluster().localNode())) {
            String val = sync ? c.localPeek(i, CachePeekMode.ONHEAP) : c.get(i);

            assertEquals("Key check failed [igniteInstanceName=" + ignite.name() + ", cache=" + c.getName() +
                    ", key=" + i + ']', Integer.toString(i), val);
        }
    }
}
 
Example 3
Source File: CacheMvccBasicContinuousQueryTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @param primaryCache Cache.
 * @param size Number of keys.
 * @return Keys belong to a given part.
 * @throws Exception If failed.
 */
private List<Integer> singlePartKeys(IgniteCache<Object, Object> primaryCache, int size, int part) throws Exception {
    Ignite ignite = primaryCache.unwrap(Ignite.class);

    List<Integer> res = new ArrayList<>();

    final Affinity<Object> aff = ignite.affinity(primaryCache.getName());

    final ClusterNode node = ignite.cluster().localNode();

    assertTrue(GridTestUtils.waitForCondition(new GridAbsPredicate() {
        @Override public boolean apply() {
            return aff.primaryPartitions(node).length > 0;
        }
    }, 5000));

    int cnt = 0;

    for (int key = 0; key < aff.partitions() * size * 10; key++) {
        if (aff.partition(key) == part) {
            res.add(key);

            if (++cnt == size)
                break;
        }
    }

    assertEquals(size, res.size());

    return res;
}
 
Example 4
Source File: IgniteCrossCachesJoinsQueryTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @param cache Cache used to run query.
 * @param caches Cache names.
 * @return {@code True} if skip query execution.
 */
private boolean skipQuery(IgniteCache cache, String... caches) {
    // Skip join replicated/partitioned caches executed on replicated cache.
    Ignite node = (Ignite)cache.unwrap(Ignite.class);

    if (!distributedJoins() && replicated(cache)) {
        for (String cacheName : caches) {
            if (!replicated(node.cache(cacheName)))
                return true;
        }
    }

    return false;
}
 
Example 5
Source File: GridAbstractTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * Calls job on local JVM or on remote JVM in multi-JVM case.
 *
 * @param cache Cache.
 * @param job Job.
 */
public static <K, V, R> R executeOnLocalOrRemoteJvm(IgniteCache<K, V> cache, TestCacheCallable<K, V, R> job) {
    Ignite ignite = cache.unwrap(Ignite.class);

    if (!isMultiJvmObject(ignite))
        try {
            return job.call(ignite, cache);
        }
        catch (Exception e) {
            throw new IgniteException(e);
        }
    else
        return executeRemotely((IgniteCacheProcessProxy<K, V>)cache, job);
}
 
Example 6
Source File: GridCommonAbstractTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @param cache Cache.
 * @param part Partition.
 * @param cnt Count.
 * @param skipCnt Skip keys from start.
 * @return List of keys for partition.
 */
protected List<Integer> partitionKeys(IgniteCache<?, ?> cache, int part, int cnt, int skipCnt) {
    IgniteCacheProxyImpl proxy = cache.unwrap(IgniteCacheProxyImpl.class);

    GridCacheContext<?, ?> cctx = proxy.context();

    int k = 0, c = 0, skip0 = 0;

    List<Integer> keys = new ArrayList<>(cnt);

    while (c < cnt) {
        if (cctx.affinity().partition(k) == part) {
            if (skip0 < skipCnt) {
                k++;
                skip0++;

                continue;
            }

            c++;

            keys.add(k);
        }

        k++;
    }

    return keys;
}
 
Example 7
Source File: GridCommonAbstractTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @param cache Cache.
 * @param part Partition.
 * @return Unbounded iterator for partition keys.
 */
protected Iterator<Integer> partitionKeysIterator(IgniteCache<?, ?> cache, int part) {
    IgniteCacheProxyImpl proxy = cache.unwrap(IgniteCacheProxyImpl.class);

    GridCacheContext<?, ?> cctx = proxy.context();

    return new Iterator<Integer>() {
        int cur, next = 0;

        {
            advance();
        }

        private void advance() {
            while (cctx.affinity().partition(cur = next++) != part);
        }

        @Override public boolean hasNext() {
            return true;
        }

        @Override public Integer next() {
            int tmp = cur;

            advance();

            return tmp;
        }
    };
}
 
Example 8
Source File: GridCacheAbstractNodeRestartSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @param c Cache projection.
 * @param key Key.
 * @param attempt Attempt.
 */
private void printFailureDetails(IgniteCache<Integer, String> c, int key, int attempt) {
    Ignite ignite = c.unwrap(Ignite.class);

    error("*** Failure details ***");
    error("Key: " + key);
    error("Partition: " + ignite.affinity(c.getName()).partition(key));
    error("Attempt: " + attempt);
    error("Node: " + ignite.cluster().localNode().id());
}
 
Example 9
Source File: GridCacheDhtPreloadMessageCountTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * Checks if keys are present.
 *
 * @param c Cache.
 * @param keyCnt Key count.
 */
private void checkCache(IgniteCache<String, Integer> c, int keyCnt) {
    Ignite g = c.unwrap(Ignite.class);

    for (int i = 0; i < keyCnt; i++) {
        String key = Integer.toString(i);

        if (affinity(c).isPrimaryOrBackup(g.cluster().localNode(), key))
            assertEquals(Integer.valueOf(i), c.localPeek(key, CachePeekMode.ONHEAP));
    }
}
 
Example 10
Source File: GridCacheDhtPreloadDelayedSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * Checks if keys are present.
 *
 * @param c Cache.
 * @param keyCnt Key count.
 */
private void checkCache(IgniteCache<String, Integer> c, int keyCnt) {
    Ignite g = c.unwrap(Ignite.class);

    for (int i = 0; i < keyCnt; i++) {
        String key = Integer.toString(i);

        if (affinity(c).isPrimaryOrBackup(g.cluster().localNode(), key))
            assertEquals(Integer.valueOf(i), c.localPeek(key));
    }
}
 
Example 11
Source File: CacheStoreUsageMultinodeAbstractTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @param cache Cache.
 * @param key Key.
 * @param tc Transaction concurrency mode.
 * @throws Exception If failed.
 */
protected void testStoreUpdate(IgniteCache<Object, Object> cache,
   Object key,
   @Nullable TransactionConcurrency tc)
    throws Exception
{
    boolean storeOnPrimary = atomicityMode() == ATOMIC || locStore || writeBehind;

    assertTrue(writeMap.isEmpty());

    Ignite ignite = cache.unwrap(Ignite.class);

    Affinity<Object> obj = ignite.affinity(cache.getName());

    ClusterNode node = obj.mapKeyToNode(key);

    assertNotNull(node);

    String expNode = storeOnPrimary ? (String)node.attribute(ATTR_IGNITE_INSTANCE_NAME) : ignite.name();

    assertNotNull(expNode);

    log.info("Put [node=" + ignite.name() +
        ", key=" + key +
        ", primary=" + node.attribute(ATTR_IGNITE_INSTANCE_NAME) +
        ", tx=" + tc +
        ", nearCache=" + (cache.getConfiguration(CacheConfiguration.class).getNearConfiguration() != null) +
        ", storeOnPrimary=" + storeOnPrimary + ']');

    Transaction tx = tc != null ? ignite.transactions().txStart(tc, REPEATABLE_READ) : null;

    try {
        cache.put(key, key);

        if (tx != null)
            tx.commit();
    }
    finally {
        if (tx != null)
            tx.close();
    }

    boolean wait = GridTestUtils.waitForCondition(new GridAbsPredicate() {
        @Override public boolean apply() {
            return !writeMap.isEmpty();
        }
    }, 1000);

    assertTrue("Store is not updated", wait);

    assertEquals("Write on wrong node: " + writeMap, locStore ? 2 : 1, writeMap.size());

    if (!locStore)
        assertEquals(expNode, writeMap.keySet().iterator().next());

    writeMap.clear();
}
 
Example 12
Source File: IgniteCacheEntryProcessorCallTest.java    From ignite with Apache License 2.0 2 votes vote down vote up
/**
 * @param key Key.
 * @param cache Cache.
 * @param concurrency Transaction concurrency.
 * @param isolation Transaction isolation.
 * @param expCallCnt Expected entry processor calls count.
 */
private void checkEntryProcessCall(Integer key,
    IgniteCache<Integer, TestValue> cache,
    @Nullable TransactionConcurrency concurrency,
    @Nullable TransactionIsolation isolation,
    int expCallCnt) {
    Ignite ignite = cache.unwrap(Ignite.class);

    ClusterNode primary = ignite.affinity(cache.getName()).mapKeyToNode(key);

    assertNotNull(primary);

    log.info("Check call [key=" + key +
        ", primary=" + primary.attribute(ATTR_IGNITE_INSTANCE_NAME) +
        ", concurrency=" + concurrency +
        ", isolation=" + isolation + "]");

    int expCallCntOnGet = cache.getConfiguration(CacheConfiguration.class).getAtomicityMode() == TRANSACTIONAL_SNAPSHOT ?
        1 : expCallCnt;

    Transaction tx;
    TestReturnValue retVal;

    log.info("Invoke: " + key);

    // Update.
    callCnt.set(0);

    tx = startTx(cache, concurrency, isolation);

    retVal = cache.invoke(key, new TestEntryProcessor(OP_UPDATE), new TestValue(Integer.MIN_VALUE));

    if (tx != null)
        tx.commit();

    assertEquals(expCallCnt, callCnt.get());

    checkReturnValue(retVal, "null");
    checkCacheValue(cache.getName(), key, new TestValue(0));

    log.info("Invoke: " + key);

    // Get.
    callCnt.set(0);

    tx = startTx(cache, concurrency, isolation);

    retVal = cache.invoke(key, new TestEntryProcessor(OP_GET), new TestValue(Integer.MIN_VALUE));

    if (tx != null)
        tx.commit();

    assertEquals(expCallCntOnGet, callCnt.get());

    checkReturnValue(retVal, "0");
    checkCacheValue(cache.getName(), key, new TestValue(0));

    log.info("Invoke: " + key);

    // Update.
    callCnt.set(0);

    tx = startTx(cache, concurrency, isolation);

    retVal = cache.invoke(key, new TestEntryProcessor(OP_UPDATE), new TestValue(Integer.MIN_VALUE));

    if (tx != null)
        tx.commit();

    assertEquals(expCallCnt, callCnt.get());

    checkReturnValue(retVal, "0");
    checkCacheValue(cache.getName(), key, new TestValue(1));

    log.info("Invoke: " + key);

    // Remove.
    callCnt.set(0);

    tx = startTx(cache, concurrency, isolation);

    retVal = cache.invoke(key, new TestEntryProcessor(OP_REMOVE), new TestValue(Integer.MIN_VALUE));

    if (tx != null)
        tx.commit();

    assertEquals(expCallCnt, callCnt.get());

    checkReturnValue(retVal, "1");
    checkCacheValue(cache.getName(), key, null);
}