# Observe how tokens are consumed fairly across tenants.

init
----
[regular] 0B tokens available
[elastic] 0B tokens available

# For two tenants t1 and t2, try to admit two requests of 1B each at
# incrementing log positions. We specify create-times in log-position order for
# work within a given range, similar to what we do at the StoreWorkQueue level.
admit tenant=t1 pri=normal-pri create-time=1.001us size=1B range=r1 origin=n1 log-position=4/20
----
[regular] try-get=1B available=0B => insufficient tokens

admit tenant=t1 pri=normal-pri create-time=1.002us size=1B range=r1 origin=n1 log-position=4/21
----

admit tenant=t2 pri=normal-pri create-time=1.001us size=1B range=r2 origin=n1 log-position=5/20
----

admit tenant=t2 pri=normal-pri create-time=1.002us size=1B range=r2 origin=n1 log-position=5/21
----

# Observe all waiting requests.
print
----
physical-stats: work-count=4 written-bytes=4B ingested-bytes=0B
[regular work queue]: len(tenant-heap)=2 top-tenant=t1
 tenant=t1 weight=1 fifo-threshold=low-pri used=0B
  [0: pri=normal-pri create-time=1.001µs size=1B range=r1 origin=n1 log-position=4/20]
  [1: pri=normal-pri create-time=1.002µs size=1B range=r1 origin=n1 log-position=4/21]
 tenant=t2 weight=1 fifo-threshold=low-pri used=0B
  [0: pri=normal-pri create-time=1.001µs size=1B range=r2 origin=n1 log-position=5/20]
  [1: pri=normal-pri create-time=1.002µs size=1B range=r2 origin=n1 log-position=5/21]
[elastic work queue]: len(tenant-heap)=0

# Produce 2B worth of regular tokens.
granter class=regular adjust-tokens=+2B
----
[regular] 2B tokens available
[elastic] 0B tokens available

# Grant admission requests. Since we have 2B worth of tokens, and 4 waiting
# requests wanting 1B each from 2 tenants, we're able to 1 request from each
# tenant.
grant class=regular
----
admitted [tenant=t1 pri=normal-pri create-time=1.001µs size=1B range=r1 origin=n1 log-position=4/20]
admitted [tenant=t2 pri=normal-pri create-time=1.001µs size=1B range=r2 origin=n1 log-position=5/20]

# Pretty print granter state to show no more available tokens. We've consumed
# the 2B above.
granter adjust-tokens=+0B
----
[regular] 0B tokens available
[elastic] 0B tokens available

# Observe that each tenant still has one waiting request.
print
----
physical-stats: work-count=4 written-bytes=4B ingested-bytes=0B
[regular work queue]: len(tenant-heap)=2 top-tenant=t1
 tenant=t1 weight=1 fifo-threshold=low-pri used=1B
  [0: pri=normal-pri create-time=1.002µs size=1B range=r1 origin=n1 log-position=4/21]
 tenant=t2 weight=1 fifo-threshold=low-pri used=1B
  [0: pri=normal-pri create-time=1.002µs size=1B range=r2 origin=n1 log-position=5/21]
[elastic work queue]: len(tenant-heap)=0

# vim:ft=sh
