Search in sources :

Example 31 with CollectionConfiguration

use of org.apache.ignite.configuration.CollectionConfiguration in project ignite by apache.

the class GridCacheSetAbstractSelfTest method testIsolation.

/**
     * Implementation of ignite data structures internally uses special system caches, need make sure
     * that transaction on these system caches do not intersect with transactions started by user.
     *
     * @throws Exception If failed.
     */
public void testIsolation() throws Exception {
    CollectionConfiguration colCfg = collectionConfiguration();
    Ignite ignite = grid(0);
    CacheConfiguration cfg = new CacheConfiguration(DEFAULT_CACHE_NAME);
    cfg.setName("myCache");
    cfg.setAtomicityMode(TRANSACTIONAL);
    cfg.setWriteSynchronizationMode(FULL_SYNC);
    IgniteCache<Integer, Integer> cache = ignite.getOrCreateCache(cfg);
    try {
        IgniteSet<Integer> set0 = ignite.set(SET_NAME, colCfg);
        assertNotNull(set0);
        try (Transaction tx = ignite.transactions().txStart()) {
            cache.put(1, 1);
            Collection<Integer> items = new ArrayList<>(100);
            for (int i = 0; i < 100; i++) items.add(i);
            set0.addAll(items);
            tx.rollback();
        }
        assertEquals(0, cache.size());
        assertEquals(100, set0.size());
        set0.close();
    } finally {
        ignite.destroyCache(cfg.getName());
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) ArrayList(java.util.ArrayList) Ignite(org.apache.ignite.Ignite) CollectionConfiguration(org.apache.ignite.configuration.CollectionConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 32 with CollectionConfiguration

use of org.apache.ignite.configuration.CollectionConfiguration in project ignite by apache.

the class GridCacheSetAbstractSelfTest method testAffinityCall.

/**
     * @throws Exception If failed.
     */
public void testAffinityCall() throws Exception {
    final CollectionConfiguration colCfg = collectionConfiguration();
    colCfg.setCollocated(false);
    colCfg.setCacheMode(CacheMode.PARTITIONED);
    try (final IgniteSet<Integer> set1 = grid(0).set("Set1", colCfg)) {
        GridTestUtils.assertThrows(log, new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                set1.affinityCall(new IgniteCallable<Object>() {

                    @Override
                    public Object call() {
                        return null;
                    }
                });
                return null;
            }
        }, IgniteException.class, "Failed to execute affinityCall() for non-collocated set: " + set1.name() + ". This operation is supported only for collocated sets.");
    }
    colCfg.setCollocated(true);
    try (final IgniteSet<Integer> set2 = grid(0).set("Set2", colCfg)) {
        set2.add(100);
        Integer res = set2.affinityCall(new IgniteCallable<Integer>() {

            @IgniteInstanceResource
            private IgniteEx ignite;

            @Override
            public Integer call() {
                assertTrue(ignite.cachex("datastructures_0").affinity().isPrimaryOrBackup(ignite.cluster().localNode(), "Set2"));
                return set2.iterator().next();
            }
        });
        assertEquals(100, res.intValue());
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) IgniteCallable(org.apache.ignite.lang.IgniteCallable) IgniteEx(org.apache.ignite.internal.IgniteEx) CollectionConfiguration(org.apache.ignite.configuration.CollectionConfiguration) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException)

Example 33 with CollectionConfiguration

use of org.apache.ignite.configuration.CollectionConfiguration in project ignite by apache.

the class GridCacheSetAbstractSelfTest method testMultithreaded.

/**
     * @param collocated Collocation flag.
     * @throws Exception If failed.
     */
private void testMultithreaded(final boolean collocated) throws Exception {
    CollectionConfiguration colCfg = config(collocated);
    Set<Integer> set0 = grid(0).set(SET_NAME, colCfg);
    assertNotNull(set0);
    Collection<IgniteInternalFuture> futs = new ArrayList<>();
    final int THREADS_PER_NODE = 5;
    final int KEY_RANGE = 10_000;
    final int ITERATIONS = 3000;
    for (int i = 0; i < gridCount(); i++) {
        final int idx = i;
        futs.add(GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                IgniteSet<Integer> set = grid(idx).set(SET_NAME, null);
                assertNotNull(set);
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                for (int i = 0; i < ITERATIONS; i++) {
                    switch(rnd.nextInt(4)) {
                        case 0:
                            set.add(rnd.nextInt(KEY_RANGE));
                            break;
                        case 1:
                            set.remove(rnd.nextInt(KEY_RANGE));
                            break;
                        case 2:
                            set.contains(rnd.nextInt(KEY_RANGE));
                            break;
                        case 3:
                            for (Integer val : set) assertNotNull(val);
                            break;
                        default:
                            fail();
                    }
                    if ((i + 1) % 500 == 0)
                        log.info("Executed iterations: " + (i + 1));
                }
                return null;
            }
        }, THREADS_PER_NODE, "testSetMultithreaded"));
    }
    for (IgniteInternalFuture fut : futs) fut.get();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) CollectionConfiguration(org.apache.ignite.configuration.CollectionConfiguration) Callable(java.util.concurrent.Callable) IgniteCallable(org.apache.ignite.lang.IgniteCallable)

Example 34 with CollectionConfiguration

use of org.apache.ignite.configuration.CollectionConfiguration in project ignite by apache.

the class GridCacheSetAbstractSelfTest method testApi.

/**
     * @param collocated Collocation flag.
     * @throws Exception If failed.
     */
private void testApi(boolean collocated) throws Exception {
    CollectionConfiguration colCfg = config(collocated);
    assertNotNull(grid(0).set(SET_NAME, colCfg));
    for (int i = 0; i < gridCount(); i++) {
        Set<Integer> set = grid(i).set(SET_NAME, null);
        assertNotNull(set);
        assertFalse(set.contains(1));
        assertEquals(0, set.size());
        assertTrue(set.isEmpty());
    }
    // Add, isEmpty.
    assertTrue(grid(0).set(SET_NAME, null).add(1));
    for (int i = 0; i < gridCount(); i++) {
        Set<Integer> set = grid(i).set(SET_NAME, null);
        assertEquals(1, set.size());
        assertFalse(set.isEmpty());
        assertTrue(set.contains(1));
        assertFalse(set.add(1));
        assertFalse(set.contains(100));
    }
    // Remove.
    assertTrue(grid(0).set(SET_NAME, null).remove(1));
    for (int i = 0; i < gridCount(); i++) {
        Set<Integer> set = grid(i).set(SET_NAME, null);
        assertEquals(0, set.size());
        assertTrue(set.isEmpty());
        assertFalse(set.contains(1));
        assertFalse(set.remove(1));
    }
    // Contains all.
    Collection<Integer> col1 = new ArrayList<>();
    Collection<Integer> col2 = new ArrayList<>();
    final int ITEMS = 100;
    for (int i = 0; i < ITEMS; i++) {
        assertTrue(grid(i % gridCount()).set(SET_NAME, null).add(i));
        col1.add(i);
        col2.add(i);
    }
    col2.add(ITEMS);
    for (int i = 0; i < gridCount(); i++) {
        Set<Integer> set = grid(i).set(SET_NAME, null);
        assertEquals(ITEMS, set.size());
        assertTrue(set.containsAll(col1));
        assertFalse(set.containsAll(col2));
    }
    for (int i = 0; i < gridCount(); i++) {
        Set<Integer> set = grid(i).set(SET_NAME, null);
        assertArrayContent(set.toArray(), ITEMS);
        assertArrayContent(set.toArray(new Integer[ITEMS]), ITEMS);
    }
    // Remove all.
    Collection<Integer> rmvCol = new ArrayList<>();
    for (int i = ITEMS - 10; i < ITEMS; i++) rmvCol.add(i);
    assertTrue(grid(0).set(SET_NAME, null).removeAll(rmvCol));
    for (int i = 0; i < gridCount(); i++) {
        Set<Integer> set = grid(i).set(SET_NAME, null);
        assertFalse(set.removeAll(rmvCol));
        for (Integer val : rmvCol) assertFalse(set.contains(val));
        assertArrayContent(set.toArray(), ITEMS - 10);
        assertArrayContent(set.toArray(new Integer[ITEMS - 10]), ITEMS - 10);
    }
    // Add all.
    assertTrue(grid(0).set(SET_NAME, null).addAll(rmvCol));
    for (int i = 0; i < gridCount(); i++) {
        Set<Integer> set = grid(i).set(SET_NAME, null);
        assertEquals(ITEMS, set.size());
        assertFalse(set.addAll(rmvCol));
        for (Integer val : rmvCol) assertTrue(set.contains(val));
    }
    // Retain all.
    assertTrue(grid(0).set(SET_NAME, null).retainAll(rmvCol));
    for (int i = 0; i < gridCount(); i++) {
        Set<Integer> set = grid(i).set(SET_NAME, null);
        assertEquals(rmvCol.size(), set.size());
        assertFalse(set.retainAll(rmvCol));
        for (int val = 0; val < 10; val++) assertFalse(set.contains(val));
        for (int val : rmvCol) assertTrue(set.contains(val));
    }
    // Clear.
    grid(0).set(SET_NAME, null).clear();
    for (int i = 0; i < gridCount(); i++) {
        Set<Integer> set = grid(i).set(SET_NAME, null);
        assertEquals(0, set.size());
        assertTrue(set.isEmpty());
        assertFalse(set.contains(0));
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) CollectionConfiguration(org.apache.ignite.configuration.CollectionConfiguration)

Example 35 with CollectionConfiguration

use of org.apache.ignite.configuration.CollectionConfiguration in project ignite by apache.

the class GridCacheQueueApiSelfAbstractTest method testNotReuseCache.

/**
     * @throws Exception If failed.
     */
public void testNotReuseCache() throws Exception {
    CollectionConfiguration colCfg1 = collectionConfiguration();
    CollectionConfiguration colCfg2 = collectionConfiguration();
    if (colCfg2.getAtomicityMode() == ATOMIC)
        colCfg2.setAtomicityMode(TRANSACTIONAL);
    else
        colCfg2.setAtomicityMode(ATOMIC);
    IgniteQueue queue1 = grid(0).queue("Queue1", 0, colCfg1);
    IgniteQueue queue2 = grid(0).queue("Queue2", 0, colCfg2);
    assertNotSame(getQueueCache(queue1), getQueueCache(queue2));
}
Also used : IgniteQueue(org.apache.ignite.IgniteQueue) CollectionConfiguration(org.apache.ignite.configuration.CollectionConfiguration)

Aggregations

CollectionConfiguration (org.apache.ignite.configuration.CollectionConfiguration)48 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)14 ArrayList (java.util.ArrayList)8 IgniteException (org.apache.ignite.IgniteException)8 IgniteQueue (org.apache.ignite.IgniteQueue)7 IgniteCallable (org.apache.ignite.lang.IgniteCallable)7 Test (org.junit.Test)6 Callable (java.util.concurrent.Callable)5 Ignite (org.apache.ignite.Ignite)4 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)4 IgniteEx (org.apache.ignite.internal.IgniteEx)4 IgniteInstanceResource (org.apache.ignite.resources.IgniteInstanceResource)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 IgniteQueueEndpoint (org.apache.camel.component.ignite.queue.IgniteQueueEndpoint)3 IgniteSetEndpoint (org.apache.camel.component.ignite.set.IgniteSetEndpoint)3 IgniteSet (org.apache.ignite.IgniteSet)3 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)3 HashSet (java.util.HashSet)2 JndiRegistry (org.apache.camel.impl.JndiRegistry)2 IgniteClientDisconnectedException (org.apache.ignite.IgniteClientDisconnectedException)2