Search in sources :

Example 6 with Conflatable

use of org.apache.geode.internal.cache.Conflatable in project geode by apache.

the class HARegionQueue method putGIIDataInRegion.

/**
   * Repopulates the HARegion after the GII is over so as to reset the counters and populate the
   * DACE objects for the thread identifiers . This method should be invoked as the last method in
   * the constructor . Thus while creating BlockingQueue this method should be invoked lastly in the
   * derived class constructor , after the HARegionQueue contructor is complete. Otherwise, the
   * ReentrantLock will be null.
   */
void putGIIDataInRegion() throws CacheException, InterruptedException {
    Set entrySet = this.region.entrySet(false);
    // be populated
    if (!entrySet.isEmpty()) {
        this.puttingGIIDataInQueue = true;
        final boolean isDebugEnabled = logger.isDebugEnabled();
        try {
            Region.Entry entry = null;
            Map orderedMap = new TreeMap();
            Iterator iterator = entrySet.iterator();
            Object key = null;
            while (iterator.hasNext()) {
                entry = (Region.Entry) iterator.next();
                key = entry.getKey();
                if (isDebugEnabled) {
                    logger.debug("processing queue key {} and value {}", key, entry.getValue());
                }
                if (key instanceof Long) {
                    if (!(entry.getValue() instanceof ClientMarkerMessageImpl)) {
                        orderedMap.put(key, entry.getValue());
                    }
                }
                this.region.localDestroy(key);
            }
            long max = 0;
            long counterInRegion = 0;
            entrySet = orderedMap.entrySet();
            if (!entrySet.isEmpty()) {
                Map.Entry mapEntry = null;
                iterator = entrySet.iterator();
                while (iterator.hasNext()) {
                    mapEntry = (Map.Entry) iterator.next();
                    Conflatable val = (Conflatable) mapEntry.getValue();
                    if (val != null && val.getEventId() != null) {
                        counterInRegion = ((Long) mapEntry.getKey()).intValue();
                        // TODO: remove this assertion
                        Assert.assertTrue(counterInRegion > max);
                        max = counterInRegion;
                        // putInQueue(val);
                        // logger.info(LocalizedStrings.DEBUG, this + " putting GII entry #" + counterInRegion
                        // + " into queue: " + val);
                        this.put(val);
                    } else if (isDebugEnabled) {
                        logger.debug("bug 44959 encountered: HARegion.putGIIDataInRegion found null eventId in {}", val);
                    }
                }
            }
            this.tailKey.set(max);
        } finally {
            this.puttingGIIDataInQueue = false;
            if (isDebugEnabled) {
                logger.debug("{} done putting GII data into queue", this);
            }
        }
    }
    // TODO:Asif: Avoid invocation of this method
    startHAServices(this.region.getCache());
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) TreeMap(java.util.TreeMap) Iterator(java.util.Iterator) AtomicLong(java.util.concurrent.atomic.AtomicLong) HARegion(org.apache.geode.internal.cache.HARegion) Region(org.apache.geode.cache.Region) ClientMarkerMessageImpl(org.apache.geode.internal.cache.tier.sockets.ClientMarkerMessageImpl) Conflatable(org.apache.geode.internal.cache.Conflatable) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap)

Example 7 with Conflatable

use of org.apache.geode.internal.cache.Conflatable in project geode by apache.

the class HARegionQueue method expireTheEventOrThreadIdentifier.

/**
   * This function is invoked by the createCacheListenerForHARegion for theHARegionQueue & also by
   * overridden function createCacheListenerForHARegion of the HARegionQueueJUnitTest class for the
   * test testConcurrentEventExpiryAndTake. This function provides the meaningful functionality for
   * expiry of the Event object as well as ThreadIdentifier
   *
   * @param event event object representing the data being expired
   */
void expireTheEventOrThreadIdentifier(EntryEvent event) throws CacheException {
    final boolean isDebugEnabled = logger.isDebugEnabled();
    if (isDebugEnabled) {
        logger.debug("HARegionQueue::afterInvalidate. Entry Event being invalidated:{}, isPrimaryQueue:{}", event, HARegionQueue.this.isPrimary());
    }
    Object key = event.getKey();
    if (key instanceof ThreadIdentifier) {
        // Check if the sequenceID present as value against this key is same
        // as
        // the last dispatched sequence & the size of set containing the
        // counters
        // is 0. If yes the Dace should be removed
        // Get DACE
        DispatchedAndCurrentEvents dace = (DispatchedAndCurrentEvents) HARegionQueue.this.eventsMap.get(key);
        Assert.assertTrue(dace != null);
        Long expirySequenceID = (Long) event.getOldValue();
        boolean expired = dace.expireOrUpdate(expirySequenceID, (ThreadIdentifier) key);
        if (isDebugEnabled) {
            logger.debug("HARegionQueue::afterInvalidate:Size of the region after expiring or updating the ThreadIdentifier={}", HARegionQueue.this.region.keys().size());
            logger.debug("HARegionQueue::afterInvalidate:ThreadIdentifier expired={}", expired);
        }
    } else if (key instanceof Long) {
        // if (destroyFromAvailableIDsAndRegion((Long)key)) {
        destroyFromQueue(key);
        Conflatable cf = (Conflatable) event.getOldValue();
        EventID id = cf.getEventId();
        byte[] memID = id.getMembershipID();
        long threadId = id.getThreadID();
        DispatchedAndCurrentEvents dace = (DispatchedAndCurrentEvents) eventsMap.get(new ThreadIdentifier(memID, threadId));
        if (shouldBeConflated(cf)) {
            dace.destroy((Long) key, cf.getKeyToConflate(), cf.getRegionToConflate());
        } else {
            dace.destroy((Long) key);
        }
    // }
    } else {
    // unexpected condition, throw exception?
    }
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) EventID(org.apache.geode.internal.cache.EventID) Conflatable(org.apache.geode.internal.cache.Conflatable)

Example 8 with Conflatable

use of org.apache.geode.internal.cache.Conflatable in project geode by apache.

the class SerialGatewaySenderQueue method removeOldEntry.

private boolean removeOldEntry(Conflatable object, Long tailKey) throws CacheException {
    final boolean isDebugEnabled = logger.isDebugEnabled();
    boolean keepOldEntry = true;
    // - the object can be conflated
    if (this.enableConflation && object.shouldBeConflated()) {
        if (isDebugEnabled) {
            logger.debug("{}: Conflating {} at queue index={} queue size={} head={} tail={}", this, object, tailKey, size(), this.headKey, tailKey);
        }
        // Determine whether this region / key combination is already indexed.
        // If so, it is already in the queue. Update the value in the queue and
        // set the shouldAddToQueue flag accordingly.
        String rName = object.getRegionToConflate();
        Object key = object.getKeyToConflate();
        Long previousIndex;
        synchronized (this) {
            Map<Object, Long> latestIndexesForRegion = this.indexes.get(rName);
            if (latestIndexesForRegion == null) {
                latestIndexesForRegion = new HashMap<Object, Long>();
                this.indexes.put(rName, latestIndexesForRegion);
            }
            previousIndex = latestIndexesForRegion.put(key, tailKey);
        }
        if (isDebugEnabled) {
            logger.debug("{}: Adding index key={}->index={} for {} head={} tail={}", this, key, tailKey, object, this.headKey, tailKey);
        }
        // peekedIds list prevents us from removing an entry that was not peeked.
        if (previousIndex != null) {
            if (isDebugEnabled) {
                logger.debug("{}: Indexes contains index={} for key={} head={} tail={} and it can be used.", this, previousIndex, key, this.headKey, tailKey);
            }
            keepOldEntry = false;
        } else {
            if (isDebugEnabled) {
                logger.debug("{}: No old entry for key={} head={} tail={} not removing old entry.", this, key, this.headKey, tailKey);
            }
            this.stats.incConflationIndexesMapSize();
            keepOldEntry = true;
        }
        // Replace the object's value into the queue if necessary
        if (!keepOldEntry) {
            Conflatable previous = (Conflatable) this.region.remove(previousIndex);
            this.stats.decQueueSize(1);
            if (isDebugEnabled) {
                logger.debug("{}: Previous conflatable at key={} head={} tail={}: {}", this, previousIndex, this.headKey, tailKey, previous);
                logger.debug("{}: Current conflatable at key={} head={} tail={}: {}", this, tailKey, this.headKey, tailKey, object);
                if (previous != null) {
                    logger.debug("{}: Removed {} and added {} for key={} head={} tail={} in queue for region={} old event={}", this, previous.getValueToConflate(), object.getValueToConflate(), key, this.headKey, tailKey, rName, previous);
                }
            }
        }
    } else {
        if (isDebugEnabled) {
            logger.debug("{}: Not conflating {} queue size: {} head={} tail={}", this, object, size(), this.headKey, tailKey);
        }
    }
    return keepOldEntry;
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Conflatable(org.apache.geode.internal.cache.Conflatable)

Example 9 with Conflatable

use of org.apache.geode.internal.cache.Conflatable in project geode by apache.

the class SerialGatewaySenderQueue method removeIndex.

// No need to synchronize because it is called from a synchronized method
private void removeIndex(Long qkey) {
    // Determine whether conflation is enabled for this queue and object
    if (this.enableConflation) {
        // only call get after checking enableConflation for bug 40508
        Object o = optimalGet(qkey);
        if (o instanceof Conflatable) {
            Conflatable object = (Conflatable) o;
            if (object.shouldBeConflated()) {
                // Otherwise, remove the index from the indexes map.
                String rName = object.getRegionToConflate();
                Object key = object.getKeyToConflate();
                Map<Object, Long> latestIndexesForRegion = this.indexes.get(rName);
                if (latestIndexesForRegion != null) {
                    // Remove the index.
                    Long index = latestIndexesForRegion.remove(key);
                    // into the map initially.
                    if (index != null) {
                        this.stats.decConflationIndexesMapSize();
                    }
                    if (logger.isDebugEnabled()) {
                        if (index != null) {
                            logger.debug("{}: Removed index {} for {}", this, index, object);
                        }
                    }
                }
            }
        }
    }
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Conflatable(org.apache.geode.internal.cache.Conflatable)

Example 10 with Conflatable

use of org.apache.geode.internal.cache.Conflatable in project geode by apache.

the class HARegionQueueJUnitTest method testBatchPeekWithRemove.

private void testBatchPeekWithRemove(boolean createBlockingQueue) throws InterruptedException, IOException, ClassNotFoundException {
    HARegionQueue regionQueue = createHARegionQueue(createBlockingQueue);
    for (int i = 0; i < 10; ++i) {
        EventID ev1 = new EventID(new byte[] { 1 }, 1, i);
        Conflatable cf1 = new ConflatableObject("key", "value", ev1, false, this.testName.getMethodName());
        regionQueue.put(cf1);
    }
    List objs = regionQueue.peek(10, 5000);
    assertThat(objs.size(), is(10));
    Iterator itr = objs.iterator();
    int j = 0;
    while (itr.hasNext()) {
        Conflatable conf = (Conflatable) itr.next();
        assertThat(conf, notNullValue());
        assertThat("The sequence ID of the objects in the queue are not as expected", conf.getEventId().getSequenceID(), is((long) j++));
    }
    regionQueue.remove();
    assertThat(regionQueue.size(), is(0));
}
Also used : Iterator(java.util.Iterator) EventID(org.apache.geode.internal.cache.EventID) List(java.util.List) Conflatable(org.apache.geode.internal.cache.Conflatable)

Aggregations

Conflatable (org.apache.geode.internal.cache.Conflatable)39 EventID (org.apache.geode.internal.cache.EventID)29 ClientSubscriptionTest (org.apache.geode.test.junit.categories.ClientSubscriptionTest)22 Test (org.junit.Test)22 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)21 AtomicLong (java.util.concurrent.atomic.AtomicLong)9 Map (java.util.Map)8 CacheException (org.apache.geode.cache.CacheException)8 HashMap (java.util.HashMap)7 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)7 ConcurrentMap (java.util.concurrent.ConcurrentMap)7 IOException (java.io.IOException)5 List (java.util.List)5 ArrayList (java.util.ArrayList)4 Iterator (java.util.Iterator)4 HashSet (java.util.HashSet)3 Set (java.util.Set)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 EntryEvent (org.apache.geode.cache.EntryEvent)3 CacheListenerAdapter (org.apache.geode.cache.util.CacheListenerAdapter)3