use of org.apache.geode.cache.query.MultiIndexCreationException in project geode by apache.
the class IndexCreationMsg method operateOnPartitionedRegion.
/**
* This method actually operates on the partitioned region and creates given list of indexes from
* a index creation message.
*
* @param dm distribution manager.
* @param pr partitioned region on which to create an index.
* @throws CacheException indicating a cache level error
* @throws ForceReattemptException if the peer is no longer available
*/
@Override
protected boolean operateOnPartitionedRegion(DistributionManager dm, PartitionedRegion pr, long startTime) throws CacheException, ForceReattemptException {
// region exists
ReplyException replyEx = null;
boolean result = false;
List<Index> indexes = null;
List<String> failedIndexNames = new ArrayList<String>();
if (logger.isDebugEnabled()) {
StringBuilder sb = new StringBuilder();
for (IndexCreationData icd : indexDefinitions) {
sb.append(icd.getIndexName()).append(" ");
}
logger.debug("Processing index creation message on this remote partitioned region vm for indexes: {}", sb);
}
try {
indexes = pr.createIndexes(true, indexDefinitions);
} catch (IndexCreationException e1) {
replyEx = new ReplyException(LocalizedStrings.IndexCreationMsg_REMOTE_INDEX_CREAION_FAILED.toLocalizedString(), e1);
} catch (MultiIndexCreationException exx) {
failedIndexNames.addAll(exx.getExceptionsMap().keySet());
if (logger.isDebugEnabled()) {
StringBuffer exceptionMsgs = new StringBuffer();
for (Exception ex : exx.getExceptionsMap().values()) {
exceptionMsgs.append(ex.getMessage()).append("\n");
}
logger.debug("Got an MultiIndexCreationException with \n: {}", exceptionMsgs);
logger.debug("{} indexes were created succesfully", failedIndexNames.size());
}
replyEx = new ReplyException(LocalizedStrings.IndexCreationMsg_REMOTE_INDEX_CREAION_FAILED.toLocalizedString(), exx);
}
if (null == replyEx) {
result = true;
}
if (result) {
Map<String, Integer> indexBucketsMap = new HashMap<String, Integer>();
for (Index index : indexes) {
PartitionedIndex prIndex = (PartitionedIndex) index;
indexBucketsMap.put(prIndex.getName(), prIndex.getNumberOfIndexedBuckets());
}
sendReply(getSender(), getProcessorId(), dm, replyEx, result, indexBucketsMap, pr.getDataStore().getAllLocalBuckets().size());
} else {
// add the indexes that were successfully created to the map
Map<String, Integer> indexBucketsMap = new HashMap<String, Integer>();
for (IndexCreationData icd : indexDefinitions) {
// if the index was successfully created
if (!failedIndexNames.contains(icd.getIndexName())) {
PartitionedIndex prIndex = (PartitionedIndex) pr.getIndex(icd.getIndexName());
indexBucketsMap.put(icd.getIndexName(), prIndex.getNumberOfIndexedBuckets());
}
}
sendReply(getSender(), getProcessorId(), dm, replyEx, result, indexBucketsMap, pr.getDataStore().getAllLocalBuckets().size());
}
if (logger.isDebugEnabled()) {
logger.debug("Multi Index creation completed on remote host and has sent the reply to the originating vm.");
}
return false;
}
use of org.apache.geode.cache.query.MultiIndexCreationException in project geode by apache.
the class PRQueryDUnitHelper method getCacheSerializableRunnableForDefineIndex.
public CacheSerializableRunnable getCacheSerializableRunnableForDefineIndex(final String prRegionName, final ArrayList<String> indexName, final ArrayList<String> indexedExpression, final ArrayList<String> fromClause) {
SerializableRunnable prIndexCreator = new CacheSerializableRunnable("PartitionedIndexCreator") {
@Override
public void run2() {
List<Index> indexes = null;
try {
Cache cache = getCache();
QueryService qs = cache.getQueryService();
Region region = cache.getRegion(prRegionName);
for (int i = 0; i < indexName.size(); i++) {
qs.defineIndex(indexName.get(i), indexedExpression.get(i), fromClause == null ? region.getFullPath() : fromClause.get(i));
}
indexes = qs.createDefinedIndexes();
} catch (Exception ex) {
if (ex instanceof MultiIndexCreationException) {
StringBuffer sb = new StringBuffer();
for (Exception e : ((MultiIndexCreationException) ex).getExceptionsMap().values()) {
sb.append(e.getMessage()).append("\n");
}
fail("Multi index creation failed, " + sb);
} else {
Assert.fail("Creating Index in this vm failed : ", ex);
}
}
assertNotNull("Indexes should have been created.", indexes);
}
};
return (CacheSerializableRunnable) prIndexCreator;
}
use of org.apache.geode.cache.query.MultiIndexCreationException in project geode by apache.
the class IndexManager method populateIndexes.
/**
* populates all the indexes in the region
*/
public void populateIndexes(Collection<Index> indexSet) throws MultiIndexCreationException {
waitBeforeUpdate();
if (region.getCache().getLogger().infoEnabled()) {
region.getCache().getLogger().info("Populating indexes for region " + region.getName());
}
boolean throwException = false;
HashMap<String, Exception> exceptionsMap = new HashMap<String, Exception>();
boolean oldReadSerialized = DefaultQuery.getPdxReadSerialized();
DefaultQuery.setPdxReadSerialized(true);
try {
Iterator entryIter = ((LocalRegion) region).getBestIterator(true);
while (entryIter.hasNext()) {
RegionEntry entry = (RegionEntry) entryIter.next();
if (entry == null || entry.isInvalidOrRemoved()) {
continue;
}
// Fault in the value once before index update so that every index
// update does not have
// to read the value from disk every time.
entry.getValue((LocalRegion) this.region);
Iterator<Index> indexSetIterator = indexSet.iterator();
while (indexSetIterator.hasNext()) {
AbstractIndex index = (AbstractIndex) indexSetIterator.next();
if (!index.isPopulated() && index.getType() != IndexType.PRIMARY_KEY) {
if (logger.isDebugEnabled()) {
logger.debug("Adding to index :{}{} value :{}", index.getName(), this.region.getFullPath(), entry.getKey());
}
long start = ((AbstractIndex) index).updateIndexUpdateStats();
try {
index.addIndexMapping(entry);
} catch (IMQException e) {
if (logger.isDebugEnabled()) {
logger.debug("Adding to index failed for: {}, {}", index.getName(), e.getMessage(), e);
}
exceptionsMap.put(index.indexName, e);
indexSetIterator.remove();
throwException = true;
}
((AbstractIndex) index).updateIndexUpdateStats(start);
}
}
}
setPopulateFlagForIndexes(indexSet);
if (throwException) {
throw new MultiIndexCreationException(exceptionsMap);
}
} finally {
DefaultQuery.setPdxReadSerialized(oldReadSerialized);
notifyAfterUpdate();
}
}
use of org.apache.geode.cache.query.MultiIndexCreationException in project geode by apache.
the class PartitionedRegion method populateEmptyIndexes.
private boolean populateEmptyIndexes(Set<Index> indexes, HashMap<String, Exception> exceptionsMap) {
boolean throwException = false;
if (getDataStore() != null && indexes.size() > 0) {
Set localBuckets = getDataStore().getAllLocalBuckets();
Iterator it = localBuckets.iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
Region bucket = (Region) entry.getValue();
if (bucket == null) {
continue;
}
IndexManager bucketIndexManager = IndexUtils.getIndexManager(bucket, true);
Set<Index> bucketIndexes = getBucketIndexesForPRIndexes(bucket, indexes);
try {
bucketIndexManager.populateIndexes(bucketIndexes);
} catch (MultiIndexCreationException ex) {
exceptionsMap.putAll(ex.getExceptionsMap());
throwException = true;
}
}
}
return throwException;
}
use of org.apache.geode.cache.query.MultiIndexCreationException in project geode by apache.
the class PartitionedRegion method createIndexes.
public List<Index> createIndexes(boolean remotelyOriginated, HashSet<IndexCreationData> indexDefinitions) throws MultiIndexCreationException, CacheException, ForceReattemptException, IndexCreationException {
if (remotelyOriginated && dataStore == null) {
// data store where as it should have.
if (getLocalMaxMemory() != 0) {
throw new IndexCreationException(LocalizedStrings.PartitionedRegion_DATA_STORE_ON_THIS_VM_IS_NULL_AND_THE_LOCAL_MAX_MEMORY_IS_NOT_ZERO_THE_DATA_POLICY_IS_0_AND_THE_LOCALMAXMEMEORY_IS_1.toLocalizedString(getDataPolicy(), (long) getLocalMaxMemory()));
}
// Not have to do anything since the region is just an Accessor and
// does not store any data.
logger.info(LocalizedStrings.PartitionedRegion_THIS_IS_AN_ACCESSOR_VM_AND_DOESNT_CONTAIN_DATA);
return new ArrayList<Index>();
}
Set<Index> indexes = new HashSet<Index>();
boolean throwException = false;
HashMap<String, Exception> exceptionsMap = new HashMap<String, Exception>();
// First step is creating all the defined indexes.
// Do not send the IndexCreationMsg to remote nodes now.
throwException |= createEmptyIndexes(indexDefinitions, remotelyOriginated, indexes, exceptionsMap);
// If same indexes are created locally and also being created by a remote index creation msg
// populate such indexes only once.
Set<Index> unpopulatedIndexes = null;
if (indexes.size() > 0) {
unpopulatedIndexes = getUnpopulatedIndexes(indexes);
}
// Second step is iterating over REs and populating all the created indexes
if (unpopulatedIndexes != null && unpopulatedIndexes.size() > 0) {
throwException |= populateEmptyIndexes(unpopulatedIndexes, exceptionsMap);
}
// Third step is to send the message to remote nodes
// Locally originated create index request.
// Send create request to other PR nodes.
throwException |= sendCreateIndexesMessage(remotelyOriginated, indexDefinitions, indexes, exceptionsMap);
// If exception is throw in any of the above steps
if (throwException) {
throw new MultiIndexCreationException(exceptionsMap);
}
// set the populate flag for all the created PR indexes
for (Index ind : indexes) {
((AbstractIndex) ind).setPopulated(true);
}
return new ArrayList<Index>(indexes);
}
Aggregations