Search in sources :

Example 1 with BasicRecorderEvent

use of com.netflix.simianarmy.basic.BasicRecorderEvent in project SimianArmy by Netflix.

the class SimpleDBRecorder method findEvents.

/**
 * Find events.
 *
 * @param queryMap
 *            the query map
 * @param after
 *            the start time to query for all events after
 * @return the list
 */
protected List<Event> findEvents(Map<String, String> queryMap, long after) {
    StringBuilder query = new StringBuilder(String.format("select * from `%s` where region = '%s'", domain, region));
    for (Map.Entry<String, String> pair : queryMap.entrySet()) {
        query.append(String.format(" and %s = '%s'", pair.getKey(), pair.getValue()));
    }
    query.append(String.format(" and eventTime > '%d'", after));
    // always return with most recent record first
    query.append(" order by eventTime desc");
    List<Event> list = new LinkedList<Event>();
    SelectRequest request = new SelectRequest(query.toString());
    request.setConsistentRead(Boolean.TRUE);
    SelectResult result = new SelectResult();
    do {
        result = sdbClient().select(request.withNextToken(result.getNextToken()));
        for (Item item : result.getItems()) {
            Map<String, String> fields = new HashMap<String, String>();
            Map<String, String> res = new HashMap<String, String>();
            for (Attribute attr : item.getAttributes()) {
                if (Keys.KEYSET.contains(attr.getName())) {
                    res.put(attr.getName(), attr.getValue());
                } else {
                    fields.put(attr.getName(), attr.getValue());
                }
            }
            String eid = res.get(Keys.id.name());
            String ereg = res.get(Keys.region.name());
            MonkeyType monkeyType = valueToEnum(MonkeyType.class, res.get(Keys.monkeyType.name()));
            EventType eventType = valueToEnum(EventType.class, res.get(Keys.eventType.name()));
            long eventTime = Long.parseLong(res.get(Keys.eventTime.name()));
            list.add(new BasicRecorderEvent(monkeyType, eventType, ereg, eid, eventTime).addFields(fields));
        }
    } while (result.getNextToken() != null);
    return list;
}
Also used : MonkeyType(com.netflix.simianarmy.MonkeyType) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Attribute(com.amazonaws.services.simpledb.model.Attribute) ReplaceableAttribute(com.amazonaws.services.simpledb.model.ReplaceableAttribute) EventType(com.netflix.simianarmy.EventType) BasicRecorderEvent(com.netflix.simianarmy.basic.BasicRecorderEvent) LinkedList(java.util.LinkedList) SelectResult(com.amazonaws.services.simpledb.model.SelectResult) Item(com.amazonaws.services.simpledb.model.Item) BasicRecorderEvent(com.netflix.simianarmy.basic.BasicRecorderEvent) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) SelectRequest(com.amazonaws.services.simpledb.model.SelectRequest)

Example 2 with BasicRecorderEvent

use of com.netflix.simianarmy.basic.BasicRecorderEvent in project SimianArmy by Netflix.

the class TestChaosMonkeyResource method mkEvent.

private MonkeyRecorder.Event mkEvent(String instance) {
    final MonkeyType monkeyType = ChaosMonkey.Type.CHAOS;
    final EventType eventType = ChaosMonkey.EventTypes.CHAOS_TERMINATION;
    // SUPPRESS CHECKSTYLE MagicNumber
    return new BasicRecorderEvent(monkeyType, eventType, "region", instance, 1330538400000L).addField("groupType", "ASG").addField("groupName", "testGroup");
}
Also used : MonkeyType(com.netflix.simianarmy.MonkeyType) EventType(com.netflix.simianarmy.EventType) BasicRecorderEvent(com.netflix.simianarmy.basic.BasicRecorderEvent)

Example 3 with BasicRecorderEvent

use of com.netflix.simianarmy.basic.BasicRecorderEvent in project SimianArmy by Netflix.

the class TestRDSRecorder method testFindEvent.

@SuppressWarnings("unchecked")
@Test
public void testFindEvent() {
    Event evt1 = new BasicRecorderEvent(Type.MONKEY, EventTypes.EVENT, "region", "testId1", 1330538400000L);
    mkSelectResult("testId1", evt1);
    Event evt2 = new BasicRecorderEvent(Type.MONKEY, EventTypes.EVENT, "region", "testId2", 1330538400000L);
    mkSelectResult("testId2", evt2);
    ArrayList<Event> events = new ArrayList<>();
    TestRDSRecorder recorder = new TestRDSRecorder();
    events.add(evt1);
    events.add(evt2);
    when(recorder.getJdbcTemplate().query(Matchers.anyString(), Matchers.argThat(new ArgumentMatcher<Object[]>() {

        @Override
        public boolean matches(Object argument) {
            Object[] args = (Object[]) argument;
            Assert.assertTrue(args[0] instanceof String);
            Assert.assertEquals((String) args[0], REGION);
            return true;
        }
    }), Matchers.any(RowMapper.class))).thenReturn(events);
    Map<String, String> query = new LinkedHashMap<String, String>();
    query.put("instanceId", "testId1");
    verifyEvents(recorder.findEvents(query, new Date(0)));
}
Also used : BasicRecorderEvent(com.netflix.simianarmy.basic.BasicRecorderEvent) ArrayList(java.util.ArrayList) Date(java.util.Date) LinkedHashMap(java.util.LinkedHashMap) ArgumentMatcher(org.mockito.ArgumentMatcher) BasicRecorderEvent(com.netflix.simianarmy.basic.BasicRecorderEvent) RowMapper(org.springframework.jdbc.core.RowMapper) Test(org.testng.annotations.Test)

Example 4 with BasicRecorderEvent

use of com.netflix.simianarmy.basic.BasicRecorderEvent in project SimianArmy by Netflix.

the class RDSRecorder method mapEvent.

private Event mapEvent(ResultSet rs) throws SQLException {
    String json = rs.getString("dataJson");
    ObjectMapper mapper = new ObjectMapper();
    Event event = null;
    try {
        String id = rs.getString(FIELD_ID);
        MonkeyType monkeyType = SimpleDBRecorder.valueToEnum(MonkeyType.class, rs.getString(FIELD_MONKEY_TYPE));
        EventType eventType = SimpleDBRecorder.valueToEnum(EventType.class, rs.getString(FIELD_EVENT_TYPE));
        String region = rs.getString(FIELD_REGION);
        long time = rs.getLong(FIELD_EVENT_TIME);
        event = new BasicRecorderEvent(monkeyType, eventType, region, id, time);
        TypeReference<Map<String, String>> typeRef = new TypeReference<Map<String, String>>() {
        };
        Map<String, String> map = mapper.readValue(json, typeRef);
        for (String key : map.keySet()) {
            event.addField(key, map.get(key));
        }
    } catch (IOException ie) {
        LOGGER.error("Error parsing resource from json", ie);
    }
    return event;
}
Also used : MonkeyType(com.netflix.simianarmy.MonkeyType) EventType(com.netflix.simianarmy.EventType) BasicRecorderEvent(com.netflix.simianarmy.basic.BasicRecorderEvent) BasicRecorderEvent(com.netflix.simianarmy.basic.BasicRecorderEvent) TypeReference(com.fasterxml.jackson.core.type.TypeReference) IOException(java.io.IOException) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

BasicRecorderEvent (com.netflix.simianarmy.basic.BasicRecorderEvent)4 EventType (com.netflix.simianarmy.EventType)3 MonkeyType (com.netflix.simianarmy.MonkeyType)3 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 Attribute (com.amazonaws.services.simpledb.model.Attribute)1 Item (com.amazonaws.services.simpledb.model.Item)1 ReplaceableAttribute (com.amazonaws.services.simpledb.model.ReplaceableAttribute)1 SelectRequest (com.amazonaws.services.simpledb.model.SelectRequest)1 SelectResult (com.amazonaws.services.simpledb.model.SelectResult)1 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 ArgumentMatcher (org.mockito.ArgumentMatcher)1 RowMapper (org.springframework.jdbc.core.RowMapper)1 Test (org.testng.annotations.Test)1