How to use hazelCast distributed lock in alfresco repo side webscript/Action

cancel
Showing results for 
Search instead for 
Did you mean: 
svijay
Active Member

How to use hazelCast distributed lock in alfresco repo side webscript/Action

We are in requirement to synchronize the access to repository webscript/Action based on NodRef.

Currently application is running in clustered  mode. 

What is the best way to achieve locking mechanism in clustered environment in alfresco..?

In hazelcast I found below resource for distributed locking

2.7. Distributed Lock 

How can we use this kind of lock in Alfresco...?

Axel Faust‌ Can you pls provide your suggestion.?

3 Replies
afaust
Master

Re: How to use hazelCast distributed lock in alfresco repo side webscript/Action

You can achieve locking without using any Hazelcast APIs. Alfresco has a JobLockService which you can use to obtain, maintain and release locks for specific semantic operations using qualified names as identifiers (qualified name could be dynamically created using NodeRef identity, though that may pollute the alf_qname table, and would be undesirable in my view). Alternatively, you could also use the simpler LockService to lock the specific node, though that would also interfere with any other user operations on the node, not just your specific action.

As I have indicated in another thread, you should rely as little as possible on the underlying Hazelcast APIs, since they are a) rarely needed since Alfresco already provides some higher level concepts and b) limiting the compatibility of extensions to Enterprise Edition only. In ~9 years working on Alfresco (including 6 years with Enterprise-only customers with clustered systems up to 70.000 users), I have never had to rely on Hazelcast API in any of my business logic or encountered a situtation where this would be a viable alternative.

svijay
Active Member

Re: How to use hazelCast distributed lock in alfresco repo side webscript/Action

Hi Axel Faust

Thanks for the reply.

We just want to block the thread which is processing in same node.

From my understanding if we use JobLockService it will make entry in table(alf_lock_resource) which will be persisted for ever.(Pls correct me if I am wrong).

If we add lock based on the qname as nodeId the table will grow bigger day by day.

Below is the actual business scenario in which we want handle the concurrency

Multiple users will work on same node concurrently, business logic is written similar to below code kept inside webscript

   if(nodeRef.getProperty(customProperty)== null){

       String customPropertyValue=generateUniqueId();

         nodeRef.setProperty(customProperty,customPropertyValue);

   }

//Return  from webscript

return customPropertyValue

This webscript will be called from external system, where there is possibility two user can call this webscript on same node concurrently.

Due to concurrency some time both calls receive different result which is undesirable.

We want it to be same result irrespective of concurrent access.

Kindly provide your suggestion for above business case.

Thanks

afaust
Master

Re: How to use hazelCast distributed lock in alfresco repo side webscript/Action

The code pattern you have provided should work perfectly fine with the default transaction handling of Alfresco without any need of locking. When two threads / operations on different cluster servers execute the action at the same time, only one will get to commit its changes, the other will rollback due to a ConcurrencyFailureException (result of Alfresco optimistic locking). Then the thread that failed will retry the transaction, automatically detect that the property now has a value and not generate a different unique ID.