Search in sources :

Example 1 with PdxSerializationException

use of org.apache.geode.pdx.PdxSerializationException in project geode by apache.

the class AbstractRegionEntry method prepareValueForCache.

@Override
@Retained(ABSTRACT_REGION_ENTRY_PREPARE_VALUE_FOR_CACHE)
public Object prepareValueForCache(RegionEntryContext r, @Retained(ABSTRACT_REGION_ENTRY_PREPARE_VALUE_FOR_CACHE) Object val, EntryEventImpl event, boolean isEntryUpdate) {
    if (r != null && r.getOffHeap() && okToStoreOffHeap(val, this)) {
        if (val instanceof StoredObject) {
            // Check to see if val has the same compression settings as this region.
            // The recursive calls in this section are safe because
            // we only do it after copy the off-heap value to the heap.
            // This is needed to fix bug 52057.
            StoredObject soVal = (StoredObject) val;
            assert !soVal.isCompressed();
            if (r.getCompressor() != null) {
                // val is uncompressed and we need a compressed value.
                // So copy the off-heap value to the heap in a form that can be compressed.
                byte[] valAsBytes = soVal.getValueAsHeapByteArray();
                Object heapValue;
                if (soVal.isSerialized()) {
                    heapValue = CachedDeserializableFactory.create(valAsBytes);
                } else {
                    heapValue = valAsBytes;
                }
                return prepareValueForCache(r, heapValue, event, isEntryUpdate);
            }
            if (soVal.hasRefCount()) {
                // if the reused guy has a refcount then need to inc it
                if (!soVal.retain()) {
                    throw new IllegalStateException("Could not use an off heap value because it was freed");
                }
            }
        // else it is has no refCount so just return it as prepared.
        } else {
            byte[] data;
            boolean isSerialized = !(val instanceof byte[]);
            if (isSerialized) {
                if (event != null && event.getCachedSerializedNewValue() != null) {
                    data = event.getCachedSerializedNewValue();
                } else if (val instanceof CachedDeserializable) {
                    data = ((CachedDeserializable) val).getSerializedValue();
                } else if (val instanceof PdxInstance) {
                    try {
                        data = ((ConvertableToBytes) val).toBytes();
                    } catch (IOException e) {
                        throw new PdxSerializationException("Could not convert " + val + " to bytes", e);
                    }
                } else {
                    data = EntryEventImpl.serialize(val);
                }
            } else {
                data = (byte[]) val;
            }
            byte[] compressedData = compressBytes(r, data);
            // TODO: array comparison is broken
            boolean isCompressed = compressedData != data;
            ReferenceCountHelper.setReferenceCountOwner(this);
            // fix for bug 47875
            MemoryAllocator ma = MemoryAllocatorImpl.getAllocator();
            val = ma.allocateAndInitialize(compressedData, isSerialized, isCompressed, data);
            ReferenceCountHelper.setReferenceCountOwner(null);
        }
        return val;
    }
    @Unretained Object nv = val;
    if (nv instanceof StoredObject) {
        // This off heap value is being put into a on heap region.
        byte[] data = ((StoredObject) nv).getSerializedValue();
        nv = CachedDeserializableFactory.create(data);
    }
    if (nv instanceof PdxInstanceImpl) {
        // So get the serialized bytes and use a CachedDeserializable.
        try {
            byte[] data = ((ConvertableToBytes) nv).toBytes();
            byte[] compressedData = compressBytes(r, data);
            // TODO: array comparison is broken
            if (data == compressedData) {
                nv = CachedDeserializableFactory.create(data);
            } else {
                nv = compressedData;
            }
        } catch (IOException e) {
            throw new PdxSerializationException("Could not convert " + nv + " to bytes", e);
        }
    } else {
        nv = compress(r, nv, event);
    }
    return nv;
}
Also used : MemoryAllocator(org.apache.geode.internal.offheap.MemoryAllocator) IOException(java.io.IOException) ConvertableToBytes(org.apache.geode.pdx.internal.ConvertableToBytes) PdxSerializationException(org.apache.geode.pdx.PdxSerializationException) StoredObject(org.apache.geode.internal.offheap.StoredObject) PdxInstance(org.apache.geode.pdx.PdxInstance) PdxInstanceImpl(org.apache.geode.pdx.internal.PdxInstanceImpl) StoredObject(org.apache.geode.internal.offheap.StoredObject) Unretained(org.apache.geode.internal.offheap.annotations.Unretained) Retained(org.apache.geode.internal.offheap.annotations.Retained)

Example 2 with PdxSerializationException

use of org.apache.geode.pdx.PdxSerializationException in project geode by apache.

the class PdxWriterImpl method updateMetaData.

private void updateMetaData(String fieldName, FieldType type, boolean isIdentityField) {
    beforeFieldWrite();
    if (definingNewPdxType()) {
        PdxField ft = new PdxField(fieldName, this.fieldId, this.vlfCount, type, isIdentityField);
        this.newType.addField(ft);
    } else if (doExtraValidation()) {
        PdxField ft = this.existingType.getPdxField(fieldName);
        if (ft == null) {
            throw new PdxSerializationException("Did not expect field " + fieldName + " to be serialized since it was not the first time this class was serialized.");
        }
        if (this.fieldId != ft.getFieldIndex()) {
            throw new PdxSerializationException("Detected that the order in which the fields are serialized changed since the first time this class was serialized.");
        }
        if (!ft.getFieldType().equals(type)) {
            throw new PdxSerializationException("Expected field " + fieldName + " to be of type " + ft.getFieldType() + " not of type " + type);
        }
    }
}
Also used : PdxSerializationException(org.apache.geode.pdx.PdxSerializationException)

Example 3 with PdxSerializationException

use of org.apache.geode.pdx.PdxSerializationException in project geode by apache.

the class PdxInstanceImpl method getObject.

@Override
public Object getObject() {
    if (getPdxType().getNoDomainClass()) {
        // Following code added to convert Json/PdxInstance into the Java object.
        if (this.getClassName().equals("__GEMFIRE_JSON")) {
            // introspect the JSON, does the @type meta-data exist.
            String className = extractTypeMetaData();
            if (StringUtils.isNotBlank(className)) {
                try {
                    String JSON = JSONFormatter.toJSON(this);
                    ObjectMapper objMapper = USE_STATIC_MAPPER ? mapper : createObjectMapper();
                    Object classInstance = objMapper.readValue(JSON, ClassPathLoader.getLatest().forName(className));
                    return classInstance;
                } catch (Exception e) {
                    throw new PdxSerializationException("Could not deserialize as java class type could not resolved", e);
                }
            }
        }
        return this;
    }
    boolean wouldReadSerialized = PdxInstanceImpl.getPdxReadSerialized();
    if (!wouldReadSerialized) {
        return getUnmodifiableReader().basicGetObject();
    } else {
        PdxInstanceImpl.setPdxReadSerialized(false);
        try {
            return getUnmodifiableReader().basicGetObject();
        } finally {
            PdxInstanceImpl.setPdxReadSerialized(true);
        }
    }
}
Also used : PdxSerializationException(org.apache.geode.pdx.PdxSerializationException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) PdxSerializationException(org.apache.geode.pdx.PdxSerializationException) InternalGemFireException(org.apache.geode.InternalGemFireException) IOException(java.io.IOException)

Example 4 with PdxSerializationException

use of org.apache.geode.pdx.PdxSerializationException in project geode by apache.

the class DistributedMulticastRegionDUnitTest method testMulticastWithRegionOpsException.

@Test
public void testMulticastWithRegionOpsException() {
    Host host = Host.getHost(0);
    final VM vm0 = host.getVM(0);
    final VM vm1 = host.getVM(1);
    try {
        final String name = "mcastRegion";
        locatorPort = startLocator();
        vm0.invoke("setSysProps", () -> setSysProps());
        vm1.invoke("setSysProps", () -> setSysProps());
        // 1. start locator with mcast port
        vm0.invoke("createRegion", () -> {
            createRegion(name, getRegionAttributes());
            return "";
        });
        vm1.invoke("createRegion", () -> {
            createRegion(name, getRegionAttributes());
            return "";
        });
        vm0.invoke("do Put() with exception test", () -> {
            final Region region = getRootRegion().getSubregion(name);
            boolean gotReplyException = false;
            for (int i = 0; i < 1; i++) {
                try {
                    region.put(i, new TestObjectThrowsException());
                } catch (PdxSerializationException e) {
                    gotReplyException = true;
                } catch (Exception e) {
                    region.getCache().getLogger().info("Got exception of type " + e.getClass().toString());
                }
            }
            assertTrue("We should have got ReplyException ", gotReplyException);
        });
        vm0.invoke("validateMulticastOpsAfterRegionOps", () -> validateMulticastOpsAfterRegionOps());
        vm1.invoke("validateMulticastOpsAfterRegionOps", () -> validateMulticastOpsAfterRegionOps());
        closeLocator();
    } finally {
        SerializableRunnable unsetSysProp = new CacheSerializableRunnable("Create Region") {

            public void run2() throws CacheException {
                CachedDeserializableFactory.STORE_ALL_VALUE_FORMS = false;
            }
        };
        vm0.invoke(unsetSysProp);
        vm1.invoke(unsetSysProp);
    }
}
Also used : VM(org.apache.geode.test.dunit.VM) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) Region(org.apache.geode.cache.Region) Host(org.apache.geode.test.dunit.Host) PdxSerializationException(org.apache.geode.pdx.PdxSerializationException) PdxSerializationException(org.apache.geode.pdx.PdxSerializationException) IOException(java.io.IOException) CacheException(org.apache.geode.cache.CacheException) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 5 with PdxSerializationException

use of org.apache.geode.pdx.PdxSerializationException in project geode by apache.

the class AbstractRegionEntry method checkPdxEquals.

/**
   * This method fixes bug 43643
   */
private static boolean checkPdxEquals(PdxInstance pdx, Object obj) {
    if (!(obj instanceof PdxInstance)) {
        // if we are not readSerialized.
        if (obj instanceof CachedDeserializable) {
            CachedDeserializable cdObj = (CachedDeserializable) obj;
            if (!cdObj.isSerialized()) {
                // obj is actually a byte[] which will never be equal to a PdxInstance
                return false;
            }
            Object cdVal = cdObj.getValue();
            if (cdVal instanceof byte[]) {
                byte[] cdValBytes = (byte[]) cdVal;
                PdxInstance pi = InternalDataSerializer.readPdxInstance(cdValBytes, GemFireCacheImpl.getForPdx("Could not check value equality"));
                if (pi != null) {
                    return pi.equals(pdx);
                } else {
                    // since obj is serialized as something other than pdx it must not equal our pdx
                    return false;
                }
            } else {
                // remove the cd wrapper so that obj is the actual value we want to compare.
                obj = cdVal;
            }
        }
        if (obj != null && obj.getClass().getName().equals(pdx.getClassName())) {
            InternalCache internalCache = GemFireCacheImpl.getForPdx("Could not access Pdx registry");
            if (internalCache != null) {
                PdxSerializer pdxSerializer;
                if (obj instanceof PdxSerializable) {
                    pdxSerializer = null;
                } else {
                    pdxSerializer = internalCache.getPdxSerializer();
                }
                if (pdxSerializer != null || obj instanceof PdxSerializable) {
                    // try to convert obj to a PdxInstance
                    HeapDataOutputStream hdos = new HeapDataOutputStream(Version.CURRENT);
                    try {
                        if (InternalDataSerializer.autoSerialized(obj, hdos) || InternalDataSerializer.writePdx(hdos, internalCache, obj, pdxSerializer)) {
                            PdxInstance pi = InternalDataSerializer.readPdxInstance(hdos.toByteArray(), internalCache);
                            if (pi != null) {
                                obj = pi;
                            }
                        }
                    } catch (IOException | PdxSerializationException ignore) {
                    // we are not able to convert it so just fall through
                    }
                }
            }
        }
    }
    return basicEquals(obj, pdx);
}
Also used : PdxSerializer(org.apache.geode.pdx.PdxSerializer) PdxInstance(org.apache.geode.pdx.PdxInstance) HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) StoredObject(org.apache.geode.internal.offheap.StoredObject) IOException(java.io.IOException) PdxSerializationException(org.apache.geode.pdx.PdxSerializationException) PdxSerializable(org.apache.geode.pdx.PdxSerializable)

Aggregations

PdxSerializationException (org.apache.geode.pdx.PdxSerializationException)9 IOException (java.io.IOException)6 PdxInstance (org.apache.geode.pdx.PdxInstance)3 InternalGemFireException (org.apache.geode.InternalGemFireException)2 StoredObject (org.apache.geode.internal.offheap.StoredObject)2 PdxSerializable (org.apache.geode.pdx.PdxSerializable)2 PdxSerializer (org.apache.geode.pdx.PdxSerializer)2 PdxInstanceImpl (org.apache.geode.pdx.internal.PdxInstanceImpl)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 List (java.util.List)1 CacheException (org.apache.geode.cache.CacheException)1 Region (org.apache.geode.cache.Region)1 NameResolutionException (org.apache.geode.cache.query.NameResolutionException)1 QueryInvocationTargetException (org.apache.geode.cache.query.QueryInvocationTargetException)1 HeapDataOutputStream (org.apache.geode.internal.HeapDataOutputStream)1 InternalCache (org.apache.geode.internal.cache.InternalCache)1 MemoryAllocator (org.apache.geode.internal.offheap.MemoryAllocator)1 Retained (org.apache.geode.internal.offheap.annotations.Retained)1