curator-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Arie Zilberstein <azilberst...@salesforce.com>
Subject Distributed TimedSemaphore
Date Tue, 13 May 2014 20:11:29 GMT
Hi,

Can anyone advise how to implement a distributed timed semaphore? An
in-memory implementation of this object, TimedSemaphore, exists in Apache
Commons. Here is its description from the Javadoc:

---Start of description
A specialized semaphore implementation that provides a number of permits in
a given time frame.

This class is similar to the java.util.concurrent.Semaphore class provided
by the JDK in that it manages a configurable number of permits.

Using the acquire() method a permit can be requested by a thread.

However, there is an additional timing dimension: there is no release()
method for freeing a permit, but all permits are automatically released at
the end of a configurable time frame.

If a thread calls acquire() and the available permits are already exhausted
for this
time frame, the thread is blocked. When the time frame ends all permits
requested so far are restored, and blocking threads are waked up again, so
that they can try to acquire a new permit. This basically means that in the
specified time frame only the given number of operations is possible.

A use case for this class is to artificially limit the load produced by
a process. As an example consider an application that issues database
queries
on a production system in a background process to gather statistical
information. This background processing should not produce so much database
load that the functionality and the performance of the production system are
impacted. Here a TimedSemaphore could be installed to guarantee that
only a given number of database queries are issued per second.
---End of description

So, as a first approximation I thought about using the Curator shared
semaphore; I would call acquire() to acquire a permit, and I would
immediately fire up a thread that releases the permit after a specified
amount of time. A major problem with this approach is that, using the
Curator semaphore, killed processes / threads automatically release their
permit so that other clients can then acquire permits although they
shouldn't be allowed to do that (permits should only be released when they
are expired).

So what is the best way to implement a timed semaphore?

Thanks,
Arie

Mime
View raw message