Search in sources :

Example 86 with CacheException

use of org.apache.geode.cache.CacheException in project geode by apache.

the class CqQueryUsingPoolDUnitTest method createCQ.

/* Register CQs */
public void createCQ(VM vm, final String poolName, final String cqName, final String queryStr) {
    vm.invoke(new CacheSerializableRunnable("Create CQ :" + cqName) {

        @Override
        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 cq1 = qService.newCq(cqName, queryStr, cqa);
                assertTrue("newCq() state mismatch", cq1.getState().isStopped());
            } catch (Exception ex) {
                LogWriterUtils.getLogWriter().info("QueryService is :" + qService, ex);
                Assert.fail("Failed to create CQ " + cqName + " . ", ex);
            }
        }
    });
}
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) 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 87 with CacheException

use of org.apache.geode.cache.CacheException in project geode by apache.

the class DurableClientSimpleDUnitTest method testRejectClientWhenDrainingCq.

/**
   * Tests situation where a client is trying to reconnect while a cq is being drained. The client
   * should be rejected until no cqs are currently being drained
   */
@Test
public void testRejectClientWhenDrainingCq() throws Exception {
    try {
        IgnoredException.addIgnoredException(LocalizedStrings.CacheClientNotifier_COULD_NOT_CONNECT_DUE_TO_CQ_BEING_DRAINED.toLocalizedString());
        IgnoredException.addIgnoredException("Could not initialize a primary queue on startup. No queue servers available.");
        String greaterThan5Query = "select * from /" + regionName + " p where p.ID > 5";
        String allQuery = "select * from /" + regionName + " p where p.ID > -1";
        String lessThan5Query = "select * from /" + regionName + " p where p.ID < 5";
        // Start server 1
        Integer[] ports = ((Integer[]) this.server1VM.invoke(() -> CacheServerTestUtil.createCacheServerReturnPorts(regionName, new Boolean(true))));
        final int serverPort = ports[0].intValue();
        final String durableClientId = getName() + "_client";
        this.durableClientVM.invoke(() -> CacheServerTestUtil.disableShufflingOfEndpoints());
        startDurableClient(durableClientVM, durableClientId, serverPort, regionName);
        // register durable cqs
        createCq(durableClientVM, "GreaterThan5", greaterThan5Query, true);
        createCq(durableClientVM, "All", allQuery, true);
        createCq(durableClientVM, "LessThan5", lessThan5Query, true);
        // send client ready
        sendClientReady(durableClientVM);
        verifyDurableClientOnServer(server1VM, durableClientId);
        // Stop the durable client
        this.disconnectDurableClient(true);
        // Start normal publisher client
        startClient(publisherClientVM, serverPort, regionName);
        // Publish some entries
        publishEntries(publisherClientVM, regionName, 10);
        this.server1VM.invoke(new CacheSerializableRunnable("Set test hook") {

            @Override
            public void run2() throws CacheException {
                // Set the Test Hook!
                // This test hook will pause during the drain process
                CacheClientProxy.testHook = new RejectClientReconnectTestHook();
            }
        });
        this.server1VM.invokeAsync(new CacheSerializableRunnable("Close cq for durable client") {

            @Override
            public void run2() throws CacheException {
                final CacheClientNotifier ccnInstance = CacheClientNotifier.getInstance();
                final CacheClientProxy clientProxy = ccnInstance.getClientProxy(durableClientId);
                ClientProxyMembershipID proxyId = clientProxy.getProxyID();
                try {
                    ccnInstance.closeClientCq(durableClientId, "All");
                } catch (CqException e) {
                    fail("failed", e);
                }
            }
        });
        // Restart the durable client
        startDurableClient(durableClientVM, durableClientId, serverPort, regionName);
        this.server1VM.invoke(new CacheSerializableRunnable("verify was rejected at least once") {

            @Override
            public void run2() throws CacheException {
                WaitCriterion ev = new WaitCriterion() {

                    @Override
                    public boolean done() {
                        return CacheClientProxy.testHook != null && (((RejectClientReconnectTestHook) CacheClientProxy.testHook).wasClientRejected());
                    }

                    @Override
                    public String description() {
                        return null;
                    }
                };
                Wait.waitForCriterion(ev, 10 * 1000, 200, true);
                assertTrue(((RejectClientReconnectTestHook) CacheClientProxy.testHook).wasClientRejected());
            }
        });
        checkPrimaryUpdater(durableClientVM);
        // After rejection, the client will retry and eventually connect
        // Verify durable client on server2
        verifyDurableClientOnServer(server1VM, durableClientId);
        createCq(durableClientVM, "GreaterThan5", "select * from /" + regionName + " p where p.ID > 5", true);
        createCq(durableClientVM, "All", "select * from /" + regionName + " p where p.ID > -1", true);
        createCq(durableClientVM, "LessThan5", "select * from /" + regionName + " p where p.ID < 5", true);
        // send client ready
        sendClientReady(durableClientVM);
        checkCqListenerEvents(durableClientVM, "GreaterThan5", 4, /* numEventsExpected */
        4, /* numEventsToWaitFor */
        15);
        checkCqListenerEvents(durableClientVM, "LessThan5", 5, /* numEventsExpected */
        5, /* numEventsToWaitFor */
        15);
        checkCqListenerEvents(durableClientVM, "All", 0, /* numEventsExpected */
        1, /* numEventsToWaitFor */
        5);
        // Stop the durable client
        this.durableClientVM.invoke(() -> CacheServerTestUtil.closeCache());
        // Stop the publisher client
        this.publisherClientVM.invoke(() -> CacheServerTestUtil.closeCache());
        // Stop the server
        this.server1VM.invoke(() -> CacheServerTestUtil.closeCache());
    } finally {
        this.server1VM.invoke(new CacheSerializableRunnable("unset test hook") {

            @Override
            public void run2() throws CacheException {
                CacheClientProxy.testHook = null;
            }
        });
    }
}
Also used : CacheException(org.apache.geode.cache.CacheException) CqException(org.apache.geode.cache.query.CqException) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) ClientSubscriptionTest(org.apache.geode.test.junit.categories.ClientSubscriptionTest) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 88 with CacheException

use of org.apache.geode.cache.CacheException in project geode by apache.

the class DurableClientTestCase method durableFailover.

/**
   * Test a durable client with 2 servers where the client fails over from one to another server
   * with a publisher/feeder performing operations and the client verifying updates received.
   * Redundancy level is set to 1 for this test case.
   */
public void durableFailover(int redundancyLevel) throws InterruptedException {
    // Start server 1
    Integer[] ports = ((Integer[]) this.server1VM.invoke(() -> CacheServerTestUtil.createCacheServerReturnPorts(regionName, new Boolean(true))));
    final int server1Port = ports[0].intValue();
    // Start server 2 using the same mcast port as server 1
    final int server2Port = ((Integer) this.server2VM.invoke(() -> CacheServerTestUtil.createCacheServer(regionName, new Boolean(true)))).intValue();
    // Stop server 2
    this.server2VM.invoke(() -> CacheServerTestUtil.closeCache());
    // Start a durable client
    final String durableClientId = getName() + "_client";
    // keep the client alive for 60 seconds
    final int durableClientTimeout = 60;
    Pool clientPool;
    if (redundancyLevel == 1) {
        clientPool = getClientPool(NetworkUtils.getServerHostName(Host.getHost(0)), server1Port, server2Port, true);
    } else {
        clientPool = getClientPool(NetworkUtils.getServerHostName(Host.getHost(0)), server1Port, server2Port, true, 0);
    }
    this.durableClientVM.invoke(() -> CacheServerTestUtil.disableShufflingOfEndpoints());
    this.durableClientVM.invoke(() -> CacheServerTestUtil.createCacheClient(clientPool, regionName, getClientDistributedSystemProperties(durableClientId, durableClientTimeout), Boolean.TRUE));
    // Send clientReady message
    this.durableClientVM.invoke(new CacheSerializableRunnable("Send clientReady") {

        public void run2() throws CacheException {
            CacheServerTestUtil.getCache().readyForEvents();
        }
    });
    // Have the durable client register interest in all keys
    this.durableClientVM.invoke(new CacheSerializableRunnable("Register interest") {

        public void run2() throws CacheException {
            // Get the region
            Region region = CacheServerTestUtil.getCache().getRegion(regionName);
            assertNotNull(region);
            // Register interest in all keys
            region.registerInterestRegex(".*", InterestResultPolicy.NONE, true);
        }
    });
    // Re-start server2
    this.server2VM.invoke(() -> CacheServerTestUtil.createCacheServer(regionName, new Boolean(true), new Integer(server2Port)));
    // Start normal publisher client
    this.publisherClientVM.invoke(() -> CacheServerTestUtil.createCacheClient(getClientPool(NetworkUtils.getServerHostName(publisherClientVM.getHost()), server1Port, server2Port, false), regionName));
    // Publish some entries
    final int numberOfEntries = 1;
    publishEntries(numberOfEntries);
    // Verify the durable client received the updates
    this.verifyListenerUpdates(numberOfEntries);
    try {
        java.lang.Thread.sleep(5000);
    } catch (java.lang.InterruptedException ex) {
        fail("interrupted");
    }
    // Stop the durable client
    this.disconnectDurableClient(true);
    // Publish updates during client downtime
    publishEntries(numberOfEntries);
    // Re-start the durable client that is kept alive on the server
    this.restartDurableClient(new Object[] { clientPool, regionName, getClientDistributedSystemProperties(durableClientId, durableClientTimeout), Boolean.TRUE });
    // Have the durable client register interest in all keys
    this.durableClientVM.invoke(new CacheSerializableRunnable("Register interest") {

        public void run2() throws CacheException {
            // Get the region
            Region region = CacheServerTestUtil.getCache().getRegion(regionName);
            assertNotNull(region);
            // Register interest in all keys
            region.registerInterestRegex(".*", InterestResultPolicy.NONE, true);
        }
    });
    // Publish second round of updates
    this.publisherClientVM.invoke(new CacheSerializableRunnable("Publish entries before failover") {

        public void run2() throws CacheException {
            // Get the region
            Region region = CacheServerTestUtil.getCache().getRegion(regionName);
            assertNotNull(region);
            // Publish some entries
            for (int i = 1; i < numberOfEntries + 1; i++) {
                String keyAndValue = String.valueOf(i);
                region.put(keyAndValue, keyAndValue);
            }
        }
    });
    // Verify the durable client received the updates before failover
    this.verifyListenerUpdates(numberOfEntries + 1, numberOfEntries);
    try {
        java.lang.Thread.sleep(1000);
    } catch (java.lang.InterruptedException ex) {
        fail("interrupted");
    }
    setPrimaryRecoveryCheck();
    // Stop server 1 - publisher will put 10 entries during shutdown/primary identification
    this.server1VM.invoke(() -> CacheServerTestUtil.closeCache());
    this.durableClientVM.invoke(new CacheSerializableRunnable("Get") {

        public void run2() throws CacheException {
            // Get the region
            Region region = CacheServerTestUtil.getCache().getRegion(regionName);
            assertNotNull(region);
            assertNull(region.getEntry("0"));
        }
    });
    checkPrimaryRecovery();
    // Publish second round of updates after failover
    this.publisherClientVM.invoke(new CacheSerializableRunnable("Publish entries after failover") {

        public void run2() throws CacheException {
            // Get the region
            Region region = CacheServerTestUtil.getCache().getRegion(regionName);
            assertNotNull(region);
            // Publish some entries
            for (int i = 2; i < numberOfEntries + 2; i++) {
                String keyAndValue = String.valueOf(i);
                region.put(keyAndValue, keyAndValue);
            }
        }
    });
    // Verify the durable client received the updates after failover
    this.verifyListenerUpdates(numberOfEntries + 2, numberOfEntries);
    // Stop the durable client
    this.durableClientVM.invoke(() -> CacheServerTestUtil.closeCache());
    // Stop the publisher client
    this.publisherClientVM.invoke(() -> CacheServerTestUtil.closeCache());
    // Stop server 2
    this.server2VM.invoke(() -> CacheServerTestUtil.closeCache());
}
Also used : CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) CacheException(org.apache.geode.cache.CacheException) Region(org.apache.geode.cache.Region) Pool(org.apache.geode.cache.client.Pool)

Example 89 with CacheException

use of org.apache.geode.cache.CacheException in project geode by apache.

the class DurableClientTestCase method flushEntries.

/*
   * Due to the way removal from ha region queue is implemented a dummy cq or interest needs to be
   * created and a dummy value used so that none of the actual cqs will be triggered and yet an
   * event will flush the queue
   */
protected void flushEntries(VM server, VM client, final String regionName) {
    // ack counts
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
    registerInterest(client, regionName, false);
    server.invoke(new CacheSerializableRunnable("flush entries") {

        public void run2() throws CacheException {
            CqService service = ((InternalCache) CacheServerTestUtil.getCache()).getCqService();
            // Get the region
            Region region = CacheServerTestUtil.getCache().getRegion(regionName);
            assertNotNull(region);
            region.put("LAST", "ENTRY");
        }
    });
}
Also used : CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) CacheException(org.apache.geode.cache.CacheException) Region(org.apache.geode.cache.Region) CqService(org.apache.geode.cache.query.internal.cq.CqService)

Example 90 with CacheException

use of org.apache.geode.cache.CacheException in project geode by apache.

the class DurableClientTestCase method testStartStopStartDurableClient.

/**
   * Test that starting, stopping then restarting a durable client is correctly processed by the
   * server.
   */
@Test
public void testStartStopStartDurableClient() {
    // Start a server
    int serverPort = ((Integer) this.server1VM.invoke(() -> CacheServerTestUtil.createCacheServer(regionName, new Boolean(true)))).intValue();
    // Start a durable client that is kept alive on the server when it stops
    // normally
    final String durableClientId = getName() + "_client";
    // keep the client alive for 60 seconds
    final int durableClientTimeout = 60;
    // final boolean durableClientKeepAlive = true; // keep the client alive when it stops normally
    this.durableClientVM.invoke(() -> CacheServerTestUtil.createCacheClient(getClientPool(NetworkUtils.getServerHostName(durableClientVM.getHost()), serverPort, true), regionName, getClientDistributedSystemProperties(durableClientId, durableClientTimeout)));
    // Send clientReady message
    this.durableClientVM.invoke(new CacheSerializableRunnable("Send clientReady") {

        public void run2() throws CacheException {
            CacheServerTestUtil.getCache().readyForEvents();
        }
    });
    // Verify durable client on server
    this.server1VM.invoke(new CacheSerializableRunnable("Verify durable client") {

        public void run2() throws CacheException {
            // Find the proxy
            checkNumberOfClientProxies(1);
            CacheClientProxy proxy = getClientProxy();
            assertNotNull(proxy);
            // Verify that it is durable and its properties are correct
            assertTrue(proxy.isDurable());
            assertEquals(durableClientId, proxy.getDurableId());
            assertEquals(durableClientTimeout, proxy.getDurableTimeout());
        // assertIndexDetailsEquals(durableClientKeepAlive, proxy.getDurableKeepAlive());
        }
    });
    // Stop the durable client
    this.disconnectDurableClient(true);
    // Verify the durable client still exists on the server
    this.server1VM.invoke(new CacheSerializableRunnable("Verify durable client") {

        public void run2() throws CacheException {
            // Find the proxy
            CacheClientProxy proxy = getClientProxy();
            assertNotNull(proxy);
        }
    });
    // Re-start the durable client
    this.restartDurableClient(new Object[] { getClientPool(NetworkUtils.getServerHostName(durableClientVM.getHost()), serverPort, true), regionName, getClientDistributedSystemProperties(durableClientId, durableClientTimeout) });
    // Verify durable client on server
    this.server1VM.invoke(new CacheSerializableRunnable("Verify durable client") {

        public void run2() throws CacheException {
            // Find the proxy
            checkNumberOfClientProxies(1);
            CacheClientProxy proxy = getClientProxy();
            assertNotNull(proxy);
            // Verify that it is durable and its properties are correct
            assertTrue(proxy.isDurable());
            assertEquals(durableClientId, proxy.getDurableId());
            assertEquals(durableClientTimeout, proxy.getDurableTimeout());
        }
    });
    // Stop the durable client
    this.durableClientVM.invoke(() -> CacheServerTestUtil.closeCache());
    // Stop the server
    this.server1VM.invoke(() -> CacheServerTestUtil.closeCache());
}
Also used : CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) CacheException(org.apache.geode.cache.CacheException) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) Test(org.junit.Test)

Aggregations

CacheException (org.apache.geode.cache.CacheException)638 Test (org.junit.Test)445 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)407 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)376 VM (org.apache.geode.test.dunit.VM)375 Region (org.apache.geode.cache.Region)351 Host (org.apache.geode.test.dunit.Host)330 AttributesFactory (org.apache.geode.cache.AttributesFactory)204 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)201 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)191 IOException (java.io.IOException)124 LocalRegion (org.apache.geode.internal.cache.LocalRegion)118 QueryService (org.apache.geode.cache.query.QueryService)107 PartitionAttributesFactory (org.apache.geode.cache.PartitionAttributesFactory)85 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)85 SelectResults (org.apache.geode.cache.query.SelectResults)80 IgnoredException (org.apache.geode.test.dunit.IgnoredException)72 ClientSubscriptionTest (org.apache.geode.test.junit.categories.ClientSubscriptionTest)64 AsyncInvocation (org.apache.geode.test.dunit.AsyncInvocation)61 Cache (org.apache.geode.cache.Cache)59