use of org.apache.geode.cache.CacheLoader in project geode by apache.
the class SearchLoadAndWriteProcessor method searchAndLoad.
/**
* If we have a local cache loader and the region is not global, then invoke the loader If the
* region is local, or the result is non-null, then return whatever the loader returned do a
* netSearch amongst selected peers if netSearch returns a blob, deserialize the blob and return
* that as the result netSearch failed, so all we can do at this point is do a load return result
* from load
*/
private void searchAndLoad(EntryEventImpl event, TXStateInterface txState, Object localValue) throws CacheLoaderException, TimeoutException {
RegionAttributes attrs = region.getAttributes();
Scope scope = attrs.getScope();
DataPolicy dataPolicy = attrs.getDataPolicy();
if (txState != null) {
TXEntryState tx = txState.txReadEntry(event.getKeyInfo(), region, false, true);
if (tx != null) {
if (tx.noValueInSystem()) {
// If the tx view has it invalid or destroyed everywhere
// then don't do a netsearch. We want to see the
// transactional view.
load(event);
return;
}
}
}
// if mirrored then we can optimize by skipping netsearch in some cases,
// and if also skip netSearch if we find an INVALID token since we
// know it was distributed. (Otherwise it would be a LOCAL_INVALID token)
{
if (localValue == Token.INVALID || dataPolicy.withReplication()) {
load(event);
return;
}
}
Object obj = null;
if (!scope.isGlobal()) {
// copy into local var to prevent race condition
CacheLoader loader = ((AbstractRegion) region).basicGetLoader();
if (loader != null) {
obj = doLocalLoad(loader, true);
Assert.assertTrue(obj != Token.INVALID && obj != Token.LOCAL_INVALID);
event.setNewValue(obj);
this.isSerialized = false;
this.result = obj;
return;
}
if (scope.isLocal()) {
return;
}
}
netSearchForBlob();
if (this.result != null) {
Assert.assertTrue(this.result != Token.INVALID && this.result != Token.LOCAL_INVALID);
if (this.isSerialized) {
event.setSerializedNewValue((byte[]) this.result);
} else {
event.setNewValue(this.result);
}
event.setVersionTag(this.versionTag);
return;
}
load(event);
}
use of org.apache.geode.cache.CacheLoader in project geode by apache.
the class AbstractRegion method setCacheLoader.
@Override
public synchronized CacheLoader setCacheLoader(CacheLoader cacheLoader) {
checkReadiness();
CacheLoader oldLoader = this.cacheLoader;
assignCacheLoader(cacheLoader);
cacheLoaderChanged(oldLoader);
return oldLoader;
}
use of org.apache.geode.cache.CacheLoader in project geode by apache.
the class DiskRegionDUnitTest method testLRUCCSizeOne.
/**
* Tests a disk-based region with an {@link LRUCapacityController} with size 1 and an eviction
* action of "overflow".
*/
@Test
public void testLRUCCSizeOne() throws CacheException {
int threshold = 1;
final String name = this.getUniqueName();
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.LOCAL);
factory.setEvictionAttributes(EvictionAttributes.createLRUEntryAttributes(threshold, EvictionAction.OVERFLOW_TO_DISK));
factory.setCacheLoader(new CacheLoader() {
public Object load(LoaderHelper helper) throws CacheLoaderException {
return "LOADED VALUE";
}
public void close() {
}
});
DiskStoreFactory dsf = getCache().createDiskStoreFactory();
factory.setDiskSynchronous(true);
File d = new File("DiskRegions" + OSProcess.getId());
d.mkdirs();
dsf.setDiskDirs(new File[] { d });
DiskStore ds = dsf.create(name);
factory.setDiskStoreName(ds.getName());
Region region = createRegion(name, factory.create());
LRUStatistics lruStats = getLRUStats(region);
assertNotNull(lruStats);
for (int i = 1; i <= 1; i++) {
Object key = new Integer(i);
Object value = String.valueOf(i);
region.put(key, value);
assertEquals(1, lruStats.getCounter());
assertEquals(0, lruStats.getEvictions());
}
for (int i = 2; i <= 10; i++) {
Object key = new Integer(i);
Object value = String.valueOf(i);
region.put(key, value);
assertEquals(1, lruStats.getCounter());
assertEquals(i - 1, lruStats.getEvictions());
}
for (int i = 11; i <= 20; i++) {
Object key = new Integer(i);
// Object value = String.valueOf(i);
// Invoke loader
region.get(key);
assertEquals(1, lruStats.getCounter());
assertEquals(i - 1, lruStats.getEvictions());
}
}
use of org.apache.geode.cache.CacheLoader in project geode by apache.
the class TXDistributedDUnitTest method testDACKLoadedMessage.
/**
* Test distributed ack transactions that consist only of data from loaded values
*/
@Test
public void testDACKLoadedMessage() throws Exception {
final CacheTransactionManager txMgr = this.getCache().getCacheTransactionManager();
final String rgnName = getUniqueName();
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_ACK);
factory.setEarlyAck(false);
factory.setCacheLoader(new CacheLoader() {
public Object load(LoaderHelper helper) {
return "val" + helper.getArgument();
}
public void close() {
}
});
Region rgn = getCache().createRegion(rgnName, factory.create());
Invoke.invokeInEveryVM(new SerializableRunnable("testDACKLoadedMessage: intial configuration") {
public void run() {
try {
AttributesFactory factory2 = new AttributesFactory();
factory2.setScope(Scope.DISTRIBUTED_ACK);
factory2.setEarlyAck(false);
// factory.setDataPolicy(DataPolicy.REPLICATE);
factory2.setMirrorType(MirrorType.KEYS);
getCache().createRegion(rgnName, factory2.create());
} catch (CacheException e) {
Assert.fail("While creating region", e);
}
}
});
// Confirm the standard case
txMgr.begin();
rgn.put("key1", "val1");
txMgr.commit();
assertEquals("val1", rgn.getEntry("key1").getValue());
Invoke.invokeInEveryVM(new SerializableRunnable("testDACKLoadedMessage: confirm standard case") {
public void run() {
Region rgn1 = getCache().getRegion(rgnName);
assertEquals("val1", rgn1.getEntry("key1").getValue());
}
});
// Confirm loaded value case
txMgr.begin();
rgn.get("key2", new Integer(2));
txMgr.commit();
assertEquals("val2", rgn.getEntry("key2").getValue());
Invoke.invokeInEveryVM(new SerializableRunnable("testDACKLoadedMessage: confirm standard case") {
public void run() {
Region rgn1 = getCache().getRegion(rgnName);
assertEquals("val2", rgn1.getEntry("key2").getValue());
}
});
// This should use the ack w/ the lockid
txMgr.begin();
rgn.put("key3", "val3");
rgn.get("key4", new Integer(4));
txMgr.commit();
Invoke.invokeInEveryVM(new SerializableRunnable("testDACKLoadedMessage: confirm standard case") {
public void run() {
Region rgn1 = getCache().getRegion(rgnName);
assertEquals("val3", rgn1.getEntry("key3").getValue());
assertEquals("val4", rgn1.getEntry("key4").getValue());
}
});
}
use of org.apache.geode.cache.CacheLoader in project geode by apache.
the class SearchAndLoadDUnitTest method testNetLoad.
@Test
public void testNetLoad() throws CacheException, InterruptedException {
disconnectAllFromDS();
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm1 = host.getVM(1);
final String name = this.getUniqueName() + "-ACK";
final String objectName = "B";
final Integer value = new Integer(43);
loaderInvoked = false;
remoteLoaderInvoked = false;
vm0.invoke(new SerializableRunnable("Create ACK Region") {
public void run() {
try {
loaderInvoked = false;
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_ACK);
factory.setEarlyAck(false);
// factory.setCacheLoader(new CacheLoader() {
// public Object load(LoaderHelper helper) {
/// loaderInvoked = true;
// return value;
// }
//
// public void close() {
//
// }
// });
Region region = createRegion(name, factory.create());
region.create(objectName, null);
} catch (CacheException ex) {
Assert.fail("While creating ACK region", ex);
}
}
});
vm1.invoke(new SerializableRunnable("Create ACK Region") {
public void run() {
try {
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_ACK);
factory.setEarlyAck(false);
factory.setCacheLoader(new CacheLoader() {
public Object load(LoaderHelper helper) {
remoteLoaderInvoked = true;
return value;
}
public void close() {
}
});
createRegion(name, factory.create());
} catch (CacheException ex) {
Assert.fail("While creating ACK region", ex);
}
}
});
vm0.invoke(new SerializableRunnable("Get a value from remote loader") {
public void run() {
for (int i = 0; i < 1; i++) {
try {
Object result = getRootRegion().getSubregion(name).get(objectName);
assertEquals(value, result);
assertEquals(new Boolean(loaderInvoked), Boolean.FALSE);
// getRootRegion().getSubregion(name).invalidate(objectName);
} catch (CacheLoaderException cle) {
Assert.fail("While getting value for ACK region", cle);
}/*
* catch(EntryNotFoundException enfe) { fail("While getting value for ACK region", enfe);
*
* }
*/
catch (TimeoutException te) {
Assert.fail("While getting value for ACK region", te);
}
}
}
});
}
Aggregations