Search in sources :

Example 21 with IgniteCheckedException

use of org.apache.ignite.IgniteCheckedException in project ignite by apache.

the class TxPessimisticDeadlockDetectionTest method doTestDeadlock.

/**
     * @throws Exception If failed.
     */
private void doTestDeadlock(final int txCnt, final boolean loc, boolean lockPrimaryFirst, final boolean clientTx, final IgniteClosure<Integer, Object> transformer) throws Exception {
    log.info(">>> Test deadlock [txCnt=" + txCnt + ", loc=" + loc + ", lockPrimaryFirst=" + lockPrimaryFirst + ", clientTx=" + clientTx + ", transformer=" + transformer.getClass().getName() + ']');
    final AtomicInteger threadCnt = new AtomicInteger();
    final CyclicBarrier barrier = new CyclicBarrier(txCnt);
    final AtomicReference<TransactionDeadlockException> deadlockErr = new AtomicReference<>();
    final List<List<Integer>> keySets = generateKeys(txCnt, loc, !lockPrimaryFirst);
    final Set<Integer> involvedKeys = new GridConcurrentHashSet<>();
    final Set<Integer> involvedLockedKeys = new GridConcurrentHashSet<>();
    final Set<IgniteInternalTx> involvedTxs = new GridConcurrentHashSet<>();
    IgniteInternalFuture<Long> fut = GridTestUtils.runMultiThreadedAsync(new Runnable() {

        @Override
        public void run() {
            int threadNum = threadCnt.incrementAndGet();
            Ignite ignite = loc ? ignite(0) : ignite(clientTx ? threadNum - 1 + txCnt : threadNum - 1);
            IgniteCache<Object, Integer> cache = ignite.cache(CACHE_NAME);
            List<Integer> keys = keySets.get(threadNum - 1);
            int txTimeout = 500 + txCnt * 100;
            try (Transaction tx = ignite.transactions().txStart(PESSIMISTIC, REPEATABLE_READ, txTimeout, 0)) {
                involvedTxs.add(((TransactionProxyImpl) tx).tx());
                Integer key = keys.get(0);
                involvedKeys.add(key);
                Object k;
                log.info(">>> Performs put [node=" + ((IgniteKernal) ignite).localNode() + ", tx=" + tx + ", key=" + transformer.apply(key) + ']');
                cache.put(transformer.apply(key), 0);
                involvedLockedKeys.add(key);
                barrier.await();
                key = keys.get(1);
                ClusterNode primaryNode = ((IgniteCacheProxy) cache).context().affinity().primaryByKey(key, NONE);
                List<Integer> primaryKeys = primaryKeys(grid(primaryNode).cache(CACHE_NAME), 5, key + (100 * threadNum));
                Map<Object, Integer> entries = new HashMap<>();
                involvedKeys.add(key);
                entries.put(transformer.apply(key), 0);
                for (Integer i : primaryKeys) {
                    involvedKeys.add(i);
                    entries.put(transformer.apply(i), 1);
                    k = transformer.apply(i + 13);
                    involvedKeys.add(i + 13);
                    entries.put(k, 2);
                }
                log.info(">>> Performs put [node=" + ((IgniteKernal) ignite).localNode() + ", tx=" + tx + ", entries=" + entries + ']');
                cache.putAll(entries);
                tx.commit();
            } catch (Throwable e) {
                // At least one stack trace should contain TransactionDeadlockException.
                if (hasCause(e, TransactionTimeoutException.class) && hasCause(e, TransactionDeadlockException.class)) {
                    if (deadlockErr.compareAndSet(null, cause(e, TransactionDeadlockException.class)))
                        U.error(log, "At least one stack trace should contain " + TransactionDeadlockException.class.getSimpleName(), e);
                }
            }
        }
    }, loc ? 2 : txCnt, "tx-thread");
    try {
        fut.get();
    } catch (IgniteCheckedException e) {
        U.error(null, "Unexpected exception", e);
        fail();
    }
    U.sleep(1000);
    TransactionDeadlockException deadlockE = deadlockErr.get();
    assertNotNull(deadlockE);
    boolean fail = false;
    // Check transactions, futures and entry locks state.
    for (int i = 0; i < NODES_CNT * 2; i++) {
        Ignite ignite = ignite(i);
        int cacheId = ((IgniteCacheProxy) ignite.cache(CACHE_NAME)).context().cacheId();
        GridCacheSharedContext<Object, Object> cctx = ((IgniteKernal) ignite).context().cache().context();
        IgniteTxManager txMgr = cctx.tm();
        Collection<IgniteInternalTx> activeTxs = txMgr.activeTransactions();
        for (IgniteInternalTx tx : activeTxs) {
            Collection<IgniteTxEntry> entries = tx.allEntries();
            for (IgniteTxEntry entry : entries) {
                if (entry.cacheId() == cacheId) {
                    fail = true;
                    U.error(log, "Transaction still exists: " + "\n" + tx.xidVersion() + "\n" + tx.nearXidVersion() + "\n nodeId=" + cctx.localNodeId() + "\n tx=" + tx);
                }
            }
        }
        Collection<IgniteInternalFuture<?>> futs = txMgr.deadlockDetectionFutures();
        assertTrue(futs.isEmpty());
        GridCacheAdapter<Object, Integer> intCache = internalCache(i, CACHE_NAME);
        GridCacheConcurrentMap map = intCache.map();
        for (Integer key : involvedKeys) {
            Object key0 = transformer.apply(key);
            KeyCacheObject keyCacheObj = intCache.context().toCacheKeyObject(key0);
            GridCacheMapEntry entry = map.getEntry(keyCacheObj);
            if (entry != null)
                assertNull("Entry still has locks " + entry, entry.mvccAllLocal());
        }
    }
    if (fail)
        fail("Some transactions still exist");
    // Check deadlock report
    String msg = deadlockE.getMessage();
    for (IgniteInternalTx tx : involvedTxs) assertTrue(msg.contains("[txId=" + tx.xidVersion() + ", nodeId=" + tx.nodeId() + ", threadId=" + tx.threadId() + ']'));
    for (Integer key : involvedKeys) {
        if (involvedLockedKeys.contains(key))
            assertTrue(msg.contains("[key=" + transformer.apply(key) + ", cache=" + CACHE_NAME + ']'));
        else
            assertFalse(msg.contains("[key=" + transformer.apply(key)));
    }
}
Also used : TransactionDeadlockException(org.apache.ignite.transactions.TransactionDeadlockException) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) ArrayList(java.util.ArrayList) List(java.util.List) Ignite(org.apache.ignite.Ignite) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteKernal(org.apache.ignite.internal.IgniteKernal) GridCacheConcurrentMap(org.apache.ignite.internal.processors.cache.GridCacheConcurrentMap) IgniteCache(org.apache.ignite.IgniteCache) AtomicReference(java.util.concurrent.atomic.AtomicReference) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TransactionTimeoutException(org.apache.ignite.transactions.TransactionTimeoutException) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) GridCacheMapEntry(org.apache.ignite.internal.processors.cache.GridCacheMapEntry) HashMap(java.util.HashMap) Map(java.util.Map) GridCacheConcurrentMap(org.apache.ignite.internal.processors.cache.GridCacheConcurrentMap)

Example 22 with IgniteCheckedException

use of org.apache.ignite.IgniteCheckedException in project ignite by apache.

the class GridMapQueryExecutor method sendNextPage.

/**
     * @param nodeRess Results.
     * @param node Node.
     * @param qr Query results.
     * @param qry Query.
     * @param segmentId Index segment ID.
     * @param pageSize Page size.
     */
private void sendNextPage(NodeResults nodeRess, ClusterNode node, QueryResults qr, int qry, int segmentId, int pageSize) {
    QueryResult res = qr.result(qry);
    assert res != null;
    if (res.closed)
        return;
    int page = res.page;
    List<Value[]> rows = new ArrayList<>(Math.min(64, pageSize));
    boolean last = res.fetchNextPage(rows, pageSize);
    if (last) {
        res.close();
        if (qr.isAllClosed())
            nodeRess.remove(qr.qryReqId, segmentId, qr);
    }
    try {
        boolean loc = node.isLocal();
        GridQueryNextPageResponse msg = new GridQueryNextPageResponse(qr.qryReqId, segmentId, qry, page, page == 0 ? res.rowCnt : -1, res.cols, loc ? null : toMessages(rows, new ArrayList<Message>(res.cols)), loc ? rows : null);
        if (loc)
            h2.reduceQueryExecutor().onMessage(ctx.localNodeId(), msg);
        else
            ctx.io().sendToGridTopic(node, GridTopic.TOPIC_QUERY, msg, QUERY_POOL);
    } catch (IgniteCheckedException e) {
        log.error("Failed to send message.", e);
        throw new IgniteException(e);
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Message(org.apache.ignite.plugin.extensions.communication.Message) IgniteException(org.apache.ignite.IgniteException) GridQueryNextPageResponse(org.apache.ignite.internal.processors.query.h2.twostep.messages.GridQueryNextPageResponse) ArrayList(java.util.ArrayList)

Example 23 with IgniteCheckedException

use of org.apache.ignite.IgniteCheckedException in project ignite by apache.

the class OptimizedObjectStreamSelfTest method testRequireSerializable.

/**
     * @throws Exception If failed.
     */
public void testRequireSerializable() throws Exception {
    try {
        OptimizedMarshaller marsh = new OptimizedMarshaller(true);
        marsh.setContext(CTX);
        marsh.marshal(new Object());
        assert false : "Exception not thrown.";
    } catch (IgniteCheckedException e) {
        NotSerializableException serEx = e.getCause(NotSerializableException.class);
        if (serEx == null)
            throw e;
    }
}
Also used : NotSerializableException(java.io.NotSerializableException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) OptimizedMarshaller(org.apache.ignite.internal.marshaller.optimized.OptimizedMarshaller)

Example 24 with IgniteCheckedException

use of org.apache.ignite.IgniteCheckedException in project ignite by apache.

the class GridLog4j2InitializedTest method testLogInitialize.

/** */
public void testLogInitialize() {
    IgniteConfiguration cfg = new IgniteConfiguration();
    cfg.setIgniteInstanceName("grid" + 1);
    cfg.setNodeId(new UUID(1, 1));
    // cfg.setIgniteHome("/home/glutters/Documenti/apache-ignite/ignite-master/ignite/");
    URL xml = U.resolveIgniteUrl("config/ignite-log4j2.xml");
    IgniteLogger log;
    try {
        log = new Log4J2Logger(xml);
        // log.isQuiet();
        cfg.setGridLogger(log);
    } catch (IgniteCheckedException e) {
        e.printStackTrace();
        return;
    }
    assert log.isInfoEnabled() == true;
    if (log.isDebugEnabled())
        log.debug("This is 'debug' message.");
    log.info("This is 'info' message.");
    log.warning("This is 'warning' message.");
    log.warning("This is 'warning' message.", new Exception("It's a test warning exception"));
    log.error("This is 'error' message.");
    assert log.getLogger(GridLog4j2InitializedTest.class.getName()) instanceof Log4J2Logger;
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) UUID(java.util.UUID) IgniteLogger(org.apache.ignite.IgniteLogger) URL(java.net.URL) IgniteCheckedException(org.apache.ignite.IgniteCheckedException)

Example 25 with IgniteCheckedException

use of org.apache.ignite.IgniteCheckedException in project ignite by apache.

the class IgfsSharedMemoryTestServer method main.

@SuppressWarnings({ "BusyWait", "InfiniteLoopStatement" })
public static void main(String[] args) throws IgniteCheckedException {
    System.out.println("Starting server ...");
    // Tell our process PID to the wrapper.
    X.println(GridJavaProcess.PID_MSG_PREFIX + U.jvmPid());
    InputStream is = null;
    try {
        IpcServerEndpoint srv = new IpcSharedMemoryServerEndpoint(U.defaultWorkDirectory());
        new IgniteTestResources().inject(srv);
        srv.start();
        System.out.println("IPC shared memory server endpoint started");
        IpcEndpoint clientEndpoint = srv.accept();
        is = clientEndpoint.inputStream();
        for (; ; ) {
            X.println("Before read.");
            is.read();
            Thread.sleep(IpcSharedMemoryCrashDetectionSelfTest.RW_SLEEP_TIMEOUT);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        U.closeQuiet(is);
    }
}
Also used : IpcEndpoint(org.apache.ignite.internal.util.ipc.IpcEndpoint) IpcServerEndpoint(org.apache.ignite.internal.util.ipc.IpcServerEndpoint) IgniteTestResources(org.apache.ignite.testframework.junits.IgniteTestResources) InputStream(java.io.InputStream) IgniteCheckedException(org.apache.ignite.IgniteCheckedException)

Aggregations

IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1568 IgniteException (org.apache.ignite.IgniteException)388 ArrayList (java.util.ArrayList)237 IOException (java.io.IOException)226 ClusterNode (org.apache.ignite.cluster.ClusterNode)215 Map (java.util.Map)181 UUID (java.util.UUID)163 HashMap (java.util.HashMap)160 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)150 Test (org.junit.Test)143 List (java.util.List)139 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)138 Nullable (org.jetbrains.annotations.Nullable)134 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)118 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)109 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)105 IgniteInterruptedCheckedException (org.apache.ignite.internal.IgniteInterruptedCheckedException)104 Collection (java.util.Collection)97 HashSet (java.util.HashSet)97 Ignite (org.apache.ignite.Ignite)96