Skip to content

symfonic.memory.backends.mongo_leases

mongo_leases

Private Mongo leases join receipt transactions through one tenant guard.

Takeover waits for that guard's commit; this is not public Mongo HMS admission.

MongoLeases

MongoLeases(domain: _ReceiptDomain)

Replica-set LeasePort sharing one owned receipt domain.

Source code in src/symfonic/memory/backends/mongo_leases.py
def __init__(self, domain: _ReceiptDomain) -> None:
    if type(domain) is not _ReceiptDomain:
        raise MongoSessionError("Mongo leases require an exact receipt domain")
    try:
        from pymongo.read_concern import ReadConcern
        from pymongo.write_concern import WriteConcern
    except ImportError as exc:
        raise ImportError("Mongo leases require: pip install symfonic-core[mongodb]") from exc

    self._domain = domain
    domain._cycle_lease_required = True
    self._client = domain._client
    self._collection = domain._client[domain._receipt_database][_COLLECTION].with_options(
        read_concern=ReadConcern("majority"),
        write_concern=WriteConcern("majority"),
    )

transaction_domain property

transaction_domain: _ReceiptDomain

The exact owned client/transaction shared with graph and receipt.

hold_for_update async

hold_for_update(lease: Lease) -> bool

Write the lease row inside the receipt transaction until commit.

Source code in src/symfonic/memory/backends/mongo_leases.py
async def hold_for_update(self, lease: Lease) -> bool:
    """Write the lease row inside the receipt transaction until commit."""
    scope = _identity(lease)
    domain = self._domain
    session = domain._session_for_call()
    if session is None or domain._guard_tenant != scope.tenant:
        raise MongoSessionError("Lease fence requires matching armed receipt transaction")
    if not lease_covers_operation(scope, domain._guard_scope):
        domain._receipt_mutation_failed = True
        raise MongoSessionError("Lease scope does not cover receipt operation")
    # Sample server time after preceding body work.
    now = await self._guard_time(scope.tenant, session)
    result = await _settle(
        self._collection.update_one(
            {"_id": lease.scope_path, "owner": lease.owner,
             "generation": lease.generation or "",
             "ttl_seconds": lease.ttl_seconds, "expires_at": {"$gt": now}},
            {"$inc": {"fence_revision": 1}},
            session=session,
        )
    )
    if result.matched_count != 1:
        domain._receipt_mutation_failed = True
        return False
    domain._cycle_fenced = True
    return True