Search in sources :

Example 16 with IgniteFuture

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

the class GridMultipleJobsSelfTest method runTest.

/**
     * @param jobsNum Number of jobs.
     * @param threadNum Number of threads.
     * @param jobCls Job class.
     * @throws Exception If failed.
     */
private void runTest(final int jobsNum, int threadNum, final Class<? extends IgniteCallable<Boolean>> jobCls) throws Exception {
    final Ignite ignite1 = grid(1);
    final CountDownLatch latch = new CountDownLatch(jobsNum);
    final AtomicInteger jobsCnt = new AtomicInteger();
    final AtomicInteger resCnt = new AtomicInteger();
    GridTestUtils.runMultiThreaded(new CAX() {

        @Override
        public void applyx() throws IgniteCheckedException {
            while (true) {
                int cnt = jobsCnt.incrementAndGet();
                if (cnt > jobsNum)
                    break;
                IgniteCallable<Boolean> job;
                try {
                    job = jobCls.newInstance();
                } catch (Exception e) {
                    throw new IgniteCheckedException("Could not instantiate a job.", e);
                }
                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();
                        } finally {
                            latch.countDown();
                            long cnt = resCnt.incrementAndGet();
                            if (cnt % LOG_MOD == 0)
                                X.println("Results count: " + cnt);
                        }
                    }
                });
            }
        }
    }, threadNum, "TEST-THREAD");
    latch.await();
}
Also used : CIX1(org.apache.ignite.internal.util.typedef.CIX1) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteCallable(org.apache.ignite.lang.IgniteCallable) IgniteFuture(org.apache.ignite.lang.IgniteFuture) Ignite(org.apache.ignite.Ignite) CAX(org.apache.ignite.internal.util.typedef.CAX) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteCheckedException(org.apache.ignite.IgniteCheckedException)

Example 17 with IgniteFuture

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

the class DataStreamerImplSelfTest method testNoDataNodesOnFlush.

/**
     * Test logging on {@code DataStreamer.addData()} method when cache have no data nodes
     *
     * @throws Exception If fail.
     */
public void testNoDataNodesOnFlush() throws Exception {
    boolean failed = false;
    cnt = 0;
    noNodesFilter = true;
    try {
        Ignite ignite = startGrid(1);
        IgniteFuture fut = null;
        try (IgniteDataStreamer<Integer, String> streamer = ignite.dataStreamer(DEFAULT_CACHE_NAME)) {
            fut = streamer.addData(1, "1");
            streamer.flush();
        } catch (IllegalStateException ignored) {
            try {
                fut.get();
                fail("DataStreamer ignores failed streaming.");
            } catch (CacheServerNotFoundException ignored2) {
            // No-op.
            }
            failed = true;
        }
    } finally {
        noNodesFilter = false;
        assertTrue(failed);
    }
}
Also used : CacheServerNotFoundException(org.apache.ignite.cache.CacheServerNotFoundException) IgniteFuture(org.apache.ignite.lang.IgniteFuture) Ignite(org.apache.ignite.Ignite)

Example 18 with IgniteFuture

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

the class DataStreamProcessorSelfTest method checkLoaderMultithreaded.

/**
     * Tests loader in multithreaded environment with various count of grids started.
     *
     * @param nodesCntNoCache How many nodes should be started without cache.
     * @param nodesCntCache How many nodes should be started with cache.
     * @throws Exception If failed.
     */
protected void checkLoaderMultithreaded(int nodesCntNoCache, int nodesCntCache) throws Exception {
    try {
        // Start all required nodes.
        int idx = 1;
        useCache = true;
        for (int i = 0; i < nodesCntCache; i++) startGrid(idx++);
        useCache = false;
        for (int i = 0; i < nodesCntNoCache; i++) startGrid(idx++);
        Ignite g1 = grid(idx - 1);
        // Get and configure loader.
        final IgniteDataStreamer<Integer, Integer> ldr = g1.dataStreamer(DEFAULT_CACHE_NAME);
        ldr.receiver(DataStreamerCacheUpdaters.<Integer, Integer>individual());
        ldr.perNodeBufferSize(2);
        // Define count of puts.
        final AtomicInteger idxGen = new AtomicInteger();
        final AtomicBoolean done = new AtomicBoolean();
        try {
            final int totalPutCnt = 50000;
            IgniteInternalFuture<?> fut1 = multithreadedAsync(new Callable<Object>() {

                @Override
                public Object call() throws Exception {
                    Collection<IgniteFuture<?>> futs = new ArrayList<>();
                    while (!done.get()) {
                        int idx = idxGen.getAndIncrement();
                        if (idx >= totalPutCnt) {
                            info(">>> Stopping producer thread since maximum count of puts reached.");
                            break;
                        }
                        futs.add(ldr.addData(idx, idx));
                    }
                    ldr.flush();
                    for (IgniteFuture<?> fut : futs) fut.get();
                    return null;
                }
            }, 5, "producer");
            IgniteInternalFuture<?> fut2 = multithreadedAsync(new Callable<Object>() {

                @Override
                public Object call() throws Exception {
                    while (!done.get()) {
                        ldr.flush();
                        U.sleep(100);
                    }
                    return null;
                }
            }, 1, "flusher");
            // Define index of node being restarted.
            final int restartNodeIdx = nodesCntCache + nodesCntNoCache + 1;
            IgniteInternalFuture<?> fut3 = multithreadedAsync(new Callable<Object>() {

                @Override
                public Object call() throws Exception {
                    try {
                        for (int i = 0; i < 5; i++) {
                            Ignite g = startGrid(restartNodeIdx);
                            UUID id = g.cluster().localNode().id();
                            info(">>>>>>> Started node: " + id);
                            U.sleep(1000);
                            stopGrid(getTestIgniteInstanceName(restartNodeIdx), true);
                            info(">>>>>>> Stopped node: " + id);
                        }
                    } finally {
                        done.set(true);
                        info("Start stop thread finished.");
                    }
                    return null;
                }
            }, 1, "start-stop-thread");
            fut1.get();
            fut2.get();
            fut3.get();
        } finally {
            ldr.close(false);
        }
    } finally {
        stopAllGrids();
    }
}
Also used : IgniteFuture(org.apache.ignite.lang.IgniteFuture) IgniteException(org.apache.ignite.IgniteException) CacheException(javax.cache.CacheException) IgniteFutureCancelledException(org.apache.ignite.lang.IgniteFutureCancelledException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Collection(java.util.Collection) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID)

Example 19 with IgniteFuture

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

the class SchemaExchangeSelfTest method testServerRestartWithNewTypes.

/**
     * Test client reconnect after server restart accompanied by schema change.
     *
     * @throws Exception If failed.
     */
public void testServerRestartWithNewTypes() throws Exception {
    IgniteEx node1 = start(1, KeyClass.class, ValueClass.class);
    assertTypes(node1, ValueClass.class);
    IgniteEx node2 = startClientNoCache(2);
    assertTypes(node2);
    node2.cache(CACHE_NAME);
    assertTypes(node2, ValueClass.class);
    stopGrid(1);
    assertTrue(GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return grid(2).context().clientDisconnected();
        }
    }, 10_000L));
    IgniteFuture reconnFut = null;
    try {
        node2.cache(CACHE_NAME);
        fail();
    } catch (IgniteClientDisconnectedException e) {
        reconnFut = e.reconnectFuture();
    }
    node1 = start(1, KeyClass.class, ValueClass.class, KeyClass2.class, ValueClass2.class);
    assertTypes(node1, ValueClass.class, ValueClass2.class);
    reconnFut.get();
    assertTypes(node2);
    node2.cache(CACHE_NAME);
    assertTypes(node2, ValueClass.class, ValueClass2.class);
}
Also used : GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) IgniteEx(org.apache.ignite.internal.IgniteEx) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteFuture(org.apache.ignite.lang.IgniteFuture)

Example 20 with IgniteFuture

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

the class CacheAsyncOperationsFailoverAbstractTest method startAsyncOperations.

/**
     * @param ops Number of operations.
     * @param cache Cache.
     * @return Futures.
     * @throws Exception If failed.
     */
private List<IgniteFuture<?>> startAsyncOperations(final int ops, final IgniteCache<TestKey, TestValue> cache) throws Exception {
    final List<IgniteFuture<?>> futs = Collections.synchronizedList(new ArrayList<IgniteFuture<?>>(ops));
    final AtomicInteger left = new AtomicInteger(ops);
    GridTestUtils.runMultiThreaded(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            List<IgniteFuture<?>> futs0 = new ArrayList<>();
            ThreadLocalRandom rnd = ThreadLocalRandom.current();
            while (left.getAndDecrement() > 0) {
                TreeMap<TestKey, TestValue> map = new TreeMap<>();
                int keys = 50;
                for (int k = 0; k < keys; k++) map.put(new TestKey(rnd.nextInt(10_000)), new TestValue(k));
                IgniteFuture<?> fut = cache.putAllAsync(map);
                assertNotNull(fut);
                futs0.add(fut);
            }
            futs.addAll(futs0);
            return null;
        }
    }, 10, "put-thread");
    assertEquals(ops, futs.size());
    return futs;
}
Also used : IgniteFuture(org.apache.ignite.lang.IgniteFuture) TreeMap(java.util.TreeMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

IgniteFuture (org.apache.ignite.lang.IgniteFuture)65 Ignite (org.apache.ignite.Ignite)31 ArrayList (java.util.ArrayList)23 IgniteException (org.apache.ignite.IgniteException)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)17 CountDownLatch (java.util.concurrent.CountDownLatch)14 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)12 CacheException (javax.cache.CacheException)10 IgniteCompute (org.apache.ignite.IgniteCompute)8 CI1 (org.apache.ignite.internal.util.typedef.CI1)7 UUID (java.util.UUID)6 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 ClusterNode (org.apache.ignite.cluster.ClusterNode)6 List (java.util.List)5 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)5 IgniteRunnable (org.apache.ignite.lang.IgniteRunnable)5 Collection (java.util.Collection)4 LinkedList (java.util.LinkedList)4 IgniteInClosure (org.apache.ignite.lang.IgniteInClosure)4 HashMap (java.util.HashMap)3