Search in sources :

Example 56 with QueryService

use of org.apache.geode.cache.query.QueryService in project geode by apache.

the class QueryUsingPoolDUnitTest method executeQueries.

private void executeQueries(String regionName, String poolName1) {
    String queryString;
    SelectResults results = null;
    QueryService qService = null;
    try {
        qService = (PoolManager.find(poolName1)).getQueryService();
    } catch (Exception e) {
        Assert.fail("Failed to get QueryService.", e);
    }
    queryString = "SELECT DISTINCT itr.value FROM " + regionName + ".entries itr where itr.key = 'key-1'";
    try {
        Query query = qService.newQuery(queryString);
        results = (SelectResults) query.execute();
    } catch (Exception e) {
        Assert.fail("Failed executing " + queryString, e);
    }
    assertEquals(1, results.size());
    assertTrue(!results.getCollectionType().allowsDuplicates() && !results.getCollectionType().getElementType().isStructType());
    assertTrue(results.asList().get(0) instanceof TestObject);
    queryString = "SELECT DISTINCT itr.key FROM " + regionName + ".entries itr where itr.key = 'key-1'";
    try {
        Query query = qService.newQuery(queryString);
        results = (SelectResults) query.execute();
    } catch (Exception e) {
        Assert.fail("Failed executing " + queryString, e);
    }
    assertEquals(1, results.size());
    assertTrue(!results.getCollectionType().allowsDuplicates() && !results.getCollectionType().getElementType().isStructType());
    assertEquals("key-1", results.asList().get(0));
}
Also used : SelectResults(org.apache.geode.cache.query.SelectResults) Query(org.apache.geode.cache.query.Query) DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) QueryService(org.apache.geode.cache.query.QueryService) IOException(java.io.IOException) CacheException(org.apache.geode.cache.CacheException) IgnoredException(org.apache.geode.test.dunit.IgnoredException)

Example 57 with QueryService

use of org.apache.geode.cache.query.QueryService in project geode by apache.

the class QueryUsingPoolDUnitTest method testRemoteJoinRegionQueries.

/**
   * Tests remote join query execution.
   */
@Test
public void testRemoteJoinRegionQueries() throws CacheException {
    final String name = this.getName();
    final String rootRegionName = "root";
    final Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    final int numberOfEntries = 100;
    // Start server
    vm0.invoke("Create Bridge Server", () -> {
        createAndStartBridgeServer();
        AttributesFactory factory = new AttributesFactory();
        factory.setScope(Scope.LOCAL);
        createRegion(name + "1", factory.create());
        createRegion(name + "2", factory.create());
    });
    // Initialize server region
    vm0.invoke("Create Bridge Server", () -> {
        Region region1 = getRootRegion().getSubregion(name + "1");
        for (int i = 0; i < numberOfEntries; i++) {
            region1.put("key-" + i, new TestObject(i, "ibm"));
        }
        Region region2 = getRootRegion().getSubregion(name + "2");
        for (int i = 0; i < numberOfEntries; i++) {
            region2.put("key-" + i, new TestObject(i, "ibm"));
        }
    });
    // Create client region
    final int port = vm0.invoke("getCacheServerPort", () -> QueryUsingPoolDUnitTest.getCacheServerPort());
    final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
    final String regionName1 = "/" + rootRegionName + "/" + name + "1";
    final String regionName2 = "/" + rootRegionName + "/" + name + "2";
    // Create client pool.
    final String poolName = "testRemoteJoinRegionQueries";
    vm1.invoke("createPool", () -> createPool(poolName, host0, port));
    // Execute client queries
    vm1.invoke("Execute queries", () -> {
        String queryString = null;
        SelectResults results = null;
        QueryService qService = null;
        try {
            qService = (PoolManager.find(poolName)).getQueryService();
        } catch (Exception e) {
            Assert.fail("Failed to get QueryService.", e);
        }
        queryString = "select distinct a, b.price from " + regionName1 + " a, " + regionName2 + " b where a.price = b.price";
        try {
            Query query = qService.newQuery(queryString);
            results = (SelectResults) query.execute();
        } catch (Exception e) {
            Assert.fail("Failed executing " + queryString, e);
        }
        assertEquals(numberOfEntries, results.size());
        assertTrue(!results.getCollectionType().allowsDuplicates() && results.getCollectionType().getElementType().isStructType());
        queryString = "select distinct a, b.price from " + regionName1 + " a, " + regionName2 + " b where a.price = b.price and a.price = 50";
        try {
            Query query = qService.newQuery(queryString);
            results = (SelectResults) query.execute();
        } catch (Exception e) {
            Assert.fail("Failed executing " + queryString, e);
        }
        assertEquals(1, results.size());
        assertTrue(!results.getCollectionType().allowsDuplicates() && results.getCollectionType().getElementType().isStructType());
    });
    // Stop server
    vm0.invoke("Stop CacheServer", () -> stopBridgeServer(getCache()));
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) SelectResults(org.apache.geode.cache.query.SelectResults) Query(org.apache.geode.cache.query.Query) DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) QueryService(org.apache.geode.cache.query.QueryService) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) Host(org.apache.geode.test.dunit.Host) IOException(java.io.IOException) CacheException(org.apache.geode.cache.CacheException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 58 with QueryService

use of org.apache.geode.cache.query.QueryService in project geode by apache.

the class QueryUsingPoolDUnitTest method testClientServerCompiledQueryCleanup.

/**
   * Tests client-server compiled query register and cleanup. It creates the client connections
   * without the subscription enabled. This doesn't create any client proxy on the server.
   */
@Test
public void testClientServerCompiledQueryCleanup() throws CacheException {
    final String name = this.getName();
    final Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    VM vm2 = host.getVM(2);
    final int numberOfEntries = 100;
    final Object[][] params = new Object[][] { // 0
    { "key-1" }, // 1
    { 101 }, // 2
    { 101 }, // 3
    { 101 }, // 4
    { 50, "ibm" }, // 5
    { 50, "ibm" } };
    // Start server
    final int port = vm0.invoke("Create Bridge Server", () -> {
        setupBridgeServerAndCreateData(name, numberOfEntries);
        QueryService queryService = getCache().getQueryService();
        queryService.newQuery("Select * from " + regName);
        DefaultQuery.setTestCompiledQueryClearTime(2 * 1000);
        return getCacheServerPort();
    });
    final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
    // Create client pool.
    final String poolName = "testClientServerQueriesWithParams";
    final boolean subscriptiuonEnabled = false;
    vm1.invoke("createPool", () -> createPool(poolName, host0, port, subscriptiuonEnabled));
    vm2.invoke("createPool", () -> createPool(poolName, host0, port, subscriptiuonEnabled));
    // Execute client queries
    vm1.invoke("Execute queries", () -> executeCompiledQueries(poolName, params));
    // Execute client queries
    vm2.invoke("Execute queries", () -> executeCompiledQueries(poolName, params));
    // Validate maintained compiled queries.
    vm0.invoke("validate Compiled query", () -> validateCompiledQuery(0));
    // Recreate compiled queries.
    // Execute client queries
    vm1.invoke("Execute queries", () -> executeCompiledQueries(poolName, params));
    // Execute client queries
    // The client2 will be using the queries.
    vm2.invokeAsync("Execute queries", () -> {
        for (int i = 0; i < 10; i++) {
            Wait.pause(10);
            executeCompiledQueries(poolName, params);
        }
    });
    // Validate maintained compiled queries.
    vm0.invoke("validate Compiled query", () -> validateCompiledQuery(queryString.length));
    // Close clients
    // Let the compiled queries to be idle (not used).
    // pause(2 * 1000);
    // Validate maintained compiled queries.
    vm0.invoke("validate Compiled query", () -> validateCompiledQuery(0));
    // Recreate compiled queries.
    // Execute client queries
    vm1.invoke("Execute queries", () -> executeCompiledQueries(poolName, params));
    // Validate maintained compiled queries.
    vm0.invoke("validate compiled query.", () -> {
        long compiledQueryCount = CacheClientNotifier.getInstance().getStats().getCompiledQueryCount();
        assertEquals(queryString.length, compiledQueryCount);
    });
    // Close clients
    vm2.invoke("closeClient", () -> closeClient());
    vm1.invoke("closeClient", () -> closeClient());
    // Validate maintained compiled queries.
    // since not used it should get cleaned up after sometime.
    vm0.invoke("validate Compiled query", () -> validateCompiledQuery(0));
    // Stop server
    vm0.invoke("Stop CacheServer", () -> stopBridgeServer(getCache()));
}
Also used : QueryService(org.apache.geode.cache.query.QueryService) VM(org.apache.geode.test.dunit.VM) Host(org.apache.geode.test.dunit.Host) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 59 with QueryService

use of org.apache.geode.cache.query.QueryService in project geode by apache.

the class QueryUsingPoolDUnitTest method testUnSupportedOps.

@Test
public void testUnSupportedOps() throws Exception {
    final String name = this.getName();
    final String rootRegionName = "root";
    final Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    final int numberOfEntries = 100;
    // Start server
    vm0.invoke("Create Bridge Server", () -> {
        createAndStartBridgeServer();
        AttributesFactory factory = new AttributesFactory();
        factory.setScope(Scope.LOCAL);
        final Region region1 = createRegion(name, factory.createRegionAttributes());
        final Region region2 = createRegion(name + "_2", factory.createRegionAttributes());
        for (int i = 0; i < numberOfEntries; i++) {
            region1.put("key-" + i, new TestObject(i, "ibm"));
            region2.put("key-" + i, new TestObject(i, "ibm"));
        }
    });
    final int port = vm0.invoke("getCacheServerPort", () -> QueryUsingPoolDUnitTest.getCacheServerPort());
    final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
    final String regionName1 = "/" + rootRegionName + "/" + name;
    // Create client pool.
    final String poolName = "testUnSupportedOps";
    vm1.invoke("createPool", () -> createPool(poolName, host0, port));
    // Execute client queries in VM1
    vm1.invoke("Execute queries", () -> {
        final Region region1 = ((ClientCache) getCache()).createClientRegionFactory(ClientRegionShortcut.LOCAL).create(name);
        String queryString = "select distinct * from " + regionName1 + " where ticker = $1";
        Object[] params = new Object[] { new String("ibm") };
        // SelectResults results = null;
        QueryService qService = null;
        try {
            qService = (PoolManager.find(poolName)).getQueryService();
        } catch (Exception e) {
            Assert.fail("Failed to get QueryService.", e);
        }
        // Testing Remote Query with params.
        try {
            Query query = qService.newQuery(queryString);
            query.execute(params);
        } catch (UnsupportedOperationException e) {
        // Expected behavior.
        } catch (Exception e) {
            Assert.fail("Failed with UnExpected Exception.", e);
        }
        // Test with Index.
        try {
            qService.createIndex("test", IndexType.FUNCTIONAL, "ticker", regionName1);
        } catch (UnsupportedOperationException e) {
        // Expected behavior.
        } catch (Exception e) {
            Assert.fail("Failed with UnExpected Exception.", e);
        }
        try {
            String importString = "import org.apache.geode.admin.QueryUsingPoolDUnitTest.TestObject;";
            qService.createIndex("test", IndexType.FUNCTIONAL, "ticker", regionName1, importString);
        } catch (UnsupportedOperationException e) {
        // Expected behavior.
        } catch (Exception e) {
            Assert.fail("Failed with UnExpected Exception.", e);
        }
        try {
            qService.getIndex(region1, "test");
        } catch (UnsupportedOperationException e) {
        // Expected behavior.
        }
        try {
            qService.getIndexes(region1);
        } catch (UnsupportedOperationException e) {
        // Expected behavior.
        }
        try {
            qService.getIndexes(region1, IndexType.FUNCTIONAL);
        } catch (UnsupportedOperationException e) {
        // Expected behavior.
        }
        try {
            qService.removeIndexes(region1);
        } catch (UnsupportedOperationException e) {
        // Expected behavior.
        }
        try {
            qService.removeIndexes();
        } catch (UnsupportedOperationException e) {
        // Expected behavior.
        }
    });
    // Stop server
    vm0.invoke("Stop CacheServer", () -> {
        QueryObserverHolder.setInstance(new QueryObserverAdapter());
        stopBridgeServer(getCache());
    });
}
Also used : Query(org.apache.geode.cache.query.Query) DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) Host(org.apache.geode.test.dunit.Host) IOException(java.io.IOException) CacheException(org.apache.geode.cache.CacheException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) AttributesFactory(org.apache.geode.cache.AttributesFactory) QueryService(org.apache.geode.cache.query.QueryService) QueryObserverAdapter(org.apache.geode.cache.query.internal.QueryObserverAdapter) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 60 with QueryService

use of org.apache.geode.cache.query.QueryService 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);
    }
}
Also used : DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) Query(org.apache.geode.cache.query.Query) CacheException(org.apache.geode.cache.CacheException) TypeMismatchException(org.apache.geode.cache.query.TypeMismatchException) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) Host(org.apache.geode.test.dunit.Host) NameResolutionException(org.apache.geode.cache.query.NameResolutionException) FunctionDomainException(org.apache.geode.cache.query.FunctionDomainException) NameResolutionException(org.apache.geode.cache.query.NameResolutionException) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) QueryExecutionTimeoutException(org.apache.geode.cache.query.QueryExecutionTimeoutException) ServerOperationException(org.apache.geode.cache.client.ServerOperationException) IndexInvalidException(org.apache.geode.cache.query.IndexInvalidException) CacheException(org.apache.geode.cache.CacheException) QueryExecutionLowMemoryException(org.apache.geode.cache.query.QueryExecutionLowMemoryException) QueryException(org.apache.geode.cache.query.QueryException) TypeMismatchException(org.apache.geode.cache.query.TypeMismatchException) SelectResults(org.apache.geode.cache.query.SelectResults) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) QueryService(org.apache.geode.cache.query.QueryService) FunctionDomainException(org.apache.geode.cache.query.FunctionDomainException) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable)

Aggregations

QueryService (org.apache.geode.cache.query.QueryService)532 Test (org.junit.Test)383 SelectResults (org.apache.geode.cache.query.SelectResults)331 Region (org.apache.geode.cache.Region)316 Query (org.apache.geode.cache.query.Query)316 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)215 Portfolio (org.apache.geode.cache.query.data.Portfolio)199 CacheException (org.apache.geode.cache.CacheException)139 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)118 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)110 Index (org.apache.geode.cache.query.Index)109 Host (org.apache.geode.test.dunit.Host)104 VM (org.apache.geode.test.dunit.VM)104 Iterator (java.util.Iterator)102 Cache (org.apache.geode.cache.Cache)92 DefaultQuery (org.apache.geode.cache.query.internal.DefaultQuery)86 Struct (org.apache.geode.cache.query.Struct)70 RegionNotFoundException (org.apache.geode.cache.query.RegionNotFoundException)68 IOException (java.io.IOException)67 DefaultQueryService (org.apache.geode.cache.query.internal.DefaultQueryService)65