Search in sources :

Example 1 with TransactionDuplicateKeyException

use of org.apache.ignite.transactions.TransactionDuplicateKeyException in project ignite by apache.

the class DmlUtils method dmlDoInsert.

/**
 * Execute INSERT statement plan.
 * @param cursor Cursor to take inserted data from.
 * @param pageSize Batch size for streaming, anything <= 0 for single page operations.
 * @return Number of items affected.
 * @throws IgniteCheckedException if failed, particularly in case of duplicate keys.
 */
@SuppressWarnings({ "unchecked" })
private static long dmlDoInsert(UpdatePlan plan, Iterable<List<?>> cursor, int pageSize) throws IgniteCheckedException {
    GridCacheContext cctx = plan.cacheContext();
    // If we have just one item to put, just do so
    if (plan.rowCount() == 1) {
        IgniteBiTuple t = plan.processRow(cursor.iterator().next());
        try (MTC.TraceSurroundings ignored = MTC.support(cctx.kernalContext().tracing().create(SQL_CACHE_UPDATE, MTC.span()).addTag(SQL_CACHE_UPDATES, () -> "1"))) {
            if (cctx.cache().putIfAbsent(t.getKey(), t.getValue()))
                return 1;
            else
                throw new TransactionDuplicateKeyException("Duplicate key during INSERT [key=" + t.getKey() + ']');
        }
    } else {
        // Keys that failed to INSERT due to duplication.
        DmlBatchSender sender = new DmlBatchSender(cctx, pageSize, 1);
        for (List<?> row : cursor) {
            final IgniteBiTuple keyValPair = plan.processRow(row);
            sender.add(keyValPair.getKey(), new DmlStatementsProcessor.InsertEntryProcessor(keyValPair.getValue()), 0);
        }
        sender.flush();
        SQLException resEx = sender.error();
        if (!F.isEmpty(sender.failedKeys())) {
            String msg = "Failed to INSERT some keys because they are already in cache " + "[keys=" + sender.failedKeys() + ']';
            SQLException dupEx = new SQLException(msg, SqlStateCode.CONSTRAINT_VIOLATION);
            if (resEx == null)
                resEx = dupEx;
            else
                resEx.setNextException(dupEx);
        }
        if (resEx != null)
            throw new IgniteSQLException(resEx);
        return sender.updateCount();
    }
}
Also used : TransactionDuplicateKeyException(org.apache.ignite.transactions.TransactionDuplicateKeyException) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) SQLException(java.sql.SQLException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) DmlStatementsProcessor(org.apache.ignite.internal.processors.query.h2.DmlStatementsProcessor) MTC(org.apache.ignite.internal.processors.tracing.MTC)

Example 2 with TransactionDuplicateKeyException

use of org.apache.ignite.transactions.TransactionDuplicateKeyException in project ignite by apache.

the class IgniteUtils method exceptionConverters.

/**
 * Gets map with converters to convert internal checked exceptions to public API unchecked exceptions.
 *
 * @return Exception converters.
 */
private static Map<Class<? extends IgniteCheckedException>, C1<IgniteCheckedException, IgniteException>> exceptionConverters() {
    Map<Class<? extends IgniteCheckedException>, C1<IgniteCheckedException, IgniteException>> m = new HashMap<>();
    m.put(IgniteInterruptedCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            return new IgniteInterruptedException(e.getMessage(), (InterruptedException) e.getCause());
        }
    });
    m.put(IgniteFutureCancelledCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            return new IgniteFutureCancelledException(e.getMessage(), e);
        }
    });
    m.put(IgniteFutureTimeoutCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            return new IgniteFutureTimeoutException(e.getMessage(), e);
        }
    });
    m.put(ClusterGroupEmptyCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            return new ClusterGroupEmptyException(e.getMessage(), e);
        }
    });
    m.put(ClusterTopologyCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            ClusterTopologyException topEx = new ClusterTopologyException(e.getMessage(), e);
            ClusterTopologyCheckedException checked = (ClusterTopologyCheckedException) e;
            if (checked.retryReadyFuture() != null)
                topEx.retryReadyFuture(new IgniteFutureImpl<>(checked.retryReadyFuture()));
            return topEx;
        }
    });
    m.put(IgniteDeploymentCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            return new IgniteDeploymentException(e.getMessage(), e);
        }
    });
    m.put(ComputeTaskTimeoutCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            return new ComputeTaskTimeoutException(e.getMessage(), e);
        }
    });
    m.put(ComputeTaskCancelledCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            return new ComputeTaskCancelledException(e.getMessage(), e);
        }
    });
    m.put(IgniteTxRollbackCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            return new TransactionRollbackException(e.getMessage(), e);
        }
    });
    m.put(IgniteTxHeuristicCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            return new TransactionHeuristicException(e.getMessage(), e);
        }
    });
    m.put(IgniteTxTimeoutCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            if (e.getCause() instanceof TransactionDeadlockException)
                return new TransactionTimeoutException(e.getMessage(), e.getCause());
            return new TransactionTimeoutException(e.getMessage(), e);
        }
    });
    m.put(IgniteTxOptimisticCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            return new TransactionOptimisticException(e.getMessage(), e);
        }
    });
    m.put(IgniteClientDisconnectedCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            return new IgniteClientDisconnectedException(((IgniteClientDisconnectedCheckedException) e).reconnectFuture(), e.getMessage(), e);
        }
    });
    m.put(IgniteTxSerializationCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            return new TransactionSerializationException(e.getMessage(), e);
        }
    });
    m.put(IgniteTxDuplicateKeyCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            return new TransactionDuplicateKeyException(e.getMessage(), e);
        }
    });
    m.put(IgniteTxAlreadyCompletedCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            return new TransactionAlreadyCompletedException(e.getMessage(), e);
        }
    });
    return m;
}
Also used : TransactionDeadlockException(org.apache.ignite.transactions.TransactionDeadlockException) TransactionDuplicateKeyException(org.apache.ignite.transactions.TransactionDuplicateKeyException) TransactionSerializationException(org.apache.ignite.transactions.TransactionSerializationException) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) IgniteDeploymentException(org.apache.ignite.IgniteDeploymentException) ClusterGroupEmptyException(org.apache.ignite.cluster.ClusterGroupEmptyException) TransactionRollbackException(org.apache.ignite.transactions.TransactionRollbackException) TransactionHeuristicException(org.apache.ignite.transactions.TransactionHeuristicException) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) C1(org.apache.ignite.internal.util.typedef.C1) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) ComputeTaskTimeoutException(org.apache.ignite.compute.ComputeTaskTimeoutException) TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) TransactionAlreadyCompletedException(org.apache.ignite.transactions.TransactionAlreadyCompletedException) ComputeTaskCancelledException(org.apache.ignite.compute.ComputeTaskCancelledException) IgniteFutureTimeoutException(org.apache.ignite.lang.IgniteFutureTimeoutException) TransactionTimeoutException(org.apache.ignite.transactions.TransactionTimeoutException) IgniteClientDisconnectedCheckedException(org.apache.ignite.internal.IgniteClientDisconnectedCheckedException) ClusterTopologyException(org.apache.ignite.cluster.ClusterTopologyException) IgniteFutureCancelledException(org.apache.ignite.lang.IgniteFutureCancelledException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Aggregations

TransactionDuplicateKeyException (org.apache.ignite.transactions.TransactionDuplicateKeyException)2 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1 IdentityHashMap (java.util.IdentityHashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 IgniteClientDisconnectedException (org.apache.ignite.IgniteClientDisconnectedException)1 IgniteDeploymentException (org.apache.ignite.IgniteDeploymentException)1 IgniteException (org.apache.ignite.IgniteException)1 IgniteInterruptedException (org.apache.ignite.IgniteInterruptedException)1 ClusterGroupEmptyException (org.apache.ignite.cluster.ClusterGroupEmptyException)1 ClusterTopologyException (org.apache.ignite.cluster.ClusterTopologyException)1 ComputeTaskCancelledException (org.apache.ignite.compute.ComputeTaskCancelledException)1 ComputeTaskTimeoutException (org.apache.ignite.compute.ComputeTaskTimeoutException)1 IgniteClientDisconnectedCheckedException (org.apache.ignite.internal.IgniteClientDisconnectedCheckedException)1 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)1 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)1 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)1 DmlStatementsProcessor (org.apache.ignite.internal.processors.query.h2.DmlStatementsProcessor)1