use of org.apache.geode.cache.DataPolicy in project geode by apache.
the class DataCommandFunction method remove.
@SuppressWarnings({ "rawtypes" })
public DataCommandResult remove(String key, String keyClass, String regionName, String removeAllKeys) {
InternalCache cache = getCache();
if (StringUtils.isEmpty(regionName)) {
return DataCommandResult.createRemoveResult(key, null, null, CliStrings.REMOVE__MSG__REGIONNAME_EMPTY, false);
}
if (StringUtils.isEmpty(removeAllKeys) && (key == null)) {
return DataCommandResult.createRemoveResult(null, null, null, CliStrings.REMOVE__MSG__KEY_EMPTY, false);
}
Region region = cache.getRegion(regionName);
if (region == null) {
return DataCommandResult.createRemoveInfoResult(key, null, null, CliStrings.format(CliStrings.REMOVE__MSG__REGION_NOT_FOUND, regionName), false);
} else {
if (removeAllKeys == null) {
Object keyObject;
try {
keyObject = getClassObject(key, keyClass);
} catch (ClassNotFoundException e) {
return DataCommandResult.createRemoveResult(key, null, null, "ClassNotFoundException " + keyClass, false);
} catch (IllegalArgumentException e) {
return DataCommandResult.createRemoveResult(key, null, null, "Error in converting JSON " + e.getMessage(), false);
}
if (region.containsKey(keyObject)) {
Object value = region.remove(keyObject);
if (logger.isDebugEnabled()) {
logger.debug("Removed key {} successfully", key);
}
// return DataCommandResult.createRemoveResult(key, value, null, null);
Object[] array = getJSONForNonPrimitiveObject(value);
DataCommandResult result = DataCommandResult.createRemoveResult(key, array[1], null, null, true);
if (array[0] != null) {
result.setValueClass((String) array[0]);
}
return result;
} else {
return DataCommandResult.createRemoveInfoResult(key, null, null, CliStrings.REMOVE__MSG__KEY_NOT_FOUND_REGION, false);
}
} else {
DataPolicy policy = region.getAttributes().getDataPolicy();
if (!policy.withPartitioning()) {
region.clear();
if (logger.isDebugEnabled()) {
logger.debug("Cleared all keys in the region - {}", regionName);
}
return DataCommandResult.createRemoveInfoResult(key, null, null, CliStrings.format(CliStrings.REMOVE__MSG__CLEARED_ALL_CLEARS, regionName), true);
} else {
return DataCommandResult.createRemoveInfoResult(key, null, null, CliStrings.REMOVE__MSG__CLEAREALL_NOT_SUPPORTED_FOR_PARTITIONREGION, false);
}
}
}
}
use of org.apache.geode.cache.DataPolicy in project geode by apache.
the class DiskRegionTestImpl method testBackupFillValues.
/**
* Tests fillValues on backup regions.
*
* Note: The regions in the following description all have the same unique name. 1) Create backup
* region in VM0 and add some values so they get backed up 2) Close that region 3) Create
* non-mirrored distributed region in VM1 and populate with over 1M of data 4) Create a mirrored
* KEYS region in VM2. This will cause VM2 to have all the keys but no values. 5) Re-create the
* backup region in VM0 with mirroring KEY_VALUES. This will get the keys from VM2 and the values
* from VM1 using fillValues. The region should end up with the keys created in step 1, and they
* should not be faulted into the VM.
*/
public void testBackupFillValues() throws CacheException {
RegionAttributes attrs = this.rtc.getRegionAttributes();
assertTrue("This test not appropriate for non-backup regions", attrs.getPersistBackup());
final String name = this.rtc.getUniqueName();
final String key1 = "KEY1";
final String key2 = "KEY2";
final String value1 = "VALUE1";
final String value2 = "VALUE2";
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm1 = host.getVM(1);
// VM vm2 = host.getVM(2);
vm0.invoke(new CacheSerializableRunnable("Create backup Region in VM0") {
public void run2() throws CacheException {
Region rgn = DiskRegionTestImpl.this.rtc.createRegion(name);
rgn.create(key1, value1);
rgn.create(key2, value2);
// create entries that will be overwritten by getInitialImage below
rgn.create(new Integer(0), "TEMP-0");
rgn.create(new Integer(1), "TEMP-1");
// no longer to close cache in 6.5, otherwise the 2 vms will splitbrain
// CacheTestCase.closeCache();
}
});
vm1.invoke(new CacheSerializableRunnable("Create & Populate non-mirrored in VM1") {
public void run2() throws CacheException {
AttributesFactory factory = new AttributesFactory();
// set scope to be same as test region
Scope scope = DiskRegionTestImpl.this.rtc.getRegionAttributes().getScope();
factory.setScope(scope);
DataPolicy dataPolicy = DiskRegionTestImpl.this.rtc.getRegionAttributes().getDataPolicy();
factory.setDataPolicy(dataPolicy);
RegionAttributes attrs2 = factory.create();
Region rgn = DiskRegionTestImpl.this.rtc.createRegion(name, attrs2);
// Fill the region with some keys.
for (int i = 0; i < NUM_ENTRIES; i++) {
byte[] value = new byte[VALUE_SIZE];
Arrays.fill(value, (byte) 0xAB);
rgn.put(new Integer(i), value);
}
// just for sanity:
assertEquals(NUM_ENTRIES + 2, rgn.keySet().size());
}
});
vm0.invoke(new CacheSerializableRunnable("Close Cache in VM0") {
public void run2() throws CacheException {
CacheTestCase.closeCache();
}
});
// vm2.invoke(new CacheSerializableRunnable("Create mirrored KEYS region in VM2") {
// public void run2() throws CacheException {
// AttributesFactory factory = new AttributesFactory();
// // set scope to be same as test region
// Scope scope = DiskRegionTestImpl.this.rtc.getRegionAttributes().getScope();
// factory.setScope(scope);
// // set mirror KEYS
// factory.setMirrorType(MirrorType.KEYS);
// RegionAttributes attrs2 = factory.create();
// Region rgn = DiskRegionTestImpl.this.rtc.createRegion(name, attrs2);
// }
// });
String runnableName = "Re-create backup region in VM0 with mirror " + "KEYS_VALUES and Do Verification";
vm0.invoke(new CacheSerializableRunnable(runnableName) {
public void run2() throws CacheException {
AttributesFactory factory = new AttributesFactory(DiskRegionTestImpl.this.rtc.getRegionAttributes());
factory.setDataPolicy(DataPolicy.PERSISTENT_REPLICATE);
RegionAttributes attrs2 = factory.create();
// DebuggerSupport.waitForJavaDebugger(rtc.getLogWriter(), "About to create region...");
Region rgn = DiskRegionTestImpl.this.rtc.createRegion(name, attrs2);
// verify
assertEquals(NUM_ENTRIES + 2, rgn.keySet().size());
boolean RECOVER_VALUES = true;
if (RECOVER_VALUES) {
assertEquals(value1, rgn.getEntry(key1).getValue());
assertEquals(value2, rgn.getEntry(key2).getValue());
} else {
assertNull(valueInVM(rgn, key1));
assertNull(valueInVM(rgn, key2));
}
assertEquals(value1, valueOnDisk(rgn, key1));
assertEquals(value2, valueOnDisk(rgn, key2));
// The following also verifies TEMP values were overwritten
for (int i = 0; i < NUM_ENTRIES; i++) {
Region.Entry entry = rgn.getEntry(new Integer(i));
assertNotNull("No entry for key " + i, entry);
byte[] v = (byte[]) entry.getValue();
assertNotNull("Null value for key " + i, v);
assertEquals(VALUE_SIZE, v.length);
// test a byte
assertEquals((byte) 0xAB, v[i % VALUE_SIZE]);
}
rgn.close();
rgn = DiskRegionTestImpl.this.rtc.createRegion(name, attrs2);
// verify
assertEquals(NUM_ENTRIES + 2, rgn.keySet().size());
if (RECOVER_VALUES) {
assertEquals(value1, rgn.getEntry(key1).getValue());
assertEquals(value2, rgn.getEntry(key2).getValue());
} else {
assertNull(valueInVM(rgn, key1));
assertNull(valueInVM(rgn, key2));
}
assertEquals(value1, valueOnDisk(rgn, key1));
assertEquals(value2, valueOnDisk(rgn, key2));
// The following also verifies TEMP values were overwritten
for (int i = 0; i < NUM_ENTRIES; i++) {
Region.Entry entry = rgn.getEntry(new Integer(i));
assertNotNull("No entry for key " + i, entry);
byte[] v = (byte[]) entry.getValue();
assertNotNull("Null value for key " + i, v);
assertEquals(VALUE_SIZE, v.length);
// test a byte
assertEquals((byte) 0xAB, v[i % VALUE_SIZE]);
}
}
private Object valueInVM(Region rgn, Object key) throws EntryNotFoundException {
org.apache.geode.internal.cache.LocalRegion lrgn = (org.apache.geode.internal.cache.LocalRegion) rgn;
return lrgn.getValueInVM(key);
}
private Object valueOnDisk(Region rgn, Object key) throws EntryNotFoundException {
org.apache.geode.internal.cache.LocalRegion lrgn = (org.apache.geode.internal.cache.LocalRegion) rgn;
return lrgn.getValueOnDisk(key);
}
});
}
use of org.apache.geode.cache.DataPolicy 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.DataPolicy in project geode by apache.
the class MultiVMRegionTestCase method testTXNonblockingGetInitialImage.
/**
* Tests that distributed ack operations do not block while another cache is doing a
* getInitialImage.
*/
@Test
public void testTXNonblockingGetInitialImage() throws Exception {
assumeTrue(supportsReplication());
assumeTrue(supportsTransactions());
// don't run this test if global scope since its too difficult to predict
// how many concurrent operations will occur
assumeFalse(getRegionAttributes().getScope().isGlobal());
assumeFalse(getRegionAttributes().getDataPolicy().withPersistence());
final String name = this.getUniqueName();
final byte[][] values = new byte[NB1_NUM_ENTRIES][];
for (int i = 0; i < NB1_NUM_ENTRIES; i++) {
values[i] = new byte[NB1_VALUE_SIZE];
Arrays.fill(values[i], (byte) 0x42);
}
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm2 = host.getVM(2);
SerializableRunnable create = new CacheSerializableRunnable("Create Mirrored Region") {
@Override
public void run2() throws CacheException {
beginCacheXml();
{
// root region must be DACK because its used to sync up async subregions
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_ACK);
factory.setDataPolicy(DataPolicy.NORMAL);
factory.setSubscriptionAttributes(new SubscriptionAttributes(InterestPolicy.ALL));
createRootRegion(factory.create());
}
{
AttributesFactory factory = new AttributesFactory(getRegionAttributes());
if (getRegionAttributes().getDataPolicy() == DataPolicy.NORMAL) {
factory.setDataPolicy(DataPolicy.PRELOADED);
}
factory.setSubscriptionAttributes(new SubscriptionAttributes(InterestPolicy.ALL));
createRegion(name, factory.create());
}
finishCacheXml(name);
// reset slow
org.apache.geode.internal.cache.InitialImageOperation.slowImageProcessing = 0;
}
};
vm0.invoke(new CacheSerializableRunnable("Create Nonmirrored Region") {
@Override
public void run2() throws CacheException {
{
// root region must be DACK because its used to sync up async subregions
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_ACK);
factory.setDataPolicy(DataPolicy.EMPTY);
createRootRegion(factory.create());
}
{
AttributesFactory factory = new AttributesFactory(getRegionAttributes());
createRegion(name, factory.create());
}
// reset slow
org.apache.geode.internal.cache.InitialImageOperation.slowImageProcessing = 0;
}
});
vm0.invoke(new CacheSerializableRunnable("Put initial data") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
for (int i = 0; i < NB1_NUM_ENTRIES; i++) {
region.put(new Integer(i), values[i]);
}
assertEquals(NB1_NUM_ENTRIES, region.keySet().size());
}
});
// start asynchronous process that does updates to the data
AsyncInvocation async = vm0.invokeAsync(new CacheSerializableRunnable("Do Nonblocking Operations") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
// wait for profile of getInitialImage cache to show up
final org.apache.geode.internal.cache.CacheDistributionAdvisor adv = ((org.apache.geode.internal.cache.DistributedRegion) region).getCacheDistributionAdvisor();
final int expectedProfiles = 1;
WaitCriterion ev = new WaitCriterion() {
@Override
public boolean done() {
DataPolicy currentPolicy = getRegionAttributes().getDataPolicy();
if (currentPolicy == DataPolicy.PRELOADED) {
return (adv.advisePreloadeds().size() + adv.adviseReplicates().size()) >= expectedProfiles;
} else {
return adv.adviseReplicates().size() >= expectedProfiles;
}
}
@Override
public String description() {
return "replicate count never reached " + expectedProfiles;
}
};
Wait.waitForCriterion(ev, 100 * 1000, 200, true);
// operate on every odd entry with different value, alternating between
// updates, invalidates, and destroys. These operations are likely
// to be nonblocking if a sufficient number of updates get through
// before the get initial image is complete.
CacheTransactionManager txMgr = getCache().getCacheTransactionManager();
for (int i = 1; i < NB1_NUM_ENTRIES; i += 2) {
Object key = new Integer(i);
switch(i % 6) {
case // UPDATE
1:
// use the current timestamp so we know when it happened
// we could have used last modification timestamps, but
// this works without enabling statistics
Object value = new Long(System.currentTimeMillis());
txMgr.begin();
region.put(key, value);
txMgr.commit();
// }
break;
case // INVALIDATE
3:
txMgr.begin();
region.invalidate(key);
txMgr.commit();
if (getRegionAttributes().getScope().isDistributedAck()) {
// do a nonblocking netSearch
assertNull(region.get(key));
}
break;
case // DESTROY
5:
txMgr.begin();
region.destroy(key);
txMgr.commit();
if (getRegionAttributes().getScope().isDistributedAck()) {
// do a nonblocking netSearch
assertNull(region.get(key));
}
break;
default:
fail("unexpected modulus result: " + i);
break;
}
}
// add some new keys
for (int i = NB1_NUM_ENTRIES; i < NB1_NUM_ENTRIES + 200; i++) {
txMgr.begin();
region.create(new Integer(i), new Long(System.currentTimeMillis()));
txMgr.commit();
}
// now do a put and our DACK root region which will not complete
// until processed on otherside which means everything done before this
// point has been processed
getRootRegion().put("DONE", "FLUSH_OPS");
}
});
// slow down image processing to make it more likely to get async updates
if (!getRegionAttributes().getScope().isGlobal()) {
vm2.invoke(new SerializableRunnable("Set slow image processing") {
@Override
public void run() {
// if this is a no_ack test, then we need to slow down more because of the
// pauses in the nonblocking operations
int pause = 200;
org.apache.geode.internal.cache.InitialImageOperation.slowImageProcessing = pause;
}
});
}
AsyncInvocation asyncGII = vm2.invokeAsync(create);
if (!getRegionAttributes().getScope().isGlobal()) {
// wait for nonblocking operations to complete
ThreadUtils.join(async, 30 * 1000);
vm2.invoke(new SerializableRunnable("Set fast image processing") {
@Override
public void run() {
org.apache.geode.internal.cache.InitialImageOperation.slowImageProcessing = 0;
}
});
org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("after async nonblocking ops complete");
}
// wait for GII to complete
ThreadUtils.join(asyncGII, 30 * 1000);
final long iiComplete = System.currentTimeMillis();
org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("Complete GetInitialImage at: " + System.currentTimeMillis());
if (getRegionAttributes().getScope().isGlobal()) {
// wait for nonblocking operations to complete
ThreadUtils.join(async, 30 * 1000);
}
if (async.exceptionOccurred()) {
fail("async failed", async.getException());
}
if (asyncGII.exceptionOccurred()) {
fail("asyncGII failed", asyncGII.getException());
}
// Locally destroy the region in vm0 so we know that they are not found by
// a netSearch
vm0.invoke(new CacheSerializableRunnable("Locally destroy region") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
region.localDestroyRegion();
}
});
// invoke repeating so noack regions wait for all updates to get processed
vm2.invokeRepeatingIfNecessary(new CacheSerializableRunnable("Verify entryCount") {
boolean entriesDumped = false;
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
// expected entry count (subtract entries destroyed)
int entryCount = NB1_NUM_ENTRIES + 200 - NB1_NUM_ENTRIES / 6;
int actualCount = region.entrySet(false).size();
if (actualCount == NB1_NUM_ENTRIES + 200) {
// entries not destroyed, dump entries that were supposed to have been destroyed
dumpDestroyedEntries(region);
}
assertEquals(entryCount, actualCount);
}
private void dumpDestroyedEntries(Region region) throws EntryNotFoundException {
if (entriesDumped)
return;
entriesDumped = true;
LogWriter logger = org.apache.geode.test.dunit.LogWriterUtils.getLogWriter();
logger.info("DUMPING Entries with values in VM that should have been destroyed:");
for (int i = 5; i < NB1_NUM_ENTRIES; i += 6) {
logger.info(i + "-->" + ((org.apache.geode.internal.cache.LocalRegion) region).getValueInVM(new Integer(i)));
}
}
}, 5000);
vm2.invoke(new CacheSerializableRunnable("Verify keys/values & Nonblocking") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
// expected entry count (subtract entries destroyed)
int entryCount = NB1_NUM_ENTRIES + 200 - NB1_NUM_ENTRIES / 6;
assertEquals(entryCount, region.entrySet(false).size());
// determine how many entries were updated before getInitialImage
// was complete
int numConcurrent = 0;
for (int i = 0; i < NB1_NUM_ENTRIES + 200; i++) {
Region.Entry entry = region.getEntry(new Integer(i));
Object v = entry == null ? null : entry.getValue();
if (i < NB1_NUM_ENTRIES) {
// old keys
switch(i % 6) {
// even keys are originals
case 0:
case 2:
case 4:
assertNotNull(entry);
assertTrue(Arrays.equals(values[i], (byte[]) v));
break;
case // updated
1:
assertNotNull(v);
assertTrue("Value for key " + i + " is not a Long, is a " + v.getClass().getName(), v instanceof Long);
Long timestamp = (Long) entry.getValue();
if (timestamp.longValue() < iiComplete) {
numConcurrent++;
}
break;
case // invalidated
3:
assertNotNull(entry);
assertNull("Expected value for " + i + " to be null, but was " + v, v);
break;
case // destroyed
5:
assertNull(entry);
break;
default:
fail("unexpected modulus result: " + (i % 6));
break;
}
} else {
// new keys
assertNotNull(v);
assertTrue("Value for key " + i + " is not a Long, is a " + v.getClass().getName(), v instanceof Long);
Long timestamp = (Long) entry.getValue();
if (timestamp.longValue() < iiComplete) {
numConcurrent++;
}
}
}
org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info(name + ": " + numConcurrent + " entries out of " + entryCount + " were updated concurrently with getInitialImage");
// make sure at least some of them were concurrent
{
int min = 30;
assertTrue("Not enough updates concurrent with getInitialImage occurred to my liking. " + numConcurrent + " entries out of " + entryCount + " were updated concurrently with getInitialImage, and I'd expect at least " + min + " or so", numConcurrent >= min);
}
}
});
}
use of org.apache.geode.cache.DataPolicy in project geode by apache.
the class LuceneIndexImpl method withPersistence.
protected boolean withPersistence() {
RegionAttributes ra = dataRegion.getAttributes();
DataPolicy dp = ra.getDataPolicy();
final boolean withPersistence = dp.withPersistence();
return withPersistence;
}
Aggregations