Search in sources :

Example 11 with IndexNameConflictException

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

the class QueryIndexDUnitTest method createIndexAndUpdatesOnOverflowRegionsAndValidateResults.

@Test
public void createIndexAndUpdatesOnOverflowRegionsAndValidateResults() throws Exception {
    Host host = Host.getHost(0);
    VM[] vms = new VM[] { host.getVM(0), host.getVM(1) };
    // Create and load regions on all vms.
    for (int i = 0; i < vms.length; i++) {
        int finalI = i;
        vms[i].invoke(() -> QueryIndexDUnitTest.createAndLoadOverFlowRegions("testOfValid3" + "vm" + finalI, new Boolean(true), new Boolean(true)));
    }
    vms[0].invoke(new CacheSerializableRunnable("Execute query validate results") {

        public void run2() throws CacheException {
            Cache cache = basicGetCache();
            String[] regionNames = new String[] { "replicateOverFlowRegion", "replicatePersistentOverFlowRegion", "prOverFlowRegion", "prPersistentOverFlowRegion" };
            QueryService qs = cache.getQueryService();
            Region region = null;
            String[] qString = new String[] { "SELECT * FROM /REGION_NAME pf WHERE pf.ID = 1", "SELECT ID FROM /REGION_NAME pf WHERE pf.ID = 1", "SELECT * FROM /REGION_NAME pf WHERE pf.ID > 5", "SELECT ID FROM /REGION_NAME pf WHERE pf.ID > 5", "SELECT * FROM /REGION_NAME.keys key WHERE key.ID = 1", "SELECT ID FROM /REGION_NAME.keys key WHERE key.ID = 1", "SELECT * FROM /REGION_NAME.keys key WHERE key.ID > 5", "SELECT ID FROM /REGION_NAME.keys key WHERE key.ID > 5" };
            // Execute Query without index.
            SelectResults[] srWithoutIndex = new SelectResults[qString.length * regionNames.length];
            String[] queryString = new String[qString.length * regionNames.length];
            int r = 0;
            try {
                for (int q = 0; q < qString.length; q++) {
                    for (int i = 0; i < regionNames.length; i++) {
                        String queryStr = qString[q].replace("REGION_NAME", regionNames[i]);
                        Query query = qs.newQuery(queryStr);
                        queryString[r] = queryStr;
                        srWithoutIndex[r] = (SelectResults) query.execute();
                        r++;
                    }
                }
            } catch (Exception ex) {
                logger.info("Failed to Execute query", ex);
                fail("Failed to Execute query.");
            }
            // Create index.
            try {
                for (int i = 0; i < regionNames.length; i++) {
                    region = cache.getRegion(regionNames[i]);
                    String indexName = "idIndex" + regionNames[i];
                    cache.getLogger().fine("createIndexOnOverFlowRegions() checking for index: " + indexName);
                    try {
                        if (qs.getIndex(region, indexName) == null) {
                            cache.getLogger().fine("createIndexOnOverFlowRegions() Index doesn't exist, creating index: " + indexName);
                            Index i1 = qs.createIndex(indexName, "pf.ID", "/" + regionNames[i] + " pf");
                        }
                        indexName = "keyIdIndex" + regionNames[i];
                        if (qs.getIndex(region, indexName) == null) {
                            cache.getLogger().fine("createIndexOnOverFlowRegions() Index doesn't exist, creating index: " + indexName);
                            Index i2 = qs.createIndex(indexName, "key.ID", "/" + regionNames[i] + ".keys key");
                        }
                    } catch (IndexNameConflictException ice) {
                    // Ignore. The pr may have created the index through
                    // remote index create message from peer.
                    }
                }
            } catch (Exception ex) {
                logger.info("Failed to create index", ex);
                fail("Failed to create index.");
            }
            int numObjects = 50;
            for (int i = 0; i < regionNames.length; i++) {
                region = cache.getRegion(regionNames[i]);
                for (int cnt = 0; cnt < numObjects; cnt++) {
                    region.put(new Portfolio(cnt), new Portfolio(cnt));
                }
            }
            for (int i = 0; i < regionNames.length; i++) {
                region = cache.getRegion(regionNames[i]);
                String indexName = "idIndex" + regionNames[i];
                Index i1, i2;
                if ((i1 = qs.getIndex(region, indexName)) != null) {
                    assertEquals("Unexpected number of keys in the index ", numObjects, i1.getStatistics().getNumberOfKeys());
                    assertEquals("Unexpected number of values in the index ", numObjects, i1.getStatistics().getNumberOfValues());
                }
                indexName = "keyIdIndex" + regionNames[i];
                if ((i2 = qs.getIndex(region, indexName)) != null) {
                    assertEquals("Unexpected number of keys in the index ", numObjects, i2.getStatistics().getNumberOfKeys());
                    assertEquals("Unexpected number of values in the index ", numObjects, i2.getStatistics().getNumberOfValues());
                }
            }
            // Execute Query with index.
            SelectResults[] srWithIndex = new SelectResults[qString.length * regionNames.length];
            try {
                r = 0;
                for (int q = 0; q < qString.length; q++) {
                    for (int i = 0; i < regionNames.length; i++) {
                        QueryObserverImpl observer = new QueryObserverImpl();
                        QueryObserverHolder.setInstance(observer);
                        String queryStr = qString[q].replace("REGION_NAME", regionNames[i]);
                        Query query = qs.newQuery(queryStr);
                        srWithIndex[r++] = (SelectResults) query.execute();
                        if (!observer.isIndexesUsed) {
                            fail("Index not used for query. " + queryStr);
                        }
                    }
                }
            } catch (Exception ex) {
                logger.info("Failed to Execute query", ex);
                fail("Failed to Execute query.");
            }
            // Compare results with and without index.
            StructSetOrResultsSet ssORrs = new StructSetOrResultsSet();
            SelectResults[][] sr = new SelectResults[1][2];
            for (int i = 0; i < srWithIndex.length; i++) {
                sr[0][0] = srWithoutIndex[i];
                sr[0][1] = srWithIndex[i];
                logger.info("Comparing the result for the query : " + queryString[i] + " Index in ResultSet is: " + i);
                ssORrs.CompareQueryResultsWithoutAndWithIndexes(sr, 1, queryString);
            }
        }
    });
}
Also used : Query(org.apache.geode.cache.query.Query) StructSetOrResultsSet(org.apache.geode.cache.query.functional.StructSetOrResultsSet) CacheException(org.apache.geode.cache.CacheException) Portfolio(org.apache.geode.cache.query.data.Portfolio) Host(org.apache.geode.test.dunit.Host) Index(org.apache.geode.cache.query.Index) CacheException(org.apache.geode.cache.CacheException) QueryException(org.apache.geode.cache.query.QueryException) IndexNameConflictException(org.apache.geode.cache.query.IndexNameConflictException) IndexExistsException(org.apache.geode.cache.query.IndexExistsException) SelectResults(org.apache.geode.cache.query.SelectResults) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) QueryService(org.apache.geode.cache.query.QueryService) IndexNameConflictException(org.apache.geode.cache.query.IndexNameConflictException) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 12 with IndexNameConflictException

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

the class QueryIndexDUnitTest method createIndexOnOverflowRegionsAndValidateResults.

@Test
public void createIndexOnOverflowRegionsAndValidateResults() throws Exception {
    Host host = Host.getHost(0);
    VM[] vms = new VM[] { host.getVM(0), host.getVM(1) };
    // Create and load regions on all vms.
    for (int i = 0; i < vms.length; i++) {
        int finalI = i;
        vms[i].invoke(() -> QueryIndexDUnitTest.createAndLoadOverFlowRegions("testOfValid" + "vm" + finalI, new Boolean(true), new Boolean(false)));
    }
    vms[0].invoke(new CacheSerializableRunnable("Execute query validate results") {

        public void run2() throws CacheException {
            Cache cache = basicGetCache();
            String[] regionNames = new String[] { "replicateOverFlowRegion", "replicatePersistentOverFlowRegion", "prOverFlowRegion", "prPersistentOverFlowRegion" };
            QueryService qs = cache.getQueryService();
            Region region = null;
            int numObjects = 10;
            // The index should get updated accordingly.
            for (int i = 0; i < regionNames.length; i++) {
                region = cache.getRegion(regionNames[i]);
                for (int cnt = 1; cnt < numObjects; cnt++) {
                    region.put(new Portfolio(cnt), new Portfolio(cnt));
                }
            }
            String[] qString = new String[] { "SELECT * FROM /REGION_NAME pf WHERE pf.ID = 1", "SELECT ID FROM /REGION_NAME pf WHERE pf.ID = 1", "SELECT * FROM /REGION_NAME pf WHERE pf.ID > 5", "SELECT ID FROM /REGION_NAME pf WHERE pf.ID > 5", "SELECT * FROM /REGION_NAME.keys key WHERE key.ID = 1", "SELECT ID FROM /REGION_NAME.keys key WHERE key.ID = 1", "SELECT * FROM /REGION_NAME.keys key WHERE key.ID > 5", "SELECT ID FROM /REGION_NAME.keys key WHERE key.ID > 5" };
            // Execute Query without index.
            SelectResults[] srWithoutIndex = new SelectResults[qString.length * regionNames.length];
            String[] queryString = new String[qString.length * regionNames.length];
            int r = 0;
            try {
                for (int q = 0; q < qString.length; q++) {
                    for (int i = 0; i < regionNames.length; i++) {
                        String queryStr = qString[q].replace("REGION_NAME", regionNames[i]);
                        Query query = qs.newQuery(queryStr);
                        queryString[r] = queryStr;
                        srWithoutIndex[r] = (SelectResults) query.execute();
                        r++;
                    }
                }
            } catch (Exception ex) {
                LogWriterUtils.getLogWriter().info("Failed to Execute query", ex);
                fail("Failed to Execute query.");
            }
            // Create index.
            try {
                for (int i = 0; i < regionNames.length; i++) {
                    region = cache.getRegion(regionNames[i]);
                    String indexName = "idIndex" + regionNames[i];
                    cache.getLogger().fine("createIndexOnOverFlowRegions() checking for index: " + indexName);
                    try {
                        if (qs.getIndex(region, indexName) == null) {
                            cache.getLogger().fine("createIndexOnOverFlowRegions() Index doesn't exist, creating index: " + indexName);
                            Index i1 = qs.createIndex(indexName, "pf.ID", "/" + regionNames[i] + " pf");
                        }
                        indexName = "keyIdIndex" + regionNames[i];
                        if (qs.getIndex(region, indexName) == null) {
                            cache.getLogger().fine("createIndexOnOverFlowRegions() Index doesn't exist, creating index: " + indexName);
                            Index i2 = qs.createIndex(indexName, "key.ID", "/" + regionNames[i] + ".keys key");
                        }
                    } catch (IndexNameConflictException ice) {
                    // Ignore. The pr may have created the index through
                    // remote index create message from peer.
                    }
                }
            } catch (Exception ex) {
                logger.info("Failed to create index", ex);
                fail("Failed to create index.");
            }
            // Execute Query with index.
            SelectResults[] srWithIndex = new SelectResults[qString.length * regionNames.length];
            try {
                r = 0;
                for (int q = 0; q < qString.length; q++) {
                    for (int i = 0; i < regionNames.length; i++) {
                        QueryObserverImpl observer = new QueryObserverImpl();
                        QueryObserverHolder.setInstance(observer);
                        String queryStr = qString[q].replace("REGION_NAME", regionNames[i]);
                        Query query = qs.newQuery(queryStr);
                        srWithIndex[r++] = (SelectResults) query.execute();
                        if (!observer.isIndexesUsed) {
                            fail("Index not used for query. " + queryStr);
                        }
                    }
                }
            } catch (Exception ex) {
                LogWriterUtils.getLogWriter().info("Failed to Execute query", ex);
                fail("Failed to Execute query.");
            }
            // Compare results with and without index.
            StructSetOrResultsSet ssORrs = new StructSetOrResultsSet();
            SelectResults[][] sr = new SelectResults[1][2];
            for (int i = 0; i < srWithIndex.length; i++) {
                sr[0][0] = srWithoutIndex[i];
                sr[0][1] = srWithIndex[i];
                logger.info("Comparing the result for the query : " + queryString[i] + " Index in ResultSet is: " + i);
                ssORrs.CompareQueryResultsWithoutAndWithIndexes(sr, 1, queryString);
            }
            // The index should get updated accordingly.
            for (int i = 0; i < regionNames.length; i++) {
                region = cache.getRegion(regionNames[i]);
                for (int cnt = 1; cnt < numObjects; cnt++) {
                    if (cnt % 2 == 0) {
                        region.destroy(new Portfolio(cnt));
                    }
                }
                for (int cnt = 10; cnt < numObjects; cnt++) {
                    if (cnt % 2 == 0) {
                        region.put(new Portfolio(cnt), new Portfolio(cnt));
                    }
                }
            }
            // Execute Query with index.
            srWithIndex = new SelectResults[qString.length * regionNames.length];
            try {
                r = 0;
                for (int q = 0; q < qString.length; q++) {
                    for (int i = 0; i < regionNames.length; i++) {
                        QueryObserverImpl observer = new QueryObserverImpl();
                        QueryObserverHolder.setInstance(observer);
                        String queryStr = qString[q].replace("REGION_NAME", regionNames[i]);
                        Query query = qs.newQuery(queryStr);
                        srWithIndex[r++] = (SelectResults) query.execute();
                        if (!observer.isIndexesUsed) {
                            fail("Index not used for query. " + queryStr);
                        }
                    }
                }
            } catch (Exception ex) {
                logger.info("Failed to Execute query", ex);
                fail("Failed to Execute query.");
            }
        }
    });
}
Also used : Query(org.apache.geode.cache.query.Query) StructSetOrResultsSet(org.apache.geode.cache.query.functional.StructSetOrResultsSet) CacheException(org.apache.geode.cache.CacheException) Portfolio(org.apache.geode.cache.query.data.Portfolio) Host(org.apache.geode.test.dunit.Host) Index(org.apache.geode.cache.query.Index) CacheException(org.apache.geode.cache.CacheException) QueryException(org.apache.geode.cache.query.QueryException) IndexNameConflictException(org.apache.geode.cache.query.IndexNameConflictException) IndexExistsException(org.apache.geode.cache.query.IndexExistsException) SelectResults(org.apache.geode.cache.query.SelectResults) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) QueryService(org.apache.geode.cache.query.QueryService) IndexNameConflictException(org.apache.geode.cache.query.IndexNameConflictException) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 13 with IndexNameConflictException

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

the class QueryIndexDUnitTest method createIndexOnOverflowRegionsAndValidateResultsUsingParams.

@Test
public void createIndexOnOverflowRegionsAndValidateResultsUsingParams() throws Exception {
    Host host = Host.getHost(0);
    VM[] vms = new VM[] { host.getVM(0), host.getVM(1) };
    // Create and load regions on all vms.
    for (int i = 0; i < vms.length; i++) {
        int finalI = i;
        vms[i].invoke(() -> QueryIndexDUnitTest.createAndLoadOverFlowRegions("testOfValidUseParams" + "vm" + finalI, new Boolean(true), new Boolean(false)));
    }
    vms[0].invoke(new CacheSerializableRunnable("Execute query validate results") {

        public void run2() throws CacheException {
            Cache cache = basicGetCache();
            String[] regionNames = new String[] { "replicateOverFlowRegion", "replicatePersistentOverFlowRegion", "prOverFlowRegion", "prPersistentOverFlowRegion" };
            QueryService qs = cache.getQueryService();
            Region region = null;
            int numObjects = 10;
            // The index should get updated accordingly.
            for (int i = 0; i < regionNames.length; i++) {
                region = cache.getRegion(regionNames[i]);
                for (int cnt = 1; cnt < numObjects; cnt++) {
                    region.put(new Portfolio(cnt), new Integer(cnt + 100));
                }
            }
            String[] qString = new String[] { "SELECT * FROM /REGION_NAME pf WHERE pf = $1", "SELECT * FROM /REGION_NAME pf WHERE pf > $1", "SELECT * FROM /REGION_NAME.values pf WHERE pf < $1", "SELECT * FROM /REGION_NAME.keys k WHERE k.ID = $1", "SELECT key.ID FROM /REGION_NAME.keys key WHERE key.ID = $1", "SELECT ID, status FROM /REGION_NAME.keys WHERE ID = $1", "SELECT k.ID, k.status FROM /REGION_NAME.keys k WHERE k.ID = $1 and k.status = $2", "SELECT * FROM /REGION_NAME.keys key WHERE key.ID > $1", "SELECT key.ID FROM /REGION_NAME.keys key WHERE key.ID > $1 and key.status = $2" };
            // Execute Query without index.
            SelectResults[] srWithoutIndex = new SelectResults[qString.length * regionNames.length];
            String[] queryString = new String[qString.length * regionNames.length];
            int r = 0;
            try {
                for (int q = 0; q < qString.length; q++) {
                    for (int i = 0; i < regionNames.length; i++) {
                        String queryStr = qString[q].replace("REGION_NAME", regionNames[i]);
                        Query query = qs.newQuery(queryStr);
                        queryString[r] = queryStr;
                        srWithoutIndex[r] = (SelectResults) query.execute(new Object[] { new Integer(5), "active" });
                        r++;
                    }
                }
            } catch (Exception ex) {
                logger.info("Failed to Execute query", ex);
                fail("Failed to Execute query.");
            }
            // Create index.
            String indexName = "";
            try {
                for (int i = 0; i < regionNames.length; i++) {
                    region = cache.getRegion(regionNames[i]);
                    indexName = "idIndex" + regionNames[i];
                    cache.getLogger().fine("createIndexOnOverFlowRegions() checking for index: " + indexName);
                    try {
                        if (qs.getIndex(region, indexName) == null) {
                            cache.getLogger().fine("createIndexOnOverFlowRegions() Index doesn't exist, creating index: " + indexName);
                            Index i1 = qs.createIndex(indexName, "pf", "/" + regionNames[i] + " pf");
                        }
                        indexName = "valueIndex" + regionNames[i];
                        if (qs.getIndex(region, indexName) == null) {
                            cache.getLogger().fine("createIndexOnOverFlowRegions() Index doesn't exist, creating index: " + indexName);
                            Index i1 = qs.createIndex(indexName, "pf", "/" + regionNames[i] + ".values pf");
                        }
                        indexName = "keyIdIndex" + regionNames[i];
                        if (qs.getIndex(region, indexName) == null) {
                            cache.getLogger().fine("createIndexOnOverFlowRegions() Index doesn't exist, creating index: " + indexName);
                            Index i2 = qs.createIndex(indexName, "key.ID", "/" + regionNames[i] + ".keys key");
                        }
                    } catch (IndexNameConflictException ice) {
                    // Ignore. The pr may have created the index through
                    // remote index create message from peer.
                    }
                }
            } catch (Exception ex) {
                LogWriterUtils.getLogWriter().info("Failed to create index", ex);
                fail("Failed to create index." + indexName);
            }
            // Execute Query with index.
            SelectResults[] srWithIndex = new SelectResults[qString.length * regionNames.length];
            try {
                r = 0;
                for (int q = 0; q < qString.length; q++) {
                    for (int i = 0; i < regionNames.length; i++) {
                        QueryObserverImpl observer = new QueryObserverImpl();
                        QueryObserverHolder.setInstance(observer);
                        String queryStr = qString[q].replace("REGION_NAME", regionNames[i]);
                        Query query = qs.newQuery(queryStr);
                        srWithIndex[r++] = (SelectResults) query.execute(new Object[] { new Integer(5), "active" });
                        if (!observer.isIndexesUsed) {
                            fail("Index not used for query. " + queryStr);
                        }
                    }
                }
            } catch (Exception ex) {
                logger.info("Failed to Execute query", ex);
                fail("Failed to Execute query.");
            }
            // Compare results with and without index.
            StructSetOrResultsSet ssORrs = new StructSetOrResultsSet();
            SelectResults[][] sr = new SelectResults[1][2];
            for (int i = 0; i < srWithIndex.length; i++) {
                sr[0][0] = srWithoutIndex[i];
                sr[0][1] = srWithIndex[i];
                logger.info("Comparing the result for the query : " + queryString[i] + " Index in ResultSet is: " + i);
                ssORrs.CompareQueryResultsWithoutAndWithIndexes(sr, 1, queryString);
            }
            // The index should get updated accordingly.
            for (int i = 0; i < regionNames.length; i++) {
                region = cache.getRegion(regionNames[i]);
                for (int cnt = 1; cnt < numObjects; cnt++) {
                    if (cnt % 2 == 0) {
                        region.destroy(new Portfolio(cnt));
                    }
                }
                // Add destroyed entries
                for (int cnt = 1; cnt < numObjects; cnt++) {
                    if (cnt % 2 == 0) {
                        region.put(new Portfolio(cnt), new Integer(cnt + 100));
                    }
                }
            }
            // Execute Query with index.
            srWithIndex = new SelectResults[qString.length * regionNames.length];
            try {
                r = 0;
                for (int q = 0; q < qString.length; q++) {
                    for (int i = 0; i < regionNames.length; i++) {
                        QueryObserverImpl observer = new QueryObserverImpl();
                        QueryObserverHolder.setInstance(observer);
                        String queryStr = qString[q].replace("REGION_NAME", regionNames[i]);
                        Query query = qs.newQuery(queryStr);
                        srWithIndex[r++] = (SelectResults) query.execute(new Object[] { new Integer(5), "active" });
                        if (!observer.isIndexesUsed) {
                            fail("Index not used for query. " + queryStr);
                        }
                    }
                }
            } catch (Exception ex) {
                logger.info("Failed to Execute query", ex);
                fail("Failed to Execute query.");
            }
            // Compare results with and without index.
            for (int i = 0; i < srWithIndex.length; i++) {
                sr[0][0] = srWithoutIndex[i];
                sr[0][1] = srWithIndex[i];
                logger.info("Comparing the result for the query : " + queryString[i] + " Index in ResultSet is: " + i);
                ssORrs.CompareQueryResultsWithoutAndWithIndexes(sr, 1, queryString);
            }
        }
    });
}
Also used : Query(org.apache.geode.cache.query.Query) StructSetOrResultsSet(org.apache.geode.cache.query.functional.StructSetOrResultsSet) CacheException(org.apache.geode.cache.CacheException) Portfolio(org.apache.geode.cache.query.data.Portfolio) Host(org.apache.geode.test.dunit.Host) Index(org.apache.geode.cache.query.Index) CacheException(org.apache.geode.cache.CacheException) QueryException(org.apache.geode.cache.query.QueryException) IndexNameConflictException(org.apache.geode.cache.query.IndexNameConflictException) IndexExistsException(org.apache.geode.cache.query.IndexExistsException) SelectResults(org.apache.geode.cache.query.SelectResults) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) QueryService(org.apache.geode.cache.query.QueryService) IndexNameConflictException(org.apache.geode.cache.query.IndexNameConflictException) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 14 with IndexNameConflictException

use of org.apache.geode.cache.query.IndexNameConflictException 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 15 with IndexNameConflictException

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

IndexNameConflictException (org.apache.geode.cache.query.IndexNameConflictException)16 IndexExistsException (org.apache.geode.cache.query.IndexExistsException)15 Cache (org.apache.geode.cache.Cache)9 Index (org.apache.geode.cache.query.Index)9 QueryService (org.apache.geode.cache.query.QueryService)8 Region (org.apache.geode.cache.Region)7 RegionNotFoundException (org.apache.geode.cache.query.RegionNotFoundException)6 Test (org.junit.Test)6 Query (org.apache.geode.cache.query.Query)5 QueryException (org.apache.geode.cache.query.QueryException)5 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)5 CacheException (org.apache.geode.cache.CacheException)4 SelectResults (org.apache.geode.cache.query.SelectResults)4 Portfolio (org.apache.geode.cache.query.data.Portfolio)4 StructSetOrResultsSet (org.apache.geode.cache.query.functional.StructSetOrResultsSet)4 Host (org.apache.geode.test.dunit.Host)4 VM (org.apache.geode.test.dunit.VM)4 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)4 IndexInvalidException (org.apache.geode.cache.query.IndexInvalidException)3 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)3