Search in sources :

Example 16 with IgniteCheckedException

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

the class SqlListenerNioServerBuffer method read.

/**
     * @param buf Buffer.
     * @return Message bytes or {@code null} if message is not fully read yet.
     * @throws IgniteCheckedException If failed to parse message.
     */
@Nullable
public byte[] read(ByteBuffer buf) throws IgniteCheckedException {
    if (cnt < 0) {
        for (; cnt < 0 && buf.hasRemaining(); cnt++) msgSize |= (buf.get() & 0xFF) << (8 * (4 + cnt));
        if (cnt < 0)
            return null;
        // If count is 0 then message size should be inited.
        if (msgSize <= 0)
            throw new IgniteCheckedException("Invalid message size: " + msgSize);
        data = new byte[msgSize];
    }
    assert msgSize > 0;
    assert cnt >= 0;
    int remaining = buf.remaining();
    // If there are more bytes in buffer.
    if (remaining > 0) {
        int missing = msgSize - cnt;
        // Read only up to message size.
        if (missing > 0) {
            int len = missing < remaining ? missing : remaining;
            buf.get(data, cnt, len);
            cnt += len;
        }
    }
    if (cnt == msgSize) {
        byte[] data0 = data;
        reset();
        return data0;
    } else
        return null;
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Nullable(org.jetbrains.annotations.Nullable)

Example 17 with IgniteCheckedException

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

the class GridJobMetricsProcessor method start.

/** {@inheritDoc} */
@Override
public void start(boolean activeOnStart) throws IgniteCheckedException {
    assertParameter(histSize > 0, "metricsHistorySize > 0");
    assertParameter(expireTime > 0, "metricsExpireTime > 0");
    if (metrics.snapshotsQueues == null)
        throw new IgniteCheckedException("Invalid concurrency level configured " + "(is 'IGNITE_JOBS_METRICS_CONCURRENCY_LEVEL' system property properly set?).");
    if (log.isDebugEnabled())
        log.debug("Job metrics processor started [histSize=" + histSize + ", concurLvl=" + CONCURRENCY_LEVEL + ", expireTime=" + expireTime + ']');
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException)

Example 18 with IgniteCheckedException

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

the class GridQueryProcessor method querySqlFieldsNoCache.

/**
     * Query SQL fields without strict dependency on concrete cache.
     *
     * @param qry Query.
     * @param keepBinary Keep binary flag.
     * @return Cursor.
     */
public FieldsQueryCursor<List<?>> querySqlFieldsNoCache(final SqlFieldsQuery qry, final boolean keepBinary) {
    checkxEnabled();
    validateSqlFieldsQuery(qry);
    if (qry.isLocal())
        throw new IgniteException("Local query is not supported without specific cache.");
    if (qry.getSchema() == null)
        qry.setSchema(QueryUtils.DFLT_SCHEMA);
    if (!busyLock.enterBusy())
        throw new IllegalStateException("Failed to execute query (grid is stopping).");
    try {
        IgniteOutClosureX<FieldsQueryCursor<List<?>>> clo = new IgniteOutClosureX<FieldsQueryCursor<List<?>>>() {

            @Override
            public FieldsQueryCursor<List<?>> applyx() throws IgniteCheckedException {
                GridQueryCancel cancel = new GridQueryCancel();
                return idx.queryDistributedSqlFields(qry.getSchema(), qry, keepBinary, cancel, null);
            }
        };
        return executeQuery(GridCacheQueryType.SQL_FIELDS, qry.getSql(), null, clo, true);
    } catch (IgniteCheckedException e) {
        throw new CacheException(e);
    } finally {
        busyLock.leaveBusy();
    }
}
Also used : IgniteOutClosureX(org.apache.ignite.internal.util.lang.IgniteOutClosureX) FieldsQueryCursor(org.apache.ignite.cache.query.FieldsQueryCursor) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheException(javax.cache.CacheException) IgniteException(org.apache.ignite.IgniteException) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList)

Example 19 with IgniteCheckedException

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

the class TcpCommunicationSpi method safeHandshake.

/**
     * Performs handshake in timeout-safe way.
     *
     * @param client Client.
     * @param recovery Recovery descriptor if use recovery handshake, otherwise {@code null}.
     * @param rmtNodeId Remote node.
     * @param timeout Timeout for handshake.
     * @param sslMeta Session meta.
     * @param handshakeConnIdx Non null connection index if need send it in handshake.
     * @throws IgniteCheckedException If handshake failed or wasn't completed withing timeout.
     * @return Handshake response.
     */
@SuppressWarnings("ThrowFromFinallyBlock")
private <T> long safeHandshake(T client, @Nullable GridNioRecoveryDescriptor recovery, UUID rmtNodeId, long timeout, GridSslMeta sslMeta, @Nullable Integer handshakeConnIdx) throws IgniteCheckedException {
    HandshakeTimeoutObject<T> obj = new HandshakeTimeoutObject<>(client, U.currentTimeMillis() + timeout);
    addTimeoutObject(obj);
    long rcvCnt = 0;
    try {
        if (client instanceof GridCommunicationClient)
            ((GridCommunicationClient) client).doHandshake(new HandshakeClosure(rmtNodeId));
        else {
            SocketChannel ch = (SocketChannel) client;
            boolean success = false;
            try {
                BlockingSslHandler sslHnd = null;
                ByteBuffer buf;
                if (isSslEnabled()) {
                    assert sslMeta != null;
                    sslHnd = new BlockingSslHandler(sslMeta.sslEngine(), ch, directBuf, ByteOrder.nativeOrder(), log);
                    if (!sslHnd.handshake())
                        throw new IgniteCheckedException("SSL handshake is not completed.");
                    ByteBuffer handBuff = sslHnd.applicationBuffer();
                    if (handBuff.remaining() < NodeIdMessage.MESSAGE_FULL_SIZE) {
                        buf = ByteBuffer.allocate(1000);
                        int read = ch.read(buf);
                        if (read == -1)
                            throw new IgniteCheckedException("Failed to read remote node ID (connection closed).");
                        buf.flip();
                        buf = sslHnd.decode(buf);
                    } else
                        buf = handBuff;
                } else {
                    buf = ByteBuffer.allocate(NodeIdMessage.MESSAGE_FULL_SIZE);
                    for (int i = 0; i < NodeIdMessage.MESSAGE_FULL_SIZE; ) {
                        int read = ch.read(buf);
                        if (read == -1)
                            throw new IgniteCheckedException("Failed to read remote node ID (connection closed).");
                        i += read;
                    }
                }
                UUID rmtNodeId0 = U.bytesToUuid(buf.array(), Message.DIRECT_TYPE_SIZE);
                if (!rmtNodeId.equals(rmtNodeId0))
                    throw new IgniteCheckedException("Remote node ID is not as expected [expected=" + rmtNodeId + ", rcvd=" + rmtNodeId0 + ']');
                else if (log.isDebugEnabled())
                    log.debug("Received remote node ID: " + rmtNodeId0);
                if (isSslEnabled()) {
                    assert sslHnd != null;
                    ch.write(sslHnd.encrypt(ByteBuffer.wrap(U.IGNITE_HEADER)));
                } else
                    ch.write(ByteBuffer.wrap(U.IGNITE_HEADER));
                ClusterNode locNode = getLocalNode();
                if (locNode == null)
                    throw new IgniteCheckedException("Local node has not been started or " + "fully initialized [isStopping=" + getSpiContext().isStopping() + ']');
                if (recovery != null) {
                    HandshakeMessage msg;
                    int msgSize = HandshakeMessage.MESSAGE_FULL_SIZE;
                    if (handshakeConnIdx != null) {
                        msg = new HandshakeMessage2(locNode.id(), recovery.incrementConnectCount(), recovery.received(), handshakeConnIdx);
                        msgSize += 4;
                    } else {
                        msg = new HandshakeMessage(locNode.id(), recovery.incrementConnectCount(), recovery.received());
                    }
                    if (log.isDebugEnabled())
                        log.debug("Writing handshake message [locNodeId=" + locNode.id() + ", rmtNode=" + rmtNodeId + ", msg=" + msg + ']');
                    buf = ByteBuffer.allocate(msgSize);
                    buf.order(ByteOrder.nativeOrder());
                    boolean written = msg.writeTo(buf, null);
                    assert written;
                    buf.flip();
                    if (isSslEnabled()) {
                        assert sslHnd != null;
                        ch.write(sslHnd.encrypt(buf));
                    } else
                        ch.write(buf);
                } else {
                    if (isSslEnabled()) {
                        assert sslHnd != null;
                        ch.write(sslHnd.encrypt(ByteBuffer.wrap(nodeIdMessage().nodeIdBytesWithType)));
                    } else
                        ch.write(ByteBuffer.wrap(nodeIdMessage().nodeIdBytesWithType));
                }
                if (recovery != null) {
                    if (log.isDebugEnabled())
                        log.debug("Waiting for handshake [rmtNode=" + rmtNodeId + ']');
                    if (isSslEnabled()) {
                        assert sslHnd != null;
                        buf = ByteBuffer.allocate(1000);
                        buf.order(ByteOrder.nativeOrder());
                        ByteBuffer decode = ByteBuffer.allocate(2 * buf.capacity());
                        decode.order(ByteOrder.nativeOrder());
                        for (int i = 0; i < RecoveryLastReceivedMessage.MESSAGE_FULL_SIZE; ) {
                            int read = ch.read(buf);
                            if (read == -1)
                                throw new IgniteCheckedException("Failed to read remote node recovery handshake " + "(connection closed).");
                            buf.flip();
                            ByteBuffer decode0 = sslHnd.decode(buf);
                            i += decode0.remaining();
                            decode = appendAndResizeIfNeeded(decode, decode0);
                            buf.clear();
                        }
                        decode.flip();
                        rcvCnt = decode.getLong(Message.DIRECT_TYPE_SIZE);
                        if (decode.limit() > RecoveryLastReceivedMessage.MESSAGE_FULL_SIZE) {
                            decode.position(RecoveryLastReceivedMessage.MESSAGE_FULL_SIZE);
                            sslMeta.decodedBuffer(decode);
                        }
                        ByteBuffer inBuf = sslHnd.inputBuffer();
                        if (inBuf.position() > 0)
                            sslMeta.encodedBuffer(inBuf);
                    } else {
                        buf = ByteBuffer.allocate(RecoveryLastReceivedMessage.MESSAGE_FULL_SIZE);
                        buf.order(ByteOrder.nativeOrder());
                        for (int i = 0; i < RecoveryLastReceivedMessage.MESSAGE_FULL_SIZE; ) {
                            int read = ch.read(buf);
                            if (read == -1)
                                throw new IgniteCheckedException("Failed to read remote node recovery handshake " + "(connection closed).");
                            i += read;
                        }
                        rcvCnt = buf.getLong(Message.DIRECT_TYPE_SIZE);
                    }
                    if (log.isDebugEnabled())
                        log.debug("Received handshake message [rmtNode=" + rmtNodeId + ", rcvCnt=" + rcvCnt + ']');
                    if (rcvCnt == -1) {
                        if (log.isDebugEnabled())
                            log.debug("Connection rejected, will retry client creation [rmtNode=" + rmtNodeId + ']');
                    } else
                        success = true;
                } else
                    success = true;
            } catch (IOException e) {
                if (log.isDebugEnabled())
                    log.debug("Failed to read from channel: " + e);
                throw new IgniteCheckedException("Failed to read from channel.", e);
            } finally {
                if (!success)
                    U.closeQuiet(ch);
            }
        }
    } finally {
        boolean cancelled = obj.cancel();
        if (cancelled)
            removeTimeoutObject(obj);
        // Ignoring whatever happened after timeout - reporting only timeout event.
        if (!cancelled)
            throw new HandshakeTimeoutException("Failed to perform handshake due to timeout (consider increasing " + "'connectionTimeout' configuration property).");
    }
    return rcvCnt;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) SocketChannel(java.nio.channels.SocketChannel) BlockingSslHandler(org.apache.ignite.internal.util.nio.ssl.BlockingSslHandler) IOException(java.io.IOException) GridCommunicationClient(org.apache.ignite.internal.util.nio.GridCommunicationClient) ByteBuffer(java.nio.ByteBuffer) IpcEndpoint(org.apache.ignite.internal.util.ipc.IpcEndpoint) IpcSharedMemoryServerEndpoint(org.apache.ignite.internal.util.ipc.shmem.IpcSharedMemoryServerEndpoint) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) EVT_NODE_LEFT(org.apache.ignite.events.EventType.EVT_NODE_LEFT) LT(org.apache.ignite.internal.util.typedef.internal.LT) UUID(java.util.UUID)

Example 20 with IgniteCheckedException

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

the class TxOptimisticDeadlockDetectionTest 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() + ']');
    TestCommunicationSpi.init(txCnt);
    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(OPTIMISTIC, REPEATABLE_READ, txTimeout, 0)) {
                IgniteInternalTx tx0 = ((TransactionProxyImpl) tx).tx();
                involvedTxs.add(tx0);
                Integer key = keys.get(0);
                involvedKeys.add(key);
                Object k;
                log.info(">>> Performs put [node=" + ((IgniteKernal) ignite).localNode().id() + ", tx=" + tx.xid() + ", 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().id() + ", tx=" + tx.xid() + ", entries=" + entries + ']');
                cache.putAll(entries);
                tx.commit();
            } catch (Throwable e) {
                log.info("Expected exception: " + e);
                e.printStackTrace(System.out);
                // 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))) {
                        log.info("At least one stack trace should contain " + TransactionDeadlockException.class.getSimpleName());
                        e.printStackTrace(System.out);
                    }
                }
            }
        }
    }, 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("Failed to detect deadlock", 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) List(java.util.List) ArrayList(java.util.ArrayList) 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) Map(java.util.Map) GridCacheConcurrentMap(org.apache.ignite.internal.processors.cache.GridCacheConcurrentMap) HashMap(java.util.HashMap)

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