create-tenant tenant=5
----

token-bucket-request tenant=5
instance_id: 1
instance_lease: "foo"
seq_num: 1
consumption:
  ru: 10
  kvru: 10
  read_batches: 1
  read_req: 20
  read_bytes: 30
  write_batches: 3
  write_req: 40
  write_bytes: 50
  sql_pods_cpu_seconds: 60
  pgwire_egress_bytes: 70
  estimated_cpu_seconds: 80
----

inspect tenant=5
----
Bucket state: token-burst-limit=0  token-refill-rate=10000  token-current=0  token-current-avg=0
Consumption: ru=10 kvru=10  reads=20 in 1 batches (30 bytes)  writes=40 in 3 batches (50 bytes)  pod-cpu-usage: 60 secs  pgwire-egress=70 bytes  external-egress=0 bytes  external-ingress=0 bytes  estimated-cpu: 80 secs
Rates: write-batches=0,0.3  estimated-cpu=0,8
Last update: 00:00:00.000
First active instance: 1
  Instance 1:  lease="foo"  seq=1  next-instance=0  last-update=00:00:00.000

# Request with higher seq_num contributes to consumption.
token-bucket-request tenant=5
instance_id: 1
instance_lease: "foo"
seq_num: 2
consumption:
  ru: 10
  kvru: 10
  read_batches: 1
  read_req: 20
  read_bytes: 30
  write_batches: 3
  write_req: 40
  write_bytes: 50
  sql_pods_cpu_seconds: 60
  pgwire_egress_bytes: 70
  estimated_cpu_seconds: 80
----

inspect tenant=5
----
Bucket state: token-burst-limit=0  token-refill-rate=10000  token-current=0  token-current-avg=0
Consumption: ru=20 kvru=20  reads=40 in 2 batches (60 bytes)  writes=80 in 6 batches (100 bytes)  pod-cpu-usage: 120 secs  pgwire-egress=140 bytes  external-egress=0 bytes  external-ingress=0 bytes  estimated-cpu: 160 secs
Rates: write-batches=0,0.6  estimated-cpu=0,16
Last update: 00:00:00.000
First active instance: 1
  Instance 1:  lease="foo"  seq=2  next-instance=0  last-update=00:00:00.000

# Request with duplicate seq_num should not contribute to consumption.
token-bucket-request tenant=5
instance_id: 1
instance_lease: "foo"
seq_num: 2
consumption:
  ru: 10
  kvru: 10
  read_batches: 1
  read_req: 20
  read_bytes: 30
  write_batches: 3
  write_req: 40
  write_bytes: 50
  sql_pods_cpu_seconds: 60
  pgwire_egress_bytes: 70
  estimated_cpu_seconds: 80
----

inspect tenant=5
----
Bucket state: token-burst-limit=0  token-refill-rate=10000  token-current=0  token-current-avg=0
Consumption: ru=20 kvru=20  reads=40 in 2 batches (60 bytes)  writes=80 in 6 batches (100 bytes)  pod-cpu-usage: 120 secs  pgwire-egress=140 bytes  external-egress=0 bytes  external-ingress=0 bytes  estimated-cpu: 160 secs
Rates: write-batches=0,0.6  estimated-cpu=0,16
Last update: 00:00:00.000
First active instance: 1
  Instance 1:  lease="foo"  seq=2  next-instance=0  last-update=00:00:00.000

# Request with older seq_num should not contribute to consumption.
token-bucket-request tenant=5
instance_id: 1
instance_lease: "foo"
seq_num: 1
consumption:
  ru: 10
  read_batches: 1
  read_req: 20
  read_bytes: 30
  write_batches: 3
  write_req: 40
  write_bytes: 50
  sql_pods_cpu_seconds: 60
  pgwire_egress_bytes: 70
  estimated_cpu_seconds: 80
----

inspect tenant=5
----
Bucket state: token-burst-limit=0  token-refill-rate=10000  token-current=0  token-current-avg=0
Consumption: ru=20 kvru=20  reads=40 in 2 batches (60 bytes)  writes=80 in 6 batches (100 bytes)  pod-cpu-usage: 120 secs  pgwire-egress=140 bytes  external-egress=0 bytes  external-ingress=0 bytes  estimated-cpu: 160 secs
Rates: write-batches=0,0.6  estimated-cpu=0,16
Last update: 00:00:00.000
First active instance: 1
  Instance 1:  lease="foo"  seq=2  next-instance=0  last-update=00:00:00.000

# However, if the lease changes, we accept a new sequence number.
token-bucket-request tenant=5
instance_id: 1
instance_lease: "bar"
seq_num: 1
consumption:
  ru: 10
  kvru: 20
  read_batches: 1
  read_req: 20
  read_bytes: 30
  write_batches: 3
  write_req: 40
  write_bytes: 50
  sql_pods_cpu_seconds: 60
  pgwire_egress_bytes: 70
  estimated_cpu_seconds: 80
----

inspect tenant=5
----
Bucket state: token-burst-limit=0  token-refill-rate=10000  token-current=0  token-current-avg=0
Consumption: ru=30 kvru=40  reads=60 in 3 batches (90 bytes)  writes=120 in 9 batches (150 bytes)  pod-cpu-usage: 180 secs  pgwire-egress=210 bytes  external-egress=0 bytes  external-ingress=0 bytes  estimated-cpu: 240 secs
Rates: write-batches=0,0.9  estimated-cpu=0,24
Last update: 00:00:00.000
First active instance: 1
  Instance 1:  lease="bar"  seq=1  next-instance=0  last-update=00:00:00.000

# A different instance has its own sequence number.
token-bucket-request tenant=5
instance_id: 2
instance_lease: "baz"
seq_num: 1
consumption:
  ru: 10
  kvru: 30
  read_batches: 1
  read_req: 20
  read_bytes: 30
  write_batches: 3
  write_req: 40
  write_bytes: 50
  sql_pods_cpu_seconds: 60
  pgwire_egress_bytes: 70
  estimated_cpu_seconds: 80
----

inspect tenant=5
----
Bucket state: token-burst-limit=0  token-refill-rate=10000  token-current=0  token-current-avg=0
Consumption: ru=40 kvru=70  reads=80 in 4 batches (120 bytes)  writes=160 in 12 batches (200 bytes)  pod-cpu-usage: 240 secs  pgwire-egress=280 bytes  external-egress=0 bytes  external-ingress=0 bytes  estimated-cpu: 320 secs
Rates: write-batches=0,1.2  estimated-cpu=0,32
Last update: 00:00:00.000
First active instance: 1
  Instance 1:  lease="bar"  seq=1  next-instance=2  last-update=00:00:00.000
  Instance 2:  lease="baz"  seq=1  next-instance=0  last-update=00:00:00.000
