use of com.netflix.simianarmy.MonkeyRecorder.Event in project SimianArmy by Netflix.
the class ChaosMonkeyResource method addTerminationEvent.
private Response.Status addTerminationEvent(String groupType, String groupName, ChaosType chaosType, JsonGenerator gen) throws IOException {
LOGGER.info("Running on-demand termination for instance group type '{}' and name '{}'", groupType, groupName);
Response.Status responseStatus;
try {
Event evt = monkey.terminateNow(groupType, groupName, chaosType);
if (evt != null) {
responseStatus = Response.Status.OK;
gen.writeStringField("monkeyType", evt.monkeyType().name());
gen.writeStringField("eventId", evt.id());
gen.writeNumberField("eventTime", evt.eventTime().getTime());
gen.writeStringField("region", evt.region());
for (Map.Entry<String, String> pair : evt.fields().entrySet()) {
gen.writeStringField(pair.getKey(), pair.getValue());
}
} else {
responseStatus = Response.Status.INTERNAL_SERVER_ERROR;
gen.writeStringField("message", String.format("Failed to terminate instance in group %s [type %s]", groupName, groupType));
}
} catch (FeatureNotEnabledException e) {
responseStatus = Response.Status.FORBIDDEN;
gen.writeStringField("message", e.getMessage());
} catch (InstanceGroupNotFoundException e) {
responseStatus = Response.Status.NOT_FOUND;
gen.writeStringField("message", e.getMessage());
} catch (NotFoundException e) {
// Available instance cannot be found to terminate, maybe the instance is already gone
responseStatus = Response.Status.GONE;
gen.writeStringField("message", e.getMessage());
}
LOGGER.info("On-demand termination completed.");
return responseStatus;
}
use of com.netflix.simianarmy.MonkeyRecorder.Event in project SimianArmy by Netflix.
the class TestMonkeyContext method getEventReport.
@Override
public String getEventReport() {
StringBuilder report = new StringBuilder();
for (Event event : eventReport) {
report.append(event.eventType());
report.append(" ");
report.append(event.id());
}
return report.toString();
}
use of com.netflix.simianarmy.MonkeyRecorder.Event in project SimianArmy by Netflix.
the class AbstractJanitor method markResources.
/**
* Marks all resources obtained from the crawler as cleanup candidate if
* the janitor rule engine thinks so.
*/
@Override
public void markResources() {
markedResources.clear();
unmarkedResources.clear();
checkedResourcesCount = 0;
Map<String, Resource> trackedMarkedResources = getTrackedMarkedResources();
List<Resource> crawledResources = crawler.resources(resourceType);
LOGGER.info(String.format("Looking for cleanup candidate in %d crawled resources.", crawledResources.size()));
Date now = calendar.now().getTime();
for (Resource resource : crawledResources) {
checkedResourcesCount++;
Resource trackedResource = trackedMarkedResources.get(resource.getId());
if (!ruleEngine.isValid(resource)) {
// If the resource is already marked, ignore it
if (trackedResource != null) {
LOGGER.debug(String.format("Resource %s is already marked.", resource.getId()));
continue;
}
LOGGER.info(String.format("Marking resource %s of type %s with expected termination time as %s", resource.getId(), resource.getResourceType(), resource.getExpectedTerminationTime()));
resource.setState(CleanupState.MARKED);
resource.setMarkTime(now);
if (!leashed) {
resourceTracker.addOrUpdate(resource);
if (recorder != null) {
Event evt = recorder.newEvent(Type.JANITOR, EventTypes.MARK_RESOURCE, resource, resource.getId());
recorder.recordEvent(evt);
}
postMark(resource);
} else {
LOGGER.info(String.format("The janitor is leashed, no data change is made for marking the resource %s.", resource.getId()));
}
markedResources.add(resource);
} else if (trackedResource != null) {
// The resource was marked and now the rule engine does not consider it as a cleanup candidate.
// So the janitor needs to unmark the resource.
LOGGER.info(String.format("Unmarking resource %s", resource.getId()));
resource.setState(CleanupState.UNMARKED);
if (!leashed) {
resourceTracker.addOrUpdate(resource);
if (recorder != null) {
Event evt = recorder.newEvent(Type.JANITOR, EventTypes.UNMARK_RESOURCE, resource, resource.getId());
recorder.recordEvent(evt);
}
} else {
LOGGER.info(String.format("The janitor is leashed, no data change is made for unmarking the resource %s.", resource.getId()));
}
unmarkedResources.add(resource);
}
}
// Unmark the resources that are terminated by user so not returned by the crawler.
unmarkUserTerminatedResources(crawledResources, trackedMarkedResources);
}
use of com.netflix.simianarmy.MonkeyRecorder.Event in project SimianArmy by Netflix.
the class ChaosMonkeyResource method getChaosEvents.
/**
* Gets the chaos events. Creates GET /api/v1/chaos api which outputs the chaos events in json. Users can specify
* cgi query params to filter the results and use "since" query param to set the start of a timerange. "since" should
* be specified in milliseconds since the epoch.
*
* @param uriInfo
* the uri info
* @return the chaos events json response
* @throws IOException
* Signals that an I/O exception has occurred.
*/
@GET
public Response getChaosEvents(@Context UriInfo uriInfo) throws IOException {
Map<String, String> query = new HashMap<String, String>();
Date date = null;
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));
}
}
// if "since" not set, default to 24 hours ago
if (date == null) {
Calendar now = monkey.context().calendar().now();
now.add(Calendar.DAY_OF_YEAR, -1);
date = now.getTime();
}
List<Event> evts = monkey.context().recorder().findEvents(ChaosMonkey.Type.CHAOS, ChaosMonkey.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("eventId", evt.id());
gen.writeStringField("eventType", evt.eventType().name());
gen.writeNumberField("eventTime", evt.eventTime().getTime());
gen.writeStringField("region", evt.region());
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.toString("UTF-8")).build();
}
Aggregations