Skip to content
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

core: Store inputs (reducer info + args) in commitlog #1091

Merged
merged 1 commit into from
Apr 18, 2024
Merged

Conversation

kim
Copy link
Contributor

@kim kim commented Apr 15, 2024

Prerequisite for auto-disconnect after a database crash, requested for analytics purposes.

Expected complexity level and risk

2

Testing

Describe any testing you've done, and any testing you'd like your reviewers to do,
so that you're confident that all the changes work as expected!

  • I wrote a test

pub(crate) fn commit_tx_with_reducer_info(
&self,
ctx: &ExecutionContext,
reducer_info: Option<ReducerOp<'_>>,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This does not exactly spark joy. How do we feel about repurposing ExecutionContext to optionally contain more metadata about the reducer?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd ask @joshua-spacetime what the original intention for ExecutionContext was first. I know it was for metrics, but did he also have this kind of thing in mind?

Copy link
Collaborator

Choose a reason for hiding this comment

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

No objections @kim. That's what it's for.

@kim kim force-pushed the kim/log-inputs branch from 8ee042c to 2fa7989 Compare April 15, 2024 17:11
@bfops bfops added the release-any To be landed in any release window label Apr 15, 2024
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.

Some minor thoughts. Not familiar enough with this code to approve though.

crates/core/src/host/mod.rs Outdated Show resolved Hide resolved
crates/core/src/db/relational_db.rs Outdated Show resolved Hide resolved
crates/core/src/db/relational_db.rs Outdated Show resolved Hide resolved
// - write to the reducer log
// (sometimes used to assert that a reducer indeed ran)
// - schedule a reducer to run at a later time
// - potentially in the future: perform RPCs
Copy link
Contributor

@cloutiertyler cloutiertyler Apr 16, 2024

Choose a reason for hiding this comment

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

Not sure what the first one is but in the near future the second and third ones will write to the log. Writing to the log for RPCs is necessary to ensure exactly once delivery/processing (because in the event of a crash you need to be able to retransmit those messages for which you had not yet received an ack). You can also imagine a world in which we would only want to send RPCs to other databases iff the transactions succeeds.

I think it might also be related to distributed transactions as well if we ever wanted to do that. i.e. transactions between actors.

Basically things change quite a bit if we allow side effects in reducers which we currently do not do (or shouldn't do but do do with scheduled reducers).

Definitely something to noodle on into the future for sure.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, I meant to change the logic to ignore all transactions which do not mutate the database. But, that is wrong:

A module may not define connect / disconnect, or only define one of them. Or define them, but not modify any tables. Or worse, return an error.

I don't know what to do in the last case, but ignoring that we do indeed need to write out transactions which originate from reducer calls but don't modify the database.

Copy link
Contributor

@cloutiertyler cloutiertyler left a comment

Choose a reason for hiding this comment

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

Overall seems fine to me. Will wait for others' comments to be resolved before approving.

@kim kim force-pushed the kim/log-inputs branch from 2fa7989 to 9bbb1bb Compare April 16, 2024 08:37
Copy link
Contributor

@gefjon gefjon left a comment

Choose a reason for hiding this comment

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

:excellent:

crates/core/src/db/relational_db.rs Outdated Show resolved Hide resolved
@kim kim force-pushed the kim/log-inputs branch from 9bbb1bb to f4d27de Compare April 17, 2024 10:40
@kim
Copy link
Contributor Author

kim commented Apr 17, 2024

Reroll:

  • Store the reducer metadata in ExecutionContext
  • Borrow the introduction of the dbic method from Protobufectomy: server #1077, which allows to get access to the database from an abstract module context
  • Using that database, commit an empty transaction including the reducer context in case either one of __identity_connected__ or __identity_disconnected__ is not defined in the module. This ensures we always have those inputs paired in the commitlog.
  • Do not log transactions which don't modify the database state, except for __identity_connected__ / __identity_disconnected__, which are always logged for the same reason.

@kim kim force-pushed the kim/log-inputs branch from f4d27de to 3c03b26 Compare April 17, 2024 10:50
@kim kim requested review from cloutiertyler and gefjon April 17, 2024 10:52
// timetravel queries may benefit from seeing all inputs, even if
// the database state did not change.
let is_noop = || inserts.is_empty() && deletes.is_empty();
let is_connect_disconnect = |ctx: &ReducerContext| {
Copy link
Contributor

Choose a reason for hiding this comment

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

This thought is sort of unrelated to this commit, but I'll just note that we should really make sure these reducers are not callable from outside of SpacetimeDB. I don't actually know that they aren't.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch ... "dunder" reducers are callable like any other reducer afaics. Because, they are just like any other reducer?

Copy link
Contributor

@cloutiertyler cloutiertyler 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 good to me.

Prerequisite for auto-disconnect after a database crash, requested for
analytics purposes.
@kim kim force-pushed the kim/log-inputs branch from 3c03b26 to ecc2cf8 Compare April 18, 2024 10:11
@kim kim added this pull request to the merge queue Apr 18, 2024
Merged via the queue into master with commit 2894d36 Apr 18, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-any To be landed in any release window
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants