Search in sources :

Example 16 with IgniteServices

use of org.apache.ignite.IgniteServices in project ignite by apache.

the class ServicesExample method main.

/**
 * Executes example.
 *
 * @param args Command line arguments, none required.
 * @throws Exception If example execution failed.
 */
public static void main(String[] args) throws Exception {
    // Mark this node as client node.
    Ignition.setClientMode(true);
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        if (!ExamplesUtils.hasServerNodes(ignite))
            return;
        // Deploy services only on server nodes.
        IgniteServices svcs = ignite.services(ignite.cluster().forServers());
        try {
            // Deploy cluster singleton.
            svcs.deployClusterSingleton("myClusterSingletonService", new SimpleMapServiceImpl());
            // Deploy node singleton.
            svcs.deployNodeSingleton("myNodeSingletonService", new SimpleMapServiceImpl());
            // Deploy 2 instances, regardless of number nodes.
            svcs.deployMultiple("myMultiService", new SimpleMapServiceImpl(), 2, /*total number*/
            0);
            // Example for using a service proxy
            // to access a remotely deployed service.
            serviceProxyExample(ignite);
            // Example for auto-injecting service proxy
            // into remote closure execution.
            serviceInjectionExample(ignite);
        } finally {
            // Undeploy all services.
            ignite.services().cancelAll();
        }
    }
}
Also used : IgniteServices(org.apache.ignite.IgniteServices) Ignite(org.apache.ignite.Ignite)

Example 17 with IgniteServices

use of org.apache.ignite.IgniteServices in project ignite by apache.

the class ClientServiceInvokeRequest method process.

/**
 * {@inheritDoc}
 */
@Override
public ClientResponse process(ClientConnectionContext ctx) {
    if (F.isEmpty(name))
        throw new IgniteException("Service name can't be empty");
    if (F.isEmpty(methodName))
        throw new IgniteException("Method name can't be empty");
    ServiceDescriptor desc = findServiceDescriptor(ctx, name);
    Class<?> svcCls = desc.serviceClass();
    ClusterGroupAdapter grp = ctx.kernalContext().cluster().get();
    grp = (ClusterGroupAdapter) (nodeIds.isEmpty() ? grp.forServers() : grp.forNodeIds(nodeIds));
    IgniteServices services = grp.services();
    try {
        Object res;
        if (PlatformService.class.isAssignableFrom(svcCls)) {
            // Never deserialize platform service arguments and result: may contain platform-only types.
            PlatformService proxy = ((IgniteServicesImpl) services).serviceProxy(name, PlatformService.class, false, timeout, true);
            res = proxy.invokeMethod(methodName, keepBinary(), false, args, callAttrs);
        } else {
            // Deserialize Java service arguments when not in keepBinary mode.
            if (!keepBinary() && args.length > 0) {
                for (int i = 0; i < args.length; i++) {
                    if (paramTypeIds != null)
                        // Skip parameter typeId, we already read it in constructor.
                        reader.readInt();
                    args[i] = reader.readObject();
                }
            }
            GridServiceProxy<?> proxy = new GridServiceProxy<>(grp, name, Service.class, false, timeout, ctx.kernalContext(), null, true);
            Method method = resolveMethod(ctx, svcCls);
            if (!BinaryArray.useBinaryArrays())
                PlatformServices.convertArrayArgs(args, method);
            res = proxy.invokeMethod(method, args, callAttrs);
        }
        return new ClientObjectResponse(requestId(), res);
    } catch (PlatformNativeException e) {
        ctx.kernalContext().log(getClass()).error("Failed to invoke platform service", e);
        throw new IgniteException("Failed to invoke platform service, see server logs for details");
    } catch (Throwable e) {
        throw new IgniteException(e);
    }
}
Also used : GridServiceProxy(org.apache.ignite.internal.processors.service.GridServiceProxy) IgniteServices(org.apache.ignite.IgniteServices) ClientObjectResponse(org.apache.ignite.internal.processors.platform.client.ClientObjectResponse) Method(java.lang.reflect.Method) PlatformService(org.apache.ignite.internal.processors.platform.services.PlatformService) ServiceDescriptor(org.apache.ignite.services.ServiceDescriptor) IgniteException(org.apache.ignite.IgniteException) PlatformNativeException(org.apache.ignite.internal.processors.platform.PlatformNativeException) IgniteServicesImpl(org.apache.ignite.internal.IgniteServicesImpl) ClusterGroupAdapter(org.apache.ignite.internal.cluster.ClusterGroupAdapter)

Example 18 with IgniteServices

use of org.apache.ignite.IgniteServices in project ignite by apache.

the class IgniteClientReconnectServicesTest method testReconnectInDeployingNew.

/**
 * @throws Exception If failed.
 */
@SuppressWarnings("ThrowableResultOfMethodCallIgnored")
@Test
public void testReconnectInDeployingNew() throws Exception {
    IgniteEx client = grid(serverCount());
    assertTrue(client.cluster().localNode().isClient());
    final IgniteServices services = client.services();
    Ignite srv = ignite(0);
    final IgniteInternalFuture<Object> fut = GridTestUtils.runAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            try {
                services.deployClusterSingleton("testReconnectInDeploying", new TestServiceImpl());
            } catch (IgniteClientDisconnectedException e) {
                checkAndWait(e);
                return true;
            }
            return false;
        }
    });
    reconnectClientNode(client, srv, () -> {
        // Check that client waiting operation.
        GridTestUtils.assertThrows(log, () -> fut.get(200), IgniteFutureTimeoutCheckedException.class, null);
        try {
            assertNotDone(fut);
        } catch (Exception e) {
            fail("Unexpected exception has been thrown, err=" + e.getMessage());
        }
    });
    assertTrue((Boolean) fut.get(2, TimeUnit.SECONDS));
}
Also used : IgniteServices(org.apache.ignite.IgniteServices) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) Ignite(org.apache.ignite.Ignite) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteException(org.apache.ignite.IgniteException) Test(org.junit.Test)

Example 19 with IgniteServices

use of org.apache.ignite.IgniteServices in project ignite by apache.

the class IgniteServiceDynamicCachesSelfTest method testDeployCalledAfterCacheStart.

/**
 * @throws Exception If failed.
 */
@Test
public void testDeployCalledAfterCacheStart() throws Exception {
    String cacheName = "cache";
    CacheConfiguration ccfg = new CacheConfiguration(cacheName);
    ccfg.setBackups(1);
    Ignite ig = ignite(0);
    ig.createCache(ccfg);
    try {
        final IgniteServices svcs = ig.services();
        final String svcName = "myService";
        svcs.deployKeyAffinitySingleton(svcName, new TestService(), cacheName, primaryKey(ig.cache(cacheName)));
        boolean res = GridTestUtils.waitForCondition(new PA() {

            @Override
            public boolean apply() {
                return svcs.service(svcName) != null;
            }
        }, 10 * 1000);
        assertTrue("Service was not deployed", res);
        ig.destroyCache(cacheName);
        res = GridTestUtils.waitForCondition(new PA() {

            @Override
            public boolean apply() {
                return svcs.service(svcName) == null;
            }
        }, 10 * 1000);
        assertTrue("Service was not undeployed", res);
    } finally {
        ig.services().cancelAll();
        ig.destroyCache(cacheName);
    }
}
Also used : PA(org.apache.ignite.internal.util.typedef.PA) IgniteServices(org.apache.ignite.IgniteServices) Ignite(org.apache.ignite.Ignite) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 20 with IgniteServices

use of org.apache.ignite.IgniteServices in project ignite by apache.

the class CacheLateAffinityAssignmentTest method testServiceReassign.

/**
 * @throws Exception If failed.
 */
@Test
public void testServiceReassign() throws Exception {
    skipCheckOrder = true;
    Ignite ignite0 = startServer(0, 1);
    IgniteServices svcs = ignite0.services();
    for (int i = 0; i < 10; i++) svcs.deployKeyAffinitySingleton("service-" + i, new TestServiceImpl(i), CACHE_NAME1, i);
    startServer(1, 2);
    startServer(2, 3);
    Map<String, List<List<ClusterNode>>> assignments = checkAffinity(3, topVer(3, 1), true);
    checkServicesDeploy(ignite(0), assignments.get(CACHE_NAME1));
    stopGrid(0);
    boolean primaryChanged = calculateAffinity(4, false, assignments);
    assignments = checkAffinity(2, topVer(4, 0), !primaryChanged);
    if (primaryChanged)
        checkAffinity(2, topVer(4, 1), true);
    checkServicesDeploy(ignite(1), assignments.get(CACHE_NAME1));
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteServices(org.apache.ignite.IgniteServices) Ignite(org.apache.ignite.Ignite) List(java.util.List) ArrayList(java.util.ArrayList) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Aggregations

IgniteServices (org.apache.ignite.IgniteServices)30 Test (org.junit.Test)26 Ignite (org.apache.ignite.Ignite)19 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)15 CountDownLatch (java.util.concurrent.CountDownLatch)11 GridCommonTest (org.apache.ignite.testframework.junits.common.GridCommonTest)9 IgniteException (org.apache.ignite.IgniteException)8 IgniteEx (org.apache.ignite.internal.IgniteEx)8 IgniteClientDisconnectedException (org.apache.ignite.IgniteClientDisconnectedException)4 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)3 Service (org.apache.ignite.services.Service)3 ServiceConfiguration (org.apache.ignite.services.ServiceConfiguration)3 DummyService (org.apache.ignite.internal.processors.service.DummyService)2 PA (org.apache.ignite.internal.util.typedef.PA)2 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ClusterGroup (org.apache.ignite.cluster.ClusterGroup)1 ClusterNode (org.apache.ignite.cluster.ClusterNode)1