Search in sources :

Example 31 with Service

use of org.apache.ignite.services.Service in project ignite by apache.

the class GridServiceProcessorProxySelfTest method testLocalProxyInvocation.

/**
 * @throws Exception If failed.
 */
public void testLocalProxyInvocation() throws Exception {
    final String name = "testLocalProxyInvocation";
    final Ignite ignite = grid(0);
    ignite.services().deployNodeSingleton(name, new MapServiceImpl<String, Integer>());
    for (int i = 0; i < nodeCount(); i++) {
        final int idx = i;
        final AtomicReference<MapService<Integer, String>> ref = new AtomicReference<>();
        // wait because after deployNodeSingleton we don't have guarantees what service was deploy.
        boolean wait = GridTestUtils.waitForCondition(new PA() {

            @Override
            public boolean apply() {
                MapService<Integer, String> svc = grid(idx).services().serviceProxy(name, MapService.class, false);
                ref.set(svc);
                return svc instanceof Service;
            }
        }, 2000);
        // Make sure service is a local instance.
        assertTrue("Invalid service instance [srv=" + ref.get() + ", node=" + i + ']', wait);
        ref.get().put(i, Integer.toString(i));
    }
    MapService<Integer, String> map = ignite.services().serviceProxy(name, MapService.class, false);
    for (int i = 0; i < nodeCount(); i++) assertEquals(1, map.size());
}
Also used : Service(org.apache.ignite.services.Service) AtomicReference(java.util.concurrent.atomic.AtomicReference) PA(org.apache.ignite.internal.util.typedef.PA) Ignite(org.apache.ignite.Ignite)

Example 32 with Service

use of org.apache.ignite.services.Service in project ignite by apache.

the class GridServiceProcessorProxySelfTest method testRemoteNotStickProxyInvocation.

/**
 * @throws Exception If failed.
 */
public void testRemoteNotStickProxyInvocation() throws Exception {
    final String name = "testRemoteNotStickProxyInvocation";
    final Ignite ignite = grid(0);
    ignite.services().deployNodeSingleton(name, new MapServiceImpl<String, Integer>());
    // Get remote proxy.
    MapService<Integer, String> svc = ignite.services(ignite.cluster().forRemotes()).serviceProxy(name, MapService.class, false);
    // Make sure service is a local instance.
    assertFalse(svc instanceof Service);
    for (int i = 0; i < nodeCount(); i++) svc.put(i, Integer.toString(i));
    int size = 0;
    for (ClusterNode n : ignite.cluster().forRemotes().nodes()) {
        MapService<Integer, String> map = ignite.services(ignite.cluster().forNode(n)).serviceProxy(name, MapService.class, false);
        // Make sure service is a local instance.
        assertFalse(map instanceof Service);
        size += map.size();
    }
    assertEquals(nodeCount(), size);
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) Service(org.apache.ignite.services.Service) Ignite(org.apache.ignite.Ignite)

Example 33 with Service

use of org.apache.ignite.services.Service in project ignite by apache.

the class GridServiceProcessor method cancel.

/**
 * @param ctxs Contexts to cancel.
 * @param cancelCnt Number of contexts to cancel.
 */
private void cancel(Iterable<ServiceContextImpl> ctxs, int cancelCnt) {
    for (Iterator<ServiceContextImpl> it = ctxs.iterator(); it.hasNext(); ) {
        ServiceContextImpl svcCtx = it.next();
        // Flip cancelled flag.
        svcCtx.setCancelled(true);
        // Notify service about cancellation.
        Service svc = svcCtx.service();
        if (svc != null) {
            try {
                svc.cancel(svcCtx);
            } catch (Throwable e) {
                log.error("Failed to cancel service (ignoring) [name=" + svcCtx.name() + ", execId=" + svcCtx.executionId() + ']', e);
                if (e instanceof Error)
                    throw e;
            } finally {
                try {
                    ctx.resource().cleanup(svc);
                } catch (IgniteCheckedException e) {
                    U.error(log, "Failed to clean up service (will ignore): " + svcCtx.name(), e);
                }
            }
        }
        // Close out executor thread for the service.
        // This will cause the thread to be interrupted.
        svcCtx.executor().shutdownNow();
        it.remove();
        if (log.isInfoEnabled())
            log.info("Cancelled service instance [name=" + svcCtx.name() + ", execId=" + svcCtx.executionId() + ']');
        if (--cancelCnt == 0)
            break;
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) ExecutorService(java.util.concurrent.ExecutorService) Service(org.apache.ignite.services.Service)

Example 34 with Service

use of org.apache.ignite.services.Service in project ignite by apache.

the class GridServiceProcessor method services.

/**
 * @param name Service name.
 * @param <T> Service type.
 * @return Services by specified service name.
 */
@SuppressWarnings("unchecked")
public <T> Collection<T> services(String name) {
    ctx.security().authorize(name, SecurityPermission.SERVICE_INVOKE, null);
    Collection<ServiceContextImpl> ctxs;
    synchronized (locSvcs) {
        ctxs = locSvcs.get(name);
    }
    if (ctxs == null)
        return null;
    synchronized (ctxs) {
        Collection<T> res = new ArrayList<>(ctxs.size());
        for (ServiceContextImpl ctx : ctxs) {
            Service svc = ctx.service();
            if (svc != null)
                res.add((T) svc);
        }
        return res;
    }
}
Also used : LT(org.apache.ignite.internal.util.typedef.internal.LT) ArrayList(java.util.ArrayList) ExecutorService(java.util.concurrent.ExecutorService) Service(org.apache.ignite.services.Service)

Example 35 with Service

use of org.apache.ignite.services.Service in project ignite by apache.

the class GridServiceProcessor method onKernalStop.

/**
 * {@inheritDoc}
 */
@Override
public void onKernalStop(boolean cancel) {
    if (ctx.isDaemon())
        return;
    GridSpinBusyLock busyLock = this.busyLock;
    // Will not release it.
    if (busyLock != null) {
        busyLock.block();
        this.busyLock = null;
    }
    startLatch.countDown();
    U.shutdownNow(GridServiceProcessor.class, depExe, log);
    if (!ctx.clientNode())
        ctx.event().removeDiscoveryEventListener(topLsnr);
    Collection<ServiceContextImpl> ctxs = new ArrayList<>();
    synchronized (locSvcs) {
        for (Collection<ServiceContextImpl> ctxs0 : locSvcs.values()) ctxs.addAll(ctxs0);
    }
    for (ServiceContextImpl ctx : ctxs) {
        ctx.setCancelled(true);
        Service svc = ctx.service();
        if (svc != null)
            try {
                svc.cancel(ctx);
            } catch (Throwable e) {
                log.error("Failed to cancel service (ignoring) [name=" + ctx.name() + ", execId=" + ctx.executionId() + ']', e);
                if (e instanceof Error)
                    throw e;
            }
        ctx.executor().shutdownNow();
    }
    for (ServiceContextImpl ctx : ctxs) {
        try {
            if (log.isInfoEnabled() && !ctxs.isEmpty())
                log.info("Shutting down distributed service [name=" + ctx.name() + ", execId8=" + U.id8(ctx.executionId()) + ']');
            ctx.executor().awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
        } catch (InterruptedException ignore) {
            Thread.currentThread().interrupt();
            U.error(log, "Got interrupted while waiting for service to shutdown (will continue stopping node): " + ctx.name());
        }
    }
    Exception err = new IgniteCheckedException("Operation has been cancelled (node is stopping).");
    cancelFutures(depFuts, err);
    cancelFutures(undepFuts, err);
    if (log.isDebugEnabled())
        log.debug("Stopped service processor.");
}
Also used : GridSpinBusyLock(org.apache.ignite.internal.util.GridSpinBusyLock) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) ArrayList(java.util.ArrayList) ExecutorService(java.util.concurrent.ExecutorService) Service(org.apache.ignite.services.Service) ServiceDeploymentException(org.apache.ignite.services.ServiceDeploymentException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) SecurityException(org.apache.ignite.plugin.security.SecurityException) IgniteClientDisconnectedCheckedException(org.apache.ignite.internal.IgniteClientDisconnectedCheckedException) IgniteException(org.apache.ignite.IgniteException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Aggregations

Service (org.apache.ignite.services.Service)50 ExecutorService (java.util.concurrent.ExecutorService)22 PlatformService (org.apache.ignite.internal.processors.platform.services.PlatformService)18 Ignite (org.apache.ignite.Ignite)17 Test (org.junit.Test)17 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)15 ArrayList (java.util.ArrayList)11 ClusterNode (org.apache.ignite.cluster.ClusterNode)10 CountDownLatch (java.util.concurrent.CountDownLatch)9 IgniteException (org.apache.ignite.IgniteException)9 IgniteEx (org.apache.ignite.internal.IgniteEx)9 ServiceConfiguration (org.apache.ignite.services.ServiceConfiguration)8 IgniteServices (org.apache.ignite.IgniteServices)6 IgniteInterruptedCheckedException (org.apache.ignite.internal.IgniteInterruptedCheckedException)6 IgniteInterruptedException (org.apache.ignite.IgniteInterruptedException)4 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)4 ServiceContext (org.apache.ignite.services.ServiceContext)4 GridTestUtils (org.apache.ignite.testframework.GridTestUtils)4 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)4 IOException (java.io.IOException)3