Search in sources :

Example 71 with GridCacheContext

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

the class IgniteH2Indexing method createSortedIndex.

/**
     * Create sorted index.
     *
     * @param schema Schema.
     * @param name Index name,
     * @param tbl Table.
     * @param pk Primary key flag.
     * @param cols Columns.
     * @return Index.
     */
public GridH2IndexBase createSortedIndex(H2Schema schema, String name, GridH2Table tbl, boolean pk, List<IndexColumn> cols, int inlineSize) {
    try {
        GridCacheContext cctx = tbl.cache();
        if (log.isDebugEnabled())
            log.debug("Creating cache index [cacheId=" + cctx.cacheId() + ", idxName=" + name + ']');
        final int segments = tbl.rowDescriptor().context().config().getQueryParallelism();
        return new H2TreeIndex(cctx, tbl, name, pk, cols, inlineSize, segments);
    } catch (IgniteCheckedException e) {
        throw new IgniteException(e);
    }
}
Also used : H2TreeIndex(org.apache.ignite.internal.processors.query.h2.database.H2TreeIndex) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException)

Example 72 with GridCacheContext

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

the class DmlStatementsProcessor method doFastUpdate.

/**
     * Perform single cache operation based on given args.
     * @param args Query parameters.
     * @return 1 if an item was affected, 0 otherwise.
     * @throws IgniteCheckedException if failed.
     */
@SuppressWarnings({ "unchecked", "ConstantConditions" })
private static UpdateResult doFastUpdate(UpdatePlan plan, Object[] args) throws IgniteCheckedException {
    GridCacheContext cctx = plan.tbl.rowDescriptor().context();
    FastUpdateArguments singleUpdate = plan.fastUpdateArgs;
    assert singleUpdate != null;
    boolean valBounded = (singleUpdate.val != FastUpdateArguments.NULL_ARGUMENT);
    if (singleUpdate.newVal != FastUpdateArguments.NULL_ARGUMENT) {
        // Single item UPDATE
        Object key = singleUpdate.key.apply(args);
        Object newVal = singleUpdate.newVal.apply(args);
        if (valBounded) {
            Object val = singleUpdate.val.apply(args);
            return (cctx.cache().replace(key, val, newVal) ? UpdateResult.ONE : UpdateResult.ZERO);
        } else
            return (cctx.cache().replace(key, newVal) ? UpdateResult.ONE : UpdateResult.ZERO);
    } else {
        // Single item DELETE
        Object key = singleUpdate.key.apply(args);
        Object val = singleUpdate.val.apply(args);
        if (// No _val bound in source query
        singleUpdate.val == FastUpdateArguments.NULL_ARGUMENT)
            return cctx.cache().remove(key) ? UpdateResult.ONE : UpdateResult.ZERO;
        else
            return cctx.cache().remove(key, val) ? UpdateResult.ONE : UpdateResult.ZERO;
    }
}
Also used : GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) BinaryObject(org.apache.ignite.binary.BinaryObject) FastUpdateArguments(org.apache.ignite.internal.processors.query.h2.dml.FastUpdateArguments)

Example 73 with GridCacheContext

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

the class GridContinuousProcessor method start.

/** {@inheritDoc} */
@Override
public void start(boolean activeOnStart) throws IgniteCheckedException {
    if (ctx.config().isDaemon())
        return;
    retryDelay = ctx.config().getNetworkSendRetryDelay();
    retryCnt = ctx.config().getNetworkSendRetryCount();
    marsh = ctx.config().getMarshaller();
    ctx.event().addLocalEventListener(new GridLocalEventListener() {

        @SuppressWarnings({ "fallthrough", "TooBroadScope" })
        @Override
        public void onEvent(Event evt) {
            assert evt instanceof DiscoveryEvent;
            assert evt.type() == EVT_NODE_LEFT || evt.type() == EVT_NODE_FAILED;
            UUID nodeId = ((DiscoveryEvent) evt).eventNode().id();
            clientInfos.remove(nodeId);
            // Unregister handlers created by left node.
            for (Map.Entry<UUID, RemoteRoutineInfo> e : rmtInfos.entrySet()) {
                UUID routineId = e.getKey();
                RemoteRoutineInfo info = e.getValue();
                if (nodeId.equals(info.nodeId)) {
                    if (info.autoUnsubscribe)
                        unregisterRemote(routineId);
                    if (info.hnd.isQuery())
                        info.hnd.onNodeLeft();
                }
            }
            for (Map.Entry<IgniteUuid, SyncMessageAckFuture> e : syncMsgFuts.entrySet()) {
                SyncMessageAckFuture fut = e.getValue();
                if (fut.nodeId().equals(nodeId)) {
                    SyncMessageAckFuture fut0 = syncMsgFuts.remove(e.getKey());
                    if (fut0 != null) {
                        ClusterTopologyCheckedException err = new ClusterTopologyCheckedException("Node left grid while sending message to: " + nodeId);
                        fut0.onDone(err);
                    }
                }
            }
        }
    }, EVT_NODE_LEFT, EVT_NODE_FAILED);
    ctx.event().addLocalEventListener(new GridLocalEventListener() {

        @Override
        public void onEvent(Event evt) {
            cancelFutures(new IgniteCheckedException("Topology segmented"));
        }
    }, EVT_NODE_SEGMENTED);
    ctx.discovery().setCustomEventListener(StartRoutineDiscoveryMessage.class, new CustomEventListener<StartRoutineDiscoveryMessage>() {

        @Override
        public void onCustomEvent(AffinityTopologyVersion topVer, ClusterNode snd, StartRoutineDiscoveryMessage msg) {
            if (!snd.id().equals(ctx.localNodeId()) && !ctx.isStopping())
                processStartRequest(snd, msg);
        }
    });
    ctx.discovery().setCustomEventListener(StartRoutineAckDiscoveryMessage.class, new CustomEventListener<StartRoutineAckDiscoveryMessage>() {

        @Override
        public void onCustomEvent(AffinityTopologyVersion topVer, ClusterNode snd, StartRoutineAckDiscoveryMessage msg) {
            StartFuture fut = startFuts.remove(msg.routineId());
            if (fut != null) {
                if (msg.errs().isEmpty()) {
                    LocalRoutineInfo routine = locInfos.get(msg.routineId());
                    // Update partition counters.
                    if (routine != null && routine.handler().isQuery()) {
                        Map<UUID, Map<Integer, T2<Long, Long>>> cntrsPerNode = msg.updateCountersPerNode();
                        Map<Integer, T2<Long, Long>> cntrs = msg.updateCounters();
                        GridCacheAdapter<Object, Object> interCache = ctx.cache().internalCache(routine.handler().cacheName());
                        GridCacheContext cctx = interCache != null ? interCache.context() : null;
                        if (cctx != null && cntrsPerNode != null && !cctx.isLocal() && cctx.affinityNode())
                            cntrsPerNode.put(ctx.localNodeId(), cctx.topology().updateCounters(false));
                        routine.handler().updateCounters(topVer, cntrsPerNode, cntrs);
                    }
                    fut.onRemoteRegistered();
                } else {
                    IgniteCheckedException firstEx = F.first(msg.errs().values());
                    fut.onDone(firstEx);
                    stopRoutine(msg.routineId());
                }
            }
        }
    });
    ctx.discovery().setCustomEventListener(StopRoutineDiscoveryMessage.class, new CustomEventListener<StopRoutineDiscoveryMessage>() {

        @Override
        public void onCustomEvent(AffinityTopologyVersion topVer, ClusterNode snd, StopRoutineDiscoveryMessage msg) {
            if (!snd.id().equals(ctx.localNodeId())) {
                UUID routineId = msg.routineId();
                unregisterRemote(routineId);
            }
            for (Map<UUID, LocalRoutineInfo> clientInfo : clientInfos.values()) {
                if (clientInfo.remove(msg.routineId()) != null)
                    break;
            }
        }
    });
    ctx.discovery().setCustomEventListener(StopRoutineAckDiscoveryMessage.class, new CustomEventListener<StopRoutineAckDiscoveryMessage>() {

        @Override
        public void onCustomEvent(AffinityTopologyVersion topVer, ClusterNode snd, StopRoutineAckDiscoveryMessage msg) {
            StopFuture fut = stopFuts.remove(msg.routineId());
            if (fut != null)
                fut.onDone();
        }
    });
    ctx.io().addMessageListener(TOPIC_CONTINUOUS, new GridMessageListener() {

        @Override
        public void onMessage(UUID nodeId, Object obj) {
            GridContinuousMessage msg = (GridContinuousMessage) obj;
            if (msg.data() == null && msg.dataBytes() != null) {
                try {
                    msg.data(U.unmarshal(marsh, msg.dataBytes(), U.resolveClassLoader(ctx.config())));
                } catch (IgniteCheckedException e) {
                    U.error(log, "Failed to process message (ignoring): " + msg, e);
                    return;
                }
            }
            switch(msg.type()) {
                case MSG_EVT_NOTIFICATION:
                    processNotification(nodeId, msg);
                    break;
                case MSG_EVT_ACK:
                    processMessageAck(msg);
                    break;
                default:
                    assert false : "Unexpected message received: " + msg.type();
            }
        }
    });
    ctx.cacheObjects().onContinuousProcessorStarted(ctx);
    ctx.service().onContinuousProcessorStarted(ctx);
    if (log.isDebugEnabled())
        log.debug("Continuous processor started.");
}
Also used : GridLocalEventListener(org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener) GridMessageListener(org.apache.ignite.internal.managers.communication.GridMessageListener) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridCacheAdapter(org.apache.ignite.internal.processors.cache.GridCacheAdapter) UUID(java.util.UUID) T2(org.apache.ignite.internal.util.typedef.T2) ClusterNode(org.apache.ignite.cluster.ClusterNode) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) AtomicLong(java.util.concurrent.atomic.AtomicLong) Event(org.apache.ignite.events.Event) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) GridTimeoutObject(org.apache.ignite.internal.processors.timeout.GridTimeoutObject) Map(java.util.Map) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Example 74 with GridCacheContext

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

the class DataStructuresProcessor method getCollection.

/**
     * @param c Closure creating collection.
     * @param dsInfo Data structure info.
     * @param create Create flag.
     * @return Collection instance.
     * @throws IgniteCheckedException If failed.
     */
@Nullable
private <T> T getCollection(final IgniteClosureX<GridCacheContext, T> c, final DataStructureInfo dsInfo, boolean create) throws IgniteCheckedException {
    awaitInitialization();
    Map<String, DataStructureInfo> dsMap = utilityCache.get(DATA_STRUCTURES_KEY);
    if (!create && (dsMap == null || !dsMap.containsKey(dsInfo.name)))
        return null;
    IgniteCheckedException err = validateDataStructure(dsMap, dsInfo, create);
    if (err != null)
        throw err;
    if (!create) {
        DataStructureInfo oldInfo = dsMap.get(dsInfo.name);
        assert oldInfo.info instanceof CollectionInfo : oldInfo.info;
        String cacheName = ((CollectionInfo) oldInfo.info).cacheName;
        GridCacheContext cacheCtx = ctx.cache().getOrStartCache(cacheName).context();
        return c.applyx(cacheCtx);
    }
    return retryTopologySafe(new IgniteOutClosureX<T>() {

        @Override
        public T applyx() throws IgniteCheckedException {
            try (GridNearTxLocal tx = utilityCache.txStartEx(PESSIMISTIC, REPEATABLE_READ)) {
                T2<String, IgniteCheckedException> res = utilityCache.invoke(DATA_STRUCTURES_KEY, new AddCollectionProcessor(dsInfo)).get();
                IgniteCheckedException err = res.get2();
                if (err != null)
                    throw err;
                String cacheName = res.get1();
                final GridCacheContext cacheCtx = ctx.cache().internalCache(cacheName).context();
                T col = c.applyx(cacheCtx);
                tx.commit();
                return col;
            }
        }
    });
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) EVT_NODE_LEFT(org.apache.ignite.events.EventType.EVT_NODE_LEFT) SET(org.apache.ignite.internal.processors.datastructures.DataStructuresProcessor.DataStructureType.SET) GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal) T2(org.apache.ignite.internal.util.typedef.T2) Nullable(org.jetbrains.annotations.Nullable)

Example 75 with GridCacheContext

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

the class MemoryPolicyInitializationTest method verifyCacheMemoryPolicy.

/**
     * @param cache Cache.
     * @param plcName Policy name.
     */
private void verifyCacheMemoryPolicy(IgniteCache cache, String plcName) {
    GridCacheContext ctx = U.field(cache, "ctx");
    assertEquals(plcName, ctx.memoryPolicy().config().getName());
}
Also used : GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext)

Aggregations

GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)147 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)37 GridCacheEntryRemovedException (org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException)22 HashMap (java.util.HashMap)21 ClusterNode (org.apache.ignite.cluster.ClusterNode)20 Map (java.util.Map)19 UUID (java.util.UUID)18 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)18 GridCacheEntryEx (org.apache.ignite.internal.processors.cache.GridCacheEntryEx)16 ArrayList (java.util.ArrayList)15 IgniteException (org.apache.ignite.IgniteException)14 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)14 List (java.util.List)13 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)13 GridCacheVersion (org.apache.ignite.internal.processors.cache.version.GridCacheVersion)13 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)11 ConcurrentMap (java.util.concurrent.ConcurrentMap)10 IgniteTxEntry (org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry)10 GridCacheEntryInfo (org.apache.ignite.internal.processors.cache.GridCacheEntryInfo)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8