Skip to content

Commit

Permalink
Enabling running outside of ASG when using vnodes
Browse files Browse the repository at this point in the history
  • Loading branch information
zmarois committed Mar 22, 2018
1 parent 010c5d1 commit 640c7c0
Show file tree
Hide file tree
Showing 21 changed files with 294 additions and 222 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,11 @@
*/
public abstract class AbstractConfigSource implements IConfigSource {

private String asgName;
private String region;

@Override
public void intialize(final String asgName, final String region) {
this.asgName = checkNotNull(asgName, "ASG name is not defined");
public void intialize(final String appid, final String region) {
checkNotNull(appid, "app name is not defined");
this.region = checkNotNull(region, "Region is not defined");
}

Expand Down Expand Up @@ -158,10 +157,6 @@ public List<String> getList(String prop, List<String> defaultValue) {
return defaultValue;
}

protected String getAsgName() {
return asgName;
}

protected String getRegion() {
return region;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ public CompositeConfigSource(final IConfigSource... sources) {
}

@Override
public void intialize(final String asgName, final String region) {
public void intialize(final String appid, final String region) {
for (final IConfigSource source : sources) {
//TODO should this catch any potential exceptions?
source.intialize(asgName, region);
source.intialize(appid, region);
}
}

Expand Down
4 changes: 2 additions & 2 deletions priam/src/main/java/com/netflix/priam/IConfigSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ public interface IConfigSource {
/**
* Must be called before any other method. This method will allow implementations to do any setup that they require
* before being called.
* @param asgName: Name of the asg
* @param appid: Name of the cluster
* @param region: Name of the region
*/
void intialize(String asgName, String region);
void intialize(String appid, String region);

/**
* A non-negative integer indicating a count of elements.
Expand Down
3 changes: 0 additions & 3 deletions priam/src/main/java/com/netflix/priam/IConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -582,9 +582,6 @@ public interface IConfiguration {

public boolean getAutoBoostrap();

//if using with Datastax Enterprise
public String getDseClusterType();

public boolean isCreateNewTokenEnable();

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public PropertiesConfigSource(final Properties properties) {
}

@Override
public void intialize(final String asgName, final String region) {
super.intialize(asgName, region);
public void intialize(final String appid, final String region) {
super.intialize(appid, region);
Properties properties = new Properties();
URL url = PropertiesConfigSource.class.getClassLoader().getResource(priamFile);
if (url != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,13 @@ public SimpleDBConfigSource(final ICredential provider) {
}

@Override
public void intialize(final String asgName, final String region) {
super.intialize(asgName, region);
public void intialize(final String appid, final String region) {
super.intialize(appid, region);

// End point is us-east-1
AmazonSimpleDB simpleDBClient = AmazonSimpleDBClient.builder().withCredentials(provider.getAwsCredentialProvider()).build();

String nextToken = null;
String appid = asgName.lastIndexOf('-') > 0 ? asgName.substring(0, asgName.indexOf('-')) : asgName;
logger.info("appid used to fetch properties is: {}", appid);
do {
SelectRequest request = new SelectRequest(String.format(ALL_QUERY, appid));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public final class SystemPropertiesConfigSource extends AbstractConfigSource {
private final Map<String, String> data = Maps.newConcurrentMap();

@Override
public void intialize(final String asgName, final String region) {
super.intialize(asgName, region);
public void intialize(final String appid, final String region) {
super.intialize(appid, region);

Properties systemProps = System.getProperties();

Expand Down
113 changes: 113 additions & 0 deletions priam/src/main/java/com/netflix/priam/aws/ASGMembership.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright 2013 Netflix, Inc.
*
* 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 com.netflix.priam.aws;

import com.amazonaws.services.autoscaling.AmazonAutoScaling;
import com.amazonaws.services.autoscaling.AmazonAutoScalingClient;
import com.amazonaws.services.autoscaling.model.*;
import com.amazonaws.services.autoscaling.model.Instance;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.AmazonEC2Client;
import com.amazonaws.services.ec2.model.*;
import com.amazonaws.services.ec2.model.Filter;
import com.google.common.collect.Lists;
import com.google.inject.Inject;
import com.google.inject.name.Named;
import com.netflix.priam.IConfiguration;
import com.netflix.priam.ICredential;
import com.netflix.priam.identity.IMembership;
import com.netflix.priam.identity.InstanceEnvIdentity;
import com.netflix.priam.identity.PriamInstance;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
* Class to query amazon ASG for its members to provide - Number of valid nodes
* in the ASG - Number of zones - Methods for adding ACLs for the nodes
*/
public class ASGMembership extends AbstractAWSMembership {
private static final Logger logger = LoggerFactory.getLogger(ASGMembership.class);
private final ICredential thisAccountProvider;

@Inject
public ASGMembership(IConfiguration config, ICredential provider, @Named("awsec2roleassumption") ICredential crossAccountProvider, InstanceEnvIdentity insEnvIdentity) {
super(config, provider, crossAccountProvider, insEnvIdentity);
this.thisAccountProvider = provider;
}

@Override
protected List<String> getLiveInstances(ICredential provider) {
AmazonAutoScaling client = null;
try {
List<String> asgNames = new ArrayList<>();
asgNames.add(config.getASGName());
asgNames.addAll(Arrays.asList(config.getSiblingASGNames().split("\\s*,\\s*")));
client = getAutoScalingClient(provider);
DescribeAutoScalingGroupsRequest asgReq = new DescribeAutoScalingGroupsRequest().withAutoScalingGroupNames(asgNames.toArray(new String[asgNames.size()]));
DescribeAutoScalingGroupsResult res = client.describeAutoScalingGroups(asgReq);

List<String> instanceIds = Lists.newArrayList();
for (AutoScalingGroup asg : res.getAutoScalingGroups()) {
for (Instance ins : asg.getInstances())
if (isInstanceStateLive(ins.getLifecycleState()))
instanceIds.add(ins.getInstanceId());
}
if (logger.isInfoEnabled()) {
logger.info(String.format("Querying Amazon returned following instance in the RAC: %s, ASGs: %s --> %s", config.getRac(), StringUtils.join(asgNames, ","), StringUtils.join(instanceIds, ",")));
}
return instanceIds;
} finally {
if (client != null)
client.shutdown();
}
}

/**
* Actual membership AWS source of truth...
*/
@Override
public int getRacMembershipSize() {
AmazonAutoScaling client = null;
try {
client = getAutoScalingClient(thisAccountProvider);
DescribeAutoScalingGroupsRequest asgReq = new DescribeAutoScalingGroupsRequest().withAutoScalingGroupNames(config.getASGName());
DescribeAutoScalingGroupsResult res = client.describeAutoScalingGroups(asgReq);
int size = 0;
for (AutoScalingGroup asg : res.getAutoScalingGroups()) {
size += asg.getMaxSize();
}
logger.info("Query on ASG returning {} instances", size);
return size;
} finally {
if (client != null)
client.shutdown();
}
}

private AmazonAutoScaling getAutoScalingClient(ICredential provider)
{
AmazonAutoScaling client = new AmazonAutoScalingClient(provider.getAwsCredentialProvider());
client.setEndpoint("autoscaling." + config.getDC() + ".amazonaws.com");
return client;
}
}
Loading

0 comments on commit 640c7c0

Please sign in to comment.