Search in sources :

Example 1 with IgniteSpiContext

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

the class GridManagerAdapter method onKernalStart.

/** {@inheritDoc} */
@Override
public final void onKernalStart(boolean activeOnStart) 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);
                }

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

                @SuppressWarnings("deprecation")
                @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;
                }

                @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();
                }

                /**
                     * @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) 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) Collection(java.util.Collection) Event(org.apache.ignite.events.Event) IgniteSpiTimeoutObject(org.apache.ignite.spi.IgniteSpiTimeoutObject) GridSpiTimeoutObject(org.apache.ignite.internal.processors.timeout.GridSpiTimeoutObject) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with IgniteSpiContext

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

the class JobStealingCollisionSpi method checkBusy.

/**
     * Check if node is busy and activate/reject proper number of jobs.
     *
     * @param waitJobs Waiting jobs.
     * @param activeJobs Active jobs.
     * @return Number of rejected jobs.
     */
private int checkBusy(Collection<CollisionJobContext> waitJobs, Collection<CollisionJobContext> activeJobs) {
    int activeSize = activeJobs.size();
    int waitSize = waitJobs.size();
    waitingNum = waitJobs.size();
    runningNum = activeSize;
    IgniteSpiContext ctx = getSpiContext();
    int activated = 0;
    int rejected = 0;
    Collection<CollisionJobContext> waitPriJobs = sortJobs(waitJobs, waitSize);
    int activeJobsThreshold0 = activeJobsThreshold;
    int waitJobsThreshold0 = waitJobsThreshold;
    for (CollisionJobContext waitCtx : waitPriJobs) {
        if (activeJobs.size() < activeJobsThreshold0) {
            activated++;
            // We also need to make sure that job is not being rejected by another thread.
            synchronized (waitCtx.getJobContext()) {
                waitCtx.activate();
            }
        } else if (stealReqs.get() > 0) {
            if (waitCtx.getJob().getClass().isAnnotationPresent(JobStealingDisabled.class))
                continue;
            // Collision count attribute.
            Integer stealingCnt = waitCtx.getJobContext().getAttribute(STEALING_ATTEMPT_COUNT_ATTR);
            // has not been exceeded.
            if (stealingCnt != null) {
                // If job exceeded failover threshold, skip it.
                if (stealingCnt >= maxStealingAttempts) {
                    if (log.isDebugEnabled())
                        log.debug("Waiting job exceeded stealing attempts and won't be rejected " + "(will try other jobs on waiting list): " + waitCtx);
                    continue;
                }
            } else
                stealingCnt = 0;
            // Check if allowed to reject job.
            int jobsToReject = waitPriJobs.size() - activated - rejected - waitJobsThreshold0;
            if (log.isDebugEnabled())
                log.debug("Jobs to reject count [jobsToReject=" + jobsToReject + ", waitCtx=" + waitCtx + ']');
            if (jobsToReject <= 0)
                break;
            Integer pri = waitCtx.getJobContext().getAttribute(STEALING_PRIORITY_ATTR);
            if (pri == null)
                pri = DFLT_JOB_PRIORITY;
            // counter to prevent excessive iteration over nodes under load.
            for (Iterator<Entry<UUID, MessageInfo>> iter = rcvMsgMap.entrySet().iterator(); iter.hasNext() && stealReqs.get() > 0; ) {
                Entry<UUID, MessageInfo> entry = iter.next();
                UUID nodeId = entry.getKey();
                // Node has left topology.
                if (ctx.node(nodeId) == null) {
                    iter.remove();
                    continue;
                }
                MessageInfo info = entry.getValue();
                synchronized (info) {
                    int jobsAsked = info.jobsToSteal();
                    assert jobsAsked >= 0;
                    // Skip nodes that have not asked for jobs to steal.
                    if (jobsAsked == 0)
                        // Move to next node.
                        continue;
                    // If message is expired, ignore it.
                    if (info.expired()) {
                        // Subtract expired messages.
                        stealReqs.addAndGet(-info.jobsToSteal());
                        info.reset(0);
                        continue;
                    }
                    // Check that waiting job has thief node in topology.
                    boolean found = false;
                    for (UUID id : waitCtx.getTaskSession().getTopology()) {
                        if (id.equals(nodeId)) {
                            found = true;
                            break;
                        }
                    }
                    if (!found) {
                        if (log.isDebugEnabled())
                            log.debug("Thief node does not belong to task topology [thief=" + nodeId + ", task=" + waitCtx.getTaskSession() + ']');
                        continue;
                    }
                    if (stealReqs.get() <= 0)
                        break;
                    // rejected by another thread.
                    synchronized (waitCtx.getJobContext()) {
                        boolean cancel = waitCtx.getJobContext().getAttribute(THIEF_NODE_ATTR) == null;
                        if (cancel) {
                            // Mark job as stolen.
                            waitCtx.getJobContext().setAttribute(THIEF_NODE_ATTR, nodeId);
                            waitCtx.getJobContext().setAttribute(STEALING_ATTEMPT_COUNT_ATTR, stealingCnt + 1);
                            waitCtx.getJobContext().setAttribute(STEALING_PRIORITY_ATTR, pri + 1);
                            if (log.isDebugEnabled())
                                log.debug("Will try to reject job due to steal request [ctx=" + waitCtx + ", thief=" + nodeId + ']');
                            int i = stealReqs.decrementAndGet();
                            if (i >= 0 && waitCtx.cancel()) {
                                rejected++;
                                info.reset(jobsAsked - 1);
                                if (log.isDebugEnabled())
                                    log.debug("Rejected job due to steal request [ctx=" + waitCtx + ", nodeId=" + nodeId + ']');
                            } else {
                                if (log.isDebugEnabled())
                                    log.debug("Failed to reject job [i=" + i + ']');
                                waitCtx.getJobContext().setAttribute(THIEF_NODE_ATTR, null);
                                waitCtx.getJobContext().setAttribute(STEALING_ATTEMPT_COUNT_ATTR, stealingCnt);
                                waitCtx.getJobContext().setAttribute(STEALING_PRIORITY_ATTR, pri);
                                stealReqs.incrementAndGet();
                            }
                        }
                    }
                    // Move to next job.
                    break;
                }
            }
        } else
            // No more jobs to steal or activate.
            break;
    }
    return rejected;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteSpiContext(org.apache.ignite.spi.IgniteSpiContext) Entry(java.util.Map.Entry) Iterator(java.util.Iterator) CollisionJobContext(org.apache.ignite.spi.collision.CollisionJobContext) UUID(java.util.UUID)

Example 3 with IgniteSpiContext

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

the class GridManagerLocalMessageListenerSelfTest method testSendMessage.

/**
     * @throws Exception If failed.
     */
public void testSendMessage() throws Exception {
    startGridsMultiThreaded(2);
    IgniteSpiContext ctx0 = ((IgniteSpiAdapter) grid(0).context().io().getSpi()).getSpiContext();
    IgniteSpiContext ctx1 = ((IgniteSpiAdapter) grid(1).context().io().getSpi()).getSpiContext();
    String topic = "test-topic";
    final CountDownLatch latch = new CountDownLatch(1);
    ctx1.addLocalMessageListener(topic, new IgniteBiPredicate<UUID, Object>() {

        @Override
        public boolean apply(UUID nodeId, Object msg) {
            assertEquals("Message", msg);
            latch.countDown();
            return true;
        }
    });
    long time = System.nanoTime();
    ctx0.send(grid(1).localNode(), "Message", topic);
    assert latch.await(3, SECONDS);
    time = System.nanoTime() - time;
    info(">>>");
    info(">>> send() time (ms): " + MILLISECONDS.convert(time, NANOSECONDS));
    info(">>>");
}
Also used : IgniteSpiContext(org.apache.ignite.spi.IgniteSpiContext) IgniteSpiAdapter(org.apache.ignite.spi.IgniteSpiAdapter) CountDownLatch(java.util.concurrent.CountDownLatch) UUID(java.util.UUID)

Aggregations

UUID (java.util.UUID)3 IgniteSpiContext (org.apache.ignite.spi.IgniteSpiContext)3 Serializable (java.io.Serializable)1 Collection (java.util.Collection)1 IdentityHashMap (java.util.IdentityHashMap)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Duration (javax.cache.expiry.Duration)1 ExpiryPolicy (javax.cache.expiry.ExpiryPolicy)1 TouchedExpiryPolicy (javax.cache.expiry.TouchedExpiryPolicy)1 IgniteCache (org.apache.ignite.IgniteCache)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 ClusterNode (org.apache.ignite.cluster.ClusterNode)1 Event (org.apache.ignite.events.Event)1 GridComponent (org.apache.ignite.internal.GridComponent)1 GridMessageListener (org.apache.ignite.internal.managers.communication.GridMessageListener)1 GridLocalEventListener (org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener)1