Search in sources :

Example 41 with IgniteFuture

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

the class IgniteClientReconnectDiscoveryStateTest method testReconnect.

/**
 * @throws Exception If failed.
 */
public void testReconnect() throws Exception {
    final Ignite client = ignite(serverCount());
    assertTrue(client.cluster().localNode().isClient());
    long topVer = 4;
    IgniteCluster cluster = client.cluster();
    cluster.nodeLocalMap().put("locMapKey", 10);
    Map<Integer, Integer> nodeCnt = new HashMap<>();
    nodeCnt.put(1, 1);
    nodeCnt.put(2, 2);
    nodeCnt.put(3, 3);
    nodeCnt.put(4, 4);
    for (Map.Entry<Integer, Integer> e : nodeCnt.entrySet()) {
        Collection<ClusterNode> nodes = cluster.topology(e.getKey());
        assertNotNull("No nodes for topology: " + e.getKey(), nodes);
        assertEquals((int) e.getValue(), nodes.size());
    }
    ClusterNode locNode = cluster.localNode();
    assertEquals(topVer, locNode.order());
    TestTcpDiscoverySpi srvSpi = spi(clientRouter(client));
    final CountDownLatch reconnectLatch = new CountDownLatch(1);
    client.events().localListen(new IgnitePredicate<Event>() {

        @Override
        public boolean apply(Event evt) {
            if (evt.type() == EVT_CLIENT_NODE_DISCONNECTED) {
                info("Disconnected: " + evt);
                IgniteFuture<?> fut = client.cluster().clientReconnectFuture();
                assertNotNull(fut);
                assertFalse(fut.isDone());
            } else if (evt.type() == EVT_CLIENT_NODE_RECONNECTED) {
                info("Reconnected: " + evt);
                reconnectLatch.countDown();
            }
            return true;
        }
    }, EVT_CLIENT_NODE_DISCONNECTED, EVT_CLIENT_NODE_RECONNECTED);
    srvSpi.failNode(client.cluster().localNode().id(), null);
    waitReconnectEvent(reconnectLatch);
    // Client failed and rejoined.
    topVer += 2;
    locNode = cluster.localNode();
    assertEquals(topVer, locNode.order());
    assertEquals(topVer, cluster.topologyVersion());
    nodeCnt.put(5, 3);
    nodeCnt.put(6, 4);
    for (Map.Entry<Integer, Integer> e : nodeCnt.entrySet()) {
        Collection<ClusterNode> nodes = cluster.topology(e.getKey());
        assertNotNull("No nodes for topology: " + e.getKey(), nodes);
        assertEquals((int) e.getValue(), nodes.size());
    }
    assertEquals(10, cluster.nodeLocalMap().get("locMapKey"));
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) HashMap(java.util.HashMap) IgniteFuture(org.apache.ignite.lang.IgniteFuture) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteCluster(org.apache.ignite.IgniteCluster) Event(org.apache.ignite.events.Event) Ignite(org.apache.ignite.Ignite) Map(java.util.Map) HashMap(java.util.HashMap)

Example 42 with IgniteFuture

use of org.apache.ignite.lang.IgniteFuture 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 IgniteFuture

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

the class CacheFutureExceptionSelfTest method testGet.

/**
 * @param nearCache If {@code true} creates near cache on client.
 * @param cpyOnRead Cache copy on read flag.
 * @throws Exception If failed.
 */
private void testGet(boolean nearCache, boolean cpyOnRead) throws Exception {
    fail = false;
    Ignite srv = grid(0);
    Ignite client = grid(1);
    final String cacheName = nearCache ? ("NEAR-CACHE-" + cpyOnRead) : ("CACHE-" + cpyOnRead);
    CacheConfiguration<Object, Object> ccfg = new CacheConfiguration<>(DEFAULT_CACHE_NAME);
    ccfg.setCopyOnRead(cpyOnRead);
    ccfg.setName(cacheName);
    IgniteCache<Object, Object> cache = srv.createCache(ccfg);
    cache.put("key", new NotSerializableClass());
    IgniteCache<Object, Object> clientCache = nearCache ? client.createNearCache(cacheName, new NearCacheConfiguration<>()) : client.cache(cacheName);
    fail = true;
    final CountDownLatch futLatch = new CountDownLatch(1);
    clientCache.getAsync("key").listen(new IgniteInClosure<IgniteFuture<Object>>() {

        @Override
        public void apply(IgniteFuture<Object> fut) {
            assertTrue(fut.isDone());
            try {
                fut.get();
                fail();
            } catch (CacheException e) {
                log.info("Expected error: " + e);
                futLatch.countDown();
            }
        }
    });
    assertTrue(futLatch.await(5, SECONDS));
    srv.destroyCache(cache.getName());
}
Also used : CacheException(javax.cache.CacheException) IgniteFuture(org.apache.ignite.lang.IgniteFuture) CountDownLatch(java.util.concurrent.CountDownLatch) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) Ignite(org.apache.ignite.Ignite) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 44 with IgniteFuture

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

the class GridJobServicesAddNodeTest method testServiceDescriptorsJob.

/**
 * @throws Exception If test failed.
 */
public void testServiceDescriptorsJob() throws Exception {
    final int tasks = 5000;
    final int threads = 10;
    final Ignite ignite1 = grid(1);
    final CountDownLatch latch = new CountDownLatch(tasks);
    final AtomicInteger jobsCnt = new AtomicInteger();
    final AtomicInteger resCnt = new AtomicInteger();
    ignite1.services().deployClusterSingleton("jobsSvc", new DummyService());
    GridTestUtils.runMultiThreadedAsync(new CAX() {

        @Override
        public void applyx() throws IgniteCheckedException {
            while (true) {
                int cnt = jobsCnt.incrementAndGet();
                if (cnt > 5000)
                    break;
                IgniteCallable<Boolean> job;
                job = new ServiceDescriptorsJob();
                IgniteFuture<Boolean> fut = ignite1.compute().callAsync(job);
                if (cnt % LOG_MOD == 0)
                    X.println("Submitted jobs: " + cnt);
                fut.listen(new CIX1<IgniteFuture<Boolean>>() {

                    @Override
                    public void applyx(IgniteFuture<Boolean> f) {
                        try {
                            assert f.get();
                            long cnt = resCnt.incrementAndGet();
                            if (cnt % LOG_MOD == 0)
                                X.println("Results count: " + cnt);
                        } finally {
                            latch.countDown();
                        }
                    }
                });
                IgniteUtils.sleep(5);
            }
        }
    }, threads, "TEST-THREAD");
    int additionalNodesStarted = 0;
    while (!latch.await(threads, TimeUnit.MILLISECONDS)) {
        if (additionalNodesStarted++ <= MAX_ADD_NODES) {
            startGrid(2 + additionalNodesStarted);
        }
    }
    assertEquals("Jobs cnt != Results cnt", jobsCnt.get() - threads, resCnt.get());
}
Also used : CIX1(org.apache.ignite.internal.util.typedef.CIX1) DummyService(org.apache.ignite.internal.processors.service.DummyService) IgniteFuture(org.apache.ignite.lang.IgniteFuture) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteCallable(org.apache.ignite.lang.IgniteCallable) Ignite(org.apache.ignite.Ignite) CAX(org.apache.ignite.internal.util.typedef.CAX)

Example 45 with IgniteFuture

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

the class IgniteComputeJobOneThreadTest method testNoTimeout.

/**
 * @throws Exception If failed.
 */
public void testNoTimeout() throws Exception {
    Ignite ignite = ignite(0);
    IgniteFuture fut = null;
    for (int i = 0; i < 10000; i++) {
        fut = ignite.compute().runAsync(new IgniteRunnable() {

            @Override
            public void run() {
            }
        });
    }
    fut.get();
    assertTrue(true);
}
Also used : IgniteFuture(org.apache.ignite.lang.IgniteFuture) Ignite(org.apache.ignite.Ignite) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable)

Aggregations

IgniteFuture (org.apache.ignite.lang.IgniteFuture)76 Ignite (org.apache.ignite.Ignite)36 ArrayList (java.util.ArrayList)28 IgniteException (org.apache.ignite.IgniteException)22 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)20 CountDownLatch (java.util.concurrent.CountDownLatch)16 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)12 CacheException (javax.cache.CacheException)11 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)10 IgniteCompute (org.apache.ignite.IgniteCompute)9 CI1 (org.apache.ignite.internal.util.typedef.CI1)9 List (java.util.List)7 ClusterNode (org.apache.ignite.cluster.ClusterNode)7 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)7 UUID (java.util.UUID)6 Collection (java.util.Collection)5 IgniteInClosure (org.apache.ignite.lang.IgniteInClosure)5 IgniteRunnable (org.apache.ignite.lang.IgniteRunnable)5 HashMap (java.util.HashMap)4 LinkedList (java.util.LinkedList)4