# A simple ingest-and-excise test that ensures invariants are respected in the
# presence of overlapping writes to the memtable, flushes, and compactions. The
# individual commands don't assert much themselves, depending on Pebble's
# invariant violations to trigger if a sequence violates invariants.

define
L0
  a.SET.3:v
  apple.SET.3:v
  b.SET.3:v
L0
  a.SET.2:v
  avocado.SET.3:v
----
L0.1:
  000004:[a#3,SET-b#3,SET]
L0.0:
  000005:[a#2,SET-avocado#3,SET]
0:define

build ext-ab
set a a
set anchovy anchovy
del-range a b
range-key-del a b
----
1:build

build ext-bc
set b b
set banana banana
del-range b c
range-key-del b c
----
2:build

batch
set apple 4
----
3:batch

batch
set banana 5
----
4:batch

flush
----
5:flush

maybe-compact
----
6:maybe-compact

# Create a memtable overlap with the ingested sst to trigger a flushable ingest.
batch
set alpha 5
----
7:batch

ingest-and-excise excise=a-b ext-ab
----
8:ingest-and-excise

ingest-and-excise excise=b-c ext-bc
----
9:ingest-and-excise

# Re-run the same sequence of operations 10 times, injecting random latency between
# some steps and within some IO operations. Randomize the ordering of the last four
# steps.

run io-latency=(.1,100µs) step-latency=(.2,5ms) count=10
sequential( 0:define 1:build 2:build reorder( 3:batch 4:batch 5:flush 6:maybe-compact 7:batch 8:ingest-and-excise 9:ingest-and-excise ) )
----
ok

# Re-run the same sequence of operations 10 times, injecting random latency between
# some steps and within some IO operations. Run the last four steps in parallel.

run io-latency=(.1,100µs) step-latency=(.2,5ms) count=10
sequential( 0:define 1:build 2:build parallel( 3:batch 4:batch 5:flush 6:maybe-compact 7:batch 8:ingest-and-excise 9:ingest-and-excise ) )
----
ok

# Run a test with two batches committing in parallel, competing for the
# remaining bytes of the mutable memtable. Only one of the batches can fit in
# the remaining bytes, and the other must rotate the memtable.

reset
----

define memtable-size=65536
----
0:define

memtable-info reserved in-use
----
flushable queue: 1 entries
mutable:
  alloced:  65536
  reserved: 1123
  in-use:   0
1:memtable-info

batch
set a <rand-bytes=30000>
----
2:batch

batch
set b <rand-bytes=30000>
----
3:batch

memtable-info
----
flushable queue: 1 entries
mutable:
  alloced:  65536
4:memtable-info

batch
set c <rand-bytes=3000>
----
5:batch

batch
set d <rand-bytes=3000>
----
6:batch

run count=100
sequential( 0:define 1:memtable-info 2:batch 3:batch 4:memtable-info parallel( 5:batch 6:batch ) )
----
ok

excise a z
----
7:excise

run count=100
sequential( 0:define 1:memtable-info 2:batch 3:batch 4:memtable-info parallel( 5:batch 6:batch ) 7:excise )
----
ok
