EC2AutoScaler: Support for setting subnetId.

This commit is contained in:
Gian Merlino 2015-03-10 11:28:12 -07:00
parent c7ec8ba15b
commit 9235b45063
5 changed files with 134 additions and 4 deletions

View File

@ -117,6 +117,7 @@ public class EC2AutoScaler implements AutoScaler<EC2EnvironmentConfig>
.withSecurityGroupIds(workerConfig.getSecurityGroupIds()) .withSecurityGroupIds(workerConfig.getSecurityGroupIds())
.withPlacement(new Placement(envConfig.getAvailabilityZone())) .withPlacement(new Placement(envConfig.getAvailabilityZone()))
.withKeyName(workerConfig.getKeyName()) .withKeyName(workerConfig.getKeyName())
.withSubnetId(workerConfig.getSubnetId())
.withUserData(userDataBase64) .withUserData(userDataBase64)
); );

View File

@ -32,6 +32,7 @@ public class EC2NodeData
private final int maxInstances; private final int maxInstances;
private final List<String> securityGroupIds; private final List<String> securityGroupIds;
private final String keyName; private final String keyName;
private final String subnetId;
@JsonCreator @JsonCreator
public EC2NodeData( public EC2NodeData(
@ -40,7 +41,8 @@ public class EC2NodeData
@JsonProperty("minInstances") int minInstances, @JsonProperty("minInstances") int minInstances,
@JsonProperty("maxInstances") int maxInstances, @JsonProperty("maxInstances") int maxInstances,
@JsonProperty("securityGroupIds") List<String> securityGroupIds, @JsonProperty("securityGroupIds") List<String> securityGroupIds,
@JsonProperty("keyName") String keyName @JsonProperty("keyName") String keyName,
@JsonProperty("subnetId") String subnetId
) )
{ {
this.amiId = amiId; this.amiId = amiId;
@ -49,6 +51,7 @@ public class EC2NodeData
this.maxInstances = maxInstances; this.maxInstances = maxInstances;
this.securityGroupIds = securityGroupIds; this.securityGroupIds = securityGroupIds;
this.keyName = keyName; this.keyName = keyName;
this.subnetId = subnetId;
} }
@JsonProperty @JsonProperty
@ -87,6 +90,12 @@ public class EC2NodeData
return keyName; return keyName;
} }
@JsonProperty
public String getSubnetId()
{
return subnetId;
}
@Override @Override
public String toString() public String toString()
{ {
@ -97,6 +106,7 @@ public class EC2NodeData
", maxInstances=" + maxInstances + ", maxInstances=" + maxInstances +
", securityGroupIds=" + securityGroupIds + ", securityGroupIds=" + securityGroupIds +
", keyName='" + keyName + '\'' + ", keyName='" + keyName + '\'' +
", subnetId='" + subnetId + '\'' +
'}'; '}';
} }
@ -130,6 +140,9 @@ public class EC2NodeData
if (securityGroupIds != null ? !securityGroupIds.equals(that.securityGroupIds) : that.securityGroupIds != null) { if (securityGroupIds != null ? !securityGroupIds.equals(that.securityGroupIds) : that.securityGroupIds != null) {
return false; return false;
} }
if (subnetId != null ? !subnetId.equals(that.subnetId) : that.subnetId != null) {
return false;
}
return true; return true;
} }
@ -143,6 +156,7 @@ public class EC2NodeData
result = 31 * result + maxInstances; result = 31 * result + maxInstances;
result = 31 * result + (securityGroupIds != null ? securityGroupIds.hashCode() : 0); result = 31 * result + (securityGroupIds != null ? securityGroupIds.hashCode() : 0);
result = 31 * result + (keyName != null ? keyName.hashCode() : 0); result = 31 * result + (keyName != null ? keyName.hashCode() : 0);
result = 31 * result + (subnetId != null ? subnetId.hashCode() : 0);
return result; return result;
} }
} }

View File

@ -0,0 +1,111 @@
/*
* Druid - a distributed column store.
* Copyright (C) 2014 Metamarkets Group Inc.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package io.druid.indexing.overlord.autoscaling;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.InjectableValues;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Charsets;
import com.google.common.collect.Lists;
import com.google.common.io.BaseEncoding;
import io.druid.indexing.overlord.autoscaling.ec2.EC2AutoScaler;
import io.druid.jackson.DefaultObjectMapper;
import org.junit.Assert;
import org.junit.Test;
public class EC2AutoScalerSerdeTest
{
final String json = "{\n"
+ " \"envConfig\" : {\n"
+ " \"availabilityZone\" : \"westeros-east-1a\",\n"
+ " \"nodeData\" : {\n"
+ " \"amiId\" : \"ami-abc\",\n"
+ " \"instanceType\" : \"t1.micro\",\n"
+ " \"keyName\" : \"iron\",\n"
+ " \"maxInstances\" : 1,\n"
+ " \"minInstances\" : 1,\n"
+ " \"securityGroupIds\" : [\"kingsguard\"],\n"
+ " \"subnetId\" : \"redkeep\"\n"
+ " },\n"
+ " \"userData\" : {\n"
+ " \"data\" : \"VERSION=:VERSION:\\n\","
+ " \"impl\" : \"string\",\n"
+ " \"versionReplacementString\" : \":VERSION:\"\n"
+ " }\n"
+ " },\n"
+ " \"maxNumWorkers\" : 3,\n"
+ " \"minNumWorkers\" : 2,\n"
+ " \"type\" : \"ec2\"\n"
+ "}";
@Test
public void testSerde() throws Exception
{
final ObjectMapper objectMapper = new DefaultObjectMapper();
objectMapper.setInjectableValues(
new InjectableValues()
{
@Override
public Object findInjectableValue(
Object o, DeserializationContext deserializationContext, BeanProperty beanProperty, Object o1
)
{
return null;
}
}
);
final EC2AutoScaler autoScaler = objectMapper.readValue(json, EC2AutoScaler.class);
Assert.assertEquals(3, autoScaler.getMaxNumWorkers());
Assert.assertEquals(2, autoScaler.getMinNumWorkers());
Assert.assertEquals("westeros-east-1a", autoScaler.getEnvConfig().getAvailabilityZone());
// nodeData
Assert.assertEquals("ami-abc", autoScaler.getEnvConfig().getNodeData().getAmiId());
Assert.assertEquals("t1.micro", autoScaler.getEnvConfig().getNodeData().getInstanceType());
Assert.assertEquals("iron", autoScaler.getEnvConfig().getNodeData().getKeyName());
Assert.assertEquals(1, autoScaler.getEnvConfig().getNodeData().getMaxInstances());
Assert.assertEquals(1, autoScaler.getEnvConfig().getNodeData().getMinInstances());
Assert.assertEquals(
Lists.newArrayList("kingsguard"),
autoScaler.getEnvConfig().getNodeData().getSecurityGroupIds()
);
Assert.assertEquals("redkeep", autoScaler.getEnvConfig().getNodeData().getSubnetId());
// userData
Assert.assertEquals(
"VERSION=1234\n",
new String(
BaseEncoding.base64()
.decode(autoScaler.getEnvConfig().getUserData().withVersion("1234").getUserDataBase64()),
Charsets.UTF_8
)
);
// Round trip.
Assert.assertEquals(
"Round trip",
autoScaler,
objectMapper.readValue(objectMapper.writeValueAsBytes(autoScaler), EC2AutoScaler.class)
);
}
}

View File

@ -25,7 +25,10 @@ import com.amazonaws.services.ec2.model.Reservation;
import com.amazonaws.services.ec2.model.RunInstancesRequest; import com.amazonaws.services.ec2.model.RunInstancesRequest;
import com.amazonaws.services.ec2.model.RunInstancesResult; import com.amazonaws.services.ec2.model.RunInstancesResult;
import com.amazonaws.services.ec2.model.TerminateInstancesRequest; import com.amazonaws.services.ec2.model.TerminateInstancesRequest;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Charsets;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.io.BaseEncoding;
import io.druid.indexing.overlord.autoscaling.ec2.EC2AutoScaler; import io.druid.indexing.overlord.autoscaling.ec2.EC2AutoScaler;
import io.druid.indexing.overlord.autoscaling.ec2.EC2EnvironmentConfig; import io.druid.indexing.overlord.autoscaling.ec2.EC2EnvironmentConfig;
import io.druid.indexing.overlord.autoscaling.ec2.EC2NodeData; import io.druid.indexing.overlord.autoscaling.ec2.EC2NodeData;
@ -89,7 +92,7 @@ public class EC2AutoScalerTest
1, 1,
new EC2EnvironmentConfig( new EC2EnvironmentConfig(
"us-east-1a", "us-east-1a",
new EC2NodeData(AMI_ID, INSTANCE_ID, 1, 1, Lists.<String>newArrayList(), "foo"), new EC2NodeData(AMI_ID, INSTANCE_ID, 1, 1, Lists.<String>newArrayList(), "foo", "mySubnet"),
new GalaxyEC2UserData(new DefaultObjectMapper(), "env", "version", "type") new GalaxyEC2UserData(new DefaultObjectMapper(), "env", "version", "type")
), ),
amazonEC2Client, amazonEC2Client,

View File

@ -54,7 +54,8 @@ public class WorkerBehaviorConfigTest
3, 3,
5, 5,
Arrays.asList("securityGroupIds"), Arrays.asList("securityGroupIds"),
"keyNames" "keyNames",
"subnetId"
), ),
new StringEC2UserData( new StringEC2UserData(
"availZone", "availZone",