Search in sources :

Example 6 with IgniteFuture

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

the class IgniteCacheConfigVariationsFullApiTest method testInvokeException.

/**
     * @throws Exception If failed.
     */
public void testInvokeException() throws Exception {
    final IgniteCache cache = jcache();
    final IgniteFuture fut = cache.invokeAsync("key2", ERR_PROCESSOR);
    assertThrows(log, new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            fut.chain(new IgniteClosure<IgniteFuture, Object>() {

                @Override
                public Object apply(IgniteFuture o) {
                    return o.get();
                }
            });
            fut.get();
            return null;
        }
    }, EntryProcessorException.class, null);
}
Also used : IgniteClosure(org.apache.ignite.lang.IgniteClosure) IgniteCache(org.apache.ignite.IgniteCache) IgniteFuture(org.apache.ignite.lang.IgniteFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheException(javax.cache.CacheException)

Example 7 with IgniteFuture

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

the class DataStreamerMultiThreadedSelfTest method startStopIgnites.

/**
     * @throws Exception If failed.
     */
private void startStopIgnites() throws Exception {
    for (int attempt = 0; attempt < 3; ++attempt) {
        log.info("Iteration: " + attempt);
        final Ignite ignite = startGrid(0);
        Set<IgniteFuture> futs = new HashSet<>();
        final AtomicInteger igniteId = new AtomicInteger(1);
        IgniteInternalFuture<?> fut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                for (int i = 1; i < 5; ++i) startGrid(igniteId.incrementAndGet());
                return true;
            }
        }, 2, "start-node-thread");
        if (dynamicCache)
            ignite.getOrCreateCache(cacheConfiguration());
        try (final DataStreamerImpl dataLdr = (DataStreamerImpl) ignite.dataStreamer(DEFAULT_CACHE_NAME)) {
            Random rnd = new Random();
            long endTime = U.currentTimeMillis() + 15_000;
            while (!fut.isDone() && U.currentTimeMillis() < endTime) futs.add(dataLdr.addData(rnd.nextInt(100_000), String.valueOf(rnd.nextInt(100_000))));
        }
        for (IgniteFuture f : futs) f.get();
        fut.get();
        stopAllGrids();
    }
}
Also used : IgniteFuture(org.apache.ignite.lang.IgniteFuture) Random(java.util.Random) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Ignite(org.apache.ignite.Ignite) HashSet(java.util.HashSet)

Example 8 with IgniteFuture

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

the class GridTaskFailoverAffinityRunTest method nodeRestart.

/**
     * @throws Exception If failed.
     */
private void nodeRestart() throws Exception {
    startGridsMultiThreaded(4);
    assertEquals((Boolean) clientMode, grid(0).configuration().isClientMode());
    final AtomicBoolean stop = new AtomicBoolean();
    final AtomicInteger gridIdx = new AtomicInteger(1);
    final long stopTime = System.currentTimeMillis() + 60_000;
    IgniteInternalFuture<?> fut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            int grid = gridIdx.getAndIncrement();
            while (!stop.get() && System.currentTimeMillis() < stopTime) {
                stopGrid(grid);
                startGrid(grid);
            }
            return null;
        }
    }, 2, "restart-thread");
    try {
        while (System.currentTimeMillis() < stopTime) {
            Collection<IgniteFuture<?>> futs = new ArrayList<>(1000);
            for (int i = 0; i < 1000; i++) {
                IgniteFuture<?> fut0 = grid(0).compute().affinityCallAsync(DEFAULT_CACHE_NAME, i, new TestJob());
                assertNotNull(fut0);
                futs.add(fut0);
            }
            for (IgniteFuture<?> fut0 : futs) {
                try {
                    fut0.get();
                } catch (IgniteException ignore) {
                // No-op.
                }
            }
        }
    } finally {
        stop.set(true);
        fut.get();
    }
}
Also used : ArrayList(java.util.ArrayList) IgniteFuture(org.apache.ignite.lang.IgniteFuture) IgniteException(org.apache.ignite.IgniteException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteException(org.apache.ignite.IgniteException)

Example 9 with IgniteFuture

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

the class GridTaskFutureImplStopGridSelfTest method testGet.

/**
     * @throws Exception If test failed.
     */
public void testGet() throws Exception {
    Ignite ignite = startGrid(getTestIgniteInstanceName());
    Thread futThread = null;
    try {
        final ComputeTaskFuture<?> fut = executeAsync(ignite.compute(), GridStopTestTask.class.getName(), null);
        fut.listen(new CI1<IgniteFuture>() {

            @SuppressWarnings({ "NakedNotify" })
            @Override
            public void apply(IgniteFuture gridFut) {
                synchronized (mux) {
                    mux.notifyAll();
                }
            }
        });
        final CountDownLatch latch = new CountDownLatch(1);
        final AtomicBoolean failed = new AtomicBoolean(false);
        futThread = new Thread(new Runnable() {

            /** {@inheritDoc} */
            @Override
            public void run() {
                try {
                    startSignal.await();
                    Object res = fut.get();
                    info("Task result: " + res);
                } catch (Throwable e) {
                    failed.set(true);
                    // Make sure that message contains info about stopping grid.
                    assert e.getMessage().startsWith("Task failed due to stopping of the grid:");
                } finally {
                    latch.countDown();
                }
            }
        }, "test-task-future-thread");
        futThread.start();
        long delta = WAIT_TIME;
        long end = System.currentTimeMillis() + delta;
        synchronized (mux) {
            while (cnt < SPLIT_COUNT && delta > 0) {
                mux.wait(delta);
                delta = end - System.currentTimeMillis();
            }
        }
        // Stops grid.
        stopGrid(getTestIgniteInstanceName());
        boolean finished = latch.await(WAIT_TIME, TimeUnit.MILLISECONDS);
        info("Future thread [alive=" + futThread.isAlive() + ']');
        info("Test task result [failed=" + failed.get() + ", taskFuture=" + fut + ']');
        assert finished : "Future thread was not stopped.";
        assert fut.isDone();
    } finally {
        if (futThread != null && futThread.isAlive()) {
            info("Task future thread interruption.");
            futThread.interrupt();
        }
        if (G.state(getTestIgniteInstanceName()) != IgniteState.STOPPED)
            stopGrid(getTestIgniteInstanceName());
    }
}
Also used : IgniteFuture(org.apache.ignite.lang.IgniteFuture) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Ignite(org.apache.ignite.Ignite)

Example 10 with IgniteFuture

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

the class GridCachePutAllFailoverSelfTest method checkPutAllFailoverColocated.

/**
     * Tests putAll() method along with failover and cache backup.
     *
     * Checks that the resulting primary cache size is the same as
     * expected.
     *
     * @param near Near enabled.
     * @param workerCnt Worker count.
     * @param shutdownCnt Shutdown count.
     * @throws Exception If failed.
     */
public void checkPutAllFailoverColocated(boolean near, int workerCnt, int shutdownCnt) throws Exception {
    nearEnabled = near;
    backups = shutdownCnt;
    Collection<Integer> testKeys = generateTestKeys();
    final Ignite master = startGrid(MASTER);
    List<Ignite> workers = new ArrayList<>(workerCnt);
    for (int i = 1; i <= workerCnt; i++) workers.add(startGrid("worker" + i));
    info("Master: " + master.cluster().localNode().id());
    List<Ignite> runningWorkers = new ArrayList<>(workerCnt);
    for (int i = 1; i <= workerCnt; i++) {
        UUID id = workers.get(i - 1).cluster().localNode().id();
        info(String.format("Worker%d: %s", i, id));
        runningWorkers.add(workers.get(i - 1));
    }
    try {
        Map<UUID, Collection<Integer>> dataChunks = new HashMap<>();
        int chunkCntr = 0;
        final AtomicBoolean jobFailed = new AtomicBoolean(false);
        int failoverPushGap = 0;
        final CountDownLatch emptyLatch = new CountDownLatch(1);
        final AtomicBoolean inputExhausted = new AtomicBoolean();
        IgniteCompute comp = compute(master.cluster().forPredicate(workerNodesFilter));
        for (Integer key : testKeys) {
            ClusterNode mappedNode = master.affinity(CACHE_NAME).mapKeyToNode(key);
            UUID nodeId = mappedNode.id();
            Collection<Integer> data = dataChunks.get(nodeId);
            if (data == null) {
                data = new ArrayList<>(DATA_CHUNK_SIZE);
                dataChunks.put(nodeId, data);
            }
            data.add(key);
            if (data.size() == DATA_CHUNK_SIZE) {
                // time to send data
                chunkCntr++;
                log.info("Pushing data chunk [chunkNo=" + chunkCntr + "]");
                ComputeTaskFuture<Void> fut = comp.executeAsync(new GridCachePutAllTask(nodeId, CACHE_NAME), data);
                // Blocks if queue is full.
                resQueue.put(fut);
                fut.listen(new CI1<IgniteFuture<Void>>() {

                    @Override
                    public void apply(IgniteFuture<Void> f) {
                        ComputeTaskFuture<?> taskFut = (ComputeTaskFuture<?>) f;
                        try {
                            //if something went wrong - we'll get exception here
                            taskFut.get();
                        } catch (IgniteException e) {
                            log.error("Job failed", e);
                            jobFailed.set(true);
                        }
                        // Remove complete future from queue to allow other jobs to proceed.
                        resQueue.remove(taskFut);
                        if (inputExhausted.get() && resQueue.isEmpty())
                            emptyLatch.countDown();
                    }
                });
                data = new ArrayList<>(DATA_CHUNK_SIZE);
                dataChunks.put(nodeId, data);
                if (chunkCntr >= FAIL_ON_CHUNK_NO) {
                    if (workerCnt - runningWorkers.size() < shutdownCnt) {
                        if (failoverPushGap > 0)
                            failoverPushGap--;
                        else {
                            Ignite victim = runningWorkers.remove(0);
                            info("Shutting down node: " + victim.cluster().localNode().id());
                            stopGrid(victim.name());
                            // Fail next node after some jobs have been pushed.
                            failoverPushGap = FAILOVER_PUSH_GAP;
                        }
                    }
                }
            }
        }
        for (Map.Entry<UUID, Collection<Integer>> entry : dataChunks.entrySet()) {
            ComputeTaskFuture<Void> fut = comp.executeAsync(new GridCachePutAllTask(entry.getKey(), CACHE_NAME), entry.getValue());
            // Blocks if queue is full.
            resQueue.put(fut);
            fut.listen(new CI1<IgniteFuture<Void>>() {

                @Override
                public void apply(IgniteFuture<Void> f) {
                    ComputeTaskFuture<?> taskFut = (ComputeTaskFuture<?>) f;
                    try {
                        //if something went wrong - we'll get exception here
                        taskFut.get();
                    } catch (IgniteException e) {
                        log.error("Job failed", e);
                        jobFailed.set(true);
                    }
                    // Remove complete future from queue to allow other jobs to proceed.
                    resQueue.remove(taskFut);
                    if (inputExhausted.get() && resQueue.isEmpty())
                        emptyLatch.countDown();
                }
            });
        }
        inputExhausted.set(true);
        if (resQueue.isEmpty())
            emptyLatch.countDown();
        // Wait for queue to empty.
        log.info("Waiting for empty queue...");
        boolean failedWait = false;
        if (!emptyLatch.await(AWAIT_TIMEOUT_SEC, TimeUnit.SECONDS)) {
            info(">>> Failed to wait for queue to empty.");
            failedWait = true;
        }
        if (!failedWait)
            assertFalse("One or more jobs have failed.", jobFailed.get());
        Collection<Integer> absentKeys = findAbsentKeys(runningWorkers.get(0), testKeys);
        if (!failedWait && !absentKeys.isEmpty()) {
            // Give some time to preloader.
            U.sleep(15000);
            absentKeys = findAbsentKeys(runningWorkers.get(0), testKeys);
        }
        info(">>> Absent keys: " + absentKeys);
        assertTrue(absentKeys.isEmpty());
        // Actual primary cache size.
        int primaryCacheSize = 0;
        for (Ignite g : runningWorkers) {
            info("Cache size [node=" + g.name() + ", localSize=" + g.cache(CACHE_NAME).localSize() + ", localPrimarySize=" + g.cache(CACHE_NAME).localSize(PRIMARY) + ']');
            primaryCacheSize += g.cache(CACHE_NAME).localSize(PRIMARY);
        }
        assertEquals(TEST_MAP_SIZE, primaryCacheSize);
        for (Ignite g : runningWorkers) assertEquals(TEST_MAP_SIZE, g.cache(CACHE_NAME).size(PRIMARY));
    } finally {
        stopAllGrids();
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IgniteFuture(org.apache.ignite.lang.IgniteFuture) IgniteException(org.apache.ignite.IgniteException) ComputeTaskFuture(org.apache.ignite.compute.ComputeTaskFuture) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID) IgniteCompute(org.apache.ignite.IgniteCompute) ClusterNode(org.apache.ignite.cluster.ClusterNode) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Collection(java.util.Collection) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap)

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