use of org.apache.ignite.services.Service in project ignite by apache.
the class GridServiceProcessorProxySelfTest method testLocalProxyInvocation.
/**
* @throws Exception If failed.
*/
public void testLocalProxyInvocation() throws Exception {
final String name = "testLocalProxyInvocation";
final Ignite ignite = grid(0);
ignite.services().deployNodeSingleton(name, new MapServiceImpl<String, Integer>());
for (int i = 0; i < nodeCount(); i++) {
final int idx = i;
final AtomicReference<MapService<Integer, String>> ref = new AtomicReference<>();
// wait because after deployNodeSingleton we don't have guarantees what service was deploy.
boolean wait = GridTestUtils.waitForCondition(new PA() {
@Override
public boolean apply() {
MapService<Integer, String> svc = grid(idx).services().serviceProxy(name, MapService.class, false);
ref.set(svc);
return svc instanceof Service;
}
}, 2000);
// Make sure service is a local instance.
assertTrue("Invalid service instance [srv=" + ref.get() + ", node=" + i + ']', wait);
ref.get().put(i, Integer.toString(i));
}
MapService<Integer, String> map = ignite.services().serviceProxy(name, MapService.class, false);
for (int i = 0; i < nodeCount(); i++) assertEquals(1, map.size());
}
use of org.apache.ignite.services.Service in project ignite by apache.
the class GridServiceProcessorProxySelfTest method testRemoteNotStickProxyInvocation.
/**
* @throws Exception If failed.
*/
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);
}
use of org.apache.ignite.services.Service in project ignite by apache.
the class GridServiceProcessor method cancel.
/**
* @param ctxs Contexts to cancel.
* @param cancelCnt Number of contexts to cancel.
*/
private void cancel(Iterable<ServiceContextImpl> ctxs, int cancelCnt) {
for (Iterator<ServiceContextImpl> it = ctxs.iterator(); it.hasNext(); ) {
ServiceContextImpl svcCtx = it.next();
// Flip cancelled flag.
svcCtx.setCancelled(true);
// Notify service about cancellation.
Service svc = svcCtx.service();
if (svc != null) {
try {
svc.cancel(svcCtx);
} catch (Throwable e) {
log.error("Failed to cancel service (ignoring) [name=" + svcCtx.name() + ", execId=" + svcCtx.executionId() + ']', e);
if (e instanceof Error)
throw e;
} finally {
try {
ctx.resource().cleanup(svc);
} catch (IgniteCheckedException e) {
U.error(log, "Failed to clean up service (will ignore): " + svcCtx.name(), e);
}
}
}
// Close out executor thread for the service.
// This will cause the thread to be interrupted.
svcCtx.executor().shutdownNow();
it.remove();
if (log.isInfoEnabled())
log.info("Cancelled service instance [name=" + svcCtx.name() + ", execId=" + svcCtx.executionId() + ']');
if (--cancelCnt == 0)
break;
}
}
use of org.apache.ignite.services.Service in project ignite by apache.
the class GridServiceProcessor method services.
/**
* @param name Service name.
* @param <T> Service type.
* @return Services by specified service name.
*/
@SuppressWarnings("unchecked")
public <T> Collection<T> services(String name) {
ctx.security().authorize(name, SecurityPermission.SERVICE_INVOKE, null);
Collection<ServiceContextImpl> ctxs;
synchronized (locSvcs) {
ctxs = locSvcs.get(name);
}
if (ctxs == null)
return null;
synchronized (ctxs) {
Collection<T> res = new ArrayList<>(ctxs.size());
for (ServiceContextImpl ctx : ctxs) {
Service svc = ctx.service();
if (svc != null)
res.add((T) svc);
}
return res;
}
}
use of org.apache.ignite.services.Service in project ignite by apache.
the class GridServiceProcessor method onKernalStop.
/**
* {@inheritDoc}
*/
@Override
public void onKernalStop(boolean cancel) {
if (ctx.isDaemon())
return;
GridSpinBusyLock busyLock = this.busyLock;
// Will not release it.
if (busyLock != null) {
busyLock.block();
this.busyLock = null;
}
startLatch.countDown();
U.shutdownNow(GridServiceProcessor.class, depExe, log);
if (!ctx.clientNode())
ctx.event().removeDiscoveryEventListener(topLsnr);
Collection<ServiceContextImpl> ctxs = new ArrayList<>();
synchronized (locSvcs) {
for (Collection<ServiceContextImpl> ctxs0 : locSvcs.values()) ctxs.addAll(ctxs0);
}
for (ServiceContextImpl ctx : ctxs) {
ctx.setCancelled(true);
Service svc = ctx.service();
if (svc != null)
try {
svc.cancel(ctx);
} catch (Throwable e) {
log.error("Failed to cancel service (ignoring) [name=" + ctx.name() + ", execId=" + ctx.executionId() + ']', e);
if (e instanceof Error)
throw e;
}
ctx.executor().shutdownNow();
}
for (ServiceContextImpl ctx : ctxs) {
try {
if (log.isInfoEnabled() && !ctxs.isEmpty())
log.info("Shutting down distributed service [name=" + ctx.name() + ", execId8=" + U.id8(ctx.executionId()) + ']');
ctx.executor().awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
} catch (InterruptedException ignore) {
Thread.currentThread().interrupt();
U.error(log, "Got interrupted while waiting for service to shutdown (will continue stopping node): " + ctx.name());
}
}
Exception err = new IgniteCheckedException("Operation has been cancelled (node is stopping).");
cancelFutures(depFuts, err);
cancelFutures(undepFuts, err);
if (log.isDebugEnabled())
log.debug("Stopped service processor.");
}
Aggregations