Search in sources :

Example 1 with IgniteSet

use of org.apache.ignite.IgniteSet in project ignite by apache.

the class GridCacheSetAbstractSelfTest method testCleanup.

/**
 * @param collocated Collocation flag.
 * @throws Exception If failed.
 */
private void testCleanup(boolean collocated) throws Exception {
    CollectionConfiguration colCfg = config(collocated);
    final IgniteSet<Integer> set0 = grid(0).set(SET_NAME, colCfg);
    assertNotNull(set0);
    final Collection<Set<Integer>> sets = new ArrayList<>();
    for (int i = 0; i < gridCount(); i++) {
        IgniteSet<Integer> set = grid(i).set(SET_NAME, null);
        assertNotNull(set);
        sets.add(set);
    }
    Collection<Integer> items = new ArrayList<>(10_000);
    for (int i = 0; i < 10_000; i++) items.add(i);
    set0.addAll(items);
    assertEquals(10_000, set0.size());
    final AtomicBoolean stop = new AtomicBoolean();
    final AtomicInteger val = new AtomicInteger(10_000);
    IgniteInternalFuture<?> fut;
    try {
        fut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                try {
                    while (!stop.get()) {
                        for (Set<Integer> set : sets) set.add(val.incrementAndGet());
                    }
                } catch (IllegalStateException e) {
                    log.info("Set removed: " + e);
                }
                return null;
            }
        }, 5, "set-add-thread");
        set0.close();
    } finally {
        stop.set(true);
    }
    fut.get();
    int cnt = 0;
    GridCacheContext cctx = GridTestUtils.getFieldValue(set0, "cctx");
    boolean separated = separated(set0);
    if (separated)
        awaitPartitionMapExchange();
    for (int i = 0; i < gridCount(); i++) {
        GridCacheAdapter cache = grid(i).context().cache().internalCache(cctx.name());
        if (separated) {
            assertNull("Cache " + cctx.name() + " was not destroyed.", cache);
            continue;
        }
        for (Object e : cache.localEntries(new CachePeekMode[] { CachePeekMode.ALL })) {
            cnt++;
            log.info("Unexpected entry: " + e);
        }
    }
    assertEquals("Found unexpected cache entries", 0, cnt);
    for (final Set<Integer> set : sets) {
        GridTestUtils.assertThrows(log, new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                set.add(10);
                return null;
            }
        }, IllegalStateException.class, null);
    }
}
Also used : HashSet(java.util.HashSet) IgniteSet(org.apache.ignite.IgniteSet) Set(java.util.Set) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) ArrayList(java.util.ArrayList) CollectionConfiguration(org.apache.ignite.configuration.CollectionConfiguration) Callable(java.util.concurrent.Callable) IgniteCallable(org.apache.ignite.lang.IgniteCallable) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridCacheAdapter(org.apache.ignite.internal.processors.cache.GridCacheAdapter)

Example 2 with IgniteSet

use of org.apache.ignite.IgniteSet in project ignite by apache.

the class GridCacheSetAbstractSelfTest method testCacheReuse.

/**
 * @param collocated Collocation flag.
 */
private void testCacheReuse(boolean collocated) {
    Ignite ignite = grid(0);
    CollectionConfiguration colCfg = collectionConfiguration().setCollocated(collocated);
    colCfg.setAtomicityMode(ATOMIC);
    colCfg.setGroupName("grp1");
    IgniteSet set1 = ignite.set("set1", colCfg);
    IgniteSet set2 = ignite.set("set2", colCfg);
    assertEquals(separated(set1), cctx(set1).cacheId() != cctx(set2).cacheId());
    colCfg.setAtomicityMode(TRANSACTIONAL);
    IgniteSet set3 = ignite.set("set3", colCfg);
    IgniteSet set4 = ignite.set("set4", colCfg);
    assertEquals(separated(set3), cctx(set3).cacheId() != cctx(set4).cacheId());
    assertTrue(cctx(set1).cacheId() != cctx(set3).cacheId());
    assertTrue(cctx(set1).groupId() == cctx(set3).groupId());
    colCfg.setGroupName("gtp2");
    IgniteSet set5 = ignite.set("set5", colCfg);
    IgniteSet set6 = ignite.set("set6", colCfg);
    assertEquals(separated(set5), cctx(set5).cacheId() != cctx(set6).cacheId());
    assertTrue(cctx(set1).groupId() != cctx(set5).groupId());
    Stream.of(set1, set2, set3, set4, set5, set6).forEach(IgniteSet::close);
}
Also used : IgniteSet(org.apache.ignite.IgniteSet) Ignite(org.apache.ignite.Ignite) CollectionConfiguration(org.apache.ignite.configuration.CollectionConfiguration)

Example 3 with IgniteSet

use of org.apache.ignite.IgniteSet in project ignite by apache.

the class GridCacheSetAbstractSelfTest method testCacheReuse.

/**
 * Test that sets within the same group and compatible configurations are stored in the same cache.
 *
 * @throws Exception If failed.
 */
public void testCacheReuse() throws Exception {
    Ignite ignite = grid(0);
    CollectionConfiguration colCfg = collectionConfiguration();
    colCfg.setAtomicityMode(ATOMIC);
    colCfg.setGroupName("grp1");
    IgniteSet set1 = ignite.set("set1", colCfg);
    IgniteSet set2 = ignite.set("set2", colCfg);
    assert cctx(set1).cacheId() == cctx(set2).cacheId();
    colCfg.setAtomicityMode(TRANSACTIONAL);
    IgniteSet set3 = ignite.set("set3", colCfg);
    IgniteSet set4 = ignite.set("set4", colCfg);
    assert cctx(set3).cacheId() == cctx(set4).cacheId();
    assert cctx(set1).cacheId() != cctx(set3).cacheId();
    assert cctx(set1).groupId() == cctx(set3).groupId();
    colCfg.setGroupName("gtp2");
    IgniteSet set5 = ignite.set("set5", colCfg);
    IgniteSet set6 = ignite.set("set6", colCfg);
    assert cctx(set5).cacheId() == cctx(set6).cacheId();
    assert cctx(set1).groupId() != cctx(set5).groupId();
}
Also used : IgniteSet(org.apache.ignite.IgniteSet) Ignite(org.apache.ignite.Ignite) CollectionConfiguration(org.apache.ignite.configuration.CollectionConfiguration)

Example 4 with IgniteSet

use of org.apache.ignite.IgniteSet in project ignite by apache.

the class IgniteDataStructureUniqueNameTest method testUniqueName.

/**
 * @param singleGrid If {@code true} uses single grid.
 * @throws Exception If failed.
 */
private void testUniqueName(final boolean singleGrid) throws Exception {
    final String name = IgniteUuid.randomUuid().toString();
    final int DS_TYPES = 6;
    final int THREADS = DS_TYPES * 3;
    for (int iter = 0; iter < 20; iter++) {
        log.info("Iteration: " + iter);
        List<IgniteInternalFuture<Object>> futs = new ArrayList<>(THREADS);
        final CyclicBarrier barrier = new CyclicBarrier(THREADS);
        for (int i = 0; i < THREADS; i++) {
            final int idx = i;
            IgniteInternalFuture<Object> fut = GridTestUtils.runAsync(new Callable<Object>() {

                @Override
                public Object call() throws Exception {
                    try {
                        Thread.currentThread().setName("test thread-" + idx);
                        barrier.await();
                        Ignite ignite = singleGrid ? ignite(0) : ignite(idx % gridCount());
                        Object res;
                        switch(idx % DS_TYPES) {
                            case 0:
                                log.info("Create atomic long, grid: " + ignite.name());
                                res = ignite.atomicLong(name, 0, true);
                                break;
                            case 1:
                                log.info("Create atomic sequence, grid: " + ignite.name());
                                res = ignite.atomicSequence(name, 0, true);
                                break;
                            case 2:
                                log.info("Create atomic stamped, grid: " + ignite.name());
                                res = ignite.atomicStamped(name, 0, true, true);
                                break;
                            case 3:
                                log.info("Create atomic reference, grid: " + ignite.name());
                                res = ignite.atomicReference(name, null, true);
                                break;
                            case 4:
                                log.info("Create queue, grid: " + ignite.name());
                                res = ignite.queue(name, 0, config(false));
                                break;
                            case 5:
                                log.info("Create set, grid: " + ignite.name());
                                res = ignite.set(name, config(false));
                                break;
                            default:
                                fail();
                                return null;
                        }
                        log.info("Thread created: " + res);
                        return res;
                    } catch (IgniteException e) {
                        log.info("Failed: " + e);
                        return e;
                    }
                }
            });
            futs.add(fut);
        }
        Closeable dataStructure = null;
        int createdCnt = 0;
        for (IgniteInternalFuture<Object> fut : futs) {
            Object res = fut.get();
            if (res instanceof IgniteException || res instanceof IgniteCheckedException)
                continue;
            assertTrue("Unexpected object: " + res, res instanceof IgniteAtomicLong || res instanceof IgniteAtomicSequence || res instanceof IgniteAtomicReference || res instanceof IgniteAtomicStamped || res instanceof IgniteCountDownLatch || res instanceof IgniteQueue || res instanceof IgniteSet || res instanceof IgniteSemaphore || res instanceof IgniteLock);
            log.info("Data structure created: " + dataStructure);
            createdCnt++;
            if (dataStructure != null)
                assertEquals(dataStructure.getClass(), res.getClass());
            else
                dataStructure = (Closeable) res;
        }
        assertNotNull(dataStructure);
        assertEquals(3, createdCnt);
        dataStructure.close();
    }
}
Also used : IgniteAtomicReference(org.apache.ignite.IgniteAtomicReference) Closeable(java.io.Closeable) ArrayList(java.util.ArrayList) IgniteAtomicLong(org.apache.ignite.IgniteAtomicLong) IgniteCountDownLatch(org.apache.ignite.IgniteCountDownLatch) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) CyclicBarrier(java.util.concurrent.CyclicBarrier) IgniteSet(org.apache.ignite.IgniteSet) IgniteAtomicStamped(org.apache.ignite.IgniteAtomicStamped) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) IgniteQueue(org.apache.ignite.IgniteQueue) IgniteAtomicSequence(org.apache.ignite.IgniteAtomicSequence) Ignite(org.apache.ignite.Ignite) IgniteSemaphore(org.apache.ignite.IgniteSemaphore) IgniteLock(org.apache.ignite.IgniteLock)

Example 5 with IgniteSet

use of org.apache.ignite.IgniteSet in project ignite by apache.

the class IgniteClientReconnectApiExceptionTest method dataStructureOperationsTest.

/**
 * @throws Exception If failed.
 */
@SuppressWarnings("unchecked")
private void dataStructureOperationsTest() throws Exception {
    final Ignite client = startClientGrid(serverCount());
    doTestIgniteOperationOnDisconnect(client, Arrays.asList(// Check atomic long.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                client.atomicLong("testAtomic", 41, true);
            } catch (IgniteClientDisconnectedException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            return client.atomicLong("testAtomic", 41, true);
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            assertNotNull(o);
            IgniteAtomicLong atomicLong = (IgniteAtomicLong) o;
            assertEquals(42, atomicLong.incrementAndGet());
            return true;
        }
    }), // Check set.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                client.set("testSet", getCollectionConfiguration());
            } catch (IgniteClientDisconnectedException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            return client.set("testSet", getCollectionConfiguration());
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            assertNotNull(o);
            IgniteSet set = (IgniteSet) o;
            String val = "testVal";
            set.add(val);
            assertEquals(1, set.size());
            assertTrue(set.contains(val));
            return true;
        }
    }), // Check ignite queue.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                client.queue("TestQueue", 10, getCollectionConfiguration());
            } catch (IgniteClientDisconnectedException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            return client.queue("TestQueue", 10, getCollectionConfiguration());
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            assertNotNull(o);
            IgniteQueue queue = (IgniteQueue) o;
            String val = "Test";
            queue.add(val);
            assertEquals(val, queue.poll());
            return true;
        }
    })));
}
Also used : IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteAtomicLong(org.apache.ignite.IgniteAtomicLong) Callable(java.util.concurrent.Callable) IgniteCallable(org.apache.ignite.lang.IgniteCallable) EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheException(javax.cache.CacheException) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteSet(org.apache.ignite.IgniteSet) IgniteQueue(org.apache.ignite.IgniteQueue) Ignite(org.apache.ignite.Ignite) T2(org.apache.ignite.internal.util.typedef.T2)

Aggregations

IgniteSet (org.apache.ignite.IgniteSet)5 Ignite (org.apache.ignite.Ignite)4 CollectionConfiguration (org.apache.ignite.configuration.CollectionConfiguration)3 ArrayList (java.util.ArrayList)2 Callable (java.util.concurrent.Callable)2 IgniteAtomicLong (org.apache.ignite.IgniteAtomicLong)2 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 IgniteException (org.apache.ignite.IgniteException)2 IgniteQueue (org.apache.ignite.IgniteQueue)2 IgniteCallable (org.apache.ignite.lang.IgniteCallable)2 Closeable (java.io.Closeable)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 CacheException (javax.cache.CacheException)1 EntryProcessorException (javax.cache.processor.EntryProcessorException)1 IgniteAtomicReference (org.apache.ignite.IgniteAtomicReference)1 IgniteAtomicSequence (org.apache.ignite.IgniteAtomicSequence)1