bits_test.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. package nebula
  2. import (
  3. "testing"
  4. "github.com/slackhq/nebula/test"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. // snapshot returns the bitmap as a []bool of length b.length, for readable
  8. // test assertions against the now-packed []uint64 storage.
  9. func (b *Bits) snapshot() []bool {
  10. out := make([]bool, b.length)
  11. for i := uint64(0); i < b.length; i++ {
  12. out[i] = b.get(i)
  13. }
  14. return out
  15. }
  16. func TestBitsRequiresPowerOfTwo(t *testing.T) {
  17. assert.Panics(t, func() { NewBits(10) })
  18. assert.Panics(t, func() { NewBits(0) })
  19. assert.NotPanics(t, func() { NewBits(1) })
  20. assert.NotPanics(t, func() { NewBits(16) })
  21. assert.NotPanics(t, func() { NewBits(1024) })
  22. assert.NotPanics(t, func() { NewBits(16384) })
  23. }
  24. func TestBits(t *testing.T) {
  25. l := test.NewLogger()
  26. b := NewBits(16)
  27. assert.EqualValues(t, 16, b.length)
  28. // This is initialized to zero - receive one. This should work.
  29. assert.True(t, b.Check(l, 1))
  30. assert.True(t, b.Update(l, 1))
  31. assert.EqualValues(t, 1, b.current)
  32. g := []bool{true, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false}
  33. assert.Equal(t, g, b.snapshot())
  34. // Receive two
  35. assert.True(t, b.Check(l, 2))
  36. assert.True(t, b.Update(l, 2))
  37. assert.EqualValues(t, 2, b.current)
  38. g = []bool{true, true, true, false, false, false, false, false, false, false, false, false, false, false, false, false}
  39. assert.Equal(t, g, b.snapshot())
  40. // Receive two again - it will fail
  41. assert.False(t, b.Check(l, 2))
  42. assert.False(t, b.Update(l, 2))
  43. assert.EqualValues(t, 2, b.current)
  44. // Jump ahead to 25, which clears the window and sets slot 25%16 = 9.
  45. assert.True(t, b.Check(l, 25))
  46. assert.True(t, b.Update(l, 25))
  47. assert.EqualValues(t, 25, b.current)
  48. g = []bool{false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false}
  49. assert.Equal(t, g, b.snapshot())
  50. // Mark 24, which is in window (current 25, length 16, window covers [10,25]).
  51. assert.True(t, b.Check(l, 24))
  52. assert.True(t, b.Update(l, 24))
  53. assert.EqualValues(t, 25, b.current)
  54. g = []bool{false, false, false, false, false, false, false, false, true, true, false, false, false, false, false, false}
  55. assert.Equal(t, g, b.snapshot())
  56. // Mark 5, not allowed because 5 <= current-length (25-16=9).
  57. assert.False(t, b.Check(l, 5))
  58. assert.False(t, b.Update(l, 5))
  59. assert.EqualValues(t, 25, b.current)
  60. g = []bool{false, false, false, false, false, false, false, false, true, true, false, false, false, false, false, false}
  61. assert.Equal(t, g, b.snapshot())
  62. // Make sure we handle wrapping around once to the same slot. With
  63. // length=16, packets 1 and 17 share slot 1.
  64. b = NewBits(16)
  65. assert.True(t, b.Update(l, 1))
  66. assert.True(t, b.Update(l, 17))
  67. assert.Equal(t, []bool{false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false}, b.snapshot())
  68. // Walk through a few windows in order
  69. b = NewBits(16)
  70. for i := uint64(1); i <= 100; i++ {
  71. assert.True(t, b.Check(l, i), "Error while checking %v", i)
  72. assert.True(t, b.Update(l, i), "Error while updating %v", i)
  73. }
  74. assert.False(t, b.Check(l, 1), "Out of window check")
  75. }
  76. func TestBitsLargeJumps(t *testing.T) {
  77. l := test.NewLogger()
  78. // length=16. Update(55) from current=0:
  79. // warmup, per-bit loop sees no n>16 with unset bits (slot 0 was set by
  80. // NewBits and gets re-evaluated when n=16; n=16 is not strictly > 16),
  81. // so the loop contributes 0. The jump exceeds the window so we record
  82. // 55 - 0 - 16 = 39 packets fell out the back.
  83. b := NewBits(16)
  84. b.lostCounter.Clear()
  85. assert.True(t, b.Update(l, 55))
  86. assert.Equal(t, int64(39), b.lostCounter.Count())
  87. // Update(100): clears 16 slots starting at slot 56%16=8. Only slot 7 (for
  88. // packet 55) was set, so 16 - 1 = 15 evicted slots had unset bits.
  89. // Plus 100 - 55 - 16 = 29 packets fell past the window. Total 44.
  90. assert.True(t, b.Update(l, 100))
  91. assert.Equal(t, int64(39+44), b.lostCounter.Count())
  92. // Update(200): same shape: 16 - 1 = 15 evicted unset, plus 200 - 100 - 16 = 84 past window. Total 99.
  93. assert.True(t, b.Update(l, 200))
  94. assert.Equal(t, int64(39+44+99), b.lostCounter.Count())
  95. }
  96. func TestBitsDupeCounter(t *testing.T) {
  97. l := test.NewLogger()
  98. b := NewBits(16)
  99. b.lostCounter.Clear()
  100. b.dupeCounter.Clear()
  101. b.outOfWindowCounter.Clear()
  102. assert.True(t, b.Update(l, 1))
  103. assert.Equal(t, int64(0), b.dupeCounter.Count())
  104. assert.False(t, b.Update(l, 1))
  105. assert.Equal(t, int64(1), b.dupeCounter.Count())
  106. assert.True(t, b.Update(l, 2))
  107. assert.Equal(t, int64(1), b.dupeCounter.Count())
  108. assert.True(t, b.Update(l, 3))
  109. assert.Equal(t, int64(1), b.dupeCounter.Count())
  110. assert.False(t, b.Update(l, 1))
  111. assert.Equal(t, int64(0), b.lostCounter.Count())
  112. assert.Equal(t, int64(2), b.dupeCounter.Count())
  113. assert.Equal(t, int64(0), b.outOfWindowCounter.Count())
  114. }
  115. func TestBitsOutOfWindowCounter(t *testing.T) {
  116. l := test.NewLogger()
  117. b := NewBits(16)
  118. b.lostCounter.Clear()
  119. b.dupeCounter.Clear()
  120. b.outOfWindowCounter.Clear()
  121. // Jump to 20 (warmup branch + 4 past-window packets).
  122. assert.True(t, b.Update(l, 20))
  123. assert.Equal(t, int64(0), b.outOfWindowCounter.Count())
  124. // 9 single-step advances, each evicts a slot whose bit was cleared during
  125. // the jump above and whose value was never seen, so each contributes 1
  126. // to lostCounter.
  127. for n := uint64(21); n <= 29; n++ {
  128. assert.True(t, b.Update(l, n))
  129. }
  130. assert.Equal(t, int64(0), b.outOfWindowCounter.Count())
  131. // 0 is below current-length (29-16=13) so it falls outside the window.
  132. assert.False(t, b.Update(l, 0))
  133. assert.Equal(t, int64(1), b.outOfWindowCounter.Count())
  134. // 4 from the Update(20) jump + 9 from 21..29.
  135. assert.Equal(t, int64(13), b.lostCounter.Count())
  136. assert.Equal(t, int64(0), b.dupeCounter.Count())
  137. assert.Equal(t, int64(1), b.outOfWindowCounter.Count())
  138. }
  139. func TestBitsLostCounter(t *testing.T) {
  140. l := test.NewLogger()
  141. b := NewBits(16)
  142. b.lostCounter.Clear()
  143. b.dupeCounter.Clear()
  144. b.outOfWindowCounter.Clear()
  145. // Walk 20..29 like the original, just with a bigger window. Same
  146. // reasoning as TestBitsOutOfWindowCounter: 4 past-window from Update(20),
  147. // then 9 more from the unit advances.
  148. for n := uint64(20); n <= 29; n++ {
  149. assert.True(t, b.Update(l, n))
  150. }
  151. assert.Equal(t, int64(13), b.lostCounter.Count())
  152. assert.Equal(t, int64(0), b.dupeCounter.Count())
  153. assert.Equal(t, int64(0), b.outOfWindowCounter.Count())
  154. b = NewBits(16)
  155. b.lostCounter.Clear()
  156. b.dupeCounter.Clear()
  157. b.outOfWindowCounter.Clear()
  158. // Update(15) clears the warmup window (no lost), sets slot 15.
  159. assert.True(t, b.Update(l, 15))
  160. assert.Equal(t, int64(0), b.lostCounter.Count())
  161. // Update(16): slot 0 was already set (NewBits seeded it), and 16 is not
  162. // strictly > length, so nothing is recorded as lost.
  163. assert.True(t, b.Update(l, 16))
  164. assert.Equal(t, int64(0), b.lostCounter.Count())
  165. // Update(17): we jumped straight from 0 to 15, so slot 1 was cleared
  166. // (and never re-set). 17 > 16 is past warmup, so packet 1 is recorded lost.
  167. assert.True(t, b.Update(l, 17))
  168. assert.Equal(t, int64(1), b.lostCounter.Count())
  169. // Fill in 18..30 in single steps. Each i evicts slot i%16. Slots 2..14
  170. // were all cleared during Update(15), and we never re-set any of them,
  171. // so each i in 18..30 is a fresh lost packet — 13 more.
  172. for n := uint64(18); n <= 30; n++ {
  173. assert.True(t, b.Update(l, n))
  174. }
  175. assert.Equal(t, int64(14), b.lostCounter.Count())
  176. // Jump ahead by exactly one window size.
  177. assert.True(t, b.Update(l, 46))
  178. // end = min(46, 30+16) = 46, count = 16, all slots cleared. Before the
  179. // jump every slot 0..15 had been set (Update(15), (16), (17), 18..30),
  180. // so wasSet=16 and 46 == current+length means no past-window slack:
  181. // lost contribution = 0.
  182. assert.Equal(t, int64(14), b.lostCounter.Count())
  183. // Walk 47..55. The Update(46) jump cleared every slot, so only slot 14
  184. // (for packet 46) is set when we start. Each subsequent unit step lands
  185. // on a slot that was cleared and is past warmup, so it counts as lost.
  186. // 9 more = 23.
  187. for n := uint64(47); n <= 55; n++ {
  188. assert.True(t, b.Update(l, n))
  189. }
  190. assert.Equal(t, int64(23), b.lostCounter.Count())
  191. // Jump ahead by two windows: clears the window plus past-window loss.
  192. assert.True(t, b.Update(l, 87))
  193. // current=55, length=16. end = min(87, 71) = 71. count=16, all slots
  194. // cleared. Slots set before the clear are slots 14,15,0..7 (10 total).
  195. // Lost from clear = 16 - 10 = 6. Past window: 87 - 55 - 16 = 16. +22.
  196. assert.Equal(t, int64(45), b.lostCounter.Count())
  197. assert.Equal(t, int64(0), b.dupeCounter.Count())
  198. assert.Equal(t, int64(0), b.outOfWindowCounter.Count())
  199. }
  200. func TestBitsLostCounterIssue1(t *testing.T) {
  201. l := test.NewLogger()
  202. b := NewBits(16)
  203. b.lostCounter.Clear()
  204. b.dupeCounter.Clear()
  205. b.outOfWindowCounter.Clear()
  206. // Receive 4, backfill 1, then 9, 2, 3, 5, 6, 7 (skip 8), 10, 11, 14.
  207. // Then jump to 25 — slot 25%16=9 is being evicted, but it had been set
  208. // (we received packet 9), so no spurious lost increment. The original
  209. // regression was about double-counting a missing packet when its slot
  210. // got cleared on a jump. With the jump path now using clearRange's
  211. // word-level wasSet count, the same semantics hold.
  212. assert.True(t, b.Update(l, 4))
  213. assert.Equal(t, int64(0), b.lostCounter.Count())
  214. assert.True(t, b.Update(l, 1))
  215. assert.Equal(t, int64(0), b.lostCounter.Count())
  216. assert.True(t, b.Update(l, 9))
  217. assert.Equal(t, int64(0), b.lostCounter.Count())
  218. assert.True(t, b.Update(l, 2))
  219. assert.Equal(t, int64(0), b.lostCounter.Count())
  220. assert.True(t, b.Update(l, 3))
  221. assert.Equal(t, int64(0), b.lostCounter.Count())
  222. assert.True(t, b.Update(l, 5))
  223. assert.Equal(t, int64(0), b.lostCounter.Count())
  224. assert.True(t, b.Update(l, 6))
  225. assert.Equal(t, int64(0), b.lostCounter.Count())
  226. assert.True(t, b.Update(l, 7))
  227. assert.Equal(t, int64(0), b.lostCounter.Count())
  228. // Skip packet 8.
  229. assert.True(t, b.Update(l, 10))
  230. assert.Equal(t, int64(0), b.lostCounter.Count())
  231. assert.True(t, b.Update(l, 11))
  232. assert.Equal(t, int64(0), b.lostCounter.Count())
  233. assert.True(t, b.Update(l, 14))
  234. assert.Equal(t, int64(0), b.lostCounter.Count())
  235. // Jump to 25. With length=16, slot 25%16=9 corresponds to packet 9
  236. // (which we DID receive), so its bit is set and no lost++ from that
  237. // eviction. The trace below shows the only loss is packet 8.
  238. assert.True(t, b.Update(l, 25))
  239. // current was 14, i=25. end=min(25,30)=25. count=11. startPos=15.
  240. // steady? current=14<16, so warmup branch: per-bit n=15..25, count those
  241. // with !get(n) AND n>16. n=17..25 are >16. Among slots 17%16=1..25%16=9
  242. // did we set slots 1..9 (packets 1..9)? Yes for all but slot 8 (packet 8
  243. // was skipped). n=24 maps to slot 8 which is FALSE → lost++. All other
  244. // n in 17..25 map to slots that are set. n=16 is not strictly > 16. So
  245. // lost = 1.
  246. assert.Equal(t, int64(1), b.lostCounter.Count())
  247. // Fill in 12, 13, 15, 16. Each is below current=25 (in-window). 16 must
  248. // recheck slot 0 — it was set by NewBits and then cleared by the
  249. // Update(25) jump, so 16 backfills cleanly.
  250. assert.True(t, b.Update(l, 12))
  251. assert.Equal(t, int64(1), b.lostCounter.Count())
  252. assert.True(t, b.Update(l, 13))
  253. assert.Equal(t, int64(1), b.lostCounter.Count())
  254. assert.True(t, b.Update(l, 15))
  255. assert.Equal(t, int64(1), b.lostCounter.Count())
  256. assert.True(t, b.Update(l, 16))
  257. assert.Equal(t, int64(1), b.lostCounter.Count())
  258. // We missed packet 8 above and that loss is still recorded once, never
  259. // double-counted, never zeroed.
  260. assert.Equal(t, int64(1), b.lostCounter.Count())
  261. assert.Equal(t, int64(0), b.dupeCounter.Count())
  262. assert.Equal(t, int64(0), b.outOfWindowCounter.Count())
  263. }
  264. // TestBitsWarmupOvershoot exercises the jump path's warmup arm with an
  265. // overshoot past one full window. NewBits leaves current=0 with only slot 0
  266. // "set" by the marker. Jumping straight to length+k must (a) clear every
  267. // slot the jump straddles, (b) count only past-window slack (not the
  268. // in-window slots, which never had a "lost" tenant during warmup), and
  269. // (c) leave the cursor at the new counter so subsequent unit advances
  270. // count from steady state. The marker bit at slot 0 is irrelevant once
  271. // current >= length.
  272. func TestBitsWarmupOvershoot(t *testing.T) {
  273. l := test.NewLogger()
  274. b := NewBits(16)
  275. b.lostCounter.Clear()
  276. // Jump from current=0 to i=20 (length=16, overshoot=4).
  277. // Warmup arm: counts slots in [1..16] where bit unset and n>length.
  278. // Only n=16 was unset and >length: but slot 16%16=0 is the marker,
  279. // so b.get(16) reads bits[0]=1 and skips. Result: 0 lost from the loop.
  280. // Past-window: i - current - length = 20 - 0 - 16 = 4 lost.
  281. assert.True(t, b.Update(l, 20))
  282. assert.Equal(t, int64(4), b.lostCounter.Count())
  283. assert.Equal(t, uint64(20), b.current)
  284. // Steady state now (current=20 >= length=16). Unit advance to 21
  285. // stomps slot 21%16=5, which was cleared by the jump and not reset,
  286. // so this is +1 lost.
  287. assert.True(t, b.Update(l, 21))
  288. assert.Equal(t, int64(5), b.lostCounter.Count())
  289. }
  290. // TestBitsCheckAcrossWarmupBoundary pins the underflow trick in Check's
  291. // in-window clause. While in warmup, b.current-b.length underflows uint64
  292. // to a huge value so the first OR-clause is always false; the second
  293. // clause (i < length && current < length) carries the in-window check.
  294. // Once current >= length the regimes flip cleanly.
  295. func TestBitsCheckAcrossWarmupBoundary(t *testing.T) {
  296. l := test.NewLogger()
  297. b := NewBits(16)
  298. // Warmup: current=0. Check(0) must read the marker (set) and return false.
  299. assert.False(t, b.Check(l, 0), "marker slot should look already-received")
  300. // Warmup: any 0 < i < length is in-window and unset → accepted.
  301. for i := uint64(1); i < 16; i++ {
  302. assert.True(t, b.Check(l, i), "warmup in-window i=%d should be accepted", i)
  303. }
  304. // Warmup: i >= length but > current is "next number" so accepted.
  305. assert.True(t, b.Check(l, 16))
  306. assert.True(t, b.Check(l, 1_000_000))
  307. // Cross into steady state.
  308. assert.True(t, b.Update(l, 100))
  309. // Now current=100, length=16. In-window range is [85..100].
  310. // 84 is just outside: the underflow clause activates; 84 > 100-16=84 is false.
  311. // And the warmup clause is false (current >= length). So out of window.
  312. assert.False(t, b.Check(l, 84))
  313. // 85 sits at the boundary. 85 > 84 is true → in window, unset → accept.
  314. assert.True(t, b.Check(l, 85))
  315. // 100 is current itself; not strictly greater, in-window, but already set.
  316. assert.False(t, b.Check(l, 100))
  317. // Way out: clearly out of window.
  318. assert.False(t, b.Check(l, 50))
  319. }
  320. // TestBitsMarkerInvariant verifies the seeded bits[0]=1 marker behaves
  321. // correctly across warmup and beyond. Update should never clear the marker
  322. // during warmup (clearRange skips position 0 when startPos=1), and once
  323. // current >= length the marker is no longer consulted by Check/Update on
  324. // the live path — but it must still report counter 0 as a duplicate while
  325. // we are in warmup.
  326. func TestBitsMarkerInvariant(t *testing.T) {
  327. l := test.NewLogger()
  328. b := NewBits(8)
  329. // Counter 0 is the seeded marker; Check sees it as already received.
  330. assert.False(t, b.Check(l, 0))
  331. // Update(0) at current=0 hits the duplicate branch.
  332. b.dupeCounter.Clear()
  333. assert.False(t, b.Update(l, 0))
  334. assert.Equal(t, int64(1), b.dupeCounter.Count())
  335. // Walk forward through warmup; the marker must remain set.
  336. for n := uint64(1); n <= 7; n++ {
  337. assert.True(t, b.Update(l, n))
  338. }
  339. // Position 0 (the marker) should still read as set because we never
  340. // cleared it; Update(0) still looks like a duplicate.
  341. assert.False(t, b.Check(l, 0))
  342. // Cross into steady state with a unit advance to 8: pos=0, evicts the
  343. // marker bit. The lost-counter guard (i > b.length) is false (8 == 8),
  344. // so this advance does NOT charge a lost packet — exactly what the
  345. // marker is there to prevent.
  346. b.lostCounter.Clear()
  347. assert.True(t, b.Update(l, 8))
  348. assert.Equal(t, int64(0), b.lostCounter.Count())
  349. // The slot at pos 0 is now occupied by counter 8.
  350. assert.False(t, b.Check(l, 8))
  351. }
  352. // BenchmarkBitsUpdateInOrder is the steady-state hot path: each call is
  353. // i == current+1.
  354. func BenchmarkBitsUpdateInOrder(b *testing.B) {
  355. l := test.NewLogger()
  356. z := NewBits(16384)
  357. for n := 0; n < b.N; n++ {
  358. z.Update(l, uint64(n)+1)
  359. }
  360. }
  361. // BenchmarkBitsUpdateReorder simulates light reorder within the window:
  362. // every other packet arrives one slot behind its predecessor (forces the
  363. // in-window backfill branch).
  364. func BenchmarkBitsUpdateReorder(b *testing.B) {
  365. l := test.NewLogger()
  366. z := NewBits(16384)
  367. for n := 0; n < b.N; n++ {
  368. base := uint64(n) * 2
  369. z.Update(l, base+2)
  370. z.Update(l, base+1)
  371. }
  372. }
  373. // BenchmarkBitsUpdateLargeJumps stresses the clearRange word-level path.
  374. func BenchmarkBitsUpdateLargeJumps(b *testing.B) {
  375. l := test.NewLogger()
  376. z := NewBits(16384)
  377. for n := 0; n < b.N; n++ {
  378. z.Update(l, uint64(n+1)*1000)
  379. }
  380. }