Search in sources :

Example 21 with CacheLoader

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

the class CacheAdvisorDUnitTest method testNetLoadAdviceWithAttributesMutator.

@Test
public void testNetLoadAdviceWithAttributesMutator() throws Exception {
    final String rgnName = getUniqueName();
    AttributesFactory fac = new AttributesFactory();
    fac.setScope(Scope.DISTRIBUTED_ACK);
    RegionAttributes attrs = fac.create();
    DistributedRegion rgn = (DistributedRegion) createRegion(rgnName, attrs);
    Invoke.invokeInEveryVM(new CacheSerializableRunnable("CachAdvisorTest.testNetLoadAdviceWithAttributesMutator;createRegion") {

        public void run2() throws CacheException {
            AttributesFactory f = new AttributesFactory();
            f.setScope(Scope.DISTRIBUTED_ACK);
            createRegion(rgnName, f.create());
        }
    });
    Set expected = new HashSet();
    for (int i = 1; i < vms.length; i += 2) {
        VM vm = vms[i];
        final int numVMsMinusOne = vms.length;
        InternalDistributedMember id = ids[i];
        expected.add(id);
        // final int index = i;
        vm.invoke(new CacheSerializableRunnable("CacheAdvisorDUnitTest.testNetLoadAdviceWithAttributesMutator;mutate") {

            public void run2() throws CacheException {
                Region rgn1 = getRootRegion().getSubregion(rgnName);
                assertEquals(numVMsMinusOne, ((DistributedRegion) rgn1).getDistributionAdvisor().adviseGeneric().size());
                AttributesMutator mut = rgn1.getAttributesMutator();
                mut.setCacheLoader(new CacheLoader() {

                    public Object load(LoaderHelper helper) throws CacheLoaderException {
                        return null;
                    }

                    public void close() {
                    }
                });
            }
        });
    }
    Awaitility.await().pollInterval(10, TimeUnit.MILLISECONDS).pollDelay(10, TimeUnit.MILLISECONDS).atMost(30, TimeUnit.SECONDS).until(() -> assertEquals(expected, rgn.getCacheDistributionAdvisor().adviseNetLoad()));
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) RegionAttributes(org.apache.geode.cache.RegionAttributes) CacheException(org.apache.geode.cache.CacheException) LoaderHelper(org.apache.geode.cache.LoaderHelper) AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) CacheLoader(org.apache.geode.cache.CacheLoader) HashSet(java.util.HashSet) AttributesMutator(org.apache.geode.cache.AttributesMutator) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 22 with CacheLoader

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

the class LRUEvictionControllerDUnitTest method testCacheLoader.

/**
   * Carefully verifies that region operations effect the {@link LRUStatistics} as expected in the
   * presense of a {@link CacheLoader}.
   */
@Test
public void testCacheLoader() throws CacheException {
    int threshold = 10;
    final String name = this.getUniqueName();
    AttributesFactory factory = new AttributesFactory();
    factory.setScope(Scope.LOCAL);
    factory.setEvictionAttributes(EvictionAttributes.createLRUEntryAttributes(threshold));
    factory.setCacheLoader(new CacheLoader() {

        public Object load(LoaderHelper helper) throws CacheLoaderException {
            return "LOADED VALUE";
        }

        public void close() {
        }
    });
    Region region;
    if (usingMain) {
        DistributedSystem system = DistributedSystem.connect(new Properties());
        Cache cache = CacheFactory.create(system);
        region = cache.createRegion("Test", factory.create());
    } else {
        region = createRegion(name, factory.create());
    }
    LRUStatistics lruStats = getLRUStats(region);
    assertNotNull(lruStats);
    for (int i = 1; i <= 10; i++) {
        Object key = new Integer(i);
        Object value = String.valueOf(i);
        region.put(key, value);
        assertEquals(i, lruStats.getCounter());
        assertEquals(0, lruStats.getEvictions());
    }
    for (int i = 11; i <= 20; i++) {
        Object key = new Integer(i);
        // Object value = String.valueOf(i);
        // Invoke loader
        region.get(key);
        assertEquals(10, lruStats.getCounter());
        assertEquals(i - 10, lruStats.getEvictions());
    }
}
Also used : Properties(java.util.Properties) DistributedSystem(org.apache.geode.distributed.DistributedSystem) LoaderHelper(org.apache.geode.cache.LoaderHelper) AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) LRUStatistics(org.apache.geode.internal.cache.lru.LRUStatistics) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) CacheLoader(org.apache.geode.cache.CacheLoader) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 23 with CacheLoader

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

the class HARegion method findObjectInSystem.

/**
   * @return the deserialized value
   * @see LocalRegion#findObjectInSystem(KeyInfo, boolean, TXStateInterface, boolean, Object,
   *      boolean, boolean, ClientProxyMembershipID, EntryEventImpl, boolean)
   * 
   */
@Override
protected Object findObjectInSystem(KeyInfo keyInfo, boolean isCreate, TXStateInterface txState, boolean generateCallbacks, Object localValue, boolean disableCopyOnRead, boolean preferCD, ClientProxyMembershipID requestingClient, EntryEventImpl clientEvent, boolean returnTombstones) throws CacheLoaderException, TimeoutException {
    Object value = null;
    final Object key = keyInfo.getKey();
    final Object aCallbackArgument = keyInfo.getCallbackArg();
    // copy into local var to prevent race condition
    RegionEntry re = null;
    Assert.assertTrue(!hasServerProxy());
    CacheLoader loader = basicGetLoader();
    if (loader != null) {
        final LoaderHelper loaderHelper = loaderHelperFactory.createLoaderHelper(key, aCallbackArgument, false, /* netSearchAllowed */
        true, /* netloadallowed */
        null);
        try {
            value = loader.load(loaderHelper);
        } finally {
        }
        if (value != null) {
            try {
                validateKey(key);
                Operation op;
                if (isCreate) {
                    op = Operation.LOCAL_LOAD_CREATE;
                } else {
                    op = Operation.LOCAL_LOAD_UPDATE;
                }
                @Released EntryEventImpl event = EntryEventImpl.create(this, op, key, value, aCallbackArgument, false, getMyId(), generateCallbacks);
                try {
                    re = basicPutEntry(event, 0L);
                } finally {
                    event.release();
                }
                if (txState == null) {
                }
            } catch (CacheWriterException cwe) {
            // @todo smenon Log the exception
            }
        }
    }
    if (isCreate) {
        recordMiss(re, key);
    }
    return value;
}
Also used : LoaderHelper(org.apache.geode.cache.LoaderHelper) Released(org.apache.geode.internal.offheap.annotations.Released) CacheLoader(org.apache.geode.cache.CacheLoader) Operation(org.apache.geode.cache.Operation) CacheWriterException(org.apache.geode.cache.CacheWriterException)

Example 24 with CacheLoader

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

the class TXJUnitTest method testCacheCallbacks.

/**
   * Test to make sure CacheListener callbacks are called in place with the CacheEvents properly
   * constructed
   */
@Test
public void testCacheCallbacks() throws CacheException {
    final String key1 = "Key1";
    final String value1 = "value1";
    final String value2 = "value2";
    final String callBackArg = "call back arg";
    // install listeners
    AttributesMutator<String, String> mutator = this.region.getAttributesMutator();
    TXCallBackValidator cbv = new TXCallBackValidator();
    // Cache Listener
    ValidatableCacheListener vCl = new ValidatableCacheListener() {

        TXCallBackValidator v;

        int callCount;

        int prevCallCount;

        EntryEvent lastEvent;

        @Override
        public void validate() {
            this.v.validate(this.lastEvent, this.callCount);
        }

        void validate(EntryEvent event) {
            this.v.validate(event, ++this.callCount);
        }

        @Override
        public void setValidator(TXCallBackValidator v) {
            this.v = v;
        }

        @Override
        public void close() {
        }

        @Override
        public void afterCreate(EntryEvent event) {
            lastEvent = event;
            if (this.v.isSuspendValidation()) {
                return;
            }
            this.validate(event);
            this.v.setPassedValidation(false);
            assertTrue("IsCreate Assertion!", this.v.isCreate());
            assertTrue(event.getRegion().containsKey(this.v.getKey()));
            assertTrue(event.getRegion().containsValueForKey(this.v.getKey()));
            assertNotNull(event.getRegion().getEntry(event.getKey()).getValue());
            this.v.setPassedValidation(true);
        }

        @Override
        public void afterUpdate(EntryEvent event) {
            lastEvent = event;
            if (this.v.isSuspendValidation()) {
                return;
            }
            validate(event);
            this.v.setPassedValidation(false);
            assertTrue("IsUpdate Assertion!", this.v.isUpdate());
            assertTrue(event.getRegion().containsKey(this.v.getKey()));
            assertTrue(event.getRegion().containsValueForKey(this.v.getKey()));
            assertNotNull(event.getRegion().getEntry(event.getKey()).getValue());
            this.v.setPassedValidation(true);
        }

        @Override
        public void afterInvalidate(EntryEvent event) {
            lastEvent = event;
            if (this.v.isSuspendValidation()) {
                return;
            }
            validate(event);
            this.v.setPassedValidation(false);
            assertTrue("IsInvalidate Assertion!", this.v.isInvalidate());
            assertTrue(event.getRegion().containsKey(this.v.getKey()));
            assertTrue(!event.getRegion().containsValueForKey(this.v.getKey()));
            assertNull(event.getRegion().getEntry(event.getKey()).getValue());
            this.v.setPassedValidation(true);
        }

        @Override
        public void afterDestroy(EntryEvent event) {
            lastEvent = event;
            if (this.v.isSuspendValidation()) {
                return;
            }
            validate(event);
            this.v.setPassedValidation(false);
            assertTrue("IsDestroy Assertion!", this.v.isDestroy());
            assertTrue(!event.getRegion().containsKey(this.v.getKey()));
            assertTrue(!event.getRegion().containsValueForKey(this.v.getKey()));
            assertNull(event.getRegion().getEntry(event.getKey()));
            this.v.setPassedValidation(true);
        }

        @Override
        public void afterRegionInvalidate(RegionEvent event) {
            fail("Unexpected invocation of afterRegionInvalidate");
        }

        @Override
        public void afterRegionDestroy(RegionEvent event) {
            if (!event.getOperation().isClose()) {
                fail("Unexpected invocation of afterRegionDestroy");
            }
        }

        @Override
        public void afterRegionClear(RegionEvent event) {
        }

        @Override
        public void afterRegionCreate(RegionEvent event) {
        }

        @Override
        public void afterRegionLive(RegionEvent event) {
        }

        @Override
        public void reset() {
            lastEvent = null;
            prevCallCount = callCount;
        }

        @Override
        public void validateNoEvents() {
            assertNull("Did not expect listener callback", lastEvent);
            assertEquals(prevCallCount, callCount);
        }

        @Override
        public void setExpectedCount(int count) {
            callCount = count;
        }

        @Override
        public int getCallCount() {
            return callCount;
        }
    };
    vCl.setValidator(cbv);
    mutator.setCacheListener(vCl);
    // CacheWriter
    ValidatableCacheWriter vCw = new ValidatableCacheWriter() {

        TXCallBackValidator v;

        int callCount;

        int prevCallCount;

        EntryEvent lastEvent;

        @Override
        public int getCallCount() {
            return this.callCount;
        }

        @Override
        public void localDestroyMakeup(int count) {
            this.callCount += count;
        }

        @Override
        public void validate() {
            this.v.validate(this.lastEvent, this.callCount);
        }

        void validate(EntryEvent event) {
            this.v.validate(event, ++this.callCount);
        }

        @Override
        public void setValidator(TXCallBackValidator v) {
            this.v = v;
        }

        @Override
        public void close() {
        }

        @Override
        public void beforeCreate(EntryEvent event) {
            lastEvent = event;
            if (this.v.isSuspendValidation()) {
                return;
            }
            validate(event);
            this.v.setPassedValidation(false);
            assertTrue("IsCreate Assertion!", this.v.isCreate());
            assertTrue(!event.getRegion().containsKey(this.v.getKey()));
            assertTrue(!event.getRegion().containsValueForKey(this.v.getKey()));
            assertNull(event.getRegion().getEntry(event.getKey()));
            this.v.setPassedValidation(true);
        }

        @Override
        public void beforeUpdate(EntryEvent event) {
            lastEvent = event;
            if (this.v.isSuspendValidation()) {
                return;
            }
            validate(event);
            this.v.setPassedValidation(false);
            assertTrue("IsUpdate Assertion!", this.v.isUpdate());
            assertTrue(event.getRegion().containsKey(this.v.getKey()));
            // Can not assert the following line, as the value being update may be invalid
            // assertTrue(event.getRegion().containsValueForKey(this.v.getKey()));
            this.v.setPassedValidation(true);
        }

        @Override
        public void beforeDestroy(EntryEvent event) {
            lastEvent = event;
            if (this.v.isSuspendValidation()) {
                return;
            }
            validate(event);
            this.v.setPassedValidation(false);
            assertTrue("IsDestroy Assertion!", this.v.isDestroy());
            assertTrue(event.getRegion().containsKey(this.v.getKey()));
            this.v.setPassedValidation(true);
        }

        @Override
        public void beforeRegionDestroy(RegionEvent event) {
            fail("Unexpected invocation of beforeRegionDestroy");
        }

        @Override
        public void beforeRegionClear(RegionEvent event) {
            fail("Unexpected invocation of beforeRegionClear");
        }

        @Override
        public void reset() {
            lastEvent = null;
            prevCallCount = callCount;
        }

        @Override
        public void validateNoEvents() {
            assertNull("Did not expect a writer event", lastEvent);
            assertEquals(prevCallCount, callCount);
        }
    };
    vCw.setValidator(cbv);
    mutator.setCacheWriter(vCw);
    // Cache Loader
    mutator.setCacheLoader(new CacheLoader() {

        int count = 0;

        @Override
        public Object load(LoaderHelper helper) throws CacheLoaderException {
            return count++;
        }

        @Override
        public void close() {
        }
    });
    // Use this to track the number of callout method invocations
    int appCallCount = 1;
    // Create => beforeCreate/afterCreate tests
    cbv.setKey(key1);
    cbv.setCallBackArg(callBackArg);
    cbv.setNewValue(value1, false);
    cbv.setOldValue(null, true);
    cbv.setIsDistributed(true);
    cbv.setIsLoad(false);
    cbv.setIsCreate(true);
    cbv.setIsUpdate(false);
    // Test non-transactional create expecting beforeCreate/afterCreate call
    cbv.setTXId(txMgr.getTransactionId());
    cbv.setExpectedCount(appCallCount++);
    this.region.create(key1, value1, callBackArg);
    assertTrue("Non-TX Create Validation Assertion", cbv.passedValidation());
    cbv.suspendValidation(true);
    this.region.localDestroy(key1);
    cbv.suspendValidation(false);
    // Test transactional create expecting afterCreate call
    this.txMgr.begin();
    cbv.setTXId(txMgr.getTransactionId());
    cbv.setExpectedCount(appCallCount++);
    this.region.create(key1, value1, callBackArg);
    this.txMgr.commit();
    assertTrue("TX Create Validation Assertion", cbv.passedValidation());
    cbv.suspendValidation(true);
    this.region.localDestroy(key1);
    // Put => afterCreate tests
    cbv.suspendValidation(false);
    cbv.setNewValue(value2, false);
    cbv.setOldValue(null, true);
    cbv.setIsDistributed(true);
    cbv.setIsLoad(false);
    cbv.setIsCreate(true);
    cbv.setIsUpdate(false);
    // Test non-transactional put expecting afterCreate call due to no
    // previous Entry
    cbv.setTXId(txMgr.getTransactionId());
    cbv.setExpectedCount(appCallCount++);
    this.region.put(key1, value2, callBackArg);
    assertTrue("Non-TX Put->Create Validation Assertion", cbv.passedValidation());
    cbv.suspendValidation(true);
    this.region.localDestroy(key1);
    cbv.suspendValidation(false);
    // Test transactional put expecting afterCreate call due to no
    // previous Entry
    this.txMgr.begin();
    cbv.setTXId(txMgr.getTransactionId());
    cbv.setExpectedCount(appCallCount++);
    this.region.put(key1, value2, callBackArg);
    this.txMgr.commit();
    assertTrue("TX Put->Create Validation Assertion", cbv.passedValidation());
    // Put => afterUpdate tests
    cbv.setNewValue(value1, false);
    cbv.setOldValue(value2, false);
    cbv.setIsDistributed(true);
    cbv.setIsLoad(false);
    cbv.setIsCreate(false);
    cbv.setIsUpdate(true);
    // Test non-transactional put expecting afterUpdate call due to
    // previous Entry
    cbv.setTXId(txMgr.getTransactionId());
    cbv.setExpectedCount(appCallCount++);
    this.region.put(key1, value1, callBackArg);
    assertTrue("Non-TX Put->Update Validation Assertion", cbv.passedValidation());
    cbv.suspendValidation(true);
    this.region.localDestroy(key1);
    this.region.put(key1, value2);
    cbv.suspendValidation(false);
    // Test transactional put expecting afterUpdate call due to
    // previous Entry
    this.txMgr.begin();
    cbv.setTXId(txMgr.getTransactionId());
    cbv.setExpectedCount(appCallCount++);
    this.region.put(key1, value1, callBackArg);
    this.txMgr.commit();
    assertTrue("TX Put->Update Validation Assertion", cbv.passedValidation());
    // LocalDestroy => afterDestroy, non-distributed tests
    cbv.setNewValue(null, true);
    cbv.setOldValue(value1, false);
    cbv.setIsDistributed(false);
    cbv.setIsLoad(false);
    cbv.setIsDestroy(true);
    cbv.setIsUpdate(false);
    // Test non-transactional localDestroy, expecting afterDestroy,
    // non-distributed
    cbv.setTXId(txMgr.getTransactionId());
    cbv.setExpectedCount(appCallCount++);
    this.region.localDestroy(key1, callBackArg);
    if (!isPR())
        // Account for cacheWriter not begin called
        vCw.localDestroyMakeup(1);
    assertTrue("Non-TX LocalDestroy Validation Assertion", cbv.passedValidation());
    cbv.suspendValidation(true);
    this.region.create(key1, value1);
    cbv.suspendValidation(false);
    // Test transactional localDestroy expecting afterDestroy,
    // non-distributed
    this.txMgr.begin();
    cbv.setTXId(txMgr.getTransactionId());
    cbv.setExpectedCount(appCallCount++);
    this.region.localDestroy(key1, callBackArg);
    if (!isPR())
        // Account for cacheWriter not begin called
        vCw.localDestroyMakeup(1);
    this.txMgr.commit();
    assertTrue("TX LocalDestroy Validation Assertion", cbv.passedValidation());
    // Destroy => afterDestroy, distributed tests
    cbv.setNewValue(null, true);
    cbv.setOldValue(value1, false);
    cbv.setIsDistributed(true);
    cbv.setIsLoad(false);
    cbv.setIsDestroy(true);
    cbv.suspendValidation(true);
    this.region.create(key1, value1);
    cbv.suspendValidation(false);
    // Test non-transactional Destroy, expecting afterDestroy,
    // distributed
    cbv.setTXId(txMgr.getTransactionId());
    cbv.setExpectedCount(appCallCount++);
    this.region.destroy(key1, callBackArg);
    assertTrue("Non-TX Destroy Validation Assertion", cbv.passedValidation());
    cbv.suspendValidation(true);
    this.region.create(key1, value1);
    cbv.suspendValidation(false);
    // Test transactional Destroy, expecting afterDestroy,
    // distributed
    this.txMgr.begin();
    cbv.setTXId(txMgr.getTransactionId());
    cbv.setExpectedCount(appCallCount++);
    this.region.destroy(key1, callBackArg);
    this.txMgr.commit();
    assertTrue("TX Destroy Validation Assertion", cbv.passedValidation());
    // localInvalidate => afterInvalidate, non-distributed tests
    cbv.setNewValue(null, true);
    cbv.setOldValue(value1, false);
    cbv.setIsDistributed(false);
    cbv.setIsLoad(false);
    cbv.setIsInvalidate(true);
    cbv.setIsDestroy(false);
    cbv.suspendValidation(true);
    this.region.create(key1, value1);
    cbv.suspendValidation(false);
    // Test non-transactional localInvalidate, expecting afterInvalidate
    // non-distributed
    cbv.setTXId(txMgr.getTransactionId());
    cbv.setExpectedCount(appCallCount++);
    this.region.localInvalidate(key1, callBackArg);
    assertTrue("Non-TX LocalInvalidate Validation Assertion", cbv.passedValidation());
    // Account for cacheWriter not begin called
    vCw.localDestroyMakeup(1);
    cbv.suspendValidation(true);
    this.region.put(key1, value1);
    cbv.suspendValidation(false);
    // Test transactional localInvalidate, expecting afterInvalidate
    // non-distributed
    this.txMgr.begin();
    cbv.setTXId(txMgr.getTransactionId());
    cbv.setExpectedCount(appCallCount++);
    this.region.localInvalidate(key1, callBackArg);
    this.txMgr.commit();
    assertTrue("TX LocalInvalidate Validation Assertion", cbv.passedValidation());
    // Account for cacheWriter not begin called
    vCw.localDestroyMakeup(1);
    cbv.suspendValidation(true);
    this.region.localDestroy(key1);
    // Invalidate => afterInvalidate, distributed tests
    cbv.setNewValue(null, true);
    cbv.setOldValue(value1, false);
    cbv.setIsDistributed(true);
    cbv.setIsLoad(false);
    cbv.suspendValidation(true);
    this.region.create(key1, value1);
    cbv.suspendValidation(false);
    // Test non-transactional Invalidate, expecting afterInvalidate
    // distributed
    cbv.setTXId(txMgr.getTransactionId());
    cbv.setExpectedCount(appCallCount++);
    this.region.invalidate(key1, callBackArg);
    // Account for cacheWriter not begin called
    vCw.localDestroyMakeup(1);
    assertTrue("Non-TX Invalidate Validation Assertion", cbv.passedValidation());
    cbv.suspendValidation(true);
    this.region.put(key1, value1);
    cbv.suspendValidation(false);
    // Test transactional Invalidate, expecting afterInvalidate
    // distributed
    this.txMgr.begin();
    cbv.setTXId(txMgr.getTransactionId());
    cbv.setExpectedCount(appCallCount++);
    this.region.invalidate(key1, callBackArg);
    this.txMgr.commit();
    // Account for cacheWriter not begin called
    vCw.localDestroyMakeup(1);
    assertTrue("TX Invalidate Validation Assertion", cbv.passedValidation());
    cbv.suspendValidation(true);
    this.region.localDestroy(key1);
    cbv.suspendValidation(false);
    // Create load Event tests
    int loaderValCheck = 0;
    cbv.setNewValue(loaderValCheck++, false);
    cbv.setCallBackArg(null);
    cbv.setOldValue(null, false);
    cbv.setIsDistributed(true);
    cbv.setIsCreate(true);
    cbv.setIsUpdate(false);
    cbv.setIsLoad(true);
    // Test non-transactional load, expecting afterCreate distributed
    cbv.setTXId(txMgr.getTransactionId());
    cbv.setExpectedCount(appCallCount++);
    this.region.get(key1);
    assertTrue("Non-TX Invalidate Validation Assertion", cbv.passedValidation());
    vCl.validate();
    vCw.validate();
    cbv.suspendValidation(true);
    this.region.localDestroy(key1);
    cbv.suspendValidation(false);
    // Test transactional load, expecting afterCreate distributed
    vCl.reset();
    this.txMgr.begin();
    cbv.setTXId(txMgr.getTransactionId());
    cbv.setNewValue(loaderValCheck++, false);
    cbv.setExpectedCount(appCallCount++);
    this.region.get(key1);
    this.txMgr.rollback();
    assertTrue("TX Invalidate Validation Assertion", cbv.passedValidation());
    vCw.validate();
    vCl.validateNoEvents();
    vCl.setExpectedCount(vCl.getCallCount() + 1);
    this.txMgr.begin();
    cbv.setTXId(txMgr.getTransactionId());
    cbv.setNewValue(loaderValCheck++, false);
    cbv.setExpectedCount(appCallCount++);
    this.region.get(key1);
    vCw.validate();
    vCw.reset();
    this.txMgr.commit();
    vCw.validateNoEvents();
    assertTrue("TX Invalidate Validation Assertion", cbv.passedValidation());
    vCl.validate();
    cbv.suspendValidation(true);
    this.region.localDestroy(key1);
    cbv.suspendValidation(false);
    // Update load Event tests
    cbv.suspendValidation(true);
    this.region.create(key1, null);
    cbv.suspendValidation(false);
    assertTrue(this.region.containsKey(key1));
    assertTrue(!this.region.containsValueForKey(key1));
    cbv.setNewValue(loaderValCheck++, false);
    cbv.setOldValue(null, false);
    cbv.setIsDistributed(true);
    cbv.setCallBackArg(null);
    cbv.setIsCreate(false);
    cbv.setIsUpdate(true);
    cbv.setIsLoad(true);
    // Test non-transactional load, expecting afterUpdate distributed
    cbv.setTXId(txMgr.getTransactionId());
    cbv.setExpectedCount(appCallCount++);
    this.region.get(key1);
    assertTrue("Non-TX Invalidate Validation Assertion", cbv.passedValidation());
    vCw.validate();
    vCl.validate();
    cbv.suspendValidation(true);
    this.region.invalidate(key1);
    cbv.suspendValidation(false);
    assertTrue(this.region.containsKey(key1));
    assertTrue(!this.region.containsValueForKey(key1));
    // Test transactional load, expecting afterUpdate distributed
    this.txMgr.begin();
    cbv.setTXId(txMgr.getTransactionId());
    cbv.setExpectedCount(appCallCount++);
    cbv.setNewValue(loaderValCheck++, false);
    this.region.get(key1);
    vCw.validate();
    vCw.reset();
    this.txMgr.commit();
    vCw.validateNoEvents();
    vCl.validate();
    cbv.suspendValidation(true);
    this.region.invalidate(key1);
    cbv.suspendValidation(false);
    vCl.reset();
    this.txMgr.begin();
    cbv.setTXId(txMgr.getTransactionId());
    cbv.setExpectedCount(appCallCount++);
    cbv.setNewValue(loaderValCheck++, false);
    this.region.get(key1);
    this.txMgr.rollback();
    assertTrue("TX Invalidate Validation Assertion", cbv.passedValidation());
    vCw.validate();
    vCl.validateNoEvents();
}
Also used : RegionEvent(org.apache.geode.cache.RegionEvent) LoaderHelper(org.apache.geode.cache.LoaderHelper) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) EntryEvent(org.apache.geode.cache.EntryEvent) CacheLoader(org.apache.geode.cache.CacheLoader) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 25 with CacheLoader

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

the class LocalRegion method findObjectInSystem.

/**
   * 
   * Search for the value in a server (if one exists), then try a loader.
   * 
   * If we find a value, we put it in the cache.
   * 
   * @param preferCD return the CacheDeserializable, if that's what the value is.
   * @param requestingClient the client making the request, if any
   * @param clientEvent the client's event, if any. If not null, we set the version tag
   * @return the deserialized value
   */
protected Object findObjectInSystem(KeyInfo keyInfo, boolean isCreate, TXStateInterface tx, boolean generateCallbacks, Object localValue, boolean disableCopyOnRead, boolean preferCD, ClientProxyMembershipID requestingClient, EntryEventImpl clientEvent, boolean returnTombstones) throws CacheLoaderException, TimeoutException {
    final Object key = keyInfo.getKey();
    final Object aCallbackArgument = keyInfo.getCallbackArg();
    Object value = null;
    boolean fromServer = false;
    VersionTagHolder holder = null;
    /*
     * First lets try the server
     */
    ServerRegionProxy mySRP = getServerProxy();
    if (mySRP != null) {
        holder = new VersionTagHolder();
        value = mySRP.get(key, aCallbackArgument, holder);
        fromServer = value != null;
    }
    /*
     * If we didn't get anything from the server, try the loader
     */
    if (!fromServer || value == Token.TOMBSTONE) {
        // copy into local var to prevent race condition
        CacheLoader loader = basicGetLoader();
        if (loader != null) {
            final LoaderHelper loaderHelper = this.loaderHelperFactory.createLoaderHelper(key, aCallbackArgument, false, /* netSearchAllowed */
            true, /* netloadAllowed */
            null);
            CachePerfStats stats = getCachePerfStats();
            long statStart = stats.startLoad();
            try {
                value = loader.load(loaderHelper);
                fromServer = false;
            } finally {
                stats.endLoad(statStart);
            }
        }
    }
    // have concurrency checks enabled
    if (fromServer && value == Token.TOMBSTONE && !this.concurrencyChecksEnabled) {
        value = null;
    }
    /*
     * If we got a value back, let's put it in the cache.
     */
    RegionEntry re = null;
    if (value != null && !isMemoryThresholdReachedForLoad()) {
        long startPut = CachePerfStats.getStatTime();
        validateKey(key);
        Operation op;
        if (isCreate) {
            op = Operation.LOCAL_LOAD_CREATE;
        } else {
            op = Operation.LOCAL_LOAD_UPDATE;
        }
        @Released EntryEventImpl event = EntryEventImpl.create(this, op, key, value, aCallbackArgument, false, getMyId(), generateCallbacks);
        try {
            // already one there with the same version
            if (fromServer) {
                if (alreadyInvalid(key, event)) {
                    return null;
                }
                event.setFromServer(fromServer);
                event.setVersionTag(holder.getVersionTag());
                if (clientEvent != null) {
                    clientEvent.setVersionTag(holder.getVersionTag());
                }
            }
            // the value to the server
            if (!fromServer) {
                event.setNewEventId(this.cache.getDistributedSystem());
            }
            try {
                try {
                    re = basicPutEntry(event, 0L);
                    if (!fromServer && clientEvent != null) {
                        clientEvent.setVersionTag(event.getVersionTag());
                        clientEvent.isConcurrencyConflict(event.isConcurrencyConflict());
                    }
                    if (fromServer && event.getRawNewValue() == Token.TOMBSTONE) {
                        // tombstones are destroyed entries
                        return null;
                    }
                } catch (ConcurrentCacheModificationException ignore) {
                    // this means the value attempted to overwrite a newer modification and was rejected
                    if (logger.isDebugEnabled()) {
                        logger.debug("caught concurrent modification attempt when applying {}", event);
                    }
                    notifyBridgeClients(event);
                }
                if (!getDataView().isDeferredStats()) {
                    getCachePerfStats().endPut(startPut, event.isOriginRemote());
                }
            } catch (CacheWriterException cwe) {
                if (logger.isDebugEnabled()) {
                    logger.debug("findObjectInSystem: writer exception putting entry {}", event, cwe);
                }
            }
        } finally {
            event.release();
        }
    }
    if (isCreate) {
        recordMiss(re, key);
    }
    return value;
}
Also used : Released(org.apache.geode.internal.offheap.annotations.Released) Operation(org.apache.geode.cache.Operation) ConcurrentCacheModificationException(org.apache.geode.internal.cache.versions.ConcurrentCacheModificationException) LoaderHelper(org.apache.geode.cache.LoaderHelper) ServerRegionProxy(org.apache.geode.cache.client.internal.ServerRegionProxy) StoredObject(org.apache.geode.internal.offheap.StoredObject) CacheLoader(org.apache.geode.cache.CacheLoader) CacheWriterException(org.apache.geode.cache.CacheWriterException)

Aggregations

CacheLoader (org.apache.geode.cache.CacheLoader)40 LoaderHelper (org.apache.geode.cache.LoaderHelper)32 Region (org.apache.geode.cache.Region)24 Test (org.junit.Test)24 AttributesFactory (org.apache.geode.cache.AttributesFactory)21 CacheLoaderException (org.apache.geode.cache.CacheLoaderException)20 CacheException (org.apache.geode.cache.CacheException)18 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)17 VM (org.apache.geode.test.dunit.VM)16 Host (org.apache.geode.test.dunit.Host)13 CacheWriterException (org.apache.geode.cache.CacheWriterException)9 LocalRegion (org.apache.geode.internal.cache.LocalRegion)9 AttributesMutator (org.apache.geode.cache.AttributesMutator)8 RegionAttributes (org.apache.geode.cache.RegionAttributes)8 TimeoutException (org.apache.geode.cache.TimeoutException)7 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)7 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)7 HashSet (java.util.HashSet)6 Set (java.util.Set)6 Properties (java.util.Properties)5