Search in sources :

Example 16 with PartitionedRegion

use of org.apache.geode.internal.cache.PartitionedRegion in project geode by apache.

the class DefaultQuery method checkQueryOnPR.

private QueryExecutor checkQueryOnPR(Object[] parameters) throws RegionNotFoundException, PartitionOfflineException {
    // check for PartitionedRegions. If a PartitionedRegion is referred to in the query,
    // then the following restrictions apply:
    // 1) the query must be just a SELECT expression; (preceded by zero or more IMPORT statements)
    // 2) the first FROM clause iterator cannot contain a subquery;
    // 3) PR reference can only be in the first FROM clause
    List<QueryExecutor> prs = new ArrayList<>();
    for (final Object o : getRegionsInQuery(parameters)) {
        String regionPath = (String) o;
        Region rgn = this.cache.getRegion(regionPath);
        if (rgn == null) {
            this.cache.getCancelCriterion().checkCancelInProgress(null);
            throw new RegionNotFoundException(LocalizedStrings.DefaultQuery_REGION_NOT_FOUND_0.toLocalizedString(regionPath));
        }
        if (rgn instanceof QueryExecutor) {
            ((PartitionedRegion) rgn).checkPROffline();
            prs.add((QueryExecutor) rgn);
        }
    }
    if (prs.size() == 1) {
        return prs.get(0);
    } else if (prs.size() > 1) {
        // First query has to be executed in a Function.
        if (!this.isQueryWithFunctionContext()) {
            throw new UnsupportedOperationException(LocalizedStrings.DefaultQuery_A_QUERY_ON_A_PARTITIONED_REGION_0_MAY_NOT_REFERENCE_ANY_OTHER_REGION_1.toLocalizedString(new Object[] { prs.get(0).getName(), prs.get(1).getName() }));
        }
        // If there are more than one PRs they have to be co-located.
        QueryExecutor other = null;
        for (QueryExecutor eachPR : prs) {
            boolean colocated = false;
            for (QueryExecutor allPRs : prs) {
                if (eachPR == allPRs) {
                    continue;
                }
                other = allPRs;
                if (((PartitionedRegion) eachPR).getColocatedByList().contains(allPRs) || ((PartitionedRegion) allPRs).getColocatedByList().contains(eachPR)) {
                    colocated = true;
                    break;
                }
            }
            if (!colocated) {
                throw new UnsupportedOperationException(LocalizedStrings.DefaultQuery_A_QUERY_ON_A_PARTITIONED_REGION_0_MAY_NOT_REFERENCE_ANY_OTHER_NON_COLOCATED_PARTITIONED_REGION_1.toLocalizedString(new Object[] { eachPR.getName(), other.getName() }));
            }
        }
        // eachPR
        // this is a query on a PR, check to make sure it is only a SELECT
        CompiledSelect select = getSimpleSelect();
        if (select == null) {
            throw new UnsupportedOperationException(LocalizedStrings.DefaultQuery_QUERY_MUST_BE_A_SIMPLE_SELECT_WHEN_REFERENCING_A_PARTITIONED_REGION.toLocalizedString());
        }
        // make sure the where clause references no regions
        Set regions = new HashSet();
        CompiledValue whereClause = select.getWhereClause();
        if (whereClause != null) {
            whereClause.getRegionsInQuery(regions, parameters);
            if (!regions.isEmpty()) {
                throw new UnsupportedOperationException(LocalizedStrings.DefaultQuery_THE_WHERE_CLAUSE_CANNOT_REFER_TO_A_REGION_WHEN_QUERYING_ON_A_PARTITIONED_REGION.toLocalizedString());
            }
        }
        List fromClause = select.getIterators();
        // the first iterator in the FROM clause must be just a reference to the Partitioned Region
        Iterator fromClauseIterator = fromClause.iterator();
        CompiledIteratorDef itrDef = (CompiledIteratorDef) fromClauseIterator.next();
        // By process of elimination, we know that the first iterator contains a reference
        // to the PR. Check to make sure there are no subqueries in this first iterator
        itrDef.visitNodes(new CompiledValue.NodeVisitor() {

            @Override
            public boolean visit(CompiledValue node) {
                if (node instanceof CompiledSelect) {
                    throw new UnsupportedOperationException(LocalizedStrings.DefaultQuery_WHEN_QUERYING_A_PARTITIONEDREGION_THE_FIRST_FROM_CLAUSE_ITERATOR_MUST_NOT_CONTAIN_A_SUBQUERY.toLocalizedString());
                }
                return true;
            }
        });
        // the rest of the FROM clause iterators must not reference any regions
        if (!this.isQueryWithFunctionContext()) {
            while (fromClauseIterator.hasNext()) {
                itrDef = (CompiledIteratorDef) fromClauseIterator.next();
                itrDef.getRegionsInQuery(regions, parameters);
                if (!regions.isEmpty()) {
                    throw new UnsupportedOperationException(LocalizedStrings.DefaultQuery_WHEN_QUERYING_A_PARTITIONED_REGION_THE_FROM_CLAUSE_ITERATORS_OTHER_THAN_THE_FIRST_ONE_MUST_NOT_REFERENCE_ANY_REGIONS.toLocalizedString());
                }
            }
            // check the projections, must not reference any regions
            List projs = select.getProjectionAttributes();
            if (projs != null) {
                for (Object proj1 : projs) {
                    Object[] rawProj = (Object[]) proj1;
                    CompiledValue proj = (CompiledValue) rawProj[1];
                    proj.getRegionsInQuery(regions, parameters);
                    if (!regions.isEmpty()) {
                        throw new UnsupportedOperationException(LocalizedStrings.DefaultQuery_WHEN_QUERYING_A_PARTITIONED_REGION_THE_PROJECTIONS_MUST_NOT_REFERENCE_ANY_REGIONS.toLocalizedString());
                    }
                }
            }
            // check the orderByAttrs, must not reference any regions
            List<CompiledSortCriterion> orderBys = select.getOrderByAttrs();
            if (orderBys != null) {
                for (CompiledSortCriterion orderBy : orderBys) {
                    orderBy.getRegionsInQuery(regions, parameters);
                    if (!regions.isEmpty()) {
                        throw new UnsupportedOperationException(LocalizedStrings.DefaultQuery_WHEN_QUERYING_A_PARTITIONED_REGION_THE_ORDERBY_ATTRIBUTES_MUST_NOT_REFERENCE_ANY_REGIONS.toLocalizedString());
                    }
                }
            }
        }
        // PR query is okay
        return prs.get(0);
    }
    return null;
}
Also used : HashSet(java.util.HashSet) LocalDataSet(org.apache.geode.internal.cache.LocalDataSet) Set(java.util.Set) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) ArrayList(java.util.ArrayList) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Iterator(java.util.Iterator) BucketRegion(org.apache.geode.internal.cache.BucketRegion) Region(org.apache.geode.cache.Region) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Example 17 with PartitionedRegion

use of org.apache.geode.internal.cache.PartitionedRegion in project geode by apache.

the class DefaultQueryService method createDefinedIndexes.

@Override
public List<Index> createDefinedIndexes() throws MultiIndexCreationException {
    HashSet<Index> indexes = new HashSet<Index>();
    boolean throwException = false;
    HashMap<String, Exception> exceptionsMap = new HashMap<String, Exception>();
    synchronized (indexDefinitions) {
        for (Entry<Region, HashSet<IndexCreationData>> e : indexDefinitions.entrySet()) {
            Region region = e.getKey();
            HashSet<IndexCreationData> icds = e.getValue();
            if (region instanceof PartitionedRegion) {
                throwException = createDefinedIndexesForPR(indexes, (PartitionedRegion) region, icds, exceptionsMap);
            } else {
                throwException = createDefinedIndexesForReplicatedRegion(indexes, region, icds, exceptionsMap);
            }
        }
    }
    if (throwException) {
        throw new MultiIndexCreationException(exceptionsMap);
    }
    return new ArrayList<Index>(indexes);
}
Also used : ForceReattemptException(org.apache.geode.internal.cache.ForceReattemptException) LowMemoryException(org.apache.geode.cache.LowMemoryException) CacheException(org.apache.geode.cache.CacheException) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion)

Example 18 with PartitionedRegion

use of org.apache.geode.internal.cache.PartitionedRegion in project geode by apache.

the class DefaultQueryService method createIndex.

public Index createIndex(String indexName, IndexType indexType, String indexedExpression, String fromClause, String imports, boolean loadEntries, Region region) throws IndexNameConflictException, IndexExistsException, RegionNotFoundException {
    if (pool != null) {
        throw new UnsupportedOperationException("Index creation on the server is not supported from the client.");
    }
    PartitionedIndex parIndex = null;
    if (region == null) {
        region = getRegionFromPath(imports, fromClause);
    }
    RegionAttributes ra = region.getAttributes();
    // if its a pr the create index on all of the local buckets.
    if (((LocalRegion) region).memoryThresholdReached.get() && !MemoryThresholds.isLowMemoryExceptionDisabled()) {
        LocalRegion lr = (LocalRegion) region;
        throw new LowMemoryException(LocalizedStrings.ResourceManager_LOW_MEMORY_FOR_INDEX.toLocalizedString(region.getName()), lr.getMemoryThresholdReachedMembers());
    }
    if (region instanceof PartitionedRegion) {
        try {
            parIndex = (PartitionedIndex) ((PartitionedRegion) region).createIndex(false, indexType, indexName, indexedExpression, fromClause, imports, loadEntries);
        } catch (ForceReattemptException ex) {
            region.getCache().getLoggerI18n().info(LocalizedStrings.DefaultQueryService_EXCEPTION_WHILE_CREATING_INDEX_ON_PR_DEFAULT_QUERY_PROCESSOR, ex);
        } catch (IndexCreationException exx) {
            region.getCache().getLoggerI18n().info(LocalizedStrings.DefaultQueryService_EXCEPTION_WHILE_CREATING_INDEX_ON_PR_DEFAULT_QUERY_PROCESSOR, exx);
        }
        return parIndex;
    } else {
        IndexManager indexManager = IndexUtils.getIndexManager(region, true);
        Index index = indexManager.createIndex(indexName, indexType, indexedExpression, fromClause, imports, null, null, loadEntries);
        return index;
    }
}
Also used : ForceReattemptException(org.apache.geode.internal.cache.ForceReattemptException) RegionAttributes(org.apache.geode.cache.RegionAttributes) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) LocalRegion(org.apache.geode.internal.cache.LocalRegion) LowMemoryException(org.apache.geode.cache.LowMemoryException)

Example 19 with PartitionedRegion

use of org.apache.geode.internal.cache.PartitionedRegion in project geode by apache.

the class DefaultQueryService method removeIndex.

public void removeIndex(Index index) {
    if (pool != null) {
        throw new UnsupportedOperationException("Index Operation is not supported on the Server Region.");
    }
    Region region = index.getRegion();
    if (region instanceof PartitionedRegion) {
        try {
            ((PartitionedRegion) region).removeIndex(index, false);
        } catch (ForceReattemptException ex) {
            logger.info(LocalizedMessage.create(LocalizedStrings.DefaultQueryService_EXCEPTION_REMOVING_INDEX___0), ex);
        }
        return;
    }
    // get write lock for indexes in replicated region
    // for PR lock will be taken in PartitionRegion.removeIndex
    ((AbstractIndex) index).acquireIndexWriteLockForRemove();
    try {
        IndexManager indexManager = ((LocalRegion) index.getRegion()).getIndexManager();
        indexManager.removeIndex(index);
    } finally {
        ((AbstractIndex) index).releaseIndexWriteLockForRemove();
    }
}
Also used : ForceReattemptException(org.apache.geode.internal.cache.ForceReattemptException) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) LocalRegion(org.apache.geode.internal.cache.LocalRegion)

Example 20 with PartitionedRegion

use of org.apache.geode.internal.cache.PartitionedRegion in project geode by apache.

the class PartitionMessage method process.

/**
   * Upon receipt of the message, both process the message and send an acknowledgement, not
   * necessarily in that order. Note: Any hang in this message may cause a distributed deadlock for
   * those threads waiting for an acknowledgement.
   * 
   * @throws PartitionedRegionException if the region does not exist (typically, if it has been
   *         destroyed)
   */
@Override
public void process(final DistributionManager dm) {
    Throwable thr = null;
    boolean sendReply = true;
    PartitionedRegion pr = null;
    long startTime = 0;
    EntryLogger.setSource(getSender(), "PR");
    try {
        if (checkCacheClosing(dm) || checkDSClosing(dm)) {
            thr = new CacheClosedException(LocalizedStrings.PartitionMessage_REMOTE_CACHE_IS_CLOSED_0.toLocalizedString(dm.getId()));
            return;
        }
        pr = getPartitionedRegion();
        if (pr == null && failIfRegionMissing()) {
            // if the distributed system is disconnecting, don't send a reply saying
            // the partitioned region can't be found (bug 36585)
            thr = new ForceReattemptException(LocalizedStrings.PartitionMessage_0_COULD_NOT_FIND_PARTITIONED_REGION_WITH_ID_1.toLocalizedString(dm.getDistributionManagerId(), regionId));
            // reply sent in finally block below
            return;
        }
        if (pr != null) {
            startTime = getStartPartitionMessageProcessingTime(pr);
        }
        thr = UNHANDLED_EXCEPTION;
        InternalCache cache = getInternalCache();
        if (cache == null) {
            throw new ForceReattemptException(LocalizedStrings.PartitionMessage_REMOTE_CACHE_IS_CLOSED_0.toLocalizedString());
        }
        TXManagerImpl txMgr = getTXManagerImpl(cache);
        TXStateProxy tx = txMgr.masqueradeAs(this);
        if (tx == null) {
            sendReply = operateOnPartitionedRegion(dm, pr, startTime);
        } else {
            try {
                if (txMgr.isClosed()) {
                    // NO DISTRIBUTED MESSAGING CAN BE DONE HERE!
                    sendReply = false;
                } else if (tx.isInProgress()) {
                    sendReply = operateOnPartitionedRegion(dm, pr, startTime);
                    tx.updateProxyServer(this.getSender());
                }
            } finally {
                txMgr.unmasquerade(tx);
            }
        }
        thr = null;
    } catch (ForceReattemptException fre) {
        thr = fre;
    } catch (DataLocationException fre) {
        thr = new ForceReattemptException(fre.getMessage(), fre);
    } catch (DistributedSystemDisconnectedException se) {
        // bug 37026: this is too noisy...
        // throw new CacheClosedException("remote system shutting down");
        // thr = se; cache is closed, no point trying to send a reply
        thr = null;
        sendReply = false;
        if (logger.isDebugEnabled()) {
            logger.debug("shutdown caught, abandoning message: {}", se.getMessage(), se);
        }
    } catch (RegionDestroyedException | RegionNotFoundException rde) {
        // [bruce] RDE does not always mean that the sender's region is also
        // destroyed, so we must send back an exception. If the sender's
        // region is also destroyed, who cares if we send it an exception
        // if (pr != null && pr.isClosed) {
        thr = new ForceReattemptException(LocalizedStrings.PartitionMessage_REGION_IS_DESTROYED_IN_0.toLocalizedString(dm.getDistributionManagerId()), rde);
    // }
    } catch (VirtualMachineError err) {
        SystemFailure.initiateFailure(err);
        // now, so don't let this thread continue.
        throw err;
    } catch (Throwable t) {
        // Whenever you catch Error or Throwable, you must also
        // catch VirtualMachineError (see above). However, there is
        // _still_ a possibility that you are dealing with a cascading
        // error condition, so you also need to check to see if the JVM
        // is still usable:
        SystemFailure.checkFailure();
        // log the exception at fine level if there is no reply to the message
        thr = null;
        if (sendReply) {
            if (!checkDSClosing(dm)) {
                thr = t;
            } else {
                // don't pass arbitrary runtime exceptions and errors back if this
                // cache/vm is closing
                thr = new ForceReattemptException(LocalizedStrings.PartitionMessage_DISTRIBUTED_SYSTEM_IS_DISCONNECTING.toLocalizedString());
            }
        }
        if (logger.isTraceEnabled(LogMarker.DM) && t instanceof RuntimeException) {
            logger.trace(LogMarker.DM, "Exception caught while processing message: {}", t.getMessage(), t);
        }
    } finally {
        if (sendReply) {
            ReplyException rex = null;
            if (thr != null) {
                // don't transmit the exception if this message was to a listener
                // and this listener is shutting down
                boolean excludeException = this.notificationOnly && ((thr instanceof CancelException) || (thr instanceof ForceReattemptException));
                if (!excludeException) {
                    rex = new ReplyException(thr);
                }
            }
            // Send the reply if the operateOnPartitionedRegion returned true
            sendReply(getSender(), this.processorId, dm, rex, pr, startTime);
            EntryLogger.clearSource();
        }
    }
}
Also used : DistributedSystemDisconnectedException(org.apache.geode.distributed.DistributedSystemDisconnectedException) TXManagerImpl(org.apache.geode.internal.cache.TXManagerImpl) DataLocationException(org.apache.geode.internal.cache.DataLocationException) RegionDestroyedException(org.apache.geode.cache.RegionDestroyedException) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) InternalCache(org.apache.geode.internal.cache.InternalCache) CacheClosedException(org.apache.geode.cache.CacheClosedException) ReplyException(org.apache.geode.distributed.internal.ReplyException) ForceReattemptException(org.apache.geode.internal.cache.ForceReattemptException) TXStateProxy(org.apache.geode.internal.cache.TXStateProxy) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) CancelException(org.apache.geode.CancelException)

Aggregations

PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)367 Test (org.junit.Test)135 Region (org.apache.geode.cache.Region)115 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)112 HashSet (java.util.HashSet)105 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)104 AttributesFactory (org.apache.geode.cache.AttributesFactory)86 VM (org.apache.geode.test.dunit.VM)86 Host (org.apache.geode.test.dunit.Host)85 ArrayList (java.util.ArrayList)77 PartitionAttributesFactory (org.apache.geode.cache.PartitionAttributesFactory)75 Set (java.util.Set)69 IOException (java.io.IOException)66 Function (org.apache.geode.cache.execute.Function)66 FunctionException (org.apache.geode.cache.execute.FunctionException)63 List (java.util.List)62 RegionAttributes (org.apache.geode.cache.RegionAttributes)61 Iterator (java.util.Iterator)60 IgnoredException (org.apache.geode.test.dunit.IgnoredException)59 Cache (org.apache.geode.cache.Cache)56