Search in sources :

Example 6 with IndexExistsException

use of org.apache.geode.cache.query.IndexExistsException in project geode by apache.

the class EquiJoinIntegrationTest method testSingleFilterWithSingleEquijoinMultipleFiltersOnSameRegionOnSameIteratorMapping.

@Test
public void testSingleFilterWithSingleEquijoinMultipleFiltersOnSameRegionOnSameIteratorMapping() throws Exception {
    createRegions();
    String[] queries = new String[] { "select * from /region1 c, /region2 s where c.pkid=1 and c.pkid = s.pkid and c.id = 1", "select * from /region1 c, /region2 s where c.id = 1 and c.pkid=1 and s.pkid = c.pkid" };
    for (int i = 0; i < 1000; i++) {
        region1.put(i, new Customer(i, i % 10));
        region2.put(i, new Customer(i, i));
    }
    executeQueriesWithIndexCombinations(queries, new DefaultIndexCreatorCallback(qs) {

        Index secondaryIndex;

        @Override
        public void createIndexForRegion1(int indexTypeId) throws RegionNotFoundException, IndexExistsException, IndexNameConflictException {
            secondaryIndex = qs.createIndex("region1 id", "p.id", "/region1 p");
            super.createIndexForRegion1(indexTypeId);
        }

        @Override
        public void destroyIndexForRegion1(int indexTypeId) {
            qs.removeIndex(secondaryIndex);
            super.destroyIndexForRegion1(indexTypeId);
        }
    }, false);
}
Also used : IndexExistsException(org.apache.geode.cache.query.IndexExistsException) IndexNameConflictException(org.apache.geode.cache.query.IndexNameConflictException) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) Index(org.apache.geode.cache.query.Index) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 7 with IndexExistsException

use of org.apache.geode.cache.query.IndexExistsException in project geode by apache.

the class EquiJoinIntegrationTest method testSingleFilterWithSingleEquijoinLimit.

@Test
public void testSingleFilterWithSingleEquijoinLimit() throws Exception {
    // In this test we are hoping the index being used will properly use the limit while taking into
    // consideration the filters of c.id and c.pkid
    // This test is set up so that if the pkid index is used and limit applied, if id is not taken
    // into consideration until later stages, it will lead to incorrect results (0)
    createRegions();
    String[] queries = new String[] { "select * from /region1 c, /region2 s where c.id = 3 and c.pkid > 2  and c.pkid = s.pkid limit 1" };
    for (int i = 0; i < 1000; i++) {
        region1.put(i, new Customer(i, i % 10));
        region2.put(i, new Customer(i, i));
    }
    executeQueriesWithIndexCombinations(queries, new DefaultIndexCreatorCallback(qs) {

        Index secondaryIndex;

        @Override
        public void createIndexForRegion1(int indexTypeId) throws RegionNotFoundException, IndexExistsException, IndexNameConflictException {
            secondaryIndex = qs.createIndex("region1 id", "p.id", "/region1 p");
            super.createIndexForRegion1(indexTypeId);
        }

        @Override
        public void destroyIndexForRegion1(int indexTypeId) {
            qs.removeIndex(secondaryIndex);
            super.destroyIndexForRegion1(indexTypeId);
        }
    }, true);
}
Also used : IndexExistsException(org.apache.geode.cache.query.IndexExistsException) IndexNameConflictException(org.apache.geode.cache.query.IndexNameConflictException) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) Index(org.apache.geode.cache.query.Index) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 8 with IndexExistsException

use of org.apache.geode.cache.query.IndexExistsException in project geode by apache.

the class IndexManager method createIndex.

// @todo need more specific list of exceptions
/**
   * Create an index that can be used when executing queries.
   * 
   * @param indexName the name of this index, used for statistics collection
   * @param indexType the type of index
   * @param origIndexedExpression the expression to index on, a function dependent on region entries
   *        individually.
   * @param origFromClause expression that evaluates to the collection(s) that will be queried over,
   *        must contain one and only one region path.
   * @return the newly created Index
   */
public Index createIndex(String indexName, IndexType indexType, String origIndexedExpression, String origFromClause, String imports, ExecutionContext externalContext, PartitionedIndex prIndex, boolean loadEntries) throws IndexNameConflictException, IndexExistsException, IndexInvalidException {
    if (QueryMonitor.isLowMemory()) {
        throw new IndexInvalidException(LocalizedStrings.IndexCreationMsg_CANCELED_DUE_TO_LOW_MEMORY.toLocalizedString());
    }
    boolean oldReadSerialized = DefaultQuery.getPdxReadSerialized();
    DefaultQuery.setPdxReadSerialized(this.region.getCache(), true);
    TXStateProxy tx = null;
    if (!((InternalCache) this.region.getCache()).isClient()) {
        tx = ((TXManagerImpl) this.region.getCache().getCacheTransactionManager()).internalSuspend();
    }
    try {
        // for now this is the only option
        String projectionAttributes = "*";
        if (getIndex(indexName) != null) {
            throw new IndexNameConflictException(LocalizedStrings.IndexManager_INDEX_NAMED_0_ALREADY_EXISTS.toLocalizedString(indexName));
        }
        IndexCreationHelper helper = null;
        boolean isCompactOrHash = false;
        // to recalculate the index key for the entry for comparisons during query.
        if (indexType == IndexType.HASH && isOverFlowRegion()) {
            indexType = IndexType.FUNCTIONAL;
        }
        if (indexType != IndexType.PRIMARY_KEY) {
            helper = new FunctionalIndexCreationHelper(origFromClause, origIndexedExpression, projectionAttributes, imports, (InternalCache) region.getCache(), externalContext, this);
            // Asif: For now support Map index as non compact .expand later
            // The limitation for compact range index also apply to hash index for now
            isCompactOrHash = shouldCreateCompactIndex((FunctionalIndexCreationHelper) helper);
        } else if (indexType == IndexType.PRIMARY_KEY) {
            helper = new PrimaryKeyIndexCreationHelper(origFromClause, origIndexedExpression, projectionAttributes, (InternalCache) region.getCache(), externalContext, this);
        } else {
            throw new AssertionError("Don't know how to set helper for " + indexType);
        }
        if (!isCompactOrHash && indexType != IndexType.PRIMARY_KEY) {
            if (indexType == IndexType.HASH) {
                if (!isIndexMaintenanceTypeSynchronous()) {
                    throw new UnsupportedOperationException(LocalizedStrings.DefaultQueryService_HASH_INDEX_CREATION_IS_NOT_SUPPORTED_FOR_ASYNC_MAINTENANCE.toLocalizedString());
                }
                throw new UnsupportedOperationException(LocalizedStrings.DefaultQueryService_HASH_INDEX_CREATION_IS_NOT_SUPPORTED_FOR_MULTIPLE_ITERATORS.toLocalizedString());
            }
            // Overflow is not supported with range index.
            if (isOverFlowRegion()) {
                throw new UnsupportedOperationException(LocalizedStrings.DefaultQueryService_INDEX_CREATION_IS_NOT_SUPPORTED_FOR_REGIONS_WHICH_OVERFLOW_TO_DISK_THE_REGION_INVOLVED_IS_0.toLocalizedString(region.getFullPath()));
            }
            // OffHeap is not supported with range index.
            if (isOffHeap()) {
                if (!isIndexMaintenanceTypeSynchronous()) {
                    throw new UnsupportedOperationException(LocalizedStrings.DefaultQueryService_OFF_HEAP_INDEX_CREATION_IS_NOT_SUPPORTED_FOR_ASYNC_MAINTENANCE_THE_REGION_IS_0.toLocalizedString(region.getFullPath()));
                }
                throw new UnsupportedOperationException(LocalizedStrings.DefaultQueryService_OFF_HEAP_INDEX_CREATION_IS_NOT_SUPPORTED_FOR_MULTIPLE_ITERATORS_THE_REGION_IS_0.toLocalizedString(region.getFullPath()));
            }
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Started creating index with indexName: {} On region: {}", indexName, region.getFullPath());
        }
        if (IndexManager.testHook != null) {
            if (logger.isDebugEnabled()) {
                logger.debug("IndexManager TestHook is set.");
            }
            if (((LocalRegion) this.region).isInitialized()) {
                testHook.hook(1);
            } else {
                testHook.hook(0);
            }
        }
        IndexTask indexTask = new IndexTask(indexName, indexType, origFromClause, origIndexedExpression, helper, isCompactOrHash, prIndex, loadEntries);
        FutureTask<Index> indexFutureTask = new FutureTask<Index>(indexTask);
        Object oldIndex = this.indexes.putIfAbsent(indexTask, indexFutureTask);
        Index index = null;
        boolean interrupted = false;
        try {
            if (oldIndex == null) {
                // Initialize index.
                indexFutureTask.run();
                // Set the index.
                index = (Index) indexFutureTask.get();
            } else {
                // Check if index creation is complete.
                if (!(oldIndex instanceof Index)) {
                    // Some other thread is creating the same index.
                    // Wait for index to be initialized from other thread.
                    ((Future) oldIndex).get();
                }
                // from this thread.
                if (getIndex(indexName) != null) {
                    throw new IndexNameConflictException(LocalizedStrings.IndexManager_INDEX_NAMED_0_ALREADY_EXISTS.toLocalizedString(indexName));
                } else {
                    throw new IndexExistsException(LocalizedStrings.IndexManager_SIMILAR_INDEX_EXISTS.toLocalizedString());
                }
            }
        } catch (InterruptedException ignored) {
            interrupted = true;
        } catch (ExecutionException ee) {
            Throwable c = ee.getCause();
            if (c instanceof IndexNameConflictException) {
                throw (IndexNameConflictException) c;
            } else if (c instanceof IndexExistsException) {
                throw (IndexExistsException) c;
            } else if (c instanceof IMQException) {
                throw new IndexInvalidException(c.getMessage());
            }
            throw new IndexInvalidException(ee);
        } finally {
            // the map.
            if (oldIndex == null && index == null) {
                Object ind = this.indexes.get(indexTask);
                if (ind != null && !(ind instanceof Index)) {
                    this.indexes.remove(indexTask);
                }
            }
            if (interrupted) {
                Thread.currentThread().interrupt();
            }
        }
        assert (index != null);
        if (logger.isDebugEnabled()) {
            logger.debug("Completed creating index with indexName: {} On region: {}", indexName, region.getFullPath());
        }
        return index;
    } finally {
        DefaultQuery.setPdxReadSerialized(this.region.getCache(), oldReadSerialized);
        if (tx != null) {
            ((TXManagerImpl) this.region.getCache().getCacheTransactionManager()).internalResume(tx);
        }
    }
}
Also used : IndexExistsException(org.apache.geode.cache.query.IndexExistsException) TXManagerImpl(org.apache.geode.internal.cache.TXManagerImpl) InternalCache(org.apache.geode.internal.cache.InternalCache) Index(org.apache.geode.cache.query.Index) LocalRegion(org.apache.geode.internal.cache.LocalRegion) FutureTask(java.util.concurrent.FutureTask) TXStateProxy(org.apache.geode.internal.cache.TXStateProxy) IndexNameConflictException(org.apache.geode.cache.query.IndexNameConflictException) Future(java.util.concurrent.Future) IndexInvalidException(org.apache.geode.cache.query.IndexInvalidException) ExecutionException(java.util.concurrent.ExecutionException)

Example 9 with IndexExistsException

use of org.apache.geode.cache.query.IndexExistsException in project geode by apache.

the class NonDistinctOrderByDUnitImpl method createIndex.

protected void createIndex(VM vm, final String indexName, IndexType indexType, final String indexedExpression, final String fromClause) {
    int indxTypeCode = -1;
    if (indexType.equals(IndexType.FUNCTIONAL)) {
        indxTypeCode = 0;
    } else if (indexType.equals(IndexType.PRIMARY_KEY)) {
        indxTypeCode = 1;
    } else if (indexType.equals(IndexType.HASH)) {
        indxTypeCode = 2;
    }
    final int finalIndxTypeCode = indxTypeCode;
    vm.invoke(new SerializableRunnable("create index") {

        public void run() {
            try {
                Cache cache = getCache();
                IndexType indxType = null;
                if (finalIndxTypeCode == 0) {
                    indxType = IndexType.FUNCTIONAL;
                } else if (finalIndxTypeCode == 1) {
                    indxType = IndexType.PRIMARY_KEY;
                } else if (finalIndxTypeCode == 2) {
                    indxType = IndexType.HASH;
                }
                cache.getQueryService().createIndex(indexName, indxType, indexedExpression, fromClause);
            } catch (RegionNotFoundException e) {
                fail(e.toString());
            } catch (IndexExistsException e) {
                fail(e.toString());
            } catch (IndexNameConflictException e) {
                fail(e.toString());
            }
        }
    });
}
Also used : IndexExistsException(org.apache.geode.cache.query.IndexExistsException) IndexNameConflictException(org.apache.geode.cache.query.IndexNameConflictException) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) IndexType(org.apache.geode.cache.query.IndexType) Cache(org.apache.geode.cache.Cache)

Example 10 with IndexExistsException

use of org.apache.geode.cache.query.IndexExistsException 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)

Aggregations

IndexExistsException (org.apache.geode.cache.query.IndexExistsException)12 IndexNameConflictException (org.apache.geode.cache.query.IndexNameConflictException)11 RegionNotFoundException (org.apache.geode.cache.query.RegionNotFoundException)6 Index (org.apache.geode.cache.query.Index)5 Cache (org.apache.geode.cache.Cache)4 QueryService (org.apache.geode.cache.query.QueryService)4 Region (org.apache.geode.cache.Region)3 IndexInvalidException (org.apache.geode.cache.query.IndexInvalidException)3 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)3 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)3 Test (org.junit.Test)3 ExecutionException (java.util.concurrent.ExecutionException)2 FutureTask (java.util.concurrent.FutureTask)2 IndexType (org.apache.geode.cache.query.IndexType)2 MultiIndexCreationException (org.apache.geode.cache.query.MultiIndexCreationException)2 LocalRegion (org.apache.geode.internal.cache.LocalRegion)2 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1