Search in sources :

Example 41 with IgniteRunnable

use of org.apache.ignite.lang.IgniteRunnable in project ignite by apache.

the class GridCacheSetAbstractSelfTest method testAffinityRun.

/**
     * @throws Exception If failed.
     */
public void testAffinityRun() 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.affinityRun(new IgniteRunnable() {

                    @Override
                    public void run() {
                    // No-op.
                    }
                });
                return null;
            }
        }, IgniteException.class, "Failed to execute affinityRun() 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);
        set2.affinityRun(new IgniteRunnable() {

            @IgniteInstanceResource
            private IgniteEx ignite;

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

Example 42 with IgniteRunnable

use of org.apache.ignite.lang.IgniteRunnable in project ignite by apache.

the class CacheConcurrentReadThroughTest method testConcurrentReadThrough.

/**
     * @throws Exception If failed.
     */
public void testConcurrentReadThrough() throws Exception {
    startGrid(0);
    client = true;
    Ignite client = startGrid(1);
    assertTrue(client.configuration().isClientMode());
    for (int iter = 0; iter < 10; iter++) {
        CacheConfiguration ccfg = new CacheConfiguration(DEFAULT_CACHE_NAME);
        final String cacheName = "test-" + iter;
        ccfg.setName(cacheName);
        ccfg.setReadThrough(true);
        ccfg.setCacheStoreFactory(new TestStoreFactory());
        ccfg.setStatisticsEnabled(true);
        client.createCache(ccfg);
        final Integer key = 1;
        TestCacheStore.loadCnt.set(0);
        Collection<IgniteFuture<?>> futs = new ArrayList<>();
        for (int i = 0; i < SYS_THREADS * 3; i++) {
            futs.add(client.compute().runAsync(new IgniteRunnable() {

                @IgniteInstanceResource
                private transient Ignite ignite;

                @Override
                public void run() {
                    assertFalse(ignite.configuration().isClientMode());
                    Object v = ignite.<Integer, Integer>cache(cacheName).get(key);
                    if (v == null)
                        throw new IgniteException("Failed to get value");
                }
            }));
        }
        for (IgniteFuture<?> fut : futs) fut.get();
        int loadCnt = TestCacheStore.loadCnt.get();
        long misses = ignite(1).cache(cacheName).metrics().getCacheMisses();
        log.info("Iteration [iter=" + iter + ", loadCnt=" + loadCnt + ", misses=" + misses + ']');
        assertTrue("Unexpected loadCnt: " + loadCnt, loadCnt > 0 && loadCnt <= SYS_THREADS);
        assertTrue("Unexpected misses: " + misses, misses > 0 && misses <= SYS_THREADS);
        client.destroyCache(cacheName);
    }
}
Also used : ArrayList(java.util.ArrayList) IgniteFuture(org.apache.ignite.lang.IgniteFuture) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteException(org.apache.ignite.IgniteException) Ignite(org.apache.ignite.Ignite) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 43 with IgniteRunnable

use of org.apache.ignite.lang.IgniteRunnable in project ignite by apache.

the class IgniteCacheReadThroughStoreCallTest method checkLoadCount.

/**
     * @param ccfg Cache configuration.
     * @throws Exception If failed.
     */
private void checkLoadCount(CacheConfiguration<Object, Object> ccfg) throws Exception {
    storeMap.clear();
    Ignite ignite0 = ignite(0);
    ignite0.createCache(ccfg);
    try {
        int key = 0;
        for (Ignite node : G.allGrids()) {
            log.info("Test for node: " + node.name());
            final IgniteCache<Object, Object> cache = node.cache(ccfg.getName());
            for (int i = 0; i < 50; i++) {
                final int k = key++;
                checkReadThrough(cache, new IgniteRunnable() {

                    @Override
                    public void run() {
                        cache.invoke(k, new TestEntryProcessor());
                    }
                }, null, null, 1);
            }
            for (int i = 0; i < 50; i++) {
                final int k = key++;
                checkReadThrough(cache, new IgniteRunnable() {

                    @Override
                    public void run() {
                        cache.put(k, k);
                    }
                }, null, null, 0);
            }
            if (ccfg.getAtomicityMode() == TRANSACTIONAL) {
                for (TransactionConcurrency concurrency : TransactionConcurrency.values()) {
                    for (TransactionIsolation isolation : values()) {
                        log.info("Test tx [concurrency=" + concurrency + ", isolation=" + isolation + ']');
                        for (int i = 0; i < 50; i++) {
                            final int k = key++;
                            checkReadThrough(cache, new IgniteRunnable() {

                                @Override
                                public void run() {
                                    cache.invoke(k, new TestEntryProcessor());
                                }
                            }, concurrency, isolation, 2);
                        }
                    }
                }
            }
        }
        ignite0.cache(ccfg.getName()).removeAll();
    } finally {
        ignite0.destroyCache(ccfg.getName());
    }
}
Also used : TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) Ignite(org.apache.ignite.Ignite) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable)

Example 44 with IgniteRunnable

use of org.apache.ignite.lang.IgniteRunnable in project ignite by apache.

the class GridTaskExecutionContextSelfTest method testWithNoFailoverClosure.

/**
     * @throws Exception If failed.
     */
public void testWithNoFailoverClosure() throws Exception {
    final IgniteRunnable r = new GridAbsClosureX() {

        @Override
        public void applyx() {
            CNT.incrementAndGet();
            throw new ComputeExecutionRejectedException("Expected error.");
        }
    };
    final Ignite g = grid(0);
    GridTestUtils.assertThrows(log, new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            g.compute().withNoFailover().run(r);
            return null;
        }
    }, ComputeExecutionRejectedException.class, "Expected error.");
    assertEquals(1, CNT.get());
}
Also used : ComputeExecutionRejectedException(org.apache.ignite.compute.ComputeExecutionRejectedException) Ignite(org.apache.ignite.Ignite) GridAbsClosureX(org.apache.ignite.internal.util.lang.GridAbsClosureX) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) ComputeExecutionRejectedException(org.apache.ignite.compute.ComputeExecutionRejectedException)

Example 45 with IgniteRunnable

use of org.apache.ignite.lang.IgniteRunnable in project ignite by apache.

the class GridFactorySelfTest method testCurrentIgnite.

/**
     * @throws Exception If failed.
     */
public void testCurrentIgnite() throws Exception {
    final String LEFT = "LEFT";
    final String RIGHT = "RIGHT";
    try {
        Ignite iLEFT = startGrid(LEFT);
        Ignite iRIGHT = startGrid(RIGHT);
        waitForDiscovery(iLEFT, iRIGHT);
        iLEFT.compute(iLEFT.cluster().forRemotes()).run(new IgniteRunnable() {

            @Override
            public void run() {
                assert Ignition.localIgnite().name().equals(RIGHT);
            }
        });
        iRIGHT.compute(iRIGHT.cluster().forRemotes()).run(new IgniteRunnable() {

            @Override
            public void run() {
                assert Ignition.localIgnite().name().equals(LEFT);
            }
        });
    } finally {
        stopAllGrids();
    }
}
Also used : Ignite(org.apache.ignite.Ignite) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable)

Aggregations

IgniteRunnable (org.apache.ignite.lang.IgniteRunnable)45 Ignite (org.apache.ignite.Ignite)21 IgniteException (org.apache.ignite.IgniteException)11 IgniteEx (org.apache.ignite.internal.IgniteEx)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 IgniteInstanceResource (org.apache.ignite.resources.IgniteInstanceResource)7 ArrayList (java.util.ArrayList)6 IOException (java.io.IOException)5 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)5 ClusterNode (org.apache.ignite.cluster.ClusterNode)5 UUID (java.util.UUID)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 IgniteCompute (org.apache.ignite.IgniteCompute)4 TaskEvent (org.apache.ignite.events.TaskEvent)4 IgniteFuture (org.apache.ignite.lang.IgniteFuture)4 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)3 Event (org.apache.ignite.events.Event)3 GridAbsPredicate (org.apache.ignite.internal.util.lang.GridAbsPredicate)3 Collection (java.util.Collection)2 RuntimeCamelException (org.apache.camel.RuntimeCamelException)2