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

GitAuto: [FEATURE] Add Saga capability #581

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

gitauto-ai[bot]
Copy link
Contributor

@gitauto-ai gitauto-ai bot commented Oct 23, 2024

Resolves #330

What is the feature

Implement SAGA pattern support in the CommandsConsumer and EventsConsumer classes to handle complex workflows involving multiple commands and events. This enhancement will enable us to manage long-running processes, coordinate processes across multiple events and commands, and implement timeouts effectively.

Why we need the feature

Our current system lacks support for the SAGA pattern, limiting our ability to manage complex workflows and transactions that span multiple events and commands. This limitation leads to challenges in process coordination, maintaining consistency, and managing timeouts. By integrating SAGA capabilities, we can handle long-running transactions and complex workflows more effectively, reducing the risk of inconsistencies and failures, and improving the overall reliability of our system.

How to implement and why

To integrate the SAGA pattern, we will follow these steps:

  1. Create a Generic Saga Handler:

    • Develop an abstract class GenericSagaHandler that inherits from the ISagaHandler interface. This class will serve as the base for SAGA implementations, handling the initiation of a SAGA when an initial event or command is received, similar to how current events and commands trigger handlers.
    • Implementing SAGA classes will inherit from GenericSagaHandler and implement the ISagaData interface to manage SAGA-specific data and state.
  2. Implement Handling Methods:

    • For each StartedBy<T> message type the SAGA handles, implement a corresponding Handle method to process the initiation of the SAGA.
    • For each Handle<T> message type, implement a Handle method to process intermediate messages that influence the SAGA's state.
    • For each HandleTimeout<T>, implement a Timeout method to manage timeout scenarios within the SAGA lifecycle.
  3. Create a Saga Data Class:

    • Define a SAGA data class to store and manage the SAGA's state across multiple messages. This class will implement the ISagaData interface.

    • Example:

      public class SagaData : ISagaData
      {
          public Guid SagaId { get; set; }
          public string State { get; set; }
          public DateTime CreatedAt { get; set; }
          // Additional properties as needed
      }
    • This data class ensures that SAGA state is maintained consistently throughout the process.

  4. Manage Correlation and State:

    • Implement correlation logic to associate incoming events and commands with the appropriate SAGA instance using identifiers like SagaId.
    • Update the SAGA state within each handler method based on the business logic, ensuring consistency and correct progression of the workflow.
  5. Implement Persistence:

    • Develop a persistence mechanism for SAGA data to allow the state to be saved and retrieved across the application's lifetime.

    • The persistence layer can support SQL, NoSQL, or in-memory storage, depending on project requirements.

    • Example:

      public class SagaRepository
      {
          public void Save(SagaData sagaData)
          {
              // Implementation to save sagaData to the chosen storage
          }
      
          public SagaData Load(Guid sagaId)
          {
              // Implementation to load sagaData from storage using sagaId
          }
      }
    • Persisting SAGA data ensures resilience and continuity, especially in distributed systems or in case of failures.

  6. Implement Completion Logic:

    • Define conditions under which the SAGA should be completed, and implement logic to finalize and clean up the SAGA instance accordingly.
    • Proper completion handling prevents resource leaks and ensures that finished SAGA processes do not interfere with ongoing ones.
  7. Integrate Timeout Mechanism:

    • Implement a timeout mechanism to handle cases where a SAGA does not reach completion within an expected timeframe.
    • Define timeout policies and implement HandleTimeout<T> methods to manage expired SAGA processes, including rollback or compensating actions if necessary.
  8. Extend ISaga Interface:

    • Ensure that the SAGA implementations extend from the ISaga interface and utilize the ISagaData implementations for consistent integration with existing infrastructure.
    • This adherence promotes uniformity and makes the SAGA components align with other messaging patterns in the system.
  9. Testing:

    • Conduct thorough testing of the SAGA implementation across various scenarios, including:
      • Normal operation with successful completion.
      • Handling of intermediate failures and retries.
      • Timeout scenarios and proper execution of compensating actions.
    • Testing ensures reliability and robustness of the SAGA integration before deployment.

By following these steps, we can effectively integrate the SAGA pattern into our system, providing enhanced capabilities for managing complex workflows and improving overall system reliability.

About backward compatibility

The introduction of SAGA capabilities is designed to be backward compatible:

  • Non-Intrusive Additions: The new classes (GenericSagaHandler, SagaData, SagaRepository) and interfaces (ISagaHandler, ISagaData) are additions that do not modify existing classes or interfaces.
  • Optional Adoption: Existing command and event handlers remain unchanged and can continue to function without adopting the SAGA pattern.
  • Integration with Existing Infrastructure: By extending existing interfaces and following established patterns, the new SAGA components integrate smoothly without disrupting current functionalities.
  • Testing for Assurance: Comprehensive testing ensures that existing features are not adversely affected by the new additions.

This approach ensures that current implementations remain stable while providing the option to leverage the SAGA pattern for new or updated workflows as needed.

Test these changes locally

git checkout -b gitauto/issue-#330-eb1ceaa8-eb9d-4cc6-ab62-7bf36608b527
git pull origin gitauto/issue-#330-eb1ceaa8-eb9d-4cc6-ab62-7bf36608b527

@github-actions github-actions bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Oct 23, 2024
Copy link
Contributor

Infisical secrets check: ✅ No secrets leaked!

💻 Scan logs
12:12AM INF scanning for exposed secrets...
12:12AM INF 579 commits scanned.
12:12AM INF scan completed in 688ms
12:12AM INF no leaks found

Copy link

@github-advanced-security github-advanced-security bot left a comment

Choose a reason for hiding this comment

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

Sonarcsharp (reported by Codacy) found more than 20 potential problems in the proposed changes. Check the Files changed tab for more details.

@AppVeyorBot
Copy link

Build CrispyWaffle 8.2.220 failed (commit d1aac282a0 by @gitauto-ai[bot])

@gstraccini gstraccini bot added communications dependencies Pull requests that update a dependency file DI enhancement New feature or request events gitauto GitAuto label to trigger the app in a issue. good first issue Good for newcomers hacktoberfest Participation in the Hacktoberfest event help wanted Extra attention is needed IoC 🎲 database Database-related operations labels Oct 23, 2024
@gstraccini gstraccini bot requested a review from guibranco October 23, 2024 00:50
@gstraccini gstraccini bot added 🚦 awaiting triage Items that are awaiting triage or categorization 🤖 bot Automated processes or integrations labels Oct 23, 2024
@guibranco guibranco removed help wanted Extra attention is needed good first issue Good for newcomers labels Oct 23, 2024
@guibranco guibranco removed the hacktoberfest Participation in the Hacktoberfest event label Oct 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🚦 awaiting triage Items that are awaiting triage or categorization 🤖 bot Automated processes or integrations communications 🎲 database Database-related operations dependencies Pull requests that update a dependency file DI enhancement New feature or request events gitauto GitAuto label to trigger the app in a issue. IoC size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FEATURE] Add Saga capability
2 participants