use of org.apache.geode.cache.InterestResultPolicy in project geode by apache.
the class ClientAuthorizationTestCase method doOp.
protected static void doOp(OperationCode op, final int[] indices, final int flagsI, final int expectedResult) throws InterruptedException {
boolean operationOmitted = false;
final int flags = flagsI;
Region region = getRegion();
if ((flags & OpFlags.USE_SUBREGION) > 0) {
assertNotNull(region);
Region subregion = null;
if ((flags & OpFlags.NO_CREATE_SUBREGION) > 0) {
if ((flags & OpFlags.CHECK_NOREGION) > 0) {
// Wait for some time for DRF update to come
waitForCondition(() -> getSubregion() == null);
subregion = getSubregion();
assertNull(subregion);
return;
} else {
// Wait for some time for DRF update to come
waitForCondition(() -> getSubregion() != null);
subregion = getSubregion();
assertNotNull(subregion);
}
} else {
subregion = createSubregion(region);
}
assertNotNull(subregion);
region = subregion;
} else if ((flags & OpFlags.CHECK_NOREGION) > 0) {
// Wait for some time for region destroy update to come
waitForCondition(() -> getRegion() == null);
region = getRegion();
assertNull(region);
return;
} else {
assertNotNull(region);
}
final String[] keys = KEYS;
final String[] vals;
if ((flags & OpFlags.USE_NEWVAL) > 0) {
vals = NVALUES;
} else {
vals = VALUES;
}
InterestResultPolicy policy = InterestResultPolicy.KEYS_VALUES;
if ((flags & OpFlags.REGISTER_POLICY_NONE) > 0) {
policy = InterestResultPolicy.NONE;
}
final int numOps = indices.length;
System.out.println("Got doOp for op: " + op.toString() + ", numOps: " + numOps + ", indices: " + indicesToString(indices) + ", expect: " + expectedResult);
boolean exceptionOccurred = false;
boolean breakLoop = false;
if (op.isGet() || op.isContainsKey() || op.isKeySet() || op.isQuery() || op.isExecuteCQ()) {
Thread.sleep(PAUSE);
}
for (int indexIndex = 0; indexIndex < numOps; ++indexIndex) {
if (breakLoop) {
break;
}
int index = indices[indexIndex];
try {
final Object key = keys[index];
final Object expectedVal = vals[index];
if (op.isGet()) {
Object value = null;
// this is the case for testing GET_ALL
if ((flags & OpFlags.USE_ALL_KEYS) > 0) {
breakLoop = true;
List keyList = new ArrayList(numOps);
Object searchKey;
for (int keyNumIndex = 0; keyNumIndex < numOps; ++keyNumIndex) {
int keyNum = indices[keyNumIndex];
searchKey = keys[keyNum];
keyList.add(searchKey);
// local invalidate some KEYS to force fetch of those KEYS from server
if ((flags & OpFlags.CHECK_NOKEY) > 0) {
AbstractRegionEntry entry = (AbstractRegionEntry) ((LocalRegion) region).getRegionEntry(searchKey);
System.out.println("" + keyNum + ": key is " + searchKey + " and entry is " + entry);
assertFalse(region.containsKey(searchKey));
} else {
if (keyNumIndex % 2 == 1) {
assertTrue(region.containsKey(searchKey));
region.localInvalidate(searchKey);
}
}
}
Map entries = region.getAll(keyList);
for (int keyNumIndex = 0; keyNumIndex < numOps; ++keyNumIndex) {
int keyNum = indices[keyNumIndex];
searchKey = keys[keyNum];
if ((flags & OpFlags.CHECK_FAIL) > 0) {
assertFalse(entries.containsKey(searchKey));
} else {
assertTrue(entries.containsKey(searchKey));
value = entries.get(searchKey);
assertEquals(vals[keyNum], value);
}
}
break;
}
if ((flags & OpFlags.LOCAL_OP) > 0) {
Callable<Boolean> condition = new Callable<Boolean>() {
private Region region;
@Override
public Boolean call() throws Exception {
Object value = getLocalValue(region, key);
return (flags & OpFlags.CHECK_FAIL) > 0 ? !expectedVal.equals(value) : expectedVal.equals(value);
}
public Callable<Boolean> init(Region region) {
this.region = region;
return this;
}
}.init(region);
waitForCondition(condition);
value = getLocalValue(region, key);
} else if ((flags & OpFlags.USE_GET_ENTRY_IN_TX) > 0) {
getCache().getCacheTransactionManager().begin();
Entry e = region.getEntry(key);
// Also, check getAll()
ArrayList a = new ArrayList();
a.addAll(a);
region.getAll(a);
getCache().getCacheTransactionManager().commit();
value = e.getValue();
} else {
if ((flags & OpFlags.CHECK_NOKEY) > 0) {
assertFalse(region.containsKey(key));
} else {
assertTrue(region.containsKey(key) || ((LocalRegion) region).getRegionEntry(key).isTombstone());
region.localInvalidate(key);
}
value = region.get(key);
}
if ((flags & OpFlags.CHECK_FAIL) > 0) {
assertFalse(expectedVal.equals(value));
} else {
assertNotNull(value);
assertEquals(expectedVal, value);
}
} else if (op.isPut()) {
region.put(key, expectedVal);
} else if (op.isPutAll()) {
HashMap map = new HashMap();
for (int i = 0; i < indices.length; i++) {
map.put(keys[indices[i]], vals[indices[i]]);
}
region.putAll(map);
breakLoop = true;
} else if (op.isDestroy()) {
// }
if ((flags & OpFlags.LOCAL_OP) > 0) {
region.localDestroy(key);
} else {
region.destroy(key);
}
} else if (op.isInvalidate()) {
if (region.containsKey(key)) {
if ((flags & OpFlags.LOCAL_OP) > 0) {
region.localInvalidate(key);
} else {
region.invalidate(key);
}
}
} else if (op.isContainsKey()) {
boolean result;
if ((flags & OpFlags.LOCAL_OP) > 0) {
result = region.containsKey(key);
} else {
result = region.containsKeyOnServer(key);
}
if ((flags & OpFlags.CHECK_FAIL) > 0) {
assertFalse(result);
} else {
assertTrue(result);
}
} else if (op.isRegisterInterest()) {
if ((flags & OpFlags.USE_LIST) > 0) {
breakLoop = true;
// Register interest list in this case
List keyList = new ArrayList(numOps);
for (int keyNumIndex = 0; keyNumIndex < numOps; ++keyNumIndex) {
int keyNum = indices[keyNumIndex];
keyList.add(keys[keyNum]);
}
region.registerInterest(keyList, policy);
} else if ((flags & OpFlags.USE_REGEX) > 0) {
breakLoop = true;
region.registerInterestRegex("key[1-" + numOps + ']', policy);
} else if ((flags & OpFlags.USE_ALL_KEYS) > 0) {
breakLoop = true;
region.registerInterest("ALL_KEYS", policy);
} else {
region.registerInterest(key, policy);
}
} else if (op.isUnregisterInterest()) {
if ((flags & OpFlags.USE_LIST) > 0) {
breakLoop = true;
// Register interest list in this case
List keyList = new ArrayList(numOps);
for (int keyNumIndex = 0; keyNumIndex < numOps; ++keyNumIndex) {
int keyNum = indices[keyNumIndex];
keyList.add(keys[keyNum]);
}
region.unregisterInterest(keyList);
} else if ((flags & OpFlags.USE_REGEX) > 0) {
breakLoop = true;
region.unregisterInterestRegex("key[1-" + numOps + ']');
} else if ((flags & OpFlags.USE_ALL_KEYS) > 0) {
breakLoop = true;
region.unregisterInterest("ALL_KEYS");
} else {
region.unregisterInterest(key);
}
} else if (op.isKeySet()) {
breakLoop = true;
Set keySet;
if ((flags & OpFlags.LOCAL_OP) > 0) {
keySet = region.keySet();
} else {
keySet = region.keySetOnServer();
}
assertNotNull(keySet);
if ((flags & OpFlags.CHECK_FAIL) == 0) {
assertEquals(numOps, keySet.size());
}
for (int keyNumIndex = 0; keyNumIndex < numOps; ++keyNumIndex) {
int keyNum = indices[keyNumIndex];
if ((flags & OpFlags.CHECK_FAIL) > 0) {
assertFalse(keySet.contains(keys[keyNum]));
} else {
assertTrue(keySet.contains(keys[keyNum]));
}
}
} else if (op.isQuery()) {
breakLoop = true;
SelectResults queryResults = region.query("SELECT DISTINCT * FROM " + region.getFullPath());
assertNotNull(queryResults);
Set queryResultSet = queryResults.asSet();
if ((flags & OpFlags.CHECK_FAIL) == 0) {
assertEquals(numOps, queryResultSet.size());
}
for (int keyNumIndex = 0; keyNumIndex < numOps; ++keyNumIndex) {
int keyNum = indices[keyNumIndex];
if ((flags & OpFlags.CHECK_FAIL) > 0) {
assertFalse(queryResultSet.contains(vals[keyNum]));
} else {
assertTrue(queryResultSet.contains(vals[keyNum]));
}
}
} else if (op.isExecuteCQ()) {
breakLoop = true;
QueryService queryService = getCache().getQueryService();
CqQuery cqQuery;
if ((cqQuery = queryService.getCq("cq1")) == null) {
CqAttributesFactory cqFact = new CqAttributesFactory();
cqFact.addCqListener(new AuthzCqListener());
CqAttributes cqAttrs = cqFact.create();
cqQuery = queryService.newCq("cq1", "SELECT * FROM " + region.getFullPath(), cqAttrs);
}
if ((flags & OpFlags.LOCAL_OP) > 0) {
// Interpret this as testing results using CqListener
final AuthzCqListener listener = (AuthzCqListener) cqQuery.getCqAttributes().getCqListener();
WaitCriterion ev = new WaitCriterion() {
@Override
public boolean done() {
if ((flags & OpFlags.CHECK_FAIL) > 0) {
return 0 == listener.getNumUpdates();
} else {
return numOps == listener.getNumUpdates();
}
}
@Override
public String description() {
return null;
}
};
waitForCriterion(ev, 3 * 1000, 200, true);
if ((flags & OpFlags.CHECK_FAIL) > 0) {
assertEquals(0, listener.getNumUpdates());
} else {
assertEquals(numOps, listener.getNumUpdates());
listener.checkPuts(vals, indices);
}
assertEquals(0, listener.getNumCreates());
assertEquals(0, listener.getNumDestroys());
assertEquals(0, listener.getNumOtherOps());
assertEquals(0, listener.getNumErrors());
} else {
SelectResults cqResults = cqQuery.executeWithInitialResults();
assertNotNull(cqResults);
Set cqResultValues = new HashSet();
for (Object o : cqResults.asList()) {
Struct s = (Struct) o;
cqResultValues.add(s.get("value"));
}
Set cqResultSet = cqResults.asSet();
if ((flags & OpFlags.CHECK_FAIL) == 0) {
assertEquals(numOps, cqResultSet.size());
}
for (int keyNumIndex = 0; keyNumIndex < numOps; ++keyNumIndex) {
int keyNum = indices[keyNumIndex];
if ((flags & OpFlags.CHECK_FAIL) > 0) {
assertFalse(cqResultValues.contains(vals[keyNum]));
} else {
assertTrue(cqResultValues.contains(vals[keyNum]));
}
}
}
} else if (op.isStopCQ()) {
breakLoop = true;
CqQuery cqQuery = getCache().getQueryService().getCq("cq1");
((AuthzCqListener) cqQuery.getCqAttributes().getCqListener()).reset();
cqQuery.stop();
} else if (op.isCloseCQ()) {
breakLoop = true;
CqQuery cqQuery = getCache().getQueryService().getCq("cq1");
((AuthzCqListener) cqQuery.getCqAttributes().getCqListener()).reset();
cqQuery.close();
} else if (op.isRegionClear()) {
breakLoop = true;
if ((flags & OpFlags.LOCAL_OP) > 0) {
region.localClear();
} else {
region.clear();
}
} else if (op.isRegionCreate()) {
breakLoop = true;
// Region subregion = createSubregion(region);
// subregion.createRegionOnServer();
// Create region on server using the DynamicRegionFactory
// Assume it has been already initialized
DynamicRegionFactory drf = DynamicRegionFactory.get();
Region subregion = drf.createDynamicRegion(regionName, SUBREGION_NAME);
assertEquals('/' + regionName + '/' + SUBREGION_NAME, subregion.getFullPath());
} else if (op.isRegionDestroy()) {
breakLoop = true;
if ((flags & OpFlags.LOCAL_OP) > 0) {
region.localDestroyRegion();
} else {
if ((flags & OpFlags.USE_SUBREGION) > 0) {
try {
DynamicRegionFactory.get().destroyDynamicRegion(region.getFullPath());
} catch (RegionDestroyedException ex) {
// harmless to ignore this
System.out.println("doOp: sub-region " + region.getFullPath() + " already destroyed");
operationOmitted = true;
}
} else {
region.destroyRegion();
}
}
} else {
fail("doOp: Unhandled operation " + op);
}
if (expectedResult != NO_EXCEPTION) {
if (!operationOmitted && !op.isUnregisterInterest()) {
fail("Expected an exception while performing operation op =" + op + "flags = " + OpFlags.description(flags));
}
}
} catch (Exception ex) {
exceptionOccurred = true;
if ((ex instanceof ServerConnectivityException || ex instanceof QueryInvocationTargetException || ex instanceof CqException) && (expectedResult == NOTAUTHZ_EXCEPTION) && (ex.getCause() instanceof NotAuthorizedException)) {
System.out.println("doOp: Got expected NotAuthorizedException when doing operation [" + op + "] with flags " + OpFlags.description(flags) + ": " + ex.getCause());
continue;
} else if (expectedResult == OTHER_EXCEPTION) {
System.out.println("doOp: Got expected exception when doing operation: " + ex.toString());
continue;
} else {
fail("doOp: Got unexpected exception when doing operation. Policy = " + policy + " flags = " + OpFlags.description(flags), ex);
}
}
}
if (!exceptionOccurred && !operationOmitted && expectedResult != NO_EXCEPTION) {
fail("Expected an exception while performing operation: " + op + " flags = " + OpFlags.description(flags));
}
}
use of org.apache.geode.cache.InterestResultPolicy in project geode by apache.
the class InterestResultPolicyDUnitTest method verifyResult.
/**
* Verifies the number of entries (including values) created on the test region at the end of
* {@link Region#registerInterest} call depending on the type of {@link InterestResultPolicy}
* registered for the region.
*
* @param interestPolicy - {@link InterestResultPolicy} registered for the region
*/
public static void verifyResult(Object interestPolicy, Object totalKeysRegistered) {
Region region1 = cache.getRegion(Region.SEPARATOR + REGION_NAME);
int entriesSize = region1.entrySet(false).size();
int keysSize = region1.keySet().size();
int valuesSize = region1.values().size();
InterestResultPolicy policy = (InterestResultPolicy) interestPolicy;
LogWriter logger = cache.getLogger();
logger.fine("policy = " + policy + " ==> entries = " + entriesSize + " ;keys = " + keysSize + ";values = " + valuesSize);
if (policy.isNone()) {
// nothing should be created on client cache
assertEquals(0, entriesSize);
assertEquals(0, keysSize);
assertEquals(0, valuesSize);
} else if (policy.isKeys()) {
// all keys should be created with values null
assertEquals(PREPOPULATED_ENTRIES, entriesSize);
assertEquals(PREPOPULATED_ENTRIES, keysSize);
assertEquals(0, valuesSize);
} else if (policy.isKeysValues()) {
// all the keys and values should be created
assertEquals(PREPOPULATED_ENTRIES, entriesSize);
assertEquals(PREPOPULATED_ENTRIES, keysSize);
assertEquals(PREPOPULATED_ENTRIES, valuesSize);
}
}
use of org.apache.geode.cache.InterestResultPolicy in project geode by apache.
the class NewRegionAttributesDUnitTest method registerInterest.
/**
* This method invokes all the registerInterest related API's for the test-region and verifies
* that <code>UnsupportedOperationException</code> is thrown in all the cases since test-region
* does not have bridge-server or bridge-client.
*
* @see Region#registerInterest(Object)
* @see Region#registerInterest(Object, InterestResultPolicy)
* @see Region#registerInterestRegex(String)
* @see Region#registerInterestRegex(String, InterestResultPolicy)
*/
public static void registerInterest() {
InterestResultPolicy policy = InterestResultPolicy.KEYS_VALUES;
int totalKeys = 5;
Region region1 = cache.getRegion(Region.SEPARATOR + REGION_NAME);
List keylist = new ArrayList();
for (int i = 0; i < totalKeys; i++) {
keylist.add("key-" + i);
}
boolean exceptionOccurred = false;
// test registerInterest(key)
try {
region1.registerInterest("DummyKey1");
} catch (UnsupportedOperationException expected) {
exceptionOccurred = true;
} finally {
if (!exceptionOccurred)
fail("UnsupportedOperationException was not thrown as expected for registerInterest(key)");
}
// test registerInterest(key,policy)
exceptionOccurred = false;
try {
region1.registerInterest("DummyKey2", policy);
} catch (UnsupportedOperationException expected) {
exceptionOccurred = true;
} finally {
if (!exceptionOccurred)
fail("UnsupportedOperationException was not thrown as expected for registerInterest(key,policy)");
}
// test registerInterest(keylist)
exceptionOccurred = false;
try {
region1.registerInterest(keylist);
} catch (UnsupportedOperationException expected) {
exceptionOccurred = true;
} finally {
if (!exceptionOccurred)
fail("UnsupportedOperationException was not thrown as expected for registerInterest(keylist)");
}
// test registerInterest(keylist,policy)
exceptionOccurred = false;
try {
region1.registerInterest(keylist, policy);
} catch (UnsupportedOperationException expected) {
exceptionOccurred = true;
} finally {
if (!exceptionOccurred)
fail("UnsupportedOperationException was not thrown as expected for registerInterest(keylist,policy)");
}
// test registerInterestRegex(expr)
exceptionOccurred = false;
try {
region1.registerInterestRegex("ke?");
} catch (UnsupportedOperationException expected) {
exceptionOccurred = true;
} finally {
if (!exceptionOccurred)
fail("UnsupportedOperationException was not thrown as expected for registerInterestRegex(expr)");
}
// test registerInterestRegex(expr,policy)
exceptionOccurred = false;
try {
region1.registerInterestRegex("ke?", InterestResultPolicy.KEYS_VALUES);
} catch (UnsupportedOperationException expected) {
exceptionOccurred = true;
} finally {
if (!exceptionOccurred)
fail("UnsupportedOperationException was not thrown as expected for registerInterestRegex(expr,policy)");
}
}
use of org.apache.geode.cache.InterestResultPolicy in project geode by apache.
the class InterestResultPolicyDUnitTest method registerInterest.
/**
* Registers the test region on client with a keylist containing all keys pre-populated on server
* and a given {@link InterestResultPolicy} type.
*
* @param interestPolicy - InterestResultPolicy type specified (NONE,KEYS,KEY_VALUES or DEFAULT)
*/
public static void registerInterest(Object interestPolicy, Object totalKeysToRegister) {
InterestResultPolicy policy = (InterestResultPolicy) interestPolicy;
int totalKeys = ((Integer) totalKeysToRegister).intValue();
Region region1 = cache.getRegion(Region.SEPARATOR + REGION_NAME);
LogWriter logger = cache.getLogger();
logger.fine("Registering interest in " + totalKeys + " keys");
List keylist = new ArrayList();
for (int i = 0; i < totalKeys; i++) {
keylist.add("key-" + i);
}
try {
region1.registerInterest(keylist, policy);
} catch (CacheWriterException e) {
Assert.fail("failed to register interestlist for the client", e);
}
}
use of org.apache.geode.cache.InterestResultPolicy in project geode by apache.
the class QueueManagerImpl method recoverSingleRegion.
// TODO this is distressingly similar to LocalRegion#processSingleInterest
private void recoverSingleRegion(LocalRegion r, Map keys, int interestType, Connection recoveredConnection, boolean isDurable, boolean receiveValues, boolean isFirstNewConnection) {
if (logger.isDebugEnabled()) {
logger.debug("{}.recoverSingleRegion starting kind={} region={}: {}", this, InterestType.getString(interestType), r.getFullPath(), keys);
}
// build a HashMap, key is policy, value is list
HashMap policyMap = new HashMap();
Iterator keysIter = keys.entrySet().iterator();
while (keysIter.hasNext()) {
// restore and commit an interest
Map.Entry me = (Map.Entry) keysIter.next();
Object key = me.getKey();
InterestResultPolicy pol = (InterestResultPolicy) me.getValue();
if (interestType == InterestType.KEY) {
// Gester: we only consolidate the key into list for InterestType.KEY
LinkedList keyList = (LinkedList) policyMap.get(pol);
if (keyList == null) {
keyList = new LinkedList();
}
keyList.add(key);
policyMap.put(pol, keyList);
} else {
// for other Interest type, do it one by one
recoverSingleKey(r, key, pol, interestType, recoveredConnection, isDurable, receiveValues, isFirstNewConnection);
}
}
// Process InterestType.KEY: Iterator list for each each policy
Iterator polIter = policyMap.entrySet().iterator();
while (polIter.hasNext()) {
Map.Entry me = (Map.Entry) polIter.next();
LinkedList keyList = (LinkedList) me.getValue();
InterestResultPolicy pol = (InterestResultPolicy) me.getKey();
recoverSingleKey(r, keyList, pol, interestType, recoveredConnection, isDurable, receiveValues, isFirstNewConnection);
}
}
Aggregations