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(sfn): handle pagination and ignore errors for expired executions history #1934

Conversation

pdecat
Copy link
Contributor

@pdecat pdecat commented Oct 8, 2023

This PR updates the aws_sfn_state_machine_execution_history table to implement pagination and ignore errors for expired executions history, which are only preserved for a maximum of 90 days.

Without this latter change, one would get errors like this one:

Error: operation error SFN: GetExecutionHistory, https response error StatusCode: 400, RequestID: 68dacbfc-af53-42f5-985e-a019c51b679f, ExecutionDoesNotExist: Execution Does Not Exist: 'arn:aw
s:states:eu-west-3:012345678901:execution:sfn-test-012345678901:b1b58243-1276-4195-863a-e83bed3dd603' (SQLSTATE HV000)

Integration test logs

Logs
Add passing integration test logs here

Example query results

Results
Add example SQL query results here (please include the input queries as well)

Copy link
Contributor

@cbruno10 cbruno10 left a comment

Choose a reason for hiding this comment

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

@pdecat Thanks for opening this PR! Can you please see my comments/questions?

var apiErr smithy.APIError
if errors.As(err, &apiErr) {
switch apiErr.(type) {
case *types.ExecutionDoesNotExist:
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a known error code for this error type? If so, could we add it to an IgnoreConfig in the ListConfig, similar to

IgnoreConfig: &plugin.IgnoreConfig{
ShouldIgnoreErrorFunc: shouldIgnoreErrors([]string{"InvalidInstanceID.NotFound", "InvalidInstanceID.Unavailable", "InvalidInstanceID.Malformed"}),
},
(this example is in a GetConfig, but ListConfig supports adding the same thing)?

The ShouldIgnoreErrorFunc in the config has a similar effect of returning nil if we encounter from the function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've tried this change:

diff --git a/aws/table_aws_sfn_state_machine_execution_history.go b/aws/table_aws_sfn_state_machine_execution_history.go
index c6ae08ce..9384b73a 100644
--- a/aws/table_aws_sfn_state_machine_execution_history.go
+++ b/aws/table_aws_sfn_state_machine_execution_history.go
@@ -2,7 +2,6 @@ package aws

 import (
        "context"
-       "errors"
        "strconv"
        "strings"
        "sync"
@@ -10,7 +9,6 @@ import (
        "github.com/aws/aws-sdk-go-v2/aws"
        "github.com/aws/aws-sdk-go-v2/service/sfn"
        "github.com/aws/aws-sdk-go-v2/service/sfn/types"
-       "github.com/aws/smithy-go"

        sfnv1 "github.com/aws/aws-sdk-go/service/sfn"

@@ -27,6 +25,9 @@ func tableAwsStepFunctionsStateMachineExecutionHistory(_ context.Context) *plugi
                        Hydrate:       listStepFunctionsStateMachineExecutionHistories,
                        Tags:          map[string]string{"service": "states", "action": "ListExecutions"},
                        ParentHydrate: listStepFunctionsStateMachines,
+                       IgnoreConfig: &plugin.IgnoreConfig{
+                               ShouldIgnoreErrorFunc: shouldIgnoreErrors([]string{"ExecutionDoesNotExist"}),
+                       },
                },
                GetMatrixItemFunc: SupportedRegionMatrix(sfnv1.EndpointsID),
                Columns: awsRegionalColumns([]*plugin.Column{
@@ -373,15 +374,6 @@ func getRowDataForExecutionHistory(ctx context.Context, d *plugin.QueryData, arn
                plugin.Logger(ctx).Trace("aws_sfn_state_machine_execution_history.getRowDataForExecutionHistory", "api_call GetExecutionHistory", arn)
                output, err := paginator.NextPage(ctx)
                if err != nil {
-                       var apiErr smithy.APIError
-                       if errors.As(err, &apiErr) {
-                               switch apiErr.(type) {
-                               case *types.ExecutionDoesNotExist:
-                                       // Ignore expired executions for which history is no longer available
-                                       plugin.Logger(ctx).Trace("aws_sfn_state_machine_execution_history.getRowDataForExecutionHistory", "api_error ignore_expired", err)
-                                       return nil, nil
-                               }
-                       }
                        plugin.Logger(ctx).Error("aws_sfn_state_machine_execution_history.getRowDataForExecutionHistory", "api_error", err)
                        return nil, err
                }

But it does not seem to work:

> with constants (r, s_arn, e_arn) as (
    values (
            'eu-west-3',
            'arn:aws:states:eu-west-3:012345678901:stateMachine:mysfn',
            'arn:aws:states:eu-west-3:012345678901:execution:mysfn:%'
        )
)
select id,
    execution_arn,
    execution_failed_event_details
from aws_sfn_state_machine_execution_history,
    constants
where region = r
    and execution_arn in (
        select execution_arn
        from aws_sfn_state_machine_execution,
            constants
        where region = r
            and state_machine_arn = s_arn
            and execution_arn like e_arn
    )
    and execution_failed_event_details is null;

Error: operation error SFN: GetExecutionHistory, https response error StatusCode: 400, RequestID: 0ce49d57-9f15-4636-8398-8ddaf01af24b, ExecutionDoesNotExist: Execution Does Not Exist: 'arn:aws:states:eu-west-3:012345678901:execution:mysfn:a4fdeb5a-0f8f-4bee-b36b-2ae036665817' (SQLSTATE HV000)

+----+---------------+--------------------------------+
| id | execution_arn | execution_failed_event_details |
+----+---------------+--------------------------------+
+----+---------------+--------------------------------+

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks @pdecat for testing (sorry for the long response time!), I think this is due to a bug where IgnoreConfig doesn't work when a ParentHydrate is used (turbot/steampipe-plugin-sdk#544), so can you please add a comment mentioning why we need to handle errors here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

}

var items []historyInfo

listHistory, err := svc.GetExecutionHistory(ctx, params)
Copy link
Contributor

Choose a reason for hiding this comment

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

Does the AWS SDK offer any pagination with svc.GetExecutionHistory? Or was this table never iterating through pages before?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This table did not implement the required pagination handling before: https://docs.aws.amazon.com/sdk-for-go/api/service/sfn/#SFN.GetExecutionHistory

@madhushreeray30 madhushreeray30 added the hacktoberfest-accepted This pull request has been accepted for Hacktoberfest label Oct 31, 2023
Copy link

This PR is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the stale No recent activity has been detected on this issue/PR and it will be closed label Dec 30, 2023
@pdecat
Copy link
Contributor Author

pdecat commented Dec 30, 2023

Not stale

@e-gineer e-gineer removed the stale No recent activity has been detected on this issue/PR and it will be closed label Dec 30, 2023
Signed-off-by: Patrick Decat <pdecat@gmail.com>
@pdecat pdecat force-pushed the fix/aws_sfn_state_machine_execution_history_expired branch from 675386e to 4b0d5cb Compare February 1, 2024 08:09
@misraved misraved merged commit be8bfe3 into turbot:main Feb 8, 2024
@pdecat pdecat deleted the fix/aws_sfn_state_machine_execution_history_expired branch February 8, 2024 07:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hacktoberfest-accepted This pull request has been accepted for Hacktoberfest
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants