Search in sources :

Example 11 with PartitionedIndex

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

the class QueryTraceJUnitTest method testTraceOnPartitionedRegionWithTracePrefixNoComments.

/**
   * No Query comments
   * 
   * @throws Exception
   */
@Test
public void testTraceOnPartitionedRegionWithTracePrefixNoComments() throws Exception {
    String prefix = "  <TRACE> ";
    // 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);
    assertTrue(((DefaultQuery) query).isTraced());
    SelectResults results = (SelectResults) query.execute();
    assertTrue(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 12 with PartitionedIndex

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

the class PRIndexStatisticsJUnitTest method testStatsForRangeIndex.

/*
   * public static Test suite() { TestSuite suite = new TestSuite(IndexMaintenanceTest.class);
   * return suite; }
   */
/**
   * Test RenageIndex IndexStatistics for keys, values, updates and uses.
   * 
   * @throws Exception
   */
@Test
public void testStatsForRangeIndex() throws Exception {
    createAndPopulateRegion();
    keyIndex1 = (IndexProtocol) qs.createIndex("multiKeyIndex1", IndexType.FUNCTIONAL, "pos.secId", "/portfolio p, p.positions.values pos");
    assertTrue(keyIndex1 instanceof PartitionedIndex);
    IndexStatistics keyIndex1Stats = keyIndex1.getStatistics();
    assertEquals(89, keyIndex1Stats.getNumberOfBucketIndexes());
    // Initial stats test (keys, values & updates)
    assertEquals(2 * 100, /* Num of values in region */
    keyIndex1Stats.getNumberOfKeys());
    assertEquals(200, keyIndex1Stats.getNumberOfValues());
    assertEquals(200, keyIndex1Stats.getNumUpdates());
    for (int i = 0; i < 100; i++) {
        region.put(Integer.toString(i), new Portfolio(i, i));
    }
    assertEquals(2 * 100, /* Num of values in region */
    keyIndex1Stats.getNumberOfKeys());
    assertEquals(200, keyIndex1Stats.getNumberOfValues());
    assertEquals(400, keyIndex1Stats.getNumUpdates());
    // IndexUsed stats test
    String queryStr = "select * from /portfolio p, p.positions.values pos where pos.secId = 'YHOO'";
    Query query = qs.newQuery(queryStr);
    for (int i = 0; i < 50; i++) {
        query.execute();
    }
    assertEquals(50, keyIndex1Stats.getTotalUses());
    // NumOfValues should be reduced.
    for (int i = 0; i < 50; i++) {
        region.invalidate(Integer.toString(i));
    }
    assertEquals(100, /* Num of values in region */
    keyIndex1Stats.getNumberOfKeys());
    assertEquals(100, keyIndex1Stats.getNumberOfValues());
    assertEquals(450, keyIndex1Stats.getNumUpdates());
    // Should not have any effect as invalidated values are destroyed
    for (int i = 0; i < 50; i++) {
        region.destroy(Integer.toString(i));
    }
    assertEquals(100, /* Num of values in region */
    keyIndex1Stats.getNumberOfKeys());
    assertEquals(100, keyIndex1Stats.getNumberOfValues());
    assertEquals(450, keyIndex1Stats.getNumUpdates());
    // NumOfKeys should get zero as all values are destroyed
    for (int i = 50; i < 100; i++) {
        region.destroy(Integer.toString(i));
    }
    assertEquals(500, keyIndex1Stats.getNumUpdates());
    assertEquals(0, keyIndex1Stats.getNumberOfKeys());
    qs.removeIndex(keyIndex1);
    region.destroyRegion();
}
Also used : IndexStatistics(org.apache.geode.cache.query.IndexStatistics) PartitionedIndex(org.apache.geode.cache.query.internal.index.PartitionedIndex) 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 13 with PartitionedIndex

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

the class PRIndexStatisticsJUnitTest method testStatsForRangeIndexBeforeRegionCreation.

/**
   * Test RenageIndex IndexStatistics for keys, values, updates and uses.
   * 
   * @throws Exception
   */
@Test
public void testStatsForRangeIndexBeforeRegionCreation() throws Exception {
    // Destroy region
    createRegion();
    assertEquals(0, region.size());
    keyIndex1 = (IndexProtocol) qs.createIndex("multiKeyIndex4", IndexType.FUNCTIONAL, "pos.secId", "/portfolio p, p.positions.values pos");
    // Recreate all entries in the region
    for (int i = 0; i < 100; i++) {
        region.put(Integer.toString(i), new Portfolio(i, i));
    }
    assertTrue(keyIndex1 instanceof PartitionedIndex);
    IndexStatistics keyIndex1Stats = keyIndex1.getStatistics();
    assertEquals(89, keyIndex1Stats.getNumberOfBucketIndexes());
    // Initial stats test (keys, values & updates)
    assertEquals(2 * 100, /* Num of values in region */
    keyIndex1Stats.getNumberOfKeys());
    assertEquals(200, keyIndex1Stats.getNumberOfValues());
    assertEquals(200, keyIndex1Stats.getNumUpdates());
    for (int i = 0; i < 100; i++) {
        region.put(Integer.toString(i), new Portfolio(i, i));
    }
    assertEquals(2 * 100, /* Num of values in region */
    keyIndex1Stats.getNumberOfKeys());
    assertEquals(200, keyIndex1Stats.getNumberOfValues());
    assertEquals(400, keyIndex1Stats.getNumUpdates());
    // IndexUsed stats test
    String queryStr = "select * from /portfolio p, p.positions.values pos where pos.secId = 'YHOO'";
    Query query = qs.newQuery(queryStr);
    for (int i = 0; i < 50; i++) {
        query.execute();
    }
    assertEquals(50, keyIndex1Stats.getTotalUses());
    // NumOfValues should be reduced.
    for (int i = 0; i < 50; i++) {
        region.invalidate(Integer.toString(i));
    }
    assertEquals(100, /* Num of values in region */
    keyIndex1Stats.getNumberOfKeys());
    assertEquals(100, keyIndex1Stats.getNumberOfValues());
    assertEquals(450, keyIndex1Stats.getNumUpdates());
    // Should not have any effect as invalidated values are destroyed
    for (int i = 0; i < 50; i++) {
        region.destroy(Integer.toString(i));
    }
    assertEquals(100, /* Num of values in region */
    keyIndex1Stats.getNumberOfKeys());
    assertEquals(100, keyIndex1Stats.getNumberOfValues());
    assertEquals(450, keyIndex1Stats.getNumUpdates());
    // NumOfKeys should get zero as all values are destroyed
    for (int i = 50; i < 100; i++) {
        region.destroy(Integer.toString(i));
    }
    assertEquals(500, keyIndex1Stats.getNumUpdates());
    assertEquals(0, keyIndex1Stats.getNumberOfKeys());
    qs.removeIndex(keyIndex1);
    region.destroyRegion();
}
Also used : IndexStatistics(org.apache.geode.cache.query.IndexStatistics) PartitionedIndex(org.apache.geode.cache.query.internal.index.PartitionedIndex) 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 14 with PartitionedIndex

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

the class PartitionedRegion method removeIndex.

/**
   * Removes a particular index on this partitioned regions instance.
   * 
   * @param ind Index to be removed.
   * 
   */
public int removeIndex(Index ind, boolean remotelyOriginated) throws CacheException, ForceReattemptException {
    int numBuckets = 0;
    IndexTask indexTask = null;
    Object prIndex = null;
    if (ind != null) {
        indexTask = new IndexTask(ind.getName());
        prIndex = this.indexes.get(indexTask);
    }
    // not in create phase, its created successfully).
    if (prIndex == null || !(prIndex instanceof Index)) {
        logger.info(LocalizedMessage.create(LocalizedStrings.PartitionedRegion_THIS_INDEX__0_IS_NOT_ON_THIS_PARTITONED_REGION___1, new Object[] { ind, this }));
        return numBuckets;
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Remove index called, IndexName: {} Index: {}  Will be removing all the bucket indexes.", ind.getName(), ind);
    }
    Index index1 = this.indexManager.getIndex(ind.getName());
    if (index1 != null) {
        this.indexManager.removeIndex(index1);
    }
    // marking the index invalid.
    if (prIndex != null) {
        PartitionedIndex index = (PartitionedIndex) prIndex;
        index.acquireIndexWriteLockForRemove();
    }
    this.indexes.remove(indexTask);
    // For releasing the write lock after removal.
    try {
        synchronized (prIndex) {
            List allBucketIndex = ((PartitionedIndex) prIndex).getBucketIndexes();
            Iterator it = allBucketIndex.iterator();
            if (logger.isDebugEnabled()) {
                logger.debug("Will be removing indexes on : {} buckets", allBucketIndex.size());
            }
            while (it.hasNext()) {
                Index in = (Index) it.next();
                LocalRegion region = ((LocalRegion) in.getRegion());
                region.waitForData();
                IndexManager indMng = region.getIndexManager();
                indMng.removeIndex(in);
                if (logger.isDebugEnabled()) {
                    logger.debug("Removed index : {} on bucket {}", in, region);
                }
                numBuckets++;
                ((PartitionedIndex) prIndex).removeFromBucketIndexes(region, in);
            }
        // while
        }
    } finally {
        ((PartitionedIndex) prIndex).releaseIndexWriteLockForRemove();
    }
    if (!remotelyOriginated) {
        // send remove index message.
        RemoveIndexesMessage.RemoveIndexesResponse response;
        logger.info(LocalizedMessage.create(LocalizedStrings.PartitionedRegion_SENDING_REMOVEINDEX_MESSAGE_TO_ALL_THE_PARTICIPATING_PRS));
        response = (RemoveIndexesMessage.RemoveIndexesResponse) RemoveIndexesMessage.send(this, ind, false);
        if (response != null) {
            response.waitForResults();
            logger.info(LocalizedMessage.create(LocalizedStrings.PartitionedRegion_DONE_WATING_FOR_REMOVE_INDEX));
            if (logger.isDebugEnabled()) {
                logger.debug("Total number of buckets which removed indexs , locally : {} and remotely removed : {} and the total number of remote buckets : {}", numBuckets, response.getRemoteRemovedIndexes(), response.getTotalRemoteBuckets());
            }
        }
    }
    return numBuckets;
}
Also used : IndexManager(org.apache.geode.cache.query.internal.index.IndexManager) PartitionedIndex(org.apache.geode.cache.query.internal.index.PartitionedIndex) PREntriesIterator(org.apache.geode.internal.cache.partitioned.PREntriesIterator) Iterator(java.util.Iterator) RemoveIndexesMessage(org.apache.geode.internal.cache.partitioned.RemoveIndexesMessage) 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) VersionedObjectList(org.apache.geode.internal.cache.tier.sockets.VersionedObjectList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) LinkedList(java.util.LinkedList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 15 with PartitionedIndex

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

the class PartitionedRegion method sendIndexCreationMsg.

/**
   * Explicitly sends an index creation message to a newly added node to the system on prs.
   * 
   * @param idM id on the newly added node.
   */
public void sendIndexCreationMsg(InternalDistributedMember idM) {
    if (!this.isIndexed()) {
        return;
    }
    RegionAdvisor advisor = (RegionAdvisor) getCacheDistributionAdvisor();
    final Set<InternalDistributedMember> recipients = advisor.adviseDataStore();
    if (!recipients.contains(idM)) {
        logger.info(LocalizedMessage.create(LocalizedStrings.PartitionedRegion_NEWLY_ADDED_MEMBER_TO_THE_PR_IS_AN_ACCESSOR_AND_WILL_NOT_RECEIVE_INDEX_INFORMATION_0, idM));
        return;
    }
    // this should add the member to a synchronized set and then sent this member
    // and index creation msg latter after its completed creating the partitioned region.
    IndexCreationMsg.IndexCreationResponse response;
    IndexCreationMsg.IndexCreationResult result;
    if (this.indexes.isEmpty()) {
        return;
    }
    Iterator it = this.indexes.values().iterator();
    HashSet<IndexCreationData> indexDefinitions = new HashSet<>();
    Set<PartitionedIndex> indexes = new HashSet<>();
    while (it.hasNext()) {
        Object ind = it.next();
        // the index is not in create phase, its created successfully).
        if (!(ind instanceof Index)) {
            continue;
        }
        PartitionedIndex prIndex = (PartitionedIndex) ind;
        indexes.add(prIndex);
        IndexCreationData icd = new IndexCreationData(prIndex.getName());
        icd.setIndexData(prIndex.getType(), prIndex.getFromClause(), prIndex.getIndexedExpression(), prIndex.getImports(), true);
        indexDefinitions.add(icd);
    }
    response = (IndexCreationMsg.IndexCreationResponse) IndexCreationMsg.send(idM, this, indexDefinitions);
    if (logger.isDebugEnabled()) {
        logger.debug("Sending explicitly index creation message to : {}", idM);
    }
    if (response != null) {
        try {
            result = response.waitForResult();
            Map<String, Integer> remoteIndexBucketsMap = result.getIndexBucketsMap();
            // set the number of remote buckets indexed for each pr index
            for (Index ind : indexes) {
                ((PartitionedIndex) ind).setRemoteBucketesIndexed(remoteIndexBucketsMap.get(ind.getName()));
            }
        } catch (ForceReattemptException e) {
            logger.info(LocalizedStrings.PartitionedRegion_FORCEREATTEMPT_EXCEPTION___0, e);
        }
    }
}
Also used : RegionAdvisor(org.apache.geode.internal.cache.partitioned.RegionAdvisor) 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) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) PREntriesIterator(org.apache.geode.internal.cache.partitioned.PREntriesIterator) Iterator(java.util.Iterator) IndexCreationMsg(org.apache.geode.internal.cache.partitioned.IndexCreationMsg) HashSet(java.util.HashSet)

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