Search in sources :

Example 41 with Service

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

the class IgniteServiceProcessor method services.

/**
 * {@inheritDoc}
 */
@Override
public <T> Collection<T> services(String name) {
    if (!enterBusy())
        return null;
    try {
        ctx.security().authorize(name, SecurityPermission.SERVICE_INVOKE);
        Collection<ServiceContextImpl> ctxs = serviceContexts(name);
        if (ctxs == null)
            return null;
        synchronized (ctxs) {
            if (F.isEmpty(ctxs))
                return null;
            Collection<T> res = new ArrayList<>(ctxs.size());
            for (ServiceContextImpl ctx : ctxs) {
                Service srvc = ctx.service();
                if (srvc != null)
                    res.add((T) srvc);
            }
            return res;
        }
    } finally {
        leaveBusy();
    }
}
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)

Example 42 with Service

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

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));
                ctxs.add(srvcCtx);
                toInit.add(srvcCtx);
            }
        }
    }
    for (final ServiceContextImpl srvcCtx : toInit) {
        final Service srvc;
        try {
            srvc = copyAndInject(cfg);
            // 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() + ']');
        // 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) IgniteException(org.apache.ignite.IgniteException) ExecutorService(java.util.concurrent.ExecutorService)

Example 43 with Service

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

the class ServicesTest method testServiceDescriptors.

/**
 * Test service descriptors returned correctly.
 */
@Test
public void testServiceDescriptors() throws Exception {
    try (IgniteClient client = startClient(0)) {
        Collection<ClientServiceDescriptor> svcs = client.services().serviceDescriptors();
        assertNotNull(svcs);
        assertEquals(3, svcs.size());
        assertTrue(svcs.stream().filter(svc -> svc.name().equals(NODE_ID_SERVICE_NAME)).peek(svc -> {
            assertEquals(NODE_ID_SERVICE_NAME, svc.name());
            assertEquals(TestNodeIdService.class.getName(), svc.serviceClass());
            assertEquals(0, svc.totalCount());
            assertEquals(1, svc.maxPerNodeCount());
            assertNull(svc.cacheName());
            assertEquals(grid(0).localNode().id(), svc.originNodeId());
            assertEquals(PlatformType.JAVA, svc.platformType());
            assertDescriptorsEquals(svc, client.services().serviceDescriptor(NODE_ID_SERVICE_NAME));
        }).findFirst().isPresent());
        assertTrue(svcs.stream().filter(svc -> svc.name().equals(NODE_SINGLTON_SERVICE_NAME)).peek(svc -> {
            assertEquals(NODE_SINGLTON_SERVICE_NAME, svc.name());
            assertEquals(TestService.class.getName(), svc.serviceClass());
            assertEquals(0, svc.totalCount());
            assertEquals(1, svc.maxPerNodeCount());
            assertNull(svc.cacheName());
            assertEquals(grid(0).localNode().id(), svc.originNodeId());
            assertEquals(PlatformType.JAVA, svc.platformType());
            assertDescriptorsEquals(svc, client.services().serviceDescriptor(NODE_SINGLTON_SERVICE_NAME));
        }).findFirst().isPresent());
        assertTrue(svcs.stream().filter(svc -> svc.name().equals(CLUSTER_SINGLTON_SERVICE_NAME)).peek(svc -> {
            assertEquals(CLUSTER_SINGLTON_SERVICE_NAME, svc.name());
            assertEquals(TestService.class.getName(), svc.serviceClass());
            assertEquals(1, svc.totalCount());
            assertEquals(1, svc.maxPerNodeCount());
            assertEquals(DEFAULT_CACHE_NAME, svc.cacheName());
            assertEquals(grid(0).localNode().id(), svc.originNodeId());
            assertEquals(PlatformType.JAVA, svc.platformType());
            assertDescriptorsEquals(svc, client.services().serviceDescriptor(CLUSTER_SINGLTON_SERVICE_NAME));
        }).findFirst().isPresent());
        assertThrowsWithCause(() -> {
            client.services().serviceDescriptor("unknown");
        }, ClientException.class);
    }
}
Also used : IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) ClientClusterGroup(org.apache.ignite.client.ClientClusterGroup) F(org.apache.ignite.internal.util.typedef.F) Arrays(java.util.Arrays) Person(org.apache.ignite.client.Person) GridTestUtils.assertThrowsWithCause(org.apache.ignite.testframework.GridTestUtils.assertThrowsWithCause) Collection(java.util.Collection) U(org.apache.ignite.internal.util.typedef.internal.U) PlatformType(org.apache.ignite.platform.PlatformType) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) Test(org.junit.Test) UUID(java.util.UUID) Ignite(org.apache.ignite.Ignite) ClientServiceDescriptor(org.apache.ignite.client.ClientServiceDescriptor) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) HashSet(java.util.HashSet) ClientException(org.apache.ignite.client.ClientException) ServiceContext(org.apache.ignite.services.ServiceContext) IgniteClient(org.apache.ignite.client.IgniteClient) Map(java.util.Map) Service(org.apache.ignite.services.Service) IgniteClient(org.apache.ignite.client.IgniteClient) ClientServiceDescriptor(org.apache.ignite.client.ClientServiceDescriptor) Test(org.junit.Test)

Example 44 with Service

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

the class GridServiceProcessorMultiNodeSelfTest method testDeployOnEachNodeButClientUpdateTopology.

/**
 * @throws Exception If failed.
 */
@Test
public void testDeployOnEachNodeButClientUpdateTopology() throws Exception {
    // Prestart client node.
    Ignite client = startGrid("client", getConfiguration("client").setClientMode(true));
    try {
        final String name = "serviceOnEachNodeButClientUpdateTopology";
        IgniteEx g = randomGrid();
        CountDownLatch latch = new CountDownLatch(nodeCount());
        DummyService.exeLatch(name, latch);
        IgniteServices svcs = g.services();
        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, nodeCount(), DummyService.started(name));
        assertEquals(name, 0, DummyService.cancelled(name));
        int servers = 2;
        latch = new CountDownLatch(servers);
        DummyService.exeLatch(name, latch);
        int clients = 2;
        startExtraNodes(servers, clients);
        try {
            latch.await();
            waitForDeployment(name, servers);
            // 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, nodeCount() + servers, DummyService.started(name) - DummyService.cancelled(name));
            checkCount(name, g, nodeCount() + servers);
        } 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 45 with Service

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

the class GridServiceProcessorMultiNodeSelfTest method testDeployOnEachNodeUpdateTopology.

/**
 * @throws Exception If failed.
 */
@Test
public void testDeployOnEachNodeUpdateTopology() throws Exception {
    // Prestart client node.
    Ignite client = startGrid("client", getConfiguration("client").setClientMode(true));
    try {
        final String name = "serviceOnEachNodeUpdateTopology";
        IgniteEx g = randomGrid();
        final int prestartedNodes = nodeCount() + 1;
        CountDownLatch latch = new CountDownLatch(prestartedNodes);
        DummyService.exeLatch(name, latch);
        ServiceConfiguration srvcCfg = new ServiceConfiguration();
        srvcCfg.setNodeFilter(new CacheConfiguration.IgniteAllNodesPredicate());
        srvcCfg.setName(name);
        srvcCfg.setMaxPerNodeCount(1);
        srvcCfg.setService(new DummyService());
        IgniteServices svcs = g.services();
        IgniteFuture<?> fut = svcs.deployAsync(srvcCfg);
        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, prestartedNodes, DummyService.started(name));
        assertEquals(name, 0, DummyService.cancelled(name));
        int servers = 2;
        int clients = 2;
        int extraNodes = servers + clients;
        latch = new CountDownLatch(extraNodes);
        DummyService.exeLatch(name, latch);
        startExtraNodes(servers, clients);
        try {
            latch.await();
            waitForDeployment(name, prestartedNodes + extraNodes);
            // 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, prestartedNodes + extraNodes, DummyService.started(name) - DummyService.cancelled(name));
            checkCount(name, g, prestartedNodes + extraNodes);
        } finally {
            stopExtraNodes(extraNodes);
        }
    } finally {
        stopGrid("client");
    }
}
Also used : ServiceConfiguration(org.apache.ignite.services.ServiceConfiguration) 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) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) Test(org.junit.Test)

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