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.
}
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.");
}
}
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;
}
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;
}
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;
}
Aggregations