Search in sources :

Example 11 with IgniteThread

use of org.apache.ignite.thread.IgniteThread in project ignite by apache.

the class HadoopExecutorService method startThread.

/**
     * @param task Task.
     */
private void startThread(final Callable<?> task) {
    String workerName;
    if (task instanceof HadoopRunnableTask) {
        final HadoopTaskInfo i = ((HadoopRunnableTask) task).taskInfo();
        workerName = "Hadoop-task-" + i.jobId() + "-" + i.type() + "-" + i.taskNumber() + "-" + i.attempt();
    } else
        workerName = task.toString();
    GridWorker w = new GridWorker(igniteInstanceName, workerName, log, lsnr) {

        @Override
        protected void body() {
            try {
                task.call();
            } catch (Exception e) {
                log.error("Failed to execute task: " + task, e);
            }
        }
    };
    workers.add(w);
    if (shutdown)
        w.cancel();
    new IgniteThread(w).start();
}
Also used : HadoopTaskInfo(org.apache.ignite.internal.processors.hadoop.HadoopTaskInfo) IgniteThread(org.apache.ignite.thread.IgniteThread) GridWorker(org.apache.ignite.internal.util.worker.GridWorker)

Example 12 with IgniteThread

use of org.apache.ignite.thread.IgniteThread in project ignite by apache.

the class GridAbstractTest method runTest.

/** {@inheritDoc} */
@SuppressWarnings({ "ProhibitedExceptionDeclared" })
@Override
protected void runTest() throws Throwable {
    final AtomicReference<Throwable> ex = new AtomicReference<>();
    Thread runner = new IgniteThread(getTestIgniteInstanceName(), "test-runner", new Runnable() {

        @Override
        public void run() {
            try {
                runTestInternal();
            } catch (Throwable e) {
                IgniteClosure<Throwable, Throwable> hnd = errorHandler();
                ex.set(hnd != null ? hnd.apply(e) : e);
            }
        }
    });
    runner.start();
    runner.join(isDebug() ? 0 : getTestTimeout());
    if (runner.isAlive()) {
        U.error(log, "Test has been timed out and will be interrupted (threads dump will be taken before interruption) [" + "test=" + getName() + ", timeout=" + getTestTimeout() + ']');
        List<Ignite> nodes = IgnitionEx.allGridsx();
        for (Ignite node : nodes) ((IgniteKernal) node).dumpDebugInfo();
        // We dump threads to stdout, because we can loose logs in case
        // the build is cancelled on TeamCity.
        U.dumpThreads(null);
        U.dumpThreads(log);
        U.interrupt(runner);
        U.join(runner, log);
        throw new TimeoutException("Test has been timed out [test=" + getName() + ", timeout=" + getTestTimeout() + ']');
    }
    Throwable t = ex.get();
    if (t != null) {
        U.error(log, "Test failed.", t);
        throw t;
    }
    assert !stopGridErr : "Error occurred on grid stop (see log for more details).";
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) Ignite(org.apache.ignite.Ignite) IgniteThread(org.apache.ignite.thread.IgniteThread) IgniteThread(org.apache.ignite.thread.IgniteThread) TimeoutException(java.util.concurrent.TimeoutException)

Example 13 with IgniteThread

use of org.apache.ignite.thread.IgniteThread in project ignite by apache.

the class HadoopExternalCommunication method start.

/**
     * Starts communication.
     *
     * @throws IgniteCheckedException If failed.
     */
public void start() throws IgniteCheckedException {
    try {
        locHost = U.getLocalHost();
    } catch (IOException e) {
        throw new IgniteCheckedException("Failed to initialize local address.", e);
    }
    try {
        shmemSrv = resetShmemServer();
    } catch (IgniteCheckedException e) {
        U.warn(log, "Failed to start shared memory communication server.", e);
    }
    try {
        // This method potentially resets local port to the value
        // local node was bound to.
        nioSrvr = resetNioServer();
    } catch (IgniteCheckedException e) {
        throw new IgniteCheckedException("Failed to initialize TCP server: " + locHost, e);
    }
    locProcDesc.address(locHost.getHostAddress());
    locProcDesc.sharedMemoryPort(boundTcpShmemPort);
    locProcDesc.tcpPort(boundTcpPort);
    locIdMsg = new ProcessHandshakeMessage(locProcDesc);
    if (shmemSrv != null) {
        shmemAcceptWorker = new ShmemAcceptWorker(shmemSrv);
        new IgniteThread(shmemAcceptWorker).start();
    }
    nioSrvr.start();
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IOException(java.io.IOException) IgniteThread(org.apache.ignite.thread.IgniteThread)

Example 14 with IgniteThread

use of org.apache.ignite.thread.IgniteThread in project ignite by apache.

the class GridMarshallerAbstractTest method unmarshal.

/**
     * @param buf Byte buffer to unmarshal.
     * @return Unmarshalled object.
     * @throws IgniteCheckedException Thrown if any exception occurs while unmarshalling.
     */
@SuppressWarnings({ "RedundantTypeArguments" })
protected static <T> T unmarshal(final byte[] buf) throws IgniteCheckedException {
    RunnableFuture<T> f = new FutureTask<>(new Callable<T>() {

        @Override
        public T call() throws IgniteCheckedException {
            return marsh.<T>unmarshal(buf, Thread.currentThread().getContextClassLoader());
        }
    });
    // Any deserialization has to be executed under a thread, that contains the Ignite instance name.
    new IgniteThread(igniteInstanceName, "unmarshal-thread", f).start();
    try {
        return f.get();
    } catch (Exception e) {
        if (e.getCause() instanceof IgniteCheckedException)
            throw (IgniteCheckedException) e.getCause();
        fail(e.getCause().getMessage());
    }
    return null;
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) FutureTask(java.util.concurrent.FutureTask) IgniteThread(org.apache.ignite.thread.IgniteThread) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IOException(java.io.IOException)

Example 15 with IgniteThread

use of org.apache.ignite.thread.IgniteThread in project ignite by apache.

the class GridDiscoveryManager method start.

/** {@inheritDoc} */
@Override
public void start(boolean activeOnStart) throws IgniteCheckedException {
    long totSysMemory = -1;
    try {
        totSysMemory = U.<Long>property(os, "totalPhysicalMemorySize");
    } catch (RuntimeException ignored) {
    // No-op.
    }
    ctx.addNodeAttribute(IgniteNodeAttributes.ATTR_PHY_RAM, totSysMemory);
    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();
    }
    metricsUpdateTask = ctx.timeout().schedule(new MetricsUpdater(), METRICS_UPDATE_FREQ, METRICS_UPDATE_FREQ);
    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();
            }
        });
    }
    spi.setListener(new DiscoverySpiListener() {

        private long gridStartTime;

        /** {@inheritDoc} */
        @Override
        public void onLocalNodeInitialized(ClusterNode locNode) {
            for (IgniteInClosure<ClusterNode> lsnr : localNodeInitLsnrs) lsnr.apply(locNode);
        }

        @Override
        public void onDiscovery(final int type, final long topVer, final ClusterNode node, final Collection<ClusterNode> topSnapshot, final Map<Long, Collection<ClusterNode>> snapshots, @Nullable DiscoverySpiCustomMessage spiCustomMsg) {
            DiscoveryCustomMessage customMsg = spiCustomMsg == null ? null : ((CustomMessageWrapper) spiCustomMsg).delegate();
            if (skipMessage(type, customMsg))
                return;
            final ClusterNode locNode = localNode();
            if (snapshots != null)
                topHist = snapshots;
            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 != DiscoveryCustomEvent.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());
            }
            final AffinityTopologyVersion nextTopVer;
            if (type == DiscoveryCustomEvent.EVT_DISCOVERY_CUSTOM_EVT) {
                assert customMsg != null;
                boolean incMinorTopVer = ctx.cache().onCustomEvent(customMsg, new AffinityTopologyVersion(topVer, minorTopVer));
                if (incMinorTopVer) {
                    minorTopVer++;
                    verChanged = true;
                }
                nextTopVer = new AffinityTopologyVersion(topVer, minorTopVer);
                if (verChanged)
                    ctx.cache().onDiscoveryEvent(type, node, nextTopVer);
            } else {
                nextTopVer = new AffinityTopologyVersion(topVer, minorTopVer);
                ctx.cache().onDiscoveryEvent(type, node, nextTopVer);
            }
            if (type == DiscoveryCustomEvent.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);
                            }
                        }
                    }
                }
            }
            final DiscoCache discoCache;
            // event notifications, since SPI notifies manager about all events from this listener.
            if (verChanged) {
                discoCache = createDiscoCache(locNode, topSnapshot);
                discoCacheHist.put(nextTopVer, discoCache);
                boolean set = updateTopologyVersionIfGreater(nextTopVer, discoCache);
                assert set || topVer == 0 : "Topology version has not been updated [this.topVer=" + topSnap + ", topVer=" + topVer + ", node=" + node + ", evt=" + U.gridEventName(type) + ']';
            } else
                // Current version.
                discoCache = discoCache();
            // If this is a local join event, just save it and do not notify listeners.
            if (type == EVT_NODE_JOINED && node.id().equals(locNode.id())) {
                if (gridStartTime == 0)
                    gridStartTime = getSpi().getGridStartTime();
                updateTopologyVersionIfGreater(new AffinityTopologyVersion(locNode.order()), 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(topSnapshot, FILTER_DAEMON)));
                locJoin.onDone(new T2<>(discoEvt, discoCache));
                return;
            } else if (type == EVT_CLIENT_NODE_DISCONNECTED) {
                assert locNode.isClient() : locNode;
                assert node.isClient() : node;
                ((IgniteKernal) ctx.grid()).onDisconnected();
                locJoin = new GridFutureAdapter<>();
                registeredCaches.clear();
                for (AffinityTopologyVersion histVer : discoCacheHist.keySet()) {
                    Object rmvd = discoCacheHist.remove(histVer);
                    assert rmvd != null : histVer;
                }
                topHist.clear();
                topSnap.set(new Snapshot(AffinityTopologyVersion.ZERO, createDiscoCache(locNode, Collections.<ClusterNode>emptySet())));
            } else if (type == EVT_CLIENT_NODE_RECONNECTED) {
                assert locNode.isClient() : locNode;
                assert node.isClient() : node;
                boolean clusterRestarted = gridStartTime != getSpi().getGridStartTime();
                gridStartTime = getSpi().getGridStartTime();
                ((IgniteKernal) ctx.grid()).onReconnected(clusterRestarted);
                ctx.cluster().clientReconnectFuture().listen(new CI1<IgniteFuture<?>>() {

                    @Override
                    public void apply(IgniteFuture<?> fut) {
                        try {
                            fut.get();
                            discoWrk.addEvent(type, nextTopVer, node, discoCache, topSnapshot, null);
                        } catch (IgniteException ignore) {
                        // No-op.
                        }
                    }
                });
                return;
            }
            if (type == EVT_CLIENT_NODE_DISCONNECTED || type == EVT_NODE_SEGMENTED || !ctx.clientDisconnected())
                discoWrk.addEvent(type, nextTopVer, node, discoCache, topSnapshot, customMsg);
        }
    });
    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 {
                for (GridComponent c : ctx.components()) c.collectGridNodeData(dataBag);
            }
            return dataBag;
        }

        @Override
        public void onExchange(DiscoveryDataBag dataBag) {
            if (ctx.localNodeId().equals(dataBag.joiningNodeId())) {
                //NodeAdded msg reached joining node after round-trip over the ring
                for (GridComponent c : ctx.components()) {
                    if (c.discoveryDataType() != null)
                        c.onGridDataReceived(dataBag.gridDiscoveryData(c.discoveryDataType().ordinal()));
                }
            } else {
                //discovery data from newly joined node has to be applied to the current old node
                for (GridComponent c : ctx.components()) {
                    if (c.discoveryDataType() != null) {
                        JoiningNodeDiscoveryData data = dataBag.newJoinerDiscoveryData(c.discoveryDataType().ordinal());
                        if (data != null)
                            c.onJoiningNodeDataReceived(data);
                    }
                }
            }
        }
    });
    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.start();
    }
    locNode = spi.getLocalNode();
    checkAttributes(discoCache().remoteNodes());
    ctx.service().initCompatibilityMode(discoCache().remoteNodes());
    // Start discovery worker.
    new IgniteThread(discoWrk).start();
    if (log.isDebugEnabled())
        log.debug(startInfo());
}
Also used : GridComponent(org.apache.ignite.internal.GridComponent) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) IgniteFuture(org.apache.ignite.lang.IgniteFuture) CI1(org.apache.ignite.internal.util.typedef.CI1) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) JoiningNodeDiscoveryData(org.apache.ignite.spi.discovery.DiscoveryDataBag.JoiningNodeDiscoveryData) DiscoveryDataBag(org.apache.ignite.spi.discovery.DiscoveryDataBag) IgniteException(org.apache.ignite.IgniteException) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) List(java.util.List) T2(org.apache.ignite.internal.util.typedef.T2) ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteKernal(org.apache.ignite.internal.IgniteKernal) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) DiscoverySpiNodeAuthenticator(org.apache.ignite.spi.discovery.DiscoverySpiNodeAuthenticator) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) DiscoverySpiListener(org.apache.ignite.spi.discovery.DiscoverySpiListener) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) IgniteClientDisconnectedCheckedException(org.apache.ignite.internal.IgniteClientDisconnectedCheckedException) IgniteException(org.apache.ignite.IgniteException) SecurityCredentials(org.apache.ignite.plugin.security.SecurityCredentials) DiscoverySpiCustomMessage(org.apache.ignite.spi.discovery.DiscoverySpiCustomMessage) ClusterMetricsSnapshot(org.apache.ignite.internal.ClusterMetricsSnapshot) DiscoverySpiDataExchange(org.apache.ignite.spi.discovery.DiscoverySpiDataExchange) DiscoverySpi(org.apache.ignite.spi.discovery.DiscoverySpi) TcpDiscoverySpi(org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi) SecurityContext(org.apache.ignite.internal.processors.security.SecurityContext) AtomicLong(java.util.concurrent.atomic.AtomicLong) IgniteInClosure(org.apache.ignite.lang.IgniteInClosure) Collection(java.util.Collection) IgniteThread(org.apache.ignite.thread.IgniteThread)

Aggregations

IgniteThread (org.apache.ignite.thread.IgniteThread)21 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)8 IOException (java.io.IOException)3 Ignite (org.apache.ignite.Ignite)3 ClusterNode (org.apache.ignite.cluster.ClusterNode)3 DiscoveryEvent (org.apache.ignite.events.DiscoveryEvent)3 GridWorker (org.apache.ignite.internal.util.worker.GridWorker)3 Collection (java.util.Collection)2 IgniteException (org.apache.ignite.IgniteException)2 IgniteClientDisconnectedCheckedException (org.apache.ignite.internal.IgniteClientDisconnectedCheckedException)2 IgniteInterruptedCheckedException (org.apache.ignite.internal.IgniteInterruptedCheckedException)2 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)2 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)2 CI1 (org.apache.ignite.internal.util.typedef.CI1)2 SparseDistributedMatrix (org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix)2 InetSocketAddress (java.net.InetSocketAddress)1 ServerSocket (java.net.ServerSocket)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 UUID (java.util.UUID)1