Search code examples
javaapache-camelopenshift

Implement semaphore with apache camel


I am having an apache camel application and am planning to deploy it to openshift. It will be running on more than one pods and i've configured a REST endpoint which will be called by a kubernetes scheduler like this:

https://i.sstatic.net/w6Rzt.png

So the openshift load balancer will guarantee that the scheduler will start only 1 pod at a time. But what i want to guarantee is that a new job is not trigerred without the previous one has been completed. So i think the best approach for this would be to use a "semaphore" and write a lock file to the file system that blocks the other applications to run but I could not find a save way to do this in camel. Has somebody of you implemented such kind technique with Apache Camel? And If yes how do you guarantee that the file is deleted on a failover of the application?


Solution

  • If you want to have only 1 pod online at a time, why do you run multiple pods? If the active pod is going down, won't kubernetes remove it and start a new one anyway?

    For a semaphore you can use a shared file system, a database or whatever store. The application would need to delete the semaphore on startup. Because if only 1 pod must be active, there must be no semaphore when a pod starts. If the semaphore exists on startup, the previous processing was probably interrupted.

    In some cases it is also possible for the service to check if an active processing job is running. In this case you can simply ignore new requests as long as a processing is going on. Much simpler than managing a persistent semaphore.

    However, your use case that a scheduler starts a process and it must not run concurrently sounds like serverless to me. Have you checked out Camel K for your case?