| 1 |
//go:build !no_antithesis_sdk |
| 2 |
|
| 3 |
package instrumentation |
| 4 |
|
| 5 |
import ( |
| 6 |
"sync" |
| 7 |
"sync/atomic" |
| 8 |
"testing" |
| 9 |
) |
| 10 |
|
| 11 |
func makeLease(epoch, grant uint64) uint64 { |
| 12 |
return (epoch&leaseEpochMask)<<leaseEpochShift | grant<<leaseGrantedShift | grant |
| 13 |
} |
| 14 |
|
| 15 |
func leaseFields(word uint64) (epoch, granted, remaining uint64) { |
| 16 |
return (word >> leaseEpochShift) & leaseEpochMask, |
| 17 |
(word >> leaseGrantedShift) & leaseFieldMask, |
| 18 |
word & leaseFieldMask |
| 19 |
} |
| 20 |
|
| 21 |
// Many goroutines hammer one lease word whose grant is smaller than the |
| 22 |
// goroutine count. The lease may hand out at most `grant` local hits, and |
| 23 |
// the decrements must stay inside the remaining field: a decrement guarded |
| 24 |
// by a stale snapshot borrows into granted, which breaks the library's |
| 25 |
// remaining <= granted invariant and makes the next call-in's |
| 26 |
// granted-remaining wrap — a library-side fatal in production. |
| 27 |
func TestLeaseLocalHitNeverOverdraws(t *testing.T) { |
| 28 |
const goroutines = 64 |
| 29 |
currentEpoch := func() (uint64, bool) { return 7, true } |
| 30 |
for round := 0; round < 300; round++ { |
| 31 |
grant := uint64(1 + round%3) |
| 32 |
lease := makeLease(7, grant) |
| 33 |
var counted atomic.Uint64 |
| 34 |
var start, done sync.WaitGroup |
| 35 |
start.Add(1) |
| 36 |
done.Add(goroutines) |
| 37 |
for g := 0; g < goroutines; g++ { |
| 38 |
go func() { |
| 39 |
defer done.Done() |
| 40 |
start.Wait() |
| 41 |
if _, ok := leaseLocalHit(&lease, currentEpoch); ok { |
| 42 |
counted.Add(1) |
| 43 |
} |
| 44 |
}() |
| 45 |
} |
| 46 |
start.Done() |
| 47 |
done.Wait() |
| 48 |
|
| 49 |
if counted.Load() > grant { |
| 50 |
t.Fatalf( |
| 51 |
"round %d: %d hits counted locally under a grant of %d", |
| 52 |
round, counted.Load(), grant, |
| 53 |
) |
| 54 |
} |
| 55 |
epoch, granted, remaining := leaseFields(atomic.LoadUint64(&lease)) |
| 56 |
if epoch != 7 || granted != grant || remaining != grant-counted.Load() { |
| 57 |
t.Fatalf( |
| 58 |
"round %d: lease word corrupted: epoch=%d granted=%d remaining=%d (grant %d, %d counted)", |
| 59 |
round, epoch, granted, remaining, grant, counted.Load(), |
| 60 |
) |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
// The revocation race: goroutines that passed the epoch check against a |
| 66 |
// stale snapshot must not decrement a word that was refreshed underneath |
| 67 |
// them. The epoch flips mid-storm; whatever interleaving occurs, the word's |
| 68 |
// fields must remain consistent and local hits must never exceed the grant. |
| 69 |
func TestLeaseLocalHitStopsAtRevocation(t *testing.T) { |
| 70 |
const goroutines = 64 |
| 71 |
for round := 0; round < 300; round++ { |
| 72 |
grant := uint64(1 + round%3) |
| 73 |
lease := makeLease(3, grant) |
| 74 |
var epochWord atomic.Uint64 |
| 75 |
epochWord.Store(3) |
| 76 |
currentEpoch := func() (uint64, bool) { return epochWord.Load(), true } |
| 77 |
var counted atomic.Uint64 |
| 78 |
var start, done sync.WaitGroup |
| 79 |
start.Add(1) |
| 80 |
done.Add(goroutines) |
| 81 |
for g := 0; g < goroutines; g++ { |
| 82 |
revoker := g == 0 |
| 83 |
go func() { |
| 84 |
defer done.Done() |
| 85 |
start.Wait() |
| 86 |
if revoker { |
| 87 |
epochWord.Add(1) |
| 88 |
return |
| 89 |
} |
| 90 |
if _, ok := leaseLocalHit(&lease, currentEpoch); ok { |
| 91 |
counted.Add(1) |
| 92 |
} |
| 93 |
}() |
| 94 |
} |
| 95 |
start.Done() |
| 96 |
done.Wait() |
| 97 |
|
| 98 |
if counted.Load() > grant { |
| 99 |
t.Fatalf( |
| 100 |
"round %d: %d hits counted locally under a grant of %d", |
| 101 |
round, counted.Load(), grant, |
| 102 |
) |
| 103 |
} |
| 104 |
epoch, granted, remaining := leaseFields(atomic.LoadUint64(&lease)) |
| 105 |
if epoch != 3 || granted != grant || remaining != grant-counted.Load() { |
| 106 |
t.Fatalf( |
| 107 |
"round %d: lease word corrupted: epoch=%d granted=%d remaining=%d (grant %d, %d counted)", |
| 108 |
round, epoch, granted, remaining, grant, counted.Load(), |
| 109 |
) |
| 110 |
} |
| 111 |
} |
| 112 |
} |