Search in sources :

Example 51 with Struct

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

the class IndexUseJUnitTest method testIndexUsageWithOrderBy2.

@Test
public void testIndexUsageWithOrderBy2() throws Exception {
    QueryService qs;
    qs = CacheUtils.getQueryService();
    LocalRegion testRgn = (LocalRegion) CacheUtils.createRegion("testRgn", null);
    int numObjects = 30;
    // and so on
    for (int i = 0; i < numObjects; i++) {
        Portfolio p = new Portfolio(i % 2);
        p.createTime = (numObjects - i);
        testRgn.put("" + i, p);
    }
    qs = CacheUtils.getQueryService();
    String[] queries = { "SELECT DISTINCT p.key, p.value FROM /testRgn.entrySet p  WHERE p.value.ID <= 10 order by p.value.createTime asc limit 1", "SELECT DISTINCT p.key, p.value FROM /testRgn.entrySet p  WHERE p.value.ID <= 10 order by p.value.createTime desc limit 1" };
    Object[][] r = new Object[queries.length][2];
    // Execute Queries without Indexes
    for (int i = 0; i < queries.length; i++) {
        Query q = null;
        try {
            q = CacheUtils.getQueryService().newQuery(queries[i]);
            CacheUtils.getLogger().info("Executing query: " + queries[i]);
            // verify individual result
            SelectResults sr = (SelectResults) q.execute();
            List results = sr.asList();
            for (int rows = 0; rows < results.size(); rows++) {
                Struct s = (Struct) results.get(0);
                Portfolio p = (Portfolio) s.get("value");
                CacheUtils.getLogger().info("p: " + p);
                if (i == 0) {
                    assertEquals(p.createTime, 1);
                } else if (i == 1) {
                    assertEquals(p.createTime, numObjects);
                }
            }
            r[i][0] = sr;
            CacheUtils.log("Executed query: " + queries[i]);
        } catch (Exception e) {
            e.printStackTrace();
            fail(q.getQueryString());
        }
    }
    Index i1 = qs.createIndex("Index1", IndexType.FUNCTIONAL, "p.value.ID", "/testRgn.entrySet p");
    // Execute Queries with Indexes
    for (int i = 0; i < queries.length; i++) {
        Query q = null;
        try {
            q = CacheUtils.getQueryService().newQuery(queries[i]);
            CacheUtils.getLogger().info("Executing query: " + queries[i]);
            QueryObserverImpl observer = new QueryObserverImpl();
            QueryObserverHolder.setInstance(observer);
            SelectResults sr = (SelectResults) q.execute();
            List results = sr.asList();
            for (int rows = 0; rows < results.size(); rows++) {
                Struct s = (Struct) results.get(0);
                Portfolio p = (Portfolio) s.get("value");
                CacheUtils.getLogger().info("index p: " + p);
                if (i == 0) {
                    assertEquals(p.createTime, 1);
                } else if (i == 1) {
                    assertEquals(p.createTime, numObjects);
                }
            }
            r[i][1] = sr;
            // r[i][1] = q.execute();
            CacheUtils.log("Executing query: " + queries[i] + " with index created");
            if (!observer.isIndexesUsed) {
                fail("Index is NOT uesd");
            }
            Iterator itr = observer.indexesUsed.iterator();
            assertTrue(itr.hasNext());
            String temp = itr.next().toString();
            assertEquals(temp, "Index1");
        } catch (Exception e) {
            e.printStackTrace();
            fail(q.getQueryString());
        }
    }
    StructSetOrResultsSet ssOrrs = new StructSetOrResultsSet();
    ssOrrs.CompareQueryResultsWithoutAndWithIndexes(r, queries.length, queries);
}
Also used : Query(org.apache.geode.cache.query.Query) StructSetOrResultsSet(org.apache.geode.cache.query.functional.StructSetOrResultsSet) Portfolio(org.apache.geode.cache.query.data.Portfolio) Index(org.apache.geode.cache.query.Index) LocalRegion(org.apache.geode.internal.cache.LocalRegion) QueryInvalidException(org.apache.geode.cache.query.QueryInvalidException) Struct(org.apache.geode.cache.query.Struct) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 52 with Struct

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

the class PartitionedRegionQueryDUnitTest method createIndexDoesNotDerializePdxObjects.

private void createIndexDoesNotDerializePdxObjects(final SerializableRunnableIF createIndex, final String queryString, PdxAssetFactory valueSupplier) {
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    SerializableRunnableIF createPR = () -> {
        Cache cache = getCache();
        PartitionAttributesFactory paf = new PartitionAttributesFactory();
        paf.setTotalNumBuckets(10);
        cache.createRegionFactory(RegionShortcut.PARTITION).setPartitionAttributes(paf.create()).create("region");
    };
    vm0.invoke(createPR);
    vm1.invoke(createPR);
    // Do Puts. These objects can't be deserialized because they throw
    // and exception from the constructor
    vm0.invoke(() -> {
        Cache cache = getCache();
        Region region = cache.getRegion("region");
        region.put(0, new PdxNotDeserializableAsset(0, "B"));
        region.put(10, new PdxNotDeserializableAsset(1, "B"));
        region.put(1, new PdxNotDeserializableAsset(1, "B"));
        IntStream.range(11, 100).forEach(i -> region.put(i, valueSupplier.getAsset(i)));
    });
    // If this tries to deserialize the assets, it will fail
    vm0.invoke(createIndex);
    vm0.invoke(() -> {
        QueryService qs = getCache().getQueryService();
        SelectResults<Struct> results = (SelectResults) qs.newQuery(queryString).execute();
        assertEquals(3, results.size());
        final Index index = qs.getIndex(getCache().getRegion("region"), "ContractDocumentIndex");
        assertEquals(1, index.getStatistics().getTotalUses());
    });
}
Also used : PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) Host(org.apache.geode.test.dunit.Host) Index(org.apache.geode.cache.query.Index) AbstractIndex(org.apache.geode.cache.query.internal.index.AbstractIndex) PartitionedIndex(org.apache.geode.cache.query.internal.index.PartitionedIndex) SerializableRunnableIF(org.apache.geode.test.dunit.SerializableRunnableIF) Cache(org.apache.geode.cache.Cache) ClientCache(org.apache.geode.cache.client.ClientCache) Struct(org.apache.geode.cache.query.Struct)

Example 53 with Struct

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

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

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

the class CqDataUsingPoolDUnitTest method testEventsDuringQueryExecution.

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

        @Override
        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);
            }
        }
    });
    // First set testhook in executeWithInitialResults so that queued events
    // are not drained before we verify there number.
    client.invoke(setTestHook());
    // Execute CQ while update is in progress.
    AsyncInvocation executeCq = client.invokeAsync(new CacheSerializableRunnable("Execute CQ AsyncInvoke") {

        @Override
        public void run2() throws CacheException {
            QueryService cqService = getCache().getQueryService();
            // Get CqQuery object.
            CqQuery cq1 = cqService.getCq(cqName);
            if (cq1 == null) {
                fail("Failed to get CQ " + cqName);
            }
            SelectResults cqResults = null;
            try {
                cqResults = cq1.executeWithInitialResults();
            } catch (Exception ex) {
                Assert.fail("CQ execution failed", ex);
            }
            // Check num of events received during executeWithInitialResults.
            final TestHook testHook = CqQueryImpl.testHook;
            Wait.waitForCriterion(new WaitCriterion() {

                @Override
                public boolean done() {
                    return testHook.numQueuedEvents() > 0;
                }

                @Override
                public String description() {
                    return "No queued events found.";
                }
            }, 3000, 5, true);
            getCache().getLogger().fine("Queued Events Size" + testHook.numQueuedEvents());
            // Make sure CQEvents are queued during execute with initial results.
            CqQueryTestListener cqListener = (CqQueryTestListener) cq1.getCqAttributes().getCqListener();
            // Wait for the last key to arrive.
            cqListener.waitForCreated("" + totalObjects);
            // Check if the events from CqListener are in order.
            int oldId = 0;
            for (Object cqEvent : cqListener.events.toArray()) {
                int newId = new Integer(cqEvent.toString()).intValue();
                if (oldId > newId) {
                    fail("Queued events for CQ Listener during execution with " + "Initial results is not in the order in which they are created.");
                }
                oldId = newId;
            }
            // Check if all the IDs are present as part of Select Results and CQ Events.
            HashSet ids = new HashSet(cqListener.events);
            for (Object o : cqResults.asList()) {
                Struct s = (Struct) o;
                ids.add(s.get("key"));
            }
            HashSet missingIds = new HashSet();
            String key = "";
            for (int i = 1; i <= totalObjects; i++) {
                key = "" + i;
                if (!(ids.contains(key))) {
                    missingIds.add(key);
                }
            }
            if (!missingIds.isEmpty()) {
                fail("Missing Keys in either ResultSet or the Cq Event list. " + " Missing keys : [size : " + missingIds.size() + "]" + missingIds + " Ids in ResultSet and CQ Events :" + ids);
            }
        }
    });
    // Keep updating region (async invocation).
    server.invoke(new CacheSerializableRunnable("Update Region") {

        @Override
        public void run2() throws CacheException {
            Wait.pause(200);
            client.invoke(new CacheSerializableRunnable("Releasing the latch") {

                @Override
                public void run2() throws CacheException {
                    // Now release the testhook so that CQListener can proceed.
                    final TestHook testHook = CqQueryImpl.testHook;
                    testHook.ready();
                }
            });
            Region region = getCache().getRegion("/root/" + cqDUnitTest.regions[0]);
            for (int i = numObjects + 1; i <= totalObjects; i++) {
                Portfolio p = new Portfolio(i);
                region.put("" + i, p);
            }
        }
    });
    // Close.
    cqDUnitTest.closeClient(client);
    cqDUnitTest.closeServer(server);
}
Also used : CacheException(org.apache.geode.cache.CacheException) Portfolio(org.apache.geode.cache.query.data.Portfolio) Host(org.apache.geode.test.dunit.Host) AsyncInvocation(org.apache.geode.test.dunit.AsyncInvocation) CqExistsException(org.apache.geode.cache.query.CqExistsException) CqException(org.apache.geode.cache.query.CqException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) CacheException(org.apache.geode.cache.CacheException) TestHook(org.apache.geode.cache.query.internal.cq.CqQueryImpl.TestHook) Struct(org.apache.geode.cache.query.Struct) SelectResults(org.apache.geode.cache.query.SelectResults) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) QueryService(org.apache.geode.cache.query.QueryService) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) CqQuery(org.apache.geode.cache.query.CqQuery) HashSet(java.util.HashSet) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) Test(org.junit.Test)

Aggregations

Struct (org.apache.geode.cache.query.Struct)110 SelectResults (org.apache.geode.cache.query.SelectResults)80 QueryService (org.apache.geode.cache.query.QueryService)70 Region (org.apache.geode.cache.Region)67 Test (org.junit.Test)66 Query (org.apache.geode.cache.query.Query)57 Iterator (java.util.Iterator)54 Portfolio (org.apache.geode.cache.query.data.Portfolio)46 DefaultQuery (org.apache.geode.cache.query.internal.DefaultQuery)30 ObjectType (org.apache.geode.cache.query.types.ObjectType)30 CompiledSelect (org.apache.geode.cache.query.internal.CompiledSelect)27 StructType (org.apache.geode.cache.query.types.StructType)26 HashSet (java.util.HashSet)19 List (java.util.List)17 PortfolioPdx (org.apache.geode.cache.query.data.PortfolioPdx)17 CacheException (org.apache.geode.cache.CacheException)16 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)16 ArrayList (java.util.ArrayList)15 Host (org.apache.geode.test.dunit.Host)14 VM (org.apache.geode.test.dunit.VM)14