Search in sources :

Example 6 with IgniteFutureTimeoutCheckedException

use of org.apache.ignite.internal.IgniteFutureTimeoutCheckedException in project ignite by apache.

the class IgniteCacheDistributedPartitionQueryNodeRestartsSelfTest method testJoinQueryUnstableTopology.

/**
     * Tests join query within region on unstable topology.
     */
public void testJoinQueryUnstableTopology() throws Exception {
    final AtomicBoolean stop = new AtomicBoolean();
    final AtomicIntegerArray states = new AtomicIntegerArray(GRIDS_COUNT);
    final Ignite client = grid("client");
    final AtomicInteger cnt = new AtomicInteger();
    IgniteInternalFuture<?> fut = multithreadedAsync(new Runnable() {

        @Override
        public void run() {
            while (!stop.get()) {
                doTestJoinQuery(client, rnd.nextInt(PARTS_PER_REGION.length) + 1);
                int cur = cnt.incrementAndGet();
                if (cur % 100 == 0)
                    log().info("Queries count: " + cur);
            }
        }
    }, QUERY_THREADS_CNT);
    final AtomicIntegerArray restartStats = new AtomicIntegerArray(GRIDS_COUNT);
    IgniteInternalFuture<?> fut2 = multithreadedAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            while (!stop.get()) {
                int grid = rnd.nextInt(GRIDS_COUNT);
                String name = getTestIgniteInstanceName(grid);
                Integer regionId = regionForGrid(name);
                // Restart nodes only from region with enough number of nodes.
                if (regionId != 3 && regionId != 4)
                    continue;
                if (states.compareAndSet(grid, 0, 1)) {
                    restartStats.incrementAndGet(grid);
                    try {
                        stopGrid(grid);
                        Thread.sleep(rnd.nextInt(NODE_RESTART_TIME));
                        startGrid(grid);
                        Thread.sleep(rnd.nextInt(NODE_RESTART_TIME));
                    } finally {
                        states.set(grid, 0);
                    }
                }
            }
            return null;
        }
    }, RESTART_THREADS_CNT);
    try {
        fut2.get(60, TimeUnit.SECONDS);
    } catch (IgniteFutureTimeoutCheckedException ignored) {
        stop.set(true);
    }
    try {
        fut.get();
    } finally {
        log().info("Queries count: " + cnt.get());
        for (int i = 0; i < GRIDS_COUNT; i++) log().info("Grid [name = " + getTestIgniteInstanceName(i) + ", idx=" + i + " ] restarts count: " + restartStats.get(i));
    }
}
Also used : AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray) IgniteFutureTimeoutCheckedException(org.apache.ignite.internal.IgniteFutureTimeoutCheckedException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteFutureTimeoutCheckedException(org.apache.ignite.internal.IgniteFutureTimeoutCheckedException) Ignite(org.apache.ignite.Ignite)

Example 7 with IgniteFutureTimeoutCheckedException

use of org.apache.ignite.internal.IgniteFutureTimeoutCheckedException in project ignite by apache.

the class GridExecutorService method invokeAny.

/**
     * {@inheritDoc}
     * <p>
     * Note, for compilation with JDK 1.6 necessary to change method signature
     * (note the {@code &lt;? extends T&gt;} clause).
     * <pre name="code" class="java">
     *     ...
     *     public &lt;T&gt; T invokeAny(Collection&lt;? extends Callable&lt;T&gt;&gt; tasks, long timeout, TimeUnit unit)
     *         throws InterruptedException, ExecutionException, TimeoutException {
     *     }
     *     ...
     * </pre>
     */
@SuppressWarnings({ "MethodWithTooExceptionsDeclared" })
@Override
public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
    A.notNull(tasks, "tasks != null");
    A.ensure(!tasks.isEmpty(), "!tasks.isEmpty()");
    A.ensure(timeout >= 0, "timeout >= 0");
    A.notNull(unit, "unit != null");
    long now = System.currentTimeMillis();
    timeout = TimeUnit.MILLISECONDS.convert(timeout, unit);
    long end = timeout == 0 ? Long.MAX_VALUE : timeout + now;
    // Prevent overflow.
    if (end < 0)
        end = Long.MAX_VALUE;
    checkShutdown();
    Collection<IgniteInternalFuture<T>> taskFuts = new ArrayList<>();
    for (Callable<T> cmd : tasks) {
        // Execute task with predefined timeout.
        IgniteInternalFuture<T> fut;
        ctx.gateway().readLock();
        try {
            fut = ctx.closure().callAsync(BALANCE, cmd, prj.nodes());
        } finally {
            ctx.gateway().readUnlock();
        }
        taskFuts.add(fut);
    }
    T res = null;
    boolean isInterrupted = false;
    boolean isResRcvd = false;
    int errCnt = 0;
    for (IgniteInternalFuture<T> fut : taskFuts) {
        now = U.currentTimeMillis();
        boolean cancel = false;
        if (!isInterrupted && !isResRcvd && now < end) {
            try {
                res = fut.get(end - now);
                isResRcvd = true;
                // Cancel next tasks (avoid current task cancellation below in loop).
                continue;
            } catch (IgniteFutureTimeoutCheckedException ignored) {
                if (log.isDebugEnabled())
                    log.debug("Timeout occurred during getting task result: " + fut);
                cancel = true;
            } catch (IgniteCheckedException e) {
                // Note: that execution may be interrupted on remote node. Possible bug.
                if (e.getCause() instanceof InterruptedException)
                    isInterrupted = true;
                else
                    errCnt++;
            }
        }
        // Cancel active task if any task interrupted, timeout elapsed or received task result before.
        if ((isInterrupted || isResRcvd || cancel) && !fut.isDone())
            cancelFuture(fut);
    }
    // Throw exception if any task wait was interrupted.
    if (isInterrupted)
        throw new InterruptedException("Got interrupted while waiting for tasks invocation.");
    // per executor service contract.
    if (!isResRcvd && taskFuts.size() == errCnt)
        throw new ExecutionException("Failed to get any task completion.", null);
    // throw timeout exception per executor service contract.
    if (!isResRcvd)
        throw new TimeoutException("Timeout occurred during tasks invocation.");
    return res;
}
Also used : ArrayList(java.util.ArrayList) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteFutureTimeoutCheckedException(org.apache.ignite.internal.IgniteFutureTimeoutCheckedException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 8 with IgniteFutureTimeoutCheckedException

use of org.apache.ignite.internal.IgniteFutureTimeoutCheckedException in project ignite by apache.

the class GridCachePartitionExchangeManager method onKernalStart0.

/** {@inheritDoc} */
@Override
protected void onKernalStart0(boolean reconnect) throws IgniteCheckedException {
    super.onKernalStart0(reconnect);
    ClusterNode loc = cctx.localNode();
    long startTime = loc.metrics().getStartTime();
    assert startTime > 0;
    // Generate dummy discovery event for local node joining.
    T2<DiscoveryEvent, DiscoCache> locJoin = cctx.discovery().localJoin();
    DiscoveryEvent discoEvt = locJoin.get1();
    DiscoCache discoCache = locJoin.get2();
    GridDhtPartitionExchangeId exchId = initialExchangeId();
    GridDhtPartitionsExchangeFuture fut = exchangeFuture(exchId, discoEvt, discoCache, null, null);
    if (reconnect)
        reconnectExchangeFut = new GridFutureAdapter<>();
    exchWorker.addFirstExchangeFuture(fut);
    if (!cctx.kernalContext().clientNode()) {
        for (int cnt = 0; cnt < cctx.gridConfig().getRebalanceThreadPoolSize(); cnt++) {
            final int idx = cnt;
            cctx.io().addOrderedHandler(rebalanceTopic(cnt), new CI2<UUID, GridCacheMessage>() {

                @Override
                public void apply(final UUID id, final GridCacheMessage m) {
                    if (!enterBusy())
                        return;
                    try {
                        GridCacheContext cacheCtx = cctx.cacheContext(m.cacheId);
                        if (cacheCtx != null) {
                            if (m instanceof GridDhtPartitionSupplyMessage)
                                cacheCtx.preloader().handleSupplyMessage(idx, id, (GridDhtPartitionSupplyMessage) m);
                            else if (m instanceof GridDhtPartitionDemandMessage)
                                cacheCtx.preloader().handleDemandMessage(idx, id, (GridDhtPartitionDemandMessage) m);
                            else
                                U.error(log, "Unsupported message type: " + m.getClass().getName());
                        }
                    } finally {
                        leaveBusy();
                    }
                }
            });
        }
    }
    new IgniteThread(cctx.igniteInstanceName(), "exchange-worker", exchWorker).start();
    if (reconnect) {
        fut.listen(new CI1<IgniteInternalFuture<AffinityTopologyVersion>>() {

            @Override
            public void apply(IgniteInternalFuture<AffinityTopologyVersion> fut) {
                try {
                    fut.get();
                    for (GridCacheContext cacheCtx : cctx.cacheContexts()) cacheCtx.preloader().onInitialExchangeComplete(null);
                    reconnectExchangeFut.onDone();
                } catch (IgniteCheckedException e) {
                    for (GridCacheContext cacheCtx : cctx.cacheContexts()) cacheCtx.preloader().onInitialExchangeComplete(e);
                    reconnectExchangeFut.onDone(e);
                }
            }
        });
    } else {
        if (log.isDebugEnabled())
            log.debug("Beginning to wait on local exchange future: " + fut);
        boolean first = true;
        while (true) {
            try {
                fut.get(cctx.preloadExchangeTimeout());
                break;
            } catch (IgniteFutureTimeoutCheckedException ignored) {
                if (first) {
                    U.warn(log, "Failed to wait for initial partition map exchange. " + "Possible reasons are: " + U.nl() + "  ^-- Transactions in deadlock." + U.nl() + "  ^-- Long running transactions (ignore if this is the case)." + U.nl() + "  ^-- Unreleased explicit locks.");
                    first = false;
                } else
                    U.warn(log, "Still waiting for initial partition map exchange [fut=" + fut + ']');
            } catch (IgniteNeedReconnectException e) {
                throw e;
            } catch (Exception e) {
                if (fut.reconnectOnError(e))
                    throw new IgniteNeedReconnectException(cctx.localNode(), e);
                throw e;
            }
        }
        AffinityTopologyVersion nodeStartVer = new AffinityTopologyVersion(discoEvt.topologyVersion(), 0);
        for (GridCacheContext cacheCtx : cctx.cacheContexts()) {
            if (nodeStartVer.equals(cacheCtx.startTopologyVersion()))
                cacheCtx.preloader().onInitialExchangeComplete(null);
        }
        if (log.isDebugEnabled())
            log.debug("Finished waiting for initial exchange: " + fut.exchangeId());
    }
}
Also used : DiscoCache(org.apache.ignite.internal.managers.discovery.DiscoCache) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) GridDhtPartitionExchangeId(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionExchangeId) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter) IgniteFutureTimeoutCheckedException(org.apache.ignite.internal.IgniteFutureTimeoutCheckedException) UUID(java.util.UUID) IgniteNeedReconnectException(org.apache.ignite.internal.IgniteNeedReconnectException) ClusterNode(org.apache.ignite.cluster.ClusterNode) GridDhtPartitionsExchangeFuture(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsExchangeFuture) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) GridDhtPartitionSupplyMessage(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionSupplyMessage) IgniteClientDisconnectedCheckedException(org.apache.ignite.internal.IgniteClientDisconnectedCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteNeedReconnectException(org.apache.ignite.internal.IgniteNeedReconnectException) IgniteFutureTimeoutCheckedException(org.apache.ignite.internal.IgniteFutureTimeoutCheckedException) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) IgniteThread(org.apache.ignite.thread.IgniteThread) GridDhtPartitionDemandMessage(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionDemandMessage)

Example 9 with IgniteFutureTimeoutCheckedException

use of org.apache.ignite.internal.IgniteFutureTimeoutCheckedException in project ignite by apache.

the class GridFutureAdapterSelfTest method checkChaining.

/**
     * @param exec Executor for chain callback.
     * @throws Exception If failed.
     */
@SuppressWarnings("ErrorNotRethrown")
private void checkChaining(ExecutorService exec) throws Exception {
    final CX1<IgniteInternalFuture<Object>, Object> passThrough = new CX1<IgniteInternalFuture<Object>, Object>() {

        @Override
        public Object applyx(IgniteInternalFuture<Object> f) throws IgniteCheckedException {
            return f.get();
        }
    };
    GridFutureAdapter<Object> fut = new GridFutureAdapter<>();
    IgniteInternalFuture<Object> chain = exec != null ? fut.chain(passThrough, exec) : fut.chain(passThrough);
    assertFalse(fut.isDone());
    assertFalse(chain.isDone());
    try {
        chain.get(20);
        fail("Expects timeout exception.");
    } catch (IgniteFutureTimeoutCheckedException e) {
        info("Expected timeout exception: " + e.getMessage());
    }
    fut.onDone("result");
    assertEquals("result", chain.get(1));
    // Test exception re-thrown.
    fut = new GridFutureAdapter<>();
    chain = exec != null ? fut.chain(passThrough, exec) : fut.chain(passThrough);
    fut.onDone(new ClusterGroupEmptyCheckedException("test exception"));
    try {
        chain.get();
        fail("Expects failed with exception.");
    } catch (ClusterGroupEmptyCheckedException e) {
        info("Expected exception: " + e.getMessage());
    }
    // Test error re-thrown.
    fut = new GridFutureAdapter<>();
    chain = exec != null ? fut.chain(passThrough, exec) : fut.chain(passThrough);
    try {
        fut.onDone(new StackOverflowError("test error"));
        if (exec == null)
            fail("Expects failed with error.");
    } catch (StackOverflowError e) {
        info("Expected error: " + e.getMessage());
    }
    try {
        chain.get();
        fail("Expects failed with error.");
    } catch (StackOverflowError e) {
        info("Expected error: " + e.getMessage());
    }
}
Also used : ClusterGroupEmptyCheckedException(org.apache.ignite.internal.cluster.ClusterGroupEmptyCheckedException) IgniteFutureTimeoutCheckedException(org.apache.ignite.internal.IgniteFutureTimeoutCheckedException) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) CX1(org.apache.ignite.internal.util.typedef.CX1)

Example 10 with IgniteFutureTimeoutCheckedException

use of org.apache.ignite.internal.IgniteFutureTimeoutCheckedException in project ignite by apache.

the class IgniteCacheCrossCacheTxFailoverTest method crossCacheTxFailover.

/**
     * @param cacheMode Cache mode.
     * @param sameAff If {@code false} uses different number of partitions for caches.
     * @param concurrency Transaction concurrency.
     * @param isolation Transaction isolation.
     * @throws Exception If failed.
     */
private void crossCacheTxFailover(CacheMode cacheMode, boolean sameAff, final TransactionConcurrency concurrency, final TransactionIsolation isolation) throws Exception {
    IgniteKernal ignite0 = (IgniteKernal) ignite(0);
    final AtomicBoolean stop = new AtomicBoolean();
    try {
        ignite0.createCache(cacheConfiguration(CACHE1, cacheMode, 256));
        ignite0.createCache(cacheConfiguration(CACHE2, cacheMode, sameAff ? 256 : 128));
        final AtomicInteger threadIdx = new AtomicInteger();
        IgniteInternalFuture<?> fut = runMultiThreadedAsync(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                int idx = threadIdx.getAndIncrement();
                Ignite ignite = ignite(idx % GRID_CNT);
                log.info("Started update thread [node=" + ignite.name() + ", client=" + ignite.configuration().isClientMode() + ']');
                IgniteCache<TestKey, TestValue> cache1 = ignite.cache(CACHE1);
                IgniteCache<TestKey, TestValue> cache2 = ignite.cache(CACHE2);
                assertNotSame(cache1, cache2);
                IgniteTransactions txs = ignite.transactions();
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                long iter = 0;
                while (!stop.get()) {
                    boolean sameKey = rnd.nextBoolean();
                    try {
                        try (Transaction tx = txs.txStart(concurrency, isolation)) {
                            if (sameKey) {
                                TestKey key = new TestKey(rnd.nextLong(KEY_RANGE));
                                cacheOperation(rnd, cache1, key);
                                cacheOperation(rnd, cache2, key);
                            } else {
                                TestKey key1 = new TestKey(rnd.nextLong(KEY_RANGE));
                                TestKey key2 = new TestKey(key1.key() + 1);
                                cacheOperation(rnd, cache1, key1);
                                cacheOperation(rnd, cache2, key2);
                            }
                            tx.commit();
                        }
                    } catch (CacheException | IgniteException e) {
                        log.info("Update error: " + e);
                    }
                    if (iter++ % 500 == 0)
                        log.info("Iteration: " + iter);
                }
                return null;
            }

            /**
                 * @param rnd Random.
                 * @param cache Cache.
                 * @param key Key.
                 */
            private void cacheOperation(ThreadLocalRandom rnd, IgniteCache<TestKey, TestValue> cache, TestKey key) {
                switch(rnd.nextInt(4)) {
                    case 0:
                        cache.put(key, new TestValue(rnd.nextLong()));
                        break;
                    case 1:
                        cache.remove(key);
                        break;
                    case 2:
                        cache.invoke(key, new TestEntryProcessor(rnd.nextBoolean() ? 1L : null));
                        break;
                    case 3:
                        cache.get(key);
                        break;
                    default:
                        assert false;
                }
            }
        }, 10, "tx-thread");
        long stopTime = System.currentTimeMillis() + 3 * 60_000;
        long topVer = ignite0.cluster().topologyVersion();
        boolean failed = false;
        while (System.currentTimeMillis() < stopTime) {
            log.info("Start node.");
            IgniteKernal ignite = (IgniteKernal) startGrid(GRID_CNT);
            assertFalse(ignite.configuration().isClientMode());
            topVer++;
            IgniteInternalFuture<?> affFut = ignite.context().cache().context().exchange().affinityReadyFuture(new AffinityTopologyVersion(topVer));
            try {
                if (affFut != null)
                    affFut.get(30_000);
            } catch (IgniteFutureTimeoutCheckedException ignored) {
                log.error("Failed to wait for affinity future after start: " + topVer);
                failed = true;
                break;
            }
            Thread.sleep(500);
            log.info("Stop node.");
            stopGrid(GRID_CNT);
            topVer++;
            affFut = ignite0.context().cache().context().exchange().affinityReadyFuture(new AffinityTopologyVersion(topVer));
            try {
                if (affFut != null)
                    affFut.get(30_000);
            } catch (IgniteFutureTimeoutCheckedException ignored) {
                log.error("Failed to wait for affinity future after stop: " + topVer);
                failed = true;
                break;
            }
        }
        stop.set(true);
        fut.get();
        assertFalse("Test failed, see log for details.", failed);
    } finally {
        stop.set(true);
        ignite0.destroyCache(CACHE1);
        ignite0.destroyCache(CACHE2);
        AffinityTopologyVersion topVer = ignite0.context().cache().context().exchange().lastTopologyFuture().get();
        for (Ignite ignite : G.allGrids()) ((IgniteKernal) ignite).context().cache().context().exchange().affinityReadyFuture(topVer).get();
        awaitPartitionMapExchange();
    }
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) IgniteCache(org.apache.ignite.IgniteCache) IgniteTransactions(org.apache.ignite.IgniteTransactions) CacheException(javax.cache.CacheException) IgniteException(org.apache.ignite.IgniteException) IgniteFutureTimeoutCheckedException(org.apache.ignite.internal.IgniteFutureTimeoutCheckedException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) IgniteFutureTimeoutCheckedException(org.apache.ignite.internal.IgniteFutureTimeoutCheckedException) Ignite(org.apache.ignite.Ignite)

Aggregations

IgniteFutureTimeoutCheckedException (org.apache.ignite.internal.IgniteFutureTimeoutCheckedException)12 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)4 ArrayList (java.util.ArrayList)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 CacheException (javax.cache.CacheException)3 Ignite (org.apache.ignite.Ignite)3 IgniteCache (org.apache.ignite.IgniteCache)3 UUID (java.util.UUID)2 AtomicIntegerArray (java.util.concurrent.atomic.AtomicIntegerArray)2 IgniteException (org.apache.ignite.IgniteException)2 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)2 ClusterNode (org.apache.ignite.cluster.ClusterNode)2 IgniteClientDisconnectedCheckedException (org.apache.ignite.internal.IgniteClientDisconnectedCheckedException)2 IgniteInterruptedCheckedException (org.apache.ignite.internal.IgniteInterruptedCheckedException)2 IgniteKernal (org.apache.ignite.internal.IgniteKernal)2 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)2 GridRandom (org.apache.ignite.internal.util.GridRandom)2 ArrayDeque (java.util.ArrayDeque)1