openc3 7.5.1 → 7.6.0

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 (169) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +0 -0
  3. data/bin/openc3cli +82 -5
  4. data/data/config/_params.yaml +3 -0
  5. data/data/config/command_modifiers.yaml +5 -3
  6. data/data/config/default_plugins.yaml +1 -0
  7. data/data/config/interface_modifiers.yaml +17 -1
  8. data/data/config/item_modifiers.yaml +24 -1
  9. data/data/config/microservice.yaml +15 -1
  10. data/data/config/parameter_modifiers.yaml +4 -4
  11. data/data/config/plugins.yaml +511 -3
  12. data/data/config/settings.yaml +17 -0
  13. data/data/config/target.yaml +53 -0
  14. data/data/config/telemetry.yaml +58 -2
  15. data/data/config/widgets.yaml +30 -0
  16. data/lib/openc3/accessors/accessor.rb +14 -2
  17. data/lib/openc3/accessors/binary_accessor.rb +25 -5
  18. data/lib/openc3/api/CLAUDE.md +1 -0
  19. data/lib/openc3/api/api.rb +1 -0
  20. data/lib/openc3/api/cmd_api.rb +9 -1
  21. data/lib/openc3/api/interface_api.rb +7 -2
  22. data/lib/openc3/api/limits_api.rb +93 -41
  23. data/lib/openc3/api/router_api.rb +7 -2
  24. data/lib/openc3/api/settings_api.rb +4 -1
  25. data/lib/openc3/api/shared_resource_api.rb +107 -0
  26. data/lib/openc3/api/tlm_api.rb +10 -0
  27. data/lib/openc3/interfaces/http_client_interface.rb +84 -27
  28. data/lib/openc3/interfaces/protocols/preidentified_protocol.rb +6 -2
  29. data/lib/openc3/interfaces/tcpip_server_interface.rb +205 -125
  30. data/lib/openc3/io/json_api.rb +2 -10
  31. data/lib/openc3/io/json_api_object.rb +2 -10
  32. data/lib/openc3/logs/packet_log_writer.rb +8 -3
  33. data/lib/openc3/microservices/decom_common.rb +4 -1
  34. data/lib/openc3/microservices/decom_microservice.rb +52 -6
  35. data/lib/openc3/microservices/interface_decom_common.rb +13 -0
  36. data/lib/openc3/microservices/interface_microservice.rb +50 -11
  37. data/lib/openc3/microservices/limits_group_microservice.rb +513 -0
  38. data/lib/openc3/microservices/log_microservice.rb +22 -8
  39. data/lib/openc3/microservices/microservice.rb +8 -2
  40. data/lib/openc3/microservices/multi_microservice.rb +76 -22
  41. data/lib/openc3/microservices/queue_microservice.rb +29 -24
  42. data/lib/openc3/migrations/20260704000000_default_bridge.rb +1 -2
  43. data/lib/openc3/migrations/20261103000001_limits_group_backfill.rb +193 -0
  44. data/lib/openc3/models/bridge_model.rb +3 -3
  45. data/lib/openc3/models/cvt_model.rb +3 -1
  46. data/lib/openc3/models/host_interface_microservice_model.rb +4 -4
  47. data/lib/openc3/models/interface_model.rb +27 -7
  48. data/lib/openc3/models/microservice_group.rb +167 -0
  49. data/lib/openc3/models/microservice_model.rb +88 -2
  50. data/lib/openc3/models/panel_model.rb +109 -0
  51. data/lib/openc3/models/plugin_model.rb +1006 -26
  52. data/lib/openc3/models/plugin_store_model.rb +6 -1
  53. data/lib/openc3/models/protected_storage_model.rb +64 -0
  54. data/lib/openc3/models/queue_model.rb +19 -10
  55. data/lib/openc3/models/scope_model.rb +80 -3
  56. data/lib/openc3/models/setting_model.rb +2 -2
  57. data/lib/openc3/models/shared_resource_model.rb +381 -0
  58. data/lib/openc3/models/target_model.rb +324 -68
  59. data/lib/openc3/operators/microservice_operator.rb +76 -9
  60. data/lib/openc3/operators/operator.rb +29 -0
  61. data/lib/openc3/packets/commands.rb +16 -16
  62. data/lib/openc3/packets/limits.rb +2 -0
  63. data/lib/openc3/packets/packet.rb +75 -4
  64. data/lib/openc3/packets/packet_config.rb +152 -13
  65. data/lib/openc3/packets/packet_item.rb +35 -14
  66. data/lib/openc3/packets/packet_item_limits.rb +67 -6
  67. data/lib/openc3/packets/parsers/limits_parser.rb +69 -2
  68. data/lib/openc3/packets/parsers/packet_item_parser.rb +6 -0
  69. data/lib/openc3/packets/parsers/packet_parser.rb +45 -0
  70. data/lib/openc3/packets/parsers/state_parser.rb +1 -1
  71. data/lib/openc3/script/CLAUDE.md +1 -0
  72. data/lib/openc3/script/api_shared.rb +17 -9
  73. data/lib/openc3/script/calendar.rb +18 -6
  74. data/lib/openc3/script/commands.rb +11 -9
  75. data/lib/openc3/script/critical_cmd.rb +22 -2
  76. data/lib/openc3/script/metadata.rb +30 -4
  77. data/lib/openc3/script/plugins.rb +8 -0
  78. data/lib/openc3/script/script.rb +8 -24
  79. data/lib/openc3/script/web_socket_api.rb +23 -17
  80. data/lib/openc3/system/system.rb +9 -3
  81. data/lib/openc3/top_level.rb +17 -0
  82. data/lib/openc3/topics/command_topic.rb +7 -1
  83. data/lib/openc3/topics/decom_interface_topic.rb +9 -3
  84. data/lib/openc3/topics/discovery_topic.rb +84 -0
  85. data/lib/openc3/topics/limits_event_topic.rb +236 -17
  86. data/lib/openc3/topics/telemetry_decom_topic.rb +12 -3
  87. data/lib/openc3/topics/topic.rb +2 -1
  88. data/lib/openc3/utilities/authentication.rb +67 -16
  89. data/lib/openc3/utilities/cli_generator.rb +117 -25
  90. data/lib/openc3/utilities/client_ip_header_filter.rb +38 -0
  91. data/lib/openc3/utilities/config_overlay.rb +24 -1
  92. data/lib/openc3/utilities/cosmos_rails_formatter.rb +18 -10
  93. data/lib/openc3/utilities/hardware_discovery.rb +279 -0
  94. data/lib/openc3/utilities/limits_rollup.rb +81 -0
  95. data/lib/openc3/utilities/local_mode.rb +48 -5
  96. data/lib/openc3/utilities/questdb_client.rb +130 -22
  97. data/lib/openc3/utilities/reingest_job.rb +12 -3
  98. data/lib/openc3/utilities/running_script.rb +206 -49
  99. data/lib/openc3/utilities/script.rb +24 -12
  100. data/lib/openc3/utilities/script_env.rb +164 -0
  101. data/lib/openc3/utilities/script_message_signing.rb +133 -0
  102. data/lib/openc3/utilities/store_autoload.rb +32 -1
  103. data/lib/openc3/utilities/store_queued.rb +5 -1
  104. data/lib/openc3/version.rb +6 -6
  105. data/templates/command_validator/command_validator.py +9 -9
  106. data/templates/{tool_react/pnpm-workspace.yaml → panel/.prettierrc.cjs} +7 -5
  107. data/templates/panel/Rakefile +24 -0
  108. data/templates/panel/package.json +17 -0
  109. data/templates/{tool_angular → panel}/pnpm-workspace.yaml +5 -0
  110. data/templates/panel/src/Panel.js +52 -0
  111. data/templates/panel/src/Panel.vue +60 -0
  112. data/templates/panel/src/panelSdk.js +24 -0
  113. data/templates/panel/vite-Panel.config.js +43 -0
  114. data/templates/tool_vue/package.json +3 -3
  115. data/templates/widget/package.json +2 -2
  116. metadata +24 -56
  117. data/templates/tool_angular/.editorconfig +0 -16
  118. data/templates/tool_angular/.gitignore +0 -44
  119. data/templates/tool_angular/.vscode/extensions.json +0 -4
  120. data/templates/tool_angular/.vscode/launch.json +0 -20
  121. data/templates/tool_angular/.vscode/tasks.json +0 -42
  122. data/templates/tool_angular/angular.json +0 -111
  123. data/templates/tool_angular/extra-webpack.config.js +0 -8
  124. data/templates/tool_angular/package.json +0 -48
  125. data/templates/tool_angular/src/app/app-routing.module.ts +0 -15
  126. data/templates/tool_angular/src/app/app.component.html +0 -22
  127. data/templates/tool_angular/src/app/app.component.scss +0 -18
  128. data/templates/tool_angular/src/app/app.component.spec.ts +0 -29
  129. data/templates/tool_angular/src/app/app.component.ts +0 -52
  130. data/templates/tool_angular/src/app/app.module.ts +0 -30
  131. data/templates/tool_angular/src/app/custom-overlay-container.ts +0 -17
  132. data/templates/tool_angular/src/app/empty-route/empty-route.component.ts +0 -7
  133. data/templates/tool_angular/src/app/openc3-api.d.ts +0 -1
  134. data/templates/tool_angular/src/assets/.gitkeep +0 -0
  135. data/templates/tool_angular/src/environments/environment.prod.ts +0 -3
  136. data/templates/tool_angular/src/environments/environment.ts +0 -16
  137. data/templates/tool_angular/src/favicon.ico +0 -0
  138. data/templates/tool_angular/src/index.html +0 -13
  139. data/templates/tool_angular/src/main.single-spa.ts +0 -40
  140. data/templates/tool_angular/src/single-spa/asset-url.ts +0 -12
  141. data/templates/tool_angular/src/single-spa/single-spa-props.ts +0 -8
  142. data/templates/tool_angular/src/styles.scss +0 -1
  143. data/templates/tool_angular/tsconfig.app.json +0 -13
  144. data/templates/tool_angular/tsconfig.json +0 -33
  145. data/templates/tool_angular/tsconfig.spec.json +0 -14
  146. data/templates/tool_react/.eslintrc +0 -7
  147. data/templates/tool_react/.gitignore +0 -71
  148. data/templates/tool_react/.prettierignore +0 -8
  149. data/templates/tool_react/babel.config.json +0 -29
  150. data/templates/tool_react/jest.config.js +0 -12
  151. data/templates/tool_react/package.json +0 -51
  152. data/templates/tool_react/src/openc3-tool_name.js +0 -24
  153. data/templates/tool_react/src/root.component.js +0 -88
  154. data/templates/tool_react/src/root.component.test.js +0 -9
  155. data/templates/tool_react/webpack.config.js +0 -27
  156. data/templates/tool_svelte/.gitignore +0 -71
  157. data/templates/tool_svelte/.prettierignore +0 -8
  158. data/templates/tool_svelte/.prettierrc.js +0 -5
  159. data/templates/tool_svelte/babel.config.js +0 -12
  160. data/templates/tool_svelte/build/smui.css +0 -1
  161. data/templates/tool_svelte/jest.config.js +0 -9
  162. data/templates/tool_svelte/package.json +0 -49
  163. data/templates/tool_svelte/pnpm-workspace.yaml +0 -20
  164. data/templates/tool_svelte/rollup.config.js +0 -74
  165. data/templates/tool_svelte/src/App.svelte +0 -42
  166. data/templates/tool_svelte/src/App.test.js +0 -9
  167. data/templates/tool_svelte/src/theme/_smui-theme.scss +0 -25
  168. data/templates/tool_svelte/src/tool_name.js +0 -17
  169. data/templates/widget/LICENSE.md +0 -20
@@ -27,18 +27,56 @@ module OpenC3
27
27
  # connections. For each connection to the read port, a thread is spawned that
28
28
  # calls the read method from the interface. This data is then
29
29
  # available by calling the TcpipServer read method. For each connection to the
30
- # write port, a thread is spawned that calls the write method from the
31
- # interface when data is send to the TcpipServer via the write method.
30
+ # write port, a thread is spawned with its own queue which calls the write
31
+ # method from the interface when data is sent to the TcpipServer via the
32
+ # write method. Each client is written independently so a client which stops
33
+ # reading can't delay the other clients or accepting new connections.
32
34
  class TcpipServerInterface < StreamInterface
35
+ # Write timeout used when none is given, matching TcpipSocketStream
36
+ DEFAULT_WRITE_TIMEOUT = 10.0
37
+
38
+ # Interface for each client connection. Tracks the time spent writing to
39
+ # the socket so a client which can't keep up can be told apart from one
40
+ # whose protocols take a while (e.g. waiting for a command response).
41
+ class ClientInterface < StreamInterface
42
+ # Total seconds spent in stream writes
43
+ attr_reader :socket_write_time
44
+
45
+ def initialize
46
+ super()
47
+ @socket_write_time = 0.0
48
+ end
49
+
50
+ def write_interface(data, extra = nil)
51
+ write_interface_base(data, extra)
52
+ start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
53
+ begin
54
+ @stream.write(data)
55
+ ensure
56
+ @socket_write_time += Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
57
+ end
58
+ return data, extra
59
+ end
60
+ end
61
+
33
62
  # Data class which stores the interface and associated information
34
63
  class InterfaceInfo
35
- attr_reader :interface, :hostname, :host_ip, :port
64
+ attr_reader :interface, :host_ip, :port
65
+ # Queue of [method, packet_or_data, socket_write_time] waiting to be written to the client
66
+ attr_accessor :write_queue
67
+ # Thread which services the write_queue
68
+ attr_accessor :write_thread
36
69
 
37
- def initialize(interface, hostname, host_ip, port)
70
+ def initialize(interface, host_ip, port)
38
71
  @interface = interface
39
- @hostname = hostname
40
72
  @host_ip = host_ip
41
73
  @port = port
74
+ @write_queue = nil
75
+ @write_thread = nil
76
+ end
77
+
78
+ def to_s
79
+ "#{@host_ip}:#{@port}"
42
80
  end
43
81
  end
44
82
 
@@ -104,6 +142,8 @@ module OpenC3
104
142
  @stream_log_pair = nil
105
143
  @raw_logging_enabled = false
106
144
  @connection_mutex = Mutex.new
145
+ # Client write threads update the write statistics concurrently
146
+ @write_stats_mutex = Mutex.new
107
147
  @listen_address = "0.0.0.0"
108
148
 
109
149
  @read_allowed = false unless ConfigParser.handle_nil(read_port)
@@ -142,7 +182,7 @@ module OpenC3
142
182
  break if @cancel_threads
143
183
  end
144
184
  rescue Exception => e
145
- shutdown_interfaces(@write_interface_infos)
185
+ shutdown_write_clients()
146
186
  Logger.error("#{@name}: Tcpip server write thread unexpectedly died")
147
187
  Logger.error(e.formatted)
148
188
  end
@@ -152,7 +192,7 @@ module OpenC3
152
192
  break if @cancel_threads
153
193
  end
154
194
  rescue Exception => e
155
- shutdown_interfaces(@write_interface_infos)
195
+ shutdown_write_clients()
156
196
  Logger.error("#{@name}: Tcpip server write raw thread unexpectedly died")
157
197
  Logger.error(e.formatted)
158
198
  end
@@ -207,7 +247,7 @@ module OpenC3
207
247
  @write_raw_thread = nil
208
248
  end
209
249
 
210
- shutdown_interfaces(@write_interface_infos)
250
+ shutdown_write_clients()
211
251
  @connected = false
212
252
  super()
213
253
  end
@@ -306,13 +346,36 @@ module OpenC3
306
346
  def shutdown_interfaces(interface_infos)
307
347
  @connection_mutex.synchronize do
308
348
  interface_infos.each do |interface_info|
309
- interface_info.interface.disconnect
310
- interface_info.interface.stream_log_pair.stop if interface_info.interface.stream_log_pair
349
+ disconnect_client(interface_info)
311
350
  end
312
351
  interface_infos.clear
313
352
  end
314
353
  end
315
354
 
355
+ # Stop every client write thread and disconnect the clients
356
+ def shutdown_write_clients
357
+ interface_infos = @connection_mutex.synchronize do
358
+ infos = @write_interface_infos.dup
359
+ @write_interface_infos.clear
360
+ infos
361
+ end
362
+ # Stop and join outside the mutex because the client write threads
363
+ # take it to remove themselves as they exit
364
+ interface_infos.each do |interface_info|
365
+ stop_client_write_queue(interface_info)
366
+ disconnect_client(interface_info)
367
+ end
368
+ interface_infos.each do |interface_info|
369
+ thread = interface_info.write_thread
370
+ OpenC3.kill_thread(self, thread) if thread and thread != Thread.current
371
+ end
372
+ end
373
+
374
+ def disconnect_client(interface_info)
375
+ interface_info.interface.disconnect
376
+ interface_info.interface.stream_log_pair.stop if interface_info.interface.stream_log_pair
377
+ end
378
+
316
379
  def change_raw_logging(method)
317
380
  if @stream_log_pair
318
381
  @write_interface_infos.each do |interface_info|
@@ -380,18 +443,8 @@ module OpenC3
380
443
  end
381
444
  end
382
445
 
446
+ # No reverse DNS lookup because it blocks accepting further connections
383
447
  port, host_ip = Socket.unpack_sockaddr_in(address)
384
- hostname = ''
385
- hostname = Socket.lookup_hostname_from_ip(host_ip)
386
- # if System.instance.acl
387
- # addr = ["AF_INET", 10, "lc630", host_ip.to_s]
388
- # if not System.instance.acl.allow_addr?(addr)
389
- # # Reject connection
390
- # OpenC3.close_socket(socket)
391
- # Logger.info "#{@name}: Tcpip server rejected connection from #{hostname}(#{host_ip}):#{port}"
392
- # return
393
- # end
394
- # end
395
448
 
396
449
  # Configure TCP_NODELAY option
397
450
  socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
@@ -408,7 +461,7 @@ module OpenC3
408
461
  stream.set_option(option_name, option_values)
409
462
  end
410
463
 
411
- interface = StreamInterface.new
464
+ interface = ClientInterface.new
412
465
  # Only the read side of the connection needs a read thread
413
466
  interface.read_allowed = listen_read
414
467
  # Each connection buffers its own reads so give it the configured limit
@@ -426,25 +479,127 @@ module OpenC3
426
479
  interface.stream = stream
427
480
  interface.connect
428
481
 
482
+ interface_info = InterfaceInfo.new(interface, host_ip, port)
429
483
  if listen_write
430
484
  @write_connection_callback.call(interface) if @write_connection_callback
431
- @connection_mutex.synchronize do
432
- @write_interface_infos << InterfaceInfo.new(interface, hostname, host_ip, port)
433
- end
485
+ add_write_client(interface_info)
434
486
  end
435
487
  if listen_read
436
488
  @read_connection_callback.call(interface) if @read_connection_callback
437
489
  @connection_mutex.synchronize do
438
- @read_interface_infos << InterfaceInfo.new(interface, hostname, host_ip, port)
490
+ @read_interface_infos << interface_info
491
+ end
492
+ start_read_thread(interface_info)
493
+ end
494
+ Logger.info "#{@name}: Tcpip server accepted connection from #{interface_info}"
495
+ end
496
+
497
+ # Register a client connected to the write port and start the thread
498
+ # which writes its queued packets
499
+ def add_write_client(interface_info)
500
+ interface_info.write_queue = Queue.new
501
+ # Register before starting the thread so the thread can always remove it
502
+ @connection_mutex.synchronize do
503
+ @write_interface_infos << interface_info
504
+ end
505
+ interface_info.write_thread = Thread.new do
506
+ client_write_thread_body(interface_info)
507
+ rescue Exception => e
508
+ Logger.error "#{@name}: Tcpip server client write thread unexpectedly died"
509
+ Logger.error e.formatted
510
+ ensure
511
+ remove_write_client(interface_info)
512
+ end
513
+ end
514
+
515
+ # Writes everything queued for one client until the client disconnects,
516
+ # stops keeping up, or the queue is stopped
517
+ def client_write_thread_body(interface_info)
518
+ interface = interface_info.interface
519
+ write_queue = interface_info.write_queue
520
+ max_lag = @write_timeout || DEFAULT_WRITE_TIMEOUT
521
+ loop do
522
+ item = write_queue.pop(timeout: 0.1)
523
+ unless item
524
+ # Closed means the client is being removed
525
+ return if write_queue.closed?
526
+ # Detect disconnected clients even when packets aren't flowing
527
+ return unless client_write_connected?(interface_info)
528
+
529
+ next
439
530
  end
440
- start_read_thread(@read_interface_infos[-1])
531
+
532
+ method, packet_or_data, socket_write_time = item
533
+ # A client which is reading too slowly to keep up would otherwise
534
+ # queue data forever so drop it once it has spent more than
535
+ # write_timeout blocked on the socket since this item was queued.
536
+ # Only socket time counts, not time spent in protocols, so a
537
+ # protocol waiting for a response doesn't get the client dropped.
538
+ lag = interface.socket_write_time - socket_write_time
539
+ if lag > max_lag
540
+ Logger.error "#{@name}: Tcpip server dropping client #{interface_info} which spent #{lag.round(1)}s blocked writing since this data was queued (write timeout #{max_lag}s)"
541
+ return
542
+ end
543
+
544
+ interface_bytes_written = interface.bytes_written
545
+ interface.public_send(method, packet_or_data)
546
+ diff = interface.bytes_written - interface_bytes_written
547
+ @write_stats_mutex.synchronize do
548
+ @written_raw_data_time = interface.written_raw_data_time
549
+ @written_raw_data = interface.written_raw_data
550
+ @bytes_written += diff
551
+ end
552
+ end
553
+ rescue Errno::EPIPE, Errno::ECONNABORTED, IOError, Errno::ECONNRESET
554
+ # Client has normally disconnected
555
+ rescue Timeout::Error
556
+ Logger.error "#{@name}: Tcpip server dropping client #{interface_info} which did not accept data within the write timeout"
557
+ rescue Exception => e
558
+ if e.message != "Stream not connected for write_raw"
559
+ Logger.error "#{@name}: Error sending to client #{interface_info}: #{e.class} #{e.message}"
441
560
  end
442
- Logger.info "#{@name}: Tcpip server accepted connection from #{hostname}(#{host_ip}):#{port}"
561
+ end
562
+
563
+ # @return [Boolean] Whether the client on the write port still appears connected
564
+ def client_write_connected?(interface_info)
565
+ write_socket = interface_info.interface.stream.write_socket
566
+ if @write_port != @read_port
567
+ # Socket should return EWOULDBLOCK if it is still cleanly connected
568
+ write_socket.recvfrom_nonblock(10)
569
+ # Client has disconnected (or is invalidly sending data on the socket)
570
+ return false
571
+ else
572
+ # The read thread detects the disconnect and closes the socket
573
+ return !write_socket.closed?
574
+ end
575
+ rescue Errno::EWOULDBLOCK
576
+ # Client is still cleanly connected as far as we can tell without writing to the socket
577
+ return true
578
+ rescue Errno::ECONNRESET, Errno::ECONNABORTED, IOError
579
+ return false
580
+ end
581
+
582
+ # Called by the client write thread as it exits
583
+ def remove_write_client(interface_info)
584
+ stop_client_write_queue(interface_info)
585
+ removed = @connection_mutex.synchronize { @write_interface_infos.delete(interface_info) }
586
+ # Not found means shutdown_write_clients already removed it
587
+ return unless removed
588
+
589
+ Logger.info "#{@name}: Tcpip server lost write connection to #{interface_info}"
590
+ disconnect_client(interface_info)
591
+ end
592
+
593
+ def stop_client_write_queue(interface_info)
594
+ write_queue = interface_info.write_queue
595
+ return unless write_queue
596
+
597
+ write_queue.clear
598
+ write_queue.close
443
599
  end
444
600
 
445
601
  def start_read_thread(interface_info)
446
602
  @read_threads << Thread.new do
447
- index_to_delete = nil
448
603
  begin
449
604
  begin
450
605
  read_thread_body(interface_info.interface)
@@ -452,24 +607,12 @@ module OpenC3
452
607
  Logger.error "#{@name}: Tcpip server read thread unexpectedly died"
453
608
  Logger.error e.formatted
454
609
  end
455
- Logger.info "#{@name}: Tcpip server lost read connection to #{interface_info.hostname}(#{interface_info.host_ip}):#{interface_info.port}"
610
+ Logger.info "#{@name}: Tcpip server lost read connection to #{interface_info}"
456
611
  @read_threads.delete(Thread.current)
457
612
 
458
- index_to_delete = nil
459
613
  @connection_mutex.synchronize do
460
- index = 0
461
- @read_interface_infos.each do |read_interface_info|
462
- if interface_info.interface == read_interface_info.interface
463
- index_to_delete = index
464
- read_interface_info.interface.disconnect
465
- read_interface_info.interface.stream_log_pair.stop if read_interface_info.interface.stream_log_pair
466
- break
467
- end
468
- index += 1
469
- end
470
- ensure
471
- if index_to_delete
472
- @read_interface_infos.delete_at(index_to_delete)
614
+ if @read_interface_infos.delete(interface_info)
615
+ disconnect_client(interface_info)
473
616
  end
474
617
  end
475
618
  rescue Exception => e
@@ -481,7 +624,6 @@ module OpenC3
481
624
 
482
625
  def write_thread_body
483
626
  # Retrieve the next packet to be sent out to clients
484
- # Handles disconnected clients even when packets aren't flowing
485
627
  packet = nil
486
628
 
487
629
  loop do
@@ -491,7 +633,10 @@ module OpenC3
491
633
  packet = @write_queue.pop(true) # non_block to raise ThreadError
492
634
  break
493
635
  rescue ThreadError
494
- check_for_dead_clients()
636
+ # Sleep until we receive a packet or for 100ms
637
+ @write_mutex.synchronize do
638
+ @write_condition_variable.wait(@write_mutex, 0.1)
639
+ end
495
640
  end
496
641
  end
497
642
 
@@ -553,86 +698,21 @@ module OpenC3
553
698
  packet
554
699
  end
555
700
 
556
- def check_for_dead_clients
557
- indexes_to_delete = []
558
- index = 0
559
-
560
- @connection_mutex.synchronize do
561
- @write_interface_infos.each do |interface_info|
562
- if @write_port != @read_port
563
- # Socket should return EWOULDBLOCK if it is still cleanly connected
564
- interface_info.interface.stream.write_socket.recvfrom_nonblock(10)
565
- elsif !interface_info.interface.stream.write_socket.closed?
566
- # Let read thread detect disconnect
567
- next
568
- end
569
- # Client has disconnected (or is invalidly sending data on the socket)
570
- Logger.info "#{@name}: Tcpip server lost write connection to #{interface_info.hostname}(#{interface_info.host_ip}):#{interface_info.port}"
571
- interface_info.interface.disconnect
572
- interface_info.interface.stream_log_pair.stop if interface_info.interface.stream_log_pair
573
- indexes_to_delete.unshift(index) # Put later indexes at front of array
574
- rescue Errno::ECONNRESET, Errno::ECONNABORTED, IOError
575
- # Client has disconnected
576
- Logger.info "#{@name}: Tcpip server lost write connection to #{interface_info.hostname}(#{interface_info.host_ip}):#{interface_info.port}"
577
- interface_info.interface.disconnect
578
- interface_info.interface.stream_log_pair.stop if interface_info.interface.stream_log_pair
579
- indexes_to_delete.unshift(index) # Put later indexes at front of array
580
- rescue Errno::EWOULDBLOCK
581
- # Client is still cleanly connected as far as we can tell without writing to the socket
582
- ensure
583
- index += 1
584
- end
585
-
586
- # Delete any dead sockets
587
- indexes_to_delete.each do |index_to_delete|
588
- @write_interface_infos.delete_at(index_to_delete)
589
- end
590
- end # connection_mutex.synchronize
591
-
592
- # Sleep until we receive a packet or for 100ms
593
- @write_mutex.synchronize do
594
- @write_condition_variable.wait(@write_mutex, 0.1)
595
- end
596
- end
597
-
701
+ # Queue the packet or data for every connected client. Never blocks on a
702
+ # socket. The mutex is only held to copy the client list so a client which
703
+ # stops reading can't delay the other clients or accepting new connections.
598
704
  def write_to_clients(method, packet_or_data)
599
- @connection_mutex.synchronize do
600
- # Send data to each client - On error drop the client
601
- indexes_to_delete = []
602
- index = 0
603
- @write_interface_infos.each do |interface_info|
604
- need_disconnect = false
605
- begin
606
- interface_bytes_written = interface_info.interface.bytes_written
607
- interface_info.interface.public_send(method, packet_or_data)
608
- diff = interface_info.interface.bytes_written - interface_bytes_written
609
- @written_raw_data_time = interface_info.interface.written_raw_data_time
610
- @written_raw_data = interface_info.interface.written_raw_data
611
- @bytes_written += diff
612
- rescue Errno::EPIPE, Errno::ECONNABORTED, IOError, Errno::ECONNRESET
613
- # Client has normally disconnected
614
- need_disconnect = true
615
- rescue Exception => e
616
- if e.message != "Stream not connected for write_raw"
617
- Logger.error "#{@name}: Error sending to client: #{e.class} #{e.message}"
618
- end
619
- need_disconnect = true
620
- end
621
-
622
- if need_disconnect
623
- Logger.info "#{@name}: Tcpip server lost write connection to #{interface_info.hostname}(#{interface_info.host_ip}):#{interface_info.port}"
624
- interface_info.interface.disconnect
625
- interface_info.interface.stream_log_pair.stop if interface_info.interface.stream_log_pair
626
- indexes_to_delete.unshift(index) # Put later indexes at front of array
627
- end
628
- index += 1
629
- end
630
-
631
- # Delete any dead sockets
632
- indexes_to_delete.each do |index_to_delete|
633
- @write_interface_infos.delete_at(index_to_delete)
634
- end
635
- end # connection_mutex.synchronize
705
+ interface_infos = @connection_mutex.synchronize { @write_interface_infos.dup }
706
+ last_index = interface_infos.length - 1
707
+ interface_infos.each_with_index do |interface_info, index|
708
+ item = packet_or_data
709
+ # Each client's protocols run concurrently and may modify the packet
710
+ # so every client but the last gets its own copy
711
+ item = packet_or_data.clone if method == :write and index != last_index
712
+ interface_info.write_queue << [method, item, interface_info.interface.socket_write_time]
713
+ rescue ClosedQueueError
714
+ # Client is being removed
715
+ end
636
716
  end
637
717
 
638
718
  def details
@@ -51,17 +51,9 @@ module OpenC3
51
51
  end
52
52
  end
53
53
 
54
- # generate the auth object
54
+ # Generate the authentication object from the standard environment variables.
55
55
  def _generate_auth
56
- if ENV['OPENC3_API_TOKEN'].nil? and ENV['OPENC3_API_USER'].nil?
57
- if ENV['OPENC3_API_PASSWORD']
58
- return OpenC3Authentication.new()
59
- else
60
- return nil
61
- end
62
- else
63
- return OpenC3KeycloakAuthentication.new(ENV['OPENC3_KEYCLOAK_URL'])
64
- end
56
+ OpenC3.authentication_from_environment
65
57
  end
66
58
 
67
59
  def _request(*method_params, **kw_params)
@@ -79,17 +79,9 @@ module OpenC3
79
79
  # JsonDRb.debug = true # Enable for debugging
80
80
  end
81
81
 
82
- # generate the auth object
82
+ # Generate the authentication object from the standard environment variables.
83
83
  def generate_auth
84
- if ENV['OPENC3_API_TOKEN'].nil? and ENV['OPENC3_API_USER'].nil?
85
- if ENV['OPENC3_API_PASSWORD']
86
- return OpenC3Authentication.new()
87
- else
88
- return nil
89
- end
90
- else
91
- return OpenC3KeycloakAuthentication.new(ENV['OPENC3_KEYCLOAK_URL'])
92
- end
84
+ OpenC3.authentication_from_environment
93
85
  end
94
86
 
95
87
  # Forwards all method calls to the remote service.
@@ -318,15 +318,20 @@ module OpenC3
318
318
  flags |= OPENC3_CBOR_FLAG_MASK
319
319
  extra_encoded = extra.as_json.to_cbor
320
320
  else
321
- extra_encoded = JSON.generate(extra.as_json, allow_nan: true)
321
+ # Force binary: JSON.generate returns UTF-8 and appending that to the
322
+ # binary entry raises Encoding::CompatibilityError once the entry holds
323
+ # a byte >= 0x80, which silently drops the packet from the log
324
+ extra_encoded = JSON.generate(extra.as_json, allow_nan: true).b
322
325
  end
323
- length += extra_encoded.length
326
+ # Count bytes, not characters, so a non-ASCII value in extra can't produce
327
+ # a short entry length
328
+ length += extra_encoded.bytesize
324
329
  end
325
330
  length += OPENC3_PACKET_SECONDARY_FIXED_SIZE + data.length
326
331
  @entry.clear
327
332
  @entry << [length, flags, packet_index, time_nsec_since_epoch].pack(OPENC3_PACKET_PACK_DIRECTIVE)
328
333
  @entry << [received_time_nsec_since_epoch].pack(OPENC3_RECEIVED_TIME_PACK_DIRECTIVE) if received_time_nsec_since_epoch
329
- @entry << [extra_encoded.length].pack(OPENC3_EXTRA_LENGTH_PACK_DIRECTIVE) << extra_encoded if extra_encoded
334
+ @entry << [extra_encoded.bytesize].pack(OPENC3_EXTRA_LENGTH_PACK_DIRECTIVE) << extra_encoded if extra_encoded
330
335
  @entry << data.force_encoding('ASCII-8BIT')
331
336
  @first_time = time_nsec_since_epoch if !@first_time or time_nsec_since_epoch < @first_time
332
337
  @last_time = time_nsec_since_epoch if !@last_time or time_nsec_since_epoch > @last_time
@@ -40,6 +40,8 @@ module OpenC3
40
40
  # 'LOG' still evaluates limits (callback handles suppressing reactions).
41
41
  # 'DISABLE' skips limits evaluation entirely for stored packets and omits
42
42
  # limits states from the decommutated output.
43
+ # @param queued [Boolean] Queue the decom stream and CVT writes (StoreQueued) rather
44
+ # than writing them directly to Redis.
43
45
  # @param metric [Metric, nil] Optional; when set, records decom_duration_seconds.
44
46
  # @param error_callback [Proc, nil] Called as error_callback.call(exception) when
45
47
  # Packet#process or Packet#check_limits raises. The microservice uses this to
@@ -47,7 +49,7 @@ module OpenC3
47
49
  # @return [Integer] Number of (sub)packets published.
48
50
  def decom_and_publish(packet, scope:, target_names:, logger:, name:,
49
51
  check_limits: true, stored_limits_mode: 'PROCESS',
50
- metric: nil, error_callback: nil)
52
+ queued: false, metric: nil, error_callback: nil)
51
53
  start = Process.clock_gettime(Process::CLOCK_MONOTONIC) if metric
52
54
  published = 0
53
55
 
@@ -75,6 +77,7 @@ module OpenC3
75
77
 
76
78
  TelemetryDecomTopic.write_packet(packet_or_subpacket,
77
79
  include_limits_states: !disable_stored_limits,
80
+ queued: queued,
78
81
  scope: scope)
79
82
  published += 1
80
83
  end
@@ -83,6 +83,8 @@ module OpenC3
83
83
  class DecomMicroservice < Microservice
84
84
  include InterfaceDecomCommon
85
85
  LIMITS_STATE_INDEX = { RED_LOW: 0, YELLOW_LOW: 1, YELLOW_HIGH: 2, RED_HIGH: 3, GREEN_LOW: 4, GREEN_HIGH: 5 }
86
+ # Queue the decom stream and CVT writes and flush them every update interval (seconds)
87
+ DEFAULT_UPDATE_INTERVAL = 0.1
86
88
 
87
89
  # Decom never uses descriptions so don't keep them in memory
88
90
  def load_descriptions?
@@ -99,6 +101,7 @@ module OpenC3
99
101
  @limits_event_topic = "#{@scope}__openc3_limits_events"
100
102
  @topics << @limits_event_topic
101
103
  Topic.update_topic_offsets(@topics, db_shard: @db_shard)
104
+ setup_queued_writes()
102
105
  # Last limits event read, used to detect events trimmed before they were read.
103
106
  # Read from the stream rather than the thread's cached offsets. The run thread
104
107
  # starts reading at or after this point, so it can only cause an extra resync.
@@ -107,8 +110,24 @@ module OpenC3
107
110
  # can fire the callback synchronously for any persisted-disabled items.
108
111
  @limits_response_queue = Queue.new
109
112
  @limits_response_thread = nil
110
- System.telemetry.limits_change_callback = method(:limits_change_callback)
111
- LimitsEventTopic.sync_system(scope: @scope)
113
+ @resyncing_limits = false
114
+ # Only for this microservice's targets. Other decom microservices in the same
115
+ # process (a MULTI or MULTIGROUP) share System and handle their own targets.
116
+ callback = method(:limits_change_callback)
117
+ @target_names.each do |target_name|
118
+ System.telemetry.packets(target_name).each do |_packet_name, packet|
119
+ packet.limits_change_callback = callback
120
+ end
121
+ rescue RuntimeError
122
+ # Target has no telemetry
123
+ end
124
+ # Likewise only apply limits events to this microservice's targets. Disabling
125
+ # limits calls the target's limits change callback every time, so if every
126
+ # decom in the process applied every event, each disable would be reported
127
+ # (and its limits response run) once per decom. These are the live packet
128
+ # hashes so dynamically added packets are included.
129
+ @limits_telemetry = System.telemetry.all.select { |target_name, _packets| @target_names.include?(target_name) }
130
+ LimitsEventTopic.sync_system(scope: @scope, telemetry: @limits_telemetry)
112
131
  target_model = TargetModel.get_model(name: @target_names[0], scope: @scope)
113
132
  @stored_limits_mode = target_model ? (target_model.stored_limits_mode || 'PROCESS') : 'PROCESS'
114
133
  @error_count = 0
@@ -116,6 +135,23 @@ module OpenC3
116
135
  @metric.set(name: 'decom_error_total', value: @error_count, type: 'counter')
117
136
  end
118
137
 
138
+ # Queue the decom stream and CVT writes like interfaces queue the raw stream writes.
139
+ # The UPDATE_INTERVAL option (TargetModel DECOM_UPDATE_INTERVAL) sets how often the
140
+ # queue is flushed in one pipelined batch. 0 (or less) writes directly to Redis.
141
+ def setup_queued_writes
142
+ update_interval = DEFAULT_UPDATE_INTERVAL
143
+ ((@config && @config['options']) || []).each do |option_name, option_value|
144
+ update_interval = option_value.to_f if option_name.to_s.upcase == 'UPDATE_INTERVAL'
145
+ end
146
+ @queued = update_interval > 0.0
147
+ if @queued
148
+ # Apply to every db_shard since writes are routed to the shard of each target.
149
+ # Decoms in a microservice group share the process's queues, so the fastest wins.
150
+ EphemeralStoreQueued.set_update_interval_all_shards(update_interval, fastest: true)
151
+ StoreQueued.set_update_interval_all_shards(update_interval, fastest: true)
152
+ end
153
+ end
154
+
119
155
  def run
120
156
  @limits_response_thread = LimitsResponseThread.new(microservice_name: @name, queue: @limits_response_queue, logger: @logger, metric: @metric, scope: @scope)
121
157
  @limits_response_thread.start()
@@ -139,7 +175,7 @@ module OpenC3
139
175
  end
140
176
  @limits_event_offset = msg_id
141
177
  event = JSON.parse(msg_hash['event'], allow_nan: true, create_additions: true)
142
- LimitsEventTopic.process_event(event, scope: @scope)
178
+ LimitsEventTopic.process_event(event, scope: @scope, telemetry: @limits_telemetry)
143
179
  elsif topic =~ /__DECOMINTERFACE/
144
180
  if msg_hash.key?('inject_tlm')
145
181
  handle_inject_tlm_with_ack(msg_hash['inject_tlm'], msg_id)
@@ -179,7 +215,14 @@ module OpenC3
179
215
  def resync_if_limits_events_trimmed
180
216
  if LimitsEventTopic.events_trimmed?(@limits_event_offset, scope: @scope, db_shard: @db_shard)
181
217
  @logger.warn("#{@name}: Limits events were trimmed before they could be read, resyncing limits")
182
- LimitsEventTopic.resync_system(scope: @scope)
218
+ # Don't publish the limits changes the resync makes. Otherwise a large
219
+ # resync can trim the stream again and cause another resync.
220
+ @resyncing_limits = true
221
+ begin
222
+ LimitsEventTopic.resync_system(scope: @scope, telemetry: @limits_telemetry)
223
+ ensure
224
+ @resyncing_limits = false
225
+ end
183
226
  end
184
227
  end
185
228
 
@@ -212,6 +255,7 @@ module OpenC3
212
255
  name: @name,
213
256
  check_limits: true,
214
257
  stored_limits_mode: @stored_limits_mode,
258
+ queued: @queued,
215
259
  metric: @metric,
216
260
  error_callback: ->(e) {
217
261
  @error_count += 1
@@ -238,7 +282,7 @@ module OpenC3
238
282
  else
239
283
  packet_time = Time.now.utc
240
284
  end
241
- if value
285
+ unless value.nil?
242
286
  message = "#{packet.target_name} #{packet.packet_name} #{item.name} = #{value} is #{item.limits.state}"
243
287
  if item.limits.values
244
288
  selected_limits_set = if item.limits.values.has_key?(System.limits_set)
@@ -258,6 +302,8 @@ module OpenC3
258
302
  # If we're blue we display the blue range (GREEN_LOW - GREEN_HIGH)
259
303
  message += " (#{values[4]} to #{values[5]})"
260
304
  end
305
+ elsif item.limits.change_type
306
+ message += " (#{item.limits.change_type})"
261
307
  end
262
308
  else
263
309
  message = "#{packet.target_name} #{packet.packet_name} #{item.name} is disabled"
@@ -283,7 +329,7 @@ module OpenC3
283
329
  time_nsec: packet_time.to_nsec_from_epoch, message: message.to_s }
284
330
  suppress_stored = packet.stored && @stored_limits_mode != 'PROCESS'
285
331
  event[:suppress_stored] = true if suppress_stored
286
- LimitsEventTopic.write(event, scope: @scope)
332
+ LimitsEventTopic.write(event, scope: @scope, publish: !@resyncing_limits)
287
333
 
288
334
  if item.limits.response && !suppress_stored
289
335
  copied_packet = packet.deep_copy