这是indexloc提供的服务,不要输入任何密码
Skip to content
This repository was archived by the owner on Mar 4, 2021. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ apply plugin: 'jetty'
sourceCompatibility = 1.7
targetCompatibility = 1.7

configurations.all {
resolutionStrategy.eachDependency() {
DependencyResolveDetails details ->
if (details.requested.group == 'com.google.inject.extensions') {
details.useVersion '4.0'
}
}
}

dependencies {
// for the VMWareClient
compile 'com.cloudbees.thirdparty:vijava:5.0.0'
Expand All @@ -43,10 +52,10 @@ dependencies {
compile 'com.google.guava:guava:11.0.2'
compile 'org.apache.httpcomponents:httpclient:4.3'
compile 'com.google.auto.service:auto-service:1.0-rc2'
compile 'org.apache.jclouds.driver:jclouds-jsch:1.9.0'
compile 'org.apache.jclouds.driver:jclouds-slf4j:1.9.0'
compile 'org.apache.jclouds.api:ec2:1.9.0'
compile 'org.apache.jclouds.provider:aws-ec2:1.9.0'
compile 'org.apache.jclouds.driver:jclouds-jsch:2.0.0'
compile 'org.apache.jclouds.driver:jclouds-slf4j:2.0.0'
compile 'org.apache.jclouds.api:ec2:2.0.0'
compile 'org.apache.jclouds.provider:aws-ec2:2.0.0'
compile 'com.netflix.servo:servo-core:0.12.11'
compile 'org.springframework:spring-jdbc:4.2.5.RELEASE'
compile 'com.zaxxer:HikariCP:2.4.7'
Expand Down
59 changes: 59 additions & 0 deletions src/main/java/com/netflix/simianarmy/Instance.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
*
* Copyright 2012 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.simianarmy;

import com.netflix.simianarmy.Tag;
import java.util.List;


/**
* The interface that holds the EC2 instance metadata
*/

public interface Instance {

/**
* Gets instance id.
*
* @return instance id as string
*/
String getInstanceId();

/**
* Gets name.
*
* @return name as string
*/
String getName();

/**
* Gets host name.
*
* @return host name as string
*/
String getHostname();

/**
* Gets the user created tags for the instance.
*
* @return list of tags
*/
List<Tag> getTags();


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
*
* Copyright 2012 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.simianarmy;
import com.netflix.simianarmy.Tag;

import java.util.List;

/**
* The Class NoInstanceWithTagsFoundException.
*
* These exceptions will be thrown when an instance with all tags are NOT found
*/
public class NoInstanceWithTagsFoundException extends Exception {
private static final long serialVersionUID = 01072016L;
private List<Tag> ec2Tags;

/**
* Instantiates an NoInstanceWithTagsFoundException with the tags.
* @param ec2Tags
*/
public NoInstanceWithTagsFoundException(List<Tag> ec2Tags) {
super(errorMessage(ec2Tags));
this.ec2Tags = ec2Tags;
}

@Override
public String toString() {
return errorMessage(ec2Tags);
}

private static String errorMessage(List<Tag> ec2Tags) {
StringBuilder error = new StringBuilder(1000);
error.append(" No Instances with the following tags were found: ");
for (Tag ec2Tag : ec2Tags) {
error.append(ec2Tag.getKey() + ":" + ec2Tag.getValue() + ", ");
}
return error.toString();
}
}
78 changes: 78 additions & 0 deletions src/main/java/com/netflix/simianarmy/Tag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
*
* Copyright 2012 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.simianarmy;

import java.util.Objects;

/**
* The Class that holds the EC2 tags in name value pair.
*/
public class Tag {

private String key;
private String value;

public Tag() {}

public Tag(String key, String value) {
this.key = key;
this.value = value;
}

@Override
public String toString() {
return "Tag{" +
"key='" + key + '\'' +
", value='" + value + '\'' +
'}';
}

@Override
public int hashCode() {
return (key+value).hashCode();
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Tag tag = (Tag) obj;
return Objects.equals(key, tag.getKey())
&& Objects.equals(value, tag.getValue());
}

public String getKey() {
return key;
}

public String getValue() {
return value;
}

public void setName(String key) {
this.key = key;
}

public void setValue(String value) {
this.value = value;
}
}
62 changes: 62 additions & 0 deletions src/main/java/com/netflix/simianarmy/basic/BasicInstance.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
*
* Copyright 2012 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.simianarmy.basic;

import com.netflix.simianarmy.Instance;
import com.netflix.simianarmy.Tag;

import java.util.List;

public class BasicInstance implements Instance {

private String instanceId;
private String name;
private String hostName;
private List<Tag> tags;

public BasicInstance(String instanceId) {
this.instanceId = instanceId;
}

public BasicInstance(String instanceId, String name, String hostName, List<Tag> tags) {
this.instanceId = instanceId;
this.name = name;
this.hostName = hostName;
this.tags = tags;
}

@Override
public String getInstanceId() {
return instanceId;
}

@Override
public String getName() {
return name;
}

@Override
public String getHostname() {
return hostName;
}

@Override
public List<Tag> getTags() {
return tags;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,18 @@
*/
package com.netflix.simianarmy.basic.chaos;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.*;

import com.google.common.collect.Lists;
import com.netflix.simianarmy.Instance;
import org.apache.commons.lang.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.netflix.simianarmy.chaos.ChaosCrawler.InstanceGroup;
import com.netflix.simianarmy.chaos.ChaosInstanceSelector;

import com.netflix.simianarmy.Tag;
import com.netflix.simianarmy.NoInstanceWithTagsFoundException;
/**
* The Class BasicChaosInstanceSelector.
*/
Expand All @@ -54,13 +53,35 @@ protected Logger logger() {
public Collection<String> select(InstanceGroup group, double probability) {
int n = ((int) probability);
String selected = selectOneInstance(group, probability - n);
Collection<String> result = selectNInstances(group.instances(), n, selected);
Collection<String> result = selectNInstances(group.instanceIds(), n, selected);
if (selected != null) {
result.add(selected);
}
return result;
}

/** {@inheritDoc} */
@Override
public Collection<String> selectOneByTags(InstanceGroup group, List<Tag> ec2TagsSent) throws NoInstanceWithTagsFoundException {
List<String> matchInstances = new ArrayList<String>();
Set<Tag> ec2TagsSentSet = new HashSet<Tag>(ec2TagsSent);

for (Instance inst : group.instances()) {
Set<Tag> ec2TagsInstSet = new HashSet<Tag>(inst.getTags());
if (ec2TagsInstSet.containsAll(ec2TagsSentSet))
matchInstances.add(inst.getInstanceId());
}
Collection<String> result = null;
if (matchInstances.size() > 0){
result = selectNInstances(matchInstances, 1, null);
}
if (result == null) {
logger().info("No instances with those sent Tags were found");
throw new NoInstanceWithTagsFoundException(ec2TagsSent);
}
return result;
}

private Collection<String> selectNInstances(Collection<String> instances, int n, String selected) {
logger().info("Randomly selecting {} from {} instances, excluding {}",
new Object[] {n, instances.size(), selected});
Expand Down Expand Up @@ -90,6 +111,6 @@ private String selectOneInstance(InstanceGroup group, double probability) {
new Object[] {group.name(), group.type(), rand, probability});
return null;
}
return group.instances().get(RANDOM.nextInt(group.instances().size()));
return group.instanceIds().get(RANDOM.nextInt(group.instanceIds().size()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ private ChaosType pickChaosType(CloudClient cloudClient, String instanceId) {
}

@Override
public Event terminateNow(String type, String name, ChaosType chaosType)
throws FeatureNotEnabledException, InstanceGroupNotFoundException {
public Event terminateNow(String type, String name, ChaosType chaosType, List<Tag> ec2TagsSent)
throws FeatureNotEnabledException, InstanceGroupNotFoundException, NoInstanceWithTagsFoundException {
Validate.notNull(type);
Validate.notNull(name);
cfg.reload(name);
Expand All @@ -169,7 +169,12 @@ public Event terminateNow(String type, String name, ChaosType chaosType)
if (group == null) {
throw new InstanceGroupNotFoundException(type, name);
}
Collection<String> instances = context().chaosInstanceSelector().select(group, 1.0);
Collection<String> instances = null;
if (ec2TagsSent == null)
instances = context().chaosInstanceSelector().select(group, 1.0);
else {
instances = context().chaosInstanceSelector().selectOneByTags(group, ec2TagsSent);
}
Validate.isTrue(instances.size() <= 1);
if (instances.size() == 1) {
return terminateInstance(group, instances.iterator().next(), chaosType);
Expand Down
Loading