Search in sources :

Example 1 with CacheExistsException

use of org.apache.ignite.cache.CacheExistsException in project ignite by apache.

the class GridCacheProcessor method prepareCacheChangeRequest.

/**
     * Prepares DynamicCacheChangeRequest for cache creation.
     *
     * @param ccfg Cache configuration
     * @param cacheName Cache name
     * @param nearCfg Near cache configuration
     * @param cacheType Cache type
     * @param sql Whether the cache needs to be created as the result of SQL {@code CREATE TABLE} command.
     * @param failIfExists Fail if exists flag.
     * @param failIfNotStarted If {@code true} fails if cache is not started.
     * @return Request or {@code null} if cache already exists.
     * @throws IgniteCheckedException if some of pre-checks failed
     * @throws CacheExistsException if cache exists and failIfExists flag is {@code true}
     */
private DynamicCacheChangeRequest prepareCacheChangeRequest(@Nullable CacheConfiguration ccfg, String cacheName, @Nullable NearCacheConfiguration nearCfg, CacheType cacheType, boolean sql, boolean failIfExists, boolean failIfNotStarted) throws IgniteCheckedException {
    DynamicCacheDescriptor desc = cacheDescriptor(cacheName);
    DynamicCacheChangeRequest req = new DynamicCacheChangeRequest(UUID.randomUUID(), cacheName, ctx.localNodeId());
    req.sql(sql);
    req.failIfExists(failIfExists);
    if (ccfg != null) {
        cloneCheckSerializable(ccfg);
        if (desc != null) {
            if (failIfExists) {
                throw new CacheExistsException("Failed to start cache " + "(a cache with the same name is already started): " + cacheName);
            } else {
                CacheConfiguration descCfg = desc.cacheConfiguration();
                // Check if we were asked to start a near cache.
                if (nearCfg != null) {
                    if (CU.affinityNode(ctx.discovery().localNode(), descCfg.getNodeFilter())) {
                        // If we are on a data node and near cache was enabled, return success, else - fail.
                        if (descCfg.getNearConfiguration() != null)
                            return null;
                        else
                            throw new IgniteCheckedException("Failed to start near " + "cache (local node is an affinity node for cache): " + cacheName);
                    } else
                        // If local node has near cache, return success.
                        req.clientStartOnly(true);
                } else
                    req.clientStartOnly(true);
                req.deploymentId(desc.deploymentId());
                req.startCacheConfiguration(descCfg);
                req.schema(desc.schema());
            }
        } else {
            req.deploymentId(IgniteUuid.randomUuid());
            CacheConfiguration cfg = new CacheConfiguration(ccfg);
            CacheObjectContext cacheObjCtx = ctx.cacheObjects().contextForCache(cfg);
            initialize(cfg, cacheObjCtx);
            req.startCacheConfiguration(cfg);
            req.schema(new QuerySchema(cfg.getQueryEntities()));
        }
    } else {
        req.clientStartOnly(true);
        if (desc != null)
            ccfg = desc.cacheConfiguration();
        if (ccfg == null) {
            if (failIfNotStarted) {
                throw new CacheExistsException("Failed to start client cache " + "(a cache with the given name is not started): " + cacheName);
            } else
                return null;
        }
        req.deploymentId(desc.deploymentId());
        req.startCacheConfiguration(ccfg);
        req.schema(desc.schema());
    }
    if (nearCfg != null)
        req.nearCacheConfiguration(nearCfg);
    req.cacheType(cacheType);
    return req;
}
Also used : QuerySchema(org.apache.ignite.internal.processors.query.QuerySchema) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheExistsException(org.apache.ignite.cache.CacheExistsException) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration)

Example 2 with CacheExistsException

use of org.apache.ignite.cache.CacheExistsException in project ignite by apache.

the class GridCacheProcessor method prepareCacheChangeRequest.

/**
 * Prepares DynamicCacheChangeRequest for cache creation.
 *
 * @param ccfg Cache configuration
 * @param cacheName Cache name
 * @param nearCfg Near cache configuration
 * @param cacheType Cache type
 * @param sql Whether the cache needs to be created as the result of SQL {@code CREATE TABLE} command.
 * @param failIfExists Fail if exists flag.
 * @param failIfNotStarted If {@code true} fails if cache is not started.
 * @param disabledAfterStart If true, cache proxies will be only activated after {@link #restartProxies()}.
 * @param qryEntities Query entities.
 * @return Request or {@code null} if cache already exists.
 * @throws IgniteCheckedException if some of pre-checks failed
 * @throws CacheExistsException if cache exists and failIfExists flag is {@code true}
 */
private DynamicCacheChangeRequest prepareCacheChangeRequest(@Nullable CacheConfiguration ccfg, String cacheName, @Nullable NearCacheConfiguration nearCfg, CacheType cacheType, boolean sql, boolean failIfExists, boolean failIfNotStarted, boolean disabledAfterStart, @Nullable Collection<QueryEntity> qryEntities) throws IgniteCheckedException {
    DynamicCacheDescriptor desc = cacheDescriptor(cacheName);
    DynamicCacheChangeRequest req = new DynamicCacheChangeRequest(UUID.randomUUID(), cacheName, ctx.localNodeId());
    req.sql(sql);
    req.failIfExists(failIfExists);
    req.disabledAfterStart(disabledAfterStart);
    if (ccfg != null) {
        cloneCheckSerializable(ccfg);
        if (desc != null || MetaStorage.METASTORAGE_CACHE_NAME.equals(cacheName)) {
            if (failIfExists) {
                throw new CacheExistsException("Failed to start cache " + "(a cache with the same name is already started): " + cacheName);
            } else {
                CacheConfiguration descCfg = desc.cacheConfiguration();
                // Check if we were asked to start a near cache.
                if (nearCfg != null) {
                    if (CU.affinityNode(ctx.discovery().localNode(), descCfg.getNodeFilter())) {
                        // If we are on a data node and near cache was enabled, return success, else - fail.
                        if (descCfg.getNearConfiguration() != null)
                            return null;
                        else
                            throw new IgniteCheckedException("Failed to start near " + "cache (local node is an affinity node for cache): " + cacheName);
                    } else
                        // If local node has near cache, return success.
                        req.clientStartOnly(true);
                } else if (!CU.affinityNode(ctx.discovery().localNode(), descCfg.getNodeFilter()))
                    req.clientStartOnly(true);
                req.deploymentId(desc.deploymentId());
                req.startCacheConfiguration(descCfg);
                req.schema(desc.schema());
            }
        } else {
            req.deploymentId(IgniteUuid.randomUuid());
            CacheConfiguration cfg = new CacheConfiguration(ccfg);
            CacheObjectContext cacheObjCtx = ctx.cacheObjects().contextForCache(cfg);
            initialize(cfg, cacheObjCtx);
            req.startCacheConfiguration(cfg);
            req.schema(new QuerySchema(qryEntities != null ? QueryUtils.normalizeQueryEntities(qryEntities, cfg) : cfg.getQueryEntities()));
        }
    } else {
        req.clientStartOnly(true);
        if (desc != null)
            ccfg = desc.cacheConfiguration();
        if (ccfg == null) {
            if (failIfNotStarted) {
                throw new CacheExistsException("Failed to start client cache " + "(a cache with the given name is not started): " + cacheName);
            } else
                return null;
        }
        req.deploymentId(desc.deploymentId());
        req.startCacheConfiguration(ccfg);
        req.schema(desc.schema());
    }
    if (nearCfg != null)
        req.nearCacheConfiguration(nearCfg);
    req.cacheType(cacheType);
    return req;
}
Also used : QuerySchema(org.apache.ignite.internal.processors.query.QuerySchema) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheExistsException(org.apache.ignite.cache.CacheExistsException) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration)

Example 3 with CacheExistsException

use of org.apache.ignite.cache.CacheExistsException in project ignite by apache.

the class ClusterCachesInfo method processCacheChangeRequests.

/**
 * @param exchangeActions Exchange actions to update.
 * @param reqs Requests.
 * @param topVer Topology version.
 * @param persistedCfgs {@code True} if process start of persisted caches during cluster activation.
 * @return Process result.
 */
private CacheChangeProcessResult processCacheChangeRequests(ExchangeActions exchangeActions, Collection<DynamicCacheChangeRequest> reqs, AffinityTopologyVersion topVer, boolean persistedCfgs) {
    CacheChangeProcessResult res = new CacheChangeProcessResult();
    final List<T2<DynamicCacheChangeRequest, AffinityTopologyVersion>> reqsToComplete = new ArrayList<>();
    for (DynamicCacheChangeRequest req : reqs) {
        if (req.template()) {
            CacheConfiguration ccfg = req.startCacheConfiguration();
            assert ccfg != null : req;
            DynamicCacheDescriptor desc = registeredTemplates.get(req.cacheName());
            if (desc == null) {
                DynamicCacheDescriptor templateDesc = new DynamicCacheDescriptor(ctx, ccfg, req.cacheType(), null, true, req.initiatingNodeId(), false, false, req.deploymentId(), req.schema());
                DynamicCacheDescriptor old = registeredTemplates().put(ccfg.getName(), templateDesc);
                assert old == null;
                res.addedDescs.add(templateDesc);
            }
            if (!persistedCfgs)
                ctx.cache().completeTemplateAddFuture(ccfg.getName(), req.deploymentId());
            continue;
        }
        assert !req.clientStartOnly() : req;
        DynamicCacheDescriptor desc = registeredCaches.get(req.cacheName());
        boolean needExchange = false;
        boolean clientCacheStart = false;
        AffinityTopologyVersion waitTopVer = null;
        if (req.start()) {
            // Starting a new cache.
            if (desc == null) {
                String conflictErr = checkCacheConflict(req.startCacheConfiguration());
                if (conflictErr != null) {
                    U.warn(log, "Ignore cache start request. " + conflictErr);
                    IgniteCheckedException err = new IgniteCheckedException("Failed to start " + "cache. " + conflictErr);
                    if (persistedCfgs)
                        res.errs.add(err);
                    else
                        ctx.cache().completeCacheStartFuture(req, false, err);
                    continue;
                }
                if (req.clientStartOnly()) {
                    assert !persistedCfgs;
                    ctx.cache().completeCacheStartFuture(req, false, new IgniteCheckedException("Failed to start " + "client cache (a cache with the given name is not started): " + req.cacheName()));
                } else {
                    SchemaOperationException err = QueryUtils.checkQueryEntityConflicts(req.startCacheConfiguration(), registeredCaches.values());
                    if (err != null) {
                        if (persistedCfgs)
                            res.errs.add(err);
                        else
                            ctx.cache().completeCacheStartFuture(req, false, err);
                        continue;
                    }
                    CacheConfiguration<?, ?> ccfg = req.startCacheConfiguration();
                    assert req.cacheType() != null : req;
                    assert F.eq(ccfg.getName(), req.cacheName()) : req;
                    int cacheId = CU.cacheId(req.cacheName());
                    CacheGroupDescriptor grpDesc = registerCacheGroup(exchangeActions, topVer, ccfg, cacheId, req.initiatingNodeId(), req.deploymentId());
                    DynamicCacheDescriptor startDesc = new DynamicCacheDescriptor(ctx, ccfg, req.cacheType(), grpDesc, false, req.initiatingNodeId(), false, req.sql(), req.deploymentId(), req.schema());
                    DynamicCacheDescriptor old = registeredCaches.put(ccfg.getName(), startDesc);
                    restartingCaches.remove(ccfg.getName());
                    assert old == null;
                    ctx.discovery().setCacheFilter(startDesc.cacheId(), grpDesc.groupId(), ccfg.getName(), ccfg.getNearConfiguration() != null);
                    if (!persistedCfgs) {
                        ctx.discovery().addClientNode(req.cacheName(), req.initiatingNodeId(), req.nearCacheConfiguration() != null);
                    }
                    res.addedDescs.add(startDesc);
                    exchangeActions.addCacheToStart(req, startDesc);
                    needExchange = true;
                }
            } else {
                assert !persistedCfgs;
                assert req.initiatingNodeId() != null : req;
                if (req.failIfExists()) {
                    ctx.cache().completeCacheStartFuture(req, false, new CacheExistsException("Failed to start cache " + "(a cache with the same name is already started): " + req.cacheName()));
                } else {
                    // Cache already exists, it is possible client cache is needed.
                    ClusterNode node = ctx.discovery().node(req.initiatingNodeId());
                    boolean clientReq = node != null && !ctx.discovery().cacheAffinityNode(node, req.cacheName());
                    if (clientReq) {
                        ctx.discovery().addClientNode(req.cacheName(), req.initiatingNodeId(), req.nearCacheConfiguration() != null);
                        if (node.id().equals(req.initiatingNodeId())) {
                            desc.clientCacheStartVersion(topVer);
                            clientCacheStart = true;
                            ctx.discovery().clientCacheStartEvent(req.requestId(), F.asMap(req.cacheName(), req), null);
                        }
                    }
                }
            }
            if (!needExchange && !clientCacheStart && desc != null) {
                if (desc.clientCacheStartVersion() != null)
                    waitTopVer = desc.clientCacheStartVersion();
                else {
                    AffinityTopologyVersion nodeStartVer = new AffinityTopologyVersion(ctx.discovery().localNode().order(), 0);
                    if (desc.startTopologyVersion() != null)
                        waitTopVer = desc.startTopologyVersion();
                    else
                        waitTopVer = desc.receivedFromStartVersion();
                    if (waitTopVer == null || nodeStartVer.compareTo(waitTopVer) > 0)
                        waitTopVer = nodeStartVer;
                }
            }
        } else if (req.resetLostPartitions()) {
            if (desc != null) {
                needExchange = true;
                exchangeActions.addCacheToResetLostPartitions(req, desc);
            }
        } else if (req.stop()) {
            if (desc != null) {
                if (req.sql() && !desc.sql()) {
                    ctx.cache().completeCacheStartFuture(req, false, new IgniteCheckedException("Only cache created with CREATE TABLE may be removed with " + "DROP TABLE [cacheName=" + req.cacheName() + ']'));
                    continue;
                }
                if (!req.sql() && desc.sql()) {
                    ctx.cache().completeCacheStartFuture(req, false, new IgniteCheckedException("Only cache created with cache API may be removed with " + "direct call to destroyCache [cacheName=" + req.cacheName() + ']'));
                    continue;
                }
                DynamicCacheDescriptor old = registeredCaches.remove(req.cacheName());
                if (req.restart())
                    restartingCaches.add(req.cacheName());
                assert old != null && old == desc : "Dynamic cache map was concurrently modified [req=" + req + ']';
                ctx.discovery().removeCacheFilter(req.cacheName());
                needExchange = true;
                exchangeActions.addCacheToStop(req, desc);
                CacheGroupDescriptor grpDesc = registeredCacheGrps.get(desc.groupId());
                assert grpDesc != null && grpDesc.groupId() == desc.groupId() : desc;
                grpDesc.onCacheStopped(desc.cacheName(), desc.cacheId());
                if (!grpDesc.hasCaches()) {
                    registeredCacheGrps.remove(grpDesc.groupId());
                    ctx.discovery().removeCacheGroup(grpDesc);
                    exchangeActions.addCacheGroupToStop(grpDesc, req.destroy());
                    assert exchangeActions.checkStopRequestConsistency(grpDesc.groupId());
                    // because group will be stopped anyway.
                    if (req.destroy()) {
                        for (ExchangeActions.CacheActionData action : exchangeActions.cacheStopRequests()) {
                            if (action.descriptor().groupId() == grpDesc.groupId())
                                action.request().destroy(false);
                        }
                    }
                }
            }
        } else
            assert false : req;
        if (!needExchange) {
            if (!clientCacheStart && ctx.localNodeId().equals(req.initiatingNodeId()))
                reqsToComplete.add(new T2<>(req, waitTopVer));
        } else
            res.needExchange = true;
    }
    if (!F.isEmpty(res.addedDescs)) {
        AffinityTopologyVersion startTopVer = res.needExchange ? topVer.nextMinorVersion() : topVer;
        for (DynamicCacheDescriptor desc : res.addedDescs) {
            assert desc.template() || res.needExchange;
            desc.startTopologyVersion(startTopVer);
        }
    }
    if (!F.isEmpty(reqsToComplete)) {
        ctx.closure().callLocalSafe(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                for (T2<DynamicCacheChangeRequest, AffinityTopologyVersion> t : reqsToComplete) {
                    final DynamicCacheChangeRequest req = t.get1();
                    AffinityTopologyVersion waitTopVer = t.get2();
                    IgniteInternalFuture<?> fut = waitTopVer != null ? ctx.cache().context().exchange().affinityReadyFuture(waitTopVer) : null;
                    if (fut == null || fut.isDone())
                        ctx.cache().completeCacheStartFuture(req, false, null);
                    else {
                        fut.listen(new IgniteInClosure<IgniteInternalFuture<?>>() {

                            @Override
                            public void apply(IgniteInternalFuture<?> fut) {
                                ctx.cache().completeCacheStartFuture(req, false, null);
                            }
                        });
                    }
                }
                return null;
            }
        });
    }
    return res;
}
Also used : ArrayList(java.util.ArrayList) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) T2(org.apache.ignite.internal.util.typedef.T2) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) ClusterNode(org.apache.ignite.cluster.ClusterNode) SchemaOperationException(org.apache.ignite.internal.processors.query.schema.SchemaOperationException) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) CacheExistsException(org.apache.ignite.cache.CacheExistsException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) SchemaOperationException(org.apache.ignite.internal.processors.query.schema.SchemaOperationException) CacheExistsException(org.apache.ignite.cache.CacheExistsException) IgniteInClosure(org.apache.ignite.lang.IgniteInClosure)

Aggregations

IgniteCheckedException (org.apache.ignite.IgniteCheckedException)3 CacheExistsException (org.apache.ignite.cache.CacheExistsException)3 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)3 NearCacheConfiguration (org.apache.ignite.configuration.NearCacheConfiguration)3 QuerySchema (org.apache.ignite.internal.processors.query.QuerySchema)2 ArrayList (java.util.ArrayList)1 ClusterNode (org.apache.ignite.cluster.ClusterNode)1 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)1 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)1 SchemaOperationException (org.apache.ignite.internal.processors.query.schema.SchemaOperationException)1 T2 (org.apache.ignite.internal.util.typedef.T2)1 IgniteInClosure (org.apache.ignite.lang.IgniteInClosure)1