ignite-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kevin <kevin.c...@leonardo.com>
Subject Re: CacheStore implementation using Hibernate
Date Tue, 22 Sep 2015 14:55:29 GMT
Hi Val,

I've produced a test to show this behavior. Code at the bottom.
Classes:
IgniteNode1 - just starts a node
IgniteNode2 - starts a node, configures and starts the cache, and runs the
test
TestValue - Cache value class, has a "myInt" int field
TestValueCacheStore - dummy CacheStore
TestInterceptor - CacheInterceptor class, increments TestValue's int value
by 1 on onBeforePut

The test:
Start IgniteNode1
Start IgniteNode2
Puts a new TestValue in the cache
Puts the same TestValue in the cache again (in reality, TestValue could have
more fields and their values were changed)


This shows that:
- interceptor is called twice for each node for one "put" operation,
resulting in "myInt" being increased twice
- read-through does not occur before calling the interceptor

Perhaps there is a bug, or I am not using the interceptor correctly.

What I would like to do is to check that the existing "myInt" and the new
"myInt" are the same, and only then, increment it, otherwise, I would like
to cancel the put (eg. throw an exception)

I've had more luck with using a CacheEntryProcessor instead (making sure not
to modify the arguments in the "process" method)

Thanks

IgniteNode1

> public class IgniteNode1 {
>     public static void main (String[] args) {
>         Ignition.start();
>     }
> }

IgniteNode2

> public class IgniteNode2 {
>     public static void test(IgniteCache
> &lt;
> String, TestValue
> &gt;
>  cache, String key) {
>         System.out.println("Creating new value");
>         TestValue testValue = new TestValue(0);
>         cache.put(key, testValue);
> 
>         System.out.println("1: " + testValue);
> 
>         System.out.println("Updating value");
>         cache.put(key, testValue);
> 
>         System.out.println("2: " + testValue);
>     }
> 
>     public static void main (String[] args) {
>         Ignite ignite = Ignition.start();
> 
>         CacheConfiguration
> &lt;
> String, TestValue
> &gt;
>  cacheConfiguration = new CacheConfiguration<>("mycache");
>         IgniteReflectionFactory
> <TestValueCacheStore>
>  factory = new IgniteReflectionFactory<>(TestValueCacheStore.class, true);
>         cacheConfiguration.setCacheStoreFactory(factory);
> 
>        
> cacheConfiguration.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
>         cacheConfiguration.setReadThrough(true);
>         cacheConfiguration.setWriteThrough(true);
>         cacheConfiguration.setBackups(1);
>         cacheConfiguration.setInterceptor(new TestInterceptor());
> 
>         IgniteCache
> &lt;
> String, TestValue
> &gt;
>  cache = ignite.getOrCreateCache(cacheConfiguration);
> 
>         IgniteCompute compute = ignite.compute();
>         compute.broadcast(() -> System.out.println("TEST 1"));
>         test(cache, "test1");
> 
>         compute.broadcast(() -> System.out.println("TEST 2"));
> //        interceptor old value is null unless this is uncommented
> //        System.out.println(cache.get("testLoad"));
>         test(cache, "testLoad");
>     }
> }

TestValue

> public class TestValue implements Serializable {
>     private int myInt;
> 
>     public TestValue(int myInt) {
>         this.myInt = myInt;
>     }
> 
>     public int getMyInt() {
>         return myInt;
>     }
> 
>     public void setMyInt(int myInt) {
>         this.myInt = myInt;
>     }
> 
>     public String toString() {
>         return super.toString() + "|myInt=" + myInt;
>     }
> }

TestValueCacheStore

> public class TestValueCacheStore extends CacheStoreAdapter
> &lt;
> String, TestValue
> &gt;
>  {
>     @Override
>     public TestValue load(String key) throws CacheLoaderException {
>         System.out.println("doing read through for key: " + key);
>         if (key.equals("testLoad")) {
>             return new TestValue(10);
>         } else {
>             return null;
>         }
>     }
> 
>     @Override
>     public void write(Cache.Entry<? extends String, ? extends TestValue>
> entry) throws CacheWriterException {
>         System.out.println("writing: " + entry);
>     }
> 
>     @Override
>     public void delete(Object key) throws CacheWriterException {
>     }
> }

TestInterceptor

> public class TestInterceptor extends CacheInterceptorAdapter
> &lt;
> String, TestValue
> &gt;
>  {
>     public TestValue onBeforePut(Cache.Entry
> &lt;
> String, TestValue
> &gt;
>  entry, TestValue newVal) {
>         newVal.setMyInt(newVal.getMyInt() + 1);
> 
>         System.out.println("interceptor");
>         System.out.println("\told value: " + entry.getValue());
>         System.out.println("\tnew value: " + newVal);
> 
>         return newVal;
>     }
> }





--
View this message in context: http://apache-ignite-users.70518.x6.nabble.com/CacheStore-implementation-using-Hibernate-tp1423p1461.html
Sent from the Apache Ignite Users mailing list archive at Nabble.com.

Mime
View raw message