@Override public Cluster getEmrClusterById(String clusterId, AwsParamsDto awsParams) { Cluster cluster = null; if (StringUtils.isNotBlank(clusterId)) { DescribeClusterResult describeClusterResult = emrOperations.describeClusterRequest(getEmrClient(awsParams), new DescribeClusterRequest().withClusterId(clusterId)); if (describeClusterResult != null && describeClusterResult.getCluster() != null) { cluster = describeClusterResult.getCluster(); } } return cluster; }
@Test public void getEmrClusterByIdAssertReturnNullWhenDescribeClusterResponseClusterIsNull() throws Exception { String clusterId = "clusterId"; when(mockEmrOperations.describeClusterRequest(any(), any())).thenReturn(new DescribeClusterResult()); assertNull(emrDao.getEmrClusterById(clusterId, new AwsParamsDto())); }
/** * Helper method to determine if an Amazon EMR cluster exists * * @param client * The {@link AmazonElasticMapReduceClient} with read permissions * @param clusterIdentifier * The Amazon EMR cluster to check * @return true if the Amazon EMR cluster exists, otherwise false */ public static boolean clusterExists(AmazonElasticMapReduce client, String clusterIdentifier) { if (clusterIdentifier != null && !clusterIdentifier.isEmpty()) { ListClustersResult clustersList = client.listClusters(); ListIterator<ClusterSummary> iterator = clustersList.getClusters().listIterator(); ClusterSummary summary; for (summary = iterator.next() ; iterator.hasNext();summary = iterator.next()) { if (summary.getId().equals(clusterIdentifier)) { DescribeClusterRequest describeClusterRequest = new DescribeClusterRequest().withClusterId(clusterIdentifier); DescribeClusterResult result = client.describeCluster(describeClusterRequest); if (result != null) { Cluster cluster = result.getCluster(); //check if HBase is installed on this cluster if (isHBaseInstalled(client, cluster.getId())) return false; String state = cluster.getStatus().getState(); LOG.info(clusterIdentifier + " is " + state + ". "); if (state.equalsIgnoreCase("RUNNING") ||state.equalsIgnoreCase("WAITING")) { LOG.info("The cluster with id " + clusterIdentifier + " exists and is " + state); return true; } } } } } LOG.info("The cluster with id " + clusterIdentifier + " does not exist"); return false; }
private Optional<String> checkClusterBootStatus(AmazonElasticMapReduce emr, NewCluster cluster, TaskState state) { // Only creating a cluster, with no steps? boolean createOnly = cluster.steps() == 0; DescribeClusterResult describeClusterResult = pollingRetryExecutor(state, "describe-cluster") .withRetryInterval(DurationInterval.of(Duration.ofSeconds(30), Duration.ofMinutes(5))) .retryUnless(AmazonServiceException.class, Aws::isDeterministicException) .run(ds -> emr.describeCluster(new DescribeClusterRequest().withClusterId(cluster.id()))); ClusterStatus clusterStatus = describeClusterResult.getCluster().getStatus(); String clusterState = clusterStatus.getState(); switch (clusterState) { case "STARTING": logger.info("EMR cluster starting: {}", cluster.id()); return Optional.absent(); case "BOOTSTRAPPING": logger.info("EMR cluster bootstrapping: {}", cluster.id()); return Optional.absent(); case "RUNNING": case "WAITING": logger.info("EMR cluster up: {}", cluster.id()); return Optional.of(clusterState); case "TERMINATED_WITH_ERRORS": if (createOnly) { // TODO: log more information about the errors // TODO: inspect state change reason to figure out whether it was the boot that failed or e.g. steps submitted by another agent throw new TaskExecutionException("EMR boot failed: " + cluster.id()); } return Optional.of(clusterState); case "TERMINATING": if (createOnly) { // Keep waiting for the final state // TODO: inspect state change reason and bail early here return Optional.absent(); } return Optional.of(clusterState); case "TERMINATED": return Optional.of(clusterState); default: throw new RuntimeException("Unknown EMR cluster state: " + clusterState); } }
/** * Helper method to determine the Amazon EMR cluster state * * @param client * The {@link AmazonElasticMapReduceClient} with read permissions * @param clusterIdentifier * The Amazon EMR cluster to get the state of - e.g. j-2A98VJHDSU48M * @return The String representation of the Amazon EMR cluster state */ public static String clusterState(AmazonElasticMapReduce client, String clusterIdentifier) { DescribeClusterRequest describeClusterRequest = new DescribeClusterRequest().withClusterId(clusterIdentifier); DescribeClusterResult result= client.describeCluster(describeClusterRequest); if (result != null) { return result.getCluster().getStatus().getState(); } return null; }