Search code examples
apache-zookeeperdistributedapache-curator

Distributed semaphore on multiply services by curator


I'm new to zookeeper and curator.

I want to use zookeeper and curator to control our services. The situation is that our services are based on hardware devices, so each device as a provider could only run one task at one time. As we have many devices, I design to use curator's semaphore to count the whole number, but how to find the exactly devices is a problem. Because one job might need many devices at one time to run (devices are independent), and the number can not be defined, so each time we need to lock N devices and to send the task info

e.g.As I thought, we register device as EPHEMERAL_SEQUENTIAL in curator:

/root/services/
              /devices00000001
              /devices00000002
              ...

whether we use a lock on each device or use a flag in znode's data, it will cost us a lot of time to find the available devices. So I wander if there are some common solutions to these problems? Maybe I should not use zookeeper but other software?


Solution

  • Use an InterProcessMutex for each device - I assume each device has some unique ID. Something like:

    String path = ZKPaths.makePath("/some/base/path", deviceId);
    InterProcessMutex lock = new InterProcessMutex(curator, path);
    if ( lock.acquire(max, unit) ) {
        try {
            DO WORK HERE
        } finally {
            lock.release();
        }
    }