jonchase commented on a change in pull request #3891: URL: https://github.com/apache/camel/pull/3891#discussion_r435863948
########## File path: components/camel-aws2-athena/src/main/java/org/apache/camel/component/aws2/athena/Athena2QueryHelper.java ########## @@ -0,0 +1,452 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.aws2.athena; + +import org.apache.camel.Exchange; +import org.apache.camel.util.ObjectHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.time.Clock; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import software.amazon.awssdk.services.athena.model.GetQueryExecutionResponse; +import software.amazon.awssdk.services.athena.model.QueryExecutionState; + + +/** + * Package-private class to encapsulate the logic of running queries, waiting for completion states, retrying, etc. + */ +class Athena2QueryHelper { + private static final Logger LOG = LoggerFactory.getLogger(Athena2QueryHelper.class); + + // configuration ====================== + private final Clock clock = Clock.systemUTC(); + private final long waitTimeout; + private final long delay; + private final Set<String> retry; + private final int maxAttempts; + private final boolean resetWaitTimeoutOnAttempt; + private final long absoluteStartMs; + + // state ============================== + private long currentDelay; + private int attempts; + private boolean isFailure; + private boolean isSuccess; + private boolean isRetry; + private long startMs; + private boolean interrupted; + + Athena2QueryHelper(Exchange exchange, Athena2Configuration configuration) { + this.waitTimeout = determineWaitTimeout(exchange, configuration); + this.delay = determineDelay(exchange, configuration); + this.maxAttempts = determineMaxAttempts(exchange, configuration); + this.retry = determineRetry(exchange, configuration); + this.resetWaitTimeoutOnAttempt = determineResetWaitTimeoutOnRetry(exchange, configuration); + this.absoluteStartMs = now(); + + this.currentDelay = determineInitialDelay(exchange, configuration); + } + + private long now() { + return clock.millis(); + } + + long getElapsedMillis() { + return now() - this.absoluteStartMs; + } + + /** + * Record that a query attempt was made. This is relevant b/c only so many attempts are permitted. + */ + void markAttempt() { + if (attempts == 0) { + this.startMs = now(); + } else { + if (resetWaitTimeoutOnAttempt) { + this.startMs = now(); + } + } + ++attempts; + + this.isFailure = false; + this.isSuccess = false; + this.isRetry = false; + } + + int getAttempts() { + return this.attempts; + } + + /** + * Should another query attempt be made? + */ + boolean shouldAttempt() { + if (this.attempts >= this.maxAttempts) { + LOG.trace("AWS Athena start query execution used all {} attempts", this.maxAttempts); + return false; + } + + if (this.interrupted) { + LOG.trace("AWS Athena start query execution thread was interrupted, will try no more"); + return false; + } + + if (this.isFailure) { + LOG.trace("AWS Athena start query execution detected permanent failure"); + return false; + } + + if (this.isSuccess) { + LOG.trace("AWS Athena start query execution detected success, will try no more"); + return false; + } + + // if this.isRetry, return true + + return true; + } + + /** + * Should there be a wait for the query to complete? + */ + boolean shouldWait() { + long now = now(); + long millisWaited = now - this.startMs; + if (millisWaited >= this.waitTimeout) { + LOG + .trace("AWS Athena start query execution waited for {}, which exceeded wait timeout of {}", millisWaited, + this.waitTimeout); + return false; + } + + if (this.interrupted) { + LOG.trace("AWS Athena start query execution thread was interrupted, will wait no longer"); + return false; + } + + if (this.isFailure) { + LOG.trace("AWS Athena start query execution detected failure, will wait no longer"); + return false; + } + + if (this.isSuccess) { + LOG.trace("AWS Athena start query execution detected success, will wait no longer"); + return false; + } + + if (this.isRetry) { + LOG.trace("AWS Athena start query execution detected retry, will immediately attempt retry"); + return false; + } + + return true; + } + + void doWait() { + try { + Thread.sleep(this.currentDelay); Review comment: Is this the 'right' way of doing this in a Camel component? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org