Search in sources :

Example 1 with ServerCQImpl

use of org.apache.geode.cache.query.internal.cq.ServerCQImpl in project geode by apache.

the class ExecuteCQ61 method cmdExecute.

@Override
public void cmdExecute(Message clientMessage, ServerConnection serverConnection, long start) throws IOException, InterruptedException {
    AcceptorImpl acceptor = serverConnection.getAcceptor();
    CachedRegionHelper crHelper = serverConnection.getCachedRegionHelper();
    ClientProxyMembershipID id = serverConnection.getProxyID();
    CacheServerStats stats = serverConnection.getCacheServerStats();
    serverConnection.setAsTrue(REQUIRES_RESPONSE);
    serverConnection.setAsTrue(REQUIRES_CHUNKED_RESPONSE);
    // Retrieve the data from the message parts
    String cqName = clientMessage.getPart(0).getString();
    String cqQueryString = clientMessage.getPart(1).getString();
    int cqState = clientMessage.getPart(2).getInt();
    Part isDurablePart = clientMessage.getPart(3);
    byte[] isDurableByte = isDurablePart.getSerializedForm();
    boolean isDurable = (isDurableByte == null || isDurableByte[0] == 0) ? false : true;
    // region data policy
    Part regionDataPolicyPart = clientMessage.getPart(clientMessage.getNumberOfParts() - 1);
    byte[] regionDataPolicyPartBytes = regionDataPolicyPart.getSerializedForm();
    if (logger.isDebugEnabled()) {
        logger.debug("{}: Received {} request from {} CqName: {} queryString: {}", serverConnection.getName(), MessageType.getString(clientMessage.getMessageType()), serverConnection.getSocketString(), cqName, cqQueryString);
    }
    // Check if the Server is running in NotifyBySubscription=true mode.
    CacheClientNotifier ccn = acceptor.getCacheClientNotifier();
    if (ccn != null) {
        CacheClientProxy proxy = ccn.getClientProxy(id);
        if (proxy != null && !proxy.isNotifyBySubscription()) {
            // This should have been taken care at the client.
            String err = LocalizedStrings.ExecuteCQ_SERVER_NOTIFYBYSUBSCRIPTION_MODE_IS_SET_TO_FALSE_CQ_EXECUTION_IS_NOT_SUPPORTED_IN_THIS_MODE.toLocalizedString();
            sendCqResponse(MessageType.CQDATAERROR_MSG_TYPE, err, clientMessage.getTransactionId(), null, serverConnection);
            return;
        }
    }
    DefaultQueryService qService = null;
    CqServiceImpl cqServiceForExec = null;
    Query query = null;
    Set cqRegionNames = null;
    ExecuteCQOperationContext executeCQContext = null;
    ServerCQImpl cqQuery = null;
    try {
        qService = (DefaultQueryService) crHelper.getCache().getLocalQueryService();
        // Authorization check
        AuthorizeRequest authzRequest = serverConnection.getAuthzRequest();
        if (authzRequest != null) {
            query = qService.newQuery(cqQueryString);
            cqRegionNames = ((DefaultQuery) query).getRegionsInQuery(null);
            executeCQContext = authzRequest.executeCQAuthorize(cqName, cqQueryString, cqRegionNames);
            String newCqQueryString = executeCQContext.getQuery();
            if (!cqQueryString.equals(newCqQueryString)) {
                query = qService.newQuery(newCqQueryString);
                cqQueryString = newCqQueryString;
                cqRegionNames = executeCQContext.getRegionNames();
                if (cqRegionNames == null) {
                    cqRegionNames = ((DefaultQuery) query).getRegionsInQuery(null);
                }
            }
        }
        if (CqServiceProvider.VMOTION_DURING_CQ_REGISTRATION_FLAG) {
            VMotionObserver vmo = VMotionObserverHolder.getInstance();
            vmo.vMotionBeforeCQRegistration();
        }
        cqServiceForExec = (CqServiceImpl) qService.getCqService();
        // registering cq with serverConnection so that when CCP will require auth info it can access
        // that
        // registering cq auth before as possibility that you may get event
        serverConnection.setCq(cqName, isDurable);
        cqQuery = (ServerCQImpl) cqServiceForExec.executeCq(cqName, cqQueryString, cqState, id, ccn, isDurable, true, regionDataPolicyPartBytes[0], null);
    } catch (CqException cqe) {
        sendCqResponse(MessageType.CQ_EXCEPTION_TYPE, "", clientMessage.getTransactionId(), cqe, serverConnection);
        serverConnection.removeCq(cqName, isDurable);
        return;
    } catch (Exception e) {
        writeChunkedException(clientMessage, e, serverConnection);
        serverConnection.removeCq(cqName, isDurable);
        return;
    }
    long oldstart = start;
    boolean sendResults = false;
    boolean successQuery = false;
    if (clientMessage.getMessageType() == MessageType.EXECUTECQ_WITH_IR_MSG_TYPE) {
        sendResults = true;
    }
    // if it is a non PR query with execute query and maintain keys flags set
    if (sendResults || (CqServiceImpl.EXECUTE_QUERY_DURING_INIT && CqServiceProvider.MAINTAIN_KEYS && !cqQuery.isPR())) {
        // Execute the query and send the result-set to client.
        try {
            if (query == null) {
                query = qService.newQuery(cqQueryString);
                cqRegionNames = ((DefaultQuery) query).getRegionsInQuery(null);
            }
            ((DefaultQuery) query).setIsCqQuery(true);
            successQuery = processQuery(clientMessage, query, cqQueryString, cqRegionNames, start, cqQuery, executeCQContext, serverConnection, sendResults);
            // Update the CQ statistics.
            cqQuery.getVsdStats().setCqInitialResultsTime((DistributionStats.getStatTime()) - oldstart);
            stats.incProcessExecuteCqWithIRTime((DistributionStats.getStatTime()) - oldstart);
        // logger.fine("Time spent in execute with initial results :" +
        // DistributionStats.getStatTime() + ", " + oldstart);
        } finally {
            // If failure to execute the query, close the CQ.
            if (!successQuery) {
                try {
                    cqServiceForExec.closeCq(cqName, id);
                } catch (Exception ex) {
                // Ignore.
                }
            }
        }
    } else {
        // Don't execute query for cq.execute and
        // if it is a PR query with execute query and maintain keys flags not set
        cqQuery.cqResultKeysInitialized = true;
        successQuery = true;
    }
    if (!sendResults && successQuery) {
        // Send OK to client
        sendCqResponse(MessageType.REPLY, LocalizedStrings.ExecuteCQ_CQ_CREATED_SUCCESSFULLY.toLocalizedString(), clientMessage.getTransactionId(), null, serverConnection);
        long start2 = DistributionStats.getStatTime();
        stats.incProcessCreateCqTime(start2 - oldstart);
    }
    serverConnection.setAsTrue(RESPONDED);
}
Also used : CacheClientProxy(org.apache.geode.internal.cache.tier.sockets.CacheClientProxy) Set(java.util.Set) DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) Query(org.apache.geode.cache.query.Query) DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) CacheClientNotifier(org.apache.geode.internal.cache.tier.sockets.CacheClientNotifier) AuthorizeRequest(org.apache.geode.internal.security.AuthorizeRequest) CqException(org.apache.geode.cache.query.CqException) VMotionObserver(org.apache.geode.internal.cache.vmotion.VMotionObserver) ServerCQImpl(org.apache.geode.cache.query.internal.cq.ServerCQImpl) CqException(org.apache.geode.cache.query.CqException) IOException(java.io.IOException) CqServiceImpl(org.apache.geode.cache.query.internal.cq.CqServiceImpl) CachedRegionHelper(org.apache.geode.internal.cache.tier.CachedRegionHelper) ClientProxyMembershipID(org.apache.geode.internal.cache.tier.sockets.ClientProxyMembershipID) CacheServerStats(org.apache.geode.internal.cache.tier.sockets.CacheServerStats) Part(org.apache.geode.internal.cache.tier.sockets.Part) AcceptorImpl(org.apache.geode.internal.cache.tier.sockets.AcceptorImpl) DefaultQueryService(org.apache.geode.cache.query.internal.DefaultQueryService) ExecuteCQOperationContext(org.apache.geode.cache.operations.ExecuteCQOperationContext)

Example 2 with ServerCQImpl

use of org.apache.geode.cache.query.internal.cq.ServerCQImpl in project geode by apache.

the class CqPerfDUnitTest method testKeyMaintainance.

/**
   * Test for maintaining keys for update optimization.
   * 
   * @throws Exception
   */
@Test
public void testKeyMaintainance() 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());
    cqDUnitTest.createClient(client, port, host0);
    // HashSet for caching purpose will be created for cqs.
    final int cqSize = 2;
    // Cq1
    cqDUnitTest.createCQ(client, "testKeyMaintainance_0", cqDUnitTest.cqs[0]);
    cqDUnitTest.executeCQ(client, "testKeyMaintainance_0", false, null);
    // Cq2
    cqDUnitTest.createCQ(client, "testKeyMaintainance_1", cqDUnitTest.cqs[10]);
    cqDUnitTest.executeCQ(client, "testKeyMaintainance_1", false, null);
    cqDUnitTest.createValues(server, cqDUnitTest.regions[0], 1);
    cqDUnitTest.waitForCreated(client, "testKeyMaintainance_0", CqQueryDUnitTest.KEY + 1);
    // Entry is made into the CQs cache hashset.
    // testKeyMaintainance_0 with 1 entry and testKeyMaintainance_1 with 0
    server.invoke(new CacheSerializableRunnable("LookForCachedEventKeys1") {

        public void run2() throws CacheException {
            CqService cqService = null;
            try {
                cqService = ((DefaultQueryService) getCache().getQueryService()).getCqService();
            } catch (Exception ex) {
                LogWriterUtils.getLogWriter().info("Failed to get the internal CqService.", ex);
                Assert.fail("Failed to get the internal CqService.", ex);
            }
            Collection<? extends InternalCqQuery> cqs = cqService.getAllCqs();
            for (InternalCqQuery cq : cqs) {
                ServerCQImpl cqQuery = (ServerCQImpl) cq;
                String serverCqName = (String) cqQuery.getServerCqName();
                if (serverCqName.startsWith("testKeyMaintainance_0")) {
                    assertEquals("The number of keys cached for cq testKeyMaintainance_0 is wrong.", 1, cqQuery.getCqResultKeysSize());
                } else if (serverCqName.startsWith("testKeyMaintainance_1")) {
                    assertEquals("The number of keys cached for cq testKeyMaintainance_1 is wrong.", 0, cqQuery.getCqResultKeysSize());
                }
            }
        }
    });
    // Update 1.
    cqDUnitTest.createValues(server, cqDUnitTest.regions[0], 10);
    cqDUnitTest.waitForCreated(client, "testKeyMaintainance_0", CqQueryDUnitTest.KEY + 10);
    // Entry/check is made into the CQs cache hashset.
    // testKeyMaintainance_0 with 1 entry and testKeyMaintainance_1 with 1
    server.invoke(new CacheSerializableRunnable("LookForCachedEventKeysAfterUpdate1") {

        public void run2() throws CacheException {
            CqService cqService = null;
            try {
                cqService = ((DefaultQueryService) getCache().getQueryService()).getCqService();
            } catch (Exception ex) {
                LogWriterUtils.getLogWriter().info("Failed to get the internal CqService.", ex);
                Assert.fail("Failed to get the internal CqService.", ex);
            }
            Collection<? extends InternalCqQuery> cqs = cqService.getAllCqs();
            for (InternalCqQuery cq : cqs) {
                ServerCQImpl cqQuery = (ServerCQImpl) cq;
                String serverCqName = (String) cqQuery.getServerCqName();
                if (serverCqName.startsWith("testKeyMaintainance_0")) {
                    assertEquals("The number of keys cached for cq testKeyMaintainance_0 is wrong.", 10, cqQuery.getCqResultKeysSize());
                } else if (serverCqName.startsWith("testKeyMaintainance_1")) {
                    assertEquals("The number of keys cached for cq testKeyMaintainance_1 is wrong.", 5, cqQuery.getCqResultKeysSize());
                }
            }
        }
    });
    // Update.
    cqDUnitTest.createValues(server, cqDUnitTest.regions[0], 12);
    cqDUnitTest.waitForCreated(client, "testKeyMaintainance_0", CqQueryDUnitTest.KEY + 12);
    // Entry/check is made into the CQs cache hashset.
    // testKeyMaintainance_0 with 1 entry and testKeyMaintainance_1 with 1
    server.invoke(new CacheSerializableRunnable("LookForCachedEventKeysAfterUpdate2") {

        public void run2() throws CacheException {
            CqService cqService = null;
            try {
                cqService = ((DefaultQueryService) getCache().getQueryService()).getCqService();
            } catch (Exception ex) {
                LogWriterUtils.getLogWriter().info("Failed to get the internal CqService.", ex);
                Assert.fail("Failed to get the internal CqService.", ex);
            }
            Collection<? extends InternalCqQuery> cqs = cqService.getAllCqs();
            for (InternalCqQuery cq : cqs) {
                ServerCQImpl cqQuery = (ServerCQImpl) cq;
                String serverCqName = (String) cqQuery.getServerCqName();
                if (serverCqName.startsWith("testKeyMaintainance_0")) {
                    assertEquals("The number of keys cached for cq testKeyMaintainance_0 is wrong.", 12, cqQuery.getCqResultKeysSize());
                } else if (serverCqName.startsWith("testKeyMaintainance_1")) {
                    assertEquals("The number of keys cached for cq testKeyMaintainance_1 is wrong.", 6, cqQuery.getCqResultKeysSize());
                }
            }
        }
    });
    // Delete.
    cqDUnitTest.deleteValues(server, cqDUnitTest.regions[0], 6);
    cqDUnitTest.waitForDestroyed(client, "testKeyMaintainance_0", CqQueryDUnitTest.KEY + 6);
    // Entry/check is made into the CQs cache hashset.
    // testKeyMaintainance_0 with 1 entry and testKeyMaintainance_1 with 1
    server.invoke(new CacheSerializableRunnable("LookForCachedEventKeysAfterUpdate2") {

        public void run2() throws CacheException {
            CqService cqService = null;
            try {
                cqService = ((DefaultQueryService) getCache().getQueryService()).getCqService();
            } catch (Exception ex) {
                LogWriterUtils.getLogWriter().info("Failed to get the internal CqService.", ex);
                Assert.fail("Failed to get the internal CqService.", ex);
            }
            Collection<? extends InternalCqQuery> cqs = cqService.getAllCqs();
            for (InternalCqQuery cq : cqs) {
                ServerCQImpl cqQuery = (ServerCQImpl) cq;
                String serverCqName = (String) cqQuery.getServerCqName();
                if (serverCqName.startsWith("testKeyMaintainance_0")) {
                    assertEquals("The number of keys cached for cq testKeyMaintainance_0 is wrong.", 6, cqQuery.getCqResultKeysSize());
                } else if (serverCqName.startsWith("testKeyMaintainance_1")) {
                    assertEquals("The number of keys cached for cq testKeyMaintainance_1 is wrong.", 3, cqQuery.getCqResultKeysSize());
                }
            }
        }
    });
    // Stop CQ.
    // This should still needs to process the events so that Results are up-to-date.
    cqDUnitTest.stopCQ(client, "testKeyMaintainance_1");
    cqDUnitTest.createValues(server, cqDUnitTest.regions[0], 12);
    server.invoke(new CacheSerializableRunnable("LookForCachedEventKeysAfterUpdate2") {

        public void run2() throws CacheException {
            CqService cqService = null;
            try {
                cqService = ((DefaultQueryService) getCache().getQueryService()).getCqService();
            } catch (Exception ex) {
                LogWriterUtils.getLogWriter().info("Failed to get the internal CqService.", ex);
                Assert.fail("Failed to get the internal CqService.", ex);
            }
            Collection<? extends InternalCqQuery> cqs = cqService.getAllCqs();
            for (InternalCqQuery cq : cqs) {
                ServerCQImpl cqQuery = (ServerCQImpl) cq;
                String serverCqName = (String) cqQuery.getServerCqName();
                if (serverCqName.startsWith("testKeyMaintainance_0")) {
                    assertEquals("The number of keys cached for cq testKeyMaintainance_0 is wrong.", 12, cqQuery.getCqResultKeysSize());
                } else if (serverCqName.startsWith("testKeyMaintainance_1")) {
                    assertEquals("The number of keys cached for cq testKeyMaintainance_1 is wrong.", 6, cqQuery.getCqResultKeysSize());
                }
            }
        }
    });
    // re-start the CQ.
    cqDUnitTest.executeCQ(client, "testKeyMaintainance_1", false, null);
    // This will remove the caching for this CQ.
    cqDUnitTest.closeCQ(client, "testKeyMaintainance_1");
    server.invoke(new CacheSerializableRunnable("LookForCachedEventKeysAfterUpdate2") {

        public void run2() throws CacheException {
            CqService cqService = null;
            try {
                cqService = ((DefaultQueryService) getCache().getQueryService()).getCqService();
            } catch (Exception ex) {
                LogWriterUtils.getLogWriter().info("Failed to get the internal CqService.", ex);
                Assert.fail("Failed to get the internal CqService.", ex);
            }
            Collection<? extends InternalCqQuery> cqs = cqService.getAllCqs();
            for (InternalCqQuery cq : cqs) {
                ServerCQImpl cqQuery = (ServerCQImpl) cq;
                String serverCqName = (String) cqQuery.getServerCqName();
                if (serverCqName.startsWith("testKeyMaintainance_0")) {
                    assertEquals("The number of keys cached for cq testKeyMaintainance_0 is wrong.", 12, cqQuery.getCqResultKeysSize());
                } else if (serverCqName.startsWith("testKeyMaintainance_1")) {
                    fail("The key maintainance should not be present for this CQ.");
                }
            }
        }
    });
    // Close.
    cqDUnitTest.closeClient(client);
    cqDUnitTest.closeServer(server);
}
Also used : CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) CacheException(org.apache.geode.cache.CacheException) VM(org.apache.geode.test.dunit.VM) Collection(java.util.Collection) Host(org.apache.geode.test.dunit.Host) InternalCqQuery(org.apache.geode.cache.query.internal.cq.InternalCqQuery) CqService(org.apache.geode.cache.query.internal.cq.CqService) DefaultQueryService(org.apache.geode.cache.query.internal.DefaultQueryService) ServerCQImpl(org.apache.geode.cache.query.internal.cq.ServerCQImpl) CacheException(org.apache.geode.cache.CacheException) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 3 with ServerCQImpl

use of org.apache.geode.cache.query.internal.cq.ServerCQImpl in project geode by apache.

the class CqResultSetUsingPoolDUnitTest method testCqResultsCachingForMultipleCQs.

/**
   * Tests CQ Result Set.
   * 
   * @throws Exception
   */
@Test
public void testCqResultsCachingForMultipleCQs() throws Exception {
    final Host host = Host.getHost(0);
    VM server = host.getVM(0);
    VM client1 = host.getVM(1);
    VM client2 = host.getVM(2);
    cqDUnitTest.createServer(server);
    final int port = server.invoke(() -> CqQueryUsingPoolDUnitTest.getCacheServerPort());
    final String host0 = NetworkUtils.getServerHostName(server.getHost());
    String poolName = "testCqResults";
    final String cqName1 = "testCqResultsP_0";
    final String cqName2 = "testCqResultsP_1";
    cqDUnitTest.createPool(client1, poolName, host0, port);
    cqDUnitTest.createPool(client2, poolName, host0, port);
    // Create client.
    cqDUnitTest.createClient(client1, port, host0);
    cqDUnitTest.createClient(client2, port, host0);
    // create CQ.
    cqDUnitTest.createCQ(client1, poolName, cqName1, cqDUnitTest.cqs[0]);
    cqDUnitTest.createCQ(client2, poolName, cqName2, cqDUnitTest.cqs[0]);
    final int numObjects = 300;
    final int totalObjects = 500;
    // initialize Region.
    server.invoke(new CacheSerializableRunnable("Update Region") {

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

        public void run2() throws CacheException {
            Region region = getCache().getRegion("/root/" + cqDUnitTest.regions[0]);
            // Update (totalObjects - 1) entries.
            for (int i = 1; i < totalObjects; i++) {
                // Destroy entries.
                if (i > 25 && i < 201) {
                    region.destroy("" + i);
                    continue;
                }
                Portfolio p = new Portfolio(i);
                region.put("" + i, p);
            }
            // recreate destroyed entries.
            for (int j = 26; j < 201; j++) {
                Portfolio p = new Portfolio(j);
                region.put("" + j, p);
            }
            // Add the last key.
            Portfolio p = new Portfolio(totalObjects);
            region.put("" + totalObjects, p);
        }
    });
    // Execute CQ.
    // While region operation is in progress execute CQ.
    cqDUnitTest.executeCQ(client1, cqName1, true, null);
    cqDUnitTest.executeCQ(client2, cqName2, true, null);
    // Verify CQ Cache results.
    server.invoke(new CacheSerializableRunnable("Verify CQ Cache results") {

        public void run2() throws CacheException {
            CqService cqService = null;
            try {
                cqService = ((DefaultQueryService) getCache().getQueryService()).getCqService();
            } catch (Exception ex) {
                LogWriterUtils.getLogWriter().info("Failed to get the internal CqService.", ex);
                Assert.fail("Failed to get the internal CqService.", ex);
            }
            // Wait till all the region update is performed.
            Region region = getCache().getRegion("/root/" + cqDUnitTest.regions[0]);
            while (true) {
                if (region.get("" + totalObjects) == null) {
                    try {
                        Thread.sleep(50);
                    } catch (Exception ex) {
                    // ignore.
                    }
                    continue;
                }
                break;
            }
            Collection<? extends InternalCqQuery> cqs = cqService.getAllCqs();
            for (InternalCqQuery cq : cqs) {
                ServerCQImpl cqQuery = (ServerCQImpl) cq;
                int size = cqQuery.getCqResultKeysSize();
                if (size != totalObjects) {
                    LogWriterUtils.getLogWriter().info("The number of Cached events " + size + " is not equal to the expected size " + totalObjects);
                    HashSet expectedKeys = new HashSet();
                    for (int i = 1; i < totalObjects; i++) {
                        expectedKeys.add("" + i);
                    }
                    Set cachedKeys = cqQuery.getCqResultKeyCache();
                    expectedKeys.removeAll(cachedKeys);
                    LogWriterUtils.getLogWriter().info("Missing keys from the Cache : " + expectedKeys);
                }
                assertEquals("The number of keys cached for cq " + cqQuery.getName() + " is wrong.", totalObjects, cqQuery.getCqResultKeysSize());
            }
        }
    });
    // Close.
    cqDUnitTest.closeClient(client1);
    cqDUnitTest.closeClient(client2);
    cqDUnitTest.closeServer(server);
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) CacheException(org.apache.geode.cache.CacheException) Portfolio(org.apache.geode.cache.query.data.Portfolio) Host(org.apache.geode.test.dunit.Host) CqService(org.apache.geode.cache.query.internal.cq.CqService) ServerCQImpl(org.apache.geode.cache.query.internal.cq.ServerCQImpl) CacheException(org.apache.geode.cache.CacheException) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) Collection(java.util.Collection) InternalCqQuery(org.apache.geode.cache.query.internal.cq.InternalCqQuery) DefaultQueryService(org.apache.geode.cache.query.internal.DefaultQueryService) HashSet(java.util.HashSet) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 4 with ServerCQImpl

use of org.apache.geode.cache.query.internal.cq.ServerCQImpl in project geode by apache.

the class CqResultSetUsingPoolDUnitTest method testCqResultsCachingForPR.

/**
   * Tests CQ Result Set.
   * 
   * @throws Exception
   */
@Test
public void testCqResultsCachingForPR() throws Exception {
    final Host host = Host.getHost(0);
    VM server1 = host.getVM(0);
    VM server2 = host.getVM(1);
    VM client = host.getVM(2);
    cqDUnitTest.createServerWithPR(server1, 0, false, 0);
    cqDUnitTest.createServerWithPR(server2, 0, false, 0);
    final int port = server1.invoke(() -> CqQueryUsingPoolDUnitTest.getCacheServerPort());
    final String host0 = NetworkUtils.getServerHostName(server1.getHost());
    String poolName = "testCqResults";
    final String cqName = "testCqResultsP_0";
    cqDUnitTest.createPool(client, poolName, host0, port);
    // Create client.
    cqDUnitTest.createClient(client, port, host0);
    // create CQ.
    cqDUnitTest.createCQ(client, poolName, cqName, cqDUnitTest.cqs[0]);
    final int numObjects = 300;
    final int totalObjects = 500;
    // initialize Region.
    server1.invoke(new CacheSerializableRunnable("Update Region") {

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

        public void run2() throws CacheException {
            Region region = getCache().getRegion("/root/" + cqDUnitTest.regions[0]);
            // Update (totalObjects - 1) entries.
            for (int i = 1; i < totalObjects; i++) {
                // Destroy entries.
                if (i > 25 && i < 201) {
                    region.destroy("" + i);
                    continue;
                }
                Portfolio p = new Portfolio(i);
                region.put("" + i, p);
            }
            // recreate destroyed entries.
            for (int j = 26; j < 201; j++) {
                Portfolio p = new Portfolio(j);
                region.put("" + j, p);
            }
            // Add the last key.
            Portfolio p = new Portfolio(totalObjects);
            region.put("" + totalObjects, p);
        }
    });
    // Verify CQ Cache results.
    server1.invoke(new CacheSerializableRunnable("Verify CQ Cache results") {

        public void run2() throws CacheException {
            CqService cqService = null;
            try {
                cqService = ((DefaultQueryService) getCache().getQueryService()).getCqService();
            } catch (Exception ex) {
                LogWriterUtils.getLogWriter().info("Failed to get the internal CqService.", ex);
                Assert.fail("Failed to get the internal CqService.", ex);
            }
            Collection<? extends InternalCqQuery> cqs = cqService.getAllCqs();
            for (InternalCqQuery cq : cqs) {
                ServerCQImpl cqQuery = (ServerCQImpl) cq;
                if (cqQuery.getCqResultKeysSize() <= 0) {
                    fail("The Result Cache for CQ on PR is not working. CQ :" + cqName);
                }
            }
        }
    });
    // Close.
    cqDUnitTest.closeClient(client);
    cqDUnitTest.closeServer(server1);
    cqDUnitTest.closeServer(server2);
}
Also used : CacheException(org.apache.geode.cache.CacheException) Portfolio(org.apache.geode.cache.query.data.Portfolio) Host(org.apache.geode.test.dunit.Host) CqService(org.apache.geode.cache.query.internal.cq.CqService) ServerCQImpl(org.apache.geode.cache.query.internal.cq.ServerCQImpl) CacheException(org.apache.geode.cache.CacheException) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) Collection(java.util.Collection) InternalCqQuery(org.apache.geode.cache.query.internal.cq.InternalCqQuery) DefaultQueryService(org.apache.geode.cache.query.internal.DefaultQueryService) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 5 with ServerCQImpl

use of org.apache.geode.cache.query.internal.cq.ServerCQImpl in project geode by apache.

the class CqResultSetUsingPoolDUnitTest method testCqResultsCaching.

/**
   * Tests CQ Result Set.
   * 
   * @throws Exception
   */
@Test
public void testCqResultsCaching() 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(() -> CqQueryUsingPoolDUnitTest.getCacheServerPort());
    final String host0 = NetworkUtils.getServerHostName(server.getHost());
    String poolName = "testCqResults";
    final String cqName = "testCqResultsP_0";
    cqDUnitTest.createPool(client, poolName, host0, port);
    // Create client.
    cqDUnitTest.createClient(client, port, host0);
    // create CQ.
    cqDUnitTest.createCQ(client, poolName, cqName, cqDUnitTest.cqs[0]);
    final int numObjects = 300;
    final int totalObjects = 500;
    // initialize Region.
    server.invoke(new CacheSerializableRunnable("Update Region") {

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

        public void run2() throws CacheException {
            Region region = getCache().getRegion("/root/" + cqDUnitTest.regions[0]);
            // Update (totalObjects - 1) entries.
            for (int i = 1; i < totalObjects; i++) {
                // Destroy entries.
                if (i > 25 && i < 201) {
                    region.destroy("" + i);
                    continue;
                }
                Portfolio p = new Portfolio(i);
                region.put("" + i, p);
            }
            // recreate destroyed entries.
            for (int j = 26; j < 201; j++) {
                Portfolio p = new Portfolio(j);
                region.put("" + j, p);
            }
            // Add the last key.
            Portfolio p = new Portfolio(totalObjects);
            region.put("" + totalObjects, p);
        }
    });
    // Execute CQ.
    // While region operation is in progress execute CQ.
    cqDUnitTest.executeCQ(client, cqName, true, null);
    // Verify CQ Cache results.
    server.invoke(new CacheSerializableRunnable("Verify CQ Cache results") {

        public void run2() throws CacheException {
            CqService cqService = null;
            try {
                cqService = ((DefaultQueryService) getCache().getQueryService()).getCqService();
            } catch (Exception ex) {
                LogWriterUtils.getLogWriter().info("Failed to get the internal CqService.", ex);
                Assert.fail("Failed to get the internal CqService.", ex);
            }
            // Wait till all the region update is performed.
            Region region = getCache().getRegion("/root/" + cqDUnitTest.regions[0]);
            while (true) {
                if (region.get("" + totalObjects) == null) {
                    try {
                        Thread.sleep(50);
                    } catch (Exception ex) {
                    // ignore.
                    }
                    continue;
                }
                break;
            }
            Collection<? extends InternalCqQuery> cqs = cqService.getAllCqs();
            for (InternalCqQuery cq : cqs) {
                ServerCQImpl cqQuery = (ServerCQImpl) cq;
                if (cqQuery.getName().equals(cqName)) {
                    int size = cqQuery.getCqResultKeysSize();
                    if (size != totalObjects) {
                        LogWriterUtils.getLogWriter().info("The number of Cached events " + size + " is not equal to the expected size " + totalObjects);
                        HashSet expectedKeys = new HashSet();
                        for (int i = 1; i < totalObjects; i++) {
                            expectedKeys.add("" + i);
                        }
                        Set cachedKeys = cqQuery.getCqResultKeyCache();
                        expectedKeys.removeAll(cachedKeys);
                        LogWriterUtils.getLogWriter().info("Missing keys from the Cache : " + expectedKeys);
                    }
                    assertEquals("The number of keys cached for cq " + cqName + " is wrong.", totalObjects, cqQuery.getCqResultKeysSize());
                }
            }
        }
    });
    // Close.
    cqDUnitTest.closeClient(client);
    cqDUnitTest.closeServer(server);
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) CacheException(org.apache.geode.cache.CacheException) Portfolio(org.apache.geode.cache.query.data.Portfolio) Host(org.apache.geode.test.dunit.Host) CqService(org.apache.geode.cache.query.internal.cq.CqService) ServerCQImpl(org.apache.geode.cache.query.internal.cq.ServerCQImpl) CacheException(org.apache.geode.cache.CacheException) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) Collection(java.util.Collection) InternalCqQuery(org.apache.geode.cache.query.internal.cq.InternalCqQuery) DefaultQueryService(org.apache.geode.cache.query.internal.DefaultQueryService) HashSet(java.util.HashSet) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Aggregations

ServerCQImpl (org.apache.geode.cache.query.internal.cq.ServerCQImpl)9 Collection (java.util.Collection)8 CacheException (org.apache.geode.cache.CacheException)8 DefaultQueryService (org.apache.geode.cache.query.internal.DefaultQueryService)8 InternalCqQuery (org.apache.geode.cache.query.internal.cq.InternalCqQuery)8 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)8 Host (org.apache.geode.test.dunit.Host)8 VM (org.apache.geode.test.dunit.VM)8 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)8 Test (org.junit.Test)8 CqService (org.apache.geode.cache.query.internal.cq.CqService)7 Region (org.apache.geode.cache.Region)6 Portfolio (org.apache.geode.cache.query.data.Portfolio)6 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)6 Set (java.util.Set)5 HashSet (java.util.HashSet)4 CqServiceImpl (org.apache.geode.cache.query.internal.cq.CqServiceImpl)2 IOException (java.io.IOException)1 ExecuteCQOperationContext (org.apache.geode.cache.operations.ExecuteCQOperationContext)1 CqException (org.apache.geode.cache.query.CqException)1