Search in sources :

Example 1 with MvccQueryTracker

use of org.apache.ignite.internal.processors.cache.mvcc.MvccQueryTracker in project ignite by apache.

the class GridDhtColocatedCache method getAllAsync.

/**
 * {@inheritDoc}
 */
@Override
public IgniteInternalFuture<Map<K, V>> getAllAsync(@Nullable final Collection<? extends K> keys, boolean forcePrimary, boolean skipTx, String taskName, final boolean deserializeBinary, final boolean recovery, final ReadRepairStrategy readRepairStrategy, final boolean skipVals, final boolean needVer) {
    ctx.checkSecurity(SecurityPermission.CACHE_READ);
    if (F.isEmpty(keys))
        return new GridFinishedFuture<>(Collections.<K, V>emptyMap());
    warnIfUnordered(keys, BulkOperation.GET);
    GridNearTxLocal tx = checkCurrentTx();
    final CacheOperationContext opCtx = ctx.operationContextPerCall();
    if (!ctx.mvccEnabled() && tx != null && !tx.implicit() && !skipTx) {
        return asyncOp(tx, new AsyncOp<Map<K, V>>(keys) {

            /**
             * {@inheritDoc}
             */
            @Override
            public IgniteInternalFuture<Map<K, V>> op(GridNearTxLocal tx, AffinityTopologyVersion readyTopVer) {
                return tx.getAllAsync(ctx, readyTopVer, ctx.cacheKeysView(keys), deserializeBinary, skipVals, false, opCtx != null && opCtx.skipStore(), recovery, readRepairStrategy, needVer);
            }
        }, opCtx, /*retry*/
        false);
    }
    MvccSnapshot mvccSnapshot = null;
    MvccQueryTracker mvccTracker = null;
    if (ctx.mvccEnabled()) {
        try {
            if (tx != null)
                mvccSnapshot = MvccUtils.requestSnapshot(tx);
            else {
                mvccTracker = MvccUtils.mvccTracker(ctx, null);
                mvccSnapshot = mvccTracker.snapshot();
            }
            assert mvccSnapshot != null;
        } catch (IgniteCheckedException ex) {
            return new GridFinishedFuture<>(ex);
        }
    }
    AffinityTopologyVersion topVer;
    if (tx != null)
        topVer = tx.topologyVersion();
    else if (mvccTracker != null)
        topVer = mvccTracker.topologyVersion();
    else
        topVer = ctx.affinity().affinityTopologyVersion();
    if (readRepairStrategy != null) {
        return new GridNearReadRepairCheckOnlyFuture(ctx, ctx.cacheKeysView(keys), readRepairStrategy, opCtx == null || !opCtx.skipStore(), taskName, deserializeBinary, recovery, skipVals ? null : expiryPolicy(opCtx != null ? opCtx.expiry() : null), skipVals, needVer, false, tx).multi();
    }
    IgniteInternalFuture<Map<K, V>> fut = loadAsync(ctx.cacheKeysView(keys), opCtx == null || !opCtx.skipStore(), forcePrimary, topVer, taskName, deserializeBinary, recovery, skipVals ? null : expiryPolicy(opCtx != null ? opCtx.expiry() : null), skipVals, needVer, false, null, mvccSnapshot);
    if (mvccTracker != null) {
        final MvccQueryTracker mvccTracker0 = mvccTracker;
        fut.listen(new CI1<IgniteInternalFuture<Map<K, V>>>() {

            /**
             * {@inheritDoc}
             */
            @Override
            public void apply(IgniteInternalFuture<Map<K, V>> future) {
                if (future.isDone())
                    mvccTracker0.onDone();
            }
        });
    }
    return fut;
}
Also used : MvccSnapshot(org.apache.ignite.internal.processors.cache.mvcc.MvccSnapshot) CacheOperationContext(org.apache.ignite.internal.processors.cache.CacheOperationContext) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) MvccQueryTracker(org.apache.ignite.internal.processors.cache.mvcc.MvccQueryTracker) GridNearReadRepairCheckOnlyFuture(org.apache.ignite.internal.processors.cache.distributed.near.consistency.GridNearReadRepairCheckOnlyFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Map(java.util.Map) GridCacheConcurrentMap(org.apache.ignite.internal.processors.cache.GridCacheConcurrentMap)

Example 2 with MvccQueryTracker

use of org.apache.ignite.internal.processors.cache.mvcc.MvccQueryTracker in project ignite by apache.

the class ValidateIndexesClosure method mvccSession.

/**
 * Get session with MVCC snapshot and QueryContext.
 *
 * @param cctx Cache context.
 * @return Session with QueryContext and MVCC snapshot.
 * @throws IgniteCheckedException If failed.
 */
private Session mvccSession(GridCacheContext<?, ?> cctx) throws IgniteCheckedException {
    Session session = null;
    boolean mvccEnabled = cctx.mvccEnabled();
    if (mvccEnabled) {
        ConnectionManager connMgr = ((IgniteH2Indexing) ignite.context().query().getIndexing()).connections();
        JdbcConnection connection = (JdbcConnection) connMgr.connection().connection();
        session = (Session) connection.getSession();
        MvccQueryTracker tracker = MvccUtils.mvccTracker(cctx, true);
        MvccSnapshot mvccSnapshot = tracker.snapshot();
        final QueryContext qctx = new QueryContext(0, cacheName -> null, null, mvccSnapshot, null, true);
        session.setVariable(H2Utils.QCTX_VARIABLE_NAME, new H2Utils.ValueRuntimeSimpleObject<>(qctx));
    }
    return session;
}
Also used : H2Utils(org.apache.ignite.internal.processors.query.h2.H2Utils) MvccQueryTracker(org.apache.ignite.internal.processors.cache.mvcc.MvccQueryTracker) MvccSnapshot(org.apache.ignite.internal.processors.cache.mvcc.MvccSnapshot) ConnectionManager(org.apache.ignite.internal.processors.query.h2.ConnectionManager) JdbcConnection(org.h2.jdbc.JdbcConnection) QueryContext(org.apache.ignite.internal.processors.query.h2.opt.QueryContext) IgniteH2Indexing(org.apache.ignite.internal.processors.query.h2.IgniteH2Indexing) Session(org.h2.engine.Session)

Example 3 with MvccQueryTracker

use of org.apache.ignite.internal.processors.cache.mvcc.MvccQueryTracker in project ignite by apache.

the class IgniteH2Indexing method executeSelect.

/**
 * Execute an all-ready {@link SqlFieldsQuery}.
 *
 * @param qryDesc Plan key.
 * @param qryParams Parameters.
 * @param select Select.
 * @param keepBinary Whether binary objects must not be deserialized automatically.
 * @param cancel Query cancel state holder.
 * @return Query result.
 */
private List<? extends FieldsQueryCursor<List<?>>> executeSelect(QueryDescriptor qryDesc, QueryParameters qryParams, QueryParserResultSelect select, boolean keepBinary, GridQueryCancel cancel) {
    assert cancel != null;
    // Register query.
    long qryId = registerRunningQuery(qryDesc, qryParams, cancel, select.statement());
    try (TraceSurroundings ignored = MTC.support(ctx.tracing().create(SQL_CURSOR_OPEN, MTC.span()))) {
        GridNearTxLocal tx = null;
        MvccQueryTracker tracker = null;
        GridCacheContext mvccCctx = null;
        boolean inTx = false;
        if (select.mvccEnabled()) {
            mvccCctx = ctx.cache().context().cacheContext(select.mvccCacheId());
            if (mvccCctx == null)
                throw new IgniteCheckedException("Cache has been stopped concurrently [cacheId=" + select.mvccCacheId() + ']');
            boolean autoStartTx = !qryParams.autoCommit() && tx(ctx) == null;
            // Start new user tx in case of autocommit == false.
            if (autoStartTx)
                txStart(ctx, qryParams.timeout());
            tx = tx(ctx);
            checkActive(tx);
            inTx = tx != null;
            tracker = MvccUtils.mvccTracker(mvccCctx, tx);
        }
        int timeout = operationTimeout(qryParams.timeout(), tx);
        Iterable<List<?>> iter = executeSelect0(qryId, qryDesc, qryParams, select, keepBinary, tracker, cancel, inTx, timeout);
        // Execute SELECT FOR UPDATE if needed.
        if (select.forUpdate() && inTx)
            iter = lockSelectedRows(iter, mvccCctx, timeout, qryParams.pageSize());
        RegisteredQueryCursor<List<?>> cursor = new RegisteredQueryCursor<>(iter, cancel, runningQueryManager(), qryParams.lazy(), qryId, ctx.tracing());
        cancel.add(cursor::cancel);
        cursor.fieldsMeta(select.meta());
        cursor.partitionResult(select.twoStepQuery() != null ? select.twoStepQuery().derivedPartitions() : null);
        return singletonList(cursor);
    } catch (Exception e) {
        runningQryMgr.unregister(qryId, e);
        if (e instanceof IgniteCheckedException)
            throw U.convertException((IgniteCheckedException) e);
        if (e instanceof RuntimeException)
            throw (RuntimeException) e;
        throw new IgniteSQLException("Failed to execute SELECT statement: " + qryDesc.sql(), e);
    }
}
Also used : GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal) TraceSurroundings(org.apache.ignite.internal.processors.tracing.MTC.TraceSurroundings) QueryCancelledException(org.apache.ignite.cache.query.QueryCancelledException) BatchUpdateException(java.sql.BatchUpdateException) ClusterTopologyServerNotFoundException(org.apache.ignite.internal.cluster.ClusterTopologyServerNotFoundException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteClusterReadOnlyException(org.apache.ignite.internal.processors.cache.distributed.dht.IgniteClusterReadOnlyException) CacheServerNotFoundException(org.apache.ignite.cache.CacheServerNotFoundException) SQLException(java.sql.SQLException) IgniteException(org.apache.ignite.IgniteException) CacheException(javax.cache.CacheException) StaticMvccQueryTracker(org.apache.ignite.internal.processors.cache.mvcc.StaticMvccQueryTracker) MvccQueryTracker(org.apache.ignite.internal.processors.cache.mvcc.MvccQueryTracker) RegisteredQueryCursor(org.apache.ignite.internal.processors.cache.query.RegisteredQueryCursor) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with MvccQueryTracker

use of org.apache.ignite.internal.processors.cache.mvcc.MvccQueryTracker in project ignite by apache.

the class GridCacheQueryAdapter method executeScanQuery.

/**
 * {@inheritDoc}
 */
@Override
public GridCloseableIterator executeScanQuery() throws IgniteCheckedException {
    assert type == SCAN : "Wrong processing of query: " + type;
    if (!cctx.isLocal()) {
        GridDhtCacheAdapter<?, ?> cacheAdapter = cctx.isNear() ? cctx.near().dht() : cctx.dht();
        Set<Integer> lostParts = cacheAdapter.topology().lostPartitions();
        if (!lostParts.isEmpty()) {
            if (part == null || lostParts.contains(part)) {
                throw new CacheException(new CacheInvalidStateException("Failed to execute query because cache partition " + "has been lostParts [cacheName=" + cctx.name() + ", part=" + (part == null ? lostParts.iterator().next() : part) + ']'));
            }
        }
    }
    // Affinity nodes snapshot.
    Collection<ClusterNode> nodes = new ArrayList<>(nodes());
    cctx.checkSecurity(SecurityPermission.CACHE_READ);
    if (nodes.isEmpty()) {
        if (part != null) {
            if (forceLocal) {
                throw new IgniteCheckedException("No queryable nodes for partition " + part + " [forced local query=" + this + "]");
            }
        }
        return new GridEmptyCloseableIterator();
    }
    if (log.isDebugEnabled())
        log.debug("Executing query [query=" + this + ", nodes=" + nodes + ']');
    if (cctx.deploymentEnabled())
        cctx.deploy().registerClasses(filter);
    taskHash = cctx.kernalContext().job().currentTaskNameHash();
    final GridCacheQueryManager qryMgr = cctx.queries();
    MvccQueryTracker mvccTracker = null;
    if (cctx.mvccEnabled() && mvccSnapshot == null) {
        GridNearTxLocal tx = cctx.tm().userTx();
        if (tx != null)
            mvccSnapshot = MvccUtils.requestSnapshot(tx);
        else {
            mvccTracker = MvccUtils.mvccTracker(cctx, null);
            mvccSnapshot = mvccTracker.snapshot();
        }
        assert mvccSnapshot != null;
    }
    boolean loc = nodes.size() == 1 && F.first(nodes).id().equals(cctx.localNodeId());
    GridCloseableIterator it;
    if (loc)
        it = qryMgr.scanQueryLocal(this, true);
    else if (part != null)
        it = new ScanQueryFallbackClosableIterator(part, this, qryMgr, cctx);
    else
        it = qryMgr.scanQueryDistributed(this, nodes);
    return mvccTracker != null ? new MvccTrackingIterator(it, mvccTracker) : it;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridCloseableIterator(org.apache.ignite.internal.util.lang.GridCloseableIterator) CacheException(javax.cache.CacheException) ArrayList(java.util.ArrayList) GridEmptyCloseableIterator(org.apache.ignite.internal.util.GridEmptyCloseableIterator) GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal) MvccQueryTracker(org.apache.ignite.internal.processors.cache.mvcc.MvccQueryTracker) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheInvalidStateException(org.apache.ignite.internal.processors.cache.CacheInvalidStateException)

Example 5 with MvccQueryTracker

use of org.apache.ignite.internal.processors.cache.mvcc.MvccQueryTracker in project ignite by apache.

the class GridDhtColocatedCache method getAsync.

/**
 * {@inheritDoc}
 */
@Override
protected IgniteInternalFuture<V> getAsync(final K key, boolean forcePrimary, boolean skipTx, String taskName, final boolean deserializeBinary, final boolean skipVals, final boolean needVer) {
    ctx.checkSecurity(SecurityPermission.CACHE_READ);
    GridNearTxLocal tx = checkCurrentTx();
    final CacheOperationContext opCtx = ctx.operationContextPerCall();
    final boolean recovery = opCtx != null && opCtx.recovery();
    final ReadRepairStrategy readRepairStrategy = opCtx != null ? opCtx.readRepairStrategy() : null;
    // Get operation bypass Tx in Mvcc mode.
    if (!ctx.mvccEnabled() && tx != null && !tx.implicit() && !skipTx) {
        return asyncOp(tx, new AsyncOp<V>() {

            @Override
            public IgniteInternalFuture<V> op(GridNearTxLocal tx, AffinityTopologyVersion readyTopVer) {
                IgniteInternalFuture<Map<Object, Object>> fut = tx.getAllAsync(ctx, readyTopVer, Collections.singleton(ctx.toCacheKeyObject(key)), deserializeBinary, skipVals, false, opCtx != null && opCtx.skipStore(), recovery, readRepairStrategy, needVer);
                return fut.chain(new CX1<IgniteInternalFuture<Map<Object, Object>>, V>() {

                    @Override
                    public V applyx(IgniteInternalFuture<Map<Object, Object>> e) throws IgniteCheckedException {
                        Map<Object, Object> map = e.get();
                        assert map.isEmpty() || map.size() == 1 : map.size();
                        if (skipVals) {
                            Boolean val = map.isEmpty() ? false : (Boolean) F.firstValue(map);
                            return (V) (val);
                        }
                        return (V) F.firstValue(map);
                    }
                });
            }
        }, opCtx, /*retry*/
        false);
    }
    MvccSnapshot mvccSnapshot = null;
    MvccQueryTracker mvccTracker = null;
    if (ctx.mvccEnabled()) {
        try {
            if (tx != null)
                mvccSnapshot = MvccUtils.requestSnapshot(tx);
            else {
                mvccTracker = MvccUtils.mvccTracker(ctx, null);
                mvccSnapshot = mvccTracker.snapshot();
            }
            assert mvccSnapshot != null;
        } catch (IgniteCheckedException ex) {
            return new GridFinishedFuture<>(ex);
        }
    }
    AffinityTopologyVersion topVer;
    if (tx != null)
        topVer = tx.topologyVersion();
    else if (mvccTracker != null)
        topVer = mvccTracker.topologyVersion();
    else
        topVer = ctx.affinity().affinityTopologyVersion();
    if (readRepairStrategy != null) {
        return new GridNearReadRepairCheckOnlyFuture(ctx, Collections.singleton(ctx.toCacheKeyObject(key)), readRepairStrategy, opCtx == null || !opCtx.skipStore(), taskName, deserializeBinary, recovery, skipVals ? null : expiryPolicy(opCtx != null ? opCtx.expiry() : null), skipVals, needVer, false, tx).single();
    }
    GridPartitionedSingleGetFuture fut = new GridPartitionedSingleGetFuture(ctx, ctx.toCacheKeyObject(key), topVer, opCtx == null || !opCtx.skipStore(), forcePrimary, taskName, deserializeBinary, skipVals ? null : expiryPolicy(opCtx != null ? opCtx.expiry() : null), skipVals, needVer, /*keepCacheObjects*/
    false, opCtx != null && opCtx.recovery(), null, mvccSnapshot);
    fut.init();
    if (mvccTracker != null) {
        final MvccQueryTracker mvccTracker0 = mvccTracker;
        fut.listen(new CI1<IgniteInternalFuture<Object>>() {

            @Override
            public void apply(IgniteInternalFuture<Object> future) {
                if (future.isDone())
                    mvccTracker0.onDone();
            }
        });
    }
    return (IgniteInternalFuture<V>) fut;
}
Also used : MvccSnapshot(org.apache.ignite.internal.processors.cache.mvcc.MvccSnapshot) GridPartitionedSingleGetFuture(org.apache.ignite.internal.processors.cache.distributed.dht.GridPartitionedSingleGetFuture) CacheOperationContext(org.apache.ignite.internal.processors.cache.CacheOperationContext) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) ReadRepairStrategy(org.apache.ignite.cache.ReadRepairStrategy) MvccQueryTracker(org.apache.ignite.internal.processors.cache.mvcc.MvccQueryTracker) GridNearReadRepairCheckOnlyFuture(org.apache.ignite.internal.processors.cache.distributed.near.consistency.GridNearReadRepairCheckOnlyFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) CX1(org.apache.ignite.internal.util.typedef.CX1) Map(java.util.Map) GridCacheConcurrentMap(org.apache.ignite.internal.processors.cache.GridCacheConcurrentMap)

Aggregations

MvccQueryTracker (org.apache.ignite.internal.processors.cache.mvcc.MvccQueryTracker)5 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)4 GridNearTxLocal (org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal)4 MvccSnapshot (org.apache.ignite.internal.processors.cache.mvcc.MvccSnapshot)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 CacheException (javax.cache.CacheException)2 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)2 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)2 CacheOperationContext (org.apache.ignite.internal.processors.cache.CacheOperationContext)2 GridCacheConcurrentMap (org.apache.ignite.internal.processors.cache.GridCacheConcurrentMap)2 GridNearReadRepairCheckOnlyFuture (org.apache.ignite.internal.processors.cache.distributed.near.consistency.GridNearReadRepairCheckOnlyFuture)2 BatchUpdateException (java.sql.BatchUpdateException)1 SQLException (java.sql.SQLException)1 Collections.singletonList (java.util.Collections.singletonList)1 List (java.util.List)1 IgniteException (org.apache.ignite.IgniteException)1 CacheServerNotFoundException (org.apache.ignite.cache.CacheServerNotFoundException)1 ReadRepairStrategy (org.apache.ignite.cache.ReadRepairStrategy)1 QueryCancelledException (org.apache.ignite.cache.query.QueryCancelledException)1