这是indexloc提供的服务,不要输入任何密码
Skip to content
This repository was archived by the owner on Mar 4, 2021. It is now read-only.
Merged
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
4 changes: 4 additions & 0 deletions src/main/java/com/netflix/simianarmy/Monkey.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public Monkey(Context ctx) {

public abstract void doMonkeyBusiness();

public Context context() {
return ctx;
}

public void start() {
final Monkey me = this;
ctx.scheduler().start(type().name(), new Runnable() {
Expand Down
37 changes: 28 additions & 9 deletions src/main/java/com/netflix/simianarmy/MonkeyRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ public void replaceMonkey(Class<? extends Monkey> monkeyClass, Class<? extends M
return;
}
}
monkeys.add(factory(monkeyClass, ctxClass));
Monkey monkey = factory(monkeyClass, ctxClass);
monkeys.add(monkey);
}

public void removeMonkey(Class<? extends Monkey> monkeyClass) {
Expand All @@ -101,12 +102,22 @@ public void removeMonkey(Class<? extends Monkey> monkeyClass) {
break;
}
}

monkeyMap.remove(monkeyClass);
}

public <T extends Monkey> T factory(Class<T> monkeyClass) {
return factory(monkeyClass, getContextClass(monkeyClass));
Class<? extends Monkey.Context> ctxClass = getContextClass(monkeyClass);
if (ctxClass == null) {
// look for derived class already in our map
for (Map.Entry<Class<? extends Monkey>, Class<? extends Monkey.Context>> pair : monkeyMap.entrySet()) {
if (monkeyClass.isAssignableFrom(pair.getKey())) {
@SuppressWarnings("unchecked")
T monkey = (T) factory(pair.getKey(), pair.getValue());
return monkey;
}
}
}
return factory(monkeyClass, ctxClass);
}

public <T extends Monkey> T factory(Class<T> monkeyClass, Class<? extends Monkey.Context> contextClass) {
Expand All @@ -115,14 +126,22 @@ public <T extends Monkey> T factory(Class<T> monkeyClass, Class<? extends Monkey
// assume Monkey class has has void ctor
return monkeyClass.newInstance();
}
// assume Monkey class has ctor that take a Context inner interface as argument
// so first find the monkey Context class:
Class ctorArgClass = Class.forName(monkeyClass.getName() + "$Context");

// then find corresponding ctor
Constructor<T> ctor = monkeyClass.getDeclaredConstructor(ctorArgClass);
return ctor.newInstance(contextClass.newInstance());
for (Constructor<?> ctor : monkeyClass.getDeclaredConstructors()) {
Class<?>[] paramTypes = ctor.getParameterTypes();
if (paramTypes.length != 1) {
continue;
}
if (paramTypes[0].getName().endsWith("$Context")) {
@SuppressWarnings("unchecked")
T monkey = (T) ctor.newInstance(contextClass.newInstance());
return monkey;
}
}
} catch (Exception e) {
LOGGER.error("monkeyFactory error: ", e);
LOGGER.error("monkeyFactory error, cannot make monkey from " + monkeyClass.getName() + " with "
+ (contextClass == null ? null : contextClass.getName()), e);
}

return null;
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/netflix/simianarmy/basic/BasicContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
import com.netflix.simianarmy.aws.AWSClient;
import com.netflix.simianarmy.aws.SimpleDBRecorder;

import com.netflix.simianarmy.basic.chaos.BasicChaosCrawler;
import com.netflix.simianarmy.basic.chaos.BasicChaosInstanceSelector;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -72,7 +75,7 @@ public BasicContext() {
client = new AWSClient(account, secret, region);
scheduler = new BasicScheduler((int) config.getNumOrElse("monkey.threads", MONKEY_THREADS));
crawler = new BasicChaosCrawler(client);
selector = new ChaosInstanceSelector();
selector = new BasicChaosInstanceSelector();
String domain = config.getStrOrElse("domain", "SIMIAN_ARMY");
recorder = new SimpleDBRecorder(account, secret, region, domain);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.slf4j.LoggerFactory;

import com.netflix.simianarmy.MonkeyRunner;
import com.netflix.simianarmy.chaos.ChaosMonkey;
import com.netflix.simianarmy.basic.chaos.BasicChaosMonkey;

@SuppressWarnings("serial")
public class BasicMonkeyServer extends HttpServlet {
Expand All @@ -34,14 +34,14 @@ public class BasicMonkeyServer extends HttpServlet {
@Override
public void init() throws ServletException {
super.init();
RUNNER.replaceMonkey(ChaosMonkey.class, BasicContext.class);
RUNNER.replaceMonkey(BasicChaosMonkey.class, BasicContext.class);
RUNNER.start();
}

@Override
public void destroy() {
RUNNER.stop();
RUNNER.removeMonkey(ChaosMonkey.class);
RUNNER.removeMonkey(BasicChaosMonkey.class);
super.destroy();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*
*/
package com.netflix.simianarmy.basic;
package com.netflix.simianarmy.basic.chaos;

import java.util.List;
import java.util.LinkedList;
Expand Down Expand Up @@ -43,7 +43,7 @@ public BasicChaosCrawler(AWSClient awsClient) {
public static class BasicInstanceGroup implements InstanceGroup {
private final String name;
private final Enum type;

public BasicInstanceGroup(String name) {
this.name = name;
this.type = Types.ASG;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
*
* 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.chaos;

import java.util.Random;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

public class BasicChaosInstanceSelector implements ChaosInstanceSelector {
private static final Logger LOGGER = LoggerFactory.getLogger(ChaosInstanceSelector.class);
private static final Random RANDOM = new Random();

protected Logger logger() {
return LOGGER;
}

public String select(InstanceGroup group, double probability) {
if (probability <= 0) {
logger().info("Group {} [type {}] has disabled probability: {}",
new Object[] {group.name(), group.type(), probability});
return null;
}
double rand = Math.random();
if (rand > probability) {
logger().info("Group {} [type {}] got lucky: {} > {}",
new Object[] {group.name(), group.type(), rand, probability});
return null;
}
return group.instances().get(RANDOM.nextInt(group.instances().size()));
}
}
180 changes: 180 additions & 0 deletions src/main/java/com/netflix/simianarmy/basic/chaos/BasicChaosMonkey.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
/*
*
* 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.chaos;

import com.netflix.simianarmy.chaos.ChaosMonkey;
import com.netflix.simianarmy.MonkeyConfiguration;
import com.netflix.simianarmy.MonkeyRunner;
import com.netflix.simianarmy.MonkeyRecorder.Event;
import com.netflix.simianarmy.chaos.ChaosCrawler.InstanceGroup;

import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

import org.codehaus.jackson.JsonEncoding;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.MappingJsonFactory;

public class BasicChaosMonkey extends ChaosMonkey {
private static final Logger LOGGER = LoggerFactory.getLogger(ChaosMonkey.class);
private static final String NS = "simianarmy.chaos.";

private MonkeyConfiguration cfg;
private long runsPerDay;

public BasicChaosMonkey(ChaosMonkey.Context ctx) {
super(ctx);

this.cfg = ctx.configuration();

Calendar open = ctx.calendar().now();
Calendar close = ctx.calendar().now();
open.set(Calendar.HOUR, ctx.calendar().openHour());
close.set(Calendar.HOUR, ctx.calendar().closeHour());

TimeUnit freqUnit = ctx.scheduler().frequencyUnit();
long units = freqUnit.convert(close.getTimeInMillis() - open.getTimeInMillis(), TimeUnit.MILLISECONDS);
runsPerDay = units / ctx.scheduler().frequency();
}

public void doMonkeyBusiness() {
cfg.reload();
String prop = NS + "enabled";
if (!cfg.getBoolOrElse(prop, true)) {
LOGGER.info("ChaosMonkey disabled, set {}=true", prop);
return;
}

for (InstanceGroup group : context().chaosCrawler().groups()) {
prop = NS + group.type() + "." + group.name() + ".enabled";
String defaultProp = NS + group.type();
if (cfg.getBoolOrElse(prop, cfg.getBool(defaultProp + ".enabled"))) {
String probProp = NS + group.type() + "." + group.name() + ".probability";
double prob = cfg.getNumOrElse(probProp, cfg.getNumOrElse(defaultProp + ".probability", 1.0));
String inst = context().chaosInstanceSelector().select(group, prob / runsPerDay);
if (inst != null) {
prop = NS + "leashed";
if (cfg.getBoolOrElse(prop, true)) {
LOGGER.info("leashed ChaosMonkey prevented from killing {}, set {}=false", inst, prop);
} else {
if (hasPreviousTerminations(group)) {
LOGGER.info("ChaosMonkey takes pity on group {} [{}] since it was attacked ealier today",
group.name(), group.type());
continue;
}
try {
recordTermination(group, inst);
context().cloudClient().terminateInstance(inst);
} catch (Exception e) {
handleTerminationError(inst, e);
}
}
}
} else {
LOGGER.info("Group {} [type {}] disabled, set {}=true or {}=true",
new Object[] {group.name(), group.type(), prop, defaultProp + ".enabled"});
}
}
}

// abstracted so subclasses can decide to continue causing chaos if desired
protected void handleTerminationError(String instance, Throwable e) {
LOGGER.error("failed to terminate instance " + instance, e.getMessage());
throw new RuntimeException("failed to terminate instance " + instance, e);
}

public boolean hasPreviousTerminations(InstanceGroup group) {
Map<String, String> query = new HashMap<String, String>();
query.put("groupType", group.type().name());
query.put("groupName", group.name());
Calendar today = Calendar.getInstance();
// set to midnight
today.set(Calendar.HOUR_OF_DAY, 0);
today.set(Calendar.MINUTE, 0);
today.set(Calendar.SECOND, 0);
today.set(Calendar.MILLISECOND, 0);
List<Event> evts = context().recorder().findEvents(Type.CHAOS, EventTypes.CHAOS_TERMINATION, query,
today.getTime());
return !evts.isEmpty();
}

public void recordTermination(InstanceGroup group, String instance) {
Event evt = context().recorder().newEvent(Type.CHAOS, EventTypes.CHAOS_TERMINATION, instance);
evt.addField("groupType", group.type().name());
evt.addField("groupName", group.name());
context().recorder().recordEvent(evt);
}

@Path("/chaos")
@SuppressWarnings("serial")
public static class Servlet {
private static final MappingJsonFactory JSON_FACTORY = new MappingJsonFactory();

private ChaosMonkey monkey = MonkeyRunner.getInstance().factory(ChaosMonkey.class);

@GET
public Response getChaosEvents(@javax.ws.rs.core.Context UriInfo uriInfo) throws IOException {
Map<String, String> query = new HashMap<String, String>();
Date date = new Date(0);
for (Map.Entry<String, List<String>> pair : uriInfo.getQueryParameters().entrySet()) {
if (pair.getValue().isEmpty()) {
continue;
}
if (pair.getKey().equals("since")) {
date = new Date(Long.parseLong(pair.getValue().get(0)));
} else {
query.put(pair.getKey(), pair.getValue().get(0));
}
}

List<Event> evts = monkey.context().recorder()
.findEvents(Type.CHAOS, EventTypes.CHAOS_TERMINATION, query, date);

ByteArrayOutputStream baos = new ByteArrayOutputStream();
JsonGenerator gen = JSON_FACTORY.createJsonGenerator(baos, JsonEncoding.UTF8);
gen.writeStartArray();
for (Event evt : evts) {
gen.writeStartObject();
gen.writeStringField("monkeyType", evt.monkeyType().name());
gen.writeStringField("eventType", evt.eventType().name());
gen.writeNumberField("eventTime", evt.eventTime().getTime());
for (Map.Entry<String, String> pair : evt.fields().entrySet()) {
gen.writeStringField(pair.getKey(), pair.getValue());
}
gen.writeEndObject();
}
gen.writeEndArray();
gen.close();
return Response.status(Response.Status.OK).entity(baos).build();
}
}
}
Loading