use of org.apache.geode.cache.query.Struct in project geode by apache.
the class PutAllCSDUnitTest method testOneServer.
/**
* Tests putAll to one server.
*/
@Test
public void testOneServer() throws CacheException, InterruptedException {
final String title = "testOneServer:";
final Host host = Host.getHost(0);
VM server = host.getVM(0);
VM client1 = host.getVM(2);
VM client2 = host.getVM(3);
final String regionName = getUniqueName();
final String serverHost = NetworkUtils.getServerHostName(server.getHost());
// set <false, true> means <PR=false, notifyBySubscription=true> to enable registerInterest and
// CQ
final int serverPort = createBridgeServer(server, regionName, 0, false, 0, null);
createClient(client1, regionName, serverHost, new int[] { serverPort }, -1, -1, false, true, true);
createClient(client2, regionName, serverHost, new int[] { serverPort }, -1, -1, false, true, true);
server.invoke(new CacheSerializableRunnable(title + "server add listener") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(regionName);
region.getAttributesMutator().addCacheListener(new MyListener(false));
}
});
client2.invoke(new CacheSerializableRunnable(title + "client2 registerInterest and add listener") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(regionName);
region.getAttributesMutator().addCacheListener(new MyListener(false));
// registerInterest for ALL_KEYS
region.registerInterest("ALL_KEYS");
LogWriterUtils.getLogWriter().info("client2 registerInterest ALL_KEYS at " + region.getFullPath());
}
});
client1.invoke(new CacheSerializableRunnable(title + "client1 create local region and run putAll") {
@Override
public void run2() throws CacheException {
AttributesFactory factory2 = new AttributesFactory();
factory2.setScope(Scope.LOCAL);
factory2.addCacheListener(new MyListener(false));
createRegion("localsave", factory2.create());
Region region = doPutAll(regionName, "key-", numberOfEntries);
assertEquals(numberOfEntries, region.size());
}
});
AsyncInvocation async1 = client1.invokeAsync(new CacheSerializableRunnable(title + "client1 create CQ") {
@Override
public void run2() throws CacheException {
// create a CQ for key 10-20
Region localregion = getRootRegion().getSubregion("localsave");
CqAttributesFactory cqf1 = new CqAttributesFactory();
EOCQEventListener EOCQListener = new EOCQEventListener(localregion);
cqf1.addCqListener(EOCQListener);
CqAttributes cqa1 = cqf1.create();
String cqName1 = "EOInfoTracker";
String queryStr1 = "SELECT ALL * FROM /root/" + regionName + " ii WHERE ii.getTicker() >= '10' and ii.getTicker() < '20'";
LogWriterUtils.getLogWriter().info("Query String: " + queryStr1);
try {
QueryService cqService = getCache().getQueryService();
CqQuery EOTracker = cqService.newCq(cqName1, queryStr1, cqa1);
SelectResults rs1 = EOTracker.executeWithInitialResults();
List list1 = rs1.asList();
for (int i = 0; i < list1.size(); i++) {
Struct s = (Struct) list1.get(i);
TestObject o = (TestObject) s.get("value");
LogWriterUtils.getLogWriter().info("InitialResult:" + i + ":" + o);
localregion.put("key-" + i, o);
}
if (localregion.size() > 0) {
LogWriterUtils.getLogWriter().info("CQ is ready");
synchronized (lockObject) {
lockObject.notify();
}
}
waitTillNotify(lockObject2, 20000, (EOCQListener.num_creates == 5 && EOCQListener.num_updates == 5));
EOTracker.close();
} catch (CqClosedException e) {
Assert.fail("CQ", e);
} catch (RegionNotFoundException e) {
Assert.fail("CQ", e);
} catch (QueryInvalidException e) {
Assert.fail("CQ", e);
} catch (CqExistsException e) {
Assert.fail("CQ", e);
} catch (CqException e) {
Assert.fail("CQ", e);
}
}
});
server.invoke(new CacheSerializableRunnable(title + "verify Bridge Server") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(regionName);
assertEquals(numberOfEntries, region.size());
for (int i = 0; i < numberOfEntries; i++) {
TestObject obj = (TestObject) region.getEntry("key-" + i).getValue();
assertEquals(i, obj.getPrice());
}
}
});
// verify CQ is ready
client1.invoke(new CacheSerializableRunnable(title + "verify CQ is ready") {
@Override
public void run2() throws CacheException {
Region localregion = getRootRegion().getSubregion("localsave");
waitTillNotify(lockObject, 10000, (localregion.size() > 0));
assertTrue(localregion.size() > 0);
}
});
// verify registerInterest result at client2
client2.invoke(new CacheSerializableRunnable(title + "verify client2") {
@Override
public void run2() throws CacheException {
final Region region = getRootRegion().getSubregion(regionName);
checkRegionSize(region, numberOfEntries);
for (int i = 0; i < numberOfEntries; i++) {
TestObject obj = (TestObject) region.getEntry("key-" + i).getValue();
assertEquals(i, obj.getPrice());
}
// then do update for key 10-20 to trigger CQ at server2
// destroy key 10-14 to simulate create/update mix case
region.removeAll(Arrays.asList("key-10", "key-11", "key-12", "key-13", "key-14"), "removeAllCallback");
assertEquals(null, region.get("key-10"));
assertEquals(null, region.get("key-11"));
assertEquals(null, region.get("key-12"));
assertEquals(null, region.get("key-13"));
assertEquals(null, region.get("key-14"));
}
});
// verify CQ result at client1
client1.invoke(new CacheSerializableRunnable(title + "Verify client1") {
@Override
public void run2() throws CacheException {
Region localregion = getRootRegion().getSubregion("localsave");
for (int i = 10; i < 15; i++) {
TestObject obj = null;
int cnt = 0;
while (cnt < 100) {
obj = (TestObject) localregion.get("key-" + i);
if (obj != null) {
// wait for the key to be destroyed
Wait.pause(100);
if (LogWriterUtils.getLogWriter().fineEnabled()) {
LogWriterUtils.getLogWriter().info("Waiting 100ms(" + cnt + ") for key-" + i + " to be destroyed");
}
cnt++;
} else {
break;
}
}
}
}
});
client2.invoke(new CacheSerializableRunnable(title + "verify client2") {
@Override
public void run2() throws CacheException {
final Region region = getRootRegion().getSubregion(regionName);
LinkedHashMap map = new LinkedHashMap();
for (int i = 10; i < 20; i++) {
map.put("key-" + i, new TestObject(i * 10));
}
region.putAll(map, "putAllCallback");
}
});
// verify CQ result at client1
client1.invoke(new CacheSerializableRunnable(title + "Verify client1") {
@Override
public void run2() throws CacheException {
Region localregion = getRootRegion().getSubregion("localsave");
for (int i = 10; i < 20; i++) {
TestObject obj = null;
int cnt = 0;
while (cnt < 100) {
obj = (TestObject) localregion.get("key-" + i);
if (obj == null || obj.getPrice() != i * 10) {
Wait.pause(100);
LogWriterUtils.getLogWriter().info("Waiting 100ms(" + cnt + ") for obj.getPrice() == i*10 at entry " + i);
cnt++;
} else {
break;
}
}
assertEquals(i * 10, obj.getPrice());
}
synchronized (lockObject2) {
lockObject2.notify();
}
}
});
ThreadUtils.join(async1, 30 * 1000);
// verify stats for client putAll into distributed region
// 1. verify client staus
/*
* server2.invoke(new CacheSerializableRunnable("server2 execute putAll") { public void run2()
* throws CacheException { try { DistributedSystemConfig config =
* AdminDistributedSystemFactory.defineDistributedSystem(system, null); AdminDistributedSystem
* ads = AdminDistributedSystemFactory.getDistributedSystem(config); ads.connect();
* DistributedMember distributedMember = system.getDistributedMember(); SystemMember member =
* ads.lookupSystemMember(distributedMember);
*
* StatisticResource[] resources = member.getStats(); for (int i=0; i<resources.length; i++) {
* System.out.println("GGG:"+resources[i].getType()); if
* (resources[i].getType().equals("CacheServerClientStats")) { Statistic[] stats =
* resources[i].getStatistics(); for (int j=0; i<stats.length; i++) { if
* (stats[j].getName().equals("putAll")) {
* System.out.println("GGG:"+stats[j].getName()+":"+stats[j].getValue()); } else if
* (stats[j].getName().equals("sendPutAllTime")) {
* System.out.println("GGG:"+stats[j].getName()+":"+stats[j].getValue()); } } } } } catch
* (AdminException e) { fail("Failed while creating AdminDS", e); } } });
*/
// Test Exception handling
// verify CQ is ready
client1.invoke(new CacheSerializableRunnable(title + "test exception handling") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(regionName);
Map m = null;
boolean NPEthrowed = false;
try {
region.putAll(m, "putAllCallback");
fail("Should have thrown NullPointerException");
} catch (NullPointerException ex) {
NPEthrowed = true;
}
assertTrue(NPEthrowed);
region.localDestroyRegion();
boolean RDEthrowed = false;
try {
m = new HashMap();
for (int i = 1; i < 21; i++) {
m.put(new Integer(i), Integer.toString(i));
}
region.putAll(m, "putAllCallback");
fail("Should have thrown RegionDestroyedException");
} catch (RegionDestroyedException ex) {
RDEthrowed = true;
}
assertTrue(RDEthrowed);
try {
region.removeAll(Arrays.asList("key-10", "key-11"), "removeAllCallback");
fail("Should have thrown RegionDestroyedException");
} catch (RegionDestroyedException expected) {
}
}
});
// Stop server
stopBridgeServers(getCache());
}
use of org.apache.geode.cache.query.Struct 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.query.Struct in project geode by apache.
the class CqDataUsingPoolDUnitTest method testEventsDuringQueryExecution.
/**
* Test for events created during the CQ query execution. When CQs are executed using
* executeWithInitialResults there may be possibility that the region changes during that time may
* not be reflected in the query result set thus making the query data and region data
* inconsistent.
*/
@Test
public void testEventsDuringQueryExecution() throws Exception {
final Host host = Host.getHost(0);
VM server = host.getVM(0);
final VM client = host.getVM(1);
final String cqName = "testEventsDuringQueryExecution_0";
cqDUnitTest.createServer(server);
final int port = server.invoke(() -> CqQueryUsingPoolDUnitTest.getCacheServerPort());
final String host0 = NetworkUtils.getServerHostName(server.getHost());
String poolName = "testEventsDuringQueryExecution";
cqDUnitTest.createPool(client, poolName, host0, port);
// create CQ.
cqDUnitTest.createCQ(client, poolName, cqName, cqDUnitTest.cqs[0]);
final int numObjects = 200;
final int totalObjects = 500;
// initialize Region.
server.invoke(new CacheSerializableRunnable("Update Region") {
@Override
public void run2() throws CacheException {
Region region = getCache().getRegion("/root/" + cqDUnitTest.regions[0]);
for (int i = 1; i <= numObjects; i++) {
Portfolio p = new Portfolio(i);
region.put("" + i, p);
}
}
});
// First set testhook in executeWithInitialResults so that queued events
// are not drained before we verify there number.
client.invoke(setTestHook());
// Execute CQ while update is in progress.
AsyncInvocation executeCq = client.invokeAsync(new CacheSerializableRunnable("Execute CQ AsyncInvoke") {
@Override
public void run2() throws CacheException {
QueryService cqService = getCache().getQueryService();
// Get CqQuery object.
CqQuery cq1 = cqService.getCq(cqName);
if (cq1 == null) {
fail("Failed to get CQ " + cqName);
}
SelectResults cqResults = null;
try {
cqResults = cq1.executeWithInitialResults();
} catch (Exception ex) {
Assert.fail("CQ execution failed", ex);
}
// Check num of events received during executeWithInitialResults.
final TestHook testHook = CqQueryImpl.testHook;
Wait.waitForCriterion(new WaitCriterion() {
@Override
public boolean done() {
return testHook.numQueuedEvents() > 0;
}
@Override
public String description() {
return "No queued events found.";
}
}, 3000, 5, true);
getCache().getLogger().fine("Queued Events Size" + testHook.numQueuedEvents());
// Make sure CQEvents are queued during execute with initial results.
CqQueryTestListener cqListener = (CqQueryTestListener) cq1.getCqAttributes().getCqListener();
// Wait for the last key to arrive.
cqListener.waitForCreated("" + totalObjects);
// Check if the events from CqListener are in order.
int oldId = 0;
for (Object cqEvent : cqListener.events.toArray()) {
int newId = new Integer(cqEvent.toString()).intValue();
if (oldId > newId) {
fail("Queued events for CQ Listener during execution with " + "Initial results is not in the order in which they are created.");
}
oldId = newId;
}
// Check if all the IDs are present as part of Select Results and CQ Events.
HashSet ids = new HashSet(cqListener.events);
for (Object o : cqResults.asList()) {
Struct s = (Struct) o;
ids.add(s.get("key"));
}
HashSet missingIds = new HashSet();
String key = "";
for (int i = 1; i <= totalObjects; i++) {
key = "" + i;
if (!(ids.contains(key))) {
missingIds.add(key);
}
}
if (!missingIds.isEmpty()) {
fail("Missing Keys in either ResultSet or the Cq Event list. " + " Missing keys : [size : " + missingIds.size() + "]" + missingIds + " Ids in ResultSet and CQ Events :" + ids);
}
}
});
// Keep updating region (async invocation).
server.invoke(new CacheSerializableRunnable("Update Region") {
@Override
public void run2() throws CacheException {
Wait.pause(200);
client.invoke(new CacheSerializableRunnable("Releasing the latch") {
@Override
public void run2() throws CacheException {
// Now release the testhook so that CQListener can proceed.
final TestHook testHook = CqQueryImpl.testHook;
testHook.ready();
}
});
Region region = getCache().getRegion("/root/" + cqDUnitTest.regions[0]);
for (int i = numObjects + 1; i <= totalObjects; i++) {
Portfolio p = new Portfolio(i);
region.put("" + i, p);
}
}
});
// Close.
cqDUnitTest.closeClient(client);
cqDUnitTest.closeServer(server);
}
use of org.apache.geode.cache.query.Struct in project geode by apache.
the class CqDataDUnitTest method testMultipleExecuteWithInitialResults.
/**
* This test was created to test executeWithInitialResults being called multiple times.
* Previously, the queueEvents would be overwritten and we would lose data. This test will execute
* the method twice. The first time, the first execution will block it's own child thread (TC1).
* The second execution will block until TC1 is completed (based on how executeWithInitialResults
* is implemented) A third thread will be awaken and release the latch in the testhook for TC1 to
* complete.
*
* @throws Exception
*/
@Test
public void testMultipleExecuteWithInitialResults() throws Exception {
final int numObjects = 200;
final int totalObjects = 500;
final Host host = Host.getHost(0);
VM server = host.getVM(0);
VM client = host.getVM(1);
client.invoke(setTestHook());
final String cqName = "testMultiExecuteWithInitialResults";
// initialize server and retreive host and port values
cqDUnitTest.createServer(server);
final int port = server.invoke(() -> CqQueryDUnitTest.getCacheServerPort());
final String host0 = NetworkUtils.getServerHostName(server.getHost());
// Initialize Client.
cqDUnitTest.createClient(client, port, host0);
// create CQ.
cqDUnitTest.createCQ(client, cqName, cqDUnitTest.cqs[0]);
// initialize Region.
server.invoke(new CacheSerializableRunnable("Update Region") {
public void run2() throws CacheException {
Region region = getCache().getRegion("/root/" + cqDUnitTest.regions[0]);
for (int i = 1; i <= numObjects; i++) {
Portfolio p = new Portfolio(i);
region.put("" + i, p);
}
}
});
// Keep updating region (async invocation).
server.invokeAsync(new CacheSerializableRunnable("Update Region") {
public void run2() throws CacheException {
// Wait to give client a chance to register the cq
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
Region region = getCache().getRegion("/root/" + cqDUnitTest.regions[0]);
for (int i = numObjects + 1; i <= totalObjects; i++) {
Portfolio p = new Portfolio(i);
region.put("" + i, p);
}
}
});
// the thread that validates all results and executes first
AsyncInvocation processCqs = client.invokeAsync(new CacheSerializableRunnable("Execute CQ first") {
public void run2() throws CacheException {
SelectResults cqResults = null;
QueryService cqService = getCache().getQueryService();
// Get CqQuery object.
CqQuery cq1 = cqService.getCq(cqName);
if (cq1 == null) {
fail("Failed to get CQ " + cqName);
}
try {
cqResults = cq1.executeWithInitialResults();
} catch (Exception e) {
AssertionError err = new AssertionError("Failed to execute CQ " + cqName);
err.initCause(e);
throw err;
}
CqQueryTestListener cqListener = (CqQueryTestListener) cq1.getCqAttributes().getCqListener();
// Wait for the last key to arrive.
cqListener.waitForCreated("" + totalObjects);
// Check if the events from CqListener are in order.
int oldId = 0;
for (Object cqEvent : cqListener.events.toArray()) {
int newId = new Integer(cqEvent.toString()).intValue();
if (oldId > newId) {
fail("Queued events for CQ Listener during execution with " + "Initial results is not in the order in which they are created.");
}
oldId = newId;
}
// Check if all the IDs are present as part of Select Results and CQ
// Events.
HashSet ids = new HashSet(cqListener.events);
for (Object o : cqResults.asList()) {
Struct s = (Struct) o;
ids.add(s.get("key"));
}
HashSet missingIds = new HashSet();
String key = "";
for (int i = 1; i <= totalObjects; i++) {
key = "" + i;
if (!(ids.contains(key))) {
missingIds.add(key);
}
}
if (!missingIds.isEmpty()) {
fail("Missing Keys in either ResultSet or the Cq Event list. " + " Missing keys : [size : " + missingIds.size() + "]" + missingIds + " Ids in ResultSet and CQ Events :" + ids);
}
}
});
// the second call to executeWithInitialResults. Goes to sleep hopefully
// long enough
// for the first call to executeWithInitialResults first
client.invokeAsync(new CacheSerializableRunnable("Execute CQ second") {
public void run2() throws CacheException {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
QueryService cqService = getCache().getQueryService();
// Get CqQuery object.
CqQuery cq1 = cqService.getCq(cqName);
if (cq1 == null) {
fail("Failed to get CQ " + cqName);
}
try {
cq1.executeWithInitialResults();
} catch (IllegalStateException e) {
// we expect an error due to the cq having already being in run state
} catch (Exception e) {
AssertionError err = new AssertionError("test hook lock interrupted" + cqName);
err.initCause(e);
throw err;
}
}
});
// thread that unlatches the test hook, sleeping long enough for both
// the other two threads to execute first
client.invokeAsync(new CacheSerializableRunnable("Release latch") {
public void run2() throws CacheException {
// had a chance to invoke executeWithInitialResults
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
AssertionError err = new AssertionError("test hook lock interrupted" + cqName);
err.initCause(e);
throw err;
}
CqQueryImpl.testHook.ready();
}
});
// wait for 60 seconds for test to complete
ThreadUtils.join(processCqs, 60 * 1000);
// Close.
cqDUnitTest.closeClient(client);
cqDUnitTest.closeServer(server);
}
use of org.apache.geode.cache.query.Struct in project geode by apache.
the class PdxQueryCQDUnitTest method testCq.
/**
* Tests client-server query on PdxInstance.
*/
@Test
public void testCq() throws CacheException {
final Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm1 = host.getVM(1);
VM vm2 = host.getVM(2);
VM vm3 = host.getVM(3);
final int numberOfEntries = 10;
// where id > 5 (0-5)
final int queryLimit = 6;
// Start server1
vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
public void run2() throws CacheException {
configAndStartBridgeServer();
Region region = getRootRegion().getSubregion(regionName);
for (int i = 0; i < numberOfEntries; i++) {
region.put("key-" + i, new TestObject(i, "vmware"));
}
}
});
// Start server2
vm1.invoke(new CacheSerializableRunnable("Create Bridge Server") {
public void run2() throws CacheException {
configAndStartBridgeServer();
Region region = getRootRegion().getSubregion(regionName);
}
});
// Client pool.
final int port0 = vm0.invoke(() -> PdxQueryCQTestBase.getCacheServerPort());
final int port1 = vm1.invoke(() -> PdxQueryCQTestBase.getCacheServerPort());
final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
// Create client pool.
final String poolName = "testCqPool";
createPool(vm2, poolName, new String[] { host0 }, new int[] { port0 }, true);
createPool(vm3, poolName, new String[] { host0 }, new int[] { port1 }, true);
final String cqName = "testCq";
// Execute CQ
SerializableRunnable executeCq = new CacheSerializableRunnable("Execute queries") {
public void run2() throws CacheException {
LogWriterUtils.getLogWriter().info("### Create CQ. ###" + cqName);
// Get CQ Service.
QueryService qService = null;
try {
qService = (PoolManager.find(poolName)).getQueryService();
} catch (Exception cqe) {
Assert.fail("Failed to getCQService.", cqe);
}
// Create CQ Attributes.
CqAttributesFactory cqf = new CqAttributesFactory();
CqListener[] cqListeners = { new CqQueryTestListener(LogWriterUtils.getLogWriter()) };
((CqQueryTestListener) cqListeners[0]).cqName = cqName;
cqf.initCqListeners(cqListeners);
CqAttributes cqa = cqf.create();
// Create CQ.
try {
CqQuery cq = qService.newCq(cqName, queryString[3], cqa);
SelectResults sr = cq.executeWithInitialResults();
for (Object o : sr.asSet()) {
Struct s = (Struct) o;
Object value = s.get("value");
if (!(value instanceof TestObject)) {
fail("Expected type TestObject, not found in result set. Found type :" + o.getClass());
}
}
} catch (Exception ex) {
AssertionError err = new AssertionError("Failed to create CQ " + cqName + " . ");
err.initCause(ex);
LogWriterUtils.getLogWriter().info("QueryService is :" + qService, err);
throw err;
}
}
};
vm2.invoke(executeCq);
vm3.invoke(executeCq);
// Check for TestObject instances on Server2.
// It should be 0
vm1.invoke(new CacheSerializableRunnable("Create Bridge Server") {
public void run2() throws CacheException {
assertEquals(0, TestObject.numInstance);
}
});
// update
vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(regionName);
for (int i = 0; i < numberOfEntries * 2; i++) {
region.put("key-" + i, new TestObject(i, "vmware"));
}
// Check for TestObject instances.
assertEquals(numberOfEntries * 3, TestObject.numInstance);
}
});
// Check for TestObject instances on Server2.
// It should be 0
vm1.invoke(new CacheSerializableRunnable("Create Bridge Server") {
public void run2() throws CacheException {
assertEquals(0, TestObject.numInstance);
}
});
SerializableRunnable validateCq = new CacheSerializableRunnable("Validate CQs") {
public void run2() throws CacheException {
LogWriterUtils.getLogWriter().info("### Validating CQ. ### " + cqName);
// Get CQ Service.
QueryService cqService = null;
try {
cqService = getCache().getQueryService();
} catch (Exception cqe) {
Assert.fail("Failed to getCQService.", cqe);
}
CqQuery cQuery = cqService.getCq(cqName);
if (cQuery == null) {
fail("Failed to get CqQuery for CQ : " + cqName);
}
CqAttributes cqAttr = cQuery.getCqAttributes();
CqListener[] cqListeners = cqAttr.getCqListeners();
final CqQueryTestListener listener = (CqQueryTestListener) cqListeners[0];
// Wait for the events to show up on the client.
Wait.waitForCriterion(new WaitCriterion() {
public boolean done() {
return listener.getTotalEventCount() >= (numberOfEntries * 2 - queryLimit);
}
public String description() {
return null;
}
}, 30000, 100, false);
listener.printInfo(false);
// Check for event type.
Object[] cqEvents = listener.getEvents();
for (Object o : cqEvents) {
CqEvent cqEvent = (CqEvent) o;
Object value = cqEvent.getNewValue();
if (!(value instanceof TestObject)) {
fail("Expected type TestObject, not found in result set. Found type :" + o.getClass());
}
}
// Check for totalEvents count.
assertEquals("Total Event Count mismatch", (numberOfEntries * 2 - queryLimit), listener.getTotalEventCount());
// Check for create count.
assertEquals("Create Event mismatch", numberOfEntries, listener.getCreateEventCount());
// Check for update count.
assertEquals("Update Event mismatch", numberOfEntries - queryLimit, listener.getUpdateEventCount());
}
};
vm2.invoke(validateCq);
vm3.invoke(validateCq);
this.closeClient(vm2);
this.closeClient(vm3);
this.closeClient(vm1);
this.closeClient(vm0);
}
Aggregations