Search in sources :

Example 41 with CacheLoaderException

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

the class MultiVMRegionTestCase method testNoLoaderWithInvalidEntry.

/**
   * Tests that a remote <code>CacheLoader</code> is not invoked if the remote region has an invalid
   * entry (that is, a key, but no value).
   */
@Test
public void testNoLoaderWithInvalidEntry() throws Exception {
    assumeTrue(supportsNetLoad());
    final String name = this.getUniqueName();
    final Object key = "KEY";
    final Object value = "VALUE";
    SerializableRunnable create = new CacheSerializableRunnable("Create Region") {

        @Override
        public void run2() throws CacheException {
            Region region = createRegion(name);
            loader = new TestCacheLoader() {

                @Override
                public Object load2(LoaderHelper helper) throws CacheLoaderException {
                    return value;
                }
            };
            region.getAttributesMutator().setCacheLoader(loader);
        }
    };
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    vm0.invoke(create);
    vm1.invoke(create);
    vm1.invoke(new CacheSerializableRunnable("Create invalid entry") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            region.create(key, null);
        }
    });
    vm0.invoke(new CacheSerializableRunnable("Remote get") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            assertEquals(value, region.get(key));
            assertTrue(loader.wasInvoked());
        }
    });
    vm1.invoke(new SerializableRunnable("Verify loader") {

        @Override
        public void run() {
            assertFalse(loader.wasInvoked());
        }
    });
}
Also used : LoaderHelper(org.apache.geode.cache.LoaderHelper) CacheException(org.apache.geode.cache.CacheException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) VM(org.apache.geode.test.dunit.VM) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) LocalRegion(org.apache.geode.internal.cache.LocalRegion) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) StoredObject(org.apache.geode.internal.offheap.StoredObject) Host(org.apache.geode.test.dunit.Host) Test(org.junit.Test) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest)

Example 42 with CacheLoaderException

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

the class ProxyDUnitTest method distributedOps.

////////////////////// Test Methods //////////////////////
/**
   * check distributed ops that originate in a PROXY are correctly distributed to non-proxy regions.
   */
private void distributedOps(DataPolicy dp, InterestPolicy ip) throws CacheException {
    initOtherId();
    AttributesFactory af = new AttributesFactory();
    af.setDataPolicy(dp);
    af.setSubscriptionAttributes(new SubscriptionAttributes(ip));
    af.setScope(Scope.DISTRIBUTED_ACK);
    Region r = createRootRegion("ProxyDUnitTest", af.create());
    doCreateOtherVm();
    r.put("putkey", "putvalue1");
    getOtherVm().invoke(new CacheSerializableRunnable("check put") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            assertEquals(true, r.containsKey("putkey"));
            assertEquals("putvalue1", r.getEntry("putkey").getValue());
            r.put("putkey", "putvalue2");
        }
    });
    assertEquals(false, r.containsKey("putkey"));
    // netsearch
    assertEquals("putvalue2", r.get("putkey"));
    r.invalidate("putkey");
    getOtherVm().invoke(new CacheSerializableRunnable("check invalidate") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            assertEquals(true, r.containsKey("putkey"));
            assertEquals(null, r.getEntry("putkey").getValue());
        }
    });
    // invalid so total miss
    assertEquals(null, r.get("putkey"));
    r.destroy("putkey");
    getOtherVm().invoke(new CacheSerializableRunnable("check destroy") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            assertEquals(false, r.containsKey("putkey"));
        }
    });
    // total miss
    assertEquals(null, r.get("putkey"));
    r.create("createKey", "createValue1");
    getOtherVm().invoke(new CacheSerializableRunnable("check create") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            assertEquals(true, r.containsKey("createKey"));
            assertEquals("createValue1", r.getEntry("createKey").getValue());
        }
    });
    {
        Map m = new HashMap();
        m.put("putAllKey1", "putAllValue1");
        m.put("putAllKey2", "putAllValue2");
        r.putAll(m, "putAllCallback");
    }
    getOtherVm().invoke(new CacheSerializableRunnable("check putAll") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            assertEquals(true, r.containsKey("putAllKey1"));
            assertEquals("putAllValue1", r.getEntry("putAllKey1").getValue());
            assertEquals(true, r.containsKey("putAllKey2"));
            assertEquals("putAllValue2", r.getEntry("putAllKey2").getValue());
        }
    });
    r.clear();
    getOtherVm().invoke(new CacheSerializableRunnable("check clear") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            assertEquals(0, r.size());
        }
    });
    getOtherVm().invoke(new CacheSerializableRunnable("install CacheWriter") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            AttributesMutator am = r.getAttributesMutator();
            CacheWriter cw = new CacheWriterAdapter() {

                public void beforeCreate(EntryEvent event) throws CacheWriterException {
                    throw new CacheWriterException("expected");
                }
            };
            am.setCacheWriter(cw);
        }
    });
    try {
        r.put("putkey", "putvalue");
        fail("expected CacheWriterException");
    } catch (CacheWriterException expected) {
    }
    getOtherVm().invoke(new CacheSerializableRunnable("check clear") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            assertEquals(0, r.size());
        }
    });
    // total miss
    assertEquals(null, r.get("loadkey"));
    getOtherVm().invoke(new CacheSerializableRunnable("install CacheLoader") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            AttributesMutator am = r.getAttributesMutator();
            // clear csche writer
            am.setCacheWriter(null);
            CacheLoader cl = new CacheLoader() {

                public Object load(LoaderHelper helper) throws CacheLoaderException {
                    if (helper.getKey().equals("loadkey")) {
                        return "loadvalue";
                    } else if (helper.getKey().equals("loadexception")) {
                        throw new CacheLoaderException("expected");
                    } else {
                        return null;
                    }
                }

                public void close() {
                }
            };
            am.setCacheLoader(cl);
        }
    });
    // net load
    assertEquals("loadvalue", r.get("loadkey"));
    // total miss
    assertEquals(null, r.get("foobar"));
    try {
        r.get("loadexception");
        fail("expected CacheLoaderException");
    } catch (CacheLoaderException expected) {
    }
    r.destroyRegion();
    getOtherVm().invoke(new CacheSerializableRunnable("check clear") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            assertEquals(null, r);
        }
    });
}
Also used : CacheWriterAdapter(org.apache.geode.cache.util.CacheWriterAdapter) CacheException(org.apache.geode.cache.CacheException) HashMap(java.util.HashMap) LoaderHelper(org.apache.geode.cache.LoaderHelper) AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) EntryEvent(org.apache.geode.cache.EntryEvent) CacheWriter(org.apache.geode.cache.CacheWriter) Region(org.apache.geode.cache.Region) CacheLoader(org.apache.geode.cache.CacheLoader) HashMap(java.util.HashMap) Map(java.util.Map) SubscriptionAttributes(org.apache.geode.cache.SubscriptionAttributes) AttributesMutator(org.apache.geode.cache.AttributesMutator) CacheWriterException(org.apache.geode.cache.CacheWriterException)

Example 43 with CacheLoaderException

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

the class AbstractBaseController method getValue.

protected <T> T getValue(final String regionNamePath, final Object key, boolean postProcess) {
    Assert.notNull(key, "The Cache Region key to read the value for cannot be null!");
    Region r = getRegion(regionNamePath);
    try {
        Object value = r.get(key);
        if (postProcess) {
            return (T) securityService.postProcess(regionNamePath, key, value, false);
        } else {
            return (T) value;
        }
    } catch (SerializationException se) {
        throw new DataTypeNotSupportedException("The resource identified could not convert into the supported content characteristics (JSON)!", se);
    } catch (NullPointerException npe) {
        throw new GemfireRestException(String.format("Resource (%1$s) configuration does not allow null keys!", regionNamePath), npe);
    } catch (IllegalArgumentException iae) {
        throw new GemfireRestException(String.format("Resource (%1$s) configuration does not allow requested operation on specified key!", regionNamePath), iae);
    } catch (LeaseExpiredException lee) {
        throw new GemfireRestException("Server has encountered error while processing this request!", lee);
    } catch (TimeoutException te) {
        throw new GemfireRestException("Server has encountered timeout error while processing this request!", te);
    } catch (CacheLoaderException cle) {
        throw new GemfireRestException("Server has encountered CacheLoader error while processing this request!", cle);
    } catch (PartitionedRegionStorageException prse) {
        throw new GemfireRestException("CacheLoader could not be invoked on partitioned region!", prse);
    }
}
Also used : GemfireRestException(org.apache.geode.rest.internal.web.exception.GemfireRestException) SerializationException(org.apache.geode.SerializationException) LeaseExpiredException(org.apache.geode.distributed.LeaseExpiredException) DataTypeNotSupportedException(org.apache.geode.rest.internal.web.exception.DataTypeNotSupportedException) PartitionedRegionStorageException(org.apache.geode.cache.PartitionedRegionStorageException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) Region(org.apache.geode.cache.Region) JSONObject(org.json.JSONObject) TimeoutException(org.apache.geode.cache.TimeoutException)

Example 44 with CacheLoaderException

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

the class CqServiceImpl method closeCq.

@Override
public void closeCq(String cqName, ClientProxyMembershipID clientProxyId) throws CqException {
    String serverCqName = cqName;
    if (clientProxyId != null) {
        serverCqName = this.constructServerCqName(cqName, clientProxyId);
        removeFromCacheForServerToConstructedCQName(cqName, clientProxyId);
    }
    ServerCQImpl cQuery = null;
    StringId errMsg = null;
    Exception ex = null;
    try {
        HashMap<String, CqQueryImpl> cqMap = cqQueryMap;
        if (!cqMap.containsKey(serverCqName)) {
            /*
         * gregp 052808: We should silently fail here instead of throwing error. This is to deal
         * with races in recovery
         */
            return;
        }
        cQuery = (ServerCQImpl) cqMap.get(serverCqName);
    } catch (CacheLoaderException e1) {
        errMsg = LocalizedStrings.CqService_CQ_NOT_FOUND_IN_THE_CQ_META_REGION_CQNAME_0;
        ex = e1;
    } catch (TimeoutException e2) {
        errMsg = LocalizedStrings.CqService_TIMEOUT_WHILE_TRYING_TO_GET_CQ_FROM_META_REGION_CQNAME_0;
        ex = e2;
    } finally {
        if (ex != null) {
            String s = errMsg.toLocalizedString(cqName);
            if (logger.isDebugEnabled()) {
                logger.debug(s);
            }
            throw new CqException(s, ex);
        }
    }
    try {
        cQuery.close(false);
        // CqBaseRegion
        try {
            LocalRegion baseRegion = cQuery.getCqBaseRegion();
            if (baseRegion != null && !baseRegion.isDestroyed()) {
                // Server specific clean up.
                if (isServer()) {
                    FilterProfile fp = baseRegion.getFilterProfile();
                    if (fp != null) {
                        fp.closeCq(cQuery);
                    }
                    CacheClientProxy clientProxy = cQuery.getCacheClientNotifier().getClientProxy(clientProxyId);
                    clientProxy.decCqCount();
                    if (clientProxy.hasNoCq()) {
                        this.stats.decClientsWithCqs();
                    }
                }
            }
        } catch (Exception e) {
            // May be cache is being shutdown
            if (logger.isDebugEnabled()) {
                logger.debug("Failed to remove CQ from the base region. CqName : {}", cqName);
            }
        }
        if (isServer()) {
            removeFromBaseRegionToCqNameMap(cQuery.getRegionName(), serverCqName);
        }
        LocalRegion baseRegion = cQuery.getCqBaseRegion();
        if (baseRegion.getFilterProfile().getCqCount() <= 0) {
            if (logger.isDebugEnabled()) {
                logger.debug("Should update the profile for this partitioned region {} for not requiring old value", baseRegion);
            }
        }
    } catch (CqClosedException cce) {
        throw new CqException(cce.getMessage());
    } finally {
        this.removeFromMatchingCqMap(cQuery);
    }
}
Also used : CacheClientProxy(org.apache.geode.internal.cache.tier.sockets.CacheClientProxy) CqException(org.apache.geode.cache.query.CqException) CqClosedException(org.apache.geode.cache.query.CqClosedException) LocalRegion(org.apache.geode.internal.cache.LocalRegion) TimeoutException(org.apache.geode.cache.TimeoutException) CqExistsException(org.apache.geode.cache.query.CqExistsException) CqException(org.apache.geode.cache.query.CqException) QueryInvalidException(org.apache.geode.cache.query.QueryInvalidException) InvalidDeltaException(org.apache.geode.InvalidDeltaException) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) CqClosedException(org.apache.geode.cache.query.CqClosedException) QueryException(org.apache.geode.cache.query.QueryException) FilterProfile(org.apache.geode.internal.cache.FilterProfile) StringId(org.apache.geode.i18n.StringId) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) TimeoutException(org.apache.geode.cache.TimeoutException)

Example 45 with CacheLoaderException

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

the class LuceneQueriesIntegrationTest method shouldReturnCorrectResultsOnDeletionAfterQueryExecutionWithLoader.

@Test
public void shouldReturnCorrectResultsOnDeletionAfterQueryExecutionWithLoader() throws Exception {
    final int pageSize = 2;
    final LuceneQuery<Object, Object> query = addValuesAndCreateQuery(pageSize);
    region.getAttributesMutator().setCacheLoader(new CacheLoader() {

        @Override
        public Object load(final LoaderHelper helper) throws CacheLoaderException {
            return new TestObject("should not", "load this");
        }

        @Override
        public void close() {
        }
    });
    final PageableLuceneQueryResults<Object, Object> pages = query.findPages();
    List<LuceneResultStruct<Object, Object>> allEntries = new ArrayList<>();
    assertTrue(pages.hasNext());
    assertEquals(7, pages.size());
    // Destroying an entry from the region after the query is executed.
    region.destroy("C");
    final List<LuceneResultStruct<Object, Object>> page1 = pages.next();
    assertEquals(pageSize, page1.size());
    final List<LuceneResultStruct<Object, Object>> page2 = pages.next();
    assertEquals(pageSize, page2.size());
    final List<LuceneResultStruct<Object, Object>> page3 = pages.next();
    assertEquals(pageSize, page3.size());
    assertFalse(pages.hasNext());
    allEntries.addAll(page1);
    allEntries.addAll(page2);
    allEntries.addAll(page3);
    assertEquals(region.keySet(), allEntries.stream().map(entry -> entry.getKey()).collect(Collectors.toSet()));
    assertEquals(region.values(), allEntries.stream().map(entry -> entry.getValue()).collect(Collectors.toSet()));
}
Also used : LoaderHelper(org.apache.geode.cache.LoaderHelper) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) ArrayList(java.util.ArrayList) TestObject(org.apache.geode.cache.lucene.test.TestObject) TestObject(org.apache.geode.cache.lucene.test.TestObject) CacheLoader(org.apache.geode.cache.CacheLoader) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

CacheLoaderException (org.apache.geode.cache.CacheLoaderException)45 Test (org.junit.Test)32 LoaderHelper (org.apache.geode.cache.LoaderHelper)31 Region (org.apache.geode.cache.Region)31 AttributesFactory (org.apache.geode.cache.AttributesFactory)25 VM (org.apache.geode.test.dunit.VM)23 CacheException (org.apache.geode.cache.CacheException)22 Host (org.apache.geode.test.dunit.Host)22 CacheLoader (org.apache.geode.cache.CacheLoader)19 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)19 TimeoutException (org.apache.geode.cache.TimeoutException)15 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)15 LocalRegion (org.apache.geode.internal.cache.LocalRegion)14 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)11 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)10 CacheWriterException (org.apache.geode.cache.CacheWriterException)9 StoredObject (org.apache.geode.internal.offheap.StoredObject)8 EntryEvent (org.apache.geode.cache.EntryEvent)7 AttributesMutator (org.apache.geode.cache.AttributesMutator)6 PartitionAttributesFactory (org.apache.geode.cache.PartitionAttributesFactory)6