Search in sources :

Example 21 with CqQuery

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

the class CqServiceImpl method stopCqs.

@Override
public synchronized void stopCqs(Collection<? extends InternalCqQuery> cqs) throws CqException {
    final boolean isDebugEnabled = logger.isDebugEnabled();
    if (isDebugEnabled) {
        if (cqs == null) {
            logger.debug("CqService.stopCqs cqs : null");
        } else {
            logger.debug("CqService.stopCqs cqs : ({} queries)", cqs.size());
        }
    }
    if (cqs == null) {
        return;
    }
    String cqName = null;
    for (InternalCqQuery internalCqQuery : cqs) {
        CqQuery cq = internalCqQuery;
        if (!cq.isClosed() && cq.isRunning()) {
            try {
                cqName = cq.getName();
                cq.stop();
            } catch (QueryException | CqClosedException e) {
                if (isDebugEnabled) {
                    logger.debug("Failed to stop the CQ, CqName : {} Error : {}", cqName, e.getMessage());
                }
            }
        }
    }
}
Also used : QueryException(org.apache.geode.cache.query.QueryException) CqClosedException(org.apache.geode.cache.query.CqClosedException) CqQuery(org.apache.geode.cache.query.CqQuery)

Example 22 with CqQuery

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

the class CqDataDUnitTest method testMultipleExecuteWithInitialResults.

/**
   * This test was created to test executeWithInitialResults being called multiple times.
   * Previously, the queueEvents would be overwritten and we would lose data. This test will execute
   * the method twice. The first time, the first execution will block it's own child thread (TC1).
   * The second execution will block until TC1 is completed (based on how executeWithInitialResults
   * is implemented) A third thread will be awaken and release the latch in the testhook for TC1 to
   * complete.
   * 
   * @throws Exception
   */
@Test
public void testMultipleExecuteWithInitialResults() throws Exception {
    final int numObjects = 200;
    final int totalObjects = 500;
    final Host host = Host.getHost(0);
    VM server = host.getVM(0);
    VM client = host.getVM(1);
    client.invoke(setTestHook());
    final String cqName = "testMultiExecuteWithInitialResults";
    // initialize server and retreive host and port values
    cqDUnitTest.createServer(server);
    final int port = server.invoke(() -> CqQueryDUnitTest.getCacheServerPort());
    final String host0 = NetworkUtils.getServerHostName(server.getHost());
    // Initialize Client.
    cqDUnitTest.createClient(client, port, host0);
    // create CQ.
    cqDUnitTest.createCQ(client, cqName, cqDUnitTest.cqs[0]);
    // initialize Region.
    server.invoke(new CacheSerializableRunnable("Update Region") {

        public void run2() throws CacheException {
            Region region = getCache().getRegion("/root/" + cqDUnitTest.regions[0]);
            for (int i = 1; i <= numObjects; i++) {
                Portfolio p = new Portfolio(i);
                region.put("" + i, p);
            }
        }
    });
    // Keep updating region (async invocation).
    server.invokeAsync(new CacheSerializableRunnable("Update Region") {

        public void run2() throws CacheException {
            // Wait to give client a chance to register the cq
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
            Region region = getCache().getRegion("/root/" + cqDUnitTest.regions[0]);
            for (int i = numObjects + 1; i <= totalObjects; i++) {
                Portfolio p = new Portfolio(i);
                region.put("" + i, p);
            }
        }
    });
    // the thread that validates all results and executes first
    AsyncInvocation processCqs = client.invokeAsync(new CacheSerializableRunnable("Execute CQ first") {

        public void run2() throws CacheException {
            SelectResults cqResults = null;
            QueryService cqService = getCache().getQueryService();
            // Get CqQuery object.
            CqQuery cq1 = cqService.getCq(cqName);
            if (cq1 == null) {
                fail("Failed to get CQ " + cqName);
            }
            try {
                cqResults = cq1.executeWithInitialResults();
            } catch (Exception e) {
                AssertionError err = new AssertionError("Failed to execute  CQ " + cqName);
                err.initCause(e);
                throw err;
            }
            CqQueryTestListener cqListener = (CqQueryTestListener) cq1.getCqAttributes().getCqListener();
            // Wait for the last key to arrive.
            cqListener.waitForCreated("" + totalObjects);
            // Check if the events from CqListener are in order.
            int oldId = 0;
            for (Object cqEvent : cqListener.events.toArray()) {
                int newId = new Integer(cqEvent.toString()).intValue();
                if (oldId > newId) {
                    fail("Queued events for CQ Listener during execution with " + "Initial results is not in the order in which they are created.");
                }
                oldId = newId;
            }
            // Check if all the IDs are present as part of Select Results and CQ
            // Events.
            HashSet ids = new HashSet(cqListener.events);
            for (Object o : cqResults.asList()) {
                Struct s = (Struct) o;
                ids.add(s.get("key"));
            }
            HashSet missingIds = new HashSet();
            String key = "";
            for (int i = 1; i <= totalObjects; i++) {
                key = "" + i;
                if (!(ids.contains(key))) {
                    missingIds.add(key);
                }
            }
            if (!missingIds.isEmpty()) {
                fail("Missing Keys in either ResultSet or the Cq Event list. " + " Missing keys : [size : " + missingIds.size() + "]" + missingIds + " Ids in ResultSet and CQ Events :" + ids);
            }
        }
    });
    // the second call to executeWithInitialResults. Goes to sleep hopefully
    // long enough
    // for the first call to executeWithInitialResults first
    client.invokeAsync(new CacheSerializableRunnable("Execute CQ second") {

        public void run2() throws CacheException {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
            QueryService cqService = getCache().getQueryService();
            // Get CqQuery object.
            CqQuery cq1 = cqService.getCq(cqName);
            if (cq1 == null) {
                fail("Failed to get CQ " + cqName);
            }
            try {
                cq1.executeWithInitialResults();
            } catch (IllegalStateException e) {
            // we expect an error due to the cq having already being in run state
            } catch (Exception e) {
                AssertionError err = new AssertionError("test hook lock interrupted" + cqName);
                err.initCause(e);
                throw err;
            }
        }
    });
    // thread that unlatches the test hook, sleeping long enough for both
    // the other two threads to execute first
    client.invokeAsync(new CacheSerializableRunnable("Release latch") {

        public void run2() throws CacheException {
            // had a chance to invoke executeWithInitialResults
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                AssertionError err = new AssertionError("test hook lock interrupted" + cqName);
                err.initCause(e);
                throw err;
            }
            CqQueryImpl.testHook.ready();
        }
    });
    // wait for 60 seconds for test to complete
    ThreadUtils.join(processCqs, 60 * 1000);
    // Close.
    cqDUnitTest.closeClient(client);
    cqDUnitTest.closeServer(server);
}
Also used : CacheException(org.apache.geode.cache.CacheException) Portfolio(org.apache.geode.cache.query.data.Portfolio) Host(org.apache.geode.test.dunit.Host) AsyncInvocation(org.apache.geode.test.dunit.AsyncInvocation) CacheException(org.apache.geode.cache.CacheException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) Struct(org.apache.geode.cache.query.Struct) SelectResults(org.apache.geode.cache.query.SelectResults) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) QueryService(org.apache.geode.cache.query.QueryService) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) CqQuery(org.apache.geode.cache.query.CqQuery) HashSet(java.util.HashSet) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 23 with CqQuery

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

the class CqDataUsingPoolDUnitTest method testCqStatInitializationTimingIssue.

@Test
public void testCqStatInitializationTimingIssue() {
    disconnectAllFromDS();
    // The async close can cause this exception on the server
    IgnoredException.addIgnoredException("java.net.SocketException: Broken pipe");
    final String regionName = "testCqStatInitializationTimingIssue";
    final String cq1Name = "testCq1";
    final Host host = Host.getHost(0);
    VM server = host.getVM(0);
    VM client = host.getVM(1);
    VM client2 = host.getVM(2);
    // Start server 1
    final int server1Port = ((Integer) server.invoke(() -> CacheServerTestUtil.createCacheServer(regionName, new Boolean(true)))).intValue();
    // Start a client
    client.invoke(() -> CacheServerTestUtil.createCacheClient(getClientPool(NetworkUtils.getServerHostName(client.getHost()), server1Port), regionName));
    // Start a pub client
    client2.invoke(() -> CacheServerTestUtil.createCacheClient(getClientPool(NetworkUtils.getServerHostName(client2.getHost()), server1Port), regionName));
    // client has thread that invokes new and remove cq over and over
    client.invokeAsync(new CacheSerializableRunnable("Register cq") {

        @Override
        public void run2() throws CacheException {
            for (int i = 0; i < 10000; i++) {
                CqQuery query = createCq(regionName, cq1Name);
                if (query != null) {
                    try {
                        query.close();
                    } catch (Exception e) {
                        fail("exception while closing cq:", e);
                    }
                }
            }
        }
    });
    client2.invokeAsync(new CacheSerializableRunnable("pub updates") {

        @Override
        public void run2() throws CacheException {
            Region region = CacheServerTestUtil.getCache().getRegion(regionName);
            while (true) {
                for (int i = 0; i < 50000; i++) {
                    region.put("" + i, "" + Math.random());
                }
            }
        }
    });
    server.invokeAsync(new CacheSerializableRunnable("pub updates") {

        @Override
        public void run2() throws CacheException {
            Region region = CacheServerTestUtil.getCache().getRegion(regionName);
            while (true) {
                for (int i = 0; i < 50000; i++) {
                    region.put("" + i, "" + Math.random());
                }
            }
        }
    });
    // client has another thread that retrieves cq map and checks stat over and over
    client.invoke(new CacheSerializableRunnable("Check Stats") {

        @Override
        public void run2() throws CacheException {
            for (int i = 0; i < 10000; i++) {
                checkCqStats(cq1Name);
            }
        }
    });
    client.invoke(() -> CacheServerTestUtil.closeCache());
    client2.invoke(() -> CacheServerTestUtil.closeCache());
    server.invoke(() -> CacheServerTestUtil.closeCache());
}
Also used : CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) CacheException(org.apache.geode.cache.CacheException) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) Host(org.apache.geode.test.dunit.Host) CqQuery(org.apache.geode.cache.query.CqQuery) CqExistsException(org.apache.geode.cache.query.CqExistsException) CqException(org.apache.geode.cache.query.CqException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) CacheException(org.apache.geode.cache.CacheException) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) Test(org.junit.Test)

Example 24 with CqQuery

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

the class CqDataUsingPoolDUnitTest method createCq.

private CqQuery createCq(String regionName, String cqName) {
    // Create CQ Attributes.
    CqAttributesFactory cqAf = new CqAttributesFactory();
    // Initialize and set CqListener.
    CqListener[] cqListeners = { new CqListener() {

        @Override
        public void close() {
        }

        @Override
        public void onEvent(CqEvent aCqEvent) {
        }

        @Override
        public void onError(CqEvent aCqEvent) {
        }
    } };
    cqAf.initCqListeners(cqListeners);
    CqAttributes cqa = cqAf.create();
    // Create cq's
    // Get the query service for the Pool
    QueryService queryService = CacheServerTestUtil.getCache().getQueryService();
    CqQuery query = null;
    try {
        query = queryService.newCq(cqName, "Select * from /" + regionName, cqa);
        query.execute();
    } catch (CqExistsException e) {
        fail("Could not find specified region:" + regionName + ":", e);
    } catch (CqException e) {
        fail("Could not find specified region:" + regionName + ":", e);
    } catch (RegionNotFoundException e) {
        fail("Could not find specified region:" + regionName + ":", e);
    }
    return query;
}
Also used : CqEvent(org.apache.geode.cache.query.CqEvent) CqAttributes(org.apache.geode.cache.query.CqAttributes) QueryService(org.apache.geode.cache.query.QueryService) CqListener(org.apache.geode.cache.query.CqListener) CqException(org.apache.geode.cache.query.CqException) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) CqExistsException(org.apache.geode.cache.query.CqExistsException) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) CqQuery(org.apache.geode.cache.query.CqQuery)

Example 25 with CqQuery

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

the class CqPerfDUnitTest method testCQPerf.

/**
   * Tests the cq performance.
   * 
   * @throws Exception
   */
@Ignore("perf")
@Test
public void testCQPerf() throws Exception {
    final Host host = Host.getHost(0);
    VM server = host.getVM(0);
    VM client = host.getVM(1);
    cqDUnitTest.createServer(server);
    final int port = server.invoke(() -> CqQueryDUnitTest.getCacheServerPort());
    final String host0 = NetworkUtils.getServerHostName(server.getHost());
    // Create client.
    cqDUnitTest.createClient(client, port, host0);
    final String cqName = "testCQPerf_0";
    client.invoke(new CacheSerializableRunnable("Create CQ :" + cqName) {

        public void run2() throws CacheException {
            LogWriterUtils.getLogWriter().info("### Create CQ. ###" + cqName);
            // Get CQ Service.
            QueryService cqService = null;
            try {
                cqService = getCache().getQueryService();
            } catch (Exception cqe) {
                cqe.printStackTrace();
                fail("Failed to getCQService.");
            }
            // Create CQ Attributes.
            CqAttributesFactory cqf = new CqAttributesFactory();
            CqListener[] cqListeners = { new CqTimeTestListener(LogWriterUtils.getLogWriter()) };
            ((CqTimeTestListener) cqListeners[0]).cqName = cqName;
            cqf.initCqListeners(cqListeners);
            CqAttributes cqa = cqf.create();
            // Create and Execute CQ.
            try {
                CqQuery cq1 = cqService.newCq(cqName, cqDUnitTest.cqs[0], cqa);
                assertTrue("newCq() state mismatch", cq1.getState().isStopped());
                cq1.execute();
            } catch (Exception ex) {
                LogWriterUtils.getLogWriter().info("CqService is :" + cqService);
                ex.printStackTrace();
                AssertionError err = new AssertionError("Failed to create CQ " + cqName + " . ");
                err.initCause(ex);
                throw err;
            }
        }
    });
    final int size = 50;
    // Create values.
    cqDUnitTest.createValuesWithTime(client, cqDUnitTest.regions[0], size);
    Wait.pause(5000);
    // Update values
    cqDUnitTest.createValuesWithTime(client, cqDUnitTest.regions[0], size);
    client.invoke(new CacheSerializableRunnable("Validate CQs") {

        public void run2() throws CacheException {
            LogWriterUtils.getLogWriter().info("### Validating CQ. ### " + cqName);
            // Get CQ Service.
            QueryService cqService = null;
            try {
                cqService = getCache().getQueryService();
            } catch (Exception cqe) {
                cqe.printStackTrace();
                fail("Failed to getCqService.");
            }
            CqQuery cQuery = cqService.getCq(cqName);
            if (cQuery == null) {
                fail("Failed to get CqQuery for CQ : " + cqName);
            }
        // CqAttributes cqAttr = cQuery.getCqAttributes();
        // CqListener cqListeners[] = cqAttr.getCqListeners();
        // CqTimeTestListener listener = (CqTimeTestListener) cqListeners[0];
        // Wait for all the create to arrive.
        // for (int i=1; i <= size; i++) {
        // listener.waitForCreated(cqDUnitTest.KEY+i);
        // }
        // Wait for all the update to arrive.
        // for (int i=1; i <= size; i++) {
        // listener.waitForUpdated(cqDUnitTest.KEY+i);
        // }
        // getLogWriter().info("### Time taken for Creation of " + size + " events is :" +
        // listener.getTotalQueryCreateTime());
        // getLogWriter().info("### Time taken for Update of " + size + " events is :" +
        // listener.getTotalQueryUpdateTime());
        }
    });
    Wait.pause(10 * 60 * 1000);
    // Close.
    cqDUnitTest.closeClient(client);
    cqDUnitTest.closeServer(server);
}
Also used : CacheException(org.apache.geode.cache.CacheException) Host(org.apache.geode.test.dunit.Host) CacheException(org.apache.geode.cache.CacheException) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) DefaultQueryService(org.apache.geode.cache.query.internal.DefaultQueryService) QueryService(org.apache.geode.cache.query.QueryService) CqAttributes(org.apache.geode.cache.query.CqAttributes) VM(org.apache.geode.test.dunit.VM) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) InternalCqQuery(org.apache.geode.cache.query.internal.cq.InternalCqQuery) CqQuery(org.apache.geode.cache.query.CqQuery) Ignore(org.junit.Ignore) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Aggregations

CqQuery (org.apache.geode.cache.query.CqQuery)64 QueryService (org.apache.geode.cache.query.QueryService)49 CacheException (org.apache.geode.cache.CacheException)38 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)36 CqAttributes (org.apache.geode.cache.query.CqAttributes)35 CqAttributesFactory (org.apache.geode.cache.query.CqAttributesFactory)31 IOException (java.io.IOException)27 CqExistsException (org.apache.geode.cache.query.CqExistsException)26 DefaultQueryService (org.apache.geode.cache.query.internal.DefaultQueryService)24 RegionNotFoundException (org.apache.geode.cache.query.RegionNotFoundException)22 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)21 Test (org.junit.Test)21 CqClosedException (org.apache.geode.cache.query.CqClosedException)20 Region (org.apache.geode.cache.Region)17 SelectResults (org.apache.geode.cache.query.SelectResults)17 Host (org.apache.geode.test.dunit.Host)14 VM (org.apache.geode.test.dunit.VM)14 CqListener (org.apache.geode.cache.query.CqListener)12 Struct (org.apache.geode.cache.query.Struct)11 CqEvent (org.apache.geode.cache.query.CqEvent)10