IgniteCache.lock behaviour on a key that doesn't exist in the cache (yet)

classic Classic list List threaded Threaded
3 messages Options
yfernando yfernando
Reply | Threaded
Open this post in threaded view
|

IgniteCache.lock behaviour on a key that doesn't exist in the cache (yet)

I'm trying to write a transaction-safe way of performing a lazy load of an object from the database if it doesnt exist. However as IgniteCache doesn't have the equivalent of HashMap.computeIfAbsent, I'm trying to use the IgniteCache.lock method to achieve this.

 

The question is whether IgniteCache.lock will lock the cache for that key even if <b>the key does not yet exist in the cache</b>.

 

Following is the example code,

 

public GridOrder getOrder(OrderKey key) {

        Lock orderKeyLock = cache.lock(key);

        try {

            orderKeyLock.lock();

            if (!cache.containsKey(key)) {

                GridOrder order = loadOrderFromDb(key);

                if ( order == null) {

                    throw new IllegalStateException("Key " + key + " not in Order Cache or in Database DB Name ");

                }

                cache.put(key,order);

            }

            return cache.get(key);

        } finally {

            orderKeyLock.unlock();

        }

    }

 

Alternatively, is there a better way to achieve this?

_________________________________________________________

This email, its contents, and any attachments transmitted with it are intended only for the addressee(s) and may be confidential and legally privileged. We do not waive any confidentiality by misdelivery. If you have received this email in error, please notify the sender immediately and delete it. You should not copy it, forward it or otherwise use the contents, attachments or information in any way. Any liability for viruses is excluded to the fullest extent permitted by law.

Tudor Capital Europe LLP (TCE) is authorised and regulated by The Financial Conduct Authority (the FCA). TCE is registered as a limited liability partnership in England and Wales No: OC340673 with its registered office at 10 New Burlington Street, London, W1S 3BE, United Kingdom

aealexsandrov aealexsandrov
Reply | Threaded
Open this post in threaded view
|

Re: IgniteCache.lock behaviour on a key that doesn't exist in the cache (yet)

Hi,

I tested your code and looks like it worked fine.

However, you also can try to use next method:

https://ignite.apache.org/releases/latest/javadoc/org/apache/ignite/IgniteCache.html#getAndPutIfAbsent-K-V-

In this case you can:

1)Check cache.containsKey(key)
2)If true then prepare the default value
3)Run getAndPutIfAbsent(key, defaultValue)

If another client was able to put the value during current prepare the default then you will get that value, otherwise default value will be returned.

BR,
Andrei

8/7/2019 12:08 PM, Yohan Fernando пишет:

I'm trying to write a transaction-safe way of performing a lazy load of an object from the database if it doesnt exist. However as IgniteCache doesn't have the equivalent of HashMap.computeIfAbsent, I'm trying to use the IgniteCache.lock method to achieve this.

 

The question is whether IgniteCache.lock will lock the cache for that key even if <b>the key does not yet exist in the cache</b>.

 

Following is the example code,

 

public GridOrder getOrder(OrderKey key) {

        Lock orderKeyLock = cache.lock(key);

        try {

            orderKeyLock.lock();

            if (!cache.containsKey(key)) {

                GridOrder order = loadOrderFromDb(key);

                if ( order == null) {

                    throw new IllegalStateException("Key " + key + " not in Order Cache or in Database DB Name ");

                }

                cache.put(key,order);

            }

            return cache.get(key);

        } finally {

            orderKeyLock.unlock();

        }

    }

 

Alternatively, is there a better way to achieve this?

_________________________________________________________

This email, its contents, and any attachments transmitted with it are intended only for the addressee(s) and may be confidential and legally privileged. We do not waive any confidentiality by misdelivery. If you have received this email in error, please notify the sender immediately and delete it. You should not copy it, forward it or otherwise use the contents, attachments or information in any way. Any liability for viruses is excluded to the fullest extent permitted by law.

Tudor Capital Europe LLP (TCE) is authorised and regulated by The Financial Conduct Authority (the FCA). TCE is registered as a limited liability partnership in England and Wales No: OC340673 with its registered office at 10 New Burlington Street, London, W1S 3BE, United Kingdom

yfernando yfernando
Reply | Threaded
Open this post in threaded view
|

RE: IgniteCache.lock behaviour on a key that doesn't exist in the cache (yet)

Yes I did think of that approach but wanted to avoid the risk of the expensive load from the DB being called more than once unnecessarily. I suppose that should be balanced the cost of the IgniteCache.lock() call.

 

Thanks for the reply Andei

 

From: Andrei Aleksandrov <[hidden email]>
Sent: 07 August 2019 13:02
To: [hidden email]
Subject: Re: IgniteCache.lock behaviour on a key that doesn't exist in the cache (yet)

 

Caution: This email originated from outside of Tudor.

 

Hi,

I tested your code and looks like it worked fine.

However, you also can try to use next method:

https://ignite.apache.org/releases/latest/javadoc/org/apache/ignite/IgniteCache.html#getAndPutIfAbsent-K-V-

In this case you can:

1)Check cache.containsKey(key)
2)If true then prepare the default value
3)Run getAndPutIfAbsent(key, defaultValue)

If another client was able to put the value during current prepare the default then you will get that value, otherwise default value will be returned.

BR,
Andrei

8/7/2019 12:08 PM, Yohan Fernando пишет:

I'm trying to write a transaction-safe way of performing a lazy load of an object from the database if it doesnt exist. However as IgniteCache doesn't have the equivalent of HashMap.computeIfAbsent, I'm trying to use the IgniteCache.lock method to achieve this.

 

The question is whether IgniteCache.lock will lock the cache for that key even if <b>the key does not yet exist in the cache</b>.

 

Following is the example code,

 

public GridOrder getOrder(OrderKey key) {

        Lock orderKeyLock = cache.lock(key);

        try {

            orderKeyLock.lock();

            if (!cache.containsKey(key)) {

                GridOrder order = loadOrderFromDb(key);

                if ( order == null) {

                    throw new IllegalStateException("Key " + key + " not in Order Cache or in Database DB Name ");

                }

                cache.put(key,order);

            }

            return cache.get(key);

        } finally {

            orderKeyLock.unlock();

        }

    }

 

Alternatively, is there a better way to achieve this?

_________________________________________________________

This email, its contents, and any attachments transmitted with it are intended only for the addressee(s) and may be confidential and legally privileged. We do not waive any confidentiality by misdelivery. If you have received this email in error, please notify the sender immediately and delete it. You should not copy it, forward it or otherwise use the contents, attachments or information in any way. Any liability for viruses is excluded to the fullest extent permitted by law.

Tudor Capital Europe LLP (TCE) is authorised and regulated by The Financial Conduct Authority (the FCA). TCE is registered as a limited liability partnership in England and Wales No: OC340673 with its registered office at 10 New Burlington Street, London, W1S 3BE, United Kingdom

_________________________________________________________

This email, its contents, and any attachments transmitted with it are intended only for the addressee(s) and may be confidential and legally privileged. We do not waive any confidentiality by misdelivery. If you have received this email in error, please notify the sender immediately and delete it. You should not copy it, forward it or otherwise use the contents, attachments or information in any way. Any liability for viruses is excluded to the fullest extent permitted by law.

Tudor Capital Europe LLP (TCE) is authorised and regulated by The Financial Conduct Authority (the FCA). TCE is registered as a limited liability partnership in England and Wales No: OC340673 with its registered office at 10 New Burlington Street, London, W1S 3BE, United Kingdom