Skip to content

No singletons, state: startup hooks, default shard, codecs and threading per app [4.0, breaking] - #1182

Closed
transfix wants to merge 1 commit into
p0a/no-singletons-ariadne-4from
p0a/no-singletons-state
Closed

transfix wants to merge 1 commit into
p0a/no-singletons-ariadne-4from
p0a/no-singletons-state

Conversation

@transfix

Copy link
Copy Markdown
Owner

No singletons, state: startup hooks, the default shard, codecs and threading per app. Stacked on #1176 (Ariadne part 3). Breaking (4.0).

src/cvc/state (not state_exec, which is its own PR) keeps no process-wide state now. Nothing in this area needed to stay process-wide.

What moved, and the bugs it fixes

  • Startup hooks are each app's own (slot cvc.state.startup-hooks). A hook runs once, when that app's root is made, or at once if the root already exists.
    • startup_connection holds the hooks weakly, so it can outlive its app.
    • __system.start is set by state::instance() itself; the file-scope registrars are gone. With USING_XMLRPC, the __system.xmlrpc monitor starts there too, through xmlrpc_server_startup(app&). That build is off by default, so it was only syntax-checked.
  • The default shard is a per-app cell (slot cvc.state.default-shard). Bug fixed: the old map was keyed by app* and never pruned, so a new app at a dead app's address inherited its shard. A shard keeps a reference to the cell it installed itself in, so uninstalling never touches the app.
  • send_message's ids are a random per-shard tag plus a count, not a process-wide sequence. Bug fixed: a shard made again under the same node id repeated ids, and a peer's bus drops repeats. The header comment wrongly said an empty id stays empty; that is corrected.
  • The compression registry is shared(app&), the app's own (slot cvc.state.compression-registry). Shards and the session's hydrator use their app's.
  • Sync suppression: the thread_local depth is the app's set of suppressed threads (slot cvc.state.sync-suppression). Bug fixed: a remote apply on one app hid local changes made on the same thread to a different app (an in-process fan-out, or a slot the apply triggers).
  • The volume and geometry codecs: decoded objects were built on a hidden per-thread app, which went away when the thread exited while the object lived on. They now belong to the app that registered the codecs (the shard's).
  • state_object<T>::_useThreading (one flag per template type, in a header) is a per-app default, with setInstanceThreading still overriding it per object. cvcGL and volren only use setInstanceThreading.
  • The rest:
    • the JSON write-pin stack is a member of state_json_reader;
    • the CRC-32 table is constexpr;
    • the ws debug variable is read per broadcast;
    • the Windows-only once_flag in grpc_dns_resolver.h is gone (setting the variable again is harmless);
    • the hex digit tables in state_device_key / state_identity are constexpr.

Public API changes

  • state::on_startup(app&, f), both overloads. state::_startup, _appStartup and the init-vector typedefs are gone.
  • state_cluster_shard::default_for(app&).
  • state_compression_registry::shared(app&).
  • register_volume_geometry_codecs(app&, registry).
  • state_object<T>::setUseThreading / getUseThreading are replaced by set_state_object_threading(app&, bool) / state_object_threading(app&).
  • pycvc.i drops its %ignores for the removed statics.

Allowlist: -32 lines (cvc 23, cvcGL 9)

  • state.cpp (the startup counter, mutex, guard and the two vectors);
  • state_cluster_shard.cpp (the default registry, its mutex and guard, and send_message's sequence);
  • the compression registry instance and guard;
  • the CRC table;
  • state_json.cpp's pin stack;
  • state_sync_adapter.cpp's depth;
  • the ws debug flag and guard;
  • state_volume_codec.cpp's two decode contexts and guards;
  • _useThreading for volren's and volslice's settings (cvc) and nine cvcGL types.

Tests

Two apps in both orders, apps on threads, and a later app starting clean, in:

  • state_test (the startup hooks; threading per app and on threads);
  • state_send_message_test (the default shard per app; ids after a shard is made again);
  • state_sync_adapter_test (suppression per app; another app's change not hidden);
  • state_compression_registry_test;
  • state_volume_codec_test (decoded objects belong to the registering app);
  • state_deep_tree_test (JSON readers for several apps).

multi_app_integration_test gains a state_layer part: the default shard and a message, compression codecs, the threading default, a startup callback and a decoded volume.

Locally (Windows, MSVC Release):

  • the state suites above, state_delta_codec_test, state_cluster_shard_test, distributed_state_session_test, geometry_height_field_test;
  • app_test 121, multi_app_integration_test, volren_state_test, volslice_state_test;
  • cvc_cli_test State* (14), cvcgl_camera, cvcgl_fps_hud;
  • all 64 pycvc / pycvc_gl ctests.

Not built on Windows (conditional): state_transport_ipc_test (updated for shared(app&)), state_transport_ws_test, stream_bridge_test, room_relay_integration_test. CI builds them.

CI

Full run: the push to ci/full/no-singletons-state, https://github.com/transfix/libcvc/actions/runs/43206788348.

…ing per app

src/cvc/state (except state_exec) and state_object<T> keep no process-wide state. What each
static held moves onto the app (app::slot) or into the object that uses it.

Moved:
- state::on_startup: the process-wide _startup/_appStartup registries, their mutex and id
  counter become the app's own hooks (slot cvc.state.startup-hooks). on_startup(app &, f)
  runs f once for that app, when its root is made, or at once if it was made already; a
  startup_connection names the hooks weakly, so it may outlive the app. The built-in
  __system.start default (and, with USING_XMLRPC, the __system.xmlrpc monitor) run from
  state::instance() itself instead of file-scope static registrars.
- state_cluster_shard::default_for(app &): the default-shard map keyed by app address (never
  pruned, so a new app at a dead one's address could inherit its shard) becomes a per-app
  cell (cvc.state.default-shard). A shard keeps the cell it installed itself in, so
  uninstalling never touches the app. send_message's process-wide id counter becomes a
  per-shard random tag plus a count, so a shard made again under the same node id repeats
  no id (a peer's bus would drop it).
- state_compression_registry::shared(app &): the process-wide registry is the app's own
  (cvc.state.compression-registry); shards and the session's hydrator use their app's.
- state_sync_adapter: the thread_local suppression depth, shared by every adapter in the
  process, becomes the app's (cvc.state.sync-suppression): the threads applying a remote
  mutation to the app, or in a suppression_scope on it. A remote apply on one app no longer
  hides a local change to another app made on the same thread (an inproc fan-out, or a slot
  the apply sets off).
- register_volume_geometry_codecs(app &, registry): decoded volumes and geometries were made
  on a thread_local app (one hidden app per thread, gone at thread exit while the decoded
  object may live on); they are now the registering app's (the shard's).
- state_object<T>::_useThreading (a per-type static flag, in cvc, volren, volslice and
  cvcGL instantiations) becomes the app's default (set_state_object_threading(app &, bool),
  state_object_threading(app &); cvc.state.object-threading); setInstanceThreading still
  overrides it per object. setUseThreading/getUseThreading are gone. cvcGL and volren only
  use setInstanceThreading, so they are unchanged.
- state_json_reader: the thread_local write-pin stack is the reader's own member.
- state_delta_codec: the CRC-32 table is constexpr (read-only data).
- state_transport_ws: the CVC_WS_BROADCAST_DEBUG flag is read each broadcast (about once a
  second) instead of cached in a function-local static.
- grpc_dns_resolver.h (Windows only, not in the scan): the once_flag goes; setting
  GRPC_DNS_RESOLVER again when it is already set is harmless.
- state_device_key / state_identity: hex digit tables are constexpr arrays.

Tests (several apps each: two apps in both orders, apps on threads at once, a later app made
after another is gone starts clean):
- state_test: on_startup per app (runs once, at once after the root, disconnect, apps on
  threads, a handle outliving its app); state_object threading per app (replaces the
  per-template-type test) and on threads; the threading tests no longer restore a global.
- state_send_message_test: default shard per app, on threads, and message ids that survive
  a shard made again under the same node id.
- state_sync_adapter_test: suppression per app (and shared by an app's adapters), a remote
  apply on one app not hiding another app's change, apps on threads.
- state_compression_registry_test: each app has its own registry, on threads.
- state_volume_codec_test: decoded objects belong to the registering app, on threads.
- state_deep_tree_test: json readers of several apps interleaved on one thread and on threads.
- multi_app_integration_test: the state_layer workload part (default shard and message,
  compression codecs, state_object threading default, startup callback, decoded volume).

Allowlist: 32 lines removed (cvc 23, cvcGL 9).
@transfix

Copy link
Copy Markdown
Owner Author

Replaced by #1203, which lands the whole no-singletons stack as one PR. This branch's head is an ancestor of #1203's branch, so its commits are all there; the branch is kept.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant