Search in sources :

Example 6 with StoredObject

use of org.apache.geode.internal.offheap.StoredObject in project geode by apache.

the class GatewaySenderEventImpl method getDeserializedValue.

/**
   * Return this event's deserialized value
   * 
   * @return this event's deserialized value
   */
public Object getDeserializedValue() {
    if (this.valueIsObject == 0x00) {
        Object result = this.value;
        if (result == null) {
            @Unretained(OffHeapIdentifier.GATEWAY_SENDER_EVENT_IMPL_VALUE) Object so = this.valueObj;
            if (this.valueObjReleased) {
                throw new IllegalStateException("Value is no longer available. getDeserializedValue must be called before processEvents returns.");
            }
            if (so instanceof StoredObject) {
                return ((StoredObject) so).getValueAsDeserializedHeapObject();
            } else {
                throw new IllegalStateException("expected valueObj field to be an instance of StoredObject but it was " + so);
            }
        }
        return result;
    } else {
        Object vo = this.valueObj;
        if (vo != null) {
            if (vo instanceof StoredObject) {
                @Unretained(OffHeapIdentifier.GATEWAY_SENDER_EVENT_IMPL_VALUE) StoredObject so = (StoredObject) vo;
                return so.getValueAsDeserializedHeapObject();
            } else {
                // it is already deserialized
                return vo;
            }
        } else {
            if (this.value != null) {
                Object result = EntryEventImpl.deserialize(this.value);
                this.valueObj = result;
                return result;
            } else if (this.substituteValue != null) {
                // If the substitute value is set, return it.
                return this.substituteValue;
            } else {
                if (this.valueObjReleased) {
                    throw new IllegalStateException("Value is no longer available. getDeserializedValue must be called before processEvents returns.");
                }
                // both value and valueObj are null but we did not free it.
                return null;
            }
        }
    }
}
Also used : StoredObject(org.apache.geode.internal.offheap.StoredObject) StoredObject(org.apache.geode.internal.offheap.StoredObject) Unretained(org.apache.geode.internal.offheap.annotations.Unretained)

Example 7 with StoredObject

use of org.apache.geode.internal.offheap.StoredObject in project geode by apache.

the class GatewaySenderEventImpl method getSerializedValueSize.

public int getSerializedValueSize() {
    int localSerializedValueSize = this.serializedValueSize;
    if (localSerializedValueSize != DEFAULT_SERIALIZED_VALUE_SIZE) {
        return localSerializedValueSize;
    }
    @Unretained(OffHeapIdentifier.GATEWAY_SENDER_EVENT_IMPL_VALUE) Object vo = this.valueObj;
    if (vo instanceof StoredObject) {
        localSerializedValueSize = ((StoredObject) vo).getSizeInBytes();
    } else {
        if (this.substituteValue != null) {
            localSerializedValueSize = sizeOf(this.substituteValue);
        } else {
            localSerializedValueSize = CachedDeserializableFactory.calcMemSize(getSerializedValue());
        }
    }
    this.serializedValueSize = localSerializedValueSize;
    return localSerializedValueSize;
}
Also used : StoredObject(org.apache.geode.internal.offheap.StoredObject) StoredObject(org.apache.geode.internal.offheap.StoredObject) Unretained(org.apache.geode.internal.offheap.annotations.Unretained)

Example 8 with StoredObject

use of org.apache.geode.internal.offheap.StoredObject in project geode by apache.

the class LocalRegion method nonTXContainsValueForKey.

protected boolean nonTXContainsValueForKey(KeyInfo keyInfo) {
    checkReadiness();
    checkForNoAccess();
    if (this.diskRegion != null) {
        this.diskRegion.setClearCountReference();
    }
    try {
        RegionEntry entry = this.entries.getEntry(keyInfo.getKey());
        boolean result = entry != null;
        if (result) {
            ReferenceCountHelper.skipRefCountTracking();
            // no need to decompress since we only want to know if we have an existing value
            Object val = entry.getTransformedValue();
            if (val instanceof StoredObject) {
                OffHeapHelper.release(val);
                ReferenceCountHelper.unskipRefCountTracking();
                return true;
            }
            ReferenceCountHelper.unskipRefCountTracking();
            // No need to to check CachedDeserializable because of Bruce's fix in r30960 for bug 42162.
            // See bug 42732.
            // this works because INVALID and LOCAL_INVALID will never be faulted out of mem
            // If val is NOT_AVAILABLE that means we have a valid value on disk.
            result = !Token.isInvalidOrRemoved(val);
        }
        return result;
    } finally {
        if (this.diskRegion != null) {
            this.diskRegion.removeClearCountReference();
        }
    }
}
Also used : StoredObject(org.apache.geode.internal.offheap.StoredObject) StoredObject(org.apache.geode.internal.offheap.StoredObject)

Example 9 with StoredObject

use of org.apache.geode.internal.offheap.StoredObject in project geode by apache.

the class BlobHelper method deserializeOffHeapBlob.

/**
   * A blob is a serialized Object. This method returns the deserialized object. If a PdxInstance is
   * returned then it will refer to Chunk's off-heap memory with an unretained reference.
   */
@Unretained
public static Object deserializeOffHeapBlob(StoredObject blob) throws IOException, ClassNotFoundException {
    Object result;
    final long start = startDeserialization();
    // For both top level and nested pdxs we just want a reference to this off-heap blob.
    // No copies.
    // For non-pdx we want a stream that will read directly from the chunk.
    PdxInputStream is = new PdxInputStream(blob);
    result = DataSerializer.readObject(is);
    endDeserialization(start, blob.getDataSize());
    return result;
}
Also used : PdxInputStream(org.apache.geode.pdx.internal.PdxInputStream) StoredObject(org.apache.geode.internal.offheap.StoredObject) Unretained(org.apache.geode.internal.offheap.annotations.Unretained)

Example 10 with StoredObject

use of org.apache.geode.internal.offheap.StoredObject in project geode by apache.

the class MultiVMRegionTestCase method testRemoteCacheWriter.

/**
   * Tests that a remote {@link CacheWriter} is invoked and that <code>CacheWriter</code> arguments
   * and {@link CacheWriterException}s are propagated appropriately.
   */
@Test
public void testRemoteCacheWriter() throws Exception {
    assertTrue(getRegionAttributes().getScope().isDistributed());
    final String name = this.getUniqueName();
    final Object key = "KEY";
    final Object oldValue = "OLD_VALUE";
    final Object newValue = "NEW_VALUE";
    final Object arg = "ARG";
    final Object exception = "EXCEPTION";
    final Object key2 = "KEY2";
    final Object value2 = "VALUE2";
    SerializableRunnable create = new CacheSerializableRunnable("Create Region") {

        @Override
        public void run2() throws CacheException {
            Region region = createRegion(name);
            // Put key2 in the region before any callbacks are
            // registered, so it can be destroyed later
            region.put(key2, value2);
            assertEquals(1, region.size());
            if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
                GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
                MemoryAllocatorImpl ma = (MemoryAllocatorImpl) gfc.getOffHeapStore();
                LocalRegion reRegion;
                reRegion = (LocalRegion) region;
                RegionEntry re = reRegion.getRegionEntry(key2);
                StoredObject so = (StoredObject) re._getValue();
                assertEquals(1, so.getRefCount());
                assertEquals(1, ma.getStats().getObjects());
            }
        }
    };
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    vm0.invoke(create);
    vm1.invoke(create);
    //////// Create
    vm1.invoke(new CacheSerializableRunnable("Set Writer") {

        @Override
        public void run2() throws CacheException {
            final Region region = getRootRegion().getSubregion(name);
            writer = new TestCacheWriter() {

                @Override
                public void beforeCreate2(EntryEvent event) throws CacheWriterException {
                    if (exception.equals(event.getCallbackArgument())) {
                        String s = "Test Exception";
                        throw new CacheWriterException(s);
                    }
                    assertEquals(region, event.getRegion());
                    assertTrue(event.getOperation().isCreate());
                    assertTrue(event.getOperation().isDistributed());
                    assertFalse(event.getOperation().isExpiration());
                    assertTrue(event.isOriginRemote());
                    assertEquals(key, event.getKey());
                    assertEquals(null, event.getOldValue());
                    assertEquals(oldValue, event.getNewValue());
                    assertFalse(event.getOperation().isLoad());
                    assertFalse(event.getOperation().isLocalLoad());
                    assertFalse(event.getOperation().isNetLoad());
                    assertFalse(event.getOperation().isNetSearch());
                }
            };
            region.getAttributesMutator().setCacheWriter(writer);
            flushIfNecessary(region);
        }
    });
    vm0.invoke(new CacheSerializableRunnable("Create with Exception") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            try {
                region.put(key, oldValue, exception);
                fail("Should have thrown a CacheWriterException");
            } catch (CacheWriterException ex) {
                assertNull(region.getEntry(key));
                assertEquals(1, region.size());
                if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
                    GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
                    MemoryAllocatorImpl ma = (MemoryAllocatorImpl) gfc.getOffHeapStore();
                    assertEquals(1, ma.getStats().getObjects());
                }
            }
        }
    });
    vm1.invoke(new SerializableRunnable("Verify callback") {

        @Override
        public void run() {
            assertTrue(writer.wasInvoked());
        }
    });
    vm0.invoke(new CacheSerializableRunnable("Create with Argument") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            region.put(key, oldValue, arg);
            assertEquals(2, region.size());
            if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
                GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
                MemoryAllocatorImpl ma = (MemoryAllocatorImpl) gfc.getOffHeapStore();
                assertEquals(2, ma.getStats().getObjects());
                LocalRegion reRegion;
                reRegion = (LocalRegion) region;
                StoredObject so = (StoredObject) reRegion.getRegionEntry(key)._getValue();
                assertEquals(1, so.getRefCount());
            }
        }
    });
    vm1.invoke(new SerializableRunnable("Verify callback") {

        @Override
        public void run() {
            assertTrue(writer.wasInvoked());
        }
    });
    //////// Update
    vm1.invoke(new CacheSerializableRunnable("Set Writer") {

        @Override
        public void run2() throws CacheException {
            final Region region = getRootRegion().getSubregion(name);
            writer = new TestCacheWriter() {

                @Override
                public void beforeUpdate2(EntryEvent event) throws CacheWriterException {
                    Object argument = event.getCallbackArgument();
                    if (exception.equals(argument)) {
                        String s = "Test Exception";
                        throw new CacheWriterException(s);
                    }
                    assertEquals(arg, argument);
                    assertEquals(region, event.getRegion());
                    assertTrue(event.getOperation().isUpdate());
                    assertTrue(event.getOperation().isDistributed());
                    assertFalse(event.getOperation().isExpiration());
                    assertTrue(event.isOriginRemote());
                    assertEquals(key, event.getKey());
                    assertEquals(oldValue, event.getOldValue());
                    assertEquals(newValue, event.getNewValue());
                    assertFalse(event.getOperation().isLoad());
                    assertFalse(event.getOperation().isLocalLoad());
                    assertFalse(event.getOperation().isNetLoad());
                    assertFalse(event.getOperation().isNetSearch());
                }
            };
            region.getAttributesMutator().setCacheWriter(writer);
        }
    });
    vm0.invoke(new CacheSerializableRunnable("Update with Exception") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            try {
                region.put(key, newValue, exception);
                fail("Should have thrown a CacheWriterException");
            } catch (CacheWriterException ex) {
                Region.Entry entry = region.getEntry(key);
                assertEquals(oldValue, entry.getValue());
                assertEquals(2, region.size());
                if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
                    GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
                    MemoryAllocatorImpl ma = (MemoryAllocatorImpl) gfc.getOffHeapStore();
                    assertEquals(2, ma.getStats().getObjects());
                    LocalRegion reRegion;
                    reRegion = (LocalRegion) region;
                    StoredObject so = (StoredObject) reRegion.getRegionEntry(key)._getValue();
                    assertEquals(1, so.getRefCount());
                }
            }
        }
    });
    vm1.invoke(new SerializableRunnable("Verify callback") {

        @Override
        public void run() {
            assertTrue(writer.wasInvoked());
        }
    });
    vm0.invoke(new CacheSerializableRunnable("Update with Argument") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            region.put(key, newValue, arg);
            assertEquals(2, region.size());
            if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
                GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
                MemoryAllocatorImpl ma = (MemoryAllocatorImpl) gfc.getOffHeapStore();
                assertEquals(2, ma.getStats().getObjects());
            }
        }
    });
    vm1.invoke(new SerializableRunnable("Verify callback") {

        @Override
        public void run() {
            assertTrue(writer.wasInvoked());
        }
    });
    //////// Destroy
    vm1.invoke(new CacheSerializableRunnable("Set Writer") {

        @Override
        public void run2() throws CacheException {
            final Region region = getRootRegion().getSubregion(name);
            writer = new TestCacheWriter() {

                @Override
                public void beforeDestroy2(EntryEvent event) throws CacheWriterException {
                    Object argument = event.getCallbackArgument();
                    if (exception.equals(argument)) {
                        String s = "Test Exception";
                        throw new CacheWriterException(s);
                    }
                    assertEquals(arg, argument);
                    assertEquals(region, event.getRegion());
                    assertTrue(event.getOperation().isDestroy());
                    assertTrue(event.getOperation().isDistributed());
                    assertFalse(event.getOperation().isExpiration());
                    assertTrue(event.isOriginRemote());
                    assertEquals(key, event.getKey());
                    assertEquals(newValue, event.getOldValue());
                    assertNull(event.getNewValue());
                    assertFalse(event.getOperation().isLoad());
                    assertFalse(event.getOperation().isLocalLoad());
                    assertFalse(event.getOperation().isNetLoad());
                    assertFalse(event.getOperation().isNetSearch());
                }
            };
            region.getAttributesMutator().setCacheWriter(writer);
        }
    });
    vm0.invoke(new CacheSerializableRunnable("Destroy with Exception") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            try {
                region.destroy(key, exception);
                fail("Should have thrown a CacheWriterException");
            } catch (CacheWriterException ex) {
                assertNotNull(region.getEntry(key));
                assertEquals(2, region.size());
                if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
                    GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
                    MemoryAllocatorImpl ma = (MemoryAllocatorImpl) gfc.getOffHeapStore();
                    assertEquals(2, ma.getStats().getObjects());
                }
            }
        }
    });
    vm1.invoke(new SerializableRunnable("Verify callback") {

        @Override
        public void run() {
            assertTrue(writer.wasInvoked());
        }
    });
    vm0.invoke(new CacheSerializableRunnable("Destroy with Argument") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            region.destroy(key, arg);
            assertEquals(1, region.size());
            if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
                GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
                MemoryAllocatorImpl ma = (MemoryAllocatorImpl) gfc.getOffHeapStore();
                assertEquals(1, ma.getStats().getObjects());
            }
        }
    });
    vm1.invoke(new SerializableRunnable("Verify callback") {

        @Override
        public void run() {
            assertTrue(writer.wasInvoked());
        }
    });
    //////// Region Destroy
    vm1.invoke(new CacheSerializableRunnable("Set Writer") {

        @Override
        public void run2() throws CacheException {
            final Region region = getRootRegion().getSubregion(name);
            writer = new TestCacheWriter() {

                @Override
                public void beforeRegionDestroy2(RegionEvent event) throws CacheWriterException {
                    Object argument = event.getCallbackArgument();
                    if (exception.equals(argument)) {
                        String s = "Test Exception";
                        throw new CacheWriterException(s);
                    }
                    assertEquals(arg, argument);
                    assertEquals(region, event.getRegion());
                    assertTrue(event.getOperation().isRegionDestroy());
                    assertTrue(event.getOperation().isDistributed());
                    assertFalse(event.getOperation().isExpiration());
                    assertTrue(event.isOriginRemote());
                }
            };
            region.getAttributesMutator().setCacheWriter(writer);
        }
    });
    vm0.invoke(new CacheSerializableRunnable("Destroy with Exception") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            try {
                region.destroyRegion(exception);
                fail("Should have thrown a CacheWriterException");
            } catch (CacheWriterException ex) {
                if (region.isDestroyed()) {
                    fail("should not have an exception if region is destroyed", ex);
                }
                assertEquals(1, region.size());
                if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
                    GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
                    MemoryAllocatorImpl ma = (MemoryAllocatorImpl) gfc.getOffHeapStore();
                    assertEquals(1, ma.getStats().getObjects());
                }
            }
        }
    });
    vm1.invoke(new SerializableRunnable("Verify callback") {

        @Override
        public void run() {
            assertTrue(writer.wasInvoked());
        }
    });
    vm0.invoke(new CacheSerializableRunnable("Destroy with Argument") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            assertEquals(1, region.size());
            if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
                GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
                MemoryAllocatorImpl ma = (MemoryAllocatorImpl) gfc.getOffHeapStore();
                assertEquals(1, ma.getStats().getObjects());
            }
            region.destroyRegion(arg);
            if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
                GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
                final MemoryAllocatorImpl ma = (MemoryAllocatorImpl) gfc.getOffHeapStore();
                WaitCriterion waitForStatChange = new WaitCriterion() {

                    @Override
                    public boolean done() {
                        return ma.getStats().getObjects() == 0;
                    }

                    @Override
                    public String description() {
                        return "never saw off-heap object count go to zero. Last value was " + ma.getStats().getObjects();
                    }
                };
                Wait.waitForCriterion(waitForStatChange, 3000, 10, true);
            }
        }
    });
    vm1.invoke(new SerializableRunnable("Verify callback") {

        @Override
        public void run() {
            assertTrue(writer.wasInvoked());
        }
    });
}
Also used : CacheException(org.apache.geode.cache.CacheException) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) MemoryAllocatorImpl(org.apache.geode.internal.offheap.MemoryAllocatorImpl) Host(org.apache.geode.test.dunit.Host) LocalRegion(org.apache.geode.internal.cache.LocalRegion) RegionEvent(org.apache.geode.cache.RegionEvent) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) StoredObject(org.apache.geode.internal.offheap.StoredObject) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) VM(org.apache.geode.test.dunit.VM) EntryEvent(org.apache.geode.cache.EntryEvent) LocalRegion(org.apache.geode.internal.cache.LocalRegion) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) GemFireCacheImpl(org.apache.geode.internal.cache.GemFireCacheImpl) RegionEntry(org.apache.geode.internal.cache.RegionEntry) StoredObject(org.apache.geode.internal.offheap.StoredObject) CacheWriterException(org.apache.geode.cache.CacheWriterException) Test(org.junit.Test) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest)

Aggregations

StoredObject (org.apache.geode.internal.offheap.StoredObject)34 Test (org.junit.Test)11 UnitTest (org.apache.geode.test.junit.categories.UnitTest)10 Retained (org.apache.geode.internal.offheap.annotations.Retained)7 Unretained (org.apache.geode.internal.offheap.annotations.Unretained)7 Released (org.apache.geode.internal.offheap.annotations.Released)6 CachedDeserializable (org.apache.geode.internal.cache.CachedDeserializable)5 NewValueImporter (org.apache.geode.internal.cache.EntryEventImpl.NewValueImporter)4 OldValueImporter (org.apache.geode.internal.cache.EntryEventImpl.OldValueImporter)4 IOException (java.io.IOException)3 PdxInstance (org.apache.geode.pdx.PdxInstance)2 InterruptedIOException (java.io.InterruptedIOException)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 CacheException (org.apache.geode.cache.CacheException)1 CacheWriterException (org.apache.geode.cache.CacheWriterException)1 DiskAccessException (org.apache.geode.cache.DiskAccessException)1 EntryEvent (org.apache.geode.cache.EntryEvent)1 Region (org.apache.geode.cache.Region)1