Skip to content

Commit eb84c15

Browse files
committed
Initial commit
0 parents  commit eb84c15

File tree

7 files changed

+130
-0
lines changed

7 files changed

+130
-0
lines changed

.github/workflows/ci.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: CI
2+
on:
3+
workflow_dispatch:
4+
pull_request:
5+
# Triggering CI on default branch improves caching
6+
# see https://docs.github.com/en/free-pro-team@latest/actions/guides/caching-dependencies-to-speed-up-workflows#restrictions-for-accessing-a-cache
7+
push:
8+
branches:
9+
- main
10+
11+
jobs:
12+
test:
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@v3
18+
- name: Setup JDK
19+
uses: actions/setup-java@v3
20+
with:
21+
distribution: corretto
22+
java-version: 17
23+
cache: sbt
24+
- name: Find minimum memory required for the code to pass
25+
run: ./findMinMem.sh

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
target/
2+
3+
.bsp/
4+
5+
### IntelliJ IDEA ###
6+
.idea/
7+
8+
### Mac OS ###
9+
.DS_Store

build.sbt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Compile / run / fork := true
2+
3+
// run / javaOptions += "-Xmx10m"
4+
5+
libraryDependencies ++= Seq(
6+
"software.amazon.awssdk" % "s3" % "2.20.120",
7+
"software.amazon.awssdk" % "aws-crt-client" % "2.20.130",
8+
"org.slf4j" % "slf4j-simple" % "2.0.7",
9+
"com.google.guava" % "guava" % "32.1.2-jre"
10+
)
11+
12+
commands += Command.single("runWithMaxMem") { (state, arg) =>
13+
s"""set run / javaOptions += "-Xmx${arg}m"""" :: "run" :: state
14+
}
15+
16+
commands += Command.single("repeatRunWithMaxMem") { (state, arg) =>
17+
List.fill(20)(s"runWithMaxMem $arg") ::: state
18+
}

findMinMem.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
3+
lo=1
4+
hi=1500
5+
while [ "$((hi - lo))" -gt 1 ]; do
6+
mid=$((lo + (( hi - lo ) / 2)))
7+
echo "Memory $mid MB"
8+
9+
if sbt "repeatRunWithMaxMem $mid";
10+
then hi=$mid;
11+
else lo=$mid; fi
12+
done
13+
echo "Failed with $lo MB, Succeeded with $hi MB"
14+
15+
if [ -n "$GITHUB_STEP_SUMMARY" ]; then
16+
echo "### Required $hi MB" >> "$GITHUB_STEP_SUMMARY"
17+
fi

project/build.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sbt.version=1.9.3
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.madgag.aws.sdk.async.responsebytes;
2+
3+
import com.google.common.hash.HashCode;
4+
5+
public record KnownS3Object(String bucket, String key, HashCode sha256) {
6+
public static KnownS3Object BIG = new KnownS3Object(
7+
"sagemaker-sample-files", "datasets/tabular/customer-churn/customer-churn-data.zip",
8+
HashCode.fromString("49e94915ca0de3368a0f715c088a38596c84f64a65f77d66c79c648f356c8008")
9+
);
10+
11+
public static KnownS3Object SMALL = new KnownS3Object(
12+
"sagemaker-sample-files", "datasets/tabular/xgb-churn/test-dataset.csv",
13+
HashCode.fromString("1f395e2e95f746482989c9c943eabe0d9f6c69b505a05e067f6ae87e967aa0fc")
14+
);
15+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.madgag.aws.sdk.async.responsebytes;
2+
3+
import com.google.common.hash.HashCode;
4+
import com.google.common.hash.Hashing;
5+
import software.amazon.awssdk.auth.credentials.AnonymousCredentialsProvider;
6+
import software.amazon.awssdk.core.ResponseBytes;
7+
import software.amazon.awssdk.core.async.AsyncResponseTransformer;
8+
import software.amazon.awssdk.http.crt.AwsCrtAsyncHttpClient;
9+
import software.amazon.awssdk.services.s3.S3AsyncClient;
10+
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
11+
import software.amazon.awssdk.services.s3.model.GetObjectResponse;
12+
13+
import java.util.concurrent.ExecutionException;
14+
15+
import static com.google.common.base.Verify.verify;
16+
import static java.time.Duration.ofSeconds;
17+
import static software.amazon.awssdk.regions.Region.US_EAST_1;
18+
19+
public class Main {
20+
public static void main(String[] args) throws ExecutionException, InterruptedException {
21+
try (S3AsyncClient s3Client = buildS3Client()) {
22+
System.out.println("About to fetch file...");
23+
KnownS3Object knownS3Object = KnownS3Object.BIG;
24+
ResponseBytes<GetObjectResponse> responseBytes = s3Client.getObject(
25+
GetObjectRequest.builder().bucket(knownS3Object.bucket()).key(knownS3Object.key()).build(),
26+
AsyncResponseTransformer.toBytes()
27+
).get();
28+
System.out.println(responseBytes.response().contentLength());
29+
HashCode hashOfDownloadedData = Hashing.sha256().hashBytes(responseBytes.asByteBuffer());
30+
verify(hashOfDownloadedData.equals(knownS3Object.sha256()),
31+
"Downloaded hash (%s) doesn't match expected hash (%s)", hashOfDownloadedData, knownS3Object.sha256());
32+
}
33+
}
34+
35+
private static S3AsyncClient buildS3Client() {
36+
return S3AsyncClient.builder()
37+
.region(US_EAST_1)
38+
.credentialsProvider(AnonymousCredentialsProvider.create())
39+
.httpClientBuilder(AwsCrtAsyncHttpClient
40+
.builder()
41+
.connectionTimeout(ofSeconds(3))
42+
.maxConcurrency(100)).build();
43+
}
44+
45+
}

0 commit comments

Comments
 (0)