Search in sources :

Example 1 with ChangeGlobalStateMessage

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

the class GridClusterStateProcessor method start.

/** {@inheritDoc} */
@Override
public void start(boolean activeOnStart) throws IgniteCheckedException {
    super.start(activeOnStart);
    globalState = activeOnStart ? ACTIVE : INACTIVE;
    cacheProc = ctx.cache();
    sharedCtx = cacheProc.context();
    sharedCtx.io().addHandler(0, GridChangeGlobalStateMessageResponse.class, new CI2<UUID, GridChangeGlobalStateMessageResponse>() {

        @Override
        public void apply(UUID nodeId, GridChangeGlobalStateMessageResponse msg) {
            processChangeGlobalStateResponse(nodeId, msg);
        }
    });
    ctx.discovery().setCustomEventListener(ChangeGlobalStateMessage.class, new CustomEventListener<ChangeGlobalStateMessage>() {

        @Override
        public void onCustomEvent(AffinityTopologyVersion topVer, ClusterNode snd, ChangeGlobalStateMessage msg) {
            assert topVer != null;
            assert snd != null;
            assert msg != null;
            boolean activate = msg.activate();
            ChangeGlobalStateContext actx = lastCgsCtx;
            if (actx != null && globalState == TRANSITION) {
                GridChangeGlobalStateFuture f = cgsLocFut.get();
                if (log.isDebugEnabled())
                    log.debug("Concurrent " + prettyStr(activate) + " [id=" + ctx.localNodeId() + " topVer=" + topVer + " actx=" + actx + ", msg=" + msg + "]");
                if (f != null && f.requestId.equals(msg.requestId()))
                    f.onDone(new IgniteCheckedException("Concurrent change state, now in progress=" + (activate) + ", initiatingNodeId=" + actx.initiatingNodeId + ", you try=" + (prettyStr(activate)) + ", locNodeId=" + ctx.localNodeId()));
                msg.concurrentChangeState();
            } else {
                if (log.isInfoEnabled())
                    log.info("Create " + prettyStr(activate) + " context [id=" + ctx.localNodeId() + " topVer=" + topVer + ", reqId=" + msg.requestId() + ", initiatingNodeId=" + msg.initiatorNodeId() + "]");
                lastCgsCtx = new ChangeGlobalStateContext(msg.requestId(), msg.initiatorNodeId(), msg.getDynamicCacheChangeBatch(), msg.activate());
                globalState = TRANSITION;
            }
        }
    });
    ctx.event().addLocalEventListener(lsr, EVT_NODE_LEFT, EVT_NODE_FAILED);
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) ChangeGlobalStateMessage(org.apache.ignite.internal.processors.cache.ChangeGlobalStateMessage) UUID(java.util.UUID) GridChangeGlobalStateMessageResponse(org.apache.ignite.internal.processors.cache.GridChangeGlobalStateMessageResponse)

Example 2 with ChangeGlobalStateMessage

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

the class GridClusterStateProcessor method changeGlobalState.

/**
     *
     */
public IgniteInternalFuture<?> changeGlobalState(final boolean activate) {
    if (cacheProc.transactions().tx() != null || sharedCtx.lockedTopologyVersion(null) != null)
        throw new IgniteException("Cannot " + prettyStr(activate) + " cluster, because cache locked on transaction.");
    if ((globalState == ACTIVE && activate) || (this.globalState == INACTIVE && !activate))
        return new GridFinishedFuture<>();
    final UUID requestId = UUID.randomUUID();
    final GridChangeGlobalStateFuture cgsFut = new GridChangeGlobalStateFuture(requestId, activate, ctx);
    if (!cgsLocFut.compareAndSet(null, cgsFut)) {
        GridChangeGlobalStateFuture locF = cgsLocFut.get();
        if (locF.activate == activate)
            return locF;
        else
            return new GridFinishedFuture<>(new IgniteException("fail " + prettyStr(activate) + ", because now in progress" + prettyStr(locF.activate)));
    }
    try {
        if (ctx.clientNode()) {
            AffinityTopologyVersion topVer = ctx.discovery().topologyVersionEx();
            IgniteCompute comp = ((ClusterGroupAdapter) ctx.cluster().get().forServers()).compute().withAsync();
            if (log.isInfoEnabled())
                log.info("Send " + prettyStr(activate) + " request from client node [id=" + ctx.localNodeId() + " topVer=" + topVer + " ]");
            comp.run(new ClientChangeGlobalStateComputeRequest(activate));
            comp.future().listen(new CI1<IgniteFuture>() {

                @Override
                public void apply(IgniteFuture fut) {
                    try {
                        fut.get();
                        cgsFut.onDone();
                    } catch (Exception e) {
                        cgsFut.onDone(e);
                    }
                }
            });
        } else {
            List<DynamicCacheChangeRequest> reqs = new ArrayList<>();
            DynamicCacheChangeRequest changeGlobalStateReq = new DynamicCacheChangeRequest(requestId, activate ? ACTIVE : INACTIVE, ctx.localNodeId());
            reqs.add(changeGlobalStateReq);
            reqs.addAll(activate ? cacheProc.startAllCachesRequests() : cacheProc.stopAllCachesRequests());
            ChangeGlobalStateMessage changeGlobalStateMsg = new ChangeGlobalStateMessage(requestId, ctx.localNodeId(), activate, new DynamicCacheChangeBatch(reqs));
            try {
                ctx.discovery().sendCustomEvent(changeGlobalStateMsg);
                if (ctx.isStopping())
                    cgsFut.onDone(new IgniteCheckedException("Failed to execute " + prettyStr(activate) + " request, " + "node is stopping."));
            } catch (IgniteCheckedException e) {
                log.error("Fail create or send change global state request." + cgsFut, e);
                cgsFut.onDone(e);
            }
        }
    } catch (IgniteCheckedException e) {
        log.error("Fail create or send change global state request." + cgsFut, e);
        cgsFut.onDone(e);
    }
    return cgsFut;
}
Also used : DynamicCacheChangeRequest(org.apache.ignite.internal.processors.cache.DynamicCacheChangeRequest) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) ArrayList(java.util.ArrayList) ChangeGlobalStateMessage(org.apache.ignite.internal.processors.cache.ChangeGlobalStateMessage) IgniteFuture(org.apache.ignite.lang.IgniteFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) DynamicCacheChangeBatch(org.apache.ignite.internal.processors.cache.DynamicCacheChangeBatch) UUID(java.util.UUID) IgniteCompute(org.apache.ignite.IgniteCompute)

Aggregations

UUID (java.util.UUID)2 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)2 ChangeGlobalStateMessage (org.apache.ignite.internal.processors.cache.ChangeGlobalStateMessage)2 ArrayList (java.util.ArrayList)1 IgniteCompute (org.apache.ignite.IgniteCompute)1 IgniteException (org.apache.ignite.IgniteException)1 ClusterNode (org.apache.ignite.cluster.ClusterNode)1 IgniteInterruptedCheckedException (org.apache.ignite.internal.IgniteInterruptedCheckedException)1 DynamicCacheChangeBatch (org.apache.ignite.internal.processors.cache.DynamicCacheChangeBatch)1 DynamicCacheChangeRequest (org.apache.ignite.internal.processors.cache.DynamicCacheChangeRequest)1 GridChangeGlobalStateMessageResponse (org.apache.ignite.internal.processors.cache.GridChangeGlobalStateMessageResponse)1 IgniteFuture (org.apache.ignite.lang.IgniteFuture)1