Search in sources :

Example 56 with CqQuery

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

the class CqStateDUnitTest method testBug51222.

// this test is disabled due to a 25% failure rate in
// CI testing. See internal ticket #52229
@Ignore("TODO: test is disabled due to flickering")
@Test
public void testBug51222() throws Exception {
    // The client can log this when the server shuts down.
    IgnoredException.addIgnoredException("Could not find any server");
    IgnoredException.addIgnoredException("java.net.ConnectException");
    final String cqName = "theCqInQuestion";
    final String regionName = "aattbbss";
    final Host host = Host.getHost(0);
    VM serverA = host.getVM(1);
    VM serverB = host.getVM(2);
    VM client = host.getVM(3);
    final int[] ports = AvailablePortHelper.getRandomAvailableTCPPorts(2);
    startCacheServer(serverA, ports[0], getAuthenticatedServerProperties());
    createReplicatedRegion(serverA, regionName, null);
    final String host0 = NetworkUtils.getServerHostName(serverA.getHost());
    startClient(client, new VM[] { serverA, serverB }, ports, 1, getClientProperties());
    createCQ(client, cqName, "select * from /" + regionName, null);
    // create the cacheserver but regions must be present first or else cq execute will fail with no
    // region found
    createCacheServer(serverB, ports[1], getServerProperties(0));
    createReplicatedRegion(serverB, regionName, null);
    startCacheServers(serverB);
    AsyncInvocation async = executeCQ(client, cqName);
    ThreadUtils.join(async, 10000);
    Boolean clientRunning = (Boolean) client.invoke(new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            final CqQuery cq = getCache().getQueryService().getCq(cqName);
            Wait.waitForCriterion(new WaitCriterion() {

                @Override
                public boolean done() {
                    return cq.getState().isRunning();
                }

                @Override
                public String description() {
                    return "waiting for Cq to be in a running state: " + cq;
                }
            }, 30000, 1000, false);
            return cq.getState().isRunning();
        }
    });
    assertTrue("Client was not running", clientRunning);
    // hope that server 2 comes up before num retries is exhausted by the execute cq command
    // hope that the redundancy satisfier sends message and is executed after execute cq has been
    // executed
    // This is the only way bug 51222 would be noticed
    // verify that the cq on the server is still in RUNNING state;
    Boolean isRunning = (Boolean) serverB.invoke(new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            CqQuery cq = getCache().getQueryService().getCqs()[0];
            return cq.getState().isRunning();
        }
    });
    assertTrue("Cq was not running on server", isRunning);
}
Also used : WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Host(org.apache.geode.test.dunit.Host) CqQuery(org.apache.geode.cache.query.CqQuery) AsyncInvocation(org.apache.geode.test.dunit.AsyncInvocation) Ignore(org.junit.Ignore) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 57 with CqQuery

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

the class PartitionedRegionCqQueryDUnitTest method getCqs.

private static String[] getCqs() {
    CqQuery[] cqs = CacheFactory.getAnyInstance().getQueryService().getCqs();
    String[] cqnames = new String[cqs.length];
    int idx = 0;
    for (CqQuery cq : cqs) {
        cqnames[idx++] = cq.getName();
    }
    return cqnames;
}
Also used : CqQuery(org.apache.geode.cache.query.CqQuery)

Example 58 with CqQuery

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

the class PartitionedRegionCqQueryDUnitTest method testEventsDuringQueryExecution.

/**
   * Test for events created during the CQ query execution. When CQs are executed using
   * executeWithInitialResults there may be possibility that the region changes during that time may
   * not be reflected in the query result set thus making the query data and region data
   * inconsistent.
   */
@Test
public void testEventsDuringQueryExecution() throws Exception {
    final Host host = Host.getHost(0);
    VM server1 = host.getVM(0);
    VM server2 = host.getVM(1);
    VM client = host.getVM(2);
    final String cqName = "testEventsDuringQueryExecution_0";
    // Server.
    createServer(server1);
    createServer(server2);
    final int port = server1.invoke(() -> PartitionedRegionCqQueryDUnitTest.getCacheServerPort());
    final String host0 = NetworkUtils.getServerHostName(server1.getHost());
    // Initialize Client.
    createClient(client, port, host0);
    // create CQ.
    createCQ(client, cqName, cqs[0]);
    final int numObjects = 200;
    final int totalObjects = 500;
    // initialize Region.
    server1.invoke(new CacheSerializableRunnable("Update Region") {

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

        public void run2() throws CacheException {
            Region region = getCache().getRegion("/root/" + regions[0]);
            for (int i = numObjects + 1; i <= totalObjects; i++) {
                Portfolio p = new Portfolio(i);
                region.put("" + i, p);
            }
        }
    });
    // Execute CQ while update is in progress.
    client.invoke(new CacheSerializableRunnable("Execute CQ") {

        public void run2() throws CacheException {
            QueryService cqService = getCache().getQueryService();
            // Get CqQuery object.
            CqQuery cq1 = cqService.getCq(cqName);
            if (cq1 == null) {
                fail("Failed to get CQ " + cqName);
            }
            SelectResults cqResults = null;
            try {
                cqResults = cq1.executeWithInitialResults();
            } catch (Exception ex) {
                fail("Failed to execute  CQ " + cqName, ex);
            }
            CqQueryTestListener cqListener = (CqQueryTestListener) cq1.getCqAttributes().getCqListener();
            // Wait for the last key to arrive.
            for (int i = 0; i < 4; i++) {
                try {
                    cqListener.waitForCreated("" + totalObjects);
                    // Found skip from the loop.
                    break;
                } catch (CacheException ex) {
                    if (i == 3) {
                        throw ex;
                    }
                }
            }
            // 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);
            }
        }
    });
    cqHelper.closeClient(client);
    cqHelper.closeServer(server2);
    cqHelper.closeServer(server1);
}
Also used : CacheException(org.apache.geode.cache.CacheException) Portfolio(org.apache.geode.cache.query.data.Portfolio) Host(org.apache.geode.test.dunit.Host) IOException(java.io.IOException) CacheException(org.apache.geode.cache.CacheException) 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) LocalRegion(org.apache.geode.internal.cache.LocalRegion) 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 59 with CqQuery

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

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

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