-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathAuroraTestUtility.java
960 lines (857 loc) · 36.2 KB
/
AuroraTestUtility.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed 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 integration.util;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import integration.DatabaseEngine;
import integration.DriverHelper;
import integration.TestDatabaseInfo;
import integration.TestEnvironmentInfo;
import integration.TestInstanceInfo;
import integration.container.ConnectionStringHelper;
import integration.container.TestEnvironment;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.Properties;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.auth.credentials.AwsSessionCredentials;
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.core.waiters.WaiterResponse;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.ec2.Ec2Client;
import software.amazon.awssdk.services.ec2.model.DescribeSecurityGroupsResponse;
import software.amazon.awssdk.services.ec2.model.Ec2Exception;
import software.amazon.awssdk.services.rds.RdsClient;
import software.amazon.awssdk.services.rds.model.CreateDbClusterRequest;
import software.amazon.awssdk.services.rds.model.CreateDbInstanceRequest;
import software.amazon.awssdk.services.rds.model.DBCluster;
import software.amazon.awssdk.services.rds.model.DBClusterMember;
import software.amazon.awssdk.services.rds.model.DBEngineVersion;
import software.amazon.awssdk.services.rds.model.DBInstance;
import software.amazon.awssdk.services.rds.model.DbClusterNotFoundException;
import software.amazon.awssdk.services.rds.model.DeleteDbClusterResponse;
import software.amazon.awssdk.services.rds.model.DeleteDbInstanceRequest;
import software.amazon.awssdk.services.rds.model.DescribeDbClustersRequest;
import software.amazon.awssdk.services.rds.model.DescribeDbClustersResponse;
import software.amazon.awssdk.services.rds.model.DescribeDbEngineVersionsRequest;
import software.amazon.awssdk.services.rds.model.DescribeDbEngineVersionsResponse;
import software.amazon.awssdk.services.rds.model.DescribeDbInstancesResponse;
import software.amazon.awssdk.services.rds.model.FailoverDbClusterResponse;
import software.amazon.awssdk.services.rds.model.Filter;
import software.amazon.awssdk.services.rds.model.ModifyDbInstanceRequest;
import software.amazon.awssdk.services.rds.model.Tag;
import software.amazon.awssdk.services.rds.waiters.RdsWaiter;
import software.amazon.jdbc.util.StringUtils;
/**
* Creates and destroys AWS RDS Clusters and Instances. To use this functionality the following environment variables
* must be defined: - AWS_ACCESS_KEY_ID - AWS_SECRET_ACCESS_KEY
*/
public class AuroraTestUtility {
private static final Logger LOGGER = Logger.getLogger(AuroraTestUtility.class.getName());
// Default values
private String dbUsername = "my_test_username";
private String dbPassword = "my_test_password";
private String dbName = "test";
private String dbIdentifier = "test-identifier";
private String dbEngine = "aurora-postgresql";
private String dbEngineVersion = "13.9";
private String dbInstanceClass = "db.r5.large";
private final Region dbRegion;
private final String dbSecGroup = "default";
private int numOfInstances = 5;
private ArrayList<TestInstanceInfo> instances = new ArrayList<>();
private final RdsClient rdsClient;
private final Ec2Client ec2Client;
private static final Random rand = new Random();
private static final String DUPLICATE_IP_ERROR_CODE = "InvalidPermission.Duplicate";
/**
* Initializes an AmazonRDS & AmazonEC2 client. RDS client used to create/destroy clusters & instances. EC2 client
* used to add/remove IP from security group.
*/
public AuroraTestUtility() {
this(Region.US_EAST_1, DefaultCredentialsProvider.create());
}
/**
* Initializes an AmazonRDS & AmazonEC2 client.
*
* @param region define AWS Regions, refer to
* <a href="https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Concepts.RegionsAndAvailabilityZones.html">Regions, Availability Zones, and Local Zones</a>
*/
public AuroraTestUtility(Region region) {
this(region, DefaultCredentialsProvider.create());
}
/**
* Initializes an AmazonRDS & AmazonEC2 client.
*
* @param region define AWS Regions, refer to
* <a href="https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Concepts.RegionsAndAvailabilityZones.html">Regions, Availability Zones, and Local Zones</a>
*/
public AuroraTestUtility(String region) {
this(getRegionInternal(region), DefaultCredentialsProvider.create());
}
public AuroraTestUtility(
String region, String awsAccessKeyId, String awsSecretAccessKey, String awsSessionToken) {
this(
getRegionInternal(region),
StaticCredentialsProvider.create(
StringUtils.isNullOrEmpty(awsSessionToken)
? AwsBasicCredentials.create(awsAccessKeyId, awsSecretAccessKey)
: AwsSessionCredentials.create(awsAccessKeyId, awsSecretAccessKey, awsSessionToken)));
}
/**
* Initializes an AmazonRDS & AmazonEC2 client.
*
* @param region define AWS Regions, refer to
* <a href="https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Concepts.RegionsAndAvailabilityZones.html">Regions, Availability Zones, and Local Zones</a>
* @param credentialsProvider Specific AWS credential provider
*/
public AuroraTestUtility(Region region, AwsCredentialsProvider credentialsProvider) {
dbRegion = region;
rdsClient =
RdsClient.builder().region(dbRegion).credentialsProvider(credentialsProvider).build();
ec2Client =
Ec2Client.builder().region(dbRegion).credentialsProvider(credentialsProvider).build();
}
protected static Region getRegionInternal(String rdsRegion) {
Optional<Region> regionOptional =
Region.regions().stream().filter(r -> r.id().equalsIgnoreCase(rdsRegion)).findFirst();
if (regionOptional.isPresent()) {
return regionOptional.get();
}
throw new IllegalArgumentException(String.format("Unknown AWS region '%s'.", rdsRegion));
}
/**
* Creates RDS Cluster/Instances and waits until they are up, and proper IP whitelisting for databases.
*
* @param username Master username for access to database
* @param password Master password for access to database
* @param dbName Database name
* @param identifier Database cluster identifier
* @param engine Database engine to use, refer to
* https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Welcome.html
* @param instanceClass instance class, refer to
* https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Concepts.DBInstanceClass.html
* @param version the database engine's version
* @return An endpoint for one of the instances
* @throws InterruptedException when clusters have not started after 30 minutes
*/
public String createCluster(
String username,
String password,
String dbName,
String identifier,
String engine,
String instanceClass,
String version,
int numOfInstances,
ArrayList<TestInstanceInfo> instances)
throws InterruptedException {
dbUsername = username;
dbPassword = password;
this.dbName = dbName;
dbIdentifier = identifier;
dbEngine = engine;
dbInstanceClass = instanceClass;
dbEngineVersion = version;
this.numOfInstances = numOfInstances;
this.instances = instances;
return createCluster();
}
/**
* Creates RDS Cluster/Instances and waits until they are up, and proper IP whitelisting for databases.
*
* @return An endpoint for one of the instances
* @throws InterruptedException when clusters have not started after 30 minutes
*/
public String createCluster() throws InterruptedException {
// Create Cluster
final Tag testRunnerTag = Tag.builder().key("env").value("test-runner").build();
final CreateDbClusterRequest dbClusterRequest =
CreateDbClusterRequest.builder()
.dbClusterIdentifier(dbIdentifier)
.databaseName(dbName)
.masterUsername(dbUsername)
.masterUserPassword(dbPassword)
.sourceRegion(dbRegion.id())
.enableIAMDatabaseAuthentication(true)
.engine(dbEngine)
.engineVersion(dbEngineVersion)
.storageEncrypted(true)
.tags(testRunnerTag)
.build();
rdsClient.createDBCluster(dbClusterRequest);
// Create Instances
for (int i = 1; i <= numOfInstances; i++) {
final String instanceName = dbIdentifier + "-" + i;
rdsClient.createDBInstance(
CreateDbInstanceRequest.builder()
.dbClusterIdentifier(dbIdentifier)
.dbInstanceIdentifier(instanceName)
.dbInstanceClass(dbInstanceClass)
.engine(dbEngine)
.engineVersion(dbEngineVersion)
.publiclyAccessible(true)
.tags(testRunnerTag)
.build());
}
// Wait for all instances to be up
final RdsWaiter waiter = rdsClient.waiter();
WaiterResponse<DescribeDbInstancesResponse> waiterResponse =
waiter.waitUntilDBInstanceAvailable(
(requestBuilder) ->
requestBuilder.filters(
Filter.builder().name("db-cluster-id").values(dbIdentifier).build()),
(configurationBuilder) -> configurationBuilder.waitTimeout(Duration.ofMinutes(30)));
if (waiterResponse.matched().exception().isPresent()) {
deleteCluster();
throw new InterruptedException(
"Unable to start AWS RDS Cluster & Instances after waiting for 30 minutes");
}
final DescribeDbInstancesResponse dbInstancesResult =
rdsClient.describeDBInstances(
(builder) ->
builder.filters(
Filter.builder().name("db-cluster-id").values(dbIdentifier).build()));
final String endpoint = dbInstancesResult.dbInstances().get(0).endpoint().address();
final String clusterDomainPrefix = endpoint.substring(endpoint.indexOf('.') + 1);
for (DBInstance instance : dbInstancesResult.dbInstances()) {
this.instances.add(
new TestInstanceInfo(
instance.dbInstanceIdentifier(),
instance.endpoint().address(),
instance.endpoint().port()));
}
return clusterDomainPrefix;
}
/**
* Creates an RDS instance under the current cluster and waits until it is up.
*
* @param instanceId the desired instance ID of the new instance
* @return the instance info of the new instance
* @throws InterruptedException if the new instance is not available within 5 minutes
*/
public TestInstanceInfo createInstance(String instanceId) throws InterruptedException {
final Tag testRunnerTag = Tag.builder().key("env").value("test-runner").build();
final TestEnvironmentInfo info = TestEnvironment.getCurrent().getInfo();
rdsClient.createDBInstance(
CreateDbInstanceRequest.builder()
.dbClusterIdentifier(info.getAuroraClusterName())
.dbInstanceIdentifier(instanceId)
.dbInstanceClass(dbInstanceClass)
.engine(info.getDatabaseEngine())
.engineVersion(info.getDatabaseEngineVersion())
.publiclyAccessible(true)
.tags(testRunnerTag)
.build());
// Wait for the instance to become available
final RdsWaiter waiter = rdsClient.waiter();
WaiterResponse<DescribeDbInstancesResponse> waiterResponse =
waiter.waitUntilDBInstanceAvailable(
(requestBuilder) ->
requestBuilder.filters(
Filter.builder().name("db-instance-id").values(instanceId).build()),
(configurationBuilder) -> configurationBuilder.waitTimeout(Duration.ofMinutes(15)));
if (waiterResponse.matched().exception().isPresent()) {
throw new InterruptedException(
"Instance creation timeout for " + instanceId
+ ". The instance was not available within 5 minutes");
}
final DescribeDbInstancesResponse dbInstancesResult =
rdsClient.describeDBInstances(
(builder) ->
builder.filters(
Filter.builder().name("db-instance-id").values(instanceId).build()));
if (dbInstancesResult.dbInstances().size() != 1) {
throw new RuntimeException(
"The describeDBInstances request for newly created instance " + instanceId
+ " returned an unexpected number of instances: "
+ dbInstancesResult.dbInstances().size());
}
DBInstance instance = dbInstancesResult.dbInstances().get(0);
TestInstanceInfo instanceInfo = new TestInstanceInfo(
instance.dbInstanceIdentifier(),
instance.endpoint().address(),
instance.endpoint().port());
this.instances.add(instanceInfo);
return instanceInfo;
}
/**
* Deletes an RDS instance.
*
* @param instanceToDelete the info for the instance to delete
* @throws InterruptedException if the instance has not been deleted within 5 minutes
*/
public void deleteInstance(TestInstanceInfo instanceToDelete) throws InterruptedException {
rdsClient.deleteDBInstance(
DeleteDbInstanceRequest.builder()
.dbInstanceIdentifier(instanceToDelete.getInstanceId())
.skipFinalSnapshot(true)
.build());
this.instances.remove(instanceToDelete);
final RdsWaiter waiter = rdsClient.waiter();
WaiterResponse<DescribeDbInstancesResponse> waiterResponse = waiter.waitUntilDBInstanceDeleted(
(requestBuilder) -> requestBuilder.filters(
Filter.builder().name("db-instance-id").values(instanceToDelete.getInstanceId())
.build()),
(configurationBuilder) -> configurationBuilder.waitTimeout(Duration.ofMinutes(15)));
if (waiterResponse.matched().exception().isPresent()) {
throw new InterruptedException(
"Instance deletion timeout for " + instanceToDelete.getInstanceId()
+ ". The instance was not deleted within 5 minutes");
}
}
/**
* Gets public IP.
*
* @return public IP of user
* @throws UnknownHostException when checkip host isn't available
*/
public String getPublicIPAddress() throws UnknownHostException {
String ip;
try {
URL ipChecker = new URL("http://checkip.amazonaws.com");
BufferedReader reader = new BufferedReader(new InputStreamReader(ipChecker.openStream()));
ip = reader.readLine();
} catch (Exception e) {
throw new UnknownHostException("Unable to get IP");
}
return ip;
}
/**
* Authorizes IP to EC2 Security groups for RDS access.
*/
public void ec2AuthorizeIP(String ipAddress) {
if (StringUtils.isNullOrEmpty(ipAddress)) {
return;
}
if (ipExists(ipAddress)) {
return;
}
try {
ec2Client.authorizeSecurityGroupIngress(
(builder) ->
builder
.groupName(dbSecGroup)
.cidrIp(ipAddress + "/32")
.ipProtocol("-1") // All protocols
.fromPort(0) // For all ports
.toPort(65535));
} catch (Ec2Exception exception) {
if (!DUPLICATE_IP_ERROR_CODE.equalsIgnoreCase(exception.awsErrorDetails().errorCode())) {
throw exception;
}
}
}
private boolean ipExists(String ipAddress) {
final DescribeSecurityGroupsResponse response =
ec2Client.describeSecurityGroups(
(builder) ->
builder
.groupNames(dbSecGroup)
.filters(
software.amazon.awssdk.services.ec2.model.Filter.builder()
.name("ip-permission.cidr")
.values(ipAddress + "/32")
.build()));
return response != null && !response.securityGroups().isEmpty();
}
/**
* De-authorizes IP from EC2 Security groups.
*/
public void ec2DeauthorizesIP(String ipAddress) {
if (StringUtils.isNullOrEmpty(ipAddress)) {
return;
}
try {
ec2Client.revokeSecurityGroupIngress(
(builder) ->
builder
.groupName(dbSecGroup)
.cidrIp(ipAddress + "/32")
.ipProtocol("-1") // All protocols
.fromPort(0) // For all ports
.toPort(65535));
} catch (Ec2Exception exception) {
// Ignore
}
}
/**
* Destroys all instances and clusters. Removes IP from EC2 whitelist.
*
* @param identifier database identifier to delete
*/
public void deleteCluster(String identifier) {
dbIdentifier = identifier;
deleteCluster();
}
/**
* Destroys all instances and clusters. Removes IP from EC2 whitelist.
*/
public void deleteCluster() {
// Tear down instances
for (int i = 1; i <= numOfInstances; i++) {
try {
rdsClient.deleteDBInstance(
DeleteDbInstanceRequest.builder()
.dbInstanceIdentifier(dbIdentifier + "-" + i)
.skipFinalSnapshot(true)
.build());
} catch (Exception ex) {
LOGGER.finest("Error deleting instance " + dbIdentifier + "-" + i + ". " + ex.getMessage());
// Ignore this error and continue with other instances
}
}
// Tear down cluster
int remainingAttempts = 5;
while (--remainingAttempts > 0) {
try {
DeleteDbClusterResponse response = rdsClient.deleteDBCluster(
(builder -> builder.skipFinalSnapshot(true).dbClusterIdentifier(dbIdentifier)));
if (response.sdkHttpResponse().isSuccessful()) {
break;
}
TimeUnit.SECONDS.sleep(30);
} catch (DbClusterNotFoundException ex) {
// ignore
} catch (Exception ex) {
LOGGER.warning("Error deleting db cluster " + dbIdentifier + ": " + ex);
}
}
}
public boolean doesClusterExist(final String clusterId) {
final DescribeDbClustersRequest request =
DescribeDbClustersRequest.builder().dbClusterIdentifier(clusterId).build();
try {
rdsClient.describeDBClusters(request);
} catch (DbClusterNotFoundException ex) {
return false;
}
return true;
}
public DBCluster getClusterInfo(final String clusterId) {
final DescribeDbClustersRequest request =
DescribeDbClustersRequest.builder().dbClusterIdentifier(clusterId).build();
final DescribeDbClustersResponse response = rdsClient.describeDBClusters(request);
if (!response.hasDbClusters()) {
throw new RuntimeException("Cluster " + clusterId + " not found.");
}
return response.dbClusters().get(0);
}
public DatabaseEngine getClusterEngine(final DBCluster cluster) {
switch (cluster.engine()) {
case "aurora-postgresql":
return DatabaseEngine.PG;
case "aurora-mysql":
return DatabaseEngine.MYSQL;
default:
throw new UnsupportedOperationException(cluster.engine());
}
}
public List<TestInstanceInfo> getClusterInstanceIds(final String clusterId) {
final DescribeDbInstancesResponse dbInstancesResult =
rdsClient.describeDBInstances(
(builder) ->
builder.filters(Filter.builder().name("db-cluster-id").values(clusterId).build()));
List<TestInstanceInfo> result = new ArrayList<>();
for (DBInstance instance : dbInstancesResult.dbInstances()) {
result.add(
new TestInstanceInfo(
instance.dbInstanceIdentifier(),
instance.endpoint().address(),
instance.endpoint().port()));
}
return result;
}
public void waitUntilClusterHasRightState(String clusterId) throws InterruptedException {
String status = getDBCluster(clusterId).status();
while (!"available".equalsIgnoreCase(status)) {
TimeUnit.MILLISECONDS.sleep(1000);
status = getDBCluster(clusterId).status();
}
}
public DBCluster getDBCluster(String clusterId) {
final DescribeDbClustersResponse dbClustersResult =
rdsClient.describeDBClusters((builder) -> builder.dbClusterIdentifier(clusterId));
final List<DBCluster> dbClusterList = dbClustersResult.dbClusters();
return dbClusterList.get(0);
}
public List<String> getAuroraInstanceIds() throws SQLException {
return getAuroraInstanceIds(
TestEnvironment.getCurrent().getInfo().getRequest().getDatabaseEngine(),
ConnectionStringHelper.getUrl(),
TestEnvironment.getCurrent().getInfo().getDatabaseInfo().getUsername(),
TestEnvironment.getCurrent().getInfo().getDatabaseInfo().getPassword());
}
public List<String> getAuroraInstanceIds(
DatabaseEngine databaseEngine, String connectionUrl, String userName, String password)
throws SQLException {
String retrieveTopologySql;
switch (databaseEngine) {
case MYSQL:
retrieveTopologySql =
"SELECT SERVER_ID, SESSION_ID FROM information_schema.replica_host_status "
+ "ORDER BY IF(SESSION_ID = 'MASTER_SESSION_ID', 0, 1)";
break;
case PG:
retrieveTopologySql =
"SELECT SERVER_ID, SESSION_ID FROM aurora_replica_status() "
+ "ORDER BY CASE WHEN SESSION_ID = 'MASTER_SESSION_ID' THEN 0 ELSE 1 END";
break;
default:
throw new UnsupportedOperationException(databaseEngine.toString());
}
ArrayList<String> auroraInstances = new ArrayList<>();
try (final Connection conn = DriverManager.getConnection(connectionUrl, userName, password);
final Statement stmt = conn.createStatement();
final ResultSet resultSet = stmt.executeQuery(retrieveTopologySql)) {
while (resultSet.next()) {
// Get Instance endpoints
final String hostEndpoint = resultSet.getString("SERVER_ID");
auroraInstances.add(hostEndpoint);
}
}
return auroraInstances;
}
public Boolean isDBInstanceWriter(String instanceId) {
return isDBInstanceWriter(
TestEnvironment.getCurrent().getInfo().getAuroraClusterName(), instanceId);
}
public Boolean isDBInstanceWriter(String clusterId, String instanceId) {
return getMatchedDBClusterMember(clusterId, instanceId).isClusterWriter();
}
public DBClusterMember getMatchedDBClusterMember(String clusterId, String instanceId) {
final List<DBClusterMember> matchedMemberList =
getDBClusterMemberList(clusterId).stream()
.filter(dbClusterMember -> dbClusterMember.dbInstanceIdentifier().equals(instanceId))
.collect(Collectors.toList());
if (matchedMemberList.isEmpty()) {
throw new RuntimeException(
"Cannot find cluster member whose db instance identifier is " + instanceId);
}
return matchedMemberList.get(0);
}
public List<DBClusterMember> getDBClusterMemberList(String clusterId) {
final DBCluster dbCluster = getDBCluster(clusterId);
return dbCluster.dbClusterMembers();
}
public void makeSureInstancesUp(List<String> instances) throws InterruptedException {
makeSureInstancesUp(instances, true);
}
protected void makeSureInstancesUp(List<String> instances, boolean finalCheck)
throws InterruptedException {
final ExecutorService executorService = Executors.newFixedThreadPool(instances.size());
final ConcurrentHashMap<String, Boolean> remainingInstances = new ConcurrentHashMap<>();
instances.forEach((k) -> remainingInstances.put(k, true));
final Properties props = ConnectionStringHelper.getDefaultProperties();
DriverHelper.setConnectTimeout(props, 5, TimeUnit.SECONDS);
DriverHelper.setSocketTimeout(props, 5, TimeUnit.SECONDS);
for (final String id : instances) {
executorService.submit(
() -> {
while (true) {
TestInstanceInfo instanceInfo =
TestEnvironment.getCurrent().getInfo().getDatabaseInfo().getInstance(id);
try (final Connection conn =
DriverManager.getConnection(
ConnectionStringHelper.getUrlWithPlugins(
instanceInfo.getHost(),
instanceInfo.getPort(),
TestEnvironment.getCurrent()
.getInfo()
.getDatabaseInfo()
.getDefaultDbName(),
""),
props)) {
remainingInstances.remove(id);
break;
} catch (final SQLException ex) {
// Continue waiting until instance is up.
LOGGER.log(Level.FINEST, "Exception while trying to connect to instance " + id, ex);
} catch (final Exception ex) {
LOGGER.log(Level.SEVERE, "Exception:", ex);
break;
}
TimeUnit.MILLISECONDS.sleep(5000);
}
return null;
});
}
executorService.shutdown();
boolean isDone = executorService.awaitTermination(7, TimeUnit.MINUTES);
if (!isDone) {
LOGGER.finest("Some task are not completed. Shutting down them now.");
executorService.shutdownNow();
}
if (finalCheck) {
assertTrue(
remainingInstances.isEmpty(),
"The following instances are still down: \n"
+ String.join("\n", remainingInstances.keySet()));
}
}
// Attempt to run a query after the instance is down.
// This should initiate the driver failover, first query after a failover
// should always throw with the expected error message.
public void assertFirstQueryThrows(Connection connection, Class expectedSQLExceptionClass) {
assertThrows(
expectedSQLExceptionClass,
() ->
queryInstanceId(
TestEnvironment.getCurrent().getInfo().getRequest().getDatabaseEngine(),
connection));
}
public void failoverClusterAndWaitUntilWriterChanged() throws InterruptedException {
String clusterId = TestEnvironment.getCurrent().getInfo().getAuroraClusterName();
failoverClusterToATargetAndWaitUntilWriterChanged(
clusterId,
getDBClusterWriterInstanceId(clusterId),
getRandomDBClusterReaderInstanceId(clusterId));
}
public void failoverClusterToATargetAndWaitUntilWriterChanged(
String initialWriterId, String targetWriterId) throws InterruptedException {
failoverClusterToATargetAndWaitUntilWriterChanged(
TestEnvironment.getCurrent().getInfo().getAuroraClusterName(),
initialWriterId,
targetWriterId);
}
public void failoverClusterToATargetAndWaitUntilWriterChanged(
String clusterId, String initialWriterId, String targetWriterId)
throws InterruptedException {
LOGGER.finest(String.format("failover from %s to target: %s", initialWriterId, targetWriterId));
final TestDatabaseInfo dbInfo = TestEnvironment.getCurrent().getInfo().getDatabaseInfo();
final String clusterEndpoint = dbInfo.getClusterEndpoint();
failoverClusterToTarget(clusterId, targetWriterId);
int remainingAttempts = 5;
while (!hasWriterChanged(initialWriterId, TimeUnit.MINUTES.toNanos(5))) {
// if writer is not changed, try triggering failover again
remainingAttempts--;
if (remainingAttempts == 0) {
throw new RuntimeException("Failover cluster request was not successful.");
}
failoverClusterToTarget(clusterId, targetWriterId);
}
// Failover has finished, wait for DNS to be updated so cluster endpoint resolves to the correct writer instance.
String clusterIp = hostToIP(clusterEndpoint);
String targetWriterIp = hostToIP(dbInfo.getInstance(targetWriterId).getHost());
while (!clusterIp.equals(targetWriterIp)) {
TimeUnit.SECONDS.sleep(1);
clusterIp = hostToIP(clusterEndpoint);
targetWriterIp = hostToIP(dbInfo.getInstance(targetWriterId).getHost());
}
// Wait for target instance to be verified as a writer
while (!isDBInstanceWriter(targetWriterId)) {
TimeUnit.SECONDS.sleep(1);
}
LOGGER.finest(String.format("finished failover from %s to target: %s", initialWriterId, targetWriterId));
}
private boolean hasWriterChanged(String initialWriterId, long timeoutNanos)
throws InterruptedException {
final long waitUntil = System.nanoTime() + timeoutNanos;
String currentWriterId = getDBClusterWriterInstanceId();
while (initialWriterId.equals(currentWriterId)) {
if (waitUntil < System.nanoTime()) {
return false;
}
TimeUnit.MILLISECONDS.sleep(3000);
// Calling the RDS API to get writer Id.
currentWriterId = getDBClusterWriterInstanceId();
}
return true;
}
public void failoverClusterToTarget(String clusterId, String targetInstanceId)
throws InterruptedException {
waitUntilClusterHasRightState(clusterId);
int remainingAttempts = 10;
while (--remainingAttempts > 0) {
try {
FailoverDbClusterResponse response = rdsClient.failoverDBCluster(
(builder) ->
builder
.dbClusterIdentifier(clusterId)
.targetDBInstanceIdentifier(targetInstanceId));
if (!response.sdkHttpResponse().isSuccessful()) {
LOGGER.finest(String.format("failoverDBCluster response: %d, %s",
response.sdkHttpResponse().statusCode(),
response.sdkHttpResponse().statusText()));
} else {
return;
}
} catch (final Exception e) {
LOGGER.finest(String.format("failoverDBCluster request to %s failed: %s", targetInstanceId, e.getMessage()));
TimeUnit.MILLISECONDS.sleep(1000);
}
}
throw new RuntimeException("Failed to request a cluster failover.");
}
public String hostToIP(String hostname) {
try {
final InetAddress inet = InetAddress.getByName(hostname);
return inet.getHostAddress();
} catch (UnknownHostException e) {
fail("The IP address of host " + hostname + " could not be determined");
return null;
}
}
public String getRandomDBClusterReaderInstanceId(String clusterId) {
final List<DBClusterMember> matchedMemberList =
getDBClusterMemberList(clusterId).stream()
.filter(x -> !x.isClusterWriter())
.collect(Collectors.toList());
if (matchedMemberList.isEmpty()) {
fail("Failed to failover cluster - a reader instance could not be found.");
}
return matchedMemberList.get(rand.nextInt(matchedMemberList.size())).dbInstanceIdentifier();
}
public String getDBClusterWriterInstanceId() {
return getDBClusterWriterInstanceId(
TestEnvironment.getCurrent().getInfo().getAuroraClusterName());
}
public String getDBClusterWriterInstanceId(String clusterId) {
final List<DBClusterMember> matchedMemberList =
getDBClusterMemberList(clusterId).stream()
.filter(DBClusterMember::isClusterWriter)
.collect(Collectors.toList());
if (matchedMemberList.isEmpty()) {
throw new RuntimeException(clusterId);
}
// Should be only one writer at index 0.
return matchedMemberList.get(0).dbInstanceIdentifier();
}
protected String getInstanceIdSql(DatabaseEngine databaseEngine) {
switch (databaseEngine) {
case MYSQL:
return "SELECT @@aurora_server_id as id";
case PG:
return "SELECT aurora_db_instance_identifier()";
default:
throw new UnsupportedOperationException(databaseEngine.toString());
}
}
public String queryInstanceId(Connection connection) throws SQLException {
return queryInstanceId(
TestEnvironment.getCurrent().getInfo().getRequest().getDatabaseEngine(), connection);
}
public String queryInstanceId(DatabaseEngine databaseEngine, Connection connection)
throws SQLException {
try (final Statement stmt = connection.createStatement()) {
return executeInstanceIdQuery(databaseEngine, stmt);
}
}
public String executeInstanceIdQuery(DatabaseEngine databaseEngine, Statement stmt)
throws SQLException {
try (final ResultSet rs = stmt.executeQuery(getInstanceIdSql(databaseEngine))) {
if (rs.next()) {
return rs.getString(1);
}
}
return null;
}
public void createUser(Connection conn, String username, String password) throws SQLException {
DatabaseEngine engine = TestEnvironment.getCurrent().getInfo().getRequest().getDatabaseEngine();
String dropUserSql = getCreateUserSql(engine, username, password);
try (Statement stmt = conn.createStatement()) {
stmt.execute(dropUserSql);
}
}
protected String getCreateUserSql(DatabaseEngine engine, String username, String password) {
switch (engine) {
case MYSQL:
return "CREATE USER " + username + " identified by '" + password + "'";
case PG:
return "CREATE USER " + username + " with password '" + password + "'";
default:
throw new UnsupportedOperationException(engine.toString());
}
}
public void addAuroraAwsIamUser(
DatabaseEngine databaseEngine,
String connectionUrl,
String userName,
String password,
String dbUser,
String databaseName)
throws SQLException {
try (final Connection conn = DriverManager.getConnection(connectionUrl, userName, password);
final Statement stmt = conn.createStatement()) {
switch (databaseEngine) {
case MYSQL:
stmt.execute("DROP USER IF EXISTS " + dbUser + ";");
stmt.execute(
"CREATE USER " + dbUser + " IDENTIFIED WITH AWSAuthenticationPlugin AS 'RDS';");
stmt.execute("GRANT ALL PRIVILEGES ON " + databaseName + ".* TO '" + dbUser + "'@'%';");
break;
case PG:
stmt.execute("DROP USER IF EXISTS " + dbUser + ";");
stmt.execute("CREATE USER " + dbUser + ";");
stmt.execute("GRANT rds_iam TO " + dbUser + ";");
stmt.execute("GRANT ALL PRIVILEGES ON DATABASE " + databaseName + " TO " + dbUser + ";");
break;
default:
throw new UnsupportedOperationException(databaseEngine.toString());
}
}
}
public List<String> getEngineVersions(String engine) {
final List<String> res = new ArrayList<>();
final DescribeDbEngineVersionsResponse versions = rdsClient.describeDBEngineVersions(
DescribeDbEngineVersionsRequest.builder().engine(engine).build()
);
for (DBEngineVersion version : versions.dbEngineVersions()) {
res.add(version.engineVersion());
}
return res;
}
public String getLatestVersion(String engine) {
return getEngineVersions(engine)
.stream()
.sorted(Comparator.reverseOrder())
.findFirst()
.orElse(null);
}
public String getLTSVersion(String engine) {
final DescribeDbEngineVersionsResponse versions = rdsClient.describeDBEngineVersions(
DescribeDbEngineVersionsRequest.builder().defaultOnly(true).engine(engine).build()
);
if (!versions.dbEngineVersions().isEmpty()) {
return versions.dbEngineVersions().get(0).engineVersion();
}
throw new RuntimeException("Failed to find LTS version");
}
}