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

refactor: Replace subscription events publisher #2686

Merged

Conversation

nasdf
Copy link
Member

@nasdf nasdf commented Jun 4, 2024

Relevant issue(s)

Resolves #2685

Description

This PR replaces subscription event publishers with a simple go channel. This is a pre-requisite to a follow up events package refactor.

Tasks

  • I made sure the code is well commented, particularly hard-to-understand areas.
  • I made sure the repository-held documentation is changed accordingly.
  • I made sure the pull request title adheres to the conventional commit style (the subset used in the project can be found in tools/configs/chglog/config.yml).
  • I made sure to discuss its limitations such as threats to validity, vulnerability to mistake and misuse, robustness to invalidation of assumptions, resource requirements, ...

How has this been tested?

make test

Specify the platform(s) on which this was tested:

  • MacOS

@nasdf nasdf added the refactor This issue specific to or requires *notable* refactoring of existing codebases and components label Jun 4, 2024
@nasdf nasdf added this to the DefraDB v0.12 milestone Jun 4, 2024
@nasdf nasdf self-assigned this Jun 4, 2024
@nasdf nasdf changed the title refactor: Replace subscription events refactor: Replace subscription events publisher Jun 4, 2024
Copy link

codecov bot commented Jun 4, 2024

Codecov Report

Attention: Patch coverage is 91.93548% with 5 lines in your changes missing coverage. Please review.

Project coverage is 78.08%. Comparing base (dd0e5af) to head (2e7d81b).

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #2686      +/-   ##
===========================================
- Coverage    78.16%   78.08%   -0.08%     
===========================================
  Files          308      308              
  Lines        23081    23077       -4     
===========================================
- Hits         18040    18019      -21     
- Misses        3675     3687      +12     
- Partials      1366     1371       +5     
Flag Coverage Δ
all-tests 78.08% <91.94%> (-0.08%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
cli/request.go 75.00% <100.00%> (+2.08%) ⬆️
http/client.go 55.79% <100.00%> (+0.64%) ⬆️
http/handler_ccip.go 95.38% <100.00%> (ø)
http/handler_store.go 85.38% <100.00%> (+0.46%) ⬆️
internal/db/request.go 83.33% <100.00%> (-3.15%) ⬇️
internal/db/subscriptions.go 87.23% <88.89%> (-1.14%) ⬇️

... and 9 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update dd0e5af...2e7d81b. Read the comment docs.

@nasdf nasdf requested a review from a team June 4, 2024 23:15
@@ -265,9 +265,9 @@ type RequestResult struct {
// GQL contains the immediate results of the GQL request.
GQL GQLResult

// Pub contains a pointer to an event stream which channels any subscription results
// if the request was a GQL subscription.
Pub *events.Publisher[events.Update]
Copy link
Contributor

Choose a reason for hiding this comment

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

discussion: I think you are removing functionality here - IIRC the events package stuff allows for multiple concurrent readers of the stream. The simple Go chan that you are replacing it lacks this functionality and will mean any concurrent readers will be competing with each other for each item.

The CLI/http clients are unaffected, but the embedded Go client is.

Copy link
Member Author

Choose a reason for hiding this comment

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

It makes more sense to me that we assume one reader per request by default. The consumer of the channel then has the option to decide if they want to create an events.Publisher and publish the channel results to it.

return nil, client.NewErrUnexpectedType[request.ObjectSubscription]("SubscriptionSelection", selections)
}
// unsubscribing from this publisher will cause a race condition
// https://github.com/sourcenetwork/defradb/issues/2687
Copy link
Contributor

Choose a reason for hiding this comment

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

question: Is this a new issue, or was the old code affected to?

And where is the race condition? Is it a test artefact, or production code?

Copy link
Member Author

Choose a reason for hiding this comment

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

I believe this is an old issue. The previous code never called Unsubscribe. The race happens between the db.Close and the pub.Unsubscribe so it's possible to happen in production, but likely would cause no issues.

Copy link
Contributor

@AndrewSisley AndrewSisley left a comment

Choose a reason for hiding this comment

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

The code is easy to read, but I have a todo/question RE timeouts before I can approve :)

select {
case val := <-pub.Event():
evt = val
case <-ctx.Done():
Copy link
Contributor

Choose a reason for hiding this comment

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

todo: By not looping through the pub.Event() with a direct for loop, I think you may have removed the handling of a timeout - it looks like in the event of a timeout evt will be default/empty and bad things might happen later in this func.

Can you please confirm the behaviour, and make any necessary adjustments please?

(note: IIRC testing of subscriptions is not as comprehensive as most of our other queries, and it uses an old framework)

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch! I've updated it to handle the channel closing.

Copy link
Contributor

@AndrewSisley AndrewSisley left a comment

Choose a reason for hiding this comment

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

LGTM, I'm curious as to what is coming next :)

@nasdf nasdf merged commit a7004b2 into sourcenetwork:develop Jun 5, 2024
36 of 37 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactor This issue specific to or requires *notable* refactoring of existing codebases and components
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use event system for subsystem events only
2 participants