ehcache / ehcache3

Ehcache 3.x line
http://www.ehcache.org
Apache License 2.0
2.02k stars 581 forks source link

SerializingCopier for non Serializable Object #604

Closed JSAhuja closed 9 years ago

JSAhuja commented 9 years ago

For the below test, Description is NOT implementing Serializable:

 CacheConfiguration<Integer, Description> cacheConfiguration = CacheConfigurationBuilder.newCacheConfigurationBuilder()
        .withResourcePools(ResourcePoolsBuilder.newResourcePoolsBuilder().heap(10, EntryUnit.ENTRIES).build())
        .add(new DefaultCopierConfiguration<Description>((Class)SerializingCopier.class, CopierConfiguration.Type.VALUE))
        .buildConfig(Integer.class, Description.class);

    CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
        .withCache("cache", cacheConfiguration)
        .build(true);

    Cache<Integer, Description> cache = cacheManager.getCache("cache", Integer.class, Description.class);

Stack Trace:

java.lang.NullPointerException: null serializer
    at org.ehcache.internal.store.heap.holders.SerializedOnHeapValueHolder.<init>(SerializedOnHeapValueHolder.java:35)
    at org.ehcache.internal.store.heap.holders.SerializedOnHeapValueHolder.<init>(SerializedOnHeapValueHolder.java:46)
    at org.ehcache.internal.store.heap.OnHeapStore.makeSerializedValue(OnHeapStore.java:957)
    at org.ehcache.internal.store.heap.OnHeapStore.makeValue(OnHeapStore.java:950)
    at org.ehcache.internal.store.heap.OnHeapStore.newCreateValueHolder(OnHeapStore.java:935)
    at org.ehcache.internal.store.heap.OnHeapStore.access$500(OnHeapStore.java:89)
    at org.ehcache.internal.store.heap.OnHeapStore$14.apply(OnHeapStore.java:723)
    at org.ehcache.internal.store.heap.OnHeapStore$14.apply(OnHeapStore.java:699)
    at org.ehcache.internal.store.heap.OnHeapStore$MapWrapper$3.apply(OnHeapStore.java:1204)
    at org.ehcache.internal.store.heap.OnHeapStore$MapWrapper$3.apply(OnHeapStore.java:1201)
    at org.ehcache.internal.concurrent.ConcurrentHashMap.compute(ConcurrentHashMap.java:1840)
    at org.ehcache.internal.store.heap.OnHeapStore$MapWrapper.compute(OnHeapStore.java:1201)
    at org.ehcache.internal.store.heap.OnHeapStore.compute(OnHeapStore.java:699)
    at org.ehcache.internal.store.heap.OnHeapStore.compute(OnHeapStore.java:691)
    at org.ehcache.Ehcache.put(Ehcache.java:255)
    at com.terracotta.qa.ehcache.CustomCopierTest.basicCustomCopierTest(CustomCopierTest.java:100)

Something more meaningful message will help.

alexsnaps commented 9 years ago

I think I have fixed this as part of #605 (see PR #609) for CacheManager level Copier, if I read this code right, the same check as being done in "my" else if branch needs to happen in the branch above. Though it will already fail in the SerializingCopier constructor, as I added a check & a NPE... though, we probably want to throw an IllegalStateException with a message along side the one I added.

alexsnaps commented 9 years ago

Though, now reading the stuff above, I have no idea what's expected from this issue. Here's my take. This should throw, but throw much earlier. i.e. when everything gets wired together (which is what the comment above refers to).

alexsnaps commented 9 years ago

I should read it all: Something more meaningful message will help. Right, so my first comment totally applies.

JSAhuja commented 9 years ago

Full test code:

public class testClass {

    @Test
    public void test604()
    {
        CacheConfiguration<Integer, Description> cacheConfiguration = CacheConfigurationBuilder.newCacheConfigurationBuilder()
                .withResourcePools(ResourcePoolsBuilder.newResourcePoolsBuilder().heap(10, EntryUnit.ENTRIES).build())
                .add(new DefaultCopierConfiguration<Description>((Class) SerializingCopier.class, CopierConfiguration.Type.VALUE))
                .buildConfig(Integer.class, Description.class);

        CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
                .withCache("cache", cacheConfiguration)
                .build(true);

        Cache<Integer, Description> cache = cacheManager.getCache("cache", Integer.class, Description.class);
        Description desc = new Description(1234, "foo");

        cache.put(1, desc);
        assertThat(cache.get(1), equalTo(desc));
        cacheManager.close();
    }
}
 class Description{
    int id;
    String alias;

    Description(Description other) {
        this.id = other.id;
        this.alias = other.alias;
    }

    Description(int id, String alias) {
        this.id = id;
        this.alias = alias;
    }

    @Override
    public boolean equals(final Object other) {
        if(this == other) return true;
        if(other == null || this.getClass() != other.getClass()) return false;

        Description that = (Description)other;
        if(id != that.id) return false;
        if ((alias == null) ? (alias != null) : !alias.equals(that.alias)) return false;
        return true;
    }

    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + id;
        result = 31 * result + (alias == null ? 0 : alias.hashCode());
        return result;
    }
}

Full console log:

"C:\Program Files\Java\jdk1.7.0_51\bin\java" -ea -Didea.launcher.port=7538 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\bin" -Dfile.encoding=windows-1252 -classpath "C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\idea_rt.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\plugins\junit\lib\junit-rt.jar;C:\Program Files\Java\jdk1.7.0_51\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.7.0_51\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.7.0_51\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.7.0_51\jre\lib\jce.jar;C:\Program Files\Java\jdk1.7.0_51\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.7.0_51\jre\lib\jfxrt.jar;C:\Program Files\Java\jdk1.7.0_51\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.7.0_51\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.7.0_51\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.7.0_51\jre\lib\resources.jar;C:\Program Files\Java\jdk1.7.0_51\jre\lib\rt.jar;C:\Program Files\Java\jdk1.7.0_51\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.7.0_51\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.7.0_51\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.7.0_51\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.7.0_51\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.7.0_51\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.7.0_51\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.7.0_51\jre\lib\ext\zipfs.jar;C:\Users\jia\Documents\SourceTree\ehcache3\impl\build\classes\test;C:\Users\jia\Documents\SourceTree\ehcache3\impl\build\classes\main;C:\Users\jia\Documents\SourceTree\ehcache3\impl\build\resources\test;C:\Users\jia\Documents\SourceTree\ehcache3\impl\build\resources\main;C:\Users\jia\Documents\SourceTree\ehcache3\api\build\classes\test;C:\Users\jia\Documents\SourceTree\ehcache3\api\build\classes\main;C:\Users\jia\Documents\SourceTree\ehcache3\api\build\resources\test;C:\Users\jia\Documents\SourceTree\ehcache3\api\build\resources\main;C:\Users\jia\.gradle\caches\modules-2\files-2.1\com.google.code.findbugs\annotations\3.0.0\e20984c024b3baf63d06cf481c65c242dcd541de\annotations-3.0.0.jar;C:\Users\jia\.gradle\caches\modules-2\files-2.1\junit\junit\4.11\4e031bb61df09069aeb2bffb4019e7a5034a4ee0\junit-4.11.jar;C:\Users\jia\.gradle\caches\modules-2\files-2.1\org.hamcrest\hamcrest-library\1.3\4785a3c21320980282f9f33d0d1264a69040538f\hamcrest-library-1.3.jar;C:\Users\jia\.gradle\caches\modules-2\files-2.1\org.mockito\mockito-core\1.9.5\c3264abeea62c4d2f367e21484fbb40c7e256393\mockito-core-1.9.5.jar;C:\Users\jia\.gradle\caches\modules-2\files-2.1\org.hamcrest\hamcrest-core\1.3\42a25dc3219429f0e5d060061f71acb49bf010a0\hamcrest-core-1.3.jar;C:\Users\jia\.gradle\caches\modules-2\files-2.1\org.objenesis\objenesis\1.0\9b473564e792c2bdf1449da1f0b1b5bff9805704\objenesis-1.0.jar;C:\Users\jia\.gradle\caches\modules-2\files-2.1\org.slf4j\slf4j-simple\1.7.7\8095d0b9f7e0a9cd79a663c740e0f8fb31d0e2c8\slf4j-simple-1.7.7.jar;C:\Users\jia\.gradle\caches\modules-2\files-2.1\org.slf4j\slf4j-api\1.7.7\2b8019b6249bb05d81d3a3094e468753e2b21311\slf4j-api-1.7.7.jar;C:\Users\jia\Documents\SourceTree\ehcache3\core\build\classes\test;C:\Users\jia\Documents\SourceTree\ehcache3\core\build\classes\main;C:\Users\jia\Documents\SourceTree\ehcache3\core\build\resources\test;C:\Users\jia\Documents\SourceTree\ehcache3\core\build\resources\main;C:\Users\jia\Documents\SourceTree\ehcache3\spi-tester\build\classes\main;C:\Users\jia\Documents\SourceTree\ehcache3\spi-tester\build\resources\test;C:\Users\jia\Documents\SourceTree\ehcache3\spi-tester\build\resources\main;C:\Users\jia\.gradle\caches\modules-2\files-2.1\org.terracotta\statistics\1.1.0\3d4bff7253415d04041210e56dbe948d2010ce50\statistics-1.1.0.jar;C:\Users\jia\Documents\SourceTree\ehcache3\core-spi-test\build\classes\main;C:\Users\jia\Documents\SourceTree\ehcache3\core-spi-test\build\resources\test;C:\Users\jia\Documents\SourceTree\ehcache3\core-spi-test\build\resources\main;C:\Users\jia\.gradle\caches\modules-2\files-2.1\org.terracotta\offheap-store\2.1.0\3eb049c8e26d8216810561be022eb63fae96caf0\offheap-store-2.1.0.jar;C:\Users\jia\.gradle\caches\modules-2\files-2.1\org.ow2.asm\asm-all\5.0.4\e6244859997b3d4237a552669279780876228909\asm-all-5.0.4.jar" com.intellij.rt.execution.application.AppMain com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 org.ehcache.docs.testClass,test604
[main] INFO org.ehcache.spi.ServiceLocator - All Services successfully started.
[main] INFO org.ehcache.EhcacheManager - Cache 'cache' is getting created in EhcacheManager.
[main] INFO org.ehcache.spi.serialization.DefaultSerializationProvider - Serializer for <java.lang.Integer> : org.ehcache.internal.serialization.CompactJavaSerializer@1a4f27a3
[main] INFO org.ehcache.spi.copy.DefaultCopyProvider - Copier for <java.lang.Integer> : org.ehcache.internal.copy.IdentityCopier@168386ad
[main] INFO org.ehcache.spi.copy.DefaultCopyProvider - Copier for <org.ehcache.docs.Description> : org.ehcache.internal.copy.SerializingCopier@14fed367
[main] INFO class org.ehcache.Ehcache-cache - Initialize successful.
[main] INFO org.ehcache.EhcacheManager - Cache 'cache' created in EhcacheManager.
[main] INFO org.ehcache.EhcacheManager - Initialize successful.

java.lang.NullPointerException: null serializer
    at org.ehcache.internal.store.heap.holders.SerializedOnHeapValueHolder.<init>(SerializedOnHeapValueHolder.java:35)
    at org.ehcache.internal.store.heap.holders.SerializedOnHeapValueHolder.<init>(SerializedOnHeapValueHolder.java:46)
    at org.ehcache.internal.store.heap.OnHeapStore.makeSerializedValue(OnHeapStore.java:957)
    at org.ehcache.internal.store.heap.OnHeapStore.makeValue(OnHeapStore.java:950)
    at org.ehcache.internal.store.heap.OnHeapStore.newCreateValueHolder(OnHeapStore.java:935)
    at org.ehcache.internal.store.heap.OnHeapStore.access$500(OnHeapStore.java:89)
    at org.ehcache.internal.store.heap.OnHeapStore$14.apply(OnHeapStore.java:723)
    at org.ehcache.internal.store.heap.OnHeapStore$14.apply(OnHeapStore.java:699)
    at org.ehcache.internal.store.heap.OnHeapStore$MapWrapper$3.apply(OnHeapStore.java:1204)
    at org.ehcache.internal.store.heap.OnHeapStore$MapWrapper$3.apply(OnHeapStore.java:1201)
    at org.ehcache.internal.concurrent.ConcurrentHashMap.compute(ConcurrentHashMap.java:1840)
    at org.ehcache.internal.store.heap.OnHeapStore$MapWrapper.compute(OnHeapStore.java:1201)
    at org.ehcache.internal.store.heap.OnHeapStore.compute(OnHeapStore.java:699)
    at org.ehcache.internal.store.heap.OnHeapStore.compute(OnHeapStore.java:691)
    at org.ehcache.Ehcache.put(Ehcache.java:255)
    at org.ehcache.docs.testClass.test604(testClass.java:73)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)