rubydb 0.1.5 → 0.1.6

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 (88) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +4 -0
  3. data/CHANGELOG.md +14 -0
  4. data/Gemfile.lock +1 -1
  5. data/README.md +296 -227
  6. data/Rakefile +6 -1
  7. data/accelerator/bin/SHA256SUMS +6 -0
  8. data/accelerator/bin/rubydb-accelerator-darwin-amd64 +0 -0
  9. data/accelerator/bin/rubydb-accelerator-darwin-arm64 +0 -0
  10. data/accelerator/bin/rubydb-accelerator-linux-amd64 +0 -0
  11. data/accelerator/bin/rubydb-accelerator-linux-arm64 +0 -0
  12. data/accelerator/bin/rubydb-accelerator-windows-amd64.exe +0 -0
  13. data/accelerator/bin/rubydb-accelerator-windows-arm64.exe +0 -0
  14. data/accelerator/cmd/rubydb-accelerator/main.go +11 -0
  15. data/accelerator/go.mod +3 -0
  16. data/accelerator/internal/execution/aggregate.go +94 -0
  17. data/accelerator/internal/execution/distinct.go +22 -0
  18. data/accelerator/internal/execution/filter.go +73 -0
  19. data/accelerator/internal/execution/join.go +79 -0
  20. data/accelerator/internal/execution/operators.go +167 -0
  21. data/accelerator/internal/execution/scan.go +20 -0
  22. data/accelerator/internal/execution/sort.go +62 -0
  23. data/accelerator/internal/execution/types.go +136 -0
  24. data/accelerator/internal/execution/value.go +67 -0
  25. data/accelerator/internal/memory/arena.go +47 -0
  26. data/accelerator/internal/memory/reuse.go +22 -0
  27. data/accelerator/internal/metrics/registry.go +67 -0
  28. data/accelerator/internal/parallel/bounded_queue.go +56 -0
  29. data/accelerator/internal/parallel/scheduler.go +47 -0
  30. data/accelerator/internal/parallel/worker_pool.go +53 -0
  31. data/accelerator/internal/protocol/cancellation.go +48 -0
  32. data/accelerator/internal/protocol/columnar.go +263 -0
  33. data/accelerator/internal/protocol/frame.go +187 -0
  34. data/accelerator/internal/runtime/worker.go +521 -0
  35. data/accelerator/internal/storage/page_reader.go +81 -0
  36. data/accelerator/internal/storage/snapshot_scan.go +539 -0
  37. data/accelerator/internal/wal/checksum.go +13 -0
  38. data/accelerator/internal/wal/compression.go +41 -0
  39. data/accelerator/internal/wal/group_commit.go +24 -0
  40. data/accelerator/internal/wal/record_encoder.go +40 -0
  41. data/adapters/activerecord/README.md +8 -3
  42. data/adapters/activerecord/lib/active_record/connection_adapters/rubydb_adapter.rb +50 -42
  43. data/adapters/activerecord/rubydb-activerecord.gemspec +1 -1
  44. data/docs/README.md +3 -1
  45. data/docs/architecture/go-accelerator.md +179 -0
  46. data/docs/cli.md +24 -0
  47. data/docs/contributing/benchmarking.md +16 -0
  48. data/docs/developer/local-development.md +32 -0
  49. data/docs/release.md +2 -2
  50. data/lessons/02-local-development.md +2 -2
  51. data/lessons/04-rails-complex-apps.md +2 -2
  52. data/lessons/05-rubydb-production-server.md +2 -2
  53. data/lessons/07-hybrid-microservices.md +175 -90
  54. data/lessons/10-release-readiness.md +184 -117
  55. data/lessons/11-community-adapter.md +323 -0
  56. data/lessons/12-rails-ecommerce-pressure.md +263 -0
  57. data/lib/rubydb/accelerator/client.rb +451 -0
  58. data/lib/rubydb/accelerator/error.rb +22 -0
  59. data/lib/rubydb/accelerator/manager.rb +606 -0
  60. data/lib/rubydb/accelerator.rb +13 -0
  61. data/lib/rubydb/cli/application.rb +6 -1
  62. data/lib/rubydb/cli/commands/accelerator.rb +72 -0
  63. data/lib/rubydb/cli/commands/doctor.rb +3 -0
  64. data/lib/rubydb/client/client.rb +7 -0
  65. data/lib/rubydb/client/connection.rb +15 -0
  66. data/lib/rubydb/client/result.rb +5 -1
  67. data/lib/rubydb/configuration/defaults.rb +12 -0
  68. data/lib/rubydb/configuration/validation.rb +8 -1
  69. data/lib/rubydb/execution/accelerator_dispatch.rb +30 -0
  70. data/lib/rubydb/execution/cost_model.rb +72 -0
  71. data/lib/rubydb/execution/executor.rb +373 -11
  72. data/lib/rubydb/execution/operator_selection.rb +57 -0
  73. data/lib/rubydb/execution/physical_plan.rb +47 -0
  74. data/lib/rubydb/execution/planner.rb +12 -46
  75. data/lib/rubydb/execution/sort_executor.rb +22 -8
  76. data/lib/rubydb/indexes/btree.rb +31 -2
  77. data/lib/rubydb/rubydb.rb +7 -1
  78. data/lib/rubydb/server/session.rb +45 -0
  79. data/lib/rubydb/storage/engine.rb +74 -12
  80. data/lib/rubydb/storage/snapshot_reader.rb +167 -0
  81. data/lib/rubydb/version.rb +1 -1
  82. data/lib/rubydb/wal/archive.rb +17 -0
  83. data/lib/rubydb/wal/wal.rb +1 -0
  84. data/rubydb.gemspec +12 -2
  85. data/scripts/build_accelerator +49 -0
  86. data/scripts/release +34 -4
  87. data/scripts/replication_failover_drill +2 -2
  88. metadata +49 -1
@@ -0,0 +1,606 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "base64"
4
+ require "digest"
5
+ require "json"
6
+ require "open3"
7
+ require "rbconfig"
8
+ require "securerandom"
9
+ require "timeout"
10
+
11
+ module RubyDB
12
+ module Accelerator
13
+ # Owns one long-lived Go accelerator process. It deliberately uses a
14
+ # private stdio channel instead of a public TCP listener: there is no port
15
+ # to expose, and the Ruby process remains the database owner.
16
+ class Manager
17
+ DEFAULT_TIMEOUT = 30
18
+ DEFAULT_MAX_FRAME_SIZE = 16 * 1024 * 1024
19
+ PROTOCOL_VERSION = 1
20
+ BINARY_VERSION = 1
21
+ BINARY_MAGIC = "RDBB".b
22
+ BINARY_ROWS_TYPE = 1
23
+ BINARY_JOIN_TYPE = 2
24
+ BINARY_SNAPSHOT_SCAN_TYPE = 3
25
+
26
+ attr_reader :mode, :binary_path, :min_rows, :read_pipeline, :capabilities, :max_rows, :max_result_bytes
27
+
28
+ def initialize(config = {})
29
+ config = config.transform_keys(&:to_sym)
30
+ @mode = (config[:mode] || ENV.fetch("RUBYDB_ACCELERATOR", "auto")).to_s.downcase
31
+ validate_mode!
32
+ @binary_path = resolve_binary(config[:binary] || ENV["RUBYDB_ACCELERATOR_BIN"])
33
+ @timeout = Float(config[:timeout] || ENV.fetch("RUBYDB_ACCELERATOR_TIMEOUT", DEFAULT_TIMEOUT))
34
+ @max_frame_size = Integer(config[:max_frame_size] || DEFAULT_MAX_FRAME_SIZE)
35
+ @max_rows = Integer(config[:max_rows] || ENV.fetch("RUBYDB_ACCELERATOR_MAX_ROWS", "1000000"))
36
+ @max_result_bytes = Integer(config[:max_result_bytes] || ENV.fetch("RUBYDB_ACCELERATOR_MAX_RESULT_BYTES", @max_frame_size))
37
+ @min_rows = Integer(config[:min_rows] || ENV.fetch("RUBYDB_ACCELERATOR_MIN_ROWS", 256))
38
+ @read_pipeline = (config[:read_pipeline] || ENV.fetch("RUBYDB_ACCELERATOR_READ_PIPELINE", "on")).to_s.downcase
39
+ raise ArgumentError, "read_pipeline must be off or on" unless %w[off on].include?(@read_pipeline)
40
+ raise ArgumentError, "min_rows must be non-negative" if @min_rows.negative?
41
+ raise ArgumentError, "timeout must be positive" unless @timeout.positive?
42
+ raise ArgumentError, "max_frame_size must be positive" unless @max_frame_size.positive?
43
+ raise ArgumentError, "max_rows must be positive" unless @max_rows.positive?
44
+ raise ArgumentError, "max_result_bytes must be positive" unless @max_result_bytes.positive?
45
+ @mutex = Mutex.new
46
+ @write_mutex = Mutex.new
47
+ @handshake_mutex = Mutex.new
48
+ @pending = {}
49
+ @stdin = nil
50
+ @stdout = nil
51
+ @stderr = nil
52
+ @wait_thread = nil
53
+ @reader_thread = nil
54
+ @stderr_thread = nil
55
+ @started = false
56
+ @handshaken = false
57
+ @stopping = false
58
+ @worker_started_once = false
59
+ @worker_starts = 0
60
+ @worker_restarts = 0
61
+ @worker_failures = 0
62
+ @capabilities = []
63
+ @performance_decisions = {}
64
+ @performance_samples = Hash.new { |hash, key| hash[key] = {count: 0, fallbacks: 0, total_ms: 0.0, rows: 0} }
65
+ @last_error = nil
66
+ rescue ArgumentError, TypeError => error
67
+ raise Error, "Invalid RubyDB accelerator configuration: #{error.message}"
68
+ end
69
+
70
+ def available?
71
+ return false if @mode == "off"
72
+
73
+ !@binary_path.nil? && File.file?(@binary_path)
74
+ end
75
+
76
+ def enabled?
77
+ @mode != "off" && available?
78
+ end
79
+
80
+ def started?
81
+ @mutex.synchronize { @started }
82
+ end
83
+
84
+ def stats
85
+ @mutex.synchronize do
86
+ {
87
+ mode: @mode,
88
+ available: available?,
89
+ started: @started,
90
+ binary_path: @binary_path,
91
+ protocol_version: PROTOCOL_VERSION,
92
+ capabilities: @capabilities.dup,
93
+ performance_decisions: @performance_decisions.dup,
94
+ performance_samples: @performance_samples.transform_values(&:dup),
95
+ min_rows: @min_rows,
96
+ max_rows: @max_rows,
97
+ max_result_bytes: @max_result_bytes,
98
+ read_pipeline: @read_pipeline,
99
+ worker_pid: worker_pid,
100
+ worker_starts: @worker_starts,
101
+ worker_restarts: @worker_restarts,
102
+ worker_failures: @worker_failures,
103
+ last_error: @last_error
104
+ }
105
+ end
106
+ end
107
+
108
+ # Restart the private worker after an external failure. The next request
109
+ # also starts a failed worker lazily, but this explicit operation is
110
+ # useful to supervisors and release/soak tests that need to verify the
111
+ # lifecycle independently of a query.
112
+ def restart
113
+ @mutex.synchronize do
114
+ stop_locked
115
+ @stopping = false
116
+ start_locked
117
+ end
118
+ true
119
+ end
120
+
121
+ def request(type, payload = {}, timeout: @timeout)
122
+ return nil if @mode == "off"
123
+ return nil unless prepare_transport!
124
+ ensure_handshake(timeout)
125
+ exchange_json(type.to_s, payload, timeout)
126
+ rescue Error
127
+ raise
128
+ rescue => error
129
+ @last_error = "#{error.class}: #{error.message}"
130
+ @mutex.synchronize { stop_locked }
131
+ raise ProtocolError, "RubyDB accelerator communication failed: #{error.message}"
132
+ end
133
+
134
+ # Sends a bounded binary request over the same private stdio channel.
135
+ # Large row batches avoid JSON and base64 conversion on both sides.
136
+ def binary_request(type, payload, timeout: @timeout)
137
+ return nil if @mode == "off"
138
+ return nil unless prepare_transport!
139
+ ensure_handshake(timeout)
140
+ exchange_binary(type.to_s, payload, timeout)
141
+ rescue Error
142
+ raise
143
+ rescue => error
144
+ @last_error = "#{error.class}: #{error.message}"
145
+ @mutex.synchronize { stop_locked }
146
+ raise ProtocolError, "RubyDB accelerator communication failed: #{error.message}"
147
+ end
148
+
149
+ # Collects a sequence of bounded JSON result batches from the worker.
150
+ # The worker may execute the request concurrently with other requests;
151
+ # only this request's queue is consumed here.
152
+ def request_stream(type, payload = {}, timeout: @timeout)
153
+ return nil if @mode == "off"
154
+ return nil unless prepare_transport!
155
+ ensure_handshake(timeout)
156
+
157
+ id = "acc_#{SecureRandom.hex(12)}"
158
+ queue = register_pending(id)
159
+ write_locked(message(type.to_s, payload, id))
160
+ rows = []
161
+ aggregates = nil
162
+ Timeout.timeout(timeout, TimeoutError) do
163
+ loop do
164
+ response = queue.pop
165
+ raise response if response.is_a?(Exception)
166
+ unless response["success"] == true
167
+ raise RequestError.new(response["error"].to_s, code: response["code"])
168
+ end
169
+
170
+ batch = response["payload"] || {}
171
+ rows.concat(Array(batch["rows"]))
172
+ aggregates = batch["aggregates"] if batch.key?("aggregates")
173
+ break unless response["more"] == true
174
+ end
175
+ end
176
+ {"rows" => rows, "row_count" => rows.length, "aggregates" => aggregates}
177
+ rescue TimeoutError
178
+ cancel_request(id, timeout)
179
+ raise TimeoutError.new("RubyDB accelerator request timed out", code: "timeout")
180
+ ensure
181
+ @mutex.synchronize { @pending.delete(id) } if id
182
+ end
183
+
184
+ def supports?(capability)
185
+ @mutex.synchronize { @capabilities.include?(capability.to_s) }
186
+ end
187
+
188
+ def performance_calibrated?(workload)
189
+ @mutex.synchronize { @performance_decisions.key?(workload.to_sym) }
190
+ end
191
+
192
+ def acceleration_preferred?(workload)
193
+ return true if @mode == "required"
194
+
195
+ @mutex.synchronize { @performance_decisions.fetch(workload.to_sym, true) }
196
+ end
197
+
198
+ def record_performance(workload, ruby_ms:, go_ms:)
199
+ return true if @mode == "required"
200
+
201
+ preferred = go_ms < ruby_ms
202
+ @mutex.synchronize { @performance_decisions[workload.to_sym] = preferred }
203
+ preferred
204
+ end
205
+
206
+ def observe_performance(workload, milliseconds:, rows: 0, fallback: false)
207
+ @mutex.synchronize do
208
+ sample = @performance_samples[workload.to_sym]
209
+ sample[:count] += 1
210
+ sample[:fallbacks] += 1 if fallback
211
+ sample[:total_ms] += Float(milliseconds)
212
+ sample[:rows] += Integer(rows)
213
+ end
214
+ end
215
+
216
+ def accelerator_policy(workload, input_rows:, estimated_bytes: 0, deadline_at: nil)
217
+ return false if @mode == "off"
218
+ return false unless supports?(workload_capability(workload))
219
+ return false if input_rows && input_rows.to_i > @max_rows
220
+ return false if estimated_bytes.to_i > @max_result_bytes
221
+ return false if deadline_at && Time.now >= deadline_at
222
+
223
+ acceleration_preferred?(workload)
224
+ end
225
+
226
+ def close
227
+ @mutex.synchronize do
228
+ if @started && @stdin && !@stdin.closed?
229
+ begin
230
+ write_locked(message("terminate"))
231
+ rescue
232
+ nil
233
+ end
234
+ end
235
+ stop_locked
236
+ end
237
+ true
238
+ end
239
+
240
+ private
241
+
242
+ def prepare_transport!
243
+ return nil if @mode == "off"
244
+
245
+ @mutex.synchronize do
246
+ unless available?
247
+ raise UnavailableError.new("RubyDB accelerator binary is not available", code: "binary_missing") if @mode == "required"
248
+
249
+ return nil
250
+ end
251
+ start_locked
252
+ true
253
+ end
254
+ end
255
+
256
+ def workload_capability(workload)
257
+ case workload.to_sym
258
+ when :scan, :index_scan, :sort, :aggregate, :distinct, :window, :join, :merge_join
259
+ "rows_pipeline"
260
+ when :snapshot_scan
261
+ "snapshot_scan"
262
+ when :compression
263
+ "gzip"
264
+ when :checksum
265
+ "sha256"
266
+ when :wal_batch
267
+ "wal_batch"
268
+ else
269
+ workload.to_s
270
+ end
271
+ end
272
+
273
+ def validate_mode!
274
+ return if %w[auto off required].include?(@mode)
275
+
276
+ raise ArgumentError, "mode must be auto, off, or required"
277
+ end
278
+
279
+ def start_locked
280
+ return if @started
281
+ if @binary_path.nil? || !File.file?(@binary_path)
282
+ raise UnavailableError.new("RubyDB accelerator binary is not available", code: "binary_missing")
283
+ end
284
+ verify_binary_integrity!
285
+
286
+ @stdin, @stdout, @stderr, @wait_thread = Open3.popen3(@binary_path)
287
+ # The worker protocol carries both newline-delimited JSON and binary
288
+ # frames. Ruby's Windows pipes default to text/UTF-8 behavior, which
289
+ # rejects binary bytes such as a snapshot page header. Put every pipe
290
+ # in binary mode before the reader or request threads can use it.
291
+ @stdin.binmode
292
+ @stdout.binmode
293
+ @stderr.binmode
294
+ @started = true
295
+ @handshaken = false
296
+ @stopping = false
297
+ @worker_starts += 1
298
+ @worker_restarts += 1 if @worker_started_once
299
+ @worker_started_once = true
300
+ stdout = @stdout
301
+ @reader_thread = Thread.new { reader_loop(stdout) }
302
+ stderr = @stderr
303
+ @stderr_thread = Thread.new do
304
+ stderr.each_line do |line|
305
+ @last_error = line.to_s.strip unless line.to_s.strip.empty?
306
+ end
307
+ rescue IOError
308
+ nil
309
+ end
310
+ rescue Errno::ENOENT => error
311
+ @last_error = error.message
312
+ stop_locked
313
+ raise UnavailableError.new("Unable to start RubyDB accelerator: #{error.message}", code: "spawn_failed")
314
+ end
315
+
316
+ def ensure_handshake(timeout)
317
+ @handshake_mutex.synchronize do
318
+ return if @mutex.synchronize { @handshaken }
319
+
320
+ response = exchange_json("handshake", {
321
+ protocol_version: PROTOCOL_VERSION,
322
+ client_name: "rubydb-ruby",
323
+ client_version: (defined?(RubyDB::VERSION) ? RubyDB::VERSION : "unknown")
324
+ }, timeout)
325
+ unless response["protocol_version"].to_i == PROTOCOL_VERSION
326
+ @mutex.synchronize { stop_locked }
327
+ raise ProtocolError.new("RubyDB accelerator protocol handshake failed", code: "protocol_mismatch")
328
+ end
329
+ @mutex.synchronize do
330
+ @capabilities = Array(response["capabilities"]).map(&:to_s)
331
+ @handshaken = true
332
+ end
333
+ end
334
+ end
335
+
336
+ def exchange_json(type, payload, timeout)
337
+ id = "acc_#{SecureRandom.hex(12)}"
338
+ queue = register_pending(id)
339
+ write_locked(message(type, payload, id))
340
+ response = wait_for_pending(id, queue, timeout)
341
+ unless response["success"] == true
342
+ raise RequestError.new(response["error"].to_s, code: response["code"])
343
+ end
344
+
345
+ response["payload"] || {}
346
+ rescue TimeoutError
347
+ cancel_request(id, timeout)
348
+ raise TimeoutError.new("RubyDB accelerator request timed out", code: "timeout")
349
+ end
350
+
351
+ def register_pending(id)
352
+ queue = Queue.new
353
+ @mutex.synchronize do
354
+ raise ProtocolError.new("RubyDB accelerator is not running", code: "not_started") unless @started
355
+
356
+ @pending[id] = queue
357
+ end
358
+ queue
359
+ end
360
+
361
+ def wait_for_pending(id, queue, timeout)
362
+ value = Timeout.timeout(timeout, TimeoutError) { queue.pop }
363
+ raise value if value.is_a?(Exception)
364
+
365
+ value
366
+ ensure
367
+ @mutex.synchronize { @pending.delete(id) }
368
+ end
369
+
370
+ def cancel_request(target_id, timeout)
371
+ return unless @mutex.synchronize { @started }
372
+
373
+ cancel_id = "acc_cancel_#{SecureRandom.hex(8)}"
374
+ queue = register_pending(cancel_id)
375
+ write_locked(message("cancel", {target_id: target_id}, cancel_id))
376
+ wait_for_pending(cancel_id, queue, [Float(timeout), 1.0].min)
377
+ rescue Timeout::Error, TimeoutError, IOError, SystemCallError, ProtocolError
378
+ @mutex.synchronize { stop_locked }
379
+ end
380
+
381
+ def write_locked(value)
382
+ frame = "#{JSON.generate(value)}\n"
383
+ raise ProtocolError.new("RubyDB accelerator request exceeds max frame size", code: "frame_too_large") if frame.bytesize > @max_frame_size
384
+
385
+ @write_mutex.synchronize do
386
+ @stdin.write(frame)
387
+ @stdin.flush
388
+ end
389
+ end
390
+
391
+ def exchange_binary(type, payload, timeout)
392
+ type_id = case type
393
+ when "rows_pipeline", "rows_pipeline_binary" then BINARY_ROWS_TYPE
394
+ when "hash_join" then BINARY_JOIN_TYPE
395
+ when "snapshot_scan" then BINARY_SNAPSHOT_SCAN_TYPE
396
+ else
397
+ raise RequestError.new("Unsupported binary accelerator operation", code: "unsupported_operation")
398
+ end
399
+ id = "acc_#{SecureRandom.hex(12)}"
400
+ queue = register_pending(id)
401
+ write_binary_locked(type_id, id, payload)
402
+ result = wait_for_pending(id, queue, timeout)
403
+ if result.is_a?(Hash) && result[:binary_error]
404
+ error_payload = JSON.parse(result[:binary_error])
405
+ raise RequestError.new(error_payload["error"].to_s, code: error_payload["code"])
406
+ end
407
+ result
408
+ rescue TimeoutError
409
+ cancel_request(id, timeout)
410
+ raise TimeoutError.new("RubyDB accelerator request timed out", code: "timeout")
411
+ end
412
+
413
+ def write_binary_locked(type_id, id, payload)
414
+ payload = payload.to_s.b
415
+ if id.bytesize > 1024 || payload.bytesize > @max_frame_size || id.bytesize + payload.bytesize > @max_frame_size
416
+ raise ProtocolError.new("RubyDB accelerator binary request exceeds max frame size", code: "frame_too_large")
417
+ end
418
+ header = [BINARY_MAGIC, BINARY_VERSION, 1, type_id, 0, id.bytesize, payload.bytesize].pack("a4C4vV")
419
+ @write_mutex.synchronize do
420
+ @stdin.write(header)
421
+ @stdin.write(id)
422
+ @stdin.write(payload)
423
+ @stdin.flush
424
+ end
425
+ end
426
+
427
+ def reader_loop(stdout)
428
+ loop do
429
+ prefix = stdout.read(4)
430
+ raise EOFError if prefix.nil? || prefix.empty? || prefix.bytesize != 4
431
+
432
+ if prefix == BINARY_MAGIC
433
+ header = prefix + read_from_worker(stdout, 10)
434
+ raise ProtocolError.new("Invalid binary response header", code: "invalid_response") unless header.getbyte(4) == BINARY_VERSION
435
+
436
+ id_length = header.byteslice(8, 2).unpack1("v")
437
+ payload_length = header.byteslice(10, 4).unpack1("V")
438
+ raise ProtocolError.new("Binary response exceeds max frame size", code: "frame_too_large") if id_length > 1024 || payload_length > @max_frame_size || id_length + payload_length > @max_frame_size
439
+
440
+ body = read_from_worker(stdout, id_length + payload_length)
441
+ id = body.byteslice(0, id_length).to_s
442
+ payload = body.byteslice(id_length, payload_length).to_s.b
443
+ value = if header.getbyte(7) == 0
444
+ payload
445
+ else
446
+ {binary_error: payload}
447
+ end
448
+ deliver_pending(id, value)
449
+ else
450
+ line = prefix + stdout.gets.to_s
451
+ raise ProtocolError.new("Invalid JSON response", code: "invalid_response") if line.bytesize > @max_frame_size
452
+
453
+ response = JSON.parse(line)
454
+ deliver_pending(response["id"].to_s, response)
455
+ end
456
+ end
457
+ rescue => error
458
+ mark_worker_failure(error)
459
+ end
460
+
461
+ def mark_worker_failure(error)
462
+ message = "#{error.class}: #{error.message}"
463
+ streams = nil
464
+ pending = []
465
+ @mutex.synchronize do
466
+ return if @stopping
467
+
468
+ @last_error = message
469
+ @worker_failures += 1
470
+ @started = false
471
+ @handshaken = false
472
+ pending = @pending.values.dup
473
+ @pending.clear
474
+ streams = [@stdin, @stdout, @stderr]
475
+ @stdin = @stdout = @stderr = nil
476
+ end
477
+ pending.each { |queue| queue << ProtocolError.new("RubyDB accelerator reader stopped: #{message}", code: "reader_stopped") }
478
+ streams.each do |stream|
479
+ stream.close unless stream.nil? || stream.closed?
480
+ rescue IOError
481
+ nil
482
+ end
483
+ end
484
+
485
+ def read_from_worker(stdout, length)
486
+ value = stdout.read(length)
487
+ raise EOFError if value.nil? || value.bytesize != length
488
+
489
+ value
490
+ end
491
+
492
+ def deliver_pending(id, value)
493
+ queue = @mutex.synchronize { @pending[id] }
494
+ queue << value if queue
495
+ end
496
+
497
+ def message(type, payload = {}, id = "acc_#{SecureRandom.hex(12)}")
498
+ {"id" => id, "type" => type, "payload" => payload}
499
+ end
500
+
501
+ def stop_locked
502
+ @stopping = true
503
+ stdin = @stdin
504
+ stdout = @stdout
505
+ stderr = @stderr
506
+ wait_thread = @wait_thread
507
+ reader_thread = @reader_thread
508
+ @stdin = @stdout = @stderr = @wait_thread = nil
509
+ @reader_thread = nil
510
+ @started = false
511
+ @handshaken = false
512
+ @capabilities = []
513
+
514
+ pending = @pending.values.dup
515
+ @pending.clear
516
+ pending.each { |queue| queue << ProtocolError.new("RubyDB accelerator stopped", code: "stopped") }
517
+
518
+ [stdin, stdout, stderr].each do |io|
519
+ io&.close unless io.nil? || io.closed?
520
+ rescue IOError
521
+ nil
522
+ end
523
+
524
+ if wait_thread
525
+ begin
526
+ wait_thread.join(0.25)
527
+ if wait_thread.alive?
528
+ pid = wait_thread.respond_to?(:pid) ? wait_thread.pid : nil
529
+ Process.kill("TERM", pid) if pid
530
+ wait_thread.join(0.25)
531
+ end
532
+ rescue SystemCallError
533
+ nil
534
+ end
535
+ end
536
+
537
+ if reader_thread && reader_thread != Thread.current
538
+ reader_thread.kill
539
+ reader_thread.join(0.25)
540
+ end
541
+
542
+ @stderr_thread&.kill
543
+ @stderr_thread = nil
544
+ end
545
+
546
+ def worker_pid
547
+ return nil unless @wait_thread&.respond_to?(:pid)
548
+
549
+ @wait_thread.pid
550
+ rescue
551
+ nil
552
+ end
553
+
554
+ def resolve_binary(configured)
555
+ return File.expand_path(configured.to_s) unless configured.nil? || configured.to_s.empty?
556
+
557
+ filename = "rubydb-accelerator-#{platform_name}"
558
+ filename += ".exe" if windows?
559
+ candidates = [
560
+ File.join(__dir__, "bin", filename),
561
+ File.expand_path(File.join(__dir__, "..", "..", "..", "accelerator", "bin", filename)),
562
+ File.expand_path(File.join(Dir.pwd, "accelerator", "bin", filename))
563
+ ]
564
+ candidates.find { |path| File.file?(path) }
565
+ end
566
+
567
+ def verify_binary_integrity!
568
+ manifest = File.join(File.dirname(@binary_path), "SHA256SUMS")
569
+ return unless File.file?(manifest)
570
+
571
+ entry = File.readlines(manifest, chomp: true).find do |line|
572
+ digest, name = line.split(/\s+/, 2)
573
+ name == File.basename(@binary_path) && digest && digest.match?(/\A[0-9a-f]{64}\z/i)
574
+ end
575
+ raise UnavailableError.new("RubyDB accelerator binary is not listed in SHA256SUMS", code: "binary_checksum_missing") unless entry
576
+
577
+ expected = entry.split(/\s+/, 2).first.downcase
578
+ actual = Digest::SHA256.file(@binary_path).hexdigest
579
+ return if expected == actual
580
+
581
+ raise UnavailableError.new("RubyDB accelerator binary checksum mismatch", code: "binary_checksum_mismatch")
582
+ end
583
+
584
+ def windows?
585
+ RbConfig::CONFIG["host_os"].to_s.match?(/mswin|mingw|cygwin/i)
586
+ end
587
+
588
+ def platform_name
589
+ os = if windows?
590
+ "windows"
591
+ elsif RbConfig::CONFIG["host_os"].to_s.match?(/darwin/i)
592
+ "darwin"
593
+ else
594
+ "linux"
595
+ end
596
+ cpu = RbConfig::CONFIG["host_cpu"].to_s
597
+ arch = case cpu
598
+ when /aarch64|arm64/ then "arm64"
599
+ when /386|i.86/ then "386"
600
+ else "amd64"
601
+ end
602
+ "#{os}-#{arch}"
603
+ end
604
+ end
605
+ end
606
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "accelerator/error"
4
+ require_relative "accelerator/manager"
5
+ require_relative "accelerator/client"
6
+
7
+ module RubyDB
8
+ # Optional performance accelerator. The Ruby implementation remains the
9
+ # source of truth; this client only delegates explicitly supported, pure
10
+ # read or utility operations to a managed Go worker.
11
+ module Accelerator
12
+ end
13
+ end
@@ -74,6 +74,7 @@ module RubyDB
74
74
  @commands[:inspect] = Commands::Inspect.new(@output, @formatter).method(:execute)
75
75
  @commands[:vacuum] = Commands::Vacuum.new(@output, @formatter).method(:execute)
76
76
  @commands[:doctor] = Commands::Doctor.new(@output, @formatter).method(:execute)
77
+ @commands[:accelerator] = Commands::Accelerator.new(@output, @formatter).method(:execute)
77
78
  end
78
79
 
79
80
  def parse_global_options(argv)
@@ -112,7 +113,10 @@ module RubyDB
112
113
  end
113
114
 
114
115
  begin
115
- parser.parse!(argv)
116
+ # Leave command-specific flags (for example `accelerator --ping`)
117
+ # for the selected command parser. Global flags remain accepted
118
+ # before the command name.
119
+ parser.order!(argv)
116
120
  rescue OptionParser::ParseError => e
117
121
  @output.error(e.message)
118
122
  print_help
@@ -155,6 +159,7 @@ module RubyDB
155
159
  @output.puts " inspect Inspect database internals"
156
160
  @output.puts " vacuum Vacuum the database"
157
161
  @output.puts " doctor Run health checks"
162
+ @output.puts " accelerator Inspect or verify the Go accelerator"
158
163
  @output.puts
159
164
  @output.puts "Run 'rubydb <command> --help' for more information on a command."
160
165
  end