Search in sources :

Example 1 with CqResults

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

the class CQPDXPostProcessorDUnitTest method testCQ.

@Test
public void testCQ() {
    String query = "select * from /" + REGION_NAME;
    client1.invoke(() -> {
        ClientCache cache = createClientCache("super-user", "1234567", server.getPort());
        Region region = createProxyRegion(cache, REGION_NAME);
        Pool pool = PoolManager.find(region);
        QueryService qs = pool.getQueryService();
        CqAttributesFactory factory = new CqAttributesFactory();
        factory.addCqListener(new CqListenerImpl() {

            @Override
            public void onEvent(final CqEvent aCqEvent) {
                Object key = aCqEvent.getKey();
                Object value = aCqEvent.getNewValue();
                if (key.equals("key1")) {
                    assertTrue(value instanceof SimpleClass);
                } else if (key.equals("key2")) {
                    assertTrue(Arrays.equals(BYTES, (byte[]) value));
                }
            }
        });
        CqAttributes cqa = factory.create();
        // Create the CqQuery
        CqQuery cq = qs.newCq("CQ1", query, cqa);
        CqResults results = cq.executeWithInitialResults();
    });
    client2.invoke(() -> {
        ClientCache cache = createClientCache("authRegionUser", "1234567", server.getPort());
        Region region = createProxyRegion(cache, REGION_NAME);
        region.put("key1", new SimpleClass(1, (byte) 1));
        region.put("key2", BYTES);
    });
    // wait for events to fire
    Awaitility.await().atMost(1, TimeUnit.SECONDS);
    PDXPostProcessor pp = (PDXPostProcessor) SecurityService.getSecurityService().getPostProcessor();
    assertEquals(pp.getCount(), 2);
}
Also used : ClientCache(org.apache.geode.cache.client.ClientCache) SecurityTestUtil.createClientCache(org.apache.geode.security.SecurityTestUtil.createClientCache) SimpleClass(org.apache.geode.pdx.SimpleClass) CqListenerImpl(org.apache.geode.cache.query.internal.cq.CqListenerImpl) CqEvent(org.apache.geode.cache.query.CqEvent) QueryService(org.apache.geode.cache.query.QueryService) CqAttributes(org.apache.geode.cache.query.CqAttributes) Region(org.apache.geode.cache.Region) SecurityTestUtil.createProxyRegion(org.apache.geode.security.SecurityTestUtil.createProxyRegion) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) CqResults(org.apache.geode.cache.query.CqResults) Pool(org.apache.geode.cache.client.Pool) CqQuery(org.apache.geode.cache.query.CqQuery) SecurityTest(org.apache.geode.test.junit.categories.SecurityTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 2 with CqResults

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

the class CQPostProcessorDunitTest method testPostProcess.

@Test
public void testPostProcess() {
    String query = "select * from /AuthRegion";
    client1.invoke(() -> {
        ClientCache cache = createClientCache("super-user", "1234567", server.getPort());
        Region region = createProxyRegion(cache, REGION_NAME);
        Pool pool = PoolManager.find(region);
        QueryService qs = pool.getQueryService();
        CqAttributesFactory factory = new CqAttributesFactory();
        factory.addCqListener(new CqListenerImpl() {

            @Override
            public void onEvent(final CqEvent aCqEvent) {
                assertEquals("key6", aCqEvent.getKey());
                assertEquals("super-user/AuthRegion/key6/value6", aCqEvent.getNewValue());
            }
        });
        CqAttributes cqa = factory.create();
        // Create the CqQuery
        CqQuery cq = qs.newCq("CQ1", query, cqa);
        CqResults results = cq.executeWithInitialResults();
        assertEquals(5, results.size());
        String resultString = results.toString();
        assertTrue(resultString, resultString.contains("key:key0,value:super-user/null/key0/value0"));
        assertTrue(resultString.contains("key:key1,value:super-user/null/key1/value1"));
        assertTrue(resultString.contains("key:key2,value:super-user/null/key2/value2"));
        assertTrue(resultString.contains("key:key3,value:super-user/null/key3/value3"));
        assertTrue(resultString.contains("key:key4,value:super-user/null/key4/value4"));
    });
    client2.invoke(() -> {
        ClientCache cache = createClientCache("authRegionUser", "1234567", server.getPort());
        Region region = createProxyRegion(cache, REGION_NAME);
        region.put("key6", "value6");
    });
}
Also used : CqListenerImpl(org.apache.geode.cache.query.internal.cq.CqListenerImpl) CqEvent(org.apache.geode.cache.query.CqEvent) QueryService(org.apache.geode.cache.query.QueryService) CqAttributes(org.apache.geode.cache.query.CqAttributes) Region(org.apache.geode.cache.Region) SecurityTestUtil.createProxyRegion(org.apache.geode.security.SecurityTestUtil.createProxyRegion) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) CqResults(org.apache.geode.cache.query.CqResults) Pool(org.apache.geode.cache.client.Pool) ClientCache(org.apache.geode.cache.client.ClientCache) SecurityTestUtil.createClientCache(org.apache.geode.security.SecurityTestUtil.createClientCache) CqQuery(org.apache.geode.cache.query.CqQuery) SecurityTest(org.apache.geode.test.junit.categories.SecurityTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 3 with CqResults

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

the class ClientCQImpl method executeWithInitialResults.

/**
   * Start or resume executing the query. Gets or updates the CQ results and returns them.
   */
@Override
public <E> CqResults<E> executeWithInitialResults() throws CqClosedException, RegionNotFoundException, CqException {
    synchronized (queuedEventsSynchObject) {
        // until first call is completed.
        while (queuedEvents != null) {
            try {
                queuedEventsSynchObject.wait();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
        // At this point we know queuedEvents is null and no one is adding to queuedEvents yet.
        this.queuedEvents = new ConcurrentLinkedQueue<CqEventImpl>();
    }
    if (CqQueryImpl.testHook != null) {
        testHook.pauseUntilReady();
    }
    // Send CQ request to servers.
    // If an exception is thrown, we need to clean up the queuedEvents
    // or else client will hang on next executeWithInitialResults
    CqResults initialResults;
    try {
        initialResults = (CqResults) executeCqOnRedundantsAndPrimary(true);
    } catch (RegionNotFoundException | CqException | RuntimeException e) {
        queuedEvents = null;
        throw e;
    }
    // initial results can be added to the queue.
    synchronized (queuedEventsSynchObject) {
        // Invoke the CQ Listeners with the received CQ Events.
        try {
            if (!this.queuedEvents.isEmpty()) {
                try {
                    Runnable r = new Runnable() {

                        @Override
                        public void run() {
                            Object[] eventArray = null;
                            if (CqQueryImpl.testHook != null) {
                                testHook.setEventCount(queuedEvents.size());
                            }
                            // Synchronization for the executer thread.
                            synchronized (queuedEventsSynchObject) {
                                try {
                                    eventArray = queuedEvents.toArray();
                                    // Process through the events
                                    for (Object cqEvent : eventArray) {
                                        cqService.invokeListeners(cqName, ClientCQImpl.this, (CqEventImpl) cqEvent);
                                        stats.decQueuedCqListenerEvents();
                                    }
                                } finally {
                                    // Make sure that we notify waiting threads or else possible dead lock
                                    queuedEvents.clear();
                                    queuedEvents = null;
                                    queuedEventsSynchObject.notify();
                                }
                            }
                        }
                    };
                    final LoggingThreadGroup group = LoggingThreadGroup.createThreadGroup("CQEventHandler", logger);
                    Thread thread = new Thread(group, r, "CQEventHandler For " + cqName);
                    thread.setDaemon(true);
                    thread.start();
                } catch (Exception ex) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Exception while invoking the CQ Listener with queued events.", ex);
                    }
                }
            } else {
                queuedEvents = null;
            }
        } finally {
            queuedEventsSynchObject.notify();
        }
        return initialResults;
    }
}
Also used : RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) CqException(org.apache.geode.cache.query.CqException) LoggingThreadGroup(org.apache.geode.internal.logging.LoggingThreadGroup) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) CqException(org.apache.geode.cache.query.CqException) CacheClosedException(org.apache.geode.cache.CacheClosedException) CancelException(org.apache.geode.CancelException) GemFireSecurityException(org.apache.geode.security.GemFireSecurityException) CqClosedException(org.apache.geode.cache.query.CqClosedException) CqResults(org.apache.geode.cache.query.CqResults)

Aggregations

CqResults (org.apache.geode.cache.query.CqResults)3 Region (org.apache.geode.cache.Region)2 ClientCache (org.apache.geode.cache.client.ClientCache)2 Pool (org.apache.geode.cache.client.Pool)2 CqAttributes (org.apache.geode.cache.query.CqAttributes)2 CqAttributesFactory (org.apache.geode.cache.query.CqAttributesFactory)2 CqEvent (org.apache.geode.cache.query.CqEvent)2 CqQuery (org.apache.geode.cache.query.CqQuery)2 QueryService (org.apache.geode.cache.query.QueryService)2 CqListenerImpl (org.apache.geode.cache.query.internal.cq.CqListenerImpl)2 SecurityTestUtil.createClientCache (org.apache.geode.security.SecurityTestUtil.createClientCache)2 SecurityTestUtil.createProxyRegion (org.apache.geode.security.SecurityTestUtil.createProxyRegion)2 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)2 SecurityTest (org.apache.geode.test.junit.categories.SecurityTest)2 Test (org.junit.Test)2 CancelException (org.apache.geode.CancelException)1 CacheClosedException (org.apache.geode.cache.CacheClosedException)1 CqClosedException (org.apache.geode.cache.query.CqClosedException)1 CqException (org.apache.geode.cache.query.CqException)1 RegionNotFoundException (org.apache.geode.cache.query.RegionNotFoundException)1