# -------------------------------------------------------------------------
# A scan finds many abandoned intents from same txn that don't get added to
# the lock table, and get resolved.
# -------------------------------------------------------------------------

# This setting causes the txn status cache to be consulted when discovered
# locks > 1.
debug-set-discovered-locks-threshold-to-consult-txn-status-cache n=1
----

new-txn name=txn1 ts=10,1 epoch=0
----

new-txn name=txn2 ts=10,1 epoch=0
----

new-request name=req1 txn=txn1 ts=10,1
  scan key=a endkey=b
----

sequence req=req1
----
[1] sequence req1: sequencing request
[1] sequence req1: acquiring latches
[1] sequence req1: scanning lock table for conflicting locks
[1] sequence req1: sequencing complete, returned guard

handle-lock-conflict-error req=req1 lease-seq=1
  lock txn=txn2 key=a
----
[2] handle lock conflict error req1: handled conflicting locks on ‹"a"›, released latches

debug-lock-table
----
num=1
 lock: "a"
  holder: txn: 00000002-0000-0000-0000-000000000000 epoch: 0, iso: Serializable, ts: 10.000000000,1, info: repl [Intent]

sequence req=req1
----
[3] sequence req1: re-sequencing request
[3] sequence req1: acquiring latches
[3] sequence req1: scanning lock table for conflicting locks
[3] sequence req1: waiting in lock wait-queues
[3] sequence req1: lock wait-queue event: wait for txn 00000002 holding lock @ key ‹"a"› (queuedLockingRequests: 0, queuedReaders: 1)
[3] sequence req1: pushing after 0s for: deadlock/liveness detection = true, timeout enforcement = false, priority enforcement = false, wait policy error = false
[3] sequence req1: pushing timestamp of txn 00000002 above 10.000000000,1
[3] sequence req1: blocked on select in concurrency_test.(*cluster).PushTransaction

debug-lock-table
----
num=1
 lock: "a"
  holder: txn: 00000002-0000-0000-0000-000000000000 epoch: 0, iso: Serializable, ts: 10.000000000,1, info: repl [Intent]
   waiting readers:
    req: 1, txn: 00000001-0000-0000-0000-000000000000

debug-advance-clock ts=123
----

# txn1 is the distinguished waiter on key "a". It will push txn2, notice that it
# is aborted, and then resolve key "a". This places txn2 in the txnStatusCache.
on-txn-updated txn=txn2 status=aborted
----
[-] update txn: aborting txn2
[3] sequence req1: resolving intent ‹"a"› for txn 00000002 with ABORTED status
[3] sequence req1: lock wait-queue event: done waiting
[3] sequence req1: conflicted with ‹00000002-0000-0000-0000-000000000000› on ‹"a"› for 123.000s
[3] sequence req1: acquiring latches
[3] sequence req1: scanning lock table for conflicting locks
[3] sequence req1: sequencing complete, returned guard

debug-lock-table
----
num=0

finish req=req1
----
[-] finish req1: finishing request

new-request name=req2 txn=txn1 ts=10,1
  scan key=b endkey=z
----

sequence req=req2
----
[4] sequence req2: sequencing request
[4] sequence req2: acquiring latches
[4] sequence req2: scanning lock table for conflicting locks
[4] sequence req2: sequencing complete, returned guard

# The locks get resolved instead of being added to the lock table.
handle-lock-conflict-error req=req2 lease-seq=1
  lock txn=txn2 key=b
  lock txn=txn2 key=c
  lock txn=txn2 key=d
  lock txn=txn2 key=e
  lock txn=txn2 key=f
  lock txn=txn2 key=g
  lock txn=txn2 key=h
  lock txn=txn2 key=i
  lock txn=txn2 key=j
----
[5] handle lock conflict error req2: resolving a batch of 9 intent(s)
[5] handle lock conflict error req2: resolving intent ‹"b"› for txn 00000002 with ABORTED status
[5] handle lock conflict error req2: resolving intent ‹"c"› for txn 00000002 with ABORTED status
[5] handle lock conflict error req2: resolving intent ‹"d"› for txn 00000002 with ABORTED status
[5] handle lock conflict error req2: resolving intent ‹"e"› for txn 00000002 with ABORTED status
[5] handle lock conflict error req2: resolving intent ‹"f"› for txn 00000002 with ABORTED status
[5] handle lock conflict error req2: resolving intent ‹"g"› for txn 00000002 with ABORTED status
[5] handle lock conflict error req2: resolving intent ‹"h"› for txn 00000002 with ABORTED status
[5] handle lock conflict error req2: resolving intent ‹"i"› for txn 00000002 with ABORTED status
[5] handle lock conflict error req2: resolving intent ‹"j"› for txn 00000002 with ABORTED status
[5] handle lock conflict error req2: handled conflicting locks on ‹"b"›, ‹"c"›, ‹"d"›, ‹"e"›, ‹"f"›, ‹"g"›, ‹"h"›, ‹"i"›, ‹"j"›, released latches

debug-lock-table
----
num=0

sequence req=req2
----
[6] sequence req2: re-sequencing request
[6] sequence req2: acquiring latches
[6] sequence req2: scanning lock table for conflicting locks
[6] sequence req2: sequencing complete, returned guard

finish req=req2
----
[-] finish req2: finishing request

reset namespace
----
