com.gemstone.gemfire.cache.query.internal.ExecutionContextTest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gemfire-junit Show documentation
Show all versions of gemfire-junit Show documentation
SnappyData store based off Pivotal GemFireXD
/*
* Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License. You
* may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License. See accompanying
* LICENSE file.
*/
/*
* Created on Oct 13, 2005
*
*
*/
package com.gemstone.gemfire.cache.query.internal;
import java.util.ArrayList;
import java.util.concurrent.*;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import com.gemstone.gemfire.SystemFailure;
import com.gemstone.gemfire.cache.query.CacheUtils;
import com.gemstone.gemfire.cache.query.IndexType;
import com.gemstone.gemfire.cache.query.SelectResults;
import junit.framework.TestCase;
import com.gemstone.gemfire.cache.query.data.*;
import io.snappydata.test.dunit.DistributedTestBase;
/**
* @author Asif
*
*
*/
public class ExecutionContextTest extends TestCase {
boolean failure = false;
String exceptionStr ="";
public ExecutionContextTest(String testName) {
super(testName);
}
protected void setUp() throws Exception {
this.failure = false;
CacheUtils.startCache();
}
protected void tearDown() throws Exception {
CacheUtils.closeCache();
}
public void testFunctionaAddToIndependentRuntimeItrMapWithoutIndex() {
CacheUtils.createRegion("portfolio", Portfolio.class);
// compileFromClause returns a List
QCompiler compiler = new QCompiler(CacheUtils.getLogger().convertToLogWriterI18n());
List list = compiler.compileFromClause("/portfolio p, p.positions");
ExecutionContext context = new QueryExecutionContext(null, CacheUtils.getCache());
context.newScope(context.assosciateScopeID());
try {
Iterator iter = list.iterator();
while (iter.hasNext()) {
CompiledIteratorDef iterDef = (CompiledIteratorDef) iter.next();
context.addDependencies(new CompiledID("dummy"), iterDef
.computeDependencies(context));
RuntimeIterator rIter = iterDef.getRuntimeIterator(context);
context.bindIterator(rIter);
context.addToIndependentRuntimeItrMap(iterDef);
assertTrue(
" The index_interanal_id is not set as per expectation of iter'n'",
rIter.getIndexInternalID().equals(rIter.getInternalId()));
}
}
catch (Exception e) {
e.printStackTrace();
fail("Test failed due to Exception = " + e);
}
}
public void testFunctionaAddToIndependentRuntimeItrMapWithIndex() {
try {
CacheUtils.createRegion("portfolio", Portfolio.class);
DefaultQueryService qs = new DefaultQueryService(CacheUtils.getCache());
qs.createIndex("myindex", IndexType.FUNCTIONAL, "pf.id",
"/portfolio pf, pf.positions pos");
// compileFromClause returns a List
QCompiler compiler = new QCompiler(CacheUtils.getLogger().convertToLogWriterI18n());
List list = compiler.compileFromClause("/portfolio p, p.positions");
ExecutionContext context = new QueryExecutionContext(null, CacheUtils
.getCache());
context.newScope(context.assosciateScopeID());
Iterator iter = list.iterator();
int i = 0;
while (iter.hasNext()) {
CompiledIteratorDef iterDef = (CompiledIteratorDef) iter.next();
++i;
context.addDependencies(new CompiledID("dummy"), iterDef
.computeDependencies(context));
RuntimeIterator rIter = iterDef.getRuntimeIterator(context);
context.addToIndependentRuntimeItrMap(iterDef);
context.bindIterator(rIter);
assertTrue(
" The index_interanal_id is not set as per expectation of index_iter'n'",
rIter.getIndexInternalID().equals("index_iter" + i));
}
}
catch (Exception e) {
fail("Test failed sue to Exception = " + e);
}
}
public void testObtainingRegionPath() {
try {
CacheUtils.createRegion("portfolio", Portfolio.class);
DefaultQueryService qs = new DefaultQueryService(CacheUtils.getCache());
qs.createIndex("myindex", IndexType.FUNCTIONAL, "pf.id",
"/portfolio pf, pf.positions pos");
// compileFromClause returns a List
QCompiler compiler = new QCompiler(CacheUtils.getLogger().convertToLogWriterI18n());
List list = compiler.compileFromClause("/portfolio p, p.positions");
ExecutionContext context = new QueryExecutionContext(null, CacheUtils
.getCache());
context.newScope(context.assosciateScopeID());
Iterator iter = list.iterator();
int i = 0;
CompiledIteratorDef iterDef = null;
while (iter.hasNext()) {
iterDef = (CompiledIteratorDef) iter.next();
++i;
context.addDependencies(new CompiledID("dummy"), iterDef
.computeDependencies(context));
RuntimeIterator rIter = iterDef.getRuntimeIterator(context);
context.addToIndependentRuntimeItrMap(iterDef);
context.bindIterator(rIter);
assertTrue(
" The index_interanal_id is not set as per expectation of index_iter'n'",
rIter.getIndexInternalID().equals("index_iter" + i));
}
Set temp = new HashSet();
context.computeUtlimateDependencies(iterDef, temp);
String regionPath = context
.getRegionPathForIndependentRuntimeIterator((RuntimeIterator) temp
.iterator().next());
if (!(regionPath != null && regionPath.equals("/portfolio"))) {
fail(" Region path is either null or not equal to /portfolio. The regionpath obtained = "
+ regionPath);
}
System.out
.println(" ***********The Region Path obatined = " + regionPath);
}
catch (Exception e) {
fail("Test failed sue to Exception = " + e);
}
}
public void testCurrScopeDpndntItrsBasedOnSingleIndpndntItr() {
CacheUtils.createRegion("portfolio", Portfolio.class);
CacheUtils.createRegion("dummy", null);
// compileFromClause returns a List
QCompiler compiler = new QCompiler(CacheUtils.getLogger().convertToLogWriterI18n());
List list = compiler
.compileFromClause("/portfolio p, p.positions, p.addreses addrs, addrs.collection1 coll1, /dummy d1, d1.collection2 d2");
RuntimeIterator indItr = null;
ExecutionContext context = new QueryExecutionContext(null, CacheUtils.getCache());
context.newScope(context.assosciateScopeID());
int i = 0;
List checkList = new ArrayList();
try {
Iterator iter = list.iterator();
while (iter.hasNext()) {
CompiledIteratorDef iterDef = (CompiledIteratorDef) iter.next();
context.addDependencies(new CompiledID("test"), iterDef
.computeDependencies(context));
RuntimeIterator rIter = iterDef.getRuntimeIterator(context);
if (i == 0) {
indItr = rIter;
checkList.add(rIter);
}
else {
if (i < 4) {
checkList.add(rIter);
}
}
++i;
context.bindIterator(rIter);
context.addToIndependentRuntimeItrMap(iterDef);
assertTrue(
" The index_interanal_id is not set as per expectation of iter'n'",
rIter.getIndexInternalID().equals(rIter.getInternalId()));
}
List list1 = context
.getCurrScopeDpndntItrsBasedOnSingleIndpndntItr(indItr);
if (list1.size() != 4) {
fail("The dependency set returned incorrect resul with size ="
+ list1.size());
}
for (int j = 0; j < 4; ++j) {
assertEquals(list1.get(j), checkList.get(j));
}
}
catch (Exception e) {
e.printStackTrace();
fail("Test failed due to Exception = " + e);
}
}
public void testScopeIndex1() {
CacheUtils.createRegion("portfolios", Portfolio.class);
CacheUtils.createRegion("positions", Position.class);
// compileFromClause returns a List
String qry = "select distinct p.pf, ELEMENT(select distinct pf1 from /portfolios pf1 where pf1.getID = p.pf.getID ) from (select distinct pf, pos from /portfolios pf, pf.positions.values pos) p, (select distinct * from /positions rtPos where rtPos.secId = p.pos.secId) as y " +
"where ( select distinct pf2 from /portfolios pf2 ).size() <> 0 ";
ExecutionContext context = new QueryExecutionContext(null, CacheUtils.getCache());
QCompiler compiler = new QCompiler(CacheUtils.getLogger().convertToLogWriterI18n());
CompiledValue query =compiler.compileQuery(qry);
helperComputeDependencyPhase(context,query);
Set runtimeItrs = context.getDependencySet(query, true);
Iterator itr = runtimeItrs.iterator();
while(itr.hasNext()) {
RuntimeIterator rItr = (RuntimeIterator)itr.next();
if(rItr.getName().equals("p")) {
assertTrue( "The scopeID of outer iterator is not 1",rItr.getScopeID() ==1 );
}else if(rItr.getName().equals("pf")) {
assertTrue( "The scopeID of first inner level iterator is not 2",rItr.getScopeID() ==2 );
}else if(rItr.getName().equals("pos")) {
assertTrue( "The scopeID of first inner level iterator is not 2",rItr.getScopeID() ==2 );
}else if(rItr.getName().equals("rtPos")) {
assertTrue( "The scopeID of second inner level iterator is not 3",rItr.getScopeID() ==3 );
}else if(rItr.getName().equals("y")) {
assertTrue( "The scopeID of outer level iterator is not 1",rItr.getScopeID() ==1 );
}else if(rItr.getName().equals("pf1")) {
assertTrue( "The scopeID of inner level iterator is not 5",rItr.getScopeID() ==5 );
}else if(rItr.getName().equals("pf2")) {
assertTrue( "The scopeID of inner level iterator is not 4",rItr.getScopeID() ==4 );
}else {
fail ("No such iterator with name = "+ rItr.getName() + "should be available");
}
}
helperEvaluateQuery(context, query);
runtimeItrs = context.getDependencySet(query, true);
itr = runtimeItrs.iterator();
while(itr.hasNext()) {
RuntimeIterator rItr = (RuntimeIterator)itr.next();
if(rItr.getName().equals("p")) {
assertTrue( "The scopeID of outer iterator is not 1",rItr.getScopeID() ==1 );
}else if(rItr.getName().equals("pf")) {
assertTrue( "The scopeID of first inner level iterator is not 2",rItr.getScopeID() ==2 );
}else if(rItr.getName().equals("pos")) {
assertTrue( "The scopeID of first inner level iterator is not 2",rItr.getScopeID() ==2 );
}else if(rItr.getName().equals("rtPos")) {
assertTrue( "The scopeID of second inner level iterator is not 3",rItr.getScopeID() ==3 );
}else if(rItr.getName().equals("y")) {
assertTrue( "The scopeID of outer level iterator is not 1",rItr.getScopeID() ==1 );
}else if(rItr.getName().equals("pf1")) {
assertTrue( "The scopeID of inner level iterator is not 5",rItr.getScopeID() ==5 );
}else if(rItr.getName().equals("pf2")) {
assertTrue( "The scopeID of inner level iterator is not 4",rItr.getScopeID() ==4 );
}else {
fail ("No such iterator with name = "+ rItr.getName() + "should be available");
}
}
}
public void testMultiThreadedScopeIndex() {
CacheUtils.createRegion("portfolios", Portfolio.class);
CacheUtils.createRegion("positions", Position.class);
// compileFromClause returns a List
String qry = "select distinct p.pf from (select distinct pf, pos from /portfolios pf, pf.positions.values pos) p, (select distinct * from /positions rtPos where rtPos.secId = p.pos.secId) as y";
final int TOATL_THREADS = 80;
final CountDownLatch latch = new CountDownLatch(TOATL_THREADS);
QCompiler compiler = new QCompiler(CacheUtils.getLogger().convertToLogWriterI18n());
final CompiledValue query =compiler.compileQuery(qry);
Runnable runnable = new Runnable() {
public void run() {
try {
latch.countDown();
latch.await();
ExecutionContext context = new QueryExecutionContext(null, CacheUtils.getCache());
helperComputeDependencyPhase(context,query);
Set runtimeItrs = context.getDependencySet(query, true);
Iterator itr = runtimeItrs.iterator();
while(itr.hasNext()) {
RuntimeIterator rItr = (RuntimeIterator)itr.next();
if(rItr.getName().equals("p")) {
assertTrue( "The scopeID of outer iterator is not 1",rItr.getScopeID() ==1 );
}else if(rItr.getName().equals("pf")) {
assertTrue( "The scopeID of first inner level iterator is not 2",rItr.getScopeID() ==2 );
}else if(rItr.getName().equals("pos")) {
assertTrue( "The scopeID of first inner level iterator is not 2",rItr.getScopeID() ==2 );
}else if(rItr.getName().equals("rtPos")) {
assertTrue( "The scopeID of second inner level iterator is not 3",rItr.getScopeID() ==3 );
}else if(rItr.getName().equals("y")) {
assertTrue( "The scopeID of outer level iterator is not 1",rItr.getScopeID() ==1 );
}else {
fail ("No such iterator with name = "+ rItr.getName() + "should be available");
}
Thread.yield();
}
helperEvaluateQuery(context, query);
runtimeItrs = context.getDependencySet(query, true);
itr = runtimeItrs.iterator();
while(itr.hasNext()) {
RuntimeIterator rItr = (RuntimeIterator)itr.next();
if(rItr.getName().equals("p")) {
assertTrue( "The scopeID of outer iterator is not 1",rItr.getScopeID() ==1 );
}else if(rItr.getName().equals("pf")) {
assertTrue( "The scopeID of first inner level iterator is not 2",rItr.getScopeID() ==2 );
}else if(rItr.getName().equals("pos")) {
assertTrue( "The scopeID of first inner level iterator is not 2",rItr.getScopeID() ==2 );
}else if(rItr.getName().equals("rtPos")) {
assertTrue( "The scopeID of second inner level iterator is not 3",rItr.getScopeID() ==3 );
}else if(rItr.getName().equals("y")) {
assertTrue( "The scopeID of outer level iterator is not 1",rItr.getScopeID() ==1 );
}else {
fail ("No such iterator with name = "+ rItr.getName() + "should be available");
}
}
}
catch (VirtualMachineError e) {
SystemFailure.initiateFailure(e);
throw e;
}
catch(Throwable th) {
exceptionStr = th.toString();
failure = true;
}
}
};
Thread th[] = new Thread[TOATL_THREADS];
for (int i =0; i < th.length ;++i) {
th[i] = new Thread( runnable);
}
for (int i =0; i < th.length ;++i) {
th[i].start();
}
for (int i =0; i < th.length ;++i) {
try {
DistributedTestBase.join(th[i], 30 * 1000, null);
}catch(Exception e) {
fail(e.toString());
}
}
if(failure) {
fail(exceptionStr);
}
}
protected void helperComputeDependencyPhase(ExecutionContext context, CompiledValue query) {
try {
query.computeDependencies(context);
}
catch (Exception e) {
e.printStackTrace();
fail("Test failed due to Exception = " + e);
}
}
protected SelectResults helperEvaluateQuery(ExecutionContext context, CompiledValue query) {
SelectResults rs = null;
try {
rs= (SelectResults)query.evaluate(context);
}
catch (Exception e) {
e.printStackTrace();
fail("Test failed due to Exception = " + e);
}
return rs;
}
}