reclaimableQuotaAdmissionGenerator function

SharedPoolAdmissionGenerator reclaimableQuotaAdmissionGenerator(
  1. List<int> quotas, {
  2. required int leaseTransfers,
})

Creates quota reservations that inactive pools release to active borrowers.

A pool refreshes its reservation lease on an accepted transfer. Every other accepted transfer ages that lease by one. Once leaseTransfers accepted transfers have occurred without a pool refresh, another pool may borrow its unoccupied quota. This deterministic, transfer-aged lease deliberately keeps policy state transfer-gated.

Implementation

SharedPoolAdmissionGenerator reclaimableQuotaAdmissionGenerator(
        List<int> quotas,
        {required int leaseTransfers}) =>
    (
        {required poolSelected,
        required poolOccupancies,
        required capacityAvailable,
        required accepted,
        required clk,
        required reset,
        required name}) {
      if (quotas.length != poolSelected.length ||
          quotas.length != poolOccupancies.length ||
          quotas.any((quota) => quota < 1)) {
        throw ArgumentError.value(quotas, 'quotas',
            'Must provide one positive quota for every pool.');
      }
      if (leaseTransfers < 1) {
        throw ArgumentError.value(
            leaseTransfers, 'leaseTransfers', 'Must be at least one.');
      }

      final leaseWidth = max(1, (leaseTransfers + 1).log2Ceil);
      final leaseRemaining = List.generate(
          quotas.length,
          (pool) =>
              Logic(name: '${name}_lease_remaining_$pool', width: leaseWidth));
      Logic admitted = Const(0);
      for (var pool = 0; pool < quotas.length; pool++) {
        Logic otherReservationsReleased = Const(1);
        for (var other = 0; other < quotas.length; other++) {
          if (other == pool) {
            continue;
          }
          final otherHasUnusedQuota = poolOccupancies[other]
              .lt(Const(quotas[other], width: poolOccupancies[other].width))
              .named('${name}_other_has_unused_quota_${pool}_$other');
          final otherQuotaReleased =
              (~otherHasUnusedQuota | leaseRemaining[other].eq(0))
                  .named('${name}_other_quota_released_${pool}_$other');
          otherReservationsReleased =
              (otherReservationsReleased & otherQuotaReleased)
                  .named('${name}_other_reservations_released_${pool}_$other');
        }
        final withinQuota = poolOccupancies[pool]
            .lt(Const(quotas[pool], width: poolOccupancies[pool].width))
            .named('${name}_within_quota_$pool');
        final poolMayBorrowQuota = (withinQuota | otherReservationsReleased)
            .named('${name}_pool_may_borrow_quota_$pool');
        admitted = (admitted | (poolSelected[pool] & poolMayBorrowQuota))
            .named('${name}_admitted_through_pool_$pool');
      }

      Sequential(clk, reset: reset, resetValues: {
        for (final remaining in leaseRemaining) remaining: 0
      }, [
        If(accepted, then: [
          for (var pool = 0; pool < quotas.length; pool++)
            If(poolSelected[pool], then: [
              leaseRemaining[pool] < Const(leaseTransfers, width: leaseWidth),
            ], orElse: [
              If(~leaseRemaining[pool].eq(0), then: [
                leaseRemaining[pool] <
                    leaseRemaining[pool] - Const(1, width: leaseWidth),
              ])
            ])
        ])
      ]);
      return admitted.named('${name}_reclaimable_quota_admitted');
    };