Skip to content

Conversation

@gefjon
Copy link
Contributor

@gefjon gefjon commented Jun 23, 2023

Description of Changes

Prior to this commit, callbacks in the Rust client SDK shared access to the global ClientCache while running asynchronously. This meant that a long-running or delayed callback could observe the ClientCache in a state later than the one that caused the callback's invocation, and had no way to access the specific state for which it was invoked.

With this commit, each Invoke message to the callback worker includes an Arc<ClientCache> snapshot of the DB state when that callback was invoked. The callback worker stores that state in a thread_local, and methods that inspect
tables (e.g. TableType::iter) read the state out of the thread_local when it is present. This allows callbacks to observe exactly the state which caused their invocation, never a later state, while maintaining the C#-like API where ClientCache access is based on free functions or static trait methods.

API

  • This is a breaking change to the module API
  • This is a breaking change to the ClientAPI

If the API is breaking, please state below what will break

gefjon added 3 commits June 20, 2023 17:24
This requires deriving `Debug` for tables and generated types.
In the future, we should be careful that all spacetime types
can derive `Debug`.
Prior to this commit, callbacks in the Rust client SDK shared access to the global
ClientCache while running asynchronously. This meant that a long-running or delayed
callback could observe the ClientCache in a state later than the one that caused the
callback's invocation, and had no way to access the specific state for which it was
invoked.

With this commit, each `Invoke` message to the callback worker includes an
`Arc<ClientCache>` snapshot of the DB state when that callback was invoked. The callback
worker stores that state in a `thread_local`, and methods that inspect
tables (e.g. `TableType::iter`) read the state out of the `thread_local` when it is
present. This allows callbacks to observe exactly the state which caused their invocation,
never a later state, while maintaining the C#-like API where `ClientCache` access is based
on free functions or static trait methods.
@gefjon gefjon requested review from Centril and jdetter June 23, 2023 15:20
Copy link
Contributor

@Centril Centril left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will review more tomorrow :)

@gefjon gefjon mentioned this pull request Jun 27, 2023
2 tasks
- Type alias `ClientCacheView = Arc<ClientCache>` makes shared views into a particular
  `ClientCache` state more clear.
- Type alias `SharedCell<T> = Arc<Mutex<T>>` makes thread-safe shared mutable places more
  clear.
- CLI generate helper functions `iter_reducer_items` and `iter_table_items` deduplicate
  loops and reduce rightward drift.
- Assorted doc comments.
@gefjon gefjon requested a review from Centril June 28, 2023 16:41
Comment on lines 31 to 45
/// The most recent state of the `ClientCache`, kept in a shared cell
/// so that the `receiver_loop` can update it, and non-callback table accesses
/// can observe it via `global_connection::current_or_global_state`.
///
/// If you expand these type aliases, you get `Arc<Mutex<Arc<ClientCache>>>`,
/// which looks somewhat strange. The type aliases are intended to make clear
/// the purpose of the two layers of refcounting:
///
/// The outer layer, around the `Mutex`, allows for a shared mutable cell
/// by which multiple concurrent workers can communicate the most recent state.
///
/// The inner layer, around the `ClientCache`, allows those workers to
/// cheaply extract a reference to the `ClientCache`
/// without holding a lock for the lifetime of that reference,
/// and without changes to the state invalidating the reference.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is excellent! It aids understanding by a lot! Perhaps the word "snapshot" could be added in the last paragraph, if it feels natural?

///
/// The existing `ClientCacheView` in `client_cache` is not mutated,
/// so handles on it held in other places (e.g. by callback workers)
/// remain valid.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add "That is, these workers have their own snapshots."

Copy link
Contributor

@Centril Centril left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great! With the last changes, things were easier to understand.

@gefjon gefjon enabled auto-merge (squash) June 29, 2023 14:53
@gefjon gefjon merged commit 5b28cb6 into master Jun 29, 2023
@Centril Centril deleted the phoebe/client-sdk-consistent-state branch June 29, 2023 17:40
cloutiertyler pushed a commit that referenced this pull request Aug 1, 2023
Prior to this commit, callbacks in the Rust client SDK shared access to the global
ClientCache while running asynchronously. This meant that a long-running or delayed
callback could observe the ClientCache in a state later than the one that caused the
callback's invocation, and had no way to access the specific state for which it was
invoked.

With this commit, each `Invoke` message to the callback worker includes an
`Arc<ClientCache>` snapshot of the DB state when that callback was invoked. The callback
worker stores that state in a `thread_local`, and methods that inspect
tables (e.g. `TableType::iter`) read the state out of the `thread_local` when it is
present. This allows callbacks to observe exactly the state which caused their invocation,
never a later state, while maintaining the C#-like API where `ClientCache` access is based
on free functions or static trait methods.

---------

Co-authored-by: John Detter <4099508+jdetter@users.noreply.github.com>
cloutiertyler pushed a commit that referenced this pull request Aug 1, 2023
Prior to this commit, callbacks in the Rust client SDK shared access to the global
ClientCache while running asynchronously. This meant that a long-running or delayed
callback could observe the ClientCache in a state later than the one that caused the
callback's invocation, and had no way to access the specific state for which it was
invoked.

With this commit, each `Invoke` message to the callback worker includes an
`Arc<ClientCache>` snapshot of the DB state when that callback was invoked. The callback
worker stores that state in a `thread_local`, and methods that inspect
tables (e.g. `TableType::iter`) read the state out of the `thread_local` when it is
present. This allows callbacks to observe exactly the state which caused their invocation,
never a later state, while maintaining the C#-like API where `ClientCache` access is based
on free functions or static trait methods.

---------

Co-authored-by: John Detter <4099508+jdetter@users.noreply.github.com>
bfops pushed a commit that referenced this pull request Jul 16, 2025
Merged the player and logged_out_player tables into only a player table, to match the way it is used in the tutorial documentation. Slight changes to how logged_out_player was being referenced was required due to the tables having different architecture.
bfops pushed a commit that referenced this pull request Jul 17, 2025
* Copied changes from BitCraft

* Applied state diff changes

* State diffs complete

* Compilation fix

* Fix build issues

---------

Co-authored-by: John Detter <no-reply@boppygames.gg>
bfops pushed a commit that referenced this pull request Aug 7, 2025
* Copied changes from BitCraft

* Applied state diff changes

* State diffs complete

* Compilation fix

* Fix build issues

---------

Co-authored-by: John Detter <no-reply@boppygames.gg>
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.

4 participants