Skip to content

Commit c433b6e

Browse files
java: refactor SR cluster management
- Deletes MR examples; this will be added back once SDKs are available with updated APIs - Combines all SR actions into a single test case. This'll let CI run faster because we don't have to create a cluster for each operation. - Adds a sweeper that deletes leaked clusters. Only runs in CI. - Remove unused and duplicate dependencies. - Readme corresponds to this code instead of pgJDBC ;)
1 parent aa08d7c commit c433b6e

12 files changed

Lines changed: 241 additions & 477 deletions

File tree

.github/workflows/java-cm-integ-tests.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ jobs:
5252

5353
- name: Configure and run integration for cluster management
5454
working-directory: ./java/cluster_management
55+
env:
56+
IS_CI: "true"
5557
run: |
5658
mvn validate
5759
mvn initialize

java/cluster_management/README.md

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,50 @@
22

33
## Overview
44

5-
The code examples in this topic show you how to use the Java PgJDBC to work with Aurora DSQL.
5+
The code examples in this topic show you how to use the AWS Java SDK v2 with DSQL
6+
to create, update, get, and delete single- and multi-Region clusters.
7+
8+
Each file in the [/example](src/main/java/org/example) directory demonstrates a minimum
9+
working example for each operation. The `example()` method for each operation is invoked
10+
in [`DsqlClusterManagementTest.java`](src/test/java/org/example/DsqlClusterManagementTest.java).
611

712
## Run the examples
813

14+
### ⚠️ Important
15+
16+
* Running this code might result in charges to your AWS account.
17+
* We recommend that you grant your code least privilege. At most, grant only the
18+
minimum permissions required to perform the task. For more information, see
19+
[Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege).
20+
* This code is not tested in every AWS Region. For more information, see
21+
[AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services).
22+
923
### Prerequisites
1024

11-
- java version >= 17 is needed
25+
- java version >= 17 is installed.
26+
- Valid AWS credentials can be discovered by the [default provider chain](https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/credentials-chain.html).
27+
28+
### Execute tests to create and delete clusters
1229

13-
### Run the example tests
30+
See [`DsqlClusterManagementTest.java`](src/test/java/org/example/DsqlClusterManagementTest.java) for configuration
31+
used in the `setup()` function.
1432

1533
```sh
16-
# Use the account credentials dedicated for javascript
17-
export CLUSTER_ENDPOINT="<your cluster endpoint>"
18-
export REGION="<your cluster region>"
34+
# Optional: Single-Region examples will execute in REGION_1. Defaults to 'us-east-1'.
35+
export REGION_1="us-east-1"
36+
37+
# Optional: Multi-Region examples will create clusters in REGION_1 and REGION_2
38+
# with WITNESS_REGION as witness for both. Defaults to 'us-east-2' for REGION_2
39+
# and 'us-west-2' for WITNESS_REGION.
40+
export REGION_2="us-east-2"
41+
export WITNESS_REGION="us-west-2"
42+
43+
# Will create, update, read, then delete clusters
1944
mvn test
2045
```
2146

47+
Test execution will take around five minutes as it waits for clusters to complete activation and deletion.
48+
2249
---
2350

2451
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.

java/cluster_management/pom.xml

Lines changed: 14 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@
99
<version>1.0-SNAPSHOT</version>
1010

1111
<properties>
12-
<maven.compiler.source>1.8</maven.compiler.source>
13-
<maven.compiler.target>1.8</maven.compiler.target>
12+
<maven.compiler.release>17</maven.compiler.release>
1413
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1514
<mainClass>org.example.Example</mainClass>
16-
<project.build.awssdk.version>2.29.27</project.build.awssdk.version>
15+
<project.build.awssdk.version>2.31.41</project.build.awssdk.version>
1716
</properties>
1817

1918
<dependencies>
@@ -26,21 +25,6 @@
2625
<dependency>
2726
<groupId>software.amazon.awssdk</groupId>
2827
<artifactId>dsql</artifactId>
29-
<version>2.29.27</version>
30-
</dependency>
31-
<dependency>
32-
<groupId>software.amazon.awssdk</groupId>
33-
<artifactId>regions</artifactId>
34-
<version>${project.build.awssdk.version}</version>
35-
</dependency>
36-
<dependency>
37-
<groupId>software.amazon.awssdk</groupId>
38-
<artifactId>aws-core</artifactId>
39-
<version>${project.build.awssdk.version}</version>
40-
</dependency>
41-
<dependency>
42-
<groupId>software.amazon.awssdk</groupId>
43-
<artifactId>aws-json-protocol</artifactId>
4428
<version>${project.build.awssdk.version}</version>
4529
</dependency>
4630
<dependency>
@@ -49,6 +33,12 @@
4933
<version>${project.build.awssdk.version}</version>
5034
<scope>compile</scope>
5135
</dependency>
36+
<dependency>
37+
<groupId>org.slf4j</groupId>
38+
<artifactId>slf4j-nop</artifactId>
39+
<version>2.0.17</version>
40+
<scope>runtime</scope>
41+
</dependency>
5242
</dependencies>
5343

5444
<build>
@@ -57,6 +47,11 @@
5747
<groupId>org.apache.maven.plugins</groupId>
5848
<artifactId>maven-surefire-plugin</artifactId>
5949
<version>3.0.0-M5</version>
50+
<configuration>
51+
<includes>
52+
<include>**/*Test.java</include>
53+
</includes>
54+
</configuration>
6055
</plugin>
6156
<plugin>
6257
<groupId>org.apache.maven.plugins</groupId>
@@ -83,43 +78,19 @@
8378
</arguments>
8479
</configuration>
8580
</plugin>
86-
<plugin>
87-
<groupId>org.apache.maven.plugins</groupId>
88-
<artifactId>maven-jar-plugin</artifactId>
89-
<version>3.1.0</version>
90-
<configuration>
91-
<archive>
92-
<manifest>
93-
<addClasspath>true</addClasspath>
94-
<mainClass>org.example.Example</mainClass>
95-
</manifest>
96-
</archive>
97-
</configuration>
98-
</plugin>
99-
<!-- Add the assemble plugin with standard configuration -->
10081
<plugin>
10182
<artifactId>maven-assembly-plugin</artifactId>
10283
<configuration>
10384
<archive>
10485
<manifest>
105-
<mainClass>org.example.Example</mainClass>
86+
<mainClass>${mainClass}</mainClass>
10687
</manifest>
10788
</archive>
10889
<descriptorRefs>
10990
<descriptorRef>jar-with-dependencies</descriptorRef>
11091
</descriptorRefs>
11192
</configuration>
11293
</plugin>
113-
<plugin>
114-
<groupId>org.apache.maven.plugins</groupId>
115-
<artifactId>maven-surefire-plugin</artifactId>
116-
<version>3.0.0-M5</version>
117-
<configuration>
118-
<includes>
119-
<include>**/*Test.java</include>
120-
</includes>
121-
</configuration>
122-
</plugin>
12394
</plugins>
12495
</build>
12596
</project>

java/cluster_management/src/main/java/org/example/ConnectionUtil.java

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,54 @@
11
package org.example;
22

33
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
4-
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
5-
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
64
import software.amazon.awssdk.regions.Region;
7-
import software.amazon.awssdk.retries.StandardRetryStrategy;
5+
import software.amazon.awssdk.retries.api.BackoffStrategy;
86
import software.amazon.awssdk.services.dsql.DsqlClient;
9-
import software.amazon.awssdk.services.dsql.model.ClusterStatus;
107
import software.amazon.awssdk.services.dsql.model.CreateClusterRequest;
118
import software.amazon.awssdk.services.dsql.model.CreateClusterResponse;
9+
import software.amazon.awssdk.services.dsql.model.GetClusterResponse;
1210

13-
import java.util.HashMap;
11+
import java.time.Duration;
1412
import java.util.Map;
1513

1614
public class CreateCluster {
1715

18-
public static void main(String[] args) throws Exception {
16+
public static void main(String[] args) {
1917
Region region = Region.US_EAST_1;
2018

21-
ClientOverrideConfiguration clientOverrideConfiguration = ClientOverrideConfiguration.builder()
22-
.retryStrategy(StandardRetryStrategy.builder().build())
23-
.build();
24-
25-
DsqlClient client = DsqlClient.builder()
26-
.httpClient(UrlConnectionHttpClient.create())
27-
.overrideConfiguration(clientOverrideConfiguration)
28-
.region(region)
29-
.credentialsProvider(DefaultCredentialsProvider.create())
30-
.build();
31-
32-
boolean deletionProtectionEnabled = true;
33-
Map<String, String> tags = new HashMap<>();
34-
tags.put("Name", "FooBar");
35-
36-
String identifier = createCluster(client, deletionProtectionEnabled, tags);
37-
System.out.println("Cluster Id: " + identifier);
19+
try (
20+
DsqlClient client = DsqlClient.builder()
21+
.region(region)
22+
.credentialsProvider(DefaultCredentialsProvider.create())
23+
.build()
24+
) {
25+
GetClusterResponse cluster = example(client);
26+
System.out.println("Created " + cluster);
27+
}
3828
}
3929

40-
public static String createCluster(DsqlClient client, boolean deletionProtectionEnabled, Map<String, String> tags) throws Exception {
41-
CreateClusterRequest createClusterRequest = CreateClusterRequest
42-
.builder()
43-
.deletionProtectionEnabled(deletionProtectionEnabled)
44-
.tags(tags)
30+
public static GetClusterResponse example(DsqlClient client) {
31+
CreateClusterRequest request = CreateClusterRequest.builder()
32+
.deletionProtectionEnabled(true)
33+
.tags(Map.of(
34+
"Name", "java single region cluster",
35+
"Repo", "aws-samples/aurora-dsql-samples"
36+
))
4537
.build();
46-
CreateClusterResponse res = client.createCluster(createClusterRequest);
47-
if (res.status() == ClusterStatus.CREATING) {
48-
return res.identifier();
49-
} else {
50-
throw new Exception("Failed to create cluster");
51-
}
38+
CreateClusterResponse cluster = client.createCluster(request);
39+
System.out.println("Created " + cluster.arn());
40+
41+
// The DSQL SDK offers a built-in waiter to poll for a cluster's
42+
// transition to ACTIVE.
43+
System.out.println("Waiting for cluster to become ACTIVE");
44+
GetClusterResponse activeCluster = client.waiter().waitUntilClusterActive(
45+
getCluster -> getCluster.identifier(cluster.identifier()),
46+
config -> config.backoffStrategyV2(
47+
BackoffStrategy.fixedDelayWithoutJitter(Duration.ofSeconds(10))
48+
).waitTimeout(Duration.ofMinutes(5))
49+
).matched().response().orElseThrow();
50+
System.out.println("Cluster is ACTIVE: " + activeCluster);
51+
52+
return activeCluster;
5253
}
5354
}

java/cluster_management/src/main/java/org/example/CreateMultiRegionCluster.java

Lines changed: 0 additions & 68 deletions
This file was deleted.

0 commit comments

Comments
 (0)