Search in sources :

Example 6 with CqEvent

use of org.apache.geode.cache.query.CqEvent 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 7 with CqEvent

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

the class ClientSnapshotDUnitTest method testImport.

@Test
public void testImport() throws Exception {
    int count = 1000;
    for (int i = 0; i < count; i++) {
        region.put(i, new MyObject(i, "clienttest " + i));
    }
    SerializableCallable export = new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            File f = new File(getDiskDirs()[0], "client-import.snapshot");
            Region<Integer, MyObject> r = getCache().getRegion("clienttest");
            r.getSnapshotService().save(f, SnapshotFormat.GEMFIRE);
            return f;
        }
    };
    Host.getHost(0).getVM(3).invoke(export);
    for (int i = 0; i < count; i++) {
        region.put(i, new MyObject(i, "XXX"));
    }
    SerializableCallable imp = new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            final AtomicBoolean cqtest = new AtomicBoolean(false);
            CqAttributesFactory af = new CqAttributesFactory();
            af.addCqListener(new CqListenerAdapter() {

                @Override
                public void onEvent(CqEvent aCqEvent) {
                    cqtest.set(true);
                }
            });
            Region<Integer, MyObject> r = getCache().getRegion("clienttest");
            CqQuery cq = r.getRegionService().getQueryService().newCq("SELECT * FROM /clienttest", af.create());
            cq.execute();
            File f = new File(getDiskDirs()[0], "client-import.snapshot");
            r.getSnapshotService().load(f, SnapshotFormat.GEMFIRE);
            return cqtest.get();
        }
    };
    // add callbacks
    region.getAttributesMutator().setCacheWriter(new CacheWriterAdapter<Integer, MyObject>() {

        @Override
        public void beforeUpdate(EntryEvent<Integer, MyObject> event) {
            fail("CacheWriter invoked during import");
        }
    });
    final AtomicBoolean cltest = new AtomicBoolean(false);
    region.getAttributesMutator().addCacheListener(new CacheListenerAdapter<Integer, MyObject>() {

        @Override
        public void afterUpdate(EntryEvent<Integer, MyObject> event) {
            cltest.set(true);
        }
    });
    boolean cqtest = (Boolean) Host.getHost(0).getVM(3).invoke(imp);
    assertEquals("CacheListener invoked during import", false, cltest.get());
    assertEquals("CqListener invoked during import", false, cqtest);
    for (MyObject obj : region.values()) {
        assertTrue(obj.getF2().startsWith("clienttest"));
    }
}
Also used : CqListenerAdapter(org.apache.geode.cache.util.CqListenerAdapter) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CqEvent(org.apache.geode.cache.query.CqEvent) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) MyObject(com.examples.snapshot.MyObject) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) CqQuery(org.apache.geode.cache.query.CqQuery) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) File(java.io.File) ClientSubscriptionTest(org.apache.geode.test.junit.categories.ClientSubscriptionTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 8 with CqEvent

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

the class ClientSnapshotDUnitTest method testClientCallbacks.

@Test
public void testClientCallbacks() throws Exception {
    int count = 1000;
    for (int i = 0; i < count; i++) {
        region.put(i, new MyObject(i, "clienttest " + i));
    }
    File f = new File(getDiskDirs()[0], "client-callback.snapshot");
    region.getSnapshotService().save(f, SnapshotFormat.GEMFIRE);
    for (int i = 0; i < count; i++) {
        region.put(i, new MyObject(i, "XXX"));
    }
    SerializableCallable callbacks = new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            Region<Integer, MyObject> r = getCache().getRegion("clienttest");
            r.registerInterestRegex(".*");
            r.getAttributesMutator().setCacheWriter(new CacheWriterAdapter<Integer, MyObject>() {

                @Override
                public void beforeUpdate(EntryEvent<Integer, MyObject> event) {
                    fail("CacheWriter invoked during import");
                }
            });
            r.getAttributesMutator().addCacheListener(new CacheListenerAdapter<Integer, MyObject>() {

                @Override
                public void afterUpdate(EntryEvent<Integer, MyObject> event) {
                    fail("CacheListener was invoked during import");
                }
            });
            final AtomicBoolean cqtest = new AtomicBoolean(false);
            CqAttributesFactory af = new CqAttributesFactory();
            af.addCqListener(new CqListenerAdapter() {

                @Override
                public void onEvent(CqEvent aCqEvent) {
                    fail("Cq was invoked during import");
                }
            });
            CqQuery cq = r.getRegionService().getQueryService().newCq("SELECT * FROM /clienttest", af.create());
            cq.execute();
            return null;
        }
    };
    Host.getHost(0).getVM(3).invoke(callbacks);
    region.getSnapshotService().load(f, SnapshotFormat.GEMFIRE);
}
Also used : CqListenerAdapter(org.apache.geode.cache.util.CqListenerAdapter) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CqEvent(org.apache.geode.cache.query.CqEvent) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) MyObject(com.examples.snapshot.MyObject) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) CqQuery(org.apache.geode.cache.query.CqQuery) File(java.io.File) ClientSubscriptionTest(org.apache.geode.test.junit.categories.ClientSubscriptionTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 9 with CqEvent

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

the class DeltaPropagationWithCQDUnitTest method registerCQs.

public static void registerCQs(Integer numOfListeners, String name) throws Exception {
    QueryService qs = pool.getQueryService();
    CqAttributesFactory caf = new CqAttributesFactory();
    CqListenerAdapter[] cqListeners = new CqListenerAdapter[numOfListeners];
    for (int i = 0; i < numOfListeners; i++) {
        cqListeners[i] = new CqListenerAdapter() {

            @Override
            public void onEvent(CqEvent event) {
                System.out.println("CqListener.onEvent invoked.  Event=" + event);
                if (event.getDeltaValue() != null) {
                    deltasFound++;
                }
                // The first CQ event dispatched with a delta will not have a newValue.
                // Attempting to access the newValue will cause an exception to be
                // thrown, exiting this listener and causing the full value to be
                // read from the server. The listener is then invoked a second time
                // and getNewValue will succeed
                event.getNewValue();
                if (event.getDeltaValue() != null) {
                    // if there's a newValue we should ignore the delta bytes
                    deltasFound--;
                }
                System.out.println("deltasFound=" + deltasFound);
                cqEvents++;
                System.out.println("cqEvents is now " + cqEvents);
            }

            @Override
            public void onError(CqEvent event) {
                System.out.println("CqListener.onError invoked.  Event=" + event);
                if (event.getDeltaValue() != null) {
                    deltasFound++;
                }
                event.getNewValue();
                if (event.getDeltaValue() != null) {
                    deltasFound--;
                }
                System.out.println("deltasFound=" + deltasFound);
                cqErrors++;
                System.out.println("cqErrors is now " + cqErrors);
            }
        };
        caf.addCqListener(cqListeners[i]);
    }
    CqQuery cQuery = qs.newCq(name, CQ1, caf.create());
    cQuery.execute();
    if (qs.getCq(name) == null) {
        fail("Failed to get CQ " + name);
    }
}
Also used : CqEvent(org.apache.geode.cache.query.CqEvent) QueryService(org.apache.geode.cache.query.QueryService) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) CqQuery(org.apache.geode.cache.query.CqQuery) CqListenerAdapter(org.apache.geode.cache.util.CqListenerAdapter)

Example 10 with CqEvent

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

the class ClientToServerDeltaDUnitTest method createClientCache.

/*
   * create client cache
   */
public static void createClientCache(String host, Integer port, Boolean attachListener, Boolean isEmpty, Boolean isCq, String[] cqQueryString, Boolean registerInterestAll, Boolean enableSubscription) throws Exception {
    updates = 0;
    create = 0;
    firstUpdate = null;
    secondUpdate = null;
    error = false;
    lastKeyReceived = false;
    Properties props = new Properties();
    props.setProperty(MCAST_PORT, "0");
    props.setProperty(LOCATORS, "");
    new ClientToServerDeltaDUnitTest().createCache(props);
    pool = (PoolImpl) PoolManager.createFactory().addServer(host, port.intValue()).setThreadLocalConnections(true).setMinConnections(2).setSubscriptionEnabled(enableSubscription).setSubscriptionRedundancy(0).setReadTimeout(10000).setPingInterval(1000).setSocketBufferSize(32768).create("ClientToServerDeltaDunitTestPool");
    AttributesFactory factory = new AttributesFactory();
    factory.setScope(Scope.DISTRIBUTED_ACK);
    factory.setConcurrencyChecksEnabled(true);
    if (isEmpty) {
        factory.setSubscriptionAttributes(new SubscriptionAttributes(InterestPolicy.ALL));
        factory.setDataPolicy(DataPolicy.EMPTY);
    } else {
        factory.setDataPolicy(DataPolicy.NORMAL);
    }
    factory.setPoolName(pool.getName());
    factory.setCloningEnabled(false);
    // region with empty data policy
    RegionAttributes attrs = factory.create();
    region = cache.createRegion(REGION_NAME, attrs);
    if (attachListener) {
        region.getAttributesMutator().addCacheListener(new CacheListenerAdapter() {

            @Override
            public void afterCreate(EntryEvent event) {
                create++;
                if (LAST_KEY.equals(event.getKey())) {
                    lastKeyReceived = true;
                }
                ;
            }

            @Override
            public void afterUpdate(EntryEvent event) {
                switch(updates) {
                    case 0:
                        // first delta
                        validateUpdates(event, firstUpdate, "FIRST");
                        updates++;
                        break;
                    case 1:
                        // combine delta
                        validateUpdates(event, firstUpdate, "FIRST");
                        validateUpdates(event, secondUpdate, "SECOND");
                        updates++;
                        break;
                    default:
                        break;
                }
            }
        });
    }
    if (registerInterestAll) {
        region.registerInterest("ALL_KEYS");
    }
    if (isCq) {
        CqAttributesFactory cqf = new CqAttributesFactory();
        CqListenerAdapter cqlist = new CqListenerAdapter() {

            @Override
            public void onEvent(CqEvent cqEvent) {
                Object key = cqEvent.getKey();
                if (LAST_KEY.equals(key)) {
                    lastKeyReceived = true;
                }
                logger.fine("CQ event received for (key, value): (" + key + ", " + cqEvent.getNewValue() + ")");
            }

            @Override
            public void onError(CqEvent cqEvent) {
                logger.fine("CQ error received for key: " + cqEvent.getKey());
            }
        };
        cqf.addCqListener(cqlist);
        CqAttributes cqa = cqf.create();
        for (int i = 0; i < cqQueryString.length; i++) {
            CqQuery cq = cache.getQueryService().newCq("Delta_Query_" + i, cqQueryString[i], cqa);
            cq.execute();
        }
    }
}
Also used : RegionAttributes(org.apache.geode.cache.RegionAttributes) Properties(java.util.Properties) CqListenerAdapter(org.apache.geode.cache.util.CqListenerAdapter) AttributesFactory(org.apache.geode.cache.AttributesFactory) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) CqEvent(org.apache.geode.cache.query.CqEvent) CqAttributes(org.apache.geode.cache.query.CqAttributes) EntryEvent(org.apache.geode.cache.EntryEvent) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) CqQuery(org.apache.geode.cache.query.CqQuery) SubscriptionAttributes(org.apache.geode.cache.SubscriptionAttributes)

Aggregations

CqAttributesFactory (org.apache.geode.cache.query.CqAttributesFactory)11 CqEvent (org.apache.geode.cache.query.CqEvent)11 CqQuery (org.apache.geode.cache.query.CqQuery)10 CqAttributes (org.apache.geode.cache.query.CqAttributes)8 QueryService (org.apache.geode.cache.query.QueryService)6 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)6 Test (org.junit.Test)6 CqListenerAdapter (org.apache.geode.cache.util.CqListenerAdapter)5 Region (org.apache.geode.cache.Region)4 CqListener (org.apache.geode.cache.query.CqListener)4 ClientCache (org.apache.geode.cache.client.ClientCache)3 Pool (org.apache.geode.cache.client.Pool)3 SecurityTestUtil.createClientCache (org.apache.geode.security.SecurityTestUtil.createClientCache)3 SecurityTestUtil.createProxyRegion (org.apache.geode.security.SecurityTestUtil.createProxyRegion)3 SecurityTest (org.apache.geode.test.junit.categories.SecurityTest)3 MyObject (com.examples.snapshot.MyObject)2 File (java.io.File)2 Properties (java.util.Properties)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AttributesFactory (org.apache.geode.cache.AttributesFactory)2