use of org.apache.geode.cache.query.CqQuery 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.CqQuery in project geode by apache.
the class PRDeltaPropagationDUnitTest method createClientCache.
public static void createClientCache(Integer port1, Boolean subscriptionEnable, Boolean isEmpty, Boolean isCq) throws Exception {
PRDeltaPropagationDUnitTest test = new PRDeltaPropagationDUnitTest();
Properties props = new Properties();
props.setProperty(MCAST_PORT, "0");
props.setProperty(LOCATORS, "");
test.createCache(props);
lastKeyReceived = false;
queryUpdateExecuted = false;
queryDestroyExecuted = false;
notADeltaInstanceObj = false;
isFailed = false;
procced = false;
numValidCqEvents = 0;
PoolImpl p = (PoolImpl) PoolManager.createFactory().addServer("localhost", port1).setSubscriptionEnabled(true).setSubscriptionRedundancy(0).setThreadLocalConnections(true).setMinConnections(6).setReadTimeout(20000).setPingInterval(10000).setRetryAttempts(5).create("PRDeltaPropagationDUnitTestPool");
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.LOCAL);
factory.setConcurrencyChecksEnabled(true);
if (isEmpty.booleanValue()) {
factory.setSubscriptionAttributes(new SubscriptionAttributes(InterestPolicy.ALL));
factory.setDataPolicy(DataPolicy.EMPTY);
}
factory.setPoolName(p.getName());
factory.setCloningEnabled(false);
factory.addCacheListener(new CacheListenerAdapter() {
@Override
public void afterCreate(EntryEvent event) {
if (LAST_KEY.equals(event.getKey())) {
lastKeyReceived = true;
}
}
});
RegionAttributes attrs = factory.create();
deltaPR = cache.createRegion(REGION_NAME, attrs);
if (subscriptionEnable.booleanValue()) {
deltaPR.registerInterest("ALL_KEYS");
}
pool = p;
if (isCq.booleanValue()) {
CqAttributesFactory cqf = new CqAttributesFactory();
CqListenerAdapter cqlist = new CqListenerAdapter() {
@Override
@SuppressWarnings("synthetic-access")
public void onEvent(CqEvent cqEvent) {
if (LAST_KEY.equals(cqEvent.getKey().toString())) {
lastKeyReceived = true;
} else if (!(cqEvent.getNewValue() instanceof Delta)) {
notADeltaInstanceObj = true;
} else if (cqEvent.getQueryOperation().isUpdate() && cqEvent.getBaseOperation().isUpdate() && DELTA_KEY.equals(cqEvent.getKey().toString())) {
queryUpdateExecuted = true;
} else if (cqEvent.getQueryOperation().isDestroy() && cqEvent.getBaseOperation().isUpdate() && DELTA_KEY.equals(cqEvent.getKey().toString())) {
queryDestroyExecuted = true;
}
if (forOldNewCQVarification) {
if (DELTA_KEY.equals(cqEvent.getKey().toString())) {
if (numValidCqEvents == 0 && ((DeltaTestImpl) cqEvent.getNewValue()).getIntVar() == 8) {
procced = true;
} else if (procced && numValidCqEvents == 1 && ((DeltaTestImpl) cqEvent.getNewValue()).getIntVar() == 10) {
// this tell us that every thing is fine
isFailed = true;
}
}
}
numValidCqEvents++;
}
};
cqf.addCqListener(cqlist);
CqAttributes cqa = cqf.create();
CqQuery cq = cache.getQueryService().newCq("CQ_Delta", CQ, cqa);
cq.execute();
}
}
use of org.apache.geode.cache.query.CqQuery in project geode by apache.
the class CQClientAuthDUnitTest method testPostProcess.
@Test
public void testPostProcess() {
String query = "select * from /" + REGION_NAME;
client1.invoke(() -> {
Properties props = new Properties();
props.setProperty(LOCATORS, "");
props.setProperty(MCAST_PORT, "0");
props.setProperty(SECURITY_CLIENT_AUTH_INIT, UserPasswordAuthInit.class.getName() + ".create");
ClientCacheFactory factory = new ClientCacheFactory(props);
factory.addPoolServer("localhost", server.getPort());
factory.setPoolThreadLocalConnections(false);
factory.setPoolMinConnections(5);
factory.setPoolSubscriptionEnabled(true);
factory.setPoolMultiuserAuthentication(true);
ClientCache clientCache = factory.create();
Region region = clientCache.createClientRegionFactory(ClientRegionShortcut.PROXY).create(REGION_NAME);
Pool pool = PoolManager.find(region);
Properties userProps = new Properties();
userProps.setProperty("security-username", "super-user");
userProps.setProperty("security-password", "1234567");
ProxyCache cache = (ProxyCache) clientCache.createAuthenticatedView(userProps, pool.getName());
QueryService qs = cache.getQueryService();
CqAttributesFactory cqAttributesFactory = new CqAttributesFactory();
CqAttributes cqa = cqAttributesFactory.create();
// Create the CqQuery
CqQuery cq = qs.newCq("CQ1", query, cqa, true);
cq.execute();
});
}
use of org.apache.geode.cache.query.CqQuery in project geode by apache.
the class CQPDXPostProcessorDUnitTest method testCQ.
@Test
public void testCQ() {
String query = "select * from /" + REGION_NAME;
client1.invoke(() -> {
ClientCache cache = createClientCache("super-user", "1234567", server.getPort());
Region region = createProxyRegion(cache, REGION_NAME);
Pool pool = PoolManager.find(region);
QueryService qs = pool.getQueryService();
CqAttributesFactory factory = new CqAttributesFactory();
factory.addCqListener(new CqListenerImpl() {
@Override
public void onEvent(final CqEvent aCqEvent) {
Object key = aCqEvent.getKey();
Object value = aCqEvent.getNewValue();
if (key.equals("key1")) {
assertTrue(value instanceof SimpleClass);
} else if (key.equals("key2")) {
assertTrue(Arrays.equals(BYTES, (byte[]) value));
}
}
});
CqAttributes cqa = factory.create();
// Create the CqQuery
CqQuery cq = qs.newCq("CQ1", query, cqa);
CqResults results = cq.executeWithInitialResults();
});
client2.invoke(() -> {
ClientCache cache = createClientCache("authRegionUser", "1234567", server.getPort());
Region region = createProxyRegion(cache, REGION_NAME);
region.put("key1", new SimpleClass(1, (byte) 1));
region.put("key2", BYTES);
});
// wait for events to fire
Awaitility.await().atMost(1, TimeUnit.SECONDS);
PDXPostProcessor pp = (PDXPostProcessor) SecurityService.getSecurityService().getPostProcessor();
assertEquals(pp.getCount(), 2);
}
use of org.apache.geode.cache.query.CqQuery in project geode by apache.
the class CQPostProcessorDunitTest method testPostProcess.
@Test
public void testPostProcess() {
String query = "select * from /AuthRegion";
client1.invoke(() -> {
ClientCache cache = createClientCache("super-user", "1234567", server.getPort());
Region region = createProxyRegion(cache, REGION_NAME);
Pool pool = PoolManager.find(region);
QueryService qs = pool.getQueryService();
CqAttributesFactory factory = new CqAttributesFactory();
factory.addCqListener(new CqListenerImpl() {
@Override
public void onEvent(final CqEvent aCqEvent) {
assertEquals("key6", aCqEvent.getKey());
assertEquals("super-user/AuthRegion/key6/value6", aCqEvent.getNewValue());
}
});
CqAttributes cqa = factory.create();
// Create the CqQuery
CqQuery cq = qs.newCq("CQ1", query, cqa);
CqResults results = cq.executeWithInitialResults();
assertEquals(5, results.size());
String resultString = results.toString();
assertTrue(resultString, resultString.contains("key:key0,value:super-user/null/key0/value0"));
assertTrue(resultString.contains("key:key1,value:super-user/null/key1/value1"));
assertTrue(resultString.contains("key:key2,value:super-user/null/key2/value2"));
assertTrue(resultString.contains("key:key3,value:super-user/null/key3/value3"));
assertTrue(resultString.contains("key:key4,value:super-user/null/key4/value4"));
});
client2.invoke(() -> {
ClientCache cache = createClientCache("authRegionUser", "1234567", server.getPort());
Region region = createProxyRegion(cache, REGION_NAME);
region.put("key6", "value6");
});
}
Aggregations