Search in sources :

Example 21 with PartitionedIndex

use of org.apache.geode.cache.query.internal.index.PartitionedIndex in project geode by apache.

the class QueryTraceJUnitTest method testNegTraceOnPartitionedRegionWithTracePrefix.

/**
   * negative testing: if <TRACE> is in comments not tracing is done.
   * 
   * @throws Exception
   */
@Test
public void testNegTraceOnPartitionedRegionWithTracePrefix() throws Exception {
    String slComment = "-- single line comment with TRACE \n";
    String mlComment = " /* Multi-line comments here" + "* ends here " + "* with TRACE too" + "*/";
    String prefix = slComment + mlComment;
    // Create Partition Region
    PartitionAttributesFactory paf = new PartitionAttributesFactory();
    paf.setTotalNumBuckets(NUM_BKTS);
    AttributesFactory af = new AttributesFactory();
    af.setPartitionAttributes(paf.create());
    region = CacheUtils.createRegion("portfolio", af.create(), false);
    if (region.size() == 0) {
        for (int i = 1; i <= 100; i++) {
            region.put(Integer.toString(i), new Portfolio(i, i));
        }
    }
    assertEquals(100, region.size());
    qs = CacheUtils.getQueryService();
    keyIndex1 = (IndexProtocol) qs.createIndex(INDEX_NAME, IndexType.FUNCTIONAL, "ID", "/portfolio ");
    assertTrue(keyIndex1 instanceof PartitionedIndex);
    Query query = qs.newQuery(prefix + queryStr);
    assertFalse(((DefaultQuery) query).isTraced());
    SelectResults results = (SelectResults) query.execute();
    assertFalse(QueryObserverHolder.getInstance() instanceof IndexTrackingQueryObserver);
    // The query should return all elements in region.
    assertEquals(region.size(), results.size());
    QueryObserverHolder.reset();
}
Also used : PartitionedIndex(org.apache.geode.cache.query.internal.index.PartitionedIndex) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) SelectResults(org.apache.geode.cache.query.SelectResults) Query(org.apache.geode.cache.query.Query) Portfolio(org.apache.geode.cache.query.data.Portfolio) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 22 with PartitionedIndex

use of org.apache.geode.cache.query.internal.index.PartitionedIndex in project geode by apache.

the class PartitionedRegionDataStore method getIndexes.

private List getIndexes(String rootRegion, String bucketRegion) {
    List indexes = null;
    if (!this.partitionedRegion.isIndexed()) {
        return indexes;
    }
    // Get PR indexes.
    Map indexMap = this.partitionedRegion.getIndex();
    if (indexMap == null || indexMap.isEmpty()) {
        return indexes;
    }
    // Build index info thats used to create indexes on bucket regions.
    indexes = new ArrayList();
    Set indexSet = indexMap.entrySet();
    for (Iterator it = indexSet.iterator(); it.hasNext(); ) {
        try {
            Map.Entry indexEntry = (Map.Entry) it.next();
            PartitionedIndex index = (PartitionedIndex) indexEntry.getValue();
            IndexCreationData icd = new IndexCreationData(index.getName());
            new QCompiler();
            String imports = index.getImports();
            icd.setIndexData(index.getType(), index.getCanonicalizedFromClause(), index.getCanonicalizedIndexedExpression(), index.getImports());
            icd.setPartitionedIndex(index);
            indexes.add(icd);
        } catch (Exception ignor) {
            // since bucket creation should not fail.
            logger.info(LocalizedMessage.create(LocalizedStrings.PartitionedRegionDataStore_EXCPETION__IN_BUCKET_INDEX_CREATION_, ignor.getLocalizedMessage()), ignor);
        }
    }
    return indexes;
}
Also used : IndexCreationData(org.apache.geode.cache.query.internal.index.IndexCreationData) PartitionedIndex(org.apache.geode.cache.query.internal.index.PartitionedIndex) Entry(org.apache.geode.cache.Region.Entry) SizeEntry(org.apache.geode.internal.cache.PartitionedRegion.SizeEntry) QCompiler(org.apache.geode.cache.query.internal.QCompiler) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) BucketMovedException(org.apache.geode.internal.cache.execute.BucketMovedException) InternalGemFireException(org.apache.geode.InternalGemFireException) QueryInvalidException(org.apache.geode.cache.query.QueryInvalidException) FunctionException(org.apache.geode.cache.execute.FunctionException) IOException(java.io.IOException)

Example 23 with PartitionedIndex

use of org.apache.geode.cache.query.internal.index.PartitionedIndex 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 24 with PartitionedIndex

use of org.apache.geode.cache.query.internal.index.PartitionedIndex 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 25 with PartitionedIndex

use of org.apache.geode.cache.query.internal.index.PartitionedIndex in project geode by apache.

the class PRQueryDUnitHelper method getCacheSerializableRunnableForIndexCreationCheck.

public CacheSerializableRunnable getCacheSerializableRunnableForIndexCreationCheck(final String name) {
    return new CacheSerializableRunnable("PrIndexCreationCheck") {

        @Override
        public void run2() {
            Cache cache1 = getCache();
            LogWriter logger = cache1.getLogger();
            PartitionedRegion region = (PartitionedRegion) cache1.getRegion(name);
            Map indexMap = region.getIndex();
            Set indexSet = indexMap.entrySet();
            Iterator it = indexSet.iterator();
            while (it.hasNext()) {
                Map.Entry entry = (Map.Entry) it.next();
                Index index = (Index) entry.getValue();
                logger.info("the partitioned index created on this region " + " " + index);
                logger.info("Current number of buckets indexed : " + "" + ((PartitionedIndex) index).getNumberOfIndexedBuckets());
            }
            JUnit4CacheTestCase.closeCache();
            JUnit4DistributedTestCase.disconnectFromDS();
        }
    };
}
Also used : PartitionedIndex(org.apache.geode.cache.query.internal.index.PartitionedIndex) Set(java.util.Set) StructSetOrResultsSet(org.apache.geode.cache.query.functional.StructSetOrResultsSet) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) LogWriter(org.apache.geode.LogWriter) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Iterator(java.util.Iterator) Index(org.apache.geode.cache.query.Index) PartitionedIndex(org.apache.geode.cache.query.internal.index.PartitionedIndex) Map(java.util.Map) Cache(org.apache.geode.cache.Cache)

Aggregations

PartitionedIndex (org.apache.geode.cache.query.internal.index.PartitionedIndex)32 Index (org.apache.geode.cache.query.Index)17 Query (org.apache.geode.cache.query.Query)17 Test (org.junit.Test)17 Portfolio (org.apache.geode.cache.query.data.Portfolio)15 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)13 SelectResults (org.apache.geode.cache.query.SelectResults)9 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)9 AttributesFactory (org.apache.geode.cache.AttributesFactory)8 PartitionAttributesFactory (org.apache.geode.cache.PartitionAttributesFactory)8 IndexStatistics (org.apache.geode.cache.query.IndexStatistics)8 AbstractIndex (org.apache.geode.cache.query.internal.index.AbstractIndex)8 CacheException (org.apache.geode.cache.CacheException)7 QueryService (org.apache.geode.cache.query.QueryService)7 IOException (java.io.IOException)6 Region (org.apache.geode.cache.Region)6 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)6 Iterator (java.util.Iterator)5 LogWriter (org.apache.geode.LogWriter)5 Cache (org.apache.geode.cache.Cache)5