Search in sources :

Example 31 with ServiceConfiguration

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

the class GridServiceProcessor method deployAll.

/**
 * @param cfgs Service configurations.
 * @param dfltNodeFilter Default NodeFilter.
 * @return Future for deployment.
 */
private IgniteInternalFuture<?> deployAll(Collection<ServiceConfiguration> cfgs, @Nullable IgnitePredicate<ClusterNode> dfltNodeFilter) {
    assert cfgs != null;
    PreparedConfigurations srvCfg = prepareServiceConfigurations(cfgs, dfltNodeFilter);
    List<ServiceConfiguration> cfgsCp = srvCfg.cfgs;
    List<GridServiceDeploymentFuture> failedFuts = srvCfg.failedFuts;
    Collections.sort(cfgsCp, new Comparator<ServiceConfiguration>() {

        @Override
        public int compare(ServiceConfiguration cfg1, ServiceConfiguration cfg2) {
            return cfg1.getName().compareTo(cfg2.getName());
        }
    });
    GridServiceDeploymentCompoundFuture res;
    while (true) {
        res = new GridServiceDeploymentCompoundFuture();
        if (ctx.deploy().enabled())
            ctx.cache().context().deploy().ignoreOwnership(true);
        try {
            if (cfgsCp.size() == 1)
                writeServiceToCache(res, cfgsCp.get(0));
            else if (cfgsCp.size() > 1) {
                try (Transaction tx = serviceCache().txStart(PESSIMISTIC, READ_COMMITTED)) {
                    for (ServiceConfiguration cfg : cfgsCp) {
                        try {
                            writeServiceToCache(res, cfg);
                        } catch (IgniteCheckedException e) {
                            if (X.hasCause(e, ClusterTopologyCheckedException.class))
                                // Retry.
                                throw e;
                            else
                                U.error(log, e.getMessage());
                        }
                    }
                    tx.commit();
                }
            }
            break;
        } catch (IgniteException | IgniteCheckedException e) {
            for (String name : res.servicesToRollback()) depFuts.remove(name).onDone(e);
            if (X.hasCause(e, ClusterTopologyCheckedException.class)) {
                if (log.isDebugEnabled())
                    log.debug("Topology changed while deploying services (will retry): " + e.getMessage());
            } else {
                res.onDone(new IgniteCheckedException(new ServiceDeploymentException("Failed to deploy provided services.", e, cfgs)));
                return res;
            }
        } finally {
            if (ctx.deploy().enabled())
                ctx.cache().context().deploy().ignoreOwnership(false);
        }
    }
    if (ctx.clientDisconnected()) {
        IgniteClientDisconnectedCheckedException err = new IgniteClientDisconnectedCheckedException(ctx.cluster().clientReconnectFuture(), "Failed to deploy services, client node disconnected: " + cfgs);
        for (String name : res.servicesToRollback()) {
            GridServiceDeploymentFuture fut = depFuts.remove(name);
            if (fut != null)
                fut.onDone(err);
        }
        return new GridFinishedFuture<>(err);
    }
    if (failedFuts != null) {
        for (GridServiceDeploymentFuture fut : failedFuts) res.add(fut, false);
    }
    res.markInitialized();
    return res;
}
Also used : ServiceDeploymentException(org.apache.ignite.services.ServiceDeploymentException) IgniteSystemProperties.getString(org.apache.ignite.IgniteSystemProperties.getString) GridFinishedFuture(org.apache.ignite.internal.util.future.GridFinishedFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) ServiceConfiguration(org.apache.ignite.services.ServiceConfiguration) Transaction(org.apache.ignite.transactions.Transaction) IgniteException(org.apache.ignite.IgniteException) IgniteClientDisconnectedCheckedException(org.apache.ignite.internal.IgniteClientDisconnectedCheckedException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Example 32 with ServiceConfiguration

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

the class GridServiceProcessor method deployMultiple.

/**
 * @param name Service name.
 * @param svc Service.
 * @param totalCnt Total count.
 * @param maxPerNodeCnt Max per-node count.
 * @return Future.
 */
public IgniteInternalFuture<?> deployMultiple(ClusterGroup prj, String name, Service svc, int totalCnt, int maxPerNodeCnt) {
    ServiceConfiguration cfg = new ServiceConfiguration();
    cfg.setName(name);
    cfg.setService(svc);
    cfg.setTotalCount(totalCnt);
    cfg.setMaxPerNodeCount(maxPerNodeCnt);
    return deployAll(prj, Collections.singleton(cfg));
}
Also used : ServiceConfiguration(org.apache.ignite.services.ServiceConfiguration)

Example 33 with ServiceConfiguration

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

the class GridServiceProcessor method reassign.

/**
 * Reassigns service to nodes.
 *
 * @param dep Service deployment.
 * @param topVer Topology version.
 * @throws IgniteCheckedException If failed.
 */
private void reassign(GridServiceDeployment dep, AffinityTopologyVersion topVer) throws IgniteCheckedException {
    IgniteInternalCache<Object, Object> cache = serviceCache();
    ServiceConfiguration cfg = dep.configuration();
    Object nodeFilter = cfg.getNodeFilter();
    if (nodeFilter != null)
        ctx.resource().injectGeneric(nodeFilter);
    int totalCnt = cfg.getTotalCount();
    int maxPerNodeCnt = cfg.getMaxPerNodeCount();
    String cacheName = cfg.getCacheName();
    Object affKey = cfg.getAffinityKey();
    while (true) {
        GridServiceAssignments assigns = new GridServiceAssignments(cfg, dep.nodeId(), topVer.topologyVersion());
        Collection<ClusterNode> nodes;
        // Call node filter outside of transaction.
        if (affKey == null) {
            nodes = ctx.discovery().nodes(topVer);
            if (assigns.nodeFilter() != null) {
                Collection<ClusterNode> nodes0 = new ArrayList<>();
                for (ClusterNode node : nodes) {
                    if (assigns.nodeFilter().apply(node))
                        nodes0.add(node);
                }
                nodes = nodes0;
            }
        } else
            nodes = null;
        try (GridNearTxLocal tx = cache.txStartEx(PESSIMISTIC, REPEATABLE_READ)) {
            GridServiceAssignmentsKey key = new GridServiceAssignmentsKey(cfg.getName());
            GridServiceAssignments oldAssigns = (GridServiceAssignments) cache.get(key);
            Map<UUID, Integer> cnts = new HashMap<>();
            if (affKey != null) {
                ClusterNode n = ctx.affinity().mapKeyToNode(cacheName, affKey, topVer);
                if (n != null) {
                    int cnt = maxPerNodeCnt == 0 ? totalCnt == 0 ? 1 : totalCnt : maxPerNodeCnt;
                    cnts.put(n.id(), cnt);
                }
            } else {
                if (!nodes.isEmpty()) {
                    int size = nodes.size();
                    int perNodeCnt = totalCnt != 0 ? totalCnt / size : maxPerNodeCnt;
                    int remainder = totalCnt != 0 ? totalCnt % size : 0;
                    if (perNodeCnt >= maxPerNodeCnt && maxPerNodeCnt != 0) {
                        perNodeCnt = maxPerNodeCnt;
                        remainder = 0;
                    }
                    for (ClusterNode n : nodes) cnts.put(n.id(), perNodeCnt);
                    assert perNodeCnt >= 0;
                    assert remainder >= 0;
                    if (remainder > 0) {
                        int cnt = perNodeCnt + 1;
                        if (oldAssigns != null) {
                            Collection<UUID> used = new HashSet<>();
                            // Avoid redundant moving of services.
                            for (Map.Entry<UUID, Integer> e : oldAssigns.assigns().entrySet()) {
                                // Do not assign services to left nodes.
                                if (ctx.discovery().node(e.getKey()) == null)
                                    continue;
                                // If old count and new count match, then reuse the assignment.
                                if (e.getValue() == cnt) {
                                    cnts.put(e.getKey(), cnt);
                                    used.add(e.getKey());
                                    if (--remainder == 0)
                                        break;
                                }
                            }
                            if (remainder > 0) {
                                List<Map.Entry<UUID, Integer>> entries = new ArrayList<>(cnts.entrySet());
                                // Randomize.
                                Collections.shuffle(entries);
                                for (Map.Entry<UUID, Integer> e : entries) {
                                    // Assign only the ones that have not been reused from previous assignments.
                                    if (!used.contains(e.getKey())) {
                                        if (e.getValue() < maxPerNodeCnt || maxPerNodeCnt == 0) {
                                            e.setValue(e.getValue() + 1);
                                            if (--remainder == 0)
                                                break;
                                        }
                                    }
                                }
                            }
                        } else {
                            List<Map.Entry<UUID, Integer>> entries = new ArrayList<>(cnts.entrySet());
                            // Randomize.
                            Collections.shuffle(entries);
                            for (Map.Entry<UUID, Integer> e : entries) {
                                e.setValue(e.getValue() + 1);
                                if (--remainder == 0)
                                    break;
                            }
                        }
                    }
                }
            }
            assigns.assigns(cnts);
            cache.put(key, assigns);
            tx.commit();
            break;
        } catch (ClusterTopologyCheckedException e) {
            if (log.isDebugEnabled())
                log.debug("Topology changed while reassigning (will retry): " + e.getMessage());
            U.sleep(10);
        }
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal) IgniteSystemProperties.getString(org.apache.ignite.IgniteSystemProperties.getString) ServiceConfiguration(org.apache.ignite.services.ServiceConfiguration) GridTimeoutObject(org.apache.ignite.internal.processors.timeout.GridTimeoutObject) UUID(java.util.UUID) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) HashSet(java.util.HashSet) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Example 34 with ServiceConfiguration

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

the class PlatformServices method dotnetDeploy.

/**
 * Deploys dotnet service.
 *
 * @param reader Binary reader.
 * @param services Services.
 */
private void dotnetDeploy(BinaryRawReaderEx reader, IgniteServices services) {
    ServiceConfiguration cfg = dotnetConfiguration(reader);
    services.deploy(cfg);
}
Also used : ServiceConfiguration(org.apache.ignite.services.ServiceConfiguration)

Example 35 with ServiceConfiguration

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

the class PlatformServices method dotnetConfiguration.

/**
 * Read the dotnet service configuration.
 *
 * @param reader Binary reader,
 * @return Service configuration.
 */
@NotNull
private ServiceConfiguration dotnetConfiguration(BinaryRawReaderEx reader) {
    ServiceConfiguration cfg = new ServiceConfiguration();
    cfg.setName(reader.readString());
    cfg.setService(new PlatformDotNetServiceImpl(reader.readObjectDetached(), platformCtx, srvKeepBinary));
    cfg.setTotalCount(reader.readInt());
    cfg.setMaxPerNodeCount(reader.readInt());
    cfg.setCacheName(reader.readString());
    cfg.setAffinityKey(reader.readObjectDetached());
    Object filter = reader.readObjectDetached();
    if (filter != null)
        cfg.setNodeFilter(platformCtx.createClusterNodeFilter(filter));
    return cfg;
}
Also used : ServiceConfiguration(org.apache.ignite.services.ServiceConfiguration) PlatformDotNetServiceImpl(org.apache.ignite.internal.processors.platform.dotnet.PlatformDotNetServiceImpl) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ServiceConfiguration (org.apache.ignite.services.ServiceConfiguration)71 Test (org.junit.Test)29 Ignite (org.apache.ignite.Ignite)20 ArrayList (java.util.ArrayList)18 CountDownLatch (java.util.concurrent.CountDownLatch)18 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)15 ClusterNode (org.apache.ignite.cluster.ClusterNode)10 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)9 IgniteUuid (org.apache.ignite.lang.IgniteUuid)8 ServiceDeploymentException (org.apache.ignite.services.ServiceDeploymentException)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 UUID (java.util.UUID)6 IgniteException (org.apache.ignite.IgniteException)6 IgniteClientDisconnectedCheckedException (org.apache.ignite.internal.IgniteClientDisconnectedCheckedException)5 IgniteEx (org.apache.ignite.internal.IgniteEx)5 HashMap (java.util.HashMap)4 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)4 Service (org.apache.ignite.services.Service)4 HashSet (java.util.HashSet)3 List (java.util.List)3