init
----

set-try-get-return-value v=true
----

admit id=1 tenant=53 priority=0 create-time-millis=1 bypass=false
----
tryGet: returning true
id 1: admit succeeded

print
----
closed epoch: 0 tenantHeap len: 0
 tenant-id: 53 used: 1, w: 1, fifo: -128

# tryGet will return false, so work will queue up.
set-try-get-return-value v=false
----

admit id=2 tenant=53 priority=0 create-time-millis=3 bypass=false
----
tryGet: returning false

print
----
closed epoch: 0 tenantHeap len: 1 top tenant: 53
 tenant-id: 53 used: 1, w: 1, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 3, epoch: 0, qt: 100]

admit id=3 tenant=53 priority=0 create-time-millis=2 bypass=false
----

# Tenant 53 has two waiting requests. The one that arrived second is earlier
# in the heap because of a smaller create-time-millis.
print
----
closed epoch: 0 tenantHeap len: 1 top tenant: 53
 tenant-id: 53 used: 1, w: 1, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 2, epoch: 0, qt: 100] [1: pri: normal-pri, ct: 3, epoch: 0, qt: 100]

# Request from tenant 71.
admit id=4 tenant=71 priority=-128 create-time-millis=4 bypass=false
----

# Another request from tenant 71. This one has higher priority so will be
# earlier in the heap, even though it has higher create-time-millis.
admit id=5 tenant=71 priority=0 create-time-millis=5 bypass=false
----

# Tenant 71 is the top of the heap since has not used any cpu time.
print
----
closed epoch: 0 tenantHeap len: 2 top tenant: 71
 tenant-id: 53 used: 1, w: 1, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 2, epoch: 0, qt: 100] [1: pri: normal-pri, ct: 3, epoch: 0, qt: 100]
 tenant-id: 71 used: 0, w: 1, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 5, epoch: 0, qt: 100] [1: pri: low-pri, ct: 4, epoch: 0, qt: 100]

granted chain-id=5
----
continueGrantChain 5
id 5: admit succeeded
granted: returned 1

# Both tenants have used 1 cpu nano time. The tie is broken arbitrarily in
# favor of tenant 71.
print
----
closed epoch: 0 tenantHeap len: 2 top tenant: 53
 tenant-id: 53 used: 1, w: 1, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 2, epoch: 0, qt: 100] [1: pri: normal-pri, ct: 3, epoch: 0, qt: 100]
 tenant-id: 71 used: 1, w: 1, fifo: -128 waiting work heap: [0: pri: low-pri, ct: 4, epoch: 0, qt: 100]

# Cancel a request from tenant 53.
cancel-work id=3
----
id 3: admit failed

print
----
closed epoch: 0 tenantHeap len: 2 top tenant: 53
 tenant-id: 53 used: 1, w: 1, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 3, epoch: 0, qt: 100]
 tenant-id: 71 used: 1, w: 1, fifo: -128 waiting work heap: [0: pri: low-pri, ct: 4, epoch: 0, qt: 100]

# The work admitted for tenant 53 is done and consumed no cpu-time
work-done id=1 cpu-time=0
----
returnGrant 1

# Tenant 53 has used no cpu, so it becomes the top of the heap.
print
----
closed epoch: 0 tenantHeap len: 2 top tenant: 53
 tenant-id: 53 used: 0, w: 1, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 3, epoch: 0, qt: 100]
 tenant-id: 71 used: 1, w: 1, fifo: -128 waiting work heap: [0: pri: low-pri, ct: 4, epoch: 0, qt: 100]

# Requests from a system and application tenant bypass admission control,
# but are reflected in the WorkQueue state.
admit id=6 tenant=1 priority=0 create-time-millis=6 bypass=true
----
tookWithoutPermission 1
id 6: admit succeeded

admit id=9 tenant=71 priority=0 create-time-millis=7 bypass=true
----
tookWithoutPermission 1
id 9: admit succeeded

print
----
closed epoch: 0 tenantHeap len: 2 top tenant: 53
 tenant-id: 1 used: 1, w: 1, fifo: -128
 tenant-id: 53 used: 0, w: 1, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 3, epoch: 0, qt: 100]
 tenant-id: 71 used: 2, w: 1, fifo: -128 waiting work heap: [0: pri: low-pri, ct: 4, epoch: 0, qt: 100]

# The bypass work is done and consumed 10 and 0 cpu nanos respectively.
work-done id=6 cpu-time=10
----
returnGrant 1

work-done id=9 cpu-time=0
----
returnGrant 1

print
----
closed epoch: 0 tenantHeap len: 2 top tenant: 53
 tenant-id: 1 used: 10, w: 1, fifo: -128
 tenant-id: 53 used: 0, w: 1, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 3, epoch: 0, qt: 100]
 tenant-id: 71 used: 1, w: 1, fifo: -128 waiting work heap: [0: pri: low-pri, ct: 4, epoch: 0, qt: 100]

# Another request from tenant 53, which is behind the existing request in the heap.
admit id=7 tenant=53 priority=0 create-time-millis=5 bypass=false
----

print
----
closed epoch: 0 tenantHeap len: 2 top tenant: 53
 tenant-id: 1 used: 10, w: 1, fifo: -128
 tenant-id: 53 used: 0, w: 1, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 3, epoch: 0, qt: 100] [1: pri: normal-pri, ct: 5, epoch: 0, qt: 100]
 tenant-id: 71 used: 1, w: 1, fifo: -128 waiting work heap: [0: pri: low-pri, ct: 4, epoch: 0, qt: 100]

granted chain-id=7
----
continueGrantChain 7
id 2: admit succeeded
granted: returned 1

# Both tenants have used 1 cpu nano time. The tie is broken arbitrarily in
# favor of tenant 53.
print
----
closed epoch: 0 tenantHeap len: 2 top tenant: 53
 tenant-id: 1 used: 10, w: 1, fifo: -128
 tenant-id: 53 used: 1, w: 1, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 5, epoch: 0, qt: 100]
 tenant-id: 71 used: 1, w: 1, fifo: -128 waiting work heap: [0: pri: low-pri, ct: 4, epoch: 0, qt: 100]

# The work admitted for tenant 53 is done and has consumed 20 cpu nanos, so
# tenant 71 moves to the top of the heap.
work-done id=2 cpu-time=20
----
returnGrant 1

print
----
closed epoch: 0 tenantHeap len: 2 top tenant: 71
 tenant-id: 1 used: 10, w: 1, fifo: -128
 tenant-id: 53 used: 20, w: 1, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 5, epoch: 0, qt: 100]
 tenant-id: 71 used: 1, w: 1, fifo: -128 waiting work heap: [0: pri: low-pri, ct: 4, epoch: 0, qt: 100]

gc-tenants-and-reset-used
----
closed epoch: 0 tenantHeap len: 2 top tenant: 71
 tenant-id: 1 used: 0, w: 1, fifo: -128
 tenant-id: 53 used: 0, w: 1, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 5, epoch: 0, qt: 100]
 tenant-id: 71 used: 0, w: 1, fifo: -128 waiting work heap: [0: pri: low-pri, ct: 4, epoch: 0, qt: 100]

granted chain-id=9
----
continueGrantChain 9
id 4: admit succeeded
granted: returned 1

# Tenant 71 has used 1 cpu nano.
print
----
closed epoch: 0 tenantHeap len: 1 top tenant: 53
 tenant-id: 1 used: 0, w: 1, fifo: -128
 tenant-id: 53 used: 0, w: 1, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 5, epoch: 0, qt: 100]
 tenant-id: 71 used: 1, w: 1, fifo: -128

# Try to return more cpu than used, to check that there is no overflow.
work-done id=4 cpu-time=-5
----
returnGrant 1

print
----
closed epoch: 0 tenantHeap len: 1 top tenant: 53
 tenant-id: 1 used: 0, w: 1, fifo: -128
 tenant-id: 53 used: 0, w: 1, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 5, epoch: 0, qt: 100]
 tenant-id: 71 used: 0, w: 1, fifo: -128

granted chain-id=10
----
continueGrantChain 10
id 7: admit succeeded
granted: returned 1

# No more waiting requests.
print
----
closed epoch: 0 tenantHeap len: 0
 tenant-id: 1 used: 0, w: 1, fifo: -128
 tenant-id: 53 used: 1, w: 1, fifo: -128
 tenant-id: 71 used: 0, w: 1, fifo: -128

# Granted returns false.
granted chain-id=10
----
granted: returned 0

print
----
closed epoch: 0 tenantHeap len: 0
 tenant-id: 1 used: 0, w: 1, fifo: -128
 tenant-id: 53 used: 1, w: 1, fifo: -128
 tenant-id: 71 used: 0, w: 1, fifo: -128

init
----

set-try-get-return-value v=false
----

admit id=1 tenant=53 priority=0 create-time-millis=1 bypass=false
----
tryGet: returning false

# Make the request wait long enough that we switch to LIFO.
advance-time millis=205
----
closed epoch: 2 tenantHeap len: 1 top tenant: 53
 tenant-id: 53 used: 0, w: 1, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 1, epoch: 0, qt: 100]

print
----
closed epoch: 2 tenantHeap len: 1 top tenant: 53
 tenant-id: 53 used: 0, w: 1, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 1, epoch: 0, qt: 100]

granted chain-id=5
----
continueGrantChain 5
id 1: admit succeeded
granted: returned 1

print
----
closed epoch: 2 tenantHeap len: 0
 tenant-id: 53 used: 1, w: 1, fifo: -128

# Switch to LIFO since request waited for 205ms.
advance-time millis=100
----
closed epoch: 3 tenantHeap len: 0
 tenant-id: 53 used: 1, w: 1, fifo: 1

admit id=2 tenant=53 priority=0 create-time-millis=50 bypass=false
----
tryGet: returning false

admit id=3 tenant=53 priority=0 create-time-millis=399 bypass=false
----

admit id=4 tenant=53 priority=0 create-time-millis=400 bypass=false
----

# Two requests are in closed epochs and one is in open epoch.
print
----
closed epoch: 3 tenantHeap len: 1 top tenant: 53
 tenant-id: 53 used: 1, w: 1, fifo: 1 waiting work heap: [0: pri: normal-pri, ct: 399, epoch: 3, qt: 405, lifo-ordering] [1: pri: normal-pri, ct: 50, epoch: 0, qt: 405, lifo-ordering] open epochs heap: [0: pri: normal-pri, ct: 400, epoch: 4, qt: 405]

# Latest request in closed epoch is granted.
granted chain-id=6
----
continueGrantChain 6
id 3: admit succeeded
granted: returned 1

# Older request in closed epoch is granted.
granted chain-id=7
----
continueGrantChain 7
id 2: admit succeeded
granted: returned 1

# Only request is in open epoch.
print
----
closed epoch: 3 tenantHeap len: 1 top tenant: 53
 tenant-id: 53 used: 3, w: 1, fifo: 1 open epochs heap: [0: pri: normal-pri, ct: 400, epoch: 4, qt: 405]

# Add request to closed epoch.
admit id=5 tenant=53 priority=0 create-time-millis=300 bypass=false
----

# Add request in open epoch 5 which is different from existing open epoch
# request that has epoch 4.
admit id=6 tenant=53 priority=0 create-time-millis=500 bypass=false
----

# Open epochs heap is ordered in rough FIFO.
print
----
closed epoch: 3 tenantHeap len: 1 top tenant: 53
 tenant-id: 53 used: 3, w: 1, fifo: 1 waiting work heap: [0: pri: normal-pri, ct: 300, epoch: 3, qt: 405, lifo-ordering] open epochs heap: [0: pri: normal-pri, ct: 400, epoch: 4, qt: 405] [1: pri: normal-pri, ct: 500, epoch: 5, qt: 405]

# Add high priority request in open epoch 5.
admit id=7 tenant=53 priority=127 create-time-millis=550 bypass=false
----

# The high priority request goes into the normal heap since it is >= the fifo
# threshold, and so is still using FIFO ordering.
print
----
closed epoch: 3 tenantHeap len: 1 top tenant: 53
 tenant-id: 53 used: 3, w: 1, fifo: 1 waiting work heap: [0: pri: high-pri, ct: 550, epoch: 5, qt: 405] [1: pri: normal-pri, ct: 300, epoch: 3, qt: 405, lifo-ordering] open epochs heap: [0: pri: normal-pri, ct: 400, epoch: 4, qt: 405] [1: pri: normal-pri, ct: 500, epoch: 5, qt: 405]

# Make the request wait for 60ms so we don't switch back to fifo.
advance-time millis=60
----
closed epoch: 3 tenantHeap len: 1 top tenant: 53
 tenant-id: 53 used: 3, w: 1, fifo: 1 waiting work heap: [0: pri: high-pri, ct: 550, epoch: 5, qt: 405] [1: pri: normal-pri, ct: 300, epoch: 3, qt: 405, lifo-ordering] open epochs heap: [0: pri: normal-pri, ct: 400, epoch: 4, qt: 405] [1: pri: normal-pri, ct: 500, epoch: 5, qt: 405]

granted chain-id=8
----
continueGrantChain 8
id 7: admit succeeded
granted: returned 1

granted chain-id=9
----
continueGrantChain 9
id 5: admit succeeded
granted: returned 1

# Add another request to closed epoch that is subject to LIFO ordering.
admit id=8 tenant=53 priority=0 create-time-millis=350 bypass=false
----

print
----
closed epoch: 3 tenantHeap len: 1 top tenant: 53
 tenant-id: 53 used: 5, w: 1, fifo: 1 waiting work heap: [0: pri: normal-pri, ct: 350, epoch: 3, qt: 465, lifo-ordering] open epochs heap: [0: pri: normal-pri, ct: 400, epoch: 4, qt: 405] [1: pri: normal-pri, ct: 500, epoch: 5, qt: 405]

# One request moved from open to closed epoch heap.
advance-time millis=40
----
closed epoch: 4 tenantHeap len: 1 top tenant: 53
 tenant-id: 53 used: 5, w: 1, fifo: 1 waiting work heap: [0: pri: normal-pri, ct: 400, epoch: 4, qt: 405, lifo-ordering] [1: pri: normal-pri, ct: 350, epoch: 3, qt: 465, lifo-ordering] open epochs heap: [0: pri: normal-pri, ct: 500, epoch: 5, qt: 405]

granted chain-id=10
----
continueGrantChain 10
id 4: admit succeeded
granted: returned 1

print
----
closed epoch: 4 tenantHeap len: 1 top tenant: 53
 tenant-id: 53 used: 6, w: 1, fifo: 1 waiting work heap: [0: pri: normal-pri, ct: 350, epoch: 3, qt: 465, lifo-ordering] open epochs heap: [0: pri: normal-pri, ct: 500, epoch: 5, qt: 405]

granted chain-id=11
----
continueGrantChain 11
id 8: admit succeeded
granted: returned 1

print
----
closed epoch: 4 tenantHeap len: 1 top tenant: 53
 tenant-id: 53 used: 7, w: 1, fifo: 1 open epochs heap: [0: pri: normal-pri, ct: 500, epoch: 5, qt: 405]

# Can dequeue from the open epochs heap if nothing else is remaining.
granted chain-id=12
----
continueGrantChain 12
id 6: admit succeeded
granted: returned 1

print
----
closed epoch: 4 tenantHeap len: 0
 tenant-id: 53 used: 8, w: 1, fifo: 1

# Add a request for an already closed epoch.
admit id=9 tenant=53 priority=0 create-time-millis=380 bypass=false
----
tryGet: returning false

print
----
closed epoch: 4 tenantHeap len: 1 top tenant: 53
 tenant-id: 53 used: 8, w: 1, fifo: 1 waiting work heap: [0: pri: normal-pri, ct: 380, epoch: 3, qt: 505, lifo-ordering]

# This time advance means the previous request will see significant queueing.
advance-time millis=100
----
closed epoch: 5 tenantHeap len: 1 top tenant: 53
 tenant-id: 53 used: 8, w: 1, fifo: 1 waiting work heap: [0: pri: normal-pri, ct: 380, epoch: 3, qt: 505, lifo-ordering]

# This request in an already closed epoch gets ahead because of higher
# create-time-millis.
admit id=10 tenant=53 priority=0 create-time-millis=390 bypass=false
----

print
----
closed epoch: 5 tenantHeap len: 1 top tenant: 53
 tenant-id: 53 used: 8, w: 1, fifo: 1 waiting work heap: [0: pri: normal-pri, ct: 390, epoch: 3, qt: 605, lifo-ordering] [1: pri: normal-pri, ct: 380, epoch: 3, qt: 505, lifo-ordering]

granted chain-id=12
----
continueGrantChain 12
id 10: admit succeeded
granted: returned 1

print
----
closed epoch: 5 tenantHeap len: 1 top tenant: 53
 tenant-id: 53 used: 9, w: 1, fifo: 1 waiting work heap: [0: pri: normal-pri, ct: 380, epoch: 3, qt: 505, lifo-ordering]

# This advance will switch all priorities back to FIFO.
advance-time millis=100
----
closed epoch: 6 tenantHeap len: 1 top tenant: 53
 tenant-id: 53 used: 9, w: 1, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 380, epoch: 3, qt: 505, lifo-ordering]

admit id=11 tenant=53 priority=0 create-time-millis=610 bypass=false
----

admit id=12 tenant=53 priority=-128 create-time-millis=615 bypass=false
----

# When comparing work with LIFO and FIFO marking, with the same priority, we
# pick LIFO. The lower priority request is ordered at the end even though it
# has the highest create time.
print
----
closed epoch: 6 tenantHeap len: 1 top tenant: 53
 tenant-id: 53 used: 9, w: 1, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 610, epoch: 6, qt: 705] [1: pri: normal-pri, ct: 380, epoch: 3, qt: 505, lifo-ordering] [2: pri: low-pri, ct: 615, epoch: 6, qt: 705]

granted chain-id=13
----
continueGrantChain 13
id 11: admit succeeded
granted: returned 1

# With the remaining two items, the priority is different, so higher priority
# is preferred.
print
----
closed epoch: 6 tenantHeap len: 1 top tenant: 53
 tenant-id: 53 used: 10, w: 1, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 380, epoch: 3, qt: 505, lifo-ordering] [1: pri: low-pri, ct: 615, epoch: 6, qt: 705]

granted chain-id=14
----
continueGrantChain 14
id 9: admit succeeded
granted: returned 1

granted chain-id=15
----
continueGrantChain 15
id 12: admit succeeded
granted: returned 1

# Advance time again. Since one of the priority=0 requests experienced high
# latency, switch that back to LIFO.
advance-time millis=100
----
closed epoch: 7 tenantHeap len: 0
 tenant-id: 53 used: 12, w: 1, fifo: 1

# Add a request whose epoch is not closed.
admit id=13 tenant=53 priority=0 create-time-millis=810 bypass=false
----
tryGet: returning false

print
----
closed epoch: 7 tenantHeap len: 1 top tenant: 53
 tenant-id: 53 used: 12, w: 1, fifo: 1 open epochs heap: [0: pri: normal-pri, ct: 810, epoch: 8, qt: 805]

# Cancel that request.
cancel-work id=13
----
id 13: admit failed

print
----
closed epoch: 7 tenantHeap len: 0
 tenant-id: 53 used: 12, w: 1, fifo: 1

# Closed epoch advances. The FIFO threshold is not changed since the only
# request was canceled.
advance-time millis=100
----
closed epoch: 8 tenantHeap len: 0
 tenant-id: 53 used: 12, w: 1, fifo: 1

# Closed epoch advances. All priorities are now subject to FIFO.
advance-time millis=100
----
closed epoch: 9 tenantHeap len: 0
 tenant-id: 53 used: 12, w: 1, fifo: -128

# Test tenant weights.
init
----

set-try-get-return-value v=false
----

# Empty map is fine.
set-tenant-weights weights=
----
closed epoch: 0 tenantHeap len: 0

# Tenant 5 gets a weight of 1.
admit id=1 tenant=5 priority=0 create-time-millis=1 bypass=false
----
tryGet: returning false

print
----
closed epoch: 0 tenantHeap len: 1 top tenant: 5
 tenant-id: 5 used: 0, w: 1, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 1, epoch: 0, qt: 100]

# Weight is unchanged for tenant 5.
set-tenant-weights weights=10:11
----
closed epoch: 0 tenantHeap len: 1 top tenant: 5
 tenant-id: 5 used: 0, w: 1, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 1, epoch: 0, qt: 100]

# Now tenant 5 has a new weight.
set-tenant-weights weights=5:6,10:11
----
closed epoch: 0 tenantHeap len: 1 top tenant: 5
 tenant-id: 5 used: 0, w: 6, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 1, epoch: 0, qt: 100]

admit id=2 tenant=10 priority=0 create-time-millis=1 bypass=false
----

print
----
closed epoch: 0 tenantHeap len: 2 top tenant: 10
 tenant-id: 5 used: 0, w: 6, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 1, epoch: 0, qt: 100]
 tenant-id: 10 used: 0, w: 11, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 1, epoch: 0, qt: 100]

granted chain-id=1
----
continueGrantChain 1
id 2: admit succeeded
granted: returned 1

granted chain-id=2
----
continueGrantChain 2
id 1: admit succeeded
granted: returned 1

# Both tenants are using 1 slot.
print
----
closed epoch: 0 tenantHeap len: 0
 tenant-id: 5 used: 1, w: 6, fifo: -128
 tenant-id: 10 used: 1, w: 11, fifo: -128

# Add additional requests for each tenant
admit id=3 tenant=5 priority=0 create-time-millis=1 bypass=false
----
tryGet: returning false

admit id=4 tenant=10 priority=0 create-time-millis=1 bypass=false
----

# The top of the heap is tenant 10 since it has a higher weight.
print
----
closed epoch: 0 tenantHeap len: 2 top tenant: 10
 tenant-id: 5 used: 1, w: 6, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 1, epoch: 0, qt: 100]
 tenant-id: 10 used: 1, w: 11, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 1, epoch: 0, qt: 100]

# Grant to tenant 10 so it is using 2 slots.
granted chain-id=3
----
continueGrantChain 3
id 4: admit succeeded
granted: returned 1

print
----
closed epoch: 0 tenantHeap len: 1 top tenant: 5
 tenant-id: 5 used: 1, w: 6, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 1, epoch: 0, qt: 100]
 tenant-id: 10 used: 2, w: 11, fifo: -128

# Another request from tenant 10.
admit id=5 tenant=10 priority=0 create-time-millis=1 bypass=false
----

# Tenant 10's weight is not high enough for it to be the top of the heap.
print
----
closed epoch: 0 tenantHeap len: 2 top tenant: 5
 tenant-id: 5 used: 1, w: 6, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 1, epoch: 0, qt: 100]
 tenant-id: 10 used: 2, w: 11, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 1, epoch: 0, qt: 100]

# Increase tenant 10's weight so it becomes the top of the heap. Weight
# scaling is also applied here to make the max weight 20, and reduce tenant
# 5's weight to 4.
set-tenant-weights weights=5:6,10:30
----
closed epoch: 0 tenantHeap len: 2 top tenant: 10
 tenant-id: 5 used: 1, w: 4, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 1, epoch: 0, qt: 100]
 tenant-id: 10 used: 2, w: 20, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 1, epoch: 0, qt: 100]

# Restore all weights to 1. Tenant 5 is now top of the heap.
set-tenant-weights weights=5:1,10:1
----
closed epoch: 0 tenantHeap len: 2 top tenant: 5
 tenant-id: 5 used: 1, w: 1, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 1, epoch: 0, qt: 100]
 tenant-id: 10 used: 2, w: 1, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 1, epoch: 0, qt: 100]

# Adjust weights to make tenant 10 just slightly preferable over tenant 5.
set-tenant-weights weights=5:6,10:13
----
closed epoch: 0 tenantHeap len: 2 top tenant: 10
 tenant-id: 5 used: 1, w: 6, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 1, epoch: 0, qt: 100]
 tenant-id: 10 used: 2, w: 13, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 1, epoch: 0, qt: 100]

# Add another request for tenant 10.
admit id=6 tenant=10 priority=0 create-time-millis=1 bypass=false
----

granted chain-id=4
----
continueGrantChain 4
id 5: admit succeeded
granted: returned 1

# Tenant 10 is using 3 slots and tenant 5 is using 1 slot. Tenant 5 is the top
# of the heap.
print
----
closed epoch: 0 tenantHeap len: 2 top tenant: 5
 tenant-id: 5 used: 1, w: 6, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 1, epoch: 0, qt: 100]
 tenant-id: 10 used: 3, w: 13, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 1, epoch: 0, qt: 100]

# Bump up tenant 10's weight to a huge value. Tenant 5's weight is not scaled
# down to 0, since the minimum weight is 1.
set-tenant-weights weights=5:1,10:100000
----
closed epoch: 0 tenantHeap len: 2 top tenant: 10
 tenant-id: 5 used: 1, w: 1, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 1, epoch: 0, qt: 100]
 tenant-id: 10 used: 3, w: 20, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 1, epoch: 0, qt: 100]

granted chain-id=5
----
continueGrantChain 5
id 6: admit succeeded
granted: returned 1

granted chain-id=6
----
continueGrantChain 6
id 3: admit succeeded
granted: returned 1

# Test that batch update of tenant weights is working correctly.
init
----

set-try-get-return-value v=false
----

admit id=1 tenant=1 priority=0 create-time-millis=1 bypass=false
----
tryGet: returning false

admit id=2 tenant=2 priority=0 create-time-millis=1 bypass=false
----

admit id=3 tenant=3 priority=0 create-time-millis=1 bypass=false
----

admit id=4 tenant=4 priority=0 create-time-millis=1 bypass=false
----

admit id=5 tenant=5 priority=0 create-time-millis=1 bypass=false
----

admit id=6 tenant=6 priority=0 create-time-millis=1 bypass=false
----

admit id=7 tenant=7 priority=0 create-time-millis=1 bypass=false
----

admit id=8 tenant=8 priority=0 create-time-millis=1 bypass=false
----

print
----
closed epoch: 0 tenantHeap len: 8 top tenant: 1
 tenant-id: 1 used: 0, w: 1, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 1, epoch: 0, qt: 100]
 tenant-id: 2 used: 0, w: 1, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 1, epoch: 0, qt: 100]
 tenant-id: 3 used: 0, w: 1, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 1, epoch: 0, qt: 100]
 tenant-id: 4 used: 0, w: 1, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 1, epoch: 0, qt: 100]
 tenant-id: 5 used: 0, w: 1, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 1, epoch: 0, qt: 100]
 tenant-id: 6 used: 0, w: 1, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 1, epoch: 0, qt: 100]
 tenant-id: 7 used: 0, w: 1, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 1, epoch: 0, qt: 100]
 tenant-id: 8 used: 0, w: 1, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 1, epoch: 0, qt: 100]

# Set weights for 7 out of the 8 tenants.
set-tenant-weights weights=1:2,2:3,3:4,4:5,5:6,7:8,8:9
----
closed epoch: 0 tenantHeap len: 8 top tenant: 8
 tenant-id: 1 used: 0, w: 2, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 1, epoch: 0, qt: 100]
 tenant-id: 2 used: 0, w: 3, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 1, epoch: 0, qt: 100]
 tenant-id: 3 used: 0, w: 4, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 1, epoch: 0, qt: 100]
 tenant-id: 4 used: 0, w: 5, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 1, epoch: 0, qt: 100]
 tenant-id: 5 used: 0, w: 6, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 1, epoch: 0, qt: 100]
 tenant-id: 6 used: 0, w: 1, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 1, epoch: 0, qt: 100]
 tenant-id: 7 used: 0, w: 8, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 1, epoch: 0, qt: 100]
 tenant-id: 8 used: 0, w: 9, fifo: -128 waiting work heap: [0: pri: normal-pri, ct: 1, epoch: 0, qt: 100]

granted chain-id=1
----
continueGrantChain 1
id 8: admit succeeded
granted: returned 1

granted chain-id=2
----
continueGrantChain 2
id 7: admit succeeded
granted: returned 1

granted chain-id=3
----
continueGrantChain 3
id 5: admit succeeded
granted: returned 1

granted chain-id=4
----
continueGrantChain 4
id 4: admit succeeded
granted: returned 1

granted chain-id=5
----
continueGrantChain 5
id 3: admit succeeded
granted: returned 1

granted chain-id=6
----
continueGrantChain 6
id 2: admit succeeded
granted: returned 1

granted chain-id=7
----
continueGrantChain 7
id 1: admit succeeded
granted: returned 1

granted chain-id=8
----
continueGrantChain 8
id 6: admit succeeded
granted: returned 1

print
----
closed epoch: 0 tenantHeap len: 0
 tenant-id: 1 used: 1, w: 2, fifo: -128
 tenant-id: 2 used: 1, w: 3, fifo: -128
 tenant-id: 3 used: 1, w: 4, fifo: -128
 tenant-id: 4 used: 1, w: 5, fifo: -128
 tenant-id: 5 used: 1, w: 6, fifo: -128
 tenant-id: 6 used: 1, w: 1, fifo: -128
 tenant-id: 7 used: 1, w: 8, fifo: -128
 tenant-id: 8 used: 1, w: 9, fifo: -128
