tigerbeetle 0.0.40 → 0.17.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (293) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +0 -25
  3. data/README.md +670 -80
  4. data/docs/migration.md +201 -0
  5. data/sig/tigerbeetle.rbs +271 -0
  6. data/src/ext/tigerbeetle/extconf.rb +47 -0
  7. data/src/ext/tigerbeetle/lib/aarch64-linux-gnu.2.27/libtb_client.so +0 -0
  8. data/src/ext/tigerbeetle/lib/aarch64-linux-musl/libtb_client.so +0 -0
  9. data/src/ext/tigerbeetle/lib/aarch64-macos/libtb_client.dylib +0 -0
  10. data/src/ext/tigerbeetle/lib/x86_64-linux-gnu.2.27/libtb_client.so +0 -0
  11. data/src/ext/tigerbeetle/lib/x86_64-linux-musl/libtb_client.so +0 -0
  12. data/src/ext/tigerbeetle/lib/x86_64-macos/libtb_client.dylib +0 -0
  13. data/src/ext/tigerbeetle/lib/x86_64-windows/tb_client.dll +0 -0
  14. data/src/ext/tigerbeetle/rb_tb_gen.h +458 -0
  15. data/{ext/tb_client/tigerbeetle/src/clients/rust/assets → src/ext/tigerbeetle}/tb_client.h +18 -16
  16. data/src/ext/tigerbeetle/tigerbeetle.c +310 -0
  17. data/src/tigerbeetle/bindings.rb +347 -0
  18. data/src/tigerbeetle/client.rb +129 -0
  19. data/src/tigerbeetle/completion_dispatcher.rb +108 -0
  20. data/src/tigerbeetle/id.rb +40 -0
  21. data/src/tigerbeetle/tb.rb +3 -0
  22. data/src/tigerbeetle/version.rb +3 -0
  23. data/src/tigerbeetle.rb +39 -0
  24. metadata +33 -350
  25. data/CHANGELOG.md +0 -162
  26. data/ext/tb_client/extconf.rb +0 -41
  27. data/ext/tb_client/tigerbeetle/LICENSE +0 -177
  28. data/ext/tb_client/tigerbeetle/build.zig +0 -2296
  29. data/ext/tb_client/tigerbeetle/src/aof.zig +0 -1000
  30. data/ext/tb_client/tigerbeetle/src/build/fetch.zig +0 -112
  31. data/ext/tb_client/tigerbeetle/src/build_multiversion.zig +0 -808
  32. data/ext/tb_client/tigerbeetle/src/cdc/amqp/protocol.zig +0 -1283
  33. data/ext/tb_client/tigerbeetle/src/cdc/amqp/spec.zig +0 -1704
  34. data/ext/tb_client/tigerbeetle/src/cdc/amqp/types.zig +0 -341
  35. data/ext/tb_client/tigerbeetle/src/cdc/amqp.zig +0 -1450
  36. data/ext/tb_client/tigerbeetle/src/cdc/runner.zig +0 -1659
  37. data/ext/tb_client/tigerbeetle/src/clients/c/samples/main.c +0 -406
  38. data/ext/tb_client/tigerbeetle/src/clients/c/tb_client/context.zig +0 -1092
  39. data/ext/tb_client/tigerbeetle/src/clients/c/tb_client/echo_client.zig +0 -286
  40. data/ext/tb_client/tigerbeetle/src/clients/c/tb_client/packet.zig +0 -158
  41. data/ext/tb_client/tigerbeetle/src/clients/c/tb_client/signal.zig +0 -229
  42. data/ext/tb_client/tigerbeetle/src/clients/c/tb_client/signal_fuzz.zig +0 -110
  43. data/ext/tb_client/tigerbeetle/src/clients/c/tb_client.h +0 -386
  44. data/ext/tb_client/tigerbeetle/src/clients/c/tb_client.zig +0 -34
  45. data/ext/tb_client/tigerbeetle/src/clients/c/tb_client_exports.zig +0 -281
  46. data/ext/tb_client/tigerbeetle/src/clients/c/tb_client_header.zig +0 -312
  47. data/ext/tb_client/tigerbeetle/src/clients/c/tb_client_header_test.zig +0 -138
  48. data/ext/tb_client/tigerbeetle/src/clients/c/test.zig +0 -466
  49. data/ext/tb_client/tigerbeetle/src/clients/docs_samples.zig +0 -157
  50. data/ext/tb_client/tigerbeetle/src/clients/docs_types.zig +0 -90
  51. data/ext/tb_client/tigerbeetle/src/clients/dotnet/ci.zig +0 -203
  52. data/ext/tb_client/tigerbeetle/src/clients/dotnet/docs.zig +0 -79
  53. data/ext/tb_client/tigerbeetle/src/clients/dotnet/dotnet_bindings.zig +0 -542
  54. data/ext/tb_client/tigerbeetle/src/clients/go/ci.zig +0 -109
  55. data/ext/tb_client/tigerbeetle/src/clients/go/docs.zig +0 -86
  56. data/ext/tb_client/tigerbeetle/src/clients/go/go_bindings.zig +0 -370
  57. data/ext/tb_client/tigerbeetle/src/clients/go/pkg/native/tb_client.h +0 -386
  58. data/ext/tb_client/tigerbeetle/src/clients/java/ci.zig +0 -167
  59. data/ext/tb_client/tigerbeetle/src/clients/java/docs.zig +0 -126
  60. data/ext/tb_client/tigerbeetle/src/clients/java/java_bindings.zig +0 -996
  61. data/ext/tb_client/tigerbeetle/src/clients/java/src/client.zig +0 -748
  62. data/ext/tb_client/tigerbeetle/src/clients/java/src/jni.zig +0 -3238
  63. data/ext/tb_client/tigerbeetle/src/clients/java/src/jni_tests.zig +0 -1718
  64. data/ext/tb_client/tigerbeetle/src/clients/java/src/jni_thread_cleaner.zig +0 -190
  65. data/ext/tb_client/tigerbeetle/src/clients/node/ci.zig +0 -104
  66. data/ext/tb_client/tigerbeetle/src/clients/node/docs.zig +0 -75
  67. data/ext/tb_client/tigerbeetle/src/clients/node/node.zig +0 -522
  68. data/ext/tb_client/tigerbeetle/src/clients/node/node_bindings.zig +0 -267
  69. data/ext/tb_client/tigerbeetle/src/clients/node/src/c.zig +0 -3
  70. data/ext/tb_client/tigerbeetle/src/clients/node/src/translate.zig +0 -379
  71. data/ext/tb_client/tigerbeetle/src/clients/python/ci.zig +0 -131
  72. data/ext/tb_client/tigerbeetle/src/clients/python/docs.zig +0 -63
  73. data/ext/tb_client/tigerbeetle/src/clients/python/python_bindings.zig +0 -588
  74. data/ext/tb_client/tigerbeetle/src/clients/rust/ci.zig +0 -73
  75. data/ext/tb_client/tigerbeetle/src/clients/rust/docs.zig +0 -106
  76. data/ext/tb_client/tigerbeetle/src/clients/rust/rust_bindings.zig +0 -305
  77. data/ext/tb_client/tigerbeetle/src/config.zig +0 -296
  78. data/ext/tb_client/tigerbeetle/src/constants.zig +0 -790
  79. data/ext/tb_client/tigerbeetle/src/copyhound.zig +0 -202
  80. data/ext/tb_client/tigerbeetle/src/counting_allocator.zig +0 -72
  81. data/ext/tb_client/tigerbeetle/src/direction.zig +0 -120
  82. data/ext/tb_client/tigerbeetle/src/docs_website/build.zig +0 -158
  83. data/ext/tb_client/tigerbeetle/src/docs_website/src/content.zig +0 -156
  84. data/ext/tb_client/tigerbeetle/src/docs_website/src/docs.zig +0 -252
  85. data/ext/tb_client/tigerbeetle/src/docs_website/src/file_checker.zig +0 -313
  86. data/ext/tb_client/tigerbeetle/src/docs_website/src/html.zig +0 -87
  87. data/ext/tb_client/tigerbeetle/src/docs_website/src/page_writer.zig +0 -63
  88. data/ext/tb_client/tigerbeetle/src/docs_website/src/redirects.zig +0 -47
  89. data/ext/tb_client/tigerbeetle/src/docs_website/src/search_index_writer.zig +0 -28
  90. data/ext/tb_client/tigerbeetle/src/docs_website/src/service_worker_writer.zig +0 -61
  91. data/ext/tb_client/tigerbeetle/src/docs_website/src/single_page_writer.zig +0 -169
  92. data/ext/tb_client/tigerbeetle/src/docs_website/src/website.zig +0 -46
  93. data/ext/tb_client/tigerbeetle/src/ewah.zig +0 -445
  94. data/ext/tb_client/tigerbeetle/src/ewah_benchmark.zig +0 -128
  95. data/ext/tb_client/tigerbeetle/src/ewah_fuzz.zig +0 -171
  96. data/ext/tb_client/tigerbeetle/src/fuzz_tests.zig +0 -179
  97. data/ext/tb_client/tigerbeetle/src/integration_tests.zig +0 -662
  98. data/ext/tb_client/tigerbeetle/src/io/common.zig +0 -155
  99. data/ext/tb_client/tigerbeetle/src/io/darwin.zig +0 -1093
  100. data/ext/tb_client/tigerbeetle/src/io/linux.zig +0 -1880
  101. data/ext/tb_client/tigerbeetle/src/io/test.zig +0 -1005
  102. data/ext/tb_client/tigerbeetle/src/io/windows.zig +0 -1598
  103. data/ext/tb_client/tigerbeetle/src/io.zig +0 -34
  104. data/ext/tb_client/tigerbeetle/src/iops.zig +0 -134
  105. data/ext/tb_client/tigerbeetle/src/list.zig +0 -236
  106. data/ext/tb_client/tigerbeetle/src/lsm/binary_search.zig +0 -848
  107. data/ext/tb_client/tigerbeetle/src/lsm/binary_search_benchmark.zig +0 -179
  108. data/ext/tb_client/tigerbeetle/src/lsm/cache_map.zig +0 -424
  109. data/ext/tb_client/tigerbeetle/src/lsm/cache_map_fuzz.zig +0 -420
  110. data/ext/tb_client/tigerbeetle/src/lsm/compaction.zig +0 -2114
  111. data/ext/tb_client/tigerbeetle/src/lsm/composite_key.zig +0 -185
  112. data/ext/tb_client/tigerbeetle/src/lsm/forest.zig +0 -1146
  113. data/ext/tb_client/tigerbeetle/src/lsm/forest_fuzz.zig +0 -1102
  114. data/ext/tb_client/tigerbeetle/src/lsm/forest_table_iterator.zig +0 -200
  115. data/ext/tb_client/tigerbeetle/src/lsm/groove.zig +0 -1495
  116. data/ext/tb_client/tigerbeetle/src/lsm/k_way_merge.zig +0 -739
  117. data/ext/tb_client/tigerbeetle/src/lsm/k_way_merge_benchmark.zig +0 -166
  118. data/ext/tb_client/tigerbeetle/src/lsm/manifest.zig +0 -754
  119. data/ext/tb_client/tigerbeetle/src/lsm/manifest_level.zig +0 -1294
  120. data/ext/tb_client/tigerbeetle/src/lsm/manifest_level_fuzz.zig +0 -510
  121. data/ext/tb_client/tigerbeetle/src/lsm/manifest_log.zig +0 -1241
  122. data/ext/tb_client/tigerbeetle/src/lsm/manifest_log_fuzz.zig +0 -628
  123. data/ext/tb_client/tigerbeetle/src/lsm/node_pool.zig +0 -247
  124. data/ext/tb_client/tigerbeetle/src/lsm/scan_buffer.zig +0 -116
  125. data/ext/tb_client/tigerbeetle/src/lsm/scan_builder.zig +0 -543
  126. data/ext/tb_client/tigerbeetle/src/lsm/scan_fuzz.zig +0 -938
  127. data/ext/tb_client/tigerbeetle/src/lsm/scan_lookup.zig +0 -293
  128. data/ext/tb_client/tigerbeetle/src/lsm/scan_merge.zig +0 -359
  129. data/ext/tb_client/tigerbeetle/src/lsm/scan_range.zig +0 -99
  130. data/ext/tb_client/tigerbeetle/src/lsm/scan_state.zig +0 -17
  131. data/ext/tb_client/tigerbeetle/src/lsm/scan_tree.zig +0 -962
  132. data/ext/tb_client/tigerbeetle/src/lsm/schema.zig +0 -617
  133. data/ext/tb_client/tigerbeetle/src/lsm/scratch_memory.zig +0 -84
  134. data/ext/tb_client/tigerbeetle/src/lsm/segmented_array.zig +0 -1500
  135. data/ext/tb_client/tigerbeetle/src/lsm/segmented_array_benchmark.zig +0 -149
  136. data/ext/tb_client/tigerbeetle/src/lsm/segmented_array_fuzz.zig +0 -7
  137. data/ext/tb_client/tigerbeetle/src/lsm/set_associative_cache.zig +0 -865
  138. data/ext/tb_client/tigerbeetle/src/lsm/table.zig +0 -607
  139. data/ext/tb_client/tigerbeetle/src/lsm/table_memory.zig +0 -843
  140. data/ext/tb_client/tigerbeetle/src/lsm/table_value_iterator.zig +0 -90
  141. data/ext/tb_client/tigerbeetle/src/lsm/timestamp_range.zig +0 -40
  142. data/ext/tb_client/tigerbeetle/src/lsm/tree.zig +0 -629
  143. data/ext/tb_client/tigerbeetle/src/lsm/tree_fuzz.zig +0 -933
  144. data/ext/tb_client/tigerbeetle/src/lsm/zig_zag_merge.zig +0 -534
  145. data/ext/tb_client/tigerbeetle/src/message_buffer.zig +0 -469
  146. data/ext/tb_client/tigerbeetle/src/message_bus.zig +0 -1219
  147. data/ext/tb_client/tigerbeetle/src/message_bus_fuzz.zig +0 -936
  148. data/ext/tb_client/tigerbeetle/src/message_pool.zig +0 -343
  149. data/ext/tb_client/tigerbeetle/src/multiversion.zig +0 -2195
  150. data/ext/tb_client/tigerbeetle/src/queue.zig +0 -390
  151. data/ext/tb_client/tigerbeetle/src/repl/completion.zig +0 -201
  152. data/ext/tb_client/tigerbeetle/src/repl/parser.zig +0 -1356
  153. data/ext/tb_client/tigerbeetle/src/repl/terminal.zig +0 -496
  154. data/ext/tb_client/tigerbeetle/src/repl.zig +0 -1034
  155. data/ext/tb_client/tigerbeetle/src/scripts/amqp.zig +0 -973
  156. data/ext/tb_client/tigerbeetle/src/scripts/cfo.zig +0 -1866
  157. data/ext/tb_client/tigerbeetle/src/scripts/changelog.zig +0 -304
  158. data/ext/tb_client/tigerbeetle/src/scripts/ci.zig +0 -227
  159. data/ext/tb_client/tigerbeetle/src/scripts/client_readmes.zig +0 -658
  160. data/ext/tb_client/tigerbeetle/src/scripts/devhub.zig +0 -466
  161. data/ext/tb_client/tigerbeetle/src/scripts/release.zig +0 -1058
  162. data/ext/tb_client/tigerbeetle/src/scripts.zig +0 -105
  163. data/ext/tb_client/tigerbeetle/src/shell.zig +0 -1195
  164. data/ext/tb_client/tigerbeetle/src/stack.zig +0 -260
  165. data/ext/tb_client/tigerbeetle/src/state_machine/auditor.zig +0 -911
  166. data/ext/tb_client/tigerbeetle/src/state_machine/workload.zig +0 -2079
  167. data/ext/tb_client/tigerbeetle/src/state_machine.zig +0 -4872
  168. data/ext/tb_client/tigerbeetle/src/state_machine_fuzz.zig +0 -288
  169. data/ext/tb_client/tigerbeetle/src/state_machine_tests.zig +0 -3128
  170. data/ext/tb_client/tigerbeetle/src/static_allocator.zig +0 -82
  171. data/ext/tb_client/tigerbeetle/src/stdx/bit_set.zig +0 -157
  172. data/ext/tb_client/tigerbeetle/src/stdx/bounded_array.zig +0 -292
  173. data/ext/tb_client/tigerbeetle/src/stdx/debug.zig +0 -65
  174. data/ext/tb_client/tigerbeetle/src/stdx/flags.zig +0 -1414
  175. data/ext/tb_client/tigerbeetle/src/stdx/huge_page_allocator.zig +0 -115
  176. data/ext/tb_client/tigerbeetle/src/stdx/mlock.zig +0 -92
  177. data/ext/tb_client/tigerbeetle/src/stdx/prng.zig +0 -677
  178. data/ext/tb_client/tigerbeetle/src/stdx/radix.zig +0 -336
  179. data/ext/tb_client/tigerbeetle/src/stdx/ring_buffer.zig +0 -511
  180. data/ext/tb_client/tigerbeetle/src/stdx/sort_test.zig +0 -112
  181. data/ext/tb_client/tigerbeetle/src/stdx/stdx.zig +0 -1163
  182. data/ext/tb_client/tigerbeetle/src/stdx/testing/low_level_hash_vectors.zig +0 -142
  183. data/ext/tb_client/tigerbeetle/src/stdx/testing/snaptest.zig +0 -361
  184. data/ext/tb_client/tigerbeetle/src/stdx/time_units.zig +0 -275
  185. data/ext/tb_client/tigerbeetle/src/stdx/unshare.zig +0 -295
  186. data/ext/tb_client/tigerbeetle/src/stdx/vendored/aegis.zig +0 -436
  187. data/ext/tb_client/tigerbeetle/src/stdx/windows.zig +0 -48
  188. data/ext/tb_client/tigerbeetle/src/stdx/zipfian.zig +0 -402
  189. data/ext/tb_client/tigerbeetle/src/storage.zig +0 -489
  190. data/ext/tb_client/tigerbeetle/src/storage_fuzz.zig +0 -180
  191. data/ext/tb_client/tigerbeetle/src/testing/bench.zig +0 -146
  192. data/ext/tb_client/tigerbeetle/src/testing/cluster/grid_checker.zig +0 -53
  193. data/ext/tb_client/tigerbeetle/src/testing/cluster/journal_checker.zig +0 -61
  194. data/ext/tb_client/tigerbeetle/src/testing/cluster/manifest_checker.zig +0 -76
  195. data/ext/tb_client/tigerbeetle/src/testing/cluster/message_bus.zig +0 -110
  196. data/ext/tb_client/tigerbeetle/src/testing/cluster/network.zig +0 -412
  197. data/ext/tb_client/tigerbeetle/src/testing/cluster/state_checker.zig +0 -331
  198. data/ext/tb_client/tigerbeetle/src/testing/cluster/storage_checker.zig +0 -458
  199. data/ext/tb_client/tigerbeetle/src/testing/cluster.zig +0 -1198
  200. data/ext/tb_client/tigerbeetle/src/testing/exhaustigen.zig +0 -128
  201. data/ext/tb_client/tigerbeetle/src/testing/fixtures.zig +0 -181
  202. data/ext/tb_client/tigerbeetle/src/testing/fuzz.zig +0 -144
  203. data/ext/tb_client/tigerbeetle/src/testing/id.zig +0 -97
  204. data/ext/tb_client/tigerbeetle/src/testing/io.zig +0 -317
  205. data/ext/tb_client/tigerbeetle/src/testing/marks.zig +0 -126
  206. data/ext/tb_client/tigerbeetle/src/testing/packet_simulator.zig +0 -533
  207. data/ext/tb_client/tigerbeetle/src/testing/reply_sequence.zig +0 -154
  208. data/ext/tb_client/tigerbeetle/src/testing/state_machine.zig +0 -389
  209. data/ext/tb_client/tigerbeetle/src/testing/storage.zig +0 -1247
  210. data/ext/tb_client/tigerbeetle/src/testing/table.zig +0 -249
  211. data/ext/tb_client/tigerbeetle/src/testing/time.zig +0 -98
  212. data/ext/tb_client/tigerbeetle/src/testing/tmp_tigerbeetle.zig +0 -212
  213. data/ext/tb_client/tigerbeetle/src/testing/vortex/constants.zig +0 -26
  214. data/ext/tb_client/tigerbeetle/src/testing/vortex/faulty_network.zig +0 -579
  215. data/ext/tb_client/tigerbeetle/src/testing/vortex/java_driver/ci.zig +0 -39
  216. data/ext/tb_client/tigerbeetle/src/testing/vortex/logged_process.zig +0 -214
  217. data/ext/tb_client/tigerbeetle/src/testing/vortex/rust_driver/ci.zig +0 -34
  218. data/ext/tb_client/tigerbeetle/src/testing/vortex/supervisor.zig +0 -785
  219. data/ext/tb_client/tigerbeetle/src/testing/vortex/workload.zig +0 -543
  220. data/ext/tb_client/tigerbeetle/src/testing/vortex/zig_driver.zig +0 -181
  221. data/ext/tb_client/tigerbeetle/src/tidy.zig +0 -1449
  222. data/ext/tb_client/tigerbeetle/src/tigerbeetle/benchmark_driver.zig +0 -227
  223. data/ext/tb_client/tigerbeetle/src/tigerbeetle/benchmark_load.zig +0 -1069
  224. data/ext/tb_client/tigerbeetle/src/tigerbeetle/cli.zig +0 -1422
  225. data/ext/tb_client/tigerbeetle/src/tigerbeetle/inspect.zig +0 -1658
  226. data/ext/tb_client/tigerbeetle/src/tigerbeetle/inspect_integrity.zig +0 -518
  227. data/ext/tb_client/tigerbeetle/src/tigerbeetle/libtb_client.zig +0 -36
  228. data/ext/tb_client/tigerbeetle/src/tigerbeetle/main.zig +0 -646
  229. data/ext/tb_client/tigerbeetle/src/tigerbeetle.zig +0 -958
  230. data/ext/tb_client/tigerbeetle/src/time.zig +0 -236
  231. data/ext/tb_client/tigerbeetle/src/trace/event.zig +0 -745
  232. data/ext/tb_client/tigerbeetle/src/trace/statsd.zig +0 -462
  233. data/ext/tb_client/tigerbeetle/src/trace.zig +0 -556
  234. data/ext/tb_client/tigerbeetle/src/unit_tests.zig +0 -321
  235. data/ext/tb_client/tigerbeetle/src/vopr.zig +0 -1785
  236. data/ext/tb_client/tigerbeetle/src/vortex.zig +0 -101
  237. data/ext/tb_client/tigerbeetle/src/vsr/checkpoint_trailer.zig +0 -473
  238. data/ext/tb_client/tigerbeetle/src/vsr/checksum.zig +0 -208
  239. data/ext/tb_client/tigerbeetle/src/vsr/checksum_benchmark.zig +0 -43
  240. data/ext/tb_client/tigerbeetle/src/vsr/client.zig +0 -768
  241. data/ext/tb_client/tigerbeetle/src/vsr/client_replies.zig +0 -532
  242. data/ext/tb_client/tigerbeetle/src/vsr/client_sessions.zig +0 -338
  243. data/ext/tb_client/tigerbeetle/src/vsr/clock.zig +0 -1019
  244. data/ext/tb_client/tigerbeetle/src/vsr/fault_detector.zig +0 -279
  245. data/ext/tb_client/tigerbeetle/src/vsr/free_set.zig +0 -1381
  246. data/ext/tb_client/tigerbeetle/src/vsr/free_set_fuzz.zig +0 -315
  247. data/ext/tb_client/tigerbeetle/src/vsr/grid.zig +0 -1460
  248. data/ext/tb_client/tigerbeetle/src/vsr/grid_blocks_missing.zig +0 -757
  249. data/ext/tb_client/tigerbeetle/src/vsr/grid_scrubber.zig +0 -797
  250. data/ext/tb_client/tigerbeetle/src/vsr/journal.zig +0 -2586
  251. data/ext/tb_client/tigerbeetle/src/vsr/marzullo.zig +0 -308
  252. data/ext/tb_client/tigerbeetle/src/vsr/message_header.zig +0 -1777
  253. data/ext/tb_client/tigerbeetle/src/vsr/multi_batch.zig +0 -715
  254. data/ext/tb_client/tigerbeetle/src/vsr/multi_batch_fuzz.zig +0 -185
  255. data/ext/tb_client/tigerbeetle/src/vsr/repair_budget.zig +0 -333
  256. data/ext/tb_client/tigerbeetle/src/vsr/replica.zig +0 -12356
  257. data/ext/tb_client/tigerbeetle/src/vsr/replica_format.zig +0 -416
  258. data/ext/tb_client/tigerbeetle/src/vsr/replica_reformat.zig +0 -165
  259. data/ext/tb_client/tigerbeetle/src/vsr/replica_test.zig +0 -2928
  260. data/ext/tb_client/tigerbeetle/src/vsr/routing.zig +0 -1075
  261. data/ext/tb_client/tigerbeetle/src/vsr/superblock.zig +0 -1603
  262. data/ext/tb_client/tigerbeetle/src/vsr/superblock_fuzz.zig +0 -484
  263. data/ext/tb_client/tigerbeetle/src/vsr/superblock_quorums.zig +0 -405
  264. data/ext/tb_client/tigerbeetle/src/vsr/superblock_quorums_fuzz.zig +0 -355
  265. data/ext/tb_client/tigerbeetle/src/vsr/sync.zig +0 -29
  266. data/ext/tb_client/tigerbeetle/src/vsr.zig +0 -1727
  267. data/lib/tb_client/shared_lib.rb +0 -66
  268. data/lib/tb_client.rb +0 -282
  269. data/lib/tigerbeetle/account.rb +0 -38
  270. data/lib/tigerbeetle/account_balance.rb +0 -23
  271. data/lib/tigerbeetle/account_filter.rb +0 -31
  272. data/lib/tigerbeetle/atomic_counter.rb +0 -14
  273. data/lib/tigerbeetle/client.rb +0 -214
  274. data/lib/tigerbeetle/converters/account.rb +0 -63
  275. data/lib/tigerbeetle/converters/account_balance.rb +0 -31
  276. data/lib/tigerbeetle/converters/account_filter.rb +0 -32
  277. data/lib/tigerbeetle/converters/base.rb +0 -35
  278. data/lib/tigerbeetle/converters/create_accounts_result.rb +0 -21
  279. data/lib/tigerbeetle/converters/create_transfers_result.rb +0 -21
  280. data/lib/tigerbeetle/converters/query_filter.rb +0 -33
  281. data/lib/tigerbeetle/converters/time.rb +0 -23
  282. data/lib/tigerbeetle/converters/transfer.rb +0 -64
  283. data/lib/tigerbeetle/converters/uint_128.rb +0 -24
  284. data/lib/tigerbeetle/converters.rb +0 -12
  285. data/lib/tigerbeetle/error.rb +0 -4
  286. data/lib/tigerbeetle/id.rb +0 -30
  287. data/lib/tigerbeetle/platforms.rb +0 -9
  288. data/lib/tigerbeetle/query_filter.rb +0 -31
  289. data/lib/tigerbeetle/request.rb +0 -7
  290. data/lib/tigerbeetle/transfer.rb +0 -40
  291. data/lib/tigerbeetle/version.rb +0 -4
  292. data/lib/tigerbeetle.rb +0 -13
  293. data/tigerbeetle.gemspec +0 -60
@@ -1,2114 +0,0 @@
1
- //! Compaction moves or merges a table's values from the previous level.
2
- //!
3
- //! Each Compaction is paced to run in an arbitrary amount of beats, by the forest.
4
- //!
5
- //! Compaction overview:
6
- //!
7
- //! 1. Given:
8
- //!
9
- //! - levels A and B, where A+1=B
10
- //! - a single table in level A ("table A")
11
- //! - all tables from level B which intersect table A's key range ("tables B")
12
- //! (This can include anything between 0 tables and all of level B's tables.)
13
- //!
14
- //! 2. If table A's key range is disjoint from the keys in level B, move table A into level B.
15
- //! All done! (But if the key ranges intersect, jump to step 3).
16
- //!
17
- //! 3. Create an iterator from the sort-merge of table A and the concatenation of tables B.
18
- //! If the same key exists in level A and B, take A's and discard B's. †
19
- //!
20
- //! 4. Write the sort-merge iterator into a sequence of new tables on disk.
21
- //!
22
- //! 5. Update the input tables in the Manifest with their new `snapshot_max` so that they become
23
- //! invisible to subsequent read transactions.
24
- //!
25
- //! 6. Insert the new level-B tables into the Manifest.
26
- //!
27
- //! † When A's value is a tombstone, there is a special case for garbage collection. When either:
28
- //! * level B is the final level, or
29
- //! * A's key does not exist in B or any deeper level,
30
- //! then the tombstone is omitted from the compacted output, see: `compaction_must_drop_tombstones`.
31
- //!
32
- const std = @import("std");
33
- const mem = std.mem;
34
- const math = std.math;
35
- const assert = std.debug.assert;
36
- const Allocator = std.mem.Allocator;
37
-
38
- const log = std.log.scoped(.compaction);
39
-
40
- const constants = @import("../constants.zig");
41
-
42
- const stdx = @import("stdx");
43
- const maybe = stdx.maybe;
44
- const vsr = @import("../vsr.zig");
45
- const trace = @import("../trace.zig");
46
- const StackType = @import("../stack.zig").StackType;
47
- const IOPSType = @import("../iops.zig").IOPSType;
48
- const GridType = @import("../vsr/grid.zig").GridType;
49
- const BlockPtr = @import("../vsr/grid.zig").BlockPtr;
50
- const BlockPtrConst = @import("../vsr/grid.zig").BlockPtrConst;
51
- const allocate_block = @import("../vsr/grid.zig").allocate_block;
52
- const TableInfoType = @import("manifest.zig").TreeTableInfoType;
53
- const ManifestType = @import("manifest.zig").ManifestType;
54
- const schema = @import("schema.zig");
55
- const RingBufferType = stdx.RingBufferType;
56
-
57
- /// The upper-bound count of input tables to a single tree's compaction.
58
- ///
59
- /// - +1 from level A.
60
- /// - +lsm_growth_factor from level B. The A-input table cannot overlap with an extra B-input table
61
- /// because input table selection is least-overlap. If the input table overlaps on one or both
62
- /// edges, there must be another table with less overlap to select.
63
- pub const compaction_tables_input_max = 1 + constants.lsm_growth_factor;
64
-
65
- /// The upper-bound count of output tables from a single tree's compaction.
66
- /// In the "worst" case, no keys are overwritten/merged, and no tombstones are dropped.
67
- pub const compaction_tables_output_max = compaction_tables_input_max;
68
-
69
- /// The minimum number of blocks required for a single beat of a single compaction.
70
- ///
71
- /// Compaction needs to carry over the output index block and all input blocks to the next beat:
72
- /// One index and one value block for the output table, one index block for level A, two index
73
- /// blocks for level B (to allow prefetching), and `lsm_compaction_queue_read_max/2` value blocks
74
- /// for the two input tables.
75
- pub const compaction_block_count_beat_min: u32 =
76
- (1 + 1) + (1 + 2) + constants.lsm_compaction_queue_read_max;
77
-
78
- const half_bar_beat_count = @divExact(constants.lsm_compaction_ops, 2);
79
-
80
- /// Resources shared by all compactions.
81
- ///
82
- /// ResourcePool is a singleton owned by the Forest, but it doesn't depend on Forest type.
83
- pub fn ResourcePoolType(comptime Grid: type) type {
84
- return struct {
85
- reads: IOPSType(BlockRead, constants.lsm_compaction_iops_read_max) = .{},
86
- writes: IOPSType(BlockWrite, constants.lsm_compaction_iops_write_max) = .{},
87
- cpus: IOPSType(CPU, 1) = .{},
88
- blocks: StackType(Block),
89
- blocks_backing_storage: []Block,
90
- grid_reservation: ?Grid.Reservation = null,
91
-
92
- const ResourcePool = @This();
93
-
94
- const BlockRead = struct {
95
- grid_read: Grid.Read,
96
- block: *Block,
97
- compaction: *anyopaque,
98
-
99
- fn parent(read: *BlockRead, comptime Compaction: type) *Compaction {
100
- return @as(*Compaction, @ptrCast(@alignCast(read.compaction)));
101
- }
102
- };
103
-
104
- const BlockWrite = struct {
105
- grid_write: Grid.Write,
106
- block: *Block,
107
- compaction: *anyopaque,
108
-
109
- fn parent(write: *BlockWrite, comptime Compaction: type) *Compaction {
110
- return @as(*Compaction, @ptrCast(@alignCast(write.compaction)));
111
- }
112
- };
113
-
114
- /// While we don't currently have a CPU pool, we already treat CPU as a resource, by storing
115
- /// it in a ring-buffer of length one.
116
- const CPU = struct {
117
- next_tick: Grid.NextTick,
118
- block: *Block,
119
- compaction: *anyopaque,
120
-
121
- fn parent(cpu: *CPU, comptime Compaction: type) *Compaction {
122
- return @as(*Compaction, @ptrCast(@alignCast(cpu.compaction)));
123
- }
124
- };
125
-
126
- const Block = struct {
127
- ptr: BlockPtr,
128
- stage: enum {
129
- // block is in the resource pool.
130
- free,
131
-
132
- // Block is owned by a table builder.
133
- build_index_block,
134
- build_value_block,
135
-
136
- // Block is in the read queue.
137
- read_index_block,
138
- read_index_block_done,
139
- read_value_block,
140
- read_value_block_done,
141
-
142
- // Block is in the read queue and is used by merge.
143
- // Next stage is either free or loops back to read_value_block_done.
144
- merge,
145
-
146
- // Block is in the write queue. Goes directly to free from here.
147
- write_value_block,
148
- write_index_block,
149
- },
150
-
151
- link: StackType(Block).Link,
152
- };
153
-
154
- pub fn init(allocator: mem.Allocator, block_count: u32) !ResourcePool {
155
- const blocks_backing_storage = try allocator.alloc(Block, block_count);
156
- var blocks_allocated: u32 = 0;
157
- errdefer {
158
- for (blocks_backing_storage[0..blocks_allocated]) |block| {
159
- allocator.free(block.ptr);
160
- }
161
- allocator.free(blocks_backing_storage);
162
- }
163
-
164
- for (blocks_backing_storage) |*block| {
165
- block.* = .{
166
- .ptr = try allocate_block(allocator),
167
- .stage = .free,
168
- .link = .{},
169
- };
170
- blocks_allocated += 1;
171
- }
172
- assert(blocks_allocated == block_count);
173
-
174
- var blocks = StackType(Block).init(.{
175
- .capacity = blocks_allocated,
176
- .verify_push = false,
177
- });
178
- for (blocks_backing_storage) |*block| blocks.push(block);
179
-
180
- return .{
181
- .blocks = blocks,
182
- .blocks_backing_storage = blocks_backing_storage,
183
- };
184
- }
185
-
186
- pub fn deinit(pool: *ResourcePool, allocator: Allocator) void {
187
- for (pool.blocks_backing_storage) |block| {
188
- allocator.free(block.ptr);
189
- }
190
- allocator.free(pool.blocks_backing_storage);
191
- }
192
-
193
- pub fn reset(pool: *ResourcePool) void {
194
- pool.* = .{
195
- .blocks = StackType(Block).init(.{
196
- .capacity = pool.blocks.capacity(),
197
- .verify_push = false,
198
- }),
199
- .blocks_backing_storage = pool.blocks_backing_storage,
200
- };
201
- for (pool.blocks_backing_storage) |*block| {
202
- block.* = .{
203
- .ptr = block.ptr,
204
- .stage = .free,
205
- .link = .{},
206
- };
207
- pool.blocks.push(block);
208
- }
209
- }
210
-
211
- // NB: idle does not check that no blocks are acquired! Although no IO can happen between
212
- // the beats, it is valid to carry over some blocks.
213
- pub fn idle(pool: *ResourcePool) bool {
214
- return pool.reads.executing() == 0 and
215
- pool.writes.executing() == 0 and
216
- pool.cpus.executing() == 0;
217
- }
218
-
219
- pub fn blocks_acquired(pool: *ResourcePool) u32 {
220
- assert(pool.blocks.count() <= pool.blocks_backing_storage.len);
221
- return @as(u32, @intCast(pool.blocks_backing_storage.len - pool.blocks.count()));
222
- }
223
-
224
- pub fn blocks_free(pool: *ResourcePool) u32 {
225
- return pool.blocks.count();
226
- }
227
-
228
- fn block_acquire(pool: *@This()) ?*Block {
229
- const block = pool.blocks.pop() orelse return null;
230
- assert(block.stage == .free);
231
- assert(block.link.next == null);
232
- return block;
233
- }
234
-
235
- fn block_release(pool: *@This(), block: *Block) void {
236
- assert(block.stage == .free);
237
- assert(block.link.next == null);
238
- pool.blocks.push(block);
239
- }
240
-
241
- pub fn format(
242
- self: @This(),
243
- comptime _: []const u8,
244
- _: std.fmt.FormatOptions,
245
- writer: anytype,
246
- ) !void {
247
- return writer.print("ResourcePool{{ " ++
248
- ".reads = {}/{}, .writes = {}/{}, .cpus = {}/{}, .blocks = {}/{} }}", .{
249
- self.reads.available(), self.reads.total(),
250
- self.writes.available(), self.writes.total(),
251
- self.cpus.available(), self.cpus.total(),
252
- self.blocks.count, self.blocks_backing_storage.len,
253
- });
254
- }
255
- };
256
- }
257
-
258
- pub fn CompactionType(comptime Tree: type, comptime Storage: type) type {
259
- return struct {
260
- const Compaction = @This();
261
-
262
- const Grid = GridType(Storage);
263
- const ResourcePool = ResourcePoolType(Grid);
264
-
265
- const Table = Tree.Table;
266
- const Manifest = ManifestType(Table, Storage);
267
- const TableInfo = TableInfoType(Table);
268
- const TableInfoReference = Manifest.TableInfoReference;
269
- const CompactionRange = Manifest.CompactionRange;
270
-
271
- const Value = Table.Value;
272
- const key_from_value = Table.key_from_value;
273
- const tombstone = Table.tombstone;
274
-
275
- const TableInfoA = union(enum) {
276
- immutable: []Value,
277
- disk: TableInfoReference,
278
- };
279
-
280
- const Position = struct {
281
- index_block: u32 = 0,
282
- value_block: u32 = 0,
283
- value: u32 = 0,
284
-
285
- pub fn format(
286
- self: @This(),
287
- comptime _: []const u8,
288
- _: std.fmt.FormatOptions,
289
- writer: anytype,
290
- ) !void {
291
- return writer.print("Position{{ .index_block = {}, " ++
292
- ".value_block = {}, .value = {} }}", .{
293
- self.index_block,
294
- self.value_block,
295
- self.value,
296
- });
297
- }
298
- };
299
-
300
- // Globally scoped fields:
301
- // ----------------------
302
- grid: *Grid,
303
- tree: *Tree,
304
- level_b: u8,
305
-
306
- stage: enum {
307
- inactive,
308
- beat,
309
- beat_quota_done,
310
- paused,
311
- } = .inactive,
312
-
313
- // Bar-scoped fields:
314
- // -----------------
315
-
316
- /// `op_min` is the first op/beat of this compaction's half-bar.
317
- /// `op_min` is used as a snapshot — the compaction's input tables must be visible
318
- /// to `op_min`.
319
- ///
320
- /// After this compaction finishes:
321
- /// - `op_min + half_bar_beat_count - 1` will be the input tables' snapshot_max.
322
- /// - `op_min + half_bar_beat_count` will be the output tables' snapshot_min.
323
- op_min: u64 = 0,
324
-
325
- table_info_a: ?TableInfoA = null,
326
- range_b: ?CompactionRange = null,
327
-
328
- /// Whether this compaction will use the move-table optimization.
329
- /// Specifically, this field is set to True if the optimal compaction
330
- /// table in level A can simply be moved to level B.
331
- move_table: bool = false,
332
- /// Levels may choose to drop tombstones if keys aren't included in the lower levels.
333
- /// This invariant is always true for the last level as it doesn't have any lower ones.
334
- drop_tombstones: bool = false,
335
-
336
- /// Counters track physical IO and are not fully deterministic. In particular, `in` and
337
- /// `dropped` values can vary between the replicas.
338
- ///
339
- /// Counters obey accounting equation of compaction:
340
- /// out = in - dropped
341
- counters: struct {
342
- in: u64 = 0,
343
- dropped: u64 = 0, // Tombstones.
344
- out: u64 = 0,
345
-
346
- fn consistent(counters: @This()) bool {
347
- return counters.out == counters.in - counters.dropped;
348
- }
349
- } = .{},
350
-
351
- /// Quotas track logical progress of compaction, determine pacing and must be deterministic.
352
- /// Quotas count consumed input values. That is, every time an output block is written,
353
- /// the done quota is incremented by the number of input values which contributed to the
354
- /// output block.
355
- ///
356
- /// At the start of the bar, the total number of input values is known. The beat quota is
357
- /// then set based on the number of values left and beats left.
358
- quotas: struct {
359
- beat: u64 = 0,
360
- beat_done: u64 = 0,
361
- bar: u64 = 0,
362
- bar_done: u64 = 0,
363
-
364
- fn beat_exhausted(quotas: @This()) bool {
365
- assert(quotas.beat_done <= quotas.bar_done);
366
- assert(quotas.bar_done <= quotas.bar);
367
- return quotas.beat_done >= quotas.beat;
368
- }
369
-
370
- fn bar_exhausted(quotas: @This()) bool {
371
- assert(quotas.bar_done <= quotas.bar);
372
- return quotas.bar_done == quotas.bar;
373
- }
374
- } = .{},
375
-
376
- // Position points at the next value from the layer that should be feed into the merge
377
- // algorithm.
378
- level_a_position: Position = .{},
379
- level_b_position: Position = .{},
380
-
381
- /// Manifest log appends are queued up until bar_complete is explicitly called to ensure
382
- /// they are applied deterministically relative to other concurrent compactions.
383
- // Worst-case manifest updates:
384
- // See docs/about/internals/lsm.md "Compaction Table Overlap" for more detail.
385
- manifest_entries: stdx.BoundedArrayType(struct {
386
- operation: enum {
387
- insert_to_level_b,
388
- move_to_level_b,
389
- },
390
- table: TableInfo,
391
- }, compaction_tables_output_max) = .{},
392
-
393
- table_builder: Table.Builder = .{},
394
- table_builder_index_block: ?*ResourcePool.Block = null,
395
- table_builder_value_block: ?*ResourcePool.Block = null,
396
-
397
- // The progress through immutable table is persisted throughout the bar.
398
- level_a_immutable_stage: enum { ready, merge, exhausted } = .ready,
399
-
400
- // Beat-scoped fields:
401
- // ------------------
402
- pool: ?*ResourcePool = null,
403
- callback: ?*const fn (pool: *ResourcePool, tree_id: u16, values_consumed: u64) void = null,
404
-
405
- // IO queues:
406
- //
407
- // Compaction structure is such that the data can be read (and written) concurrently, but
408
- // the merge must happen sequentially. It is reminiscent of state machine's prefetch/execute
409
- // split.
410
- //
411
- // When a block is read from disk, it is added to the tail of the corresponding queue. When
412
- // the head block from both level a and level b queues is in the .read_value_block_done
413
- // state, the two blocks are popped off the queues and passed down to the merge. At this
414
- // point, any number of the blocks still in the queues can continue their read operations.
415
- //
416
- // For index blocks, queues of length one are used. Because an index block is freed as soon
417
- // as the read for the last value block is scheduled, the pipeline should not dry out even
418
- // when switching between the tables.
419
- //
420
- // Note that level_{a,b}_position fields track the logical, deterministic progression of
421
- // compaction.
422
- //
423
- // For output blocks:
424
- // - the order of completions doesn't matter,
425
- // - the blocks are not used after the completion of the IO. That is, only the number of
426
- // outstanding operations needs to be tracked. Use a RingBuffer with void elements rather
427
- // than an u32 for API uniformity and comptime upper bound. We have
428
- // <https://github.com/ziglang/zig/issues/3806> at home.
429
- //
430
- // In addition to static max size, the queues are additionally limited at runtime by the
431
- // number of available free blocks. The queues are not limited by IOPS --- it is assumed
432
- // that there are enough IOPS to fill up all the queues.
433
- level_a_index_block: RingBufferType(*ResourcePool.Block, .{
434
- .array = 1,
435
- }) = .{ .buffer = undefined },
436
-
437
- level_a_value_block: RingBufferType(*ResourcePool.Block, .{
438
- .array = @divExact(constants.lsm_compaction_queue_read_max, 2),
439
- }) = .{ .buffer = undefined },
440
-
441
- level_b_index_block: RingBufferType(*ResourcePool.Block, .{
442
- .array = 2, // Prefetch next table's index block
443
- }) = .{ .buffer = undefined },
444
-
445
- level_b_value_block: RingBufferType(*ResourcePool.Block, .{
446
- .array = @divExact(constants.lsm_compaction_queue_read_max, 2),
447
- }) = .{ .buffer = undefined },
448
-
449
- output_blocks: RingBufferType(void, .{
450
- .array = constants.lsm_compaction_queue_write_max,
451
- }) = .{ .buffer = undefined },
452
-
453
- pub fn init(tree: *Tree, grid: *Grid, level_b: u8) Compaction {
454
- assert(level_b < constants.lsm_levels);
455
-
456
- return Compaction{
457
- .grid = grid,
458
- .tree = tree,
459
- .level_b = level_b,
460
- };
461
- }
462
-
463
- pub fn reset(compaction: *Compaction) void {
464
- compaction.grid.trace.cancel(.compact_beat);
465
- compaction.grid.trace.cancel(.compact_beat_merge);
466
- compaction.* = .{
467
- .grid = compaction.grid,
468
- .tree = compaction.tree,
469
- .level_b = compaction.level_b,
470
- };
471
- }
472
-
473
- /// Assert consistency of the compaction counters between beats. This isn't as
474
- /// straightforward as calling counters.consistent() as we do at the end of the bar, since
475
- /// we may carry over multiple input value blocks, and an output value block over to the
476
- /// next beat.
477
- ///
478
- ///
479
- /// Compute values_in, values_dropped, values_out, values_in_flight, and assert:
480
- /// values_out + values_in_flight == values_in - values_dropped
481
- ///
482
- // values_in: Values from the input value blocks that have been read from disk.
483
- // values_dropped: Values dropped during merge.
484
- // values_out: Values from the output value blocks that have been written to disk,
485
- // plus the values from the output value block being carried over to the
486
- // next beat (output of merge but not written to disk yet).
487
- // values_in_flight: Values from the input value blocks being carried over to the next
488
- // beat, minus the values that have already been consumed during
489
- // merge.
490
- fn assert_counter_consistency_between_beats(compaction: *const Compaction) void {
491
- const values_in = compaction.counters.in;
492
- const values_out = compaction.counters.out + compaction.table_builder.value_count;
493
- const values_dropped = compaction.counters.dropped;
494
-
495
- var values_in_flight: u64 = 0;
496
-
497
- if (compaction.table_info_a.? == .immutable) {
498
- assert(compaction.level_a_value_block.empty());
499
- if (compaction.level_a_immutable_stage != .exhausted) {
500
- values_in_flight += compaction.table_info_a.?.immutable.len;
501
- }
502
- }
503
-
504
- var level_a_value_block_iterator = compaction.level_a_value_block.iterator();
505
- while (level_a_value_block_iterator.next()) |block| {
506
- values_in_flight += Table.value_block_values_used(block.ptr).len;
507
- }
508
- values_in_flight -= compaction.level_a_position.value;
509
-
510
- var level_b_value_block_iterator = compaction.level_b_value_block.iterator();
511
- while (level_b_value_block_iterator.next()) |block| {
512
- values_in_flight += Table.value_block_values_used(block.ptr).len;
513
- }
514
-
515
- values_in_flight -= compaction.level_b_position.value;
516
-
517
- assert(values_out + values_in_flight == values_in - values_dropped);
518
- }
519
-
520
- pub fn assert_between_bars(compaction: *const Compaction) void {
521
- assert(compaction.stage == .inactive);
522
- assert(compaction.idle());
523
- assert(compaction.block_queues_empty_output());
524
- assert(compaction.block_queues_empty_input());
525
-
526
- assert(compaction.table_builder.state == .no_blocks);
527
- assert(compaction.table_builder_value_block == null);
528
- assert(compaction.table_builder_index_block == null);
529
- assert(compaction.manifest_entries.empty());
530
- }
531
-
532
- fn idle(compaction: *const Compaction) bool {
533
- return compaction.pool == null and
534
- compaction.callback == null and
535
- compaction.quotas.beat_exhausted();
536
- }
537
-
538
- fn block_queues_empty_output(compaction: *const Compaction) bool {
539
- return compaction.output_blocks.empty();
540
- }
541
-
542
- fn block_queues_empty_input(compaction: *const Compaction) bool {
543
- return compaction.level_a_index_block.empty() and
544
- compaction.level_a_value_block.empty() and
545
- compaction.level_b_index_block.empty() and
546
- compaction.level_b_value_block.empty();
547
- }
548
-
549
- /// Plan the work for the bar:
550
- /// - check if compaction is needed at all (if the level_a is full),
551
- /// - find a table on level_a and the corresponding range on level_b that should be
552
- /// compacted,
553
- /// - compute the bar quota (just the total number of values in all input tables),
554
- /// - execute move table optimization if range_b turns out to be empty.
555
- pub fn bar_commence(compaction: *Compaction, op: u64) u64 {
556
- assert(compaction.idle());
557
- assert(compaction.block_queues_empty_output());
558
- assert(compaction.block_queues_empty_input());
559
-
560
- assert(compaction.stage == .inactive);
561
- assert(op == compaction_op_min(op));
562
-
563
- compaction.stage = .paused;
564
- compaction.op_min = op;
565
-
566
- if (compaction.level_b == 0) {
567
- // Do not start compaction if the immutable table does not require compaction.
568
- if (compaction.tree.table_immutable.mutability.immutable.flushed) {
569
- assert(compaction.quotas.bar == 0);
570
- assert(compaction.quotas.bar_exhausted());
571
- log.debug("{s}:{}: bar_commence: immutable table flushed", .{
572
- compaction.tree.config.name,
573
- compaction.level_b,
574
- });
575
- return 0;
576
- }
577
-
578
- const table_value_count_limit = Table.value_count_max;
579
- assert(compaction.tree.table_immutable.count() > 0);
580
- assert(compaction.tree.table_immutable.count() <= table_value_count_limit);
581
-
582
- // If the mutable table will fit in the free capacity of the immutable table (even
583
- // in the projected "worst" case of all full batches during the second half-bar),
584
- // then defer compacting the immutable table into level 0.
585
- //
586
- // This optimization cannot apply to the last bar before a checkpoint trigger, since
587
- // recovery from the checkpoint only replays that final bar, which must reconstruct
588
- // the original immutable table.
589
- // TODO(Snapshots) This optimization must be disabled to take a persistent snapshot.
590
- const mutable_count_half_bar_first = compaction.tree.table_mutable.count();
591
- const mutable_count_half_bar_last = @divExact(table_value_count_limit, 2);
592
- const mutable_count = mutable_count_half_bar_first + mutable_count_half_bar_last;
593
- const immutable_count = compaction.tree.table_immutable.count();
594
- if (immutable_count + mutable_count <= table_value_count_limit) {
595
- const op_checkpoint =
596
- compaction.grid.superblock.working.vsr_state.checkpoint.header.op;
597
- const op_checkpoint_next = vsr.Checkpoint.checkpoint_after(op_checkpoint);
598
- const op_checkpoint_trigger_next =
599
- vsr.Checkpoint.trigger_for_checkpoint(op_checkpoint_next).?;
600
- const compaction_op_max = op + (half_bar_beat_count - 1);
601
- const last_half_bar_of_checkpoint =
602
- compaction_op_max == op_checkpoint_trigger_next;
603
-
604
- if (!last_half_bar_of_checkpoint) {
605
- assert(compaction.quotas.bar == 0);
606
- assert(compaction.quotas.bar_exhausted());
607
- log.debug("{s}:{}: bar_commence: immutable table flush skipped " ++
608
- "({}+{}+{} ≤ {})", .{
609
- compaction.tree.config.name,
610
- compaction.level_b,
611
- immutable_count,
612
- mutable_count_half_bar_first,
613
- mutable_count_half_bar_last,
614
- table_value_count_limit,
615
- });
616
- return 0;
617
- }
618
- }
619
-
620
- compaction.table_info_a = .{
621
- .immutable = compaction.tree.table_immutable.values_used(),
622
- };
623
-
624
- compaction.range_b = compaction.tree.manifest.immutable_table_compaction_range(
625
- compaction.tree.table_immutable.key_min(),
626
- compaction.tree.table_immutable.key_max(),
627
- .{ .value_count = compaction.tree.table_immutable.count() },
628
- );
629
-
630
- // +1 to count the immutable table (level A).
631
- assert(compaction.range_b.?.tables.count() + 1 <= compaction_tables_input_max);
632
- assert(compaction.range_b.?.key_min <= compaction.tree.table_immutable.key_min());
633
- assert(compaction.tree.table_immutable.key_max() <= compaction.range_b.?.key_max);
634
- } else {
635
- const level_a = compaction.level_b - 1;
636
-
637
- // Do not start compaction if level A does not require compaction.
638
- const table_range = compaction.tree.manifest.compaction_table(level_a) orelse {
639
- assert(compaction.quotas.bar == 0);
640
- assert(compaction.quotas.bar_exhausted());
641
- log.debug("{s}:{}: bar_commence: nothing to compact", .{
642
- compaction.tree.config.name,
643
- compaction.level_b,
644
- });
645
- return 0;
646
- };
647
-
648
- compaction.table_info_a = .{ .disk = table_range.table_a };
649
- compaction.range_b = table_range.range_b;
650
-
651
- assert(compaction.range_b.?.tables.count() + 1 <= compaction_tables_input_max);
652
- assert(compaction.table_info_a.?.disk.table_info.key_min <=
653
- compaction.table_info_a.?.disk.table_info.key_max);
654
- assert(compaction.range_b.?.key_min <=
655
- compaction.table_info_a.?.disk.table_info.key_min);
656
- assert(compaction.table_info_a.?.disk.table_info.key_max <=
657
- compaction.range_b.?.key_max);
658
- }
659
-
660
- switch (compaction.table_info_a.?) {
661
- .immutable => {},
662
- .disk => |table| {
663
- assert(!compaction.grid.free_set.is_released(table.table_info.address));
664
- assert(!compaction.grid.free_set.is_free(table.table_info.address));
665
- },
666
- }
667
- for (compaction.range_b.?.tables.slice()) |table| {
668
- assert(!compaction.grid.free_set.is_released(table.table_info.address));
669
- assert(!compaction.grid.free_set.is_free(table.table_info.address));
670
- }
671
-
672
- var quota_bar = switch (compaction.table_info_a.?) {
673
- .immutable => compaction.tree.table_immutable.count(),
674
- .disk => |table| table.table_info.value_count,
675
- };
676
- for (compaction.range_b.?.tables.const_slice()) |*table| {
677
- quota_bar += table.table_info.value_count;
678
- }
679
- compaction.quotas = .{
680
- .beat = 0,
681
- .beat_done = 0,
682
- .bar = quota_bar,
683
- .bar_done = 0,
684
- };
685
-
686
- log.debug("{s}:{}: bar_commence: quota_bar_done={} quota_bar={}", .{
687
- compaction.tree.config.name,
688
- compaction.level_b,
689
- compaction.quotas.bar_done,
690
- compaction.quotas.bar,
691
- });
692
- compaction.move_table = compaction.table_info_a.? == .disk and
693
- compaction.range_b.?.tables.empty();
694
- compaction.drop_tombstones = compaction.tree.manifest
695
- .compaction_must_drop_tombstones(compaction.level_b, &compaction.range_b.?);
696
-
697
- // The last level must always drop tombstones.
698
- if (compaction.level_b == constants.lsm_levels - 1) assert(compaction.drop_tombstones);
699
-
700
- assert(std.meta.eql(compaction.counters, .{}));
701
- inline for (.{ compaction.level_a_position, compaction.level_b_position }) |position| {
702
- assert(std.meta.eql(position, .{}));
703
- }
704
-
705
- // Append the entries to the manifest update queue here and now if we're doing
706
- // move table. They'll be applied later by bar_complete().
707
- if (compaction.move_table) {
708
- const snapshot_max = snapshot_max_for_table_input(compaction.op_min);
709
- assert(compaction.table_info_a.?.disk.table_info.snapshot_max >= snapshot_max);
710
-
711
- compaction.manifest_entries.push(.{
712
- .operation = .move_to_level_b,
713
- .table = compaction.table_info_a.?.disk.table_info.*,
714
- });
715
-
716
- const value_count = compaction.table_info_a.?.disk.table_info.value_count;
717
-
718
- compaction.quotas.beat = value_count;
719
- compaction.quotas.beat_done = value_count;
720
- compaction.quotas.bar_done = value_count;
721
-
722
- assert(compaction.quotas.beat_exhausted());
723
- assert(compaction.quotas.bar_exhausted());
724
-
725
- return 0;
726
- } else {
727
- if (compaction.table_info_a.? == .immutable) {
728
- compaction.counters.in += compaction.table_info_a.?.immutable.len;
729
- }
730
- return compaction.quotas.bar;
731
- }
732
- }
733
-
734
- /// Apply the changes that have been accumulated in memory to the manifest and remove any
735
- /// tables that are now invisible.
736
- pub fn bar_complete(compaction: *Compaction) void {
737
- assert(compaction.idle());
738
- assert(compaction.block_queues_empty_output());
739
- assert(compaction.block_queues_empty_input());
740
-
741
- assert(compaction.stage == .paused);
742
- assert(compaction.counters.consistent());
743
- assert(compaction.quotas.bar_exhausted());
744
- // Assert blocks have been released back to the pipeline.
745
- assert(compaction.table_builder.state == .no_blocks);
746
- assert(compaction.table_builder_index_block == null);
747
- assert(compaction.table_builder_value_block == null);
748
-
749
- defer {
750
- compaction.* = .{
751
- .grid = compaction.grid,
752
- .tree = compaction.tree,
753
- .level_b = compaction.level_b,
754
- };
755
- assert(compaction.stage == .inactive);
756
- }
757
-
758
- if (compaction.table_info_a == null) {
759
- assert(compaction.range_b == null);
760
- assert(compaction.manifest_entries.count() == 0);
761
- assert(compaction.quotas.bar == 0);
762
- if (compaction.level_b == 0) {
763
- // Either:
764
- // - the immutable table is empty (already flushed), or
765
- // - the mutable table will be absorbed into the immutable table.
766
- maybe(compaction.tree.table_immutable.mutability.immutable.flushed);
767
- }
768
- return;
769
- }
770
- assert(compaction.table_info_a != null);
771
- assert(compaction.range_b != null);
772
- assert(compaction.quotas.bar > 0);
773
-
774
- switch (compaction.table_info_a.?) {
775
- .immutable => {},
776
- .disk => |table| {
777
- if (compaction.move_table) {
778
- assert(!compaction.grid.free_set.is_released(table.table_info.address));
779
- assert(!compaction.grid.free_set.is_free(table.table_info.address));
780
- } else {
781
- assert(compaction.grid.free_set.is_released(table.table_info.address));
782
- }
783
- },
784
- }
785
- for (compaction.range_b.?.tables.slice()) |table| {
786
- assert(compaction.grid.free_set.is_released(table.table_info.address));
787
- }
788
-
789
- log.debug("{s}:{}: bar_complete: " ++
790
- "values_in={} values_out={} values_dropped={}", .{
791
- compaction.tree.config.name,
792
- compaction.level_b,
793
- compaction.counters.in,
794
- compaction.counters.out,
795
- compaction.counters.dropped,
796
- });
797
-
798
- compaction.grid.trace.count(
799
- .{ .compaction_values_physical = .{
800
- .tree = @enumFromInt(compaction.tree.config.id),
801
- } },
802
- compaction.counters.out,
803
- );
804
- if (compaction.level_b == 0) {
805
- if (compaction.table_info_a.? == .immutable) {
806
- compaction.grid.trace.count(
807
- .{ .compaction_values_logical = .{
808
- .tree = @enumFromInt(compaction.tree.config.id),
809
- } },
810
- compaction.table_info_a.?.immutable.len,
811
- );
812
- }
813
- }
814
-
815
- // Mark the immutable table as flushed, if we were compacting into level 0.
816
- if (compaction.level_b == 0) {
817
- assert(!compaction.tree.table_immutable.mutability.immutable.flushed);
818
- compaction.tree.table_immutable.mutability.immutable.flushed = true;
819
- }
820
-
821
- // Each compaction's manifest updates are deferred to the end of the last
822
- // bar to ensure:
823
- // - manifest log updates are ordered deterministically relative to one another, and
824
- // - manifest updates are not visible until after the blocks are all on disk.
825
- const manifest = &compaction.tree.manifest;
826
- const level_b = compaction.level_b;
827
- const snapshot_max = snapshot_max_for_table_input(compaction.op_min);
828
-
829
- var manifest_removed_value_count: u64 = 0;
830
- var manifest_added_value_count: u64 = 0;
831
-
832
- if (compaction.move_table) {
833
- // If no compaction is required, don't update snapshot_max.
834
- } else {
835
- // These updates MUST precede insert_table() and move_table() since they use
836
- // references to modify the ManifestLevel in-place.
837
- switch (compaction.table_info_a.?) {
838
- .immutable => {
839
- manifest_removed_value_count = compaction.tree.table_immutable.count();
840
- },
841
- .disk => |table_info| {
842
- manifest_removed_value_count += table_info.table_info.value_count;
843
- manifest.update_table(level_b - 1, snapshot_max, table_info);
844
- },
845
- }
846
- for (compaction.range_b.?.tables.const_slice()) |table| {
847
- manifest_removed_value_count += table.table_info.value_count;
848
- manifest.update_table(level_b, snapshot_max, table);
849
- }
850
- }
851
-
852
- for (compaction.manifest_entries.slice()) |*entry| {
853
- switch (entry.operation) {
854
- .insert_to_level_b => {
855
- manifest.insert_table(level_b, &entry.table);
856
- manifest_added_value_count += entry.table.value_count;
857
- },
858
- .move_to_level_b => {
859
- manifest.move_table(level_b - 1, level_b, &entry.table);
860
- manifest_removed_value_count += entry.table.value_count;
861
- manifest_added_value_count += entry.table.value_count;
862
- },
863
- }
864
- }
865
- if (compaction.move_table) {
866
- assert(std.meta.eql(compaction.counters, .{}));
867
- assert(manifest_added_value_count == manifest_removed_value_count);
868
- assert(manifest_added_value_count > 0);
869
- } else {
870
- assert(manifest_added_value_count == compaction.counters.out);
871
- assert(manifest_removed_value_count == compaction.counters.in);
872
- assert(manifest_removed_value_count - manifest_added_value_count ==
873
- compaction.counters.dropped);
874
- }
875
-
876
- // Hide any tables that are now invisible.
877
- manifest.remove_invisible_tables(
878
- level_b,
879
- &.{},
880
- compaction.range_b.?.key_min,
881
- compaction.range_b.?.key_max,
882
- );
883
- if (level_b > 0) {
884
- manifest.remove_invisible_tables(
885
- level_b - 1,
886
- &.{},
887
- compaction.range_b.?.key_min,
888
- compaction.range_b.?.key_max,
889
- );
890
- }
891
- }
892
-
893
- pub fn beat_commence(
894
- compaction: *Compaction,
895
- values_count: u64,
896
- ) void {
897
- assert(compaction.idle());
898
- assert(compaction.stage == .paused);
899
- assert(compaction.block_queues_empty_output());
900
- // We may be carrying over some blocks from the previous beat.
901
- maybe(compaction.block_queues_empty_input());
902
-
903
- if (compaction.move_table) assert(compaction.quotas.bar_exhausted());
904
-
905
- // Run the compaction up to completion of the bar quota, if possible.
906
- const values_remaining = (compaction.quotas.bar - compaction.quotas.bar_done);
907
-
908
- compaction.quotas.beat = @min(values_count, values_remaining);
909
- compaction.quotas.beat_done = 0;
910
- assert(compaction.quotas.beat <= compaction.quotas.bar);
911
- }
912
-
913
- /// The entry point to the actual compaction work for the beat. Called by the forest.
914
- pub fn compaction_dispatch_enter(
915
- compaction: *Compaction,
916
- options: struct {
917
- pool: *ResourcePool,
918
- callback: *const fn (pool: *ResourcePool, tree_id: u16, values_consumed: u64) void,
919
- },
920
- ) enum { pending, ready } {
921
- assert(compaction.stage == .paused);
922
- assert(compaction.block_queues_empty_output());
923
- // We may be carrying over some blocks from the previous beat.
924
- maybe(compaction.block_queues_empty_input());
925
-
926
- if (compaction.move_table) assert(compaction.quotas.bar_exhausted());
927
-
928
- if (compaction.quotas.bar_exhausted()) {
929
- log.debug("{}: {s}:{}: beat_commence: bar quota={} fulfilled, done={}", .{
930
- compaction.grid.superblock.replica_index.?,
931
- compaction.tree.config.name,
932
- compaction.level_b,
933
- compaction.quotas.bar,
934
- compaction.quotas.bar_done,
935
- });
936
- return .ready;
937
- }
938
-
939
- if (compaction.quotas.beat_exhausted()) {
940
- log.debug("{s}:{}: beat_commence: beat quota={} fulfilled, done={}", .{
941
- compaction.tree.config.name,
942
- compaction.level_b,
943
- compaction.quotas.beat,
944
- compaction.quotas.beat_done,
945
- });
946
- return .ready;
947
- }
948
-
949
- assert(!compaction.move_table);
950
-
951
- compaction.grid.trace.start(.{ .compact_beat = .{
952
- .tree = @enumFromInt(compaction.tree.config.id),
953
- .level_b = compaction.level_b,
954
- } });
955
-
956
- assert(options.pool.idle());
957
- assert(options.pool.grid_reservation != null);
958
-
959
- compaction.pool = options.pool;
960
- compaction.callback = options.callback;
961
- compaction.stage = .beat;
962
-
963
- compaction.compaction_dispatch();
964
- return .pending;
965
- }
966
-
967
- // While beat_commence is called by the forest sequentially for each compaction, to get
968
- // deterministic grid reservations, each compaction completes its own beat's work
969
- // asynchronously
970
- fn beat_complete(compaction: *Compaction) void {
971
- assert(compaction.stage == .beat_quota_done);
972
- switch (compaction.table_builder.state) {
973
- .no_blocks => {},
974
- .index_and_value_block => {
975
- assert(!compaction.table_builder.index_block_full());
976
- assert(!compaction.table_builder.value_block_full());
977
- assert(!compaction.quotas.bar_exhausted());
978
- },
979
- .index_block => {
980
- assert(!compaction.table_builder.index_block_full());
981
- assert(!compaction.quotas.bar_exhausted());
982
- },
983
- }
984
-
985
- if (compaction.table_info_a.? == .immutable) {
986
- switch (compaction.level_a_immutable_stage) {
987
- .ready, .exhausted => {},
988
- .merge => unreachable,
989
- }
990
- }
991
-
992
- assert(compaction.block_queues_empty_output());
993
- // We may be carrying over some input blocks to the next beat.
994
- maybe(compaction.block_queues_empty_input());
995
-
996
- compaction.assert_counter_consistency_between_beats();
997
-
998
- assert(compaction.pool.?.idle());
999
- maybe(compaction.pool.?.blocks_acquired() > 0);
1000
-
1001
- if (compaction.quotas.bar_exhausted()) {
1002
- assert(compaction.table_builder.state == .no_blocks);
1003
- assert(compaction.table_builder_value_block == null);
1004
- assert(compaction.table_builder_index_block == null);
1005
- assert(compaction.block_queues_empty_input());
1006
- }
1007
-
1008
- const pool = compaction.pool.?;
1009
- const callback = compaction.callback.?;
1010
-
1011
- compaction.stage = .paused;
1012
- compaction.callback = null;
1013
- compaction.pool = null;
1014
-
1015
- assert(compaction.idle());
1016
- assert(pool.idle());
1017
- log.debug("{s}:{}: beat_complete: quota_beat_done={} quota_beat={} " ++
1018
- "quota_bar_done={} quota_bar={}", .{
1019
- compaction.tree.config.name,
1020
- compaction.level_b,
1021
- compaction.quotas.beat_done,
1022
- compaction.quotas.beat,
1023
- compaction.quotas.bar_done,
1024
- compaction.quotas.bar,
1025
- });
1026
-
1027
- callback(pool, compaction.tree.config.id, compaction.quotas.beat_done);
1028
- }
1029
-
1030
- // Compaction is a lot of work: read input tables from both levels, merge their value
1031
- // blocks, write the results to disk. Many of these jobs can proceed in parallel. For
1032
- // example, only a single value block from each level is needed to start a merge.
1033
- //
1034
- // The job of compaction_dispatch is to kick off all the jobs. There are several additional
1035
- // concerns:
1036
- // - All jobs use the same common pool of resources (ResourcePool). The jobs are started
1037
- // in the order that splits resources fairly (e.g., reads from level a and level b
1038
- // alternate). Fairness also ensures that the process does not deadlock.
1039
- // - Jobs have dependencies --- merging needs value blocks, reading a value block needs the
1040
- // corresponding index blocks.
1041
- // - A single bar of compaction should process only a fraction of the input, so the
1042
- // processes can be suspended in the middle.
1043
- //
1044
- // A beat of compaction ends when both:
1045
- // - at least quota.bar of input values is consumed,
1046
- // - there's no incomplete output value blocks.
1047
- //
1048
- // In other words, the only compaction state that gets carried over to the next beat is a
1049
- // partially full index block. The current beat must end with writing a value block, and the
1050
- // next beat must start with re-reading level_a and level_b index and value blocks.
1051
- fn compaction_dispatch(compaction: *Compaction) void {
1052
- switch (compaction.stage) {
1053
- .beat,
1054
- .beat_quota_done,
1055
- => {},
1056
- .inactive,
1057
- .paused,
1058
- => unreachable,
1059
- }
1060
-
1061
- // The loop below runs while (progressed) and, every time progressed is set to true,
1062
- // one of the safety_counter resources is acquired.
1063
- var progressed = true;
1064
- const safety_counter =
1065
- compaction.pool.?.reads.available() +
1066
- compaction.pool.?.writes.available() +
1067
- compaction.pool.?.cpus.available() + 1;
1068
- for (0..safety_counter) |_| {
1069
- if (!progressed) break;
1070
- progressed = false;
1071
-
1072
- if (compaction.stage == .beat_quota_done) {
1073
- // Just wait for all in-flight jobs to complete.
1074
- return compaction.compaction_dispatch_beat_quota_done();
1075
- }
1076
-
1077
- // To avoid deadlocks, allocate blocks for the table builder first.
1078
- if (compaction.table_builder.state == .no_blocks) {
1079
- assert(compaction.table_builder_index_block == null);
1080
- if (compaction.pool.?.block_acquire()) |block| {
1081
- assert(block.stage == .free);
1082
- block.stage = .build_index_block;
1083
- compaction.table_builder.set_index_block(block.ptr);
1084
- compaction.table_builder_index_block = block;
1085
- } else {
1086
- assert(compaction.output_blocks.count > 0);
1087
- }
1088
- }
1089
-
1090
- if (compaction.table_builder.state == .index_block) {
1091
- assert(compaction.table_builder_value_block == null);
1092
- if (compaction.pool.?.block_acquire()) |block| {
1093
- assert(block.stage == .free);
1094
- block.stage = .build_value_block;
1095
- compaction.table_builder.set_value_block(block.ptr);
1096
- compaction.table_builder_value_block = block;
1097
- } else {
1098
- assert(compaction.output_blocks.count > 0);
1099
- }
1100
- }
1101
-
1102
- const level_a_index_block_next =
1103
- compaction.level_a_position.index_block +
1104
- @as(u32, @intCast(compaction.level_a_index_block.count));
1105
- const level_b_index_block_next =
1106
- compaction.level_b_position.index_block +
1107
- @as(u32, @intCast(compaction.level_b_index_block.count));
1108
- const level_a_value_block_next =
1109
- compaction.level_a_position.value_block +
1110
- @as(u32, @intCast(compaction.level_a_value_block.count));
1111
- const level_b_value_block_next =
1112
- compaction.level_b_position.value_block +
1113
- @as(u32, @intCast(compaction.level_b_value_block.count));
1114
-
1115
- // Read level A index block (for level_b > 0).
1116
- if (compaction.table_info_a.? == .disk) {
1117
- assert(compaction.level_b > 0);
1118
- if (!compaction.level_a_index_block.full() and
1119
- level_a_index_block_next < 1)
1120
- {
1121
- if (compaction.pool.?.block_acquire()) |block| {
1122
- const read = compaction.pool.?.reads.acquire().?;
1123
-
1124
- assert(block.stage == .free);
1125
- block.stage = .read_index_block;
1126
- compaction.level_a_index_block.push_assume_capacity(block);
1127
-
1128
- compaction.read_index_block(.level_a, read, block);
1129
- progressed = true;
1130
- } else {
1131
- assert(compaction.level_a_index_block.count > 0 or
1132
- compaction.output_blocks.count > 0);
1133
- }
1134
- }
1135
- }
1136
-
1137
- // Read level B index block.
1138
- if (!compaction.level_b_index_block.full() and
1139
- level_b_index_block_next < compaction.range_b.?.tables.count())
1140
- {
1141
- if (compaction.pool.?.block_acquire()) |block| {
1142
- const read = compaction.pool.?.reads.acquire().?;
1143
-
1144
- assert(block.stage == .free);
1145
- block.stage = .read_index_block;
1146
- compaction.level_b_index_block.push_assume_capacity(block);
1147
-
1148
- compaction.read_index_block(.level_b, read, block);
1149
- progressed = true;
1150
- } else {
1151
- assert(compaction.level_b_index_block.count > 0 or
1152
- compaction.output_blocks.count > 0);
1153
- }
1154
- }
1155
-
1156
- // Read level A value block.
1157
- if (compaction.table_info_a.? == .immutable) {
1158
- // The whole table is in memory, no need to read anything.
1159
- assert(compaction.level_a_index_block.count == 0);
1160
- } else {
1161
- if (compaction.level_a_index_block.head()) |index_block| {
1162
- if (index_block.stage == .read_index_block_done) {
1163
- const index_schema = schema.TableIndex.from(index_block.ptr);
1164
- const value_blocks_count =
1165
- index_schema.value_blocks_used(index_block.ptr);
1166
- if (!compaction.level_a_value_block.full() and
1167
- level_a_value_block_next < value_blocks_count)
1168
- {
1169
- if (compaction.pool.?.block_acquire()) |block| {
1170
- const read = compaction.pool.?.reads.acquire().?;
1171
-
1172
- assert(block.stage == .free);
1173
- block.stage = .read_value_block;
1174
- compaction.level_a_value_block.push_assume_capacity(block);
1175
-
1176
- compaction.read_value_block(.level_a, read, block);
1177
- progressed = true;
1178
- } else {
1179
- assert(compaction.level_a_value_block.count > 0 or
1180
- compaction.output_blocks.count > 0);
1181
- }
1182
- }
1183
- } else {
1184
- assert(index_block.stage == .read_index_block);
1185
- }
1186
- }
1187
- }
1188
-
1189
- // Read level B value block.
1190
- if (compaction.level_b_index_block.head()) |index_block| {
1191
- if (index_block.stage == .read_index_block_done) {
1192
- const index_schema = schema.TableIndex.from(index_block.ptr);
1193
- const value_blocks_count =
1194
- index_schema.value_blocks_used(index_block.ptr);
1195
-
1196
- if (!compaction.level_b_value_block.full() and
1197
- level_b_value_block_next < value_blocks_count)
1198
- {
1199
- if (compaction.pool.?.block_acquire()) |block| {
1200
- const read = compaction.pool.?.reads.acquire().?;
1201
-
1202
- assert(block.stage == .free);
1203
- block.stage = .read_value_block;
1204
- compaction.level_b_value_block.push_assume_capacity(block);
1205
-
1206
- compaction.read_value_block(.level_b, read, block);
1207
- progressed = true;
1208
- } else {
1209
- assert(compaction.level_b_value_block.count > 0 or
1210
- compaction.output_blocks.count > 0);
1211
- }
1212
- }
1213
- } else {
1214
- assert(index_block.stage == .read_index_block);
1215
- }
1216
- }
1217
-
1218
- const level_a_ready_immutable = compaction.table_info_a.? == .immutable and
1219
- compaction.level_a_immutable_stage == .ready;
1220
- const level_a_ready_disk = compaction.table_info_a.? == .disk and
1221
- compaction.level_a_value_block.head() != null and
1222
- compaction.level_a_value_block.head().?.stage == .read_value_block_done;
1223
- const level_a_ready = level_a_ready_immutable or level_a_ready_disk;
1224
-
1225
- const level_a_exhausted_immutable = compaction.table_info_a.? == .immutable and
1226
- compaction.level_a_immutable_stage == .exhausted;
1227
- const level_a_exhausted_disk = compaction.table_info_a.? == .disk and
1228
- compaction.level_a_index_block.count == 0 and
1229
- compaction.level_a_value_block.count == 0;
1230
- const level_a_exhausted = level_a_exhausted_immutable or level_a_exhausted_disk;
1231
-
1232
- const level_b_ready = compaction.level_b_value_block.head() != null and
1233
- compaction.level_b_value_block.head().?.stage == .read_value_block_done;
1234
-
1235
- const level_b_exhausted =
1236
- compaction.level_b_index_block.count == 0 and
1237
- compaction.level_b_value_block.count == 0;
1238
- const levels_exhausted = level_a_exhausted and level_b_exhausted;
1239
-
1240
- assert(levels_exhausted == compaction.quotas.bar_exhausted());
1241
-
1242
- if (compaction.table_builder.state == .index_and_value_block) {
1243
- if (level_a_exhausted and level_b_exhausted) {
1244
- assert(compaction.stage == .beat_quota_done);
1245
- } else if ((level_a_exhausted or level_a_ready) and
1246
- (level_b_exhausted or level_b_ready) and
1247
- !compaction.table_builder.value_block_full())
1248
- {
1249
- const cpu = compaction.pool.?.cpus.acquire().?;
1250
- compaction.merge(cpu);
1251
- progressed = true;
1252
- }
1253
-
1254
- // Write value and index blocks. It is important for correctness that both the
1255
- // value block and index block are written together. Otherwise, we may end up
1256
- // overflowing the index block's capacity for value block addresses.
1257
- if (compaction.output_blocks.spare_capacity() >= 2) {
1258
- if (compaction.table_builder.value_block_full()) {
1259
- assert(!compaction.output_blocks.full());
1260
- const write = compaction.pool.?.writes.acquire().?;
1261
- compaction.write_value_block(write, .{
1262
- .address = compaction.grid.acquire(
1263
- compaction.pool.?.grid_reservation.?,
1264
- ),
1265
- });
1266
- progressed = true;
1267
- }
1268
-
1269
- if (compaction.table_builder.index_block_full()) {
1270
- assert(!compaction.output_blocks.full());
1271
- const write = compaction.pool.?.writes.acquire().?;
1272
- compaction.write_index_block(write, .{
1273
- .address = compaction.grid.acquire(
1274
- compaction.pool.?.grid_reservation.?,
1275
- ),
1276
- });
1277
- progressed = true;
1278
- }
1279
- }
1280
- }
1281
- } else unreachable;
1282
- assert(!progressed);
1283
- assert(!compaction.pool.?.idle());
1284
- }
1285
-
1286
- fn compaction_dispatch_beat_quota_done(compaction: *Compaction) void {
1287
- assert(compaction.stage == .beat_quota_done);
1288
-
1289
- if (compaction.table_builder.state == .index_and_value_block and
1290
- (compaction.table_builder.value_block_full() or compaction.quotas.bar_exhausted()))
1291
- {
1292
- if (compaction.table_builder.value_block_empty()) {
1293
- assert(compaction.quotas.bar_exhausted());
1294
- const value_block = compaction.table_builder_value_block.?;
1295
- compaction.table_builder_value_block = null;
1296
- compaction.table_builder.state = .index_block;
1297
- assert(value_block.stage == .build_value_block);
1298
- value_block.stage = .free;
1299
- compaction.pool.?.block_release(value_block);
1300
- } else {
1301
- if (!compaction.output_blocks.full()) {
1302
- const write = compaction.pool.?.writes.acquire().?;
1303
- compaction.write_value_block(write, .{
1304
- .address = compaction.grid.acquire(
1305
- compaction.pool.?.grid_reservation.?,
1306
- ),
1307
- });
1308
- assert(compaction.table_builder.state == .index_block);
1309
- assert(compaction.table_builder_value_block == null);
1310
- }
1311
- }
1312
- }
1313
-
1314
- if (compaction.table_builder.state == .index_block and
1315
- (compaction.table_builder.index_block_full() or compaction.quotas.bar_exhausted()))
1316
- {
1317
- if (compaction.table_builder.index_block_empty()) {
1318
- assert(compaction.quotas.bar_exhausted());
1319
- const index_block = compaction.table_builder_index_block.?;
1320
- compaction.table_builder_index_block = null;
1321
- compaction.table_builder.state = .no_blocks;
1322
- assert(index_block.stage == .build_index_block);
1323
- index_block.stage = .free;
1324
- compaction.pool.?.block_release(index_block);
1325
- } else {
1326
- if (!compaction.output_blocks.full()) {
1327
- const write = compaction.pool.?.writes.acquire().?;
1328
- compaction.write_index_block(write, .{
1329
- .address = compaction.grid.acquire(
1330
- compaction.pool.?.grid_reservation.?,
1331
- ),
1332
- });
1333
- assert(compaction.table_builder.state == .no_blocks);
1334
- assert(compaction.table_builder_value_block == null);
1335
- assert(compaction.table_builder_index_block == null);
1336
- }
1337
- }
1338
- }
1339
-
1340
- if (compaction.output_blocks.count > 0) {
1341
- return;
1342
- }
1343
-
1344
- switch (compaction.table_builder.state) {
1345
- .no_blocks => {},
1346
- .index_and_value_block => {
1347
- assert(!compaction.table_builder.index_block_full());
1348
- assert(!compaction.table_builder.value_block_full());
1349
- assert(!compaction.quotas.bar_exhausted());
1350
- },
1351
- .index_block => {
1352
- assert(!compaction.table_builder.index_block_full());
1353
- assert(!compaction.quotas.bar_exhausted());
1354
- },
1355
- }
1356
-
1357
- var level_a_value_block_iterator = compaction.level_a_value_block.iterator();
1358
- while (level_a_value_block_iterator.next()) |block| {
1359
- if (block.stage == .read_value_block) return;
1360
-
1361
- assert(block.stage == .read_value_block_done);
1362
- }
1363
-
1364
- var level_a_index_block_iterator = compaction.level_a_index_block.iterator();
1365
- while (level_a_index_block_iterator.next()) |block| {
1366
- if (block.stage == .read_index_block) return;
1367
-
1368
- assert(block.stage == .read_index_block_done);
1369
- }
1370
-
1371
- var level_b_value_block_iterator = compaction.level_b_value_block.iterator();
1372
- while (level_b_value_block_iterator.next()) |block| {
1373
- if (block.stage == .read_value_block) return;
1374
-
1375
- assert(block.stage == .read_value_block_done);
1376
- }
1377
-
1378
- var level_b_index_block_iterator = compaction.level_b_index_block.iterator();
1379
- while (level_b_index_block_iterator.next()) |block| {
1380
- if (block.stage == .read_index_block) return;
1381
-
1382
- assert(block.stage == .read_index_block_done);
1383
- }
1384
-
1385
- compaction.grid.trace.stop(.{ .compact_beat = .{
1386
- .tree = @enumFromInt(compaction.tree.config.id),
1387
- .level_b = compaction.level_b,
1388
- } });
1389
- compaction.beat_complete();
1390
- }
1391
-
1392
- fn read_index_block(
1393
- compaction: *Compaction,
1394
- level: enum { level_a, level_b },
1395
- read: *ResourcePool.BlockRead,
1396
- index_block: *ResourcePool.Block,
1397
- ) void {
1398
- const level_b_index_block_next =
1399
- compaction.level_b_position.index_block +
1400
- @as(u32, @intCast(compaction.level_b_index_block.count));
1401
-
1402
- assert(compaction.stage == .beat or compaction.stage == .beat_quota_done);
1403
- assert(index_block.stage == .read_index_block);
1404
- switch (level) {
1405
- .level_a => assert(compaction.level_a_position.index_block == 0),
1406
- .level_b => {
1407
- assert(level_b_index_block_next - 1 < compaction.range_b.?.tables.count());
1408
- assert(level_b_index_block_next > 0);
1409
- },
1410
- }
1411
-
1412
- const table_ref = switch (level) {
1413
- .level_a => compaction.table_info_a.?.disk,
1414
- .level_b => compaction.range_b.?.tables.get(level_b_index_block_next - 1),
1415
- };
1416
- read.block = index_block;
1417
- read.compaction = compaction;
1418
- compaction.grid.read_block(
1419
- .{ .from_local_or_global_storage = read_index_block_callback },
1420
- &read.grid_read,
1421
- table_ref.table_info.address,
1422
- table_ref.table_info.checksum,
1423
- .{ .cache_read = true, .cache_write = true },
1424
- );
1425
- }
1426
-
1427
- fn read_index_block_callback(grid_read: *Grid.Read, index_block: BlockPtrConst) void {
1428
- const read: *ResourcePool.BlockRead = @fieldParentPtr("grid_read", grid_read);
1429
- const compaction: *Compaction = read.parent(Compaction);
1430
- const block = read.block;
1431
- compaction.pool.?.reads.release(read);
1432
-
1433
- assert(block.stage == .read_index_block);
1434
- stdx.copy_disjoint(.exact, u8, block.ptr, index_block);
1435
- block.stage = .read_index_block_done;
1436
- compaction.compaction_dispatch();
1437
- }
1438
-
1439
- fn read_value_block(
1440
- compaction: *Compaction,
1441
- level: enum { level_a, level_b },
1442
- read: *ResourcePool.BlockRead,
1443
- value_block: *ResourcePool.Block,
1444
- ) void {
1445
- assert(compaction.stage == .beat or compaction.stage == .beat_quota_done);
1446
- assert(value_block.stage == .read_value_block);
1447
- if (level == .level_a) assert(compaction.table_info_a.? == .disk);
1448
-
1449
- const index_block = switch (level) {
1450
- .level_a => compaction.level_a_index_block.head().?,
1451
- .level_b => compaction.level_b_index_block.head().?,
1452
- };
1453
-
1454
- const level_a_value_block_next =
1455
- compaction.level_a_position.value_block +
1456
- @as(u32, @intCast(compaction.level_a_value_block.count));
1457
- const level_b_value_block_next =
1458
- compaction.level_b_position.value_block +
1459
- @as(u32, @intCast(compaction.level_b_value_block.count));
1460
-
1461
- const value_block_index = blk: {
1462
- switch (level) {
1463
- .level_a => {
1464
- assert(level_a_value_block_next > 0);
1465
- break :blk level_a_value_block_next - 1;
1466
- },
1467
- .level_b => {
1468
- assert(level_b_value_block_next > 0);
1469
- break :blk level_b_value_block_next - 1;
1470
- },
1471
- }
1472
- };
1473
-
1474
- const index_schema = schema.TableIndex.from(index_block.ptr);
1475
-
1476
- const value_block_address =
1477
- index_schema.value_addresses_used(index_block.ptr)[value_block_index];
1478
- const value_block_checksum =
1479
- index_schema.value_checksums_used(index_block.ptr)[value_block_index];
1480
-
1481
- read.block = value_block;
1482
- read.compaction = compaction;
1483
- compaction.grid.read_block(
1484
- .{ .from_local_or_global_storage = read_value_block_callback },
1485
- &read.grid_read,
1486
- value_block_address,
1487
- value_block_checksum.value,
1488
- .{ .cache_read = true, .cache_write = true },
1489
- );
1490
- }
1491
-
1492
- // TODO: Support for LSM snapshots would require us to only remove blocks
1493
- // that are invisible.
1494
- fn read_value_block_release_table(
1495
- compaction: *Compaction,
1496
- index_block: BlockPtrConst,
1497
- ) void {
1498
- const index_schema = schema.TableIndex.from(index_block);
1499
- const index_block_address = Table.block_address(index_block);
1500
- const value_block_addresses = index_schema.value_addresses_used(index_block);
1501
-
1502
- // Tables are released when the index block is no longer needed. Given that the same
1503
- // index block can get re-read across the bar, the same table can be released twice.
1504
- if (compaction.grid.free_set.is_released(index_block_address)) {
1505
- for (value_block_addresses) |address| {
1506
- assert(compaction.grid.free_set.is_released(address));
1507
- }
1508
- } else {
1509
- compaction.grid.release(value_block_addresses);
1510
- compaction.grid.release(&.{index_block_address});
1511
- }
1512
- }
1513
-
1514
- fn read_value_block_callback(grid_read: *Grid.Read, value_block: BlockPtrConst) void {
1515
- const read: *ResourcePool.BlockRead = @fieldParentPtr("grid_read", grid_read);
1516
- const compaction: *Compaction = read.parent(Compaction);
1517
- const block = read.block;
1518
- compaction.pool.?.reads.release(read);
1519
-
1520
- assert(block.stage == .read_value_block);
1521
- stdx.copy_disjoint(.exact, u8, block.ptr, value_block);
1522
- block.stage = .read_value_block_done;
1523
- compaction.counters.in += Table.value_block_values_used(block.ptr).len;
1524
- compaction.compaction_dispatch();
1525
- }
1526
-
1527
- fn merge(compaction: *Compaction, cpu: *ResourcePool.CPU) void {
1528
- assert(!compaction.quotas.bar_exhausted());
1529
-
1530
- if (compaction.table_info_a.? == .immutable) {
1531
- if (compaction.level_a_immutable_stage == .ready) {
1532
- compaction.level_a_immutable_stage = .merge;
1533
- } else assert(compaction.level_a_immutable_stage == .exhausted);
1534
- } else {
1535
- if (compaction.level_a_value_block.head()) |block| {
1536
- assert(block.stage == .read_value_block_done);
1537
- block.stage = .merge;
1538
- } else assert(compaction.level_b_value_block.head() != null);
1539
- }
1540
-
1541
- if (compaction.level_b_value_block.head()) |block| {
1542
- assert(block.stage == .read_value_block_done);
1543
- block.stage = .merge;
1544
- }
1545
-
1546
- assert(compaction.table_builder.state == .index_and_value_block);
1547
-
1548
- cpu.compaction = compaction;
1549
- compaction.grid.on_next_tick(merge_callback, &cpu.next_tick);
1550
- }
1551
-
1552
- fn merge_callback(next_tick: *Grid.NextTick) void {
1553
- const cpu: *ResourcePool.CPU = @fieldParentPtr("next_tick", next_tick);
1554
- const compaction: *Compaction = cpu.parent(Compaction);
1555
- compaction.pool.?.cpus.release(cpu);
1556
- assert(compaction.table_builder.state == .index_and_value_block);
1557
-
1558
- compaction.grid.trace.start(.{ .compact_beat_merge = .{
1559
- .tree = @enumFromInt(compaction.tree.config.id),
1560
- .level_b = compaction.level_b,
1561
- } });
1562
-
1563
- const values_source_a, const values_source_b = compaction.merge_inputs();
1564
- assert(values_source_a != null or values_source_b != null);
1565
-
1566
- const values_target = compaction.table_builder
1567
- .value_block_values()[compaction.table_builder.value_count..];
1568
-
1569
- inline for ([_]?[]const Value{
1570
- values_source_a,
1571
- values_source_b,
1572
- values_target,
1573
- }) |values_maybe| {
1574
- if (values_maybe) |values| {
1575
- assert(values.len > 0);
1576
- assert(values.len <= Table.data.value_count_max);
1577
- }
1578
- }
1579
-
1580
- // Do the actual merge from inputs to the output (table builder).
1581
- const merge_result: MergeResult = if (values_source_a == null) blk: {
1582
- const consumed = values_copy(values_target, values_source_b.?);
1583
- break :blk .{
1584
- .consumed_a = 0,
1585
- .consumed_b = consumed,
1586
- .dropped = 0,
1587
- .produced = consumed,
1588
- };
1589
- } else if (values_source_b == null) blk: {
1590
- if (compaction.drop_tombstones) {
1591
- const copy_result = values_copy_drop_tombstones(
1592
- values_target,
1593
- values_source_a.?,
1594
- );
1595
- break :blk .{
1596
- .consumed_a = copy_result.consumed,
1597
- .consumed_b = 0,
1598
- .dropped = copy_result.dropped,
1599
- .produced = copy_result.produced,
1600
- };
1601
- } else {
1602
- const consumed = values_copy(values_target, values_source_a.?);
1603
- break :blk .{
1604
- .consumed_a = consumed,
1605
- .consumed_b = 0,
1606
- .dropped = 0,
1607
- .produced = consumed,
1608
- };
1609
- }
1610
- } else values_merge(
1611
- values_target,
1612
- values_source_a.?,
1613
- values_source_b.?,
1614
- compaction.drop_tombstones,
1615
- );
1616
-
1617
- compaction.level_a_position.value += merge_result.consumed_a;
1618
- compaction.level_b_position.value += merge_result.consumed_b;
1619
- compaction.table_builder.value_count += merge_result.produced;
1620
-
1621
- if (compaction.table_info_a.? == .immutable) {
1622
- assert(compaction.level_a_position.value <= Table.value_count_max);
1623
- } else {
1624
- assert(compaction.level_a_position.value <= Table.data.value_count_max);
1625
- }
1626
- assert(compaction.level_b_position.value <= Table.data.value_count_max);
1627
- assert(compaction.table_builder.value_count <= Table.data.value_count_max);
1628
-
1629
- const consumed_ab = merge_result.consumed_a + merge_result.consumed_b;
1630
-
1631
- compaction.quotas.bar_done += consumed_ab;
1632
- compaction.quotas.beat_done += consumed_ab;
1633
-
1634
- compaction.counters.dropped += merge_result.dropped;
1635
-
1636
- assert(compaction.quotas.bar_done <= compaction.quotas.bar);
1637
-
1638
- compaction.merge_advance_position();
1639
-
1640
- // NB: although all the work here is synchronous, we don't defer trace.stop precisely
1641
- // to exclude compaction.dispatch call below.
1642
- compaction.grid.trace.stop(.{ .compact_beat_merge = .{
1643
- .tree = @enumFromInt(compaction.tree.config.id),
1644
- .level_b = compaction.level_b,
1645
- } });
1646
- compaction.compaction_dispatch();
1647
- }
1648
-
1649
- fn merge_inputs(compaction: *const Compaction) struct { ?[]const Value, ?[]const Value } {
1650
- const level_a_values_used: ?[]const Value = values: {
1651
- switch (compaction.table_info_a.?) {
1652
- .immutable => {
1653
- if (compaction.level_a_immutable_stage == .merge) {
1654
- break :values compaction.table_info_a.?.immutable;
1655
- } else {
1656
- assert(compaction.level_a_immutable_stage == .exhausted);
1657
- break :values null;
1658
- }
1659
- },
1660
- .disk => {
1661
- if (compaction.level_a_value_block.head()) |block| {
1662
- assert(block.stage == .merge);
1663
- break :values Table.value_block_values_used(block.ptr);
1664
- } else {
1665
- break :values null;
1666
- }
1667
- },
1668
- }
1669
- };
1670
-
1671
- const level_b_values_used: ?[]const Value = values: {
1672
- if (compaction.level_b_value_block.head()) |block| {
1673
- assert(block.stage == .merge);
1674
- break :values Table.value_block_values_used(block.ptr);
1675
- } else {
1676
- break :values null;
1677
- }
1678
- };
1679
- assert(!(level_a_values_used == null and level_b_values_used == null));
1680
-
1681
- const level_a_values = if (level_a_values_used) |values_used| values: {
1682
- const values_remaining = values_used[compaction.level_a_position.value..];
1683
- // Only consume one block at a time so that a beat never outputs past its quota
1684
- // by more than one value block.
1685
- const limit = @min(
1686
- Table.data.value_count_max,
1687
- values_remaining.len,
1688
- );
1689
- break :values values_remaining[0..limit];
1690
- } else null;
1691
-
1692
- const level_b_values = if (level_b_values_used) |values_used|
1693
- values_used[compaction.level_b_position.value..]
1694
- else
1695
- null;
1696
-
1697
- return .{ level_a_values, level_b_values };
1698
- }
1699
-
1700
- // merge_callback advances just position.values. Here, we implement the carry-flag logic,
1701
- // advancing value_block and index_block. This is also the place where determine that the
1702
- // beat's quota of work is done and begin to wind down the dispatch loop.
1703
- fn merge_advance_position(compaction: *Compaction) void {
1704
- if (compaction.table_info_a.? == .immutable) {
1705
- if (compaction.level_a_immutable_stage == .merge) {
1706
- if (compaction.level_a_position.value ==
1707
- compaction.table_info_a.?.immutable.len)
1708
- {
1709
- compaction.level_a_position.value_block += 1;
1710
- assert(compaction.level_a_position.value_block == 1);
1711
- compaction.level_a_position.value = 0;
1712
- compaction.level_a_immutable_stage = .exhausted;
1713
- } else {
1714
- compaction.level_a_immutable_stage = .ready;
1715
- }
1716
- } else {
1717
- assert(compaction.level_a_immutable_stage == .exhausted);
1718
- }
1719
- } else {
1720
- if (compaction.level_a_value_block.head()) |value_block| {
1721
- assert(value_block.stage == .merge);
1722
- if (compaction.level_a_position.value ==
1723
- Table.value_block_values_used(value_block.ptr).len)
1724
- {
1725
- _ = compaction.level_a_value_block.pop();
1726
-
1727
- compaction.level_a_position.value_block += 1;
1728
- compaction.level_a_position.value = 0;
1729
-
1730
- const index_block = compaction.level_a_index_block.head().?;
1731
- assert(index_block.stage == .read_index_block_done);
1732
- const index_schema = schema.TableIndex.from(index_block.ptr);
1733
- const value_blocks_count =
1734
- index_schema.value_blocks_used(index_block.ptr);
1735
-
1736
- // It is imperative that we pop the index block when the final value block
1737
- // is popped. While it is tempting to pop the index block when we issue
1738
- // a read for the final value block, this would be incorrect as it would
1739
- // lead to an incorrect index being computed for level_a_value_block_next
1740
- // in `compaction_dispatch`.
1741
- if (compaction.level_a_position.value_block == value_blocks_count) {
1742
- compaction.level_a_position.index_block += 1;
1743
- assert(compaction.level_a_position.index_block == 1);
1744
- compaction.level_a_position.value_block = 0;
1745
-
1746
- const popped = compaction.level_a_index_block.pop().?;
1747
- assert(popped == index_block);
1748
- compaction.read_value_block_release_table(index_block.ptr);
1749
- index_block.stage = .free;
1750
- compaction.pool.?.block_release(index_block);
1751
- }
1752
-
1753
- value_block.stage = .free;
1754
- compaction.pool.?.block_release(value_block);
1755
- } else {
1756
- value_block.stage = .read_value_block_done;
1757
- }
1758
- } else {
1759
- assert(compaction.level_a_position.value == 0); // Level A exhausted.
1760
- }
1761
- }
1762
-
1763
- if (compaction.level_b_value_block.head()) |value_block| {
1764
- assert(value_block.stage == .merge);
1765
- if (compaction.level_b_position.value ==
1766
- Table.value_block_values_used(value_block.ptr).len)
1767
- {
1768
- _ = compaction.level_b_value_block.pop().?;
1769
- compaction.level_b_position.value_block += 1;
1770
- compaction.level_b_position.value = 0;
1771
-
1772
- const index_block = compaction.level_b_index_block.head().?;
1773
- assert(index_block.stage == .read_index_block_done);
1774
- const index_schema = schema.TableIndex.from(index_block.ptr);
1775
- const value_blocks_count =
1776
- index_schema.value_blocks_used(index_block.ptr);
1777
-
1778
- // It is imperative that we pop the index block when the final value block
1779
- // is popped. While it is tempting to pop the index block when we issue
1780
- // a read for the final value block, this would be incorrect as it would
1781
- // lead to an incorrect index being computed for level_b_value_block_next
1782
- // in `compaction_dispatch`.
1783
- if (compaction.level_b_position.value_block == value_blocks_count) {
1784
- compaction.level_b_position.index_block += 1;
1785
- compaction.level_b_position.value_block = 0;
1786
-
1787
- const popped = compaction.level_b_index_block.pop().?;
1788
- assert(popped == index_block);
1789
- compaction.read_value_block_release_table(index_block.ptr);
1790
- index_block.stage = .free;
1791
-
1792
- compaction.pool.?.block_release(index_block);
1793
- }
1794
-
1795
- value_block.stage = .free;
1796
- compaction.pool.?.block_release(value_block);
1797
- } else {
1798
- value_block.stage = .read_value_block_done;
1799
- }
1800
- } else {
1801
- assert(compaction.level_b_position.value == 0); // Level B exhausted.
1802
- }
1803
-
1804
- if (compaction.quotas.beat_exhausted()) {
1805
- assert(compaction.stage == .beat);
1806
- compaction.stage = .beat_quota_done;
1807
- }
1808
- }
1809
-
1810
- fn write_value_block(
1811
- compaction: *Compaction,
1812
- write: *ResourcePool.BlockWrite,
1813
- options: struct { address: u64 },
1814
- ) void {
1815
- const block = compaction.table_builder_value_block.?;
1816
- assert(block.stage == .build_value_block);
1817
- assert(compaction.table_builder.value_block == block.ptr);
1818
- assert(!compaction.output_blocks.full());
1819
-
1820
- compaction.counters.out += compaction.table_builder.value_count;
1821
- compaction.table_builder.value_block_finish(.{
1822
- .cluster = compaction.grid.superblock.working.cluster,
1823
- .release = compaction.grid.superblock.working.vsr_state.checkpoint.release,
1824
- .address = options.address,
1825
- .snapshot_min = snapshot_min_for_table_output(compaction.op_min),
1826
- .tree_id = compaction.tree.config.id,
1827
- });
1828
- assert(compaction.table_builder.state == .index_block);
1829
- compaction.table_builder_value_block = null;
1830
-
1831
- compaction.output_blocks.push_assume_capacity({});
1832
- block.stage = .write_value_block;
1833
-
1834
- write.block = block;
1835
- write.compaction = compaction;
1836
- compaction.grid.create_block(write_block_callback, &write.grid_write, &write.block.ptr);
1837
- }
1838
-
1839
- fn write_index_block(
1840
- compaction: *Compaction,
1841
- write: *ResourcePool.BlockWrite,
1842
- options: struct { address: u64 },
1843
- ) void {
1844
- const block = compaction.table_builder_index_block.?;
1845
- assert(block.stage == .build_index_block);
1846
- assert(compaction.table_builder.index_block == block.ptr);
1847
- assert(!compaction.output_blocks.full());
1848
-
1849
- const table = compaction.table_builder.index_block_finish(.{
1850
- .cluster = compaction.grid.superblock.working.cluster,
1851
- .release = compaction.grid.superblock.working.vsr_state.checkpoint.release,
1852
- .address = options.address,
1853
- .snapshot_min = snapshot_min_for_table_output(compaction.op_min),
1854
- .tree_id = compaction.tree.config.id,
1855
- });
1856
- assert(compaction.table_builder.state == .no_blocks);
1857
- compaction.table_builder_index_block = null;
1858
-
1859
- compaction.manifest_entries.push(.{
1860
- .operation = .insert_to_level_b,
1861
- .table = table,
1862
- });
1863
-
1864
- compaction.output_blocks.push_assume_capacity({});
1865
- block.stage = .write_index_block;
1866
-
1867
- write.block = block;
1868
- write.compaction = compaction;
1869
- compaction.grid.create_block(write_block_callback, &write.grid_write, &write.block.ptr);
1870
- }
1871
-
1872
- fn write_block_callback(grid_write: *Grid.Write) void {
1873
- const write: *ResourcePool.BlockWrite = @fieldParentPtr("grid_write", grid_write);
1874
- const compaction: *Compaction = write.parent(Compaction);
1875
- const block = write.block;
1876
- compaction.pool.?.writes.release(write);
1877
-
1878
- assert(block.stage == .write_value_block or block.stage == .write_index_block);
1879
- block.stage = .free;
1880
- compaction.pool.?.block_release(block);
1881
-
1882
- const popped = compaction.output_blocks.pop();
1883
- assert(popped != null);
1884
-
1885
- compaction.compaction_dispatch();
1886
- }
1887
-
1888
- // The three functions below are hot CPU loops doing the actual merging, TigerBeetle's data
1889
- // plane. To reduce the probability of the optimizer getting confused over pointers, don't
1890
- // use 'self' and instead specify all inputs and outputs explicitly. Its the caller's job to
1891
- // apply control plane changes to the compaction state.
1892
- //
1893
- // TODO: Add micro benchmarks.
1894
-
1895
- fn values_copy(values_target: []Value, values_source: []const Value) u32 {
1896
- assert(values_source.len > 0);
1897
- assert(values_source.len <= Table.data.value_count_max);
1898
- assert(values_target.len > 0);
1899
- assert(values_target.len <= Table.data.value_count_max);
1900
-
1901
- const len: u32 = @intCast(@min(values_source.len, values_target.len));
1902
- stdx.copy_disjoint(
1903
- .exact,
1904
- Value,
1905
- values_target[0..len],
1906
- values_source[0..len],
1907
- );
1908
-
1909
- return len;
1910
- }
1911
-
1912
- const CopyDropTombstonesResult = struct {
1913
- consumed: u32,
1914
- dropped: u32,
1915
- produced: u32,
1916
- };
1917
- /// Copy values from values_source to values_target, dropping tombstones as we go.
1918
- fn values_copy_drop_tombstones(
1919
- values_target: []Value,
1920
- values_source: []const Value,
1921
- ) CopyDropTombstonesResult {
1922
- assert(values_source.len > 0);
1923
- assert(values_source.len <= Table.data.value_count_max);
1924
- assert(values_target.len > 0);
1925
- assert(values_target.len <= Table.data.value_count_max);
1926
-
1927
- var index_source: usize = 0;
1928
- var index_target: usize = 0;
1929
- // Merge as many values as possible.
1930
- while (index_source < values_source.len and
1931
- index_target < values_target.len)
1932
- {
1933
- const value_in = &values_source[index_source];
1934
- index_source += 1;
1935
- if (tombstone(value_in)) {
1936
- assert(Table.usage != .secondary_index);
1937
- continue;
1938
- }
1939
- values_target[index_target] = value_in.*;
1940
- index_target += 1;
1941
- }
1942
- const copy_result: CopyDropTombstonesResult = .{
1943
- .consumed = @intCast(index_source),
1944
- .dropped = @intCast(index_source - index_target),
1945
- .produced = @intCast(index_target),
1946
- };
1947
- assert(copy_result.consumed > 0);
1948
- assert(copy_result.consumed <= values_source.len);
1949
- assert(copy_result.dropped <= copy_result.consumed);
1950
- assert(copy_result.produced <= values_target.len);
1951
- assert(copy_result.produced == copy_result.consumed - copy_result.dropped);
1952
- return copy_result;
1953
- }
1954
-
1955
- const MergeResult = struct {
1956
- consumed_a: u32,
1957
- consumed_b: u32,
1958
- dropped: u32,
1959
- produced: u32,
1960
- };
1961
-
1962
- /// Merge values from table_a and table_b, with table_a taking precedence. Tombstones may
1963
- /// or may not be dropped depending on bar.drop_tombstones.
1964
- fn values_merge(
1965
- values_target: []Value,
1966
- values_source_a: []const Value,
1967
- values_source_b: []const Value,
1968
- drop_tombstones: bool,
1969
- ) MergeResult {
1970
- assert(values_source_a.len > 0);
1971
- assert(values_source_a.len <= Table.data.value_count_max);
1972
- assert(values_source_b.len > 0);
1973
- assert(values_source_b.len <= Table.data.value_count_max);
1974
- assert(values_target.len > 0);
1975
- assert(values_target.len <= Table.data.value_count_max);
1976
-
1977
- var index_source_a: usize = 0;
1978
- var index_source_b: usize = 0;
1979
- var index_target: usize = 0;
1980
-
1981
- while (index_source_a < values_source_a.len and
1982
- index_source_b < values_source_b.len and
1983
- index_target < values_target.len)
1984
- {
1985
- const value_a = &values_source_a[index_source_a];
1986
- const value_b = &values_source_b[index_source_b];
1987
- switch (std.math.order(key_from_value(value_a), key_from_value(value_b))) {
1988
- .lt => { // Pick value from level a.
1989
- index_source_a += 1;
1990
- if (drop_tombstones and tombstone(value_a)) {
1991
- assert(Table.usage != .secondary_index);
1992
- continue;
1993
- }
1994
- values_target[index_target] = value_a.*;
1995
- index_target += 1;
1996
- },
1997
- .gt => { // Pick value from level b.
1998
- index_source_b += 1;
1999
- values_target[index_target] = value_b.*;
2000
- index_target += 1;
2001
- },
2002
- .eq => { // Values have equal keys -- collapse them!
2003
- index_source_a += 1;
2004
- index_source_b += 1;
2005
-
2006
- if (comptime Table.usage == .secondary_index) {
2007
- // Secondary index optimization --- cancel out put and remove.
2008
- assert(tombstone(value_a) != tombstone(value_b));
2009
- } else {
2010
- if (drop_tombstones and tombstone(value_a)) continue;
2011
- values_target[index_target] = value_a.*;
2012
- index_target += 1;
2013
- }
2014
- },
2015
- }
2016
- }
2017
-
2018
- const merge_result: MergeResult = .{
2019
- .consumed_a = @intCast(index_source_a),
2020
- .consumed_b = @intCast(index_source_b),
2021
- .dropped = @intCast(index_source_a + index_source_b - index_target),
2022
- .produced = @intCast(index_target),
2023
- };
2024
- assert(merge_result.consumed_a > 0 or merge_result.consumed_b > 0);
2025
- assert(merge_result.consumed_a <= values_source_a.len);
2026
- assert(merge_result.consumed_b <= values_source_b.len);
2027
- assert(merge_result.dropped <= merge_result.consumed_a + merge_result.consumed_b);
2028
- assert(merge_result.produced <= values_target.len);
2029
- assert(merge_result.produced ==
2030
- merge_result.consumed_a + merge_result.consumed_b - merge_result.dropped);
2031
- return merge_result;
2032
- }
2033
- };
2034
- }
2035
-
2036
- pub fn snapshot_max_for_table_input(op_min: u64) u64 {
2037
- return snapshot_min_for_table_output(op_min) - 1;
2038
- }
2039
-
2040
- pub fn snapshot_min_for_table_output(op_min: u64) u64 {
2041
- assert(op_min > 0);
2042
- assert(op_min % @divExact(constants.lsm_compaction_ops, 2) == 0);
2043
- return op_min + @divExact(constants.lsm_compaction_ops, 2);
2044
- }
2045
-
2046
- /// Returns the first op of the compaction (Compaction.op_min) for a given op/beat.
2047
- ///
2048
- /// After this compaction finishes:
2049
- /// - `op_min + half_bar_beat_count - 1` will be the input tables' snapshot_max.
2050
- /// - `op_min + half_bar_beat_count` will be the output tables' snapshot_min.
2051
- ///
2052
- /// Each half-bar has a separate op_min (for deriving the output snapshot_min) instead of each full
2053
- /// bar because this allows the output tables of the first half-bar's compaction to be prefetched
2054
- /// against earlier — hopefully while they are still warm in the cache from being written.
2055
- ///
2056
- ///
2057
- /// These charts depict the commit/compact ops over a series of
2058
- /// commits and compactions (with lsm_compaction_ops=8).
2059
- ///
2060
- /// Legend:
2061
- ///
2062
- /// ┼ full bar (first half-bar start)
2063
- /// ┬ half bar (second half-bar start)
2064
- /// This is incremented at the end of each compact().
2065
- /// . op is in mutable table (in memory)
2066
- /// , op is in immutable table (in memory)
2067
- /// # op is on disk
2068
- /// ✓ checkpoint() may follow compact()
2069
- ///
2070
- /// 0 2 4 6 8 0 2 4 6
2071
- /// ┼───┬───┼───┬───┼
2072
- /// . ╷ ╷ init(superblock.commit_min=0)⎤ Compaction is effectively a noop for the
2073
- /// .. ╷ ╷ commit;compact( 1) start/end ⎥ first bar because there are no tables on
2074
- /// ... ╷ ╷ commit;compact( 2) start/end ⎥ disk yet, and no immutable table to
2075
- /// .... ╷ ╷ commit;compact( 3) start/end ⎥ flush.
2076
- /// ..... ╷ ╷ commit;compact( 4) start/end ⎥
2077
- /// ...... ╷ ╷ commit;compact( 5) start/end ⎥ This applies:
2078
- /// ....... ╷ ╷ commit;compact( 6) start/end ⎥ - when the LSM is starting on a freshly
2079
- /// ........╷ ╷ commit;compact( 7) start ⎤⎥ formatted data file, and also
2080
- /// ,,,,,,,,. ╷ ✓ compact( 7) end⎦⎦ - when the LSM is recovering from a crash
2081
- /// ,,,,,,,,. ╷ commit;compact( 8) start/end (see below).
2082
- /// ,,,,,,,,.. ╷ commit;compact( 9) start/end
2083
- /// ,,,,,,,,... ╷ commit;compact(10) start/end
2084
- /// ,,,,,,,,.... ╷ commit;compact(11) start/end
2085
- /// ,,,,,,,,..... ╷ commit;compact(12) start/end
2086
- /// ,,,,,,,,...... ╷ commit;compact(13) start/end
2087
- /// ,,,,,,,,....... ╷ commit;compact(14) start/end
2088
- /// ,,,,,,,,........╷ commit;compact(15) start ⎤
2089
- /// ########,,,,,,,,╷ ✓ compact(15) end⎦
2090
- /// ########,,,,,,,,. commit;compact(16) start/end
2091
- /// ┼───┬───┼───┬───┼
2092
- /// 0 2 4 6 8 0 2 4 6
2093
- /// ┼───┬───┼───┬───┼ Recover with a checkpoint taken at op 15.
2094
- /// ######## ╷ init(superblock.commit_min=7) At op 15, ops 8…15 are in memory, so they
2095
- /// ########. ╷ commit ( 8) start/end ⎤ were dropped by the crash.
2096
- /// ########.. ╷ commit ( 9) start/end ⎥
2097
- /// ########... ╷ commit (10) start/end ⎥ But compaction is not run for ops 8…15
2098
- /// ########.... ╷ commit (11) start/end ⎥ because it was already performed
2099
- /// ########..... ╷ commit (12) start/end ⎥ before the checkpoint.
2100
- /// ########...... ╷ commit (13) start/end ⎥
2101
- /// ########....... ╷ commit (14) start/end ⎥ We can begin to compact again at op 16,
2102
- /// ########........╷ commit (15) start ⎤⎥ because those compactions (if previously
2103
- /// ########,,,,,,,,╷ ✓ (15) end⎦⎦ performed) are not included in the
2104
- /// ########,,,,,,,,. commit;compact(16) start/end checkpoint.
2105
- /// ┼───┬───┼───┬───┼
2106
- /// 0 2 4 6 8 0 2 4 6
2107
- ///
2108
- /// Notice how in the checkpoint recovery example above, we are careful not to `compact(op)` twice
2109
- /// for any op (even if we crash/recover), since that could lead to differences between replicas'
2110
- /// storage. The last bar of `commit()`s is always only in memory, so it is safe to repeat.
2111
- pub fn compaction_op_min(op: u64) u64 {
2112
- assert(op >= half_bar_beat_count);
2113
- return op - op % half_bar_beat_count;
2114
- }