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();
}
}
Aggregations