Search in sources :

Example 36 with CqQuery

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

the class DeltaToRegionRelationCQRegistrationDUnitTest method registerCqThroughPool.

/*
   * register cq from pool
   */
public static void registerCqThroughPool(String name, String Cquery, Boolean cqWithIR) {
    QueryService cqService = null;
    // get cq service
    try {
        cqService = p.getQueryService();
    } catch (Exception cqe) {
        cqe.printStackTrace();
        fail("Failed to getCqService.");
    }
    // Create CQ Attributes.
    // do not attach any listiner lets see its response
    CqAttributesFactory cqf = new CqAttributesFactory();
    CqAttributes cqa = cqf.create();
    // Create and Execute CQ.
    try {
        CqQuery cq1 = cqService.newCq(name, Cquery, cqa);
        assertTrue("newCq() state mismatch", cq1.getState().isStopped());
        if (cqWithIR)
            cq1.executeWithInitialResults();
        else
            cq1.execute();
    } catch (Exception ex) {
        fail("Failed to create CQ " + cqName1, ex);
    }
    CqQuery cQuery = cqService.getCq(name);
    if (cQuery == null) {
        fail("Failed to get CqQuery for CQ : " + cqName1);
    }
}
Also used : QueryService(org.apache.geode.cache.query.QueryService) CqAttributes(org.apache.geode.cache.query.CqAttributes) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) CqQuery(org.apache.geode.cache.query.CqQuery)

Example 37 with CqQuery

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

the class ProxyQueryService method getCqs.

public ClientCQ[] getCqs(String regionName) throws CqException {
    preOp();
    Collection<? extends InternalCqQuery> cqs = null;
    try {
        ArrayList<CqQuery> cqList = new ArrayList<CqQuery>();
        cqs = ((DefaultQueryService) realQueryService).getCqService().getAllCqs(regionName);
        for (InternalCqQuery cq : cqs) {
            if (this.cqNames.contains(cq.getName())) {
                cqList.add((CqQuery) cq);
            }
        }
        ClientCQ[] results = new ClientCQ[cqList.size()];
        cqList.toArray(results);
        return results;
    } catch (CqException cqe) {
        if (logger.isDebugEnabled()) {
            logger.debug("Unable to get Cqs. Error: {}", cqe.getMessage(), cqe);
        }
        throw cqe;
    }
}
Also used : ClientCQ(org.apache.geode.cache.query.internal.cq.ClientCQ) CqException(org.apache.geode.cache.query.CqException) ArrayList(java.util.ArrayList) InternalCqQuery(org.apache.geode.cache.query.internal.cq.InternalCqQuery) InternalCqQuery(org.apache.geode.cache.query.internal.cq.InternalCqQuery) CqQuery(org.apache.geode.cache.query.CqQuery)

Example 38 with CqQuery

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

the class CqQueryDUnitTest method createCQ.

public void createCQ(VM vm, final String cqName, final String queryStr, final boolean isBridgeMemberTest) {
    vm.invoke(new CacheSerializableRunnable("Create CQ :" + cqName) {

        public void run2() throws CacheException {
            // pause(60 * 1000);
            // getLogWriter().info("### DEBUG CREATE CQ START ####");
            // pause(20 * 1000);
            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 CqQueryTestListener(LogWriterUtils.getLogWriter()) };
            // ((CqQueryTestListener)cqListeners[0]).cqName = cqName;
            // if (isBridgeMemberTest) {
            // testListenerForBridgeMembershipTest = (CqQueryTestListener)cqListeners[0];
            // }
            cqf.initCqListeners(cqListeners);
            CqAttributes cqa = cqf.create();
            // Create CQ.
            try {
                CqQuery cq1 = cqService.newCq(cqName, queryStr, cqa);
                assertTrue("newCq() state mismatch", cq1.getState().isStopped());
            } catch (Exception ex) {
                AssertionError err = new AssertionError("Failed to create CQ " + cqName + " . ");
                err.initCause(ex);
                LogWriterUtils.getLogWriter().info("CqService is :" + cqService, err);
                throw err;
            }
        }
    });
}
Also used : CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) CacheException(org.apache.geode.cache.CacheException) DefaultQueryService(org.apache.geode.cache.query.internal.DefaultQueryService) QueryService(org.apache.geode.cache.query.QueryService) CqAttributes(org.apache.geode.cache.query.CqAttributes) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) CqQuery(org.apache.geode.cache.query.CqQuery) CqExistsException(org.apache.geode.cache.query.CqExistsException) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) CqClosedException(org.apache.geode.cache.query.CqClosedException) IOException(java.io.IOException) CacheException(org.apache.geode.cache.CacheException)

Example 39 with CqQuery

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

the class CqQueryUsingPoolDUnitTest method createAndExecCQNoName.

/* Register CQs with no name, execute, and close */
public void createAndExecCQNoName(VM vm, final String poolName, final String queryStr) {
    vm.invoke(new CacheSerializableRunnable("Create CQ with no name:") {

        @Override
        public void run2() throws CacheException {
            // pause(60 * 1000);
            LogWriterUtils.getLogWriter().info("### DEBUG CREATE CQ START ####");
            // pause(20 * 1000);
            LogWriterUtils.getLogWriter().info("### Create CQ with no name. ###");
            // Get CQ Service.
            QueryService qService = null;
            CqQuery cq1 = null;
            String cqName = null;
            try {
                qService = (PoolManager.find(poolName)).getQueryService();
            } catch (Exception cqe) {
                Assert.fail("Failed to getCQService.", cqe);
            }
            SelectResults cqResults = null;
            for (int i = 0; i < 20; ++i) {
                // Create CQ Attributes.
                CqAttributesFactory cqf = new CqAttributesFactory();
                CqListener[] cqListeners = { new CqQueryTestListener(LogWriterUtils.getLogWriter()) };
                cqf.initCqListeners(cqListeners);
                CqAttributes cqa = cqf.create();
                // Create CQ with no name and execute with initial results.
                try {
                    cq1 = qService.newCq(queryStr, cqa);
                    ((CqQueryTestListener) cqListeners[0]).cqName = cq1.getName();
                } catch (Exception ex) {
                    LogWriterUtils.getLogWriter().info("CQService is :" + qService);
                    Assert.fail("Failed to create CQ with no name" + " . ", ex);
                }
                if (cq1 == null) {
                    LogWriterUtils.getLogWriter().info("Failed to get CqQuery object for CQ with no name.");
                } else {
                    cqName = cq1.getName();
                    LogWriterUtils.getLogWriter().info("Created CQ with no name, generated CQ name: " + cqName + " CQ state:" + cq1.getState());
                    assertTrue("Create CQ with no name illegal state", cq1.getState().isStopped());
                }
                if (i % 2 == 0) {
                    try {
                        cqResults = cq1.executeWithInitialResults();
                    } catch (Exception ex) {
                        LogWriterUtils.getLogWriter().info("CqService is :" + qService);
                        Assert.fail("Failed to execute CQ with initial results, cq name: " + cqName + " . ", ex);
                    }
                    LogWriterUtils.getLogWriter().info("initial result size = " + cqResults.size());
                    LogWriterUtils.getLogWriter().info("CQ state after execute with initial results = " + cq1.getState());
                    assertTrue("executeWithInitialResults() state mismatch", cq1.getState().isRunning());
                } else {
                    try {
                        cq1.execute();
                    } catch (Exception ex) {
                        LogWriterUtils.getLogWriter().info("CQService is :" + qService);
                        Assert.fail("Failed to execute CQ " + cqName + " . ", ex);
                    }
                    LogWriterUtils.getLogWriter().info("CQ state after execute = " + cq1.getState());
                    assertTrue("execute() state mismatch", cq1.getState().isRunning());
                }
                // Close the CQ
                try {
                    cq1.close();
                } catch (Exception ex) {
                    LogWriterUtils.getLogWriter().info("CqService is :" + qService);
                    Assert.fail("Failed to close CQ " + cqName + " . ", ex);
                }
                assertTrue("closeCq() state mismatch", cq1.getState().isClosed());
            }
        }
    });
}
Also used : SelectResults(org.apache.geode.cache.query.SelectResults) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) CacheException(org.apache.geode.cache.CacheException) DefaultQueryService(org.apache.geode.cache.query.internal.DefaultQueryService) QueryService(org.apache.geode.cache.query.QueryService) CqAttributes(org.apache.geode.cache.query.CqAttributes) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) CqQuery(org.apache.geode.cache.query.CqQuery) CqExistsException(org.apache.geode.cache.query.CqExistsException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) RMIException(org.apache.geode.test.dunit.RMIException) IOException(java.io.IOException) CacheException(org.apache.geode.cache.CacheException)

Example 40 with CqQuery

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

the class CqQueryUsingPoolDUnitTest method testFilterRegistrationDuringGII.

/**
   * Test Filter registration during GII. Bug fix 39014
   */
@Test
public void testFilterRegistrationDuringGII() throws Exception {
    final Host host = Host.getHost(0);
    VM server1 = host.getVM(0);
    VM server2 = host.getVM(1);
    VM client1 = host.getVM(2);
    VM client2 = host.getVM(3);
    Wait.pause(3 * 1000);
    createServer(server1);
    final int port1 = server1.invoke(() -> CqQueryUsingPoolDUnitTest.getCacheServerPort());
    final String host0 = NetworkUtils.getServerHostName(server1.getHost());
    final int[] ports = AvailablePortHelper.getRandomAvailableTCPPorts(1);
    String poolName = "testFilterRegistrationDuringGII";
    createPool(client1, poolName, new String[] { host0, host0 }, new int[] { port1, ports[0] }, "-1");
    createPool(client2, poolName, new String[] { host0, host0 }, new int[] { port1, ports[0] }, "-1");
    createClient(client1, new int[] { port1, ports[0] }, host0, "-1", poolName);
    createClient(client2, new int[] { port1, ports[0] }, host0, "-1", poolName);
    // Create CQs.
    final int numCQs = 2;
    for (int i = 0; i < numCQs; i++) {
        // Create CQs.
        createCQ(client1, poolName, "client1_" + i, cqs[i]);
        executeCQ(client1, "client1_" + i, false, null);
        createCQ(client2, poolName, "client2_" + i, cqs[i]);
        executeCQ(client2, "client2_" + i, false, null);
    }
    final int interestSize = 20;
    registerInterestListCQ(client1, regions[0], interestSize, false);
    registerInterestListCQ(client2, regions[0], 0, true);
    Wait.pause(1 * 1000);
    // CREATE.
    createValues(server1, regions[0], 100);
    createValues(server1, regions[1], 10);
    waitForCreated(client1, "client1_0", KEY + 10);
    // Create server2.
    server2.invoke(new CacheSerializableRunnable("Create Cache Server") {

        @Override
        public void run2() throws CacheException {
            LogWriterUtils.getLogWriter().info("### Create Cache Server. ###");
            AttributesFactory factory = new AttributesFactory();
            factory.setScope(Scope.DISTRIBUTED_ACK);
            factory.setMirrorType(MirrorType.KEYS_VALUES);
            for (int i = 0; i < regions.length; i++) {
                createRegion(regions[i], factory.createRegionAttributes());
            }
            InitialImageOperation.slowImageProcessing = 100;
            try {
                try {
                    startBridgeServer(ports[0], true);
                } catch (Exception ex) {
                    Assert.fail("While starting CacheServer", ex);
                }
                while (true) {
                    if (InitialImageOperation.slowImageSleeps > 0) {
                        // Create events while GII for HARegion is in progress.
                        LocalRegion region1 = (LocalRegion) getRootRegion().getSubregion(regions[0]);
                        for (int i = 90; i <= 120; i++) {
                            region1.put(KEY + i, new Portfolio(i, i));
                        }
                        break;
                    }
                    Wait.pause(20);
                }
            } finally {
                InitialImageOperation.slowImageProcessing = 0;
            }
        }
    });
    Wait.pause(3 * 1000);
    // Check if CQs are registered as part of GII.
    server2.invoke(new CacheSerializableRunnable("Create values") {

        @Override
        public void run2() throws CacheException {
            DefaultQueryService qs = (DefaultQueryService) getCache().getQueryService();
            Collection<CacheClientProxy> proxies = CacheClientNotifier.getInstance().getClientProxies();
            Iterator iter = proxies.iterator();
            try {
                for (CacheClientProxy p : proxies) {
                    ClientProxyMembershipID clientId = p.getProxyID();
                    List cqs = qs.getCqService().getAllClientCqs(clientId);
                    getCache().getLogger().fine("Number of CQs found for client :" + clientId + " are :" + cqs.size());
                    if (cqs.size() != numCQs) {
                        fail("Number of CQs registerted by the client is :" + cqs.size() + " less than expected : " + numCQs);
                    }
                    CqQuery cq = (CqQuery) cqs.get(0);
                    LocalRegion region1 = (LocalRegion) getRootRegion().getSubregion(regions[0]);
                    if (cq.getName().startsWith("client1_")) {
                        if (region1.getFilterProfile().getKeysOfInterest(clientId) == null || region1.getFilterProfile().getKeysOfInterest(clientId).size() != interestSize) {
                            fail("Interest registartion during Secondary HARegion creation has failed.");
                        }
                    } else {
                        if (!region1.getFilterProfile().isInterestedInAllKeys(clientId)) {
                            fail("Interest registartion during Secondary HARegion creation has failed.");
                        }
                    }
                }
            } catch (Exception ex) {
                fail("Exception while validating filter count. ", ex);
            }
        }
    });
    // Close.
    closeClient(client1);
    closeClient(client2);
    closeServer(server1);
    closeServer(server2);
}
Also used : CacheClientProxy(org.apache.geode.internal.cache.tier.sockets.CacheClientProxy) CacheException(org.apache.geode.cache.CacheException) Portfolio(org.apache.geode.cache.query.data.Portfolio) Host(org.apache.geode.test.dunit.Host) LocalRegion(org.apache.geode.internal.cache.LocalRegion) CqExistsException(org.apache.geode.cache.query.CqExistsException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) RMIException(org.apache.geode.test.dunit.RMIException) IOException(java.io.IOException) CacheException(org.apache.geode.cache.CacheException) ClientProxyMembershipID(org.apache.geode.internal.cache.tier.sockets.ClientProxyMembershipID) AttributesFactory(org.apache.geode.cache.AttributesFactory) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) VM(org.apache.geode.test.dunit.VM) Iterator(java.util.Iterator) Collection(java.util.Collection) List(java.util.List) ArrayList(java.util.ArrayList) CqQuery(org.apache.geode.cache.query.CqQuery) DefaultQueryService(org.apache.geode.cache.query.internal.DefaultQueryService) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) Test(org.junit.Test)

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