Search in sources :

Example 21 with GatewaySenderEventImpl

use of org.apache.geode.internal.cache.wan.GatewaySenderEventImpl in project geode by apache.

the class ParallelGatewaySenderQueue method peek.

public List peek(int batchSize, int timeToWait) throws InterruptedException, CacheException {
    final boolean isDebugEnabled = logger.isDebugEnabled();
    PartitionedRegion prQ = getRandomShadowPR();
    List<GatewaySenderEventImpl> batch = new ArrayList<>();
    if (prQ == null || prQ.getLocalMaxMemory() == 0) {
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        blockProcessorThreadIfRequired();
        return batch;
    }
    long start = System.currentTimeMillis();
    long end = start + timeToWait;
    // Add peeked events
    addPeekedEvents(batch, batchSize);
    int bId = -1;
    while (batch.size() < batchSize) {
        if (areLocalBucketQueueRegionsPresent() && ((bId = getRandomPrimaryBucket(prQ)) != -1)) {
            GatewaySenderEventImpl object = (GatewaySenderEventImpl) peekAhead(prQ, bId);
            if (object != null) {
                GatewaySenderEventImpl copy = object.makeHeapCopyIfOffHeap();
                if (copy == null) {
                    if (stats != null) {
                        stats.incEventsNotQueuedConflated();
                    }
                    continue;
                }
                object = copy;
            }
            // Conflate here
            if (object != null) {
                if (isDebugEnabled) {
                    logger.debug("The gatewayEventImpl in peek is {}", object);
                }
                batch.add(object);
                peekedEvents.add(object);
            } else {
                // If time to wait is -1 (don't wait) or time interval has elapsed
                long currentTime = System.currentTimeMillis();
                if (isDebugEnabled) {
                    logger.debug("{}: Peeked object was null. Peek current time: {}", this, currentTime);
                }
                if (timeToWait == -1 || (end <= currentTime)) {
                    if (isDebugEnabled) {
                        logger.debug("{}: Peeked object was null.. Peek breaking", this);
                    }
                    break;
                }
                if (isDebugEnabled) {
                    logger.debug("{}: Peeked object was null. Peek continuing", this);
                }
                continue;
            }
        } else {
            // If time to wait is -1 (don't wait) or time interval has elapsed
            long currentTime = System.currentTimeMillis();
            if (isDebugEnabled) {
                logger.debug("{}: Peek current time: {}", this, currentTime);
            }
            if (timeToWait == -1 || (end <= currentTime)) {
                if (isDebugEnabled) {
                    logger.debug("{}: Peek breaking", this);
                }
                break;
            }
            if (isDebugEnabled) {
                logger.debug("{}: Peek continuing", this);
            }
            // Sleep a bit before trying again.
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                break;
            }
            continue;
        }
    }
    if (isDebugEnabled) {
        logger.debug("{}: Peeked a batch of {} entries. The size of the queue is {}. localSize is {}", this, batch.size(), size(), localSize());
    }
    if (batch.size() == 0) {
        blockProcessorThreadIfRequired();
    }
    return batch;
}
Also used : PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) ArrayList(java.util.ArrayList) GatewaySenderEventImpl(org.apache.geode.internal.cache.wan.GatewaySenderEventImpl)

Example 22 with GatewaySenderEventImpl

use of org.apache.geode.internal.cache.wan.GatewaySenderEventImpl in project geode by apache.

the class SerialGatewaySenderEventProcessor method enqueueEvent.

/**
   * Add the input object to the event queue
   */
@Override
public void enqueueEvent(EnumListenerEvent operation, EntryEvent event, Object substituteValue) throws IOException, CacheException {
    // There is a case where the event is serialized for processing. The
    // region is not
    // serialized along with the event since it is a transient field. I
    // created an
    // intermediate object (GatewayEventImpl) to avoid this since the region
    // name is
    // used in the sendBatch method, and it can't be null. See EntryEventImpl
    // for details.
    GatewaySenderEventImpl senderEvent = null;
    boolean isPrimary = sender.isPrimary();
    if (!isPrimary) {
        // over while we're processing an event as a secondaryEvent.
        synchronized (unprocessedEventsLock) {
            // Test whether this gateway is the primary.
            if (sender.isPrimary()) {
                isPrimary = true;
            } else {
                // If it is not, create an uninitialized GatewayEventImpl and
                // put it into the map of unprocessed events.
                // OFFHEAP
                senderEvent = new GatewaySenderEventImpl(operation, event, substituteValue, false);
                // ok
                handleSecondaryEvent(senderEvent);
            }
        }
    }
    if (isPrimary) {
        Region region = event.getRegion();
        boolean isPDXRegion = (region instanceof DistributedRegion && region.getName().equals(PeerTypeRegistration.REGION_NAME));
        if (!isPDXRegion) {
            waitForFailoverCompletion();
        }
        // If it is, create and enqueue an initialized GatewayEventImpl
        // OFFHEAP ok
        senderEvent = new GatewaySenderEventImpl(operation, event, substituteValue);
        boolean queuedEvent = false;
        try {
            queuedEvent = queuePrimaryEvent(senderEvent);
        } finally {
            // when the event is accessed through the region queue.
            if (!queuedEvent) {
                GatewaySenderEventImpl.release(senderEvent);
            }
        }
    }
}
Also used : GatewaySenderEventImpl(org.apache.geode.internal.cache.wan.GatewaySenderEventImpl) DistributedRegion(org.apache.geode.internal.cache.DistributedRegion) Region(org.apache.geode.cache.Region) DistributedRegion(org.apache.geode.internal.cache.DistributedRegion)

Aggregations

GatewaySenderEventImpl (org.apache.geode.internal.cache.wan.GatewaySenderEventImpl)22 Region (org.apache.geode.cache.Region)5 AbstractBucketRegionQueue (org.apache.geode.internal.cache.AbstractBucketRegionQueue)5 DistributedRegion (org.apache.geode.internal.cache.DistributedRegion)5 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)5 ArrayList (java.util.ArrayList)4 BucketRegionQueue (org.apache.geode.internal.cache.BucketRegionQueue)4 LocalRegion (org.apache.geode.internal.cache.LocalRegion)4 UnitTest (org.apache.geode.test.junit.categories.UnitTest)4 Test (org.junit.Test)4 LinkedList (java.util.LinkedList)3 List (java.util.List)3 Queue (java.util.Queue)3 EventID (org.apache.geode.internal.cache.EventID)3 GatewaySenderStats (org.apache.geode.internal.cache.wan.GatewaySenderStats)3 BucketRegion (org.apache.geode.internal.cache.BucketRegion)2 RegionQueue (org.apache.geode.internal.cache.RegionQueue)2 EventWrapper (org.apache.geode.internal.cache.wan.AbstractGatewaySender.EventWrapper)2 ConcurrentParallelGatewaySenderQueue (org.apache.geode.internal.cache.wan.parallel.ConcurrentParallelGatewaySenderQueue)2 StoredObject (org.apache.geode.internal.offheap.StoredObject)2