MongoLeaseStore

class MongoLeaseStore(client: MongoClient, databaseName: String, clock: Clock = Clock.systemUTC(), dispatcher: CoroutineDispatcher = VirtualThreads.dispatcher, ddl: Ddl = Ddl.AUTO, operationTimeout: Duration = MongoTimeouts.OPERATION, wowVersion: String? = WowVersion.CURRENT) : LeaseStore

The LeaseStore on MongoDB: one document per (group, partition) in wow_lease and one per (group, node), with the node's internal address and the lease groups it serves, in wow_lease_member (one per service and node, its key field holding the service), each keyed by a unique index.

Every operation is one single-document write. An acquisition is a conditional upsert (see conditionalUpsert) whose filter admits only an expired lease or the acquirer's own, and whose pipeline update keeps the epoch when the acquirer still holds the lease live and adds one otherwise. MongoDB re-evaluates the filter after a write conflict, so concurrent acquisitions have exactly one winner. A release keeps the document with expiry 0, so the next acquisition continues its epoch.

Times come from clock, so they are the calling node's: keep the TTL well above the nodes' clock skew. Calls run on dispatcher, virtual threads by default.

An acquisition, a renewal and a heartbeat take at most the TTL they are given, never more than operationTimeout (LeaseStore's time bound): each of their round trips carries what is left of it as the driver's client-side timeout, which, when it passes, closes the connection and fails the call as transient; the server may still apply the write. Under ddl: none the first acquisition and the first heartbeat of a process first make sure of their collection's key (MongoUniqueKeys), within the same bound: the listing, and a wait on another call's, take what is left of it.

A heartbeat also deletes the member records of its key long expired (LeaseStore.heartbeat, ExpiredMembers): one deleteMany on the member key's prefix, at most once per TTL.

Constructors

Link copied to clipboard
constructor(client: MongoClient, databaseName: String, clock: Clock = Clock.systemUTC(), dispatcher: CoroutineDispatcher = VirtualThreads.dispatcher, ddl: Ddl = Ddl.AUTO, operationTimeout: Duration = MongoTimeouts.OPERATION, wowVersion: String? = WowVersion.CURRENT)

Functions

Link copied to clipboard
open suspend override fun acquire(group: String, partition: Int, owner: String, ttl: Duration): Lease?
Link copied to clipboard

Creates the key indexes when missing.

Link copied to clipboard
open suspend override fun heartbeat(service: String, node: String, address: String, ttl: Duration, groups: Set<String>): Member
Link copied to clipboard
open suspend override fun leave(service: String, node: String)
Link copied to clipboard
open suspend override fun members(service: String): List<Member>
Link copied to clipboard
open suspend override fun owners(group: String): List<Lease>
Link copied to clipboard
open suspend override fun release(group: String, partition: Int, owner: String): Boolean
Link copied to clipboard
open suspend override fun renew(group: String, partitions: Collection<Int>, owner: String, ttl: Duration): List<Lease>

Two round trips for every partition: the live leases owner holds are extended to one expiry, then the leases at that expiry are read back. A node renews its leases one round at a time, so no other call of owner sets the same expiry in between.