use of org.apache.geode.test.dunit.SerializableCallable in project geode by apache.
the class SelectStarQueryDUnitTest method testByteArrayReplicatedRegion.
@Test
public void testByteArrayReplicatedRegion() throws Exception {
final Host host = Host.getHost(0);
final VM server1 = host.getVM(0);
final VM client = host.getVM(3);
final byte[] ba = new byte[] { 1, 2, 3, 4, 5 };
// create servers and regions
final int port = (Integer) server1.invoke(new SerializableCallable("Create Server1") {
@Override
public Object call() throws Exception {
Region r1 = getCache().createRegionFactory(RegionShortcut.REPLICATE).create(regName);
// put domain objects
for (int i = 0; i < 10; i++) {
r1.put("key-" + i, ba);
}
CacheServer server = getCache().addCacheServer();
int port = AvailablePortHelper.getRandomAvailablePortForDUnitSite();
server.setPort(port);
server.start();
return port;
}
});
server1.invoke(new SerializableCallable("Set observer") {
@Override
public Object call() throws Exception {
oldObserver = QueryObserverHolder.setInstance(new QueryResultTrackingObserver());
return null;
}
});
// create client
client.invoke(new SerializableCallable("Create client") {
@Override
public Object call() throws Exception {
ClientCacheFactory cf = new ClientCacheFactory();
cf.addPoolServer(getServerHostName(server1.getHost()), port);
ClientCache cache = getClientCache(cf);
cache.createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY).create(regName);
return null;
}
});
// put serialized PortfolioPdx objects
client.invoke(new SerializableCallable("Put objects") {
@Override
public Object call() throws Exception {
Region r1 = getRootRegion(regName);
for (int i = 10; i < 20; i++) {
r1.put("key-" + i, ba);
}
return null;
}
});
// query remotely from client
client.invoke(new SerializableCallable("Query") {
@Override
public Object call() throws Exception {
getLogWriter().info("Querying remotely from client");
QueryService localQS = null;
QueryService remoteQS = null;
try {
localQS = ((ClientCache) getCache()).getLocalQueryService();
remoteQS = ((ClientCache) getCache()).getQueryService();
} catch (Exception e) {
fail("Exception getting query service ", e);
}
SelectResults res = null;
SelectResults[][] sr = new SelectResults[1][2];
for (int i = 0; i < 6; i++) {
try {
res = (SelectResults) localQS.newQuery(queries[i]).execute();
sr[0][0] = res;
res = (SelectResults) remoteQS.newQuery(queries[i]).execute();
sr[0][1] = res;
CacheUtils.compareResultsOfWithAndWithoutIndex(sr);
} catch (Exception e) {
fail("Error executing query: " + queries[i], e);
}
assertEquals(resultSize[i], res.size());
if (i == 3) {
int cnt = ((Integer) res.iterator().next());
assertEquals(20, cnt);
} else {
for (Object o : res) {
if (o instanceof byte[]) {
int j = 0;
for (byte b : ((byte[]) o)) {
if (b != ba[j++]) {
fail("Bytes in byte array are different when queried from client");
}
}
} else {
fail("Result objects for remote client query: " + queries[i] + " should be instance of PortfolioPdx and not " + o.getClass());
}
}
}
}
return null;
}
});
// verify if objects iterated by query are serialized
server1.invoke(new SerializableCallable("Get observer") {
@Override
public Object call() throws Exception {
QueryObserver observer = QueryObserverHolder.getInstance();
assertTrue(QueryObserverHolder.hasObserver());
assertTrue(observer instanceof QueryResultTrackingObserver);
QueryResultTrackingObserver resultObserver = (QueryResultTrackingObserver) observer;
assertFalse(resultObserver.isObjectSerialized());
return null;
}
});
// verify if objects returned by local server query are not serialized
server1.invoke(new SerializableCallable("Query") {
@Override
public Object call() throws Exception {
QueryObserver observer = QueryObserverHolder.setInstance(new QueryResultTrackingObserver());
QueryService qs = null;
try {
qs = getCache().getQueryService();
} catch (Exception e) {
fail("Exception getting query service ", e);
}
SelectResults res = null;
for (int i = 0; i < 6; i++) {
try {
res = (SelectResults) qs.newQuery(queries[i]).execute();
} catch (Exception e) {
fail("Error executing query: " + queries[i], e);
}
assertEquals(resultSize[i], res.size());
if (i == 3) {
int cnt = ((Integer) res.iterator().next());
assertEquals(20, cnt);
} else {
for (Object o : res) {
if (o instanceof byte[]) {
int j = 0;
for (byte b : ((byte[]) o)) {
if (b != ba[j++]) {
fail("Bytes in byte array are different when queried locally on server");
}
}
} else {
fail("Result objects for server local query: " + queries[i] + " should be instance of byte array and not " + o.getClass());
}
}
}
}
observer = QueryObserverHolder.getInstance();
assertTrue(QueryObserverHolder.hasObserver());
assertTrue(observer instanceof QueryResultTrackingObserver);
QueryResultTrackingObserver resultObserver = (QueryResultTrackingObserver) observer;
assertFalse(resultObserver.isObjectSerialized());
// reset observer
QueryObserverHolder.setInstance(oldObserver);
return null;
}
});
closeCache(client);
closeCache(server1);
}
use of org.apache.geode.test.dunit.SerializableCallable in project geode by apache.
the class SelectStarQueryDUnitTest method testByteArrayPartitionedRegion.
@Test
public void testByteArrayPartitionedRegion() throws Exception {
final Host host = Host.getHost(0);
final VM server1 = host.getVM(0);
final VM client = host.getVM(3);
final VM server2 = host.getVM(1);
final VM server3 = host.getVM(2);
final byte[][] objs = new byte[10][5];
for (int i = 0; i < objs.length; i++) {
objs[i] = new byte[] { 1, 2, 3, 4, 5 };
}
// create servers and regions
final int port1 = startPartitionedCacheServer(server1, objs);
final int port2 = startPartitionedCacheServer(server2, objs);
final int port3 = startPartitionedCacheServer(server3, objs);
server1.invoke(new SerializableCallable("Set observer") {
@Override
public Object call() throws Exception {
oldObserver = QueryObserverHolder.setInstance(new QueryResultTrackingObserver());
return null;
}
});
// create client
client.invoke(new SerializableCallable("Create client") {
@Override
public Object call() throws Exception {
ClientCacheFactory cf = new ClientCacheFactory();
cf.addPoolServer(getServerHostName(server1.getHost()), port1);
cf.addPoolServer(getServerHostName(server2.getHost()), port2);
cf.addPoolServer(getServerHostName(server3.getHost()), port3);
ClientCache cache = getClientCache(cf);
cache.createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY).create(regName);
return null;
}
});
// put serialized PortfolioPdx objects
client.invoke(new SerializableCallable("Put objects") {
@Override
public Object call() throws Exception {
Region r1 = getRootRegion(regName);
for (int i = 10; i < 20; i++) {
r1.put("key-" + i, new byte[] { 1, 2, 3, 4, 5 });
}
return null;
}
});
// query remotely from client
client.invoke(new SerializableCallable("Query") {
@Override
public Object call() throws Exception {
getLogWriter().info("Querying remotely from client");
QueryService localQS = null;
QueryService remoteQS = null;
try {
localQS = ((ClientCache) getCache()).getLocalQueryService();
remoteQS = ((ClientCache) getCache()).getQueryService();
} catch (Exception e) {
fail("Exception getting query service ", e);
}
SelectResults res = null;
SelectResults[][] sr = new SelectResults[1][2];
for (int i = 0; i < 6; i++) {
try {
res = (SelectResults) localQS.newQuery(queries[i]).execute();
sr[0][0] = res;
res = (SelectResults) remoteQS.newQuery(queries[i]).execute();
sr[0][1] = res;
CacheUtils.compareResultsOfWithAndWithoutIndex(sr);
} catch (Exception e) {
fail("Error executing query: " + queries[i], e);
}
assertEquals(resultSize[i], res.size());
if (i == 3) {
int cnt = ((Integer) res.iterator().next());
assertEquals(20, cnt);
} else {
for (Object o : res) {
if (o instanceof byte[]) {
int j = 0;
for (byte b : ((byte[]) o)) {
if (b != objs[0][j++]) {
fail("Bytes in byte array are different when queried from client");
}
}
} else {
fail("Result objects for remote client query: " + queries[i] + " should be instance of PortfolioPdx and not " + o.getClass());
}
}
}
}
return null;
}
});
// verify if objects iterated by query are serialized
server1.invoke(new SerializableCallable("Get observer") {
@Override
public Object call() throws Exception {
QueryObserver observer = QueryObserverHolder.getInstance();
assertTrue(QueryObserverHolder.hasObserver());
assertTrue(observer instanceof QueryResultTrackingObserver);
QueryResultTrackingObserver resultObserver = (QueryResultTrackingObserver) observer;
assertFalse(resultObserver.isObjectSerialized());
return null;
}
});
// verify if objects returned by local server query are not serialized
server1.invoke(new SerializableCallable("Query") {
@Override
public Object call() throws Exception {
QueryObserver observer = QueryObserverHolder.setInstance(new QueryResultTrackingObserver());
QueryService qs = null;
try {
qs = getCache().getQueryService();
} catch (Exception e) {
fail("Exception getting query service ", e);
}
SelectResults res = null;
for (int i = 0; i < 6; i++) {
try {
res = (SelectResults) qs.newQuery(queries[i]).execute();
} catch (Exception e) {
fail("Error executing query: " + queries[i], e);
}
assertEquals(resultSize[i], res.size());
if (i == 3) {
int cnt = ((Integer) res.iterator().next());
assertEquals(20, cnt);
} else {
for (Object o : res) {
if (o instanceof byte[]) {
int j = 0;
for (byte b : ((byte[]) o)) {
if (b != objs[0][j++]) {
fail("Bytes in byte array are different when queried locally on server");
}
}
} else {
fail("Result objects for server local query: " + queries[i] + " should be instance of byte array and not " + o.getClass());
}
}
}
}
observer = QueryObserverHolder.getInstance();
assertTrue(QueryObserverHolder.hasObserver());
assertTrue(observer instanceof QueryResultTrackingObserver);
QueryResultTrackingObserver resultObserver = (QueryResultTrackingObserver) observer;
assertFalse(resultObserver.isObjectSerialized());
// reset observer
QueryObserverHolder.setInstance(oldObserver);
return null;
}
});
closeCache(client);
closeCache(server1);
}
use of org.apache.geode.test.dunit.SerializableCallable in project geode by apache.
the class SelectStarQueryDUnitTest method testSelectStarQueryForPdxObjects.
@Test
public void testSelectStarQueryForPdxObjects() throws Exception {
final Host host = Host.getHost(0);
final VM server1 = host.getVM(0);
final VM client = host.getVM(3);
// create servers and regions
final int port1 = startReplicatedCacheServer(server1);
server1.invoke(new SerializableCallable("Set observer") {
@Override
public Object call() throws Exception {
oldObserver = QueryObserverHolder.setInstance(new QueryResultTrackingObserver());
return null;
}
});
// create client
client.invoke(new SerializableCallable("Create client") {
@Override
public Object call() throws Exception {
ClientCacheFactory cf = new ClientCacheFactory();
cf.addPoolServer(getServerHostName(server1.getHost()), port1);
ClientCache cache = getClientCache(cf);
cache.createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY).create(regName);
return null;
}
});
// Update with serialized PortfolioPdx objects
client.invoke(new SerializableCallable("Put objects") {
@Override
public Object call() throws Exception {
Region r1 = getRootRegion(regName);
for (int i = 0; i < 20; i++) {
r1.put("key-" + i, new PortfolioPdx(i));
}
return null;
}
});
// query remotely from client
client.invoke(new SerializableCallable("Query") {
@Override
public Object call() throws Exception {
getLogWriter().info("Querying remotely from client");
QueryService localQS = null;
QueryService remoteQS = null;
try {
localQS = ((ClientCache) getCache()).getLocalQueryService();
remoteQS = ((ClientCache) getCache()).getQueryService();
} catch (Exception e) {
fail("Exception getting query service ", e);
}
SelectResults res = null;
SelectResults[][] sr = new SelectResults[1][2];
for (int i = 0; i < queries.length; i++) {
try {
res = (SelectResults) localQS.newQuery(queries[i]).execute();
sr[0][0] = res;
res = (SelectResults) remoteQS.newQuery(queries[i]).execute();
sr[0][1] = res;
CacheUtils.compareResultsOfWithAndWithoutIndex(sr);
} catch (Exception e) {
fail("Error executing query: " + queries[i], e);
}
assertEquals(resultSize[i], res.size());
if (i == 3) {
int cnt = ((Integer) res.iterator().next());
assertEquals(20, cnt);
} else {
for (Object rs : res) {
if (rs instanceof StructImpl) {
for (Object obj : ((StructImpl) rs).getFieldValues()) {
if (obj instanceof PortfolioPdx || obj instanceof PositionPdx) {
} else {
fail("Result objects for remote client query: " + queries[i] + " should be instance of PortfolioPdx and not " + obj.getClass());
}
}
} else if (rs instanceof PortfolioPdx) {
} else {
fail("Result objects for remote client query: " + queries[i] + " should be instance of PortfolioPdx and not " + rs.getClass());
}
}
}
}
return null;
}
});
// verify if objects iterated by query are serialized
server1.invoke(new SerializableCallable("Get observer") {
@Override
public Object call() throws Exception {
QueryObserver observer = QueryObserverHolder.getInstance();
assertTrue(QueryObserverHolder.hasObserver());
assertTrue(observer instanceof QueryResultTrackingObserver);
QueryResultTrackingObserver resultObserver = (QueryResultTrackingObserver) observer;
assertTrue(resultObserver.isObjectSerialized());
return null;
}
});
// verify if objects returned by local server query are not serialized
server1.invoke(new SerializableCallable("Query") {
@Override
public Object call() throws Exception {
QueryObserver observer = QueryObserverHolder.setInstance(new QueryResultTrackingObserver());
QueryService qs = null;
try {
qs = getCache().getQueryService();
} catch (Exception e) {
fail("Exception getting query service ", e);
}
SelectResults res = null;
for (int i = 0; i < queries.length; i++) {
try {
res = (SelectResults) qs.newQuery(queries[i]).execute();
} catch (Exception e) {
fail("Error executing query: " + queries[i], e);
}
assertEquals(resultSize[i], res.size());
if (i == 3) {
int cnt = ((Integer) res.iterator().next());
assertEquals(20, cnt);
} else {
for (Object rs : res) {
if (rs instanceof StructImpl) {
for (Object obj : ((StructImpl) rs).getFieldValues()) {
if (obj instanceof PortfolioPdx || obj instanceof PositionPdx) {
} else {
fail("Result objects for remote client query: " + queries[i] + " should be instance of PortfolioPdx and not " + obj.getClass());
}
}
} else if (rs instanceof PortfolioPdx) {
} else {
fail("Result objects for remote client query: " + queries[i] + " should be instance of PortfolioPdx and not " + rs.getClass());
}
}
}
}
observer = QueryObserverHolder.getInstance();
assertTrue(QueryObserverHolder.hasObserver());
assertTrue(observer instanceof QueryResultTrackingObserver);
QueryResultTrackingObserver resultObserver = (QueryResultTrackingObserver) observer;
assertFalse(resultObserver.isObjectSerialized());
QueryObserverHolder.setInstance(oldObserver);
return null;
}
});
// verify if Pdx instances are returned by local server query
// if read-serialized is set true
server1.invoke(new SerializableCallable("Query") {
@Override
public Object call() throws Exception {
GemFireCacheImpl cache = (GemFireCacheImpl) getCache();
cache.setReadSerialized(true);
QueryService qs = null;
try {
qs = getCache().getQueryService();
} catch (Exception e) {
fail("Exception getting query service ", e);
}
SelectResults res = null;
for (int i = 0; i < queries.length; i++) {
try {
res = (SelectResults) qs.newQuery(queries[i]).execute();
} catch (Exception e) {
fail("Error executing query: " + queries[i], e);
}
assertEquals(resultSize[i], res.size());
if (i == 3) {
int cnt = ((Integer) res.iterator().next());
assertEquals(20, cnt);
} else {
for (Object rs : res) {
if (rs instanceof StructImpl) {
for (Object obj : ((StructImpl) rs).getFieldValues()) {
if (obj instanceof PdxInstance) {
} else {
fail("Result objects for remote client query: " + queries[i] + " should be instance of PdxInstance and not " + obj.getClass());
}
}
} else if (rs instanceof PdxInstance) {
} else {
fail("Result objects for remote client query: " + queries[i] + " should be instance of PdxInstance and not " + rs.getClass());
}
}
}
}
return null;
}
});
closeCache(client);
closeCache(server1);
}
use of org.apache.geode.test.dunit.SerializableCallable in project geode by apache.
the class ResourceManagerWithQueryMonitorDUnitTest method doCriticalMemoryHitAddResultsTestWithMultipleServers.
private void doCriticalMemoryHitAddResultsTestWithMultipleServers(final String regionName, boolean createPR, final int criticalThreshold, final boolean disabledQueryMonitorForLowMem, final int queryTimeout, final boolean hitCriticalThreshold) throws Exception {
// create region on the server
final Host host = Host.getHost(0);
final VM server1 = host.getVM(0);
final VM server2 = host.getVM(1);
final VM client = host.getVM(2);
final int numObjects = 200;
try {
final int[] port = AvailablePortHelper.getRandomAvailableTCPPorts(2);
startCacheServer(server1, port[0], criticalThreshold, disabledQueryMonitorForLowMem, queryTimeout, regionName, createPR, 0);
startCacheServer(server2, port[1], criticalThreshold, true, -1, regionName, createPR, 0);
startClient(client, server1, port[0], regionName);
populateData(server2, regionName, numObjects);
createCancelDuringAddResultsTestHook(server1);
client.invoke(new SerializableCallable("executing query to be canceled during add results") {
public Object call() {
QueryService qs = null;
try {
qs = getCache().getQueryService();
Query query = qs.newQuery("Select * From /" + regionName);
SelectResults results = (SelectResults) query.execute();
if (hitCriticalThreshold && disabledQueryMonitorForLowMem == false) {
throw new CacheException("should have hit low memory") {
};
}
} catch (Exception e) {
handleException(e, hitCriticalThreshold, disabledQueryMonitorForLowMem, queryTimeout);
}
return 0;
}
});
verifyRejectedObjects(server1, disabledQueryMonitorForLowMem, queryTimeout, hitCriticalThreshold);
// Pause for a second and then let's recover
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
// Recover from critical heap
if (hitCriticalThreshold) {
vmRecoversFromCriticalHeap(server1);
}
// Check to see if query execution is ok under "normal" or "healthy" conditions
client.invoke(new CacheSerializableRunnable("Executing query when system is 'Normal'") {
public void run2() {
try {
QueryService qs = getCache().getQueryService();
Query query = qs.newQuery("Select * From /" + regionName);
SelectResults results = (SelectResults) query.execute();
assertEquals(numObjects, results.size());
} catch (QueryInvocationTargetException e) {
assertFalse(true);
} catch (NameResolutionException e) {
assertFalse(true);
} catch (TypeMismatchException e) {
assertFalse(true);
} catch (FunctionDomainException e) {
assertFalse(true);
}
}
});
// Recover from critical heap
if (hitCriticalThreshold) {
vmRecoversFromCriticalHeap(server1);
}
} finally {
stopServer(server1);
stopServer(server2);
}
}
use of org.apache.geode.test.dunit.SerializableCallable in project geode by apache.
the class QueryParamsAuthorizationDUnitTest method testQueryParamsInAuthCallback.
@Ignore("Bug 51079")
@Test
public void testQueryParamsInAuthCallback() throws Exception {
final Host host = Host.getHost(0);
final VM server1 = host.getVM(0);
final VM client = host.getVM(1);
// create servers and regions
final int port = (Integer) server1.invoke(new SerializableCallable("Create Server1") {
@Override
public Object call() throws Exception {
CacheFactory cf = new CacheFactory().set(MCAST_PORT, "0").set(SECURITY_CLIENT_ACCESSOR, "org.apache.geode.cache.query.dunit.QueryAuthorization.create").set(SECURITY_CLIENT_AUTHENTICATOR, DummyAuthenticator.class.getName() + ".create");
Cache cache = getCache(cf);
cache.createRegionFactory(RegionShortcut.REPLICATE).create(regName);
CacheServer server = cache.addCacheServer();
int port = AvailablePortHelper.getRandomAvailablePortForDUnitSite();
server.setPort(port);
server.start();
return port;
}
});
// create client
client.invoke(new SerializableCallable("Create client") {
@Override
public Object call() throws Exception {
ClientCacheFactory ccf = new ClientCacheFactory().addPoolServer(NetworkUtils.getServerHostName(server1.getHost()), port).set(SECURITY_CLIENT_AUTH_INIT, UserPasswordAuthInit.class.getName() + ".create").set(SECURITY_PREFIX + "username", "root").set(SECURITY_PREFIX + "password", "root");
ClientCache cache = getClientCache(ccf);
Region r1 = cache.createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY).create(regName);
for (int i = 0; i < 20; i++) {
r1.put("key-" + i, new Portfolio(i));
}
QueryService qs = cache.getQueryService();
Object[] params = new Object[] { "active", 0 };
SelectResults sr = (SelectResults) qs.newQuery("select * from " + r1.getFullPath() + " where status = $1 and ID > $2 ").execute(params);
assertTrue("Result size should be greater than 0 ", sr.size() > 0);
return null;
}
});
}
Aggregations