Hazelcast Lock Operation on Map

586 views
Skip to first unread message

Manayam Srivalli

unread,
Jan 11, 2019, 6:40:21 AM1/11/19
to Hazelcast
I want to lock all operations like get,post ,update and delete  on Hazelcast map using hazelcast  lock operation for certain period  of time and I need to do  unlock the map to perform operation after completion of time period

I used sample code snippet
Imap<String,Object> map  = hazelcastInsatnce.getMap("testmap");
map.lock("testmap");
map.unlock("testmap")

The above code is not working I used both IMap and Lock both are not working
I tried to lock the key ,it is also failing.

is it possible to lock all the operation on map.if possible how to implement it in java

Please help me.

ky...@hazelcast.com

unread,
Jan 11, 2019, 3:37:18 PM1/11/19
to Hazelcast
The lock and unlock methods for an IMap instance are for locking specific entries (by key) only. Please see the javadocs for details: https://docs.hazelcast.org/docs/3.11.1/javadoc/

In your sample code, you're locking the entry with key "testmap" in the IMap named "testmap". Why do you need to lock the entire map? Perhaps can suggest a different way to accomplish what you're looking to do.

Guido Medina

unread,
Jan 11, 2019, 3:50:48 PM1/11/19
to haze...@googlegroups.com
Clearly you haven't tried to read the documentation, you are doing it wrong: https://docs.hazelcast.org/docs/3.11.1/manual/html-single/index.html#lock
Lock lock = hazelcastInstance.getLock( "myLock" );
lock.lock();
try {
    // do something here
} finally {
    lock.unlock();
}

--
You received this message because you are subscribed to the Google Groups "Hazelcast" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hazelcast+...@googlegroups.com.
To post to this group, send email to haze...@googlegroups.com.
Visit this group at https://groups.google.com/group/hazelcast.
To view this discussion on the web visit https://groups.google.com/d/msgid/hazelcast/8db44feb-e26d-4f18-93ec-6310e7c8bc23%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Guido Medina

unread,
Jan 11, 2019, 3:55:21 PM1/11/19
to haze...@googlegroups.com
Well, no, what you are trying to do is probably a pessimistic locking and the want you are doing it should simply work which is documented here: https://docs.hazelcast.org/docs/3.11.1/manual/html-single/index.html#pessimistic-looking

Guido Medina

unread,
Jan 11, 2019, 3:55:56 PM1/11/19
to haze...@googlegroups.com
I meant "the way you are doing it", not "the want..."

Srivalli

unread,
Jan 12, 2019, 8:22:32 PM1/12/19
to Hazelcast
My scenario is,I have two different clusters.when one cluster is crashed and restarted,Inorder to replicate the data from second cluster which is up and having data,Iam using sync operation through sync command.
During the sync process in middle I don't want other services to use those maps until sync operation completes.Because if other services try to access the data,the data may not available as sync process is in progress,that's the reason I want to lock the whole map,so that other services not to perform any operations on that map.
Is there any other options to lock the read and write access on particular map

oxy...@gmail.com

unread,
Jan 13, 2019, 7:17:52 AM1/13/19
to Hazelcast
I don't think that feature is practical for Hazelcast, or at least it is not something available at the moment, I think that you should use a count down latch per map per application and signal the latch when that map replication is over.

private final ConcurrentMap<String, CountDownLatch> latches = new ConcurrentHashMap<>();

public void replicateMap(String name) {
 
try {
   
// TODO: Replicate the map here
 
} finally {
   
// Whatever happens count down the latch so that it doesn't cause a deadlock in case of errors.
    latches
.computeIfAbsent(name, k -> new CountDownLatch(1)).countDown();
 
}
}

public IMap getMap(String name) {

 
try {
    latches
.computeIfAbsent(name, k -> new CountDownLatch(1)).await();
 
} catch (InterruptedException ignored) {
 
}
  return hazelcastInstance.getIMap(name);
}

That code will block when the application tries to get a particular map until it is fully replicated.
Hope that helps,

Guido.

Manayam Srivalli

unread,
Jan 14, 2019, 5:26:54 AM1/14/19
to haze...@googlegroups.com
I have one doubt .When one cluster will crash .I am doing sync operation using Synchronizing WAN Target cluster concept using rest call
 uri http://member_ip:port/hazelcast/rest/mancenter/wan/sync/allmaps.  I have 3 microservices which are using these hazelcast maps to validate the data.

Before  calling sync rest call service .I need to block all the maps,so that 3 microservices cannot access the maps.Once maps are blocked  l will hit the sync rest service ,once sync process is done for all the maps.then I need to unblock all the maps,to give availability of all the maps with the data to 3 services.

Microservice can hit the maps at any time for validation.whenever they try to hit the map ,I need to give some alert message or response,that indicates it  cannot access the data as sync process is in progress.

Is it possible ,with the above code,There I could see ,replication of data is map by map.But in this scenario, Iam not writing any code to replicate.Iam just using Hazelcast Sync rest call ,there I think  it is replicating the data parallely for all maps

--
You received this message because you are subscribed to the Google Groups "Hazelcast" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hazelcast+...@googlegroups.com.
To post to this group, send email to haze...@googlegroups.com.
Visit this group at https://groups.google.com/group/hazelcast.

For more options, visit https://groups.google.com/d/optout.


--

"valli"

oxy...@gmail.com

unread,
Jan 14, 2019, 6:33:46 AM1/14/19
to Hazelcast
Well, I wasn't aware of how the replication was happening, I thought that it was map by map which is why I suggested such method,
I guess you need a global latch and signal it when the whole replication is finished but that will simply lock your whole application until everything is over.

In that case just use one CountDownLatch for the whole application.

Guido.

Manayam Srivalli

unread,
Jan 16, 2019, 11:59:27 AM1/16/19
to haze...@googlegroups.com
Thank you so much for your suggestion.I need to discuss with other members to implement it 

Reply all
Reply to author
Forward
0 new messages