-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Asif Sohail Mohammed <nsifmoh@amazon.com>
- Loading branch information
1 parent
4862ef3
commit 824eee3
Showing
11 changed files
with
395 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...r-plugins/s3-source/src/main/java/com/amazon/dataprepper/plugins/source/BackoffUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.amazon.dataprepper.plugins.source; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Random; | ||
|
||
public class BackoffUtils { | ||
private static final Logger LOG = LoggerFactory.getLogger(BackoffUtils.class); | ||
|
||
private int numberOfRetries; | ||
private long timeToWait; | ||
|
||
private final Random random = new Random(); | ||
|
||
public BackoffUtils(final int numberOfRetries, final long timeToWait) { | ||
this.numberOfRetries = numberOfRetries; | ||
this.timeToWait = timeToWait; | ||
} | ||
|
||
public boolean shouldRetry() { | ||
return numberOfRetries > 0; | ||
} | ||
|
||
public void errorOccurred() { | ||
numberOfRetries -= 1; | ||
|
||
if (shouldRetry()) { | ||
waitUntilNextTry(); | ||
timeToWait += random.nextInt(1000); | ||
} | ||
} | ||
|
||
private void waitUntilNextTry() { | ||
try { | ||
Thread.sleep(timeToWait); | ||
} catch (InterruptedException e) { | ||
LOG.error("Thread is interrupted.", e); | ||
} | ||
} | ||
|
||
public void doNotRetry() { | ||
numberOfRetries = 0; | ||
} | ||
|
||
public int getNumberOfTriesLeft() { | ||
return numberOfRetries; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...s3-source/src/main/java/com/amazon/dataprepper/plugins/source/S3ClientAuthentication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.amazon.dataprepper.plugins.source; | ||
|
||
import com.amazon.dataprepper.plugins.source.configuration.AwsAuthenticationOptions; | ||
import software.amazon.awssdk.regions.Region; | ||
import software.amazon.awssdk.services.s3.S3Client; | ||
import software.amazon.awssdk.services.sts.StsClient; | ||
|
||
public class S3ClientAuthentication { | ||
private S3SourceConfig s3SourceConfig; | ||
private AwsAuthenticationOptions awsAuthenticationOptions; | ||
|
||
public S3ClientAuthentication(final S3SourceConfig s3SourceConfig) { | ||
this.s3SourceConfig = s3SourceConfig; | ||
this.awsAuthenticationOptions = s3SourceConfig.getAWSAuthentication(); | ||
} | ||
|
||
public S3Client createS3Client(final StsClient stsClient) { | ||
|
||
return software.amazon.awssdk.services.s3.S3Client.builder() | ||
.region(Region.of(s3SourceConfig.getAWSAuthentication().getAwsRegion())) | ||
.credentialsProvider(awsAuthenticationOptions.authenticateAwsConfiguration(stsClient)) | ||
.build(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
...3-source/src/main/java/com/amazon/dataprepper/plugins/source/SqsClientAuthentication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.amazon.dataprepper.plugins.source; | ||
|
||
import com.amazon.dataprepper.plugins.source.configuration.AwsAuthenticationOptions; | ||
import software.amazon.awssdk.regions.Region; | ||
import software.amazon.awssdk.services.sqs.SqsClient; | ||
import software.amazon.awssdk.services.sts.StsClient; | ||
|
||
public class SqsClientAuthentication { | ||
private S3SourceConfig s3SourceConfig; | ||
private AwsAuthenticationOptions awsAuthenticationOptions; | ||
|
||
public SqsClientAuthentication(final S3SourceConfig s3SourceConfig) { | ||
this.s3SourceConfig = s3SourceConfig; | ||
awsAuthenticationOptions = s3SourceConfig.getAWSAuthentication(); | ||
} | ||
|
||
public SqsClient createSqsClient(final StsClient stsClient) { | ||
|
||
return SqsClient.builder() | ||
.credentialsProvider(awsAuthenticationOptions.authenticateAwsConfiguration(stsClient)) | ||
.region(Region.of(s3SourceConfig.getAWSAuthentication().getAwsRegion())) | ||
.build(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...ugins/s3-source/src/test/java/com/amazon/dataprepper/plugins/source/BackoffUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.amazon.dataprepper.plugins.source; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
class BackoffUtilsTest { | ||
private static final int NUMBER_OF_RETRIES = 5; | ||
private static final int TIME_TO_WAIT = 50; | ||
|
||
BackoffUtils backoffUtils; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
backoffUtils = new BackoffUtils(NUMBER_OF_RETRIES, TIME_TO_WAIT); | ||
} | ||
|
||
|
||
@Test | ||
void shouldRetry_should_return_true_for_value_greater_than_zero() { | ||
assertTrue(backoffUtils.shouldRetry()); | ||
} | ||
|
||
@Test | ||
void shouldRetry_should_return_false_for_zero() { | ||
backoffUtils = new BackoffUtils(0, 1000); | ||
assertFalse(backoffUtils.shouldRetry()); | ||
} | ||
|
||
@Test | ||
void errorOccurred_should_decrement_retires() { | ||
backoffUtils.errorOccurred(); | ||
assertThat(backoffUtils.getNumberOfTriesLeft(), equalTo(NUMBER_OF_RETRIES - 1)); | ||
} | ||
|
||
@Test | ||
void doNotRetry_should_set_retries_to_zero() { | ||
backoffUtils.doNotRetry(); | ||
assertThat(backoffUtils.getNumberOfTriesLeft(), equalTo(0)); | ||
} | ||
} |
Oops, something went wrong.