-
Notifications
You must be signed in to change notification settings - Fork 664
Rust client SDK consistent state views for callbacks #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
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.
Centril
left a comment
There was a problem hiding this 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 :)
- 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.
| /// 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. |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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."
Centril
left a comment
There was a problem hiding this 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.
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>
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>
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.
* 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>
* 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>
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
Invokemessage to the callback worker includes anArc<ClientCache>snapshot of the DB state when that callback was invoked. The callback worker stores that state in athread_local, and methods that inspecttables (e.g.
TableType::iter) read the state out of thethread_localwhen 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 whereClientCacheaccess is based on free functions or static trait methods.API
If the API is breaking, please state below what will break