Search in sources :

Example 21 with Service

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

the class GridServiceProcessorMultiNodeSelfTest method testDeployOnEachProjectionNodeUpdateTopology.

/**
 * @throws Exception If failed.
 */
@Test
public void testDeployOnEachProjectionNodeUpdateTopology() throws Exception {
    // Prestart client node.
    Ignite client = startGrid("client", getConfiguration("client").setClientMode(true));
    try {
        final String name = "serviceOnEachProjectionNodeUpdateTopology";
        IgniteEx g = randomGrid();
        int prestartedSrvcs = 1;
        CountDownLatch latch = new CountDownLatch(prestartedSrvcs);
        DummyService.exeLatch(name, latch);
        IgniteServices svcs = g.services(g.cluster().forClients());
        IgniteFuture<?> fut = svcs.deployNodeSingletonAsync(name, new DummyService());
        info("Deployed service: " + name);
        fut.get();
        info("Finished waiting for service future: " + name);
        latch.await();
        // Ensure service is deployed
        assertNotNull(client.services().serviceProxy(name, Service.class, false, 2000));
        assertEquals(name, prestartedSrvcs, DummyService.started(name));
        assertEquals(name, 0, DummyService.cancelled(name));
        int servers = 2;
        int clients = 2;
        latch = new CountDownLatch(clients);
        DummyService.exeLatch(name, latch);
        startExtraNodes(servers, clients);
        try {
            latch.await();
            waitForDeployment(name, clients);
            // Since we start extra nodes, there may be extra start and cancel events,
            // so we check only the difference between start and cancel and
            // not start and cancel events individually.
            assertEquals(name, clients + prestartedSrvcs, DummyService.started(name) - DummyService.cancelled(name));
            checkCount(name, g, clients + prestartedSrvcs);
        } finally {
            stopExtraNodes(servers + clients);
        }
    } finally {
        stopGrid("client");
    }
}
Also used : IgniteServices(org.apache.ignite.IgniteServices) IgniteEx(org.apache.ignite.internal.IgniteEx) Service(org.apache.ignite.services.Service) Ignite(org.apache.ignite.Ignite) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 22 with Service

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

the class GridServiceProcessorProxySelfTest method testSingletonProxyInvocation.

/**
 * @throws Exception If failed.
 */
@Test
public void testSingletonProxyInvocation() throws Exception {
    final String name = "testProxyInvocationFromSeveralNodes";
    final Ignite ignite = grid(0);
    ignite.services(ignite.cluster().forLocal()).deployClusterSingleton(name, new MapServiceImpl<String, Integer>());
    for (int i = 1; i < nodeCount(); i++) {
        MapService<Integer, String> svc = grid(i).services().serviceProxy(name, MapService.class, false, 1_000L);
        // Make sure service is a proxy.
        assertFalse(svc instanceof Service);
        svc.put(i, Integer.toString(i));
    }
    assertEquals(nodeCount() - 1, ignite.services().serviceProxy(name, MapService.class, false).size());
}
Also used : Service(org.apache.ignite.services.Service) Ignite(org.apache.ignite.Ignite) Test(org.junit.Test)

Example 23 with Service

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

the class GridServiceProcessorProxySelfTest method testRemoteNotStickProxyInvocation.

/**
 * @throws Exception If failed.
 */
@Test
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) Test(org.junit.Test)

Example 24 with Service

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

the class IgniteServiceProcessor method redeploy.

/**
 * Redeploys local services based on assignments.
 * <p/>
 * Invokes from services deployment worker.
 *
 * @param srvcId Service id.
 * @param cfg Service configuration.
 * @param top Service topology.
 * @throws IgniteCheckedException In case of deployment errors.
 */
void redeploy(IgniteUuid srvcId, ServiceConfiguration cfg, Map<UUID, Integer> top) throws IgniteCheckedException {
    String name = cfg.getName();
    String cacheName = cfg.getCacheName();
    Object affKey = cfg.getAffinityKey();
    int assignCnt = top.getOrDefault(ctx.localNodeId(), 0);
    Collection<ServiceContextImpl> ctxs = locServices.computeIfAbsent(srvcId, c -> new ArrayList<>());
    Collection<ServiceContextImpl> toInit = new ArrayList<>();
    synchronized (ctxs) {
        if (ctxs.size() > assignCnt) {
            int cancelCnt = ctxs.size() - assignCnt;
            cancel(ctxs, cancelCnt);
        } else if (ctxs.size() < assignCnt) {
            int createCnt = assignCnt - ctxs.size();
            for (int i = 0; i < createCnt; i++) {
                ServiceContextImpl srvcCtx = new ServiceContextImpl(name, UUID.randomUUID(), cacheName, affKey, Executors.newSingleThreadExecutor(threadFactory), cfg.isStatisticsEnabled());
                ctxs.add(srvcCtx);
                toInit.add(srvcCtx);
            }
        }
    }
    ReadOnlyMetricRegistry invocationMetrics = null;
    for (final ServiceContextImpl srvcCtx : toInit) {
        final Service srvc;
        try {
            srvc = copyAndInject(cfg, srvcCtx);
            // Initialize service.
            srvc.init(srvcCtx);
            srvcCtx.service(srvc);
        } catch (Throwable e) {
            U.error(log, "Failed to initialize service (service will not be deployed): " + name, e);
            synchronized (ctxs) {
                ctxs.removeAll(toInit);
            }
            throw new IgniteCheckedException("Error occured during service initialization: " + "[locId=" + ctx.localNodeId() + ", name=" + name + ']', e);
        }
        if (log.isInfoEnabled())
            log.info("Starting service instance [name=" + srvcCtx.name() + ", execId=" + srvcCtx.executionId() + ']');
        if (cfg.isStatisticsEnabled()) {
            if (invocationMetrics == null)
                invocationMetrics = createServiceMetrics(srvcCtx);
            srvcCtx.metrics(invocationMetrics);
        }
        // Start service in its own thread.
        final ExecutorService exe = srvcCtx.executor();
        exe.execute(new Runnable() {

            @Override
            public void run() {
                try {
                    srvc.execute(srvcCtx);
                } catch (InterruptedException | IgniteInterruptedCheckedException ignore) {
                    if (log.isDebugEnabled())
                        log.debug("Service thread was interrupted [name=" + srvcCtx.name() + ", execId=" + srvcCtx.executionId() + ']');
                } catch (IgniteException e) {
                    if (e.hasCause(InterruptedException.class) || e.hasCause(IgniteInterruptedCheckedException.class)) {
                        if (log.isDebugEnabled())
                            log.debug("Service thread was interrupted [name=" + srvcCtx.name() + ", execId=" + srvcCtx.executionId() + ']');
                    } else {
                        U.error(log, "Service execution stopped with error [name=" + srvcCtx.name() + ", execId=" + srvcCtx.executionId() + ']', e);
                    }
                } catch (Throwable e) {
                    U.error(log, "Service execution stopped with error [name=" + srvcCtx.name() + ", execId=" + srvcCtx.executionId() + ']', e);
                    if (e instanceof Error)
                        throw (Error) e;
                } finally {
                    // Suicide.
                    exe.shutdownNow();
                }
            }
        });
    }
}
Also used : ArrayList(java.util.ArrayList) PlatformService(org.apache.ignite.internal.processors.platform.services.PlatformService) ExecutorService(java.util.concurrent.ExecutorService) Service(org.apache.ignite.services.Service) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) ReadOnlyMetricRegistry(org.apache.ignite.spi.metric.ReadOnlyMetricRegistry) IgniteException(org.apache.ignite.IgniteException) ExecutorService(java.util.concurrent.ExecutorService)

Example 25 with Service

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

the class IgniteServiceDeployment2ClassLoadersDefaultMarshallerTest method serviceConfig.

/**
 * @param firstGrp First group flag.
 * @return Service configuration.
 * @throws Exception If failed.
 */
private ServiceConfiguration serviceConfig(final boolean firstGrp) throws Exception {
    ServiceConfiguration srvCfg = new ServiceConfiguration();
    srvCfg.setNodeFilter(new TestNodeFilter(firstGrp ? grp1 : grp2));
    Class<Service> srvcCls;
    if (firstGrp)
        srvcCls = (Class<Service>) extClsLdr1.loadClass(NOOP_SERVICE_CLS_NAME);
    else
        srvcCls = (Class<Service>) extClsLdr2.loadClass(NOOP_SERVICE_2_CLS_NAME);
    Service srvc = srvcCls.newInstance();
    srvCfg.setService(srvc);
    srvCfg.setName("TestDeploymentService" + (firstGrp ? 1 : 2));
    srvCfg.setMaxPerNodeCount(1);
    return srvCfg;
}
Also used : ServiceConfiguration(org.apache.ignite.services.ServiceConfiguration) Service(org.apache.ignite.services.Service)

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