Mutual exclusion between 2 tasks of 2 different process intances


#1

I’m working with workflows that create an aws cluster then run calculations on it then terminate the cluster. The cluster creation mechanism works with a pool of available clusters (prepared in advanced). Either there is a cluster in the pool and the process will use it for the calculation (and of course flag it as no more available) or the pool is empty and the process will create a new cluster on the fly wait for it to be ready then run the calculations on it. I’m using java delegates to make call to aws api to manipulate the clusters.

My question is how can I ensure that multiple instances of my workflow wont call the same delegate at the same time ? For example let’s say the pool contains only 1 available cluster but 2 instances start at the same time.What I want is that one instance get the cluster and the other creates a new one. What I absolutely dont want is that both instances get the same cluster. Is there a way to deal with that at the bpmn level or at the java delegate level using some kind of mutex ?


#2

Hi @iprion,

There are probably many ways to solve this. The one that comes to mind for me:

  • Encapsulate the logic of cluster management in a separate service
  • Expose two methods: 1) Get cluster from pool 2) Return cluster to pool
  • The service makes sure that a cluster is never used more than once in parallel (e.g. by Java synchronization primitives)
  • In the Java delegate, just assume that the returned cluster can always be used
  • If you need some time to spin up a new cluster when the pool is exhausted, you can either block the executing thread that calls the JavaDelegate (watch out for things like transaction timeouts) or implement a time-based retry mechanism in both java or BPMN

Cheers,
thorben