public NeptuneClusterMetadata refreshClusterMetadata()

in gremlin-client/src/main/java/software/amazon/neptune/cluster/GetEndpointsFromNeptuneManagementApi.java [97:209]


    public NeptuneClusterMetadata refreshClusterMetadata() {
        try {
            NeptuneClientBuilder builder = NeptuneClient.builder();

            if (clientConfiguration != null){
                builder = builder.overrideConfiguration(clientConfiguration);
            }
            if (httpClientBuilder != null) {
                builder = builder.httpClientBuilder(httpClientBuilder);
            }

            if (StringUtils.isNotEmpty(region)) {
                builder = builder.region(Region.of(region));
            }

            if (credentials != null) {
                builder = builder.credentialsProvider(credentials);
            } else if (!iamProfile.equals(IamAuthConfig.DEFAULT_PROFILE)) {
                builder = builder.credentialsProvider(ProfileCredentialsProvider.create(iamProfile));
            }

            NeptuneClient neptune = builder.build();

            DescribeDbClustersResponse describeDBClustersResult = neptune
                    .describeDBClusters(DescribeDbClustersRequest.builder().dbClusterIdentifier(clusterId).build());

            if (describeDBClustersResult.dbClusters().isEmpty()) {
                throw new IllegalStateException(String.format("Unable to find cluster %s", clusterId));
            }

            DBCluster dbCluster = describeDBClustersResult.dbClusters().get(0);

            String clusterEndpoint = dbCluster.endpoint();
            String readerEndpoint = dbCluster.readerEndpoint();

            List<DBClusterMember> dbClusterMembers = dbCluster.dbClusterMembers();
            Optional<DBClusterMember> clusterWriter = dbClusterMembers.stream()
                    .filter(DBClusterMember::isClusterWriter)
                    .findFirst();

            String primary = clusterWriter.map(DBClusterMember::dbInstanceIdentifier).orElse("");
            List<String> replicas = dbClusterMembers.stream()
                    .filter(dbClusterMember -> !dbClusterMember.isClusterWriter())
                    .map(DBClusterMember::dbInstanceIdentifier)
                    .collect(Collectors.toList());

            DescribeDbInstancesRequest describeDBInstancesRequest = DescribeDbInstancesRequest.builder()
                    .filters(
                            Collections.singletonList(
                                    Filter.builder()
                                            .name("db-cluster-id")
                                            .values(dbCluster.dbClusterIdentifier())
                                            .build()
                            )
                    )
                    .build();

            DescribeDbInstancesResponse describeDBInstancesResult = neptune
                    .describeDBInstances(describeDBInstancesRequest);

            Collection<NeptuneInstanceMetadata> instances = new ArrayList<>();
            describeDBInstancesResult.dbInstances()
                    .forEach(c -> {
                                String role = "unknown";
                                if (primary.equals(c.dbInstanceIdentifier())) {
                                    role = "writer";
                                }
                                if (replicas.contains(c.dbInstanceIdentifier())) {
                                    role = "reader";
                                }
                                String address = c.endpoint() == null ? null : c.endpoint().address();
                                Map<String, String> tags = getTags(c.dbInstanceArn(), neptune);
                                Map<String, String> annotations = getAnnotations(tags);
                                instances.add(
                                        new NeptuneInstanceMetadata()
                                                .withInstanceId(c.dbInstanceIdentifier())
                                                .withRole(role)
                                                .withAddress(address)
                                                .withStatus(c.dbInstanceStatus())
                                                .withAvailabilityZone(c.availabilityZone())
                                                .withInstanceType(c.dbInstanceClass())
                                                .withTags(tags)
                                                .withAnnotations(annotations));
                            }
                    );

            neptune.close();

            NeptuneClusterMetadata clusterMetadata = new NeptuneClusterMetadata()
                    .withInstances(instances)
                    .withClusterEndpoint(clusterEndpoint)
                    .withReaderEndpoint(readerEndpoint);

            cachedClusterMetadata.set(clusterMetadata);

            return clusterMetadata;

        } catch (NeptuneException e) {
            if (e.isThrottlingException()) {
                logger.warn("Calls to the Neptune Management API are being throttled. Reduce the refresh rate and stagger refresh agent requests, or use a NeptuneEndpointsInfoLambda proxy.");
                NeptuneClusterMetadata clusterMetadata = cachedClusterMetadata.get();
                if (clusterMetadata != null) {
                    logger.warn("Returning cached cluster metadata");
                    return clusterMetadata;
                } else {
                    throw e;
                }
            } else {
                throw e;
            }
        }

    }