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

Add :ignore behavior for discovery events #3069

Closed
wants to merge 1 commit into from
Closed

Add :ignore behavior for discovery events #3069

wants to merge 1 commit into from

Conversation

balanza
Copy link
Member

@balanza balanza commented Oct 15, 2024

A received discovery event is discarded even if valid and correctly mapped to a command. This can happen if it does not make sense for the state of the SapSystem aggregate at that moment.

Namely, this happens with the RegisterApplicationInstance command. When the relative database instance has not been registered yet, the command cannot be processed.

This scenario is not considered an error anymore; instead, the command is just ignored and the publishing client receives a 202 Accepted response.


Despite being applied to discovery events, the behavior is available to any other command that's been enriched (example)

@@ -26,6 +26,11 @@ defmodule Trento.Infrastructure.Commanded.Middleware.Enrich do
pipeline
|> respond({:error, reason})
|> halt

{:ignore, reason} ->
Copy link
Member Author

Choose a reason for hiding this comment

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

question(naming): I'm open to alternative names for this: halt, skip, noop... ‏

@balanza balanza requested review from arbulu89, fabriziosestito and nelsonkopliku and removed request for fabriziosestito October 15, 2024 21:11
Copy link
Contributor

@arbulu89 arbulu89 left a comment

Choose a reason for hiding this comment

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

Hey @balanza ,
What do you think of the next option?
Simply return {:error, :database_not_registered} in the enrichment code, as before. The same goes for the discovery. And in the FallbackController, instead of returning 404, simply return 202. I mean, move the logic to the FallbackController in this scenario. I think, internally, for the enrichment protocol and discovery, this can continue being an error, and you decide how to handle this error somewhere else.

Otherwise, I see this new aggregate_dispatch_results really confusing an error prone. For example, ignored messages coming with error ones, are kind of ignored, and only the error message is logged.

@@ -36,7 +36,7 @@ defimpl Trento.Infrastructure.Commanded.Middleware.Enrichable,
}}

nil ->
{:error, :database_not_registered}
{:ignore, :database_not_registered}
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's add here, before returning, a concise message of what's happening, to give real undoubetable hint about what's their issue.
Something like:

Logger.error("database instance associated to application instance #{sid} not registered in Trento. Please make sure that Trento agent is running on database instances associated to this SAP system")

You can get the sid in the RegisterApplicationInstance command I guess

Copy link
Member Author

Choose a reason for hiding this comment

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

Should be an error or a warning?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, maybe an warning is more appropriate

defp aggregate_dispatch_results({:ignore, reasons}, {:error, reason}),
do: {:error, [reason | reasons]}

defp aggregate_dispatch_results({:error, reasons}, :ok), do: {:errors, reasons}
Copy link
Contributor

Choose a reason for hiding this comment

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

We should return {:error, reasons} in any case

@balanza
Copy link
Member Author

balanza commented Oct 16, 2024

@arbulu89 I'm trying to avoid the if :database_not_registered then thing flooding into the dispatcher logic. The introduction of :ignore serves to allow each event processing pipeline to decide whether is an error or not, depending on the case.

I see the implementation I'm proposing is maybe too convoluted.

As an alternative, I'm keen to just return 202 Accepted on every discarded event, as there is nothing the Agent can do with the error. But I see it can be a big stretch for now.

@arbulu89
Copy link
Contributor

I'm trying to avoid the if :database_not_registered then thing flooding into the dispatcher logic. The introduction of :ignore serves to allow each event processing pipeline to decide whether is an error or not, depending on the case.

What I propose doesn't even require to do almost any single change in the enrichment or discovery code.
You could replace the {:error, :database_not_registered} by {:error, :associated_database_not_found} in enrich_register_application_instance.ex, and in the fallback controller include this:

def call(conn, {:error, :associated_database_not_found}) do
    conn
      |> put_status(:accepted)
      |> json(%{})
end

It simply defines what type of return to use in the controller in case of we don't have the happy path

@nelsonkopliku
Copy link
Member

I don't mind the ignore semantics. However it is true that returning a different error, perhaps a more specific one, allowing us to catch it in the outer layer and act accordingly (the fallback controller in this case returning a 202) is simpler and requires less changes.

@balanza
Copy link
Member Author

balanza commented Oct 16, 2024

@nelsonkopliku @arbulu89 Thanks for the review, I buy your point and I'll go for a simpler solution. I open a new PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants