use of org.apache.geode.cache.util.CacheWriterAdapter in project geode by apache.
the class OplogJUnitTest method testPutClearPut.
// @todo this test is failing for some reason. Does it need to be fixed?
/**
* Bug test to reproduce the bug 37261. The scenario which this test depicts is not actually the
* cause of Bug 37261. This test validates the case where a synch persist only entry1 is created
* in Oplog1. A put operation on entry2 causes the switch , but before Oplog1 is rolled , the
* entry1 is modified so that it references Oplog2. Thus in effect roller will skip rolling entry1
* when rolling Oplog1.Now entry1 is deleted in Oplog2 and then a rolling happens. There should
* not be any error
*/
// @Test
// public void testBug37261_1()
// {
// CacheObserver old = CacheObserverHolder.getInstance();
// try {
// // Create a persist only region with rolling true
// diskProps.setPersistBackup(true);
// diskProps.setRolling(true);
// diskProps.setCompactionThreshold(100);
// diskProps.setMaxOplogSize(1024);
// diskProps.setSynchronous(true);
// this.proceed = false;
// region = DiskRegionHelperFactory.getSyncPersistOnlyRegion(cache,
// diskProps);
// // create an entry 1 in oplog1,
// region.put("key1", new byte[800]);
// // Asif the second put will cause a switch to oplog 2 & also cause the
// // oplog1
// // to be submitted to the roller
// LocalRegion.ISSUE_CALLBACKS_TO_CACHE_OBSERVER = true;
// CacheObserverHolder.setInstance(new CacheObserverAdapter() {
// public void beforeGoingToCompact()
// {
// // modify entry 1 so that it points to the new switched oplog
// Thread th = new Thread(new Runnable() {
// public void run()
// {
// region.put("key1", new byte[400]);
// }
// });
// th.start();
// try {
// DistributedTestCase.join(th, 30 * 1000, null);
// }
// catch (Exception e) {
// e.printStackTrace();
// failureCause = e.toString();
// failure = true;
// }
// }
// public void afterHavingCompacted()
// {
// synchronized (OplogJUnitTest.this) {
// rollerThread = Thread.currentThread();
// OplogJUnitTest.this.notify();
// OplogJUnitTest.this.proceed = true;
// }
// }
// });
// region.put("key2", new byte[300]);
// synchronized (this) {
// if (!this.proceed) {
// this.wait(15000);
// assertTrue(this.proceed);
// }
// }
// this.proceed = false;
// // Asif Delete the 1st entry
// region.destroy("key1");
// CacheObserverHolder.setInstance(new CacheObserverAdapter() {
// public void afterHavingCompacted()
// {
// synchronized (OplogJUnitTest.this) {
// OplogJUnitTest.this.notify();
// OplogJUnitTest.this.proceed = true;
// }
// }
// });
// // Coz another switch and wait till rolling done
// region.put("key2", new byte[900]);
// synchronized (this) {
// if (!this.proceed) {
// this.wait(15000);
// assertFalse(this.proceed);
// }
// }
// // Check if the roller is stil alive
// assertTrue(rollerThread.isAlive());
// }
// catch (Exception e) {
// e.printStackTrace();
// fail("Test failed du toe xception" + e);
// }
// finally {
// CacheObserverHolder.setInstance(old);
// LocalRegion.ISSUE_CALLBACKS_TO_CACHE_OBSERVER = false;
// }
// }
/**
* Tests the condition when a 'put' is in progress and concurrent 'clear' and 'put'(on the same
* key) occur. Thus if after Htree ref was set (in 'put'), the region got cleared (and same key
* re-'put'), the entry will get recorded in the new Oplog without a corresponding create (
* because the Oplogs containing create have already been deleted due to the clear operation).
* This put should not proceed. Also, Region creation after closing should not give an exception.
*/
@Test
public void testPutClearPut() {
try {
// Create a persist only region with rolling true
diskProps.setPersistBackup(true);
diskProps.setRolling(true);
diskProps.setMaxOplogSize(1024);
diskProps.setSynchronous(true);
this.proceed = false;
region = DiskRegionHelperFactory.getSyncPersistOnlyRegion(cache, diskProps, Scope.LOCAL);
final Thread clearOp = new Thread(new Runnable() {
public void run() {
try {
LocalRegion.ISSUE_CALLBACKS_TO_CACHE_OBSERVER = false;
region.clear();
region.put("key1", "value3");
} catch (Exception e) {
testFailed = true;
failureCause = "Encountered Exception=" + e;
}
}
});
region.getAttributesMutator().setCacheWriter(new CacheWriterAdapter() {
@Override
public void beforeUpdate(EntryEvent event) throws CacheWriterException {
clearOp.start();
}
});
try {
ThreadUtils.join(clearOp, 30 * 1000);
} catch (Exception e) {
testFailed = true;
failureCause = "Encountered Exception=" + e;
e.printStackTrace();
}
region.create("key1", "value1");
LocalRegion.ISSUE_CALLBACKS_TO_CACHE_OBSERVER = true;
region.put("key1", "value2");
if (!testFailed) {
region.close();
region = DiskRegionHelperFactory.getSyncPersistOnlyRegion(cache, diskProps, Scope.LOCAL);
} else {
fail(failureCause);
}
} catch (Exception e) {
e.printStackTrace();
fail("Test failed due to exception" + e);
} finally {
testFailed = false;
proceed = false;
LocalRegion.ISSUE_CALLBACKS_TO_CACHE_OBSERVER = false;
}
}
use of org.apache.geode.cache.util.CacheWriterAdapter in project geode by apache.
the class MapInterfaceJUnitTest method testSetValue.
@Test
public void testSetValue() {
Properties props = new Properties();
props.setProperty(MCAST_PORT, "0");
props.setProperty(LOCATORS, "");
DistributedSystem ds = DistributedSystem.connect(props);
Cache cache = null;
AttributesFactory factory = null;
try {
cache = CacheFactory.create(ds);
factory = new AttributesFactory();
factory.setScope(Scope.LOCAL);
factory.setCacheWriter(new CacheWriterAdapter() {
@Override
public void beforeUpdate(EntryEvent event) throws CacheWriterException {
synchronized (this) {
this.notify();
counter++;
MapInterfaceJUnitTest.this.hasBeenNotified = true;
}
}
});
region2 = cache.createRegion("testingRegion", factory.create());
region2.put(new Integer(2), new Integer(2));
this.hasBeenNotified = false;
DoesPut doesPut = new DoesPut();
new Thread(doesPut).start();
synchronized (this) {
if (!this.hasBeenNotified) {
this.wait(3000);
}
}
if (!this.hasBeenNotified) {
fail(" beforeCreate call back did not come");
}
assertEquals(counter, 1);
} catch (Exception e) {
throw new AssertionError(" failed due to ", e);
}
}
use of org.apache.geode.cache.util.CacheWriterAdapter in project geode by apache.
the class RemoteTransactionDUnitTest method testOriginRemoteIsTrueForRemoteReplicatedRegions.
@Test
public void testOriginRemoteIsTrueForRemoteReplicatedRegions() {
Host host = Host.getHost(0);
VM accessor = host.getVM(0);
VM datastore = host.getVM(1);
initAccessorAndDataStore(accessor, datastore, 0);
class OriginRemoteRRWriter extends CacheWriterAdapter {
int fireC = 0;
int fireD = 0;
int fireU = 0;
public void beforeCreate(EntryEvent event) throws CacheWriterException {
if (!event.isOriginRemote()) {
throw new CacheWriterException("SUP?? This CREATE is supposed to be isOriginRemote");
}
fireC++;
}
public void beforeDestroy(EntryEvent event) throws CacheWriterException {
getGemfireCache().getLoggerI18n().fine("SWAP:writer:createEvent:" + event);
if (!event.isOriginRemote()) {
throw new CacheWriterException("SUP?? This DESTROY is supposed to be isOriginRemote");
}
fireD++;
}
public void beforeUpdate(EntryEvent event) throws CacheWriterException {
if (!event.isOriginRemote()) {
throw new CacheWriterException("SUP?? This UPDATE is supposed to be isOriginRemote");
}
fireU++;
}
}
datastore.invoke(new SerializableCallable() {
public Object call() throws Exception {
Region refRegion = getCache().getRegion(D_REFERENCE);
refRegion.getAttributesMutator().setCacheWriter(new OriginRemoteRRWriter());
return null;
}
});
accessor.invoke(new DoOpsInTX(OP.PUT));
accessor.invoke(new SerializableCallable() {
public Object call() throws Exception {
TXManagerImpl mgr = getGemfireCache().getTxManager();
TXStateProxy tx = mgr.internalSuspend();
assertNotNull(tx);
mgr.internalResume(tx);
mgr.commit();
return null;
}
});
accessor.invoke(new DoOpsInTX(OP.DESTROY));
accessor.invoke(new SerializableCallable() {
public Object call() throws Exception {
TXManagerImpl mgr = getGemfireCache().getTxManager();
TXStateProxy tx = mgr.internalSuspend();
assertNotNull(tx);
mgr.internalResume(tx);
mgr.commit();
return null;
}
});
accessor.invoke(new DoOpsInTX(OP.PUT));
accessor.invoke(new SerializableCallable() {
public Object call() throws Exception {
TXManagerImpl mgr = getGemfireCache().getTxManager();
TXStateProxy tx = mgr.internalSuspend();
assertNotNull(tx);
mgr.internalResume(tx);
mgr.commit();
return null;
}
});
datastore.invoke(new SerializableCallable() {
public Object call() throws Exception {
Region refRegion = getCache().getRegion(D_REFERENCE);
OriginRemoteRRWriter w = (OriginRemoteRRWriter) refRegion.getAttributes().getCacheWriter();
assertEquals(1, w.fireC);
assertEquals(1, w.fireD);
assertEquals(1, w.fireU);
return null;
}
});
}
Aggregations