use of org.apache.geode.cache.query.RegionNotFoundException in project geode by apache.
the class PRQueryRegionClosedJUnitTest method testQueryingWithRegionClose.
/**
* Tests the execution of query on a PartitionedRegion created on a single data store. <br>
* 1. Creates a PR with redundancy=0 on a single VM. <br>
* 2. Puts some test Objects in cache.<br>
* 3. Create a Thread and fire queries on the data and verifies the result.<br>
* 4. Create another Thread and call Region#close() on the PR region.<br>
*
*
* @throws Exception
*/
@Test
public void testQueryingWithRegionClose() throws Exception {
logger.info("PRQueryRegionClosedJUnitTest#testQueryingWithRegionClose: Test Started ");
logger.info("PRQueryRegionClosedJUnitTest#testQueryingWithRegionClose: creating PR Region ");
final Region region = PartitionedRegionTestHelper.createPartitionedRegion(regionName, localMaxMemory, redundancy);
final Region localRegion = PartitionedRegionTestHelper.createLocalRegion(localRegionName);
final StringBuffer errorBuf = new StringBuffer("");
PortfolioData[] portfolios = new PortfolioData[100];
try {
for (int j = 0; j < 100; j++) {
portfolios[j] = new PortfolioData(j);
}
logger.info("PRQueryRegionClosedJUnitTest#testQueryingWithRegionClose: populating PortfolioData into the PR Datastore ");
populateData(region, portfolios);
logger.info("PRQueryRegionClosedJUnitTest#testQueryingWithRegionClose: populating PortfolioData into the PR Datastore ");
populateData(localRegion, portfolios);
final String[] queryString = { "ID = 0 OR ID = 1", "ID > 4 AND ID < 9", "ID = 5", "ID < 5 ", "ID <= 5" };
logger.info("PRQueryRegionClosedJUnitTest#testQueryingWithRegionClose: Creating a Thread which will fire queries on the datastore");
Thread t1 = new Thread(new Runnable() {
public void run() {
final String expectedRegionDestroyedException = RegionDestroyedException.class.getName();
logger.info("<ExpectedException action=add>" + expectedRegionDestroyedException + "</ExpectedException>");
for (int i = 0; i < queryString.length; i++) {
try {
SelectResults resSetPR = region.query(queryString[i]);
SelectResults resSetLocal = localRegion.query(queryString[i]);
String failureString = PartitionedRegionTestHelper.compareResultSets(resSetPR, resSetLocal);
Thread.sleep(delayQuery);
if (failureString != null) {
errorBuf.append(failureString);
throw (new Exception(failureString));
}
} catch (InterruptedException ie) {
fail("interrupted");
} catch (RegionDestroyedException rde) {
logger.info("PRQueryRegionClosedJUnitTest#testQueryingWithRegionClose: RegionDestroyedException as Expected " + rde);
} catch (RegionNotFoundException rnfe) {
logger.info("PRQueryRegionClosedJUnitTest#testQueryingWithRegionClose: RegionNotFoundException as Expected " + rnfe);
} catch (QueryInvocationTargetException qite) {
logger.info("PRQueryRegionClosedJUnitTest#testQueryingWithRegionClose: QueryInvocationTargetException as Expected " + qite);
} catch (Exception qe) {
logger.info("PRQueryRegionClosedJUnitTest#testQueryingWithRegionClose: Unexpected Exception " + qe);
encounteredException = true;
StringWriter sw = new StringWriter();
qe.printStackTrace(new PrintWriter(sw));
errorBuf.append(sw);
}
}
logger.info("<ExpectedException action=remove>" + expectedRegionDestroyedException + "</ExpectedException>");
}
});
logger.info("PRQueryRegionClosedJUnitTest#testQueryingWithRegionClose: Creating a Thread which will call Region.destroyRegion() on the datastore ");
Thread t2 = new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(2500);
} catch (InterruptedException ie) {
fail("interrupted");
}
region.close();
logger.info("PROperationWithQueryDUnitTest#getCacheSerializableRunnableForRegionClose: Region Closed on VM ");
}
});
logger.info("PRQueryRegionClosedJUnitTest#testQueryingWithRegionClose: Initiating the Threads");
t1.start();
t2.start();
logger.info("PRQueryRegionClosedJUnitTest#testQueryingWithRegionClose: Waiting for the Threads to join ");
t1.join(30000);
assertFalse(t1.isAlive());
t2.join(30000);
assertFalse(t2.isAlive());
logger.info("PRQueryRegionClosedJUnitTest#testQueryingWithRegionClose: checking for any Unexpected Exception's occurred");
assertFalse("PRQueryRegionClosedJUnitTest#testQueryingWithRegionClose: Exception occurred in Query-thread", encounteredException);
} catch (Exception e) {
e.printStackTrace();
fail("PRQueryRegionClosedJUnitTest#testQueryingWithRegionClose: Test failed because of exception " + e);
}
logger.info("PRQueryRegionClosedJUnitTest#testQueryingWithRegionClose: Test Ended");
}
use of org.apache.geode.cache.query.RegionNotFoundException in project geode by apache.
the class PRQueryRegionDestroyedJUnitTest method testQueryOnSingleDataStore.
/**
* Tests the execution of query on a PartitionedRegion created on a single data store. <br>
* 1. Creates a PR with redundancy=0 on a single VM. <br>
* 2. Puts some test Objects in cache.<br>
* 3. Create a Thread and fire queries on the data and verifies the result.<br>
* 4. Create another Thread and call Region#destroyRegion() on the PR region.<br>
*
*
* @throws Exception
*/
@Test
public void testQueryOnSingleDataStore() throws Exception {
logger.info("PRQueryRegionDestroyedJUnitTest#testQueryOnSingleDataStore: Test Started ");
logger.info("PRQueryRegionDestroyedJUnitTest#testQueryOnSingleDataStore: creating PR Region ");
final Region region = PartitionedRegionTestHelper.createPartitionedRegion(regionName, localMaxMemory, redundancy);
final Region localRegion = PartitionedRegionTestHelper.createLocalRegion(localRegionName);
final StringBuffer errorBuf = new StringBuffer("");
PortfolioData[] portfolios = new PortfolioData[dataSize];
try {
for (int j = 0; j < dataSize; j++) {
portfolios[j] = new PortfolioData(j);
}
logger.info("PRQueryRegionDestroyedJUnitTest#testQueryOnSingleDataStore: populating PortfolioData into the PR Datastore ");
populateData(region, portfolios);
logger.info("PRQueryRegionDestroyedJUnitTest#testQueryOnSingleDataStore: populating PortfolioData into the PR Datastore ");
populateData(localRegion, portfolios);
final String[] queryString = { "ID = 0 OR ID = 1", "ID > 4 AND ID < 9", "ID = 5", "ID < 5 ", "ID <= 5" };
logger.info("PRQueryRegionDestroyedJUnitTest#testQueryOnSingleDataStore: Creating a Thread which will fire queries on the datastore");
Thread t1 = new Thread(new Runnable() {
public void run() {
final String expectedRegionDestroyedException = RegionDestroyedException.class.getName();
logger.info("<ExpectedException action=add>" + expectedRegionDestroyedException + "</ExpectedException>");
for (int i = 0; i < queryString.length; i++) {
try {
SelectResults resSetPR = region.query(queryString[i]);
SelectResults resSetLocal = localRegion.query(queryString[i]);
String failureString = PartitionedRegionTestHelper.compareResultSets(resSetPR, resSetLocal);
Thread.sleep(delayQuery);
if (failureString != null) {
errorBuf.append(failureString);
throw (new Exception(failureString));
}
} catch (InterruptedException ie) {
fail("interrupted");
} catch (QueryInvocationTargetException qite) {
logger.info("PRQueryRegionDestroyedJUnitTest#testQueryOnSingleDataStore: QueryInvocationTargetException as Expected " + qite);
} catch (RegionDestroyedException rde) {
logger.info("PRQueryRegionDestroyedJUnitTest#testQueryOnSingleDataStore: RegionDestroyedException as Expected " + rde);
} catch (RegionNotFoundException rnfe) {
logger.info("PRQueryRegionDestroyedJUnitTest#testQueryOnSingleDataStore: RegionNotFoundException as Expected " + rnfe);
} catch (Exception qe) {
logger.info("PRQueryRegionDestroyedJUnitTest#testQueryOnSingleDataStore: Unexpected Exception " + qe);
encounteredException = true;
StringWriter sw = new StringWriter();
qe.printStackTrace(new PrintWriter(sw));
errorBuf.append(sw);
}
}
logger.info("<ExpectedException action=remove>" + expectedRegionDestroyedException + "</ExpectedException>");
}
});
logger.info("PRQueryRegionDestroyedJUnitTest#testQueryOnSingleDataStore: Creating a Thread which will call Region.destroyRegion() on the datastore ");
Thread t2 = new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(2500);
} catch (InterruptedException ie) {
logger.info("PRQueryRegionDestroyedJUnitTest#testQueryOnSingleDataStore:Thread Interrupted Exceptionduring region Destroy ");
fail("interrupted");
}
region.destroyRegion();
}
});
logger.info("PRQueryRegionDestroyedJUnitTest#testQueryOnSingleDataStore: Initiating the Threads");
t1.start();
t2.start();
logger.info("PRQueryRegionDestroyedJUnitTest#testQueryOnSingleDataStore: Waiting for the Threads to join ");
ThreadUtils.join(t1, 30 * 1000);
ThreadUtils.join(t2, 30 * 1000);
logger.info("PRQueryRegionDestroyedJUnitTest#testQueryOnSingleDataStore: checking for any Unexpected Exception's occurred");
assertFalse("PRQueryRegionDestroyedJUnitTest#testQueryOnSingleDataStore: Exception occurred in Query-thread", encounteredException);
} catch (Exception e) {
e.printStackTrace();
fail("PRQueryRegionDestroyedJUnitTest#testQueryOnSingleDataStore: Test failed because of exception " + e);
}
logger.info("PRQueryRegionDestroyedJUnitTest#testQueryOnSingleDataStore: Test Ended");
}
use of org.apache.geode.cache.query.RegionNotFoundException in project geode by apache.
the class CreateIndexFunction method execute.
@Override
public void execute(FunctionContext context) {
final IndexInfo indexInfo = (IndexInfo) context.getArguments();
String memberId = null;
try {
Cache cache = CacheFactory.getAnyInstance();
memberId = cache.getDistributedSystem().getDistributedMember().getId();
QueryService queryService = cache.getQueryService();
String indexName = indexInfo.getIndexName();
String indexedExpression = indexInfo.getIndexedExpression();
String fromClause = indexInfo.getRegionPath();
// Check to see if the region path contains an alias e.g "/region1 r1"
// Then the first string will be the regionPath
String[] regionPathTokens = fromClause.trim().split(" ");
String regionPath = regionPathTokens[0];
switch(indexInfo.getIndexType()) {
case IndexInfo.RANGE_INDEX:
queryService.createIndex(indexName, indexedExpression, fromClause);
break;
case IndexInfo.KEY_INDEX:
queryService.createKeyIndex(indexName, indexedExpression, fromClause);
break;
case IndexInfo.HASH_INDEX:
queryService.createHashIndex(indexName, indexedExpression, fromClause);
break;
default:
queryService.createIndex(indexName, indexedExpression, fromClause);
}
regionPath = getValidRegionName(cache, regionPath);
setResultInSender(context, indexInfo, memberId, cache, regionPath);
} catch (IndexExistsException e) {
String message = CliStrings.format(CliStrings.CREATE_INDEX__INDEX__EXISTS, indexInfo.getIndexName());
context.getResultSender().lastResult(new CliFunctionResult(memberId, false, message));
} catch (IndexNameConflictException e) {
String message = CliStrings.format(CliStrings.CREATE_INDEX__NAME__CONFLICT, indexInfo.getIndexName());
context.getResultSender().lastResult(new CliFunctionResult(memberId, false, message));
} catch (RegionNotFoundException e) {
String message = CliStrings.format(CliStrings.CREATE_INDEX__INVALID__REGIONPATH, indexInfo.getRegionPath());
context.getResultSender().lastResult(new CliFunctionResult(memberId, false, message));
} catch (IndexInvalidException e) {
context.getResultSender().lastResult(new CliFunctionResult(memberId, e, e.getMessage()));
} catch (Exception e) {
String exceptionMessage = CliStrings.format(CliStrings.EXCEPTION_CLASS_AND_MESSAGE, e.getClass().getName(), e.getMessage());
context.getResultSender().lastResult(new CliFunctionResult(memberId, e, e.getMessage()));
}
}
use of org.apache.geode.cache.query.RegionNotFoundException in project geode by apache.
the class OrderByPartitionedDUnitTest method createIndex.
private void createIndex(VM vm, final String indexName, IndexType indexType, final String indexedExpression, final String fromClause) {
int indxTypeCode = -1;
if (indexType.equals(IndexType.FUNCTIONAL)) {
indxTypeCode = 0;
} else if (indexType.equals(IndexType.PRIMARY_KEY)) {
indxTypeCode = 1;
} else if (indexType.equals(IndexType.HASH)) {
indxTypeCode = 2;
}
final int finalIndxTypeCode = indxTypeCode;
vm.invoke(new SerializableRunnable("create index") {
public void run() {
try {
Cache cache = getCache();
IndexType indxType = null;
if (finalIndxTypeCode == 0) {
indxType = IndexType.FUNCTIONAL;
} else if (finalIndxTypeCode == 1) {
indxType = IndexType.PRIMARY_KEY;
} else if (finalIndxTypeCode == 2) {
indxType = IndexType.HASH;
}
cache.getQueryService().createIndex(indexName, indxType, indexedExpression, fromClause);
} catch (RegionNotFoundException e) {
fail(e.toString());
} catch (IndexExistsException e) {
fail(e.toString());
} catch (IndexNameConflictException e) {
fail(e.toString());
}
}
});
}
use of org.apache.geode.cache.query.RegionNotFoundException 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());
}
Aggregations