mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(cloudformation): provision EMR steps, instance groups and instance fleets (#9159)
A template can now create AWS::EMR::Step, AWS::EMR::InstanceGroupConfig and AWS::EMR::InstanceFleetConfig. None was served, so the stack reported CREATE_COMPLETE with nothing behind it. EmrCfnProvisioner adds them through AddJobFlowSteps, AddInstanceGroups and AddInstanceFleet, reading each as those calls read their requests, from methods EmrHandler now shares, and against the registry schemas, now in the vendored property types. - A step's Ref and Id are its id, and its LogUri and EncryptionKeyArn are its StepMonitoringConfiguration. Every property is create-only, so a change adds a new step, and a step stays in its cluster's history when deleted, as the schema's delete handler calls nothing. - An instance group's Ref and InstanceGroupId are its id. Its InstanceCount and AutoScalingPolicy change in place, and the delete removes its policy and shrinks it to nothing, as a group cannot be removed from its cluster. - An instance fleet's Ref and InstanceFleetId are its id. Its target capacities change in place through ModifyInstanceFleet, and a change to InstanceTypeConfigs or ResizeSpecifications is accepted with a warning and not applied, as Floci's ModifyInstanceFleet changes the capacities alone. The delete shrinks it to nothing. - A create-only change adds a new group or fleet; a later resource's failure, or a failure of the update itself, puts back what it had changed; and a group or fleet whose cluster has ended is left alone. A group's role and a fleet's type must be TASK, the one value the template reference allows, and a delete shrinks one once its cluster is running or waiting, waiting for one still starting, and removes a policy the group has.
This commit is contained in:
1 parent
2f99c0ae8c
commit
5db8d098e7
11 files changed
+1112
-21
No files matched your search
+140
@@ -0,0 +1,140 @@
|
||||
package io.floci.aws.services.cloudformation;
|
||||
|
||||
import io.floci.aws.testing.CfnStackWaits;
|
||||
import io.floci.aws.testing.CfnTestStack;
|
||||
import io.floci.aws.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
import software.amazon.awssdk.services.cloudformation.model.Stack;
|
||||
|
||||
import static io.floci.aws.testing.CfnStackWaits.output;
|
||||
import static io.floci.aws.testing.EmrRequests.emr;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasItem;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* Provisions {@code AWS::EMR::Step}, {@code AWS::EMR::InstanceGroupConfig} and {@code AWS::EMR::InstanceFleetConfig}
|
||||
* on clusters of the same stack and reads them back through EMR: each {@code Ref} and {@code Fn::GetAtt} names its
|
||||
* step, group or fleet, an update resizes the group and the fleet in place, a later resource's failure puts the sizes
|
||||
* back, and deleting the stack shrinks them with their clusters.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class EmrStepGroupFleetCfnIntegrationTest {
|
||||
|
||||
@RegisterExtension
|
||||
final CfnTestStack stack = new CfnTestStack("emr-step-group-fleet-cfn-it");
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@Test
|
||||
void addResizeRollBackAndDeleteAStepAnInstanceGroupAndAnInstanceFleet() {
|
||||
Stack created = stack.create(template(1, 1, false));
|
||||
String groups = output(created, "Groups");
|
||||
String fleets = output(created, "Fleets");
|
||||
String stepId = output(created, "StepRef");
|
||||
String groupId = output(created, "GroupRef");
|
||||
String fleetId = output(created, "FleetRef");
|
||||
assertEquals(stepId, output(created, "StepId"));
|
||||
assertEquals(groupId, output(created, "GroupId"));
|
||||
assertEquals(fleetId, output(created, "FleetId"));
|
||||
emr("ListSteps", "{\"ClusterId\": \"" + groups + "\"}").then().statusCode(200)
|
||||
.body("Steps.Id", hasItem(stepId));
|
||||
groupCount(groups, groupId, 1);
|
||||
fleetCapacity(fleets, fleetId, 1);
|
||||
|
||||
Stack resized = stack.update(template(2, 2, false), "UPDATE_COMPLETE");
|
||||
assertEquals(groupId, output(resized, "GroupRef"));
|
||||
assertEquals(fleetId, output(resized, "FleetRef"));
|
||||
groupCount(groups, groupId, 2);
|
||||
fleetCapacity(fleets, fleetId, 2);
|
||||
|
||||
stack.update(template(3, 3, true), "UPDATE_ROLLBACK_COMPLETE");
|
||||
groupCount(groups, groupId, 2);
|
||||
fleetCapacity(fleets, fleetId, 2);
|
||||
|
||||
CfnStackWaits.deleteStack(stack.name());
|
||||
|
||||
emr("DescribeCluster", "{\"ClusterId\": \"" + groups + "\"}").then().statusCode(200)
|
||||
.body("Cluster.Status.State", equalTo("TERMINATED"));
|
||||
emr("DescribeCluster", "{\"ClusterId\": \"" + fleets + "\"}").then().statusCode(200)
|
||||
.body("Cluster.Status.State", equalTo("TERMINATED"));
|
||||
}
|
||||
|
||||
private static String template(int taskCount, int fleetCapacity, boolean laterFails) {
|
||||
return """
|
||||
{
|
||||
"Resources": {
|
||||
"Groups": {
|
||||
"Type": "AWS::EMR::Cluster",
|
||||
"Properties": {
|
||||
"Name": "groups", "ReleaseLabel": "emr-7.5.0", "JobFlowRole": "EMR_EC2_DefaultRole",
|
||||
"ServiceRole": "EMR_DefaultRole",
|
||||
"Instances": {"KeepJobFlowAliveWhenNoSteps": true,
|
||||
"MasterInstanceGroup": {"InstanceCount": 1, "InstanceType": "m5.xlarge"},
|
||||
"CoreInstanceGroup": {"InstanceCount": 1, "InstanceType": "m5.xlarge"}}
|
||||
}
|
||||
},
|
||||
"Fleets": {
|
||||
"Type": "AWS::EMR::Cluster",
|
||||
"Properties": {
|
||||
"Name": "fleets", "ReleaseLabel": "emr-7.5.0", "JobFlowRole": "EMR_EC2_DefaultRole",
|
||||
"ServiceRole": "EMR_DefaultRole",
|
||||
"Instances": {"KeepJobFlowAliveWhenNoSteps": true,
|
||||
"MasterInstanceFleet": {"TargetOnDemandCapacity": 1,
|
||||
"InstanceTypeConfigs": [{"InstanceType": "m5.xlarge"}]},
|
||||
"CoreInstanceFleet": {"TargetOnDemandCapacity": 1,
|
||||
"InstanceTypeConfigs": [{"InstanceType": "m5.xlarge"}]}}
|
||||
}
|
||||
},
|
||||
"Step": {
|
||||
"Type": "AWS::EMR::Step",
|
||||
"Properties": {
|
||||
"JobFlowId": {"Ref": "Groups"}, "Name": "pi", "ActionOnFailure": "CONTINUE",
|
||||
"HadoopJarStep": {"Jar": "command-runner.jar", "Args": ["spark-example", "SparkPi"]}
|
||||
}
|
||||
},
|
||||
"Group": {
|
||||
"Type": "AWS::EMR::InstanceGroupConfig",
|
||||
"Properties": {
|
||||
"JobFlowId": {"Ref": "Groups"}, "InstanceRole": "TASK", "InstanceType": "m5.xlarge",
|
||||
"InstanceCount": %d, "Name": "spot"
|
||||
}
|
||||
},
|
||||
"Fleet": {
|
||||
"Type": "AWS::EMR::InstanceFleetConfig",
|
||||
"Properties": {
|
||||
"ClusterId": {"Ref": "Fleets"}, "InstanceFleetType": "TASK", "Name": "tasks",
|
||||
"TargetOnDemandCapacity": %d, "InstanceTypeConfigs": [{"InstanceType": "m5.xlarge"}]
|
||||
}
|
||||
}%s
|
||||
},
|
||||
"Outputs": {
|
||||
"Groups": {"Value": {"Ref": "Groups"}},
|
||||
"Fleets": {"Value": {"Ref": "Fleets"}},
|
||||
"StepRef": {"Value": {"Ref": "Step"}},
|
||||
"StepId": {"Value": {"Fn::GetAtt": ["Step", "Id"]}},
|
||||
"GroupRef": {"Value": {"Ref": "Group"}},
|
||||
"GroupId": {"Value": {"Fn::GetAtt": ["Group", "InstanceGroupId"]}},
|
||||
"FleetRef": {"Value": {"Ref": "Fleet"}},
|
||||
"FleetId": {"Value": {"Fn::GetAtt": ["Fleet", "InstanceFleetId"]}}
|
||||
}
|
||||
}
|
||||
""".formatted(taskCount, fleetCapacity, laterFails ? CfnStackWaits.failingResourceAfter("Fleet") : "");
|
||||
}
|
||||
|
||||
private static void groupCount(String clusterId, String groupId, int count) {
|
||||
emr("ListInstanceGroups", "{\"ClusterId\": \"" + clusterId + "\"}").then().statusCode(200)
|
||||
.body("InstanceGroups.find { it.Id == '" + groupId + "' }.RequestedInstanceCount", equalTo(count));
|
||||
}
|
||||
|
||||
private static void fleetCapacity(String clusterId, String fleetId, int capacity) {
|
||||
emr("ListInstanceFleets", "{\"ClusterId\": \"" + clusterId + "\"}").then().statusCode(200)
|
||||
.body("InstanceFleets.find { it.Id == '" + fleetId + "' }.TargetOnDemandCapacity", equalTo(capacity));
|
||||
}
|
||||
}
|
||||
+153
-1
@@ -8,6 +8,7 @@ import io.floci.aws.services.emr.EmrHandler;
|
||||
import io.floci.aws.services.emr.EmrService;
|
||||
import io.floci.aws.services.emr.EmrService.InstanceGroupModification;
|
||||
import io.floci.aws.services.emr.model.EmrCluster;
|
||||
import io.floci.aws.services.emr.model.EmrInstanceFleet;
|
||||
import io.floci.aws.services.emr.model.EmrInstanceGroup;
|
||||
import io.floci.aws.services.emr.model.SecurityConfiguration;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -33,7 +34,7 @@ import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/** {@code AWS::EMR::SecurityConfiguration} and {@code AWS::EMR::Cluster} in isolation, with EMR mocked. */
|
||||
/** Amazon EMR's own CloudFormation types over {@link EmrService} in isolation, with EMR mocked. */
|
||||
class EmrCfnProvisionerTest {
|
||||
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
@@ -329,4 +330,155 @@ class EmrCfnProvisionerTest {
|
||||
|
||||
verify(emr).terminateJobFlows(List.of(CLUSTER_ID));
|
||||
}
|
||||
|
||||
private static StackResource typed(String type, String physicalId) {
|
||||
StackResource r = resource(physicalId);
|
||||
r.setLogicalId("Child");
|
||||
r.setResourceType(type);
|
||||
return r;
|
||||
}
|
||||
|
||||
private static final String POLICY = """
|
||||
{"Constraints": {"MinCapacity": 1, "MaxCapacity": 4}, "Rules": []}""";
|
||||
|
||||
private static String groupProps(int count, String policy) {
|
||||
return """
|
||||
{"JobFlowId": "%s", "InstanceRole": "TASK", "InstanceType": "m5.xlarge", "InstanceCount": %d,
|
||||
"Name": "spot", "Configurations": [{"Classification": "yarn-site",
|
||||
"ConfigurationProperties": {"yarn.nodemanager.vmem-check-enabled": "false"}}]%s}
|
||||
""".formatted(CLUSTER_ID, count, policy == null ? "" : ", \"AutoScalingPolicy\": " + policy);
|
||||
}
|
||||
|
||||
private static String fleetProps(int onDemand) {
|
||||
return """
|
||||
{"ClusterId": "%s", "InstanceFleetType": "TASK", "Name": "tasks", "TargetOnDemandCapacity": %d,
|
||||
"InstanceTypeConfigs": [{"InstanceType": "m5.xlarge"}]}
|
||||
""".formatted(CLUSTER_ID, onDemand);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aStepIsAddedWithItsPropertiesAndMonitoringInAddJobFlowStepsMembersAndStaysWhenDeleted() {
|
||||
when(emr.addJobFlowSteps(eq(CLUSTER_ID), any())).thenReturn(List.of("s-STEP1"));
|
||||
StackResource r = typed("AWS::EMR::Step", null);
|
||||
|
||||
provisioner.provision(r, json("""
|
||||
{"JobFlowId": "%s", "Name": "pi", "ActionOnFailure": "CONTINUE", "LogUri": "s3://logs/steps/",
|
||||
"HadoopJarStep": {"Jar": "command-runner.jar", "Args": ["spark-example", "SparkPi"],
|
||||
"StepProperties": [{"Key": "k", "Value": "v"}]}}
|
||||
""".formatted(CLUSTER_ID)), ctx(null, null));
|
||||
provisioner.delete(r, "us-east-1");
|
||||
|
||||
ArgumentCaptor<JsonNode> steps = ArgumentCaptor.forClass(JsonNode.class);
|
||||
verify(handler).stepsFrom(steps.capture(), eq(null));
|
||||
JsonNode step = steps.getValue().get(0);
|
||||
assertEquals(json("[{\"Key\": \"k\", \"Value\": \"v\"}]"), step.path("HadoopJarStep").get("Properties"));
|
||||
assertEquals("s3://logs/steps/",
|
||||
step.path("StepMonitoringConfiguration").path("S3MonitoringConfiguration").path("LogUri").asText());
|
||||
assertEquals("s-STEP1", r.getPhysicalId());
|
||||
assertEquals("s-STEP1", r.getAttributes().get("Id"));
|
||||
verify(emr, never()).cancelSteps(any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anInstanceGroupIsAddedThenResizedAndGivenAPolicyInPlaceAndARollbackPutsThemBack() {
|
||||
when(emr.addInstanceGroups(eq(CLUSTER_ID), any())).thenReturn(List.of("ig-TASK"));
|
||||
StackResource r = typed("AWS::EMR::InstanceGroupConfig", null);
|
||||
provisioner.provision(r, json(groupProps(1, null)), ctx(null, null));
|
||||
ArgumentCaptor<JsonNode> groups = ArgumentCaptor.forClass(JsonNode.class);
|
||||
verify(handler).instanceGroupsFrom(groups.capture());
|
||||
assertEquals("TASK", groups.getValue().get(0).path("InstanceRole").asText());
|
||||
assertEquals("false", groups.getValue().get(0).path("Configurations").get(0).path("Properties")
|
||||
.path("yarn.nodemanager.vmem-check-enabled").asText());
|
||||
assertEquals("ig-TASK", r.getAttributes().get("InstanceGroupId"));
|
||||
|
||||
when(emr.describeCluster(CLUSTER_ID)).thenReturn(emrCluster(CLUSTER_ID, "WAITING"));
|
||||
EmrInstanceGroup live = group("ig-TASK", "TASK", "spot");
|
||||
live.setRequestedInstanceCount(1);
|
||||
when(emr.listInstanceGroups(CLUSTER_ID)).thenReturn(List.of(live));
|
||||
provisioner.provision(r, json(groupProps(3, POLICY)), ctx("ig-TASK", groupProps(1, null)));
|
||||
provisioner.rollbackUpdate(r);
|
||||
|
||||
verify(emr).modifyInstanceGroups(CLUSTER_ID, "us-east-1",
|
||||
List.of(new InstanceGroupModification("ig-TASK", 3, null)));
|
||||
verify(emr).putAutoScalingPolicy(CLUSTER_ID, "ig-TASK", json(POLICY));
|
||||
verify(emr).modifyInstanceGroups(CLUSTER_ID, "us-east-1",
|
||||
List.of(new InstanceGroupModification("ig-TASK", 1, null)));
|
||||
verify(emr).removeAutoScalingPolicy(CLUSTER_ID, "ig-TASK");
|
||||
assertEquals("ig-TASK", r.getPhysicalId());
|
||||
// The policy the group has again is the one the next update and the delete compare with: none.
|
||||
assertFalse(r.getAttributes().containsKey(EmrCfnProvisioner.GROUP_POLICY_ATTR));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anInstanceFleetIsResizedInPlaceAndARollbackPutsItsCapacityBack() {
|
||||
when(handler.instanceFleetFrom(any())).thenReturn(new EmrInstanceFleet());
|
||||
when(emr.addInstanceFleet(eq(CLUSTER_ID), any())).thenReturn("if-TASK");
|
||||
StackResource r = typed("AWS::EMR::InstanceFleetConfig", null);
|
||||
provisioner.provision(r, json(fleetProps(1)), ctx(null, null));
|
||||
when(emr.describeCluster(CLUSTER_ID)).thenReturn(emrCluster(CLUSTER_ID, "WAITING"));
|
||||
EmrInstanceFleet live = new EmrInstanceFleet();
|
||||
live.setId("if-TASK");
|
||||
live.setTargetOnDemandCapacity(1);
|
||||
when(emr.listInstanceFleets(CLUSTER_ID)).thenReturn(List.of(live));
|
||||
|
||||
provisioner.provision(r, json(fleetProps(4)), ctx("if-TASK", fleetProps(1)));
|
||||
provisioner.rollbackUpdate(r);
|
||||
|
||||
verify(emr).modifyInstanceFleet(CLUSTER_ID, "if-TASK", 4, 0);
|
||||
verify(emr).modifyInstanceFleet(CLUSTER_ID, "if-TASK", 1, 0);
|
||||
assertEquals("if-TASK", r.getAttributes().get("InstanceFleetId"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anInstanceGroupOrFleetDeleteShrinksItAndOneWhoseClusterEndedIsLeftAlone() {
|
||||
when(emr.describeCluster(CLUSTER_ID)).thenReturn(emrCluster(CLUSTER_ID, "WAITING"));
|
||||
EmrInstanceGroup withPolicy = group("ig-TASK", "TASK", "spot");
|
||||
withPolicy.setAutoScalingPolicy(POLICY);
|
||||
when(emr.listInstanceGroups(CLUSTER_ID)).thenReturn(List.of(withPolicy));
|
||||
StackResource group = typed("AWS::EMR::InstanceGroupConfig", "ig-TASK");
|
||||
group.getAttributes().put(EmrCfnProvisioner.CLUSTER_ID_ATTR, CLUSTER_ID);
|
||||
StackResource fleet = typed("AWS::EMR::InstanceFleetConfig", "if-TASK");
|
||||
fleet.getAttributes().put(EmrCfnProvisioner.CLUSTER_ID_ATTR, CLUSTER_ID);
|
||||
StackResource orphan = typed("AWS::EMR::InstanceGroupConfig", "ig-GONE");
|
||||
orphan.getAttributes().put(EmrCfnProvisioner.CLUSTER_ID_ATTR, "j-ENDED");
|
||||
when(emr.describeCluster("j-ENDED")).thenReturn(emrCluster("j-ENDED", "TERMINATED"));
|
||||
|
||||
provisioner.delete(group, "us-east-1");
|
||||
provisioner.delete(fleet, "us-east-1");
|
||||
provisioner.delete(orphan, "us-east-1");
|
||||
|
||||
verify(emr).removeAutoScalingPolicy(CLUSTER_ID, "ig-TASK");
|
||||
verify(emr).modifyInstanceGroups(CLUSTER_ID, "us-east-1",
|
||||
List.of(new InstanceGroupModification("ig-TASK", 0, null)));
|
||||
verify(emr).modifyInstanceFleet(CLUSTER_ID, "if-TASK", 0, 0);
|
||||
verify(emr, never()).modifyInstanceGroups(eq("j-ENDED"), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anInstanceGroupOrFleetOfAnotherRoleThanTaskIsRefused() {
|
||||
AwsException group = assertThrows(AwsException.class, () -> provisioner.provision(
|
||||
typed("AWS::EMR::InstanceGroupConfig", null),
|
||||
json(groupProps(1, null).replace("\"TASK\"", "\"CORE\"")), ctx(null, null)));
|
||||
AwsException fleet = assertThrows(AwsException.class, () -> provisioner.provision(
|
||||
typed("AWS::EMR::InstanceFleetConfig", null),
|
||||
json(fleetProps(1).replace("\"TASK\"", "\"MASTER\"")), ctx(null, null)));
|
||||
|
||||
assertEquals("InstanceRole must be TASK, the one value the template reference allows, not CORE.",
|
||||
group.getMessage());
|
||||
assertEquals("InstanceFleetType must be TASK, the one value the template reference allows, not MASTER.",
|
||||
fleet.getMessage());
|
||||
verifyNoInteractions(emr);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aFleetWhoseClusterIsStillStartingIsShrunkOnceItRuns() {
|
||||
when(emr.describeCluster(CLUSTER_ID)).thenReturn(emrCluster(CLUSTER_ID, "STARTING"),
|
||||
emrCluster(CLUSTER_ID, "BOOTSTRAPPING"), emrCluster(CLUSTER_ID, "WAITING"));
|
||||
StackResource fleet = typed("AWS::EMR::InstanceFleetConfig", "if-TASK");
|
||||
fleet.getAttributes().put(EmrCfnProvisioner.CLUSTER_ID_ATTR, CLUSTER_ID);
|
||||
|
||||
provisioner.delete(fleet, "us-east-1");
|
||||
|
||||
verify(emr).modifyInstanceFleet(CLUSTER_ID, "if-TASK", 0, 0);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user