Search in sources :

Example 71 with CacheSerializableRunnable

use of org.apache.geode.cache30.CacheSerializableRunnable 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)

Example 72 with CacheSerializableRunnable

use of org.apache.geode.cache30.CacheSerializableRunnable in project geode by apache.

the class PrCqUsingPoolDUnitTest method createServer.

/**
   * Create a bridge server with partitioned region.
   * 
   * @param server VM where to create the bridge server.
   * @param port bridge server port.
   * @param isAccessor if true the under lying partitioned region will not host data on this vm.
   * @param redundantCopies number of redundant copies for the primary bucket.
   */
public void createServer(VM server, final int port, final boolean isAccessor, final int redundantCopies) {
    SerializableRunnable createServer = new CacheSerializableRunnable("Create Cache Server") {

        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);
            // int maxMem = 0;
            AttributesFactory attr = new AttributesFactory();
            // attr.setValueConstraint(valueConstraint);
            PartitionAttributesFactory paf = new PartitionAttributesFactory();
            if (isAccessor) {
                paf.setLocalMaxMemory(0);
            }
            PartitionAttributes prAttr = paf.setTotalNumBuckets(197).setRedundantCopies(redundantCopies).create();
            attr.setPartitionAttributes(prAttr);
            assertFalse(getSystem().isLoner());
            // 0);
            for (int i = 0; i < regions.length; i++) {
                Region r = createRegion(regions[i], attr.create());
                LogWriterUtils.getLogWriter().info("Server created the region: " + r);
            }
            // pause(2000);
            try {
                startBridgeServer(port, true);
            } catch (Exception ex) {
                Assert.fail("While starting CacheServer", ex);
            }
        // pause(2000);
        }
    };
    server.invoke(createServer);
}
Also used : PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) 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) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) PartitionAttributes(org.apache.geode.cache.PartitionAttributes) Region(org.apache.geode.cache.Region) IOException(java.io.IOException) CacheException(org.apache.geode.cache.CacheException)

Example 73 with CacheSerializableRunnable

use of org.apache.geode.cache30.CacheSerializableRunnable in project geode by apache.

the class PdxQueryCQDUnitTest method testCq.

/**
   * Tests client-server query on PdxInstance.
   */
@Test
public void testCq() throws CacheException {
    final Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    VM vm2 = host.getVM(2);
    VM vm3 = host.getVM(3);
    final int numberOfEntries = 10;
    // where id > 5 (0-5)
    final int queryLimit = 6;
    // Start server1
    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            configAndStartBridgeServer();
            Region region = getRootRegion().getSubregion(regionName);
            for (int i = 0; i < numberOfEntries; i++) {
                region.put("key-" + i, new TestObject(i, "vmware"));
            }
        }
    });
    // Start server2
    vm1.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            configAndStartBridgeServer();
            Region region = getRootRegion().getSubregion(regionName);
        }
    });
    // Client pool.
    final int port0 = vm0.invoke(() -> PdxQueryCQTestBase.getCacheServerPort());
    final int port1 = vm1.invoke(() -> PdxQueryCQTestBase.getCacheServerPort());
    final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
    // Create client pool.
    final String poolName = "testCqPool";
    createPool(vm2, poolName, new String[] { host0 }, new int[] { port0 }, true);
    createPool(vm3, poolName, new String[] { host0 }, new int[] { port1 }, true);
    final String cqName = "testCq";
    // Execute CQ
    SerializableRunnable executeCq = new CacheSerializableRunnable("Execute queries") {

        public void run2() throws CacheException {
            LogWriterUtils.getLogWriter().info("### Create CQ. ###" + cqName);
            // Get CQ Service.
            QueryService qService = null;
            try {
                qService = (PoolManager.find(poolName)).getQueryService();
            } catch (Exception cqe) {
                Assert.fail("Failed to getCQService.", cqe);
            }
            // Create CQ Attributes.
            CqAttributesFactory cqf = new CqAttributesFactory();
            CqListener[] cqListeners = { new CqQueryTestListener(LogWriterUtils.getLogWriter()) };
            ((CqQueryTestListener) cqListeners[0]).cqName = cqName;
            cqf.initCqListeners(cqListeners);
            CqAttributes cqa = cqf.create();
            // Create CQ.
            try {
                CqQuery cq = qService.newCq(cqName, queryString[3], cqa);
                SelectResults sr = cq.executeWithInitialResults();
                for (Object o : sr.asSet()) {
                    Struct s = (Struct) o;
                    Object value = s.get("value");
                    if (!(value instanceof TestObject)) {
                        fail("Expected type TestObject, not found in result set. Found type :" + o.getClass());
                    }
                }
            } catch (Exception ex) {
                AssertionError err = new AssertionError("Failed to create CQ " + cqName + " . ");
                err.initCause(ex);
                LogWriterUtils.getLogWriter().info("QueryService is :" + qService, err);
                throw err;
            }
        }
    };
    vm2.invoke(executeCq);
    vm3.invoke(executeCq);
    // Check for TestObject instances on Server2.
    // It should be 0
    vm1.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            assertEquals(0, TestObject.numInstance);
        }
    });
    // update
    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(regionName);
            for (int i = 0; i < numberOfEntries * 2; i++) {
                region.put("key-" + i, new TestObject(i, "vmware"));
            }
            // Check for TestObject instances.
            assertEquals(numberOfEntries * 3, TestObject.numInstance);
        }
    });
    // Check for TestObject instances on Server2.
    // It should be 0
    vm1.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            assertEquals(0, TestObject.numInstance);
        }
    });
    SerializableRunnable validateCq = 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) {
                Assert.fail("Failed to getCQService.", cqe);
            }
            CqQuery cQuery = cqService.getCq(cqName);
            if (cQuery == null) {
                fail("Failed to get CqQuery for CQ : " + cqName);
            }
            CqAttributes cqAttr = cQuery.getCqAttributes();
            CqListener[] cqListeners = cqAttr.getCqListeners();
            final CqQueryTestListener listener = (CqQueryTestListener) cqListeners[0];
            // Wait for the events to show up on the client.
            Wait.waitForCriterion(new WaitCriterion() {

                public boolean done() {
                    return listener.getTotalEventCount() >= (numberOfEntries * 2 - queryLimit);
                }

                public String description() {
                    return null;
                }
            }, 30000, 100, false);
            listener.printInfo(false);
            // Check for event type.
            Object[] cqEvents = listener.getEvents();
            for (Object o : cqEvents) {
                CqEvent cqEvent = (CqEvent) o;
                Object value = cqEvent.getNewValue();
                if (!(value instanceof TestObject)) {
                    fail("Expected type TestObject, not found in result set. Found type :" + o.getClass());
                }
            }
            // Check for totalEvents count.
            assertEquals("Total Event Count mismatch", (numberOfEntries * 2 - queryLimit), listener.getTotalEventCount());
            // Check for create count.
            assertEquals("Create Event mismatch", numberOfEntries, listener.getCreateEventCount());
            // Check for update count.
            assertEquals("Update Event mismatch", numberOfEntries - queryLimit, listener.getUpdateEventCount());
        }
    };
    vm2.invoke(validateCq);
    vm3.invoke(validateCq);
    this.closeClient(vm2);
    this.closeClient(vm3);
    this.closeClient(vm1);
    this.closeClient(vm0);
}
Also used : CqQueryTestListener(org.apache.geode.cache.query.cq.dunit.CqQueryTestListener) CacheException(org.apache.geode.cache.CacheException) CqListener(org.apache.geode.cache.query.CqListener) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) TestObject(org.apache.geode.cache.query.dunit.PdxQueryCQTestBase.TestObject) Host(org.apache.geode.test.dunit.Host) CacheException(org.apache.geode.cache.CacheException) Struct(org.apache.geode.cache.query.Struct) SelectResults(org.apache.geode.cache.query.SelectResults) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) CqEvent(org.apache.geode.cache.query.CqEvent) QueryService(org.apache.geode.cache.query.QueryService) CqAttributes(org.apache.geode.cache.query.CqAttributes) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) TestObject(org.apache.geode.cache.query.dunit.PdxQueryCQTestBase.TestObject) CqQuery(org.apache.geode.cache.query.CqQuery) ClientSubscriptionTest(org.apache.geode.test.junit.categories.ClientSubscriptionTest) Test(org.junit.Test) SerializationTest(org.apache.geode.test.junit.categories.SerializationTest) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 74 with CacheSerializableRunnable

use of org.apache.geode.cache30.CacheSerializableRunnable in project geode by apache.

the class PdxQueryCQDUnitTest method testCqAndInterestRegistrationsWithFailOver.

/**
   * Tests client-server query on PdxInstance.
   */
@Test
public void testCqAndInterestRegistrationsWithFailOver() throws CacheException {
    final Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    VM vm2 = host.getVM(2);
    VM vm3 = host.getVM(3);
    final int numberOfEntries = 10;
    // where id > 5 (0-5)
    final int queryLimit = 6;
    final String[] queries = new String[] { "SELECT * FROM " + regName + " p WHERE p.ticker = 'vmware'", "SELECT * FROM " + regName + " WHERE id > 5" };
    // Start server1
    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            configAndStartBridgeServer(false, true);
            Region region = getRootRegion().getSubregion(regionName);
        }
    });
    // Start server2
    vm1.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            configAndStartBridgeServer(false, true);
            Region region = getRootRegion().getSubregion(regionName);
        }
    });
    // Start server3
    vm2.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            configAndStartBridgeServer(false, true);
            Region region = getRootRegion().getSubregion(regionName);
        }
    });
    // Client pool.
    final int port0 = vm0.invoke(() -> PdxQueryCQTestBase.getCacheServerPort());
    final int port1 = vm1.invoke(() -> PdxQueryCQTestBase.getCacheServerPort());
    final int port2 = vm2.invoke(() -> PdxQueryCQTestBase.getCacheServerPort());
    final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
    // Create client pool.
    final String poolName = "testCqPool";
    createPool(vm3, poolName, new String[] { host0, host0, host0 }, new int[] { port1, port0, port2 }, true, 1);
    final String cqName = "testCq";
    vm3.invoke(new CacheSerializableRunnable("init region") {

        public void run2() throws CacheException {
            QueryService localQueryService = null;
            AttributesFactory factory = new AttributesFactory();
            factory.setScope(Scope.LOCAL);
            ClientServerTestCase.configureConnectionPool(factory, host0, port1, -1, true, -1, -1, null);
            Region region = createRegion(regionName, rootRegionName, factory.create());
            for (int i = 0; i < numberOfEntries; i++) {
                region.put("key-" + i, new TestObject(i, "vmware"));
            }
        }
    });
    SerializableRunnable subscribe = new CacheSerializableRunnable("subscribe") {

        public void run2() throws CacheException {
            // Register interest
            Region region = getRootRegion().getSubregion(regionName);
            List list = new ArrayList();
            for (int i = 1; i <= numberOfEntries * 3; i++) {
                if (i % 4 == 0) {
                    list.add("key-" + i);
                }
            }
            region.registerInterest(list);
            LogWriterUtils.getLogWriter().info("### Create CQ. ###" + cqName);
            // Get CQ Service.
            QueryService qService = null;
            try {
                qService = (PoolManager.find(poolName)).getQueryService();
            } catch (Exception cqe) {
                Assert.fail("Failed to getCQService.", cqe);
            }
            // Create CQ Attributes.
            for (int i = 0; i < queries.length; i++) {
                CqAttributesFactory cqf = new CqAttributesFactory();
                CqListener[] cqListeners = { new CqQueryTestListener(LogWriterUtils.getLogWriter()) };
                ((CqQueryTestListener) cqListeners[0]).cqName = (cqName + i);
                cqf.initCqListeners(cqListeners);
                CqAttributes cqa = cqf.create();
                // Create CQ.
                try {
                    CqQuery cq = qService.newCq(cqName + i, queries[i], cqa);
                    SelectResults sr = cq.executeWithInitialResults();
                    for (Object o : sr.asSet()) {
                        Struct s = (Struct) o;
                        Object value = s.get("value");
                        if (!(value instanceof TestObject)) {
                            fail("Expected type TestObject, not found in result set. Found type :" + o.getClass());
                        }
                    }
                } catch (Exception ex) {
                    AssertionError err = new AssertionError("Failed to create CQ " + cqName + " . ");
                    err.initCause(ex);
                    LogWriterUtils.getLogWriter().info("QueryService is :" + qService, err);
                    throw err;
                }
            }
        }
    };
    vm3.invoke(subscribe);
    // Check for TestObject instances on Server2.
    // It should be 0
    vm1.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            assertEquals(0, TestObject.numInstance);
        }
    });
    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(regionName);
            // Check for TestObject instances.
            assertEquals(0, TestObject.numInstance);
        }
    });
    // update
    vm3.invoke(new CacheSerializableRunnable("Update") {

        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(regionName);
            for (int i = 0; i < numberOfEntries * 2; i++) {
                region.put("key-" + i, new TestObject(i, "vmware"));
            }
        }
    });
    // Validate CQs.
    for (int i = 0; i < queries.length; i++) {
        int expectedEvent = 0;
        int updateEvents = 0;
        if (i != 0) {
            expectedEvent = (numberOfEntries * 2) - queryLimit;
            updateEvents = numberOfEntries - queryLimit;
        } else {
            expectedEvent = numberOfEntries * 2;
            updateEvents = numberOfEntries;
        }
        validateCq(vm3, cqName + i, expectedEvent, numberOfEntries, updateEvents);
    }
    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(regionName);
            // Check for TestObject instances.
            assertEquals(0, TestObject.numInstance);
        }
    });
    vm1.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            assertEquals(0, TestObject.numInstance);
        }
    });
    // Update
    vm3.invokeAsync(new CacheSerializableRunnable("Update") {

        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(regionName);
            for (int i = 0; i < numberOfEntries * 2; i++) {
                region.put("key-" + i, new TestObject(i, "vmware"));
            }
        }
    });
    // Kill server
    this.closeClient(vm0);
    // validate cq
    for (int i = 0; i < queries.length; i++) {
        int expectedEvent = 0;
        int updateEvents = 0;
        if (i != 0) {
            // Double the previous time
            expectedEvent = (numberOfEntries * 4) - (queryLimit * 2);
            updateEvents = (numberOfEntries * 3) - (queryLimit * 2);
        } else {
            expectedEvent = numberOfEntries * 4;
            updateEvents = numberOfEntries * 3;
        }
        validateCq(vm3, cqName + i, expectedEvent, numberOfEntries, updateEvents);
    }
    this.closeClient(vm1);
    // Check for TestObject instances on Server3.
    // It should be 0
    vm2.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            assertEquals(0, TestObject.numInstance);
        }
    });
    this.closeClient(vm2);
    this.closeClient(vm3);
}
Also used : CacheException(org.apache.geode.cache.CacheException) ArrayList(java.util.ArrayList) Struct(org.apache.geode.cache.query.Struct) AttributesFactory(org.apache.geode.cache.AttributesFactory) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) SelectResults(org.apache.geode.cache.query.SelectResults) ArrayList(java.util.ArrayList) List(java.util.List) CqQuery(org.apache.geode.cache.query.CqQuery) CqQueryTestListener(org.apache.geode.cache.query.cq.dunit.CqQueryTestListener) CqListener(org.apache.geode.cache.query.CqListener) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) TestObject(org.apache.geode.cache.query.dunit.PdxQueryCQTestBase.TestObject) Host(org.apache.geode.test.dunit.Host) CacheException(org.apache.geode.cache.CacheException) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) QueryService(org.apache.geode.cache.query.QueryService) CqAttributes(org.apache.geode.cache.query.CqAttributes) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) TestObject(org.apache.geode.cache.query.dunit.PdxQueryCQTestBase.TestObject) ClientSubscriptionTest(org.apache.geode.test.junit.categories.ClientSubscriptionTest) Test(org.junit.Test) SerializationTest(org.apache.geode.test.junit.categories.SerializationTest) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 75 with CacheSerializableRunnable

use of org.apache.geode.cache30.CacheSerializableRunnable in project geode by apache.

the class PdxQueryCQDUnitTest method testCqAndInterestRegistrations.

/**
   * Tests client-server query on PdxInstance.
   */
@Test
public void testCqAndInterestRegistrations() throws CacheException {
    final Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    VM vm2 = host.getVM(2);
    VM vm3 = host.getVM(3);
    final int numberOfEntries = 10;
    // where id > 5 (0-5)
    final int queryLimit = 6;
    final String[] queries = new String[] { "SELECT * FROM " + regName + " p WHERE p.ticker = 'vmware'", "SELECT * FROM " + regName + " WHERE id > 5" };
    // Start server1
    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            configAndStartBridgeServer(false, true);
            Region region = getRootRegion().getSubregion(regionName);
        }
    });
    // Start server2
    vm1.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            configAndStartBridgeServer(false, true);
            Region region = getRootRegion().getSubregion(regionName);
        }
    });
    // Client pool.
    final int port0 = vm0.invoke(() -> PdxQueryCQTestBase.getCacheServerPort());
    final int port1 = vm1.invoke(() -> PdxQueryCQTestBase.getCacheServerPort());
    final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
    // Create client pool.
    final String poolName = "testCqPool";
    createPool(vm2, poolName, new String[] { host0, host0 }, new int[] { port0, port1 }, true);
    createPool(vm3, poolName, new String[] { host0, host0 }, new int[] { port1, port0 }, true);
    final String cqName = "testCq";
    vm3.invoke(new CacheSerializableRunnable("init region") {

        public void run2() throws CacheException {
            QueryService localQueryService = null;
            AttributesFactory factory = new AttributesFactory();
            factory.setScope(Scope.LOCAL);
            ClientServerTestCase.configureConnectionPool(factory, host0, port1, -1, true, -1, -1, null);
            Region region = createRegion(regionName, rootRegionName, factory.create());
            for (int i = 0; i < numberOfEntries; i++) {
                region.put("key-" + i, new TestObject(i, "vmware"));
            }
        }
    });
    vm2.invoke(new CacheSerializableRunnable("init region") {

        public void run2() throws CacheException {
            QueryService localQueryService = null;
            AttributesFactory factory = new AttributesFactory();
            factory.setScope(Scope.LOCAL);
            ClientServerTestCase.configureConnectionPool(factory, host0, port0, -1, true, -1, -1, null);
            Region region = createRegion(regionName, rootRegionName, factory.create());
        }
    });
    SerializableRunnable subscribe = new CacheSerializableRunnable("subscribe") {

        public void run2() throws CacheException {
            // Register interest
            Region region = getRootRegion().getSubregion(regionName);
            List list = new ArrayList();
            for (int i = 1; i <= numberOfEntries * 3; i++) {
                if (i % 4 == 0) {
                    list.add("key-" + i);
                }
            }
            region.registerInterest(list);
            LogWriterUtils.getLogWriter().info("### Create CQ. ###" + cqName);
            // Get CQ Service.
            QueryService qService = null;
            try {
                qService = (PoolManager.find(poolName)).getQueryService();
            } catch (Exception cqe) {
                Assert.fail("Failed to getCQService.", cqe);
            }
            // Create CQ Attributes.
            for (int i = 0; i < queries.length; i++) {
                CqAttributesFactory cqf = new CqAttributesFactory();
                CqListener[] cqListeners = { new CqQueryTestListener(LogWriterUtils.getLogWriter()) };
                ((CqQueryTestListener) cqListeners[0]).cqName = (cqName + i);
                cqf.initCqListeners(cqListeners);
                CqAttributes cqa = cqf.create();
                // Create CQ.
                try {
                    CqQuery cq = qService.newCq(cqName + i, queries[i], cqa);
                    SelectResults sr = cq.executeWithInitialResults();
                    for (Object o : sr.asSet()) {
                        Struct s = (Struct) o;
                        Object value = s.get("value");
                        if (!(value instanceof TestObject)) {
                            fail("Expected type TestObject, not found in result set. Found type :" + o.getClass());
                        }
                    }
                } catch (Exception ex) {
                    AssertionError err = new AssertionError("Failed to create CQ " + cqName + " . ");
                    err.initCause(ex);
                    LogWriterUtils.getLogWriter().info("QueryService is :" + qService, err);
                    throw err;
                }
            }
        }
    };
    vm2.invoke(subscribe);
    vm3.invoke(subscribe);
    // Check for TestObject instances on Server2.
    // It should be 0
    vm1.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            assertEquals(0, TestObject.numInstance);
        }
    });
    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(regionName);
            // Check for TestObject instances.
            assertEquals(0, TestObject.numInstance);
        }
    });
    vm3.invoke(new CacheSerializableRunnable("Update") {

        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(regionName);
            for (int i = 0; i < numberOfEntries * 2; i++) {
                region.put("key-" + i, new TestObject(i, "vmware"));
            }
        }
    });
    // Validate CQs.
    for (int i = 0; i < queries.length; i++) {
        int expectedEvent = 0;
        int updateEvents = 0;
        if (i != 0) {
            expectedEvent = numberOfEntries * 2 - queryLimit;
            updateEvents = numberOfEntries - queryLimit;
        } else {
            expectedEvent = numberOfEntries * 2;
            updateEvents = numberOfEntries;
        }
        validateCq(vm2, cqName + i, expectedEvent, numberOfEntries, updateEvents);
        validateCq(vm3, cqName + i, expectedEvent, numberOfEntries, updateEvents);
    }
    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(regionName);
            // Check for TestObject instances.
            assertEquals(0, TestObject.numInstance);
        }
    });
    // Check for TestObject instances on Server2.
    // It should be 0
    vm1.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            assertEquals(0, TestObject.numInstance);
        }
    });
    this.closeClient(vm2);
    this.closeClient(vm3);
    this.closeClient(vm1);
    this.closeClient(vm0);
}
Also used : CacheException(org.apache.geode.cache.CacheException) ArrayList(java.util.ArrayList) Struct(org.apache.geode.cache.query.Struct) AttributesFactory(org.apache.geode.cache.AttributesFactory) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) SelectResults(org.apache.geode.cache.query.SelectResults) ArrayList(java.util.ArrayList) List(java.util.List) CqQuery(org.apache.geode.cache.query.CqQuery) CqQueryTestListener(org.apache.geode.cache.query.cq.dunit.CqQueryTestListener) CqListener(org.apache.geode.cache.query.CqListener) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) TestObject(org.apache.geode.cache.query.dunit.PdxQueryCQTestBase.TestObject) Host(org.apache.geode.test.dunit.Host) CacheException(org.apache.geode.cache.CacheException) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) QueryService(org.apache.geode.cache.query.QueryService) CqAttributes(org.apache.geode.cache.query.CqAttributes) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) TestObject(org.apache.geode.cache.query.dunit.PdxQueryCQTestBase.TestObject) ClientSubscriptionTest(org.apache.geode.test.junit.categories.ClientSubscriptionTest) Test(org.junit.Test) SerializationTest(org.apache.geode.test.junit.categories.SerializationTest) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Aggregations

CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)595 CacheException (org.apache.geode.cache.CacheException)415 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)369 Test (org.junit.Test)369 Region (org.apache.geode.cache.Region)307 VM (org.apache.geode.test.dunit.VM)279 Host (org.apache.geode.test.dunit.Host)274 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)179 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)165 AttributesFactory (org.apache.geode.cache.AttributesFactory)145 IOException (java.io.IOException)135 Cache (org.apache.geode.cache.Cache)124 QueryService (org.apache.geode.cache.query.QueryService)118 PartitionAttributesFactory (org.apache.geode.cache.PartitionAttributesFactory)107 LocalRegion (org.apache.geode.internal.cache.LocalRegion)106 SelectResults (org.apache.geode.cache.query.SelectResults)85 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)75 ClientServerTest (org.apache.geode.test.junit.categories.ClientServerTest)71 IgnoredException (org.apache.geode.test.dunit.IgnoredException)65 ClientSubscriptionTest (org.apache.geode.test.junit.categories.ClientSubscriptionTest)61