Preparing your design workspace

Loading the problem and your saved attempt…

Design and Implement a Thread-Safe Lease Manager

Grant time-limited leases so that at most one client holds each shared resource, with renewal, holder-only release, and waiting acquire under many threads, then implement the core.

Design and implement a thread-safe lease manager in Java. A lease manager controls access to shared resources: a client asks for a lease on a resource, and at any moment at most one client may hold a lease on a given resource. Many clients call the lease manager from different threads at the same time.

For this exercise, assume the following.

- Resources and clients are identified by string IDs. Any resource ID may be requested; resources do not have to be registered first. - Every lease is granted for a duration in milliseconds that the client chooses. Time comes from a clock the lease manager is given.

A lease granted at time t for D milliseconds is held until t + D. At exactly t + D it has expired and the resource is free again, even if the holder never released it.

- A client can either try to get a lease and get an answer right away, or ask for a lease and wait up to a given number of milliseconds for the resource to become free. - The holder can renew its lease before it expires. A renewal made at time t for D milliseconds makes the lease last until t + D.

An expired lease cannot be renewed; the client has to ask for a new one. - Only the holder of a lease can release it. A release or renewal that uses an expired lease, or a lease the caller does not hold, fails with a clear answer and never ends anyone else's lease.

- Leases are not re-entrant: a client that asks again for a resource it already holds is refused and should renew instead. - One process runs the lease manager. Keeping leases across restarts and running on several servers are out of scope.

Design the objects, their interfaces, and the main flows, then implement the core.