Search in sources :

Example 16 with Service

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

the class GridServiceProxy method invokeMethod.

/**
 * Invoek the method.
 *
 * @param mtd Method.
 * @param args Arugments.
 * @return Result.
 */
@SuppressWarnings("BusyWait")
public Object invokeMethod(final Method mtd, final Object[] args) {
    if (U.isHashCodeMethod(mtd))
        return System.identityHashCode(proxy);
    else if (U.isEqualsMethod(mtd))
        return proxy == args[0];
    else if (U.isToStringMethod(mtd))
        return GridServiceProxy.class.getSimpleName() + " [name=" + name + ", sticky=" + sticky + ']';
    ctx.gateway().readLock();
    try {
        final long startTime = U.currentTimeMillis();
        while (true) {
            ClusterNode node = null;
            try {
                node = nodeForService(name, sticky);
                if (node == null)
                    throw new IgniteException("Failed to find deployed service: " + name);
                // If service is deployed locally, then execute locally.
                if (node.isLocal()) {
                    ServiceContextImpl svcCtx = ctx.service().serviceContext(name);
                    if (svcCtx != null) {
                        Service svc = svcCtx.service();
                        if (svc != null)
                            return mtd.invoke(svc, args);
                    }
                } else {
                    if (node.version().compareTo(SVC_POOL_SINCE_VER) >= 0)
                        ctx.task().setThreadContext(TC_IO_POLICY, GridIoPolicy.SERVICE_POOL);
                    // Execute service remotely.
                    return ctx.closure().callAsyncNoFailover(GridClosureCallMode.BROADCAST, new ServiceProxyCallable(mtd.getName(), name, mtd.getParameterTypes(), args), Collections.singleton(node), false, waitTimeout, true).get();
                }
            } catch (GridServiceNotFoundException | ClusterTopologyCheckedException e) {
                if (log.isDebugEnabled())
                    log.debug("Service was not found or topology changed (will retry): " + e.getMessage());
            } catch (RuntimeException | Error e) {
                throw e;
            } catch (IgniteCheckedException e) {
                throw U.convertException(e);
            } catch (Exception e) {
                throw new IgniteException(e);
            }
            // If we are here, that means that service was not found
            // or topology was changed. In this case, we erase the
            // previous sticky node and try again.
            rmtNode.compareAndSet(node, null);
            // Add sleep between retries to avoid busy-wait loops.
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                throw new IgniteException(e);
            }
            if (waitTimeout > 0 && U.currentTimeMillis() - startTime >= waitTimeout)
                throw new IgniteException("Service acquire timeout was reached, stopping. [timeout=" + waitTimeout + "]");
        }
    } finally {
        ctx.gateway().readUnlock();
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) Service(org.apache.ignite.services.Service) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Aggregations

Service (org.apache.ignite.services.Service)16 Ignite (org.apache.ignite.Ignite)7 ExecutorService (java.util.concurrent.ExecutorService)6 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)4 ArrayList (java.util.ArrayList)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 IgniteException (org.apache.ignite.IgniteException)3 IgniteServices (org.apache.ignite.IgniteServices)3 ClusterNode (org.apache.ignite.cluster.ClusterNode)3 ServiceConfiguration (org.apache.ignite.services.ServiceConfiguration)3 IgniteInterruptedCheckedException (org.apache.ignite.internal.IgniteInterruptedCheckedException)2 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)2 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 IgniteSystemProperties.getString (org.apache.ignite.IgniteSystemProperties.getString)1 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)1 IgniteClientDisconnectedCheckedException (org.apache.ignite.internal.IgniteClientDisconnectedCheckedException)1 GridSpinBusyLock (org.apache.ignite.internal.util.GridSpinBusyLock)1 PA (org.apache.ignite.internal.util.typedef.PA)1