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

fix(graphQL): GraphQL subscription not notified on DialogActivityCreated #1187

Merged

Conversation

oskogstad
Copy link
Collaborator

@oskogstad oskogstad commented Sep 24, 2024

Description

Removes limitations on creating DialogUpdatedEvent
(They where not created if the only thing changed in the aggregate was added activities)

We now create DialogUpdatedEvent for any change in the aggregate. When processing events after a successful DB save we check for either a DialogUpdated or DialogDeleted per dialogID, and notify each dialogEvents GraphQL topic.

Related Issue(s)

Verification

  • Your code builds clean without any errors or warnings
  • Manual testing done (required)
  • Relevant automated test added (if you find this hard, leave it and we'll help out)

Documentation

  • Documentation is updated (either in docs-directory, Altinnpedia or a separate linked PR in altinn-studio-docs., if applicable)

Summary by CodeRabbit

  • New Features
    • Enhanced event handling for dialog updates, streamlining the process and reducing redundant event processing.
    • Introduced a mechanism to prevent multiple sends for the same dialog within a short time frame, improving efficiency.
  • Bug Fixes
    • Improved logic for processing domain events, ensuring clearer and more efficient handling.

@oskogstad oskogstad requested a review from a team as a code owner September 24, 2024 21:57
Copy link
Contributor

coderabbitai bot commented Sep 24, 2024

📝 Walkthrough
📝 Walkthrough
📝 Walkthrough

Walkthrough

The changes streamline the event handling process in the DialogEntity and ConvertDomainEventsToOutboxMessagesInterceptor classes. The OnUpdate method in DialogEntity now directly adds a DialogUpdatedDomainEvent to the _domainEvents list without conditions. In the interceptor, a new method, SendDialogUpdated, implements throttling to prevent duplicate event sending within a short time frame. The SavedChangesAsync method is also refactored to improve clarity and efficiency in processing domain events.

Changes

File Change Summary
src/Digdir.Domain.Dialogporten.Domain/Dialogs/Entities/DialogEntity.cs, src/Digdir.Domain.Dialogporten.Infrastructure/DomainEvents/Outbox/ConvertDomainEventsToOutboxMessagesInterceptor.cs Modified OnUpdate method to simplify event handling in DialogEntity. Added SendDialogUpdated method in the interceptor for throttling dialog updated events and refactored SavedChangesAsync for improved event processing.

Possibly related issues

Possibly related PRs

Suggested reviewers

  • knuhau: Suggested as a reviewer for their expertise in the related codebase.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

Review details

Configuration used: .coderabbit.yaml
Review profile: ASSERTIVE

Commits

Files that changed from the base of the PR and between cdceec2 and 0270b64.

Files selected for processing (1)
  • src/Digdir.Domain.Dialogporten.Infrastructure/DomainEvents/Outbox/ConvertDomainEventsToOutboxMessagesInterceptor.cs (4 hunks)

@elsand
Copy link
Member

elsand commented Sep 26, 2024

I'm not sure I understand this change. I'm assuming the DialogActivityCreated is produced first as we walk up the aggregate, and that the DialogUpdated event effectively never gets produced whenever an activity is appended? Won't this break consumers who only listen for DialogUpdated events?

@oskogstad
Copy link
Collaborator Author

oskogstad commented Sep 30, 2024

I'm not sure I understand this change. I'm assuming the DialogActivityCreated is produced first as we walk up the aggregate, and that the DialogUpdated event effectively never gets produced whenever an activity is appended? Won't this break consumers who only listen for DialogUpdated events?

You mean for Altinn Events? This does not affect that, only the GQL subscription.

The old code only notified GQL-subs when DialogUpdated happened, so if a SO sends a patch which only adds a DialogActivity, AF would never be notified.

This new version pings the GQL-sub on DialogActivityCreated and DialogUpdated, basically combining the two events. (And adding a short-lived cache so they don't get two events)

The alternative is to have two separate pings for each of those, and having AF write two new queries, one that fetches the dialog without the activities, and one where they only fetch activities.
They would then get two events with almost no delay and perform two trips to the backend.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: ASSERTIVE

📥 Commits

Files that changed from the base of the PR and between 0270b64 and 845c565.

📒 Files selected for processing (2)
  • src/Digdir.Domain.Dialogporten.Domain/Dialogs/Entities/DialogEntity.cs (1 hunks)
  • src/Digdir.Domain.Dialogporten.Infrastructure/DomainEvents/Outbox/ConvertDomainEventsToOutboxMessagesInterceptor.cs (1 hunks)
🧰 Additional context used
📓 Learnings (1)
src/Digdir.Domain.Dialogporten.Infrastructure/DomainEvents/Outbox/ConvertDomainEventsToOutboxMessagesInterceptor.cs (1)
Learnt from: oskogstad
PR: digdir/dialogporten#1187
File: src/Digdir.Domain.Dialogporten.Infrastructure/DomainEvents/Outbox/ConvertDomainEventsToOutboxMessagesInterceptor.cs:105-122
Timestamp: 2024-09-24T22:26:57.178Z
Learning: In the `ConvertDomainEventsToOutboxMessagesInterceptor` class, using a static in-memory cache (`UpdateEventThrottleCache`) for throttling in the `SendDialogUpdated` method is acceptable because only one instance of the WebAPI receives the REST request that generates these events. The throttling is meant to be local to prevent clients from receiving multiple notifications when both an update event and an activity created event are generated in the same transaction.
🔇 Additional comments (2)
src/Digdir.Domain.Dialogporten.Domain/Dialogs/Entities/DialogEntity.cs (1)

77-78: Simplified OnUpdate method: Approve with concerns

The simplification of the OnUpdate method improves code readability and maintainability. It ensures that a DialogUpdatedDomainEvent is always published when the method is called, which aligns with the goal of combining DialogActivityCreated and DialogUpdated events as mentioned in the PR discussion.

However, I have two concerns:

  1. This change may lead to more frequent event publishing, which could potentially impact performance. Can you confirm if this increase in event frequency is intentional and has been considered?

  2. The PR objectives mention implementing throttling for update events, but this change doesn't seem to directly address that. Could you clarify where the throttling logic is implemented, or if it's handled elsewhere in the system?

To better understand the impact of this change, could you run the following script to check for other usages of DialogUpdatedDomainEvent in the codebase?

This will help us verify if there are any other places where the event is used or filtered, which might be affected by this change.

src/Digdir.Domain.Dialogporten.Infrastructure/DomainEvents/Outbox/ConvertDomainEventsToOutboxMessagesInterceptor.cs (1)

67-95: ⚠️ Potential issue

Removal of Throttling May Lead to Duplicate Notifications

Based on previous learnings, a throttling mechanism using a static in-memory cache (UpdateEventThrottleCache) was implemented to prevent clients from receiving multiple notifications when both an update event and a DialogActivityCreated event are generated in the same transaction. The current changes remove this throttling mechanism, which may reintroduce the issue of clients receiving duplicate notifications, potentially leading to unnecessary load and suboptimal user experience.

Consider reintroducing the throttling mechanism or adjusting the event handling logic to ensure that clients receive only a single notification when related events occur in the same transaction.

⛔ Skipped due to learnings
Learnt from: oskogstad
PR: digdir/dialogporten#1187
File: src/Digdir.Domain.Dialogporten.Infrastructure/DomainEvents/Outbox/ConvertDomainEventsToOutboxMessagesInterceptor.cs:105-122
Timestamp: 2024-09-24T22:26:57.178Z
Learning: In the `ConvertDomainEventsToOutboxMessagesInterceptor` class, using a static in-memory cache (`UpdateEventThrottleCache`) for throttling in the `SendDialogUpdated` method is acceptable because only one instance of the WebAPI receives the REST request that generates these events. The throttling is meant to be local to prevent clients from receiving multiple notifications when both an update event and an activity created event are generated in the same transaction.

Copy link

sonarqubecloud bot commented Oct 1, 2024

@oskogstad
Copy link
Collaborator Author

Change suggestion from Magnus pushed
Re-tagging this issue for the next planning, quicker to explain there

@oskogstad oskogstad changed the title feat(graphQL): Add update event throttling to dialog event subscription fix(graphQL): GraphQL subscription not notified on DialogActivityCreated Oct 2, 2024
@oskogstad oskogstad merged commit f28e291 into main Oct 2, 2024
23 checks passed
@oskogstad oskogstad deleted the feat/add-update-event-throttling-on-gql-subscription branch October 2, 2024 12:53
oskogstad pushed a commit that referenced this pull request Oct 2, 2024
🤖 I have created a release *beep* *boop*
---


##
[1.20.2](v1.20.1...v1.20.2)
(2024-10-02)


### Bug Fixes

* (webAPI): Add revision to search dto (ServiceOwner)
([#1216](#1216))
([3b6d130](3b6d130))
* **graphQL:** GraphQL subscription not notified on
DialogActivityCreated
([#1187](#1187))
([f28e291](f28e291))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
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