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