use of org.apache.geode.cache.query.IndexCreationException 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.IndexCreationException in project geode by apache.
the class PartitionedRegion method createIndex.
public Index createIndex(boolean remotelyOriginated, IndexType indexType, String indexName, String indexedExpression, String fromClause, String imports, boolean loadEntries, boolean sendMessage) throws ForceReattemptException, IndexCreationException, IndexNameConflictException, IndexExistsException {
// Check if its remote request and this vm is an accessor.
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(LocalizedMessage.create(LocalizedStrings.PartitionedRegion_THIS_IS_AN_ACCESSOR_VM_AND_DOESNT_CONTAIN_DATA));
return null;
}
// Create indexManager.
if (this.indexManager == null) {
this.indexManager = IndexUtils.getIndexManager(this, true);
}
if (logger.isDebugEnabled()) {
logger.debug("Started creating index with Index Name :{} On PartitionedRegion {}, Indexfrom caluse={}, Remote Request: {}", indexName, this.getFullPath(), fromClause, remotelyOriginated);
}
IndexTask indexTask = new IndexTask(remotelyOriginated, indexType, indexName, indexedExpression, fromClause, imports, loadEntries);
FutureTask<Index> indexFutureTask = new FutureTask<Index>(indexTask);
// This will return either the Index FutureTask or Index itself, based
// on whether the index creation is in process or completed.
Object ind = this.indexes.putIfAbsent(indexTask, indexFutureTask);
// Check if its instance of Index, in that the case throw index exists exception.
if (ind instanceof Index) {
if (remotelyOriginated) {
return (Index) ind;
}
throw new IndexNameConflictException(LocalizedStrings.IndexManager_INDEX_NAMED_0_ALREADY_EXISTS.toLocalizedString(indexName));
}
FutureTask<Index> oldIndexFutureTask = (FutureTask<Index>) ind;
Index index = null;
boolean interrupted = false;
try {
if (oldIndexFutureTask == null) {
// Index doesn't exist, create index.
indexFutureTask.run();
index = indexFutureTask.get();
if (index != null) {
this.indexes.put(indexTask, index);
PartitionedIndex prIndex = (PartitionedIndex) index;
indexManager.addIndex(indexName, index);
// Send create request to other PR nodes.
if (!remotelyOriginated && sendMessage) {
logger.info(LocalizedMessage.create(LocalizedStrings.PartitionedRegion_CREATED_INDEX_LOCALLY_SENDING_INDEX_CREATION_MESSAGE_TO_ALL_MEMBERS_AND_WILL_BE_WAITING_FOR_RESPONSE_0, prIndex));
HashSet<IndexCreationData> singleIndexDefinition = new HashSet<IndexCreationData>();
IndexCreationData icd = new IndexCreationData(indexName);
icd.setIndexData(indexType, fromClause, indexedExpression, imports, loadEntries);
singleIndexDefinition.add(icd);
IndexCreationMsg.IndexCreationResponse response = null;
try {
response = (IndexCreationMsg.IndexCreationResponse) IndexCreationMsg.send(null, PartitionedRegion.this, singleIndexDefinition);
if (response != null) {
IndexCreationMsg.IndexCreationResult result = response.waitForResult();
Map<String, Integer> indexBucketsMap = result.getIndexBucketsMap();
if (indexBucketsMap != null && indexBucketsMap.size() > 0) {
prIndex.setRemoteBucketesIndexed(indexBucketsMap.values().iterator().next());
}
}
} catch (UnsupportedOperationException ignore) {
// if remote nodes are of older versions indexes will not be created there, so remove
// index on this node as well.
this.indexes.remove(index);
indexManager.removeIndex(index);
throw new IndexCreationException(LocalizedStrings.PartitionedRegion_INDEX_CREATION_FAILED_ROLLING_UPGRADE.toLocalizedString());
}
}
}
} else {
// Some other thread is trying to create the same index.
// Wait for index to be initialized from other thread.
index = oldIndexFutureTask.get();
if (remotelyOriginated) {
return index;
}
throw new IndexNameConflictException(LocalizedStrings.IndexManager_INDEX_NAMED_0_ALREADY_EXISTS.toLocalizedString(indexName));
}
} catch (InterruptedException ignore) {
interrupted = true;
} catch (ExecutionException ee) {
if (!remotelyOriginated) {
Throwable cause = ee.getCause();
if (cause instanceof IndexNameConflictException) {
throw (IndexNameConflictException) cause;
} else if (cause instanceof IndexExistsException) {
throw (IndexExistsException) cause;
}
throw new IndexInvalidException(ee);
}
} finally {
// If the index is not successfully created, remove IndexTask from the map.
if (index == null) {
ind = this.indexes.get(indexTask);
if (index != null && !(index instanceof Index)) {
this.indexes.remove(indexTask);
}
}
if (interrupted) {
Thread.currentThread().interrupt();
}
}
if (logger.isDebugEnabled()) {
logger.debug("Completed creating index with Index Name :{} On PartitionedRegion {}, Remote Request: {}", indexName, this.getFullPath(), remotelyOriginated);
}
return index;
}
use of org.apache.geode.cache.query.IndexCreationException 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);
}
use of org.apache.geode.cache.query.IndexCreationException in project geode by apache.
the class PartitionedRegion method sendCreateIndexesMessage.
private boolean sendCreateIndexesMessage(boolean remotelyOriginated, HashSet<IndexCreationData> indexDefinitions, Set<Index> indexes, HashMap<String, Exception> exceptionsMap) throws CacheException, ForceReattemptException {
boolean throwException = false;
if (!remotelyOriginated) {
logger.info(LocalizedStrings.PartitionedRegion_CREATED_INDEX_LOCALLY_SENDING_INDEX_CREATION_MESSAGE_TO_ALL_MEMBERS_AND_WILL_BE_WAITING_FOR_RESPONSE_0);
IndexCreationMsg.IndexCreationResponse response;
try {
response = (IndexCreationMsg.IndexCreationResponse) IndexCreationMsg.send(null, this, indexDefinitions);
if (response != null) {
IndexCreationMsg.IndexCreationResult result = response.waitForResult();
Map<String, Integer> remoteIndexBucketsMap = result.getIndexBucketsMap();
// set the number of remote buckets indexed for each pr index
if (remoteIndexBucketsMap != null) {
for (Index ind : indexes) {
if (remoteIndexBucketsMap.containsKey(ind.getName())) {
((PartitionedIndex) ind).setRemoteBucketesIndexed(remoteIndexBucketsMap.get(ind.getName()));
}
}
}
}
} catch (UnsupportedOperationException ignore) {
// there, so remove index on this node as well.
for (Index ind : indexes) {
exceptionsMap.put(ind.getName(), new IndexCreationException(LocalizedStrings.PartitionedRegion_INDEX_CREATION_FAILED_ROLLING_UPGRADE.toLocalizedString()));
this.indexes.remove(ind);
indexManager.removeIndex(ind);
}
throwException = true;
}
}
return throwException;
}
use of org.apache.geode.cache.query.IndexCreationException in project geode by apache.
the class RollingUpgrade2DUnitTest method createIndexes.
public static void createIndexes(String regionPath, GemFireCache cache) {
try {
QueryService service = cache.getQueryService();
service.defineIndex("statusIndex", "status", regionPath);
service.defineIndex("IDIndex", "ID", regionPath);
service.defineIndex("secIdIndex", "pos.secId", regionPath + " p, p.positions.values pos");
try {
service.createDefinedIndexes();
fail("Index creation should have failed");
} catch (Exception e) {
Assert.assertTrue("Only MultiIndexCreationException should have been thrown and not " + e.getClass(), e instanceof MultiIndexCreationException);
Assert.assertEquals("3 exceptions should have be present in the exceptionsMap.", 3, ((MultiIndexCreationException) e).getExceptionsMap().values().size());
for (Exception ex : ((MultiIndexCreationException) e).getExceptionsMap().values()) {
Assert.assertTrue("Index creation should have been failed with IndexCreationException ", ex instanceof IndexCreationException);
Assert.assertEquals("Incorrect exception message ", LocalizedStrings.PartitionedRegion_INDEX_CREATION_FAILED_ROLLING_UPGRADE.toLocalizedString(), ((IndexCreationException) ex).getMessage());
}
}
} catch (Exception e) {
throw new Error("Exception ", e);
}
}
Aggregations