You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ignite.apache.org by xero <mp...@gmail.com> on 2018/07/14 03:48:28 UTC

Ignite Transaction and Cache Events

Hi,
Do you know if there is mechanism to subscribe for to transaction events
(pre or post commit)?

My scenario is the following:
I have 2 caches (A and B) that are modified in the same transaction. On the
other hand, I have a third C cache whose entries are calculated based on A
and B values.
I would like to update C once the transaction that modifies A and B commits.

I plan to collect the modifications of A and B caches by subscribing to the
events (which have the associated transaction id  in the cix field). I would
like to group all the events associated with a certain cix but, I can not
find a way to hook to the transaction's commit event in order to ensure that
all the cache events were triggered.
Then, if the tx modified 5 entries in A and 5 entries in B, I would like to
group them once I have collected all 10 events.

Any help would be greatly appreciated




--
Sent from: http://apache-ignite-users.70518.x6.nabble.com/

Re: Ignite Transaction and Cache Events

Posted by Prasad Bhalerao <pr...@gmail.com>.
I think you don't have to do anything extra to group all events for same
transaction. It doesn't not matter how many caches you are updating in
single transaction.
Let's assume that you are updating 10 caches in single transaction. When
you do commit, your all 10 cache store's writeAll or deleteAll methods will
be invoked. These writeAll and deleteAll methods will get all your cache
updates entries (cache events ) as method parameters.
Just put all these events on queue/topic.
As these methods are invoked as a result of commit operation, you can be
sure that you have collected all cache events.

All though each cache configuration has it's own CacheStoreSessionListener,
I think it should not be a problem. Ignite might be managing it internally.
Can you please do a small poc to check this?

Just write all cache events to some queue and throw some runtime exception
from one of you cachestore just to make sure that transaction is rolledback
and nothing is published on queue.
Similarly do successful commit and see if you can get all cache events in
your message listener/consumer.

Thanks,
Prasad

On Sun, Jul 15, 2018, 11:01 PM xero <mp...@gmail.com> wrote:

> Thanks for replying PRasad,
> I understand your approach but, I still don't get /when/ I should group all
> events for the same transaction. In my tests I get onSessionStart and
> onSessionEnd for each cache (both sharing the same cix). I would love to
> have a post commit hook because at that point I'm sure that I have
> collected
> all the events. (In this example I have A and B caches but, in reality
> could
> have multiple of them).
>
> Example implementing CacheStoreSessionListener to log events:
> em.getTransaction().begin();
> A a = new A();
> a.setData(10);
> em.persist(a);
>
> b b = new b();
> b.setIntData(20);
> em.persist(b);
>
> em.getTransaction().commit();
>
> Triggered events:
> OnSessionStart cacheName:a_entity
> xid:e1fceee9461-00000000-0888-5e7a-0000-000000000001
> OnSessionStart cacheName:b_entity
> xid:e1fceee9461-00000000-0888-5e7a-0000-000000000001
> OnSessionEnd cacheName:a_entity
> xid:e1fceee9461-00000000-0888-5e7a-0000-000000000001 commit:true
> OnSessionEnd cacheName:b_entity
> xid:e1fceee9461-00000000-0888-5e7a-0000-000000000001 commit:true
>
>
> Is there a way of getting only one onSessionEnd event? or getting an event
> associated with the transaction?
>
> Thanks!
>
>
>
> --
> Sent from: http://apache-ignite-users.70518.x6.nabble.com/
>

Re: Ignite Transaction and Cache Events

Posted by xero <mp...@gmail.com>.
Thanks for replying PRasad,
I understand your approach but, I still don't get /when/ I should group all
events for the same transaction. In my tests I get onSessionStart and
onSessionEnd for each cache (both sharing the same cix). I would love to
have a post commit hook because at that point I'm sure that I have collected
all the events. (In this example I have A and B caches but, in reality could
have multiple of them).

Example implementing CacheStoreSessionListener to log events:
em.getTransaction().begin();
A a = new A();
a.setData(10);
em.persist(a);

b b = new b();
b.setIntData(20);
em.persist(b);

em.getTransaction().commit();

Triggered events:
OnSessionStart cacheName:a_entity
xid:e1fceee9461-00000000-0888-5e7a-0000-000000000001
OnSessionStart cacheName:b_entity
xid:e1fceee9461-00000000-0888-5e7a-0000-000000000001
OnSessionEnd cacheName:a_entity
xid:e1fceee9461-00000000-0888-5e7a-0000-000000000001 commit:true
OnSessionEnd cacheName:b_entity
xid:e1fceee9461-00000000-0888-5e7a-0000-000000000001 commit:true


Is there a way of getting only one onSessionEnd event? or getting an event
associated with the transaction?

Thanks!



--
Sent from: http://apache-ignite-users.70518.x6.nabble.com/

Re: Ignite Transaction and Cache Events

Posted by Prasad Bhalerao <pr...@gmail.com>.
I think you can use write through approach to update your third Cache "C".

Implement CacheStore interface or extend CacheStoreAdapter class and
override write/writeAll and delete/deleteAll methods. These methods will be
invoked by ignite when you do commit on ignite transaction. If only one
cache entry is updated or deleted, write or delete method will be invoked.
If more than one cache entries are updated, writeAll or deleteAll method
will be invoked on commit. In write/writeAll and delete/deleteAll method
you can write a simple producer code which will write the updated entries
of cache A and Cache B to some message queue in a single transaction. So if
the transaction is rolled back, all the cache entries written to message
queue (e.g. Kafka topic or any jms queue ) will be rolled back.

You can plug any messaging mechanism's dat source using
CacheStoreSessionListener interface. Just implement this interface and
override onSessionStart and onSessionEnd methods. In onSeesionStart method
you need to attach messaging mechanism connection to session and in
onSessionEnd method you need to commit or rollback. Please
check CacheStoreSessionListener class and you will understand what I am
trying to say.

In short you can use writethrough mechanism to write your cache events to
messaging queue.
You can write a simple listener which will listen to this queue and read
the cache events and do the cache updates in cache C.

Thanks,
PRasad


On Sat, Jul 14, 2018 at 9:18 AM xero <mp...@gmail.com> wrote:

> Hi,
> Do you know if there is mechanism to subscribe for to transaction events
> (pre or post commit)?
>
> My scenario is the following:
> I have 2 caches (A and B) that are modified in the same transaction. On the
> other hand, I have a third C cache whose entries are calculated based on A
> and B values.
> I would like to update C once the transaction that modifies A and B
> commits.
>
> I plan to collect the modifications of A and B caches by subscribing to the
> events (which have the associated transaction id  in the cix field). I
> would
> like to group all the events associated with a certain cix but, I can not
> find a way to hook to the transaction's commit event in order to ensure
> that
> all the cache events were triggered.
> Then, if the tx modified 5 entries in A and 5 entries in B, I would like to
> group them once I have collected all 10 events.
>
> Any help would be greatly appreciated
>
>
>
>
> --
> Sent from: http://apache-ignite-users.70518.x6.nabble.com/
>