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 waiter for Athena QueryExecutionFinished #2626

Closed
wants to merge 1 commit into from

Conversation

steven-aerts
Copy link
Contributor

Implements aws/aws-sdk#80 and aws/aws-sdk-java#1646 for the aws-java-sdk-v2.

Motivation and Context

It makes it a lot easier to query athena from the sdk. It does this by implementing a highly requested feature for boto and java v1 sdk.
This PR works and is submitted, I am however wondering if this is the correct place to do so. As the files under codegen-resources seem to come from an (internal?) repository I cannot find back. As these files seem to be shared somehow with other language bindings for AWS.

Let me know if PR's like this have to be submitted in any other way, then I will do accordingly. I think it would be cool if this could be submitted and made available to other language bindings too and close aws/aws-sdk#80 for all of them.

Description

Testing

Screenshots (if appropriate)

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

Checklist

  • I have read the CONTRIBUTING document
  • Local run of mvn install succeeds
  • My code follows the code style of this project
  • My change requires a change to the Javadoc documentation
  • I have updated the Javadoc documentation accordingly
  • I have read the README document
  • I have added tests to cover my changes
  • All new and existing tests passed
  • A short description of the change has been added to the CHANGELOG
  • My change is to implement 1.11 parity feature and I have updated LaunchChangelog

License

  • I confirm that this pull request can be released under the Apache 2 license

@drissamri
Copy link

This patch would help us clean up a lot of boilerplate code on our side. Would be nice to see this get merged soon

@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@debora-ito
Copy link
Member

Hi @steven-aerts thank you for the contribution but we cannot merge it unfortunately.

Waiter models are owned and maintained by the service teams. The models must be provided upstream so that all the SDKs can make use of them. I'll ping the Athena team again to ask for an update.

@debora-ito debora-ito closed this Jul 28, 2021
@zoewangg
Copy link
Contributor

Hi @steven-aerts, @drissamri, in case you are not aware, you can use the Waiter (public API) class directly with the polling functions while we are waiting on the service model change.

https://github.com/aws/aws-sdk-java-v2/blob/master/core/sdk-core/src/main/java/software/amazon/awssdk/core/waiters/Waiter.java#L29

@drissamri
Copy link

drissamri commented Jul 29, 2021

I'm unable to find any examples of using Waiters directly, do you have any tips on the implementation below? Do I need to use the errorOnException acceptor instead of resp.matched().exception().ifPresent?

private void waitForQueryToComplete(String queryExecutionId, long timeOutInMillis) {
    GetQueryExecutionRequest getQueryExecutionRequest = GetQueryExecutionRequest.builder()
            .queryExecutionId(queryExecutionId)
            .build();
            
    Waiter<GetQueryExecutionResponse> waiter = Waiter.builder(GetQueryExecutionResponse.class)
            .overrideConfiguration(cfg -> cfg
                    .waitTimeout(Duration.ofMillis(timeOutInMillis)))
            .addAcceptor(successOnResponseAcceptor(resp -> SUCCEEDED == resp.queryExecution().status().state()))
            .addAcceptor(errorOnResponseAcceptor(resp -> FAILED == resp.queryExecution().status().state()))
            .addAcceptor(errorOnResponseAcceptor(resp -> CANCELLED == resp.queryExecution().status().state()))
            .addAcceptor(retryOnResponseAcceptor(resp -> true))
            .build();
            
    WaiterResponse<GetQueryExecutionResponse> resp = waiter.run(() -> athenaClient.getQueryExecution(getQueryExecutionRequest));
    resp.matched().response().ifPresent(e -> LOG.info("Success query"));
    resp.matched().exception().ifPresent(ex -> {
        throw new PublicServiceProblem(SERVICE_MALFUNCTION, ex.getMessage()).exception();
    });
}

@zoewangg
Copy link
Contributor

@drissamri you can find the sample code in other service waiter classes such as DefaultS3Waiter (you can build the s3 module using mvn clean install -pl :s3 -P quick --am)

If an exception is thrown from the operation and is not matched with any acceptors, the request will fail. https://github.com/aws/aws-sdk-java-v2/blob/master/core/sdk-core/src/main/java/software/amazon/awssdk/core/internal/waiters/WaiterExecutor.java#L83, so you don't have to add an errorOnException acceptor.

resp.matched().exception() actually means a matched exception has transitioned the waiter to the desired state. Some operations expects error response to be returned from service. Taking DynamoDbWaiter#waitUntilTableNotExists as an example, it will only succeed if ResourceNotFoundException is thrown. If the operation finishes succesfully, resp.matched().exception() will return the ResourceNotFoundException and resp.matched().response() will be empty.

For athenaClient.getQueryExecution, because it expects a successful service response with SUCCEED state, resp.matched().exception() will always be optional.empty. An exception will be thrown if the waiter operation fails, so if you want to handle the exceptions, you probably need to add try-catch for waiter.run

Hope this helps. Please let me know if you have further questions.

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.

Add waiters for Athena
4 participants