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

Lookup lifecycle reducers by lifecycle flag, not by name #2132

Merged
merged 4 commits into from
Jan 17, 2025

Conversation

coolreader18
Copy link
Collaborator

Description of Changes

I uplifted the ReducerId lookup stuff from module_host into schema, so that it lets the ModuleHost look up lifecycle reducers by their ids. I was a bit confused about ReducersMap, since it claimed to sort the reducer names but afaict did nothing of the sort. Is it that the RawReducerDefV9s supposed to be sorted? Cause if so I can't find any validation code enforcing it.

Expected complexity level and risk

2

Testing

It's not possible to fully test this since we don't have csharp modules in smoketests yet, but the test_lifecycle_reducers_cant_be_called test still passes and host doesn't mention the dunderscore names at all.

@coolreader18 coolreader18 requested a review from kazimuth January 16, 2025 20:22
@cloutiertyler
Copy link
Contributor

Considering we were able to add windows to CI, how difficult would it be to add CSharp modules to the smoketests?

@coolreader18
Copy link
Collaborator Author

I think @RReverser was working on that? That might've been the wrong impression though

@RReverser
Copy link
Contributor

how difficult would it be to add CSharp modules to the smoketests?

Windows is not a requirement for that, as we already run unit and integration tests for C# modules in Linux-based Github Actions with no issues. It's just a matter of someone actually writing said tests.

Here we don't even need smoketests actually, integration tests are enough. C# modules were already emitting correct modules and we have a test for connect/disconnect events. The problem is that Rust counterpart wasn't (isn't?) ready yet, so our tests have to match Rust naming, which hid the issue that occurs when using custom reducer names.

Once Rust is fixed to preserve custom reducer names as well (#1891), we'll just need to change the names of reducers in

// TODO: these method names shouldn't be special, but for now they have to match Rust snapshots.
// See https://github.com/clockworklabs/SpacetimeDB/issues/1891.
// For now, disable the error that would be raised due to those names starting with `__`.
#pragma warning disable STDB0009
[SpacetimeDB.Reducer(ReducerKind.ClientConnected)]
public static void __identity_connected__(ReducerContext ctx)
{
ctx.Db.connected.Insert(new Connected { identity = ctx.CallerIdentity });
}
[SpacetimeDB.Reducer(ReducerKind.ClientDisconnected)]
public static void __identity_disconnected__(ReducerContext ctx)
, and it will close this testing gap.

@RReverser
Copy link
Contributor

I was a bit confused about ReducersMap, since it claimed to sort the reducer names but afaict did nothing of the sort.

That might be outdated comment. They used to be sort, but they must not be - in fact, that was the cause of an issue fixed by #1987.

@coolreader18
Copy link
Collaborator Author

If I just make that change as part of this, it'll fix the testing issue, and it's really pretty straightforward.

@RReverser
Copy link
Contributor

That does sound great.

Once you fix the Rust bindings to preserve reducer names, the ensure_same_schema test should immediately start failing (as expected), and then the linked C# counterpart can be renamed to original reducer names as well.

@coolreader18
Copy link
Collaborator Author

@RReverser they seem to not be failing?

@coolreader18 coolreader18 linked an issue Jan 17, 2025 that may be closed by this pull request
Copy link
Collaborator

@bfops bfops left a comment

Choose a reason for hiding this comment

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

LGTM on my codeowned files (I didn't review the rest of the PR).

@RReverser
Copy link
Contributor

@RReverser they seem to not be failing?

Hmm. Probably because those reducers are ignored by ponder_auto_migrate because we filter them out now.

let diff = ponder_auto_migrate(&cs, &rs)

We used to preserve them so they were exposed both as regular reducers and as event handlers, but the filtering logic changed as part of this PR.

We'll need to enhance the diffing test to account for those hidden reducers in the future, but for now can you change connect-disconnect's Lib.cs to match the rust version regardless please?

Copy link
Contributor

@kazimuth kazimuth left a comment

Choose a reason for hiding this comment

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

Approved once my caveat is addressed.

(In retrospect I wish we'd just done reducer_id_from_name but it's too late now.)

@coolreader18 coolreader18 added this pull request to the merge queue Jan 17, 2025
Merged via the queue into master with commit cf6ac18 Jan 17, 2025
8 of 9 checks passed
bfops pushed a commit that referenced this pull request Jan 24, 2025
@coolreader18 coolreader18 linked an issue Jan 28, 2025 that may be closed by this pull request
@coolreader18 coolreader18 deleted the noa/lifecycles-v9 branch February 11, 2025 02:53
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.

Prohibit calling lifecycle reducers in a non-janky way Special reducers don't preserve user-defined names
5 participants