Search in sources :

Example 36 with IgniteSpiException

use of org.apache.ignite.spi.IgniteSpiException in project ignite by apache.

the class SharedFsCheckpointSpi method saveCheckpoint.

/**
 * {@inheritDoc}
 */
@Override
public boolean saveCheckpoint(String key, byte[] state, long timeout, boolean overwrite) throws IgniteSpiException {
    assert key != null;
    long expireTime = 0;
    if (timeout > 0) {
        expireTime = U.currentTimeMillis() + timeout;
        if (expireTime < 0)
            expireTime = Long.MAX_VALUE;
    }
    boolean saved = false;
    while (!saved) {
        File file = new File(folder, getUniqueFileName(key));
        if (file.exists() && !overwrite)
            return false;
        File tmpFile = new File(folder, getUniqueFileName(key) + getUniqueFileName(Thread.currentThread().getName()));
        if (tmpFile.exists()) {
            if (!overwrite)
                return false;
            if (log.isDebugEnabled())
                log.warning("Overriding existing temp file: " + tmpFile.getAbsolutePath());
        }
        try {
            SharedFsUtils.write(tmpFile, new SharedFsCheckpointData(state, expireTime, host, key), marsh, log);
            if (file.exists()) {
                if (overwrite)
                    file.delete();
                else
                    return false;
            }
            if (tmpFile.renameTo(file))
                saved = true;
            else
                return false;
        } catch (IOException e) {
            // Select next shared directory if exists, otherwise throw exception
            if (getNextSharedPath() != null)
                continue;
            else
                throw new IgniteSpiException("Failed to write checkpoint data into file: " + tmpFile.getAbsolutePath(), e);
        } catch (IgniteCheckedException e) {
            throw new IgniteSpiException("Failed to marshal checkpoint data into file: " + tmpFile.getAbsolutePath(), e);
        } finally {
            tmpFile.delete();
        }
        if (timeout > 0)
            timeoutTask.add(file, new SharedFsTimeData(expireTime, file.lastModified(), key));
        saved = true;
    }
    return true;
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IOException(java.io.IOException) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) File(java.io.File)

Example 37 with IgniteSpiException

use of org.apache.ignite.spi.IgniteSpiException in project ignite by apache.

the class GridManagerAdapter method stopSpi.

/**
 * Stops wrapped SPI.
 *
 * @throws IgniteCheckedException If underlying SPI could not be stopped.
 */
protected final void stopSpi() throws IgniteCheckedException {
    for (T spi : spis) {
        if (spiMap.remove(spi) == null) {
            if (log.isDebugEnabled())
                log.debug("Will not stop SPI since it has not been started by this manager: " + spi);
            continue;
        }
        if (log.isDebugEnabled())
            log.debug("Stopping SPI: " + spi);
        try {
            spi.spiStop();
            if (log.isDebugEnabled())
                log.debug("SPI module stopped OK: " + spi.getClass().getName());
        } catch (IgniteSpiException e) {
            throw new IgniteCheckedException("Failed to stop SPI: " + spi, e);
        }
        try {
            cleanup(spi);
            ctx.resource().cleanup(spi);
        } catch (IgniteCheckedException e) {
            U.error(log, "Failed to remove injected resources from SPI (ignoring): " + spi, e);
        }
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException)

Example 38 with IgniteSpiException

use of org.apache.ignite.spi.IgniteSpiException in project ignite by apache.

the class GridManagerAdapter method startSpi.

/**
 * Starts wrapped SPI.
 *
 * @throws IgniteCheckedException If wrapped SPI could not be started.
 */
protected final void startSpi() throws IgniteCheckedException {
    Collection<String> names = U.newHashSet(spis.length);
    for (T spi : spis) {
        if (spi instanceof IgniteSpiAdapter)
            ((IgniteSpiAdapter) spi).onBeforeStart();
        // Save SPI to map to make sure to stop it properly.
        Boolean res = spiMap.put(spi, Boolean.TRUE);
        assert res == null;
        if (!injected) {
            // Inject all spi resources.
            ctx.resource().inject(spi);
            // Inject SPI internal objects.
            inject(spi);
        }
        // Print-out all SPI parameters only in DEBUG mode.
        if (log.isDebugEnabled())
            log.debug("Starting SPI: " + spi);
        if (names.contains(spi.getName()))
            throw new IgniteCheckedException("Duplicate SPI name (need to explicitly configure 'setName()' property): " + spi.getName());
        names.add(spi.getName());
        if (log.isDebugEnabled())
            log.debug("Starting SPI implementation: " + spi.getClass().getName());
        onBeforeSpiStart();
        try {
            spi.spiStart(ctx.igniteInstanceName());
        } catch (IgniteSpiException e) {
            throw new IgniteCheckedException("Failed to start SPI: " + spi, e);
        }
        onAfterSpiStart();
        parseNodeAttributes(spi);
        if (log.isDebugEnabled())
            log.debug("SPI module started OK: " + spi.getClass().getName());
    }
    injected = true;
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteSpiAdapter(org.apache.ignite.spi.IgniteSpiAdapter) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException)

Example 39 with IgniteSpiException

use of org.apache.ignite.spi.IgniteSpiException in project ignite by apache.

the class GridManagerAdapter method onKernalStart.

/**
 * {@inheritDoc}
 */
@Override
public final void onKernalStart(boolean active) throws IgniteCheckedException {
    for (final IgniteSpi spi : spis) {
        try {
            spi.onContextInitialized(new IgniteSpiContext() {

                @Override
                public boolean isStopping() {
                    return ctx.isStopping();
                }

                @Override
                public Collection<ClusterNode> remoteNodes() {
                    return ctx.discovery().remoteNodes();
                }

                @Override
                public Collection<ClusterNode> nodes() {
                    return ctx.discovery().allNodes();
                }

                @Override
                public ClusterNode localNode() {
                    return ctx.discovery().localNode();
                }

                @Override
                public Collection<ClusterNode> remoteDaemonNodes() {
                    final Collection<ClusterNode> all = ctx.discovery().daemonNodes();
                    return !localNode().isDaemon() ? all : F.view(all, new IgnitePredicate<ClusterNode>() {

                        @Override
                        public boolean apply(ClusterNode n) {
                            return n.isDaemon();
                        }
                    });
                }

                @Nullable
                @Override
                public ClusterNode node(UUID nodeId) {
                    A.notNull(nodeId, "nodeId");
                    return ctx.discovery().node(nodeId);
                }

                @Override
                public boolean pingNode(UUID nodeId) {
                    A.notNull(nodeId, "nodeId");
                    try {
                        return ctx.discovery().pingNode(nodeId);
                    } catch (IgniteCheckedException e) {
                        throw U.convertException(e);
                    }
                }

                @Override
                public void send(ClusterNode node, Serializable msg, String topic) throws IgniteSpiException {
                    A.notNull(node, "node");
                    A.notNull(msg, "msg");
                    A.notNull(topic, "topic");
                    try {
                        if (msg instanceof Message)
                            ctx.io().sendToCustomTopic(node, topic, (Message) msg, SYSTEM_POOL);
                        else
                            ctx.io().sendUserMessage(Collections.singletonList(node), msg, topic, false, 0, false);
                    } catch (IgniteCheckedException e) {
                        throw unwrapException(e);
                    }
                }

                @Override
                public void addLocalMessageListener(Object topic, IgniteBiPredicate<UUID, ?> p) {
                    A.notNull(topic, "topic");
                    A.notNull(p, "p");
                    ctx.io().addUserMessageListener(topic, p);
                }

                @Override
                public void removeLocalMessageListener(Object topic, IgniteBiPredicate<UUID, ?> p) {
                    A.notNull(topic, "topic");
                    A.notNull(topic, "p");
                    ctx.io().removeUserMessageListener(topic, p);
                }

                @Override
                public void addMessageListener(GridMessageListener lsnr, String topic) {
                    A.notNull(lsnr, "lsnr");
                    A.notNull(topic, "topic");
                    ctx.io().addMessageListener(topic, lsnr);
                }

                @Override
                public boolean removeMessageListener(GridMessageListener lsnr, String topic) {
                    A.notNull(lsnr, "lsnr");
                    A.notNull(topic, "topic");
                    return ctx.io().removeMessageListener(topic, lsnr);
                }

                @Override
                public void addLocalEventListener(GridLocalEventListener lsnr, int... types) {
                    A.notNull(lsnr, "lsnr");
                    ctx.event().addLocalEventListener(lsnr, types);
                }

                @Override
                public boolean removeLocalEventListener(GridLocalEventListener lsnr) {
                    A.notNull(lsnr, "lsnr");
                    return ctx.event().removeLocalEventListener(lsnr);
                }

                @Override
                public boolean isEventRecordable(int... types) {
                    for (int t : types) if (!ctx.event().isRecordable(t))
                        return false;
                    return true;
                }

                @Override
                public void recordEvent(Event evt) {
                    A.notNull(evt, "evt");
                    if (ctx.event().isRecordable(evt.type()))
                        ctx.event().record(evt);
                }

                @Override
                public void registerPort(int port, IgnitePortProtocol proto) {
                    ctx.ports().registerPort(port, proto, spi.getClass());
                }

                @Override
                public void deregisterPort(int port, IgnitePortProtocol proto) {
                    ctx.ports().deregisterPort(port, proto, spi.getClass());
                }

                @Override
                public void deregisterPorts() {
                    ctx.ports().deregisterPorts(spi.getClass());
                }

                @Nullable
                @Override
                public <K, V> V get(String cacheName, K key) {
                    return ctx.cache().<K, V>jcache(cacheName).get(key);
                }

                @Nullable
                @Override
                public <K, V> V put(String cacheName, K key, V val, long ttl) {
                    try {
                        if (ttl > 0) {
                            ExpiryPolicy plc = new TouchedExpiryPolicy(new Duration(MILLISECONDS, ttl));
                            IgniteCache<K, V> cache = ctx.cache().<K, V>publicJCache(cacheName).withExpiryPolicy(plc);
                            return cache.getAndPut(key, val);
                        } else
                            return ctx.cache().<K, V>jcache(cacheName).getAndPut(key, val);
                    } catch (IgniteCheckedException e) {
                        throw CU.convertToCacheException(e);
                    }
                }

                @Nullable
                @Override
                public <K, V> V putIfAbsent(String cacheName, K key, V val, long ttl) {
                    try {
                        if (ttl > 0) {
                            ExpiryPolicy plc = new TouchedExpiryPolicy(new Duration(MILLISECONDS, ttl));
                            IgniteCache<K, V> cache = ctx.cache().<K, V>publicJCache(cacheName).withExpiryPolicy(plc);
                            return cache.getAndPutIfAbsent(key, val);
                        } else
                            return ctx.cache().<K, V>jcache(cacheName).getAndPutIfAbsent(key, val);
                    } catch (IgniteCheckedException e) {
                        throw CU.convertToCacheException(e);
                    }
                }

                @Nullable
                @Override
                public <K, V> V remove(String cacheName, K key) {
                    return ctx.cache().<K, V>jcache(cacheName).getAndRemove(key);
                }

                @Override
                public <K> boolean containsKey(String cacheName, K key) {
                    return ctx.cache().cache(cacheName).containsKey(key);
                }

                @Override
                public int partition(String cacheName, Object key) {
                    return ctx.cache().cache(cacheName).affinity().partition(key);
                }

                @Override
                public IgniteNodeValidationResult validateNode(ClusterNode node) {
                    for (GridComponent comp : ctx) {
                        IgniteNodeValidationResult err = comp.validateNode(node);
                        if (err != null)
                            return err;
                    }
                    return null;
                }

                @Nullable
                @Override
                public IgniteNodeValidationResult validateNode(ClusterNode node, DiscoveryDataBag discoData) {
                    for (GridComponent comp : ctx) {
                        if (comp.discoveryDataType() == null)
                            continue;
                        IgniteNodeValidationResult err = comp.validateNode(node, discoData.newJoinerDiscoveryData(comp.discoveryDataType().ordinal()));
                        if (err != null)
                            return err;
                    }
                    return null;
                }

                @Override
                public Collection<SecuritySubject> authenticatedSubjects() {
                    try {
                        return ctx.security().authenticatedSubjects();
                    } catch (IgniteCheckedException e) {
                        throw U.convertException(e);
                    }
                }

                @Override
                public SecuritySubject authenticatedSubject(UUID subjId) {
                    try {
                        return ctx.security().authenticatedSubject(subjId);
                    } catch (IgniteCheckedException e) {
                        throw U.convertException(e);
                    }
                }

                @Override
                public MessageFormatter messageFormatter() {
                    return ctx.io().formatter();
                }

                @Override
                public MessageFactory messageFactory() {
                    return ctx.io().messageFactory();
                }

                @Override
                public boolean tryFailNode(UUID nodeId, @Nullable String warning) {
                    return ctx.discovery().tryFailNode(nodeId, warning);
                }

                @Override
                public void failNode(UUID nodeId, @Nullable String warning) {
                    ctx.discovery().failNode(nodeId, warning);
                }

                @Override
                public void addTimeoutObject(IgniteSpiTimeoutObject obj) {
                    ctx.timeout().addTimeoutObject(new GridSpiTimeoutObject(obj));
                }

                @Override
                public void removeTimeoutObject(IgniteSpiTimeoutObject obj) {
                    ctx.timeout().removeTimeoutObject(new GridSpiTimeoutObject(obj));
                }

                @Override
                public Map<String, Object> nodeAttributes() {
                    return ctx.nodeAttributes();
                }

                @Override
                public boolean communicationFailureResolveSupported() {
                    return ctx.discovery().communicationErrorResolveSupported();
                }

                @Override
                public void resolveCommunicationFailure(ClusterNode node, Exception err) {
                    ctx.discovery().resolveCommunicationError(node, err);
                }

                @Override
                public ReadOnlyMetricRegistry getOrCreateMetricRegistry(String name) {
                    return ctx.metric().registry(name);
                }

                @Override
                public void removeMetricRegistry(String name) {
                    ctx.metric().remove(name);
                }

                @Override
                public Iterable<ReadOnlyMetricRegistry> metricRegistries() {
                    return ctx.metric();
                }

                @Override
                public void addMetricRegistryCreationListener(Consumer<ReadOnlyMetricRegistry> lsnr) {
                    ctx.metric().addMetricRegistryCreationListener(lsnr);
                }

                /**
                 * @param e Exception to handle.
                 * @return GridSpiException Converted exception.
                 */
                private IgniteSpiException unwrapException(IgniteCheckedException e) {
                    // Avoid double-wrapping.
                    if (e.getCause() instanceof IgniteSpiException)
                        return (IgniteSpiException) e.getCause();
                    return new IgniteSpiException("Failed to execute SPI context method.", e);
                }
            });
        } catch (IgniteSpiException e) {
            throw new IgniteCheckedException("Failed to initialize SPI context.", e);
        }
    }
    onKernalStart0();
}
Also used : IgniteSpiContext(org.apache.ignite.spi.IgniteSpiContext) Serializable(java.io.Serializable) IgniteSpiTimeoutObject(org.apache.ignite.spi.IgniteSpiTimeoutObject) Message(org.apache.ignite.plugin.extensions.communication.Message) GridLocalEventListener(org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener) IgniteNodeValidationResult(org.apache.ignite.spi.IgniteNodeValidationResult) GridComponent(org.apache.ignite.internal.GridComponent) GridMessageListener(org.apache.ignite.internal.managers.communication.GridMessageListener) MessageFormatter(org.apache.ignite.plugin.extensions.communication.MessageFormatter) GridSpiTimeoutObject(org.apache.ignite.internal.processors.timeout.GridSpiTimeoutObject) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgnitePortProtocol(org.apache.ignite.spi.IgnitePortProtocol) DiscoveryDataBag(org.apache.ignite.spi.discovery.DiscoveryDataBag) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) UUID(java.util.UUID) ClusterNode(org.apache.ignite.cluster.ClusterNode) MessageFactory(org.apache.ignite.plugin.extensions.communication.MessageFactory) SecuritySubject(org.apache.ignite.plugin.security.SecuritySubject) IgniteCache(org.apache.ignite.IgniteCache) IgniteSpi(org.apache.ignite.spi.IgniteSpi) Duration(javax.cache.expiry.Duration) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) ReadOnlyMetricRegistry(org.apache.ignite.spi.metric.ReadOnlyMetricRegistry) Collection(java.util.Collection) Event(org.apache.ignite.events.Event) GridSpiTimeoutObject(org.apache.ignite.internal.processors.timeout.GridSpiTimeoutObject) IgniteSpiTimeoutObject(org.apache.ignite.spi.IgniteSpiTimeoutObject) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) Nullable(org.jetbrains.annotations.Nullable)

Example 40 with IgniteSpiException

use of org.apache.ignite.spi.IgniteSpiException in project ignite by apache.

the class TcpDiscoveryIpFinderAdapter method discoveryClientMode.

/**
 * @return {@code True} if TCP discovery works in client mode.
 * @deprecated Since 2.8. May return incorrect value if client and server nodes shares same {@link
 * TcpDiscoveryIpFinder} instance.
 */
@Deprecated
protected boolean discoveryClientMode() {
    boolean clientMode;
    Ignite ignite0 = ignite;
    if (ignite0 != null) {
        // Can be null if used in tests without starting Ignite.
        DiscoverySpi discoSpi = ignite0.configuration().getDiscoverySpi();
        if (!(discoSpi instanceof TcpDiscoverySpi))
            throw new IgniteSpiException("TcpDiscoveryIpFinder should be used with TcpDiscoverySpi: " + discoSpi);
        clientMode = ignite0.configuration().isClientMode() && !((TcpDiscoverySpi) discoSpi).isForceServerMode();
    } else
        clientMode = false;
    return clientMode;
}
Also used : DiscoverySpi(org.apache.ignite.spi.discovery.DiscoverySpi) TcpDiscoverySpi(org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi) Ignite(org.apache.ignite.Ignite) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) TcpDiscoverySpi(org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi)

Aggregations

IgniteSpiException (org.apache.ignite.spi.IgniteSpiException)131 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)59 IOException (java.io.IOException)32 InetSocketAddress (java.net.InetSocketAddress)22 ClusterNode (org.apache.ignite.cluster.ClusterNode)21 IgniteInterruptedCheckedException (org.apache.ignite.internal.IgniteInterruptedCheckedException)21 IgniteException (org.apache.ignite.IgniteException)20 ArrayList (java.util.ArrayList)14 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)14 HashMap (java.util.HashMap)13 UUID (java.util.UUID)13 Nullable (org.jetbrains.annotations.Nullable)12 Test (org.junit.Test)12 File (java.io.File)10 Message (org.apache.ignite.plugin.extensions.communication.Message)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 SSLException (javax.net.ssl.SSLException)8 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)8 SocketTimeoutException (java.net.SocketTimeoutException)7 Ignite (org.apache.ignite.Ignite)7