Search in sources :

Example 6 with CI1

use of org.apache.ignite.internal.util.typedef.CI1 in project ignite by apache.

the class GridDiscoveryManager method start.

/**
 * {@inheritDoc}
 */
@Override
public void start() throws IgniteCheckedException {
    ctx.addNodeAttribute(ATTR_OFFHEAP_SIZE, requiredOffheap());
    ctx.addNodeAttribute(ATTR_DATA_REGIONS_OFFHEAP_SIZE, configuredOffheap());
    DiscoverySpi spi = getSpi();
    discoOrdered = discoOrdered();
    histSupported = historySupported();
    isLocDaemon = ctx.isDaemon();
    hasRslvrs = !ctx.config().isClientMode() && !F.isEmpty(ctx.config().getSegmentationResolvers());
    segChkFreq = ctx.config().getSegmentCheckFrequency();
    if (hasRslvrs) {
        if (segChkFreq < 0)
            throw new IgniteCheckedException("Segment check frequency cannot be negative: " + segChkFreq);
        if (segChkFreq > 0 && segChkFreq < 2000)
            U.warn(log, "Configuration parameter 'segmentCheckFrequency' is too low " + "(at least 2000 ms recommended): " + segChkFreq);
        int segResAttemp = ctx.config().getSegmentationResolveAttempts();
        if (segResAttemp < 1)
            throw new IgniteCheckedException("Segment resolve attempts cannot be negative or zero: " + segResAttemp);
        checkSegmentOnStart();
    }
    spi.setMetricsProvider(createMetricsProvider());
    if (ctx.security().enabled()) {
        if (isSecurityCompatibilityMode())
            ctx.addNodeAttribute(ATTR_SECURITY_COMPATIBILITY_MODE, true);
        spi.setAuthenticator(new DiscoverySpiNodeAuthenticator() {

            @Override
            public SecurityContext authenticateNode(ClusterNode node, SecurityCredentials cred) {
                try {
                    return ctx.security().authenticateNode(node, cred);
                } catch (IgniteCheckedException e) {
                    throw U.convertException(e);
                }
            }

            @Override
            public boolean isGlobalNodeAuthentication() {
                return ctx.security().isGlobalNodeAuthentication();
            }
        });
    }
    if (ctx.config().getCommunicationFailureResolver() != null)
        ctx.resource().injectGeneric(ctx.config().getCommunicationFailureResolver());
    // Shared reference between DiscoverySpiListener and DiscoverySpiDataExchange.
    AtomicReference<IgniteFuture<?>> lastStateChangeEvtLsnrFutRef = new AtomicReference<>();
    spi.setListener(new DiscoverySpiListener() {

        private long gridStartTime;

        private final Marshaller marshaller = MarshallerUtils.jdkMarshaller(ctx.igniteInstanceName());

        /**
         * {@inheritDoc}
         */
        @Override
        public void onLocalNodeInitialized(ClusterNode locNode) {
            for (IgniteInClosure<ClusterNode> lsnr : locNodeInitLsnrs) lsnr.apply(locNode);
            if (locNode instanceof IgniteClusterNode) {
                final IgniteClusterNode node = (IgniteClusterNode) locNode;
                if (consistentId != null)
                    node.setConsistentId(consistentId);
            }
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public IgniteFuture<?> onDiscovery(DiscoveryNotification notification) {
            GridFutureAdapter<?> notificationFut = new GridFutureAdapter<>();
            discoNtfWrk.submit(notificationFut, ctx.security().enabled() ? new SecurityAwareNotificationTask(notification) : new NotificationTask(notification));
            IgniteFuture<?> fut = new IgniteFutureImpl<>(notificationFut);
            // TODO could be optimized with more specific conditions.
            switch(notification.type()) {
                case EVT_NODE_JOINED:
                case EVT_NODE_LEFT:
                case EVT_NODE_FAILED:
                    if (!CU.isPersistenceEnabled(ctx.config()))
                        lastStateChangeEvtLsnrFutRef.set(fut);
                    break;
                case EVT_DISCOVERY_CUSTOM_EVT:
                    lastStateChangeEvtLsnrFutRef.set(fut);
            }
            return fut;
        }

        /**
         * @param notification Notification.
         */
        private void onDiscovery0(DiscoveryNotification notification) {
            int type = notification.type();
            ClusterNode node = notification.getNode();
            long topVer = notification.getTopVer();
            DiscoveryCustomMessage customMsg = notification.getCustomMsgData() == null ? null : ((CustomMessageWrapper) notification.getCustomMsgData()).delegate();
            if (skipMessage(notification.type(), customMsg))
                return;
            final ClusterNode locNode = localNode();
            if (notification.getTopHist() != null)
                topHist = notification.getTopHist();
            boolean verChanged;
            if (type == EVT_NODE_METRICS_UPDATED)
                verChanged = false;
            else {
                if (type != EVT_NODE_SEGMENTED && type != EVT_CLIENT_NODE_DISCONNECTED && type != EVT_CLIENT_NODE_RECONNECTED && type != EVT_DISCOVERY_CUSTOM_EVT) {
                    minorTopVer = 0;
                    verChanged = true;
                } else
                    verChanged = false;
            }
            if (type == EVT_NODE_FAILED || type == EVT_NODE_LEFT) {
                for (DiscoCache c : discoCacheHist.values()) c.updateAlives(node);
                updateClientNodes(node.id());
            }
            boolean locJoinEvt = type == EVT_NODE_JOINED && node.id().equals(locNode.id());
            ChangeGlobalStateFinishMessage stateFinishMsg = null;
            if (type == EVT_NODE_FAILED || type == EVT_NODE_LEFT)
                stateFinishMsg = ctx.state().onNodeLeft(node);
            final AffinityTopologyVersion nextTopVer;
            if (type == EVT_DISCOVERY_CUSTOM_EVT) {
                assert customMsg != null;
                boolean incMinorTopVer;
                if (customMsg instanceof ChangeGlobalStateMessage) {
                    incMinorTopVer = ctx.state().onStateChangeMessage(new AffinityTopologyVersion(topVer, minorTopVer), (ChangeGlobalStateMessage) customMsg, discoCache());
                } else if (customMsg instanceof ChangeGlobalStateFinishMessage) {
                    ctx.state().onStateFinishMessage((ChangeGlobalStateFinishMessage) customMsg);
                    Snapshot snapshot = topSnap.get();
                    // Topology version does not change, but need create DiscoCache with new state.
                    DiscoCache discoCache = snapshot.discoCache.copy(snapshot.topVer, ctx.state().clusterState());
                    topSnap.set(new Snapshot(snapshot.topVer, discoCache));
                    incMinorTopVer = false;
                } else {
                    incMinorTopVer = ctx.cache().onCustomEvent(customMsg, new AffinityTopologyVersion(topVer, minorTopVer), node);
                }
                if (incMinorTopVer) {
                    minorTopVer++;
                    verChanged = true;
                }
                nextTopVer = new AffinityTopologyVersion(topVer, minorTopVer);
                if (incMinorTopVer)
                    ctx.cache().onDiscoveryEvent(type, customMsg, node, nextTopVer, ctx.state().clusterState());
            } else {
                nextTopVer = new AffinityTopologyVersion(topVer, minorTopVer);
                ctx.cache().onDiscoveryEvent(type, customMsg, node, nextTopVer, ctx.state().clusterState());
            }
            DiscoCache discoCache;
            // event notifications, since SPI notifies manager about all events from this listener.
            if (verChanged) {
                Snapshot snapshot = topSnap.get();
                if (customMsg == null) {
                    discoCache = createDiscoCache(nextTopVer, ctx.state().clusterState(), locNode, notification.getTopSnapshot());
                } else if (customMsg instanceof ChangeGlobalStateMessage) {
                    discoCache = createDiscoCache(nextTopVer, ctx.state().pendingState((ChangeGlobalStateMessage) customMsg), locNode, notification.getTopSnapshot());
                } else
                    discoCache = customMsg.createDiscoCache(GridDiscoveryManager.this, nextTopVer, snapshot.discoCache);
                discoCacheHist.put(nextTopVer, discoCache);
                assert snapshot.topVer.compareTo(nextTopVer) < 0 : "Topology version out of order [this.topVer=" + topSnap + ", topVer=" + topVer + ", node=" + node + ", nextTopVer=" + nextTopVer + ", evt=" + U.gridEventName(type) + ']';
                topSnap.set(new Snapshot(nextTopVer, discoCache));
            } else
                // Current version.
                discoCache = discoCache();
            if (locJoinEvt || !node.isClient() && !node.isDaemon()) {
                if (type == EVT_NODE_LEFT || type == EVT_NODE_FAILED || type == EVT_NODE_JOINED) {
                    boolean discoCacheRecalculationRequired = ctx.state().autoAdjustInMemoryClusterState(node.id(), notification.getTopSnapshot(), discoCache, topVer, minorTopVer);
                    if (discoCacheRecalculationRequired) {
                        discoCache = createDiscoCache(nextTopVer, ctx.state().clusterState(), locNode, notification.getTopSnapshot());
                        discoCacheHist.put(nextTopVer, discoCache);
                        topSnap.set(new Snapshot(nextTopVer, discoCache));
                    }
                }
            }
            if (type == EVT_DISCOVERY_CUSTOM_EVT) {
                for (Class cls = customMsg.getClass(); cls != null; cls = cls.getSuperclass()) {
                    List<CustomEventListener<DiscoveryCustomMessage>> list = customEvtLsnrs.get(cls);
                    if (list != null) {
                        for (CustomEventListener<DiscoveryCustomMessage> lsnr : list) {
                            try {
                                lsnr.onCustomEvent(nextTopVer, node, customMsg);
                            } catch (Exception e) {
                                U.error(log, "Failed to notify direct custom event listener: " + customMsg, e);
                            }
                        }
                    }
                }
            }
            SecurityContext secCtx = remoteSecurityContext(ctx);
            // If this is a local join event, just save it and do not notify listeners.
            if (locJoinEvt) {
                if (gridStartTime == 0)
                    gridStartTime = getSpi().getGridStartTime();
                topSnap.set(new Snapshot(nextTopVer, discoCache));
                startLatch.countDown();
                DiscoveryEvent discoEvt = new DiscoveryEvent();
                discoEvt.node(ctx.discovery().localNode());
                discoEvt.eventNode(node);
                discoEvt.type(EVT_NODE_JOINED);
                discoEvt.topologySnapshot(topVer, new ArrayList<>(F.view(notification.getTopSnapshot(), FILTER_NOT_DAEMON)));
                if (notification.getSpanContainer() != null)
                    discoEvt.span(notification.getSpanContainer().span());
                discoWrk.discoCache = discoCache;
                if (!ctx.clientDisconnected()) {
                    // The security processor must be notified first, since {@link IgniteSecurity#onLocalJoin}
                    // finishes local node security context initialization that can be demanded by other Ignite
                    // components.
                    ctx.security().onLocalJoin();
                    if (!isLocDaemon) {
                        ctx.cache().context().versions().onLocalJoin(topVer);
                        ctx.cache().context().coordinators().onLocalJoin(discoEvt, discoCache);
                        ctx.cache().context().exchange().onLocalJoin(discoEvt, discoCache);
                        ctx.service().onLocalJoin(discoEvt, discoCache);
                        ctx.encryption().onLocalJoin();
                        ctx.cluster().onLocalJoin();
                    }
                }
                IgniteInternalFuture<Boolean> transitionWaitFut = ctx.state().onLocalJoin(discoCache);
                locJoin.onDone(new DiscoveryLocalJoinData(discoEvt, discoCache, transitionWaitFut, ctx.state().clusterState().active()));
                return;
            } else if (type == EVT_CLIENT_NODE_DISCONNECTED) {
                assert locNode.isClient() : locNode;
                assert node.isClient() : node;
                ((IgniteKernal) ctx.grid()).onDisconnected();
                if (!locJoin.isDone())
                    locJoin.onDone(new IgniteCheckedException("Node disconnected"));
                locJoin = new GridFutureAdapter<>();
                registeredCaches.clear();
                registeredCacheGrps.clear();
                for (AffinityTopologyVersion histVer : discoCacheHist.keySet()) {
                    Object rmvd = discoCacheHist.remove(histVer);
                    assert rmvd != null : histVer;
                }
                topHist.clear();
                topSnap.set(new Snapshot(AffinityTopologyVersion.ZERO, createDiscoCache(AffinityTopologyVersion.ZERO, ctx.state().clusterState(), locNode, Collections.singleton(locNode))));
            } else if (type == EVT_CLIENT_NODE_RECONNECTED) {
                assert locNode.isClient() : locNode;
                assert node.isClient() : node;
                ctx.security().onLocalJoin();
                boolean clusterRestarted = gridStartTime != getSpi().getGridStartTime();
                gridStartTime = getSpi().getGridStartTime();
                ((IgniteKernal) ctx.grid()).onReconnected(clusterRestarted);
                ctx.cache().context().coordinators().onLocalJoin(localJoinEvent(), discoCache);
                ctx.cache().context().exchange().onLocalJoin(localJoinEvent(), discoCache);
                ctx.service().onLocalJoin(localJoinEvent(), discoCache);
                DiscoCache discoCache0 = discoCache;
                ctx.cluster().clientReconnectFuture().listen(new CI1<IgniteFuture<?>>() {

                    @Override
                    public void apply(IgniteFuture<?> fut) {
                        try {
                            fut.get();
                            discoWrk.addEvent(new NotificationEvent(EVT_CLIENT_NODE_RECONNECTED, nextTopVer, node, discoCache0, notification.getTopSnapshot(), null, notification.getSpanContainer(), secCtx));
                        } catch (IgniteException ignore) {
                        // No-op.
                        }
                    }
                });
                return;
            }
            if (type == EVT_CLIENT_NODE_DISCONNECTED || type == EVT_NODE_SEGMENTED || !ctx.clientDisconnected())
                discoWrk.addEvent(new NotificationEvent(type, nextTopVer, node, discoCache, notification.getTopSnapshot(), customMsg, notification.getSpanContainer(), secCtx));
            if (stateFinishMsg != null)
                discoWrk.addEvent(new NotificationEvent(EVT_DISCOVERY_CUSTOM_EVT, nextTopVer, node, discoCache, notification.getTopSnapshot(), stateFinishMsg, notification.getSpanContainer(), secCtx));
            if (type == EVT_CLIENT_NODE_DISCONNECTED)
                discoWrk.awaitDisconnectEvent();
        }

        /**
         * Extends {@link NotificationTask} to run in a security context owned by the initiator of the
         * discovery event.
         */
        class SecurityAwareNotificationTask extends NotificationTask {

            /**
             */
            public SecurityAwareNotificationTask(DiscoveryNotification notification) {
                super(notification);
            }

            /**
             */
            @Override
            public void run() {
                DiscoverySpiCustomMessage customMsg = notification.getCustomMsgData();
                if (customMsg instanceof SecurityAwareCustomMessageWrapper) {
                    UUID secSubjId = ((SecurityAwareCustomMessageWrapper) customMsg).securitySubjectId();
                    try (OperationSecurityContext ignored = ctx.security().withContext(secSubjId)) {
                        super.run();
                    }
                } else {
                    SecurityContext initiatorNodeSecCtx = nodeSecurityContext(marshaller, U.resolveClassLoader(ctx.config()), notification.getNode());
                    try (OperationSecurityContext ignored = ctx.security().withContext(initiatorNodeSecCtx)) {
                        super.run();
                    }
                }
            }
        }

        /**
         * Represents task to handle discovery notification asynchronously.
         */
        class NotificationTask implements Runnable {

            /**
             */
            protected final DiscoveryNotification notification;

            /**
             */
            public NotificationTask(DiscoveryNotification notification) {
                this.notification = notification;
            }

            /**
             * {@inheritDoc}
             */
            @Override
            public void run() {
                synchronized (discoEvtMux) {
                    onDiscovery0(notification);
                }
            }
        }
    });
    spi.setDataExchange(new DiscoverySpiDataExchange() {

        @Override
        public DiscoveryDataBag collect(DiscoveryDataBag dataBag) {
            assert dataBag != null;
            assert dataBag.joiningNodeId() != null;
            if (ctx.localNodeId().equals(dataBag.joiningNodeId())) {
                for (GridComponent c : ctx.components()) c.collectJoiningNodeData(dataBag);
            } else {
                waitForLastStateChangeEventFuture();
                for (GridComponent c : ctx.components()) c.collectGridNodeData(dataBag);
            }
            return dataBag;
        }

        @Override
        public void onExchange(DiscoveryDataBag dataBag) {
            assert dataBag != null;
            assert dataBag.joiningNodeId() != null;
            if (ctx.localNodeId().equals(dataBag.joiningNodeId())) {
                // NodeAdded msg reached joining node after round-trip over the ring.
                IGridClusterStateProcessor stateProc = ctx.state();
                stateProc.onGridDataReceived(dataBag.gridDiscoveryData(stateProc.discoveryDataType().ordinal()));
                for (GridComponent c : ctx.components()) {
                    if (c.discoveryDataType() != null && c != stateProc)
                        c.onGridDataReceived(dataBag.gridDiscoveryData(c.discoveryDataType().ordinal()));
                }
            } else {
                // Discovery data from newly joined node has to be applied to the current old node.
                IGridClusterStateProcessor stateProc = ctx.state();
                JoiningNodeDiscoveryData data0 = dataBag.newJoinerDiscoveryData(stateProc.discoveryDataType().ordinal());
                assert data0 != null;
                stateProc.onJoiningNodeDataReceived(data0);
                for (GridComponent c : ctx.components()) {
                    if (c.discoveryDataType() != null && c != stateProc) {
                        JoiningNodeDiscoveryData data = dataBag.newJoinerDiscoveryData(c.discoveryDataType().ordinal());
                        if (data != null)
                            c.onJoiningNodeDataReceived(data);
                    }
                }
            }
        }

        /**
         */
        private void waitForLastStateChangeEventFuture() {
            IgniteFuture<?> lastStateChangeEvtLsnrFut = lastStateChangeEvtLsnrFutRef.get();
            if (lastStateChangeEvtLsnrFut != null) {
                Thread currThread = Thread.currentThread();
                GridWorker worker = currThread instanceof IgniteDiscoveryThread ? ((IgniteDiscoveryThread) currThread).worker() : null;
                if (worker != null)
                    worker.blockingSectionBegin();
                try {
                    lastStateChangeEvtLsnrFut.get();
                } finally {
                    // Guaranteed to be invoked in the same thread as DiscoverySpiListener#onDiscovery.
                    // No additional synchronization for reference is required.
                    lastStateChangeEvtLsnrFutRef.set(null);
                    if (worker != null)
                        worker.blockingSectionEnd();
                }
            }
        }
    });
    new DiscoveryMessageNotifierThread(discoNtfWrk).start();
    startSpi();
    registeredDiscoSpi = true;
    try {
        U.await(startLatch);
    } catch (IgniteInterruptedException e) {
        throw new IgniteCheckedException("Failed to start discovery manager (thread has been interrupted).", e);
    }
    // Start segment check worker only if frequency is greater than 0.
    if (hasRslvrs && segChkFreq > 0) {
        segChkWrk = new SegmentCheckWorker();
        segChkThread = new IgniteThread(segChkWrk);
        segChkThread.setUncaughtExceptionHandler(new OomExceptionHandler(ctx));
        segChkThread.start();
    }
    locNode = spi.getLocalNode();
    checkAttributes(discoCache().remoteNodes());
    // Start discovery worker.
    new IgniteThread(discoWrk).start();
    if (log.isDebugEnabled())
        log.debug(startInfo());
}
Also used : CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) CI1(org.apache.ignite.internal.util.typedef.CI1) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) List(java.util.List) UUID(java.util.UUID) OperationSecurityContext(org.apache.ignite.internal.processors.security.OperationSecurityContext) ClusterNode(org.apache.ignite.cluster.ClusterNode) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) DiscoverySpiNodeAuthenticator(org.apache.ignite.spi.discovery.DiscoverySpiNodeAuthenticator) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) IgniteDiscoveryThread(org.apache.ignite.spi.discovery.IgniteDiscoveryThread) SecurityCredentials(org.apache.ignite.plugin.security.SecurityCredentials) DiscoverySpiDataExchange(org.apache.ignite.spi.discovery.DiscoverySpiDataExchange) IgniteThread(org.apache.ignite.thread.IgniteThread) OomExceptionHandler(org.apache.ignite.thread.OomExceptionHandler) GridComponent(org.apache.ignite.internal.GridComponent) IgniteFuture(org.apache.ignite.lang.IgniteFuture) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) JoiningNodeDiscoveryData(org.apache.ignite.spi.discovery.DiscoveryDataBag.JoiningNodeDiscoveryData) DiscoveryDataBag(org.apache.ignite.spi.discovery.DiscoveryDataBag) IGridClusterStateProcessor(org.apache.ignite.internal.processors.cluster.IGridClusterStateProcessor) IgniteKernal(org.apache.ignite.internal.IgniteKernal) Marshaller(org.apache.ignite.marshaller.Marshaller) ChangeGlobalStateMessage(org.apache.ignite.internal.processors.cluster.ChangeGlobalStateMessage) AtomicReference(java.util.concurrent.atomic.AtomicReference) DiscoverySpiListener(org.apache.ignite.spi.discovery.DiscoverySpiListener) GridWorker(org.apache.ignite.internal.util.worker.GridWorker) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) IgniteClientDisconnectedCheckedException(org.apache.ignite.internal.IgniteClientDisconnectedCheckedException) IgniteException(org.apache.ignite.IgniteException) NodeStoppingException(org.apache.ignite.internal.NodeStoppingException) IgniteThread(org.apache.ignite.thread.IgniteThread) IgniteDiscoveryThread(org.apache.ignite.spi.discovery.IgniteDiscoveryThread) DiscoverySpiCustomMessage(org.apache.ignite.spi.discovery.DiscoverySpiCustomMessage) DiscoverySpi(org.apache.ignite.spi.discovery.DiscoverySpi) TcpDiscoverySpi(org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi) SecurityContext(org.apache.ignite.internal.processors.security.SecurityContext) SecurityUtils.remoteSecurityContext(org.apache.ignite.internal.processors.security.SecurityUtils.remoteSecurityContext) SecurityUtils.withRemoteSecurityContext(org.apache.ignite.internal.processors.security.SecurityUtils.withRemoteSecurityContext) OperationSecurityContext(org.apache.ignite.internal.processors.security.OperationSecurityContext) SecurityUtils.nodeSecurityContext(org.apache.ignite.internal.processors.security.SecurityUtils.nodeSecurityContext) IgniteInClosure(org.apache.ignite.lang.IgniteInClosure) DiscoveryNotification(org.apache.ignite.spi.discovery.DiscoveryNotification) ChangeGlobalStateFinishMessage(org.apache.ignite.internal.processors.cluster.ChangeGlobalStateFinishMessage)

Example 7 with CI1

use of org.apache.ignite.internal.util.typedef.CI1 in project ignite by apache.

the class GridNearTxLocal method commitAsyncLocal.

/**
 * Commits local part of colocated transaction.
 *
 * @return Commit future.
 */
public IgniteInternalFuture<IgniteInternalTx> commitAsyncLocal() {
    if (log.isDebugEnabled())
        log.debug("Committing colocated tx locally: " + this);
    IgniteInternalFuture<?> prep = prepFut;
    // Do not create finish future if there are no remote nodes.
    if (F.isEmpty(dhtMap) && F.isEmpty(nearMap)) {
        IgniteInternalFuture fut = prep != null ? prep : new GridFinishedFuture<>(this);
        if (fut.isDone())
            cctx.tm().mvccFinish(this);
        else
            fut.listen(f -> cctx.tm().mvccFinish(this));
        return fut;
    }
    final GridDhtTxFinishFuture fut = new GridDhtTxFinishFuture<>(cctx, this, true);
    cctx.mvcc().addFuture(fut, fut.futureId());
    if (prep == null || prep.isDone()) {
        assert prep != null || optimistic();
        IgniteCheckedException err = null;
        try {
            if (prep != null)
                // Check for errors of a parent future.
                prep.get();
        } catch (IgniteCheckedException e) {
            err = e;
            U.error(log, "Failed to prepare transaction: " + this, e);
        } catch (Throwable t) {
            fut.onDone(t);
            throw t;
        }
        if (err != null)
            fut.rollbackOnError(err);
        else
            fut.finish(true);
    } else
        prep.listen(new CI1<IgniteInternalFuture<?>>() {

            @Override
            public void apply(IgniteInternalFuture<?> f) {
                IgniteCheckedException err = null;
                try {
                    // Check for errors of a parent future.
                    f.get();
                } catch (IgniteCheckedException e) {
                    err = e;
                    U.error(log, "Failed to prepare transaction: " + this, e);
                } catch (Throwable t) {
                    fut.onDone(t);
                    throw t;
                }
                if (err != null)
                    fut.rollbackOnError(err);
                else
                    fut.finish(true);
            }
        });
    return fut;
}
Also used : GridCacheMvccCandidate(org.apache.ignite.internal.processors.cache.GridCacheMvccCandidate) IgniteTxRollbackCheckedException(org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter) GridFinishedFuture(org.apache.ignite.internal.util.future.GridFinishedFuture) GridDistributedTxMapping(org.apache.ignite.internal.processors.cache.distributed.GridDistributedTxMapping) EVT_CACHE_OBJECT_READ(org.apache.ignite.events.EventType.EVT_CACHE_OBJECT_READ) EntryProcessor(javax.cache.processor.EntryProcessor) Map(java.util.Map) Cache(javax.cache.Cache) IgniteCacheExpiryPolicy(org.apache.ignite.internal.processors.cache.IgniteCacheExpiryPolicy) DELETE(org.apache.ignite.internal.processors.cache.GridCacheOperation.DELETE) GridStringBuilder(org.apache.ignite.internal.util.GridStringBuilder) IgniteInClosure(org.apache.ignite.lang.IgniteInClosure) SER_READ_NOT_EMPTY_VER(org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry.SER_READ_NOT_EMPTY_VER) IgniteTxTimeoutCheckedException(org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException) GridToStringExclude(org.apache.ignite.internal.util.tostring.GridToStringExclude) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) MvccSnapshotFuture(org.apache.ignite.internal.processors.cache.mvcc.MvccSnapshotFuture) MvccProcessor(org.apache.ignite.internal.processors.cache.mvcc.MvccProcessor) TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) Set(java.util.Set) GridCacheVersionedFuture(org.apache.ignite.internal.processors.cache.GridCacheVersionedFuture) GridDhtTxLocalAdapter(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxLocalAdapter) MTC(org.apache.ignite.internal.processors.tracing.MTC) TransactionProxy(org.apache.ignite.internal.processors.cache.transactions.TransactionProxy) TX_NEAR_ENLIST_READ(org.apache.ignite.internal.processors.tracing.SpanType.TX_NEAR_ENLIST_READ) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) EntryGetResult(org.apache.ignite.internal.processors.cache.EntryGetResult) GridDhtDetachedCacheEntry(org.apache.ignite.internal.processors.cache.distributed.dht.colocated.GridDhtDetachedCacheEntry) U(org.apache.ignite.internal.util.typedef.internal.U) SecurityPermission(org.apache.ignite.plugin.security.SecurityPermission) GridNearReadRepairCheckOnlyFuture(org.apache.ignite.internal.processors.cache.distributed.near.consistency.GridNearReadRepairCheckOnlyFuture) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) COMMITTING(org.apache.ignite.transactions.TransactionState.COMMITTING) ClusterNode(org.apache.ignite.cluster.ClusterNode) CI1(org.apache.ignite.internal.util.typedef.CI1) IgniteInternalTx(org.apache.ignite.internal.processors.cache.transactions.IgniteInternalTx) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) MvccUtils(org.apache.ignite.internal.processors.cache.mvcc.MvccUtils) S(org.apache.ignite.internal.util.typedef.internal.S) C2(org.apache.ignite.internal.util.typedef.C2) IgniteTxManager(org.apache.ignite.internal.processors.cache.transactions.IgniteTxManager) C1(org.apache.ignite.internal.util.typedef.C1) PREPARED(org.apache.ignite.transactions.TransactionState.PREPARED) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) MvccCoordinator(org.apache.ignite.internal.processors.cache.mvcc.MvccCoordinator) IgniteTxOptimisticCheckedException(org.apache.ignite.internal.transactions.IgniteTxOptimisticCheckedException) COMMITTED(org.apache.ignite.transactions.TransactionState.COMMITTED) PREPARING(org.apache.ignite.transactions.TransactionState.PREPARING) IgniteTxEntry(org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry) GridNearReadRepairFuture(org.apache.ignite.internal.processors.cache.distributed.near.consistency.GridNearReadRepairFuture) AtomicLong(java.util.concurrent.atomic.AtomicLong) IgniteConsistencyViolationException(org.apache.ignite.internal.processors.cache.distributed.near.consistency.IgniteConsistencyViolationException) GridCacheSharedContext(org.apache.ignite.internal.processors.cache.GridCacheSharedContext) MvccSnapshot(org.apache.ignite.internal.processors.cache.mvcc.MvccSnapshot) CacheEntryPredicate(org.apache.ignite.internal.processors.cache.CacheEntryPredicate) GridDistributedCacheEntry(org.apache.ignite.internal.processors.cache.distributed.GridDistributedCacheEntry) GridCacheDrInfo(org.apache.ignite.internal.processors.cache.dr.GridCacheDrInfo) CX1(org.apache.ignite.internal.util.typedef.CX1) IgniteUuid(org.apache.ignite.lang.IgniteUuid) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) TRANSFORM(org.apache.ignite.internal.processors.cache.GridCacheOperation.TRANSFORM) GridInvokeValue(org.apache.ignite.internal.processors.cache.distributed.dht.GridInvokeValue) UNKNOWN(org.apache.ignite.transactions.TransactionState.UNKNOWN) TransactionProxyRollbackOnlyImpl(org.apache.ignite.internal.processors.cache.transactions.TransactionProxyRollbackOnlyImpl) CREATE(org.apache.ignite.internal.processors.cache.GridCacheOperation.CREATE) TX_NEAR_ENLIST_WRITE(org.apache.ignite.internal.processors.tracing.SpanType.TX_NEAR_ENLIST_WRITE) GridDhtTxPrepareFuture(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxPrepareFuture) GridInClosure3(org.apache.ignite.internal.util.lang.GridInClosure3) AtomicReferenceFieldUpdater(java.util.concurrent.atomic.AtomicReferenceFieldUpdater) Collection(java.util.Collection) ReadRepairStrategy(org.apache.ignite.cache.ReadRepairStrategy) UUID(java.util.UUID) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) TransactionProxyImpl(org.apache.ignite.internal.processors.cache.transactions.TransactionProxyImpl) Instant(java.time.Instant) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) CI2(org.apache.ignite.internal.util.typedef.CI2) GridCacheOperation(org.apache.ignite.internal.processors.cache.GridCacheOperation) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) Nullable(org.jetbrains.annotations.Nullable) NOOP(org.apache.ignite.internal.processors.cache.GridCacheOperation.NOOP) IgniteTxKey(org.apache.ignite.internal.processors.cache.transactions.IgniteTxKey) CU(org.apache.ignite.internal.util.typedef.internal.CU) ACTIVE(org.apache.ignite.transactions.TransactionState.ACTIVE) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) GridDhtCacheEntry(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtCacheEntry) ROLLING_BACK(org.apache.ignite.transactions.TransactionState.ROLLING_BACK) GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) NodeStoppingException(org.apache.ignite.internal.NodeStoppingException) GridCacheReturn(org.apache.ignite.internal.processors.cache.GridCacheReturn) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) HashMap(java.util.HashMap) IgniteBiClosure(org.apache.ignite.lang.IgniteBiClosure) SER_READ_EMPTY_ENTRY_VER(org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry.SER_READ_EMPTY_ENTRY_VER) HashSet(java.util.HashSet) IgniteClosure(org.apache.ignite.lang.IgniteClosure) IgniteUtils(org.apache.ignite.internal.util.IgniteUtils) CacheException(javax.cache.CacheException) UpdateSourceIterator(org.apache.ignite.internal.processors.query.UpdateSourceIterator) GridLeanMap(org.apache.ignite.internal.util.GridLeanMap) F(org.apache.ignite.internal.util.typedef.F) Iterator(java.util.Iterator) GridTimeoutObject(org.apache.ignite.internal.processors.timeout.GridTimeoutObject) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) ROLLED_BACK(org.apache.ignite.transactions.TransactionState.ROLLED_BACK) UPDATE(org.apache.ignite.internal.processors.cache.GridCacheOperation.UPDATE) MARKED_ROLLBACK(org.apache.ignite.transactions.TransactionState.MARKED_ROLLBACK) MvccCoordinatorChangeAware(org.apache.ignite.internal.processors.cache.mvcc.MvccCoordinatorChangeAware) GridEmbeddedFuture(org.apache.ignite.internal.util.future.GridEmbeddedFuture) TransactionState(org.apache.ignite.transactions.TransactionState) GridDhtTxFinishFuture(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxFinishFuture) CacheOperationContext(org.apache.ignite.internal.processors.cache.CacheOperationContext) EnlistOperation(org.apache.ignite.internal.processors.query.EnlistOperation) GridPlainRunnable(org.apache.ignite.internal.util.lang.GridPlainRunnable) Collections(java.util.Collections) GridClosureException(org.apache.ignite.internal.util.lang.GridClosureException) READ(org.apache.ignite.internal.processors.cache.GridCacheOperation.READ) TraceSurroundings(org.apache.ignite.internal.processors.tracing.MTC.TraceSurroundings) GridDhtTxFinishFuture(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxFinishFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CI1(org.apache.ignite.internal.util.typedef.CI1) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture)

Example 8 with CI1

use of org.apache.ignite.internal.util.typedef.CI1 in project ignite by apache.

the class GridNearTxQueryEnlistFuture method map.

/**
 * @param topLocked Topology locked flag.
 */
@Override
protected void map(final boolean topLocked) {
    try {
        Map<ClusterNode, IntArrayHolder> map;
        boolean locallyMapped = false;
        AffinityAssignment assignment = cctx.affinity().assignment(topVer);
        if (parts != null) {
            map = U.newHashMap(parts.length);
            for (int i = 0; i < parts.length; i++) {
                ClusterNode pNode = assignment.get(parts[i]).get(0);
                map.computeIfAbsent(pNode, n -> new IntArrayHolder()).add(parts[i]);
                updateMappings(pNode);
                if (!locallyMapped && pNode.isLocal())
                    locallyMapped = true;
            }
        } else {
            Set<ClusterNode> nodes = assignment.primaryPartitionNodes();
            map = U.newHashMap(nodes.size());
            for (ClusterNode pNode : nodes) {
                map.put(pNode, null);
                updateMappings(pNode);
                if (!locallyMapped && pNode.isLocal())
                    locallyMapped = true;
            }
        }
        if (map.isEmpty())
            throw new ClusterTopologyServerNotFoundException("Failed to find data nodes for cache (all partition " + "nodes left the grid). [fut=" + toString() + ']');
        int idx = 0;
        boolean first = true, clientFirst = false;
        GridDhtTxQueryEnlistFuture localFut = null;
        for (Map.Entry<ClusterNode, IntArrayHolder> entry : map.entrySet()) {
            MiniFuture mini;
            ClusterNode node = entry.getKey();
            IntArrayHolder parts = entry.getValue();
            add(mini = new MiniFuture(node));
            if (node.isLocal()) {
                localFut = new GridDhtTxQueryEnlistFuture(cctx.localNode().id(), lockVer, mvccSnapshot, threadId, futId, // The common tx logic expects non-zero mini-future ids (negative local and positive non-local).
                -(++idx), tx, cacheIds, parts == null ? null : parts.array(), schema, qry, params, flags, pageSize, remainingTime(), cctx);
                updateLocalFuture(localFut);
                localFut.listen(new CI1<IgniteInternalFuture<Long>>() {

                    @Override
                    public void apply(IgniteInternalFuture<Long> fut) {
                        assert fut.error() != null || fut.result() != null : fut;
                        try {
                            clearLocalFuture((GridDhtTxQueryEnlistFuture) fut);
                            GridNearTxQueryEnlistResponse res = fut.error() == null ? createResponse(fut) : null;
                            mini.onResult(res, fut.error());
                        } catch (IgniteCheckedException e) {
                            mini.onResult(null, e);
                        } finally {
                            CU.unwindEvicts(cctx);
                        }
                    }
                });
            } else {
                if (first) {
                    clientFirst = cctx.localNode().isClient() && !topLocked && !tx.hasRemoteLocks();
                    first = false;
                }
                GridNearTxQueryEnlistRequest req = new GridNearTxQueryEnlistRequest(cctx.cacheId(), threadId, futId, // The common tx logic expects non-zero mini-future ids (negative local and positive non-local).
                ++idx, topVer, lockVer, mvccSnapshot, cacheIds, parts == null ? null : parts.array(), schema, qry, params, flags, pageSize, remainingTime(), tx.remainingTime(), tx.taskNameHash(), clientFirst);
                sendRequest(req, node.id());
            }
        }
        markInitialized();
        if (localFut != null)
            localFut.init();
    } catch (Throwable e) {
        onDone(e);
        if (e instanceof Error)
            throw (Error) e;
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) Arrays(java.util.Arrays) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter) QUERY_POOL(org.apache.ignite.internal.managers.communication.GridIoPolicy.QUERY_POOL) GridToStringExclude(org.apache.ignite.internal.util.tostring.GridToStringExclude) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridDistributedTxMapping(org.apache.ignite.internal.processors.cache.distributed.GridDistributedTxMapping) GridCacheMessage(org.apache.ignite.internal.processors.cache.GridCacheMessage) Set(java.util.Set) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) U(org.apache.ignite.internal.util.typedef.internal.U) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) AffinityAssignment(org.apache.ignite.internal.processors.affinity.AffinityAssignment) NearTxQueryEnlistResultHandler.createResponse(org.apache.ignite.internal.processors.cache.distributed.dht.NearTxQueryEnlistResultHandler.createResponse) ClusterNode(org.apache.ignite.cluster.ClusterNode) CI1(org.apache.ignite.internal.util.typedef.CI1) Map(java.util.Map) CU(org.apache.ignite.internal.util.typedef.internal.CU) S(org.apache.ignite.internal.util.typedef.internal.S) GridDhtTxQueryEnlistFuture(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxQueryEnlistFuture) ClusterTopologyServerNotFoundException(org.apache.ignite.internal.cluster.ClusterTopologyServerNotFoundException) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) Collections(java.util.Collections) AffinityAssignment(org.apache.ignite.internal.processors.affinity.AffinityAssignment) GridDhtTxQueryEnlistFuture(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxQueryEnlistFuture) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) ClusterTopologyServerNotFoundException(org.apache.ignite.internal.cluster.ClusterTopologyServerNotFoundException) Map(java.util.Map)

Example 9 with CI1

use of org.apache.ignite.internal.util.typedef.CI1 in project ignite by apache.

the class GridMarshallerPerformanceTest method testByteBuffer.

/**
 * @throws Exception If failed.
 */
@Test
public void testByteBuffer() throws Exception {
    final ByteBuffer buf = ByteBuffer.allocate(1024);
    IgniteInClosure<TestObject> writer = new CI1<TestObject>() {

        @Override
        public void apply(TestObject obj) {
            buf.clear();
            obj.write(buf);
        }
    };
    IgniteOutClosure<TestObject> reader = new CO<TestObject>() {

        @Override
        public TestObject apply() {
            buf.flip();
            return TestObject.read(buf);
        }
    };
    runTest("ByteBuffer", writer, reader);
}
Also used : CI1(org.apache.ignite.internal.util.typedef.CI1) CO(org.apache.ignite.internal.util.typedef.CO) ByteBuffer(java.nio.ByteBuffer) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 10 with CI1

use of org.apache.ignite.internal.util.typedef.CI1 in project ignite by apache.

the class GridJobStealingCollisionSpiSelfTest method testMultiplePassiveZeroActive.

/**
 * @throws Exception If test failed.
 */
@Test
public void testMultiplePassiveZeroActive() throws Exception {
    final List<CollisionJobContext> waitCtxs = new ArrayList<>(2);
    final List<CollisionJobContext> activeCtxs = new ArrayList<>(2);
    CI1<GridTestCollisionJobContext> lsnr = new CI1<GridTestCollisionJobContext>() {

        @Override
        public void apply(GridTestCollisionJobContext c) {
            if (waitCtxs.remove(c))
                activeCtxs.add(c);
        }
    };
    Collections.addAll(waitCtxs, new GridTestCollisionJobContext(createTaskSession(), 1, lsnr), new GridTestCollisionJobContext(createTaskSession(), 2, lsnr), new GridTestCollisionJobContext(createTaskSession(), 3, lsnr));
    ClusterNode rmtNode = F.first(getSpiContext().remoteNodes());
    // Emulate message to steal 2 jobs.
    getSpiContext().triggerMessage(rmtNode, new JobStealingRequest(2));
    getSpi().onCollision(new GridCollisionTestContext(activeCtxs, waitCtxs));
    // Make sure that one job got activated.
    checkActivated((GridTestCollisionJobContext) activeCtxs.get(0));
    // Rejected.
    checkRejected((GridTestCollisionJobContext) waitCtxs.get(0), rmtNode);
    checkRejected((GridTestCollisionJobContext) waitCtxs.get(1), rmtNode);
    // Make sure that no message was sent.
    Serializable msg = getSpiContext().removeSentMessage(rmtNode);
    assert msg == null;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) Serializable(java.io.Serializable) ArrayList(java.util.ArrayList) CI1(org.apache.ignite.internal.util.typedef.CI1) CollisionJobContext(org.apache.ignite.spi.collision.CollisionJobContext) GridTestCollisionJobContext(org.apache.ignite.spi.collision.GridTestCollisionJobContext) GridCollisionTestContext(org.apache.ignite.spi.collision.GridCollisionTestContext) GridTestCollisionJobContext(org.apache.ignite.spi.collision.GridTestCollisionJobContext) GridSpiTest(org.apache.ignite.testframework.junits.spi.GridSpiTest) GridSpiAbstractTest(org.apache.ignite.testframework.junits.spi.GridSpiAbstractTest) Test(org.junit.Test)

Aggregations

CI1 (org.apache.ignite.internal.util.typedef.CI1)32 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)12 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)11 ClusterNode (org.apache.ignite.cluster.ClusterNode)11 Test (org.junit.Test)11 IgniteFuture (org.apache.ignite.lang.IgniteFuture)9 ArrayList (java.util.ArrayList)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 IgniteException (org.apache.ignite.IgniteException)7 IgniteInterruptedCheckedException (org.apache.ignite.internal.IgniteInterruptedCheckedException)7 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)7 Collection (java.util.Collection)6 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)6 IgniteSpiException (org.apache.ignite.spi.IgniteSpiException)6 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)6 Nullable (org.jetbrains.annotations.Nullable)6 List (java.util.List)5 UUID (java.util.UUID)5 DiscoveryEvent (org.apache.ignite.events.DiscoveryEvent)5 GridFutureAdapter (org.apache.ignite.internal.util.future.GridFutureAdapter)5