use of org.apache.geode.cache.CacheEvent in project geode by apache.
the class MultiVMRegionTestCase method testEntryTtlDestroyEvent.
/**
* Tests that an entry in a distributed region that expires with a distributed destroy causes an
* event in other VM with isExpiration flag set.
*/
// GEODE-583: time sensitive, expiration, waitForCriterion, short
@Category(FlakyTest.class)
// timeouts
@Test
public void testEntryTtlDestroyEvent() throws Exception {
assumeTrue(getRegionAttributes().getPartitionAttributes() == null);
final String name = this.getUniqueName();
// ms
final int timeout = 22;
final Object key = "KEY";
final Object value = "VALUE";
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm1 = host.getVM(1);
class DestroyListener extends TestCacheListener {
boolean eventIsExpiration = false;
@Override
public void afterDestroyBeforeAddEvent(EntryEvent event) {
eventIsExpiration = event.getOperation().isExpiration();
}
@Override
public void afterDestroy2(EntryEvent event) {
if (event.isOriginRemote()) {
assertTrue(!event.getDistributedMember().equals(getSystem().getDistributedMember()));
} else {
assertEquals(getSystem().getDistributedMember(), event.getDistributedMember());
}
assertEquals(Operation.EXPIRE_DESTROY, event.getOperation());
assertEquals(value, event.getOldValue());
eventIsExpiration = event.getOperation().isExpiration();
}
@Override
public void afterCreate2(EntryEvent event) {
// ignore
}
@Override
public void afterUpdate2(EntryEvent event) {
// ignore
}
}
SerializableRunnable createRegion = new CacheSerializableRunnable("Create with Listener") {
@Override
public void run2() throws CacheException {
AttributesFactory fac = new AttributesFactory(getRegionAttributes());
fac.addCacheListener(destroyListener = new DestroyListener());
createRegion(name, fac.create());
}
};
vm1.invoke(createRegion);
vm0.invoke(new CacheSerializableRunnable("Create with TTL") {
@Override
public void run2() throws CacheException {
AttributesFactory factory = new AttributesFactory(getRegionAttributes());
factory.setStatisticsEnabled(true);
ExpirationAttributes expire = new ExpirationAttributes(timeout, ExpirationAction.DESTROY);
factory.setEntryTimeToLive(expire);
if (!getRegionAttributes().getDataPolicy().withReplication()) {
factory.setDataPolicy(DataPolicy.NORMAL);
factory.setSubscriptionAttributes(new SubscriptionAttributes(InterestPolicy.ALL));
}
System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
try {
createRegion(name, factory.create());
ExpiryTask.suspendExpiration();
// suspend to make sure we can see that the put is distributed to this member
} finally {
System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
}
}
});
try {
// let region create finish before doing put
// pause(10);
vm1.invoke(new SerializableCallable() {
@Override
public Object call() throws Exception {
Region region = getRootRegion().getSubregion(name);
DestroyListener dl = (DestroyListener) region.getAttributes().getCacheListeners()[0];
dl.enableEventHistory();
region.put(key, value);
// reset listener after create event
assertTrue(dl.wasInvoked());
List<CacheEvent> history = dl.getEventHistory();
CacheEvent ce = history.get(0);
dl.disableEventHistory();
assertEquals(Operation.CREATE, ce.getOperation());
return null;
}
});
vm0.invoke(new CacheSerializableRunnable("Check create received from vm1") {
@Override
public void run2() throws CacheException {
final Region region = getRootRegion().getSubregion(name);
WaitCriterion waitForCreate = new WaitCriterion() {
@Override
public boolean done() {
return region.getEntry(key) != null;
}
@Override
public String description() {
return "never saw create of " + key;
}
};
Wait.waitForCriterion(waitForCreate, 3000, 10, true);
}
});
} finally {
vm0.invoke(new CacheSerializableRunnable("resume expiration") {
@Override
public void run2() throws CacheException {
ExpiryTask.permitExpiration();
}
});
}
// now wait for it to expire
vm0.invoke(new CacheSerializableRunnable("Check local destroy") {
@Override
public void run2() throws CacheException {
final Region region = getRootRegion().getSubregion(name);
WaitCriterion waitForExpire = new WaitCriterion() {
@Override
public boolean done() {
return region.getEntry(key) == null;
}
@Override
public String description() {
return "never saw expire of " + key + " entry=" + region.getEntry(key);
}
};
Wait.waitForCriterion(waitForExpire, 4000, 10, true);
}
});
vm1.invoke(new CacheSerializableRunnable("Verify destroyed and event") {
@Override
public void run2() throws CacheException {
final Region region = getRootRegion().getSubregion(name);
WaitCriterion waitForExpire = new WaitCriterion() {
@Override
public boolean done() {
return region.getEntry(key) == null;
}
@Override
public String description() {
return "never saw expire of " + key + " entry=" + region.getEntry(key);
}
};
Wait.waitForCriterion(waitForExpire, 4000, 10, true);
assertTrue(destroyListener.waitForInvocation(555));
assertTrue(((DestroyListener) destroyListener).eventIsExpiration);
}
});
}
use of org.apache.geode.cache.CacheEvent in project geode by apache.
the class QueueMsgDUnitTest method testQueueWhenRoleMissing.
/**
* Make sure that cache operations are queued when a required role is missing
*/
@Ignore("TODO: test is disabled")
@Test
public void testQueueWhenRoleMissing() throws Exception {
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_ACK);
DistributedRegion r = (DistributedRegion) createRootRegion(factory.create());
final CachePerfStats stats = r.getCachePerfStats();
int queuedOps = stats.getReliableQueuedOps();
r.create("createKey", "createValue", "createCBArg");
r.invalidate("createKey", "invalidateCBArg");
r.put("createKey", "putValue", "putCBArg");
r.destroy("createKey", "destroyCBArg");
assertEquals(queuedOps + 4, stats.getReliableQueuedOps());
queuedOps = stats.getReliableQueuedOps();
{
Map m = new TreeMap();
m.put("aKey", "aValue");
m.put("bKey", "bValue");
r.putAll(m);
}
assertEquals(queuedOps + 2, stats.getReliableQueuedOps());
queuedOps = stats.getReliableQueuedOps();
r.invalidateRegion("invalidateRegionCBArg");
assertEquals(queuedOps + 1, stats.getReliableQueuedOps());
queuedOps = stats.getReliableQueuedOps();
r.clear();
assertEquals(queuedOps + 1, stats.getReliableQueuedOps());
queuedOps = stats.getReliableQueuedOps();
// @todo darrel: try some other ops
VM vm = Host.getHost(0).getVM(0);
// now create a system that fills this role since it does not create the
// region our queue should not be flushed
vm.invoke(new SerializableRunnable() {
public void run() {
Properties config = new Properties();
config.setProperty(ROLES, "missing");
getSystem(config);
}
});
// we still should have everything queued since the region is not created
assertEquals(queuedOps, stats.getReliableQueuedOps());
// now create the region
vm.invoke(new CacheSerializableRunnable("create root") {
public void run2() throws CacheException {
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_ACK);
factory.setDataPolicy(DataPolicy.NORMAL);
factory.setSubscriptionAttributes(new SubscriptionAttributes(InterestPolicy.ALL));
TestCacheListener cl = new TestCacheListener() {
public void afterCreate2(EntryEvent event) {
}
public void afterUpdate2(EntryEvent event) {
}
public void afterInvalidate2(EntryEvent event) {
}
public void afterDestroy2(EntryEvent event) {
}
};
cl.enableEventHistory();
factory.addCacheListener(cl);
createRootRegion(factory.create());
}
});
// after some amount of time we should see the queuedOps flushed
WaitCriterion ev = new WaitCriterion() {
public boolean done() {
return stats.getReliableQueuedOps() == 0;
}
public String description() {
return "waiting for reliableQueuedOps to become 0";
}
};
Wait.waitForCriterion(ev, 5 * 1000, 200, true);
// now check that the queued op was delivered
vm.invoke(new CacheSerializableRunnable("check") {
public void run2() throws CacheException {
Region r = getRootRegion();
assertEquals(null, r.getEntry("createKey"));
// assertIndexDetailsEquals("putValue", r.getEntry("createKey").getValue());
{
int evIdx = 0;
TestCacheListener cl = (TestCacheListener) r.getAttributes().getCacheListener();
List events = cl.getEventHistory();
{
CacheEvent ce = (CacheEvent) events.get(evIdx++);
assertEquals(Operation.REGION_CREATE, ce.getOperation());
}
{
EntryEvent ee = (EntryEvent) events.get(evIdx++);
assertEquals(Operation.CREATE, ee.getOperation());
assertEquals("createKey", ee.getKey());
assertEquals("createValue", ee.getNewValue());
assertEquals(null, ee.getOldValue());
assertEquals("createCBArg", ee.getCallbackArgument());
assertEquals(true, ee.isOriginRemote());
}
{
EntryEvent ee = (EntryEvent) events.get(evIdx++);
assertEquals(Operation.INVALIDATE, ee.getOperation());
assertEquals("createKey", ee.getKey());
assertEquals(null, ee.getNewValue());
assertEquals("createValue", ee.getOldValue());
assertEquals("invalidateCBArg", ee.getCallbackArgument());
assertEquals(true, ee.isOriginRemote());
}
{
EntryEvent ee = (EntryEvent) events.get(evIdx++);
assertEquals(Operation.UPDATE, ee.getOperation());
assertEquals("createKey", ee.getKey());
assertEquals("putValue", ee.getNewValue());
assertEquals(null, ee.getOldValue());
assertEquals("putCBArg", ee.getCallbackArgument());
assertEquals(true, ee.isOriginRemote());
}
{
EntryEvent ee = (EntryEvent) events.get(evIdx++);
assertEquals(Operation.DESTROY, ee.getOperation());
assertEquals("createKey", ee.getKey());
assertEquals(null, ee.getNewValue());
assertEquals("putValue", ee.getOldValue());
assertEquals("destroyCBArg", ee.getCallbackArgument());
assertEquals(true, ee.isOriginRemote());
}
{
EntryEvent ee = (EntryEvent) events.get(evIdx++);
assertEquals(Operation.PUTALL_CREATE, ee.getOperation());
assertEquals("aKey", ee.getKey());
assertEquals("aValue", ee.getNewValue());
assertEquals(null, ee.getOldValue());
assertEquals(null, ee.getCallbackArgument());
assertEquals(true, ee.isOriginRemote());
}
{
EntryEvent ee = (EntryEvent) events.get(evIdx++);
assertEquals(Operation.PUTALL_CREATE, ee.getOperation());
assertEquals("bKey", ee.getKey());
assertEquals("bValue", ee.getNewValue());
assertEquals(null, ee.getOldValue());
assertEquals(null, ee.getCallbackArgument());
assertEquals(true, ee.isOriginRemote());
}
{
RegionEvent re = (RegionEvent) events.get(evIdx++);
assertEquals(Operation.REGION_INVALIDATE, re.getOperation());
assertEquals("invalidateRegionCBArg", re.getCallbackArgument());
assertEquals(true, re.isOriginRemote());
}
{
RegionEvent re = (RegionEvent) events.get(evIdx++);
assertEquals(Operation.REGION_CLEAR, re.getOperation());
assertEquals(null, re.getCallbackArgument());
assertEquals(true, re.isOriginRemote());
}
assertEquals(evIdx, events.size());
}
}
});
}
use of org.apache.geode.cache.CacheEvent in project geode by apache.
the class TXRmtEvent method getCreateEvents.
public List getCreateEvents() {
if (this.events == null) {
return Collections.EMPTY_LIST;
} else {
ArrayList result = new ArrayList(this.events.size());
Iterator it = this.events.iterator();
while (it.hasNext()) {
CacheEvent ce = (CacheEvent) it.next();
if (ce.getOperation().isCreate() && isEventUserVisible(ce)) {
result.add(ce);
}
}
if (result.isEmpty()) {
return Collections.EMPTY_LIST;
} else {
return Collections.unmodifiableList(result);
}
}
}
use of org.apache.geode.cache.CacheEvent in project geode by apache.
the class SearchLoadAndWriteProcessor method doLocalWrite.
private boolean doLocalWrite(CacheWriter writer, CacheEvent pevent, int paction) throws CacheWriterException {
// Return if the inhibit all notifications flag is set
if (pevent instanceof EntryEventImpl) {
if (((EntryEventImpl) pevent).inhibitAllNotifications()) {
if (logger.isDebugEnabled()) {
logger.debug("Notification inhibited for key {}", pevent);
}
return false;
}
}
@Released CacheEvent event = getEventForListener(pevent);
int action = paction;
if (event.getOperation().isCreate() && action == BEFOREUPDATE) {
action = BEFORECREATE;
}
try {
switch(action) {
case BEFORECREATE:
writer.beforeCreate((EntryEvent) event);
break;
case BEFOREDESTROY:
writer.beforeDestroy((EntryEvent) event);
break;
case BEFOREUPDATE:
writer.beforeUpdate((EntryEvent) event);
break;
case BEFOREREGIONDESTROY:
writer.beforeRegionDestroy((RegionEvent) event);
break;
case BEFOREREGIONCLEAR:
writer.beforeRegionClear((RegionEvent) event);
break;
default:
break;
}
} finally {
if (event != pevent) {
if (event instanceof EntryEventImpl) {
((Releasable) event).release();
}
}
}
this.localWrite = true;
return true;
}
Aggregations