Search in sources :

Example 1 with CqException

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

the class PutAllCSDUnitTest method testOneServer.

/**
   * Tests putAll to one server.
   */
@Test
public void testOneServer() throws CacheException, InterruptedException {
    final String title = "testOneServer:";
    final Host host = Host.getHost(0);
    VM server = host.getVM(0);
    VM client1 = host.getVM(2);
    VM client2 = host.getVM(3);
    final String regionName = getUniqueName();
    final String serverHost = NetworkUtils.getServerHostName(server.getHost());
    // set <false, true> means <PR=false, notifyBySubscription=true> to enable registerInterest and
    // CQ
    final int serverPort = createBridgeServer(server, regionName, 0, false, 0, null);
    createClient(client1, regionName, serverHost, new int[] { serverPort }, -1, -1, false, true, true);
    createClient(client2, regionName, serverHost, new int[] { serverPort }, -1, -1, false, true, true);
    server.invoke(new CacheSerializableRunnable(title + "server add listener") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(regionName);
            region.getAttributesMutator().addCacheListener(new MyListener(false));
        }
    });
    client2.invoke(new CacheSerializableRunnable(title + "client2 registerInterest and add listener") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(regionName);
            region.getAttributesMutator().addCacheListener(new MyListener(false));
            // registerInterest for ALL_KEYS
            region.registerInterest("ALL_KEYS");
            LogWriterUtils.getLogWriter().info("client2 registerInterest ALL_KEYS at " + region.getFullPath());
        }
    });
    client1.invoke(new CacheSerializableRunnable(title + "client1 create local region and run putAll") {

        @Override
        public void run2() throws CacheException {
            AttributesFactory factory2 = new AttributesFactory();
            factory2.setScope(Scope.LOCAL);
            factory2.addCacheListener(new MyListener(false));
            createRegion("localsave", factory2.create());
            Region region = doPutAll(regionName, "key-", numberOfEntries);
            assertEquals(numberOfEntries, region.size());
        }
    });
    AsyncInvocation async1 = client1.invokeAsync(new CacheSerializableRunnable(title + "client1 create CQ") {

        @Override
        public void run2() throws CacheException {
            // create a CQ for key 10-20
            Region localregion = getRootRegion().getSubregion("localsave");
            CqAttributesFactory cqf1 = new CqAttributesFactory();
            EOCQEventListener EOCQListener = new EOCQEventListener(localregion);
            cqf1.addCqListener(EOCQListener);
            CqAttributes cqa1 = cqf1.create();
            String cqName1 = "EOInfoTracker";
            String queryStr1 = "SELECT ALL * FROM /root/" + regionName + " ii WHERE ii.getTicker() >= '10' and ii.getTicker() < '20'";
            LogWriterUtils.getLogWriter().info("Query String: " + queryStr1);
            try {
                QueryService cqService = getCache().getQueryService();
                CqQuery EOTracker = cqService.newCq(cqName1, queryStr1, cqa1);
                SelectResults rs1 = EOTracker.executeWithInitialResults();
                List list1 = rs1.asList();
                for (int i = 0; i < list1.size(); i++) {
                    Struct s = (Struct) list1.get(i);
                    TestObject o = (TestObject) s.get("value");
                    LogWriterUtils.getLogWriter().info("InitialResult:" + i + ":" + o);
                    localregion.put("key-" + i, o);
                }
                if (localregion.size() > 0) {
                    LogWriterUtils.getLogWriter().info("CQ is ready");
                    synchronized (lockObject) {
                        lockObject.notify();
                    }
                }
                waitTillNotify(lockObject2, 20000, (EOCQListener.num_creates == 5 && EOCQListener.num_updates == 5));
                EOTracker.close();
            } catch (CqClosedException e) {
                Assert.fail("CQ", e);
            } catch (RegionNotFoundException e) {
                Assert.fail("CQ", e);
            } catch (QueryInvalidException e) {
                Assert.fail("CQ", e);
            } catch (CqExistsException e) {
                Assert.fail("CQ", e);
            } catch (CqException e) {
                Assert.fail("CQ", e);
            }
        }
    });
    server.invoke(new CacheSerializableRunnable(title + "verify Bridge Server") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(regionName);
            assertEquals(numberOfEntries, region.size());
            for (int i = 0; i < numberOfEntries; i++) {
                TestObject obj = (TestObject) region.getEntry("key-" + i).getValue();
                assertEquals(i, obj.getPrice());
            }
        }
    });
    // verify CQ is ready
    client1.invoke(new CacheSerializableRunnable(title + "verify CQ is ready") {

        @Override
        public void run2() throws CacheException {
            Region localregion = getRootRegion().getSubregion("localsave");
            waitTillNotify(lockObject, 10000, (localregion.size() > 0));
            assertTrue(localregion.size() > 0);
        }
    });
    // verify registerInterest result at client2
    client2.invoke(new CacheSerializableRunnable(title + "verify client2") {

        @Override
        public void run2() throws CacheException {
            final Region region = getRootRegion().getSubregion(regionName);
            checkRegionSize(region, numberOfEntries);
            for (int i = 0; i < numberOfEntries; i++) {
                TestObject obj = (TestObject) region.getEntry("key-" + i).getValue();
                assertEquals(i, obj.getPrice());
            }
            // then do update for key 10-20 to trigger CQ at server2
            // destroy key 10-14 to simulate create/update mix case
            region.removeAll(Arrays.asList("key-10", "key-11", "key-12", "key-13", "key-14"), "removeAllCallback");
            assertEquals(null, region.get("key-10"));
            assertEquals(null, region.get("key-11"));
            assertEquals(null, region.get("key-12"));
            assertEquals(null, region.get("key-13"));
            assertEquals(null, region.get("key-14"));
        }
    });
    // verify CQ result at client1
    client1.invoke(new CacheSerializableRunnable(title + "Verify client1") {

        @Override
        public void run2() throws CacheException {
            Region localregion = getRootRegion().getSubregion("localsave");
            for (int i = 10; i < 15; i++) {
                TestObject obj = null;
                int cnt = 0;
                while (cnt < 100) {
                    obj = (TestObject) localregion.get("key-" + i);
                    if (obj != null) {
                        // wait for the key to be destroyed
                        Wait.pause(100);
                        if (LogWriterUtils.getLogWriter().fineEnabled()) {
                            LogWriterUtils.getLogWriter().info("Waiting 100ms(" + cnt + ") for key-" + i + " to be destroyed");
                        }
                        cnt++;
                    } else {
                        break;
                    }
                }
            }
        }
    });
    client2.invoke(new CacheSerializableRunnable(title + "verify client2") {

        @Override
        public void run2() throws CacheException {
            final Region region = getRootRegion().getSubregion(regionName);
            LinkedHashMap map = new LinkedHashMap();
            for (int i = 10; i < 20; i++) {
                map.put("key-" + i, new TestObject(i * 10));
            }
            region.putAll(map, "putAllCallback");
        }
    });
    // verify CQ result at client1
    client1.invoke(new CacheSerializableRunnable(title + "Verify client1") {

        @Override
        public void run2() throws CacheException {
            Region localregion = getRootRegion().getSubregion("localsave");
            for (int i = 10; i < 20; i++) {
                TestObject obj = null;
                int cnt = 0;
                while (cnt < 100) {
                    obj = (TestObject) localregion.get("key-" + i);
                    if (obj == null || obj.getPrice() != i * 10) {
                        Wait.pause(100);
                        LogWriterUtils.getLogWriter().info("Waiting 100ms(" + cnt + ") for obj.getPrice() == i*10 at entry " + i);
                        cnt++;
                    } else {
                        break;
                    }
                }
                assertEquals(i * 10, obj.getPrice());
            }
            synchronized (lockObject2) {
                lockObject2.notify();
            }
        }
    });
    ThreadUtils.join(async1, 30 * 1000);
    // verify stats for client putAll into distributed region
    // 1. verify client staus
    /*
     * server2.invoke(new CacheSerializableRunnable("server2 execute putAll") { public void run2()
     * throws CacheException { try { DistributedSystemConfig config =
     * AdminDistributedSystemFactory.defineDistributedSystem(system, null); AdminDistributedSystem
     * ads = AdminDistributedSystemFactory.getDistributedSystem(config); ads.connect();
     * DistributedMember distributedMember = system.getDistributedMember(); SystemMember member =
     * ads.lookupSystemMember(distributedMember);
     * 
     * StatisticResource[] resources = member.getStats(); for (int i=0; i<resources.length; i++) {
     * System.out.println("GGG:"+resources[i].getType()); if
     * (resources[i].getType().equals("CacheServerClientStats")) { Statistic[] stats =
     * resources[i].getStatistics(); for (int j=0; i<stats.length; i++) { if
     * (stats[j].getName().equals("putAll")) {
     * System.out.println("GGG:"+stats[j].getName()+":"+stats[j].getValue()); } else if
     * (stats[j].getName().equals("sendPutAllTime")) {
     * System.out.println("GGG:"+stats[j].getName()+":"+stats[j].getValue()); } } } } } catch
     * (AdminException e) { fail("Failed while creating AdminDS", e); } } });
     */
    // Test Exception handling
    // verify CQ is ready
    client1.invoke(new CacheSerializableRunnable(title + "test exception handling") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(regionName);
            Map m = null;
            boolean NPEthrowed = false;
            try {
                region.putAll(m, "putAllCallback");
                fail("Should have thrown NullPointerException");
            } catch (NullPointerException ex) {
                NPEthrowed = true;
            }
            assertTrue(NPEthrowed);
            region.localDestroyRegion();
            boolean RDEthrowed = false;
            try {
                m = new HashMap();
                for (int i = 1; i < 21; i++) {
                    m.put(new Integer(i), Integer.toString(i));
                }
                region.putAll(m, "putAllCallback");
                fail("Should have thrown RegionDestroyedException");
            } catch (RegionDestroyedException ex) {
                RDEthrowed = true;
            }
            assertTrue(RDEthrowed);
            try {
                region.removeAll(Arrays.asList("key-10", "key-11"), "removeAllCallback");
                fail("Should have thrown RegionDestroyedException");
            } catch (RegionDestroyedException expected) {
            }
        }
    });
    // Stop server
    stopBridgeServers(getCache());
}
Also used : CacheException(org.apache.geode.cache.CacheException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) RegionDestroyedException(org.apache.geode.cache.RegionDestroyedException) CqClosedException(org.apache.geode.cache.query.CqClosedException) AsyncInvocation(org.apache.geode.test.dunit.AsyncInvocation) Struct(org.apache.geode.cache.query.Struct) LinkedHashMap(java.util.LinkedHashMap) AttributesFactory(org.apache.geode.cache.AttributesFactory) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) SelectResults(org.apache.geode.cache.query.SelectResults) List(java.util.List) ArrayList(java.util.ArrayList) CqQuery(org.apache.geode.cache.query.CqQuery) CqException(org.apache.geode.cache.query.CqException) QueryInvalidException(org.apache.geode.cache.query.QueryInvalidException) Host(org.apache.geode.test.dunit.Host) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) CqAttributes(org.apache.geode.cache.query.CqAttributes) QueryService(org.apache.geode.cache.query.QueryService) VM(org.apache.geode.test.dunit.VM) CqExistsException(org.apache.geode.cache.query.CqExistsException) Region(org.apache.geode.cache.Region) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ClientSubscriptionTest(org.apache.geode.test.junit.categories.ClientSubscriptionTest) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) ClientServerTest(org.apache.geode.test.junit.categories.ClientServerTest) Test(org.junit.Test)

Example 2 with CqException

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

the class ClientAuthorizationTestCase method doOp.

protected static void doOp(OperationCode op, final int[] indices, final int flagsI, final int expectedResult) throws InterruptedException {
    boolean operationOmitted = false;
    final int flags = flagsI;
    Region region = getRegion();
    if ((flags & OpFlags.USE_SUBREGION) > 0) {
        assertNotNull(region);
        Region subregion = null;
        if ((flags & OpFlags.NO_CREATE_SUBREGION) > 0) {
            if ((flags & OpFlags.CHECK_NOREGION) > 0) {
                // Wait for some time for DRF update to come
                waitForCondition(() -> getSubregion() == null);
                subregion = getSubregion();
                assertNull(subregion);
                return;
            } else {
                // Wait for some time for DRF update to come
                waitForCondition(() -> getSubregion() != null);
                subregion = getSubregion();
                assertNotNull(subregion);
            }
        } else {
            subregion = createSubregion(region);
        }
        assertNotNull(subregion);
        region = subregion;
    } else if ((flags & OpFlags.CHECK_NOREGION) > 0) {
        // Wait for some time for region destroy update to come
        waitForCondition(() -> getRegion() == null);
        region = getRegion();
        assertNull(region);
        return;
    } else {
        assertNotNull(region);
    }
    final String[] keys = KEYS;
    final String[] vals;
    if ((flags & OpFlags.USE_NEWVAL) > 0) {
        vals = NVALUES;
    } else {
        vals = VALUES;
    }
    InterestResultPolicy policy = InterestResultPolicy.KEYS_VALUES;
    if ((flags & OpFlags.REGISTER_POLICY_NONE) > 0) {
        policy = InterestResultPolicy.NONE;
    }
    final int numOps = indices.length;
    System.out.println("Got doOp for op: " + op.toString() + ", numOps: " + numOps + ", indices: " + indicesToString(indices) + ", expect: " + expectedResult);
    boolean exceptionOccurred = false;
    boolean breakLoop = false;
    if (op.isGet() || op.isContainsKey() || op.isKeySet() || op.isQuery() || op.isExecuteCQ()) {
        Thread.sleep(PAUSE);
    }
    for (int indexIndex = 0; indexIndex < numOps; ++indexIndex) {
        if (breakLoop) {
            break;
        }
        int index = indices[indexIndex];
        try {
            final Object key = keys[index];
            final Object expectedVal = vals[index];
            if (op.isGet()) {
                Object value = null;
                // this is the case for testing GET_ALL
                if ((flags & OpFlags.USE_ALL_KEYS) > 0) {
                    breakLoop = true;
                    List keyList = new ArrayList(numOps);
                    Object searchKey;
                    for (int keyNumIndex = 0; keyNumIndex < numOps; ++keyNumIndex) {
                        int keyNum = indices[keyNumIndex];
                        searchKey = keys[keyNum];
                        keyList.add(searchKey);
                        // local invalidate some KEYS to force fetch of those KEYS from server
                        if ((flags & OpFlags.CHECK_NOKEY) > 0) {
                            AbstractRegionEntry entry = (AbstractRegionEntry) ((LocalRegion) region).getRegionEntry(searchKey);
                            System.out.println("" + keyNum + ": key is " + searchKey + " and entry is " + entry);
                            assertFalse(region.containsKey(searchKey));
                        } else {
                            if (keyNumIndex % 2 == 1) {
                                assertTrue(region.containsKey(searchKey));
                                region.localInvalidate(searchKey);
                            }
                        }
                    }
                    Map entries = region.getAll(keyList);
                    for (int keyNumIndex = 0; keyNumIndex < numOps; ++keyNumIndex) {
                        int keyNum = indices[keyNumIndex];
                        searchKey = keys[keyNum];
                        if ((flags & OpFlags.CHECK_FAIL) > 0) {
                            assertFalse(entries.containsKey(searchKey));
                        } else {
                            assertTrue(entries.containsKey(searchKey));
                            value = entries.get(searchKey);
                            assertEquals(vals[keyNum], value);
                        }
                    }
                    break;
                }
                if ((flags & OpFlags.LOCAL_OP) > 0) {
                    Callable<Boolean> condition = new Callable<Boolean>() {

                        private Region region;

                        @Override
                        public Boolean call() throws Exception {
                            Object value = getLocalValue(region, key);
                            return (flags & OpFlags.CHECK_FAIL) > 0 ? !expectedVal.equals(value) : expectedVal.equals(value);
                        }

                        public Callable<Boolean> init(Region region) {
                            this.region = region;
                            return this;
                        }
                    }.init(region);
                    waitForCondition(condition);
                    value = getLocalValue(region, key);
                } else if ((flags & OpFlags.USE_GET_ENTRY_IN_TX) > 0) {
                    getCache().getCacheTransactionManager().begin();
                    Entry e = region.getEntry(key);
                    // Also, check getAll()
                    ArrayList a = new ArrayList();
                    a.addAll(a);
                    region.getAll(a);
                    getCache().getCacheTransactionManager().commit();
                    value = e.getValue();
                } else {
                    if ((flags & OpFlags.CHECK_NOKEY) > 0) {
                        assertFalse(region.containsKey(key));
                    } else {
                        assertTrue(region.containsKey(key) || ((LocalRegion) region).getRegionEntry(key).isTombstone());
                        region.localInvalidate(key);
                    }
                    value = region.get(key);
                }
                if ((flags & OpFlags.CHECK_FAIL) > 0) {
                    assertFalse(expectedVal.equals(value));
                } else {
                    assertNotNull(value);
                    assertEquals(expectedVal, value);
                }
            } else if (op.isPut()) {
                region.put(key, expectedVal);
            } else if (op.isPutAll()) {
                HashMap map = new HashMap();
                for (int i = 0; i < indices.length; i++) {
                    map.put(keys[indices[i]], vals[indices[i]]);
                }
                region.putAll(map);
                breakLoop = true;
            } else if (op.isDestroy()) {
                // }
                if ((flags & OpFlags.LOCAL_OP) > 0) {
                    region.localDestroy(key);
                } else {
                    region.destroy(key);
                }
            } else if (op.isInvalidate()) {
                if (region.containsKey(key)) {
                    if ((flags & OpFlags.LOCAL_OP) > 0) {
                        region.localInvalidate(key);
                    } else {
                        region.invalidate(key);
                    }
                }
            } else if (op.isContainsKey()) {
                boolean result;
                if ((flags & OpFlags.LOCAL_OP) > 0) {
                    result = region.containsKey(key);
                } else {
                    result = region.containsKeyOnServer(key);
                }
                if ((flags & OpFlags.CHECK_FAIL) > 0) {
                    assertFalse(result);
                } else {
                    assertTrue(result);
                }
            } else if (op.isRegisterInterest()) {
                if ((flags & OpFlags.USE_LIST) > 0) {
                    breakLoop = true;
                    // Register interest list in this case
                    List keyList = new ArrayList(numOps);
                    for (int keyNumIndex = 0; keyNumIndex < numOps; ++keyNumIndex) {
                        int keyNum = indices[keyNumIndex];
                        keyList.add(keys[keyNum]);
                    }
                    region.registerInterest(keyList, policy);
                } else if ((flags & OpFlags.USE_REGEX) > 0) {
                    breakLoop = true;
                    region.registerInterestRegex("key[1-" + numOps + ']', policy);
                } else if ((flags & OpFlags.USE_ALL_KEYS) > 0) {
                    breakLoop = true;
                    region.registerInterest("ALL_KEYS", policy);
                } else {
                    region.registerInterest(key, policy);
                }
            } else if (op.isUnregisterInterest()) {
                if ((flags & OpFlags.USE_LIST) > 0) {
                    breakLoop = true;
                    // Register interest list in this case
                    List keyList = new ArrayList(numOps);
                    for (int keyNumIndex = 0; keyNumIndex < numOps; ++keyNumIndex) {
                        int keyNum = indices[keyNumIndex];
                        keyList.add(keys[keyNum]);
                    }
                    region.unregisterInterest(keyList);
                } else if ((flags & OpFlags.USE_REGEX) > 0) {
                    breakLoop = true;
                    region.unregisterInterestRegex("key[1-" + numOps + ']');
                } else if ((flags & OpFlags.USE_ALL_KEYS) > 0) {
                    breakLoop = true;
                    region.unregisterInterest("ALL_KEYS");
                } else {
                    region.unregisterInterest(key);
                }
            } else if (op.isKeySet()) {
                breakLoop = true;
                Set keySet;
                if ((flags & OpFlags.LOCAL_OP) > 0) {
                    keySet = region.keySet();
                } else {
                    keySet = region.keySetOnServer();
                }
                assertNotNull(keySet);
                if ((flags & OpFlags.CHECK_FAIL) == 0) {
                    assertEquals(numOps, keySet.size());
                }
                for (int keyNumIndex = 0; keyNumIndex < numOps; ++keyNumIndex) {
                    int keyNum = indices[keyNumIndex];
                    if ((flags & OpFlags.CHECK_FAIL) > 0) {
                        assertFalse(keySet.contains(keys[keyNum]));
                    } else {
                        assertTrue(keySet.contains(keys[keyNum]));
                    }
                }
            } else if (op.isQuery()) {
                breakLoop = true;
                SelectResults queryResults = region.query("SELECT DISTINCT * FROM " + region.getFullPath());
                assertNotNull(queryResults);
                Set queryResultSet = queryResults.asSet();
                if ((flags & OpFlags.CHECK_FAIL) == 0) {
                    assertEquals(numOps, queryResultSet.size());
                }
                for (int keyNumIndex = 0; keyNumIndex < numOps; ++keyNumIndex) {
                    int keyNum = indices[keyNumIndex];
                    if ((flags & OpFlags.CHECK_FAIL) > 0) {
                        assertFalse(queryResultSet.contains(vals[keyNum]));
                    } else {
                        assertTrue(queryResultSet.contains(vals[keyNum]));
                    }
                }
            } else if (op.isExecuteCQ()) {
                breakLoop = true;
                QueryService queryService = getCache().getQueryService();
                CqQuery cqQuery;
                if ((cqQuery = queryService.getCq("cq1")) == null) {
                    CqAttributesFactory cqFact = new CqAttributesFactory();
                    cqFact.addCqListener(new AuthzCqListener());
                    CqAttributes cqAttrs = cqFact.create();
                    cqQuery = queryService.newCq("cq1", "SELECT * FROM " + region.getFullPath(), cqAttrs);
                }
                if ((flags & OpFlags.LOCAL_OP) > 0) {
                    // Interpret this as testing results using CqListener
                    final AuthzCqListener listener = (AuthzCqListener) cqQuery.getCqAttributes().getCqListener();
                    WaitCriterion ev = new WaitCriterion() {

                        @Override
                        public boolean done() {
                            if ((flags & OpFlags.CHECK_FAIL) > 0) {
                                return 0 == listener.getNumUpdates();
                            } else {
                                return numOps == listener.getNumUpdates();
                            }
                        }

                        @Override
                        public String description() {
                            return null;
                        }
                    };
                    waitForCriterion(ev, 3 * 1000, 200, true);
                    if ((flags & OpFlags.CHECK_FAIL) > 0) {
                        assertEquals(0, listener.getNumUpdates());
                    } else {
                        assertEquals(numOps, listener.getNumUpdates());
                        listener.checkPuts(vals, indices);
                    }
                    assertEquals(0, listener.getNumCreates());
                    assertEquals(0, listener.getNumDestroys());
                    assertEquals(0, listener.getNumOtherOps());
                    assertEquals(0, listener.getNumErrors());
                } else {
                    SelectResults cqResults = cqQuery.executeWithInitialResults();
                    assertNotNull(cqResults);
                    Set cqResultValues = new HashSet();
                    for (Object o : cqResults.asList()) {
                        Struct s = (Struct) o;
                        cqResultValues.add(s.get("value"));
                    }
                    Set cqResultSet = cqResults.asSet();
                    if ((flags & OpFlags.CHECK_FAIL) == 0) {
                        assertEquals(numOps, cqResultSet.size());
                    }
                    for (int keyNumIndex = 0; keyNumIndex < numOps; ++keyNumIndex) {
                        int keyNum = indices[keyNumIndex];
                        if ((flags & OpFlags.CHECK_FAIL) > 0) {
                            assertFalse(cqResultValues.contains(vals[keyNum]));
                        } else {
                            assertTrue(cqResultValues.contains(vals[keyNum]));
                        }
                    }
                }
            } else if (op.isStopCQ()) {
                breakLoop = true;
                CqQuery cqQuery = getCache().getQueryService().getCq("cq1");
                ((AuthzCqListener) cqQuery.getCqAttributes().getCqListener()).reset();
                cqQuery.stop();
            } else if (op.isCloseCQ()) {
                breakLoop = true;
                CqQuery cqQuery = getCache().getQueryService().getCq("cq1");
                ((AuthzCqListener) cqQuery.getCqAttributes().getCqListener()).reset();
                cqQuery.close();
            } else if (op.isRegionClear()) {
                breakLoop = true;
                if ((flags & OpFlags.LOCAL_OP) > 0) {
                    region.localClear();
                } else {
                    region.clear();
                }
            } else if (op.isRegionCreate()) {
                breakLoop = true;
                // Region subregion = createSubregion(region);
                // subregion.createRegionOnServer();
                // Create region on server using the DynamicRegionFactory
                // Assume it has been already initialized
                DynamicRegionFactory drf = DynamicRegionFactory.get();
                Region subregion = drf.createDynamicRegion(regionName, SUBREGION_NAME);
                assertEquals('/' + regionName + '/' + SUBREGION_NAME, subregion.getFullPath());
            } else if (op.isRegionDestroy()) {
                breakLoop = true;
                if ((flags & OpFlags.LOCAL_OP) > 0) {
                    region.localDestroyRegion();
                } else {
                    if ((flags & OpFlags.USE_SUBREGION) > 0) {
                        try {
                            DynamicRegionFactory.get().destroyDynamicRegion(region.getFullPath());
                        } catch (RegionDestroyedException ex) {
                            // harmless to ignore this
                            System.out.println("doOp: sub-region " + region.getFullPath() + " already destroyed");
                            operationOmitted = true;
                        }
                    } else {
                        region.destroyRegion();
                    }
                }
            } else {
                fail("doOp: Unhandled operation " + op);
            }
            if (expectedResult != NO_EXCEPTION) {
                if (!operationOmitted && !op.isUnregisterInterest()) {
                    fail("Expected an exception while performing operation op =" + op + "flags = " + OpFlags.description(flags));
                }
            }
        } catch (Exception ex) {
            exceptionOccurred = true;
            if ((ex instanceof ServerConnectivityException || ex instanceof QueryInvocationTargetException || ex instanceof CqException) && (expectedResult == NOTAUTHZ_EXCEPTION) && (ex.getCause() instanceof NotAuthorizedException)) {
                System.out.println("doOp: Got expected NotAuthorizedException when doing operation [" + op + "] with flags " + OpFlags.description(flags) + ": " + ex.getCause());
                continue;
            } else if (expectedResult == OTHER_EXCEPTION) {
                System.out.println("doOp: Got expected exception when doing operation: " + ex.toString());
                continue;
            } else {
                fail("doOp: Got unexpected exception when doing operation. Policy = " + policy + " flags = " + OpFlags.description(flags), ex);
            }
        }
    }
    if (!exceptionOccurred && !operationOmitted && expectedResult != NO_EXCEPTION) {
        fail("Expected an exception while performing operation: " + op + " flags = " + OpFlags.description(flags));
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) RegionDestroyedException(org.apache.geode.cache.RegionDestroyedException) ArrayList(java.util.ArrayList) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Callable(java.util.concurrent.Callable) Struct(org.apache.geode.cache.query.Struct) ServerConnectivityException(org.apache.geode.cache.client.ServerConnectivityException) InterestResultPolicy(org.apache.geode.cache.InterestResultPolicy) Entry(org.apache.geode.cache.Region.Entry) AbstractRegionEntry(org.apache.geode.internal.cache.AbstractRegionEntry) SelectResults(org.apache.geode.cache.query.SelectResults) ArrayList(java.util.ArrayList) List(java.util.List) CqQuery(org.apache.geode.cache.query.CqQuery) HashSet(java.util.HashSet) DynamicRegionFactory(org.apache.geode.cache.DynamicRegionFactory) CqException(org.apache.geode.cache.query.CqException) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) CqException(org.apache.geode.cache.query.CqException) RegionDestroyedException(org.apache.geode.cache.RegionDestroyedException) ServerConnectivityException(org.apache.geode.cache.client.ServerConnectivityException) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) AbstractRegionEntry(org.apache.geode.internal.cache.AbstractRegionEntry) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) QueryService(org.apache.geode.cache.query.QueryService) CqAttributes(org.apache.geode.cache.query.CqAttributes) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with CqException

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

the class ServerCQImpl method registerCq.

@Override
public void registerCq(ClientProxyMembershipID p_clientProxyId, CacheClientNotifier p_ccn, int p_cqState) throws CqException, RegionNotFoundException {
    CacheClientProxy clientProxy = null;
    this.clientProxyId = p_clientProxyId;
    if (p_ccn != null) {
        this.ccn = p_ccn;
        clientProxy = p_ccn.getClientProxy(p_clientProxyId, true);
    }
    validateCq();
    final boolean isDebugEnabled = logger.isDebugEnabled();
    StringId msg = LocalizedStrings.ONE_ARG;
    Throwable t = null;
    try {
        this.query = constructServerSideQuery();
        if (isDebugEnabled) {
            logger.debug("Server side query for the cq: {} is: {}", cqName, this.query.getQueryString());
        }
    } catch (Exception ex) {
        t = ex;
        if (ex instanceof ClassNotFoundException) {
            msg = LocalizedStrings.CqQueryImpl_CLASS_NOT_FOUND_EXCEPTION_THE_ANTLRJAR_OR_THE_SPCIFIED_CLASS_MAY_BE_MISSING_FROM_SERVER_SIDE_CLASSPATH_ERROR_0;
        } else {
            msg = LocalizedStrings.CqQueryImpl_ERROR_WHILE_PARSING_THE_QUERY_ERROR_0;
        }
    } finally {
        if (t != null) {
            String s = msg.toLocalizedString(t);
            if (isDebugEnabled) {
                logger.debug(s, t);
            }
            throw new CqException(s);
        }
    }
    // Update Regions Book keeping.
    // TODO replace getRegion() with getRegionByPathForProcessing() so this doesn't block
    // if the region is still being initialized
    this.cqBaseRegion = (LocalRegion) cqService.getCache().getRegion(regionName);
    if (this.cqBaseRegion == null) {
        throw new RegionNotFoundException(LocalizedStrings.CqQueryImpl_REGION__0_SPECIFIED_WITH_CQ_NOT_FOUND_CQNAME_1.toLocalizedString(new Object[] { regionName, this.cqName }));
    }
    // Make sure that the region is partitioned or
    // replicated with distributed ack or global.
    DataPolicy dp = this.cqBaseRegion.getDataPolicy();
    this.isPR = dp.withPartitioning();
    if (!(this.isPR || dp.withReplication())) {
        String errMsg = null;
        // replicated regions with eviction set to local destroy get turned into preloaded
        if (dp.withPreloaded() && cqBaseRegion.getAttributes().getEvictionAttributes() != null && cqBaseRegion.getAttributes().getEvictionAttributes().getAction().equals(EvictionAction.LOCAL_DESTROY)) {
            errMsg = LocalizedStrings.CqQueryImpl_CQ_NOT_SUPPORTED_FOR_REPLICATE_WITH_LOCAL_DESTROY.toString(this.regionName, cqBaseRegion.getAttributes().getEvictionAttributes().getAction());
        } else {
            errMsg = "The region " + this.regionName + "  specified in CQ creation is neither replicated nor partitioned; " + "only replicated or partitioned regions are allowed in CQ creation.";
        }
        if (isDebugEnabled) {
            logger.debug(errMsg);
        }
        throw new CqException(errMsg);
    }
    if ((dp.withReplication() && (!(cqBaseRegion.getAttributes().getScope().isDistributedAck() || cqBaseRegion.getAttributes().getScope().isGlobal())))) {
        String errMsg = "The replicated region " + this.regionName + " specified in CQ creation does not have scope supported by CQ." + " The CQ supported scopes are DISTRIBUTED_ACK and GLOBAL.";
        if (isDebugEnabled) {
            logger.debug(errMsg);
        }
        throw new CqException(errMsg);
    }
    // Can be null by the time we are here
    if (clientProxy != null) {
        clientProxy.incCqCount();
        if (clientProxy.hasOneCq()) {
            cqService.stats().incClientsWithCqs();
        }
        if (isDebugEnabled) {
            logger.debug("Added CQ to the base region: {} With key as: {}", cqBaseRegion.getFullPath(), serverCqName);
        }
    }
    this.updateCqCreateStats();
    // Initialize the state of CQ.
    if (this.cqState.getState() != p_cqState) {
        setCqState(p_cqState);
    }
    // it to other matching cqs for performance reasons
    if (p_cqState == CqStateImpl.RUNNING) {
        // Add to the matchedCqMap.
        cqService.addToMatchingCqMap(this);
    }
    // Initialize CQ results (key) cache.
    if (CqServiceProvider.MAINTAIN_KEYS) {
        this.cqResultKeys = new HashMap<Object, Object>();
        // added to the results cache (not from the CQ Results).
        if (this.isPR) {
            this.setCqResultsCacheInitialized();
        } else {
            this.destroysWhileCqResultsInProgress = new HashSet<Object>();
        }
    }
    if (p_ccn != null) {
        try {
            cqService.addToCqMap(this);
        } catch (CqExistsException cqe) {
            // Should not happen.
            throw new CqException(LocalizedStrings.CqQueryImpl_UNABLE_TO_CREATE_CQ_0_ERROR__1.toLocalizedString(new Object[] { cqName, cqe.getMessage() }));
        }
        this.cqBaseRegion.getFilterProfile().registerCq(this);
    }
}
Also used : CacheClientProxy(org.apache.geode.internal.cache.tier.sockets.CacheClientProxy) CqException(org.apache.geode.cache.query.CqException) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) CqExistsException(org.apache.geode.cache.query.CqExistsException) CqException(org.apache.geode.cache.query.CqException) CqClosedException(org.apache.geode.cache.query.CqClosedException) IOException(java.io.IOException) QueryException(org.apache.geode.cache.query.QueryException) StringId(org.apache.geode.i18n.StringId) CqExistsException(org.apache.geode.cache.query.CqExistsException) DataPolicy(org.apache.geode.cache.DataPolicy)

Example 4 with CqException

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

the class CloseCQ method cmdExecute.

@Override
public void cmdExecute(Message clientMessage, ServerConnection serverConnection, long start) throws IOException {
    CachedRegionHelper crHelper = serverConnection.getCachedRegionHelper();
    ClientProxyMembershipID id = serverConnection.getProxyID();
    CacheServerStats stats = serverConnection.getCacheServerStats();
    // Based on MessageType.QUERY
    // Added by Rao 2/1/2007
    serverConnection.setAsTrue(REQUIRES_RESPONSE);
    serverConnection.setAsTrue(REQUIRES_CHUNKED_RESPONSE);
    start = DistributionStats.getStatTime();
    // Retrieve the data from the message parts
    String cqName = clientMessage.getPart(0).getString();
    if (logger.isDebugEnabled()) {
        logger.debug("{}: Received close CQ request from {} cqName: {}", serverConnection.getName(), serverConnection.getSocketString(), cqName);
    }
    // Process the query request
    if (cqName == null) {
        String err = LocalizedStrings.CloseCQ_THE_CQNAME_FOR_THE_CQ_CLOSE_REQUEST_IS_NULL.toLocalizedString();
        sendCqResponse(MessageType.CQDATAERROR_MSG_TYPE, err, clientMessage.getTransactionId(), null, serverConnection);
        return;
    }
    this.securityService.authorizeDataManage();
    // Process CQ close request
    try {
        // Append Client ID to CQ name
        CqService cqService = crHelper.getCache().getCqService();
        cqService.start();
        String serverCqName = cqName;
        if (id != null) {
            serverCqName = cqService.constructServerCqName(cqName, id);
        }
        InternalCqQuery cqQuery = cqService.getCq(serverCqName);
        AuthorizeRequest authzRequest = serverConnection.getAuthzRequest();
        if (authzRequest != null) {
            String queryStr = null;
            Set cqRegionNames = null;
            if (cqQuery != null) {
                queryStr = cqQuery.getQueryString();
                cqRegionNames = new HashSet();
                cqRegionNames.add(((InternalCqQuery) cqQuery).getRegionName());
                authzRequest.closeCQAuthorize(cqName, queryStr, cqRegionNames);
            }
        }
        // String cqNameWithClientId = new String(cqName + "__" +
        // getMembershipID());
        cqService.closeCq(cqName, id);
        if (cqQuery != null)
            serverConnection.removeCq(cqName, cqQuery.isDurable());
    } catch (CqException cqe) {
        sendCqResponse(MessageType.CQ_EXCEPTION_TYPE, "", clientMessage.getTransactionId(), cqe, serverConnection);
        return;
    } catch (Exception e) {
        String err = LocalizedStrings.CloseCQ_EXCEPTION_WHILE_CLOSING_CQ_CQNAME_0.toLocalizedString(cqName);
        sendCqResponse(MessageType.CQ_EXCEPTION_TYPE, err, clientMessage.getTransactionId(), e, serverConnection);
        return;
    }
    // Send OK to client
    sendCqResponse(MessageType.REPLY, LocalizedStrings.CloseCQ_CQ_CLOSED_SUCCESSFULLY.toLocalizedString(), clientMessage.getTransactionId(), null, serverConnection);
    serverConnection.setAsTrue(RESPONDED);
    {
        long oldStart = start;
        start = DistributionStats.getStatTime();
        stats.incProcessCloseCqTime(start - oldStart);
    }
}
Also used : CachedRegionHelper(org.apache.geode.internal.cache.tier.CachedRegionHelper) ClientProxyMembershipID(org.apache.geode.internal.cache.tier.sockets.ClientProxyMembershipID) Set(java.util.Set) HashSet(java.util.HashSet) CacheServerStats(org.apache.geode.internal.cache.tier.sockets.CacheServerStats) AuthorizeRequest(org.apache.geode.internal.security.AuthorizeRequest) CqException(org.apache.geode.cache.query.CqException) InternalCqQuery(org.apache.geode.cache.query.internal.cq.InternalCqQuery) CqService(org.apache.geode.cache.query.internal.cq.CqService) IOException(java.io.IOException) CqException(org.apache.geode.cache.query.CqException) HashSet(java.util.HashSet)

Example 5 with CqException

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

the class ExecuteCQ 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;
    if (logger.isDebugEnabled()) {
        logger.debug("{}: Received {} request from {} CqName: {} queryString: {}", serverConnection.getName(), MessageType.getString(clientMessage.getMessageType()), serverConnection.getSocketString(), cqName, cqQueryString);
    }
    DefaultQueryService qService = null;
    CqService cqServiceForExec = null;
    Query query = null;
    Set cqRegionNames = null;
    ExecuteCQOperationContext executeCQContext = null;
    ServerCQ 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);
                }
            }
        }
        cqServiceForExec = qService.getCqService();
        cqQuery = cqServiceForExec.executeCq(cqName, cqQueryString, cqState, id, acceptor.getCacheClientNotifier(), isDurable, false, 0, null);
    } catch (CqException cqe) {
        sendCqResponse(MessageType.CQ_EXCEPTION_TYPE, "", clientMessage.getTransactionId(), cqe, serverConnection);
        return;
    } catch (Exception e) {
        writeChunkedException(clientMessage, e, serverConnection);
        return;
    }
    long oldstart = start;
    boolean sendResults = false;
    boolean successQuery = false;
    if (clientMessage.getMessageType() == MessageType.EXECUTECQ_WITH_IR_MSG_TYPE) {
        sendResults = true;
    }
    // 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.
            }
        }
    }
    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 : Set(java.util.Set) DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) Query(org.apache.geode.cache.query.Query) AuthorizeRequest(org.apache.geode.internal.security.AuthorizeRequest) CqException(org.apache.geode.cache.query.CqException) CqService(org.apache.geode.cache.query.internal.cq.CqService) IOException(java.io.IOException) CqException(org.apache.geode.cache.query.CqException) 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) ServerCQ(org.apache.geode.cache.query.internal.cq.ServerCQ) DefaultQueryService(org.apache.geode.cache.query.internal.DefaultQueryService) ExecuteCQOperationContext(org.apache.geode.cache.operations.ExecuteCQOperationContext)

Aggregations

CqException (org.apache.geode.cache.query.CqException)46 CacheException (org.apache.geode.cache.CacheException)20 RegionNotFoundException (org.apache.geode.cache.query.RegionNotFoundException)20 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)20 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)16 Test (org.junit.Test)16 CqExistsException (org.apache.geode.cache.query.CqExistsException)15 QueryService (org.apache.geode.cache.query.QueryService)14 CqClosedException (org.apache.geode.cache.query.CqClosedException)12 ClientSubscriptionTest (org.apache.geode.test.junit.categories.ClientSubscriptionTest)9 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)9 IOException (java.io.IOException)8 List (java.util.List)8 CqQuery (org.apache.geode.cache.query.CqQuery)8 DefaultQueryService (org.apache.geode.cache.query.internal.DefaultQueryService)8 CqService (org.apache.geode.cache.query.internal.cq.CqService)8 Set (java.util.Set)7 CqAttributesFactory (org.apache.geode.cache.query.CqAttributesFactory)7 QueryException (org.apache.geode.cache.query.QueryException)7 InternalCqQuery (org.apache.geode.cache.query.internal.cq.InternalCqQuery)7