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