Search in sources :

Example 1 with IndexCreationException

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;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Index(org.apache.geode.cache.query.Index) PartitionedIndex(org.apache.geode.cache.query.internal.index.PartitionedIndex) ReplyException(org.apache.geode.distributed.internal.ReplyException) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) ForceReattemptException(org.apache.geode.internal.cache.ForceReattemptException) IndexCreationException(org.apache.geode.cache.query.IndexCreationException) RegionDestroyedException(org.apache.geode.cache.RegionDestroyedException) CancelException(org.apache.geode.CancelException) MultiIndexCreationException(org.apache.geode.cache.query.MultiIndexCreationException) IOException(java.io.IOException) CacheException(org.apache.geode.cache.CacheException) ReplyException(org.apache.geode.distributed.internal.ReplyException) PartitionedRegionException(org.apache.geode.internal.cache.PartitionedRegionException) IndexCreationData(org.apache.geode.cache.query.internal.index.IndexCreationData) PartitionedIndex(org.apache.geode.cache.query.internal.index.PartitionedIndex) IndexCreationException(org.apache.geode.cache.query.IndexCreationException) MultiIndexCreationException(org.apache.geode.cache.query.MultiIndexCreationException) MultiIndexCreationException(org.apache.geode.cache.query.MultiIndexCreationException)

Example 2 with IndexCreationException

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;
}
Also used : IndexExistsException(org.apache.geode.cache.query.IndexExistsException) Index(org.apache.geode.cache.query.Index) PartitionedIndex(org.apache.geode.cache.query.internal.index.PartitionedIndex) AbstractIndex(org.apache.geode.cache.query.internal.index.AbstractIndex) IndexCreationData(org.apache.geode.cache.query.internal.index.IndexCreationData) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PartitionedIndex(org.apache.geode.cache.query.internal.index.PartitionedIndex) FutureTask(java.util.concurrent.FutureTask) IndexNameConflictException(org.apache.geode.cache.query.IndexNameConflictException) IndexCreationException(org.apache.geode.cache.query.IndexCreationException) MultiIndexCreationException(org.apache.geode.cache.query.MultiIndexCreationException) IndexCreationMsg(org.apache.geode.internal.cache.partitioned.IndexCreationMsg) ExecutionException(java.util.concurrent.ExecutionException) IndexInvalidException(org.apache.geode.cache.query.IndexInvalidException) HashSet(java.util.HashSet)

Example 3 with IndexCreationException

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);
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) IndexCreationException(org.apache.geode.cache.query.IndexCreationException) MultiIndexCreationException(org.apache.geode.cache.query.MultiIndexCreationException) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) AbstractIndex(org.apache.geode.cache.query.internal.index.AbstractIndex) Index(org.apache.geode.cache.query.Index) PartitionedIndex(org.apache.geode.cache.query.internal.index.PartitionedIndex) AbstractIndex(org.apache.geode.cache.query.internal.index.AbstractIndex) MultiIndexCreationException(org.apache.geode.cache.query.MultiIndexCreationException) TimeoutException(org.apache.geode.cache.TimeoutException) IndexCreationException(org.apache.geode.cache.query.IndexCreationException) NameResolutionException(org.apache.geode.cache.query.NameResolutionException) RegionDestroyedException(org.apache.geode.cache.RegionDestroyedException) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) InternalGemFireException(org.apache.geode.InternalGemFireException) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) TransactionDataRebalancedException(org.apache.geode.cache.TransactionDataRebalancedException) LockServiceDestroyedException(org.apache.geode.distributed.LockServiceDestroyedException) GatewaySenderException(org.apache.geode.internal.cache.wan.GatewaySenderException) PartitionOfflineException(org.apache.geode.cache.persistence.PartitionOfflineException) IOException(java.io.IOException) CacheException(org.apache.geode.cache.CacheException) GatewaySenderConfigurationException(org.apache.geode.internal.cache.wan.GatewaySenderConfigurationException) ExecutionException(java.util.concurrent.ExecutionException) ReplyException(org.apache.geode.distributed.internal.ReplyException) IndexNameConflictException(org.apache.geode.cache.query.IndexNameConflictException) TypeMismatchException(org.apache.geode.cache.query.TypeMismatchException) IndexExistsException(org.apache.geode.cache.query.IndexExistsException) FunctionDomainException(org.apache.geode.cache.query.FunctionDomainException) EntryExistsException(org.apache.geode.cache.EntryExistsException) PartitionedRegionDistributionException(org.apache.geode.cache.PartitionedRegionDistributionException) PartitionedRegionStorageException(org.apache.geode.cache.PartitionedRegionStorageException) FunctionException(org.apache.geode.cache.execute.FunctionException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) NoSuchElementException(java.util.NoSuchElementException) QueryException(org.apache.geode.cache.query.QueryException) PartitionNotAvailableException(org.apache.geode.cache.partition.PartitionNotAvailableException) LowMemoryException(org.apache.geode.cache.LowMemoryException) InternalFunctionInvocationTargetException(org.apache.geode.internal.cache.execute.InternalFunctionInvocationTargetException) IndexInvalidException(org.apache.geode.cache.query.IndexInvalidException) PRLocallyDestroyedException(org.apache.geode.internal.cache.partitioned.PRLocallyDestroyedException) RegionExistsException(org.apache.geode.cache.RegionExistsException) CancelException(org.apache.geode.CancelException) DiskAccessException(org.apache.geode.cache.DiskAccessException) CacheWriterException(org.apache.geode.cache.CacheWriterException) TransactionException(org.apache.geode.cache.TransactionException) CacheClosedException(org.apache.geode.cache.CacheClosedException) ConcurrentCacheModificationException(org.apache.geode.internal.cache.versions.ConcurrentCacheModificationException) MultiIndexCreationException(org.apache.geode.cache.query.MultiIndexCreationException) TransactionDataNotColocatedException(org.apache.geode.cache.TransactionDataNotColocatedException) EmptyRegionFunctionException(org.apache.geode.cache.execute.EmptyRegionFunctionException) HashSet(java.util.HashSet)

Example 4 with IndexCreationException

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;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PartitionedIndex(org.apache.geode.cache.query.internal.index.PartitionedIndex) IndexCreationException(org.apache.geode.cache.query.IndexCreationException) MultiIndexCreationException(org.apache.geode.cache.query.MultiIndexCreationException) IndexCreationMsg(org.apache.geode.internal.cache.partitioned.IndexCreationMsg) Index(org.apache.geode.cache.query.Index) PartitionedIndex(org.apache.geode.cache.query.internal.index.PartitionedIndex) AbstractIndex(org.apache.geode.cache.query.internal.index.AbstractIndex)

Example 5 with IndexCreationException

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);
    }
}
Also used : QueryService(org.apache.geode.cache.query.QueryService) IndexCreationException(org.apache.geode.cache.query.IndexCreationException) MultiIndexCreationException(org.apache.geode.cache.query.MultiIndexCreationException) MultiIndexCreationException(org.apache.geode.cache.query.MultiIndexCreationException) IndexCreationException(org.apache.geode.cache.query.IndexCreationException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) FunctionException(org.apache.geode.cache.execute.FunctionException) RMIException(org.apache.geode.test.dunit.RMIException) MultiIndexCreationException(org.apache.geode.cache.query.MultiIndexCreationException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Aggregations

IndexCreationException (org.apache.geode.cache.query.IndexCreationException)5 MultiIndexCreationException (org.apache.geode.cache.query.MultiIndexCreationException)5 Index (org.apache.geode.cache.query.Index)4 PartitionedIndex (org.apache.geode.cache.query.internal.index.PartitionedIndex)4 IOException (java.io.IOException)3 AbstractIndex (org.apache.geode.cache.query.internal.index.AbstractIndex)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 ExecutionException (java.util.concurrent.ExecutionException)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 CancelException (org.apache.geode.CancelException)2 CacheException (org.apache.geode.cache.CacheException)2 RegionDestroyedException (org.apache.geode.cache.RegionDestroyedException)2 FunctionException (org.apache.geode.cache.execute.FunctionException)2 IndexExistsException (org.apache.geode.cache.query.IndexExistsException)2 IndexInvalidException (org.apache.geode.cache.query.IndexInvalidException)2 IndexNameConflictException (org.apache.geode.cache.query.IndexNameConflictException)2 IndexCreationMsg (org.apache.geode.internal.cache.partitioned.IndexCreationMsg)2 MalformedURLException (java.net.MalformedURLException)1