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 new Promisify Anything (AWS Athena edition) example #57

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

Conversation

pcholakov
Copy link
Contributor

@pcholakov pcholakov commented Dec 15, 2023

This example shows how to use Restate to promisify a complex dependency, in this case executing queries on AWS Athena.
A long-polling HTTP client is set up to simulate long-polling against the Restate ingress endpoint that wraps a query's
result in a very simple promise. Using a consistent idempotency token turns this into a true durable promise.

@pcholakov pcholakov marked this pull request as ready for review February 6, 2024 08:22
Comment on lines 13 to 29
const executionId = await ctx.sideEffect(async () => {
try {
const startQueryResult = await client.send(
new athena.StartQueryExecutionCommand({
QueryString: query ?? QUERY_SQL,
WorkGroup: "demo-workgroup",
ClientRequestToken: requestId,
}),
);
return startQueryResult.QueryExecutionId as string;
} catch (error) {
if (error instanceof athena.AthenaServiceException)
if (error.$fault === "client") throw new restate.TerminalError(error.message);

throw error; // rethrow other exceptions -- side effect retry policy will handle them
}
});
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@tillrohrmann WDYT about this alternative:

const startQueryOutput = await ctx.sideEffect(
  stopOnError(
    (e) => (e as { $fault?: string }).$fault === "client",
    async () =>
      await client.send(
        new athena.StartQueryExecutionCommand({
          QueryString: query ?? QUERY_SQL,
          WorkGroup: "demo-workgroup",
          ClientRequestToken: requestId,
        }),
      ),
  ),
);

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is a good improvement which makes things easier to understand :-)

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!

Comment on lines +28 to +30
const state = await ctx.sideEffect(async () =>
assertFinalState((await client.send(new athena.GetQueryExecutionCommand(queryId))).QueryExecution?.Status?.State),
);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@tillrohrmann what do you think about this - as an alternative to a for-loop or throwing inside the sideEffect body?

Copy link
Contributor

Choose a reason for hiding this comment

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

Generally looks good. What might not be so easy to understand is that we are using Restate's retry functionality to wait for the query to complete. Maybe adding something like ". Retrying." to the message of the Error in assertFinalState can make this clearer?

@pcholakov
Copy link
Contributor Author

pcholakov commented Feb 16, 2024 via email

@tillrohrmann tillrohrmann removed their request for review July 22, 2024 09:52
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.

2 participants