Search in sources :

Example 1 with AsyncEvent

use of org.apache.geode.cache.asyncqueue.AsyncEvent in project geode by apache.

the class SerialGatewaySenderQueue method peek.

public List<AsyncEvent> peek(int size, int timeToWait) throws CacheException {
    final boolean isTraceEnabled = logger.isTraceEnabled();
    long start = System.currentTimeMillis();
    long end = start + timeToWait;
    if (isTraceEnabled) {
        logger.trace("{}: Peek start time={} end time={} time to wait={}", this, start, end, timeToWait);
    }
    // why
    List<AsyncEvent> batch = new ArrayList<AsyncEvent>(size * 2);
    // *2?
    while (batch.size() < size) {
        AsyncEvent object = peekAhead();
        // Conflate here
        if (object != null) {
            batch.add(object);
        } else {
            // If time to wait is -1 (don't wait) or time interval has elapsed
            long currentTime = System.currentTimeMillis();
            if (isTraceEnabled) {
                logger.trace("{}: Peek current time: {}", this, currentTime);
            }
            if (timeToWait == -1 || (end <= currentTime)) {
                if (isTraceEnabled) {
                    logger.trace("{}: Peek breaking", this);
                }
                break;
            }
            if (isTraceEnabled) {
                logger.trace("{}: Peek continuing", this);
            }
            // Sleep a bit before trying again.
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                break;
            }
            continue;
        }
    }
    if (isTraceEnabled) {
        logger.trace("{}: Peeked a batch of {} entries", this, batch.size());
    }
    return batch;
// OFFHEAP: all returned AsyncEvent end up being removed from queue after the batch is sent
// so no need to worry about off-heap refCount.
}
Also used : ArrayList(java.util.ArrayList) AsyncEvent(org.apache.geode.cache.asyncqueue.AsyncEvent)

Example 2 with AsyncEvent

use of org.apache.geode.cache.asyncqueue.AsyncEvent in project geode by apache.

the class SerialGatewaySenderQueue method initializeRegion.

/**
   * Initializes the <code>Region</code> backing this queue. The <code>Region</code>'s scope is
   * DISTRIBUTED_NO_ACK and mirror type is KEYS_VALUES and is set to overflow to disk based on the
   * <code>GatewayQueueAttributes</code>.
   * 
   * @param sender The GatewaySender <code>SerialGatewaySenderImpl</code>
   * @param listener The GemFire <code>CacheListener</code>. The <code>CacheListener</code> can be
   *        null.
   */
@SuppressWarnings({ "unchecked", "rawtypes" })
private void initializeRegion(AbstractGatewaySender sender, CacheListener listener) {
    final InternalCache gemCache = sender.getCache();
    this.region = gemCache.getRegion(this.regionName);
    if (this.region == null) {
        AttributesFactory<Long, AsyncEvent> factory = new AttributesFactory<Long, AsyncEvent>();
        factory.setScope(NO_ACK ? Scope.DISTRIBUTED_NO_ACK : Scope.DISTRIBUTED_ACK);
        factory.setDataPolicy(this.enablePersistence ? DataPolicy.PERSISTENT_REPLICATE : DataPolicy.REPLICATE);
        if (logger.isDebugEnabled()) {
            logger.debug("The policy of region is {}", (this.enablePersistence ? DataPolicy.PERSISTENT_REPLICATE : DataPolicy.REPLICATE));
        }
        // when the user of this queue is a secondary VM.
        if (listener != null) {
            factory.addCacheListener(listener);
        }
        // allow for no overflow directory
        EvictionAttributes ea = EvictionAttributes.createLIFOMemoryAttributes(this.maximumQueueMemory, EvictionAction.OVERFLOW_TO_DISK);
        factory.setEvictionAttributes(ea);
        factory.setConcurrencyChecksEnabled(false);
        factory.setDiskStoreName(this.diskStoreName);
        // In case of persistence write to disk sync and in case of eviction write in async
        factory.setDiskSynchronous(this.isDiskSynchronous);
        // Create the region
        if (logger.isDebugEnabled()) {
            logger.debug("{}: Attempting to create queue region: {}", this, this.regionName);
        }
        final RegionAttributes<Long, AsyncEvent> ra = factory.create();
        try {
            SerialGatewaySenderQueueMetaRegion meta = new SerialGatewaySenderQueueMetaRegion(this.regionName, ra, null, gemCache, sender);
            try {
                this.region = gemCache.createVMRegion(this.regionName, ra, new InternalRegionArguments().setInternalMetaRegion(meta).setDestroyLockFlag(true).setSnapshotInputStream(null).setImageTarget(null).setIsUsedForSerialGatewaySenderQueue(true).setInternalRegion(true).setSerialGatewaySender(sender));
            } catch (IOException veryUnLikely) {
                logger.fatal(LocalizedMessage.create(LocalizedStrings.SingleWriteSingleReadRegionQueue_UNEXPECTED_EXCEPTION_DURING_INIT_OF_0, this.getClass()), veryUnLikely);
            } catch (ClassNotFoundException alsoUnlikely) {
                logger.fatal(LocalizedMessage.create(LocalizedStrings.SingleWriteSingleReadRegionQueue_UNEXPECTED_EXCEPTION_DURING_INIT_OF_0, this.getClass()), alsoUnlikely);
            }
            if (logger.isDebugEnabled()) {
                logger.debug("{}: Created queue region: {}", this, this.region);
            }
        } catch (CacheException e) {
            logger.fatal(LocalizedMessage.create(LocalizedStrings.SingleWriteSingleReadRegionQueue_0_THE_QUEUE_REGION_NAMED_1_COULD_NOT_BE_CREATED, new Object[] { this, this.regionName }), e);
        }
    } else {
        throw new IllegalStateException("Queue region " + this.region.getFullPath() + " already exists.");
    }
}
Also used : EvictionAttributes(org.apache.geode.cache.EvictionAttributes) CacheException(org.apache.geode.cache.CacheException) InternalCache(org.apache.geode.internal.cache.InternalCache) InternalRegionArguments(org.apache.geode.internal.cache.InternalRegionArguments) IOException(java.io.IOException) AsyncEvent(org.apache.geode.cache.asyncqueue.AsyncEvent) AttributesFactory(org.apache.geode.cache.AttributesFactory) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Example 3 with AsyncEvent

use of org.apache.geode.cache.asyncqueue.AsyncEvent in project geode by apache.

the class SerialGatewaySenderQueue method optimalGet.

/**
   * Does a get that gets the value without fault values in from disk.
   */
private AsyncEvent optimalGet(Long k) {
    // Get the object at that key (to remove the index).
    LocalRegion lr = (LocalRegion) this.region;
    Object o = null;
    try {
        o = lr.getValueInVMOrDiskWithoutFaultIn(k);
        if (o != null && o instanceof CachedDeserializable) {
            o = ((CachedDeserializable) o).getDeserializedValue(lr, lr.getRegionEntry(k));
        }
    } catch (EntryNotFoundException ok) {
    // just return null;
    }
    // bug #46023 do not return a destroyed entry marker
    if (o == Token.TOMBSTONE) {
        o = null;
    }
    return (AsyncEvent) o;
}
Also used : CachedDeserializable(org.apache.geode.internal.cache.CachedDeserializable) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) LocalRegion(org.apache.geode.internal.cache.LocalRegion) AsyncEvent(org.apache.geode.cache.asyncqueue.AsyncEvent)

Example 4 with AsyncEvent

use of org.apache.geode.cache.asyncqueue.AsyncEvent in project geode by apache.

the class SerialGatewaySenderQueue method getObjectInSerialSenderQueue.

private AsyncEvent getObjectInSerialSenderQueue(Long currentKey) {
    AsyncEvent object = optimalGet(currentKey);
    if ((null != object) && logger.isDebugEnabled()) {
        logger.debug("{}: Peeked {}->{}", this, currentKey, object);
    }
    if (object != null && object instanceof GatewaySenderEventImpl) {
        GatewaySenderEventImpl copy = ((GatewaySenderEventImpl) object).makeHeapCopyIfOffHeap();
        if (copy == null) {
            logger.debug("Unable to make heap copy and will not be added to peekedIds for object" + " : {} ", object.toString());
        }
        object = copy;
    }
    return object;
}
Also used : GatewaySenderEventImpl(org.apache.geode.internal.cache.wan.GatewaySenderEventImpl) AsyncEvent(org.apache.geode.cache.asyncqueue.AsyncEvent)

Example 5 with AsyncEvent

use of org.apache.geode.cache.asyncqueue.AsyncEvent in project geode by apache.

the class RebalanceOperationDUnitTest method createPRRegionWithAsyncQueue.

private DistributedMember createPRRegionWithAsyncQueue(VM vm0, final int localMaxMemory) {
    SerializableCallable createPrRegion = new SerializableCallable("createRegion") {

        public Object call() {
            Cache cache = getCache();
            // Create an async event listener that doesn't dispatch anything
            cache.createAsyncEventQueueFactory().setMaximumQueueMemory(1).setParallel(true).create("parallelQueue", new AsyncEventListener() {

                @Override
                public void close() {
                }

                @Override
                public boolean processEvents(List<AsyncEvent> events) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        return false;
                    }
                    return false;
                }
            });
            AttributesFactory attr = new AttributesFactory();
            attr.addAsyncEventQueueId("parallelQueue");
            PartitionAttributesFactory paf = new PartitionAttributesFactory();
            paf.setRedundantCopies(1);
            paf.setRecoveryDelay(-1);
            paf.setStartupRecoveryDelay(-1);
            paf.setLocalMaxMemory(localMaxMemory);
            PartitionAttributes prAttr = paf.create();
            attr.setPartitionAttributes(prAttr);
            cache.createRegion("region1", attr.create());
            return cache.getDistributedSystem().getDistributedMember();
        }
    };
    final DistributedMember member1 = (DistributedMember) vm0.invoke(createPrRegion);
    return member1;
}
Also used : PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) PartitionAttributes(org.apache.geode.cache.PartitionAttributes) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) DistributedMember(org.apache.geode.distributed.DistributedMember) AsyncEvent(org.apache.geode.cache.asyncqueue.AsyncEvent) Cache(org.apache.geode.cache.Cache) AsyncEventListener(org.apache.geode.cache.asyncqueue.AsyncEventListener)

Aggregations

AsyncEvent (org.apache.geode.cache.asyncqueue.AsyncEvent)16 AsyncEventListener (org.apache.geode.cache.asyncqueue.AsyncEventListener)4 IOException (java.io.IOException)3 AsyncEventQueue (org.apache.geode.cache.asyncqueue.AsyncEventQueue)3 RepositoryManager (org.apache.geode.cache.lucene.internal.repository.RepositoryManager)3 UnitTest (org.apache.geode.test.junit.categories.UnitTest)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 InternalGemFireError (org.apache.geode.InternalGemFireError)2 AttributesFactory (org.apache.geode.cache.AttributesFactory)2 Cache (org.apache.geode.cache.Cache)2 Region (org.apache.geode.cache.Region)2 IndexRepository (org.apache.geode.cache.lucene.internal.repository.IndexRepository)2 BucketNotFoundException (org.apache.geode.internal.cache.BucketNotFoundException)2 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)2 WaitCriterion (org.apache.geode.test.dunit.WaitCriterion)2