Search in sources :

Example 6 with GatewaySenderEventImpl

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

the class SerialGatewaySenderEventProcessor method handleFailover.

/**
   * Handle failover. This method is called when a secondary <code>GatewaySender</code> becomes a
   * primary <code>GatewaySender</code>.
   * 
   * Once this secondary becomes the primary, it must:
   * <ul>
   * <li>Remove the queue's CacheListener
   * <li>Process the map of unprocessed events (those it has seen but the previous primary had not
   * yet processed before it crashed). These will include both queued and unqueued events. Remove
   * from the queue any events that were already sent
   * <li>Clear the unprocessed events map
   * </ul>
   */
protected void handleFailover() {
    /*
     * We must hold this lock while we're processing these maps to prevent us from handling a
     * secondary event while failover occurs. See enqueueEvent
     */
    synchronized (this.unprocessedEventsLock) {
        // Remove the queue's CacheListener
        this.queue.removeCacheListener();
        this.unprocessedTokens = null;
        // Process the map of unprocessed events
        logger.info(LocalizedMessage.create(LocalizedStrings.GatewayImpl_GATEWAY_FAILOVER_INITIATED_PROCESSING_0_UNPROCESSED_EVENTS, this.unprocessedEvents.size()));
        GatewaySenderStats statistics = this.sender.getStatistics();
        if (!this.unprocessedEvents.isEmpty()) {
            // do a reap for bug 37603
            // to get rid of timed out events
            reapOld(statistics, true);
            // now iterate over the region queue to figure out what unprocessed
            // events are already in the queue
            {
                Iterator it = this.queue.getRegion().values().iterator();
                while (it.hasNext() && !stopped()) {
                    Object o = it.next();
                    if (o != null && o instanceof GatewaySenderEventImpl) {
                        GatewaySenderEventImpl ge = (GatewaySenderEventImpl) o;
                        EventWrapper unprocessedEvent = this.unprocessedEvents.remove(ge.getEventId());
                        if (unprocessedEvent != null) {
                            unprocessedEvent.event.release();
                            if (this.unprocessedEvents.isEmpty()) {
                                break;
                            }
                        }
                    }
                }
            }
            // now for every unprocessed event add it to the end of the queue
            {
                Iterator<Map.Entry<EventID, EventWrapper>> it = this.unprocessedEvents.entrySet().iterator();
                while (it.hasNext()) {
                    if (stopped())
                        break;
                    Map.Entry<EventID, EventWrapper> me = it.next();
                    EventWrapper ew = me.getValue();
                    GatewaySenderEventImpl gatewayEvent = ew.event;
                    // Initialize each gateway event. This initializes the key,
                    // value
                    // and callback arg based on the EntryEvent.
                    // TODO:wan70, remove dependencies from old code
                    gatewayEvent.initialize();
                    // Verify that they GatewayEventCallbackArgument is initialized.
                    // If not, initialize it. It won't be initialized if a client to
                    // this GatewayHub VM was the creator of this event. This Gateway
                    // will be the first one to process it. If will be initialized if
                    // this event was sent to this Gateway from another GatewayHub
                    // (either directly or indirectly).
                    GatewaySenderEventCallbackArgument seca = gatewayEvent.getSenderCallbackArgument();
                    if (seca.getOriginatingDSId() == GatewaySender.DEFAULT_DISTRIBUTED_SYSTEM_ID) {
                        seca.setOriginatingDSId(sender.getMyDSId());
                        seca.initializeReceipientDSIds(Collections.singletonList(sender.getRemoteDSId()));
                    }
                    it.remove();
                    boolean queuedEvent = false;
                    try {
                        queuedEvent = queuePrimaryEvent(gatewayEvent);
                    } catch (IOException ex) {
                        if (!stopped()) {
                            logger.warn(LocalizedMessage.create(LocalizedStrings.GatewayImpl_EVENT_DROPPED_DURING_FAILOVER_0, gatewayEvent), ex);
                        }
                    } catch (CacheException ex) {
                        if (!stopped()) {
                            logger.warn(LocalizedMessage.create(LocalizedStrings.GatewayImpl_EVENT_DROPPED_DURING_FAILOVER_0, gatewayEvent), ex);
                        }
                    } finally {
                        if (!queuedEvent) {
                            gatewayEvent.release();
                        }
                    }
                }
            }
            // Clear the unprocessed events map
            statistics.clearUnprocessedMaps();
        }
        // Iterate the entire queue and mark all events as possible
        // duplicate
        logger.info(LocalizedMessage.create(LocalizedStrings.GatewayImpl_0__MARKING__1__EVENTS_AS_POSSIBLE_DUPLICATES, new Object[] { getSender(), Integer.valueOf(this.queue.size()) }));
        Iterator it = this.queue.getRegion().values().iterator();
        while (it.hasNext() && !stopped()) {
            Object o = it.next();
            if (o != null && o instanceof GatewaySenderEventImpl) {
                GatewaySenderEventImpl ge = (GatewaySenderEventImpl) o;
                ge.setPossibleDuplicate(true);
            }
        }
        releaseUnprocessedEvents();
    }
// synchronized
}
Also used : GatewaySenderStats(org.apache.geode.internal.cache.wan.GatewaySenderStats) CacheException(org.apache.geode.cache.CacheException) Iterator(java.util.Iterator) GatewaySenderEventImpl(org.apache.geode.internal.cache.wan.GatewaySenderEventImpl) EventID(org.apache.geode.internal.cache.EventID) GatewaySenderEventCallbackArgument(org.apache.geode.internal.cache.wan.GatewaySenderEventCallbackArgument) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) EventWrapper(org.apache.geode.internal.cache.wan.AbstractGatewaySender.EventWrapper)

Example 7 with GatewaySenderEventImpl

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

the class SerialGatewaySenderEventProcessor method releaseUnprocessedEvents.

private void releaseUnprocessedEvents() {
    synchronized (this.unprocessedEventsLock) {
        Map<EventID, EventWrapper> m = this.unprocessedEvents;
        if (m != null) {
            for (EventWrapper ew : m.values()) {
                GatewaySenderEventImpl gatewayEvent = ew.event;
                gatewayEvent.release();
            }
            this.unprocessedEvents = null;
        }
    }
}
Also used : GatewaySenderEventImpl(org.apache.geode.internal.cache.wan.GatewaySenderEventImpl) EventID(org.apache.geode.internal.cache.EventID) EventWrapper(org.apache.geode.internal.cache.wan.AbstractGatewaySender.EventWrapper)

Example 8 with GatewaySenderEventImpl

use of org.apache.geode.internal.cache.wan.GatewaySenderEventImpl 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 9 with GatewaySenderEventImpl

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

the class AbstractBucketRegionQueue method loadEventsFromTempQueue.

protected void loadEventsFromTempQueue() {
    if (logger.isDebugEnabled()) {
        logger.debug("For bucket {} about to load events from the temp queue...", getId());
    }
    Set queues = this.getPartitionedRegion().getParallelGatewaySender().getQueues();
    if (queues != null) {
        ConcurrentParallelGatewaySenderQueue prq = (ConcurrentParallelGatewaySenderQueue) queues.toArray()[0];
        // synchronized (prq.getBucketToTempQueueMap()) {
        BlockingQueue<GatewaySenderEventImpl> tempQueue = prq.getBucketTmpQueue(getId());
        // .getBucketToTempQueueMap().get(getId());
        if (tempQueue != null && !tempQueue.isEmpty()) {
            synchronized (tempQueue) {
                try {
                    // ParallelQueueRemovalMessage checks for the key in BucketRegionQueue
                    // and if not found there, it removes it from tempQueue. When tempQueue
                    // is getting loaded in BucketRegionQueue, it may not find the key in both.
                    // To fix this race, load the events in writeLock.
                    getInitializationLock().writeLock().lock();
                    // add the events from tempQueue to the region
                    GatewaySenderEventImpl event;
                    while ((event = tempQueue.poll()) != null) {
                        try {
                            event.setPossibleDuplicate(true);
                            if (this.addToQueue(event.getShadowKey(), event)) {
                                event = null;
                            }
                        } catch (ForceReattemptException e) {
                            if (logger.isDebugEnabled()) {
                                logger.debug("For bucket {} , enqueing event {} caused exception", getId(), event, e);
                            }
                        } finally {
                            if (event != null) {
                                event.release();
                            }
                        }
                    }
                } finally {
                    if (!tempQueue.isEmpty()) {
                        for (GatewaySenderEventImpl e : tempQueue) {
                            e.release();
                        }
                        tempQueue.clear();
                    }
                    getInitializationLock().writeLock().unlock();
                }
            }
        }
    // }
    }
}
Also used : ConcurrentHashSet(org.apache.geode.internal.concurrent.ConcurrentHashSet) ConcurrentParallelGatewaySenderQueue(org.apache.geode.internal.cache.wan.parallel.ConcurrentParallelGatewaySenderQueue) GatewaySenderEventImpl(org.apache.geode.internal.cache.wan.GatewaySenderEventImpl)

Example 10 with GatewaySenderEventImpl

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

the class BucketRegionQueueHelper method addEvent.

public GatewaySenderEventImpl addEvent(Object key) {
    this.bucketRegionQueue.getEventTracker().setInitialized();
    this.bucketRegionQueue.entries.disableLruUpdateCallback();
    GatewaySenderEventImpl event = mock(GatewaySenderEventImpl.class);
    this.bucketRegionQueue.entries.initialImagePut(key, 0, event, false, false, null, null, false);
    this.bucketRegionQueue.entries.enableLruUpdateCallback();
    return event;
}
Also used : GatewaySenderEventImpl(org.apache.geode.internal.cache.wan.GatewaySenderEventImpl)

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